Skip to content

Commit e551456

Browse files
committed
Add safe wrappers around fs in run_make_support
1 parent 20ad958 commit e551456

File tree

30 files changed

+159
-61
lines changed

30 files changed

+159
-61
lines changed

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

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use std::fs;
2+
use std::path::Path;
3+
4+
/// A safe wrapper around `std::fs::remove_file` that prints the file path if an operation fails.
5+
pub fn remove_file<P: AsRef<Path>>(path: P) {
6+
fs::remove_file(path.as_ref()).expect(&format!(
7+
"the file in path \"{:?}\" could not be removed.",
8+
path.as_ref().display()
9+
));
10+
}
11+
12+
/// A safe wrapper around `std::fs::copy` that prints the file paths if an operation fails.
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 safe wrapper around `std::fs::File::create` that prints the file path if an operation fails.
22+
pub fn create_file<P: AsRef<Path>>(path: P) {
23+
fs::File::create(path.as_ref()).expect(&format!(
24+
"the file in path \"{:?}\" could not be created.",
25+
path.as_ref().display()
26+
));
27+
}
28+
29+
/// A safe wrapper around `std::fs::read` that prints the file path if an operation fails.
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 safe wrapper around `std::fs::read_to_string` that prints the file path if an operation fails.
36+
pub fn read_to_string<P: AsRef<Path>>(path: P) -> String {
37+
fs::read_to_string(path.as_ref()).expect(&format!(
38+
"the file in path \"{:?}\" could not be read into a String.",
39+
path.as_ref().display()
40+
))
41+
}
42+
43+
/// A safe wrapper around `std::fs::read_dir` that prints the file path if an operation fails.
44+
pub fn read_dir<P: AsRef<Path>>(path: P) -> fs::ReadDir {
45+
fs::read_dir(path.as_ref()).expect(&format!(
46+
"the directory in path \"{:?}\" could not be read.",
47+
path.as_ref().display()
48+
))
49+
}
50+
51+
/// A safe wrapper around `std::fs::write` that prints the file path if an operation fails.
52+
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) {
53+
fs::read(path.as_ref(), contents.as_ref()).expect(&format!(
54+
"the file in path \"{:?}\" could not be written to.",
55+
path.as_ref().display()
56+
));
57+
}
58+
59+
/// A safe wrapper around `std::fs::remove_dir_all` that prints the file path if an operation fails.
60+
pub fn remove_dir_all<P: AsRef<Path>>(path: P) {
61+
fs::remove_dir_all(path.as_ref()).expect(&format!(
62+
"the directory in path \"{:?}\" could not be removed alongside all its contents.",
63+
path.as_ref().display(),
64+
));
65+
}
66+
67+
/// A safe wrapper around `std::fs::create_dir` that prints the file path if an operation fails.
68+
pub fn create_dir<P: AsRef<Path>>(path: P) {
69+
fs::create_dir(path.as_ref()).expect(&format!(
70+
"the directory in path \"{:?}\" could not be created.",
71+
path.as_ref().display()
72+
));
73+
}
74+
75+
/// A safe wrapper around `std::fs::create_dir_all` that prints the file path if an operation fails.
76+
pub fn create_dir_all<P: AsRef<Path>>(path: P) {
77+
fs::create_dir_all(path.as_ref()).expect(&format!(
78+
"the directory (and all its parents) in path \"{:?}\" could not be created.",
79+
path.as_ref().display()
80+
));
81+
}
82+
83+
/// A safe wrapper around `std::fs::metadata` that prints the file path if an operation fails.
84+
pub fn metadata<P: AsRef<Path>>(path: P) -> fs::Metadata {
85+
fs::metadata(path.as_ref()).expect(&format!(
86+
"the file's metadata in path \"{:?}\" could not be read.",
87+
path.as_ref().display()
88+
))
89+
}
90+
91+
/// A safe wrapper around `std::fs::rename` that prints the file paths if an operation fails.
92+
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) {
93+
fs::rename(from.as_ref(), to.as_ref()).expect(&format!(
94+
"the file \"{:?}\" could not be moved over to \"{:?}\".",
95+
from.as_ref().display(),
96+
to.as_ref().display(),
97+
));
98+
}

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

