2 // ignore-emscripten no processes
3 // ignore-sgx no processes
8 use std::process::Command;
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);
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
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();
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)
40 Path::new("../foo").join(exe)
43 let status = Command::new(relative_exe)
48 assert!(status.success());