@@ -15,9 +15,9 @@ pub mod fs_helpers;
1515pub mod fs_wrapper;
1616pub mod path_helpers;
1717pub mod run;
18+ pub mod scoped_run;
1819pub mod targets;
1920
20- use std:: fs;
2121use std:: panic;
2222use 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.
7575pub 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`.
@@ -316,24 +287,3 @@ pub fn assert_not_contains<S1: AsRef<str>, S2: AsRef<str>>(haystack: S1, needle:
316287 panic ! ( "needle was unexpectedly found in haystack" ) ;
317288 }
318289}
319-
320- /// This function is designed for running commands in a temporary directory
321- /// that is cleared after the function ends.
322- ///
323- /// What this function does:
324- /// 1) Creates a temporary directory (`tmpdir`)
325- /// 2) Copies all files from the current directory to `tmpdir`
326- /// 3) Changes the current working directory to `tmpdir`
327- /// 4) Calls `callback`
328- /// 5) Switches working directory back to the original one
329- /// 6) Removes `tmpdir`
330- pub fn run_in_tmpdir < F : FnOnce ( ) > ( callback : F ) {
331- let original_dir = cwd ( ) ;
332- let tmpdir = original_dir. join ( "../temporary-directory" ) ;
333- copy_dir_all ( "." , & tmpdir) ;
334-
335- std:: env:: set_current_dir ( & tmpdir) . unwrap ( ) ;
336- callback ( ) ;
337- std:: env:: set_current_dir ( original_dir) . unwrap ( ) ;
338- fs:: remove_dir_all ( tmpdir) . unwrap ( ) ;
339- }
0 commit comments