返回 CodeWhale
home_resolver.rs
根目录 / crates / cli / tests / home_resolver.rs
1 use std::ffi::OsString;
2 use std::path::{Path, PathBuf};
3 use std::sync::{Mutex, MutexGuard};
4
5 use codewhale_config::{
6 CODEWHALE_APP_DIR, CONFIG_FILE_NAME, LEGACY_APP_DIR, codewhale_home, default_config_path,
7 };
8 use codewhale_secrets::FileKeyringStore;
9 use codewhale_state::StateStore;
10
11 static ENV_LOCK: Mutex<()> = Mutex::new(());
12
13 struct ProcessEnv {
14 _lock: MutexGuard<'static, ()>,
15 cwd: PathBuf,
16 home: Option<OsString>,
17 userprofile: Option<OsString>,
18 codewhale_home: Option<OsString>,
19 }
20
21 impl ProcessEnv {
22 fn install(root: &Path, home: &Path, userprofile: &Path, codewhale_home: &OsString) -> Self {
23 let lock = ENV_LOCK
24 .lock()
25 .unwrap_or_else(std::sync::PoisonError::into_inner);
26 let prior = Self {
27 _lock: lock,
28 cwd: std::env::current_dir().expect("current directory"),
29 home: std::env::var_os("HOME"),
30 userprofile: std::env::var_os("USERPROFILE"),
31 codewhale_home: std::env::var_os("CODEWHALE_HOME"),
32 };
33
34 // SAFETY: this integration-test process serializes all environment and
35 // current-directory mutation with ENV_LOCK.
36 unsafe {
37 std::env::set_var("HOME", home);
38 std::env::set_var("USERPROFILE", userprofile);
39 std::env::set_var("CODEWHALE_HOME", codewhale_home);
40 }
41 std::env::set_current_dir(root).expect("install isolated current directory");
42 prior
43 }
44 }
45
46 impl Drop for ProcessEnv {
47 fn drop(&mut self) {
48 std::env::set_current_dir(&self.cwd).expect("restore current directory");
49 // SAFETY: this integration-test process serializes all environment and
50 // current-directory mutation with ENV_LOCK.
51 unsafe {
52 restore_var("HOME", self.home.take());
53 restore_var("USERPROFILE", self.userprofile.take());
54 restore_var("CODEWHALE_HOME", self.codewhale_home.take());
55 }
56 }
57 }
58
59 unsafe fn restore_var(name: &str, value: Option<OsString>) {
60 match value {
61 Some(value) => unsafe { std::env::set_var(name, value) },
62 None => unsafe { std::env::remove_var(name) },
63 }
64 }
65
66 #[test]
67 fn whitespace_override_uses_home_before_userprofile_and_allows_legacy_fallback() {
68 let tmp = tempfile::tempdir().expect("temporary root");
69 let home = tmp.path().join("home");
70 let userprofile = tmp.path().join("userprofile");
71 let legacy = home.join(LEGACY_APP_DIR);
72 std::fs::create_dir_all(&legacy).expect("legacy directory");
73 std::fs::write(legacy.join(CONFIG_FILE_NAME), b"provider = \"ollama\"\n")
74 .expect("legacy config");
75 std::fs::write(legacy.join("state.db"), b"").expect("legacy state marker");
76
77 let _env = ProcessEnv::install(tmp.path(), &home, &userprofile, &OsString::from(" \t "));
78
79 assert_eq!(
80 codewhale_home().expect("config home"),
81 home.join(CODEWHALE_APP_DIR)
82 );
83 assert_eq!(
84 default_config_path().expect("config path"),
85 legacy.join(CONFIG_FILE_NAME)
86 );
87
88 let state = StateStore::open(None).expect("default state store");
89 assert_eq!(state.db_path(), legacy.join("state.db"));
90
91 let (primary_secrets, legacy_secrets) =
92 FileKeyringStore::default_paths_read_only().expect("secret paths");
93 assert_eq!(
94 primary_secrets,
95 home.join(CODEWHALE_APP_DIR)
96 .join("secrets")
97 .join("secrets.json")
98 );
99 assert_eq!(
100 legacy_secrets,
101 Some(legacy.join("secrets").join("secrets.json"))
102 );
103 }
104
105 #[cfg(unix)]
106 #[test]
107 fn non_unicode_override_is_one_explicit_isolation_boundary() {
108 use std::os::unix::ffi::OsStringExt;
109
110 use codewhale_state::default_state_db_path;
111
112 let tmp = tempfile::tempdir().expect("temporary root");
113 let home = tmp.path().join("home");
114 let userprofile = tmp.path().join("userprofile");
115 let explicit = tmp
116 .path()
117 .join(OsString::from_vec(b"codewhale-\xff-home".to_vec()));
118 let _env = ProcessEnv::install(
119 tmp.path(),
120 &home,
121 &userprofile,
122 &explicit.as_os_str().to_os_string(),
123 );
124
125 assert_eq!(codewhale_home().expect("config home"), explicit);
126 assert_eq!(
127 default_config_path().expect("config path"),
128 explicit.join(CONFIG_FILE_NAME)
129 );
130
131 assert_eq!(default_state_db_path(), explicit.join("state.db"));
132
133 let (primary_secrets, legacy_secrets) =
134 FileKeyringStore::default_paths_read_only().expect("secret paths");
135 assert_eq!(
136 primary_secrets,
137 explicit.join("secrets").join("secrets.json")
138 );
139 assert_eq!(legacy_secrets, None);
140 }
141
141 lines RUST