@@ -8,6 +8,28 @@ use winreg::{
8
8
RegKey , RegValue ,
9
9
} ;
10
10
11
+ /// Support testing of code that mutates global state
12
+ pub fn with_saved_global_state < S > (
13
+ getter : impl Fn ( ) -> io:: Result < S > ,
14
+ setter : impl Fn ( S ) ,
15
+ f : & mut dyn FnMut ( ) ,
16
+ ) {
17
+ // Lock protects concurrent mutation of registry
18
+ static LOCK : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
19
+ let _g = LOCK . lock ( ) ;
20
+
21
+ // Save and restore the global state here to keep from trashing things.
22
+ let saved_state =
23
+ getter ( ) . expect ( "Error getting global state: Better abort to avoid trashing it" ) ;
24
+ let _g = scopeguard:: guard ( saved_state, setter) ;
25
+
26
+ f ( ) ;
27
+ }
28
+
29
+ pub fn with_saved_path ( f : & mut dyn FnMut ( ) ) {
30
+ with_saved_global_state ( get_path, restore_path, f)
31
+ }
32
+
11
33
#[ cfg( windows) ]
12
34
pub fn get_path ( ) -> io:: Result < Option < RegValue > > {
13
35
let root = RegKey :: predef ( HKEY_CURRENT_USER ) ;
@@ -34,20 +56,6 @@ fn restore_path(p: Option<RegValue>) {
34
56
}
35
57
}
36
58
37
- /// Support testing of code that mutates global path state
38
- pub fn with_saved_path ( f : & mut dyn FnMut ( ) ) {
39
- // Lock protects concurrent mutation of registry
40
- static LOCK : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
41
- let _g = LOCK . lock ( ) ;
42
-
43
- // On windows these tests mess with the user's PATH. Save
44
- // and restore them here to keep from trashing things.
45
- let saved_path = get_path ( ) . expect ( "Error getting PATH: Better abort to avoid trashing it." ) ;
46
- let _g = scopeguard:: guard ( saved_path, restore_path) ;
47
-
48
- f ( ) ;
49
- }
50
-
51
59
#[ cfg( unix) ]
52
60
pub fn get_path ( ) -> io:: Result < Option < ( ) > > {
53
61
Ok ( None )
0 commit comments