Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rm: add verbose output and trim multiple slashes #1988

Merged
merged 16 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::collections::VecDeque;
use std::fs;
use std::io::{stderr, stdin, BufRead, Write};
use std::ops::BitOr;
use std::path::Path;
use std::path::{Component, Path, PathBuf};
use walkdir::{DirEntry, WalkDir};

#[derive(Eq, PartialEq, Clone, Copy)]
Expand Down Expand Up @@ -251,7 +251,7 @@ fn handle_dir(path: &Path, options: &Options) -> bool {

let is_root = path.has_root() && path.parent().is_none();
if options.recursive && (!is_root || !options.preserve_root) {
if options.interactive != InteractiveMode::Always {
if options.interactive != InteractiveMode::Always && !options.verbose {
// we need the extra crate because apparently fs::remove_dir_all() does not function
// correctly on Windows
if let Err(e) = remove_dir_all(path) {
Expand Down Expand Up @@ -311,7 +311,7 @@ fn remove_dir(path: &Path, options: &Options) -> bool {
match fs::remove_dir(path) {
Ok(_) => {
if options.verbose {
println!("removed directory '{}'", path.display());
println!("removed directory '{}'", unify(path).display());
}
}
Err(e) => {
Expand Down Expand Up @@ -349,7 +349,7 @@ fn remove_file(path: &Path, options: &Options) -> bool {
match fs::remove_file(path) {
Ok(_) => {
if options.verbose {
println!("removed '{}'", path.display());
println!("removed '{}'", unify(path).display());
}
}
Err(e) => {
Expand All @@ -370,6 +370,45 @@ fn prompt_file(path: &Path, is_dir: bool) -> bool {
}
}

// copied from https://github.com/rust-lang/cargo/blob/2e4cfc2b7d43328b207879228a2ca7d427d188bb/src/cargo/util/paths.rs#L65-L90
// both projects are MIT https://github.com/rust-lang/cargo/blob/master/LICENSE-MIT
// for std impl progress see rfc https://github.com/rust-lang/rfcs/issues/2208
// replace this once that lands
pub fn normalize_path(path: &Path) -> PathBuf {
marvin-bitterlich marked this conversation as resolved.
Show resolved Hide resolved
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
PathBuf::from(c.as_os_str())
} else {
PathBuf::new()
};

for component in components {
match component {
Component::Prefix(..) => unreachable!(),
Component::RootDir => {
ret.push(component.as_os_str());
}
Component::CurDir => {}
Component::ParentDir => {
ret.pop();
}
Component::Normal(c) => {
ret.push(c);
}
}
}
ret
}

fn unify(path: &Path) -> PathBuf {
// copied from https://github.com/rust-lang/cargo/blob/2e4cfc2b7d43328b207879228a2ca7d427d188bb/src/cargo/util/paths.rs#L65-L90
// both projects are MIT https://github.com/rust-lang/cargo/blob/master/LICENSE-MIT
// for std impl progress see rfc https://github.com/rust-lang/rfcs/issues/2208
// TODO: replace this once that lands
normalize_path(path)
}

fn prompt(msg: &str) -> bool {
let _ = stderr().write_all(msg.as_bytes());
let _ = stderr().flush();
Expand Down
30 changes: 30 additions & 0 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,33 @@ fn test_rm_no_operand() {
ucmd.fails()
.stderr_is("rm: error: missing an argument\nrm: error: for help, try 'rm --help'\n");
}

#[test]
fn test_rm_verbose_slash() {
let (at, mut ucmd) = at_and_ucmd!();
let dir = "test_rm_verbose_slash_directory";
let file_a = &format!("{}/test_rm_verbose_slash_file_a", dir);

at.mkdir(dir);
marvin-bitterlich marked this conversation as resolved.
Show resolved Hide resolved
at.touch(file_a);

let separator = if cfg!(windows) {
"\\"
} else {
"/"
};
let file_a_normalized = &format!("{}{}test_rm_verbose_slash_file_a", dir, separator);

ucmd.arg("-r")
.arg("-f")
.arg("-v")
.arg(&format!("{}///", dir))
.succeeds()
.stdout_only(format!(
"removed '{}'\nremoved directory '{}'\n",
file_a_normalized, dir
));

assert!(!at.dir_exists(dir));
assert!(!at.file_exists(file_a));
}