Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add outputCompactLogs option to allow for overriding compactLogs for output file specifically #139

Merged
merged 4 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});