@@ -5,39 +5,31 @@ use std::sync::Mutex;
5
5
use lazy_static:: lazy_static;
6
6
#[ cfg( not( unix) ) ]
7
7
use winreg:: {
8
- enums:: { RegType , HKEY_CURRENT_USER , KEY_READ , KEY_WRITE } ,
8
+ enums:: { HKEY_CURRENT_USER , KEY_READ , KEY_WRITE } ,
9
9
RegKey , RegValue ,
10
10
} ;
11
11
12
12
#[ cfg( not( unix) ) ]
13
- use crate :: utils:: utils;
14
-
15
- #[ cfg( not( unix) ) ]
16
- pub fn get_path ( ) -> Option < String > {
13
+ pub fn get_path ( ) -> std:: io:: Result < Option < RegValue > > {
17
14
let root = RegKey :: predef ( HKEY_CURRENT_USER ) ;
18
15
let environment = root
19
16
. open_subkey_with_flags ( "Environment" , KEY_READ | KEY_WRITE )
20
17
. unwrap ( ) ;
21
- // XXX: copied from the mock support crate, but I am suspicous of this
22
- // code: This uses ok to allow signalling None for 'delete', but this
23
- // can fail e.g. with !(winerror::ERROR_BAD_FILE_TYPE) or other
24
- // failures; which will lead to attempting to delete the users path
25
- // rather than aborting the test suite.
26
- environment. get_value ( "PATH" ) . ok ( )
18
+ match environment. get_raw_value ( "PATH" ) {
19
+ Ok ( val) => Ok ( Some ( val) ) ,
20
+ Err ( ref e) if e. kind ( ) == std:: io:: ErrorKind :: NotFound => Ok ( None ) ,
21
+ Err ( e) => Err ( e) ,
22
+ }
27
23
}
28
24
29
25
#[ cfg( not( unix) ) ]
30
- fn restore_path ( p : Option < String > ) {
26
+ fn restore_path ( p : Option < RegValue > ) {
31
27
let root = RegKey :: predef ( HKEY_CURRENT_USER ) ;
32
28
let environment = root
33
29
. open_subkey_with_flags ( "Environment" , KEY_READ | KEY_WRITE )
34
30
. unwrap ( ) ;
35
31
if let Some ( p) = p. as_ref ( ) {
36
- let reg_value = RegValue {
37
- bytes : utils:: string_to_winreg_bytes ( & p) ,
38
- vtype : RegType :: REG_EXPAND_SZ ,
39
- } ;
40
- environment. set_raw_value ( "PATH" , & reg_value) . unwrap ( ) ;
32
+ environment. set_raw_value ( "PATH" , & p) . unwrap ( ) ;
41
33
} else {
42
34
let _ = environment. delete_value ( "PATH" ) ;
43
35
}
@@ -53,16 +45,16 @@ pub fn with_saved_path(f: &dyn Fn()) {
53
45
54
46
// On windows these tests mess with the user's PATH. Save
55
47
// and restore them here to keep from trashing things.
56
- let saved_path = get_path ( ) ;
48
+ let saved_path = get_path ( ) . expect ( "Error getting PATH: Better abort to avoid trashing it." ) ;
57
49
let _g = scopeguard:: guard ( saved_path, restore_path) ;
58
50
59
51
f ( ) ;
60
52
}
61
53
62
54
#[ cfg( unix) ]
63
- pub fn get_path ( ) -> Option < String > {
64
- None
55
+ pub fn get_path ( ) -> std :: io :: Result < Option < ( ) > > {
56
+ Ok ( None )
65
57
}
66
58
67
59
#[ cfg( unix) ]
68
- fn restore_path ( _: Option < String > ) { }
60
+ fn restore_path ( _: Option < ( ) > ) { }
0 commit comments