From b8d5602655c4cebe8d304bedf053fff2331477a6 Mon Sep 17 00:00:00 2001 From: Remi Rampin Date: Tue, 30 Jun 2015 13:22:08 -0400 Subject: [PATCH 1/2] Implement mv's --strip-trailing-slashes --- src/mv/mv.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/mv/mv.rs b/src/mv/mv.rs index cabb0ec0a..e9d676ef3 100644 --- a/src/mv/mv.rs +++ b/src/mv/mv.rs @@ -18,7 +18,7 @@ extern crate libc; use std::fs::{self, PathExt}; use std::io::{BufRead, BufReader, Result, stdin, Write}; use std::os::unix::fs::MetadataExt; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; #[path = "../common/util.rs"] #[macro_use] @@ -60,9 +60,8 @@ pub fn uumain(args: Vec) -> i32 { opts.optflag("f", "force", "do not prompt before overwriting"); opts.optflag("i", "interactive", "prompt before override"); opts.optflag("n", "no-clobber", "do not overwrite an existing file"); - // I have yet to find a use-case (and thereby write a test) where this option is useful. - //opts.optflag("", "strip-trailing-slashes", "remove any trailing slashes from each SOURCE\n \ - // argument"); + opts.optflag("", "strip-trailing-slashes", "remove any trailing slashes from each SOURCE\n \ + argument"); opts.optopt("S", "suffix", "override the usual backup suffix", "SUFFIX"); opts.optopt("t", "target-directory", "move all SOURCE arguments into DIRECTORY", "DIRECTORY"); opts.optflag("T", "no-target-directory", "treat DEST as a normal file"); @@ -150,8 +149,21 @@ pub fn uumain(args: Vec) -> i32 { verbose: matches.opt_present("v"), }; - let string_to_path = |s: &String| { PathBuf::from(s) }; - let paths: Vec = matches.free.iter().map(string_to_path).collect(); + let paths: Vec = { + fn string_to_path<'a>(s: &'a String) -> &'a Path { + Path::new(s) + }; + fn strip_slashes<'a>(p: &'a Path) -> &'a Path { + p.components().as_path() + } + let to_owned = |p: &Path| p.to_owned(); + let arguments = matches.free.iter().map(string_to_path); + if matches.opt_present("strip-trailing-slashes") { + arguments.map(strip_slashes).map(to_owned).collect() + } else { + arguments.map(to_owned).collect() + } + }; if matches.opt_present("version") { println!("{} {}", NAME, VERSION); From 85cb9aca94a781e8de638e529f84385577300f71 Mon Sep 17 00:00:00 2001 From: Remi Rampin Date: Tue, 30 Jun 2015 23:17:16 -0400 Subject: [PATCH 2/2] Add a test --- test/mv.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/mv.rs b/test/mv.rs index ad3df4e82..d99ff19ae 100644 --- a/test/mv.rs +++ b/test/mv.rs @@ -57,6 +57,28 @@ fn test_mv_move_file_into_dir() { assert!(Path::new(&format!("{}/{}", dir, file)).is_file()); } +#[test] +fn test_mv_strip_slashes() { + let dir = "test_mv_strip_slashes_dir"; + let file = "test_mv_strip_slashes_file"; + let mut source = file.to_owned(); + source.push_str("/"); + + mkdir(dir); + touch(file); + + let result = run(Command::new(PROGNAME).arg(&source).arg(dir)); + assert!(!result.success); + + assert!(!Path::new(&format!("{}/{}", dir, file)).is_file()); + + let result = run(Command::new(PROGNAME).arg("--strip-trailing-slashes").arg(source).arg(dir)); + assert_empty_stderr!(result); + assert!(result.success); + + assert!(Path::new(&format!("{}/{}", dir, file)).is_file()); +} + #[test] fn test_mv_multiple_files() { let target_dir = "test_mv_multiple_files_dir";