@@ -5,39 +5,31 @@ use std::sync::Mutex;
55use lazy_static:: lazy_static;
66#[ cfg( not( unix) ) ]
77use winreg:: {
8- enums:: { RegType , HKEY_CURRENT_USER , KEY_READ , KEY_WRITE } ,
8+ enums:: { HKEY_CURRENT_USER , KEY_READ , KEY_WRITE } ,
99 RegKey , RegValue ,
1010} ;
1111
1212#[ 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 > > {
1714 let root = RegKey :: predef ( HKEY_CURRENT_USER ) ;
1815 let environment = root
1916 . open_subkey_with_flags ( "Environment" , KEY_READ | KEY_WRITE )
2017 . 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+ }
2723}
2824
2925#[ cfg( not( unix) ) ]
30- fn restore_path ( p : Option < String > ) {
26+ fn restore_path ( p : Option < RegValue > ) {
3127 let root = RegKey :: predef ( HKEY_CURRENT_USER ) ;
3228 let environment = root
3329 . open_subkey_with_flags ( "Environment" , KEY_READ | KEY_WRITE )
3430 . unwrap ( ) ;
3531 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 ( ) ;
4133 } else {
4234 let _ = environment. delete_value ( "PATH" ) ;
4335 }
@@ -53,16 +45,16 @@ pub fn with_saved_path(f: &dyn Fn()) {
5345
5446 // On windows these tests mess with the user's PATH. Save
5547 // 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." ) ;
5749 let _g = scopeguard:: guard ( saved_path, restore_path) ;
5850
5951 f ( ) ;
6052}
6153
6254#[ cfg( unix) ]
63- pub fn get_path ( ) -> Option < String > {
64- None
55+ pub fn get_path ( ) -> std :: io :: Result < Option < ( ) > > {
56+ Ok ( None )
6557}
6658
6759#[ cfg( unix) ]
68- fn restore_path ( _: Option < String > ) { }
60+ fn restore_path ( _: Option < ( ) > ) { }
0 commit comments