@@ -15,9 +15,9 @@ pub mod fs_helpers;
15
15
pub mod fs_wrapper;
16
16
pub mod path_helpers;
17
17
pub mod run;
18
+ pub mod scoped_run;
18
19
pub mod targets;
19
20
20
- use std:: fs;
21
21
use std:: panic;
22
22
use std:: path:: { Path , PathBuf } ;
23
23
@@ -74,39 +74,10 @@ pub use path_helpers::{cwd, cygpath_windows, path, source_root};
74
74
/// Helpers for common fs operations.
75
75
pub use fs_helpers:: { copy_dir_all, create_symlink, read_dir} ;
76
76
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} ;
104
79
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 } ;
110
81
111
82
/// Browse the directory `path` non-recursively and return all files which respect the parameters
112
83
/// outlined by `closure`.
@@ -296,24 +267,3 @@ pub fn assert_not_contains<S1: AsRef<str>, S2: AsRef<str>>(haystack: S1, needle:
296
267
panic ! ( "needle was unexpectedly found in haystack" ) ;
297
268
}
298
269
}
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
- }
0 commit comments