1- import { cp , mkdir , unlink } from 'node:fs/promises' ;
1+ import {
2+ cp ,
3+ mkdir ,
4+ unlink ,
5+ writeFile ,
6+ } from 'node:fs/promises' ;
27import path from 'node:path' ;
38import process from 'node:process' ;
49import test from 'ava' ;
@@ -227,7 +232,7 @@ if (isWindows) {
227232
228233 const environmentName = 'NoDefaultCurrentDirectoryInExePath' ;
229234 const originalValue = process . env [ environmentName ] ;
230- process . env [ environmentName ] = '1' ;
235+ delete process . env [ environmentName ] ;
231236 t . teardown ( async ( ) => {
232237 if ( originalValue === undefined ) {
233238 delete process . env [ environmentName ] ;
@@ -246,13 +251,59 @@ if (isWindows) {
246251 PathExt : '.EXE' ,
247252 } ,
248253 } ;
254+ const { stdout : currentDirectoryStdout } = await execa ( command , [ '--print' , 'process.execPath' ] , options ) ;
255+ t . is ( currentDirectoryStdout . toLowerCase ( ) , currentDirectoryExecutable . toLowerCase ( ) ) ;
256+
257+ const { stdout : currentDirectoryStdoutSync } = execaSync ( command , [ '--print' , 'process.execPath' ] , options ) ;
258+ t . is ( currentDirectoryStdoutSync . toLowerCase ( ) , currentDirectoryExecutable . toLowerCase ( ) ) ;
259+
260+ process . env [ environmentName ] = '1' ;
249261 const { stdout} = await execa ( command , [ '--print' , 'process.execPath' ] , options ) ;
250262 t . is ( stdout . toLowerCase ( ) , pathExecutable . toLowerCase ( ) ) ;
251263
252264 const { stdout : stdoutSync } = execaSync ( command , [ '--print' , 'process.execPath' ] , options ) ;
253265 t . is ( stdoutSync . toLowerCase ( ) , pathExecutable . toLowerCase ( ) ) ;
254266 } ) ;
255267
268+ test . serial ( 'Uses the resolved batch file with NoDefaultCurrentDirectoryInExePath' , async t => {
269+ const binaryDirectory = path . join ( FIXTURES_DIRECTORY , 'node_modules' , '.bin' ) ;
270+ await mkdir ( binaryDirectory , { recursive : true } ) ;
271+ const command = 'batch-current-directory' ;
272+ const currentDirectoryBatchFile = path . join ( FIXTURES_DIRECTORY , `${ command } .cmd` ) ;
273+ const pathBatchFile = path . join ( binaryDirectory , `${ command } .cmd` ) ;
274+ await Promise . all ( [
275+ writeFile ( currentDirectoryBatchFile , '@echo current directory' ) ,
276+ writeFile ( pathBatchFile , '@echo PATH' ) ,
277+ ] ) ;
278+
279+ const environmentName = 'NoDefaultCurrentDirectoryInExePath' ;
280+ const originalValue = process . env [ environmentName ] ;
281+ process . env [ environmentName ] = '1' ;
282+ t . teardown ( async ( ) => {
283+ if ( originalValue === undefined ) {
284+ delete process . env [ environmentName ] ;
285+ } else {
286+ process . env [ environmentName ] = originalValue ;
287+ }
288+
289+ await Promise . all ( [ unlink ( currentDirectoryBatchFile ) , unlink ( pathBatchFile ) ] ) ;
290+ } ) ;
291+
292+ const options = {
293+ cwd : FIXTURES_DIRECTORY ,
294+ extendEnv : false ,
295+ env : {
296+ Path : binaryDirectory ,
297+ PathExt : '.CMD' ,
298+ } ,
299+ } ;
300+ const { stdout} = await execa ( command , options ) ;
301+ t . is ( stdout , 'PATH' ) ;
302+
303+ const { stdout : stdoutSync } = execaSync ( command , options ) ;
304+ t . is ( stdoutSync , 'PATH' ) ;
305+ } ) ;
306+
256307 test ( 'Does not search PATH for drive-relative commands' , async t => {
257308 const binaryDirectory = path . join ( FIXTURES_DIRECTORY , 'node_modules' , '.bin' ) ;
258309 await mkdir ( binaryDirectory , { recursive : true } ) ;
0 commit comments