]> git.proxmox.com Git - rustc.git/blame - vendor/gix-command/tests/command.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / vendor / gix-command / tests / command.rs
CommitLineData
0a29b90c
FG
1use gix_testtools::Result;
2
3mod prepare {
4 fn quoted(input: &[&str]) -> String {
5 input.iter().map(|s| format!("\"{s}\"")).collect::<Vec<_>>().join(" ")
6 }
7 #[test]
8 fn single_and_multiple_arguments() {
9 let cmd = std::process::Command::from(gix_command::prepare("ls").arg("first").args(["second", "third"]));
10 assert_eq!(format!("{cmd:?}"), quoted(&["ls", "first", "second", "third"]));
11 }
12}
13
14mod spawn {
15 #[cfg(unix)]
16 use bstr::ByteSlice;
17
18 #[test]
19 #[cfg(unix)]
20 fn environment_variables_are_passed_one_by_one() -> crate::Result {
21 let out = gix_command::prepare("echo $FIRST $SECOND")
22 .env("FIRST", "first")
23 .env("SECOND", "second")
24 .with_shell()
25 .spawn()?
26 .wait_with_output()?;
27 assert_eq!(out.stdout.as_bstr(), "first second\n");
28 Ok(())
29 }
30
31 #[test]
32 #[cfg(unix)]
33 fn disallow_shell() -> crate::Result {
781aab86 34 let out = gix_command::prepare("PATH= echo hi")
0a29b90c
FG
35 .with_shell()
36 .spawn()?
37 .wait_with_output()?;
38 assert_eq!(out.stdout.as_bstr(), "hi\n");
781aab86
FG
39
40 let mut cmd: std::process::Command = gix_command::prepare("echo hi").with_shell().without_shell().into();
0a29b90c 41 assert!(
781aab86 42 cmd.env_remove("PATH").spawn().is_err(),
0a29b90c
FG
43 "no command named 'echo hi' exists"
44 );
45 Ok(())
46 }
47
48 #[test]
49 fn direct_command_execution_searches_in_path() -> crate::Result {
50 assert!(gix_command::prepare(if cfg!(unix) { "ls" } else { "dir.exe" })
51 .spawn()?
52 .wait()?
53 .success());
54 Ok(())
55 }
56
57 #[cfg(unix)]
58 #[test]
59 fn direct_command_with_absolute_command_path() -> crate::Result {
60 assert!(gix_command::prepare("/bin/ls").spawn()?.wait()?.success());
61 Ok(())
62 }
63
64 mod with_shell {
65 use gix_testtools::bstr::ByteSlice;
66
67 #[test]
68 fn command_in_path_with_args() -> crate::Result {
69 assert!(gix_command::prepare(if cfg!(unix) { "ls -l" } else { "dir.exe -a" })
70 .with_shell()
71 .spawn()?
72 .wait()?
73 .success());
74 Ok(())
75 }
76
77 #[test]
78 fn sh_shell_specific_script_code() -> crate::Result {
79 assert!(gix_command::prepare(":;:;:").with_shell().spawn()?.wait()?.success());
80 Ok(())
81 }
82
83 #[test]
84 fn sh_shell_specific_script_code_with_single_extra_arg() -> crate::Result {
85 let out = gix_command::prepare("echo")
86 .with_shell()
87 .arg("1")
88 .spawn()?
89 .wait_with_output()?;
90 assert!(out.status.success());
91 #[cfg(not(windows))]
92 assert_eq!(out.stdout.as_bstr(), "1\n");
93 #[cfg(windows)]
94 assert_eq!(out.stdout.as_bstr(), "1\r\n");
95 Ok(())
96 }
97
98 #[test]
99 fn sh_shell_specific_script_code_with_multiple_extra_args() -> crate::Result {
100 let out = gix_command::prepare("echo")
101 .with_shell()
102 .arg("1")
103 .arg("2")
104 .spawn()?
105 .wait_with_output()?;
106 assert!(out.status.success());
107 #[cfg(not(windows))]
108 assert_eq!(out.stdout.as_bstr(), "1 2\n");
109 #[cfg(windows)]
110 assert_eq!(out.stdout.as_bstr(), "1 2\r\n");
111 Ok(())
112 }
113 }
114}