From 1f1bf4c71b73351193fbcb0deff298b17884c9cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9D=B0=E5=8F=8B=20Jieyou=20Xu=20=28Joe=29?= Date: Wed, 17 Jul 2024 13:31:27 +0000 Subject: [PATCH] run_make_support: use `fs` internally, but `rfs` to tests --- .../run-make-support/src/assertion_helpers.rs | 8 ++++---- src/tools/run-make-support/src/diff/mod.rs | 16 +++++++++------- src/tools/run-make-support/src/lib.rs | 9 ++++++++- src/tools/run-make-support/src/scoped_run.rs | 12 ++++++------ src/tools/run-make-support/src/string.rs | 8 ++++---- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/tools/run-make-support/src/assertion_helpers.rs b/src/tools/run-make-support/src/assertion_helpers.rs index 07992afc4b96..4b5b349431db 100644 --- a/src/tools/run-make-support/src/assertion_helpers.rs +++ b/src/tools/run-make-support/src/assertion_helpers.rs @@ -3,7 +3,7 @@ use std::panic; use std::path::Path; -use crate::fs as rfs; +use crate::fs; /// Assert that `actual` is equal to `expected`. #[track_caller] @@ -50,14 +50,14 @@ pub fn assert_not_contains, N: AsRef>(haystack: H, needle: N) /// Assert that all files in `dir1` exist and have the same content in `dir2` pub fn assert_dirs_are_equal(dir1: impl AsRef, dir2: impl AsRef) { 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(); if entry_path.is_dir() { assert_dirs_are_equal(&entry_path, &dir2.join(entry_name)); } else { let path2 = dir2.join(entry_name); - let file1 = rfs::read(&entry_path); - let file2 = rfs::read(&path2); + let file1 = fs::read(&entry_path); + let file2 = fs::read(&path2); // We don't use `assert_eq!` because they are `Vec`, so not great for display. // Why not using String? Because there might be minified files or even potentially diff --git a/src/tools/run-make-support/src/diff/mod.rs b/src/tools/run-make-support/src/diff/mod.rs index 9f3548891192..ac6623316065 100644 --- a/src/tools/run-make-support/src/diff/mod.rs +++ b/src/tools/run-make-support/src/diff/mod.rs @@ -1,10 +1,12 @@ -use regex::Regex; -use similar::TextDiff; use std::path::{Path, PathBuf}; -use crate::fs as rfs; +use regex::Regex; +use similar::TextDiff; + use build_helper::drop_bomb::DropBomb; +use crate::fs; + #[cfg(test)] mod tests; @@ -43,7 +45,7 @@ impl Diff { /// Specify the expected output for the diff from a file. pub fn expected_file>(&mut self, path: P) -> &mut Self { 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(); self.expected_file = Some(path.into()); @@ -62,7 +64,7 @@ impl Diff { /// Specify the actual output for the diff from a file. pub fn actual_file>(&mut self, path: P) -> &mut Self { 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(); self.actual = Some(content); @@ -116,7 +118,7 @@ impl Diff { if let Some(ref expected_file) = self.expected_file { if std::env::var("RUSTC_BLESS_TEST").is_ok() { println!("Blessing `{}`", expected_file.display()); - rfs::write(expected_file, actual); + fs::write(expected_file, actual); return; } } @@ -138,7 +140,7 @@ impl Diff { if let Some(ref expected_file) = self.expected_file { if std::env::var("RUSTC_BLESS_TEST").is_ok() { println!("Blessing `{}`", expected_file.display()); - rfs::write(expected_file, actual); + fs::write(expected_file, actual); return; } } diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 1a5e6d092ec4..b15705e89658 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -12,13 +12,20 @@ pub mod assertion_helpers; pub mod diff; pub mod env; pub mod external_deps; -pub mod fs; pub mod path_helpers; pub mod run; pub mod scoped_run; pub mod string; 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. pub use bstr; pub use gimli; diff --git a/src/tools/run-make-support/src/scoped_run.rs b/src/tools/run-make-support/src/scoped_run.rs index 63837e98904a..074a83922c5b 100644 --- a/src/tools/run-make-support/src/scoped_run.rs +++ b/src/tools/run-make-support/src/scoped_run.rs @@ -2,7 +2,7 @@ use std::path::Path; -use crate::fs as rfs; +use crate::fs; use crate::path_helpers::cwd; use crate::targets::is_windows; @@ -34,16 +34,16 @@ where ); 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 mut new_perms = original_perms.clone(); new_perms.set_readonly(true); - rfs::set_permissions(&path, new_perms); + fs::set_permissions(&path, new_perms); let success = std::panic::catch_unwind(closure); - rfs::set_permissions(&path, original_perms); + fs::set_permissions(&path, original_perms); success.unwrap(); } @@ -60,10 +60,10 @@ where pub fn run_in_tmpdir(callback: F) { let original_dir = cwd(); let tmpdir = original_dir.join("../temporary-directory"); - rfs::copy_dir_all(".", &tmpdir); + fs::copy_dir_all(".", &tmpdir); std::env::set_current_dir(&tmpdir).unwrap(); callback(); std::env::set_current_dir(original_dir).unwrap(); - rfs::remove_dir_all(tmpdir); + fs::remove_dir_all(tmpdir); } diff --git a/src/tools/run-make-support/src/string.rs b/src/tools/run-make-support/src/string.rs index f0b1e2334d8b..a79004b8ec6b 100644 --- a/src/tools/run-make-support/src/string.rs +++ b/src/tools/run-make-support/src/string.rs @@ -1,6 +1,6 @@ use std::path::Path; -use crate::fs as rfs; +use crate::fs; 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 @@ -10,7 +10,7 @@ pub fn count_regex_matches_in_files_with_extension(re: ®ex::Regex, ext: &str) let mut count = 0; 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(); } @@ -22,7 +22,7 @@ pub fn count_regex_matches_in_files_with_extension(re: ®ex::Regex, ext: &str) /// that it contains `expected`. #[track_caller] pub fn invalid_utf8_contains, S: AsRef>(path: P, expected: S) { - let buffer = rfs::read(path.as_ref()); + let buffer = fs::read(path.as_ref()); let expected = expected.as_ref(); if !String::from_utf8_lossy(&buffer).contains(expected) { eprintln!("=== FILE CONTENTS (LOSSY) ==="); @@ -38,7 +38,7 @@ pub fn invalid_utf8_contains, S: AsRef>(path: P, expected: S /// that it does not contain `expected`. #[track_caller] pub fn invalid_utf8_not_contains, S: AsRef>(path: P, expected: S) { - let buffer = rfs::read(path.as_ref()); + let buffer = fs::read(path.as_ref()); let expected = expected.as_ref(); if String::from_utf8_lossy(&buffer).contains(expected) { eprintln!("=== FILE CONTENTS (LOSSY) ===");