From 3b5c776675b2d87021b0774cc939a5caa19db655 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Sun, 1 Nov 2015 15:31:48 -0500 Subject: [PATCH] Add tests for rmdir. I also adjusted error message to conform to GNU implementation. --- Makefile | 1 + src/rmdir/rmdir.rs | 2 +- test/common/util.rs | 4 ++ test/rmdir.rs | 114 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 test/rmdir.rs diff --git a/Makefile b/Makefile index 061a1476d..c7016e634 100644 --- a/Makefile +++ b/Makefile @@ -187,6 +187,7 @@ TEST_PROGS := \ readlink \ realpath \ rm \ + rmdir \ seq \ sort \ split \ diff --git a/src/rmdir/rmdir.rs b/src/rmdir/rmdir.rs index 467a4a76d..56df54c3c 100644 --- a/src/rmdir/rmdir.rs +++ b/src/rmdir/rmdir.rs @@ -118,7 +118,7 @@ fn remove_dir(path: &Path, ignore: bool, verbose: bool) -> Result<(), i32> { _ => (), } } else if !ignore { - show_error!("failed to remove '{}' Directory not empty", path.display()); + show_error!("failed to remove '{}': Directory not empty", path.display()); r = Err(1); } diff --git a/test/common/util.rs b/test/common/util.rs index f36d5465c..8d62fab5b 100644 --- a/test/common/util.rs +++ b/test/common/util.rs @@ -68,6 +68,10 @@ pub fn mkdir(dir: &str) { fs::create_dir(Path::new(dir)).unwrap(); } +pub fn mkdir_all(dir: &str) { + fs::create_dir_all(Path::new(dir)).unwrap(); +} + pub fn make_file(name: &str) -> File { match File::create(Path::new(name)) { Ok(f) => f, diff --git a/test/rmdir.rs b/test/rmdir.rs new file mode 100644 index 000000000..d85265d74 --- /dev/null +++ b/test/rmdir.rs @@ -0,0 +1,114 @@ +extern crate libc; + +use std::process::Command; +use util::*; + +static PROGNAME: &'static str = "./rmdir"; + +#[path = "common/util.rs"] +#[macro_use] +mod util; + +#[test] +fn test_rmdir_empty_directory_no_parents() { + let dir = "test_rmdir_empty_no_parents"; + + mkdir(dir); + assert!(dir_exists(dir)); + + let result = run(Command::new(PROGNAME).arg(dir)); + assert_empty_stderr!(result); + assert!(result.success); + + assert!(!dir_exists(dir)); +} + +#[test] +fn test_rmdir_empty_directory_with_parents() { + let dir = "test_rmdir_empty/with/parents"; + + mkdir_all(dir); + assert!(dir_exists(dir)); + + let result = run(Command::new(PROGNAME).arg("-p").arg(dir)); + assert_empty_stderr!(result); + assert!(result.success); + + assert!(!dir_exists(dir)); +} + +#[test] +fn test_rmdir_nonempty_directory_no_parents() { + let dir = "test_rmdir_nonempty_no_parents"; + let file = "test_rmdir_nonempty_no_parents/foo"; + + mkdir(dir); + assert!(dir_exists(dir)); + + touch(file); + assert!(file_exists(file)); + + let result = run(Command::new(PROGNAME).arg(dir)); + assert_eq!(result.stderr, + "rmdir: error: failed to remove 'test_rmdir_nonempty_no_parents': Directory not empty\n"); + assert!(!result.success); + + assert!(dir_exists(dir)); +} + +#[test] +fn test_rmdir_nonempty_directory_with_parents() { + let dir = "test_rmdir_nonempty/with/parents"; + let file = "test_rmdir_nonempty/with/parents/foo"; + + mkdir_all(dir); + assert!(dir_exists(dir)); + + touch(file); + assert!(file_exists(file)); + + let result = run(Command::new(PROGNAME).arg("-p").arg(dir)); + assert_eq!(result.stderr, + "rmdir: error: failed to remove 'test_rmdir_nonempty/with/parents': Directory not empty\n\ + rmdir: error: failed to remove 'test_rmdir_nonempty/with': Directory not empty\n\ + rmdir: error: failed to remove 'test_rmdir_nonempty': Directory not empty\n"); + assert!(!result.success); + + assert!(dir_exists(dir)); +} + +#[test] +fn test_rmdir_ignore_nonempty_directory_no_parents() { + let dir = "test_rmdir_ignore_nonempty_no_parents"; + let file = "test_rmdir_ignore_nonempty_no_parents/foo"; + + mkdir(dir); + assert!(dir_exists(dir)); + + touch(file); + assert!(file_exists(file)); + + let result = run(Command::new(PROGNAME).arg("--ignore-fail-on-non-empty").arg(dir)); + assert_empty_stderr!(result); + assert!(result.success); + + assert!(dir_exists(dir)); +} + +#[test] +fn test_rmdir_ignore_nonempty_directory_with_parents() { + let dir = "test_rmdir_ignore_nonempty/with/parents"; + let file = "test_rmdir_ignore_nonempty/with/parents/foo"; + + mkdir_all(dir); + assert!(dir_exists(dir)); + + touch(file); + assert!(file_exists(file)); + + let result = run(Command::new(PROGNAME).arg("--ignore-fail-on-non-empty").arg("-p").arg(dir)); + assert_empty_stderr!(result); + assert!(result.success); + + assert!(dir_exists(dir)); +}