Skip to content

Commit 1ff26b9

Browse files
committed
add some tests to be clearer about the kind of command that is created (#1103)
Note that for a moment, there was the idea about using `shell-words` split certain invocations ourselves to avoid having to use a shell. This is great on windows, where a shell might not be available. On linux though this also means that some shell-builtins might not be not be found if this is used, which limits the approach to windows, and even there it might reduce compatibility. Thus, let's not do it.
1 parent 3ff1827 commit 1ff26b9

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

gix-command/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ mod prepare {
3434

3535
/// Builder
3636
impl Prepare {
37-
/// If called, the command will not be executed directly, but with `sh`.
37+
/// If called, the command will not be executed directly, but with `sh`, but ony if the
38+
/// command passed to [`prepare`](super::prepare()) requires this.
3839
///
3940
/// This also allows to pass shell scripts as command, or use commands that contain arguments which are subsequently
4041
/// parsed by `sh`.

gix-command/tests/command.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
11
use gix_testtools::Result;
22

33
mod prepare {
4+
#[cfg(windows)]
5+
const SH: &str = "sh";
6+
#[cfg(not(windows))]
7+
const SH: &str = "/bin/sh";
8+
49
fn quoted(input: &[&str]) -> String {
510
input.iter().map(|s| format!("\"{s}\"")).collect::<Vec<_>>().join(" ")
611
}
12+
13+
#[test]
14+
fn empty() {
15+
let cmd = std::process::Command::from(gix_command::prepare(""));
16+
assert_eq!(format!("{cmd:?}"), "\"\"");
17+
}
718
#[test]
819
fn single_and_multiple_arguments() {
920
let cmd = std::process::Command::from(gix_command::prepare("ls").arg("first").args(["second", "third"]));
1021
assert_eq!(format!("{cmd:?}"), quoted(&["ls", "first", "second", "third"]));
1122
}
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+
}
1263
}
1364

1465
mod spawn {

0 commit comments

Comments
 (0)