]> git.lizzy.rs Git - rust.git/blob - src/test/ui/command/command-current-dir.rs
Rollup merge of #91312 - terrarier2111:anon-const-ice, r=jackh726
[rust.git] / src / test / ui / command / command-current-dir.rs
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 = me.parent().unwrap();
20     eprintln!("cwd={:?}", cwd);
21     // Change directory to where the exectuable is located, since this test
22     // fundamentally needs to use relative paths. In some cases (like
23     // remote-test-server), the current_dir can be somewhere else, so make
24     // sure it is something we can use. We assume we can write to this
25     // directory.
26     env::set_current_dir(&cwd).unwrap();
27     let foo = cwd.join("foo");
28     let bar = cwd.join("bar");
29     fs::create_dir_all(&foo).unwrap();
30     fs::create_dir_all(&bar).unwrap();
31     fs::copy(&me, foo.join(exe)).unwrap();
32
33     // Unfortunately this is inconsistent based on the platform, see
34     // https://github.com/rust-lang/rust/issues/37868. On Windows,
35     // it is relative *before* changing the directory, and on Unix
36     // it is *after* changing the directory.
37     let relative_exe = if cfg!(windows) {
38         Path::new("foo").join(exe)
39     } else {
40         Path::new("../foo").join(exe)
41     };
42
43     let status = Command::new(relative_exe)
44         .arg("current-dir")
45         .current_dir("bar")
46         .status()
47         .unwrap();
48     assert!(status.success());
49 }