From eb2415fb9022071fe3baadf360cf269052e2d98d Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Mon, 13 Oct 2014 21:26:14 +0100 Subject: [PATCH] Introduce a test for `cp'. --- Makefile | 1 + test/cp.rs | 37 ++++++++++++++++++++++++++++++++ test/fixtures/cp/hello_world.txt | 1 + 3 files changed, 39 insertions(+) create mode 100644 test/cp.rs create mode 100644 test/fixtures/cp/hello_world.txt diff --git a/Makefile b/Makefile index 646446340..0497c4495 100644 --- a/Makefile +++ b/Makefile @@ -129,6 +129,7 @@ INSTALLEES := \ # Programs with usable tests TEST_PROGS := \ cat \ + cp \ mkdir \ nl \ seq \ diff --git a/test/cp.rs b/test/cp.rs new file mode 100644 index 000000000..889ec867b --- /dev/null +++ b/test/cp.rs @@ -0,0 +1,37 @@ +use std::io::process::Command; +use std::io::File; +use std::io::fs::{unlink, PathExtensions}; + +static EXE: &'static str = "./cp"; +static TEST_HELLO_WORLD_SOURCE: &'static str = "hello_world.txt"; +static TEST_HELLO_WORLD_DEST: &'static str = "copy_of_hello_world.txt"; + +fn cleanup(filename: &'static str) { + let path = Path::new(filename); + if path.exists() { + unlink(&path).unwrap(); + } +} + + +#[test] +fn test_cp_cp() { + // Invoke our binary to make the copy. + let prog = Command::new(EXE) + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HELLO_WORLD_DEST) + .status(); + + // Check that the exit code represents a successful copy. + let exit_success = prog.unwrap().success(); + assert_eq!(exit_success, true); + + // Check the content of the destination file that was copied. + let contents = File::open(&Path::new(TEST_HELLO_WORLD_DEST)) + .read_to_string() + .unwrap(); + assert_eq!(contents.as_slice(), "Hello, World!\n"); + + cleanup(TEST_HELLO_WORLD_SOURCE); + cleanup(TEST_HELLO_WORLD_DEST); +} diff --git a/test/fixtures/cp/hello_world.txt b/test/fixtures/cp/hello_world.txt new file mode 100644 index 000000000..8ab686eaf --- /dev/null +++ b/test/fixtures/cp/hello_world.txt @@ -0,0 +1 @@ +Hello, World!