Skip to content

Commit 53c4234

Browse files
authored
feat: introduce --exclude-after-remap flag (#293)
Allow exclude logic to be passed to v8-to-istanbul, such that it is applied after source maps. Fixes #224
1 parent 30a8d0f commit 53c4234

File tree

6 files changed

+93
-37
lines changed

6 files changed

+93
-37
lines changed

lib/commands/report.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ exports.outputReport = async function (argv) {
1313
const report = Report({
1414
include: argv.include,
1515
exclude: argv.exclude,
16+
excludeAfterRemap: argv.excludeAfterRemap,
1617
reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter],
1718
reportsDirectory: argv['reports-dir'],
1819
tempDirectory: argv.tempDirectory,

lib/parse-args.js

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ function buildYargs (withCommands = false) {
2626
default: defaultExclude,
2727
describe: 'a list of specific files and directories that should be excluded from coverage (glob patterns are supported)'
2828
})
29+
.option('exclude-after-remap', {
30+
alias: 'a',
31+
type: 'boolean',
32+
default: false,
33+
describe: 'apply exclude logic to files after they are remapped by a source-map'
34+
})
2935
.option('include', {
3036
alias: 'n',
3137
default: [],

lib/report.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const debuglog = util.debuglog('c8')
1515
class Report {
1616
constructor ({
1717
exclude,
18+
excludeAfterRemap,
1819
include,
1920
reporter,
2021
reportsDirectory,
@@ -38,6 +39,7 @@ class Report {
3839
include: include,
3940
relativePath: !allowExternal
4041
})
42+
this.excludeAfterRemap = excludeAfterRemap
4143
this.omitRelative = omitRelative
4244
this.sourceMapCache = {}
4345
this.wrapperLength = wrapperLength
@@ -88,7 +90,11 @@ class Report {
8890
try {
8991
const sources = this._getSourceMap(v8ScriptCov)
9092
const path = resolve(this.resolve, v8ScriptCov.url)
91-
const converter = v8toIstanbul(path, this.wrapperLength, sources)
93+
const converter = v8toIstanbul(path, this.wrapperLength, sources, (path) => {
94+
if (this.excludeAfterRemap) {
95+
return !this.exclude.shouldInstrument(path)
96+
}
97+
})
9298
await converter.load()
9399

94100
if (resultCountPerPath.has(path)) {
@@ -276,9 +282,10 @@ class Report {
276282
continue
277283
}
278284
}
279-
if (this.exclude.shouldInstrument(v8ScriptCov.url) &&
280-
(!this.omitRelative || isAbsolute(v8ScriptCov.url))) {
281-
result.push(v8ScriptCov)
285+
if ((!this.omitRelative || isAbsolute(v8ScriptCov.url))) {
286+
if (this.excludeAfterRemap || this.exclude.shouldInstrument(v8ScriptCov.url)) {
287+
result.push(v8ScriptCov)
288+
}
282289
}
283290
}
284291
return { result }

test/integration.js

+16
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,20 @@ describe('c8', () => {
588588
])
589589
output.toString('utf8').should.matchSnapshot()
590590
})
591+
592+
describe('--exclude-after-remap', () => {
593+
it('applies exclude rules after source-maps are applied', () => {
594+
const { output } = spawnSync(nodePath, [
595+
c8Path,
596+
'--exclude="test/*.js"',
597+
'--exclude="**/branch-1.js"',
598+
'--exclude-after-remap',
599+
'--temp-directory=tmp/source-map',
600+
'--clean=true',
601+
nodePath,
602+
require.resolve('./fixtures/source-maps/branches/branches.rollup.js')
603+
])
604+
output.toString('utf8').should.matchSnapshot()
605+
})
606+
})
591607
})

test/integration.js.snap

+13
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ ERROR: Coverage for statements (64.29%) does not meet global threshold (95%)
112112
"
113113
`;
114114

115+
exports[`c8 --exclude-after-remap applies exclude rules after source-maps are applied 1`] = `
116+
",reachable
117+
a = true
118+
a = false
119+
-------------|---------|----------|---------|---------|-------------------
120+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
121+
-------------|---------|----------|---------|---------|-------------------
122+
All files | 100 | 100 | 100 | 100 |
123+
branch-2.js | 100 | 100 | 100 | 100 |
124+
-------------|---------|----------|---------|---------|-------------------
125+
,"
126+
`;
127+
115128
exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
116129
",bar foo
117130
------------|---------|----------|---------|---------|-------------------

test/integration.js_10.snap

+46-33
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ ERROR: Coverage for statements (64.29%) does not meet global threshold (95%)
112112
"
113113
`;
114114

115+
exports[`c8 --exclude-after-remap applies exclude rules after source-maps are applied 1`] = `
116+
",reachable
117+
a = true
118+
a = false
119+
-------------|---------|----------|---------|---------|-------------------
120+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
121+
-------------|---------|----------|---------|---------|-------------------
122+
All files | 100 | 100 | 100 | 100 |
123+
branch-2.js | 100 | 100 | 100 | 100 |
124+
-------------|---------|----------|---------|---------|-------------------
125+
,"
126+
`;
127+
115128
exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
116129
",----------|---------|----------|---------|---------|-------------------
117130
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
@@ -139,24 +152,24 @@ hey
139152
--------------------------|---------|----------|---------|---------|--------------------------------
140153
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
141154
--------------------------|---------|----------|---------|---------|--------------------------------
142-
All files | 72.94 | 58.23 | 61.11 | 72.94 |
155+
All files | 73.17 | 58.02 | 62.16 | 73.17 |
143156
bin | 78.85 | 60 | 66.67 | 78.85 |
144157
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
145-
lib | 76.25 | 51.85 | 71.43 | 76.25 |
158+
lib | 76.44 | 51.79 | 72.73 | 76.44 |
146159
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
147-
parse-args.js | 96.25 | 45.45 | 100 | 96.25 | 114-115,123-124,137-138
148-
report.js | 75.52 | 58.82 | 83.33 | 75.52 | ...210,240-241,268-269,275-277
160+
parse-args.js | 96.39 | 45.45 | 100 | 96.39 | 120-121,129-130,143-144
161+
report.js | 75.42 | 58.33 | 84.62 | 75.42 | ...216,246-247,274-275,281-283
149162
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
150-
lib/commands | 45.05 | 75 | 16.67 | 45.05 |
163+
lib/commands | 45.65 | 75 | 16.67 | 45.65 |
151164
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
152-
report.js | 93.33 | 71.43 | 50 | 93.33 | 9-10
165+
report.js | 93.55 | 71.43 | 50 | 93.55 | 9-10
153166
test/fixtures | 83.33 | 85.71 | 66.67 | 83.33 |
154167
async.js | 100 | 100 | 100 | 100 |
155168
normal.js | 75 | 66.67 | 33.33 | 75 | 14-16,18-20
156169
--------------------------|---------|----------|---------|---------|--------------------------------
157-
,ERROR: Coverage for lines (72.94%) does not meet global threshold (101%)
158-
ERROR: Coverage for branches (58.23%) does not meet global threshold (82%)
159-
ERROR: Coverage for statements (72.94%) does not meet global threshold (95%)
170+
,ERROR: Coverage for lines (73.17%) does not meet global threshold (101%)
171+
ERROR: Coverage for branches (58.02%) does not meet global threshold (82%)
172+
ERROR: Coverage for statements (73.17%) does not meet global threshold (95%)
160173
"
161174
`;
162175

@@ -166,17 +179,17 @@ ERROR: Coverage for branches (60%) does not meet threshold (82%) for bin/c8.js
166179
ERROR: Coverage for statements (78.85%) does not meet threshold (95%) for bin/c8.js
167180
ERROR: Coverage for lines (21.31%) does not meet threshold (101%) for lib/commands/check-coverage.js
168181
ERROR: Coverage for statements (21.31%) does not meet threshold (95%) for lib/commands/check-coverage.js
169-
ERROR: Coverage for lines (93.33%) does not meet threshold (101%) for lib/commands/report.js
182+
ERROR: Coverage for lines (93.55%) does not meet threshold (101%) for lib/commands/report.js
170183
ERROR: Coverage for branches (71.43%) does not meet threshold (82%) for lib/commands/report.js
171-
ERROR: Coverage for statements (93.33%) does not meet threshold (95%) for lib/commands/report.js
184+
ERROR: Coverage for statements (93.55%) does not meet threshold (95%) for lib/commands/report.js
172185
ERROR: Coverage for lines (90%) does not meet threshold (101%) for lib/is-cjs-esm-bridge.js
173186
ERROR: Coverage for branches (25%) does not meet threshold (82%) for lib/is-cjs-esm-bridge.js
174187
ERROR: Coverage for statements (90%) does not meet threshold (95%) for lib/is-cjs-esm-bridge.js
175-
ERROR: Coverage for lines (96.25%) does not meet threshold (101%) for lib/parse-args.js
188+
ERROR: Coverage for lines (96.39%) does not meet threshold (101%) for lib/parse-args.js
176189
ERROR: Coverage for branches (45.45%) does not meet threshold (82%) for lib/parse-args.js
177-
ERROR: Coverage for lines (75.52%) does not meet threshold (101%) for lib/report.js
178-
ERROR: Coverage for branches (58.82%) does not meet threshold (82%) for lib/report.js
179-
ERROR: Coverage for statements (75.52%) does not meet threshold (95%) for lib/report.js
190+
ERROR: Coverage for lines (75.42%) does not meet threshold (101%) for lib/report.js
191+
ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/report.js
192+
ERROR: Coverage for statements (75.42%) does not meet threshold (95%) for lib/report.js
180193
ERROR: Coverage for lines (45%) does not meet threshold (101%) for lib/source-map-from-file.js
181194
ERROR: Coverage for statements (45%) does not meet threshold (95%) for lib/source-map-from-file.js
182195
ERROR: Coverage for lines (100%) does not meet threshold (101%) for test/fixtures/async.js
@@ -189,9 +202,9 @@ ERROR: Coverage for statements (75%) does not meet threshold (95%) for test/fixt
189202
exports[`c8 check-coverage exits with 0 if coverage within threshold 1`] = `",,"`;
190203

191204
exports[`c8 check-coverage exits with 1 if coverage is below threshold 1`] = `
192-
",,ERROR: Coverage for lines (72.94%) does not meet global threshold (101%)
193-
ERROR: Coverage for branches (58.23%) does not meet global threshold (82%)
194-
ERROR: Coverage for statements (72.94%) does not meet global threshold (95%)
205+
",,ERROR: Coverage for lines (73.17%) does not meet global threshold (101%)
206+
ERROR: Coverage for branches (58.02%) does not meet global threshold (82%)
207+
ERROR: Coverage for statements (73.17%) does not meet global threshold (95%)
195208
"
196209
`;
197210

@@ -274,17 +287,17 @@ exports[`c8 report generates report from existing temporary files 1`] = `
274287
",--------------------------|---------|----------|---------|---------|--------------------------------
275288
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
276289
--------------------------|---------|----------|---------|---------|--------------------------------
277-
All files | 72.94 | 58.23 | 61.11 | 72.94 |
290+
All files | 73.17 | 58.02 | 62.16 | 73.17 |
278291
bin | 78.85 | 60 | 66.67 | 78.85 |
279292
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
280-
lib | 76.25 | 51.85 | 71.43 | 76.25 |
293+
lib | 76.44 | 51.79 | 72.73 | 76.44 |
281294
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
282-
parse-args.js | 96.25 | 45.45 | 100 | 96.25 | 114-115,123-124,137-138
283-
report.js | 75.52 | 58.82 | 83.33 | 75.52 | ...210,240-241,268-269,275-277
295+
parse-args.js | 96.39 | 45.45 | 100 | 96.39 | 120-121,129-130,143-144
296+
report.js | 75.42 | 58.33 | 84.62 | 75.42 | ...216,246-247,274-275,281-283
284297
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
285-
lib/commands | 45.05 | 75 | 16.67 | 45.05 |
298+
lib/commands | 45.65 | 75 | 16.67 | 45.65 |
286299
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
287-
report.js | 93.33 | 71.43 | 50 | 93.33 | 9-10
300+
report.js | 93.55 | 71.43 | 50 | 93.55 | 9-10
288301
test/fixtures | 83.33 | 85.71 | 66.67 | 83.33 |
289302
async.js | 100 | 100 | 100 | 100 |
290303
normal.js | 75 | 66.67 | 33.33 | 75 | 14-16,18-20
@@ -296,24 +309,24 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = `
296309
",--------------------------|---------|----------|---------|---------|--------------------------------
297310
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
298311
--------------------------|---------|----------|---------|---------|--------------------------------
299-
All files | 72.94 | 58.23 | 61.11 | 72.94 |
312+
All files | 73.17 | 58.02 | 62.16 | 73.17 |
300313
bin | 78.85 | 60 | 66.67 | 78.85 |
301314
c8.js | 78.85 | 60 | 66.67 | 78.85 | 22,27-29,32-33,41-43,50-51
302-
lib | 76.25 | 51.85 | 71.43 | 76.25 |
315+
lib | 76.44 | 51.79 | 72.73 | 76.44 |
303316
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
304-
parse-args.js | 96.25 | 45.45 | 100 | 96.25 | 114-115,123-124,137-138
305-
report.js | 75.52 | 58.82 | 83.33 | 75.52 | ...210,240-241,268-269,275-277
317+
parse-args.js | 96.39 | 45.45 | 100 | 96.39 | 120-121,129-130,143-144
318+
report.js | 75.42 | 58.33 | 84.62 | 75.42 | ...216,246-247,274-275,281-283
306319
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
307-
lib/commands | 45.05 | 75 | 16.67 | 45.05 |
320+
lib/commands | 45.65 | 75 | 16.67 | 45.65 |
308321
check-coverage.js | 21.31 | 100 | 0 | 21.31 | 9-11,14-27,30-44,46-61
309-
report.js | 93.33 | 71.43 | 50 | 93.33 | 9-10
322+
report.js | 93.55 | 71.43 | 50 | 93.55 | 9-10
310323
test/fixtures | 83.33 | 85.71 | 66.67 | 83.33 |
311324
async.js | 100 | 100 | 100 | 100 |
312325
normal.js | 75 | 66.67 | 33.33 | 75 | 14-16,18-20
313326
--------------------------|---------|----------|---------|---------|--------------------------------
314-
,ERROR: Coverage for lines (72.94%) does not meet global threshold (101%)
315-
ERROR: Coverage for branches (58.23%) does not meet global threshold (82%)
316-
ERROR: Coverage for statements (72.94%) does not meet global threshold (95%)
327+
,ERROR: Coverage for lines (73.17%) does not meet global threshold (101%)
328+
ERROR: Coverage for branches (58.02%) does not meet global threshold (82%)
329+
ERROR: Coverage for statements (73.17%) does not meet global threshold (95%)
317330
"
318331
`;
319332

0 commit comments

Comments
 (0)