@@ -392,10 +392,14 @@ pub fn remove_file(name: &'static str, path: &Path) -> Result<()> {
392
392
}
393
393
394
394
pub fn ensure_file_removed ( name : & ' static str , path : & Path ) -> Result < ( ) > {
395
- let result = fs :: remove_file ( path) ;
395
+ let result = remove_file ( name , path) ;
396
396
if let Err ( err) = & result {
397
- if err. kind ( ) == io:: ErrorKind :: NotFound {
398
- return Ok ( ( ) ) ;
397
+ if let Some ( retry:: Error :: Operation { error : e, .. } ) =
398
+ err. downcast_ref :: < retry:: Error < io:: Error > > ( )
399
+ {
400
+ if e. kind ( ) == io:: ErrorKind :: NotFound {
401
+ return Ok ( ( ) ) ;
402
+ }
399
403
}
400
404
}
401
405
result. with_context ( || RustupError :: RemovingFile {
@@ -756,4 +760,39 @@ mod tests {
756
760
757
761
assert_eq ! ( expected, v) ;
758
762
}
763
+
764
+ #[ test]
765
+ fn test_remove_file ( ) {
766
+ let tempdir = tempfile:: Builder :: new ( ) . prefix ( "rustup" ) . tempdir ( ) . unwrap ( ) ;
767
+ let f_path = tempdir. path ( ) . join ( "f" ) ;
768
+ File :: create ( & f_path) . unwrap ( ) ;
769
+
770
+ assert ! ( f_path. exists( ) ) ;
771
+ assert ! ( remove_file( "f" , & f_path) . is_ok( ) ) ;
772
+
773
+ assert ! ( !f_path. exists( ) ) ;
774
+ let result = remove_file ( "f" , & f_path) ;
775
+ let err = result. unwrap_err ( ) ;
776
+
777
+ match err. downcast_ref :: < RustupError > ( ) {
778
+ Some ( RustupError :: RemovingFile { name, path } ) => {
779
+ assert_eq ! ( * name, "f" ) ;
780
+ assert_eq ! ( path. clone( ) , f_path) ;
781
+ }
782
+ _ => panic ! ( "Expected an error removing file" ) ,
783
+ }
784
+ }
785
+
786
+ #[ test]
787
+ fn test_ensure_file_removed ( ) {
788
+ let tempdir = tempfile:: Builder :: new ( ) . prefix ( "rustup" ) . tempdir ( ) . unwrap ( ) ;
789
+ let f_path = tempdir. path ( ) . join ( "f" ) ;
790
+ File :: create ( & f_path) . unwrap ( ) ;
791
+
792
+ assert ! ( f_path. exists( ) ) ;
793
+ assert ! ( ensure_file_removed( "f" , & f_path) . is_ok( ) ) ;
794
+
795
+ assert ! ( !f_path. exists( ) ) ;
796
+ assert ! ( ensure_file_removed( "f" , & f_path) . is_ok( ) ) ;
797
+ }
759
798
}
0 commit comments