Skip to content

Commit 1eb64bb

Browse files
committed
fix: honor cortex home for plugin storage
1 parent 7954d02 commit 1eb64bb

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

src/cortex-cli/src/agent_cmd/tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
#[cfg(test)]
44
mod tests {
55
use crate::agent_cmd::cli::{CopyArgs, ExportArgs};
6-
use crate::agent_cmd::loader::{
7-
load_builtin_agents, parse_frontmatter, read_file_with_encoding,
8-
};
6+
use crate::agent_cmd::loader::{load_builtin_agents, parse_frontmatter};
97
use crate::agent_cmd::types::AgentMode;
8+
use crate::utils::file::read_file_with_encoding;
109

1110
#[test]
1211
fn test_read_file_with_utf8() {

src/cortex-cli/src/plugin_cmd.rs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
use anyhow::{Context, Result, bail};
1616
use clap::Parser;
17+
use cortex_engine::config::find_cortex_home;
1718
use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
1819
use serde::Serialize;
1920
use std::path::{Path, PathBuf};
@@ -698,9 +699,9 @@ struct PluginInfo {
698699

699700
/// Get the plugins directory.
700701
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")
704705
}
705706

706707
// =============================================================================
@@ -2210,11 +2211,65 @@ async fn run_publish(args: PluginPublishArgs) -> Result<()> {
22102211
mod tests {
22112212
use super::*;
22122213
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+
}
22132255

22142256
// ==========================================================================
22152257
// PluginInfo serialization tests
22162258
// ==========================================================================
22172259

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+
22182273
#[test]
22192274
fn test_plugin_info_serialization_json() {
22202275
let info = PluginInfo {

0 commit comments

Comments
 (0)