From fbed3ca2aac972244eb30dc18d69c8499a22325c Mon Sep 17 00:00:00 2001 From: Eason Yang Date: Sat, 21 Jul 2018 16:59:21 +0800 Subject: [PATCH 1/3] feat(cov): add nyc instrument passthrough add a --nyc instrument in cov.js, pass everything in it to nyc --- README.md | 6 ++++++ lib/cmd/cov.js | 12 ++++++++++++ test/lib/cmd/cov.test.js | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/README.md b/README.md index 01ed8100..a0ad69c8 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,12 @@ You can pass any mocha argv. - `-x` add dir ignore coverage, support multiple argv - `--prerequire` prerequire files for coverage instrument, you can use this options if load files slowly when call `mm.app` or `mm.cluster` - `--typescript` / `--ts` enable typescript support, default to `false`, if true, will auto add `.ts` extension and ignore `typings` and `d.ts`. +- `--nyc` nyc instruments passthrough + > This is primaryly for adding additional nyc reporters because `egg-bin cov` calls `nyc` with some fixed params like `-r json -r lcov...`, which overides `reporter` key in `.nycrc`/`package.json`. + ```bash + egg-bin cov --nyc="--reporter=teamcity -r text" + ``` + - also support all test params above. #### environment diff --git a/lib/cmd/cov.js b/lib/cmd/cov.js index c2dad72f..a5a84897 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -25,6 +25,10 @@ class CovCommand extends Command { description: 'prerequire files for coverage instrument', type: 'boolean', }, + nyc: { + description: 'nyc instruments passthrough', + type: 'string', + }, }; // you can add ignore dirs here @@ -116,6 +120,14 @@ class CovCommand extends Command { this.addExclude('**/*.d.ts'); } + // nyc args passthrough + let nycArgs = context.argv.nyc; + delete context.argv.nyc; + if (nycArgs) { + nycArgs = nycArgs.substring(1, nycArgs.length - 1); + covArgs = covArgs.concat(nycArgs.split(' ')); + } + for (const exclude of this[EXCLUDES]) { covArgs.push('-x'); covArgs.push(exclude); diff --git a/test/lib/cmd/cov.test.js b/test/lib/cmd/cov.test.js index 5558ba55..35d085ba 100644 --- a/test/lib/cmd/cov.test.js +++ b/test/lib/cmd/cov.test.js @@ -198,4 +198,27 @@ describe('test/lib/cmd/cov.test.js', () => { .expect('code', 0) .end(); }); + + it('should passthrough nyc args', function* () { + mm(process.env, 'TESTS', 'test/**/*.test.js'); + mm(process.env, 'NYC_CWD', cwd); + const child = coffee.fork(eggBin, [ 'cov', '--nyc="--reporter=teamcity -r text"' ], { cwd }) + // .debug() + .expect('stdout', /should success/) + .expect('stdout', /a\.test\.js/) + .expect('stdout', /b[\/|\\]b\.test\.js/) + .expect('stdout', /##teamcity\[blockOpened name='Code Coverage Summary'\]/) + .expect('stdout', /##teamcity\[blockClosed name='Code Coverage Summary'\]/) + .expect('stdout', /File[ |]*% Stmts[ |]*% Branch[ |]*% Funcs[ |]*% Lines/) + .notExpect('stdout', /a.js/); + + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + child.expect('stdout', /Statements {3}: 80% \( 4[\/|\\]5 \)/); + } + + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); + }); }); From b3e25d82d10a2d79210ae2e9ababa19dded2c1a3 Mon Sep 17 00:00:00 2001 From: Eason Yang Date: Sat, 21 Jul 2018 18:50:08 +0800 Subject: [PATCH 2/3] feat(cov): simplify test case --- test/lib/cmd/cov.test.js | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/test/lib/cmd/cov.test.js b/test/lib/cmd/cov.test.js index 35d085ba..60c345bd 100644 --- a/test/lib/cmd/cov.test.js +++ b/test/lib/cmd/cov.test.js @@ -199,26 +199,14 @@ describe('test/lib/cmd/cov.test.js', () => { .end(); }); - it('should passthrough nyc args', function* () { + it('should passthrough nyc args', done => { mm(process.env, 'TESTS', 'test/**/*.test.js'); mm(process.env, 'NYC_CWD', cwd); - const child = coffee.fork(eggBin, [ 'cov', '--nyc="--reporter=teamcity -r text"' ], { cwd }) + coffee.fork(eggBin, [ 'cov', '--nyc="--reporter=teamcity"' ], { cwd }) // .debug() .expect('stdout', /should success/) - .expect('stdout', /a\.test\.js/) - .expect('stdout', /b[\/|\\]b\.test\.js/) .expect('stdout', /##teamcity\[blockOpened name='Code Coverage Summary'\]/) .expect('stdout', /##teamcity\[blockClosed name='Code Coverage Summary'\]/) - .expect('stdout', /File[ |]*% Stmts[ |]*% Branch[ |]*% Funcs[ |]*% Lines/) - .notExpect('stdout', /a.js/); - - // only test on npm run test - if (!process.env.NYC_ROOT_ID) { - child.expect('stdout', /Statements {3}: 80% \( 4[\/|\\]5 \)/); - } - - yield child.expect('code', 0).end(); - // only test on npm run test - if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); + .end(done); }); }); From f37c31cb5ca8503a3189e1dc653b6415b7da2c03 Mon Sep 17 00:00:00 2001 From: Eason Yang Date: Sat, 28 Jul 2018 01:44:09 +0800 Subject: [PATCH 3/3] feat(cov): move covArgs values to options --- README.md | 8 +++++--- lib/cmd/cov.js | 11 +++-------- test/lib/cmd/cov.test.js | 2 +- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a0ad69c8..0a751ef3 100644 --- a/README.md +++ b/README.md @@ -170,10 +170,12 @@ You can pass any mocha argv. - `-x` add dir ignore coverage, support multiple argv - `--prerequire` prerequire files for coverage instrument, you can use this options if load files slowly when call `mm.app` or `mm.cluster` - `--typescript` / `--ts` enable typescript support, default to `false`, if true, will auto add `.ts` extension and ignore `typings` and `d.ts`. -- `--nyc` nyc instruments passthrough - > This is primaryly for adding additional nyc reporters because `egg-bin cov` calls `nyc` with some fixed params like `-r json -r lcov...`, which overides `reporter` key in `.nycrc`/`package.json`. +- `--nyc` nyc instruments passthrough. you can use this to overwrite egg-bin's default nyc instruments and add additional ones. + >if you want to add addtional nyc reporters, you need to use this rather than add `reporter` key in `.nycrc` because: + >* when same key exists in `.nycrc` and cmd instruments, nyc prefers instrument. + >* egg-bin have some default instruments passed to nyc like `-r` and `--temp-directory` ```bash - egg-bin cov --nyc="--reporter=teamcity -r text" + egg-bin cov --nyc="-r teamcity -r text" ``` - also support all test params above. diff --git a/lib/cmd/cov.js b/lib/cmd/cov.js index a5a84897..d0cb824f 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -28,6 +28,7 @@ class CovCommand extends Command { nyc: { description: 'nyc instruments passthrough', type: 'string', + default: '--temp-directory ./node_modules/.nyc_output -r text-summary -r json-summary -r json -r lcov', }, }; @@ -106,11 +107,6 @@ class CovCommand extends Command { getCovArgs(context) { let covArgs = [ // '--show-process-tree', - '--temp-directory', './node_modules/.nyc_output', - '-r', 'text-summary', - '-r', 'json-summary', - '-r', 'json', - '-r', 'lcov', ]; // typescript support @@ -121,10 +117,9 @@ class CovCommand extends Command { } // nyc args passthrough - let nycArgs = context.argv.nyc; - delete context.argv.nyc; + const nycArgs = context.argv.nyc; + context.argv.nyc = undefined; if (nycArgs) { - nycArgs = nycArgs.substring(1, nycArgs.length - 1); covArgs = covArgs.concat(nycArgs.split(' ')); } diff --git a/test/lib/cmd/cov.test.js b/test/lib/cmd/cov.test.js index 60c345bd..f8201c55 100644 --- a/test/lib/cmd/cov.test.js +++ b/test/lib/cmd/cov.test.js @@ -202,7 +202,7 @@ describe('test/lib/cmd/cov.test.js', () => { it('should passthrough nyc args', done => { mm(process.env, 'TESTS', 'test/**/*.test.js'); mm(process.env, 'NYC_CWD', cwd); - coffee.fork(eggBin, [ 'cov', '--nyc="--reporter=teamcity"' ], { cwd }) + coffee.fork(eggBin, [ 'cov', '--nyc=-r teamcity -r text' ], { cwd }) // .debug() .expect('stdout', /should success/) .expect('stdout', /##teamcity\[blockOpened name='Code Coverage Summary'\]/)