-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
native V8 coverage reports can now be written to disk by setting the variable NODE_V8_COVERAGE=dir PR-URL: #22527 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Yang Guo <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
- Loading branch information
Showing
14 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const { mkdirSync, writeFileSync } = require('fs'); | ||
// TODO(addaleax): add support for coverage to worker threads. | ||
const hasInspector = process.config.variables.v8_enable_inspector === 1 && | ||
require('internal/worker').isMainThread; | ||
let inspector = null; | ||
if (hasInspector) inspector = require('inspector'); | ||
|
||
let session; | ||
|
||
function writeCoverage() { | ||
if (!session) { | ||
return; | ||
} | ||
|
||
const filename = `coverage-${process.pid}-${Date.now()}.json`; | ||
try { | ||
// TODO(bcoe): switch to mkdirp once #22302 is addressed. | ||
mkdirSync(process.env.NODE_V8_COVERAGE); | ||
} catch (err) { | ||
if (err.code !== 'EEXIST') { | ||
console.error(err); | ||
return; | ||
} | ||
} | ||
|
||
const target = path.join(process.env.NODE_V8_COVERAGE, filename); | ||
|
||
try { | ||
session.post('Profiler.takePreciseCoverage', (err, coverageInfo) => { | ||
if (err) return console.error(err); | ||
try { | ||
writeFileSync(target, JSON.stringify(coverageInfo)); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}); | ||
} catch (err) { | ||
console.error(err); | ||
} finally { | ||
session.disconnect(); | ||
session = null; | ||
} | ||
} | ||
|
||
exports.writeCoverage = writeCoverage; | ||
|
||
function setup() { | ||
if (!hasInspector) { | ||
console.warn('coverage currently only supported in main thread'); | ||
return; | ||
} | ||
|
||
session = new inspector.Session(); | ||
session.connect(); | ||
session.post('Profiler.enable'); | ||
session.post('Profiler.startPreciseCoverage', { callCount: true, | ||
detailed: true }); | ||
|
||
const reallyReallyExit = process.reallyExit; | ||
|
||
process.reallyExit = function(code) { | ||
writeCoverage(); | ||
reallyReallyExit(code); | ||
}; | ||
|
||
process.on('exit', writeCoverage); | ||
} | ||
|
||
exports.setup = setup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const a = 99; | ||
if (true) { | ||
const b = 101; | ||
} else { | ||
const c = 102; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const a = 99; | ||
if (true) { | ||
const b = 101; | ||
} else { | ||
const c = 102; | ||
} | ||
process.exit(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const a = 99; | ||
if (true) { | ||
const b = 101; | ||
} else { | ||
const c = 102; | ||
} | ||
process.kill(process.pid, "SIGINT"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { spawnSync } = require('child_process'); | ||
const env = Object.assign({}, process.env, { NODE_V8_COVERAGE: '' }); | ||
spawnSync(process.execPath, [require.resolve('./subprocess')], { | ||
env: env | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const { spawnSync } = require('child_process'); | ||
const env = Object.assign({}, process.env); | ||
delete env.NODE_V8_COVERAGE | ||
spawnSync(process.execPath, [require.resolve('./subprocess')], { | ||
env: env | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const a = 99; | ||
setTimeout(() => { | ||
if (false) { | ||
const b = 101; | ||
} else if (false) { | ||
const c = 102; | ||
} | ||
}, 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict'; | ||
|
||
if (!process.config.variables.v8_enable_inspector) return; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { spawnSync } = require('child_process'); | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
let dirc = 0; | ||
function nextdir() { | ||
return `cov_${++dirc}`; | ||
} | ||
|
||
// outputs coverage when event loop is drained, with no async logic. | ||
{ | ||
const coverageDirectory = path.join(tmpdir.path, nextdir()); | ||
const output = spawnSync(process.execPath, [ | ||
require.resolve('../fixtures/v8-coverage/basic') | ||
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); | ||
assert.strictEqual(output.status, 0); | ||
const fixtureCoverage = getFixtureCoverage('basic.js', coverageDirectory); | ||
assert.ok(fixtureCoverage); | ||
// first branch executed. | ||
assert.strictEqual(fixtureCoverage.functions[1].ranges[0].count, 1); | ||
// second branch did not execute. | ||
assert.strictEqual(fixtureCoverage.functions[1].ranges[1].count, 0); | ||
} | ||
|
||
// outputs coverage when process.exit(1) exits process. | ||
{ | ||
const coverageDirectory = path.join(tmpdir.path, nextdir()); | ||
const output = spawnSync(process.execPath, [ | ||
require.resolve('../fixtures/v8-coverage/exit-1') | ||
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); | ||
assert.strictEqual(output.status, 1); | ||
const fixtureCoverage = getFixtureCoverage('exit-1.js', coverageDirectory); | ||
assert.ok(fixtureCoverage, 'coverage not found for file'); | ||
// first branch executed. | ||
assert.strictEqual(fixtureCoverage.functions[1].ranges[0].count, 1); | ||
// second branch did not execute. | ||
assert.strictEqual(fixtureCoverage.functions[1].ranges[1].count, 0); | ||
} | ||
|
||
// outputs coverage when process.kill(process.pid, "SIGINT"); exits process. | ||
{ | ||
const coverageDirectory = path.join(tmpdir.path, nextdir()); | ||
const output = spawnSync(process.execPath, [ | ||
require.resolve('../fixtures/v8-coverage/sigint') | ||
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); | ||
if (!common.isWindows) { | ||
assert.strictEqual(output.signal, 'SIGINT'); | ||
} | ||
const fixtureCoverage = getFixtureCoverage('sigint.js', coverageDirectory); | ||
assert.ok(fixtureCoverage); | ||
// first branch executed. | ||
assert.strictEqual(fixtureCoverage.functions[1].ranges[0].count, 1); | ||
// second branch did not execute. | ||
assert.strictEqual(fixtureCoverage.functions[1].ranges[1].count, 0); | ||
} | ||
|
||
// outputs coverage from subprocess. | ||
{ | ||
const coverageDirectory = path.join(tmpdir.path, nextdir()); | ||
const output = spawnSync(process.execPath, [ | ||
require.resolve('../fixtures/v8-coverage/spawn-subprocess') | ||
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); | ||
assert.strictEqual(output.status, 0); | ||
const fixtureCoverage = getFixtureCoverage('subprocess.js', | ||
coverageDirectory); | ||
assert.ok(fixtureCoverage); | ||
// first branch executed. | ||
assert.strictEqual(fixtureCoverage.functions[2].ranges[0].count, 1); | ||
// second branch did not execute. | ||
assert.strictEqual(fixtureCoverage.functions[2].ranges[1].count, 0); | ||
} | ||
|
||
// does not output coverage if NODE_V8_COVERAGE is empty. | ||
{ | ||
const coverageDirectory = path.join(tmpdir.path, nextdir()); | ||
const output = spawnSync(process.execPath, [ | ||
require.resolve('../fixtures/v8-coverage/spawn-subprocess-no-cov') | ||
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); | ||
assert.strictEqual(output.status, 0); | ||
const fixtureCoverage = getFixtureCoverage('subprocess.js', | ||
coverageDirectory); | ||
assert.strictEqual(fixtureCoverage, undefined); | ||
} | ||
|
||
// extracts the coverage object for a given fixture name. | ||
function getFixtureCoverage(fixtureFile, coverageDirectory) { | ||
const coverageFiles = fs.readdirSync(coverageDirectory); | ||
for (const coverageFile of coverageFiles) { | ||
const coverage = require(path.join(coverageDirectory, coverageFile)); | ||
for (const fixtureCoverage of coverage.result) { | ||
if (fixtureCoverage.url.indexOf(`${path.sep}${fixtureFile}`) !== -1) { | ||
return fixtureCoverage; | ||
} | ||
} | ||
} | ||
} |