From b432ec657a07abfd932abe3ab7500294d679440c Mon Sep 17 00:00:00 2001 From: Christopher Regali Date: Tue, 27 Jun 2023 10:48:31 +0200 Subject: [PATCH] - Add unit test for format guessing - Fix clippy lints - Add --open to compare cli - Propagate comparison-errors to stdout - Bump version to 0.3.2 --- Cargo.toml | 6 ++++-- src/csv/preprocessing.rs | 27 +++++++++++++++++++++++++-- src/csv/tokenizer/guess_format.rs | 2 +- src/html.rs | 2 +- src/main.rs | 29 ++++++++++++++++++++--------- 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 461db5a..64b853c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ description = "A flexible rule-based file and folder comparison tool and crate i repository = "https://github.com/VolumeGraphics/havocompare" homepage = "https://github.com/VolumeGraphics/havocompare" documentation = "https://docs.rs/havocompare" -version = "0.3.1" +version = "0.3.2" edition = "2021" license = "MIT" authors = ["Volume Graphics GmbH"] @@ -34,7 +34,7 @@ serde_json = "1.0" glob = "0.3" test-log = {version="0.2", features=["trace"]} strsim = "0.10" -itertools = "0.10" +itertools = "0.11" tera = "1.17" sha2 = "0.10" data-encoding = "2.3" @@ -45,6 +45,8 @@ rayon = "1.6" enable-ansi-support = "0.2" tempfile = "3.3" fs_extra = "1.3" +opener = "0.6" +anyhow = "1.0" [dev-dependencies] env_logger = "0.10" diff --git a/src/csv/preprocessing.rs b/src/csv/preprocessing.rs index a3241ef..6a4ef5c 100644 --- a/src/csv/preprocessing.rs +++ b/src/csv/preprocessing.rs @@ -231,6 +231,29 @@ mod tests { .unwrap() } + fn setup_table_two(delimiters: Option) -> Table { + let delimiters = delimiters.unwrap_or_default(); + Table::from_reader( + File::open("tests/csv/data/defects_headers.csv").unwrap(), + &delimiters, + ) + .unwrap() + } + + #[test] + fn test_extract_headers_two() { + let mut table = setup_table_two(None); + extract_headers(&mut table).unwrap(); + assert_eq!( + table.columns.first().unwrap().header.as_deref().unwrap(), + "Entry" + ); + assert_eq!( + table.columns.last().unwrap().header.as_deref().unwrap(), + "Radius" + ); + } + #[test] fn test_extract_headers() { let mut table = setup_table(None); @@ -260,7 +283,7 @@ mod tests { .unwrap() .rows .iter() - .all(|v| *v == csv::Value::deleted())); + .all(|v| *v == Value::deleted())); } #[test] @@ -278,7 +301,7 @@ mod tests { .unwrap() .rows .iter() - .all(|v| *v == csv::Value::deleted())); + .all(|v| *v == Value::deleted())); } #[test] diff --git a/src/csv/tokenizer/guess_format.rs b/src/csv/tokenizer/guess_format.rs index d3aab90..a38e863 100644 --- a/src/csv/tokenizer/guess_format.rs +++ b/src/csv/tokenizer/guess_format.rs @@ -79,7 +79,7 @@ pub(crate) fn guess_format_from_reader( let bufreader = BufReader::new(&mut input); debug!("Guessing format from reader..."); - for line in bufreader.lines().filter_map(|l| l.ok()) { + for line in bufreader.lines().map_while(Result::ok) { debug!("Guessing format from line: '{}'", line.as_str()); format = guess_format_from_line(line.as_str(), format.0)?; debug!("Current format: {:?}", format); diff --git a/src/html.rs b/src/html.rs index e0e0d51..935e464 100644 --- a/src/html.rs +++ b/src/html.rs @@ -70,7 +70,7 @@ pub fn compare_files>( .lines() .enumerate() .filter_map(|l| l.1.ok().map(|a| (l.0, a))) - .zip(nominal.lines().filter_map(|l| l.ok())) + .zip(nominal.lines().map_while(Result::ok)) .filter(|((_, a), n)| exclusion_list.iter().all(|exc| !exc.is_match(a)) && exclusion_list.iter().all(|exc| !exc.is_match(n)) ) diff --git a/src/main.rs b/src/main.rs index 885008b..620d1bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ +use std::path::Path; +use anyhow::anyhow; use clap::Parser; use havocompare::{compare_folders, get_schema, validate_config}; -use tracing::Level; +use tracing::{info, Level}; use tracing_subscriber::FmtSubscriber; const DEFAULT_REPORT_FOLDER: &str = "report"; @@ -18,6 +20,9 @@ enum Commands { /// Optional: Folder to store the report to, if not set the default location will be chosen. #[arg(short, long = "report_path", default_value_t = DEFAULT_REPORT_FOLDER.to_string())] report_config: String, + /// Open the report immediately after comparison + #[arg(short, long)] + open: bool }, /// Export the JsonSchema for the config files @@ -38,7 +43,7 @@ struct Arguments { command: Commands, } -fn main() { +fn main() -> Result<(), vg_errortools::MainError> { let args = Arguments::parse(); let level = if args.verbose { Level::DEBUG @@ -59,28 +64,34 @@ fn main() { "{}", get_schema().expect("Error occurred writing json schema") ); - std::process::exit(0); + Ok(()) } Commands::Compare { compare_config, nominal, actual, report_config, + open } => { + let report_path = Path::new(report_config.as_str()); let result = - compare_folders(nominal, actual, compare_config, report_config).unwrap_or(false); + compare_folders(nominal, actual, compare_config, report_path)?; + if open { + info!("Opening report"); + opener::open(report_path.join("index.html")).expect("Could not open report!"); + } if result { - std::process::exit(0); + Ok(()) } else { - std::process::exit(1); + Err(anyhow!("Comparison failed!").into()) } } Commands::Validate { compare_config } => { if validate_config(compare_config) { - std::process::exit(0); + Ok(()) } else { - std::process::exit(1); + Err(anyhow!("Validation failed!").into()) } } - }; + } }