-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
test_runner: add statement coverage support #62340
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
Open
Felipeness
wants to merge
7
commits into
nodejs:main
Choose a base branch
from
Felipeness:feat/test-runner-statement-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a95e904
feat: test_runner: add statement coverage support
Felipeness 05a5e91
fix(test_runner): fix statement coverage visitor and edge cases
Felipeness bac6828
fix: address review feedback on statement coverage
Felipeness f17a399
fix(test_runner): improve statement coverage implementation and tests
Felipeness e1cf6c7
refactor: inline visitor object in getStatements
Felipeness 5d3754e
fix(test_runner): fix lint errors, update tests for statement coverage
Felipeness ee6a76f
test_runner: address review feedback on statement coverage
Felipeness 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
|
|
@@ -38,6 +38,19 @@ const { | |
| } = require('internal/errors'); | ||
| const { matchGlobPattern } = require('internal/fs/glob'); | ||
| const { constants: { kMockSearchParam } } = require('internal/test_runner/mock/loader'); | ||
| const { Parser: AcornParser } = | ||
| require('internal/deps/acorn/acorn/dist/acorn'); | ||
| const { simple: acornWalkSimple } = | ||
| require('internal/deps/acorn/acorn-walk/dist/walk'); | ||
|
|
||
| const kStatementTypes = [ | ||
| 'ExpressionStatement', 'ReturnStatement', 'ThrowStatement', | ||
| 'IfStatement', 'WhileStatement', 'DoWhileStatement', | ||
| 'ForStatement', 'ForInStatement', 'ForOfStatement', | ||
| 'SwitchStatement', 'TryStatement', 'BreakStatement', | ||
| 'ContinueStatement', 'VariableDeclaration', 'LabeledStatement', | ||
| 'WithStatement', 'DebuggerStatement', | ||
| ]; | ||
|
|
||
| const kCoverageFileRegex = /^coverage-(\d+)-(\d{13})-(\d+)\.json$/; | ||
| const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//; | ||
|
|
@@ -69,6 +82,70 @@ class TestCoverage { | |
| } | ||
|
|
||
| #sourceLines = new SafeMap(); | ||
| #sourceStatements = new SafeMap(); | ||
|
|
||
| getStatements(fileUrl, source) { | ||
| if (this.#sourceStatements.has(fileUrl)) { | ||
| return this.#sourceStatements.get(fileUrl); | ||
| } | ||
|
|
||
| try { | ||
| source ??= readFileSync(fileURLToPath(fileUrl), 'utf8'); | ||
| } catch { | ||
| this.#sourceStatements.set(fileUrl, null); | ||
| return null; | ||
| } | ||
|
|
||
| const statements = []; | ||
|
|
||
| // Build a visitor with one handler per concrete statement type. | ||
| // acorn-walk does not fire a generic "Statement" visitor for concrete | ||
| // node types, so each type must be registered individually. | ||
| const visitor = { __proto__: null }; | ||
| const handler = (node) => { | ||
| ArrayPrototypePush(statements, { | ||
| __proto__: null, | ||
| startOffset: node.start, | ||
| endOffset: node.end, | ||
| count: 0, | ||
| }); | ||
| }; | ||
| for (let i = 0; i < kStatementTypes.length; ++i) { | ||
| visitor[kStatementTypes[i]] = handler; | ||
| } | ||
|
|
||
| // Try parsing as a module first, fall back to script for files that | ||
| // use CommonJS syntax incompatible with module mode. | ||
| let ast; | ||
| try { | ||
| ast = AcornParser.parse(source, { | ||
| __proto__: null, | ||
| ecmaVersion: 'latest', | ||
| sourceType: 'module', | ||
| allowReturnOutsideFunction: true, | ||
| allowImportExportEverywhere: true, | ||
| allowAwaitOutsideFunction: true, | ||
| }); | ||
| } catch { | ||
| try { | ||
| ast = AcornParser.parse(source, { | ||
| __proto__: null, | ||
| ecmaVersion: 'latest', | ||
| sourceType: 'script', | ||
| allowReturnOutsideFunction: true, | ||
| allowAwaitOutsideFunction: true, | ||
| }); | ||
| } catch { | ||
| this.#sourceStatements.set(fileUrl, null); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| acornWalkSimple(ast, visitor); | ||
|
|
||
| this.#sourceStatements.set(fileUrl, statements); | ||
| return statements; | ||
| } | ||
|
|
||
| getLines(fileUrl, source) { | ||
| // Split the file source into lines. Make sure the lines maintain their | ||
|
|
@@ -145,18 +222,22 @@ class TestCoverage { | |
| totalLineCount: 0, | ||
| totalBranchCount: 0, | ||
| totalFunctionCount: 0, | ||
| totalStatementCount: 0, | ||
| coveredLineCount: 0, | ||
| coveredBranchCount: 0, | ||
| coveredFunctionCount: 0, | ||
| coveredStatementCount: 0, | ||
| coveredLinePercent: 0, | ||
| coveredBranchPercent: 0, | ||
| coveredFunctionPercent: 0, | ||
| coveredStatementPercent: 0, | ||
| }, | ||
| thresholds: { | ||
| __proto__: null, | ||
| line: this.options.lineCoverage, | ||
| branch: this.options.branchCoverage, | ||
| function: this.options.functionCoverage, | ||
| statement: this.options.statementCoverage, | ||
| }, | ||
| }; | ||
|
|
||
|
|
@@ -174,7 +255,16 @@ class TestCoverage { | |
| const functionReports = []; | ||
| const branchReports = []; | ||
|
|
||
| const lines = this.getLines(url); | ||
| // Read source once and pass to both getLines and getStatements to | ||
| // avoid double disk I/O for the same file. | ||
| let source; | ||
| try { | ||
| source = readFileSync(fileURLToPath(url), 'utf8'); | ||
| } catch { | ||
| continue; | ||
| } | ||
|
|
||
| const lines = this.getLines(url, source); | ||
| if (!lines) { | ||
| continue; | ||
| } | ||
|
|
@@ -243,29 +333,83 @@ class TestCoverage { | |
| } | ||
| } | ||
|
|
||
| // Compute statement coverage by mapping V8 ranges to AST statements. | ||
| // Pass the source already read above to avoid double disk I/O. | ||
| const statements = this.getStatements(url, source); | ||
| let totalStatements = 0; | ||
| let statementsCovered = 0; | ||
| const statementReports = []; | ||
|
|
||
| if (statements) { | ||
| for (let j = 0; j < statements.length; ++j) { | ||
| const stmt = statements[j]; | ||
| let bestCount = 0; | ||
| let bestRange = null; | ||
|
|
||
| for (let fi = 0; fi < functions.length; ++fi) { | ||
| const { ranges } = functions[fi]; | ||
| for (let ri = 0; ri < ranges.length; ++ri) { | ||
| const range = ranges[ri]; | ||
| if (range.startOffset <= stmt.startOffset && | ||
| range.endOffset >= stmt.endOffset) { | ||
| const size = range.endOffset - range.startOffset; | ||
| if (bestRange === null || | ||
| size < (bestRange.endOffset - bestRange.startOffset)) { | ||
| bestCount = range.count; | ||
| bestRange = range; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| stmt.count = bestRange !== null ? bestCount : 0; | ||
|
|
||
| const stmtLine = findLineForOffset(stmt.startOffset, lines); | ||
| const isIgnored = stmtLine != null && stmtLine.ignore; | ||
|
|
||
| if (!isIgnored) { | ||
| totalStatements++; | ||
| ArrayPrototypePush(statementReports, { | ||
| __proto__: null, | ||
| line: stmtLine?.line, | ||
| count: stmt.count, | ||
| }); | ||
| if (stmt.count > 0) { | ||
| statementsCovered++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ArrayPrototypePush(coverageSummary.files, { | ||
| __proto__: null, | ||
| path: fileURLToPath(url), | ||
| totalLineCount: lines.length, | ||
| totalBranchCount: totalBranches, | ||
| totalFunctionCount: totalFunctions, | ||
| totalStatementCount: totalStatements, | ||
| coveredLineCount: coveredCnt, | ||
| coveredBranchCount: branchesCovered, | ||
| coveredFunctionCount: functionsCovered, | ||
| coveredStatementCount: statementsCovered, | ||
| coveredLinePercent: toPercentage(coveredCnt, lines.length), | ||
| coveredBranchPercent: toPercentage(branchesCovered, totalBranches), | ||
| coveredFunctionPercent: toPercentage(functionsCovered, totalFunctions), | ||
| coveredStatementPercent: toPercentage(statementsCovered, totalStatements), | ||
|
Member
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. Hmm.. since |
||
| functions: functionReports, | ||
| branches: branchReports, | ||
| lines: lineReports, | ||
| statements: statementReports, | ||
| }); | ||
|
|
||
| coverageSummary.totals.totalLineCount += lines.length; | ||
| coverageSummary.totals.totalBranchCount += totalBranches; | ||
| coverageSummary.totals.totalFunctionCount += totalFunctions; | ||
| coverageSummary.totals.totalStatementCount += totalStatements; | ||
| coverageSummary.totals.coveredLineCount += coveredCnt; | ||
| coverageSummary.totals.coveredBranchCount += branchesCovered; | ||
| coverageSummary.totals.coveredFunctionCount += functionsCovered; | ||
| coverageSummary.totals.coveredStatementCount += statementsCovered; | ||
| } | ||
|
|
||
| coverageSummary.totals.coveredLinePercent = toPercentage( | ||
|
|
@@ -280,6 +424,10 @@ class TestCoverage { | |
| coverageSummary.totals.coveredFunctionCount, | ||
| coverageSummary.totals.totalFunctionCount, | ||
| ); | ||
| coverageSummary.totals.coveredStatementPercent = toPercentage( | ||
| coverageSummary.totals.coveredStatementCount, | ||
| coverageSummary.totals.totalStatementCount, | ||
| ); | ||
| coverageSummary.files.sort(sortCoverageFiles); | ||
|
|
||
| return coverageSummary; | ||
|
|
@@ -695,4 +843,24 @@ function doesRangeContainOtherRange(range, otherRange) { | |
| range.endOffset >= otherRange.endOffset; | ||
| } | ||
|
|
||
| function findLineForOffset(offset, lines) { | ||
| let start = 0; | ||
| let end = lines.length - 1; | ||
|
|
||
| while (start <= end) { | ||
| const mid = MathFloor((start + end) / 2); | ||
| const line = lines[mid]; | ||
|
|
||
| if (offset >= line.startOffset && offset <= line.endOffset) { | ||
| return line; | ||
| } else if (offset > line.endOffset) { | ||
| start = mid + 1; | ||
| } else { | ||
| end = mid - 1; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| module.exports = { setupCoverage, TestCoverage }; | ||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this level of hard coding, is there not a general Statement handler or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call — acorn-walk's
simple()does support a genericStatementcategory visitor that fires for every node dispatched in a statement position. Replaced the hardcoded array with a singlevisitor.Statementhandler and a small deny-set forBlockStatement/EmptyStatement.This also picks up
ClassDeclaration,FunctionDeclaration, andStaticBlockwhich were missing from the original list, and stays forward-compatible with any future ESTree statement types.Simplified the double parse (module→script fallback) to a single
sourceType: 'script'pass with permissive flags too, since script mode +allowImportExportEverywherehandles both ESM and legacy CJS.