From 365b3427920c250a9614208b02f5586fff644df5 Mon Sep 17 00:00:00 2001 From: modelorganism Date: Mon, 25 Apr 2016 22:06:38 -0500 Subject: [PATCH] od: create first tests for od --- Makefile | 1 + tests/od.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 tests/od.rs diff --git a/Makefile b/Makefile index 19bc50449..09190f2eb 100644 --- a/Makefile +++ b/Makefile @@ -149,6 +149,7 @@ TEST_PROGS := \ mktemp \ mv \ nl \ + od \ paste \ printf \ ptx \ diff --git a/tests/od.rs b/tests/od.rs new file mode 100644 index 000000000..86211093f --- /dev/null +++ b/tests/od.rs @@ -0,0 +1,87 @@ +#[macro_use] +mod common; + +use common::util::*; +use std::path::Path; +use std::env; +use std::io::Write; +use std::fs::File; +use std::fs::remove_file; + +static UTIL_NAME: &'static str = "od"; + +// octal dump of 'abcdefghijklmnopqrstuvwxyz\n' +static ALPHA_OUT: &'static str = "0000000 061141 062143 063145 064147 065151 066153 067155 070157\n0000020 071161 072163 073165 074167 075171 000012 \n0000033\n"; + +// XXX We could do a better job of ensuring that we have a fresh temp dir to ourself, +// not a general one ful of other proc's leftovers. + +// Test that od can read one file and dump with default format +#[test] +fn test_file() { + let (_, mut ucmd) = testing(UTIL_NAME); + let temp = env::var("TMPDIR").unwrap_or_else(|_| env::var("TEMP").unwrap()); + let tmpdir = Path::new(&temp); + let file = tmpdir.join("test"); + + { + let mut f = File::create(&file).unwrap(); + match f.write_all(b"abcdefghijklmnopqrstuvwxyz\n") { + Err(_) => panic!("Test setup failed - could not write file"), + _ => {} + } + } + + let result = ucmd.arg(file.as_os_str()).run(); + + assert_empty_stderr!(result); + assert!(result.success); + assert_eq!(result.stdout, ALPHA_OUT); + + let _ = remove_file(file); +} + +// Test that od can read 2 files and concatenate the contents +#[test] +fn test_2files() { + let (_, mut ucmd) = testing(UTIL_NAME); + let temp = env::var("TMPDIR").unwrap_or_else(|_| env::var("TEMP").unwrap()); + let tmpdir = Path::new(&temp); + let file1 = tmpdir.join("test1"); + let file2 = tmpdir.join("test2"); + + for &(n,a) in [(1,"a"), (2,"b")].iter() { + println!("number: {} letter:{}", n, a); + } + + for &(path,data)in &[(&file1, "abcdefghijklmnop"),(&file2, "qrstuvwxyz\n")] { + let mut f = File::create(&path).unwrap(); + match f.write_all(data.as_bytes()) { + Err(_) => panic!("Test setup failed - could not write file"), + _ => {} + } + } + + let result = ucmd.arg(file1.as_os_str()).arg(file2.as_os_str()).run(); + + assert_empty_stderr!(result); + assert!(result.success); + assert_eq!(result.stdout, ALPHA_OUT); + + let _ = remove_file(file1); + let _ = remove_file(file2); +} + +// Test that od gives non-0 exit val for filename that dosen't exist. +#[test] +fn test_no_file() { + let (_, mut ucmd) = testing(UTIL_NAME); + let temp = env::var("TMPDIR").unwrap_or_else(|_| env::var("TEMP").unwrap()); + let tmpdir = Path::new(&temp); + let file = tmpdir.join("}surely'none'would'thus'a'file'name"); + + let result = ucmd.arg(file.as_os_str()).run(); + + assert!(!result.success); + +} \ No newline at end of file