Skip to content

Commit ef0ab9e

Browse files
committed
fix: honor cortex home for import
1 parent 7954d02 commit ef0ab9e

2 files changed

Lines changed: 61 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/import_cmd.rs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::collections::HashSet;
88
use std::path::{Path, PathBuf};
99

1010
use crate::styled_output::{print_info, print_success, print_warning};
11+
use cortex_engine::config::find_cortex_home;
1112
use cortex_engine::rollout::recorder::{RolloutRecorder, SessionMeta};
1213
use cortex_engine::rollout::{SESSIONS_SUBDIR, get_rollout_path};
1314
use cortex_protocol::{
@@ -45,9 +46,7 @@ impl ImportCommand {
4546
bail!("Error: Source path cannot be empty\n\nUsage: cortex import <FILE_OR_URL>");
4647
}
4748

48-
let cortex_home = dirs::home_dir()
49-
.map(|h| h.join(".cortex"))
50-
.ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
49+
let cortex_home = get_cortex_home()?;
5150

5251
// Read the export data
5352
let (json_content, is_from_url) = if self.source == "-" {
@@ -236,6 +235,11 @@ impl ImportCommand {
236235
}
237236
}
238237

238+
/// Get the cortex home directory.
239+
fn get_cortex_home() -> Result<PathBuf> {
240+
find_cortex_home().context("Could not determine Cortex home directory")
241+
}
242+
239243
/// Fetch content from a URL.
240244
async fn fetch_url(url: &str) -> Result<String> {
241245
// Use curl for fetching
@@ -494,6 +498,58 @@ fn message_to_event(message: &ExportMessage, turn_id: &mut u64, cwd: &Path) -> R
494498
#[cfg(test)]
495499
mod tests {
496500
use super::*;
501+
use serial_test::serial;
502+
use std::env;
503+
use std::ffi::{OsStr, OsString};
504+
use tempfile::TempDir;
505+
506+
struct EnvVarGuard {
507+
key: &'static str,
508+
original: Option<OsString>,
509+
}
510+
511+
impl EnvVarGuard {
512+
fn set(key: &'static str, value: impl AsRef<OsStr>) -> Self {
513+
let original = env::var_os(key);
514+
// SAFETY: These tests are serialized and restore the environment on drop.
515+
unsafe {
516+
env::set_var(key, value);
517+
}
518+
Self { key, original }
519+
}
520+
521+
fn remove(key: &'static str) -> Self {
522+
let original = env::var_os(key);
523+
// SAFETY: These tests are serialized and restore the environment on drop.
524+
unsafe {
525+
env::remove_var(key);
526+
}
527+
Self { key, original }
528+
}
529+
}
530+
531+
impl Drop for EnvVarGuard {
532+
fn drop(&mut self) {
533+
// SAFETY: These tests are serialized and restore the environment before returning.
534+
unsafe {
535+
match &self.original {
536+
Some(value) => env::set_var(self.key, value),
537+
None => env::remove_var(self.key),
538+
}
539+
}
540+
}
541+
}
542+
543+
#[test]
544+
#[serial]
545+
fn test_get_cortex_home_uses_cortex_home_env() {
546+
let temp_dir = TempDir::new().unwrap();
547+
let cortex_home = temp_dir.path().join("custom-cortex-home");
548+
let _config_dir = EnvVarGuard::remove("CORTEX_CONFIG_DIR");
549+
let _cortex_home = EnvVarGuard::set("CORTEX_HOME", &cortex_home);
550+
551+
assert_eq!(get_cortex_home().unwrap(), cortex_home);
552+
}
497553

498554
#[test]
499555
fn test_parse_export_json() {

0 commit comments

Comments
 (0)