Signature & tests for fn load_from_file()

This function almost writes itself. I need a thin layer to handle the
file IO errors and report them appropriately, and then all the magic is
a pass-through of the existing read_conf_str.

I've made basic unit tests for the most obvious scenarios. The test for
missing-file behavior is incomplete because I need to create a new error
variant.
This commit is contained in:
2025-07-17 14:20:21 -05:00
parent 551297f46b
commit 246987fa68
2 changed files with 35 additions and 0 deletions

View File

@@ -71,6 +71,10 @@ struct WholeFile {
project_overrides: Vec<PartialConfig>,
}
fn load_from_file(path: &str) -> Result<WholeFile> {
todo!();
}
fn read_conf_str(text: &str) -> Result<WholeFile> {
let mut whole = WholeFile::default();
@@ -242,4 +246,30 @@ mod tests {
let conf = read_conf_str(fx_sample_cfg);
assert!(conf.is_err());
}
#[test]
// File exists and has valid configuration.
fn load_from_file_ok() -> Result<()> {
let conf = load_from_file("test_data/sample_config.toml")?;
assert_eq!(conf, gen_expected_struct());
Ok(())
}
#[test]
// File does not exist.
fn load_from_file_missing() -> Result<()> {
let res = load_from_file("test_data/doesnt_exist.toml");
let err = res.unwrap_err();
assert_eq!(err, todo!("Need a new config::Error variant to indicate a missing config file"));
Ok(())
}
#[test]
// File exists but has garbage inside.
// TODO: This bumps against the same semantic issue as the todo note on
// the 'read_config_string_empty' test
fn load_from_file_bad() {
let res = load_from_file("test_data/missing_all_table.toml");
assert!(res.is_err());
}
}

View File

@@ -0,0 +1,5 @@
# There must be an "[all]" table or the loader will reject the config file.
["/some/other/path"]
gitea_url = "fake-url"