diff --git a/src/cortex-cli/src/debug_cmd/utils.rs b/src/cortex-cli/src/debug_cmd/utils.rs index fedf63a6f..5724fe1fe 100644 --- a/src/cortex-cli/src/debug_cmd/utils.rs +++ b/src/cortex-cli/src/debug_cmd/utils.rs @@ -9,16 +9,12 @@ use std::path::PathBuf; /// Get the cortex home directory. pub fn get_cortex_home() -> PathBuf { - dirs::home_dir() - .map(|h| h.join(".cortex")) - .unwrap_or_else(|| PathBuf::from(".cortex")) + crate::utils::paths::get_cortex_home() } /// Get the cortex home directory (alternative function name for clarity). pub fn get_cortex_home_or_default() -> PathBuf { - dirs::home_dir() - .map(|h| h.join(".cortex")) - .unwrap_or_else(|| PathBuf::from(".cortex")) + get_cortex_home() } /// Calculate directory size. diff --git a/src/cortex-cli/src/logs_cmd.rs b/src/cortex-cli/src/logs_cmd.rs index f525efc3c..754369b6b 100644 --- a/src/cortex-cli/src/logs_cmd.rs +++ b/src/cortex-cli/src/logs_cmd.rs @@ -11,6 +11,8 @@ use clap::Parser; use serde::Serialize; use std::path::PathBuf; +use crate::utils::paths::get_cortex_home; + /// Logs CLI command. #[derive(Debug, Parser)] pub struct LogsCli { @@ -58,13 +60,7 @@ struct LogFileInfo { /// Get the logs directory. fn get_logs_dir() -> PathBuf { - dirs::cache_dir() - .map(|c| c.join("cortex").join("logs")) - .unwrap_or_else(|| { - dirs::home_dir() - .map(|h| h.join(".cache").join("cortex").join("logs")) - .unwrap_or_else(|| PathBuf::from(".cache/cortex/logs")) - }) + get_cortex_home().join("logs") } /// Format bytes as human-readable string. diff --git a/src/cortex-cli/tests/logs_paths.rs b/src/cortex-cli/tests/logs_paths.rs new file mode 100644 index 000000000..8d7ca0a95 --- /dev/null +++ b/src/cortex-cli/tests/logs_paths.rs @@ -0,0 +1,53 @@ +use std::path::PathBuf; +use std::process::Command; + +#[test] +fn logs_directory_matches_debug_paths_logs_dir() { + let temp_dir = tempfile::tempdir().expect("create temp dir"); + let cortex_home = temp_dir.path().join("cortex-home"); + + let debug_output = Command::new(env!("CARGO_BIN_EXE_Cortex")) + .args(["debug", "paths", "--json"]) + .env("CORTEX_HOME", &cortex_home) + .env("NO_COLOR", "1") + .output() + .expect("run Cortex debug paths"); + + assert!( + debug_output.status.success(), + "debug paths failed\nstdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&debug_output.stdout), + String::from_utf8_lossy(&debug_output.stderr) + ); + + let paths: serde_json::Value = + serde_json::from_slice(&debug_output.stdout).expect("debug paths should return JSON"); + let debug_logs_dir = paths["logs_dir"]["path"] + .as_str() + .expect("debug paths should include logs_dir.path"); + + let logs_output = Command::new(env!("CARGO_BIN_EXE_Cortex")) + .args(["logs"]) + .env("CORTEX_HOME", &cortex_home) + .env("NO_COLOR", "1") + .output() + .expect("run Cortex logs"); + + let logs_stdout = String::from_utf8_lossy(&logs_output.stdout); + assert!( + logs_output.status.success(), + "logs failed\nstdout:{logs_stdout}\nstderr:\n{}", + String::from_utf8_lossy(&logs_output.stderr) + ); + + let logs_dir = logs_stdout + .lines() + .find_map(|line| line.strip_prefix("Logs directory: ")) + .expect("logs output should include its logs directory"); + + assert_eq!( + PathBuf::from(logs_dir), + PathBuf::from(debug_logs_dir), + "cortex logs should report the same logs directory as cortex debug paths" + ); +}