Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for instrumenting files outside of current working directory #206

Merged
merged 2 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export declare class Report {
wrapperLength?: number,
resolve?: string,
all?: boolean,
src?: Array<string>,
allowExternal: boolean
})

run(): Promise<void>;
}
}
71 changes: 43 additions & 28 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class Report {
omitRelative,
wrapperLength,
resolve: resolvePaths,
all
all,
src,
allowExternal = false
}) {
this.reporter = reporter
this.reportsDirectory = reportsDirectory
Expand All @@ -30,13 +32,24 @@ class Report {
this.resolve = resolvePaths
this.exclude = new Exclude({
exclude: exclude,
include: include
include: include,
relativePath: !allowExternal
})
this.omitRelative = omitRelative
this.sourceMapCache = {}
this.wrapperLength = wrapperLength
this.all = all
this.src = process.cwd()
this.src = this._getSrc(src)
}

_getSrc (src) {
if (typeof src === 'string') {
return [src]
} else if (Array.isArray(src)) {
return src
} else {
return [process.cwd()]
}
}

async run () {
Expand Down Expand Up @@ -159,33 +172,35 @@ class Report {
v8ProcessCovs.unshift({
result: emptyReports
})
const workingDir = process.cwd()
this.exclude.globSync(workingDir).forEach((f) => {
const fullPath = resolve(workingDir, f)
if (!fileIndex.has(fullPath)) {
const ext = extname(f)
if (ext === '.js' || ext === '.ts' || ext === '.mjs') {
const stat = statSync(f)
const sourceMap = getSourceMapFromFile(f)
if (sourceMap !== undefined) {
this.sourceMapCache[`file://${fullPath}`] = { data: JSON.parse(readFileSync(sourceMap).toString()) }
const workingDirs = this.src
for (const workingDir of workingDirs) {
this.exclude.globSync(workingDir).forEach((f) => {
const fullPath = resolve(workingDir, f)
if (!fileIndex.has(fullPath)) {
const ext = extname(fullPath)
if (ext === '.js' || ext === '.ts' || ext === '.mjs') {
const stat = statSync(fullPath)
const sourceMap = getSourceMapFromFile(fullPath)
if (sourceMap !== undefined) {
this.sourceMapCache[`file://${fullPath}`] = { data: JSON.parse(readFileSync(sourceMap).toString()) }
}
emptyReports.push({
scriptId: 0,
url: resolve(fullPath),
functions: [{
functionName: '(empty-report)',
ranges: [{
startOffset: 0,
endOffset: stat.size,
count: 0
}],
isBlockCoverage: true
}]
})
}
emptyReports.push({
scriptId: 0,
url: resolve(f),
functions: [{
functionName: '(empty-report)',
ranges: [{
startOffset: 0,
endOffset: stat.size,
count: 0
}],
isBlockCoverage: true
}]
})
}
}
})
})
}
}

return mergeProcessCovs(v8ProcessCovs)
Expand Down
4 changes: 2 additions & 2 deletions lib/source-map-from-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function getSourceMapFromFile (file) {
if (results !== null) {
const sourceMap = results[results.length - 1].split('=')[1]
if (isAbsolute(sourceMap)) {
return sourceMap
return sourceMap.trim()
} else {
const base = dirname(file)
return join(base, sourceMap)
return join(base, sourceMap).trim()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/multidir1/file1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("hi")
1 change: 1 addition & 0 deletions test/fixtures/multidir2/file2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("hi")
12 changes: 12 additions & 0 deletions test/fixtures/report/report-multi-dir-external.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Report = require('../../../lib/report')
const report = new Report({
include: ['**/*.js'],
exclude: [],
reporter: ['text'],
tempDirectory: './temp',
omitRelative: true,
all: true,
src: ['../multidir1/', '../multidir2/'],
allowExternal: true
})
report.run()
12 changes: 12 additions & 0 deletions test/fixtures/report/report-single-dir-external.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Report = require('../../../lib/report')
const report = new Report({
include: ['**/*.js'],
exclude: [],
reporter: ['text'],
tempDirectory: './temp',
omitRelative: true,
all: true,
src: '../multidir1/',
allowExternal: true
})
report.run()
1 change: 1 addition & 0 deletions test/fixtures/report/temp/coverage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"result":[]}
10 changes: 10 additions & 0 deletions test/fixtures/source-maps/padded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
exports.__esModule = true;
var loaded_1 = require("./loaded");
console.log(loaded_1["default"](0));
console.log(loaded_1["default"](1));
console.log(loaded_1["default"](-1));
//# sourceMappingURL=padded.js.map


//ew extra whitespace ^
29 changes: 26 additions & 3 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { readFileSync } = require('fs')
const { resolve } = require('path')
const { spawnSync } = require('child_process')
const { statSync } = require('fs')
const { dirname } = require('path')
const c8Path = require.resolve('../bin/c8')
const nodePath = process.execPath
const tsNodePath = './node_modules/.bin/ts-node'
Expand Down Expand Up @@ -112,10 +113,10 @@ describe('c8', () => {
const { output, status } = spawnSync(nodePath, [
c8Path,
'check-coverage',
'--exclude="test/*.js"',
'--exclude="test/fixtures/*.js"',
'--temp-directory=tmp/check-coverage',
'--lines=70',
'--branches=60',
'--branches=56',
'--statements=70'
])
status.should.equal(0)
Expand Down Expand Up @@ -431,7 +432,6 @@ describe('c8', () => {
output.toString('utf8').should.matchSnapshot()
})
})

// see: https://github.com/bcoe/c8/issues/149
it('cobertura report escapes special characters', () => {
spawnSync(nodePath, [
Expand All @@ -449,4 +449,27 @@ describe('c8', () => {
.replace(/\\/g, '/')
cobertura.toString('utf8').should.matchSnapshot()
})
describe('report', () => {
it('supports reporting on directories outside cwd', () => {
// invoke a script that uses report as an api and supplies src dirs out
// of cwd
const { output } = spawnSync(nodePath, [
require.resolve('./fixtures/report/report-multi-dir-external.js')
], {
cwd: dirname(require.resolve('./fixtures/report/report-multi-dir-external.js'))
})
output.toString('utf8').should.matchSnapshot()
})

it('supports reporting on single directories outside cwd', () => {
// invoke a script that uses report as an api and supplies src dirs out
// of cwd.
const { output } = spawnSync(nodePath, [
require.resolve('./fixtures/report/report-single-dir-external.js')
], {
cwd: dirname(require.resolve('./fixtures/report/report-single-dir-external.js'))
})
output.toString('utf8').should.matchSnapshot()
})
})
})
23 changes: 23 additions & 0 deletions test/integration.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,29 @@ ERROR: Coverage for statements (83.33%) does not meet global threshold (96%)
"
`;

exports[`c8 report supports reporting on directories outside cwd 1`] = `
",-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
multidir1 | 0 | 0 | 0 | 0 |
file1.js | 0 | 0 | 0 | 0 | 1
multidir2 | 0 | 0 | 0 | 0 |
file2.js | 0 | 0 | 0 | 0 | 1
-----------|---------|----------|---------|---------|-------------------
,"
`;

exports[`c8 report supports reporting on single directories outside cwd 1`] = `
",----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
file1.js | 0 | 0 | 0 | 0 | 1
----------|---------|----------|---------|---------|-------------------
,"
`;

exports[`c8 reports coverage for script that exits normally 1`] = `
",hey
i am a line of code
Expand Down
Loading