Skip to content

Commit 5698143

Browse files
committed
also improve remove error for files
1 parent 7f7fdaa commit 5698143

File tree

8 files changed

+24
-22
lines changed

8 files changed

+24
-22
lines changed

src/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl BuildDirectory {
147147
let source_dir = self.source_dir();
148148
if source_dir.exists() {
149149
remove_dir_all(&source_dir)
150-
.map_err(|error| crate::utils::improve_remove_dir_error(error, &source_dir))?;
150+
.map_err(|error| crate::utils::improve_remove_error(error, &source_dir))?;
151151
}
152152

153153
let mut prepare = Prepare::new(&self.workspace, toolchain, krate, &source_dir, patches);
@@ -161,7 +161,7 @@ impl BuildDirectory {
161161
})?;
162162

163163
remove_dir_all(&source_dir)
164-
.map_err(|error| crate::utils::improve_remove_dir_error(error, &source_dir))?;
164+
.map_err(|error| crate::utils::improve_remove_error(error, &source_dir))?;
165165
Ok(res)
166166
}
167167

@@ -170,7 +170,7 @@ impl BuildDirectory {
170170
let build_dir = self.build_dir();
171171
if build_dir.exists() {
172172
remove_dir_all(&build_dir)
173-
.map_err(|error| crate::utils::improve_remove_dir_error(error, &build_dir))?;
173+
.map_err(|error| crate::utils::improve_remove_error(error, &build_dir))?;
174174
}
175175
Ok(())
176176
}

src/crates/cratesio.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ impl CrateTrait for CratesIOCrate {
6262
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
6363
let path = self.cache_path(workspace);
6464
if path.exists() {
65-
std::fs::remove_file(&path)?;
65+
std::fs::remove_file(&path)
66+
.map_err(|error| crate::utils::improve_remove_error(error, &path))?;
6667
}
6768
Ok(())
6869
}

src/crates/git.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl CrateTrait for GitRepo {
122122
let path = self.cached_path(workspace);
123123
if path.exists() {
124124
remove_dir_all::remove_dir_all(&path)
125-
.map_err(|error| crate::utils::improve_remove_dir_error(error, &path))?;
125+
.map_err(|error| crate::utils::improve_remove_error(error, &path))?;
126126
}
127127
Ok(())
128128
}

src/crates/local.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ mod tests {
145145
println!("{} should cause copy to fail", bad_link.display());
146146
assert_copy_err_has_filename();
147147

148-
fs::remove_file(&bad_link)?;
148+
fs::remove_file(&bad_link)
149+
.map_err(|error| crate::utils::improve_remove_error(error, &bad_link))?;
149150
// make sure it works without that link
150151
super::copy_dir(tmp_src.path(), tmp_dest.path())?;
151152

src/crates/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Crate {
7070
dest.display()
7171
);
7272
remove_dir_all(dest)
73-
.map_err(|error| crate::utils::improve_remove_dir_error(error, dest))?;
73+
.map_err(|error| crate::utils::improve_remove_error(error, dest))?;
7474
}
7575
self.as_trait().copy_source_to(workspace, dest)
7676
}

src/prepare.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ impl<'a> Prepare<'a> {
7373
fn remove_cargo_config(&self) -> Result<(), Error> {
7474
let path = self.source_dir.join(".cargo").join("config");
7575
if path.exists() {
76-
remove_file(path.clone())?;
77-
info!("removed {}", path.as_path().display());
76+
remove_file(&path).map_err(|error| crate::utils::improve_remove_error(error, &path))?;
77+
info!("removed {}", path.display());
7878
}
7979
Ok(())
8080
}

src/utils.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,50 +51,50 @@ fn strip_verbatim_from_prefix(prefix: &PrefixComponent<'_>) -> Option<PathBuf> {
5151
}
5252

5353
#[derive(Debug)]
54-
struct DirRemoveError {
54+
struct RemoveError {
5555
kind: std::io::ErrorKind,
5656
path: String,
5757
}
5858

59-
impl std::error::Error for DirRemoveError {}
59+
impl std::error::Error for RemoveError {}
6060

61-
impl std::fmt::Display for DirRemoveError {
61+
impl std::fmt::Display for RemoveError {
6262
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6363
f.write_fmt(format_args!(
64-
"failed to remove directory '{}' : {:?}",
64+
"failed to remove '{}' : {:?}",
6565
self.path, self.kind
6666
))
6767
}
6868
}
6969

70-
pub(crate) fn improve_remove_dir_error(error: std::io::Error, path: &Path) -> std::io::Error {
70+
pub(crate) fn improve_remove_error(error: std::io::Error, path: &Path) -> std::io::Error {
7171
std::io::Error::new(
7272
error.kind(),
73-
DirRemoveError {
73+
RemoveError {
7474
kind: error.kind(),
7575
path: path.display().to_string(),
7676
},
7777
)
7878
}
7979

8080
#[test]
81-
fn custom_remove_dir_error() {
81+
fn custom_remove_error() {
8282
let path = "test/path".as_ref();
8383

84-
let expected = "failed to remove directory 'test/path' : PermissionDenied";
84+
let expected = "failed to remove 'test/path' : PermissionDenied";
8585
let tested = format!(
8686
"{}",
87-
improve_remove_dir_error(
87+
improve_remove_error(
8888
std::io::Error::from(std::io::ErrorKind::PermissionDenied),
8989
path
9090
)
9191
);
9292
assert_eq!(expected, tested);
9393

94-
let expected = "Custom { kind: PermissionDenied, error: DirRemoveError { kind: PermissionDenied, path: \"test/path\" } }";
94+
let expected = "Custom { kind: PermissionDenied, error: RemoveError { kind: PermissionDenied, path: \"test/path\" } }";
9595
let tested = format!(
9696
"{:?}",
97-
improve_remove_dir_error(
97+
improve_remove_error(
9898
std::io::Error::from(std::io::ErrorKind::PermissionDenied),
9999
path
100100
)

src/workspace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl Workspace {
214214
let dir = self.builds_dir();
215215
if dir.exists() {
216216
remove_dir_all(&dir)
217-
.map_err(|error| crate::utils::improve_remove_dir_error(error, &dir))?;
217+
.map_err(|error| crate::utils::improve_remove_error(error, &dir))?;
218218
}
219219
Ok(())
220220
}
@@ -238,7 +238,7 @@ impl Workspace {
238238
for path in &paths {
239239
if path.exists() {
240240
remove_dir_all(&path)
241-
.map_err(|error| crate::utils::improve_remove_dir_error(error, &path))?;
241+
.map_err(|error| crate::utils::improve_remove_error(error, &path))?;
242242
}
243243
}
244244

0 commit comments

Comments
 (0)