refactor(monorepo): simplify workspace path tests

This commit is contained in:
2026-09-17 09:54:13 -04:00
parent 86f5965964
commit 5c077fe3c6

View File

@@ -1,43 +1,19 @@
use std::ffi::OsString; use std::ffi::OsString;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Mutex;
use monorepo::workspace::resolve_workspace_path; use monorepo::workspace::resolve_workspace_path;
static ENV_LOCK: Mutex<()> = Mutex::new(());
const BUILD_WORKSPACE_DIRECTORY: &str = "BUILD_WORKSPACE_DIRECTORY"; const BUILD_WORKSPACE_DIRECTORY: &str = "BUILD_WORKSPACE_DIRECTORY";
struct EnvRestore { struct EnvRestore {
key: &'static str,
value: Option<OsString>, value: Option<OsString>,
} }
impl EnvRestore { impl EnvRestore {
fn set(key: &'static str, value: &str) -> Self { fn capture() -> Self {
let restore = Self { Self {
key, value: std::env::var_os(BUILD_WORKSPACE_DIRECTORY),
value: std::env::var_os(key),
};
unsafe {
std::env::set_var(key, value);
} }
restore
}
fn remove(key: &'static str) -> Self {
let restore = Self {
key,
value: std::env::var_os(key),
};
unsafe {
std::env::remove_var(key);
}
restore
} }
} }
@@ -45,42 +21,58 @@ impl Drop for EnvRestore {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
if let Some(value) = &self.value { if let Some(value) = &self.value {
std::env::set_var(self.key, value); std::env::set_var(BUILD_WORKSPACE_DIRECTORY, value);
} else { } else {
std::env::remove_var(self.key); std::env::remove_var(BUILD_WORKSPACE_DIRECTORY);
} }
} }
} }
} }
#[test] #[derive(Debug)]
fn absolute_path_returns_unchanged_path() { struct Case {
let _lock = ENV_LOCK.lock().unwrap(); name: &'static str,
let _workspace = EnvRestore::set(BUILD_WORKSPACE_DIRECTORY, "/workspace"); workspace: Option<&'static str>,
let path = PathBuf::from("/tmp/output"); path: &'static str,
expected: &'static str,
let resolved = resolve_workspace_path(path.clone());
assert_eq!(resolved, path);
} }
#[test] #[test]
fn relative_path_joins_workspace_path() { fn resolve_workspace_path_cases() {
let _lock = ENV_LOCK.lock().unwrap(); let _restore = EnvRestore::capture();
let _workspace = EnvRestore::set(BUILD_WORKSPACE_DIRECTORY, "/workspace");
let resolved = resolve_workspace_path(PathBuf::from("output/graph")); let cases = [
Case {
name: "absolute path returns unchanged path",
workspace: Some("/workspace"),
path: "/tmp/output",
expected: "/tmp/output",
},
Case {
name: "relative path joins workspace path",
workspace: Some("/workspace"),
path: "output/graph",
expected: "/workspace/output/graph",
},
Case {
name: "relative path without workspace returns unchanged path",
workspace: None,
path: "output/graph",
expected: "output/graph",
},
];
assert_eq!(resolved, PathBuf::from("/workspace/output/graph")); for case in cases {
} unsafe {
if let Some(workspace) = case.workspace {
#[test] std::env::set_var(BUILD_WORKSPACE_DIRECTORY, workspace);
fn relative_path_without_workspace_returns_unchanged_path() { } else {
let _lock = ENV_LOCK.lock().unwrap(); std::env::remove_var(BUILD_WORKSPACE_DIRECTORY);
let _workspace = EnvRestore::remove(BUILD_WORKSPACE_DIRECTORY); }
let path = PathBuf::from("output/graph"); }
let resolved = resolve_workspace_path(path.clone()); let resolved = resolve_workspace_path(PathBuf::from(case.path));
assert_eq!(resolved, path); assert_eq!(resolved, PathBuf::from(case.expected), "{}", case.name);
}
} }