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

feat: cli command that will only print errors #68

Merged
merged 9 commits into from
Jun 24, 2019
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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;
}
SamerJaser96 marked this conversation as resolved.
Show resolved Hide resolved
SamerJaser96 marked this conversation as resolved.
Show resolved Hide resolved

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
SamerJaser96 marked this conversation as resolved.
Show resolved Hide resolved
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);
});
SamerJaser96 marked this conversation as resolved.
Show resolved Hide resolved
});

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