diff --git a/src/cortex-cli/src/cache_cmd.rs b/src/cortex-cli/src/cache_cmd.rs index 423028f2..e92d4179 100644 --- a/src/cortex-cli/src/cache_cmd.rs +++ b/src/cortex-cli/src/cache_cmd.rs @@ -10,6 +10,8 @@ use clap::Parser; use serde::Serialize; use std::path::PathBuf; +use crate::debug_cmd::utils::get_cortex_home; + /// Cache CLI command. #[derive(Debug, Parser)] pub struct CacheCli { @@ -110,13 +112,7 @@ struct CacheCategory { /// Get the cache directory. fn get_cache_dir() -> PathBuf { - dirs::cache_dir() - .map(|c| c.join("cortex")) - .unwrap_or_else(|| { - dirs::home_dir() - .map(|h| h.join(".cache").join("cortex")) - .unwrap_or_else(|| PathBuf::from(".cache/cortex")) - }) + get_cortex_home().join("cache") } /// Calculate directory size recursively. diff --git a/src/cortex-cli/tests/cache_paths.rs b/src/cortex-cli/tests/cache_paths.rs new file mode 100644 index 00000000..ab81e457 --- /dev/null +++ b/src/cortex-cli/tests/cache_paths.rs @@ -0,0 +1,31 @@ +use std::process::Command; + +use serde_json::Value; + +fn cortex_json(args: &[&str]) -> Value { + let output = Command::new(env!("CARGO_BIN_EXE_Cortex")) + .args(args) + .output() + .expect("failed to run Cortex test binary"); + + assert!( + output.status.success(), + "Cortex {:?} failed\nstdout:\n{}\nstderr:\n{}", + args, + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + + serde_json::from_slice(&output.stdout).expect("Cortex command should print JSON") +} + +#[test] +fn cache_show_json_uses_debug_paths_cache_dir() { + let cache_show = cortex_json(&["cache", "show", "--json"]); + let debug_paths = cortex_json(&["debug", "paths", "--json"]); + + assert_eq!( + cache_show["cache_dir"].as_str(), + debug_paths["cache_dir"]["path"].as_str() + ); +}