Skip to content

Commit cbf4bf6

Browse files
committed
add more tests
1 parent 1cbc4b8 commit cbf4bf6

3 files changed

Lines changed: 130 additions & 13 deletions

File tree

Cargo.lock

Lines changed: 31 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coman/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,8 @@ vergen-gix = { version = "1.0.2", features = ["build", "cargo"] }
9090

9191
[dev-dependencies]
9292
claim = "0.5.0"
93+
current_dir = "0.1.2"
9394
injectorpp = "0.4.0"
9495
rstest = "0.26.1"
96+
tempfile = "3.24.0"
97+
tmp_env = "0.1.1"

coman/src/config.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,21 @@ impl Config {
267267
self.project_layer.as_ref().unwrap().write()?;
268268
}
269269
}
270+
271+
// reload config
272+
let mut builder =
273+
config::Config::builder().add_source(config::File::from_str(DEFAULT_CONFIG_TOML, config::FileFormat::Toml));
274+
builder = builder.add_source(config::File::from_str(
275+
&self.global_layer.data.to_string(),
276+
config::FileFormat::Toml,
277+
));
278+
if let Some(project_layer) = self.project_layer.clone() {
279+
builder = builder.add_source(config::File::from_str(
280+
&project_layer.data.to_string(),
281+
config::FileFormat::Toml,
282+
));
283+
}
284+
self.values = builder.build()?.try_deserialize()?;
270285
Ok(())
271286
}
272287

@@ -383,3 +398,84 @@ pub fn get_config_dir() -> PathBuf {
383398
fn project_directory() -> Option<ProjectDirs> {
384399
ProjectDirs::from("ch", "sdsc", env!("CARGO_PKG_NAME"))
385400
}
401+
#[cfg(test)]
402+
mod tests {
403+
use claim::*;
404+
use current_dir::*;
405+
use tempfile::tempdir;
406+
407+
use super::*;
408+
409+
#[test]
410+
fn test_get_project_local_config() {
411+
let temp_dir = tempdir().expect("couldn't create temp dir");
412+
413+
let pwd = temp_dir.path().join("sub").join("folder");
414+
std::fs::create_dir_all(pwd.clone()).expect("couldn't create dir");
415+
let mut locked_cwd = Cwd::mutex().lock().expect("couldn't get cwd lock");
416+
locked_cwd.set(&pwd).expect("couldn't set current dir");
417+
assert_eq!(pwd, std::env::current_dir().expect("couldn't get current dir"));
418+
419+
let config = temp_dir.path().join("coman.toml");
420+
std::fs::write(&config, "").expect("couldn't create config file");
421+
422+
let config_file = get_project_local_config_file();
423+
assert_some!(config_file.clone());
424+
assert_eq!(config_file.unwrap(), config);
425+
}
426+
427+
#[test]
428+
fn test_layer_from_path() {
429+
let temp_dir = tempdir().expect("couldn't create temp dir");
430+
let config = temp_dir.path().join("coman.toml");
431+
let content = "#some comment[cscs]\nvalue=10\n";
432+
std::fs::write(&config, content).expect("couldn't write config file");
433+
let layer = Layer::from_path(config.clone()).expect("couldn't load config");
434+
assert_eq!(layer.source, config);
435+
assert_eq!(layer.data.to_string(), content);
436+
}
437+
438+
#[test]
439+
fn test_layer_get_set() {
440+
let temp_dir = tempdir().expect("couldn't create temp dir");
441+
let config = temp_dir.path().join("coman.toml");
442+
let content = "[cscs]\nvalue=10\n";
443+
std::fs::write(&config, content).expect("couldn't write config file");
444+
let mut layer = Layer::from_path(config.clone()).expect("couldn't load config");
445+
assert_eq!(layer.get("cscs.value").unwrap().unwrap(), "10");
446+
assert_none!(layer.get("cscs.other_value").unwrap());
447+
layer.set("cscs.other_value", 20).unwrap();
448+
assert_eq!(layer.get("cscs.other_value").unwrap().unwrap(), "20");
449+
assert_eq!(layer.data.to_string(), "[cscs]\nvalue=10\nother_value = 20\n");
450+
}
451+
452+
#[test]
453+
fn test_config_read_write() {
454+
let project_dir = tempdir().expect("couldn't create temp dir");
455+
456+
let mut locked_cwd = Cwd::mutex().lock().expect("couldn't get cwd lock");
457+
locked_cwd.set(&project_dir).expect("couldn't set current dir");
458+
assert_eq!(
459+
project_dir.path(),
460+
std::env::current_dir().expect("couldn't get current dir")
461+
);
462+
463+
let project_config = project_dir.path().join("coman.toml");
464+
std::fs::write(&project_config, "[cscs]\ncurrent_system = \"project\"").expect("couldn't create config file");
465+
466+
let home_dir = tempdir().expect("couldn't create temp dir");
467+
let global_config = home_dir.path().join(".config").join("coman").join("coman.toml");
468+
std::fs::create_dir_all(global_config.parent().unwrap()).expect("couldn't create config dir");
469+
std::fs::write(&global_config, "[cscs]\ncurrent_system = \"global\"").expect("couldn't create config file");
470+
471+
let _tmp_env = tmp_env::set_var("HOME", home_dir.path().as_os_str());
472+
let mut conf = Config::new().expect("couldn't load config");
473+
assert_eq!(conf.values.cscs.current_system, "project");
474+
conf.set("cscs.current_system", "global2", true)
475+
.expect("couldn't set global config value");
476+
assert_eq!(conf.values.cscs.current_system, "project");
477+
conf.set("cscs.current_system", "project2", false)
478+
.expect("couldn't set global config value");
479+
assert_eq!(conf.values.cscs.current_system, "project2");
480+
}
481+
}

0 commit comments

Comments
 (0)