diff --git a/lib/commands/report.js b/lib/commands/report.js index 419134a1f..0b48d869f 100644 --- a/lib/commands/report.js +++ b/lib/commands/report.js @@ -39,6 +39,38 @@ exports.builder = function (yargs) { type: 'boolean', global: false }) + .option('check-coverage', { + type: 'boolean', + default: false, + describe: 'check whether coverage is within thresholds provided', + global: false + }) + .option('branches', { + default: 0, + description: 'what % of branches must be covered?', + global: false + }) + .option('functions', { + default: 0, + description: 'what % of functions must be covered?', + global: false + }) + .option('lines', { + default: 90, + description: 'what % of lines must be covered?', + global: false + }) + .option('statements', { + default: 0, + description: 'what % of statements must be covered?', + global: false + }) + .option('per-file', { + default: false, + type: 'boolean', + description: 'check thresholds per file', + global: false + }) .example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage') } @@ -46,4 +78,12 @@ exports.handler = function (argv) { process.env.NYC_CWD = process.cwd() var nyc = new NYC(argv) nyc.report() + if (argv.checkCoverage) { + nyc.checkCoverage({ + lines: argv.lines, + functions: argv.functions, + branches: argv.branches, + statements: argv.statements + }, argv['per-file']) + } } diff --git a/test/nyc-bin.js b/test/nyc-bin.js index d96666493..1aae65b97 100644 --- a/test/nyc-bin.js +++ b/test/nyc-bin.js @@ -48,6 +48,38 @@ describe('the nyc cli', function () { }) }) + describe('report and check', function () { + it('should show coverage check along with report', function (done) { + // generate some coverage info + var args = [bin, '--silent', process.execPath, './half-covered.js'] + + var proc = spawn(process.execPath, args, { + cwd: fixturesCLI, + env: env + }) + + proc.on('close', function (code) { + code.should.equal(0) + var args = [bin, 'report', '--check-coverage', '--lines=100'] + var proc = spawn(process.execPath, args, { + cwd: fixturesCLI, + env: env + }) + + var stderr = '' + proc.stderr.on('data', function (chunk) { + stderr += chunk + }) + + proc.on('close', function (code) { + code.should.not.equal(0) + stderr.should.equal('ERROR: Coverage for lines (50%) does not meet global threshold (100%)\n') + done() + }) + }) + }) + }) + describe('--exclude', function () { it('should allow default exclude rules to be overridden', function (done) { var args = [bin, '--all', '--exclude', '**/half-covered.js', process.execPath, './half-covered.js']