Skip to content

Commit

Permalink
feat: new cli option, --errors_only, for printing only the errors (#68
Browse files Browse the repository at this point in the history
)
  • Loading branch information
SamerJaser96 authored and dpopp07 committed Jun 24, 2019
1 parent 3c95271 commit a338bf0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ To build these, run `npm run pkg` in the root. The binaries (lint-openapi-linux

#### [options]
- -s (--report_statistics) : Print a simple report at the end of the output showing the frequency, in percentage, of each error/warning.
- -e (--errors_only) : Only print the errors, ignore the warnings.
- -d (--default_mode) : This option turns off [configuration](#configuration) and runs the validator in [default mode](#default-mode).
- -p (--print_validator_modules) : Print the name of the validator source file the error/warning was caught it. This can be helpful for developing validations.
- -n (--no_colors) : The output is colored by default. If this bothers you, this flag will turn off the coloring.
Expand Down
4 changes: 4 additions & 0 deletions src/cli-validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ program
)
.option(
'-c, --config <file>', 'path to config file, used instead of .validaterc if provided'
)
.option(
'-e, --errors_only',
'only print the errors, ignore the warnings'
);

/* prettier-ignore */
Expand Down
18 changes: 16 additions & 2 deletions src/cli-validator/runValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const processInput = async function(program) {
const turnOffColoring = !!program.no_colors;
const defaultMode = !!program.default_mode;
const jsonOutput = !!program.json;
const errorsOnly = !!program.errors_only;

const configFileOverride = program.config;

Expand Down Expand Up @@ -231,11 +232,24 @@ const processInput = async function(program) {
continue;
}

// the warning property tells the user if warnings are included as part of the output
// if errorsOnly is true, only errors will be returned, so need to force this to false
if (errorsOnly) {
results.warning = false;
}

if (jsonOutput) {
printJson(results, originalFile);
printJson(results, originalFile, errorsOnly);
} else {
if (results.error || results.warning) {
print(results, chalk, printValidators, reportingStats, originalFile);
print(
results,
chalk,
printValidators,
reportingStats,
originalFile,
errorsOnly
);
// fail on errors, but not if there are only warnings
if (results.error) exitCode = 1;
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/cli-validator/utils/printJsonResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const getLineNumberForPath = require(__dirname + '/../../plugins/ast/ast')
.getLineNumberForPath;

// function to print the results as json to the console.
module.exports = function printJson(results, originalFile) {
const types = ['errors', 'warnings'];
module.exports = function printJson(results, originalFile, errorsOnly) {
const types = errorsOnly ? ['errors'] : ['errors', 'warnings'];
types.forEach(type => {
each(results[type], problems => {
problems.forEach(problem => {
Expand All @@ -32,6 +32,9 @@ module.exports = function printJson(results, originalFile) {
});
});
});
if (errorsOnly) {
delete results.warnings;
}
// render the results to json in the console with 2 char spacing
const jsonstr = JSON.stringify(results, null, 2);
console.log(jsonstr);
Expand Down
5 changes: 3 additions & 2 deletions src/cli-validator/utils/printResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ module.exports = function print(
chalk,
printValidators,
reportingStats,
originalFile
originalFile,
errorsOnly
) {
const types = ['errors', 'warnings'];
const types = errorsOnly ? ['errors'] : ['errors', 'warnings'];
const colors = {
errors: 'bgRed',
warnings: 'bgYellow'
Expand Down
45 changes: 45 additions & 0 deletions test/cli-validator/tests/optionHandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ describe('cli tool - test option handling', function() {
expect(validatorsPrinted).toEqual(true);
});

it('should print only errors when the -e command is given', async function() {
const capturedText = [];

const unhookIntercept = intercept(function(txt) {
capturedText.push(stripAnsiFrom(txt));
return '';
});

const program = {};
program.args = ['./test/cli-validator/mockFiles/errAndWarn.yaml'];
program.errors_only = true;
program.default_mode = true;

await commandLineValidator(program);
unhookIntercept();

capturedText.forEach(function(line) {
expect(line.includes('warnings')).toEqual(false);
});
});

it('should print correct statistics report when -s option is given', async function() {
const capturedText = [];

Expand Down Expand Up @@ -223,6 +244,30 @@ describe('cli tool - test option handling', function() {
);
});

it('should print only errors as json output when -j -e option is given', async function() {
const capturedText = [];

const unhookIntercept = intercept(function(txt) {
capturedText.push(stripAnsiFrom(txt));
return '';
});

const program = {};
program.args = ['./test/cli-validator/mockFiles/errAndWarn.yaml'];
program.json = true;
program.errors_only = true;
program.default_mode = true;
await commandLineValidator(program);
unhookIntercept();

// capturedText should be JSON object. convert to json and check fields
const outputObject = JSON.parse(capturedText);

expect(outputObject.warning).toEqual(false);
expect(outputObject.error).toEqual(true);
expect(outputObject.warnings).toEqual(undefined);
});

it('should change output for overridden options when config file is manually specified', async function() {
const capturedText = [];

Expand Down

0 comments on commit a338bf0

Please sign in to comment.