summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/tests/process/working_directory.rs
blob: 1c3262bb9d9161be668f28330b205a1972113834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#![cfg(feature = "fs")]

#[cfg(not(target_os = "macos"))]
use rustix::fs::{Mode, OFlags};
use tempfile::{tempdir, TempDir};

#[allow(unused)]
fn tmpdir() -> TempDir {
    tempdir().expect("expected to be able to create a temporary directory")
}

/// Disable this test on macos because GHA has a weird system folder structure
/// that makes this test fail.
#[cfg(not(target_os = "macos"))]
#[test]
fn test_changing_working_directory() {
    let tmpdir = tmpdir();

    let orig_cwd = rustix::process::getcwd(Vec::new()).expect("get the cwd");
    let orig_fd_cwd = rustix::fs::openat(rustix::fs::cwd(), ".", OFlags::RDONLY, Mode::empty())
        .expect("get a fd for the current directory");

    rustix::process::chdir(tmpdir.path()).expect("changing dir to the tmp");
    let ch1_cwd = rustix::process::getcwd(Vec::new()).expect("get the cwd");

    assert_ne!(orig_cwd, ch1_cwd, "The cwd hasn't changed!");
    assert_eq!(
        ch1_cwd.to_string_lossy(),
        tmpdir.path().to_string_lossy(),
        "The cwd is not the same as the tmpdir"
    );

    #[cfg(not(target_os = "fuchsia"))]
    rustix::process::fchdir(orig_fd_cwd).expect("changing dir to the original");
    #[cfg(target_os = "fushcia")]
    rustix::process::chdir(orig_cwd).expect("changing dir to the original");
    let ch2_cwd = rustix::process::getcwd(ch1_cwd).expect("get the cwd");

    assert_eq!(
        orig_cwd, ch2_cwd,
        "The cwd wasn't changed back to the its original position"
    );
}