Skip to content

Commit 97288e0

Browse files
committed
test home override stands in for the developer's home only
Five legacy-migration and product-dir tests redirect HOME to a temp dir and expect codewhale_home() to follow it. The override now records the real user home at install and applies only while user_home() still resolves to it, so a redirected HOME gets the ordinary fallback. session_manager, settings, atomic_write, test_support, and the onboarding tests: 202 passed, 0 failed. Signed-off-by: CodeWhale Bot <hmbown@gmail.com>
1 parent bb76ef5 commit 97288e0

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

crates/paths/src/lib.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,27 @@ fn windows_home_from_environment() -> Option<PathBuf> {
108108
/// A valid explicit `CODEWHALE_HOME` is returned after `~` expansion. Otherwise
109109
/// this is `<user home>/.codewhale`.
110110
pub fn codewhale_home() -> Result<Option<PathBuf>, PathOverrideError> {
111-
Ok(codewhale_home_override()?
112-
.or_else(|| TEST_HOME_OVERRIDE.get().cloned())
113-
.or_else(|| user_home().map(|home| home.join(CODEWHALE_APP_DIR))))
111+
if let Some(explicit) = codewhale_home_override()? {
112+
return Ok(Some(explicit));
113+
}
114+
let home = user_home();
115+
// The test override stands in for the *developer's* home only. A test
116+
// that redirected HOME to a temp dir (legacy-migration and
117+
// product-dir tests) asked for the real fallback and gets it.
118+
if let Some(test_home) = TEST_HOME_OVERRIDE.get()
119+
&& home.as_deref() == Some(test_home.real_user_home.as_path())
120+
{
121+
return Ok(Some(test_home.home.clone()));
122+
}
123+
Ok(home.map(|home| home.join(CODEWHALE_APP_DIR)))
124+
}
125+
126+
struct TestHomeOverride {
127+
home: PathBuf,
128+
real_user_home: PathBuf,
114129
}
115130

116-
static TEST_HOME_OVERRIDE: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
131+
static TEST_HOME_OVERRIDE: std::sync::OnceLock<TestHomeOverride> = std::sync::OnceLock::new();
117132

118133
/// Route every implicit home lookup in this process to `home` instead of the
119134
/// user's real `~/.codewhale`. An explicit `CODEWHALE_HOME` still wins.
@@ -125,15 +140,25 @@ static TEST_HOME_OVERRIDE: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::n
125140
/// returned as `true`; later calls are ignored. Production never calls this.
126141
#[doc(hidden)]
127142
pub fn install_test_home_override(home: PathBuf) -> bool {
128-
TEST_HOME_OVERRIDE.set(home).is_ok()
143+
let Some(real_user_home) = user_home() else {
144+
return false;
145+
};
146+
TEST_HOME_OVERRIDE
147+
.set(TestHomeOverride {
148+
home,
149+
real_user_home,
150+
})
151+
.is_ok()
129152
}
130153

131154
/// The installed test home, if any. Lets a harness prove the override is in
132155
/// force before trusting an implicit lookup.
133156
#[doc(hidden)]
134157
#[must_use]
135158
pub fn test_home_override() -> Option<&'static std::path::Path> {
136-
TEST_HOME_OVERRIDE.get().map(PathBuf::as_path)
159+
TEST_HOME_OVERRIDE
160+
.get()
161+
.map(|test_home| test_home.home.as_path())
137162
}
138163

139164
/// Return the explicit config-file override, preferring the Codewhale name.

0 commit comments

Comments
 (0)