Skip to content

Commit

Permalink
First rough draft of json reporting instead of html
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Regali committed Jul 25, 2023
1 parent a8e7376 commit f530270
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 182 deletions.
20 changes: 9 additions & 11 deletions src/csv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,23 +489,21 @@ pub(crate) fn compare_paths(
nominal: impl AsRef<Path>,
actual: impl AsRef<Path>,
config: &CSVCompareConfig,
) -> Result<report::FileCompareResult, Error> {
) -> Result<report::Difference, Error> {
let nominal_file = fat_io_wrap_std(nominal.as_ref(), &File::open)?;
let actual_file = fat_io_wrap_std(actual.as_ref(), &File::open)?;

let (nominal_table, actual_table, results) =
get_diffs_readers(&nominal_file, &actual_file, config)?;
let (_, _, results) = get_diffs_readers(&nominal_file, &actual_file, config)?;
results.iter().for_each(|error| {
error!("{}", &error);
});

Ok(report::write_csv_detail(
nominal_table,
actual_table,
nominal.as_ref(),
actual.as_ref(),
results.as_slice(),
)?)
let is_error = !results.is_empty();
let result = report::Difference {
file_path: nominal.as_ref().to_path_buf(),
is_error,
detail: results.into_iter().map(report::DiffDetail::CSV).collect(),
};
Ok(result)
}

#[cfg(test)]
Expand Down
33 changes: 11 additions & 22 deletions src/external.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::report::FileCompareResult;
use crate::{report, Error};
use crate::report::{DiffDetail, Difference};
use crate::Error;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::path::Path;
Expand All @@ -17,46 +17,35 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
nominal: P,
actual: P,
config: &ExternalConfig,
) -> Result<FileCompareResult, Error> {
) -> Result<Difference, Error> {
let mut diff = Difference::new_for_file(&nominal);
let compared_file_name = nominal.as_ref().to_string_lossy().into_owned();
let mut is_error = false;
let output = std::process::Command::new(&config.executable)
.args(&config.extra_params)
.arg(nominal.as_ref())
.arg(actual.as_ref())
.output();
let (stdout_string, stderr_string, error_message) = if let Ok(output) = output {
if let Ok(output) = output {
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
info!("External stdout: {}", stdout.as_str());
info!("External stderr: {}", stderr.as_str());
let error_message = if !output.status.success() {
if !output.status.success() {
let message = format!("External checker denied file {}", &compared_file_name);
error!("{}", &message);
is_error = true;
message
} else {
"".to_owned()
diff.push_detail(DiffDetail::External { stdout, stderr });
diff.error();
};

(stdout, stderr, error_message)
} else {
let error_message = format!(
"External checker execution failed for file {}",
&compared_file_name
);
error!("{}", error_message);
is_error = true;
("".to_owned(), "".to_owned(), error_message)
diff.push_detail(DiffDetail::Error(error_message));
diff.error();
};
Ok(report::write_external_detail(
nominal,
actual,
is_error,
&stdout_string,
&stderr_string,
&error_message,
)?)
Ok(diff)
}
#[cfg(test)]
mod tests {
Expand Down
27 changes: 11 additions & 16 deletions src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{report, Deserialize, Serialize};
use data_encoding::HEXLOWER;

use crate::report::{DiffDetail, Difference};
use schemars_derive::JsonSchema;
use std::fs::File;
use std::io::Read;
Expand Down Expand Up @@ -62,29 +63,23 @@ pub fn compare_files<P: AsRef<Path>>(
nominal_path: P,
actual_path: P,
config: &HashConfig,
) -> Result<report::FileCompareResult, Error> {
) -> Result<Difference, Error> {
let act = config
.function
.hash_file(fat_io_wrap_std(actual_path.as_ref(), &File::open)?)?;
let nom = config
.function
.hash_file(fat_io_wrap_std(nominal_path.as_ref(), &File::open)?)?;

let diff = if act != nom {
vec![format!(
"Nominal file's hash is '{}' actual is '{}'",
HEXLOWER.encode(&nom),
HEXLOWER.encode(&act)
)]
} else {
vec![]
};

Ok(report::write_html_detail(
nominal_path,
actual_path,
diff.as_slice(),
)?)
let mut difference = Difference::new_for_file(nominal_path);
if act != nom {
difference.push_detail(DiffDetail::Hash {
actual: HEXLOWER.encode(&act),
nominal: HEXLOWER.encode(&nom),
});
difference.error();
}
Ok(difference)
}

#[cfg(test)]
Expand Down
17 changes: 6 additions & 11 deletions src/html.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::report;
use crate::report::{DiffDetail, Difference};
use regex::Regex;
use schemars_derive::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -58,14 +59,12 @@ pub fn compare_files<P: AsRef<Path>>(
nominal_path: P,
actual_path: P,
config: &HTMLCompareConfig,
) -> Result<report::FileCompareResult, Error> {
) -> Result<Difference, Error> {
let actual = BufReader::new(fat_io_wrap_std(actual_path.as_ref(), &File::open)?);
let nominal = BufReader::new(fat_io_wrap_std(nominal_path.as_ref(), &File::open)?);

let mut diffs: Vec<String> = Vec::new();

let exclusion_list = config.get_ignore_list()?;

let mut difference = Difference::new_for_file(nominal_path);
actual
.lines()
.enumerate()
Expand All @@ -84,16 +83,12 @@ pub fn compare_files<P: AsRef<Path>>(
);

error!("{}" , &error);

diffs.push(error);
difference.push_detail(DiffDetail::Text {score: distance, line: l});
difference.error();
}
});

Ok(report::write_html_detail(
nominal_path.as_ref(),
actual_path.as_ref(),
&diffs,
)?)
Ok(difference)
}

