oxdock_sys_test_utils/
lib.rs

1/// Shared test helpers used by multiple crates' tests.
2///
3/// Keep functionality here minimal and test-only.
4use std::env;
5
6pub struct TestEnvGuard {
7    key: &'static str,
8    value: Option<String>,
9}
10
11impl TestEnvGuard {
12    pub fn set(key: &'static str, value: &str) -> Self {
13        let prev = env::var(key).ok();
14        unsafe { env::set_var(key, value) };
15        Self { key, value: prev }
16    }
17}
18
19impl Drop for TestEnvGuard {
20    fn drop(&mut self) {
21        match &self.value {
22            Some(value) => unsafe { env::set_var(self.key, value) },
23            None => unsafe { env::remove_var(self.key) },
24        }
25    }
26}
27
28#[allow(clippy::disallowed_types)]
29use std::path::Path;
30
31/// Detect whether the current process can create filesystem symlinks under
32/// the provided target directory. Accepts a `&Path` to avoid depending on
33/// `oxdock-fs` and creating a circular crate dependency.
34#[allow(clippy::disallowed_methods, clippy::disallowed_types)]
35pub fn can_create_symlinks(target: &Path) -> bool {
36    #[cfg(unix)]
37    {
38        let _ = target;
39        true
40    }
41
42    #[cfg(windows)]
43    {
44        use std::fs;
45        use std::os::windows::fs::symlink_dir;
46        let test_src = target.join("__oxdock_test_symlink_src");
47        let test_dst = target.join("__oxdock_test_symlink_dst");
48        // Localized allowance: sys test helper may create/remove test dirs.
49        let _ = fs::create_dir_all(&test_src);
50        let ok = symlink_dir(&test_src, &test_dst).is_ok();
51        let _ = fs::remove_dir_all(&test_dst);
52        let _ = fs::remove_dir_all(&test_src);
53        ok
54    }
55
56    #[cfg(not(any(unix, windows)))]
57    {
58        let _ = target;
59        false
60    }
61}