Skip to content

Commit 242d6f6

Browse files
authored
Merge pull request #2752 from joshrotenberg/bug/ensure_file_removed_retry
Use utils::remove_file in utils::ensure_file_removed
2 parents 5b8099d + 77eed8d commit 242d6f6

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/utils/utils.rs

+42-3
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,14 @@ pub fn remove_file(name: &'static str, path: &Path) -> Result<()> {
392392
}
393393

394394
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);
396396
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+
}
399403
}
400404
}
401405
result.with_context(|| RustupError::RemovingFile {
@@ -756,4 +760,39 @@ mod tests {
756760

757761
assert_eq!(expected, v);
758762
}
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+
}
759798
}

0 commit comments

Comments
 (0)