]> git.proxmox.com Git - wasi-libc.git/blame - tools/wasi-headers/tests/verify.rs
Update to the next version of the `witx` crate (#234)
[wasi-libc.git] / tools / wasi-headers / tests / verify.rs
CommitLineData
ec86d4de 1use std::fs;
2b7e73ae 2use std::path::Path;
ec86d4de 3
cd74e1d9
DG
4#[test]
5fn assert_same_as_src() {
2b7e73ae 6 let actual_header =
ec86d4de 7 fs::read_to_string(wasi_headers::libc_wasi_api_header()).expect("read libc wasi/api.h");
2b7e73ae
AC
8 let actual_source =
9 fs::read_to_string(wasi_headers::libc_wasi_api_source()).expect("read libc wasi/api.h");
ec86d4de
PH
10 let witx_files = wasi_headers::snapshot_witx_files().expect("parse snapshot witx files");
11 let expected = wasi_headers::generate(&witx_files).expect("header generation");
2b7e73ae 12 if actual_header == expected.header && actual_source == expected.source {
cd74e1d9
DG
13 return;
14 }
15
2b7e73ae
AC
16 if actual_header != expected.header {
17 diff(
18 &wasi_headers::libc_wasi_api_header(),
19 &actual_header,
20 &expected.header,
21 );
22 }
23 if actual_header != expected.header {
24 diff(
25 &wasi_headers::libc_wasi_api_source(),
26 &actual_header,
27 &expected.header,
28 );
29 }
30}
31
32fn diff(path: &Path, actual: &str, expected: &str) {
33 eprintln!("The following diff was found between the generated copy and the");
34 eprintln!("source copy of {:?} in the tree:", path);
cd74e1d9
DG
35 eprintln!();
36
37 let mut expected_line = 1;
38 let mut actual_line = 1;
39 let mut separated = false;
40 let mut any_lines = false;
41 for diff in diff::lines(&expected, &actual) {
42 match diff {
43 diff::Result::Left(l) => {
44 eprintln!("line {}: -{}", expected_line, l);
45 expected_line += 1;
46 separated = false;
47 any_lines = true;
48 }
49 diff::Result::Both(_, _) => {
50 expected_line += 1;
51 actual_line += 1;
52 if !separated {
53 eprintln!("...");
54 separated = true;
55 }
56 }
57 diff::Result::Right(r) => {
58 eprintln!("line {}: +{}", actual_line, r);
59 actual_line += 1;
60 separated = false;
61 any_lines = true;
62 }
63 }
64 }
65
66 if !any_lines {
67 eprintln!();
68 eprintln!(
69 "Somehow there was a diff with no lines differing. Lengths: {} and {}.",
70 expected.len(),
71 actual.len()
72 );
73 for (index, (a, b)) in actual.chars().zip(expected.chars()).enumerate() {
74 if a != b {
75 eprintln!("char difference at index {}: '{}' != '{}'", index, a, b);
76 }
77 }
78 for (index, (a, b)) in actual.bytes().zip(expected.bytes()).enumerate() {
79 if a != b {
80 eprintln!("byte difference at index {}: b'{}' != b'{}'", index, a, b);
81 }
82 }
83 eprintln!();
84 eprintln!("actual: {}", actual);
85 eprintln!();
86 eprintln!("expected: {}", expected);
87 }
88
89 eprintln!();
ec86d4de
PH
90 eprintln!(
91 "To regenerate the files, run `cd tools/wasi-headers && cargo run -- generate-libc`."
92 );
cd74e1d9
DG
93 eprintln!();
94 panic!();
95}