3535// See docs/TESTING-SUITES.md for full grouping policy.
3636'use strict' ;
3737
38- const { appendFileSync, readdirSync, readFileSync, mkdtempSync, rmSync, unlinkSync, writeFileSync } = require ( 'fs' ) ;
39- const { join, basename } = require ( 'path' ) ;
38+ const { appendFileSync, readdirSync, readFileSync, realpathSync , mkdtempSync, rmSync, unlinkSync, writeFileSync } = require ( 'fs' ) ;
39+ const { join, basename, resolve } = require ( 'path' ) ;
4040const { tmpdir } = require ( 'os' ) ;
4141const { pathToFileURL } = require ( 'url' ) ;
4242const { execFileSync, spawn } = require ( 'child_process' ) ;
@@ -1251,6 +1251,129 @@ function analyzeChunkEvents(eventsPath) {
12511251 } ;
12521252}
12531253
1254+ // #4031: per-file test accounting for one chunk. `--test-force-exit` can end
1255+ // the `node --test` parent while part of a test file's results are still
1256+ // unread on the child's pipe, so the reporters (and this runner's own ndjson
1257+ // events file) never see them and the run reports a smaller count with exit 0.
1258+ // The count of REGISTERED tests therefore comes from the child, through a
1259+ // channel that does not share that pipe: scripts/lib/test-registration-ledger.cjs
1260+ // (loaded with `--require`) appends one `{type:'registered', file, count}`
1261+ // line per test-file child, and the ndjson reporter records every leaf
1262+ // `test:pass`/`test:fail` it received. A file whose registered count exceeds
1263+ // its reported count lost results.
1264+ //
1265+ // One-sided by construction: the ledger counts module-level `test()`/`it()`
1266+ // calls only, while a subtest created at run time (`t.test()`) is reported but
1267+ // not registered, so reported can exceed registered, never the reverse, absent
1268+ // a loss. A count that errs low can hide a loss; it cannot invent one.
1269+ //
1270+ // `available` is false when either file is missing/unreadable or the ledger
1271+ // holds no `registered` line: the chunk could not be accounted, which the
1272+ // caller reports instead of reading it as "nothing was lost". Truncated
1273+ // trailing lines are skipped, as in analyzeChunkEvents. Paths are compared
1274+ // after path.resolve (case-folded on win32).
1275+ function normalizeAccountedPath ( p ) {
1276+ let resolved = resolve ( p ) ;
1277+ try {
1278+ // The child's argv[1] and the runner's event `file` are the same file but
1279+ // not necessarily the same spelling (a symlinked temp root, 8.3 names).
1280+ resolved = realpathSync . native ( resolved ) ;
1281+ } catch {
1282+ // Not on disk (a synthetic path in a unit test): the resolved spelling stands.
1283+ }
1284+ return process . platform === 'win32' ? resolved . toLowerCase ( ) : resolved ;
1285+ }
1286+
1287+ function analyzeChunkAccounting ( eventsPath , ledgerPath ) {
1288+ const reported = new Map ( ) ; // normalized file -> leaf pass+fail events received
1289+ const registered = new Map ( ) ; // normalized file -> registrations counted in the child
1290+ const display = new Map ( ) ; // normalized file -> path as first seen, for messages
1291+ let eventsRead = true ;
1292+ let ledgerRead = true ;
1293+ let sawRegisteredLine = false ;
1294+ let eventsRaw = '' ;
1295+ let ledgerRaw = '' ;
1296+ try {
1297+ eventsRaw = readFileSync ( eventsPath , 'utf8' ) ;
1298+ } catch {
1299+ eventsRead = false ;
1300+ }
1301+ try {
1302+ ledgerRaw = readFileSync ( ledgerPath , 'utf8' ) ;
1303+ } catch {
1304+ ledgerRead = false ;
1305+ }
1306+ const parse = ( raw , onEvent ) => {
1307+ for ( const line of raw . split ( '\n' ) ) {
1308+ if ( line . trim ( ) === '' ) continue ;
1309+ let evt ;
1310+ try {
1311+ evt = JSON . parse ( line ) ;
1312+ } catch {
1313+ continue ; // truncated trailing line
1314+ }
1315+ onEvent ( evt ) ;
1316+ }
1317+ } ;
1318+ parse ( eventsRaw , ( evt ) => {
1319+ if ( ( evt . type !== 'test:pass' && evt . type !== 'test:fail' ) || typeof evt . file !== 'string' ) return ;
1320+ if ( evt . kind === 'suite' ) return ;
1321+ const key = normalizeAccountedPath ( evt . file ) ;
1322+ display . set ( key , display . get ( key ) || evt . file ) ;
1323+ reported . set ( key , ( reported . get ( key ) || 0 ) + 1 ) ;
1324+ } ) ;
1325+ parse ( ledgerRaw , ( evt ) => {
1326+ if ( evt . type !== 'registered' || typeof evt . file !== 'string' || ! Number . isInteger ( evt . count ) ) return ;
1327+ sawRegisteredLine = true ;
1328+ const key = normalizeAccountedPath ( evt . file ) ;
1329+ display . set ( key , display . get ( key ) || evt . file ) ;
1330+ registered . set ( key , ( registered . get ( key ) || 0 ) + evt . count ) ;
1331+ } ) ;
1332+ // A registered file with no reported entry under its own spelling is matched
1333+ // to the single UNCLAIMED reported file that shares its basename, so a path
1334+ // spelling the two sides disagree on (case on a case-insensitive filesystem,
1335+ // a mount alias) never reads as "every test of this file was lost". Zero or
1336+ // several candidates leave it unmatched, which IS reported as a shortfall.
1337+ const reportedFor = ( key ) => {
1338+ if ( reported . has ( key ) ) return reported . get ( key ) ;
1339+ const candidates = [ ...reported . keys ( ) ] . filter (
1340+ ( k ) => ! registered . has ( k ) && basename ( k ) === basename ( key ) ,
1341+ ) ;
1342+ return candidates . length === 1 ? reported . get ( candidates [ 0 ] ) : 0 ;
1343+ } ;
1344+ const shortfalls = [ ] ;
1345+ let registeredTotal = 0 ;
1346+ let reportedTotal = 0 ;
1347+ for ( const [ key , count ] of registered ) {
1348+ const got = reportedFor ( key ) ;
1349+ registeredTotal += count ;
1350+ reportedTotal += Math . min ( got , count ) ;
1351+ if ( count > got ) shortfalls . push ( { file : display . get ( key ) , registered : count , reported : got } ) ;
1352+ }
1353+ return {
1354+ available : eventsRead && ledgerRead && sawRegisteredLine ,
1355+ shortfalls,
1356+ registeredTotal,
1357+ reportedTotal,
1358+ } ;
1359+ }
1360+
1361+ // #4031: the loud failure for a chunk whose registered tests are not all
1362+ // accounted for. Names the chunk and the counts, per file and in total.
1363+ function formatAccountingFailure ( chunkNumber , chunkCount , accounting ) {
1364+ const lost = accounting . registeredTotal - accounting . reportedTotal ;
1365+ const lines = accounting . shortfalls . map (
1366+ ( s ) => ` ${ basename ( s . file ) } : ${ s . registered } registered, ${ s . reported } reported (${ s . registered - s . reported } unaccounted)` ,
1367+ ) ;
1368+ return (
1369+ `run-tests: chunk ${ chunkNumber } /${ chunkCount } FAILED test accounting — ` +
1370+ `${ accounting . registeredTotal } tests registered, ${ accounting . reportedTotal } reported ` +
1371+ `(${ lost } unaccounted). The runner exited 0, but results for tests that registered never ` +
1372+ `reached the report (--test-force-exit can end the runner before a test file's results ` +
1373+ `are read, nodejs/node#64833), so a green count here would be a lower bound.\n${ lines . join ( '\n' ) } `
1374+ ) ;
1375+ }
1376+
12541377// #3889: ranks a killed chunk's files heaviest-first using the same weigher
12551378// the packer used to build the chunk, and flags any file the timings table
12561379// has no measurement for at all (as opposed to one that IS measured but
@@ -1964,7 +2087,15 @@ async function main() {
19642087 ] ;
19652088 const reporterOverhead = reporterArgs . reduce ( ( sum , a ) => sum + a . length + 1 , 0 ) ;
19662089
1967- const FIXED_OVERHEAD = process . execPath . length + '--test' . length + concurrency . length + ( forceExit ? '--test-force-exit' . length + 1 : 0 ) + reporterOverhead + 8 ;
2090+ // #4031: the registration-ledger preload (see analyzeChunkAccounting). Its
2091+ // per-chunk output path travels via env (GSD_RUN_TESTS_LEDGER_FILE), like the
2092+ // events path, so only this fixed `--require <path>` counts toward argv.
2093+ const ledgerPreloadPath = join ( __dirname , 'lib' , 'test-registration-ledger.cjs' ) ;
2094+ const ledgerArgs = [ '--require' , ledgerPreloadPath ] ;
2095+ const ledgerOverhead = ledgerArgs . reduce ( ( sum , a ) => sum + a . length + 1 , 0 ) ;
2096+ const ledgerPathFor = ( i ) => join ( eventsDir , `chunk-${ String ( i ) . padStart ( 3 , '0' ) } .ledger.ndjson` ) ;
2097+
2098+ const FIXED_OVERHEAD = process . execPath . length + '--test' . length + concurrency . length + ( forceExit ? '--test-force-exit' . length + 1 : 0 ) + reporterOverhead + ledgerOverhead + 8 ;
19682099
19692100 // Convert the ms-denominated isolation bar into the packer's weight units
19702101 // by dividing by the live table's own mean duration — see the comment
@@ -2047,6 +2178,7 @@ async function main() {
20472178 console . error ( `run-tests: chunk ${ i + 1 } /${ chunks . length } — ${ chunks [ i ] . length } files` ) ;
20482179 }
20492180 const chunkEventsPath = eventsPathFor ( i ) ;
2181+ const chunkLedgerPath = ledgerPathFor ( i ) ;
20502182 const chunkStartedAt = process . hrtime . bigint ( ) ;
20512183 // #4936: the timeout diagnostic is printed from runChunk's own timer the
20522184 // moment the bound is exceeded — not from the result, which only arrives
@@ -2128,10 +2260,15 @@ async function main() {
21282260 ...( forceExit ? [ '--test-force-exit' ] : [ ] ) ,
21292261 concurrency ,
21302262 ...reporterArgs ,
2263+ ...ledgerArgs ,
21312264 ...chunks [ i ] ,
21322265 ] ,
21332266 {
2134- env : { ...process . env , GSD_RUN_TESTS_EVENTS_FILE : chunkEventsPath } ,
2267+ env : {
2268+ ...process . env ,
2269+ GSD_RUN_TESTS_EVENTS_FILE : chunkEventsPath ,
2270+ GSD_RUN_TESTS_LEDGER_FILE : chunkLedgerPath ,
2271+ } ,
21352272 timeoutMs : chunkTimeoutMs ,
21362273 graceMs : chunkKillGraceMs ,
21372274 onTimeout : reportChunkTimeout ,
@@ -2152,7 +2289,26 @@ async function main() {
21522289 ) ;
21532290 }
21542291 }
2292+ // #4031: a chunk that exited 0 must also ACCOUNT for every test its files
2293+ // registered. Run before the success path below, which deletes the events
2294+ // file this reads; a shortfall turns the chunk into an ordinary failure
2295+ // (non-timeout, so the remaining chunks still run and every failure is
2296+ // visible in one pass).
2297+ let accountingFailed = false ;
21552298 if ( ! result . timedOut && result . code === 0 && ! result . signal && ! result . error ) {
2299+ const accounting = analyzeChunkAccounting ( chunkEventsPath , chunkLedgerPath ) ;
2300+ if ( accounting . shortfalls . length > 0 ) {
2301+ accountingFailed = true ;
2302+ console . error ( formatAccountingFailure ( i + 1 , chunks . length , accounting ) ) ;
2303+ } else if ( ! accounting . available ) {
2304+ console . error (
2305+ `run-tests: WARNING: chunk ${ i + 1 } /${ chunks . length } could not be accounted — the ` +
2306+ 'registration ledger or the reporter events file is missing or empty, so a ' +
2307+ 'dropped result would not be detected for this chunk.' ,
2308+ ) ;
2309+ }
2310+ }
2311+ if ( ! accountingFailed && ! result . timedOut && result . code === 0 && ! result . signal && ! result . error ) {
21562312 console . error (
21572313 `run-tests: chunk ${ i + 1 } /${ chunks . length } completed in ${ elapsedMs . toFixed ( 0 ) } ms` ,
21582314 ) ;
@@ -2294,6 +2450,10 @@ module.exports = {
22942450 ISOLATION_BUDGET_FRACTION ,
22952451 partitionIsolatedFiles,
22962452 analyzeChunkEvents,
2453+ // #4031: per-file registered-vs-reported accounting, exported so the
2454+ // shortfall and accounted arms are unit-testable on synthetic ledgers.
2455+ analyzeChunkAccounting,
2456+ formatAccountingFailure,
22972457 // #4936: the chunk watchdog, exported so its arms are unit-testable with
22982458 // injected spawn/platform seams (a real wedged Windows child is not
22992459 // reproducible on demand).
0 commit comments