run_make_support: move assert_recursive_eq into assertion_helpers

This commit is contained in:
许杰友 Jieyou Xu (Joe) 2024-07-15 11:59:06 +00:00
parent 56cbfa8749
commit 88fd1df017
2 changed files with 28 additions and 27 deletions

View File

@ -1,8 +1,9 @@
//! Collection of assertions and assertion-related helpers.
use std::path::{Path, PathBuf};
use std::panic;
use std::path::{Path, PathBuf};
use crate::fs_helpers;
use crate::fs_wrapper;
use crate::path_helpers::cwd;
@ -140,3 +141,28 @@ pub fn assert_not_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N)
panic!("needle was unexpectedly found in haystack");
}
}
/// Assert that all files in `dir1` exist and have the same content in `dir2`
pub fn assert_recursive_eq(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
let dir2 = dir2.as_ref();
fs_helpers::read_dir(dir1, |entry_path| {
let entry_name = entry_path.file_name().unwrap();
if entry_path.is_dir() {
assert_recursive_eq(&entry_path, &dir2.join(entry_name));
} else {
let path2 = dir2.join(entry_name);
let file1 = fs_wrapper::read(&entry_path);
let file2 = fs_wrapper::read(&path2);
// We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display.
// Why not using String? Because there might be minified files or even potentially
// binary ones, so that would display useless output.
assert!(
file1 == file2,
"`{}` and `{}` have different content",
entry_path.display(),
path2.display(),
);
}
});
}

View File

@ -78,7 +78,7 @@ pub use fs_helpers::{copy_dir_all, create_symlink, read_dir};
pub use scoped_run::{run_in_tmpdir, test_while_readonly};
pub use assertion_helpers::{
assert_contains, assert_equals, assert_not_contains,
assert_contains, assert_equals, assert_not_contains, assert_recursive_eq,
count_regex_matches_in_files_with_extension, filename_not_in_denylist, has_extension,
has_prefix, has_suffix, invalid_utf8_contains, invalid_utf8_not_contains, not_contains,
shallow_find_files,
@ -136,28 +136,3 @@ pub fn set_host_rpath(cmd: &mut Command) {
std::env::join_paths(paths.iter()).unwrap()
});
}
/// Assert that all files in `dir1` exist and have the same content in `dir2`
pub fn assert_recursive_eq(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
let dir2 = dir2.as_ref();
read_dir(dir1, |entry_path| {
let entry_name = entry_path.file_name().unwrap();
if entry_path.is_dir() {
assert_recursive_eq(&entry_path, &dir2.join(entry_name));
} else {
let path2 = dir2.join(entry_name);
let file1 = fs_wrapper::read(&entry_path);
let file2 = fs_wrapper::read(&path2);
// We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display.
// Why not using String? Because there might be minified files or even potentially
// binary ones, so that would display useless output.
assert!(
file1 == file2,
"`{}` and `{}` have different content",
entry_path.display(),
path2.display(),
);
}
});
}