Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e174399

Browse files
committed
run_make_support: move run_in_tmpdir and test_while_readonly to scoped_run module
1 parent 7bd7729 commit e174399

File tree

2 files changed

+75
-54
lines changed

2 files changed

+75
-54
lines changed

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

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ pub mod fs_helpers;
1515
pub mod fs_wrapper;
1616
pub mod path_helpers;
1717
pub mod run;
18+
pub mod scoped_run;
1819
pub mod targets;
1920

20-
use std::fs;
2121
use std::panic;
2222
use std::path::{Path, PathBuf};
2323

@@ -74,39 +74,10 @@ pub use path_helpers::{cwd, cygpath_windows, path, source_root};
7474
/// Helpers for common fs operations.
7575
pub use fs_helpers::{copy_dir_all, create_symlink, read_dir};
7676

77-
use command::{Command, CompletedProcess};
78-
79-
// FIXME(Oneirical): This will no longer be required after compiletest receives the ability
80-
// to manipulate read-only files. See https://github.com/rust-lang/rust/issues/126334
81-
/// Ensure that the path P is read-only while the test runs, and restore original permissions
82-
/// at the end so compiletest can clean up.
83-
/// This will panic on Windows if the path is a directory (as it would otherwise do nothing)
84-
#[track_caller]
85-
pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce() + std::panic::UnwindSafe>(
86-
path: P,
87-
closure: F,
88-
) {
89-
let path = path.as_ref();
90-
if is_windows() && path.is_dir() {
91-
eprintln!("This helper function cannot be used on Windows to make directories readonly.");
92-
eprintln!(
93-
"See the official documentation:
94-
https://doc.rust-lang.org/std/fs/struct.Permissions.html#method.set_readonly"
95-
);
96-
panic!("`test_while_readonly` on directory detected while on Windows.");
97-
}
98-
let metadata = fs_wrapper::metadata(&path);
99-
let original_perms = metadata.permissions();
100-
101-
let mut new_perms = original_perms.clone();
102-
new_perms.set_readonly(true);
103-
fs_wrapper::set_permissions(&path, new_perms);
77+
/// Helpers for scoped test execution where certain properties are attempted to be maintained.
78+
pub use scoped_run::{run_in_tmpdir, test_while_readonly};
10479

105-
let success = std::panic::catch_unwind(closure);
106-
107-
fs_wrapper::set_permissions(&path, original_perms);
108-
success.unwrap();
109-
}
80+
use command::{Command, CompletedProcess};
11081

11182
/// Browse the directory `path` non-recursively and return all files which respect the parameters
11283
/// outlined by `closure`.
@@ -296,24 +267,3 @@ pub fn assert_not_contains<S1: AsRef<str>, S2: AsRef<str>>(haystack: S1, needle:
296267
panic!("needle was unexpectedly found in haystack");
297268
}
298269
}
299-
300-
/// This function is designed for running commands in a temporary directory
301-
/// that is cleared after the function ends.
302-
///
303-
/// What this function does:
304-
/// 1) Creates a temporary directory (`tmpdir`)
305-
/// 2) Copies all files from the current directory to `tmpdir`
306-
/// 3) Changes the current working directory to `tmpdir`
307-
/// 4) Calls `callback`
308-
/// 5) Switches working directory back to the original one
309-
/// 6) Removes `tmpdir`
310-
pub fn run_in_tmpdir<F: FnOnce()>(callback: F) {
311-
let original_dir = cwd();
312-
let tmpdir = original_dir.join("../temporary-directory");
313-
copy_dir_all(".", &tmpdir);
314-
315-
std::env::set_current_dir(&tmpdir).unwrap();
316-
callback();
317-
std::env::set_current_dir(original_dir).unwrap();
318-
fs::remove_dir_all(tmpdir).unwrap();
319-
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//! Collection of helpers that try to maintain certain properties while running a test closure.
2+
3+
use std::fs;
4+
use std::path::Path;
5+
6+
use crate::fs_helpers::copy_dir_all;
7+
use crate::fs_wrapper;
8+
use crate::path_helpers::cwd;
9+
use crate::targets::is_windows;
10+
11+
/// Ensure that the path P is read-only while the test runs, and restore original permissions at the
12+
/// end so compiletest can clean up. This will panic on Windows if the path is a directory (as it
13+
/// would otherwise do nothing)
14+
///
15+
/// # Pitfalls
16+
///
17+
/// - Some CI runners are ran as root which may bypass read-only permission restrictions. Unclear
18+
/// exactly when such scenarios occur.
19+
///
20+
/// # FIXME
21+
///
22+
/// FIXME(Oneirical): This will no longer be required after compiletest receives the ability to
23+
/// manipulate read-only files. See <https://github.com/rust-lang/rust/issues/126334>.
24+
#[track_caller]
25+
pub fn test_while_readonly<P, F>(path: P, closure: F)
26+
where
27+
P: AsRef<Path>,
28+
F: FnOnce() + std::panic::UnwindSafe,
29+
{
30+
let path = path.as_ref();
31+
if is_windows() && path.is_dir() {
32+
eprintln!("This helper function cannot be used on Windows to make directories readonly.");
33+
eprintln!(
34+
"See the official documentation:
35+
https://doc.rust-lang.org/std/fs/struct.Permissions.html#method.set_readonly"
36+
);
37+
panic!("`test_while_readonly` on directory detected while on Windows.");
38+
}
39+
let metadata = fs_wrapper::metadata(&path);
40+
let original_perms = metadata.permissions();
41+
42+
let mut new_perms = original_perms.clone();
43+
new_perms.set_readonly(true);
44+
fs_wrapper::set_permissions(&path, new_perms);
45+
46+
let success = std::panic::catch_unwind(closure);
47+
48+
fs_wrapper::set_permissions(&path, original_perms);
49+
success.unwrap();
50+
}
51+
52+
/// This function is designed for running commands in a temporary directory that is cleared after
53+
/// the function ends.
54+
///
55+
/// What this function does:
56+
/// 1. Creates a temporary directory (`tmpdir`)
57+
/// 2. Copies all files from the current directory to `tmpdir`
58+
/// 3. Changes the current working directory to `tmpdir`
59+
/// 4. Calls `callback`
60+
/// 5. Switches working directory back to the original one
61+
/// 6. Removes `tmpdir`
62+
pub fn run_in_tmpdir<F: FnOnce()>(callback: F) {
63+
let original_dir = cwd();
64+
let tmpdir = original_dir.join("../temporary-directory");
65+
copy_dir_all(".", &tmpdir);
66+
67+
std::env::set_current_dir(&tmpdir).unwrap();
68+
callback();
69+
std::env::set_current_dir(original_dir).unwrap();
70+
fs::remove_dir_all(tmpdir).unwrap();
71+
}

0 commit comments

Comments
 (0)