Avoid home-only tempdir tests

This commit is contained in:
starr-openai
2026-05-19 11:49:43 -07:00
parent dabc7a1a33
commit ba20022f64
2 changed files with 41 additions and 9 deletions

View File

@@ -139,10 +139,28 @@ fn mark_as_git_repo(dir: &Path) {
}
fn tempdir_outside_ambient_repo() -> TempDir {
let home = home_dir().expect("home directory should be available");
tempfile::Builder::new()
.prefix("core-skills-tests-")
.tempdir_in(home)
let mut candidates = Vec::new();
if let Some(home) = home_dir() {
candidates.push(home);
}
if let Ok(cwd) = std::env::current_dir() {
candidates.extend(cwd.ancestors().map(Path::to_path_buf));
}
candidates.push(std::env::temp_dir());
candidates
.into_iter()
.filter(|candidate| {
!candidate
.ancestors()
.any(|ancestor| ancestor.join(".git").exists())
})
.find_map(|candidate| {
tempfile::Builder::new()
.prefix("core-skills-tests-")
.tempdir_in(candidate)
.ok()
})
.expect("tempdir outside ambient repo")
}

View File

@@ -187,13 +187,27 @@ mod tests {
use pretty_assertions::assert_eq;
fn tempdir_outside_ambient_repo() -> tempfile::TempDir {
let home = std::env::var_os("HOME")
let mut candidates = Vec::new();
if let Some(home) = std::env::var_os("HOME")
.or_else(|| std::env::var_os("USERPROFILE"))
.map(PathBuf::from)
.expect("home directory should be available");
tempfile::Builder::new()
.prefix("secrets-tests-")
.tempdir_in(home)
{
candidates.push(home);
}
if let Ok(cwd) = std::env::current_dir() {
candidates.extend(cwd.ancestors().map(Path::to_path_buf));
}
candidates.push(std::env::temp_dir());
candidates
.into_iter()
.filter(|candidate| get_git_repo_root(candidate).is_none())
.find_map(|candidate| {
tempfile::Builder::new()
.prefix("secrets-tests-")
.tempdir_in(candidate)
.ok()
})
.expect("tempdir outside ambient repo")
}