Migrate validate_json.py script to rust in run-make/rustdoc-map-file test

This commit is contained in:
Guillaume Gomez 2024-08-16 12:19:39 +02:00
parent 27b93da8de
commit a2a4f2bcb5
3 changed files with 48 additions and 44 deletions

View File

@ -38,6 +38,7 @@ pub mod rfs {
pub use gimli;
pub use object;
pub use regex;
pub use serde_json;
pub use wasmparser;
// Re-exports of external dependencies.

View File

@ -1,4 +1,6 @@
use run_make_support::{python_command, rustdoc};
use run_make_support::path_helpers::read_dir_entries_recursive;
use run_make_support::rfs::read_to_string;
use run_make_support::{jzon, rustdoc};
fn main() {
let out_dir = "out";
@ -8,6 +10,48 @@ fn main() {
.arg("--generate-redirect-map")
.out_dir(&out_dir)
.run();
// FIXME (GuillaumeGomez): Port the python script to Rust as well.
python_command().arg("validate_json.py").arg(&out_dir).run();
let mut found_file = false;
read_dir_entries_recursive(&out_dir, |path| {
if !found_file
&& path.is_file()
&& path.file_name().map(|name| name == "redirect-map.json").unwrap_or(false)
{
found_file = true;
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();
for (key, expected_value) in expected.entries() {
match generated.get(key) {
Some(value) => {
if expected_value != value {
differences.push(format!("values for key `{key}` don't match"));
}
}
None => differences.push(format!("missing key `{key}`")),
}
}
for (key, data) in generated.entries() {
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);
}
}
});
if !found_file {
panic!("`redirect-map.json` file was not found");
}
}

View File

@ -1,41 +0,0 @@
#!/usr/bin/env python
import os
import sys
import json
def find_redirect_map_file(folder, errors):
for root, _dirs, files in os.walk(folder):
for name in files:
if not name.endswith("redirect-map.json"):
continue
with open(os.path.join(root, name)) as f:
data = json.load(f)
with open("expected.json") as f:
expected = json.load(f)
for key in expected:
if expected[key] != data.get(key):
errors.append("Expected `{}` for key `{}`, found: `{}`".format(
expected[key], key, data.get(key)))
else:
del data[key]
for key in data:
errors.append("Extra data not expected: key: `{}`, data: `{}`".format(
key, data[key]))
return True
return False
if len(sys.argv) != 2:
print("Expected doc directory to check!")
sys.exit(1)
errors = []
if not find_redirect_map_file(sys.argv[1], errors):
print("Didn't find the map file in `{}`...".format(sys.argv[1]))
sys.exit(1)
for err in errors:
print("=> {}".format(err))
if len(errors) != 0:
sys.exit(1)