-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: use nyc instead of istanbul #63
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3fcb07f
feat: use nyc instead of istanbul
fengmk2 072f746
f
popomore 41f9c92
f
popomore e6c854e
f
popomore e50e8f3
f
popomore 1250907
f
popomore 99eb3e2
f
popomore a65904b
f
popomore 6f83ea7
f
popomore e96bb6a
f
popomore b3e440d
f
popomore 6f2dd24
f
popomore 746c138
f
popomore d095a09
f
popomore 90fa58b
f
popomore 5350009
f
popomore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,8 @@ | |
|
||
const debug = require('debug')('egg-bin:cov'); | ||
const path = require('path'); | ||
const mkdirp = require('mz-modules/mkdirp'); | ||
const rimraf = require('mz-modules/rimraf'); | ||
const testExclude = require('test-exclude'); | ||
|
||
const Command = require('./test'); | ||
const EXCLUDES = Symbol('cov#excludes'); | ||
|
@@ -21,37 +21,23 @@ class CovCommand extends Command { | |
description: 'istanbul coverage ignore, one or more fileset patterns', | ||
type: 'string', | ||
}, | ||
prerequire: { | ||
description: 'prerequire files for coverage instrument', | ||
type: 'boolean', | ||
}, | ||
}; | ||
|
||
// you can add ignore dirs here | ||
this[EXCLUDES] = new Set([ | ||
'examples/**', | ||
'mocks_*/**', | ||
]); | ||
].concat(testExclude.defaultExclude)); | ||
} | ||
|
||
get description() { | ||
return 'Run test with coverage'; | ||
} | ||
|
||
* run(context) { | ||
const { cwd, argv, execArgv } = context; | ||
const tmpDir = path.join(cwd, '.tmp'); | ||
yield mkdirp(tmpDir); | ||
|
||
process.env.NODE_ENV = 'test'; | ||
process.env.TMPDIR = tmpDir; | ||
|
||
if (argv.prerequire) { | ||
process.env.EGG_BIN_PREREQUIRE = 'true'; | ||
} | ||
delete argv.prerequire; | ||
const { cwd, argv, execArgv, env } = context; | ||
|
||
// istanbul coverage ignore | ||
// ignore coverage | ||
if (argv.x) { | ||
this[EXCLUDES].add(argv.x); | ||
argv.x = undefined; | ||
|
@@ -61,29 +47,22 @@ class CovCommand extends Command { | |
this[EXCLUDES].add(exclude); | ||
} | ||
|
||
const covFile = require.resolve('istanbul/lib/cli.js'); | ||
const nycCli = require.resolve('nyc/bin/nyc.js'); | ||
const coverageDir = path.join(cwd, 'coverage'); | ||
yield rimraf(coverageDir); | ||
const outputDir = path.join(cwd, 'node_modules/.nyc_output'); | ||
yield rimraf(outputDir); | ||
|
||
const opt = { | ||
cwd, | ||
execArgv, | ||
// resolve istanbul path for coffee | ||
env: Object.assign({ | ||
istanbul_bin_path: covFile, | ||
}, process.env), | ||
env: Object.assign({ NODE_ENV: 'test' }, env), | ||
}; | ||
|
||
// save coverage-xxxx.json to $PWD/coverage | ||
const covArgs = this.getCovArgs(context); | ||
debug('covArgs: %j', covArgs); | ||
yield this.helper.forkNode(covFile, covArgs, opt); | ||
yield rimraf(tmpDir); | ||
|
||
// create coverage report | ||
const reportArgs = this.getReportArgs(coverageDir); | ||
debug('reportArgs: %j', reportArgs); | ||
yield this.helper.forkNode(covFile, reportArgs, opt); | ||
yield this.helper.forkNode(nycCli, covArgs, opt); | ||
} | ||
|
||
/** | ||
|
@@ -97,42 +76,33 @@ class CovCommand extends Command { | |
/** | ||
* get coverage args | ||
* @param {Object} context - { cwd, argv, ...} | ||
* @return {Array} args for istanbul | ||
* @return {Array} args for nyc | ||
* @protected | ||
*/ | ||
getCovArgs(context) { | ||
const covArgs = [ | ||
'cover', | ||
'--report', 'none', | ||
'--print', 'none', | ||
'--include-pid', | ||
let covArgs = [ | ||
// '--show-process-tree', | ||
// instrument all files in nyc process and cache to disk, | ||
// Then in mocha process, read instrumented files from cache. | ||
// | ||
// nyc | ||
// `- egg-bin test | ||
// `- mocha | ||
'--all', | ||
'--temp-directory', './node_modules/.nyc_output', | ||
'-r', 'text-summary', | ||
'-r', 'json-summary', | ||
'-r', 'json', | ||
'-r', 'lcov', | ||
]; | ||
|
||
for (const exclude of this[EXCLUDES]) { | ||
covArgs.push('-x'); | ||
covArgs.push(exclude); | ||
} | ||
const mochaFile = require.resolve('mocha/bin/_mocha'); | ||
const testArgs = this.formatTestArgs(context); | ||
debug('testArgs: %j', testArgs); | ||
return covArgs.concat(mochaFile, '--', ...testArgs); | ||
} | ||
|
||
/** | ||
* get coverage report args | ||
* @param {String} coverageDir - coverage result directory | ||
* @return {Array} args for istanbul coverage report | ||
* @protected | ||
*/ | ||
getReportArgs(coverageDir) { | ||
return [ | ||
'report', | ||
'--root', coverageDir, | ||
'text-summary', | ||
'json-summary', | ||
'json', | ||
'lcov', | ||
]; | ||
covArgs.push(require.resolve('mocha/bin/_mocha')); | ||
covArgs = covArgs.concat(this.formatTestArgs(context)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 上面可以用 |
||
return covArgs; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该不会慢吧?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
就是提前加载文件,没看到加载了额外的文件