Skip to content

Commit

Permalink
move coverage reporter to common location
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkit-30 committed Mar 15, 2023
1 parent d14ca7c commit c57d48e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 77 deletions.
39 changes: 3 additions & 36 deletions lib/internal/test_runner/reporter/spec.js
Expand Up @@ -6,7 +6,6 @@ const {
ArrayPrototypeShift,
ArrayPrototypeUnshift,
hardenRegExp,
NumberPrototypeToFixed,
RegExpPrototypeSymbolSplit,
SafeMap,
StringPrototypeRepeat,
Expand All @@ -15,7 +14,7 @@ const assert = require('assert');
const Transform = require('internal/streams/transform');
const { inspectWithNoCustomRetry } = require('internal/errors');
const { green, blue, red, white, gray } = require('internal/util/colors');
const { relative } = require('path');
const { getCoverageReport } = require('internal/test_runner/utils');


const inspectOptions = { __proto__: null, colors: true, breakLength: Infinity };
Expand Down Expand Up @@ -63,39 +62,7 @@ class SpecReporter extends Transform {
), `\n${indent} `);
return `\n${indent} ${message}\n`;
}
#coverageThresholdColor(coverage, color = blue) {
coverage = NumberPrototypeToFixed(coverage, 2);
if (coverage > 90) return `${green}${coverage}${color}`;
if (coverage < 50) return `${red}${coverage}${color}`;
return coverage;
}
#reportCoverage(nesting, symbol, color, summary) {
const indent = this.#indent(nesting);
let report = `${color}${indent}${symbol}========= coverage report =========\n`;
report += `${indent}${symbol}file | line % | branch % | funcs % | uncovered lines\n`;
for (let i = 0; i < summary.files.length; ++i) {
const {
path,
coveredLinePercent,
coveredBranchPercent,
coveredFunctionPercent,
uncoveredLineNumbers,
} = summary.files[i];
const relativePath = relative(summary.workingDirectory, path);
const uncovered = ArrayPrototypeJoin(uncoveredLineNumbers, ', ');
report += `${indent}${symbol}${relativePath} | ${this.#coverageThresholdColor(coveredLinePercent)} | ${this.#coverageThresholdColor(coveredBranchPercent)} | ` +
`${this.#coverageThresholdColor(coveredFunctionPercent)} | ${gray}${uncovered}${color}\n`;
}
const {
coveredLinePercent,
coveredBranchPercent,
coveredFunctionPercent,
} = summary.totals;
report += `${indent}${symbol}all files | ${this.#coverageThresholdColor(coveredLinePercent)} | ${this.#coverageThresholdColor(coveredBranchPercent)} | ` +
`${this.#coverageThresholdColor(coveredFunctionPercent)} |\n`;
report += `${symbol}${indent}==================================== ${white}`;
return report;
}

