diff --git a/src/options/config.rs b/src/options/config.rs index fc4f301..91fa4a6 100644 --- a/src/options/config.rs +++ b/src/options/config.rs @@ -6,6 +6,8 @@ use std::process::Command; use std::sync::Arc; use std::{env, fs}; use tokio::time::Duration; +use std::fs::OpenOptions; +use std::io::{self, Write}; const CONFIG_PATH: &str = "settings.json"; @@ -249,9 +251,27 @@ fn config_comparing(local: &Processes, remote: &Processes) -> ConfigActuality { fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomError> { match serde_json::to_string_pretty(&config) { - Ok(st) => match fs::write(config_file, st) { - Ok(_) => Ok(()), - Err(_) => Err(CustomError::Fatal), + // Ok(st) => match fs::write(config_file, st) { + // Ok(_) => Ok(()), + // Err(_) => Err(CustomError::Fatal), + // }, + Ok(st) => { + let file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .open(config_file); + match file { + Ok(fs) => { + let mut writer = fs; + match writeln!(writer, "{}", st) { + Ok(_) => Ok(()), + Err(_) => Err(CustomError::Fatal), + } + }, + Err(_) => return Err(CustomError::Fatal) + } }, Err(_) => Err(CustomError::Fatal), } @@ -263,3 +283,60 @@ fn parse_extern_config(json_string: &str) -> Option { } None } + + + + +// unit tests +#[cfg(test)] + mod config_unittests { + use super::*; + #[test] + fn parsing_valid_conf() { + assert!(load_processes("tests/examples/settings.json").is_some()); + } + #[test] + fn parsing_invalid_conf() { + assert!(load_processes("tests/examples/invalid_config.json").is_none()); + } + #[test] + fn configuration_comparing() { + // old one (kinda local) + let a = Processes { + date_of_creation: String::from("1"), + config_server: String::new(), + processes: vec![], + }; + // new one (kinda remote) + let b = Processes { + date_of_creation: String::from("2"), + config_server: String::new(), + processes: vec![], + }; + + assert_eq!(config_comparing(&a, &b), ConfigActuality::Remote); + } + #[test] + fn get_actual_config_mechanism() { + assert!(get_actual_config().is_some()) + } + #[test] + fn save_config() { + let a = Processes { + date_of_creation: String::from("1"), + config_server: String::new(), + processes: vec![], + }; + assert!(save_new_config(&a, "tests/examples/save-conf.json").is_ok()); + } + + #[test] + fn save_to_zero_file() { + let a = Processes { + date_of_creation: String::from("1"), + config_server: String::new(), + processes: vec![], + }; + assert!(save_new_config(&a, "tests/examples/none.json").is_ok()); + } + } \ No newline at end of file diff --git a/src/options/structs.rs b/src/options/structs.rs index 6838703..7e56077 100644 --- a/src/options/structs.rs +++ b/src/options/structs.rs @@ -3,7 +3,9 @@ use serde::{Deserialize, Serialize}; /// # an Error enum (next will be deleted and replaced) pub enum CustomError { Fatal, + } +#[derive(Debug, PartialEq)] pub enum ConfigActuality { Local, Remote, diff --git a/tests/examples/invalid_config.json b/tests/examples/invalid_config.json new file mode 100644 index 0000000..3a2228e --- /dev/null +++ b/tests/examples/invalid_config.json @@ -0,0 +1,33 @@ +{ + "configServer" : "localhost", + "processes": [ + { + "name": "temp-process", + "path": "/home/user/monitor/runner-rs/temp-process", + "dependencies": { + "files": [ + { + "filename": "dep-file", + "src": "/home/user/monitor/runner-rs/tests/examples/", + "triggers": { + "onDelete": "stop", + "onChange": "stay" + } + } + ], + "services": [ + { + "hostname": "ya.ru", + "port": 443, + "triggers": { + "wait": 10, + "delay": 2, + "onLost": "hold" + } + } + ] + } + } + ] + } + \ No newline at end of file diff --git a/tests/examples/none.json b/tests/examples/none.json new file mode 100644 index 0000000..e6c4897 --- /dev/null +++ b/tests/examples/none.json @@ -0,0 +1,5 @@ +{ + "dateOfCreation": "1", + "configServer": "", + "processes": [] +} diff --git a/tests/examples/save-conf.json b/tests/examples/save-conf.json new file mode 100644 index 0000000..e6c4897 --- /dev/null +++ b/tests/examples/save-conf.json @@ -0,0 +1,5 @@ +{ + "dateOfCreation": "1", + "configServer": "", + "processes": [] +}