@@ -26,6 +26,8 @@ import {
2626 parseVitestTestFiles ,
2727 preferDirectTestFiles ,
2828 parseStackedMap ,
29+ readStackedMap ,
30+ summarizeReinstallFailure ,
2931 dependencyManifestDrift ,
3032 alignExecutionTree ,
3133 resolveCiInvocation ,
@@ -67,7 +69,12 @@ describe("mutation testing workflow", () => {
6769 assert . ok (
6870 workflow . includes ( 'PR_HEAD_SHA="$(git rev-parse "$HEAD_SHA^2" 2>/dev/null || git rev-parse "$HEAD_SHA")"' ) ,
6971 )
70- assert . ok ( workflow . includes ( '--stacked-map "$STACKED_MAP"' ) )
72+ // The map crosses jobs as a file, never as an argument or an environment string: Linux caps one
73+ // argument/env string at 128 KiB (MAX_ARG_STRLEN), and a map past that cap makes the step
74+ // unstartable rather than merely large.
75+ assert . ok ( workflow . includes ( "--stacked-map-file .mutation/stacked-map.json" ) )
76+ assert . ok ( ! workflow . includes ( '--stacked-map "$STACKED_MAP"' ) )
77+ assert . ok ( workflow . includes ( "name: mutation-stacked-map" ) )
7178 assert . ok ( workflow . includes ( '--pr-head "$PR_HEAD_SHA"' ) )
7279 // The open pull request map must cover every page, otherwise a parent unit beyond the first
7380 // page is missing and the gate charges the whole unmerged chain again.
@@ -97,20 +104,27 @@ describe("mutation testing workflow", () => {
97104 // in a job that never checks out or runs pull-request code; only the filtered map crosses over.
98105 const gateJob = workflow . slice ( workflow . indexOf ( " mutation-diff:" ) )
99106 assert . ok ( ! gateJob . includes ( "GH_TOKEN" ) )
100- assert . ok ( workflow . includes ( "STACKED_MAP: ${{ needs.stacked_map.outputs.stacked_map }}" ) )
107+ // The map arrives from the artifact the map job publishes, into the job that runs the gate.
108+ assert . ok ( gateJob . includes ( "uses: actions/download-artifact@" ) )
109+ assert . ok ( gateJob . includes ( "name: mutation-stacked-map" ) )
110+ assert . ok ( ! workflow . includes ( "needs.stacked_map.outputs.stacked_map" ) )
101111 const mapJob = workflow . slice ( workflow . indexOf ( " stacked_map:" ) , workflow . indexOf ( " mutation-diff:" ) )
102112 assert . ok ( mapJob . includes ( "GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}" ) )
103113 assert . ok ( ! mapJob . includes ( "actions/checkout" ) )
104114 assert . ok ( ! mapJob . includes ( "setup-node-pnpm" ) )
105115 assert . ok ( ! mapJob . includes ( "pnpm test:mutation-ci" ) )
106116 assert . ok ( workflow . includes ( "needs: stacked_map" ) )
107- // The job output is only real if the step it reads from carries that id; without it the map is
108- // always an empty string and the gate silently falls back to the event base.
109- assert . ok ( mapJob . includes ( "id: read_map" ) )
110- // Publish as one record from a validated value, and do not continue past a failed write.
111- assert . ok ( mapJob . includes ( "printf 'stacked_map<<EOF" ) )
112- assert . ok ( ! mapJob . includes ( "Could not publish the stacked map" ) )
113- assert . equal ( workflow . match ( / c o n t i n u e - o n - e r r o r : t r u e / g) ?. length , 1 )
117+ // The map is validated before it is written, and the write is a plain file in the job that
118+ // uploads it: no step output, no job output, and no argument for the gate to receive.
119+ assert . ok ( mapJob . includes ( `printf '%s\\n' "$STACKED_MAP" > stacked-map.json` ) )
120+ assert . ok ( mapJob . includes ( "path: stacked-map.json" ) )
121+ assert . ok ( mapJob . includes ( "if-no-files-found: error" ) )
122+ assert . ok ( ! mapJob . includes ( "GITHUB_OUTPUT" ) )
123+ // Both jobs need actions:write - one to publish the map, one to read it and publish the reports.
124+ assert . equal ( workflow . match ( / a c t i o n s : w r i t e / g) ?. length , 2 )
125+ // A missing map artifact degrades to the event base inside the script, so the download step must
126+ // not fail the job; the report upload keeps its own tolerance.
127+ assert . equal ( workflow . match ( / c o n t i n u e - o n - e r r o r : t r u e / g) ?. length , 2 )
114128 assert . equal ( workflow . match ( / C o u l d n o t w r i t e t h e j o b s u m m a r y / g) ?. length , 2 )
115129 const script = fs . readFileSync ( path . join ( repositoryRoot , "scripts/stryker-diff.mjs" ) , "utf8" )
116130 assert . ok ( script . includes ( "appendSummary([], manifest.advisories, manifest)" ) )
@@ -457,6 +471,100 @@ describe("stacked unit base resolution", () => {
457471 }
458472 } )
459473
474+ it ( "matches a stacked parent by base branch when another pull request shares its head" , ( ) => {
475+ // Two open pull requests can point at the same commit while targeting different branches. Stopping
476+ // at the first head-SHA match rejects that parent for the wrong reason and falls back to the event
477+ // base, charging this unit every unmerged ancestor of a branch it is not merging into.
478+ const { repository, eventBaseSha, parentSha, childSha } = createSyntheticStack ( )
479+
480+ try {
481+ const map = [
482+ { number : 9 , headSha : parentSha , baseRef : "feature/other" } ,
483+ { number : 1 , headSha : parentSha , baseRef : "main" } ,
484+ ]
485+
486+ const unitOnMain = resolveStackedUnitBase ( repository , eventBaseSha , childSha , map , "main" )
487+ assert . equal ( unitOnMain . baseSha , parentSha )
488+ assert . equal ( unitOnMain . stackedOn , 1 )
489+
490+ // The same map read by a unit that targets the feature branch picks the other pull request,
491+ // which is the only parent whose own gate measured against this unit's base.
492+ const unitOnFeature = resolveStackedUnitBase ( repository , eventBaseSha , childSha , map , "feature/other" )
493+ assert . equal ( unitOnFeature . baseSha , parentSha )
494+ assert . equal ( unitOnFeature . stackedOn , 9 )
495+
496+ // A map whose entries match no base still degrades instead of guessing.
497+ const noMatch = resolveStackedUnitBase ( repository , eventBaseSha , childSha , map , "release/none" )
498+ assert . equal ( noMatch . baseSha , eventBaseSha )
499+ assert . equal ( noMatch . stackedOn , null )
500+ } finally {
501+ fs . rmSync ( repository , { recursive : true , force : true } )
502+ }
503+ } )
504+
505+ it ( "reads the stacked map from the artifact file the workflow publishes" , ( ) => {
506+ const directory = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "stacked-map-" ) )
507+ try {
508+ fs . writeFileSync (
509+ path . join ( directory , "stacked-map.json" ) ,
510+ '[{ "number": 1, "headSha": "' + "a" . repeat ( 40 ) + '", "draft": false, "baseRef": "main" }]' ,
511+ )
512+ const fromFile = parseStackedMap ( readStackedMap ( directory , "[]" , "stacked-map.json" ) )
513+ assert . deepEqual ( fromFile , [ { number : 1 , headSha : "a" . repeat ( 40 ) , draft : false , baseRef : "main" } ] )
514+
515+ // The inline form stays available for local runs, where no artifact exists.
516+ const inline = parseStackedMap (
517+ readStackedMap (
518+ directory ,
519+ '[{ "number": 2, "headSha": "' + "b" . repeat ( 40 ) + '", "draft": false, "baseRef": "main" }]' ,
520+ undefined ,
521+ ) ,
522+ )
523+ assert . deepEqual ( inline , [ { number : 2 , headSha : "b" . repeat ( 40 ) , draft : false , baseRef : "main" } ] )
524+
525+ // A missing artifact must not stop the gate. Falling back to the event base charges this unit
526+ // every unmerged ancestor - stricter than the intended measurement, never looser - and the
527+ // warning records which case ran.
528+ const warnings = [ ]
529+ const warn = console . warn
530+ console . warn = ( message ) => warnings . push ( message )
531+ try {
532+ assert . equal ( readStackedMap ( directory , '[{ "number": 3 }]' , "absent.json" ) , "" )
533+ } finally {
534+ console . warn = warn
535+ }
536+ assert . equal ( warnings . length , 1 )
537+ assert . ok ( warnings [ 0 ] . includes ( "the artifact was not published" ) )
538+ assert . deepEqual ( parseStackedMap ( "" ) , [ ] )
539+ } finally {
540+ fs . rmSync ( directory , { recursive : true , force : true } )
541+ }
542+ } )
543+
544+ it ( "reports the stacked base in the job summary when the aligned reinstall fails" , ( ) => {
545+ const directory = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "reinstall-summary-" ) )
546+ const summaryPath = path . join ( directory , "step-summary.md" )
547+ const previous = process . env . GITHUB_STEP_SUMMARY
548+ process . env . GITHUB_STEP_SUMMARY = summaryPath
549+ const warnings = [ ]
550+ const warn = console . warn
551+ console . warn = ( message ) => warnings . push ( message )
552+ try {
553+ summarizeReinstallFailure ( 7 , [ "src/package.json" , "pnpm-lock.yaml" ] , new Error ( "pnpm install exited 1" ) )
554+ const summary = fs . readFileSync ( summaryPath , "utf8" )
555+ // The whole point of the summary here: which base this unit was measured against.
556+ assert . ok ( summary . includes ( "Stacked unit: measured against the head of parent PR #7" ) )
557+ assert . ok ( summary . includes ( "src/package.json, pnpm-lock.yaml" ) )
558+ assert . ok ( summary . includes ( "pnpm install exited 1" ) )
559+ assert . equal ( warnings . length , 1 )
560+ } finally {
561+ console . warn = warn
562+ if ( previous === undefined ) delete process . env . GITHUB_STEP_SUMMARY
563+ else process . env . GITHUB_STEP_SUMMARY = previous
564+ fs . rmSync ( directory , { recursive : true , force : true } )
565+ }
566+ } )
567+
460568 it ( "degrades to the event base for an unparsable stacked map" , ( ) => {
461569 assert . deepEqual ( parseStackedMap ( "not json" ) , [ ] )
462570 assert . deepEqual ( parseStackedMap ( '{"number": 1}' ) , [ ] )
0 commit comments