Skip to content

Commit e1569fd

Browse files
committed
run_make_support: coalesce fs helpers into single fs module
There were *two* `read_dir` helpers, one being a simple `std::fs::read_dir` wrapper, the other has a different callback-based signature. We also rename the callback-based `read_dir` as `read_dir_entries`. Also don't top-level re-export most `fs::*` helpers.
1 parent 13a1751 commit e1569fd

File tree

6 files changed

+104
-117
lines changed

6 files changed

+104
-117
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
use std::panic;
44
use std::path::{Path, PathBuf};
55

6-
use crate::fs_helpers;
7-
use crate::fs_wrapper;
6+
use crate::fs as rfs;
87
use crate::path_helpers::cwd;
98

109
/// Browse the directory `path` non-recursively and return all files which respect the parameters
@@ -15,7 +14,7 @@ pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
1514
filter: F,
1615
) -> Vec<PathBuf> {
1716
let mut matching_files = Vec::new();
18-
for entry in fs_wrapper::read_dir(path) {
17+
for entry in rfs::read_dir(path) {
1918
let entry = entry.expect("failed to read directory entry.");
2019
let path = entry.path();
2120

@@ -61,19 +60,19 @@ pub fn count_regex_matches_in_files_with_extension(re: &regex::Regex, ext: &str)
6160

6261
let mut count = 0;
6362
for file in fetched_files {
64-
let content = fs_wrapper::read_to_string(file);
63+
let content = rfs::read_to_string(file);
6564
count += content.lines().filter(|line| re.is_match(&line)).count();
6665
}
6766

6867
count
6968
}
7069

7170
/// Read the contents of a file that cannot simply be read by
72-
/// [`read_to_string`][crate::fs_wrapper::read_to_string], due to invalid UTF-8 data, then assert
71+
/// [`read_to_string`][crate::fs::read_to_string], due to invalid UTF-8 data, then assert
7372
/// that it contains `expected`.
7473
#[track_caller]
7574
pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) {
76-
let buffer = fs_wrapper::read(path.as_ref());
75+
let buffer = rfs::read(path.as_ref());
7776
let expected = expected.as_ref();
7877
if !String::from_utf8_lossy(&buffer).contains(expected) {
7978
eprintln!("=== FILE CONTENTS (LOSSY) ===");
@@ -85,11 +84,11 @@ pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S
8584
}
8685

8786
/// Read the contents of a file that cannot simply be read by
88-
/// [`read_to_string`][crate::fs_wrapper::read_to_string], due to invalid UTF-8 data, then assert
87+
/// [`read_to_string`][crate::fs::read_to_string], due to invalid UTF-8 data, then assert
8988
/// that it does not contain `expected`.
9089
#[track_caller]
9190
pub fn invalid_utf8_not_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) {
92-
let buffer = fs_wrapper::read(path.as_ref());
91+
let buffer = rfs::read(path.as_ref());
9392
let expected = expected.as_ref();
9493
if String::from_utf8_lossy(&buffer).contains(expected) {
9594
eprintln!("=== FILE CONTENTS (LOSSY) ===");
@@ -145,14 +144,14 @@ pub fn assert_not_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N)
145144
/// Assert that all files in `dir1` exist and have the same content in `dir2`
146145
pub fn assert_recursive_eq(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
147146
let dir2 = dir2.as_ref();
148-
fs_helpers::read_dir(dir1, |entry_path| {
147+
rfs::read_dir_entries(dir1, |entry_path| {
149148
let entry_name = entry_path.file_name().unwrap();
150149
if entry_path.is_dir() {
151150
assert_recursive_eq(&entry_path, &dir2.join(entry_name));
152151
} else {
153152
let path2 = dir2.join(entry_name);
154-
let file1 = fs_wrapper::read(&entry_path);
155-
let file2 = fs_wrapper::read(&path2);
153+
let file1 = rfs::read(&entry_path);
154+
let file2 = rfs::read(&path2);
156155

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

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use regex::Regex;
22
use similar::TextDiff;
33
use std::path::{Path, PathBuf};
44

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

88
#[cfg(test)]
@@ -43,7 +43,7 @@ impl Diff {
4343
/// Specify the expected output for the diff from a file.
4444
pub fn expected_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
4545
let path = path.as_ref();
46-
let content = fs_wrapper::read_to_string(path);
46+
let content = rfs::read_to_string(path);
4747
let name = path.to_string_lossy().to_string();
4848

4949
self.expected_file = Some(path.into());
@@ -62,7 +62,7 @@ impl Diff {
6262
/// Specify the actual output for the diff from a file.
6363
pub fn actual_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
6464
let path = path.as_ref();
65-
let content = fs_wrapper::read_to_string(path);
65+
let content = rfs::read_to_string(path);
6666
let name = path.to_string_lossy().to_string();
6767

6868
self.actual = Some(content);
@@ -116,7 +116,7 @@ impl Diff {
116116
if let Some(ref expected_file) = self.expected_file {
117117
if std::env::var("RUSTC_BLESS_TEST").is_ok() {
118118
println!("Blessing `{}`", expected_file.display());
119-
fs_wrapper::write(expected_file, actual);
119+
rfs::write(expected_file, actual);
120120
return;
121121
}
122122
}
@@ -138,7 +138,7 @@ impl Diff {
138138
if let Some(ref expected_file) = self.expected_file {
139139
if std::env::var("RUSTC_BLESS_TEST").is_ok() {
140140
println!("Blessing `{}`", expected_file.display());
141-
fs_wrapper::write(expected_file, actual);
141+
rfs::write(expected_file, actual);
142142
return;
143143
}
144144
}

src/tools/run-make-support/src/fs_wrapper.rs renamed to src/tools/run-make-support/src/fs.rs

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,82 @@
1-
use std::fs;
1+
use std::io;
22
use std::path::Path;
33

4+
// FIXME(jieyouxu): modify create_symlink to panic on windows.
5+
6+
/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
7+
#[cfg(target_family = "windows")]
8+
pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
9+
if link.as_ref().exists() {
10+
std::fs::remove_dir(link.as_ref()).unwrap();
11+
}
12+
std::os::windows::fs::symlink_file(original.as_ref(), link.as_ref()).expect(&format!(
13+
"failed to create symlink {:?} for {:?}",
14+
link.as_ref().display(),
15+
original.as_ref().display(),
16+
));
17+
}
18+
19+
/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
20+
#[cfg(target_family = "unix")]
21+
pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
22+
if link.as_ref().exists() {
23+
std::fs::remove_dir(link.as_ref()).unwrap();
24+
}
25+
std::os::unix::fs::symlink(original.as_ref(), link.as_ref()).expect(&format!(
26+
"failed to create symlink {:?} for {:?}",
27+
link.as_ref().display(),
28+
original.as_ref().display(),
29+
));
30+
}
31+
32+
/// Copy a directory into another.
33+
pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
34+
fn copy_dir_all_inner(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
35+
let dst = dst.as_ref();
36+
if !dst.is_dir() {
37+
std::fs::create_dir_all(&dst)?;
38+
}
39+
for entry in std::fs::read_dir(src)? {
40+
let entry = entry?;
41+
let ty = entry.file_type()?;
42+
if ty.is_dir() {
43+
copy_dir_all_inner(entry.path(), dst.join(entry.file_name()))?;
44+
} else {
45+
std::fs::copy(entry.path(), dst.join(entry.file_name()))?;
46+
}
47+
}
48+
Ok(())
49+
}
50+
51+
if let Err(e) = copy_dir_all_inner(&src, &dst) {
52+
// Trying to give more context about what exactly caused the failure
53+
panic!(
54+
"failed to copy `{}` to `{}`: {:?}",
55+
src.as_ref().display(),
56+
dst.as_ref().display(),
57+
e
58+
);
59+
}
60+
}
61+
62+
/// Helper for reading entries in a given directory.
63+
pub fn read_dir_entries<P: AsRef<Path>, F: FnMut(&Path)>(dir: P, mut callback: F) {
64+
for entry in read_dir(dir) {
65+
callback(&entry.unwrap().path());
66+
}
67+
}
68+
469
/// A wrapper around [`std::fs::remove_file`] which includes the file path in the panic message.
570
#[track_caller]
671
pub fn remove_file<P: AsRef<Path>>(path: P) {
7-
fs::remove_file(path.as_ref())
72+
std::fs::remove_file(path.as_ref())
873
.expect(&format!("the file in path \"{}\" could not be removed", path.as_ref().display()));
974
}
1075

1176
/// A wrapper around [`std::fs::copy`] which includes the file path in the panic message.
1277
#[track_caller]
1378
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
14-
fs::copy(from.as_ref(), to.as_ref()).expect(&format!(
79+
std::fs::copy(from.as_ref(), to.as_ref()).expect(&format!(
1580
"the file \"{}\" could not be copied over to \"{}\"",
1681
from.as_ref().display(),
1782
to.as_ref().display(),
@@ -21,37 +86,37 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
2186
/// A wrapper around [`std::fs::File::create`] which includes the file path in the panic message.
2287
#[track_caller]
2388
pub fn create_file<P: AsRef<Path>>(path: P) {
24-
fs::File::create(path.as_ref())
89+
std::fs::File::create(path.as_ref())
2590
.expect(&format!("the file in path \"{}\" could not be created", path.as_ref().display()));
2691
}
2792

2893
/// A wrapper around [`std::fs::read`] which includes the file path in the panic message.
2994
#[track_caller]
3095
pub fn read<P: AsRef<Path>>(path: P) -> Vec<u8> {
31-
fs::read(path.as_ref())
96+
std::fs::read(path.as_ref())
3297
.expect(&format!("the file in path \"{}\" could not be read", path.as_ref().display()))
3398
}
3499

35100
/// A wrapper around [`std::fs::read_to_string`] which includes the file path in the panic message.
36101
#[track_caller]
37102
pub fn read_to_string<P: AsRef<Path>>(path: P) -> String {
38-
fs::read_to_string(path.as_ref()).expect(&format!(
103+
std::fs::read_to_string(path.as_ref()).expect(&format!(
39104
"the file in path \"{}\" could not be read into a String",
40105
path.as_ref().display()
41106
))
42107
}
43108

44109
/// A wrapper around [`std::fs::read_dir`] which includes the file path in the panic message.
45110
#[track_caller]
46-
pub fn read_dir<P: AsRef<Path>>(path: P) -> fs::ReadDir {
47-
fs::read_dir(path.as_ref())
111+
pub fn read_dir<P: AsRef<Path>>(path: P) -> std::fs::ReadDir {
112+
std::fs::read_dir(path.as_ref())
48113
.expect(&format!("the directory in path \"{}\" could not be read", path.as_ref().display()))
49114
}
50115

51116
/// A wrapper around [`std::fs::write`] which includes the file path in the panic message.
52117
#[track_caller]
53118
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) {
54-
fs::write(path.as_ref(), contents.as_ref()).expect(&format!(
119+
std::fs::write(path.as_ref(), contents.as_ref()).expect(&format!(
55120
"the file in path \"{}\" could not be written to",
56121
path.as_ref().display()
57122
));
@@ -60,7 +125,7 @@ pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) {
60125
/// A wrapper around [`std::fs::remove_dir_all`] which includes the file path in the panic message.
61126
#[track_caller]
62127
pub fn remove_dir_all<P: AsRef<Path>>(path: P) {
63-
fs::remove_dir_all(path.as_ref()).expect(&format!(
128+
std::fs::remove_dir_all(path.as_ref()).expect(&format!(
64129
"the directory in path \"{}\" could not be removed alongside all its contents",
65130
path.as_ref().display(),
66131
));
@@ -69,7 +134,7 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) {
69134
/// A wrapper around [`std::fs::create_dir`] which includes the file path in the panic message.
70135
#[track_caller]
71136
pub fn create_dir<P: AsRef<Path>>(path: P) {
72-
fs::create_dir(path.as_ref()).expect(&format!(
137+
std::fs::create_dir(path.as_ref()).expect(&format!(
73138
"the directory in path \"{}\" could not be created",
74139
path.as_ref().display()
75140
));
@@ -78,16 +143,16 @@ pub fn create_dir<P: AsRef<Path>>(path: P) {
78143
/// A wrapper around [`std::fs::create_dir_all`] which includes the file path in the panic message.
79144
#[track_caller]
80145
pub fn create_dir_all<P: AsRef<Path>>(path: P) {
81-
fs::create_dir_all(path.as_ref()).expect(&format!(
146+
std::fs::create_dir_all(path.as_ref()).expect(&format!(
82147
"the directory (and all its parents) in path \"{}\" could not be created",
83148
path.as_ref().display()
84149
));
85150
}
86151

87152
/// A wrapper around [`std::fs::metadata`] which includes the file path in the panic message.
88153
#[track_caller]
89-
pub fn metadata<P: AsRef<Path>>(path: P) -> fs::Metadata {
90-
fs::metadata(path.as_ref()).expect(&format!(
154+
pub fn metadata<P: AsRef<Path>>(path: P) -> std::fs::Metadata {
155+
std::fs::metadata(path.as_ref()).expect(&format!(
91156
"the file's metadata in path \"{}\" could not be read",
92157
path.as_ref().display()
93158
))
@@ -96,7 +161,7 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> fs::Metadata {
96161
/// A wrapper around [`std::fs::rename`] which includes the file path in the panic message.
97162
#[track_caller]
98163
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
99-
fs::rename(from.as_ref(), to.as_ref()).expect(&format!(
164+
std::fs::rename(from.as_ref(), to.as_ref()).expect(&format!(
100165
"the file \"{}\" could not be moved over to \"{}\"",
101166
from.as_ref().display(),
102167
to.as_ref().display(),
@@ -105,8 +170,8 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
105170

106171
/// A wrapper around [`std::fs::set_permissions`] which includes the file path in the panic message.
107172
#[track_caller]
108-
pub fn set_permissions<P: AsRef<Path>>(path: P, perm: fs::Permissions) {
109-
fs::set_permissions(path.as_ref(), perm).expect(&format!(
173+
pub fn set_permissions<P: AsRef<Path>>(path: P, perm: std::fs::Permissions) {
174+
std::fs::set_permissions(path.as_ref(), perm).expect(&format!(
110175
"the file's permissions in path \"{}\" could not be changed",
111176
path.as_ref().display()
112177
));

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

Lines changed: 0 additions & 71 deletions
This file was deleted.

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ pub mod assertion_helpers;
1212
pub mod diff;
1313
pub mod env;
1414
pub mod external_deps;
15-
pub mod fs_helpers;
16-
pub mod fs_wrapper;
15+
pub mod fs;
1716
pub mod path_helpers;
1817
pub mod run;
1918
pub mod scoped_run;
@@ -64,9 +63,6 @@ pub use artifact_names::{
6463
/// Path-related helpers.
6564
pub use path_helpers::{cwd, path, source_root};
6665

67-
/// Helpers for common fs operations.
68-
pub use fs_helpers::{copy_dir_all, create_symlink, read_dir};
69-
7066
/// Helpers for scoped test execution where certain properties are attempted to be maintained.
7167
pub use scoped_run::{run_in_tmpdir, test_while_readonly};
7268

0 commit comments

Comments
 (0)