Skip to content

Commit 0e715f7

Browse files
committed
Add new test_while_readonly helper function to run-make-support
1 parent 7c2b3b5 commit 0e715f7

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ pub fn cwd() -> PathBuf {
169169
env::current_dir().unwrap()
170170
}
171171

172+
// FIXME(Oneirical): This will no longer be required after compiletest receives the ability
173+
// to manipulate read-only files. See https://github.com/rust-lang/rust/issues/126334
174+
/// Ensure that the path P is read-only while the test runs, and restore original permissions
175+
/// at the end so compiletest can clean up.
176+
#[track_caller]
177+
pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce() + std::panic::UnwindSafe>(
178+
path: P,
179+
closure: F,
180+
) {
181+
let path = path.as_ref();
182+
let metadata = fs_wrapper::metadata(&path);
183+
let original_perms = metadata.permissions();
184+
185+
let mut new_perms = original_perms.clone();
186+
new_perms.set_readonly(true);
187+
fs_wrapper::set_permissions(&path, new_perms);
188+
189+
assert!(std::panic::catch_unwind(closure).is_ok());
190+
191+
fs_wrapper::set_permissions(&path, original_perms);
192+
}
193+
172194
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
173195
/// available on the platform!
174196
#[track_caller]

tests/run-make/inaccessible-temp-dir/rmake.rs

+12-21
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,23 @@
1313
// use a directory with non-existing parent like `/does-not-exist/output`.
1414
// See https://github.com/rust-lang/rust/issues/66530
1515

16-
//@ only-linux
17-
// Reason: set_mode is only available on Unix
18-
1916
//@ ignore-arm
2017
// Reason: linker error on `armhf-gnu`
2118

22-
use run_make_support::{fs_wrapper, rustc};
19+
use run_make_support::{fs_wrapper, rustc, test_while_readonly};
2320

2421
fn main() {
2522
// Create an inaccessible directory.
2623
fs_wrapper::create_dir("inaccessible");
27-
let meta = fs_wrapper::metadata("inaccessible");
28-
let mut perms = meta.permissions();
29-
perms.set_mode(0o000); // Lock down the directory.
30-
fs_wrapper::set_permissions("inaccessible", perms);
31-
32-
// Run rustc with `-Z temps-dir` set to a directory *inside* the inaccessible one,
33-
// so that it can't create `tmp`.
34-
rustc()
35-
.input("program.rs")
36-
.arg("-Ztemps-dir=inaccessible/tmp")
37-
.run_fail()
38-
.assert_stderr_contains(
39-
"failed to find or create the directory specified by `--temps-dir`",
40-
);
41-
42-
perms.set_mode(0o666); // Unlock the directory, so that compiletest can delete it.
43-
fs_wrapper::set_permissions("inaccessible", perms);
24+
test_while_readonly("inaccessible", || {
25+
// Run rustc with `-Z temps-dir` set to a directory *inside* the inaccessible one,
26+
// so that it can't create `tmp`.
27+
rustc()
28+
.input("program.rs")
29+
.arg("-Ztemps-dir=inaccessible/tmp")
30+
.run_fail()
31+
.assert_stderr_contains(
32+
"failed to find or create the directory specified by `--temps-dir`",
33+
);
34+
});
4435
}

tests/run-make/output-with-hyphens/rmake.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
//@ ignore-cross-compile
99

10-
use run_make_support::{path, rustc};
10+
use run_make_support::{bin_name, path, rustc};
1111

1212
fn main() {
1313
rustc().input("foo-bar.rs").crate_type("bin").run();

0 commit comments

Comments
 (0)