From b79f0cc6fb01d6d0bb6833b05d8409d909e20f1c Mon Sep 17 00:00:00 2001 From: Haoliang Gao Date: Fri, 31 Mar 2017 13:55:28 +0800 Subject: [PATCH] fix: -x only support string (#47) --- lib/cmd/cov.js | 11 +++-- test/fixtures/test-files/ignore/a.js | 3 ++ test/fixtures/test-files/test/ignore.test.js | 10 +++++ test/lib/cmd/cov.test.js | 45 ++++++++++++++------ 4 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 test/fixtures/test-files/ignore/a.js create mode 100644 test/fixtures/test-files/test/ignore.test.js diff --git a/lib/cmd/cov.js b/lib/cmd/cov.js index 0045d321..72612788 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -19,7 +19,7 @@ class CovCommand extends Command { this.options = { x: { description: 'istanbul coverage ignore, one or more fileset patterns', - type: 'array', + type: 'string', }, }; @@ -43,17 +43,21 @@ class CovCommand extends Command { process.env.TMPDIR = tmpDir; // istanbul coverage ignore - const excludes = argv.x || (process.env.COV_EXCLUDES && process.env.COV_EXCLUDES.split(',')) || []; + if (argv.x) { + this[EXCLUDES].add(argv.x); + argv.x = undefined; + } + const excludes = (process.env.COV_EXCLUDES && process.env.COV_EXCLUDES.split(',')) || []; for (const exclude of excludes) { this[EXCLUDES].add(exclude); } - argv.x = undefined; const covFile = require.resolve('istanbul/lib/cli.js'); const coverageDir = path.join(cwd, 'coverage'); yield rimraf(coverageDir); const opt = { + cwd, execArgv, // resolve istanbul path for coffee env: Object.assign({ @@ -69,6 +73,7 @@ class CovCommand extends Command { // create coverage report const reportArgs = this.getReportArgs(coverageDir); + debug('reportArgs: %j', reportArgs); yield this.helper.forkNode(covFile, reportArgs, opt); } diff --git a/test/fixtures/test-files/ignore/a.js b/test/fixtures/test-files/ignore/a.js new file mode 100644 index 00000000..17897ee7 --- /dev/null +++ b/test/fixtures/test-files/ignore/a.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = ''; diff --git a/test/fixtures/test-files/test/ignore.test.js b/test/fixtures/test-files/test/ignore.test.js new file mode 100644 index 00000000..7a899f13 --- /dev/null +++ b/test/fixtures/test-files/test/ignore.test.js @@ -0,0 +1,10 @@ +'use strict'; + +const assert = require('assert'); +const a = require('../ignore/a'); + +describe('ignore.test.js', () => { + it('should success', () => { + assert(a === ''); + }); +}); diff --git a/test/lib/cmd/cov.test.js b/test/lib/cmd/cov.test.js index 82e84ee5..651bd01d 100644 --- a/test/lib/cmd/cov.test.js +++ b/test/lib/cmd/cov.test.js @@ -25,7 +25,7 @@ describe('test/lib/cmd/cov.test.js', () => { .expect('stdout', /a\.test\.js/) .expect('stdout', /b\/b\.test\.js/) .notExpect('stdout', /a.js/) - .expect('stdout', /Statements {3}: 75% \( 3\/4 \)/) + .expect('stdout', /Statements {3}: 80% \( 4\/5 \)/) .expect('code', 0) .end(err => { assert.ifError(err); @@ -37,10 +37,10 @@ describe('test/lib/cmd/cov.test.js', () => { }); }); - it('should success with COV_EXCLUDES', done => { + it('should success with COV_EXCLUDES', function* () { mm(process.env, 'TESTS', 'test/**/*.test.js'); - mm(process.env, 'COV_EXCLUDES', 'lib/*'); - coffee.fork(eggBin, [ 'cov' ], { cwd }) + mm(process.env, 'COV_EXCLUDES', 'ignore/*'); + yield coffee.fork(eggBin, [ 'cov' ], { cwd }) .coverage(false) // .debug() .expect('stdout', /\/test\/fixtures\/test-files\/\.tmp true/) @@ -48,16 +48,35 @@ describe('test/lib/cmd/cov.test.js', () => { .expect('stdout', /a\.test\.js/) .expect('stdout', /b\/b\.test\.js/) .notExpect('stdout', /a.js/) - .expect('stdout', /Statements {3}: Unknown% \( 0\/0 \)/) + .expect('stdout', /Statements {3}: 75% \( 3\/4 \)/) .expect('code', 0) - .end(err => { - assert(!err); - assert(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - assert(!fs.existsSync(path.join(cwd, '.tmp'))); - done(); - }); + .end(); + assert(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); + assert(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); + assert(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); + assert(!fs.existsSync(path.join(cwd, '.tmp'))); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore\/a.js/.test(lcov)); + }); + + it('should success with -x to ignore files', function* () { + yield coffee.fork(eggBin, [ 'cov', '-x', 'ignore/*', 'test/**/*.test.js' ], { cwd }) + .coverage(false) + // .debug() + .expect('stdout', /\/test\/fixtures\/test-files\/\.tmp true/) + .expect('stdout', /should success/) + .expect('stdout', /a\.test\.js/) + .expect('stdout', /b\/b\.test\.js/) + .notExpect('stdout', /a.js/) + .expect('stdout', /Statements {3}: 75% \( 3\/4 \)/) + .expect('code', 0) + .end(); + assert(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); + assert(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); + assert(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); + assert(!fs.existsSync(path.join(cwd, '.tmp'))); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore\/a.js/.test(lcov)); }); it('should fail when test fail', done => {