refactor(monorepo): expose one workspace path API
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub fn resolve_workspace_path(path: PathBuf) -> PathBuf {
|
pub fn resolve_workspace_path(path: PathBuf) -> PathBuf {
|
||||||
resolve_workspace_path_from(
|
resolve_workspace_path_impl(
|
||||||
path,
|
path,
|
||||||
std::env::var_os("BUILD_WORKSPACE_DIRECTORY").map(PathBuf::from),
|
std::env::var_os("BUILD_WORKSPACE_DIRECTORY").map(PathBuf::from),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resolve_workspace_path_from(path: PathBuf, workspace: Option<PathBuf>) -> PathBuf {
|
fn resolve_workspace_path_impl(path: PathBuf, workspace: Option<PathBuf>) -> PathBuf {
|
||||||
if path.is_absolute() {
|
if path.is_absolute() {
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +1,86 @@
|
|||||||
|
use std::ffi::OsString;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use monorepo::workspace::resolve_workspace_path_from;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
restore
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove(key: &'static str) -> Self {
|
||||||
|
let restore = Self {
|
||||||
|
key,
|
||||||
|
value: std::env::var_os(key),
|
||||||
|
};
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
std::env::remove_var(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
restore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for EnvRestore {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe {
|
||||||
|
if let Some(value) = &self.value {
|
||||||
|
std::env::set_var(self.key, value);
|
||||||
|
} else {
|
||||||
|
std::env::remove_var(self.key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn absolute_path_returns_unchanged_path() {
|
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 path = PathBuf::from("/tmp/output");
|
||||||
|
|
||||||
let resolved = resolve_workspace_path_from(path.clone(), Some(PathBuf::from("/workspace")));
|
let resolved = resolve_workspace_path(path.clone());
|
||||||
|
|
||||||
assert_eq!(resolved, path);
|
assert_eq!(resolved, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn relative_path_joins_workspace_path() {
|
fn relative_path_joins_workspace_path() {
|
||||||
let resolved = resolve_workspace_path_from(
|
let _lock = ENV_LOCK.lock().unwrap();
|
||||||
PathBuf::from("output/graph"),
|
let _workspace = EnvRestore::set(BUILD_WORKSPACE_DIRECTORY, "/workspace");
|
||||||
Some(PathBuf::from("/workspace")),
|
|
||||||
);
|
let resolved = resolve_workspace_path(PathBuf::from("output/graph"));
|
||||||
|
|
||||||
assert_eq!(resolved, PathBuf::from("/workspace/output/graph"));
|
assert_eq!(resolved, PathBuf::from("/workspace/output/graph"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn relative_path_without_workspace_returns_unchanged_path() {
|
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 path = PathBuf::from("output/graph");
|
||||||
|
|
||||||
let resolved = resolve_workspace_path_from(path.clone(), None);
|
let resolved = resolve_workspace_path(path.clone());
|
||||||
|
|
||||||
assert_eq!(resolved, path);
|
assert_eq!(resolved, path);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user