Skip to content

Commit fcbcc97

Browse files
committed
Add test for Command::current_dir behavior.
1 parent a938725 commit fcbcc97

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Diff for: src/test/ui/command/command-current-dir.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// run-pass
2+
// ignore-emscripten no processes
3+
// ignore-sgx no processes
4+
5+
use std::env;
6+
use std::fs;
7+
use std::path::Path;
8+
use std::process::Command;
9+
10+
fn main() {
11+
// Checks the behavior of current_dir when used with a relative exe path.
12+
let me = env::current_exe().unwrap();
13+
if matches!(env::args().skip(1).next().as_deref(), Some("current-dir")) {
14+
let cwd = env::current_dir().unwrap();
15+
assert_eq!(cwd.file_name().unwrap(), "bar");
16+
std::process::exit(0);
17+
}
18+
let exe = me.file_name().unwrap();
19+
let cwd = std::env::current_dir().unwrap();
20+
eprintln!("cwd={:?}", cwd);
21+
let foo = cwd.join("foo");
22+
let bar = cwd.join("bar");
23+
fs::create_dir_all(&foo).unwrap();
24+
fs::create_dir_all(&bar).unwrap();
25+
fs::copy(&me, foo.join(exe)).unwrap();
26+
27+
// Unfortunately this is inconsistent based on the platform, see
28+
// https://github.com/rust-lang/rust/issues/37868. On Windows,
29+
// it is relative *before* changing the directory, and on Unix
30+
// it is *after* changing the directory.
31+
let relative_exe = if cfg!(windows) {
32+
Path::new("foo").join(exe)
33+
} else {
34+
Path::new("../foo").join(exe)
35+
};
36+
37+
let status = Command::new(relative_exe)
38+
.arg("current-dir")
39+
.current_dir("bar")
40+
.status()
41+
.unwrap();
42+
assert!(status.success());
43+
}

0 commit comments

Comments
 (0)