-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
test_runner: adds built in lcov test reporter #50018
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
Merged
nodejs-github-bot
merged 14 commits into
nodejs:main
from
philnash:test-runner-lcov-reporter
Oct 25, 2023
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c153f07
test_runner: adds built in lcov reporter
philnash f1fb047
test_runner: update lcov test output snapshot
philnash 8b60251
test_runner: fix linting errors in lcov reporter
philnash c080f97
test_runner: fixes comments
philnash 6377f29
test_runner: transform lcov output to be more consistent
philnash c079bfc
test_runner: fixing PR comments
philnash 579ce3e
test_runner: lcov reverse if statement to reduce indentation
philnash e1dbd6b
test_runner: use string replace primordial and array tosorted
philnash c218382
test_runner: skip lcov reporter test if no inspector
philnash b33f154
test_runner: correct order of function and options in tests
philnash ed9d6ca
test_runner: different way to skip test runner tests
philnash 56a137f
test_runner: consistency in skipping tests when inspector not available
philnash eb7a3ad
test_runner: alternative way to filter lcov test results
philnash 3c5ee6b
test_runner: make file path relative properly
philnash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,107 @@ | ||
'use strict'; | ||
|
||
const { relative } = require('path'); | ||
const Transform = require('internal/streams/transform'); | ||
|
||
// This reporter is based on the LCOV format, as described here: | ||
// https://ltp.sourceforge.net/coverage/lcov/geninfo.1.php | ||
// Excerpts from this documentation are included in the comments that make up | ||
// the _transform function below. | ||
class LcovReporter extends Transform { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be written more cleanly with modern stream machinery but I'm 100% OK with landing and then refactoring. |
||
constructor(options) { | ||
super({ ...options, writableObjectMode: true, __proto__: null }); | ||
} | ||
|
||
_transform(event, _encoding, callback) { | ||
philnash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (event.type !== 'test:coverage') { | ||
return callback(null); | ||
} | ||
let lcov = ''; | ||
// A tracefile is made up of several human-readable lines of text, divided | ||
// into sections. If available, a tracefile begins with the testname which | ||
// is stored in the following format: | ||
// ## TN:\<test name\> | ||
lcov += 'TN:\n'; | ||
const { | ||
data: { | ||
summary: { workingDirectory }, | ||
}, | ||
} = event; | ||
try { | ||
for (let i = 0; i < event.data.summary.files.length; i++) { | ||
const file = event.data.summary.files[i]; | ||
// For each source file referenced in the .da file, there is a section | ||
// containing filename and coverage data: | ||
// ## SF:\<path to the source file\> | ||
lcov += `SF:${relative(workingDirectory, file.path)}\n`; | ||
|
||
// Following is a list of line numbers for each function name found in | ||
// the source file: | ||
// ## FN:\<line number of function start\>,\<function name\> | ||
// | ||
// After, there is a list of execution counts for each instrumented | ||
// function: | ||
// ## FNDA:\<execution count\>,\<function name\> | ||
// | ||
// This loop adds the FN lines to the lcov variable as it goes and | ||
// gathers the FNDA lines to be added later. This way we only loop | ||
// through the list of functions once. | ||
let fnda = ''; | ||
for (let j = 0; j < file.functions.length; j++) { | ||
const func = file.functions[j]; | ||
const name = func.name || `anonymous_${j}`; | ||
benjamingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lcov += `FN:${func.line},${name}\n`; | ||
fnda += `FNDA:${func.count},${name}\n`; | ||
} | ||
lcov += fnda; | ||
|
||
// This list is followed by two lines containing the number of | ||
// functions found and hit: | ||
// ## FNF:\<number of functions found\> | ||
// ## FNH:\<number of function hit\> | ||
lcov += `FNF:${file.totalFunctionCount}\n`; | ||
lcov += `FNH:${file.coveredFunctionCount}\n`; | ||
|
||
// Branch coverage information is stored which one line per branch: | ||
// ## BRDA:\<line number\>,\<block number\>,\<branch number\>,\<taken\> | ||
// Block number and branch number are gcc internal IDs for the branch. | ||
// Taken is either '-' if the basic block containing the branch was | ||
// never executed or a number indicating how often that branch was | ||
// taken. | ||
for (let j = 0; j < file.branches.length; j++) { | ||
lcov += `BRDA:${file.branches[j].line},${j},0,${file.branches[j].count}\n`; | ||
} | ||
|
||
// Branch coverage summaries are stored in two lines: | ||
// ## BRF:\<number of branches found\> | ||
// ## BRH:\<number of branches hit\> | ||
lcov += `BRF:${file.totalBranchCount}\n`; | ||
lcov += `BRH:${file.coveredBranchCount}\n`; | ||
|
||
// Then there is a list of execution counts for each instrumented line | ||
// (i.e. a line which resulted in executable code): | ||
// ## DA:\<line number\>,\<execution count\>[,\<checksum\>] | ||
const sortedLines = file.lines.toSorted((a, b) => a.line - b.line); | ||
for (let j = 0; j < sortedLines.length; j++) { | ||
lcov += `DA:${sortedLines[j].line},${sortedLines[j].count}\n`; | ||
} | ||
|
||
// At the end of a section, there is a summary about how many lines | ||
// were found and how many were actually instrumented: | ||
// ## LH:\<number of lines with a non-zero execution count\> | ||
// ## LF:\<number of instrumented lines\> | ||
lcov += `LH:${file.coveredLineCount}\n`; | ||
lcov += `LF:${file.totalLineCount}\n`; | ||
|
||
// Each sections ends with: | ||
// end_of_record | ||
lcov += 'end_of_record\n'; | ||
} | ||
} catch (error) { | ||
return callback(error); | ||
} | ||
return callback(null, lcov); | ||
} | ||
} | ||
|
||
module.exports = LcovReporter; |
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
'use strict'; | ||
require('../../../common'); | ||
const fixtures = require('../../../common/fixtures'); | ||
const spawn = require('node:child_process').spawn; | ||
|
||
spawn(process.execPath, | ||
['--no-warnings', '--experimental-test-coverage', '--test-reporter', 'lcov', fixtures.path('test-runner/output/output.js')], { stdio: 'inherit' }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.