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

test_runner: refactor coverage to pass in config options #53931

Merged
merged 1 commit into from
Jul 22, 2024
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
120 changes: 61 additions & 59 deletions lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,11 @@ const { tmpdir } = require('os');
const { join, resolve, relative, matchesGlob } = require('path');
const { fileURLToPath } = require('internal/url');
const { kMappings, SourceMap } = require('internal/source_map/source_map');
const { parseCommandLine } = require('internal/test_runner/utils');
const kCoverageFileRegex = /^coverage-(\d+)-(\d{13})-(\d+)\.json$/;
const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//;
const kLineEndingRegex = /\r?\n$/u;
const kLineSplitRegex = /(?<=\r?\n)/u;
const kStatusRegex = /\/\* node:coverage (?<status>enable|disable) \*\//;
const {
coverageExcludeGlobs,
coverageIncludeGlobs,
} = parseCommandLine();

class CoverageLine {
constructor(line, startOffset, src, length = src?.length) {
Expand All @@ -55,10 +50,12 @@ class CoverageLine {
}

class TestCoverage {
constructor(coverageDirectory, originalCoverageDirectory, workingDirectory) {
constructor(coverageDirectory, originalCoverageDirectory, workingDirectory, excludeGlobs, includeGlobs) {
this.coverageDirectory = coverageDirectory;
this.originalCoverageDirectory = originalCoverageDirectory;
this.workingDirectory = workingDirectory;
this.excludeGlobs = excludeGlobs;
this.includeGlobs = includeGlobs;
}

#sourceLines = new SafeMap();
Expand Down Expand Up @@ -313,7 +310,7 @@ class TestCoverage {

const coverageFile = join(this.coverageDirectory, entry.name);
const coverage = JSONParse(readFileSync(coverageFile, 'utf8'));
mergeCoverage(result, this.mapCoverageWithSourceMap(coverage), this.workingDirectory);
this.mergeCoverage(result, this.mapCoverageWithSourceMap(coverage));
}

return ArrayFrom(result.values());
Expand All @@ -336,7 +333,7 @@ class TestCoverage {
const script = result[i];
const { url, functions } = script;

if (shouldSkipFileCoverage(url, this.workingDirectory) || sourceMapCache[url] == null) {
if (this.shouldSkipFileCoverage(url) || sourceMapCache[url] == null) {
newResult.set(url, script);
continue;
}
Expand Down Expand Up @@ -412,6 +409,54 @@ class TestCoverage {
return MathMin(lines[line].startOffset + entry.originalColumn, lines[line].endOffset);
}

mergeCoverage(merged, coverage) {
for (let i = 0; i < coverage.length; ++i) {
const newScript = coverage[i];
const { url } = newScript;

if (this.shouldSkipFileCoverage(url)) {
continue;
}

const oldScript = merged.get(url);

if (oldScript === undefined) {
merged.set(url, newScript);
} else {
mergeCoverageScripts(oldScript, newScript);
}
}
}

shouldSkipFileCoverage(url) {
// This check filters out core modules, which start with 'node:' in
// coverage reports, as well as any invalid coverages which have been
// observed on Windows.
if (!StringPrototypeStartsWith(url, 'file:')) return true;

const absolutePath = fileURLToPath(url);
const relativePath = relative(this.workingDirectory, absolutePath);

// This check filters out files that match the exclude globs.
if (this.excludeGlobs?.length > 0) {
for (let i = 0; i < this.excludeGlobs.length; ++i) {
if (matchesGlob(relativePath, this.excludeGlobs[i]) ||
matchesGlob(absolutePath, this.excludeGlobs[i])) return true;
}
}

// This check filters out files that do not match the include globs.
if (this.includeGlobs?.length > 0) {
for (let i = 0; i < this.includeGlobs.length; ++i) {
if (matchesGlob(relativePath, this.includeGlobs[i]) ||
matchesGlob(absolutePath, this.includeGlobs[i])) return false;
}
return true;
}

// This check filters out the node_modules/ directory, unless it is explicitly included.
return StringPrototypeIncludes(url, '/node_modules/');
}
}

function toPercentage(covered, total) {
Expand All @@ -422,7 +467,7 @@ function sortCoverageFiles(a, b) {
return StringPrototypeLocaleCompare(a.path, b.path);
}

function setupCoverage() {
function setupCoverage(options) {
let originalCoverageDirectory = process.env.NODE_V8_COVERAGE;
const cwd = process.cwd();

Expand All @@ -446,7 +491,13 @@ function setupCoverage() {
// child processes.
process.env.NODE_V8_COVERAGE = coverageDirectory;

return new TestCoverage(coverageDirectory, originalCoverageDirectory, cwd);
return new TestCoverage(
coverageDirectory,
originalCoverageDirectory,
cwd,
options.coverageExcludeGlobs,
options.coverageIncludeGlobs,
);
}

function mapRangeToLines(range, lines) {
Expand Down Expand Up @@ -490,55 +541,6 @@ function mapRangeToLines(range, lines) {
return { __proto__: null, lines: mappedLines, ignoredLines };
}

function shouldSkipFileCoverage(url, workingDirectory) {
// This check filters out core modules, which start with 'node:' in
// coverage reports, as well as any invalid coverages which have been
// observed on Windows.
if (!StringPrototypeStartsWith(url, 'file:')) return true;

const absolutePath = fileURLToPath(url);
const relativePath = relative(workingDirectory, absolutePath);

// This check filters out files that match the exclude globs.
if (coverageExcludeGlobs?.length > 0) {
for (let i = 0; i < coverageExcludeGlobs.length; ++i) {
if (matchesGlob(relativePath, coverageExcludeGlobs[i]) ||
matchesGlob(absolutePath, coverageExcludeGlobs[i])) return true;
}
}

// This check filters out files that do not match the include globs.
if (coverageIncludeGlobs?.length > 0) {
for (let i = 0; i < coverageIncludeGlobs.length; ++i) {
if (matchesGlob(relativePath, coverageIncludeGlobs[i]) ||
matchesGlob(absolutePath, coverageIncludeGlobs[i])) return false;
}
return true;
}

// This check filters out the node_modules/ directory, unless it is explicitly included.
return StringPrototypeIncludes(url, '/node_modules/');
}

function mergeCoverage(merged, coverage, workingDirectory) {
for (let i = 0; i < coverage.length; ++i) {
const newScript = coverage[i];
const { url } = newScript;

if (shouldSkipFileCoverage(url, workingDirectory)) {
continue;
}

const oldScript = merged.get(url);

if (oldScript === undefined) {
merged.set(url, newScript);
} else {
mergeCoverageScripts(oldScript, newScript);
}
}
}

function mergeCoverageScripts(oldScript, newScript) {
// Merge the functions from the new coverage into the functions from the
// existing (merged) coverage.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function configureCoverage(rootTest, globalOptions) {
const { setupCoverage } = require('internal/test_runner/coverage');

try {
return setupCoverage();
return setupCoverage(globalOptions);
} catch (err) {
const msg = `Warning: Code coverage could not be enabled. ${err}`;

Expand Down
Loading