diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 13:54:38 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 13:54:38 +0000 |
commit | 8c1ab65c0f548d20b7f177bdb736daaf603340e1 (patch) | |
tree | df55b7e75bf43f2bf500845b105afe3ac3a5157e /tools/wasi-headers/tests | |
parent | Initial commit. (diff) | |
download | wasi-libc-upstream/0.0_git20221206.8b7148f.tar.xz wasi-libc-upstream/0.0_git20221206.8b7148f.zip |
Adding upstream version 0.0~git20221206.8b7148f.upstream/0.0_git20221206.8b7148f
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/wasi-headers/tests')
-rw-r--r-- | tools/wasi-headers/tests/verify.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/tools/wasi-headers/tests/verify.rs b/tools/wasi-headers/tests/verify.rs new file mode 100644 index 0000000..faae763 --- /dev/null +++ b/tools/wasi-headers/tests/verify.rs @@ -0,0 +1,95 @@ +use std::fs; +use std::path::Path; + +#[test] +fn assert_same_as_src() { + let actual_header = + fs::read_to_string(wasi_headers::libc_wasi_api_header()).expect("read libc wasi/api.h"); + let actual_source = + fs::read_to_string(wasi_headers::libc_wasi_api_source()).expect("read libc wasi/api.h"); + let witx_files = wasi_headers::snapshot_witx_files().expect("parse snapshot witx files"); + let expected = wasi_headers::generate(&witx_files).expect("header generation"); + if actual_header == expected.header && actual_source == expected.source { + return; + } + + if actual_header != expected.header { + diff( + &wasi_headers::libc_wasi_api_header(), + &actual_header, + &expected.header, + ); + } + if actual_header != expected.header { + diff( + &wasi_headers::libc_wasi_api_source(), + &actual_header, + &expected.header, + ); + } +} + +fn diff(path: &Path, actual: &str, expected: &str) { + eprintln!("The following diff was found between the generated copy and the"); + eprintln!("source copy of {:?} in the tree:", path); + eprintln!(); + + let mut expected_line = 1; + let mut actual_line = 1; + let mut separated = false; + let mut any_lines = false; + for diff in diff::lines(&expected, &actual) { + match diff { + diff::Result::Left(l) => { + eprintln!("line {}: -{}", expected_line, l); + expected_line += 1; + separated = false; + any_lines = true; + } + diff::Result::Both(_, _) => { + expected_line += 1; + actual_line += 1; + if !separated { + eprintln!("..."); + separated = true; + } + } + diff::Result::Right(r) => { + eprintln!("line {}: +{}", actual_line, r); + actual_line += 1; + separated = false; + any_lines = true; + } + } + } + + if !any_lines { + eprintln!(); + eprintln!( + "Somehow there was a diff with no lines differing. Lengths: {} and {}.", + expected.len(), + actual.len() + ); + for (index, (a, b)) in actual.chars().zip(expected.chars()).enumerate() { + if a != b { + eprintln!("char difference at index {}: '{}' != '{}'", index, a, b); + } + } + for (index, (a, b)) in actual.bytes().zip(expected.bytes()).enumerate() { + if a != b { + eprintln!("byte difference at index {}: b'{}' != b'{}'", index, a, b); + } + } + eprintln!(); + eprintln!("actual: {}", actual); + eprintln!(); + eprintln!("expected: {}", expected); + } + + eprintln!(); + eprintln!( + "To regenerate the files, run `cd tools/wasi-headers && cargo run -- generate-libc`." + ); + eprintln!(); + panic!(); +} |