Skip to content

Commit

Permalink
remove_path is idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Mar 16, 2022
1 parent 11ab785 commit b921a60
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.2.1

- `Shell::remove_path` returns `Ok` if the path does not exist (ie the function
is now idempotent).

## 0.2.0

A major release with significant changes to the API:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "xshell"
description = "Utilities for quick shell scripting in Rust"
categories = ["development-tools::build-utils", "filesystem"]
version = "0.2.0" # also update xshell-macros/Cargo.toml and CHANGELOG.md
version = "0.2.1" # also update xshell-macros/Cargo.toml and CHANGELOG.md
license = "MIT OR Apache-2.0"
repository = "https://github.com/matklad/xshell"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
Expand All @@ -14,7 +14,7 @@ exclude = [".github/", "bors.toml", "rustfmt.toml", "cbench", "mock_bin/"]
[workspace]

[dependencies]
xshell-macros = { version = "=0.2.0", path = "./xshell-macros" }
xshell-macros = { version = "=0.2.1", path = "./xshell-macros" }

[dev-dependencies]
anyhow = "1.0.56"
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ use std::{
env::{self, current_dir, VarError},
ffi::{OsStr, OsString},
fmt, fs,
io::{self, Write},
io::{self, ErrorKind, Write},
mem,
path::{Path, PathBuf},
process::{Command, ExitStatus, Output, Stdio},
Expand Down Expand Up @@ -634,8 +634,12 @@ impl Shell {
}
fn _remove_path(&self, path: &Path) -> Result<(), Error> {
let path = self.path(path);
if path.is_file() { fs::remove_file(&path) } else { remove_dir_all(&path) }
.map_err(|err| Error::new_remove_path(err, path))
match path.metadata() {
Ok(meta) => if meta.is_dir() { remove_dir_all(&path) } else { fs::remove_file(&path) }
.map_err(|err| Error::new_remove_path(err, path)),
Err(err) if err.kind() == ErrorKind::NotFound => Ok(()),
Err(err) => Err(Error::new_remove_path(err, path)),
}
}
// endregion:fs

Expand Down
17 changes: 15 additions & 2 deletions tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod tidy;
mod env;
mod compile_failures;

use std::ffi::OsStr;
use std::{ffi::OsStr, path::Path};

use xshell::{cmd, Shell};

Expand Down Expand Up @@ -364,7 +364,7 @@ fn test_read_with_ignore() {
}

#[test]
fn test_cp() {
fn test_copy_file() {
let sh = setup();

let path;
Expand Down Expand Up @@ -397,6 +397,19 @@ fn write_makes_directory() {
assert!(folder.exists());
}

#[test]
fn test_remove_path() {
let sh = setup();

let tempdir = sh.create_temp_dir().unwrap();
sh.change_dir(tempdir.path());
sh.write_file(Path::new("a/b/c.rs"), "fn main() {}").unwrap();
assert!(tempdir.path().join("a/b/c.rs").exists());
sh.remove_path("./a").unwrap();
assert!(!tempdir.path().join("a/b/c.rs").exists());
sh.remove_path("./a").unwrap();
}

#[test]
fn recovers_from_panics() {
let sh = setup();
Expand Down
2 changes: 1 addition & 1 deletion xshell-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "xshell-macros"
description = "Private implementation detail of xshell crate"
version = "0.2.0"
version = "0.2.1"
license = "MIT OR Apache-2.0"
repository = "https://github.com/matklad/xshell"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
Expand Down

0 comments on commit b921a60

Please sign in to comment.