Skip to content

Commit

Permalink
refactor html report creation and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Adinata Wijaya committed Jul 28, 2023
1 parent 989b7e7 commit 46cdfd7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
9 changes: 4 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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!");
Expand Down
54 changes: 37 additions & 17 deletions src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ pub fn write_image_detail(
diffs: &[(&f64, &String)],
report_dir: impl AsRef<Path>,
) -> Result<Option<DetailPath>, Error> {
//TODO: fix this
if diffs.is_empty() {
return Ok(None);
}
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<String> = file
.detail
Expand Down Expand Up @@ -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(),
Expand All @@ -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
}
Expand Down Expand Up @@ -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());
}
Expand Down
8 changes: 4 additions & 4 deletions src/report/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ pub const INDEX_TEMPLATE: &str = r###"
{% endif %}
</thead>
<tbody>
{% for file in rule_report.compare_results %}
{% for file in rule_report.diffs %}
<tr {% if file.is_error %} class="error" {% endif %}>
{% if rule_report.rule.FileProperties %}
<td {% if file.additional_columns.0.is_error %} class="text-error" {% endif %}>
{{ file.compared_file_name }}
{{ file.relative_file_path }}
</td>
<td {% if file.additional_columns.1.is_error %} class="text-error" {% endif %}>
{{ file.additional_columns.1.nominal_value }}
Expand All @@ -95,9 +95,9 @@ pub const INDEX_TEMPLATE: &str = r###"
{% else %}
<td>
{% if file.detail_path %}
<a href="./{{ rule_report.rule.name }}/{{ file.detail_path.path_name }}/{{ detail_filename }}">{{ file.compared_file_name }}</a>
<a href="./{{ rule_report.rule.name }}/{{ file.detail_path.path_name }}/{{ detail_filename }}">{{ file.relative_file_path }}</a>
{% else %}
{{ file.compared_file_name }}
{{ file.relative_file_path }}
{% endif %}
</td>
<td>{% if file.is_error %} <span class="text-error">&#10006;</span> {% else %} <span style="color:green;">&#10004;</span> {% endif %}</td>
Expand Down

0 comments on commit 46cdfd7

Please sign in to comment.