|
1 | 1 | use gix_testtools::Result;
|
2 | 2 |
|
3 | 3 | mod prepare {
|
| 4 | + #[cfg(windows)] |
| 5 | + const SH: &str = "sh"; |
| 6 | + #[cfg(not(windows))] |
| 7 | + const SH: &str = "/bin/sh"; |
| 8 | + |
4 | 9 | fn quoted(input: &[&str]) -> String {
|
5 | 10 | input.iter().map(|s| format!("\"{s}\"")).collect::<Vec<_>>().join(" ")
|
6 | 11 | }
|
| 12 | + |
| 13 | + #[test] |
| 14 | + fn empty() { |
| 15 | + let cmd = std::process::Command::from(gix_command::prepare("")); |
| 16 | + assert_eq!(format!("{cmd:?}"), "\"\""); |
| 17 | + } |
7 | 18 | #[test]
|
8 | 19 | fn single_and_multiple_arguments() {
|
9 | 20 | let cmd = std::process::Command::from(gix_command::prepare("ls").arg("first").args(["second", "third"]));
|
10 | 21 | assert_eq!(format!("{cmd:?}"), quoted(&["ls", "first", "second", "third"]));
|
11 | 22 | }
|
| 23 | + |
| 24 | + #[test] |
| 25 | + fn single_and_multiple_arguments_as_part_of_command() { |
| 26 | + let cmd = std::process::Command::from(gix_command::prepare("ls first second third")); |
| 27 | + assert_eq!( |
| 28 | + format!("{cmd:?}"), |
| 29 | + quoted(&["ls first second third"]), |
| 30 | + "without shell, this is an invalid command" |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + #[test] |
| 35 | + fn single_and_multiple_arguments_as_part_of_command_with_shell() { |
| 36 | + let cmd = std::process::Command::from(gix_command::prepare("ls first second third").with_shell()); |
| 37 | + assert_eq!( |
| 38 | + format!("{cmd:?}"), |
| 39 | + quoted(&[SH, "-c", "ls first second third", "--"]), |
| 40 | + "with shell, this works as it performs word splitting" |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + #[test] |
| 45 | + fn single_and_complex_arguments_as_part_of_command_with_shell() { |
| 46 | + let cmd = std::process::Command::from(gix_command::prepare("ls --foo \"a b\"").arg("additional").with_shell()); |
| 47 | + assert_eq!( |
| 48 | + format!("{cmd:?}"), |
| 49 | + format!("\"{SH}\" \"-c\" \"ls --foo \\\"a b\\\" \\\"$@\\\"\" \"--\" \"additional\""), |
| 50 | + "with shell, this works as it performs word splitting" |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn tilde_path_and_multiple_arguments_as_part_of_command_with_shell() { |
| 56 | + let cmd = std::process::Command::from(gix_command::prepare("~/bin/exe --foo \"a b\"").with_shell()); |
| 57 | + assert_eq!( |
| 58 | + format!("{cmd:?}"), |
| 59 | + format!("\"{SH}\" \"-c\" \"~/bin/exe --foo \\\"a b\\\"\" \"--\""), |
| 60 | + "this always needs a shell as we need tilde expansion" |
| 61 | + ); |
| 62 | + } |
12 | 63 | }
|
13 | 64 |
|
14 | 65 | mod spawn {
|
|
0 commit comments