refactor(monorepo): simplify workspace path tests
This commit is contained in:
@@ -1,43 +1,19 @@
|
||||
use std::ffi::OsString;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use monorepo::workspace::resolve_workspace_path;
|
||||
|
||||
static ENV_LOCK: Mutex<()> = Mutex::new(());
|
||||
|
||||
const BUILD_WORKSPACE_DIRECTORY: &str = "BUILD_WORKSPACE_DIRECTORY";
|
||||
|
||||
struct EnvRestore {
|
||||
key: &'static str,
|
||||
value: Option<OsString>,
|
||||
}
|
||||
|
||||
impl EnvRestore {
|
||||
fn set(key: &'static str, value: &str) -> Self {
|
||||
let restore = Self {
|
||||
key,
|
||||
value: std::env::var_os(key),
|
||||
};
|
||||
|
||||
unsafe {
|
||||
std::env::set_var(key, value);
|
||||
fn capture() -> Self {
|
||||
Self {
|
||||
value: std::env::var_os(BUILD_WORKSPACE_DIRECTORY),
|
||||
}
|
||||
|
||||
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) {
|
||||
unsafe {
|
||||
if let Some(value) = &self.value {
|
||||
std::env::set_var(self.key, value);
|
||||
std::env::set_var(BUILD_WORKSPACE_DIRECTORY, value);
|
||||
} else {
|
||||
std::env::remove_var(self.key);
|
||||
std::env::remove_var(BUILD_WORKSPACE_DIRECTORY);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn absolute_path_returns_unchanged_path() {
|
||||
let _lock = ENV_LOCK.lock().unwrap();
|
||||
let _workspace = EnvRestore::set(BUILD_WORKSPACE_DIRECTORY, "/workspace");
|
||||
let path = PathBuf::from("/tmp/output");
|
||||
|
||||
let resolved = resolve_workspace_path(path.clone());
|
||||
|
||||
assert_eq!(resolved, path);
|
||||
#[derive(Debug)]
|
||||
struct Case {
|
||||
name: &'static str,
|
||||
workspace: Option<&'static str>,
|
||||
path: &'static str,
|
||||
expected: &'static str,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relative_path_joins_workspace_path() {
|
||||
let _lock = ENV_LOCK.lock().unwrap();
|
||||
let _workspace = EnvRestore::set(BUILD_WORKSPACE_DIRECTORY, "/workspace");
|
||||
fn resolve_workspace_path_cases() {
|
||||
let _restore = EnvRestore::capture();
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relative_path_without_workspace_returns_unchanged_path() {
|
||||
let _lock = ENV_LOCK.lock().unwrap();
|
||||
let _workspace = EnvRestore::remove(BUILD_WORKSPACE_DIRECTORY);
|
||||
let path = PathBuf::from("output/graph");
|
||||
|
||||
let resolved = resolve_workspace_path(path.clone());
|
||||
|
||||
assert_eq!(resolved, path);
|
||||
for case in cases {
|
||||
unsafe {
|
||||
if let Some(workspace) = case.workspace {
|
||||
std::env::set_var(BUILD_WORKSPACE_DIRECTORY, workspace);
|
||||
} else {
|
||||
std::env::remove_var(BUILD_WORKSPACE_DIRECTORY);
|
||||
}
|
||||
}
|
||||
|
||||
let resolved = resolve_workspace_path(PathBuf::from(case.path));
|
||||
|
||||
assert_eq!(resolved, PathBuf::from(case.expected), "{}", case.name);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user