+10-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub mod rustc;
1212
pub mod rustdoc;
1313

1414
use std::env;
15-
use std::fs;
1615
use std::io;
1716
use std::path::{Path, PathBuf};
1817
use std::process::{Command, Output};
@@ -25,6 +24,7 @@ pub use wasmparser;
2524
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
2625
pub use clang::{clang, Clang};
2726
pub use diff::{diff, Diff};
27+
pub use fs;
2828
pub use llvm_readobj::{llvm_readobj, LlvmReadobj};
2929
pub use run::{run, run_fail};
3030
pub use rustc::{aux_build, rustc, Rustc};
@@ -35,8 +35,9 @@ pub fn tmp_dir() -> PathBuf {
3535
env::var_os("TMPDIR").unwrap().into()
3636
}
3737

38-
pub fn tmp_path<P: AsRef<Path>>(path: P) -> PathBuf {
39-
tmp_dir().join(path.as_ref())
38+
/// Returns the directory TMPDIR/name.
39+
pub fn tmp_path<P: AsRef<Path>>(name: P) -> PathBuf {
40+
tmp_dir().join(name.as_ref())
4041
}
4142

4243
/// `TARGET`
@@ -218,15 +219,15 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
218219
fn copy_dir_all_inner(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
219220
let dst = dst.as_ref();
220221
if !dst.is_dir() {
221-
fs::create_dir_all(&dst)?;
222+
std::fs::create_dir_all(&dst)?;
222223
}
223-
for entry in fs::read_dir(src)? {
224+
for entry in std::fs::read_dir(src)? {
224225
let entry = entry?;
225226
let ty = entry.file_type()?;
226227
if ty.is_dir() {
227228
copy_dir_all_inner(entry.path(), dst.join(entry.file_name()))?;
228229
} else {
229-
fs::copy(entry.path(), dst.join(entry.file_name()))?;
230+
std::fs::copy(entry.path(), dst.join(entry.file_name()))?;
230231
}
231232
}
232233
Ok(())
@@ -245,15 +246,8 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
245246

246247
/// Check that all files in `dir1` exist and have the same content in `dir2`. Panic otherwise.
247248
pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
248-
fn read_file(path: &Path) -> Vec<u8> {
249-
match fs::read(path) {
250-
Ok(c) => c,
251-
Err(e) => panic!("Failed to read `{}`: {:?}", path.display(), e),
252-
}
253-
}
254-
255249
let dir2 = dir2.as_ref();
256-
for entry in fs::read_dir(dir1).unwrap() {
250+
for entry in fs::read_dir(dir1) {
257251
let entry = entry.unwrap();
258252
let entry_name = entry.file_name();
259253
let path = entry.path();
@@ -262,8 +256,8 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
262256
recursive_diff(&path, &dir2.join(entry_name));
263257
} else {
264258
let path2 = dir2.join(entry_name);
265-
let file1 = read_file(&path);
266-
let file2 = read_file(&path2);
259+
let file1 = fs::read(&path);
260+
let file2 = fs::read(&path2);
267261

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

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::remove_file;
67
use run_make_support::{cc, extra_c_flags, run, rustc, static_lib};
78
use std::fs;
89

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

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

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

1515
#![deny(warnings)]
1616

17+
use run_make_support::fs::{read, read_dir, write};
1718
use run_make_support::object;
1819
use run_make_support::object::read::archive::ArchiveFile;
1920
use run_make_support::object::read::Object;
@@ -41,8 +42,8 @@ fn main() {
4142

4243
// Set up the tiniest Cargo project: An empty no_std library. Just enough to run -Zbuild-std.
4344
let manifest_path = tmp_path("Cargo.toml");
44-
std::fs::write(&manifest_path, MANIFEST.as_bytes()).unwrap();
45-
std::fs::write(tmp_path("lib.rs"), b"#![no_std]").unwrap();
45+
write(&manifest_path, MANIFEST.as_bytes());
46+
write(tmp_path("lib.rs"), b"#![no_std]");
4647

4748
let path = std::env::var("PATH").unwrap();
4849
let rustc = std::env::var("RUSTC").unwrap();
@@ -71,8 +72,7 @@ fn main() {
7172
assert!(status.success());
7273

7374
let rlibs_path = target_dir.join(target).join("debug").join("deps");
74-
let compiler_builtins_rlib = std::fs::read_dir(rlibs_path)
75-
.unwrap()
75+
let compiler_builtins_rlib = read_dir(rlibs_path)
7676
.find_map(|e| {
7777
let path = e.unwrap().path();
7878
let file_name = path.file_name().unwrap().to_str().unwrap();
@@ -86,7 +86,7 @@ fn main() {
8686

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

9191
let mut defined_symbols = HashSet::new();
9292
let mut undefined_relocations = HashSet::new();

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

+3-3
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::{create_dir, remove_dir_all};
45
use run_make_support::{run, rustc, rustdoc, tmp_dir, tmp_path};
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 = tmp_path("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, &tmp_path("libt.rlib"));
1313
remove_dir_all(out_dir);
@@ -46,7 +46,7 @@ fn main() {
4646
setup_test_env(|_out_dir, extern_path| {
4747
let run_dir = "rundir";
4848
let run_dir_path = tmp_path("rundir");
49-
create_dir(&run_dir_path).expect("failed to create rundir folder");
49+
create_dir(&run_dir_path);
5050

5151
rustdoc()
5252
.current_dir(tmp_dir())

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

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

3+
use run_make_support::fs::{create_dir, remove_dir_all};
34
use run_make_support::{rustc, rustdoc, tmp_dir, tmp_path};
45
use std::env::current_dir;
5-
use std::fs::{create_dir, remove_dir_all};
66
use std::path::PathBuf;
77

88
fn mkdir(name: &str) -> PathBuf {
99
let dir = tmp_path(name);
10-
create_dir(&dir).expect("failed to create doctests folder");
10+
create_dir(&dir);
1111
dir
1212
}
1313

tests/run-make/issue-107495-archive-permissions/rmake.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#[cfg(unix)]
44
extern crate libc;
55

6+
use run_make_support::fs;
67
use run_make_support::{aux_build, tmp_path};
7-
use std::fs;
88
#[cfg(unix)]
99
use std::os::unix::fs::PermissionsExt;
1010
use std::path::Path;

tests/run-make/no-intermediate-extras/rmake.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::fs;
1111
fn main() {
1212
rustc().crate_type("rlib").arg("--test").input("foo.rs").run();
1313
assert!(
14+
// Do not use run-make-support's fs wrapper here - this needs to return an Error.
1415
fs::remove_file(tmp_path("foo.bc")).is_err(),
1516
"An unwanted .bc file was created by run-make/no-intermediate-extras."
1617
);

tests/run-make/non-unicode-env/rmake.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use run_make_support::fs;
12
use run_make_support::rustc;
23

34
fn main() {
@@ -7,6 +8,6 @@ fn main() {
78
let non_unicode: std::ffi::OsString = std::os::windows::ffi::OsStringExt::from_wide(&[0xD800]);
89
let output = rustc().input("non_unicode_env.rs").env("NON_UNICODE_VAR", non_unicode).run_fail();
910
let actual = std::str::from_utf8(&output.stderr).unwrap();
10-
let expected = std::fs::read_to_string("non_unicode_env.stderr").unwrap();
11+
let expected = fs::read_to_string("non_unicode_env.stderr");
1112
assert_eq!(actual, expected);
1213
}

tests/run-make/non-unicode-in-incremental-dir/rmake.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn main() {
55
let non_unicode: &std::ffi::OsStr = std::os::unix::ffi::OsStrExt::from_bytes(&[0xFF]);
66
#[cfg(windows)]
77
let non_unicode: std::ffi::OsString = std::os::windows::ffi::OsStringExt::from_wide(&[0xD800]);
8+
// Do not use run-make-support's fs wrapper, this needs special handling.
89
match std::fs::create_dir(tmp_path(&non_unicode)) {
910
// If an error occurs, check if creating a directory with a valid Unicode name would
1011
// succeed.
@@ -17,8 +18,8 @@ fn main() {
1718
}
1819
let incr_dir = tmp_path("incr-dir");
1920
rustc().input("foo.rs").incremental(&incr_dir).run();
20-
for crate_dir in std::fs::read_dir(&incr_dir).unwrap() {
21-
std::fs::create_dir(crate_dir.unwrap().path().join(&non_unicode)).unwrap();
21+
for crate_dir in run_make_support::fs::read_dir(&incr_dir) {
22+
run_make_support::fs::create_dir(crate_dir.unwrap().path().join(&non_unicode));
2223
}
2324
rustc().input("foo.rs").incremental(&incr_dir).run();
2425
}

tests/run-make/print-cfg/rmake.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::ffi::OsString;
1010
use std::io::BufRead;
1111
use std::iter::FromIterator;
1212

13+
use run_make_support::fs;
1314
use run_make_support::{rustc, tmp_path};
1415

1516
struct PrintCfg {
@@ -97,7 +98,7 @@ fn check(PrintCfg { target, includes, disallow }: PrintCfg) {
9798

9899
let output = rustc().target(target).arg(print_arg).run();
99100

100-
let output = std::fs::read_to_string(&tmp_path).unwrap();
101+
let output = fs::read_to_string(&tmp_path).unwrap();
101102

102103
check_(&output, includes, disallow);
103104
}

tests/run-make/print-to-output/rmake.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use std::ffi::OsString;
55

6+
use run_make_support::fs;
67
use run_make_support::{rustc, target, tmp_path};
78

89
struct Option<'a> {
@@ -52,7 +53,7 @@ fn check(args: Option) {
5253

5354
let _output = rustc().target(args.target).arg(print_arg).run();
5455

55-
std::fs::read_to_string(&tmp_path).unwrap()
56+
fs::read_to_string(&tmp_path)
5657
};
5758

5859
check_(&stdout, args.includes);

tests/run-make/repr128-dwarf/rmake.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use gimli::{AttributeValue, Dwarf, EndianRcSlice, Reader, RunTimeEndian};
55
use object::{Object, ObjectSection};
6+
use run_make_support::fs;
67
use run_make_support::{gimli, object, rustc, tmp_path};
78
use std::borrow::Cow;
89
use std::collections::HashMap;
@@ -18,9 +19,7 @@ fn main() {
1819
.join("Resources")
1920
.join("DWARF")
2021
.join("repr128");
21-
let output =
22-
std::fs::read(if dsym_location.try_exists().unwrap() { dsym_location } else { output })
23-
.unwrap();
22+
let output = fs::read(if dsym_location.try_exists().unwrap() { dsym_location } else { output });
2423
let obj = object::File::parse(output.as_slice()).unwrap();
2524
let endian = if obj.is_little_endian() { RunTimeEndian::Little } else { RunTimeEndian::Big };
2625
let dwarf = gimli::Dwarf::load(|section| -> Result<_, ()> {

tests/run-make/reset-codegen-1/rmake.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
//@ ignore-cross-compile
99

10+
use run_make_support::fs;
1011
use run_make_support::{rustc, tmp_path};
11-
use std::fs;
1212

1313
fn compile(output_file: &str, emit: Option<&str>) {
1414
let mut rustc = rustc();
@@ -31,7 +31,7 @@ fn main() {
3131
("multi-output", Some("asm,obj")),
3232
];
3333
for (output_file, emit) in flags {
34-
fs::remove_file(output_file).unwrap_or_default();
34+
std::fs::remove_file(output_file).unwrap_or_default();
3535
compile(output_file, emit);
3636
fs::remove_file(output_file);
3737
}

0 commit comments

Comments
 (0)