• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![cfg(feature = "fs")]
2 
3 #[cfg(not(any(target_os = "fuchsia", target_os = "macos")))]
4 use rustix::fs::{Mode, OFlags};
5 use tempfile::{tempdir, TempDir};
6 
7 #[allow(unused)]
tmpdir() -> TempDir8 fn tmpdir() -> TempDir {
9     tempdir().expect("expected to be able to create a temporary directory")
10 }
11 
12 /// Disable this test on macos because GHA has a weird system folder structure
13 /// that makes this test fail.
14 #[cfg(not(target_os = "macos"))]
15 #[test]
test_changing_working_directory()16 fn test_changing_working_directory() {
17     let tmpdir = tmpdir();
18 
19     let orig_cwd = rustix::process::getcwd(Vec::new()).expect("get the cwd");
20 
21     #[cfg(not(target_os = "fuchsia"))]
22     let orig_fd_cwd = rustix::fs::openat(rustix::fs::cwd(), ".", OFlags::RDONLY, Mode::empty())
23         .expect("get a fd for the current directory");
24 
25     rustix::process::chdir(tmpdir.path()).expect("changing dir to the tmp");
26     let ch1_cwd = rustix::process::getcwd(Vec::new()).expect("get the cwd");
27 
28     assert_ne!(orig_cwd, ch1_cwd, "The cwd hasn't changed!");
29     assert_eq!(
30         ch1_cwd.to_string_lossy(),
31         tmpdir.path().to_string_lossy(),
32         "The cwd is not the same as the tmpdir"
33     );
34 
35     #[cfg(not(target_os = "fuchsia"))]
36     rustix::process::fchdir(orig_fd_cwd).expect("changing dir to the original");
37     #[cfg(target_os = "fushcia")]
38     rustix::process::chdir(orig_cwd).expect("changing dir to the original");
39     let ch2_cwd = rustix::process::getcwd(ch1_cwd).expect("get the cwd");
40 
41     assert_eq!(
42         orig_cwd, ch2_cwd,
43         "The cwd wasn't changed back to the its original position"
44     );
45 }
46