Comparing version 1.2.0 to 1.3.0
@@ -17,3 +17,3 @@ #!/usr/bin/env node | ||
.alias('h', 'help') | ||
.epilog('github.com/gotwarlost/istanbul for available reporters') | ||
.epilog('http://github.com/gotwarlost/istanbul for available reporters') | ||
.argv | ||
@@ -20,0 +20,0 @@ |
#!/usr/bin/env node | ||
var sw = require('spawn-wrap') | ||
var NYC = require('../'), | ||
yargs = require('yargs') | ||
.usage('$0 [file-to-instrument]') | ||
.example('$0 ./node_modules/.bin/mocha', 'run mocha test-suite and output JSON files with coverage information') | ||
if (process.env.NYC_CWD) { | ||
var NYC = require('../') | ||
;(new NYC()).wrap() | ||
;(new NYC()).wrap() | ||
// make sure we can run coverage on | ||
// our own index.js, I like turtles. | ||
var name = require.resolve('../') | ||
delete require.cache[name] | ||
// make it so we can run coverage on nyc itself. | ||
var name = require.resolve('../') | ||
delete require.cache[name] | ||
sw.runMain() | ||
} else { | ||
var NYC = require('../') | ||
// hide the fact that nyc.js was used to execute command. | ||
if (process.argv[1].match((/(nyc.js$)|(nyc$)/))) process.argv.splice(1, 1) | ||
;(new NYC()).cleanup() | ||
// execute main on whatever file was wrapped by nyc. | ||
// ./bin/nyc.js ./node_modules/.bin/mocha | ||
if (process.argv[1]) { | ||
delete require('module')._cache[process.argv[1]] | ||
process.argv[1] = require('path').resolve(process.argv[1]) | ||
require('module').runMain() | ||
} else { | ||
yargs.showHelp() | ||
sw([__filename], { | ||
NYC_CWD: process.cwd() | ||
}) | ||
// this spawn gets wrapped | ||
var child = require('child_process').spawn( | ||
process.argv[2], | ||
process.argv.slice(3), | ||
{ stdio: 'inherit' } | ||
) | ||
child.on('close', function (code, signal) { | ||
if (signal) { | ||
process.kill(process.pid, signal) | ||
} else { | ||
process.exit(code) | ||
} | ||
}) | ||
} |
33
index.js
@@ -0,1 +1,3 @@ | ||
/* global __coverage__ */ | ||
var _ = require('lodash'), | ||
@@ -8,3 +10,2 @@ fs = require('fs'), | ||
rimraf = require('rimraf'), | ||
spawnWrap = require('spawn-wrap'), | ||
stripBom = require('strip-bom') | ||
@@ -32,6 +33,9 @@ | ||
if (!process.env.NYC_CWD) rimraf.sync(this.tmpDirectory()) | ||
mkdirp.sync(this.tmpDirectory()) | ||
} | ||
NYC.prototype.cleanup = function () { | ||
if (!process.env.NYC_CWD) rimraf.sync(this.tmpDirectory()) | ||
} | ||
NYC.prototype._wrapRequire = function () { | ||
@@ -64,8 +68,10 @@ var _this = this | ||
var _this = this, | ||
outputCoverage = function (arg) { | ||
if (!global.__coverage__) return | ||
outputCoverage = function () { | ||
var coverage | ||
if (typeof __coverage__ === 'object') coverage = __coverage__ | ||
if (!coverage) return | ||
fs.writeFileSync( | ||
path.resolve(_this.tmpDirectory(), './', process.pid + '.json'), | ||
JSON.stringify(global.__coverage__), | ||
JSON.stringify(coverage), | ||
'utf-8' | ||
@@ -75,13 +81,14 @@ ) | ||
// output temp coverage reports as processes exit. | ||
;['exit', 'SIGTERM', 'SIGINT', 'SIGHUP'].forEach(function (signal) { | ||
process.on(signal, function () { | ||
outputCoverage() | ||
if (signal === 'SIGTERM') process.exit(143) | ||
}) | ||
var _kill = process.kill | ||
process.kill = function (pid, signal) { | ||
outputCoverage() | ||
_kill(pid, signal) | ||
} | ||
process.on('exit', function () { | ||
outputCoverage() | ||
}) | ||
} | ||
NYC.prototype.wrap = function () { | ||
spawnWrap([this.subprocessBin], {NYC_CWD: this.cwd}) | ||
NYC.prototype.wrap = function (bin) { | ||
this._wrapRequire() | ||
@@ -88,0 +95,0 @@ this._wrapExit() |
{ | ||
"name": "nyc", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "forking code-coverage using istanbul.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard && ./bin/nyc.js ./node_modules/.bin/mocha --ui exports", | ||
"test": "standard && ./bin/nyc.js tap ./test/nyc-test.js", | ||
"coverage": "./bin/nyc-report.js --reporter=text-lcov | coveralls" | ||
@@ -25,2 +25,5 @@ }, | ||
], | ||
"contributors": [ | ||
"Isaac Schlueter <i@izs.me>" | ||
], | ||
"author": "Ben Coe <ben@npmjs.com>", | ||
@@ -30,17 +33,15 @@ "license": "ISC", | ||
"istanbul": "^0.3.14", | ||
"jsonstream": "^1.0.3", | ||
"lodash": "^3.8.0", | ||
"mkdirp": "^0.5.0", | ||
"rimraf": "^2.3.3", | ||
"spawn-wrap": "0.0.9", | ||
"strip-bom": "^1.0.0", | ||
"yargs": "^3.8.0", | ||
"spawn-wrap": "0.0.0" | ||
"yargs": "^3.8.0" | ||
}, | ||
"bundledDependencies": [ | ||
"spawn-wrap" | ||
], | ||
"devDependencies": { | ||
"chai": "^2.3.0", | ||
"coveralls": "^2.11.2", | ||
"mocha": "^2.2.4", | ||
"standard": "^3.7.3" | ||
"standard": "^3.7.3", | ||
"tap": "^1.0.4" | ||
}, | ||
@@ -47,0 +48,0 @@ "repository": { |
@@ -7,3 +7,51 @@ # nyc | ||
a code coverage reporter built on [istanbul](https://www.npmjs.com/package/istanbul) | ||
a code coverage tool built on [istanbul](https://www.npmjs.com/package/istanbul) | ||
that works well for applications that spawn child processes. | ||
## Usage | ||
Run your tests with the test coverage collector: | ||
```json | ||
{ | ||
"script": { | ||
"test": "nyc tap ./test/*.js", | ||
} | ||
} | ||
``` | ||
Now run the reporter afterwords to view your coverage statistics: | ||
```bash | ||
nyc-report | ||
--------------------|-----------|-----------|-----------|-----------| | ||
File | % Stmts |% Branches | % Funcs | % Lines | | ||
--------------------|-----------|-----------|-----------|-----------| | ||
./ | 85.96 | 50 | 75 | 92.31 | | ||
index.js | 85.96 | 50 | 75 | 92.31 | | ||
./test/ | 98.08 | 50 | 95 | 98.04 | | ||
nyc-test.js | 98.08 | 50 | 95 | 98.04 | | ||
./test/fixtures/ | 100 | 100 | 100 | 100 | | ||
sigint.js | 100 | 100 | 100 | 100 | | ||
sigterm.js | 100 | 100 | 100 | 100 | | ||
--------------------|-----------|-----------|-----------|-----------| | ||
All files | 91.89 | 50 | 86.11 | 95.24 | | ||
--------------------|-----------|-----------|-----------|-----------| | ||
``` | ||
or use any reporter supported by istanbul: | ||
```bash | ||
nyc-report --reporter=lcov | ||
``` | ||
or, toss a script in your package.json: | ||
```bash | ||
{ | ||
"script": { | ||
"test": "nyc-report --reporter=text-lcov | coveralls", | ||
} | ||
} | ||
``` |
@@ -0,1 +1,3 @@ | ||
#!/usr/bin/env node | ||
process.kill(process.pid, 'SIGINT') |
@@ -0,1 +1,3 @@ | ||
#!/usr/bin/env node | ||
process.kill(process.pid, 'SIGTERM') |
@@ -1,12 +0,12 @@ | ||
/* global describe, it, afterEach, before */ | ||
/* global describe, it */ | ||
require('chai').should() | ||
var _ = require('lodash'), | ||
fs = require('fs'), | ||
spawn = require('child_process').spawn, | ||
NYC = require('../'), | ||
path = require('path'), | ||
rimraf = require('rimraf') | ||
rimraf = require('rimraf'), | ||
spawn = require('child_process').spawn | ||
require('chai').should() | ||
require('tap').mochaGlobals() | ||
describe('nyc', function () { | ||
@@ -17,6 +17,6 @@ var fixtures = path.resolve(__dirname, './fixtures') | ||
afterEach(function () { | ||
function afterEach () { | ||
delete process.env.NYC_CWD | ||
rimraf.sync(path.resolve(fixtures, './nyc_output')) | ||
}) | ||
} | ||
@@ -27,2 +27,3 @@ it('sets cwd to process.cwd() if no environment variable is set', function () { | ||
nyc.cwd.should.eql(process.cwd()) | ||
afterEach() | ||
}) | ||
@@ -36,2 +37,3 @@ | ||
nyc.cwd.should.match(/nyc\/test\/fixtures/) | ||
afterEach() | ||
}) | ||
@@ -53,9 +55,7 @@ }) | ||
before(function () { | ||
it('wraps modules with coverage counters when they are required', function () { | ||
nyc = (new NYC({ | ||
cwd: process.cwd() | ||
})).wrap() | ||
}) | ||
it('wraps modules with coverage counters when they are required', function () { | ||
// clear the module cache so that | ||
@@ -66,3 +66,3 @@ // we pull index.js in again and wrap it. | ||
// when we require index.js it shoudl be wrapped. | ||
// when we require index.js it should be wrapped. | ||
var index = require('../') | ||
@@ -72,20 +72,7 @@ index.should.match(/__cov_/) | ||
it('writes coverage report when process exits', function (done) { | ||
var proc = spawn('./bin/nyc.js', ['index.js'], { | ||
cwd: process.cwd(), | ||
env: process.env, | ||
stdio: [process.stdin, process.stdout, process.stderr] | ||
}) | ||
proc.on('close', function () { | ||
fs.readdirSync('./nyc_output').length.should.be.gte(1) | ||
return done() | ||
}) | ||
}) | ||
function testSignal (signal, done) { | ||
var proc = spawn('./bin/nyc.js', ['./test/fixtures/' + signal + '.js'], { | ||
var proc = spawn(process.execPath, ['./test/fixtures/' + signal + '.js'], { | ||
cwd: process.cwd(), | ||
env: process.env, | ||
stdio: [process.stdin, process.stdout, process.stderr] | ||
stdio: 'inherit' | ||
}) | ||
@@ -106,6 +93,2 @@ | ||
it('writes coverage report when process is killed with SIGHUP', function (done) { | ||
testSignal('sighup', done) | ||
}) | ||
it('writes coverage report when process is killed with SIGINT', function (done) { | ||
@@ -121,3 +104,3 @@ testSignal('sigint', done) | ||
}), | ||
proc = spawn('../../bin/nyc.js', ['sigterm.js'], { | ||
proc = spawn('../../bin/nyc.js', ['./sigterm.js'], { | ||
cwd: fixtures, | ||
@@ -124,0 +107,0 @@ env: process.env, |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 6 instances in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
0
57
10
12501
8
12
259
+ Addedjsonstream@^1.0.3
+ Addedjsonparse@1.0.0(transitive)
+ Addedjsonstream@1.0.3(transitive)
+ Addedspawn-wrap@0.0.9(transitive)
+ Addedthrough@2.3.8(transitive)
Updatedspawn-wrap@0.0.9