From: Ruben Beltran del Rio Date: Mon, 21 Sep 2020 11:40:51 +0000 (+0200) Subject: Merge branch 'master' into rbdr-address-sonarqube-issues X-Git-Tag: v2.0.1^2~11 X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/commitdiff_plain/86405c754b7c58554745df79fc14c9820680cc55?hp=f89f9b0b99cf0042f601e387381c818207dfcc74 Merge branch 'master' into rbdr-address-sonarqube-issues --- diff --git a/.gitignore b/.gitignore index 3bbc2f1..c2a8529 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ ## PROJECT::SPECIFIC .nyc_output +coverage node_modules +.scannerwork diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cc95c38..9679aec 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,8 +1,12 @@ image: node:14 +variables: + SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache + stages: - lint - test + - quality before_script: - npm install @@ -16,3 +20,21 @@ test: stage: test script: - npm test + +quality: + stage: "quality" + needs: ["test"] + image: + name: sonarsource/sonar-scanner-cli:latest + entrypoint: [""] + cache: + key: "${CI_JOB_NAME}" + paths: + - .sonar/cache + script: + - npm run coverage + - sonar-scanner + only: + - merge_requests + - master + diff --git a/lib/utilities.js b/lib/utilities.js index 1c930af..8bb226b 100644 --- a/lib/utilities.js +++ b/lib/utilities.js @@ -2,6 +2,37 @@ const internals = { kCircularString: '[Circular]', + kAnsiCodes: { + bold: '[1m', + italics: '[3m', + underline: '[4m', + inverse: '[7m', + strikethrough: '[9m', + bold_off: '[22m', + italics_off: '[23m', + underline_off: '[24m', + inverse_off: '[27m', + strikethrough_off: '[29m', + black: '[30m', + red: '[31m', + green: '[32m', + yellow: '[33m', + blue: '[34m', + magenta: '[35m', + cyan: '[36m', + white: '[37m', + default: '[39m', + black_bg: '[40m', + red_bg: '[41m', + green_bg: '[42m', + yellow_bg: '[43m', + blue_bg: '[44m', + magenta_bg: '[45m', + cyan_bg: '[46m', + white_bg: '[47m', + default_bg: '[49m', + reset: '[0m' + }, // Get High Res time depending on platform. Currently only supporting node. @@ -75,67 +106,7 @@ module.exports = { */ getAnsiCode(ansiString) { - switch (ansiString) { - case 'bold': - return '[1m'; - case 'italics': - return '[3m'; - case 'underline': - return '[4m'; - case 'inverse': - return '[7m'; - case 'strikethrough': - return '[9m'; - case 'bold_off': - return '[22m'; - case 'italics_off': - return '[23m'; - case 'underline_off': - return '[24m'; - case 'inverse_off': - return '[27m'; - case 'strikethrough_off': - return '[29m'; - case 'black': - return '[30m'; - case 'red': - return '[31m'; - case 'green': - return '[32m'; - case 'yellow': - return '[33m'; - case 'blue': - return '[34m'; - case 'magenta': - return '[35m'; - case 'cyan': - return '[36m'; - case 'white': - return '[37m'; - case 'default': - return '[39m'; - case 'black_bg': - return '[40m'; - case 'red_bg': - return '[41m'; - case 'green_bg': - return '[42m'; - case 'yellow_bg': - return '[43m'; - case 'blue_bg': - return '[44m'; - case 'magenta_bg': - return '[45m'; - case 'cyan_bg': - return '[46m'; - case 'white_bg': - return '[47m'; - case 'default_bg': - return '[49m'; - case 'reset': // for informative purpouses - default: - return '[0m'; - } + return internals.kAnsiCodes[ansiString] || internals.kAnsiCodes.reset; }, /** diff --git a/package.json b/package.json index c6d4bdc..d48e098 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "scripts": { "document": "jsdoc2md lib/**/*.js > doc/README.md; tap -R markdown > doc/COVERAGE.md", "lint": "eslint .", - "test": "tap" + "test": "tap", + "coverage": "nyc report --reporter=text-lcov" }, "main": "./lib/cologne.js" } diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..08b778a --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,12 @@ +sonar.projectKey=rbdr_cologne +sonar.organization=rbdr + +# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. +sonar.sources=. + +# Encoding of the source code. Default is default system encoding +sonar.sourceEncoding=UTF-8 + +# Coverage path +sonar.javascript.lcov.reportPaths=coverage/lcov.info +sonar.coverage.exclusions='test/**/*' diff --git a/test/cologne.js b/test/cologne.js index 3f06763..1fce740 100644 --- a/test/cologne.js +++ b/test/cologne.js @@ -23,7 +23,7 @@ const createDummyLogger = function () { const levelChecker = function (level) { - return (count, log) => (log._level === level ? ++count : count); + return (count, log) => (log._level === level ? count + 1 : count); }; @@ -42,74 +42,74 @@ const co = new Cologne({ ] }); -const logs = ['example1', null, undefined, 1, { example: true }]; +const exampleLogs = ['example1', null, undefined, 1, { example: true }]; /** * TEST: #log() */ -co.log(...logs); +co.log(...exampleLogs); // Calculate values -const valueCheck = dummyLoggerA.values.reduce((count, log) => (typeof log._cologneLog === 'string' ? ++count : count), 0); +const valueCheck = dummyLoggerA.values.reduce((count, log) => (typeof log._cologneLog === 'string' ? count + 1 : count), 0); let levelCheck = dummyLoggerA.values.reduce(levelChecker(6), 0); // Now check the values -Tap.equal(dummyLoggerA.values.length, logs.length, +Tap.equal(dummyLoggerA.values.length, exampleLogs.length, '#log() should send every argument to the loggers'); Tap.similar(dummyLoggerA.values, dummyLoggerB.values, '#log() should send the same arguments to all the loggers'); -Tap.equal(valueCheck, logs.length, +Tap.equal(valueCheck, exampleLogs.length, '#log() should send all objects in cologne log format'); -Tap.equal(levelCheck, logs.length, +Tap.equal(levelCheck, exampleLogs.length, '#log() should default to level 6'); /** * TEST: #debug() */ -co.debug(...logs); +co.debug(...exampleLogs); levelCheck = dummyLoggerA.values.reduce(levelChecker(7), 0); -Tap.equal(levelCheck, logs.length, +Tap.equal(levelCheck, exampleLogs.length, '#debug() should set to level 7'); /** * TEST: #info() */ -co.info(...logs); +co.info(...exampleLogs); levelCheck = dummyLoggerA.values.reduce(levelChecker(6), 0); -Tap.equal(levelCheck, logs.length, +Tap.equal(levelCheck, exampleLogs.length, '#info() should set to level 6'); /** * TEST: #notice() */ -co.notice(...logs); +co.notice(...exampleLogs); levelCheck = dummyLoggerA.values.reduce(levelChecker(5), 0); -Tap.equal(levelCheck, logs.length, +Tap.equal(levelCheck, exampleLogs.length, '#notice() should set to level 5'); /** * TEST: #warn() */ -co.warn(...logs); +co.warn(...exampleLogs); levelCheck = dummyLoggerA.values.reduce(levelChecker(4), 0); -Tap.equal(levelCheck, logs.length, +Tap.equal(levelCheck, exampleLogs.length, '#warn() should set to level 4'); /** * TEST: #error() */ -co.error(...logs); +co.error(...exampleLogs); levelCheck = dummyLoggerA.values.reduce(levelChecker(3), 0); -Tap.equal(levelCheck, logs.length, +Tap.equal(levelCheck, exampleLogs.length, '#error() should set to level 3'); /** @@ -156,7 +156,7 @@ co.removeLogger(dummyLoggerC); Tap.equal(co.loggers.length, 2, '#removeLogger() should do nothing if it can\'t find a logger'); -co.log(...logs); +co.log(...exampleLogs); co.removeLogger(dummyLoggerB); co.log(1); @@ -171,7 +171,7 @@ Tap.notEqual(dummyLoggerB.values.length, dummyLoggerA.values.length, */ co.addLogger(dummyLoggerC); -co.log(...logs); +co.log(...exampleLogs); -Tap.equal(dummyLoggerC.values.length, logs.length, +Tap.equal(dummyLoggerC.values.length, exampleLogs.length, '#addLogger() should add loggers after instance is live'); diff --git a/test/loggers/console.js b/test/loggers/console.js index 3358837..87dbc23 100644 --- a/test/loggers/console.js +++ b/test/loggers/console.js @@ -62,7 +62,7 @@ const logObjectB = { message: 'MessageTwo' }; -const logs = [logObjectA, logObjectB]; +const exampleLogs = [logObjectA, logObjectB]; const regularLogger = new ConsoleLogger({}); const overrideLogger = new ConsoleLogger({ @@ -86,38 +86,38 @@ Tap.equal(regularLogger.console, global.console, logObjectA._level = 5; logObjectB._level = 6; -overrideLogger.log(...logs); // should go to info +overrideLogger.log(...exampleLogs); // should go to info logObjectA._level = 4; logObjectB._level = 4; -overrideLogger.log(...logs); // should go to warn +overrideLogger.log(...exampleLogs); // should go to warn logObjectA._level = 1; logObjectB._level = 3; -overrideLogger.log(...logs); // should go to error +overrideLogger.log(...exampleLogs); // should go to error logObjectA._level = 0; logObjectB._level = 2; -overrideLogger.log(...logs); // should go to error +overrideLogger.log(...exampleLogs); // should go to error logObjectA._level = 7; logObjectB._level = 8; -overrideLogger.log(...logs); // should go to log +overrideLogger.log(...exampleLogs); // should go to log -Tap.equal(dummyConsole.values.log, logs.length, +Tap.equal(dummyConsole.values.log, exampleLogs.length, 'It should send debug messages to console\'s #log'); -Tap.equal(dummyConsole.values.info, logs.length, +Tap.equal(dummyConsole.values.info, exampleLogs.length, 'It should send info and notice messages to console\'s #info'); -Tap.equal(dummyConsole.values.warn, logs.length, +Tap.equal(dummyConsole.values.warn, exampleLogs.length, 'It should send warn messages to console\'s #warn'); -Tap.equal(dummyConsole.values.error, logs.length * 2, +Tap.equal(dummyConsole.values.error, exampleLogs.length * 2, 'It should send error messages to console\'s #error'); /** * TEST: #log() - with formatter */ -formattedLogger.log(...logs); // should go to log +formattedLogger.log(...exampleLogs); // should go to log -Tap.similar(dummyFormatter.values, logs, +Tap.similar(dummyFormatter.values, exampleLogs, 'If available, it should send the objects to the formatter');