Skip to content

Commit

Permalink
feat: Added --config option, adding support for explicit config file
Browse files Browse the repository at this point in the history
* feat: Added --config option, adding support for explicit config file

This feature adds a -c/--config option which is a path to a config file (.validaterc).
This allows usage of configuration that are not dependent on the .validaterc being in
the same folder (or a parent) of the current working directory.
  • Loading branch information
aspear authored and dpopp07 committed Jun 4, 2019
1 parent e89ff54 commit ab6bcc2
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The `-g` flag installs the tool globally so that the validator can be run from a
- -n (--no_colors) : The output is colored by default. If this bothers you, this flag will turn off the coloring.
- -v (--version) : Print the current semantic version of the validator
- -h (--help) : This option prints the usage menu.
- -c (--config) <path/to/your/config> : Path to a validator configuration file. If provided, this is used instead of .validaterc.

_These options only apply to running the validator on a file, not to any commands._

Expand Down
3 changes: 3 additions & 0 deletions src/cli-validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ program
.option(
'-s, --report_statistics',
'report the frequency of each occurring error/warning'
)
.option(
'-c, --config <file>', 'path to config file, used instead of .validaterc if provided'
);

/* prettier-ignore */
Expand Down
4 changes: 3 additions & 1 deletion src/cli-validator/runValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const processInput = async function(program) {
const defaultMode = !!program.default_mode;
const jsonOutput = !!program.json;

const configFileOverride = program.config;

// turn on coloring by default
const colors = turnOffColoring ? false : true;

Expand Down Expand Up @@ -132,7 +134,7 @@ const processInput = async function(program) {
// process the config file for the validations
let configObject;
try {
configObject = await config.get(defaultMode, chalk);
configObject = await config.get(defaultMode, chalk, configFileOverride);
} catch (err) {
return Promise.reject(err);
}
Expand Down
12 changes: 9 additions & 3 deletions src/cli-validator/utils/processConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,17 @@ const validateConfigObject = function(configObject, chalk) {
return configObject;
};

const getConfigObject = async function(defaultMode, chalk) {
const getConfigObject = async function(defaultMode, chalk, configFileOverride) {
let configObject;

// if user explicitly provided a config file, use it instead of .validaterc
const configFileName = configFileOverride || '.validaterc';

// search up the file system for the first instance
// of the config file
const configFile = await findUp('.validaterc');
// of '.validaterc' or,
// if a config file override is passed in, use find-up
// to verify existence of the file
const configFile = await findUp(configFileName);

// if the user does not have a config file, run in default mode and warn them
// (findUp returns null if it does not find a file)
Expand Down
10 changes: 10 additions & 0 deletions test/cli-validator/mockFiles/justWarnConfigOverride.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"shared": {
"operations": {
"no_operation_id": "off"
},
"schemas": {
"no_schema_description": "error"
}
}
}
43 changes: 43 additions & 0 deletions test/cli-validator/tests/optionHandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,47 @@ describe('cli tool - test option handling', function() {
'Operations must have a non-empty `operationId`.'
);
});

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

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

const program = {};
program.args = ['./test/cli-validator/mockFiles/justWarn.yml'];
program.config =
'./test/cli-validator/mockFiles/justWarnConfigOverride.json';

const exitCode = await commandLineValidator(program);
unhookIntercept();

expect(exitCode).toEqual(1);

// simple state machine to count the number of warnings and errors.
let errorCount = 0;
let warningCount = 0;
let inErrorBlock = false;
let inWarningBlock = false;

capturedText.forEach(function(line) {
if (line.includes('errors')) {
inErrorBlock = true;
inWarningBlock = false;
} else if (line.includes('warnings')) {
inErrorBlock = false;
inWarningBlock = true;
} else if (line.includes('Message')) {
if (inErrorBlock) {
errorCount++;
} else if (inWarningBlock) {
warningCount++;
}
}
});
expect(warningCount).toEqual(1); // without the config this value is 5
expect(errorCount).toEqual(3); // without the config this value is 0
});
});

0 comments on commit ab6bcc2

Please sign in to comment.