2 // ignore-emscripten no processes
3 // ignore-sgx no processes
4 // ignore-fuchsia Needs directory creation privilege
9 use std::process::Command;
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);
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
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();
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)
41 Path::new("../foo").join(exe)
44 let status = Command::new(relative_exe)
49 assert!(status.success());