Skip to content

Commit

Permalink
Merge pull request #45 from ChrisRega/0.5.3-json-sorting
Browse files Browse the repository at this point in the history
0.5.3 json sorting
  • Loading branch information
rohdealx authored Feb 19, 2024
2 parents 4873436 + 1bf1b63 commit 452c00e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 25 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ serde_yaml = "0.9"
schemars = "0.8"
schemars_derive = "0.8"
thiserror = "1.0"
regex = "1.8"
regex = "1.10"
image = "0.24"
image-compare = "0.3.1"
image-compare = "0.3"
tracing = "0.1"
tracing-subscriber = "0.3"
serde_json = "1.0"
glob = "0.3"
test-log = {version="0.2", features=["trace"]}
strsim = "0.10"
itertools = "0.11"
strsim = "0.11"
itertools = "0.12"
tera = "1.19"
sha2 = "0.10"
data-encoding = "2.4"
data-encoding = "2.5"
permutation = "0.4"
pdf-extract = "0.7"
vg_errortools = "0.1"
Expand All @@ -47,10 +47,10 @@ tempfile = "3.8"
fs_extra = "1.3"
opener = "0.6"
anyhow = "1.0"
json_diff_ng = {version = "0.3"}
json_diff_ng = {version = "0.4"}


[dev-dependencies]
env_logger = "0.10"
env_logger = "0.11"
tracing = {version = "0.1", default-features = false}
tracing-subscriber = {version = "0.3", default-features = false, features = ["env-filter", "fmt"]}
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,41 @@ rules:
- "ignore_this_key(s?)"
```
### Use HavoCompare in your unit-tests
1. Add havocompare to your dev-dependencies:
```toml
[dev-dependencies]
havocompare = "0.5"
```
2. Use it in a unit-test like
```rust
#[test]
// works in 0.5 series
fn integ_test_dirs() {
let result_dir = process_whatever_test_data();
// just write the usual yaml file
let result = havocompare::compare_folders("../tests/data/nominal/integ_test/case", &result_dir, "../tests/data/config.yaml", "../tests/out_report").unwrap;
assert!(result);
}
#[test]
// works starting with 0.5.3 only
fn integ_test_file() {
let result_file = process_generate_image();
// see docs for all options
let compare_mode = ComparisonMode::Image(ImageCompareConfig{threshold: 0.97});
let result = havocompare::compare_files("../tests/data/nominal.png", &result_file, &compare_mode).unwrap;
assert!(result);
}
```

## Changelog

### 0.5.3
- Add option to sort json arrays (including deep sorting)
- Make single file comparison function to public api
- Update dependencies, fix broken pdf import regarding whitespaces


### 0.5.2
- Preserve white spaces in CSV and PDF report instead of being collapsed by HTML
- Add + and - symbols to the report index
Expand Down
18 changes: 11 additions & 7 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use tracing::error;
pub struct JsonConfig {
#[serde(default)]
ignore_keys: Vec<String>,
#[serde(default)]
sort_arrays: bool,
}
impl JsonConfig {
pub(crate) fn get_ignore_list(&self) -> Result<Vec<Regex>, regex::Error> {
Expand All @@ -31,7 +33,7 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
let actual = vg_errortools::fat_io_wrap_std(&actual, &std::fs::read_to_string)?;
let ignores = config.get_ignore_list()?;

let json_diff = json_diff::process::compare_jsons(&nominal, &actual);
let json_diff = json_diff::process::compare_jsons(&nominal, &actual, config.sort_arrays);
let json_diff = match json_diff {
Ok(diff) => diff,
Err(e) => {
Expand Down Expand Up @@ -113,6 +115,7 @@ mod test {
fn no_filter() {
let cfg = JsonConfig {
ignore_keys: vec![],
sort_arrays: false,
};
let result = compare_files(
"tests/integ/data/json/expected/guy.json",
Expand All @@ -128,12 +131,12 @@ mod test {
} = result.detail.first().unwrap()
{
let differences = trim_split(differences);
assert!(differences.contains(&"car -> { \"RX7\" != \"Panda Trueno\" }"));
assert!(differences.contains(&"age -> { 21 != 18 }"));
assert!(differences.contains(&"name -> { \"Keisuke\" != \"Takumi\" }"));
assert!(differences.contains(&"car->{\"RX7\"!=\"Panda Trueno\"}"));
assert!(differences.contains(&"age->{21!=18}"));
assert!(differences.contains(&"name->{\"Keisuke\"!=\"Takumi\"}"));
assert_eq!(differences.len(), 3);

assert_eq!(left.as_str(), " brothers");
assert_eq!(left.as_str(), "brothers");
assert!(right.is_empty());
assert!(root_mismatch.is_none());
} else {
Expand All @@ -145,6 +148,7 @@ mod test {
fn filter_works() {
let cfg = JsonConfig {
ignore_keys: vec!["name".to_string(), "brother(s?)".to_string()],
sort_arrays: false,
};
let result = compare_files(
"tests/integ/data/json/expected/guy.json",
Expand All @@ -160,8 +164,8 @@ mod test {
} = result.detail.first().unwrap()
{
let differences = trim_split(differences);
assert!(differences.contains(&"car -> { \"RX7\" != \"Panda Trueno\" }"));
assert!(differences.contains(&"age -> { 21 != 18 }"));
assert!(differences.contains(&"car->{\"RX7\"!=\"Panda Trueno\"}"));
assert!(differences.contains(&"age->{21!=18}"));
assert_eq!(differences.len(), 2);
assert!(right.is_empty());
assert!(left.is_empty());
Expand Down
22 changes: 12 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ 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) -> Difference {
/// Use this to compare a single file against another file using a given rule
pub fn compare_files(
nominal: impl AsRef<Path>,
actual: impl AsRef<Path>,
comparison_mode: &ComparisonMode,
) -> 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");
Expand All @@ -176,7 +181,7 @@ fn process_file(nominal: impl AsRef<Path>, actual: impl AsRef<Path>, rule: &Rule
info!("File: {file_name_nominal} | {file_name_actual}");

let compare_result: Result<Difference, Box<dyn std::error::Error>> = {
match &rule.file_type {
match comparison_mode {
ComparisonMode::CSV(conf) => {
csv::compare_paths(nominal.as_ref(), actual.as_ref(), conf).map_err(|e| e.into())
}
Expand Down Expand Up @@ -283,7 +288,7 @@ fn process_rule(
.into_iter()
.zip(actual_cleaned_paths)
.for_each(|(n, a)| {
let compare_result = process_file(n, a, rule);
let compare_result = compare_files(n, a, &rule.file_type);
all_okay &= !compare_result.is_error;
compare_results.push(compare_result);
});
Expand Down Expand Up @@ -314,13 +319,10 @@ pub fn compare_folders_cfg(

let rule_name = rule.name.as_str();

let result = match okay {
Ok(result) => result,
Err(e) => {
println!("Error occurred during rule-processing for rule {rule_name}: {e}");
false
}
};
let result = okay.unwrap_or_else(|e| {
println!("Error occurred during rule-processing for rule {rule_name}: {e}");
false
});
rule_results.push(report::RuleDifferences {
rule,
diffs: compare_results,
Expand Down
2 changes: 1 addition & 1 deletion src/pdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod test {
"tests/pdf/expected.pdf",
&HTMLCompareConfig {
threshold: 1.0,
ignore_lines: Some(vec!["/ w o r k s p a c e /".to_owned()]),
ignore_lines: Some(vec!["/workspace/".to_owned()]),
},
)
.unwrap();
Expand Down

0 comments on commit 452c00e

Please sign in to comment.