Skip to content

Commit 7ac14dc

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

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

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

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

172+
/// Ensure that the path P is read-only while the test runs, and restore original permissions
173+
// at the end so compiletest can clean up.
174+
#[track_caller]
175+
pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce()>(path: P, closure: F) {
176+
let path = path.as_ref();
177+
let metadata = fs_wrapper::metadata(&path);
178+
let original_perms = metadata.permissions();
179+
180+
let mut new_perms = original_perms.clone();
181+
new_perms.set_readonly(true);
182+
fs_wrapper::set_permissions(&path, new_perms);
183+
184+
closure();
185+
186+
fs_wrapper::set_permissions(&path, original_perms);
187+
}
188+
172189
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
173190
/// available on the platform!
174191
#[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)