From 46cdfd7bb8aaaeb0a93fc4723c26b2e8f4184e4a Mon Sep 17 00:00:00 2001 From: Adinata Wijaya Date: Fri, 28 Jul 2023 08:07:03 +0200 Subject: [PATCH] refactor html report creation and fix test --- src/main.rs | 9 ++++--- src/report/mod.rs | 54 +++++++++++++++++++++++++++++------------- src/report/template.rs | 8 +++---- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index 620d1bc..1df5139 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ -use std::path::Path; use anyhow::anyhow; use clap::Parser; use havocompare::{compare_folders, get_schema, validate_config}; +use std::path::Path; use tracing::{info, Level}; use tracing_subscriber::FmtSubscriber; @@ -22,7 +22,7 @@ enum Commands { report_config: String, /// Open the report immediately after comparison #[arg(short, long)] - open: bool + open: bool, }, /// Export the JsonSchema for the config files @@ -71,11 +71,10 @@ fn main() -> Result<(), vg_errortools::MainError> { nominal, actual, report_config, - open + open, } => { let report_path = Path::new(report_config.as_str()); - let result = - compare_folders(nominal, actual, compare_config, report_path)?; + let result = 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!"); diff --git a/src/report/mod.rs b/src/report/mod.rs index 150675f..30c47e2 100644 --- a/src/report/mod.rs +++ b/src/report/mod.rs @@ -327,7 +327,6 @@ pub fn write_image_detail( diffs: &[(&f64, &String)], report_dir: impl AsRef, ) -> Result, Error> { - //TODO: fix this if diffs.is_empty() { return Ok(None); } @@ -583,22 +582,14 @@ pub(crate) fn create_html( .diffs .iter() .map(|file| { - // let cmp_errors: Vec<&DiffDetail> = file - // .detail - // .iter() - // .filter(|r| !matches!(r, DiffDetail::Error(_))) - // .collect(); - // let diffs: Vec<&DiffDetail> = file // .detail // .iter() // .filter(|r| matches!(r, DiffDetail::Error(_))) // .collect(); - // TODO: Write cmp_errors to report // TODO: Write diffs to report -> this is crate error? - //TODO: each type needs its own detail.html and how it should be displayed in index.html (which columns etc) //TODO: use Unwrap_or_else display error in report + console let detail_path = match &rule_difference.rule.file_type { ComparisonMode::CSV(config) => { @@ -668,12 +659,10 @@ pub(crate) fn create_html( }) .collect(); - // write_pdf_detail(&file.nominal_file, &file.actual_file, &diffs, &sub_folder) - // .unwrap_or_default() write_image_detail( &file.nominal_file, &file.actual_file, - &diffs, //TODO: should actually only 1 image per file compare + &diffs, //should actually only 1 image per file compare &sub_folder, ) .unwrap_or_default() @@ -701,7 +690,7 @@ pub(crate) fn create_html( None } } - ComparisonMode::FileProperties(_) => None, //TODO: we need only additional columns in the index.html + ComparisonMode::FileProperties(_) => None, //we need only additional columns in the index.html ComparisonMode::Hash(_) => { let diffs: Vec = file .detail @@ -739,9 +728,9 @@ pub(crate) fn create_html( }) .collect(); - let result: AdditionalOverviewColumn = if let Some(_) = diffs + let result: AdditionalOverviewColumn = if diffs .iter() - .find(|d| matches!(d, MetaDataPropertyDiff::IllegalName)) + .any(|d| matches!(d, MetaDataPropertyDiff::IllegalName)) { AdditionalOverviewColumn { nominal_value: file.nominal_file.to_string_lossy().to_string(), @@ -753,7 +742,37 @@ pub(crate) fn create_html( }; additional_columns.push(result); - //TODO: + let result: AdditionalOverviewColumn = + if let Some(MetaDataPropertyDiff::Size { nominal, actual }) = diffs + .iter() + .find(|d| matches!(d, MetaDataPropertyDiff::Size { .. })) + { + AdditionalOverviewColumn { + nominal_value: format!("{nominal}"), + actual_value: format!("{actual}"), + is_error: true, + } + } else { + Default::default() + }; + additional_columns.push(result); + + let result: AdditionalOverviewColumn = + if let Some(MetaDataPropertyDiff::CreationDate { + nominal, + actual, + }) = diffs.iter().find(|d| { + matches!(d, MetaDataPropertyDiff::CreationDate { .. }) + }) { + AdditionalOverviewColumn { + nominal_value: nominal.clone(), + actual_value: actual.clone(), + is_error: true, + } + } else { + Default::default() + }; + additional_columns.push(result); additional_columns } @@ -872,7 +891,8 @@ mod tests { #[test] fn test_create_sub_folder() { - let sub_folder = create_detail_folder().unwrap(); + let report_dir = tempfile::tempdir().unwrap(); + let sub_folder = create_detail_folder(&report_dir).unwrap(); assert!(sub_folder.path.is_dir()); assert!(!sub_folder.name.is_empty()); } diff --git a/src/report/template.rs b/src/report/template.rs index 81f6889..efc1248 100644 --- a/src/report/template.rs +++ b/src/report/template.rs @@ -73,11 +73,11 @@ pub const INDEX_TEMPLATE: &str = r###" {% endif %} - {% for file in rule_report.compare_results %} + {% for file in rule_report.diffs %} {% if rule_report.rule.FileProperties %} - {{ file.compared_file_name }} + {{ file.relative_file_path }} {{ file.additional_columns.1.nominal_value }} @@ -95,9 +95,9 @@ pub const INDEX_TEMPLATE: &str = r###" {% else %} {% if file.detail_path %} - {{ file.compared_file_name }} + {{ file.relative_file_path }} {% else %} - {{ file.compared_file_name }} + {{ file.relative_file_path }} {% endif %} {% if file.is_error %} {% else %} {% endif %}