Skip to content

Commit

Permalink
fix: set the exit code for json output (#269)
Browse files Browse the repository at this point in the history
Purpose:
- Want the exit code set properly for json output.

Changes:
- Move the logic to set the exit code so that it applies to json output as well.
- If the warnings limit exceeded, add this as an error to the results so that it may be naturally included in the json output.

Tests:
- repair warnings limit tests and exit code tests
  • Loading branch information
barrett-schonefeld committed Apr 7, 2021
1 parent 3cb4021 commit b6e9899
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
46 changes: 26 additions & 20 deletions src/cli-validator/runValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,32 @@ const processInput = async function(program) {
results.hint = false;
}

// fail on errors or if number of warnings exceeds warnings limit
if (results.error) {
exitCode = 1;
} else {
// Calculate number of warnings and set exit code to 1 if warning limit exceeded
let numWarnings = 0;
for (const key of Object.keys(results.warnings)) {
numWarnings += results.warnings[key].length;
}
if (numWarnings > limitsObject.warnings) {
exitCode = 1;
// add the exceeded warnings limit as an error
if (!results.errors) {
results.errors = {};
}
results.errors['warnings-limit'] = [
{
path: [],
message: `Number of warnings (${numWarnings}) exceeds warnings limit (${
limitsObject.warnings
}).`
}
];
}
}

if (jsonOutput) {
printJson(results, originalFile, errorsOnly);
} else {
Expand All @@ -301,26 +327,6 @@ const processInput = async function(program) {
originalFile,
errorsOnly
);
// fail on errors or if number of warnings exceeds warnings limit
if (results.error) {
exitCode = 1;
} else {
// Calculate number of warnings and set exit code to 1 if warning limit exceeded
let numWarnings = 0;
for (const key of Object.keys(results.warnings)) {
numWarnings += results.warnings[key].length;
}
if (numWarnings > limitsObject.warnings) {
exitCode = 1;
console.log(
chalk.red(
`Number of warnings (${numWarnings}) exceeds warnings limit (${
limitsObject.warnings
}).`
)
);
}
}
} else {
console.log(chalk.green(`\n${validFile} passed the validator`));
if (validFile === last(filesToValidate)) console.log();
Expand Down
2 changes: 1 addition & 1 deletion src/cli-validator/utils/printResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module.exports = function print(
// print the path array as a dot-separated string

console.log(chalk[color](` Message : ${problem.message}`));
if (printRuleNames) {
if (printRuleNames && problem.rule) {
console.log(chalk[color](` Rule : ${problem.rule}`));
}
console.log(chalk[color](` Path : ${path.join('.')}`));
Expand Down
2 changes: 1 addition & 1 deletion test/cli-validator/tests/expected-output.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('cli tool - test expected output - Swagger 2', function() {
program.json = true;

const exitcode = await commandLineValidator(program);
expect(exitcode).toBe(0);
expect(exitcode).toBe(1);

const capturedText = getCapturedText(consoleSpy.mock.calls);
const jsonOutput = JSON.parse(capturedText);
Expand Down
4 changes: 1 addition & 3 deletions test/cli-validator/tests/threshold-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ describe('test the .thresholdrc limits', function() {

expect(exitCode).toEqual(1);

expect(capturedText[capturedText.length - 1].slice(0, 18)).toEqual(
`Number of warnings`
);
expect(capturedText[2].slice(14, 32)).toEqual(`Number of warnings`);
});

it('should print errors for unsupported limit options and invalid limit values', async function() {
Expand Down
4 changes: 2 additions & 2 deletions test/spectral/tests/spectral-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Spectral - test custom configuration', function() {
program.ruleset = 'test/spectral/mockFiles/mockConfig/info-and-hint.yaml';

const exitcode = await commandLineValidator(program);
expect(exitcode).toBe(0);
expect(exitcode).toBe(1);

const capturedText = getCapturedText(consoleSpy.mock.calls);
consoleSpy.mockRestore();
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('Spectral - test custom configuration', function() {
program.ruleset = 'test/spectral/mockFiles/mockConfig/extends-default.yaml';

const exitcode = await commandLineValidator(program);
expect(exitcode).toBe(0);
expect(exitcode).toBe(1);

const capturedText = getCapturedText(consoleSpy.mock.calls);
consoleSpy.mockRestore();
Expand Down

0 comments on commit b6e9899

Please sign in to comment.