]> git.lizzy.rs Git - rust.git/blob - tests/ui/command/command-current-dir.rs
Rollup merge of #106321 - compiler-errors:delayed-bug-backtrace, r=Nilstrieb
[rust.git] / tests / ui / command / command-current-dir.rs
1 // run-pass
2 // ignore-emscripten no processes
3 // ignore-sgx no processes
4 // ignore-fuchsia Needs directory creation privilege
5
6 use std::env;
7 use std::fs;
8 use std::path::Path;
9 use std::process::Command;
10
11 fn main() {
12     // Checks the behavior of current_dir when used with a relative exe path.
13     let me = env::current_exe().unwrap();
14     if matches!(env::args().skip(1).next().as_deref(), Some("current-dir")) {
15         let cwd = env::current_dir().unwrap();
16         assert_eq!(cwd.file_name().unwrap(), "bar");
17         std::process::exit(0);
18     }
19     let exe = me.file_name().unwrap();
20     let cwd = me.parent().unwrap();
21     eprintln!("cwd={:?}", cwd);
22     // Change directory to where the executable is located, since this test
23     // fundamentally needs to use relative paths. In some cases (like
24     // remote-test-server), the current_dir can be somewhere else, so make
25     // sure it is something we can use. We assume we can write to this
26     // directory.
27     env::set_current_dir(&cwd).unwrap();
28     let foo = cwd.join("foo");
29     let bar = cwd.join("bar");
30     fs::create_dir_all(&foo).unwrap();
31     fs::create_dir_all(&bar).unwrap();
32     fs::copy(&me, foo.join(exe)).unwrap();
33
34     // Unfortunately this is inconsistent based on the platform, see
35     // https://github.com/rust-lang/rust/issues/37868. On Windows,
36     // it is relative *before* changing the directory, and on Unix
37     // it is *after* changing the directory.
38     let relative_exe = if cfg!(windows) {
39         Path::new("foo").join(exe)
40     } else {
41         Path::new("../foo").join(exe)
42     };
43
44     let status = Command::new(relative_exe)
45         .arg("current-dir")
46         .current_dir("bar")
47         .status()
48         .unwrap();
49     assert!(status.success());
50 }