Skip to content

Commit ab6bcc2

Browse files
aspeardpopp07
authored andcommitted
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.
1 parent e89ff54 commit ab6bcc2

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ The `-g` flag installs the tool globally so that the validator can be run from a
6161
- -n (--no_colors) : The output is colored by default. If this bothers you, this flag will turn off the coloring.
6262
- -v (--version) : Print the current semantic version of the validator
6363
- -h (--help) : This option prints the usage menu.
64+
- -c (--config) <path/to/your/config> : Path to a validator configuration file. If provided, this is used instead of .validaterc.
6465

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

src/cli-validator/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ program
3535
.option(
3636
'-s, --report_statistics',
3737
'report the frequency of each occurring error/warning'
38+
)
39+
.option(
40+
'-c, --config <file>', 'path to config file, used instead of .validaterc if provided'
3841
);
3942

4043
/* prettier-ignore */

src/cli-validator/runValidator.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const processInput = async function(program) {
3838
const defaultMode = !!program.default_mode;
3939
const jsonOutput = !!program.json;
4040

41+
const configFileOverride = program.config;
42+
4143
// turn on coloring by default
4244
const colors = turnOffColoring ? false : true;
4345

@@ -132,7 +134,7 @@ const processInput = async function(program) {
132134
// process the config file for the validations
133135
let configObject;
134136
try {
135-
configObject = await config.get(defaultMode, chalk);
137+
configObject = await config.get(defaultMode, chalk, configFileOverride);
136138
} catch (err) {
137139
return Promise.reject(err);
138140
}

src/cli-validator/utils/processConfiguration.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,17 @@ const validateConfigObject = function(configObject, chalk) {
168168
return configObject;
169169
};
170170

171-
const getConfigObject = async function(defaultMode, chalk) {
171+
const getConfigObject = async function(defaultMode, chalk, configFileOverride) {
172172
let configObject;
173+
174+
// if user explicitly provided a config file, use it instead of .validaterc
175+
const configFileName = configFileOverride || '.validaterc';
176+
173177
// search up the file system for the first instance
174-
// of the config file
175-
const configFile = await findUp('.validaterc');
178+
// of '.validaterc' or,
179+
// if a config file override is passed in, use find-up
180+
// to verify existence of the file
181+
const configFile = await findUp(configFileName);
176182

177183
// if the user does not have a config file, run in default mode and warn them
178184
// (findUp returns null if it does not find a file)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"shared": {
3+
"operations": {
4+
"no_operation_id": "off"
5+
},
6+
"schemas": {
7+
"no_schema_description": "error"
8+
}
9+
}
10+
}

test/cli-validator/tests/optionHandling.js

+43
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,47 @@ describe('cli tool - test option handling', function() {
222222
'Operations must have a non-empty `operationId`.'
223223
);
224224
});
225+
226+
it('should change output for overridden options when config file is manually specified', async function() {
227+
const capturedText = [];
228+
229+
const unhookIntercept = intercept(function(txt) {
230+
capturedText.push(stripAnsiFrom(txt));
231+
return '';
232+
});
233+
234+
const program = {};
235+
program.args = ['./test/cli-validator/mockFiles/justWarn.yml'];
236+
program.config =
237+
'./test/cli-validator/mockFiles/justWarnConfigOverride.json';
238+
239+
const exitCode = await commandLineValidator(program);
240+
unhookIntercept();
241+
242+
expect(exitCode).toEqual(1);
243+
244+
// simple state machine to count the number of warnings and errors.
245+
let errorCount = 0;
246+
let warningCount = 0;
247+
let inErrorBlock = false;
248+
let inWarningBlock = false;
249+
250+
capturedText.forEach(function(line) {
251+
if (line.includes('errors')) {
252+
inErrorBlock = true;
253+
inWarningBlock = false;
254+
} else if (line.includes('warnings')) {
255+
inErrorBlock = false;
256+
inWarningBlock = true;
257+
} else if (line.includes('Message')) {
258+
if (inErrorBlock) {
259+
errorCount++;
260+
} else if (inWarningBlock) {
261+
warningCount++;
262+
}
263+
}
264+
});
265+
expect(warningCount).toEqual(1); // without the config this value is 5
266+
expect(errorCount).toEqual(3); // without the config this value is 0
267+
});
225268
});

0 commit comments

Comments
 (0)