Skip to content

Commit 4431675

Browse files
committed
fix: resolve unit test failures and improve config file handling
1 parent 24f90f9 commit 4431675

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

src/config.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl Config {
231231
}
232232

233233
/// Load configuration with priority order:
234-
/// 1. Explicit --config path (if different from default)
234+
/// 1. Explicit --config path (if exists and different from default)
235235
/// 2. Backend.AI environment variables
236236
/// 3. Current directory config.yaml
237237
/// 4. XDG config directory ($XDG_CONFIG_HOME/bssh/config.yaml or ~/.config/bssh/config.yaml)
@@ -244,18 +244,19 @@ impl Config {
244244
// Check if user explicitly specified a config file (different from default)
245245
let is_custom_config = expanded_cli_path != expanded_default_path;
246246

247-
if is_custom_config {
248-
// User explicitly specified a config file - use it with highest priority
247+
if is_custom_config && expanded_cli_path.exists() {
248+
// User explicitly specified a config file and it exists - use it with highest priority
249249
tracing::debug!(
250250
"Using explicitly specified config file: {:?}",
251251
expanded_cli_path
252252
);
253-
if expanded_cli_path.exists() {
254-
return Self::load(&expanded_cli_path).await;
255-
} else {
256-
// If the user specified a file that doesn't exist, return an error
257-
anyhow::bail!("Config file not found: {:?}", expanded_cli_path);
258-
}
253+
return Self::load(&expanded_cli_path).await;
254+
} else if is_custom_config {
255+
// Custom config specified but doesn't exist - log and continue
256+
tracing::debug!(
257+
"Custom config file not found, continuing with other sources: {:?}",
258+
expanded_cli_path
259+
);
259260
}
260261

261262
// Try Backend.AI environment first

src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ async fn main() -> Result<()> {
6666
// Initialize logging
6767
init_logging(cli.verbose);
6868

69+
// Check if user explicitly specified --config option
70+
let args: Vec<String> = std::env::args().collect();
71+
let has_explicit_config = args.iter().any(|arg| arg == "--config");
72+
73+
// If user explicitly specified --config, ensure the file exists
74+
if has_explicit_config {
75+
let expanded_path = if cli.config.starts_with("~") {
76+
let path_str = cli.config.to_string_lossy();
77+
if let Ok(home) = std::env::var("HOME") {
78+
PathBuf::from(path_str.replacen("~", &home, 1))
79+
} else {
80+
cli.config.clone()
81+
}
82+
} else {
83+
cli.config.clone()
84+
};
85+
86+
if !expanded_path.exists() {
87+
anyhow::bail!("Config file not found: {:?}", expanded_path);
88+
}
89+
}
90+
6991
// Load configuration with priority
7092
let config = Config::load_with_priority(&cli.config).await?;
7193

0 commit comments

Comments
 (0)