1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

Add tests for unlink.

This commit is contained in:
Joseph Crail 2015-05-15 20:11:54 -04:00
parent 497c04ad44
commit 906ba3e24a
2 changed files with 92 additions and 0 deletions

View file

@ -182,6 +182,7 @@ TEST_PROGS := \
true \
truncate \
tsort \
unlink \
unexpand
TEST ?= $(TEST_PROGS)

91
test/unlink.rs Normal file
View file

@ -0,0 +1,91 @@
#![feature(path_ext)]
#![allow(dead_code)]
extern crate libc;
use std::fs::{self, File, PathExt};
use std::path::Path;
use std::process::Command;
use std::str::from_utf8;
static PROGNAME: &'static str = "./unlink";
macro_rules! assert_empty_stderr(
($cond:expr) => (
if $cond.stderr.len() > 0 {
panic!(format!("stderr: {}", $cond.stderr))
}
);
);
struct CmdResult {
success: bool,
stderr: String,
stdout: String,
}
fn run(cmd: &mut Command) -> CmdResult {
let prog = cmd.output().unwrap();
CmdResult {
success: prog.status.success(),
stderr: from_utf8(&prog.stderr).unwrap().to_string(),
stdout: from_utf8(&prog.stdout).unwrap().to_string(),
}
}
fn mkdir(dir: &str) {
fs::create_dir(dir).unwrap();
}
fn touch(file: &str) {
File::create(file).unwrap();
}
#[test]
fn test_unlink_file() {
let file = "test_unlink_file";
touch(file);
let result = run(Command::new(PROGNAME).arg(file));
assert_empty_stderr!(result);
assert!(result.success);
assert!(!Path::new(file).exists());
}
#[test]
fn test_unlink_multiple_files() {
let file_a = "test_unlink_multiple_file_a";
let file_b = "test_unlink_multiple_file_b";
touch(file_a);
touch(file_b);
let result = run(Command::new(PROGNAME).arg(file_a).arg(file_b));
assert_eq!(result.stderr,
"unlink: error: extra operand: 'test_unlink_multiple_file_b'\nTry './unlink --help' for more information.\n");
assert!(!result.success);
}
#[test]
fn test_unlink_directory() {
let dir = "test_unlink_empty_directory";
mkdir(dir);
let result = run(Command::new(PROGNAME).arg(dir));
assert_eq!(result.stderr,
"unlink: error: cannot unlink 'test_unlink_empty_directory': Not a regular file or symlink\n");
assert!(!result.success);
}
#[test]
fn test_unlink_nonexistent() {
let file = "test_unlink_nonexistent";
let result = run(Command::new(PROGNAME).arg(file));
assert_eq!(result.stderr,
"unlink: error: Cannot stat 'test_unlink_nonexistent': No such file or directory (os error 2)\n");
assert!(!result.success);
}