Skip to content

Commit

Permalink
Merge pull request #53 from ChrisRega/main
Browse files Browse the repository at this point in the history
Update json-diff to 0.6 seriesx
  • Loading branch information
rohdealx authored Jun 5, 2024
2 parents df731a7 + e006050 commit 9fbcb77
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 40 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.6.0"
version = "0.6.1"
edition = "2021"
license = "MIT"
authors = ["Volume Graphics GmbH"]
Expand Down Expand Up @@ -35,7 +35,7 @@ serde_json = "1.0"
glob = "0.3"
test-log = { version = "0.2", features = ["trace"] }
strsim = "0.11"
itertools = "0.12"
itertools = "0.13"
tera = "1.19"
sha2 = "0.10"
data-encoding = "2.6"
Expand All @@ -48,7 +48,7 @@ tempfile = "3.10"
fs_extra = "1.3"
opener = "0.7"
anyhow = "1.0"
json_diff_ng = { version = "0.5" }
json_diff_ng = { version = "0.6", default-features = false }

[dev-dependencies]
env_logger = "0.11"
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ rules:
#### JSON comparison
Compares JSON files for different keys in both files and mismatches in values.
`ignore_keys` is a list of regexes that are matched against the individual key names, the key value pair is excluded from the comparison if a regex matches.
`ignore_keys` is a list of regexes that are matched against the individual key names, the key value pair is excluded
from the comparison if a regex matches.
The values are not affected by this.

```yaml
Expand Down Expand Up @@ -305,6 +306,10 @@ rules:

## Changelog

### 0.6.1

- Add new version of json-diff, featuring a better API and better filtering options

### 0.6.0

- Add new options for image compare module (a lot of options!)
Expand Down
31 changes: 16 additions & 15 deletions src/json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::Path;

use itertools::Itertools;
use json_diff_ng::DiffType;
use regex::Regex;
use schemars_derive::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -31,17 +32,16 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
let mut diff = Difference::new_for_file(&nominal, &actual);
let compared_file_name = nominal.as_ref().to_string_lossy().into_owned();

let nominal = vg_errortools::fat_io_wrap_std(&nominal, &std::fs::read_to_string)?;
let actual = vg_errortools::fat_io_wrap_std(&actual, &std::fs::read_to_string)?;
let nominal: String = vg_errortools::fat_io_wrap_std(&nominal, &std::fs::read_to_string)?;
let actual: String = 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, config.sort_arrays, &ignores);
let json_diff = json_diff_ng::compare_strs(&nominal, &actual, config.sort_arrays, &ignores);
let json_diff = match json_diff {
Ok(diff) => diff,
Err(e) => {
let error_message =
format!("JSON deserialization failed for {compared_file_name} (error: {e})");
format!("JSON comparison failed for {compared_file_name} (error: {e})");
error!("{}", error_message);
diff.push_detail(DiffDetail::Error(error_message));
diff.error();
Expand All @@ -57,7 +57,7 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
let left = filtered_diff
.iter()
.filter_map(|(k, v)| {
if matches!(k, json_diff::enums::DiffType::LeftExtra) {
if matches!(k, DiffType::LeftExtra) {
Some(v.to_string())
} else {
None
Expand All @@ -67,7 +67,7 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
let right = filtered_diff
.iter()
.filter_map(|(k, v)| {
if matches!(k, json_diff::enums::DiffType::RightExtra) {
if matches!(k, DiffType::RightExtra) {
Some(v.to_string())
} else {
None
Expand All @@ -77,7 +77,7 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
let differences = filtered_diff
.iter()
.filter_map(|(k, v)| {
if matches!(k, json_diff::enums::DiffType::Mismatch) {
if matches!(k, DiffType::Mismatch) {
Some(v.to_string())
} else {
None
Expand All @@ -86,7 +86,7 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
.join("\n");
let root_mismatch = filtered_diff
.iter()
.find(|(k, _v)| matches!(k, json_diff::enums::DiffType::RootMismatch))
.find(|(k, _v)| matches!(k, DiffType::RootMismatch))
.map(|(_, v)| v.to_string());

diff.push_detail(DiffDetail::Json {
Expand Down Expand Up @@ -130,12 +130,13 @@ 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 Down Expand Up @@ -163,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
46 changes: 25 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,40 @@
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]

use std::borrow::Cow;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Path, PathBuf};

use schemars::schema_for;
use schemars_derive::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tracing::{debug, error, info, span};
use vg_errortools::{fat_io_wrap_std, FatIOError};

pub use csv::CSVCompareConfig;
pub use hash::HashConfig;

use crate::external::ExternalConfig;
pub use crate::html::HTMLCompareConfig;
pub use crate::image::ImageCompareConfig;
pub use crate::json::JsonConfig;
use crate::properties::PropertiesConfig;
use crate::report::{DiffDetail, Difference};

/// comparison module for csv comparison
pub mod csv;

pub use csv::CSVCompareConfig;
use std::borrow::Cow;
mod external;
mod hash;
pub use hash::HashConfig;
mod html;
mod image;
pub use crate::image::ImageCompareConfig;
mod external;
mod pdf;
mod properties;
mod report;

mod json;
pub use crate::json::JsonConfig;

use crate::external::ExternalConfig;
pub use crate::html::HTMLCompareConfig;
use crate::properties::PropertiesConfig;
use crate::report::{DiffDetail, Difference};
use schemars::schema_for;
use schemars_derive::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Path, PathBuf};
use thiserror::Error;
use tracing::{debug, error, info, span};
use vg_errortools::{fat_io_wrap_std, FatIOError};

#[derive(Error, Debug)]
/// Top-Level Error class for all errors that can happen during havocompare-running
Expand Down Expand Up @@ -378,9 +381,10 @@ pub fn validate_config(config_file: impl AsRef<Path>) -> bool {

#[cfg(test)]
mod tests {
use super::*;
use crate::image::{CompareMode, RGBCompareMode};

use super::*;

#[test]
fn folder_not_found_is_false() {
let rule = Rule {
Expand Down

0 comments on commit 9fbcb77

Please sign in to comment.