|
14 | 14 |
|
15 | 15 | use anyhow::{Context, Result, bail}; |
16 | 16 | use clap::Parser; |
| 17 | +use cortex_engine::config::find_cortex_home; |
17 | 18 | use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher}; |
18 | 19 | use serde::Serialize; |
19 | 20 | use std::path::{Path, PathBuf}; |
@@ -698,9 +699,9 @@ struct PluginInfo { |
698 | 699 |
|
699 | 700 | /// Get the plugins directory. |
700 | 701 | fn get_plugins_dir() -> PathBuf { |
701 | | - dirs::home_dir() |
702 | | - .map(|h| h.join(".cortex").join("plugins")) |
703 | | - .unwrap_or_else(|| PathBuf::from(".cortex/plugins")) |
| 702 | + find_cortex_home() |
| 703 | + .unwrap_or_else(|_| PathBuf::from(".cortex")) |
| 704 | + .join("plugins") |
704 | 705 | } |
705 | 706 |
|
706 | 707 | // ============================================================================= |
@@ -2210,11 +2211,65 @@ async fn run_publish(args: PluginPublishArgs) -> Result<()> { |
2210 | 2211 | mod tests { |
2211 | 2212 | use super::*; |
2212 | 2213 | use clap::CommandFactory; |
| 2214 | + use serial_test::serial; |
| 2215 | + use std::env; |
| 2216 | + use std::ffi::{OsStr, OsString}; |
| 2217 | + use tempfile::TempDir; |
| 2218 | + |
| 2219 | + struct EnvVarGuard { |
| 2220 | + key: &'static str, |
| 2221 | + original: Option<OsString>, |
| 2222 | + } |
| 2223 | + |
| 2224 | + impl EnvVarGuard { |
| 2225 | + fn set(key: &'static str, value: impl AsRef<OsStr>) -> Self { |
| 2226 | + let original = env::var_os(key); |
| 2227 | + // SAFETY: These tests are serialized and restore the environment on drop. |
| 2228 | + unsafe { |
| 2229 | + env::set_var(key, value); |
| 2230 | + } |
| 2231 | + Self { key, original } |
| 2232 | + } |
| 2233 | + |
| 2234 | + fn remove(key: &'static str) -> Self { |
| 2235 | + let original = env::var_os(key); |
| 2236 | + // SAFETY: These tests are serialized and restore the environment on drop. |
| 2237 | + unsafe { |
| 2238 | + env::remove_var(key); |
| 2239 | + } |
| 2240 | + Self { key, original } |
| 2241 | + } |
| 2242 | + } |
| 2243 | + |
| 2244 | + impl Drop for EnvVarGuard { |
| 2245 | + fn drop(&mut self) { |
| 2246 | + // SAFETY: These tests are serialized and restore the environment before returning. |
| 2247 | + unsafe { |
| 2248 | + match &self.original { |
| 2249 | + Some(value) => env::set_var(self.key, value), |
| 2250 | + None => env::remove_var(self.key), |
| 2251 | + } |
| 2252 | + } |
| 2253 | + } |
| 2254 | + } |
2213 | 2255 |
|
2214 | 2256 | // ========================================================================== |
2215 | 2257 | // PluginInfo serialization tests |
2216 | 2258 | // ========================================================================== |
2217 | 2259 |
|
| 2260 | + #[test] |
| 2261 | + #[serial] |
| 2262 | + fn test_get_plugins_dir_uses_cortex_home() { |
| 2263 | + let temp_dir = TempDir::new().unwrap(); |
| 2264 | + let cortex_home = temp_dir.path().join("custom-cortex-home"); |
| 2265 | + let _config_dir = EnvVarGuard::remove("CORTEX_CONFIG_DIR"); |
| 2266 | + let _cortex_home = EnvVarGuard::set("CORTEX_HOME", &cortex_home); |
| 2267 | + |
| 2268 | + let plugins_dir = get_plugins_dir(); |
| 2269 | + |
| 2270 | + assert_eq!(plugins_dir, cortex_home.join("plugins")); |
| 2271 | + } |
| 2272 | + |
2218 | 2273 | #[test] |
2219 | 2274 | fn test_plugin_info_serialization_json() { |
2220 | 2275 | let info = PluginInfo { |
|
0 commit comments