Skip to content

Commit

Permalink
pulled in isaac's spawn-wrap module
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed May 10, 2015
1 parent beb31e1 commit 6c3f8a6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
nyc_output
coverage
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# nyc

forking code-coverage using istanbul.
a code coverage reporter built on [istanbul](https://www.npmjs.com/package/istanbul)
that works well for applications that spawn child processes.
26 changes: 21 additions & 5 deletions bin/nyc-report.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
#!/usr/bin/env node

process.env.NYC_CWD = process.cwd()

var NYC = require('../'),
nyc = new NYC()
argv = require('yargs')
.usage('$0 [options]')
.option('d', {
alias: 'cwd',
default: process.cwd(),
describe: 'root directory that contains the nyc_output folder'
})
.option('r', {
alias: 'reporter',
describe: 'coverage reporter to use',
default: 'text'
})
.help('h')
.alias('h', 'help')
.epilog('github.com/gotwarlost/istanbul for available reporters')
.argv

process.env.NYC_CWD = argv['cwd']

nyc.report()
;(new NYC({
reporter: argv.reporter
})).report()
5 changes: 2 additions & 3 deletions bin/nyc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env node

var NYC = require('../'),
nyc = new NYC()
var NYC = require('../')

nyc.wrap()
;(new NYC()).wrap()

// make it so we can run coverage on nyc.
// turtles all the way down.
Expand Down
55 changes: 8 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var _ = require('lodash'),
cp = require('child_process'),
fs = require('fs'),
istanbul = require('istanbul'),
instrumenter = new istanbul.Instrumenter(),
mkdirp = require('mkdirp'),
path = require('path'),
rimraf = require('rimraf')
rimraf = require('rimraf'),
spawnWrap = require('spawn-wrap')

function NYC (opts) {
_.extend(this, {
Expand All @@ -14,7 +14,8 @@ function NYC (opts) {
'./bin/nyc.js'
),
tempDirectory: './nyc_output',
cwd: process.env.NYC_CWD || process.cwd()
cwd: process.env.NYC_CWD || process.cwd(),
reporter: 'text'
}, opts)

var config = require(path.resolve(this.cwd, './package.json')).config || {}
Expand All @@ -30,44 +31,6 @@ function NYC (opts) {
mkdirp.sync(this.tmpDirectory())
}

NYC.prototype.wrapSpawn = function (_child /* for mocking in tests */) {
var _this = this,
child = _child || cp.spawn('echo', []),
ChildProcess = child.constructor

var spawn = ChildProcess.prototype.spawn
ChildProcess.prototype.spawn = function (options) {
if (path.basename(options.file) === 'node' ||
path.basename(options.file) === 'iojs') {
options.args = _.map(options.args, function (arg) {
if (arg === options.file) return _this.subprocessBin
else return arg
})

options.envPairs.push('NYC_CWD=' + _this.cwd)
options.args.unshift(process.execPath)
}

return spawn.call(this, options)
}

// handle cp.exec('node foo.js')
var exec = cp.exec
cp.exec = function (command/*...*/) {
var b = command.trim().split(/\s+/)
if (path.basename(b[0]) === 'node' ||
path.basename(b[0]) === 'iojs') {
b[0] = this.subprocessBin
}

var args = [b.join(' ')]
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i])
}
return exec.apply(cp, args)
}
}

NYC.prototype.wrapRequire = function () {
var _this = this

Expand All @@ -85,7 +48,7 @@ NYC.prototype.wrapRequire = function () {
if (instrument) {
content = instrumenter.instrumentSync(
content,
'/' + relFile
'./' + relFile
)
}

Expand Down Expand Up @@ -115,7 +78,7 @@ function stripBOM (content) {
}

NYC.prototype.wrap = function () {
this.wrapSpawn()
spawnWrap([this.subprocessBin], {NYC_CWD: this.cwd})
this.wrapRequire()
}

Expand All @@ -135,11 +98,9 @@ NYC.prototype.report = function () {
collector.add(report)
})

reporter.add('text')
reporter.add(this.reporter)

reporter.write(collector, true, function () {
console.log('All reports generated')
})
reporter.write(collector, true, function () {})
}

NYC.prototype.tmpDirectory = function () {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"config": {
"nyc": {
"exclude": [
"node_modules\/"
"node_modules/"
]
}
},
Expand All @@ -28,6 +28,7 @@
"lodash": "^3.8.0",
"mkdirp": "^0.5.0",
"rimraf": "^2.3.3",
"spawn-wrap": "0.0.0",
"yargs": "^3.8.0"
},
"devDependencies": {
Expand Down
34 changes: 3 additions & 31 deletions test/nyc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

require('chai').should()

var cp = require('child_process'),
NYC = require('../'),
var NYC = require('../'),
path = require('path')

describe('nyc', function () {
Expand Down Expand Up @@ -35,36 +34,9 @@ describe('nyc', function () {
})
})

describe('wrapSpawn', function () {
var child = cp.spawn('echo', [])
/*describe('wrapSpawn', function () {
it('wraps spawn() and replaces node/iojs with subprocessBin', function (done) {
var ChildProcess = child.constructor,
nyc = new NYC()

ChildProcess.prototype.spawn = function (options) {
options.args.length.should.equal(2)
options.args[1].should.equal(nyc.subprocessBin)
return done()
}

nyc.wrapSpawn(child)

cp.spawn('node', {})
})

it('wraps exec() and replaces node/iojs', function (done) {
var nyc = new NYC()

cp.exec = function (command) {
return done()
}

nyc.wrapSpawn(child)

cp.exec('node foo bar')
})
})
})*/

describe('wrapRequire', function () {
it('uses istanbul to wrap modules when required', function () {
Expand Down

0 comments on commit 6c3f8a6

Please sign in to comment.