Skip to content

Commit 1f1bf4c

Browse files
committed
run_make_support: use fs internally, but rfs to tests
1 parent a00b860 commit 1f1bf4c

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

src/tools/run-make-support/src/assertion_helpers.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::panic;
44
use std::path::Path;
55

6-
use crate::fs as rfs;
6+
use crate::fs;
77

88
/// Assert that `actual` is equal to `expected`.
99
#[track_caller]
@@ -50,14 +50,14 @@ pub fn assert_not_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N)
5050
/// Assert that all files in `dir1` exist and have the same content in `dir2`
5151
pub fn assert_dirs_are_equal(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
5252
let dir2 = dir2.as_ref();
53-
rfs::read_dir_entries(dir1, |entry_path| {
53+
fs::read_dir_entries(dir1, |entry_path| {
5454
let entry_name = entry_path.file_name().unwrap();
5555
if entry_path.is_dir() {
5656
assert_dirs_are_equal(&entry_path, &dir2.join(entry_name));
5757
} else {
5858
let path2 = dir2.join(entry_name);
59-
let file1 = rfs::read(&entry_path);
60-
let file2 = rfs::read(&path2);
59+
let file1 = fs::read(&entry_path);
60+
let file2 = fs::read(&path2);
6161

6262
// We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display.
6363
// Why not using String? Because there might be minified files or even potentially

src/tools/run-make-support/src/diff/mod.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use std::path::{Path, PathBuf};
2+
13
use regex::Regex;
24
use similar::TextDiff;
3-
use std::path::{Path, PathBuf};
45

5-
use crate::fs as rfs;
66
use build_helper::drop_bomb::DropBomb;
77

8+
use crate::fs;
9+
810
#[cfg(test)]
911
mod tests;
1012

@@ -43,7 +45,7 @@ impl Diff {
4345
/// Specify the expected output for the diff from a file.
4446
pub fn expected_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
4547
let path = path.as_ref();
46-
let content = rfs::read_to_string(path);
48+
let content = fs::read_to_string(path);
4749
let name = path.to_string_lossy().to_string();
4850

4951
self.expected_file = Some(path.into());
@@ -62,7 +64,7 @@ impl Diff {
6264
/// Specify the actual output for the diff from a file.
6365
pub fn actual_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
6466
let path = path.as_ref();
65-
let content = rfs::read_to_string(path);
67+
let content = fs::read_to_string(path);
6668
let name = path.to_string_lossy().to_string();
6769

6870
self.actual = Some(content);
@@ -116,7 +118,7 @@ impl Diff {
116118
if let Some(ref expected_file) = self.expected_file {
117119
if std::env::var("RUSTC_BLESS_TEST").is_ok() {
118120
println!("Blessing `{}`", expected_file.display());
119-
rfs::write(expected_file, actual);
121+
fs::write(expected_file, actual);
120122
return;
121123
}
122124
}
@@ -138,7 +140,7 @@ impl Diff {
138140
if let Some(ref expected_file) = self.expected_file {
139141
if std::env::var("RUSTC_BLESS_TEST").is_ok() {
140142
println!("Blessing `{}`", expected_file.display());
141-
rfs::write(expected_file, actual);
143+
fs::write(expected_file, actual);
142144
return;
143145
}
144146
}

src/tools/run-make-support/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@ pub mod assertion_helpers;
1212
pub mod diff;
1313
pub mod env;
1414
pub mod external_deps;
15-
pub mod fs;
1615
pub mod path_helpers;
1716
pub mod run;
1817
pub mod scoped_run;
1918
pub mod string;
2019
pub mod targets;
2120

21+
mod fs;
22+
23+
/// [`std::fs`] wrappers and assorted filesystem-related helpers. Public to tests as `rfs` to not be
24+
/// confused with [`std::fs`].
25+
pub mod rfs {
26+
pub use crate::fs::*;
27+
}
28+
2229
// Re-exports of third-party library crates.
2330
pub use bstr;
2431
pub use gimli;

src/tools/run-make-support/src/scoped_run.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::path::Path;
44

5-
use crate::fs as rfs;
5+
use crate::fs;
66
use crate::path_helpers::cwd;
77
use crate::targets::is_windows;
88

@@ -34,16 +34,16 @@ where
3434
);
3535
panic!("`test_while_readonly` on directory detected while on Windows.");
3636
}
37-
let metadata = rfs::metadata(&path);
37+
let metadata = fs::metadata(&path);
3838
let original_perms = metadata.permissions();
3939

4040
let mut new_perms = original_perms.clone();
4141
new_perms.set_readonly(true);
42-
rfs::set_permissions(&path, new_perms);
42+
fs::set_permissions(&path, new_perms);
4343

4444
let success = std::panic::catch_unwind(closure);
4545

46-
rfs::set_permissions(&path, original_perms);
46+
fs::set_permissions(&path, original_perms);
4747
success.unwrap();
4848
}
4949

@@ -60,10 +60,10 @@ where
6060
pub fn run_in_tmpdir<F: FnOnce()>(callback: F) {
6161
let original_dir = cwd();
6262
let tmpdir = original_dir.join("../temporary-directory");
63-
rfs::copy_dir_all(".", &tmpdir);
63+
fs::copy_dir_all(".", &tmpdir);
6464

6565
std::env::set_current_dir(&tmpdir).unwrap();
6666
callback();
6767
std::env::set_current_dir(original_dir).unwrap();
68-
rfs::remove_dir_all(tmpdir);
68+
fs::remove_dir_all(tmpdir);
6969
}

src/tools/run-make-support/src/string.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::Path;
22

3-
use crate::fs as rfs;
3+
use crate::fs;
44
use crate::path_helpers::{cwd, has_extension, shallow_find_files};
55

66
/// 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)
1010

1111
let mut count = 0;
1212
for file in fetched_files {
13-
let content = rfs::read_to_string(file);
13+
let content = fs::read_to_string(file);
1414
count += content.lines().filter(|line| re.is_match(&line)).count();
1515
}
1616

@@ -22,7 +22,7 @@ pub fn count_regex_matches_in_files_with_extension(re: &regex::Regex, ext: &str)
2222
/// that it contains `expected`.
2323
#[track_caller]
2424
pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) {
25-
let buffer = rfs::read(path.as_ref());
25+
let buffer = fs::read(path.as_ref());
2626
let expected = expected.as_ref();
2727
if !String::from_utf8_lossy(&buffer).contains(expected) {
2828
eprintln!("=== FILE CONTENTS (LOSSY) ===");
@@ -38,7 +38,7 @@ pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S
3838
/// that it does not contain `expected`.
3939
#[track_caller]
4040
pub fn invalid_utf8_not_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) {
41-
let buffer = rfs::read(path.as_ref());
41+
let buffer = fs::read(path.as_ref());
4242
let expected = expected.as_ref();
4343
if String::from_utf8_lossy(&buffer).contains(expected) {
4444
eprintln!("=== FILE CONTENTS (LOSSY) ===");

0 commit comments

Comments
 (0)