Skip to content

Commit

Permalink
allow outputCompactLogs to be set to -1 to allow for overriding compa…
Browse files Browse the repository at this point in the history
…ctLogs to allow for no compacting in output file
  • Loading branch information
bvandercar-vt committed Mar 31, 2022
1 parent b96af0f commit a4fcd73
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
14 changes: 10 additions & 4 deletions src/installLogsPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/installLogsPrinter.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"outputCompactLogs": {
"type": "number",
"minimum": 0
"minimum": -1
},
"outputRoot": {
"type": "string"
Expand Down
13 changes: 12 additions & 1 deletion test/cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', };
}
Expand Down
58 changes: 41 additions & 17 deletions test/specs/outputCompactLogs.spec.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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);
});

0 comments on commit a4fcd73

Please sign in to comment.