Skip to content

Commit 03f19d6

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

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

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

+32
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,38 @@ 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+
/// This will panic on Windows if the path is a directory (as it would otherwise do nothing)
177+
#[track_caller]
178+
pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce() + std::panic::UnwindSafe>(
179+
path: P,
180+
closure: F,
181+
) {
182+
let path = path.as_ref();
183+
if is_windows() && path.is_dir() {
184+
eprintln!("This helper function cannot be used on Windows to make directories readonly.");
185+
eprintln!(
186+
"See the official documentation:
187+
https://doc.rust-lang.org/std/fs/struct.Permissions.html#method.set_readonly"
188+
);
189+
panic!("`test_while_readonly` on directory detected while on Windows.");
190+
}
191+
let metadata = fs_wrapper::metadata(&path);
192+
let original_perms = metadata.permissions();
193+
194+
let mut new_perms = original_perms.clone();
195+
new_perms.set_readonly(true);
196+
fs_wrapper::set_permissions(&path, new_perms);
197+
198+
let success = std::panic::catch_unwind(closure);
199+
200+
fs_wrapper::set_permissions(&path, original_perms);
201+
success.unwrap();
202+
}
203+
172204
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
173205
/// available on the platform!
174206
#[track_caller]

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

+15-21
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,26 @@
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`
18+
//@ ignore-windows
19+
// Reason: `set_readonly` has no effect on directories
20+
// and does not prevent modification.
2121

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

2424
fn main() {
2525
// Create an inaccessible directory.
2626
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);
27+
test_while_readonly("inaccessible", || {
28+
// Run rustc with `-Z temps-dir` set to a directory *inside* the inaccessible one,
29+
// so that it can't create `tmp`.
30+
rustc()
31+
.input("program.rs")
32+
.arg("-Ztemps-dir=inaccessible/tmp")
33+
.run_fail()
34+
.assert_stderr_contains(
35+
"failed to find or create the directory specified by `--temps-dir`",
36+
);
37+
});
4438
}

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

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

88
//@ ignore-cross-compile
99

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

1212
fn main() {
1313
rustc().input("foo-bar.rs").crate_type("bin").run();
1414
assert!(path(bin_name("foo-bar")).exists());
1515
rustc().input("foo-bar.rs").crate_type("lib").run();
16-
assert!(path(bin_name("libfoo_bar.rlib")).exists());
16+
assert!(path(rust_lib_name("foo_bar")).exists());
1717
}

tests/run-make/parallel-rustc-no-overwrite/rmake.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ use std::sync::{Arc, Barrier};
1010
use std::thread;
1111

1212
fn main() {
13+
fs_wrapper::create_file("lib.rs");
1314
let barrier = Arc::new(Barrier::new(2));
1415
let handle = {
1516
let barrier = Arc::clone(&barrier);
1617
thread::spawn(move || {
1718
barrier.wait();
18-
rustc().crate_type("lib").arg("-Ztemps-dir=temp1").input("lib.rs");
19+
rustc().crate_type("lib").arg("-Ztemps-dir=temp1").input("lib.rs").run();
1920
})
2021
};
2122
barrier.wait();
22-
rustc().crate_type("staticlib").arg("-Ztemps-dir=temp2").input("lib.rs");
23+
rustc().crate_type("staticlib").arg("-Ztemps-dir=temp2").input("lib.rs").run();
2324
handle.join().expect("lib thread panicked");
2425
}

0 commit comments

Comments
 (0)