Skip to content

Commit

Permalink
feat: introduce --exclude-after-remap flag
Browse files Browse the repository at this point in the history
Allow exclude logic to be passed to v8-to-istanbul, such that it is applied after
source maps.

Fixes #224
  • Loading branch information
bcoe committed Mar 30, 2021
1 parent 9fd7b46 commit 2f57a5d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/commands/report.js
Expand Up @@ -13,6 +13,7 @@ exports.outputReport = async function (argv) {
const report = Report({
include: argv.include,
exclude: argv.exclude,
excludeAfterRemap: argv.excludeAfterRemap,
reporter: Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter],
reportsDirectory: argv['reports-dir'],
tempDirectory: argv.tempDirectory,
Expand Down
6 changes: 6 additions & 0 deletions lib/parse-args.js
Expand Up @@ -26,6 +26,12 @@ function buildYargs (withCommands = false) {
default: defaultExclude,
describe: 'a list of specific files and directories that should be excluded from coverage (glob patterns are supported)'
})
.option('exclude-after-remap', {
alias: 'a',
type: 'boolean',
default: false,
describe: 'apply exclude logic to files after they are remapped by a source-map'
})
.option('include', {
alias: 'n',
default: [],
Expand Down
15 changes: 11 additions & 4 deletions lib/report.js
Expand Up @@ -15,6 +15,7 @@ const debuglog = util.debuglog('c8')
class Report {
constructor ({
exclude,
excludeAfterRemap,
include,
reporter,
reportsDirectory,
Expand All @@ -38,6 +39,7 @@ class Report {
include: include,
relativePath: !allowExternal
})
this.excludeAfterRemap = excludeAfterRemap
this.omitRelative = omitRelative
this.sourceMapCache = {}
this.wrapperLength = wrapperLength
Expand Down Expand Up @@ -88,7 +90,11 @@ class Report {
try {
const sources = this._getSourceMap(v8ScriptCov)
const path = resolve(this.resolve, v8ScriptCov.url)
const converter = v8toIstanbul(path, this.wrapperLength, sources)
const converter = v8toIstanbul(path, this.wrapperLength, sources, (path) => {
if (this.excludeAfterRemap) {
return !this.exclude.shouldInstrument(path)
}
})
await converter.load()

if (resultCountPerPath.has(path)) {
Expand Down Expand Up @@ -276,9 +282,10 @@ class Report {
continue
}
}
if (this.exclude.shouldInstrument(v8ScriptCov.url) &&
(!this.omitRelative || isAbsolute(v8ScriptCov.url))) {
result.push(v8ScriptCov)
if ((!this.omitRelative || isAbsolute(v8ScriptCov.url))) {
if (this.excludeAfterRemap || this.exclude.shouldInstrument(v8ScriptCov.url)) {
result.push(v8ScriptCov)
}
}
}
return { result }
Expand Down
16 changes: 16 additions & 0 deletions test/integration.js
Expand Up @@ -588,4 +588,20 @@ describe('c8', () => {
])
output.toString('utf8').should.matchSnapshot()
})

describe('--exclude-after-remap', () => {
it('applies exclude rules after source-maps are applied', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--exclude="**/branch-1.js"',
'--exclude-after-remap',
'--temp-directory=tmp/source-map',
'--clean=true',
nodePath,
require.resolve('./fixtures/source-maps/branches/branches.rollup.js')
])
output.toString('utf8').should.matchSnapshot()
})
})
})
13 changes: 13 additions & 0 deletions test/integration.js.snap
Expand Up @@ -112,6 +112,19 @@ ERROR: Coverage for statements (64.29%) does not meet global threshold (95%)
"
`;

exports[`c8 --exclude-after-remap applies exclude rules after source-maps are applied 1`] = `
",reachable
a = true
a = false
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
branch-2.js | 100 | 100 | 100 | 100 |
-------------|---------|----------|---------|---------|-------------------
,"
`;

exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
",bar foo
------------|---------|----------|---------|---------|-------------------
Expand Down
13 changes: 13 additions & 0 deletions test/integration.js_10.snap
Expand Up @@ -112,6 +112,19 @@ ERROR: Coverage for statements (64.29%) does not meet global threshold (95%)
"
`;

exports[`c8 --exclude-after-remap applies exclude rules after source-maps are applied 1`] = `
",reachable
a = true
a = false
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
branch-2.js | 100 | 100 | 100 | 100 |
-------------|---------|----------|---------|---------|-------------------
,"
`;

exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
",----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
Expand Down

0 comments on commit 2f57a5d

Please sign in to comment.