11import { spawnSync } from "node:child_process"
2- import { readFileSync , writeFileSync } from "node:fs"
2+ import { mkdtempSync , readFileSync , rmSync , writeFileSync } from "node:fs"
3+ import { tmpdir } from "node:os"
34import { resolve } from "node:path"
45import process from "node:process"
5- import { test } from "node:test"
6-
7- if ( process . env . GITHUB_ACTIONS !== "true" && process . env . ZOO_ALLOW_COVERAGE_INPUT_MUTATION !== "true" )
8- throw new Error ( "Coverage cache input verification requires an ephemeral runner or explicit local opt-in" )
6+ import { after , test } from "node:test"
97
108const root = resolve ( import . meta. dirname , "../.." )
119const pnpm = process . platform === "win32" ? process . env . npm_execpath : "pnpm"
1210if ( ! pnpm ) throw new Error ( "pnpm executable path is unavailable" )
1311const command = process . platform === "win32" ? process . execPath : pnpm
1412const args = process . platform === "win32" ? [ pnpm ] : [ ]
1513const lanes = [ "api" , "core" , "services" , "misc" , "tree-sitter" ]
14+ const probeRoot = mkdtempSync ( resolve ( tmpdir ( ) , "zoo-code-coverage-cache-inputs-" ) )
15+
16+ const git = ( gitArgs ) => {
17+ const result = spawnSync ( "git" , gitArgs , { cwd : root , encoding : "utf8" } )
18+ if ( result . status !== 0 ) {
19+ const details = [ result . error ?. message , result . signal , result . stderr , result . stdout ] . filter ( Boolean ) . join ( "\n" )
20+ throw new Error ( details || `git exited with status ${ result . status ?? "unknown" } ` )
21+ }
22+ }
23+
24+ git ( [ "worktree" , "add" , "--detach" , probeRoot , "HEAD" ] )
25+ after ( ( ) => {
26+ try {
27+ git ( [ "worktree" , "remove" , "--force" , probeRoot ] )
28+ } finally {
29+ rmSync ( probeRoot , { recursive : true , force : true } )
30+ }
31+ } )
1632
1733const coverageTasks = ( ) => {
1834 const result = spawnSync (
1935 command ,
20- [ ...args , "turbo" , "run" , ...lanes . map ( ( lane ) => `test:coverage:${ lane } ` ) , "--filter=zoo-code" , "--dry=json" ] ,
36+ [
37+ ...args ,
38+ "turbo" ,
39+ "--cwd" ,
40+ probeRoot ,
41+ "run" ,
42+ ...lanes . map ( ( lane ) => `test:coverage:${ lane } ` ) ,
43+ "--filter=zoo-code" ,
44+ "--dry=json" ,
45+ "--no-daemon" ,
46+ ] ,
2147 { cwd : root , encoding : "utf8" } ,
2248 )
2349 if ( result . status !== 0 ) {
@@ -35,13 +61,13 @@ const coverageTasks = () => {
3561const hashes = ( ) => Object . fromEntries ( coverageTasks ( ) . map ( ( task ) => [ task . task . split ( ":" ) . at ( - 1 ) , task . hash ] ) )
3662
3763const withChangedFiles = ( paths , run ) => {
38- const originals = paths . map ( ( path ) => [ path , readFileSync ( resolve ( root , path ) , "utf8" ) ] )
64+ const originals = paths . map ( ( path ) => [ path , readFileSync ( resolve ( probeRoot , path ) , "utf8" ) ] )
3965 try {
4066 for ( const [ path , contents ] of originals )
41- writeFileSync ( resolve ( root , path ) , `${ contents } \n// cache-input-test\n` )
67+ writeFileSync ( resolve ( probeRoot , path ) , `${ contents } \n// cache-input-test\n` )
4268 return run ( )
4369 } finally {
44- for ( const [ path , contents ] of originals ) writeFileSync ( resolve ( root , path ) , contents )
70+ for ( const [ path , contents ] of originals ) writeFileSync ( resolve ( probeRoot , path ) , contents )
4571 }
4672}
4773
0 commit comments