Skip to content

Commit

Permalink
Add json checker
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRega committed Oct 4, 2023
1 parent c613c72 commit fc4115b
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ opener = "0.6"
anyhow = "1.0"
json_diff = {git = "https://github.com/ChrisRega/json-diff"}




[dev-dependencies]
env_logger = "0.10"
tracing = {version = "0.1", default-features = false}
Expand Down
10 changes: 6 additions & 4 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ pub(crate) fn compare_files<P: AsRef<Path>>(
.collect();

if !filtered_diff.is_empty() {
let all_diffs_log = filtered_diff.iter().map(|(_, v)| v).join("\n");
diff.push_detail(DiffDetail::External {
stdout: all_diffs_log,
stderr: String::new(),
let all_diffs_log = filtered_diff
.iter()
.map(|(d, v)| format!("{d}: {v}"))
.join("\n");
diff.push_detail(DiffDetail::Json {
differences: all_diffs_log,
});

diff.error();
Expand Down
60 changes: 56 additions & 4 deletions src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Difference {
return false;
}
self.is_error |= other.is_error;
self.detail.extend(other.detail.into_iter());
self.detail.extend(other.detail);
true
}
}
Expand All @@ -146,6 +146,9 @@ pub enum DiffDetail {
stdout: String,
stderr: String,
},
Json {
differences: String,
},
Properties(MetaDataPropertyDiff),
Error(String),
}
Expand Down Expand Up @@ -265,7 +268,7 @@ pub(crate) fn write_csv_detail(

let columns: Vec<CSVReportColumn> = n
.into_iter()
.zip(a.into_iter())
.zip(a)
.enumerate()
.map(|(col, (n, a))| {
let current_pos = Position { col, row };
Expand Down Expand Up @@ -500,6 +503,34 @@ pub fn write_external_detail(
Ok(Some(detail_path))
}

pub fn write_json_detail(
nominal: impl AsRef<Path>,
actual: impl AsRef<Path>,
differences: &str,
report_dir: impl AsRef<Path>,
) -> Result<Option<DetailPath>, Error> {
let detail_path = create_detail_folder(report_dir.as_ref())?;
let detail_file = detail_path.path.join(template::DETAIL_FILENAME);

let mut tera = Tera::default();
tera.add_raw_template(
&detail_file.to_string_lossy(),
template::PLAIN_JSON_DETAIL_TEMPLATE,
)?;

let mut ctx = Context::new();
ctx.insert("actual", &actual.as_ref().to_string_lossy());
ctx.insert("nominal", &nominal.as_ref().to_string_lossy());
ctx.insert("differences", differences);

let file = fat_io_wrap_std(&detail_file, &File::create)?;
debug!("detail html {:?} created", &detail_file);

tera.render_to(&detail_file.to_string_lossy(), &ctx, file)?;

Ok(Some(detail_path))
}

fn create_error_detail(
nominal: impl AsRef<Path>,
actual: impl AsRef<Path>,
Expand Down Expand Up @@ -628,7 +659,7 @@ pub(crate) fn create_html(
&file.nominal_file,
&file.actual_file,
&diffs,
&config,
config,
&sub_folder,
)
.unwrap_or_else(|e| log_detail_html_creation_error(&e))
Expand Down Expand Up @@ -703,7 +734,7 @@ pub(crate) fn create_html(
)
.unwrap_or_else(|e| log_detail_html_creation_error(&e))
}
ComparisonMode::External(_) | ComparisonMode::Json(_) => {
ComparisonMode::External(_) => {
if let Some((stdout, stderr)) = file
.detail
.iter()
Expand All @@ -725,6 +756,27 @@ pub(crate) fn create_html(
None
}
}
ComparisonMode::Json(_) => {
if let Some(differences) = file
.detail
.iter()
.filter_map(|r| match r {
DiffDetail::Json { differences } => Some(differences),
_ => None,
})
.next()
{
write_json_detail(
&file.nominal_file,
&file.actual_file,
differences,
&sub_folder,
)
.unwrap_or_else(|e| log_detail_html_creation_error(&e))
} else {
None
}
}
ComparisonMode::FileProperties(_) => None, //we need only additional columns in the index.html
ComparisonMode::Hash(_) => {
let diffs: Vec<String> = file
Expand Down
78 changes: 78 additions & 0 deletions src/report/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,84 @@ pub const PLAIN_EXTERNAL_DETAIL_TEMPLATE: &str = r###"
</table>
</body>
</html>
"###;

pub const PLAIN_JSON_DETAIL_TEMPLATE: &str = r###"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Results</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.12.1/datatables.min.css"/>
<style>
h3 {
background-color:black;
color:white;
padding:10px;
margin:10px 0;
cursor:pointer;
}
table {
table-layout: fixed;
}
table.dataTable tr.odd {
background-color: #dddddd;
}
.has_diff {
color: #0d6efdf0;
}
.has_error {
color:red;
}
#compare th {
text-align:left;
background-color: #cccccc;
padding:10px;
}
#compare td {
white-space: pre;
}
#compare td:first-child {
border-right: 1px solid black;
}
table#compare {
border:1px solid grey;
}
</style>
</head>
<body>
<h3>Compare Result of {{ actual }} and {{ nominal }}</h3>
<table id="compare">
<thead>
<tr>
<th>Differences</th>
</tr>
</thead>
<tbody>
<tr>
<td class="has_error">
{{ differences }}
</td>
</tr>
</tbody>
</table>
</body>
</html>
"###;
14 changes: 14 additions & 0 deletions tests/integ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,17 @@ fn images_test() {
)
.unwrap());
}

#[test]
fn json_test() {
let report_dir =
tempfile::tempdir().expect("Could not generate temporary directory for report");

assert!(!compare_folders(
"tests/integ/data/json/expected/",
"tests/integ/data/json/actual/",
"tests/integ/json.yml",
report_dir
)
.unwrap());
}
1 change: 1 addition & 0 deletions tests/integ/data/json/actual/guy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Takumi", "age":18, "car": "Panda Trueno"}
1 change: 1 addition & 0 deletions tests/integ/data/json/expected/guy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Keisuke", "age":21, "car": "RX7", "brothers": ["Ryosuke"]}
7 changes: 7 additions & 0 deletions tests/integ/json.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rules:
- name: "Compare JSON files"
pattern_include:
- "**/*.json"
Json:
ignore_keys:
- ""

0 comments on commit fc4115b

Please sign in to comment.