Skip to content

Commit 9dabf66

Browse files
committed
test(cli): add basic config parse test
1 parent a731c16 commit 9dabf66

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

packages/debmagic/src/config.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,13 @@ impl Default for Config {
2424
}
2525

2626
impl Config {
27-
pub fn new(cli_args: &Cli) -> anyhow::Result<Self> {
27+
pub fn new(config_files: &Vec<PathBuf>, _cli_args: &Cli) -> anyhow::Result<Self> {
2828
let mut builder = ConfigBuilder::builder();
2929

30-
let xdg_config_file = dirs::config_dir()
31-
.map(|p| p.join("debmagic").join("config.toml"))
32-
.ok_or(anyhow!("Could not determine user config directory"))?;
33-
if xdg_config_file.is_file() {
34-
let xdg_config_file = xdg_config_file.to_str();
35-
if let Some(xdg_config_file) = xdg_config_file {
36-
builder = builder.add_source(File::with_name(xdg_config_file));
37-
}
30+
for file in config_files {
31+
builder = builder.add_source(File::with_name(&file.to_string_lossy()));
3832
}
3933

40-
if let Some(config_override) = cli_args.config.as_ref().and_then(|f| f.to_str()) {
41-
builder = builder.add_source(File::with_name(config_override));
42-
}
4334
// TODO: reimplement cli arg overwrites
4435
let build = builder
4536
.build()
@@ -50,3 +41,27 @@ impl Config {
5041
config
5142
}
5243
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use crate::cli::Commands;
48+
49+
use super::*;
50+
51+
#[test]
52+
fn it_loads_a_simple_config() -> Result<(), anyhow::Error> {
53+
let test_asset_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
54+
.join("tests")
55+
.join("assets");
56+
let cfg = Config::new(
57+
&vec![test_asset_dir.join("config1.toml")],
58+
&Cli {
59+
config: None,
60+
command: Commands::Version {},
61+
},
62+
)?;
63+
assert!(cfg.dry_run);
64+
65+
Ok(())
66+
}
67+
}

packages/debmagic/src/main.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{env, path};
1+
use std::{
2+
env,
3+
path::{self, PathBuf},
4+
};
25

36
use anyhow::Context;
47
use clap::{CommandFactory, Parser};
@@ -13,9 +16,26 @@ pub mod build;
1316
pub mod cli;
1417
pub mod config;
1518

19+
fn get_config_file_paths() -> Vec<PathBuf> {
20+
let mut config_file_paths = vec![];
21+
let xdg_config_file = dirs::config_dir().map(|p| p.join("debmagic").join("config.toml"));
22+
if let Some(xdg_config_file) = xdg_config_file {
23+
if xdg_config_file.is_file() {
24+
config_file_paths.push(xdg_config_file);
25+
}
26+
}
27+
28+
config_file_paths
29+
}
30+
1631
fn main() -> anyhow::Result<()> {
1732
let cli = Cli::parse();
18-
let config = Config::new(&cli)?;
33+
34+
let mut config_file_paths = get_config_file_paths();
35+
if let Some(config_override) = &cli.config {
36+
config_file_paths.push(config_override.clone());
37+
}
38+
let config = Config::new(&config_file_paths, &cli)?;
1939

2040
let current_dir = env::current_dir()?;
2141
match &cli.command {

packages/debmagic/tests/test_config.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)