From 1ac9d686e888213f1ac8c36f3e26ce285250eaa5 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 20 Jun 2017 14:18:41 +0800 Subject: [PATCH] fix: should support multi exclude dirs (#66) 100% coverage --- lib/cmd/cov.js | 10 ++++-- test/fixtures/mocha-test/test/foo.test.js | 9 ++++++ test/lib/cmd/cov.test.js | 39 +++++++++++++++++++---- test/mocha-bin.test.js | 17 ++++++++++ 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/mocha-test/test/foo.test.js create mode 100644 test/mocha-bin.test.js diff --git a/lib/cmd/cov.js b/lib/cmd/cov.js index 949e0d71..d52be79a 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -45,12 +45,18 @@ class CovCommand extends Command { // ignore coverage if (argv.x) { - this[EXCLUDES].add(argv.x); + if (Array.isArray(argv.x)) { + for (const exclude of argv.x) { + this.addExclude(exclude); + } + } else { + this.addExclude(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); + this.addExclude(exclude); } const nycCli = require.resolve('nyc/bin/nyc.js'); diff --git a/test/fixtures/mocha-test/test/foo.test.js b/test/fixtures/mocha-test/test/foo.test.js new file mode 100644 index 00000000..224222ae --- /dev/null +++ b/test/fixtures/mocha-test/test/foo.test.js @@ -0,0 +1,9 @@ +'use strict'; + +const assert = require('assert'); + +describe('mocha-test', () => { + it('should work', () => { + assert(true); + }); +}); diff --git a/test/lib/cmd/cov.test.js b/test/lib/cmd/cov.test.js index ac7d4e27..4c5f58d1 100644 --- a/test/lib/cmd/cov.test.js +++ b/test/lib/cmd/cov.test.js @@ -16,7 +16,7 @@ describe('test/lib/cmd/cov.test.js', () => { mm(process.env, 'TESTS', 'test/**/*.test.js'); mm(process.env, 'NYC_CWD', cwd); const child = coffee.fork(eggBin, [ 'cov' ], { cwd }) - .debug() + // .debug() .expect('stdout', /should success/) .expect('stdout', /a\.test\.js/) .expect('stdout', /b[\/|\\]b\.test\.js/) @@ -62,15 +62,42 @@ describe('test/lib/cmd/cov.test.js', () => { 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 }) + it('should success with -x to ignore one dirs', function* () { + const child = coffee.fork(eggBin, [ 'cov', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }) // .debug() .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) + .notExpect('stdout', /a.js/); + + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 75% \( 3[\/|\\]4 \)/); + } + + yield child.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'))); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + }); + + it('should success with -x to ignore multi dirs', function* () { + const child = coffee.fork(eggBin, [ 'cov', '-x', 'ignore2/*', '-x', 'ignore/', 'test/**/*.test.js' ], { cwd }) + // .debug() + .expect('stdout', /should success/) + .expect('stdout', /a\.test\.js/) + .expect('stdout', /b[\/|\\]b\.test\.js/) + .notExpect('stdout', /a.js/); + + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 75% \( 3[\/|\\]4 \)/); + } + + yield child.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'))); diff --git a/test/mocha-bin.test.js b/test/mocha-bin.test.js new file mode 100644 index 00000000..911949ad --- /dev/null +++ b/test/mocha-bin.test.js @@ -0,0 +1,17 @@ +'use strict'; + +const path = require('path'); +const coffee = require('coffee'); + +describe('test/mocha-bin.test.js', () => { + const mochaBin = require.resolve('../bin/mocha.js'); + const cwd = path.join(__dirname, 'fixtures/mocha-test'); + + it('should test with mocha', () => { + return coffee.fork(mochaBin, [ 'test/*.test.js' ], { cwd }) + .debug() + .expect('stdout', /1 passing/) + .expect('code', 0) + .end(); + }); +});