From ab6bcc26baa07cd5e6a2d879f0339771d8d5e44b Mon Sep 17 00:00:00 2001 From: Aaron Spear Date: Tue, 4 Jun 2019 08:32:41 -0600 Subject: [PATCH] feat: Added --config option, adding support for explicit config file * 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. --- README.md | 1 + src/cli-validator/index.js | 3 ++ src/cli-validator/runValidator.js | 4 +- .../utils/processConfiguration.js | 12 ++++-- .../mockFiles/justWarnConfigOverride.json | 10 +++++ test/cli-validator/tests/optionHandling.js | 43 +++++++++++++++++++ 6 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 test/cli-validator/mockFiles/justWarnConfigOverride.json diff --git a/README.md b/README.md index 4bd197397..15b418997 100644 --- a/README.md +++ b/README.md @@ -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 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._ diff --git a/src/cli-validator/index.js b/src/cli-validator/index.js index 0ee1ce6fc..b93cf4443 100755 --- a/src/cli-validator/index.js +++ b/src/cli-validator/index.js @@ -35,6 +35,9 @@ program .option( '-s, --report_statistics', 'report the frequency of each occurring error/warning' + ) + .option( + '-c, --config ', 'path to config file, used instead of .validaterc if provided' ); /* prettier-ignore */ diff --git a/src/cli-validator/runValidator.js b/src/cli-validator/runValidator.js index d8690d6b2..5e51cec1e 100644 --- a/src/cli-validator/runValidator.js +++ b/src/cli-validator/runValidator.js @@ -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; @@ -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); } diff --git a/src/cli-validator/utils/processConfiguration.js b/src/cli-validator/utils/processConfiguration.js index d2138ca7f..b625d317a 100644 --- a/src/cli-validator/utils/processConfiguration.js +++ b/src/cli-validator/utils/processConfiguration.js @@ -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) diff --git a/test/cli-validator/mockFiles/justWarnConfigOverride.json b/test/cli-validator/mockFiles/justWarnConfigOverride.json new file mode 100644 index 000000000..7f40321dd --- /dev/null +++ b/test/cli-validator/mockFiles/justWarnConfigOverride.json @@ -0,0 +1,10 @@ +{ + "shared": { + "operations": { + "no_operation_id": "off" + }, + "schemas": { + "no_schema_description": "error" + } + } +} diff --git a/test/cli-validator/tests/optionHandling.js b/test/cli-validator/tests/optionHandling.js index 53745e165..c8162b3e4 100644 --- a/test/cli-validator/tests/optionHandling.js +++ b/test/cli-validator/tests/optionHandling.js @@ -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 + }); });