Skip to content

Commit

Permalink
Merge pull request #139 from bvandercar-vt/output-file-compact-logs-c…
Browse files Browse the repository at this point in the history
…onfig

add outputCompactLogs option to allow for overriding compactLogs for output file specifically
  • Loading branch information
archfz authored Mar 31, 2022
2 parents 42439cb + a4fcd73 commit 5e656c8
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ 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`.
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).
Expand Down Expand Up @@ -307,6 +313,10 @@ directory. You should add `it.only` to the test case you are working on to speed
## Release Notes
#### 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)
#### 3.4.2
- Fix incorrectly typed message type arguments. [issue](https://github.com/archfz/cypress-terminal-report/issues/132)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-terminal-report",
"version": "3.4.2",
"version": "3.5.0",
"description": "Better terminal and file output for cypress test logs.",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions src/installLogsPrinter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 21 additions & 6 deletions src/installLogsPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -73,8 +74,19 @@ function installLogsPrinter(on, options = {}) {
options.printLogsToFile === "always" ||
isHookAndShouldLog
) {
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] = messages;
writeToFileMessages[data.spec][data.test] = outputFileMessages;
}
}

Expand All @@ -83,11 +95,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;
Expand Down
4 changes: 4 additions & 0 deletions src/installLogsPrinter.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"type": "number",
"minimum": 0
},
"outputCompactLogs": {
"type": "number",
"minimum": -1
},
"outputRoot": {
"type": "string"
},
Expand Down
12 changes: 12 additions & 0 deletions test/cypress/integration/outputCompactLogs.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference types="cypress" />

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)
})
})
17 changes: 17 additions & 0 deletions test/cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ module.exports = (on, config) => {
if (config.env.compactLogs == "1") {
options.compactLogs = 1;
}
if (config.env.outputCompactLogs == "1") {
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', };
}
if (config.env.pluginBadConfig == '1') {
options = {
outputRoot: 0,
Expand Down
62 changes: 62 additions & 0 deletions test/specs/outputCompactLogs.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 () {
if (this.currentTest.state == 'failed') {
logLastRun();
}
});

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 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 fileContents = getOutputFileContents();
expect(fileContents, 'outputCompactLogs=-1 causes terminal to not compact even if compactLogs specified' ).to.not.contain(`omitted logs`);
});
}).timeout(60000);
});

0 comments on commit 5e656c8

Please sign in to comment.