Skip to content

Commit 9fbcb77

Browse files
authored
Merge pull request #53 from ChrisRega/main
Update json-diff to 0.6 seriesx
2 parents df731a7 + e006050 commit 9fbcb77

File tree

4 files changed

+50
-40
lines changed

4 files changed

+50
-40
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description = "A flexible rule-based file and folder comparison tool and crate i
44
repository = "https://github.com/VolumeGraphics/havocompare"
55
homepage = "https://github.com/VolumeGraphics/havocompare"
66
documentation = "https://docs.rs/havocompare"
7-
version = "0.6.0"
7+
version = "0.6.1"
88
edition = "2021"
99
license = "MIT"
1010
authors = ["Volume Graphics GmbH"]
@@ -35,7 +35,7 @@ serde_json = "1.0"
3535
glob = "0.3"
3636
test-log = { version = "0.2", features = ["trace"] }
3737
strsim = "0.11"
38-
itertools = "0.12"
38+
itertools = "0.13"
3939
tera = "1.19"
4040
sha2 = "0.10"
4141
data-encoding = "2.6"
@@ -48,7 +48,7 @@ tempfile = "3.10"
4848
fs_extra = "1.3"
4949
opener = "0.7"
5050
anyhow = "1.0"
51-
json_diff_ng = { version = "0.5" }
51+
json_diff_ng = { version = "0.6", default-features = false }
5252

5353
[dev-dependencies]
5454
env_logger = "0.11"

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ rules:
261261
#### JSON comparison
262262
263263
Compares JSON files for different keys in both files and mismatches in values.
264-
`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.
264+
`ignore_keys` is a list of regexes that are matched against the individual key names, the key value pair is excluded
265+
from the comparison if a regex matches.
265266
The values are not affected by this.
266267

267268
```yaml
@@ -305,6 +306,10 @@ rules:
305306

306307
## Changelog
307308

309+
### 0.6.1
310+
311+
- Add new version of json-diff, featuring a better API and better filtering options
312+
308313
### 0.6.0
309314

310315
- Add new options for image compare module (a lot of options!)

src/json.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::path::Path;
22

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

34-
let nominal = vg_errortools::fat_io_wrap_std(&nominal, &std::fs::read_to_string)?;
35-
let actual = vg_errortools::fat_io_wrap_std(&actual, &std::fs::read_to_string)?;
35+
let nominal: String = vg_errortools::fat_io_wrap_std(&nominal, &std::fs::read_to_string)?;
36+
let actual: String = vg_errortools::fat_io_wrap_std(&actual, &std::fs::read_to_string)?;
3637
let ignores = config.get_ignore_list()?;
3738

