Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3ea5e23

Browse files
committedJun 11, 2024
Auto merge of #125736 - Oneirical:run-make-file-management, r=jieyouxu
run-make-support: add wrapper for `fs` operations Suggested by #125728. The point of this wrapper is to stop silent fails caused by forgetting to `unwrap` `fs` functions. However, functions like `fs::read` which return something and get stored in a variable should cause a failure on their own if they are not unwrapped (as the `Result` will be stored in the variable, and something will be done on that `Result` that should have been done to its contents). Is it still pertinent to wrap `fs::read_to_string`, `fs::metadata` and so on? Closes: #125728 try-job: x86_64-msvc try-job: i686-mingw
2 parents 0c96061 + c84afee commit 3ea5e23

File tree

36 files changed

+211
-108
lines changed

36 files changed

+211
-108
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use std::fs;
2+
use std::path::Path;
3+
4+
/// A wrapper around [`std::fs::remove_file`] which includes the file path in the panic message..
5+
#[track_caller]
6+
pub fn remove_file<P: AsRef<Path>>(path: P) {
7+
fs::remove_file(path.as_ref())
8+
.expect(&format!("the file in path \"{}\" could not be removed", path.as_ref().display()));
9+
}
10+
11+
/// A wrapper around [`std::fs::copy`] which includes the file path in the panic message.
12+
#[track_caller]
13+
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
14+
fs::copy(from.as_ref(), to.as_ref()).expect(&format!(
15+
"the file \"{}\" could not be copied over to \"{}\"",
16+
from.as_ref().display(),
17+
to.as_ref().display(),
18+
));
19+
}
20+
21+
/// A wrapper around [`std::fs::File::create`] which includes the file path in the panic message..
22+
#[track_caller]
23+
pub fn create_file<P: AsRef<Path>>(path: P) {
24+
fs::File::create(path.as_ref())
25+
.expect(&format!("the file in path \"{}\" could not be created", path.as_ref().display()));
26+
}
27+
28+
/// A wrapper around [`std::fs::read`] which includes the file path in the panic message..
29+
#[track_caller]
30+
pub fn read<P: AsRef<Path>>(path: P) -> Vec<u8> {
31+
fs::read(path.as_ref())
32+
.expect(&format!("the file in path \"{}\" could not be read", path.as_ref().display()))
33+
}
34+
35+
/// A wrapper around [`std::fs::read_to_string`] which includes the file path in the panic message..
36+
#[track_caller]
37+
pub fn read_to_string<P: AsRef<Path>>(path: P) -> String {
38+
fs::read_to_string(path.as_ref()).expect(&format!(
39+
"the file in path \"{}\" could not be read into a String",
40+
path.as_ref().display()
41+
))
42+
}
43+
44+
/// A wrapper around [`std::fs::read_dir`] which includes the file path in the panic message..
45+
#[track_caller]
46+
pub fn read_dir<P: AsRef<Path>>(path: P) -> fs::ReadDir {
47+
fs::read_dir(path.as_ref())
48+
.expect(&format!("the directory in path \"{}\" could not be read", path.as_ref().display()))
49+
}
50+
51+
/// A wrapper around [`std::fs::write`] which includes the file path in the panic message..
52+
#[track_caller]
53+
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) {
54+
fs::write(path.as_ref(), contents.as_ref()).expect(&format!(
55+
"the file in path \"{}\" could not be written to",
56+
path.as_ref().display()
57+
));
58+
}
59+
60+
/// A wrapper around [`std::fs::remove_dir_all`] which includes the file path in the panic message..
61+
#[track_caller]
62+
pub fn remove_dir_all<P: AsRef<Path>>(path: P) {
63+
fs::remove_dir_all(path.as_ref()).expect(&format!(
64+
"the directory in path \"{}\" could not be removed alongside all its contents",
65+
path.as_ref().display(),
66+
));
67+
}
68+
69+
/// A wrapper around [`std::fs::create_dir`] which includes the file path in the panic message..
70+
#[track_caller]
71+
pub fn create_dir<P: AsRef<Path>>(path: P) {
72+
fs::create_dir(path.as_ref()).expect(&format!(
73+
"the directory in path \"{}\" could not be created",
74+
path.as_ref().display()
75+
));
76+
}
77+
78+
/// A wrapper around [`std::fs::create_dir_all`] which includes the file path in the panic message..
79+
#[track_caller]
80+
pub fn create_dir_all<P: AsRef<Path>>(path: P) {
81+
fs::create_dir_all(path.as_ref()).expect(&format!(
82+
"the directory (and all its parents) in path \"{}\" could not be created",
83+
path.as_ref().display()
84+
));
85+
}
86+
87+
/// A wrapper around [`std::fs::metadata`] which includes the file path in the panic message..
88+
#[track_caller]
89+
pub fn metadata<P: AsRef<Path>>(path: P) -> fs::Metadata {
90+
fs::metadata(path.as_ref()).expect(&format!(
91+
"the file's metadata in path \"{}\" could not be read",
92+
path.as_ref().display()
93+
))
94+
}
95+
96+
/// A wrapper around [`std::fs::rename`] which includes the file path in the panic message.
97+
#[track_caller]
98+
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
99+
fs::rename(from.as_ref(), to.as_ref()).expect(&format!(
100+
"the file \"{}\" could not be moved over to \"{}\"",
101+
from.as_ref().display(),
102+
to.as_ref().display(),
103+
));
104+
}
105+
106+
/// A wrapper around [`std::fs::set_permissions`] which includes the file path in the panic message.
107+
#[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!(
110+
"the file's permissions in path \"{}\" could not be changed",
111+
path.as_ref().display()
112+
));
113+
}

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

