Skip to content

Commit 5405489

Browse files
committed
Add safe wrappers around fs in run_make_support
1 parent 93724a2 commit 5405489

File tree

28 files changed

+126
-41
lines changed

28 files changed

+126
-41
lines changed

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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> + std::fmt::Debug>(path: P) {
6+
fs::remove_file(path.as_ref()).expect(&format!("The file in path \"{:?}\" could not be removed.", path.as_ref()));
7+
}
8+
9+
/// A safe wrapper around `std::fs::copy` that prints the file paths if an operation fails.
10+
pub fn copy<P: AsRef<Path> + std::fmt::Debug, Q: AsRef<Path> + std::fmt::Debug>(from: P, to: Q) {
11+
fs::copy(from.as_ref(), to.as_ref()).expect(
12+
&format!("The file \"{:?}\" could not be copied over to \"{:?}\".",
13+
from.as_ref(),
14+
to.as_ref(),
15+
));
16+
}
17+
18+
/// A safe wrapper around `std::fs::File::create` that prints the file path if an operation fails.
19+
pub fn create_file<P: AsRef<Path> + std::fmt::Debug>(path: P) {
20+
fs::File::create(path.as_ref()).expect(&format!("The file in path \"{:?}\" could not be created.", path.as_ref()));
21+
}
22+
23+
/// A safe wrapper around `std::fs::read` that prints the file path if an operation fails.
24+
pub fn read<P: AsRef<Path> + std::fmt::Debug>(path: P) -> Vec<u8> {
25+
fs::read(path.as_ref()).expect(&format!("The file in path \"{:?}\" could not be read.", path.as_ref()))
26+
}
27+
28+
/// A safe wrapper around `std::fs::read_to_string` that prints the file path if an operation fails.
29+
pub fn read_to_string<P: AsRef<Path> + std::fmt::Debug>(path: P) -> String {
30+
fs::read_to_string(path.as_ref())
31+
.expect(&format!("The file in path \"{:?}\" could not be read into a String.", path.as_ref()))
32+
}
33+
34+
/// A safe wrapper around `std::fs::read_dir` that prints the file path if an operation fails.
35+
pub fn read_dir<P: AsRef<Path> + std::fmt::Debug>(path: P) -> fs::ReadDir {
36+
fs::read_dir(path.as_ref()).expect(&format!("The directory in path \"{:?}\" could not be read.", path.as_ref()))
37+
}
38+
39+
/// A safe wrapper around `std::fs::write` that prints the file path if an operation fails.
40+
pub fn write<P: AsRef<Path> + std::fmt::Debug, C: AsRef<[u8]>>(path: P, contents: C) {
41+
fs::read(path.as_ref(), contents.as_ref())
42+
.expect(&format!("The file in path \"{:?}\" could not be written to.", path.as_ref()));
43+
}
44+
45+
/// A safe wrapper around `std::fs::remove_dir_all` that prints the file path if an operation fails.
46+
pub fn remove_dir_all<P: AsRef<Path> + std::fmt::Debug>(path: P) {
47+
fs::remove_dir_all(path.as_ref()).expect(
48+
&format!("The directory in path \"{:?}\" could not be removed alongside all its contents.",
49+
path.as_ref(),
50+
));
51+
}
52+
53+
/// A safe wrapper around `std::fs::create_dir` that prints the file path if an operation fails.
54+
pub fn create_dir<P: AsRef<Path> + std::fmt::Debug>(path: P) {
55+
fs::create_dir(path.as_ref())
56+
.expect(&format!("The directory in path \"{:?}\" could not be created.", path.as_ref()));
57+
}
58+
59+
/// A safe wrapper around `std::fs::create_dir_all` that prints the file path if an operation fails.
60+
pub fn create_dir_all<P: AsRef<Path> + std::fmt::Debug>(path: P) {
61+
fs::create_dir_all(path.as_ref())
62+
.expect(&format!("The directory (and all its parents) in path \"{:?}\" could not be created.", path.as_ref()));
63+
}
64+
65+
/// A safe wrapper around `std::fs::metadata` that prints the file path if an operation fails.
66+
pub fn metadata<P: AsRef<Path> + std::fmt::Debug>(path: P) -> fs::Metadata {
67+
fs::metadata(path.as_ref())
68+
.expect(&format!("The file's metadata in path \"{:?}\" could not be read.", path.as_ref()))
69+
}
70+
71+
/// A safe wrapper around `std::fs::rename` that prints the file paths if an operation fails.
72+
pub fn rename<P: AsRef<Path> + std::fmt::Debug, Q: AsRef<Path> + std::fmt::Debug>(from: P, to: Q) {
73+
fs::rename(from.as_ref(), to.as_ref()).expect(
74+
&format!("The file \"{:?}\" could not be moved over to \"{:?}\"."),
75+
from.as_ref(),
76+
to.as_ref(),
77+
));
78+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
pub mod cc;
77
pub mod clang;
88
pub mod diff;
9+
pub mod fs;
910
pub mod llvm_readobj;
1011
pub mod run;
1112
pub mod rustc;

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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)