From 0842b3f52848eff484bd063195da0ae5ee5ac201 Mon Sep 17 00:00:00 2001 From: cyq <15000851237@163.com> Date: Tue, 2 Jun 2026 09:23:00 +0800 Subject: [PATCH] test(config): use real legacy home on windows --- crates/config/src/lib.rs | 70 +++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index 9e652302..68082ad4 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -3320,10 +3320,6 @@ unix_socket_path = "/tmp/cw-hooks.sock" struct HomeEnvGuard { home: Option, userprofile: Option, - #[cfg(windows)] - homedrive: Option, - #[cfg(windows)] - homepath: Option, codewhale_home: Option, } @@ -3339,16 +3335,6 @@ unix_socket_path = "/tmp/cw-hooks.sock" Some(value) => env::set_var("USERPROFILE", value), None => env::remove_var("USERPROFILE"), } - #[cfg(windows)] - match self.homedrive.take() { - Some(value) => env::set_var("HOMEDRIVE", value), - None => env::remove_var("HOMEDRIVE"), - } - #[cfg(windows)] - match self.homepath.take() { - Some(value) => env::set_var("HOMEPATH", value), - None => env::remove_var("HOMEPATH"), - } match self.codewhale_home.take() { Some(value) => env::set_var("CODEWHALE_HOME", value), None => env::remove_var("CODEWHALE_HOME"), @@ -3357,6 +3343,34 @@ unix_socket_path = "/tmp/cw-hooks.sock" } } + struct LegacyConfigGuard { + path: PathBuf, + original: Option>, + } + + impl LegacyConfigGuard { + fn install(path: PathBuf, contents: &[u8]) -> Self { + let original = fs::read(&path).ok(); + fs::create_dir_all(path.parent().expect("legacy config parent")) + .expect("legacy dir"); + fs::write(&path, contents).expect("legacy config"); + Self { path, original } + } + } + + impl Drop for LegacyConfigGuard { + fn drop(&mut self) { + if let Some(original) = self.original.take() { + let _ = fs::write(&self.path, original); + } else { + let _ = fs::remove_file(&self.path); + if let Some(parent) = self.path.parent() { + let _ = fs::remove_dir(parent); + } + } + } + } + let unique = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .expect("clock") @@ -3365,44 +3379,32 @@ unix_socket_path = "/tmp/cw-hooks.sock" "codewhale-config-migration-{}-{unique}", std::process::id() )); + #[cfg(windows)] + let legacy_dir = legacy_deepseek_home().expect("legacy home"); + #[cfg(not(windows))] let legacy_dir = home.join(LEGACY_APP_DIR); let primary_dir = home.join(CODEWHALE_APP_DIR); - fs::create_dir_all(&legacy_dir).expect("legacy dir"); - fs::write( - legacy_dir.join(CONFIG_FILE_NAME), - "provider = \"deepseek\"\n", - ) - .expect("legacy config"); + let legacy_config = legacy_dir.join(CONFIG_FILE_NAME); + let _legacy = + LegacyConfigGuard::install(legacy_config.clone(), b"provider = \"deepseek\"\n"); let _env = HomeEnvGuard { home: env::var_os("HOME"), userprofile: env::var_os("USERPROFILE"), - #[cfg(windows)] - homedrive: env::var_os("HOMEDRIVE"), - #[cfg(windows)] - homepath: env::var_os("HOMEPATH"), codewhale_home: env::var_os("CODEWHALE_HOME"), }; // Safety: test-only environment mutation is serialized by env_lock(). unsafe { env::set_var("HOME", &home); env::set_var("USERPROFILE", &home); - #[cfg(windows)] - { - let mut components = home.components(); - if let Some(std::path::Component::Prefix(prefix)) = components.next() { - env::set_var("HOMEDRIVE", prefix.as_os_str()); - env::set_var("HOMEPATH", components.as_path()); - } - } - env::remove_var("CODEWHALE_HOME"); + env::set_var("CODEWHALE_HOME", &primary_dir); } let migration = migrate_config_if_needed() .expect("migration") .expect("legacy config should be copied"); - assert_eq!(migration.legacy_path, legacy_dir.join(CONFIG_FILE_NAME)); + assert_eq!(migration.legacy_path, legacy_config); assert_eq!(migration.primary_path, primary_dir.join(CONFIG_FILE_NAME)); let notice = migration.user_notice(); assert!(notice.contains(&legacy_dir.join(CONFIG_FILE_NAME).display().to_string()));