1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Add tests for rmdir.

I also adjusted error message to conform to GNU implementation.
This commit is contained in:
Joseph Crail 2015-11-01 15:31:48 -05:00
parent 510b024d0f
commit 3b5c776675
4 changed files with 120 additions and 1 deletions

View file

@ -187,6 +187,7 @@ TEST_PROGS := \
readlink \
realpath \
rm \
rmdir \
seq \
sort \
split \

View file

@ -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);
}

View file

@ -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,

114
test/rmdir.rs Normal file
View file

@ -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));
}