Skip to content

Commit 091b03d

Browse files
committed
fix: honor cortex home for dag storage
1 parent 7954d02 commit 091b03d

3 files changed

Lines changed: 74 additions & 8 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/dag_cmd/helpers.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
33
use anyhow::{Context, Result};
44
use cortex_agents::task::{TaskDag, TaskId, TaskSpec};
5+
use cortex_engine::config::find_cortex_home;
56
use std::collections::HashMap;
67
use std::path::PathBuf;
78

89
use super::types::{DagExecutionStats, DagOutputFormat, DagSpecInput};
910

1011
/// Get the DAG store path.
1112
pub fn get_dag_store_path() -> Result<PathBuf> {
12-
let data_dir = dirs::data_dir()
13-
.or_else(|| dirs::home_dir().map(|h| h.join(".local/share")))
14-
.context("Could not determine data directory")?;
15-
Ok(data_dir.join("cortex").join("dags"))
13+
let cortex_home = find_cortex_home().context("Could not determine Cortex home directory")?;
14+
Ok(cortex_home.join("dags"))
1615
}
1716

1817
/// Load DAG specification from file.

src/cortex-cli/src/dag_cmd/tests.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,53 @@
11
//! Tests for DAG command functionality.
22
3-
use super::helpers::convert_specs;
3+
use super::helpers::{convert_specs, get_dag_store_path};
44
use super::types::{DagSpecInput, TaskSpecInput};
55
use cortex_agents::task::{DagHydrator, Task, TaskId, TaskSpec};
6+
use serial_test::serial;
67
use std::collections::HashMap;
8+
use std::env;
9+
use std::ffi::{OsStr, OsString};
10+
use tempfile::TempDir;
711

812
use super::executor::TaskExecutor;
913

14+
struct EnvVarGuard {
15+
key: &'static str,
16+
original: Option<OsString>,
17+
}
18+
19+
impl EnvVarGuard {
20+
fn set(key: &'static str, value: impl AsRef<OsStr>) -> Self {
21+
let original = env::var_os(key);
22+
// SAFETY: These tests are serialized and restore the environment on drop.
23+
unsafe {
24+
env::set_var(key, value);
25+
}
26+
Self { key, original }
27+
}
28+
29+
fn remove(key: &'static str) -> Self {
30+
let original = env::var_os(key);
31+
// SAFETY: These tests are serialized and restore the environment on drop.
32+
unsafe {
33+
env::remove_var(key);
34+
}
35+
Self { key, original }
36+
}
37+
}
38+
39+
impl Drop for EnvVarGuard {
40+
fn drop(&mut self) {
41+
// SAFETY: These tests are serialized and restore the environment before returning.
42+
unsafe {
43+
match &self.original {
44+
Some(value) => env::set_var(self.key, value),
45+
None => env::remove_var(self.key),
46+
}
47+
}
48+
}
49+
}
50+
1051
#[test]
1152
fn test_load_yaml_spec() {
1253
let yaml = r#"
@@ -31,6 +72,33 @@ tasks:
3172
assert_eq!(spec.tasks[1].depends_on, vec!["setup"]);
3273
}
3374

75+
#[test]
76+
#[serial]
77+
fn dag_store_path_uses_cortex_home() {
78+
let temp_dir = TempDir::new().unwrap();
79+
let cortex_home = temp_dir.path().join("custom-cortex-home");
80+
let _config_dir = EnvVarGuard::remove("CORTEX_CONFIG_DIR");
81+
let _cortex_home = EnvVarGuard::set("CORTEX_HOME", &cortex_home);
82+
83+
let path = get_dag_store_path().unwrap();
84+
85+
assert_eq!(path, cortex_home.join("dags"));
86+
}
87+
88+
#[test]
89+
#[serial]
90+
fn dag_store_path_prefers_cortex_config_dir() {
91+
let temp_dir = TempDir::new().unwrap();
92+
let cortex_home = temp_dir.path().join("cortex-home");
93+
let cortex_config_dir = temp_dir.path().join("cortex-config-dir");
94+
let _cortex_home = EnvVarGuard::set("CORTEX_HOME", &cortex_home);
95+
let _config_dir = EnvVarGuard::set("CORTEX_CONFIG_DIR", &cortex_config_dir);
96+
97+
let path = get_dag_store_path().unwrap();
98+
99+
assert_eq!(path, cortex_config_dir.join("dags"));
100+
}
101+
34102
#[test]
35103
fn test_convert_specs() {
36104
let input = DagSpecInput {

0 commit comments

Comments
 (0)