@@ -222,10 +222,13 @@ mod context {
222
222
}
223
223
224
224
mod prepare {
225
- #[ cfg( windows) ]
226
- const SH : & str = "sh" ;
227
- #[ cfg( not( windows) ) ]
228
- const SH : & str = "/bin/sh" ;
225
+ use once_cell:: sync:: Lazy ;
226
+
227
+ static SH : Lazy < & ' static str > = Lazy :: new ( || {
228
+ gix_path:: env:: shell ( )
229
+ . to_str ( )
230
+ . expect ( "`prepare` tests must be run where 'sh' path is valid Unicode" )
231
+ } ) ;
229
232
230
233
fn quoted ( input : & [ & str ] ) -> String {
231
234
input. iter ( ) . map ( |s| format ! ( "\" {s}\" " ) ) . collect :: < Vec < _ > > ( ) . join ( " " )
@@ -275,7 +278,7 @@ mod prepare {
275
278
if cfg!( windows) {
276
279
quoted( & [ "ls" , "first" , "second" , "third" ] )
277
280
} else {
278
- quoted( & [ SH , "-c" , "ls first second third" , "--" ] )
281
+ quoted( & [ * SH , "-c" , "ls first second third" , "--" ] )
279
282
} ,
280
283
"with shell, this works as it performs word splitting"
281
284
) ;
@@ -311,7 +314,8 @@ mod prepare {
311
314
if cfg!( windows) {
312
315
quoted( & [ "ls" , "--foo" , "a b" , "additional" ] )
313
316
} else {
314
- format!( r#""{SH}" "-c" "ls --foo \"a b\" \"$@\"" "--" "additional""# )
317
+ let sh = * SH ;
318
+ format!( r#""{sh}" "-c" "ls --foo \"a b\" \"$@\"" "--" "additional""# )
315
319
} ,
316
320
"with shell, this works as it performs word splitting"
317
321
) ;
@@ -334,7 +338,7 @@ mod prepare {
334
338
let cmd = std:: process:: Command :: from (
335
339
gix_command:: prepare ( "ls --foo=\" a b\" " ) . command_may_be_shell_script_disallow_manual_argument_splitting ( ) ,
336
340
) ;
337
- assert_eq ! ( format!( "{cmd:?}" ) , quoted( & [ SH , "-c" , r#"ls --foo=\"a b\""# , "--" ] ) ) ;
341
+ assert_eq ! ( format!( "{cmd:?}" ) , quoted( & [ * SH , "-c" , r#"ls --foo=\"a b\""# , "--" ] ) ) ;
338
342
}
339
343
340
344
#[ test]
@@ -347,7 +351,7 @@ mod prepare {
347
351
) ;
348
352
assert_eq ! (
349
353
format!( "{cmd:?}" ) ,
350
- quoted( & [ SH , "-c" , "ls \\ \" $@\\ \" " , "--" , "--foo=a b" ] )
354
+ quoted( & [ * SH , "-c" , "ls \\ \" $@\\ \" " , "--" , "--foo=a b" ] )
351
355
) ;
352
356
}
353
357
@@ -362,7 +366,7 @@ mod prepare {
362
366
) ;
363
367
assert_eq ! (
364
368
format!( "{cmd:?}" ) ,
365
- quoted( & [ SH , "-c" , "\\ 'ls\\ ' \\ \" $@\\ \" " , "--" , "--foo=a b" ] ) ,
369
+ quoted( & [ * SH , "-c" , "\\ 'ls\\ ' \\ \" $@\\ \" " , "--" , "--foo=a b" ] ) ,
366
370
"looks strange thanks to debug printing, but is the right amount of quotes actually"
367
371
) ;
368
372
}
@@ -379,7 +383,7 @@ mod prepare {
379
383
assert_eq ! (
380
384
format!( "{cmd:?}" ) ,
381
385
quoted( & [
382
- SH ,
386
+ * SH ,
383
387
"-c" ,
384
388
"\\ 'C:\\ \\ Users\\ \\ O\\ '\\ \\ \\ '\\ 'Shaughnessy\\ \\ with space.exe\\ ' \\ \" $@\\ \" " ,
385
389
"--" ,
@@ -394,9 +398,10 @@ mod prepare {
394
398
let cmd = std:: process:: Command :: from (
395
399
gix_command:: prepare ( "ls --foo=~/path" ) . command_may_be_shell_script_allow_manual_argument_splitting ( ) ,
396
400
) ;
401
+ let sh = * SH ;
397
402
assert_eq ! (
398
403
format!( "{cmd:?}" ) ,
399
- format!( r#""{SH }" "-c" "ls --foo=~/path" "--""# ) ,
404
+ format!( r#""{sh }" "-c" "ls --foo=~/path" "--""# ) ,
400
405
"splitting can also handle quotes"
401
406
) ;
402
407
}
@@ -405,9 +410,10 @@ mod prepare {
405
410
fn tilde_path_and_multiple_arguments_as_part_of_command_with_shell ( ) {
406
411
let cmd =
407
412
std:: process:: Command :: from ( gix_command:: prepare ( "~/bin/exe --foo \" a b\" " ) . command_may_be_shell_script ( ) ) ;
413
+ let sh = * SH ;
408
414
assert_eq ! (
409
415
format!( "{cmd:?}" ) ,
410
- format!( r#""{SH }" "-c" "~/bin/exe --foo \"a b\"" "--""# ) ,
416
+ format!( r#""{sh }" "-c" "~/bin/exe --foo \"a b\"" "--""# ) ,
411
417
"this always needs a shell as we need tilde expansion"
412
418
) ;
413
419
}
@@ -419,9 +425,10 @@ mod prepare {
419
425
. command_may_be_shell_script ( )
420
426
. arg ( "store" ) ,
421
427
) ;
428
+ let sh = * SH ;
422
429
assert_eq ! (
423
430
format!( "{cmd:?}" ) ,
424
- format!( r#""{SH }" "-c" "echo \"$@\" >&2" "--" "store""# ) ,
431
+ format!( r#""{sh }" "-c" "echo \"$@\" >&2" "--" "store""# ) ,
425
432
"this is how credential helpers have to work as for some reason they don't get '$@' added in Git.\
426
433
We deal with it by not doubling the '$@' argument, which seems more flexible."
427
434
) ;
@@ -435,9 +442,10 @@ mod prepare {
435
442
. with_quoted_command ( )
436
443
. arg ( "store" ) ,
437
444
) ;
445
+ let sh = * SH ;
438
446
assert_eq ! (
439
447
format!( "{cmd:?}" ) ,
440
- format!( r#""{SH }" "-c" "echo \"$@\" >&2" "--" "store""# )
448
+ format!( r#""{sh }" "-c" "echo \"$@\" >&2" "--" "store""# )
441
449
) ;
442
450
}
443
451
}
0 commit comments