-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added configurable warning limits through .thresholdrc file
- added code to process a .thresholdrc file to get warnings limit - used warning limit to set exit code to 1 and print a message if warning limit is exceeded - added validation on the user input from .thresholdrc to check if valid keys and values provided in .thresholdrc - set default limit to Number.MAX_VALUE if no .thresholdrc provided - added tests to test error handling for .thresholdrc file and expected output when warning limits exceeded - added test to check if error thrown when invalid JSON provided in .thresholdrc - refactored error printing in processConfiguration file into a printConfigErrors function - ran npm run fix to fix style - documented how to add a warnings limit in the README
- Loading branch information
Barrett Schonefeld
committed
Feb 10, 2020
1 parent
09315b4
commit a09defa
Showing
8 changed files
with
253 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"warnings": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[ | ||
: } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"errors": 0, | ||
"warnings": "text", | ||
"population": 10 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"warnings": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// the rule names are all snake case and need to stay that way. don't lint them | ||
/* eslint-disable camelcase */ | ||
|
||
const intercept = require('intercept-stdout'); | ||
const expect = require('expect'); | ||
const stripAnsiFrom = require('strip-ansi'); | ||
|
||
const commandLineValidator = require('../../../src/cli-validator/runValidator'); | ||
|
||
describe('test the .thresholdrc limits', function() { | ||
it('should show error and set exit code to 1 when warning limit exceeded', async function() { | ||
const capturedText = []; | ||
|
||
const program = {}; | ||
program.args = ['./test/cli-validator/mockFiles/circularRefs.yml']; | ||
program.limits = './test/cli-validator/mockFiles/thresholds/fiveWarnings'; | ||
program.default_mode = true; | ||
|
||
const unhookIntercept = intercept(function(txt) { | ||
capturedText.push(stripAnsiFrom(txt)); | ||
return ''; | ||
}); | ||
|
||
const exitCode = await commandLineValidator(program); | ||
|
||
unhookIntercept(); | ||
|
||
expect(exitCode).toEqual(1); | ||
|
||
expect(capturedText[capturedText.length - 1].slice(0, 18)).toEqual( | ||
`Number of warnings` | ||
); | ||
}); | ||
|
||
it('should print errors for unsupported limit options and invalid limit values', async function() { | ||
const capturedText = []; | ||
|
||
const program = {}; | ||
program.args = ['./test/cli-validator/mockFiles/clean.yml']; | ||
program.limits = './test/cli-validator/mockFiles/thresholds/invalidValues'; | ||
program.default_mode = true; | ||
|
||
const unhookIntercept = intercept(function(txt) { | ||
capturedText.push(stripAnsiFrom(txt)); | ||
return ''; | ||
}); | ||
|
||
const exitCode = await commandLineValidator(program); | ||
|
||
unhookIntercept(); | ||
|
||
// limit values invalid, so default limit, Number.MAX_VALUE, used | ||
expect(exitCode).toEqual(0); | ||
|
||
const allOutput = capturedText.join(''); | ||
|
||
expect(allOutput.includes('"population" limit not supported.')).toEqual( | ||
true | ||
); | ||
|
||
expect(allOutput.includes('Value provided for warnings')).toEqual(true); | ||
}); | ||
|
||
it('should give exit code 0 when warnings limit not exceeded', async function() { | ||
const program = {}; | ||
program.args = ['./test/cli-validator/mockFiles/clean.yml']; | ||
program.limits = './test/cli-validator/mockFiles/thresholds/zeroWarnings'; | ||
program.default_mode = true; | ||
|
||
const exitCode = await commandLineValidator(program); | ||
|
||
expect(exitCode).toEqual(0); | ||
}); | ||
|
||
it('should give an error for invalid JSON', async function() { | ||
const program = {}; | ||
program.args = ['./test/cli-validator/mockFiles/clean.yml']; | ||
program.limits = './test/cli-validator/mockFiles/thresholds/invalidJSON'; | ||
program.default_mode = true; | ||
|
||
try { | ||
await commandLineValidator(program); | ||
// error should be thrown for invalid JSON, this should not execute | ||
expect(true).toEqual(false); | ||
} catch (err) { | ||
// invalid JSON in .thresholdrc, reject with exit code 2 | ||
expect(true).toEqual(true); | ||
} | ||
}); | ||
}); |