fix(test): resolve HOME env race condition in parallel tests

- Use get_home_dir() instead of dirs::home_dir() in get_opencode_dir()
  and get_openclaw_dir() to respect CC_SWITCH_TEST_HOME override
- Add CC_SWITCH_TEST_HOME to all TempHome implementations
- Add #[serial] to all with_test_home tests to share serialization
  with other env-mutating tests
- Remove --test-threads=1 workaround from CI
This commit is contained in:
Jason
2026-04-02 22:08:12 +08:00
parent 8cdc07c860
commit 8b44d9f54d
6 changed files with 46 additions and 7 deletions
+1 -1
View File
@@ -96,4 +96,4 @@ jobs:
run: cargo clippy --manifest-path src-tauri/Cargo.toml -- -D warnings
- name: Run tests
run: cargo test --manifest-path src-tauri/Cargo.toml -- --test-threads=1
run: cargo test --manifest-path src-tauri/Cargo.toml
+1 -3
View File
@@ -37,9 +37,7 @@ pub fn get_openclaw_dir() -> PathBuf {
return override_dir;
}
dirs::home_dir()
.map(|h| h.join(".openclaw"))
.unwrap_or_else(|| PathBuf::from(".openclaw"))
crate::config::get_home_dir().join(".openclaw")
}
/// 获取 OpenClaw 配置文件路径
+3 -3
View File
@@ -37,9 +37,9 @@ pub fn get_opencode_dir() -> PathBuf {
return override_dir;
}
dirs::home_dir()
.map(|h| h.join(".config").join("opencode"))
.unwrap_or_else(|| PathBuf::from(".config").join("opencode"))
crate::config::get_home_dir()
.join(".config")
.join("opencode")
}
pub fn get_opencode_config_path() -> PathBuf {
+9
View File
@@ -272,6 +272,7 @@ mod tests {
dir: TempDir,
original_home: Option<String>,
original_userprofile: Option<String>,
original_test_home: Option<String>,
}
impl TempHome {
@@ -279,15 +280,18 @@ mod tests {
let dir = TempDir::new().expect("failed to create temp home");
let original_home = env::var("HOME").ok();
let original_userprofile = env::var("USERPROFILE").ok();
let original_test_home = env::var("CC_SWITCH_TEST_HOME").ok();
env::set_var("HOME", dir.path());
env::set_var("USERPROFILE", dir.path());
env::set_var("CC_SWITCH_TEST_HOME", dir.path());
crate::settings::reload_settings().expect("reload settings");
Self {
dir,
original_home,
original_userprofile,
original_test_home,
}
}
}
@@ -303,6 +307,11 @@ mod tests {
Some(value) => env::set_var("USERPROFILE", value),
None => env::remove_var("USERPROFILE"),
}
match &self.original_test_home {
Some(value) => env::set_var("CC_SWITCH_TEST_HOME", value),
None => env::remove_var("CC_SWITCH_TEST_HOME"),
}
}
}
+23
View File
@@ -70,6 +70,7 @@ mod tests {
dir: TempDir,
original_home: Option<String>,
original_userprofile: Option<String>,
original_test_home: Option<String>,
}
impl TempHome {
@@ -77,14 +78,17 @@ mod tests {
let dir = TempDir::new().expect("failed to create temp home");
let original_home = env::var("HOME").ok();
let original_userprofile = env::var("USERPROFILE").ok();
let original_test_home = env::var("CC_SWITCH_TEST_HOME").ok();
env::set_var("HOME", dir.path());
env::set_var("USERPROFILE", dir.path());
env::set_var("CC_SWITCH_TEST_HOME", dir.path());
Self {
dir,
original_home,
original_userprofile,
original_test_home,
}
}
}
@@ -100,6 +104,11 @@ mod tests {
Some(value) => env::set_var("USERPROFILE", value),
None => env::remove_var("USERPROFILE"),
}
match &self.original_test_home {
Some(value) => env::set_var("CC_SWITCH_TEST_HOME", value),
None => env::remove_var("CC_SWITCH_TEST_HOME"),
}
}
}
@@ -435,6 +444,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn rename_rejects_missing_original_provider() {
with_test_home(|state, _| {
let original = openclaw_provider("deepseek");
@@ -468,6 +478,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn db_only_additive_update_survives_live_config_parse_errors() {
with_test_home(|state, home| {
let provider = openclaw_provider("deepseek");
@@ -510,6 +521,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn sync_current_provider_for_app_skips_db_only_opencode_provider() {
with_test_home(|state, _| {
let provider = opencode_provider("db-only-opencode");
@@ -529,6 +541,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn sync_current_provider_for_app_skips_db_only_openclaw_provider() {
with_test_home(|state, _| {
let provider = openclaw_provider("db-only-openclaw");
@@ -548,6 +561,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn sync_current_provider_for_app_preserves_legacy_live_opencode_provider() {
with_test_home(|state, _| {
let provider = opencode_provider("legacy-opencode");
@@ -582,6 +596,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn sync_current_provider_for_app_restores_legacy_opencode_provider_after_live_reset() {
with_test_home(|state, _| {
let provider = opencode_provider("legacy-opencode-reset");
@@ -603,6 +618,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn sync_current_provider_for_app_restores_legacy_openclaw_provider_after_live_reset() {
with_test_home(|state, _| {
let mut provider = openclaw_provider("legacy-openclaw-reset");
@@ -630,6 +646,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn import_opencode_providers_from_live_marks_provider_as_live_managed() {
with_test_home(|state, _| {
let provider = opencode_provider("imported-opencode");
@@ -657,6 +674,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn import_openclaw_providers_from_live_marks_provider_as_live_managed() {
with_test_home(|state, _| {
let mut provider = openclaw_provider("imported-openclaw");
@@ -690,6 +708,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn legacy_additive_provider_still_errors_on_live_config_parse_failure() {
with_test_home(|state, home| {
let provider = openclaw_provider("legacy-provider");
@@ -716,6 +735,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn update_persists_non_current_omo_variants_in_database() {
with_test_home(|state, _| {
for category in ["omo", "omo-slim"] {
@@ -750,6 +770,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn update_current_omo_variant_rewrites_config_from_saved_provider() {
with_test_home(|state, home| {
for category in ["omo", "omo-slim"] {
@@ -800,6 +821,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn update_current_omo_variant_does_not_persist_database_when_file_write_fails() {
with_test_home(|state, home| {
let provider = opencode_omo_provider("omo-current", "omo");
@@ -841,6 +863,7 @@ base_url = "http://localhost:8080"
}
#[test]
#[serial]
fn update_current_omo_variant_rolls_back_file_when_plugin_sync_fails() {
with_test_home(|state, home| {
let provider = opencode_omo_provider("omo-current", "omo");
+9
View File
@@ -1948,6 +1948,7 @@ mod tests {
dir: TempDir,
original_home: Option<String>,
original_userprofile: Option<String>,
original_test_home: Option<String>,
}
impl TempHome {
@@ -1955,14 +1956,17 @@ mod tests {
let dir = TempDir::new().expect("failed to create temp home");
let original_home = env::var("HOME").ok();
let original_userprofile = env::var("USERPROFILE").ok();
let original_test_home = env::var("CC_SWITCH_TEST_HOME").ok();
env::set_var("HOME", dir.path());
env::set_var("USERPROFILE", dir.path());
env::set_var("CC_SWITCH_TEST_HOME", dir.path());
Self {
dir,
original_home,
original_userprofile,
original_test_home,
}
}
}
@@ -1978,6 +1982,11 @@ mod tests {
Some(value) => env::set_var("USERPROFILE", value),
None => env::remove_var("USERPROFILE"),
}
match &self.original_test_home {
Some(value) => env::set_var("CC_SWITCH_TEST_HOME", value),
None => env::remove_var("CC_SWITCH_TEST_HOME"),
}
}
}