Skip to content

Commit

Permalink
feat: Report supports multiple external --all targets
Browse files Browse the repository at this point in the history
The `lib/report` API can now receive additional arguments that allow
it to pull in code from multiple directories even if those directories
live outside of the invokers cwd.

Additional bug fix: Fixed padding/new lines around source map urls
(and added tests)
  • Loading branch information
j03m committed Mar 26, 2020
1 parent d730c63 commit 0f2dcff
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 32 deletions.
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 ^
36 changes: 36 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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 @@ -416,4 +417,39 @@ describe('c8', () => {
output.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-target.js')
], {
cwd: dirname(require.resolve('./fixtures/report/report-target.js'))
})
output.toString('utf8').should.matchSnapshot()
})

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-target.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-target.js'))
})
output.toString('utf8').should.matchSnapshot()
})
})
})
13 changes: 13 additions & 0 deletions test/integration.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ All files | 83.33 | 85.71 | 60 | 83.33 |
"
`;

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 reports coverage for script that exits normally 1`] = `
",hey
i am a line of code
Expand Down
14 changes: 14 additions & 0 deletions test/source-map-from-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* global describe, it */
const getSourceMapFromFile = require('../lib/source-map-from-file')
const assert = require('assert')
const path = require('path')
describe('source-map-from-file', () => {
it('should parse source maps from compiled targets', () => {
const sourceMap = getSourceMapFromFile('./test/fixtures/all/ts-compiled/main.js')
assert.strictEqual(sourceMap, ['test', 'fixtures', 'all', 'ts-compiled', 'main.js.map'].join(path.sep))
})
it('should handle extra whitespace characters', () => {
const sourceMap = getSourceMapFromFile('./test/fixtures/source-maps/padded.js')
assert.strictEqual(sourceMap, ['test', 'fixtures', 'source-maps', 'padded.js.map'].join(path.sep))
})
})

0 comments on commit 0f2dcff

Please sign in to comment.