Reexport `serde_json` crate from run-make-support to give it access to `run-make` tests

This commit is contained in:
Guillaume Gomez 2024-08-16 15:09:04 +02:00
parent a2a4f2bcb5
commit 6b1637c477
3 changed files with 38 additions and 39 deletions

View File

@ -3149,6 +3149,7 @@ dependencies = [
"gimli 0.31.0", "gimli 0.31.0",
"object 0.36.2", "object 0.36.2",
"regex", "regex",
"serde_json",
"similar", "similar",
"wasmparser 0.214.0", "wasmparser 0.214.0",
] ]

View File

@ -11,3 +11,4 @@ wasmparser = { version = "0.214", default-features = false, features = ["std"] }
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace
gimli = "0.31.0" gimli = "0.31.0"
build_helper = { path = "../build_helper" } build_helper = { path = "../build_helper" }
serde_json = "1.0"

View File

@ -1,57 +1,54 @@
use run_make_support::path_helpers::read_dir_entries_recursive; // This test ensures that all items from `foo` are correctly generated into the `redirect-map.json`
// file with `--generate-redirect-map` rustdoc option.
use std::path::Path;
use run_make_support::rfs::read_to_string; use run_make_support::rfs::read_to_string;
use run_make_support::{jzon, rustdoc}; use run_make_support::{path, rustdoc, serde_json};
fn main() { fn main() {
let out_dir = "out"; let out_dir = "out";
let crate_name = "foo";
rustdoc() rustdoc()
.input("foo.rs") .input("foo.rs")
.crate_name(crate_name)
.arg("-Zunstable-options") .arg("-Zunstable-options")
.arg("--generate-redirect-map") .arg("--generate-redirect-map")
.out_dir(&out_dir) .out_dir(&out_dir)
.run(); .run();
let mut found_file = false; let generated = read_to_string(path(out_dir).join(crate_name).join("redirect-map.json"));
read_dir_entries_recursive(&out_dir, |path| { let expected = read_to_string("expected.json");
if !found_file let generated: serde_json::Value =
&& path.is_file() serde_json::from_str(&generated).expect("failed to parse JSON");
&& path.file_name().map(|name| name == "redirect-map.json").unwrap_or(false) let expected: serde_json::Value =
{ serde_json::from_str(&expected).expect("failed to parse JSON");
found_file = true; let expected = expected.as_object().unwrap();
let generated = read_to_string(path);
let expected = read_to_string("expected.json");
let generated = jzon::parse(&generated).expect("failed to parse JSON");
let expected = jzon::parse(&expected).expect("failed to parse JSON");
let mut differences = Vec::new(); let mut differences = Vec::new();
for (key, expected_value) in expected.entries() { for (key, expected_value) in expected.iter() {
match generated.get(key) { match generated.get(key) {
Some(value) => { Some(value) => {
if expected_value != value { if expected_value != value {
differences.push(format!("values for key `{key}` don't match")); differences.push(format!(
} "values for key `{key}` don't match: `{expected_value:?}` != `{value:?}`"
} ));
None => differences.push(format!("missing key `{key}`")),
} }
} }
for (key, data) in generated.entries() { None => differences.push(format!("missing key `{key}`")),
if !expected.has_key(key) {
differences
.push(format!("Extra data not expected: key: `{key}`, data: `{data}`"));
}
}
if !differences.is_empty() {
eprintln!("Found differences in JSON files:");
for diff in differences {
eprintln!("=> {diff}");
}
std::process::exit(1);
}
} }
}); }
for (key, data) in generated.as_object().unwrap().iter() {
if !expected.contains_key(key) {
differences.push(format!("Extra data not expected: key: `{key}`, data: `{data}`"));
}
}
if !found_file { if !differences.is_empty() {
panic!("`redirect-map.json` file was not found"); eprintln!("Found differences in JSON files:");
for diff in differences {
eprintln!("=> {diff}");
}
panic!("Found differences in JSON files");
} }
} }