38-
let json_diff =
39-
json_diff::process::compare_jsons(&nominal, &actual, config.sort_arrays, &ignores);
39+
let json_diff = json_diff_ng::compare_strs(&nominal, &actual, config.sort_arrays, &ignores);
4040
let json_diff = match json_diff {
4141
Ok(diff) => diff,
4242
Err(e) => {
4343
let error_message =
44-
format!("JSON deserialization failed for {compared_file_name} (error: {e})");
44+
format!("JSON comparison failed for {compared_file_name} (error: {e})");
4545
error!("{}", error_message);
4646
diff.push_detail(DiffDetail::Error(error_message));
4747
diff.error();
@@ -57,7 +57,7 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
5757
let left = filtered_diff
5858
.iter()
5959
.filter_map(|(k, v)| {
60-
if matches!(k, json_diff::enums::DiffType::LeftExtra) {
60+
if matches!(k, DiffType::LeftExtra) {
6161
Some(v.to_string())
6262
} else {
6363
None
@@ -67,7 +67,7 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
6767
let right = filtered_diff
6868
.iter()
6969
.filter_map(|(k, v)| {
70-
if matches!(k, json_diff::enums::DiffType::RightExtra) {
70+
if matches!(k, DiffType::RightExtra) {
7171
Some(v.to_string())
7272
} else {
7373
None
@@ -77,7 +77,7 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
7777
let differences = filtered_diff
7878
.iter()
7979
.filter_map(|(k, v)| {
80-
if matches!(k, json_diff::enums::DiffType::Mismatch) {
80+
if matches!(k, DiffType::Mismatch) {
8181
Some(v.to_string())
8282
} else {
8383
None
@@ -86,7 +86,7 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
8686
.join("\n");
8787
let root_mismatch = filtered_diff
8888
.iter()
89-
.find(|(k, _v)| matches!(k, json_diff::enums::DiffType::RootMismatch))
89+
.find(|(k, _v)| matches!(k, DiffType::RootMismatch))
9090
.map(|(_, v)| v.to_string());
9191

9292
diff.push_detail(DiffDetail::Json {
@@ -130,12 +130,13 @@ mod test {
130130
} = result.detail.first().unwrap()
131131
{
132132
let differences = trim_split(differences);
133-
assert!(differences.contains(&"car->{\"RX7\"!=\"Panda Trueno\"}"));
134-
assert!(differences.contains(&"age->{21!=18}"));
135-
assert!(differences.contains(&"name->{\"Keisuke\"!=\"Takumi\"}"));
133+
134+
assert!(differences.contains(&".car.(\"RX7\" != \"Panda Trueno\")"));
135+
assert!(differences.contains(&".age.(21 != 18)"));
136+
assert!(differences.contains(&".name.(\"Keisuke\" != \"Takumi\")"));
136137
assert_eq!(differences.len(), 3);
137138

138-
assert_eq!(left.as_str(), "brothers");
139+
assert_eq!(left.as_str(), ".brothers");
139140
assert!(right.is_empty());
140141
assert!(root_mismatch.is_none());
141142
} else {
@@ -163,8 +164,8 @@ mod test {
163164
} = result.detail.first().unwrap()
164165
{
165166
let differences = trim_split(differences);
166-
assert!(differences.contains(&"car->{\"RX7\"!=\"Panda Trueno\"}"));
167-
assert!(differences.contains(&"age->{21!=18}"));
167+
assert!(differences.contains(&".car.(\"RX7\" != \"Panda Trueno\")"));
168+
assert!(differences.contains(&".age.(21 != 18)"));
168169
assert_eq!(differences.len(), 2);
169170
assert!(right.is_empty());
170171
assert!(left.is_empty());

src/lib.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,40 @@
99
#![deny(clippy::unwrap_used)]
1010
#![deny(clippy::expect_used)]
1111

12+
use std::borrow::Cow;
13+
use std::fs::File;
14+
use std::io::{BufReader, Read};
15+
use std::path::{Path, PathBuf};
16+
17+
use schemars::schema_for;
18+
use schemars_derive::JsonSchema;
19+
use serde::{Deserialize, Serialize};
20+
use thiserror::Error;
21+
use tracing::{debug, error, info, span};
22+
use vg_errortools::{fat_io_wrap_std, FatIOError};
23+
24+
pub use csv::CSVCompareConfig;
25+
pub use hash::HashConfig;
26+
27+
use crate::external::ExternalConfig;
28+
pub use crate::html::HTMLCompareConfig;
29+
pub use crate::image::ImageCompareConfig;
30+
pub use crate::json::JsonConfig;
31+
use crate::properties::PropertiesConfig;
32+
use crate::report::{DiffDetail, Difference};
33+
1234
/// comparison module for csv comparison
1335
pub mod csv;
1436

15-
pub use csv::CSVCompareConfig;
16-
use std::borrow::Cow;
37+
mod external;
1738
mod hash;
18-
pub use hash::HashConfig;
1939
mod html;
2040
mod image;
21-
pub use crate::image::ImageCompareConfig;
22-
mod external;
2341
mod pdf;
2442
mod properties;
2543
mod report;
2644

2745
mod json;
28-
pub use crate::json::JsonConfig;
29-
30-
use crate::external::ExternalConfig;
31-
pub use crate::html::HTMLCompareConfig;
32-
use crate::properties::PropertiesConfig;
33-
use crate::report::{DiffDetail, Difference};
34-
use schemars::schema_for;
35-
use schemars_derive::JsonSchema;
36-
use serde::{Deserialize, Serialize};
37-
use std::fs::File;
38-
use std::io::{BufReader, Read};
39-
use std::path::{Path, PathBuf};
40-
use thiserror::Error;
41-
use tracing::{debug, error, info, span};
42-
use vg_errortools::{fat_io_wrap_std, FatIOError};
4346

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

379382
#[cfg(test)]
380383
mod tests {
381-
use super::*;
382384
use crate::image::{CompareMode, RGBCompareMode};
383385

386+
use super::*;
387+
384388
#[test]
385389
fn folder_not_found_is_false() {
386390
let rule = Rule {

0 commit comments

Comments
 (0)