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

coverage: fix broken coverage setup #25289

Merged
merged 3 commits into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions lib/internal/process/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ exports.writeCoverage = writeCoverage;
function setup() {
const { Connection } = internalBinding('inspector');
if (!Connection) {
console.warn('inspector not enabled');
process._rawDebug('inspector not enabled');
return;
}

Expand All @@ -76,11 +76,12 @@ function setup() {
}));

try {
const { cwd } = internalBinding('process_methods');
const { resolve } = require('path');
coverageDirectory = process.env.NODE_V8_COVERAGE =
resolve(process.env.NODE_V8_COVERAGE);
resolve(cwd(), process.env.NODE_V8_COVERAGE);
} catch (err) {
console.error(err);
process._rawDebug(err.toString());
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-v8-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function nextdir() {
require.resolve('../fixtures/v8-coverage/basic')
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
assert.strictEqual(output.status, 0);
assert.strictEqual(output.stderr.toString(), '');
const fixtureCoverage = getFixtureCoverage('basic.js', coverageDirectory);
assert.ok(fixtureCoverage);
// first branch executed.
Expand All @@ -38,6 +39,7 @@ function nextdir() {
require.resolve('../fixtures/v8-coverage/exit-1')
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
assert.strictEqual(output.status, 1);
assert.strictEqual(output.stderr.toString(), '');
const fixtureCoverage = getFixtureCoverage('exit-1.js', coverageDirectory);
assert.ok(fixtureCoverage, 'coverage not found for file');
// first branch executed.
Expand All @@ -55,6 +57,7 @@ function nextdir() {
if (!common.isWindows) {
assert.strictEqual(output.signal, 'SIGINT');
}
assert.strictEqual(output.stderr.toString(), '');
const fixtureCoverage = getFixtureCoverage('sigint.js', coverageDirectory);
assert.ok(fixtureCoverage);
// first branch executed.
Expand All @@ -70,6 +73,7 @@ function nextdir() {
require.resolve('../fixtures/v8-coverage/spawn-subprocess')
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
assert.strictEqual(output.status, 0);
assert.strictEqual(output.stderr.toString(), '');
const fixtureCoverage = getFixtureCoverage('subprocess.js',
coverageDirectory);
assert.ok(fixtureCoverage);
Expand All @@ -87,6 +91,7 @@ function nextdir() {
require.resolve('../fixtures/v8-coverage/worker')
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
assert.strictEqual(output.status, 0);
assert.strictEqual(output.stderr.toString(), '');
const fixtureCoverage = getFixtureCoverage('subprocess.js',
coverageDirectory);
assert.ok(fixtureCoverage);
Expand All @@ -103,6 +108,7 @@ function nextdir() {
require.resolve('../fixtures/v8-coverage/spawn-subprocess-no-cov')
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
assert.strictEqual(output.status, 0);
assert.strictEqual(output.stderr.toString(), '');
const fixtureCoverage = getFixtureCoverage('subprocess.js',
coverageDirectory);
assert.strictEqual(fixtureCoverage, undefined);
Expand All @@ -115,13 +121,35 @@ function nextdir() {
require.resolve('../fixtures/v8-coverage/async-hooks')
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
assert.strictEqual(output.status, 0);
assert.strictEqual(output.stderr.toString(), '');
const fixtureCoverage = getFixtureCoverage('async-hooks.js',
coverageDirectory);
assert.ok(fixtureCoverage);
// first branch executed.
assert.strictEqual(fixtureCoverage.functions[1].ranges[0].count, 1);
}

// Outputs coverage when the coverage directory is not absolute.
{
const coverageDirectory = nextdir();
const absoluteCoverageDirectory = path.join(tmpdir.path, coverageDirectory);
const output = spawnSync(process.execPath, [
require.resolve('../fixtures/v8-coverage/basic')
], {
cwd: tmpdir.path,
env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory }
});
assert.strictEqual(output.status, 0);
assert.strictEqual(output.stderr.toString(), '');
const fixtureCoverage = getFixtureCoverage('basic.js',
absoluteCoverageDirectory);
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);
}

// Extracts the coverage object for a given fixture name.
function getFixtureCoverage(fixtureFile, coverageDirectory) {
const coverageFiles = fs.readdirSync(coverageDirectory);
Expand Down