Skip to content

Commit

Permalink
Merge pull request #6 from bcoe/new-spawn-wrap
Browse files Browse the repository at this point in the history
things are actually close to working now \o/
  • Loading branch information
bcoe committed May 15, 2015
2 parents 610e7c0 + b0671ac commit 174750a
Show file tree
Hide file tree
Showing 33 changed files with 129 additions and 1,051 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nyc_output
coverage
node_modules
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,53 @@
[![Coverage Status](https://coveralls.io/repos/bcoe/nyc/badge.svg?branch=)](https://coveralls.io/r/bcoe/nyc?branch=)
[![NPM version](https://img.shields.io/npm/v/nyc.svg)](https://www.npmjs.com/package/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",
}
}
```
2 changes: 1 addition & 1 deletion bin/nyc-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var NYC = require('../'),
})
.help('h')
.alias('h', 'help')
.epilog('github.com/gotwarlost/istanbul for available reporters')
.epilog('http://github.com/gotwarlost/istanbul for available reporters')
.argv

process.env.NYC_CWD = argv['cwd']
Expand Down
48 changes: 30 additions & 18 deletions bin/nyc.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
#!/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 changes: 20 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* global __coverage__ */

var _ = require('lodash'),
fs = require('fs'),
istanbul = require('istanbul'),
instrumenter = new istanbul.Instrumenter(),
mkdirp = require('mkdirp'),
path = require('path'),
rimraf = require('rimraf'),
spawnWrap = require('spawn-wrap'),
stripBom = require('strip-bom')

function NYC (opts) {
Expand All @@ -28,10 +29,13 @@ function NYC (opts) {
return new RegExp(p)
})

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 () {
var _this = this

Expand Down Expand Up @@ -59,27 +63,30 @@ NYC.prototype._wrapRequire = function () {

NYC.prototype._wrapExit = function () {
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'
)
}

// 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()
this._wrapExit()
return this
Expand Down
15 changes: 0 additions & 15 deletions node_modules/spawn-wrap/LICENSE

This file was deleted.

42 changes: 0 additions & 42 deletions node_modules/spawn-wrap/README.md

This file was deleted.

134 changes: 0 additions & 134 deletions node_modules/spawn-wrap/index.js

This file was deleted.

1 change: 0 additions & 1 deletion node_modules/spawn-wrap/node_modules/.bin/which

This file was deleted.

Loading

0 comments on commit 174750a

Please sign in to comment.