run_make_support: use fs internally, but rfs to tests

This commit is contained in:
许杰友 Jieyou Xu (Joe) 2024-07-17 13:31:27 +00:00
parent a00b860bfc
commit 1f1bf4c71b
5 changed files with 31 additions and 22 deletions

View File

@ -3,7 +3,7 @@
use std::panic; use std::panic;
use std::path::Path; use std::path::Path;
use crate::fs as rfs; use crate::fs;
/// Assert that `actual` is equal to `expected`. /// Assert that `actual` is equal to `expected`.
#[track_caller] #[track_caller]
@ -50,14 +50,14 @@ pub fn assert_not_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N)
/// Assert that all files in `dir1` exist and have the same content in `dir2` /// Assert that all files in `dir1` exist and have the same content in `dir2`
pub fn assert_dirs_are_equal(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) { pub fn assert_dirs_are_equal(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
let dir2 = dir2.as_ref(); let dir2 = dir2.as_ref();
rfs::read_dir_entries(dir1, |entry_path| { fs::read_dir_entries(dir1, |entry_path| {
let entry_name = entry_path.file_name().unwrap(); let entry_name = entry_path.file_name().unwrap();
if entry_path.is_dir() { if entry_path.is_dir() {
assert_dirs_are_equal(&entry_path, &dir2.join(entry_name)); assert_dirs_are_equal(&entry_path, &dir2.join(entry_name));
} else { } else {
let path2 = dir2.join(entry_name); let path2 = dir2.join(entry_name);
let file1 = rfs::read(&entry_path); let file1 = fs::read(&entry_path);
let file2 = rfs::read(&path2); let file2 = fs::read(&path2);
// We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display. // 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 // Why not using String? Because there might be minified files or even potentially

View File

@ -1,10 +1,12 @@
use regex::Regex;
use similar::TextDiff;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use crate::fs as rfs; use regex::Regex;
use similar::TextDiff;
use build_helper::drop_bomb::DropBomb; use build_helper::drop_bomb::DropBomb;
use crate::fs;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -43,7 +45,7 @@ impl Diff {
/// Specify the expected output for the diff from a file. /// Specify the expected output for the diff from a file.
pub fn expected_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { pub fn expected_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
let path = path.as_ref(); let path = path.as_ref();
let content = rfs::read_to_string(path); let content = fs::read_to_string(path);
let name = path.to_string_lossy().to_string(); let name = path.to_string_lossy().to_string();
self.expected_file = Some(path.into()); self.expected_file = Some(path.into());
@ -62,7 +64,7 @@ impl Diff {
/// Specify the actual output for the diff from a file. /// Specify the actual output for the diff from a file.
pub fn actual_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { pub fn actual_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
let path = path.as_ref(); let path = path.as_ref();
let content = rfs::read_to_string(path); let content = fs::read_to_string(path);
let name = path.to_string_lossy().to_string(); let name = path.to_string_lossy().to_string();
self.actual = Some(content); self.actual = Some(content);
@ -116,7 +118,7 @@ impl Diff {
if let Some(ref expected_file) = self.expected_file { if let Some(ref expected_file) = self.expected_file {
if std::env::var("RUSTC_BLESS_TEST").is_ok() { if std::env::var("RUSTC_BLESS_TEST").is_ok() {
println!("Blessing `{}`", expected_file.display()); println!("Blessing `{}`", expected_file.display());
rfs::write(expected_file, actual); fs::write(expected_file, actual);
return; return;
} }
} }
@ -138,7 +140,7 @@ impl Diff {
if let Some(ref expected_file) = self.expected_file { if let Some(ref expected_file) = self.expected_file {
if std::env::var("RUSTC_BLESS_TEST").is_ok() { if std::env::var("RUSTC_BLESS_TEST").is_ok() {
println!("Blessing `{}`", expected_file.display()); println!("Blessing `{}`", expected_file.display());
rfs::write(expected_file, actual); fs::write(expected_file, actual);
return; return;
} }
} }

View File

@ -12,13 +12,20 @@ pub mod assertion_helpers;
pub mod diff; pub mod diff;
pub mod env; pub mod env;
pub mod external_deps; pub mod external_deps;
pub mod fs;
pub mod path_helpers; pub mod path_helpers;
pub mod run; pub mod run;
pub mod scoped_run; pub mod scoped_run;
pub mod string; pub mod string;
pub mod targets; pub mod targets;
mod fs;
/// [`std::fs`] wrappers and assorted filesystem-related helpers. Public to tests as `rfs` to not be
/// confused with [`std::fs`].
pub mod rfs {
pub use crate::fs::*;
}
// Re-exports of third-party library crates. // Re-exports of third-party library crates.
pub use bstr; pub use bstr;
pub use gimli; pub use gimli;

View File

@ -2,7 +2,7 @@
use std::path::Path; use std::path::Path;
use crate::fs as rfs; use crate::fs;
use crate::path_helpers::cwd; use crate::path_helpers::cwd;
use crate::targets::is_windows; use crate::targets::is_windows;
@ -34,16 +34,16 @@ where
); );
panic!("`test_while_readonly` on directory detected while on Windows."); panic!("`test_while_readonly` on directory detected while on Windows.");
} }
let metadata = rfs::metadata(&path); let metadata = fs::metadata(&path);
let original_perms = metadata.permissions(); let original_perms = metadata.permissions();
let mut new_perms = original_perms.clone(); let mut new_perms = original_perms.clone();
new_perms.set_readonly(true); new_perms.set_readonly(true);
rfs::set_permissions(&path, new_perms); fs::set_permissions(&path, new_perms);
let success = std::panic::catch_unwind(closure); let success = std::panic::catch_unwind(closure);
rfs::set_permissions(&path, original_perms); fs::set_permissions(&path, original_perms);
success.unwrap(); success.unwrap();
} }
@ -60,10 +60,10 @@ where
pub fn run_in_tmpdir<F: FnOnce()>(callback: F) { pub fn run_in_tmpdir<F: FnOnce()>(callback: F) {
let original_dir = cwd(); let original_dir = cwd();
let tmpdir = original_dir.join("../temporary-directory"); let tmpdir = original_dir.join("../temporary-directory");
rfs::copy_dir_all(".", &tmpdir); fs::copy_dir_all(".", &tmpdir);
std::env::set_current_dir(&tmpdir).unwrap(); std::env::set_current_dir(&tmpdir).unwrap();
callback(); callback();
std::env::set_current_dir(original_dir).unwrap(); std::env::set_current_dir(original_dir).unwrap();
rfs::remove_dir_all(tmpdir); fs::remove_dir_all(tmpdir);
} }

View File

@ -1,6 +1,6 @@
use std::path::Path; use std::path::Path;
use crate::fs as rfs; use crate::fs;
use crate::path_helpers::{cwd, has_extension, shallow_find_files}; use crate::path_helpers::{cwd, has_extension, shallow_find_files};
/// Gathers all files in the current working directory that have the extension `ext`, and counts /// Gathers all files in the current working directory that have the extension `ext`, and counts
@ -10,7 +10,7 @@ pub fn count_regex_matches_in_files_with_extension(re: &regex::Regex, ext: &str)
let mut count = 0; let mut count = 0;
for file in fetched_files { for file in fetched_files {
let content = rfs::read_to_string(file); let content = fs::read_to_string(file);
count += content.lines().filter(|line| re.is_match(&line)).count(); count += content.lines().filter(|line| re.is_match(&line)).count();
} }
@ -22,7 +22,7 @@ pub fn count_regex_matches_in_files_with_extension(re: &regex::Regex, ext: &str)
/// that it contains `expected`. /// that it contains `expected`.
#[track_caller] #[track_caller]
pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) { pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) {
let buffer = rfs::read(path.as_ref()); let buffer = fs::read(path.as_ref());
let expected = expected.as_ref(); let expected = expected.as_ref();
if !String::from_utf8_lossy(&buffer).contains(expected) { if !String::from_utf8_lossy(&buffer).contains(expected) {
eprintln!("=== FILE CONTENTS (LOSSY) ==="); eprintln!("=== FILE CONTENTS (LOSSY) ===");
@ -38,7 +38,7 @@ pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S
/// that it does not contain `expected`. /// that it does not contain `expected`.
#[track_caller] #[track_caller]
pub fn invalid_utf8_not_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) { pub fn invalid_utf8_not_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) {
let buffer = rfs::read(path.as_ref()); let buffer = fs::read(path.as_ref());
let expected = expected.as_ref(); let expected = expected.as_ref();
if String::from_utf8_lossy(&buffer).contains(expected) { if String::from_utf8_lossy(&buffer).contains(expected) {
eprintln!("=== FILE CONTENTS (LOSSY) ==="); eprintln!("=== FILE CONTENTS (LOSSY) ===");