From 80f42de06ac6361602f78a36e623a29c6e5ae6b6 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Sat, 14 Jan 2023 21:24:41 +0100 Subject: [PATCH] Test reading config file --- src/checker/mod.rs | 4 +-- src/config.rs | 69 ++++++++++++++++++++++++------------------ src/main.rs | 6 ++-- tests/testconfig1.toml | 5 +++ 4 files changed, 50 insertions(+), 34 deletions(-) create mode 100644 tests/testconfig1.toml diff --git a/src/checker/mod.rs b/src/checker/mod.rs index 88898e9..84ed853 100644 --- a/src/checker/mod.rs +++ b/src/checker/mod.rs @@ -9,7 +9,7 @@ use serde_json::json; pub use crate::checker::actuator::Checker as ActuatorChecker; pub use crate::checker::http::Checker as HttpChecker; pub use crate::checker::tcp::Checker as TcpChecker; -use crate::config::get_config; +use crate::config::Config; pub struct CheckResult { pub name: String, @@ -18,7 +18,7 @@ pub struct CheckResult { impl Display for CheckResult { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - let color_config = get_config().colors; + let color_config = Config::read().colors; let color = match &self.state { CheckState::Up => color_config.up, CheckState::Warn => color_config.warn, diff --git a/src/config.rs b/src/config.rs index 3f95653..a2c7301 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,7 +6,7 @@ use regex::Regex; use serde::de::{Error, Visitor}; use serde::{Deserialize, Deserializer}; -#[derive(Deserialize)] +#[derive(Default, Deserialize)] pub struct Config { #[serde(default, deserialize_with = "deserialize_duration")] pub interval: Option, @@ -16,6 +16,29 @@ pub struct Config { pub checks: Vec, } +impl Config { + fn get_config_file() -> String { + match env::args().nth(1) { + Some(config_file) => config_file, + None => format!( + "{}/.checkbar.toml", + dirs::home_dir().unwrap().to_str().unwrap_or("") + ), + } + } + + pub fn read() -> Self { + Self::read_file(Self::get_config_file().as_str()) + } + + pub fn read_file(filename: &str) -> Self { + match fs::read_to_string(filename) { + Ok(config) => toml::from_str(config.as_str()).unwrap_or_default(), + Err(_) => Config::default(), + } + } +} + #[derive(Deserialize)] pub struct ColorConfig { pub up: String, @@ -48,34 +71,6 @@ pub enum CheckType { Tcp, } -fn get_config_file() -> String { - match env::args().nth(1) { - Some(config_file) => config_file, - None => format!( - "{}/.checkbar.toml", - dirs::home_dir().unwrap().to_str().unwrap_or("") - ), - } -} - -pub fn get_config() -> Config { - match fs::read_to_string(get_config_file()) { - Ok(config) => match toml::from_str(config.as_str()) { - Ok(config) => config, - Err(_e) => Config { - interval: None, - colors: ColorConfig::default(), - checks: vec![], - }, - }, - Err(_) => Config { - interval: None, - colors: ColorConfig::default(), - checks: vec![], - }, - } -} - fn deserialize_duration<'de, D>(d: D) -> Result, D::Error> where D: Deserializer<'de>, @@ -211,6 +206,22 @@ mod tests { assert_eq!(config.colors.down, "#FF0000".to_string()); } + #[test] + fn test_should_read_and_parse_file() { + let config = Config::read_file("./tests/testconfig1.toml"); + assert_eq!(config.interval, Some(Duration::from_secs(10))); + assert_eq!(config.checks.len(), 1); + assert_eq!(config.checks[0].name, "www"); + assert_eq!(config.checks[0].url, "https://example.com"); + } + + #[test] + fn test_should_return_default_if_no_config_file() { + let config = Config::read_file("./tests/no_testconfig.toml"); + assert_eq!(config.interval, None); + assert_eq!(config.checks.len(), 0); + } + #[test] fn test_should_parse_durations() { assert_eq!(parse_duration("1m30s"), Some(Duration::from_secs(90))); diff --git a/src/main.rs b/src/main.rs index 16757e3..608a295 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use serde_json::json; use tokio::task; use crate::checker::{ActuatorChecker, CheckResult, HttpChecker, TcpChecker}; -use crate::config::{get_config, CheckConfig, CheckType}; +use crate::config::{CheckConfig, CheckType, Config}; mod checker; mod config; @@ -41,7 +41,7 @@ async fn print_states(check_configs: &[CheckConfig]) { } async fn get_click_cmd(name: String) -> Option { - for check in get_config().checks { + for check in Config::read().checks { if check.name == name { return check.click_cmd; } @@ -100,7 +100,7 @@ async fn main() { let checks = task::spawn(async { loop { - let config = get_config(); + let config = Config::read(); print_states(&config.checks).await; let interval = match config.interval { Some(value) => value, diff --git a/tests/testconfig1.toml b/tests/testconfig1.toml new file mode 100644 index 0000000..6bdb77a --- /dev/null +++ b/tests/testconfig1.toml @@ -0,0 +1,5 @@ +interval = "10s" + +[[checks]] +name = "www" +url = "https://example.com"