@@ -58,11 +58,8 @@ process.exit(0);
5858 await writeFile ( cmdPath , cmdPayload , "utf8" ) ;
5959} ;
6060
61- test ( "sync expands brace patterns for git sparse-checkout" , async ( ) => {
62- const tmpRoot = path . join (
63- tmpdir ( ) ,
64- `docs-cache-brace-${ Date . now ( ) . toString ( 36 ) } ` ,
65- ) ;
61+ const createTestContext = async ( label ) => {
62+ const tmpRoot = path . join ( tmpdir ( ) , `${ label } -${ Date . now ( ) . toString ( 36 ) } ` ) ;
6663 const binDir = path . join ( tmpRoot , "bin" ) ;
6764 const logPath = path . join ( tmpRoot , "git.log" ) ;
6865 const cacheDir = path . join ( tmpRoot , ".docs" ) ;
@@ -77,6 +74,65 @@ test("sync expands brace patterns for git sparse-checkout", async () => {
7774 await writeGitShim ( binDir , logPath ) ;
7875 await writeFile ( logPath , "" , "utf8" ) ;
7976
77+ const cleanup = async ( ) => {
78+ await rm ( tmpRoot , { recursive : true , force : true } ) ;
79+ } ;
80+
81+ return {
82+ binDir,
83+ logPath,
84+ cacheDir,
85+ configPath,
86+ gitCacheRoot,
87+ repo,
88+ cleanup,
89+ } ;
90+ } ;
91+
92+ const withModifiedPath = async ( binDir , gitCacheRoot , fn ) => {
93+ const saved = {
94+ PATH : process . env . PATH ,
95+ Path : process . env . Path ,
96+ PATHEXT : process . env . PATHEXT ,
97+ DOCS_CACHE_GIT_DIR : process . env . DOCS_CACHE_GIT_DIR ,
98+ } ;
99+ const previousPath = process . env . PATH ?? process . env . Path ;
100+ const nextPath = previousPath
101+ ? `${ binDir } ${ path . delimiter } ${ previousPath } `
102+ : binDir ;
103+
104+ process . env . PATH = nextPath ;
105+ process . env . Path = nextPath ;
106+ if ( process . platform === "win32" ) {
107+ process . env . PATHEXT = ".CMD;.BAT;.EXE;.COM" ;
108+ }
109+ process . env . DOCS_CACHE_GIT_DIR = gitCacheRoot ;
110+
111+ try {
112+ return await fn ( ) ;
113+ } finally {
114+ process . env . PATH = saved . PATH ;
115+ process . env . Path = saved . Path ;
116+ process . env . PATHEXT = saved . PATHEXT ;
117+ process . env . DOCS_CACHE_GIT_DIR = saved . DOCS_CACHE_GIT_DIR ;
118+ }
119+ } ;
120+
121+ const getSparsePatterns = ( args ) => {
122+ const patternIndex = args . indexOf ( "set" ) ;
123+ if ( patternIndex === - 1 ) return [ ] ;
124+ const noConeIndex = args . indexOf ( "--no-cone" ) ;
125+ const patternsStart =
126+ noConeIndex !== - 1 && noConeIndex > patternIndex
127+ ? noConeIndex + 1
128+ : patternIndex + 1 ;
129+ return args . slice ( patternsStart ) . filter ( ( arg ) => ! arg . startsWith ( "--" ) ) ;
130+ } ;
131+
132+ test ( "sync expands brace patterns for git sparse-checkout" , async ( ) => {
133+ const { binDir, logPath, cacheDir, configPath, gitCacheRoot, repo, cleanup } =
134+ await createTestContext ( "docs-cache-brace" ) ;
135+
80136 const config = {
81137 $schema :
82138 "https://raw.githubusercontent.com/fbosch/docs-cache/main/docs.config.schema.json" ,
@@ -93,29 +149,16 @@ test("sync expands brace patterns for git sparse-checkout", async () => {
93149 } ;
94150 await writeFile ( configPath , `${ JSON . stringify ( config , null , 2 ) } \n` , "utf8" ) ;
95151
96- const previousPath = process . env . PATH ?? process . env . Path ;
97- const previousPathExt = process . env . PATHEXT ;
98- const previousGitDir = process . env . DOCS_CACHE_GIT_DIR ;
99- const nextPath =
100- process . platform === "win32"
101- ? `${ binDir } ;${ previousPath ?? "" } `
102- : `${ binDir } :${ previousPath ?? "" } ` ;
103- const nextPathExt =
104- process . platform === "win32" ? ".CMD;.BAT;.EXE;.COM" : previousPathExt ;
105-
106- process . env . PATH = nextPath ;
107- process . env . Path = nextPath ;
108- process . env . PATHEXT = nextPathExt ;
109- process . env . DOCS_CACHE_GIT_DIR = gitCacheRoot ;
110-
111152 try {
112- await runSync ( {
113- configPath,
114- cacheDirOverride : cacheDir ,
115- json : false ,
116- lockOnly : false ,
117- offline : false ,
118- failOnMiss : false ,
153+ await withModifiedPath ( binDir , gitCacheRoot , async ( ) => {
154+ await runSync ( {
155+ configPath,
156+ cacheDirOverride : cacheDir ,
157+ json : false ,
158+ lockOnly : false ,
159+ offline : false ,
160+ failOnMiss : false ,
161+ } ) ;
119162 } ) ;
120163
121164 const logRaw = await readFile ( logPath , "utf8" ) ;
@@ -139,9 +182,7 @@ test("sync expands brace patterns for git sparse-checkout", async () => {
139182 const hasExpandedPatterns = sparseArgs . some ( ( args ) => {
140183 // Should have expanded **/*.{md,mdx,txt} into:
141184 // **/*.md, **/*.mdx, **/*.txt
142- const patternIndex = args . indexOf ( "set" ) ;
143- if ( patternIndex === - 1 ) return false ;
144- const patterns = args . slice ( patternIndex + 2 ) ; // skip "set" and "--no-cone"
185+ const patterns = getSparsePatterns ( args ) ;
145186 return (
146187 patterns . includes ( "**/*.md" ) &&
147188 patterns . includes ( "**/*.mdx" ) &&
@@ -154,32 +195,13 @@ test("sync expands brace patterns for git sparse-checkout", async () => {
154195 `Expected brace patterns to be expanded. Got: ${ JSON . stringify ( sparseArgs , null , 2 ) } ` ,
155196 ) ;
156197 } finally {
157- process . env . PATH = previousPath ;
158- process . env . Path = previousPath ;
159- process . env . PATHEXT = previousPathExt ;
160- process . env . DOCS_CACHE_GIT_DIR = previousGitDir ;
161- await rm ( tmpRoot , { recursive : true , force : true } ) ;
198+ await cleanup ( ) ;
162199 }
163200} ) ;
164201
165202test ( "sync expands default brace pattern when no include specified" , async ( ) => {
166- const tmpRoot = path . join (
167- tmpdir ( ) ,
168- `docs-cache-default-brace-${ Date . now ( ) . toString ( 36 ) } ` ,
169- ) ;
170- const binDir = path . join ( tmpRoot , "bin" ) ;
171- const logPath = path . join ( tmpRoot , "git.log" ) ;
172- const cacheDir = path . join ( tmpRoot , ".docs" ) ;
173- const configPath = path . join ( tmpRoot , "docs.config.json" ) ;
174- const gitCacheRoot = path . join ( tmpRoot , "git-cache" ) ;
175- const repo = "https://example.com/repo.git" ;
176- const repoHash = hashRepoUrl ( repo ) ;
177- const cachePath = path . join ( gitCacheRoot , repoHash ) ;
178-
179- await mkdir ( binDir , { recursive : true } ) ;
180- await mkdir ( cachePath , { recursive : true } ) ;
181- await writeGitShim ( binDir , logPath ) ;
182- await writeFile ( logPath , "" , "utf8" ) ;
203+ const { binDir, logPath, cacheDir, configPath, gitCacheRoot, repo, cleanup } =
204+ await createTestContext ( "docs-cache-default-brace" ) ;
183205
184206 const config = {
185207 $schema :
@@ -197,29 +219,16 @@ test("sync expands default brace pattern when no include specified", async () =>
197219 } ;
198220 await writeFile ( configPath , `${ JSON . stringify ( config , null , 2 ) } \n` , "utf8" ) ;
199221
200- const previousPath = process . env . PATH ?? process . env . Path ;
201- const previousPathExt = process . env . PATHEXT ;
202- const previousGitDir = process . env . DOCS_CACHE_GIT_DIR ;
203- const nextPath =
204- process . platform === "win32"
205- ? `${ binDir } ;${ previousPath ?? "" } `
206- : `${ binDir } :${ previousPath ?? "" } ` ;
207- const nextPathExt =
208- process . platform === "win32" ? ".CMD;.BAT;.EXE;.COM" : previousPathExt ;
209-
210- process . env . PATH = nextPath ;
211- process . env . Path = nextPath ;
212- process . env . PATHEXT = nextPathExt ;
213- process . env . DOCS_CACHE_GIT_DIR = gitCacheRoot ;
214-
215222 try {
216- await runSync ( {
217- configPath,
218- cacheDirOverride : cacheDir ,
219- json : false ,
220- lockOnly : false ,
221- offline : false ,
222- failOnMiss : false ,
223+ await withModifiedPath ( binDir , gitCacheRoot , async ( ) => {
224+ await runSync ( {
225+ configPath,
226+ cacheDirOverride : cacheDir ,
227+ json : false ,
228+ lockOnly : false ,
229+ offline : false ,
230+ failOnMiss : false ,
231+ } ) ;
223232 } ) ;
224233
225234 const logRaw = await readFile ( logPath , "utf8" ) ;
@@ -241,9 +250,7 @@ test("sync expands default brace pattern when no include specified", async () =>
241250 // Check that default brace pattern was expanded
242251 const sparseArgs = sparseCheckoutCalls . map ( ( call ) => JSON . parse ( call ) ) ;
243252 const hasExpandedDefaults = sparseArgs . some ( ( args ) => {
244- const patternIndex = args . indexOf ( "set" ) ;
245- if ( patternIndex === - 1 ) return false ;
246- const patterns = args . slice ( patternIndex + 2 ) ;
253+ const patterns = getSparsePatterns ( args ) ;
247254 // Default is **/*.{md,mdx,markdown,mkd,txt,rst,adoc,asciidoc}
248255 return (
249256 patterns . includes ( "**/*.md" ) &&
@@ -258,10 +265,6 @@ test("sync expands default brace pattern when no include specified", async () =>
258265 `Expected default brace patterns to be expanded. Got: ${ JSON . stringify ( sparseArgs , null , 2 ) } ` ,
259266 ) ;
260267 } finally {
261- process . env . PATH = previousPath ;
262- process . env . Path = previousPath ;
263- process . env . PATHEXT = previousPathExt ;
264- process . env . DOCS_CACHE_GIT_DIR = previousGitDir ;
265- await rm ( tmpRoot , { recursive : true , force : true } ) ;
268+ await cleanup ( ) ;
266269 }
267270} ) ;
0 commit comments