Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
427cf94
run_make_support: move `impl_common_helpers` into own `macros` module
jieyouxu Jul 15, 2024
544dda3
run_make_support: move target checks into `targets` module
jieyouxu Jul 15, 2024
a02008e
run_make_support: move `env_var{,_os}` into `env_checked` module
jieyouxu Jul 15, 2024
288c572
run_make_support: move external deps to `external_deps` module
jieyouxu Jul 15, 2024
f042e72
run_make_support: cleanup and document some lib.rs reexports
jieyouxu Jul 15, 2024
439c6f6
run_make_support: move `ar` into own module
jieyouxu Jul 15, 2024
483328d
run_make_support: move artifact name helpers into `artifact_names` mo…
jieyouxu Jul 15, 2024
dc95315
run_make_support: move path-related helpers into own module
jieyouxu Jul 15, 2024
17212ab
run_make_support: move fs helpers to own module
jieyouxu Jul 15, 2024
66cef19
run_make_support: move `run_in_tmpdir` and `test_while_readonly` to `…
jieyouxu Jul 15, 2024
f66d3d3
run_make_support: move assertions and helpers into own module
jieyouxu Jul 15, 2024
230804d
run_make_support: rename `recursive_diff` to `assert_recursive_eq`
jieyouxu Jul 15, 2024
56cbfa8
tests: update rustdoc test for renamed `assert_recursive_eq`
jieyouxu Jul 15, 2024
88fd1df
run_make_support: move `assert_recursive_eq` into `assertion_helpers`
jieyouxu Jul 15, 2024
e956808
run_make_support: move `handle_failed_output` into `util` module
jieyouxu Jul 15, 2024
aadd085
run_make_support: make `set_host_rpath` private and move into `util`
jieyouxu Jul 15, 2024
b7f7205
run_make_support: move `build_native_static_lib` under `external_deps`
jieyouxu Jul 17, 2024
a443dc4
run_make_support: rename `env_checked` -> `env`
jieyouxu Jul 17, 2024
13a1751
run_make_support: rename `cygpath_windows` to `get_windows_path` and …
jieyouxu Jul 17, 2024
e1569fd
run_make_support: coalesce fs helpers into single `fs` module
jieyouxu Jul 17, 2024
636be91
tests: update for renamed `fs` module in run_make_support
jieyouxu Jul 17, 2024
0dfecb3
run_make_support: move some helpers from assertion to path
jieyouxu Jul 17, 2024
57a2f76
run_make_support: moved some helpers into `string` module
jieyouxu Jul 17, 2024
30bdc4a
run_make_support: rename `assert_recursive_eq` to `assert_dirs_are_eq…
jieyouxu Jul 17, 2024
a00b860
tests: update rustdoc test for renamed `assert_dirs_are_equal`
jieyouxu Jul 17, 2024
1f1bf4c
run_make_support: use `fs` internally, but `rfs` to tests
jieyouxu Jul 17, 2024
d69cc1c
tests: update for `rfs` rename
jieyouxu Jul 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
run_make_support: move run_in_tmpdir and test_while_readonly to `…
…scoped_run` module
  • Loading branch information
jieyouxu committed Jul 17, 2024
commit 66cef19d11553dcc48848200afbdf113021af76c
58 changes: 4 additions & 54 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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();
}
71 changes: 71 additions & 0 deletions src/tools/run-make-support/src/scoped_run.rs
Original file line number Diff line number Diff line change
@@ -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();
}