From e662a9d24dd7e759a3ad450259a50ad6b16d073d Mon Sep 17 00:00:00 2001 From: Adinata Wijaya Date: Thu, 30 Mar 2023 15:24:50 +0200 Subject: [PATCH 1/2] Add unit test for csv header comparison --- README.md | 1 + src/lib.rs | 21 +++++++++++++++++++++ src/main.rs | 15 ++++++++++++--- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4e16417..32cf124 100644 --- a/README.md +++ b/README.md @@ -228,6 +228,7 @@ rules: - Add file metadata comparison - Add external checking - Add and adjust reports for file metadata comparison and external checking +- Added config yaml validation command to check whether file can be loaded or not ### 0.2.4 - add check for row lines of both compared csv files, and throw error if they are unequal diff --git a/src/lib.rs b/src/lib.rs index b42678c..4ab720c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -346,6 +346,27 @@ pub fn get_schema() -> Result { Ok(serde_json::to_string_pretty(&schema)?) } +/// Try to load config yaml and check whether it is a valid one. Returns true if file can be loaded, otherwise false +pub fn validate_config(config_file: impl AsRef) -> bool { + let config_file = config_file.as_ref(); + let config_file_string = config_file.to_string_lossy(); + if !config_file.exists() { + error!("Could not find config file: {config_file_string}"); + return false; + } + + match ConfigurationFile::from_file(config_file) { + Ok(_) => { + info!("Config file {config_file_string} loaded successfully"); + true + } + Err(_) => { + error!("Could not load config file {config_file_string}"); + false + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index db2ed29..885008b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use clap::Parser; -use havocompare::get_schema; +use havocompare::{compare_folders, get_schema, validate_config}; use tracing::Level; use tracing_subscriber::FmtSubscriber; @@ -22,6 +22,9 @@ enum Commands { /// Export the JsonSchema for the config files Schema, + + /// Validate config yaml + Validate { compare_config: String }, } #[derive(Parser)] @@ -65,13 +68,19 @@ fn main() { report_config, } => { let result = - havocompare::compare_folders(nominal, actual, compare_config, report_config) - .unwrap_or(false); + compare_folders(nominal, actual, compare_config, report_config).unwrap_or(false); if result { std::process::exit(0); } else { std::process::exit(1); } } + Commands::Validate { compare_config } => { + if validate_config(compare_config) { + std::process::exit(0); + } else { + std::process::exit(1); + } + } }; } From 7adde47d38c668359ca3995aec9f740bcd75f7c6 Mon Sep 17 00:00:00 2001 From: Adinata Wijaya Date: Thu, 30 Mar 2023 15:34:02 +0200 Subject: [PATCH 2/2] print serde error too --- src/lib.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4ab720c..7a41f1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -360,8 +360,11 @@ pub fn validate_config(config_file: impl AsRef) -> bool { info!("Config file {config_file_string} loaded successfully"); true } - Err(_) => { - error!("Could not load config file {config_file_string}"); + Err(e) => { + error!( + "Could not load config file {config_file_string}: {}", + e.to_string() + ); false } }