Skip to content

Commit d788d57

Browse files
committed
test: address pet-homebrew review comments (PR #394)
1 parent 2e64cc7 commit d788d57

File tree

3 files changed

+22
-45
lines changed

3 files changed

+22
-45
lines changed

crates/pet-homebrew/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ pet-core = { path = "../pet-core" }
1919
log = "0.4.21"
2020
regex = "1.10.4"
2121
rayon = "1.11.0"
22+
23+
[dev-dependencies]
24+
tempfile = "3.10"

crates/pet-homebrew/src/environment_locations.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,45 +52,31 @@ pub fn get_homebrew_prefix_bin(env_vars: &EnvVariables) -> Vec<PathBuf> {
5252
#[cfg(test)]
5353
mod tests {
5454
use super::*;
55-
use std::{
56-
fs,
57-
time::{SystemTime, UNIX_EPOCH},
58-
};
59-
60-
fn create_unique_prefix(name: &str) -> PathBuf {
61-
let unique = SystemTime::now()
62-
.duration_since(UNIX_EPOCH)
63-
.unwrap()
64-
.as_nanos();
65-
std::env::temp_dir().join(format!(
66-
"pet-homebrew-{name}-{}-{unique}",
67-
std::process::id()
68-
))
69-
}
55+
use std::fs;
56+
use tempfile::tempdir;
7057

7158
#[test]
7259
fn homebrew_prefix_bin_uses_existing_homebrew_prefix_env_var() {
73-
let homebrew_prefix = create_unique_prefix("prefix");
74-
let homebrew_bin = homebrew_prefix.join("bin");
60+
let homebrew_prefix = tempdir().unwrap();
61+
let homebrew_bin = homebrew_prefix.path().join("bin");
7562
fs::create_dir_all(&homebrew_bin).unwrap();
7663
let env_vars = EnvVariables {
7764
home: None,
7865
root: None,
7966
path: None,
80-
homebrew_prefix: Some(homebrew_prefix.to_string_lossy().to_string()),
67+
homebrew_prefix: Some(homebrew_prefix.path().to_string_lossy().to_string()),
8168
known_global_search_locations: vec![],
8269
};
8370

8471
let prefix_bins = get_homebrew_prefix_bin(&env_vars);
8572

8673
assert!(prefix_bins.contains(&homebrew_bin));
87-
88-
fs::remove_dir_all(homebrew_prefix).unwrap();
8974
}
9075

9176
#[test]
9277
fn homebrew_prefix_bin_ignores_missing_homebrew_prefix_env_var() {
93-
let missing_homebrew_prefix = create_unique_prefix("missing-prefix");
78+
let homebrew_prefix_parent = tempdir().unwrap();
79+
let missing_homebrew_prefix = homebrew_prefix_parent.path().join("missing-prefix");
9480
let env_vars = EnvVariables {
9581
home: None,
9682
root: None,

crates/pet-homebrew/src/lib.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl Locator for Homebrew {
174174
#[cfg(all(test, unix))]
175175
mod tests {
176176
use super::*;
177-
use std::time::{SystemTime, UNIX_EPOCH};
177+
use tempfile::tempdir;
178178

179179
struct TestEnvironment {
180180
homebrew_prefix: Option<String>,
@@ -202,19 +202,6 @@ mod tests {
202202
}
203203
}
204204

205-
fn create_test_dir(name: &str) -> PathBuf {
206-
let unique = SystemTime::now()
207-
.duration_since(UNIX_EPOCH)
208-
.unwrap()
209-
.as_nanos();
210-
let directory = std::env::temp_dir().join(format!(
211-
"pet-homebrew-{name}-{}-{unique}",
212-
std::process::id()
213-
));
214-
fs::create_dir_all(&directory).unwrap();
215-
directory
216-
}
217-
218205
#[test]
219206
fn homebrew_locator_reports_kind_and_supported_category() {
220207
let locator = Homebrew::from(&TestEnvironment {
@@ -273,24 +260,25 @@ mod tests {
273260
homebrew_prefix: None,
274261
});
275262

276-
let venv_root = create_test_dir("venv-reject");
277-
let venv_bin = venv_root.join("bin");
263+
let venv_root = tempdir().unwrap();
264+
let venv_bin = venv_root.path().join("bin");
278265
fs::create_dir_all(&venv_bin).unwrap();
279266
fs::write(venv_bin.join("activate"), b"").unwrap();
280267
let venv_executable = venv_bin.join("python3.12");
281268
fs::write(&venv_executable, b"").unwrap();
282-
let venv = PythonEnv::new(venv_executable, Some(venv_root.clone()), None);
269+
let venv = PythonEnv::new(venv_executable, Some(venv_root.path().to_path_buf()), None);
283270
assert!(locator.try_from(&venv).is_none());
284271

285-
let conda_root = create_test_dir("conda-reject");
286-
fs::create_dir_all(conda_root.join("conda-meta")).unwrap();
287-
let conda_executable = conda_root.join("bin").join("python3.12");
272+
let conda_root = tempdir().unwrap();
273+
fs::create_dir_all(conda_root.path().join("conda-meta")).unwrap();
274+
let conda_executable = conda_root.path().join("bin").join("python3.12");
288275
fs::create_dir_all(conda_executable.parent().unwrap()).unwrap();
289276
fs::write(&conda_executable, b"").unwrap();
290-
let conda = PythonEnv::new(conda_executable, Some(conda_root.clone()), None);
277+
let conda = PythonEnv::new(
278+
conda_executable,
279+
Some(conda_root.path().to_path_buf()),
280+
None,
281+
);
291282
assert!(locator.try_from(&conda).is_none());
292-
293-
fs::remove_dir_all(venv_root).unwrap();
294-
fs::remove_dir_all(conda_root).unwrap();
295283
}
296284
}

0 commit comments

Comments
 (0)