+8-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod clang;
88
mod command;
99
pub mod diff;
1010
mod drop_bomb;
11+
pub mod fs_wrapper;
1112
pub mod llvm_readobj;
1213
pub mod run;
1314
pub mod rustc;
@@ -153,7 +154,7 @@ pub fn dynamic_lib_extension() -> &'static str {
153154
}
154155
}
155156

156-
/// Construct a rust library (rlib) name.
157+
/// Generate the name a rust library (rlib) would have.
157158
pub fn rust_lib_name(name: &str) -> String {
158159
format!("lib{name}.rlib")
159160
}
@@ -228,15 +229,15 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
228229
fn copy_dir_all_inner(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
229230
let dst = dst.as_ref();
230231
if !dst.is_dir() {
231-
fs::create_dir_all(&dst)?;
232+
std::fs::create_dir_all(&dst)?;
232233
}
233-
for entry in fs::read_dir(src)? {
234+
for entry in std::fs::read_dir(src)? {
234235
let entry = entry?;
235236
let ty = entry.file_type()?;
236237
if ty.is_dir() {
237238
copy_dir_all_inner(entry.path(), dst.join(entry.file_name()))?;
238239
} else {
239-
fs::copy(entry.path(), dst.join(entry.file_name()))?;
240+
std::fs::copy(entry.path(), dst.join(entry.file_name()))?;
240241
}
241242
}
242243
Ok(())
@@ -255,22 +256,15 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
255256

256257
/// Check that all files in `dir1` exist and have the same content in `dir2`. Panic otherwise.
257258
pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
258-
fn read_file(path: &Path) -> Vec<u8> {
259-
match fs::read(path) {
260-
Ok(c) => c,
261-
Err(e) => panic!("Failed to read `{}`: {:?}", path.display(), e),
262-
}
263-
}
264-
265259
let dir2 = dir2.as_ref();
266260
read_dir(dir1, |entry_path| {
267261
let entry_name = entry_path.file_name().unwrap();
268262
if entry_path.is_dir() {
269263
recursive_diff(&entry_path, &dir2.join(entry_name));
270264
} else {
271265
let path2 = dir2.join(entry_name);
272-
let file1 = read_file(&entry_path);
273-
let file2 = read_file(&path2);
266+
let file1 = fs_wrapper::read(&entry_path);
267+
let file2 = fs_wrapper::read(&path2);
274268

275269
// We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display.
276270
// Why not using String? Because there might be minified files or even potentially
@@ -286,7 +280,7 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
286280
}
287281

288282
pub fn read_dir<F: Fn(&Path)>(dir: impl AsRef<Path>, callback: F) {
289-
for entry in fs::read_dir(dir).unwrap() {
283+
for entry in fs_wrapper::read_dir(dir) {
290284
callback(&entry.unwrap().path());
291285
}
292286
}

‎tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
use std::path::PathBuf;
77

8-
use run_make_support::{aux_build, rustc, source_root};
8+
use run_make_support::{aux_build, fs_wrapper, rustc, source_root};
99

1010
fn main() {
1111
aux_build().input("stable.rs").emit("metadata").run();
1212

1313
let output =
1414
rustc().input("main.rs").emit("metadata").extern_("stable", "libstable.rmeta").run();
1515

16-
let version = std::fs::read_to_string(source_root().join("src/version")).unwrap();
16+
let version = fs_wrapper::read_to_string(source_root().join("src/version"));
1717
let expected_string = format!("stable since {}", version.trim());
1818
output.assert_stderr_contains(expected_string);
1919
}

‎tests/run-make/c-link-to-rust-dylib/rmake.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
//@ ignore-cross-compile
55

6-
use std::fs::remove_file;
7-
8-
use run_make_support::{cc, cwd, dynamic_lib_extension, is_msvc, read_dir, run, run_fail, rustc};
6+
use run_make_support::{
7+
cc, cwd, dynamic_lib_extension, fs_wrapper, is_msvc, read_dir, run, run_fail, rustc,
8+
};
99

1010
fn main() {
1111
rustc().input("foo.rs").run();
@@ -28,7 +28,7 @@ fn main() {
2828
name.ends_with(".so") || name.ends_with(".dll") || name.ends_with(".dylib")
2929
})
3030
{
31-
remove_file(path).unwrap();
31+
fs_wrapper::remove_file(path);
3232
}
3333
});
3434
run_fail("bar");

‎tests/run-make/c-link-to-rust-staticlib/rmake.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
//@ ignore-cross-compile
55

6+
use run_make_support::fs_wrapper::remove_file;
67
use run_make_support::{cc, extra_c_flags, run, rustc, static_lib_name};
78
use std::fs;
89

910
fn main() {
1011
rustc().input("foo.rs").run();
1112
cc().input("bar.c").input(static_lib_name("foo")).out_exe("bar").args(&extra_c_flags()).run();
1213
run("bar");
13-
fs::remove_file(static_lib_name("foo"));
14+
remove_file(static_lib_name("foo"));
1415
run("bar");
1516
}

‎tests/run-make/cdylib/rmake.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
//@ ignore-cross-compile
1212

13-
use std::fs::remove_file;
14-
15-
use run_make_support::{cc, cwd, dynamic_lib_name, is_msvc, run, rustc};
13+
use run_make_support::{cc, cwd, dynamic_lib_name, fs_wrapper, is_msvc, run, rustc};
1614

1715
fn main() {
1816
rustc().input("bar.rs").run();
@@ -25,7 +23,7 @@ fn main() {
2523
}
2624

2725
run("foo");
28-
remove_file(dynamic_lib_name("foo")).unwrap();
26+
fs_wrapper::remove_file(dynamic_lib_name("foo"));
2927

3028
rustc().input("foo.rs").arg("-Clto").run();
3129
run("foo");

‎tests/run-make/compiler-builtins/rmake.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#![deny(warnings)]
1616

17+
use run_make_support::fs_wrapper::{read, read_dir};
1718
use run_make_support::object::read::archive::ArchiveFile;
1819
use run_make_support::object::read::Object;
1920
use run_make_support::object::ObjectSection;
@@ -55,8 +56,7 @@ fn main() {
5556
cmd.run();
5657

5758
let rlibs_path = target_dir.join(target).join("debug").join("deps");
58-
let compiler_builtins_rlib = std::fs::read_dir(rlibs_path)
59-
.unwrap()
59+
let compiler_builtins_rlib = read_dir(rlibs_path)
6060
.find_map(|e| {
6161
let path = e.unwrap().path();
6262
let file_name = path.file_name().unwrap().to_str().unwrap();
@@ -70,7 +70,7 @@ fn main() {
7070

7171
// rlib files are archives, where the archive members each a CGU, and we also have one called
7272
// lib.rmeta which is the encoded metadata. Each of the CGUs is an object file.
73-
let data = std::fs::read(compiler_builtins_rlib).unwrap();
73+
let data = read(compiler_builtins_rlib);
7474

7575
let mut defined_symbols = HashSet::new();
7676
let mut undefined_relocations = HashSet::new();

‎tests/run-make/const-prop-lint/rmake.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
// Tests that const prop lints interrupting codegen don't leave `.o` files around.
22

3-
use std::fs;
4-
5-
use run_make_support::{cwd, rustc};
3+
use run_make_support::{cwd, fs_wrapper, rustc};
64

75
fn main() {
86
rustc().input("input.rs").run_fail().assert_exit_code(1);
97

10-
for entry in fs::read_dir(cwd()).unwrap() {
8+
for entry in fs_wrapper::read_dir(cwd()) {
119
let entry = entry.unwrap();
1210
let path = entry.path();
1311

‎tests/run-make/doctests-keep-binaries/rmake.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Check that valid binaries are persisted by running them, regardless of whether the
22
// --run or --no-run option is used.
33

4+
use run_make_support::fs_wrapper::{create_dir, remove_dir_all};
45
use run_make_support::{run, rustc, rustdoc};
5-
use std::fs::{create_dir, remove_dir_all};
66
use std::path::Path;
77

88
fn setup_test_env<F: FnOnce(&Path, &Path)>(callback: F) {
99
let out_dir = Path::new("doctests");
10-
create_dir(&out_dir).expect("failed to create doctests folder");
10+
create_dir(&out_dir);
1111
rustc().input("t.rs").crate_type("rlib").run();
1212
callback(&out_dir, Path::new("libt.rlib"));
1313
remove_dir_all(out_dir);
@@ -43,9 +43,9 @@ fn main() {
4343
check_generated_binaries();
4444
});
4545
// Behavior with --test-run-directory with relative paths.
46-
setup_test_env(|_out_dir, extern_path| {
46+
setup_test_env(|_out_dir, _extern_path| {
4747
let run_dir_path = Path::new("rundir");
48-
create_dir(&run_dir_path).expect("failed to create rundir folder");
48+
create_dir(&run_dir_path);
4949

5050
rustdoc()
5151
.input("t.rs")

‎tests/run-make/doctests-runtool/rmake.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Tests behavior of rustdoc `--runtool`.
22

3+
use run_make_support::fs_wrapper::{create_dir, remove_dir_all};
34
use run_make_support::{rustc, rustdoc};
4-
use std::fs::{create_dir, remove_dir_all};
55
use std::path::PathBuf;
66

77
fn mkdir(name: &str) -> PathBuf {
88
let dir = PathBuf::from(name);
9-
create_dir(&dir).expect("failed to create doctests folder");
9+
create_dir(&dir);
1010
dir
1111
}
1212

‎tests/run-make/emit-named-files/rmake.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::fs::create_dir;
21
use std::path::Path;
32

4-
use run_make_support::rustc;
3+
use run_make_support::{fs_wrapper, rustc};
54

65
fn emit_and_check(out_dir: &Path, out_file: &str, format: &str) {
76
let out_file = out_dir.join(out_file);
@@ -12,7 +11,7 @@ fn emit_and_check(out_dir: &Path, out_file: &str, format: &str) {
1211
fn main() {
1312
let out_dir = Path::new("emit");
1413

15-
create_dir(&out_dir).unwrap();
14+
fs_wrapper::create_dir(&out_dir);
1615

1716
emit_and_check(&out_dir, "libfoo.s", "asm");
1817
emit_and_check(&out_dir, "libfoo.bc", "llvm-bc");

‎tests/run-make/incr-prev-body-beyond-eof/rmake.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
//@ ignore-nvptx64-nvidia-cuda
1414
// FIXME: can't find crate for `std`
1515

16+
use run_make_support::fs_wrapper as fs;
1617
use run_make_support::rustc;
17-
use std::fs;
1818

1919
fn main() {
20-
// FIXME(Oneirical): Use run_make_support::fs_wrapper here.
21-
fs::create_dir("src").unwrap();
22-
fs::create_dir("incr").unwrap();
23-
fs::copy("a.rs", "src/main.rs").unwrap();
20+
fs::create_dir("src");
21+
fs::create_dir("incr");
22+
fs::copy("a.rs", "src/main.rs");
2423
rustc().incremental("incr").input("src/main.rs").run();
25-
fs::copy("b.rs", "src/main.rs").unwrap();
24+
fs::copy("b.rs", "src/main.rs");
2625
rustc().incremental("incr").input("src/main.rs").run();
2726
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.