#handleEvent({ type, data }) {
let color = colors[type] ?? white;
let symbol = symbols[type] ?? ' ';
Expand Down Expand Up @@ -140,7 +107,7 @@ class SpecReporter extends Transform {
case 'test:diagnostic':
return `${color}${this.#indent(data.nesting)}${symbol}${data.message}${white}\n`;
case 'test:coverage':
return this.#reportCoverage(data.nesting, symbols['test:coverage'], blue, data.summary);
return getCoverageReport(this.#indent(data.nesting), data.summary, symbols['test:coverage'], blue);
}
}
_transform({ type, data }, encoding, callback) {
Expand Down
39 changes: 2 additions & 37 deletions lib/internal/test_runner/reporter/tap.js
Expand Up @@ -3,7 +3,6 @@ const {
ArrayPrototypeForEach,
ArrayPrototypeJoin,
ArrayPrototypePush,
NumberPrototypeToFixed,
ObjectEntries,
RegExpPrototypeSymbolReplace,
SafeMap,
Expand All @@ -13,7 +12,7 @@ const {
} = primordials;
const { inspectWithNoCustomRetry } = require('internal/errors');
const { isError, kEmptyObject } = require('internal/util');
const { relative } = require('path');
const { getCoverageReport } = require('internal/test_runner/utils');
const kDefaultIndent = ' '; // 4 spaces
const kFrameStartRegExp = /^ {4}at /;
const kLineBreakRegExp = /\n|\r\n/;
Expand Down Expand Up @@ -49,7 +48,7 @@ async function * tapReporter(source) {
yield `${indent(data.nesting)}# ${tapEscape(data.message)}\n`;
break;
case 'test:coverage':
yield reportCoverage(data.nesting, data.summary);
yield getCoverageReport(indent(data.nesting), data.summary);
break;
}
}
Expand All @@ -73,40 +72,6 @@ function reportTest(nesting, testNumber, status, name, skip, todo) {
return line;
}

function reportCoverage(nesting, summary) {
const pad = indent(nesting);
let report = `${pad}# start of coverage report\n`;

report += `${pad}# file | line % | branch % | funcs % | uncovered lines\n`;

for (let i = 0; i < summary.files.length; ++i) {
const {
path,
coveredLinePercent,
coveredBranchPercent,
coveredFunctionPercent,
uncoveredLineNumbers,
} = summary.files[i];
const relativePath = relative(summary.workingDirectory, path);
const lines = NumberPrototypeToFixed(coveredLinePercent, 2);
const branches = NumberPrototypeToFixed(coveredBranchPercent, 2);
const functions = NumberPrototypeToFixed(coveredFunctionPercent, 2);
const uncovered = ArrayPrototypeJoin(uncoveredLineNumbers, ', ');

report += `${pad}# ${relativePath} | ${lines} | ${branches} | ` +
`${functions} | ${uncovered}\n`;
}

const { totals } = summary;
report += `${pad}# all files | ` +
`${NumberPrototypeToFixed(totals.coveredLinePercent, 2)} | ` +
`${NumberPrototypeToFixed(totals.coveredBranchPercent, 2)} | ` +
`${NumberPrototypeToFixed(totals.coveredFunctionPercent, 2)} |\n`;

report += `${pad}# end of coverage report\n`;
return report;
}

function reportDetails(nesting, data = kEmptyObject) {
const { error, duration_ms } = data;
const _indent = indent(nesting);
Expand Down
49 changes: 47 additions & 2 deletions lib/internal/test_runner/utils.js
@@ -1,15 +1,16 @@
'use strict';
const {
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypePush,
NumberPrototypeToFixed,
ObjectGetOwnPropertyDescriptor,
SafePromiseAllReturnArrayLike,
RegExp,
RegExpPrototypeExec,
SafeMap,
} = primordials;

const { basename } = require('path');
const { basename, relative } = require('path');
const { createWriteStream } = require('fs');
const { pathToFileURL } = require('internal/url');
const { createDeferredPromise } = require('internal/util');
Expand All @@ -23,6 +24,7 @@ const {
kIsNodeError,
} = require('internal/errors');
const { compose } = require('stream');
const { green, red, white } = require('internal/util/colors');

const kMultipleCallbackInvocations = 'multipleCallbackInvocations';
const kRegExpPattern = /^\/(.*)\/([a-z]*)$/;
Expand Down Expand Up @@ -222,6 +224,48 @@ function parseCommandLine() {
return globalTestOptions;
}

function coverageThreshold(coverage, color = white) {
coverage = NumberPrototypeToFixed(coverage, 2);
if (color !== white) {
if (coverage > 90) return `${green}${coverage}${color}`;
if (coverage < 50) return `${red}${coverage}${color}`;
}
return coverage;
}

function getCoverageReport(pad, summary, symbol = '# ', color = white) {
let report = `${color}${pad}${symbol}start of coverage report\n`;

report += `${pad}${symbol}file | line % | branch % | funcs % | uncovered lines\n`;

for (let i = 0; i < summary.files.length; ++i) {
const {
path,
coveredLinePercent,
coveredBranchPercent,
coveredFunctionPercent,
uncoveredLineNumbers,
} = summary.files[i];
const relativePath = relative(summary.workingDirectory, path);
const lines = coverageThreshold(coveredLinePercent, color);
const branches = coverageThreshold(coveredBranchPercent, color);
const functions = coverageThreshold(coveredFunctionPercent, color);
const uncovered = ArrayPrototypeJoin(uncoveredLineNumbers, ', ');

report += `${pad}${symbol}${relativePath} | ${lines} | ${branches} | ` +
`${functions} | ${uncovered}\n`;
}

const { totals } = summary;
report += `${pad}${symbol}all files | ` +
`${coverageThreshold(totals.coveredLinePercent, color)} | ` +
`${coverageThreshold(totals.coveredBranchPercent, color)} | ` +
`${coverageThreshold(totals.coveredFunctionPercent, color)} |\n`;

report += `${pad}${symbol}end of coverage report${white}\n`;
return report;
}

module.exports = {
convertStringToRegExp,
createDeferredCallback,
Expand All @@ -230,4 +274,5 @@ module.exports = {
isTestFailureError,
parseCommandLine,
setupTestReporters,
getCoverageReport,
};
4 changes: 2 additions & 2 deletions test/parallel/test-runner-spec-coverage.js
Expand Up @@ -20,14 +20,14 @@ function findCoverageFileForPid(pid) {

function getCoverageFixtureReport() {
const report = [
'\u2139 ========= coverage report =========',
'\u2139 start of coverage report',
'\u2139 file | line % | branch % | funcs % | uncovered lines',
'\u2139 test/fixtures/test-runner/coverage.js | 78.65 | 38.46 | 60.00 | 12, ' +
'13, 16, 17, 18, 19, 20, 21, 22, 27, 39, 43, 44, 61, 62, 66, 67, 71, 72',
'\u2139 test/fixtures/test-runner/invalid-tap.js | 100.00 | 100.00 | 100.00 | ',
'\u2139 test/fixtures/v8-coverage/throw.js | 71.43 | 50.00 | 100.00 | 5, 6',
'\u2139 all files | 78.35 | 43.75 | 60.00 |',
'\u2139 ====================================',
'\u2139 end of coverage report',
].join('\n');

if (common.isWindows) {
Expand Down

0 comments on commit c57d48e

Please sign in to comment.