From edb3295303ab3b19489554ce8277da439ceaf422 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Fri, 29 May 2015 13:47:00 -0400 Subject: [PATCH] Move helper methods for tests to separate module. --- test/base64.rs | 43 +++------------------ test/common/util.rs | 87 +++++++++++++++++++++++++++++++++++++++++++ test/cp.rs | 21 ++++------- test/hashsum.rs | 70 +++++----------------------------- test/mkdir.rs | 45 ++++++++-------------- test/mv.rs | 65 ++++---------------------------- test/nl.rs | 59 ++++++++++------------------- test/rm.rs | 66 ++++---------------------------- test/split.rs | 12 +++--- test/test.rs | 8 ++-- test/tr.rs | 48 ++++++++++-------------- test/truncate.rs | 16 ++++---- test/tsort.rs | 15 +++----- test/unexpand.rs | 91 +++++++++++++++++++++++---------------------- test/unlink.rs | 40 +++----------------- 15 files changed, 255 insertions(+), 431 deletions(-) create mode 100644 test/common/util.rs diff --git a/test/base64.rs b/test/base64.rs index 3da7b54ac..106a42de5 100644 --- a/test/base64.rs +++ b/test/base64.rs @@ -1,44 +1,11 @@ -use std::io::{Read, Write}; -use std::process::{Command, Stdio}; -use std::str::from_utf8; +use std::process::Command; +use util::*; static PROGNAME: &'static str = "./base64"; -struct CmdResult { - success: bool, - stdout: String, - stderr: String, -} - -fn run_piped_stdin(cmd: &mut Command, input: &[u8])-> CmdResult { - let mut command = cmd - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .unwrap(); - - command.stdin - .take() - .unwrap_or_else(|| panic!("Could not take child process stdin")) - .write_all(input) - .unwrap_or_else(|e| panic!("{}", e)); - - let prog = command.wait_with_output().unwrap(); - CmdResult { - success: prog.status.success(), - stdout: from_utf8(&prog.stdout).unwrap().to_string(), - stderr: from_utf8(&prog.stderr).unwrap().to_string(), - } -} - -macro_rules! assert_empty_stderr( - ($cond:expr) => ( - if $cond.stderr.len() > 0 { - panic!(format!("stderr: {}", $cond.stderr)) - } - ); -); +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_encode() { diff --git a/test/common/util.rs b/test/common/util.rs new file mode 100644 index 000000000..f13c8f6f6 --- /dev/null +++ b/test/common/util.rs @@ -0,0 +1,87 @@ +#![allow(dead_code)] + +use std::fs::{self, File}; +use std::io::{Read, Write}; +use std::path::Path; +use std::process::{Command, Stdio}; +use std::str::from_utf8; + +#[macro_export] +macro_rules! assert_empty_stderr( + ($cond:expr) => ( + if $cond.stderr.len() > 0 { + panic!(format!("stderr: {}", $cond.stderr)) + } + ); +); + +pub struct CmdResult { + pub success: bool, + pub stdout: String, + pub stderr: String, +} + +pub fn run(cmd: &mut Command) -> CmdResult { + let prog = cmd.output().unwrap(); + CmdResult { + success: prog.status.success(), + stdout: from_utf8(&prog.stdout).unwrap().to_string(), + stderr: from_utf8(&prog.stderr).unwrap().to_string(), + } +} + +pub fn run_piped_stdin(cmd: &mut Command, input: &[u8])-> CmdResult { + let mut command = cmd + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + + command.stdin + .take() + .unwrap_or_else(|| panic!("Could not take child process stdin")) + .write_all(input) + .unwrap_or_else(|e| panic!("{}", e)); + + let prog = command.wait_with_output().unwrap(); + CmdResult { + success: prog.status.success(), + stdout: from_utf8(&prog.stdout).unwrap().to_string(), + stderr: from_utf8(&prog.stderr).unwrap().to_string(), + } +} + +pub fn get_file_contents(name: &str) -> String { + let mut f = File::open(Path::new(name)).unwrap(); + let mut contents = String::new(); + let _ = f.read_to_string(&mut contents); + contents +} + +pub fn mkdir(dir: &str) { + fs::create_dir(Path::new(dir)).unwrap(); +} + +pub fn make_file(name: &str) -> File { + match File::create(Path::new(name)) { + Ok(f) => f, + Err(e) => panic!("{}", e) + } +} + +pub fn touch(file: &str) { + File::create(Path::new(file)).unwrap(); +} + +pub fn cleanup(path: &'static str) { + let p = Path::new(path); + match fs::metadata(p) { + Ok(m) => if m.is_file() { + fs::remove_file(&p).unwrap(); + } else { + fs::remove_dir(&p).unwrap(); + }, + Err(_) => {} + } +} diff --git a/test/cp.rs b/test/cp.rs index e9f11bbed..d82c9af49 100644 --- a/test/cp.rs +++ b/test/cp.rs @@ -1,26 +1,21 @@ -#![feature(path_ext)] - -use std::fs::{File, PathExt, remove_file}; +use std::fs::File; use std::io::Read; -use std::path::{Path}; +use std::path::Path; use std::process::Command; +use util::*; -static EXE: &'static str = "./cp"; +static PROGNAME: &'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() { - remove_file(&path).unwrap(); - } -} - +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_cp_cp() { // Invoke our binary to make the copy. - let prog = Command::new(EXE) + let prog = Command::new(PROGNAME) .arg(TEST_HELLO_WORLD_SOURCE) .arg(TEST_HELLO_WORLD_DEST) .status(); diff --git a/test/hashsum.rs b/test/hashsum.rs index c467530da..e729093c2 100644 --- a/test/hashsum.rs +++ b/test/hashsum.rs @@ -1,61 +1,8 @@ -use std::fs::File; -use std::io::{Read, Write}; -use std::process::{Command, Stdio}; -use std::str::from_utf8; - static PROGNAME: &'static str = "./hashsum"; -struct CmdResult { - success: bool, - stdout: String, - stderr: String, -} - -fn run(cmd: &mut Command) -> CmdResult { - let prog = cmd.output().unwrap(); - CmdResult { - success: prog.status.success(), - stdout: from_utf8(&prog.stdout).unwrap().to_string(), - stderr: from_utf8(&prog.stderr).unwrap().to_string(), - } -} - -fn run_piped_stdin(cmd: &mut Command, input: &[u8])-> CmdResult { - let mut command = cmd - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .unwrap(); - - command.stdin - .take() - .unwrap_or_else(|| panic!("Could not take child process stdin")) - .write_all(input) - .unwrap_or_else(|e| panic!("{}", e)); - - let prog = command.wait_with_output().unwrap(); - CmdResult { - success: prog.status.success(), - stdout: from_utf8(&prog.stdout).unwrap().to_string(), - stderr: from_utf8(&prog.stderr).unwrap().to_string(), - } -} - -fn get_file_contents(name: &str) -> String { - let mut f = File::open(name).unwrap(); - let mut contents = String::new(); - let _ = f.read_to_string(&mut contents); - contents -} - -macro_rules! assert_empty_stderr( - ($cond:expr) => ( - if $cond.stderr.len() > 0 { - panic!(format!("stderr: {}", $cond.stderr)) - } - ); -); +#[path = "common/util.rs"] +#[macro_use] +mod util; macro_rules! get_hash( ($str:expr) => ( @@ -68,6 +15,7 @@ macro_rules! test_digest { mod $t { use std::process::Command; + use util::*; static DIGEST_ARG: &'static str = concat!("--", stringify!($t)); static EXPECTED_FILE: &'static str = concat!(stringify!($t), ".expected"); @@ -75,22 +23,22 @@ macro_rules! test_digest { #[test] fn test_single_file() { let mut cmd = Command::new(::PROGNAME); - let result = ::run(&mut cmd.arg(DIGEST_ARG).arg("input.txt")); + let result = run(&mut cmd.arg(DIGEST_ARG).arg("input.txt")); assert_empty_stderr!(result); assert!(result.success); - assert_eq!(get_hash!(result.stdout), ::get_file_contents(EXPECTED_FILE)); + assert_eq!(get_hash!(result.stdout), get_file_contents(EXPECTED_FILE)); } #[test] fn test_stdin() { - let input = ::get_file_contents("input.txt"); + let input = get_file_contents("input.txt"); let mut cmd = Command::new(::PROGNAME); - let result = ::run_piped_stdin(&mut cmd.arg(DIGEST_ARG), input.as_bytes()); + let result = run_piped_stdin(&mut cmd.arg(DIGEST_ARG), input.as_ref()); assert_empty_stderr!(result); assert!(result.success); - assert_eq!(get_hash!(result.stdout), ::get_file_contents(EXPECTED_FILE)); + assert_eq!(get_hash!(result.stdout), get_file_contents(EXPECTED_FILE)); } } )*) diff --git a/test/mkdir.rs b/test/mkdir.rs index 860725d3c..463688dc1 100644 --- a/test/mkdir.rs +++ b/test/mkdir.rs @@ -1,8 +1,5 @@ -#![feature(path_ext)] - -use std::fs::{remove_dir, PathExt}; -use std::path::Path; -use std::process::{Command, Output}; +use std::process::Command; +use util::*; static PROGNAME: &'static str = "./mkdir"; static TEST_DIR1: &'static str = "mkdir_test1"; @@ -11,61 +8,51 @@ static TEST_DIR3: &'static str = "mkdir_test3"; static TEST_DIR4: &'static str = "mkdir_test4/mkdir_test4_1"; static TEST_DIR5: &'static str = "mkdir_test5/mkdir_test5_1"; -fn run(args: &[&'static str]) -> Output { - Command::new(PROGNAME) - .args(args) - .output() - .unwrap_or_else(|e| panic!("{}", e)) -} - -fn cleanup(dir: &'static str) { - let p = Path::new(dir); - if p.exists() { - remove_dir(&p).unwrap(); - } -} +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_mkdir_mkdir() { - cleanup(TEST_DIR1); - let exit_success = run(&[TEST_DIR1]).status.success(); + let mut cmd = Command::new(PROGNAME); + let exit_success = run(&mut cmd.arg(TEST_DIR1)).success; cleanup(TEST_DIR1); assert_eq!(exit_success, true); } #[test] fn test_mkdir_dup_dir() { - cleanup(TEST_DIR2); - let exit_success = run(&[TEST_DIR2]).status.success(); + let mut cmd = Command::new(PROGNAME); + let exit_success = run(&mut cmd.arg(TEST_DIR2)).success; if !exit_success { cleanup(TEST_DIR2); panic!(); } - let exit_success2 = run(&[TEST_DIR2]).status.success(); + let exit_success2 = run(&mut cmd.arg(TEST_DIR2)).success; cleanup(TEST_DIR2); assert_eq!(exit_success2, false); } #[test] fn test_mkdir_mode() { - cleanup(TEST_DIR3); - let exit_success = run(&["-m", "755", TEST_DIR3]).status.success(); + let mut cmd = Command::new(PROGNAME); + let exit_success = run(&mut cmd.arg("-m").arg("755").arg(TEST_DIR3)).success; cleanup(TEST_DIR3); assert_eq!(exit_success, true); } #[test] fn test_mkdir_parent() { - cleanup(TEST_DIR4); - let exit_success = run(&["-p", TEST_DIR4]).status.success(); + let mut cmd = Command::new(PROGNAME); + let exit_success = run(&mut cmd.arg("-p").arg(TEST_DIR4)).success; cleanup(TEST_DIR4); assert_eq!(exit_success, true); } #[test] fn test_mkdir_no_parent() { - cleanup(TEST_DIR5); - let exit_success = run(&[TEST_DIR5]).status.success(); + let mut cmd = Command::new(PROGNAME); + let exit_success = run(&mut cmd.arg(TEST_DIR5)).success; cleanup(TEST_DIR5); assert_eq!(exit_success, false); } diff --git a/test/mv.rs b/test/mv.rs index b074fe300..ad3df4e82 100644 --- a/test/mv.rs +++ b/test/mv.rs @@ -3,65 +3,16 @@ extern crate libc; extern crate time; -use std::fs::{self, File, PathExt}; -use std::io::Write; +use std::fs::{self, PathExt}; use std::path::Path; -use std::process::{Command, Stdio}; -use std::str::from_utf8; +use std::process::Command; +use util::*; static PROGNAME: &'static str = "./mv"; -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 run_interactive(cmd: &mut Command, input: &[u8])-> CmdResult { - let mut command = cmd - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .unwrap(); - - command.stdin - .take() - .unwrap_or_else(|| panic!("Could not take child process stdin")) - .write_all(input) - .unwrap_or_else(|e| panic!("{}", e)); - - let prog = command.wait_with_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(Path::new(dir)).unwrap(); -} - -fn touch(file: &str) { - File::create(Path::new(file)).unwrap(); -} +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_mv_rename_dir() { @@ -151,7 +102,7 @@ fn test_mv_interactive() { touch(file_b); - let result1 = run_interactive(Command::new(PROGNAME).arg("-i").arg(file_a).arg(file_b), b"n"); + let result1 = run_piped_stdin(Command::new(PROGNAME).arg("-i").arg(file_a).arg(file_b), b"n"); assert_empty_stderr!(result1); assert!(result1.success); @@ -160,7 +111,7 @@ fn test_mv_interactive() { assert!(Path::new(file_b).is_file()); - let result2 = run_interactive(Command::new(PROGNAME).arg("-i").arg(file_a).arg(file_b), b"Yesh"); + let result2 = run_piped_stdin(Command::new(PROGNAME).arg("-i").arg(file_a).arg(file_b), b"Yesh"); assert_empty_stderr!(result2); assert!(result2.success); diff --git a/test/nl.rs b/test/nl.rs index 3959fce3d..29626908a 100644 --- a/test/nl.rs +++ b/test/nl.rs @@ -1,57 +1,37 @@ -use std::io::Write; -use std::process::{Command, Stdio}; -use std::str; +use std::process::Command; +use util::*; static PROGNAME: &'static str = "./nl"; -fn run(args: &[&'static str]) -> String { - let po = Command::new(PROGNAME) - .args(args) - .output() - .unwrap_or_else(|e| panic!("{}", e)); - - str::from_utf8(&po.stdout).unwrap().to_string() -} - -fn run_with_stdin(input: &str, args: &[&'static str]) -> String { - let mut process = Command::new(PROGNAME) - .args(args) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .unwrap_or_else(|e| panic!("{}", e)); - - process.stdin - .take() - .unwrap_or_else(|| panic!("Could not take child process stdin")) - .write_all(input.as_bytes()) - .unwrap_or_else(|e| panic!("{}", e)); - - let po = process.wait_with_output().unwrap_or_else(|e| panic!("{}", e)); - str::from_utf8(&po.stdout).unwrap().to_string() -} +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_stdin_nonewline() { - let out = run_with_stdin("No Newline", &[]); - assert_eq!(&out, " 1\tNo Newline\n"); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd, "No Newline".as_bytes()); + assert_eq!(result.stdout, " 1\tNo Newline\n"); } #[test] fn test_stdin_newline() { - let out = run_with_stdin("Line One\nLine Two\n", &["-s", "-", "-w", "1"]); - assert_eq!(&out, "1-Line One\n2-Line Two\n"); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-s", "-", "-w", "1"]), "Line One\nLine Two\n".as_bytes()); + assert_eq!(result.stdout, "1-Line One\n2-Line Two\n"); } #[test] fn test_padding_without_overflow() { - let out = run(&["-i", "1000", "-s", "x", "-n", "rz", "simple.txt"]); - assert_eq!(&out, "000001xL1\n001001xL2\n002001xL3\n003001xL4\n004001xL5\n005001xL6\n006001xL7\n007001xL8\n008001xL9\n009001xL10\n010001xL11\n011001xL12\n012001xL13\n013001xL14\n014001xL15\n"); + let mut cmd = Command::new(PROGNAME); + let result = run(&mut cmd.args(&["-i", "1000", "-s", "x", "-n", "rz", "simple.txt"])); + assert_eq!(result.stdout, "000001xL1\n001001xL2\n002001xL3\n003001xL4\n004001xL5\n005001xL6\n006001xL7\n007001xL8\n008001xL9\n009001xL10\n010001xL11\n011001xL12\n012001xL13\n013001xL14\n014001xL15\n"); } #[test] fn test_padding_with_overflow() { - let out = run(&["-i", "1000", "-s", "x", "-n", "rz", "-w", "4", "simple.txt"]); - assert_eq!(&out, "0001xL1\n1001xL2\n2001xL3\n3001xL4\n4001xL5\n5001xL6\n6001xL7\n7001xL8\n8001xL9\n9001xL10\n10001xL11\n11001xL12\n12001xL13\n13001xL14\n14001xL15\n"); + let mut cmd = Command::new(PROGNAME); + let result = run(&mut cmd.args(&["-i", "1000", "-s", "x", "-n", "rz", "-w", "4", "simple.txt"])); + assert_eq!(result.stdout, "0001xL1\n1001xL2\n2001xL3\n3001xL4\n4001xL5\n5001xL6\n6001xL7\n7001xL8\n8001xL9\n9001xL10\n10001xL11\n11001xL12\n12001xL13\n13001xL14\n14001xL15\n"); } #[test] @@ -66,7 +46,8 @@ fn test_sections_and_styles() { "1 |Nonempty\n2 |Nonempty\n3 |Followed by 10x empty\n\n\n\n\n4 |\n\n\n\n\n5 |\n6 |Followed by 5x empty\n\n\n\n\n7 |\n8 |Followed by 4x empty\n\n\n\n\n9 |Nonempty\n10 |Nonempty\n11 |Nonempty.\n" ), ].iter() { - let out = run(&["-s", "|", "-n", "ln", "-w", "3", "-b", "a", "-l", "5", fixture]); - assert_eq!(&out, output); + let mut cmd = Command::new(PROGNAME); + let result = run(&mut cmd.args(&["-s", "|", "-n", "ln", "-w", "3", "-b", "a", "-l", "5", fixture])); + assert_eq!(result.stdout, output); } } diff --git a/test/rm.rs b/test/rm.rs index fb6fbc727..0a75aec2c 100644 --- a/test/rm.rs +++ b/test/rm.rs @@ -2,66 +2,16 @@ extern crate libc; -use std::fs::{self, File, PathExt}; -use std::io::Write; +use std::fs::PathExt; use std::path::Path; -use std::process::{Command, Stdio}; -use std::str::from_utf8; +use std::process::Command; +use util::*; static PROGNAME: &'static str = "./rm"; -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 run_interactive(cmd: &mut Command, input: &[u8])-> CmdResult { - let mut command = cmd - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .unwrap(); - - command.stdin - .take() - .unwrap_or_else(|| panic!("Could not take child process stdin")) - .write_all(input) - .unwrap_or_else(|e| panic!("{}", e)); - - let prog = command.wait_with_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(); -} +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_rm_one_file() { @@ -100,14 +50,14 @@ fn test_rm_interactive() { touch(file_a); touch(file_b); - let result1 = run_interactive(Command::new(PROGNAME).arg("-i").arg(file_a).arg(file_b), b"n"); + let result1 = run_piped_stdin(Command::new(PROGNAME).arg("-i").arg(file_a).arg(file_b), b"n"); assert!(result1.success); assert!(Path::new(file_a).exists()); assert!(Path::new(file_b).exists()); - let result2 = run_interactive(Command::new(PROGNAME).arg("-i").arg(file_a).arg(file_b), b"Yesh"); + let result2 = run_piped_stdin(Command::new(PROGNAME).arg("-i").arg(file_a).arg(file_b), b"Yesh"); assert!(result2.success); diff --git a/test/split.rs b/test/split.rs index e893d7a5f..baf75fbca 100644 --- a/test/split.rs +++ b/test/split.rs @@ -8,20 +8,18 @@ use std::path::Path; use std::process::Command; use rand::{Rng, thread_rng}; use regex::Regex; +use util::*; static PROGNAME: &'static str = "./split"; +#[path = "common/util.rs"] +#[macro_use] +mod util; + fn random_chars(n: usize) -> String { thread_rng().gen_ascii_chars().take(n).collect::() } -fn get_file_contents(name: &str) -> Vec { - let mut f = File::open(Path::new(name)).unwrap(); - let mut contents: Vec = vec!(); - let _ = f.read_to_end(&mut contents); - contents -} - struct Glob { directory: String, regex: Regex diff --git a/test/test.rs b/test/test.rs index b792538ea..af6758ae6 100644 --- a/test/test.rs +++ b/test/test.rs @@ -9,17 +9,17 @@ use std::process::Command; -static EXE: &'static str = "./test"; +static PROGNAME: &'static str = "./test"; #[test] fn test_op_prec_and_or_1() { - let status = Command::new(EXE).arg(" ").arg("-o").arg("").arg("-a").arg("").status(); + let status = Command::new(PROGNAME).arg(" ").arg("-o").arg("").arg("-a").arg("").status(); assert_eq!(true, status.unwrap().success()); } #[test] fn test_op_prec_and_or_2() { - let status = Command::new(EXE).arg("") + let status = Command::new(PROGNAME).arg("") .arg("-a") .arg("") .arg("-o") @@ -32,6 +32,6 @@ fn test_op_prec_and_or_2() { #[test] fn test_or_as_filename() { - let status = Command::new(EXE).arg("x").arg("-a").arg("-z").arg("-o").status(); + let status = Command::new(PROGNAME).arg("x").arg("-a").arg("-z").arg("-o").status(); assert_eq!(status.unwrap().code(), Some(1)); } diff --git a/test/tr.rs b/test/tr.rs index e3c64ee1a..3da1b9605 100644 --- a/test/tr.rs +++ b/test/tr.rs @@ -1,51 +1,43 @@ -use std::io::Write; -use std::process::{Command, Stdio}; +use std::process::Command; +use util::*; static PROGNAME: &'static str = "./tr"; -fn run(input: &str, args: &[&'static str]) -> Vec { - let mut process = Command::new(PROGNAME) - .args(args) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .unwrap_or_else(|e| panic!("{}", e)); - - process.stdin.take().unwrap_or_else(|| panic!("Could not take child process stdin")) - .write_all(input.as_bytes()).unwrap_or_else(|e| panic!("{}", e)); - - let po = process.wait_with_output().unwrap_or_else(|e| panic!("{}", e)); - po.stdout -} +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_toupper() { - let out = run("!abcd!", &["a-z", "A-Z"]); - assert_eq!(&out[..], b"!ABCD!"); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["a-z", "A-Z"]), b"!abcd!"); + assert_eq!(result.stdout, "!ABCD!"); } #[test] fn test_small_set2() { - let out = run("@0123456789", &["0-9", "X"]); - assert_eq!(&out[..], b"@XXXXXXXXXX"); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["0-9", "X"]), b"@0123456789"); + assert_eq!(result.stdout, "@XXXXXXXXXX"); } #[test] fn test_unicode() { - let out = run("(,°□°), ┬─┬", &[", ┬─┬", "╯︵┻━┻"]); - assert_eq!(&out[..], "(╯°□°)╯︵┻━┻".as_bytes()); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&[", ┬─┬", "╯︵┻━┻"]), "(,°□°), ┬─┬".as_bytes()); + assert_eq!(result.stdout, "(╯°□°)╯︵┻━┻"); } #[test] fn test_delete() { - let out = run("aBcD", &["-d", "a-z"]); - assert_eq!(&out[..], b"BD"); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-d", "a-z"]), b"aBcD"); + assert_eq!(result.stdout, "BD"); } #[test] fn test_delete_complement() { - let out = run("aBcD", &["-d", "-c", "a-z"]); - assert_eq!(&out[..], b"ac"); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-d", "-c", "a-z"]), b"aBcD"); + assert_eq!(result.stdout, "ac"); } - - diff --git a/test/truncate.rs b/test/truncate.rs index e48f9c853..3461fcbf9 100644 --- a/test/truncate.rs +++ b/test/truncate.rs @@ -1,18 +1,16 @@ -use std::fs::{File, remove_file}; +use std::fs; use std::io::{Seek, SeekFrom, Write}; use std::path::Path; use std::process::Command; +use util::*; static PROGNAME: &'static str = "./truncate"; static TFILE1: &'static str = "truncate_test_1"; static TFILE2: &'static str = "truncate_test_2"; -fn make_file(name: &str) -> File { - match File::create(Path::new(name)) { - Ok(f) => f, - Err(_) => panic!() - } -} +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_increase_file_size() { @@ -24,7 +22,7 @@ fn test_increase_file_size() { if file.seek(SeekFrom::Current(0)).unwrap() != 5 * 1024 { panic!(); } - remove_file(Path::new(TFILE1)).unwrap(); + fs::remove_file(Path::new(TFILE1)).unwrap(); } #[test] @@ -39,5 +37,5 @@ fn test_decrease_file_size() { println!("{:?}", file.seek(SeekFrom::Current(0))); panic!(); } - remove_file(Path::new(TFILE2)).unwrap(); + fs::remove_file(Path::new(TFILE2)).unwrap(); } diff --git a/test/tsort.rs b/test/tsort.rs index c90c3368f..18648f3ad 100644 --- a/test/tsort.rs +++ b/test/tsort.rs @@ -1,16 +1,11 @@ -use std::fs::File; -use std::io::Read; -use std::path::Path; use std::process::Command; +use util::*; static PROGNAME: &'static str = "./tsort"; -fn get_file_contents(name: &str) -> Vec { - let mut f = File::open(Path::new(name)).unwrap(); - let mut contents: Vec = vec!(); - let _ = f.read_to_end(&mut contents); - contents -} +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_sort_call_graph() { @@ -22,5 +17,5 @@ fn test_sort_call_graph() { .output() .unwrap_or_else(|err| panic!("{}", err)); - assert_eq!(String::from_utf8(po.stdout).unwrap(), String::from_utf8(get_file_contents(output)).unwrap()); + assert_eq!(String::from_utf8(po.stdout).unwrap(), String::from_utf8(get_file_contents(output).into_bytes()).unwrap()); } diff --git a/test/unexpand.rs b/test/unexpand.rs index 59eaa9e06..cb922f761 100644 --- a/test/unexpand.rs +++ b/test/unexpand.rs @@ -1,97 +1,98 @@ -use std::io::Write; -use std::process::{Command, Stdio}; +use std::process::Command; +use util::*; static PROGNAME: &'static str = "./unexpand"; -fn run(input: &str, args: &[&'static str]) -> Vec { - let mut process = Command::new(PROGNAME) - .args(args) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .unwrap_or_else(|e| panic!("{}", e)); - - process.stdin.take().unwrap_or_else(|| panic!("Could not take child process stdin")) - .write_all(input.as_bytes()).unwrap_or_else(|e| panic!("{}", e)); - - let po = process.wait_with_output().unwrap_or_else(|e| panic!("{}", e)); - po.stdout -} +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn unexpand_init_0() { - let out = run(" 1\n 2\n 3\n 4\n", &["-t4"]); - assert_eq!(&out[..], b" 1\n 2\n 3\n\t4\n" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-t4"]), b" 1\n 2\n 3\n 4\n"); + assert_eq!(result.stdout, " 1\n 2\n 3\n\t4\n"); } #[test] fn unexpand_init_1() { - let out = run(" 5\n 6\n 7\n 8\n", &["-t4"]); - assert_eq!(&out[..], b"\t 5\n\t 6\n\t 7\n\t\t8\n" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-t4"]), b" 5\n 6\n 7\n 8\n"); + assert_eq!(result.stdout, "\t 5\n\t 6\n\t 7\n\t\t8\n"); } #[test] fn unexpand_init_list_0() { - let out = run(" 1\n 2\n 3\n 4\n", &["-t2,4"]); - assert_eq!(&out[..], b" 1\n\t2\n\t 3\n\t\t4\n" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-t2,4"]), b" 1\n 2\n 3\n 4\n"); + assert_eq!(result.stdout, " 1\n\t2\n\t 3\n\t\t4\n"); } #[test] fn unexpand_init_list_1() { // Once the list is exhausted, spaces are not converted anymore - let out = run(" 5\n 6\n 7\n 8\n", &["-t2,4"]); - assert_eq!(&out[..], b"\t\t 5\n\t\t 6\n\t\t 7\n\t\t 8\n" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-t2,4"]), b" 5\n 6\n 7\n 8\n"); + assert_eq!(result.stdout, "\t\t 5\n\t\t 6\n\t\t 7\n\t\t 8\n"); } #[test] fn unexpand_aflag_0() { - let out = run("e E\nf F\ng G\nh H\n", &["--"]); - assert_eq!(&out[..], b"e E\nf F\ng G\nh H\n" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["--"]), b"e E\nf F\ng G\nh H\n"); + assert_eq!(result.stdout, "e E\nf F\ng G\nh H\n"); } #[test] fn unexpand_aflag_1() { - let out = run("e E\nf F\ng G\nh H\n", &["-a"]); - assert_eq!(&out[..], b"e E\nf F\ng\tG\nh\t H\n" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-a"]), b"e E\nf F\ng G\nh H\n"); + assert_eq!(result.stdout, "e E\nf F\ng\tG\nh\t H\n"); } #[test] fn unexpand_aflag_2() { - let out = run("e E\nf F\ng G\nh H\n", &["-t8"]); - assert_eq!(&out[..], b"e E\nf F\ng\tG\nh\t H\n" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-t8"]), b"e E\nf F\ng G\nh H\n"); + assert_eq!(result.stdout, "e E\nf F\ng\tG\nh\t H\n"); } #[test] fn unexpand_first_only_0() { - let out = run(" A B", &["-t3"]); - assert_eq!(&out[..], b"\t\t A\t B" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-t3"]), b" A B"); + assert_eq!(result.stdout, "\t\t A\t B"); } #[test] fn unexpand_first_only_1() { - let out = run(" A B", &["-t3", "--first-only"]); - assert_eq!(&out[..], b"\t\t A B" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-t3", "--first-only"]), b" A B"); + assert_eq!(result.stdout, "\t\t A B"); } #[test] fn unexpand_trailing_space_0() { // evil // Individual spaces before fields starting with non blanks should not be // converted, unless they are at the beginning of the line. - let out = run("123 \t1\n123 1\n123 \n123 ", &["-t4"]); - assert_eq!(&out[..], b"123\t\t1\n123 1\n123 \n123 " as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-t4"]), b"123 \t1\n123 1\n123 \n123 "); + assert_eq!(result.stdout, "123\t\t1\n123 1\n123 \n123 "); } #[test] fn unexpand_trailing_space_1() { // super evil - let out = run(" abc d e f g ", &["-t1"]); - assert_eq!(&out[..], b"\tabc d e\t\tf\t\tg " as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-t1"]), b" abc d e f g "); + assert_eq!(result.stdout, "\tabc d e\t\tf\t\tg "); } #[test] fn unexpand_spaces_follow_tabs_0() { // The two first spaces can be included into the first tab. - let out = run(" \t\t A", &[]); - assert_eq!(&out[..], b"\t\t A" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd, b" \t\t A"); + assert_eq!(result.stdout, "\t\t A"); } #[test] @@ -101,12 +102,14 @@ fn unexpand_spaces_follow_tabs_1() { // evil // ' \t' -> '\t' // second tabstop (4) // ' ' -> '\t' // third tabstop (5) // ' B \t' -> ' B \t' // after the list is exhausted, nothing must change - let out = run("a \t B \t", &["-t1,4,5"]); - assert_eq!(&out[..], b"a\t\t B \t" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-t1,4,5"]), b"a \t B \t"); + assert_eq!(result.stdout, "a\t\t B \t"); } #[test] fn unexpand_spaces_after_fields() { - let out = run(" \t A B C D A\t\n", &["-a"]); - assert_eq!(&out[..], b"\t\tA B C D\t\t A\t\n" as &[u8]); + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-a"]), b" \t A B C D A\t\n"); + assert_eq!(result.stdout, "\t\tA B C D\t\t A\t\n"); } diff --git a/test/unlink.rs b/test/unlink.rs index b557f8be9..4a3fcae67 100644 --- a/test/unlink.rs +++ b/test/unlink.rs @@ -1,45 +1,17 @@ #![feature(path_ext)] -#![allow(dead_code)] extern crate libc; -use std::fs::{self, File, PathExt}; +use std::fs::PathExt; use std::path::Path; use std::process::Command; -use std::str::from_utf8; +use util::*; 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(); -} +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_unlink_file() { @@ -64,7 +36,7 @@ fn test_unlink_multiple_files() { 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"); + "unlink: error: extra operand: 'test_unlink_multiple_file_b'\nTry 'unlink --help' for more information.\n"); assert!(!result.success); }