#[cfg(test)]
Expand Down
21 changes: 9 additions & 12 deletions src/image.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::report::DiffDetail;
use crate::{get_file_name, report};
use schemars_derive::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -41,8 +42,7 @@ pub fn compare_paths<P: AsRef<Path>>(
nominal_path: P,
actual_path: P,
config: &ImageCompareConfig,
) -> Result<report::FileCompareResult, Error> {
let mut diffs: Vec<String> = Vec::new();
) -> Result<report::Difference, Error> {
let nominal = image::open(nominal_path.as_ref())?.into_rgba8();
let actual = image::open(actual_path.as_ref())?.into_rgba8();

Expand All @@ -53,6 +53,7 @@ pub fn compare_paths<P: AsRef<Path>>(
nominal_path.as_ref()
)))?;
let out_path = (nominal_file_name + "diff_image.png").to_string();
let mut result_diff = report::Difference::new_for_file(&nominal_path);

if result.score < config.threshold {
let color_map = result.image.to_color_map();
Expand All @@ -64,18 +65,14 @@ pub fn compare_paths<P: AsRef<Path>>(
config.threshold,
result.score
);

error!("{}", &error_message);

diffs.push(error_message);
diffs.push(out_path);
result_diff.push_detail(DiffDetail::Image {
diff_image: out_path,
score: result.score,
});
result_diff.error();
}

Ok(report::write_image_detail(
nominal_path.as_ref(),
actual_path.as_ref(),
&diffs,
)?)
Ok(result_diff)
}

#[cfg(test)]
Expand Down
53 changes: 25 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod report;
use crate::external::ExternalConfig;
pub use crate::html::HTMLCompareConfig;
use crate::properties::PropertiesConfig;
use crate::report::FileCompareResult;
use crate::report::{DiffDetail, Difference};
use schemars::schema_for;
use schemars_derive::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -161,19 +161,15 @@ fn filter_exclude(paths: Vec<PathBuf>, excludes: Vec<PathBuf>) -> Vec<PathBuf> {
.collect()
}

fn process_file(
nominal: impl AsRef<Path>,
actual: impl AsRef<Path>,
rule: &Rule,
) -> FileCompareResult {
fn process_file(nominal: impl AsRef<Path>, actual: impl AsRef<Path>, rule: &Rule) -> Difference {
let file_name_nominal = nominal.as_ref().to_string_lossy();
let file_name_actual = actual.as_ref().to_string_lossy();
let _file_span = span!(tracing::Level::INFO, "Processing");
let _file_span = _file_span.enter();

info!("File: {file_name_nominal} | {file_name_actual}");

let compare_result: Result<FileCompareResult, Box<dyn std::error::Error>> = {
let compare_result: Result<Difference, Box<dyn std::error::Error>> = {
match &rule.file_type {
ComparisonMode::CSV(conf) => {
csv::compare_paths(nominal.as_ref(), actual.as_ref(), conf).map_err(|e| e.into())
Expand All @@ -200,22 +196,25 @@ fn process_file(
}
}
};

match compare_result {
Ok(result) => {
if result.is_error {
error!("Files didn't match");
} else {
debug!("Files matched");
}

result
}
let compare_result = match compare_result {
Ok(r) => r,
Err(e) => {
error!("Problem comparing the files");
report::write_error_detail(nominal, actual, e)
let e = e.to_string();
error!("Problem comparing the files {}", &e);
let mut d = Difference::new_for_file(nominal);
d.error();
d.push_detail(DiffDetail::Error(e));
d
}
};

if compare_result.is_error {
error!("Files didn't match");
} else {
debug!("Files matched");
}

compare_result
}

fn get_files(
Expand All @@ -232,7 +231,7 @@ fn process_rule(
nominal: impl AsRef<Path>,
actual: impl AsRef<Path>,
rule: &Rule,
compare_results: &mut Vec<FileCompareResult>,
compare_results: &mut Vec<Difference>,
) -> Result<bool, Error> {
let _file_span = span!(tracing::Level::INFO, "Rule");
let _file_span = _file_span.enter();
Expand Down Expand Up @@ -276,9 +275,7 @@ fn process_rule(
.zip(actual_cleaned_paths.into_iter())
.for_each(|(n, a)| {
let compare_result = process_file(n, a, rule);

all_okay &= !compare_result.is_error;

compare_results.push(compare_result);
});

Expand All @@ -292,13 +289,13 @@ pub fn compare_folders_cfg(
config_struct: ConfigurationFile,
report_path: impl AsRef<Path>,
) -> Result<bool, Error> {
let mut rule_results: Vec<report::RuleResult> = Vec::new();
let mut rule_results: Vec<report::RuleDifferences> = Vec::new();

let results: Vec<bool> = config_struct
.rules
.into_iter()
.map(|rule| {
let mut compare_results: Vec<FileCompareResult> = Vec::new();
let mut compare_results: Vec<Difference> = Vec::new();
let okay = process_rule(
nominal.as_ref(),
actual.as_ref(),
Expand All @@ -315,17 +312,17 @@ pub fn compare_folders_cfg(
false
}
};
rule_results.push(report::RuleResult {
rule_results.push(report::RuleDifferences {
rule,
compare_results,
diffs: compare_results,
});

result
})
.collect();

let all_okay = results.iter().all(|result| *result);
report::create(&rule_results, report_path)?;
report::create_json(&rule_results, report_path)?;
Ok(all_okay)
}

Expand Down
Loading

0 comments on commit f530270

Please sign in to comment.