From 1ac7eff3327351a66da098182e54711effbc859b Mon Sep 17 00:00:00 2001 From: "blake.vandercar" Date: Wed, 30 Mar 2022 09:56:33 -0600 Subject: [PATCH 1/4] add outputCompactLogs argument to allow for overriding compactLogs for output file specifically --- README.md | 9 +++++++++ package-lock.json | 2 +- package.json | 2 +- src/installLogsPrinter.d.ts | 6 ++++++ src/installLogsPrinter.js | 21 +++++++++++++++------ src/installLogsPrinter.schema.json | 4 ++++ 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index dae62c2..17fe909 100755 --- a/README.md +++ b/README.md @@ -86,6 +86,11 @@ will be printed only around failing commands. Use this to have shorter output es for when there are a lot of commands in tests. When used with `options.printLogsToConsole=always` for tests that don't have any `severity=error` logs nothing will be printed. +#### `options.outputCompactLogs` +integer?; default: null; Overrides `options.compactLogs` for the file log output specifically, when `options.outputTarget` is specified. +Allows compacting of the terminal and the file output logs to different levels. +If `options.outputCompactLogs` is unspecified, file output will use `options.compactLogs`. + #### `options.outputRoot` string; default: null; Required if `options.outputTarget` provided. [More details](#logging-to-files). @@ -307,6 +312,10 @@ directory. You should add `it.only` to the test case you are working on to speed ## Release Notes +#### 3.4.3 + +- Add new feature outputCompactLogs to allow for optionally overriding compactLogs for the output file specifically [see here](#optionsoutputcompactlogs). [issue](https://github.com/archfz/cypress-terminal-report/issues/138) + #### 3.4.2 - Fix incorrectly typed message type arguments. [issue](https://github.com/archfz/cypress-terminal-report/issues/132) diff --git a/package-lock.json b/package-lock.json index e9c715c..37fb50b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "cypress-terminal-report", - "version": "3.4.2", + "version": "3.4.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index ff3ae8a..2a81728 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cypress-terminal-report", - "version": "3.4.2", + "version": "3.4.3", "description": "Better terminal and file output for cypress test logs.", "main": "index.js", "scripts": { diff --git a/src/installLogsPrinter.d.ts b/src/installLogsPrinter.d.ts index 4fe77cb..46b9f97 100644 --- a/src/installLogsPrinter.d.ts +++ b/src/installLogsPrinter.d.ts @@ -40,6 +40,12 @@ export interface PluginOptions { */ compactLogs?: number | null; + /** + * If it is set to a number greater or equal to 0, will override compactLogs for the file log output specifically. Use this for compacting of the terminal and the file output logs to different levels. + * @default null + */ + outputCompactLogs?: number | null; + /** * Required if outputTarget provided. [More details](https://github.com/archfz/cypress-terminal-report#logging-to-files). * @default null diff --git a/src/installLogsPrinter.js b/src/installLogsPrinter.js index cd11191..ce5b3a6 100755 --- a/src/installLogsPrinter.js +++ b/src/installLogsPrinter.js @@ -60,9 +60,10 @@ function installLogsPrinter(on, options = {}) { [CONSTANTS.TASK_NAME]: function (data) { let messages = data.messages; - if (typeof options.compactLogs === 'number' && options.compactLogs >= 0) { - messages = compactLogs(messages, options.compactLogs); - } + const terminalMessages = + typeof options.compactLogs === 'number' && options.compactLogs >= 0 + ? compactLogs(messages, options.compactLogs) + : messages; const isHookAndShouldLog = data.isHook && (options.includeSuccessfulHookLogs || data.state === 'failed'); @@ -73,8 +74,13 @@ function installLogsPrinter(on, options = {}) { options.printLogsToFile === "always" || isHookAndShouldLog ) { + const outputFileMessages = + typeof options.outputCompactLogs === 'number' && options.outputCompactLogs >= 0 + ? compactLogs(messages, options.outputCompactLogs) + : terminalMessages; + writeToFileMessages[data.spec] = writeToFileMessages[data.spec] || {}; - writeToFileMessages[data.spec][data.test] = messages; + writeToFileMessages[data.spec][data.test] = outputFileMessages; } } @@ -83,11 +89,14 @@ function installLogsPrinter(on, options = {}) { || options.printLogsToConsole === "always" || isHookAndShouldLog ) { - logToTerminal(messages, options, data); + logToTerminal(terminalMessages, options, data); } if (options.collectTestLogs) { - options.collectTestLogs({ spec: data.spec, test: data.test, state: data.state }, messages); + options.collectTestLogs( + {spec: data.spec, test: data.test, state: data.state}, + terminalMessages + ); } return null; diff --git a/src/installLogsPrinter.schema.json b/src/installLogsPrinter.schema.json index d8b62d5..bddac48 100644 --- a/src/installLogsPrinter.schema.json +++ b/src/installLogsPrinter.schema.json @@ -33,6 +33,10 @@ "type": "number", "minimum": 0 }, + "outputCompactLogs": { + "type": "number", + "minimum": 0 + }, "outputRoot": { "type": "string" }, From 4709413faa04ed5106349117254b82311e489e2f Mon Sep 17 00:00:00 2001 From: "blake.vandercar" Date: Wed, 30 Mar 2022 11:43:42 -0600 Subject: [PATCH 2/4] fix version --- README.md | 2 +- package-lock.json | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 17fe909..81c0a36 100755 --- a/README.md +++ b/README.md @@ -312,7 +312,7 @@ directory. You should add `it.only` to the test case you are working on to speed ## Release Notes -#### 3.4.3 +#### 3.5.0 - Add new feature outputCompactLogs to allow for optionally overriding compactLogs for the output file specifically [see here](#optionsoutputcompactlogs). [issue](https://github.com/archfz/cypress-terminal-report/issues/138) diff --git a/package-lock.json b/package-lock.json index 37fb50b..10c0bcd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "cypress-terminal-report", - "version": "3.4.3", + "version": "3.5.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2a81728..df0fd04 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cypress-terminal-report", - "version": "3.4.3", + "version": "3.5.0", "description": "Better terminal and file output for cypress test logs.", "main": "index.js", "scripts": { From b96af0fe87cfaeed2648f9116a0f55544bec5eb5 Mon Sep 17 00:00:00 2001 From: "blake.vandercar" Date: Wed, 30 Mar 2022 12:31:53 -0600 Subject: [PATCH 3/4] test outputConsoleLogs --- .../integration/outputCompactLogs.spec.js | 12 ++++++ test/cypress/plugins/index.js | 6 +++ test/specs/outputCompactLogs.spec.js | 38 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 test/cypress/integration/outputCompactLogs.spec.js create mode 100644 test/specs/outputCompactLogs.spec.js diff --git a/test/cypress/integration/outputCompactLogs.spec.js b/test/cypress/integration/outputCompactLogs.spec.js new file mode 100644 index 0000000..fa9375a --- /dev/null +++ b/test/cypress/integration/outputCompactLogs.spec.js @@ -0,0 +1,12 @@ +/// + +describe('Output compact logs', ()=>{ + // create logs to check that using outputCompactLogs overrides compactLogs for output file + it('Output compact logs', ()=>{ + for (let i = 0; i <20 ; i++) { + expect(1).to.equal(1) + } + + expect(1).to.equal(0) + }) +}) \ No newline at end of file diff --git a/test/cypress/plugins/index.js b/test/cypress/plugins/index.js index acaddee..6064148 100755 --- a/test/cypress/plugins/index.js +++ b/test/cypress/plugins/index.js @@ -52,6 +52,12 @@ module.exports = (on, config) => { if (config.env.compactLogs == "1") { options.compactLogs = 1; } + if (config.env.outputCompactLogs == "1") { + options.outputCompactLogs = 5; + options.compactLogs = 1; + options.outputRoot = config.projectRoot + '/output/'; + options.outputTarget = { 'out.txt': 'txt', }; + } if (config.env.pluginBadConfig == '1') { options = { outputRoot: 0, diff --git a/test/specs/outputCompactLogs.spec.js b/test/specs/outputCompactLogs.spec.js new file mode 100644 index 0000000..da96449 --- /dev/null +++ b/test/specs/outputCompactLogs.spec.js @@ -0,0 +1,38 @@ +import { + ICONS, + runTest, + commandBase, + logLastRun, + clean +} from "../utils"; + +const {expect} = require('chai'); +const fs = require('fs'); +const path = require('path'); + +describe('Output compact logs.', () => { + + afterEach(function () { + if (this.currentTest.state == 'failed') { + logLastRun(); + } + }); + + it('Should compact output file logs to length specified by outputCompactLogs', async () => { + await runTest(commandBase(['outputCompactLogs=1'], ['outputCompactLogs.spec.js']), (error, stdout, stderr) => { + // failure occurs on 21st log, compactLogs=1, outputCompactLogs=5 + + // test console is correct + expect(stdout).to.contain(`[ ... 19 omitted logs ... ]`); + expect(stdout).to.not.contain(`[ ... 15 omitted logs ... ]`); + + // test output log is correct + const outputFile = path.join(__dirname, '../output/out.txt'); + expect(fs.existsSync(outputFile), `Expected output file ${outputFile} to exist.`).to.be.true; + const valueBuffer = fs.readFileSync(outputFile); + let value = clean(valueBuffer.toString()); + expect(value).to.contain(`[ ... 15 omitted logs ... ]`); + expect(value).to.not.contain(`[ ... 19 omitted logs ... ]`); + }); + }).timeout(60000); +}); From a4fcd736367114949a6fde48c0941374858f4a2e Mon Sep 17 00:00:00 2001 From: "blake.vandercar" Date: Thu, 31 Mar 2022 09:24:50 -0600 Subject: [PATCH 4/4] allow outputCompactLogs to be set to -1 to allow for overriding compactLogs to allow for no compacting in output file --- README.md | 1 + src/installLogsPrinter.js | 14 +++++-- src/installLogsPrinter.schema.json | 2 +- test/cypress/plugins/index.js | 13 ++++++- test/specs/outputCompactLogs.spec.js | 58 ++++++++++++++++++++-------- 5 files changed, 65 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 81c0a36..6cb41f9 100755 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ for tests that don't have any `severity=error` logs nothing will be printed. integer?; default: null; Overrides `options.compactLogs` for the file log output specifically, when `options.outputTarget` is specified. Allows compacting of the terminal and the file output logs to different levels. If `options.outputCompactLogs` is unspecified, file output will use `options.compactLogs`. +If set to -1, output file logs will not compact even if `options.compactLogs` is set. #### `options.outputRoot` string; default: null; Required if `options.outputTarget` provided. [More details](#logging-to-files). diff --git a/src/installLogsPrinter.js b/src/installLogsPrinter.js index ce5b3a6..271cbb9 100755 --- a/src/installLogsPrinter.js +++ b/src/installLogsPrinter.js @@ -74,10 +74,16 @@ function installLogsPrinter(on, options = {}) { options.printLogsToFile === "always" || isHookAndShouldLog ) { - const outputFileMessages = - typeof options.outputCompactLogs === 'number' && options.outputCompactLogs >= 0 - ? compactLogs(messages, options.outputCompactLogs) - : terminalMessages; + let outputFileMessages + if (typeof options.outputCompactLogs === 'number' ) { + if (options.outputCompactLogs >= 0){ + outputFileMessages = compactLogs(messages, options.outputCompactLogs) + } else { + outputFileMessages = messages // if -1, override compactLogs, don't compact + } + } else{ + outputFileMessages = terminalMessages // if unspecified, go with compactLogs + } writeToFileMessages[data.spec] = writeToFileMessages[data.spec] || {}; writeToFileMessages[data.spec][data.test] = outputFileMessages; diff --git a/src/installLogsPrinter.schema.json b/src/installLogsPrinter.schema.json index bddac48..52ddc29 100644 --- a/src/installLogsPrinter.schema.json +++ b/src/installLogsPrinter.schema.json @@ -35,7 +35,7 @@ }, "outputCompactLogs": { "type": "number", - "minimum": 0 + "minimum": -1 }, "outputRoot": { "type": "string" diff --git a/test/cypress/plugins/index.js b/test/cypress/plugins/index.js index 6064148..7e93ecb 100755 --- a/test/cypress/plugins/index.js +++ b/test/cypress/plugins/index.js @@ -53,8 +53,19 @@ module.exports = (on, config) => { options.compactLogs = 1; } if (config.env.outputCompactLogs == "1") { - options.outputCompactLogs = 5; options.compactLogs = 1; + options.outputCompactLogs = 5; + options.outputRoot = config.projectRoot + '/output/'; + options.outputTarget = { 'out.txt': 'txt', }; + } + if (config.env.outputCompactLogs == "2") { + options.outputCompactLogs = 5; + options.outputRoot = config.projectRoot + '/output/'; + options.outputTarget = { 'out.txt': 'txt', }; + } + if (config.env.outputCompactLogs == "3") { + options.compactLogs = 5; + options.outputCompactLogs = -1; options.outputRoot = config.projectRoot + '/output/'; options.outputTarget = { 'out.txt': 'txt', }; } diff --git a/test/specs/outputCompactLogs.spec.js b/test/specs/outputCompactLogs.spec.js index da96449..97d238f 100644 --- a/test/specs/outputCompactLogs.spec.js +++ b/test/specs/outputCompactLogs.spec.js @@ -1,15 +1,16 @@ -import { - ICONS, - runTest, - commandBase, - logLastRun, - clean -} from "../utils"; +import { runTest, commandBase, logLastRun, clean } from "../utils"; const {expect} = require('chai'); const fs = require('fs'); const path = require('path'); +function getOutputFileContents() { + const outputFile = path.join(__dirname, '../output/out.txt'); + expect(fs.existsSync(outputFile), `Expected output file ${outputFile} to exist.`).to.be.true; + const valueBuffer = fs.readFileSync(outputFile); + return clean(valueBuffer.toString()); +} + describe('Output compact logs.', () => { afterEach(function () { @@ -18,21 +19,44 @@ describe('Output compact logs.', () => { } }); - it('Should compact output file logs to length specified by outputCompactLogs', async () => { + it('Should compact logs to length specified by compactLogs and outputCompactLogs - compactLogs=1, outputCompactLogs=5', async () => { await runTest(commandBase(['outputCompactLogs=1'], ['outputCompactLogs.spec.js']), (error, stdout, stderr) => { // failure occurs on 21st log, compactLogs=1, outputCompactLogs=5 - // test console is correct - expect(stdout).to.contain(`[ ... 19 omitted logs ... ]`); - expect(stdout).to.not.contain(`[ ... 15 omitted logs ... ]`); + // test terminal is correct + expect(stdout, 'compactLogs compacts terminal logs to correct val (1)' ).to.contain( `[ ... 19 omitted logs ... ]`); + expect(stdout, 'compactLogs does not compact terminal to outputCompactLogs val (5)' ).to.not.contain(`[ ... 15 omitted logs ... ]`); + + // test output log is correct + const fileContents = getOutputFileContents(); + expect(fileContents, 'outputCompactLogs compacts output file logs to correct val (5)').to.contain( `[ ... 15 omitted logs ... ]` ); + expect(fileContents, 'outputCompactLogs does not compact output file logs to compactLogs val (1)').to.not.contain(`[ ... 19 omitted logs ... ]`); + }); + }).timeout(60000); + + it('Should compact logs to length specified by compactLogs and outputCompactLogs - compactLogs=unspecified, outputCompactLogs=5', async () => { + await runTest(commandBase(['outputCompactLogs=2'], ['outputCompactLogs.spec.js']), (error, stdout, stderr) => { + // failure occurs on 21st log, compactLogs=unspecified (default), outputCompactLogs=5 + + // test terminal is correct + expect(stdout, 'not specifying compactLogs results in uncompacted terminal output even if outputCompactLogs>=0 specified' ).to.not.contain(`omitted logs`); + + // test output log is correct + const fileContents = getOutputFileContents(); + expect(fileContents, 'outputCompactLogs>=0 compacts terminal even if compactLogs not specified' ).to.contain(`[ ... 15 omitted logs ... ]`); + }); + }).timeout(60000); + + it('Should compact logs to length specified by compactLogs and outputCompactLogs - compactLogs=5, outputCompactLogs=-1', async () => { + await runTest(commandBase(['outputCompactLogs=3'], ['outputCompactLogs.spec.js']), (error, stdout, stderr) => { + // failure occurs on 21st log, compactLogs=5, outputCompactLogs=-1 + + // test terminal is correct + expect(stdout, 'compactLogs compacts terminal logs to correct val (5)').to.contain( `[ ... 15 omitted logs ... ]`); // test output log is correct - const outputFile = path.join(__dirname, '../output/out.txt'); - expect(fs.existsSync(outputFile), `Expected output file ${outputFile} to exist.`).to.be.true; - const valueBuffer = fs.readFileSync(outputFile); - let value = clean(valueBuffer.toString()); - expect(value).to.contain(`[ ... 15 omitted logs ... ]`); - expect(value).to.not.contain(`[ ... 19 omitted logs ... ]`); + const fileContents = getOutputFileContents(); + expect(fileContents, 'outputCompactLogs=-1 causes terminal to not compact even if compactLogs specified' ).to.not.contain(`omitted logs`); }); }).timeout(60000); });