diff --git a/bin/nyc-report.js b/bin/nyc-report.js index e94dd49be..3edb01631 100755 --- a/bin/nyc-report.js +++ b/bin/nyc-report.js @@ -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() diff --git a/bin/nyc.js b/bin/nyc.js index f4495eea0..e182cf1f9 100755 --- a/bin/nyc.js +++ b/bin/nyc.js @@ -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() @@ -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 diff --git a/index.js b/index.js old mode 100644 new mode 100755 index ff733407e..93021dd9d --- a/index.js +++ b/index.js @@ -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, { @@ -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()) } @@ -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) } @@ -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) } @@ -130,8 +131,6 @@ NYC.prototype.report = function () { )) }) - console.log(_this.tmpDirectory()) - reports.forEach(function (report) { collector.add(report) }) diff --git a/package.json b/package.json index c6a440371..0f8da1f38 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nyc", - "version": "1.0.0", + "version": "1.0.1", "description": "forking code-coverage using istanbul.", "main": "index.js", "scripts": { @@ -12,8 +12,8 @@ }, "config": { "nyc": { - "pattern": [ - "" + "exclude": [ + "node_modules\/" ] } }, diff --git a/test/fixtures/package.json b/test/fixtures/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/fixtures/package.json @@ -0,0 +1 @@ +{} diff --git a/test/nyc-test.js b/test/nyc-test.js index 0c2c0ca97..ec80ee76f 100644 --- a/test/nyc-test.js +++ b/test/nyc-test.js @@ -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) }) })