diff options
| author | 许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com> | 2024-07-15 11:38:47 +0000 |
|---|---|---|
| committer | 许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com> | 2024-07-17 12:48:22 +0000 |
| commit | 66cef19d11553dcc48848200afbdf113021af76c (patch) | |
| tree | 5abf0a4d1a6e4421af168f3cff8462af4603d90d /src | |
| parent | 17212abd1a75a83a5e7ccc4aebaf573bf5766c48 (diff) | |
| download | rust-66cef19d11553dcc48848200afbdf113021af76c.tar.gz rust-66cef19d11553dcc48848200afbdf113021af76c.zip | |
run_make_support: move `run_in_tmpdir` and `test_while_readonly` to `scoped_run` module
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/run-make-support/src/lib.rs | 58 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/scoped_run.rs | 71 |
2 files changed, 75 insertions, 54 deletions
diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 33248527f0a..0f8fc6af0b3 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -15,9 +15,9 @@ pub mod fs_helpers; pub mod fs_wrapper; pub mod path_helpers; pub mod run; +pub mod scoped_run; pub mod targets; -use std::fs; use std::panic; use std::path::{Path, PathBuf}; @@ -74,39 +74,10 @@ pub use path_helpers::{cwd, cygpath_windows, path, source_root}; /// Helpers for common fs operations. pub use fs_helpers::{copy_dir_all, create_symlink, read_dir}; -use command::{Command, CompletedProcess}; - -// FIXME(Oneirical): This will no longer be required after compiletest receives the ability -// to manipulate read-only files. See https://github.com/rust-lang/rust/issues/126334 -/// Ensure that the path P is read-only while the test runs, and restore original permissions -/// at the end so compiletest can clean up. -/// This will panic on Windows if the path is a directory (as it would otherwise do nothing) -#[track_caller] -pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce() + std::panic::UnwindSafe>( - path: P, - closure: F, -) { - let path = path.as_ref(); - if is_windows() && path.is_dir() { - eprintln!("This helper function cannot be used on Windows to make directories readonly."); - eprintln!( - "See the official documentation: - https://doc.rust-lang.org/std/fs/struct.Permissions.html#method.set_readonly" - ); - panic!("`test_while_readonly` on directory detected while on Windows."); - } - let metadata = fs_wrapper::metadata(&path); - let original_perms = metadata.permissions(); - - let mut new_perms = original_perms.clone(); - new_perms.set_readonly(true); - fs_wrapper::set_permissions(&path, new_perms); +/// Helpers for scoped test execution where certain properties are attempted to be maintained. +pub use scoped_run::{run_in_tmpdir, test_while_readonly}; - let success = std::panic::catch_unwind(closure); - - fs_wrapper::set_permissions(&path, original_perms); - success.unwrap(); -} +use command::{Command, CompletedProcess}; /// Browse the directory `path` non-recursively and return all files which respect the parameters /// outlined by `closure`. @@ -316,24 +287,3 @@ pub fn assert_not_contains<S1: AsRef<str>, S2: AsRef<str>>(haystack: S1, needle: panic!("needle was unexpectedly found in haystack"); } } - -/// This function is designed for running commands in a temporary directory -/// that is cleared after the function ends. -/// -/// What this function does: -/// 1) Creates a temporary directory (`tmpdir`) -/// 2) Copies all files from the current directory to `tmpdir` -/// 3) Changes the current working directory to `tmpdir` -/// 4) Calls `callback` -/// 5) Switches working directory back to the original one -/// 6) Removes `tmpdir` -pub fn run_in_tmpdir<F: FnOnce()>(callback: F) { - let original_dir = cwd(); - let tmpdir = original_dir.join("../temporary-directory"); - copy_dir_all(".", &tmpdir); - - std::env::set_current_dir(&tmpdir).unwrap(); - callback(); - std::env::set_current_dir(original_dir).unwrap(); - fs::remove_dir_all(tmpdir).unwrap(); -} diff --git a/src/tools/run-make-support/src/scoped_run.rs b/src/tools/run-make-support/src/scoped_run.rs new file mode 100644 index 00000000000..ab8d2429b6a --- /dev/null +++ b/src/tools/run-make-support/src/scoped_run.rs @@ -0,0 +1,71 @@ +//! Collection of helpers that try to maintain certain properties while running a test closure. + +use std::fs; +use std::path::Path; + +use crate::fs_helpers::copy_dir_all; +use crate::fs_wrapper; +use crate::path_helpers::cwd; +use crate::targets::is_windows; + +/// Ensure that the path P is read-only while the test runs, and restore original permissions at the +/// end so compiletest can clean up. This will panic on Windows if the path is a directory (as it +/// would otherwise do nothing) +/// +/// # Pitfalls +/// +/// - Some CI runners are ran as root which may bypass read-only permission restrictions. Unclear +/// exactly when such scenarios occur. +/// +/// # FIXME +/// +/// FIXME(Oneirical): This will no longer be required after compiletest receives the ability to +/// manipulate read-only files. See <https://github.com/rust-lang/rust/issues/126334>. +#[track_caller] +pub fn test_while_readonly<P, F>(path: P, closure: F) +where + P: AsRef<Path>, + F: FnOnce() + std::panic::UnwindSafe, +{ + let path = path.as_ref(); + if is_windows() && path.is_dir() { + eprintln!("This helper function cannot be used on Windows to make directories readonly."); + eprintln!( + "See the official documentation: + https://doc.rust-lang.org/std/fs/struct.Permissions.html#method.set_readonly" + ); + panic!("`test_while_readonly` on directory detected while on Windows."); + } + let metadata = fs_wrapper::metadata(&path); + let original_perms = metadata.permissions(); + + let mut new_perms = original_perms.clone(); + new_perms.set_readonly(true); + fs_wrapper::set_permissions(&path, new_perms); + + let success = std::panic::catch_unwind(closure); + + fs_wrapper::set_permissions(&path, original_perms); + success.unwrap(); +} + +/// This function is designed for running commands in a temporary directory that is cleared after +/// the function ends. +/// +/// What this function does: +/// 1. Creates a temporary directory (`tmpdir`) +/// 2. Copies all files from the current directory to `tmpdir` +/// 3. Changes the current working directory to `tmpdir` +/// 4. Calls `callback` +/// 5. Switches working directory back to the original one +/// 6. Removes `tmpdir` +pub fn run_in_tmpdir<F: FnOnce()>(callback: F) { + let original_dir = cwd(); + let tmpdir = original_dir.join("../temporary-directory"); + copy_dir_all(".", &tmpdir); + + std::env::set_current_dir(&tmpdir).unwrap(); + callback(); + std::env::set_current_dir(original_dir).unwrap(); + fs::remove_dir_all(tmpdir).unwrap(); +} |
