Skip to content

Commit

Permalink
Merge pull request #1 from bcoe/cwd-feature
Browse files Browse the repository at this point in the history
we now pass cwd around using env
  • Loading branch information
bcoe committed May 9, 2015
2 parents 46ae9f6 + d8956f1 commit beb31e1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 46 deletions.
6 changes: 3 additions & 3 deletions bin/nyc-report.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

process.env.NYC_CWD = process.cwd()

var NYC = require('../'),
nyc = new NYC({
cwd: process.cwd()
})
nyc = new NYC()

nyc.report()
13 changes: 3 additions & 10 deletions bin/nyc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#!/usr/bin/env node

var _ = require('lodash'),
fs = require('fs'),
NYC = require('../'),
nyc = new NYC({
cwd: '/Users/benjamincoe/bcoe/node-tap/'
})
var NYC = require('../'),
nyc = new NYC()

nyc.wrap()

Expand All @@ -15,10 +11,7 @@ var name = require.resolve('../')
delete require.cache[name]

// hide the fact that nyc.js was used to execute command.
fs.appendFileSync('/Users/benjamincoe/output.log', JSON.stringify(process.argv) + '\n', 'utf-8')
process.argv = _.filter(process.argv, function (arg) {
return !arg.match(/(nyc.js$)|(nyc$)|(nyc-sub.js$)|(nyc-sub$)/)
})
if (process.argv[1].match((/(nyc.js$)|(nyc$)/))) process.argv.splice(1, 1)

// execute main on the file passed to nyc:
// ./bin/nyc.js ./node_modules/.bin/mocha
Expand Down
49 changes: 24 additions & 25 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var _ = require('lodash'),
istanbul = require('istanbul'),
instrumenter = new istanbul.Instrumenter(),
mkdirp = require('mkdirp'),
path = require('path')
// rimraf = require('rimraf')
path = require('path'),
rimraf = require('rimraf')

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

// set config in config.nyc stanza of package.json.
if (!this.cwd) throw Error('can no find file', process.cwd())
// don't have a plan for loading config yet, we should pass
// around the cwd using ENV, and use the ENV for config/package.json
var config = require(path.resolve(this.cwd, './package.json')).config.nyc

// which files should we apply coverage to?
this.pattern = config.pattern || ['lib', 'index.js']
if (!Array.isArray(this.pattern)) this.pattern = [this.pattern]
this.pattern = _.map(this.pattern, function (p) {
return new RegExp('^' + p + '.*')
var config = require(path.resolve(this.cwd, './package.json')).config || {}
config = config.nyc || {}

this.exclude = config.exclude || ['node_modules\/', 'test\/']
if (!Array.isArray(this.exclude)) this.exclude = [this.exclude]
this.exclude = _.map(this.exclude, function (p) {
return new RegExp(p)
})

if (!process.env.NYC_CWD) rimraf.sync(this.tmpDirectory())
mkdirp.sync(this.tmpDirectory())
}

Expand All @@ -46,10 +43,10 @@ NYC.prototype.wrapSpawn = function (_child /* for mocking in tests */) {
if (arg === options.file) return _this.subprocessBin
else return arg
})

options.envPairs.push('NYC_CWD=' + _this.cwd)
options.args.unshift(process.execPath)
}
// node fakeamabob test/foo.js
// require('fs').appendFileSync('/Users/benjamincoe/output.log', 'Attempt Spawn: ' + JSON.stringify(options) + '\n', 'utf-8')

return spawn.call(this, options)
}
Expand All @@ -76,18 +73,22 @@ NYC.prototype.wrapRequire = function () {

// any JS you require should get coverage added.
require.extensions['.js'] = function (module, filename) {
var instrument = false
var instrument = true
var content = fs.readFileSync(filename, 'utf8')

// only instrument the file if it matches our coverage pattern.
// only instrument a file if it's not on the exclude list.
var relFile = path.relative(_this.cwd, filename)
for (var i = 0, pattern; (pattern = _this.pattern[i]) !== undefined; i++) {
if (pattern.test(relFile)) {
instrument = true
}
for (var i = 0, exclude; (exclude = _this.exclude[i]) !== undefined; i++) {
if (exclude.test(relFile)) instrument = false
}

if (instrument) {
content = instrumenter.instrumentSync(
content,
'/' + relFile
)
}

if (instrument) content = instrumenter.instrumentSync(content, filename)
module._compile(stripBOM(content), filename)
}

Expand Down Expand Up @@ -130,8 +131,6 @@ NYC.prototype.report = function () {
))
})

console.log(_this.tmpDirectory())

reports.forEach(function (report) {
collector.add(report)
})
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nyc",
"version": "1.0.0",
"version": "1.0.1",
"description": "forking code-coverage using istanbul.",
"main": "index.js",
"scripts": {
Expand All @@ -12,8 +12,8 @@
},
"config": {
"nyc": {
"pattern": [
""
"exclude": [
"node_modules\/"
]
}
},
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
31 changes: 26 additions & 5 deletions test/nyc-test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
/* global describe, it */
/* global describe, it, afterEach */

require('chai').should()

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

describe('nyc', function () {
describe('pattern', function () {
it('loads pattern from package.json in cwd', function () {
describe('cwd', function () {
afterEach(function () {
delete process.env.NYC_CWD
})

it('sets cwd to process.cwd() if no environment variable set', function () {
var nyc = new NYC()

nyc.cwd.should.eql(process.cwd())
})

it('uses NYC_CWD environment variable for cwd if it is set', function () {
process.env.NYC_CWD = path.resolve(__dirname, './fixtures')

var nyc = new NYC()

nyc.cwd.should.match(/nyc\/test\/fixtures/)
})
})

describe('exclude', function () {
it('loads exclude patterns from package.json in cwd', function () {
var nyc = new NYC()

nyc.pattern.length.should.eql(1)
nyc.exclude.length.should.eql(1)
})
})

Expand Down

0 comments on commit beb31e1

Please sign in to comment.