From 912a7283fd320b40245f94c99581005b2fa15614 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Thu, 17 Jul 2025 13:59:53 -0500 Subject: [PATCH] Externalize the test table I'm beginning work on the file reading functions, so I need some files to read in my tests. I'll also need the WholeFile struct to compare against. The input string has been moved out into a file and put back into the test fixture with `include_str!()`. The WholeFile construction has been moved to a util function so I can reuse it in another test. --- src/config.rs | 89 ++++++++++++++++-------------------- test_data/sample_config.toml | 15 ++++++ 2 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 test_data/sample_config.toml diff --git a/src/config.rs b/src/config.rs index c8ace99..a61ac4b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -145,6 +145,43 @@ mod tests { use super::*; + // Util for generating a reference struct + fn gen_expected_struct() -> WholeFile { + WholeFile { + all: PartialConfig { + project_path: None, + gitea_url: Some(String::from("http://localhost:3000")), + owner: None, + repo: None, + token: Some(String::from("fake-token")) + }, + project_overrides: vec![ + PartialConfig { + project_path: Some(String::from("/home/robert/projects/gt-tool")), + gitea_url: None, + owner: Some(String::from("robert")), + repo: Some(String::from("gt-tool")), + token: None, + }, + PartialConfig { + project_path: Some(String::from("/home/robert/projects/rcalc")), + gitea_url: None, + owner: Some(String::from("jamis")), + repo: Some(String::from("rcalc")), + token: None, + }, + PartialConfig { + project_path: Some(String::from("/home/robert/projects/rcalc-builders")), + gitea_url: None, + owner: Some(String::from("jamis")), + repo: Some(String::from("rcalc")), + token: None, + }, + + ], + } + } + #[test] fn read_single_prop() -> Result<()> { let fx_input_str = "owner = \"dingus\""; @@ -185,56 +222,8 @@ mod tests { #[test] fn read_config_string_ok() -> Result<()> { - let fx_sample_config_string = " -[all] -gitea_url = \"http://localhost:3000\" -token = \"fake-token\" - -[\"/home/robert/projects/gt-tool\"] -owner = \"robert\" -repo = \"gt-tool\" - -[\"/home/robert/projects/rcalc\"] -owner = \"jamis\" -repo = \"rcalc\" - -[\"/home/robert/projects/rcalc-builders\"] -owner = \"jamis\" -repo = \"rcalc\" -"; - let fx_expected_struct = WholeFile { - all: PartialConfig { - project_path: None, - gitea_url: Some(String::from("http://localhost:3000")), - owner: None, - repo: None, - token: Some(String::from("fake-token")) - }, - project_overrides: vec![ - PartialConfig { - project_path: Some(String::from("/home/robert/projects/gt-tool")), - gitea_url: None, - owner: Some(String::from("robert")), - repo: Some(String::from("gt-tool")), - token: None, - }, - PartialConfig { - project_path: Some(String::from("/home/robert/projects/rcalc")), - gitea_url: None, - owner: Some(String::from("jamis")), - repo: Some(String::from("rcalc")), - token: None, - }, - PartialConfig { - project_path: Some(String::from("/home/robert/projects/rcalc-builders")), - gitea_url: None, - owner: Some(String::from("jamis")), - repo: Some(String::from("rcalc")), - token: None, - }, - - ], - }; + let fx_sample_config_string = include_str!("../test_data/sample_config.toml"); + let fx_expected_struct = gen_expected_struct(); let conf = read_conf_str(fx_sample_config_string)?; println!(" ->> Test conf: {:?}", conf); println!(" ->> Ref conf: {:?}", fx_expected_struct); diff --git a/test_data/sample_config.toml b/test_data/sample_config.toml new file mode 100644 index 0000000..6c0f1d1 --- /dev/null +++ b/test_data/sample_config.toml @@ -0,0 +1,15 @@ +[all] +gitea_url = "http://localhost:3000" +token = "fake-token" + +["/home/robert/projects/gt-tool"] +owner = "robert" +repo = "gt-tool" + +["/home/robert/projects/rcalc"] +owner = "jamis" +repo = "rcalc" + +["/home/robert/projects/rcalc-builders"] +owner = "jamis" +repo = "rcalc"