Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/cortex-cli/src/debug_cmd/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 3 additions & 7 deletions src/cortex-cli/src/logs_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
53 changes: 53 additions & 0 deletions src/cortex-cli/tests/logs_paths.rs
Original file line number Diff line number Diff line change
@@ -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"
);
}