Skip to content
Closed
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
42 changes: 16 additions & 26 deletions crates/socket-patch-core/tests/crawler_maven_e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ fn options_at(root: &Path) -> CrawlerOptions {

/// Keeps the machine's Coursier / Ivy caches out of a test that pins the
/// exact repo list (a JVM marker or `--global` also crawls them): `HOME`
/// points at an empty directory and the variables naming a cache are unset
/// until drop, which restores them. Only inside `#[serial]` tests.
/// and `USERPROFILE` point at an empty directory and the variables naming
/// a cache are unset until drop, which restores them (also when the test
/// panics). Only inside `#[serial]` tests.
struct NoJvmCaches {
_home: tempfile::TempDir,
saved: Vec<(&'static str, Option<std::ffi::OsString>)>,
Expand Down Expand Up @@ -51,6 +52,17 @@ impl NoJvmCaches {
std::env::set_var("USERPROFILE", home.path());
Self { _home: home, saved }
}

/// [`Self::new`] with the account home at `home` instead. Both `HOME`
/// and `USERPROFILE` move: on Windows Maven's `user.home` is
/// `USERPROFILE` first, so a test that only moves `HOME` reads the
/// runner's real profile there.
fn with_home(home: &Path) -> Self {
let guard = Self::new();
std::env::set_var("HOME", home);
std::env::set_var("USERPROFILE", home);
guard
}
}

impl Drop for NoJvmCaches {
Expand Down Expand Up @@ -267,12 +279,9 @@ async fn get_maven_repo_paths_home_dot_m2_fallback() {
.await
.unwrap();

let _home = NoJvmCaches::with_home(tmp.path());
let prev_local = std::env::var("MAVEN_REPO_LOCAL").ok();
let prev_m2 = std::env::var("M2_HOME").ok();
let prev_home = std::env::var("HOME").ok();
std::env::remove_var("MAVEN_REPO_LOCAL");
std::env::remove_var("M2_HOME");
std::env::set_var("HOME", tmp.path());

let crawler = MavenCrawler;
let paths = crawler
Expand All @@ -283,14 +292,6 @@ async fn get_maven_repo_paths_home_dot_m2_fallback() {
if let Some(v) = prev_local {
std::env::set_var("MAVEN_REPO_LOCAL", v);
}
if let Some(v) = prev_m2 {
std::env::set_var("M2_HOME", v);
}
if let Some(v) = prev_home {
std::env::set_var("HOME", v);
} else {
std::env::remove_var("HOME");
}

// Production returns exactly the single resolved repo path — assert the
// whole vec, not just membership, so a stray extra/wrong path also fails.
Expand Down Expand Up @@ -338,12 +339,9 @@ async fn get_maven_repo_paths_global_mode_with_maven_repo_local() {
#[serial]
async fn get_maven_repo_paths_global_mode_no_m2_returns_empty() {
let tmp = tempfile::tempdir().unwrap();
let _home = NoJvmCaches::with_home(tmp.path()); // No .m2/ inside
let prev_local = std::env::var("MAVEN_REPO_LOCAL").ok();
let prev_m2 = std::env::var("M2_HOME").ok();
let prev_home = std::env::var("HOME").ok();
std::env::remove_var("MAVEN_REPO_LOCAL");
std::env::remove_var("M2_HOME");
std::env::set_var("HOME", tmp.path()); // No .m2/ inside

let crawler = MavenCrawler;
let opts = CrawlerOptions {
Expand All @@ -356,14 +354,6 @@ async fn get_maven_repo_paths_global_mode_no_m2_returns_empty() {
if let Some(v) = prev_local {
std::env::set_var("MAVEN_REPO_LOCAL", v);
}
if let Some(v) = prev_m2 {
std::env::set_var("M2_HOME", v);
}
if let Some(v) = prev_home {
std::env::set_var("HOME", v);
} else {
std::env::remove_var("HOME");
}

assert!(
paths.is_empty(),
Expand Down
Loading