Fix bugs in FakeFs::{remove_dir,rename}

This commit is contained in:
Antonio Scandurra 2022-05-05 09:02:31 +02:00
parent 4b1c46fa45
commit ecb847a027

View file

@ -493,7 +493,7 @@ impl Fs for FakeFs {
}); });
for (relative_path, entry) in removed { for (relative_path, entry) in removed {
let new_path = target.join(relative_path); let new_path = normalize_path(&target.join(relative_path));
state.entries.insert(new_path, entry); state.entries.insert(new_path, entry);
} }
@ -501,13 +501,15 @@ impl Fs for FakeFs {
Ok(()) Ok(())
} }
async fn remove_dir(&self, path: &Path, options: RemoveOptions) -> Result<()> { async fn remove_dir(&self, dir_path: &Path, options: RemoveOptions) -> Result<()> {
let path = normalize_path(path); let dir_path = normalize_path(dir_path);
let mut state = self.state.lock().await; let mut state = self.state.lock().await;
state.validate_path(&path)?; state.validate_path(&dir_path)?;
if let Some(entry) = state.entries.get(&path) { if let Some(entry) = state.entries.get(&dir_path) {
if !entry.metadata.is_dir { if !entry.metadata.is_dir {
return Err(anyhow!("cannot remove {path:?} because it is not a dir")); return Err(anyhow!(
"cannot remove {dir_path:?} because it is not a dir"
));
} }
if !options.recursive { if !options.recursive {
@ -517,14 +519,14 @@ impl Fs for FakeFs {
.filter(|path| path.starts_with(path)) .filter(|path| path.starts_with(path))
.count(); .count();
if descendants > 1 { if descendants > 1 {
return Err(anyhow!("{path:?} is not empty")); return Err(anyhow!("{dir_path:?} is not empty"));
} }
} }
state.entries.retain(|path, _| !path.starts_with(path)); state.entries.retain(|path, _| !path.starts_with(&dir_path));
state.emit_event(&[path]).await; state.emit_event(&[dir_path]).await;
} else if !options.ignore_if_not_exists { } else if !options.ignore_if_not_exists {
return Err(anyhow!("{path:?} does not exist")); return Err(anyhow!("{dir_path:?} does not exist"));
} }
Ok(()) Ok(())