Skip to content

Commit 4db05f7

Browse files
committed
fix: honor cortex config dir for whoami
1 parent 7954d02 commit 4db05f7

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/cli/handlers.rs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use anyhow::{Result, bail};
77
use clap::CommandFactory;
88
use clap_complete::{Shell, generate};
99
use std::io::{self, BufRead, Write};
10+
use std::path::PathBuf;
1011

1112
use super::args::*;
1213
use crate::login::{
@@ -532,9 +533,7 @@ fn install_completions(shell: Shell) -> Result<()> {
532533
pub async fn run_whoami() -> Result<()> {
533534
use cortex_login::{AuthMode, load_auth_with_fallback, safe_format_key};
534535

535-
let cortex_home = dirs::home_dir()
536-
.map(|h| h.join(".cortex"))
537-
.unwrap_or_else(|| std::path::PathBuf::from(".cortex"));
536+
let cortex_home = get_whoami_cortex_home();
538537

539538
// Check environment variables first
540539
if let Ok(token) = std::env::var("CORTEX_AUTH_TOKEN")
@@ -589,6 +588,10 @@ pub async fn run_whoami() -> Result<()> {
589588
Ok(())
590589
}
591590

591+
fn get_whoami_cortex_home() -> PathBuf {
592+
cortex_engine::config::find_cortex_home().unwrap_or_else(|_| PathBuf::from(".cortex"))
593+
}
594+
592595
/// Resume a previous session.
593596
pub async fn run_resume(resume_cli: ResumeCommand) -> Result<()> {
594597
use crate::utils::resolve_session_id;
@@ -996,8 +999,50 @@ pub async fn run_history(history_cli: HistoryCommand) -> Result<()> {
996999

9971000
#[cfg(test)]
9981001
mod tests {
1002+
use super::get_whoami_cortex_home;
9991003
use clap_complete::Shell;
1004+
use serial_test::serial;
1005+
use std::env;
1006+
use std::ffi::{OsStr, OsString};
10001007
use std::io::{self, ErrorKind, Write};
1008+
use tempfile::TempDir;
1009+
1010+
struct EnvVarGuard {
1011+
key: &'static str,
1012+
original: Option<OsString>,
1013+
}
1014+
1015+
impl EnvVarGuard {
1016+
fn set(key: &'static str, value: impl AsRef<OsStr>) -> Self {
1017+
let original = env::var_os(key);
1018+
unsafe {
1019+
// SAFETY: Tests using this guard are serialized with `#[serial]`.
1020+
env::set_var(key, value);
1021+
}
1022+
Self { key, original }
1023+
}
1024+
1025+
fn remove(key: &'static str) -> Self {
1026+
let original = env::var_os(key);
1027+
unsafe {
1028+
// SAFETY: Tests using this guard are serialized with `#[serial]`.
1029+
env::remove_var(key);
1030+
}
1031+
Self { key, original }
1032+
}
1033+
}
1034+
1035+
impl Drop for EnvVarGuard {
1036+
fn drop(&mut self) {
1037+
unsafe {
1038+
// SAFETY: Tests using this guard are serialized with `#[serial]`.
1039+
match &self.original {
1040+
Some(value) => env::set_var(self.key, value),
1041+
None => env::remove_var(self.key),
1042+
}
1043+
}
1044+
}
1045+
}
10011046

10021047
// =========================================================================
10031048
// Shell name parsing tests (unit test the parsing logic directly)
@@ -1019,6 +1064,17 @@ mod tests {
10191064
}
10201065
}
10211066

1067+
#[test]
1068+
#[serial]
1069+
fn test_whoami_cortex_home_uses_cortex_config_dir() {
1070+
let temp_dir = TempDir::new().unwrap();
1071+
let cortex_config_dir = temp_dir.path().join("custom-config-dir");
1072+
let _cortex_home = EnvVarGuard::remove("CORTEX_HOME");
1073+
let _config_dir = EnvVarGuard::set("CORTEX_CONFIG_DIR", &cortex_config_dir);
1074+
1075+
assert_eq!(get_whoami_cortex_home(), cortex_config_dir);
1076+
}
1077+
10221078
/// Helper to extract shell name from path (like the real function does)
10231079
fn extract_shell_name_from_path(path: &str) -> &str {
10241080
std::path::Path::new(path)

0 commit comments

Comments
 (0)