Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/pu-core/src/agent_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod tests {
use tempfile::TempDir;

fn isolate_home(tmp: &TempDir) {
unsafe { std::env::set_var("HOME", tmp.path()) };
paths::set_home_override(Some(tmp.path().to_path_buf()));
}

#[test]
Expand Down
18 changes: 18 additions & 0 deletions crates/pu-core/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,25 @@ pub fn global_schedules_dir() -> Result<PathBuf, std::io::Error> {
Ok(global_pu_dir()?.join("schedules"))
}

#[cfg(test)]
thread_local! {
static HOME_OVERRIDE: std::cell::RefCell<Option<PathBuf>> = const { std::cell::RefCell::new(None) };
}

/// Override `home_dir()` for the current test thread. Thread-safe alternative
/// to `unsafe { std::env::set_var("HOME", ...) }`.
#[cfg(test)]
pub(crate) fn set_home_override(path: Option<PathBuf>) {
HOME_OVERRIDE.with(|o| *o.borrow_mut() = path);
}

fn home_dir() -> Result<PathBuf, std::io::Error> {
#[cfg(test)]
{
if let Some(p) = HOME_OVERRIDE.with(|o| o.borrow().clone()) {
return Ok(p);
}
}
std::env::var("HOME")
.map(PathBuf::from)
.map_err(|_| std::io::Error::new(std::io::ErrorKind::NotFound, "$HOME not set"))
Expand Down
6 changes: 6 additions & 0 deletions crates/pu-core/src/schedule_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ mod tests {
use super::*;
use tempfile::TempDir;

fn isolate_home(tmp: &TempDir) {
paths::set_home_override(Some(tmp.path().to_path_buf()));
}

fn make_trigger() -> ScheduleTrigger {
ScheduleTrigger::AgentDef {
name: "security-review".to_string(),
Expand Down Expand Up @@ -504,6 +508,7 @@ created_at: "2025-06-01T00:00:00Z"
#[test]
fn given_local_and_global_schedule_defs_should_list_local_first() {
let tmp = TempDir::new().unwrap();
isolate_home(&tmp);
let root = tmp.path();
let local_dir = paths::schedules_dir(root);
std::fs::create_dir_all(&local_dir).unwrap();
Expand Down Expand Up @@ -538,6 +543,7 @@ created_at: "2025-06-01T00:00:00Z"
#[test]
fn given_no_schedule_defs_should_return_empty_list() {
let tmp = TempDir::new().unwrap();
isolate_home(&tmp);
let defs = list_schedule_defs(tmp.path());
assert!(defs.is_empty());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/pu-core/src/swarm_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod tests {
use tempfile::TempDir;

fn isolate_home(tmp: &TempDir) {
unsafe { std::env::set_var("HOME", tmp.path()) };
paths::set_home_override(Some(tmp.path().to_path_buf()));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/pu-core/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ mod tests {

/// Override HOME so global_pu_dir() points to an empty temp dir.
fn isolate_home(tmp: &TempDir) {
unsafe { std::env::set_var("HOME", tmp.path()) };
paths::set_home_override(Some(tmp.path().to_path_buf()));
}

#[test]
Expand Down
Loading