From a33e561c95921c8f5b466e88e0a01d808363c8e2 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 4 Aug 2016 12:43:24 +0800 Subject: [PATCH] feat: add COV_EXCLUDES for coverage excludes (#7) --- README.md | 8 ++++++++ README.zh-CN.md | 8 ++++++++ lib/cov_command.js | 6 ++++++ test/egg-cov.test.js | 28 +++++++++++++++++++++++++--- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8e4a62e8..23000392 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,14 @@ Using [istanbul] to run code coverage, it support all test params above. Coverage reporter will output text-summary, json and lcov. +#### excludes + +You can set `COV_EXCLUDES` env to add dir ignore coverage. + +```bash +$ COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin cov +``` + ## Custom egg-bin for your team You maybe need a custom egg-bin to implement more custom features diff --git a/README.zh-CN.md b/README.zh-CN.md index ab33216b..3d6a7d7f 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -96,6 +96,14 @@ TEST_TIMEOUT=2000 egg-bin test 支持的表报有 text-summary,json,lcov,并通过 [alicov] 上传。 +#### excludes + +可以通过设置 `COV_EXCLUDES` 环境变量忽略对指定目录的代码覆盖率测试。 + +```bash +$ COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin cov +``` + ## 定制属于你团队的 egg-bin 如果你的团队已经基于 egg 开发了属于自己的框架,那么很可能你会需要在 egg-bin 上做更多自定义功能。 diff --git a/lib/cov_command.js b/lib/cov_command.js index 663abbb0..5b2ab8fd 100644 --- a/lib/cov_command.js +++ b/lib/cov_command.js @@ -13,6 +13,12 @@ class CovCommand extends Command { 'examples/**', 'mocks_*/**', ]; + if (process.env.COV_EXCLUDES) { + const excludes = process.env.COV_EXCLUDES.split(','); + for (const exclude of excludes) { + this.excludes.push(exclude); + } + } } * run(cwd, args) { diff --git a/test/egg-cov.test.js b/test/egg-cov.test.js index c38eb50c..c71909d2 100644 --- a/test/egg-cov.test.js +++ b/test/egg-cov.test.js @@ -28,17 +28,39 @@ describe('egg-bin cov', () => { .end((err, res) => { assert.ifError(err); assert.ok(!/a.js/.test(res.stdout)); - // 测试 report json assert.ok(fs.existsSync(path.join(appdir, 'coverage/coverage-final.json'))); - // 测试 report lcov assert.ok(fs.existsSync(path.join(appdir, 'coverage/lcov-report/index.html'))); assert.ok(fs.existsSync(path.join(appdir, 'coverage/lcov.info'))); - // 已删除 assert.ok(!fs.existsSync(path.join(appdir, '.tmp'))); done(); }); }); + it('should success with COV_EXCLUDES', done => { + mm(process.env, 'TESTS', 'test/**/*.test.js'); + mm(process.env, 'COV_EXCLUDES', 'lib/*'); + coffee.fork(eggBin, [ 'cov' ], { + cwd: appdir, + }) + .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/) + .expect('stdout', /Statements {3}: 100% \( 0\/0 \)/) + .expect('code', 0) + .end((err, res) => { + assert(!err); + assert(!/a.js/.test(res.stdout)); + assert(fs.existsSync(path.join(appdir, 'coverage/coverage-final.json'))); + assert(fs.existsSync(path.join(appdir, 'coverage/lcov-report/index.html'))); + assert(fs.existsSync(path.join(appdir, 'coverage/lcov.info'))); + assert(!fs.existsSync(path.join(appdir, '.tmp'))); + done(); + }); + }); + it('should fail when test fail', done => { mm(process.env, 'TESTS', 'test/fail.js'); coffee.fork(eggBin, [ 'cov' ], {