Skip to content

Commit f8a02b4

Browse files
rpominovbcoe
authored andcommitted
feat(cli): --include and --exclude are now accepted as CLI options, thanks @rpominov \o/ see #207
1 parent de5ca18 commit f8a02b4

File tree

7 files changed

+118
-3
lines changed

7 files changed

+118
-3
lines changed

bin/nyc.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ var yargs = require('yargs/yargs')(process.argv.slice(2))
4646
type: 'boolean',
4747
describe: 'whether or not to instrument all files of the project (not just the ones touched by your test suite)'
4848
})
49+
.option('exclude', {
50+
alias: 'x',
51+
default: [],
52+
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded'
53+
})
54+
.option('include', {
55+
alias: 'n',
56+
default: [],
57+
describe: 'a list of specific files that should be covered, glob patterns are supported'
58+
})
4959
.option('require', {
5060
alias: 'i',
5161
default: [],
@@ -109,9 +119,13 @@ if (argv._[0] === 'report') {
109119
// wrap subprocesses and execute argv[1]
110120
if (!Array.isArray(argv.require)) argv.require = [argv.require]
111121
if (!Array.isArray(argv.extension)) argv.extension = [argv.extension]
122+
if (!Array.isArray(argv.exclude)) argv.exclude = [argv.exclude]
123+
if (!Array.isArray(argv.include)) argv.include = [argv.include]
112124

113125
var nyc = (new NYC({
114-
require: argv.require
126+
require: argv.require,
127+
include: argv.include,
128+
exclude: argv.exclude
115129
}))
116130
nyc.reset()
117131

@@ -127,6 +141,12 @@ if (argv._[0] === 'report') {
127141
if (argv.extension.length) {
128142
env.NYC_EXTENSION = argv.extension.join(',')
129143
}
144+
if (argv.exclude.length) {
145+
env.NYC_EXCLUDE = argv.exclude.join(',')
146+
}
147+
if (argv.include.length) {
148+
env.NYC_INCLUDE = argv.include.join(',')
149+
}
130150
sw([wrapper], env)
131151

132152
// Both running the test script invocation and the check-coverage run may

bin/wrap.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ try {
99
;(new NYC({
1010
require: process.env.NYC_REQUIRE ? process.env.NYC_REQUIRE.split(',') : [],
1111
extension: process.env.NYC_EXTENSION ? process.env.NYC_EXTENSION.split(',') : [],
12+
exclude: process.env.NYC_EXCLUDE ? process.env.NYC_EXCLUDE.split(',') : [],
13+
include: process.env.NYC_INCLUDE ? process.env.NYC_INCLUDE.split(',') : [],
1214
enableCache: process.env.NYC_CACHE === 'enable'
1315
})).wrap()
1416

index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ function NYC (opts) {
3737

3838
// load exclude stanza from config.
3939
this.include = false
40-
if (config.include) {
40+
if (config.include && config.include.length > 0) {
4141
this.include = this._prepGlobPatterns(arrify(config.include))
4242
}
4343

4444
this.exclude = this._prepGlobPatterns(
45-
['**/node_modules/**'].concat(arrify(config.exclude || ['test/**', 'test{,-*}.js', '**/*.test.js', '**/__tests__/**']))
45+
['**/node_modules/**'].concat(arrify(
46+
config.exclude && config.exclude.length > 0
47+
? config.exclude
48+
: ['test/**', 'test{,-*}.js', '**/*.test.js', '**/__tests__/**']
49+
))
4650
)
4751

4852
this.cacheDirectory = findCacheDir({name: 'nyc', cwd: this.cwd})

test/fixtures/cli/test.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// should be excluded by default.

test/fixtures/conf-empty/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "nyc",
3+
"version": "1.1.1",
4+
"description": "forking code-coverage using istanbul.",
5+
"main": "index.js"
6+
}

test/src/nyc-bin.js

+52
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,65 @@ var isWindows = require('is-windows')()
77
var fixturesCLI = path.resolve(__dirname, '../fixtures/cli')
88
var fakebin = path.resolve(fixturesCLI, 'fakebin')
99
var bin = path.resolve(__dirname, '../../bin/nyc')
10+
var rimraf = require('rimraf')
1011

1112
require('chai').should()
1213
require('tap').mochaGlobals()
1314

15+
// beforeEach
16+
rimraf.sync(path.resolve(fakebin, 'node'))
17+
rimraf.sync(path.resolve(fakebin, 'npm'))
18+
1419
describe('the nyc cli', function () {
1520
var env = { PATH: process.env.PATH }
1621

22+
describe('--include', function () {
23+
it('can be used to limit bin to instrumenting specific files', function (done) {
24+
var args = [bin, '--all', '--include', 'half-covered.js', process.execPath, './half-covered.js']
25+
26+
var proc = spawn(process.execPath, args, {
27+
cwd: fixturesCLI,
28+
env: env
29+
})
30+
31+
var stdout = ''
32+
proc.stdout.on('data', function (chunk) {
33+
stdout += chunk
34+
})
35+
36+
proc.on('close', function (code) {
37+
code.should.equal(0)
38+
stdout.should.match(/half-covered\.js/)
39+
stdout.should.not.match(/half-covered-failing\.js/)
40+
stdout.should.not.match(/test\.js/)
41+
done()
42+
})
43+
})
44+
})
45+
46+
describe('--exclude', function () {
47+
it('should allow default exclude rules to be overridden', function (done) {
48+
var args = [bin, '--all', '--exclude', '**/half-covered.js', process.execPath, './half-covered.js']
49+
50+
var proc = spawn(process.execPath, args, {
51+
cwd: fixturesCLI,
52+
env: env
53+
})
54+
55+
var stdout = ''
56+
proc.stdout.on('data', function (chunk) {
57+
stdout += chunk
58+
})
59+
60+
proc.on('close', function (code) {
61+
code.should.equal(0)
62+
stdout.should.not.match(/half-covered\.js/)
63+
stdout.should.match(/test\.js/)
64+
done()
65+
})
66+
})
67+
})
68+
1769
describe('--check-coverage', function () {
1870
it('fails when the expected coverage is below a threshold', function (done) {
1971
var args = [bin, '--check-coverage', '--lines', '51', process.execPath, './half-covered.js']

test/src/nyc-test.js

+30
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ describe('nyc', function () {
7979

8080
nyc.extensions.length.should.eql(3)
8181
})
82+
83+
it("ignores 'include' option if it's falsy or []", function () {
84+
var nyc1 = new NYC({
85+
cwd: path.resolve(__dirname, '../fixtures/conf-empty')
86+
})
87+
88+
nyc1.include.should.equal(false)
89+
90+
var nyc2 = new NYC({
91+
cwd: path.resolve(__dirname, '../fixtures/conf-empty'),
92+
include: []
93+
})
94+
95+
nyc2.include.should.equal(false)
96+
})
97+
98+
it("ignores 'exclude' option if it's falsy or []", function () {
99+
var nyc1 = new NYC({
100+
cwd: path.resolve(__dirname, '../fixtures/conf-empty')
101+
})
102+
103+
nyc1.exclude.length.should.eql(7)
104+
105+
var nyc2 = new NYC({
106+
cwd: path.resolve(__dirname, '../fixtures/conf-empty'),
107+
exclude: []
108+
})
109+
110+
nyc2.exclude.length.should.eql(7)
111+
})
82112
})
83113

84114
describe('_prepGlobPatterns', function () {

0 commit comments

Comments
 (0)