From 25926bab677f9901b8f8f709b36b4076d6d5d3bf Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Sat, 17 May 2014 15:47:44 +0200 Subject: [PATCH] Update tests to use std::process:Command --- cat/test.rs | 37 +++++++++++++++++++++++-------------- mkdir/test.rs | 16 ++++++++-------- seq/test.rs | 26 +++++++++++++------------- truncate/test.rs | 16 ++++++++-------- 4 files changed, 52 insertions(+), 43 deletions(-) diff --git a/cat/test.rs b/cat/test.rs index 9a8a60a72..6524ddda9 100644 --- a/cat/test.rs +++ b/cat/test.rs @@ -1,32 +1,41 @@ -use std::io::process::Process; +use std::io::process::Command; use std::str; #[test] fn test_output_multi_files_print_all_chars() { - let prog = Process::output("build/cat", - ["cat/fixtures/alpha.txt".to_owned(), "cat/fixtures/256.txt".to_owned(), - "-A".to_owned(), "-n".to_owned()]).unwrap(); - let out = str::from_utf8_owned(prog.output.as_slice().to_owned()).unwrap(); + let po = match Command::new("build/cat") + .arg("cat/fixtures/alpha.txt") + .arg("cat/fixtures/256.txt") + .arg("-A") + .arg("-n").output() { + + Ok(p) => p, + Err(err) => fail!("{}", err), + }; + + let out = str::from_utf8(po.output.as_slice()).unwrap(); assert_eq!(out, - " 1\tabcde$\n 2\tfghij$\n 3\tklmno$\n 4\tpqrst$\n 5\tuvwxyz$\n 6\t^@^A^B^C^D^E^F^G^H^I$\n 7\t^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_ !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^VM-^WM-^XM-^YM-^ZM-^[M-^\\M-^]M-^^M-^_M- M-!M-\"M-#M-$M-%M-&M-\'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:M-;M-M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-UM-VM-WM-XM-YM-ZM-[M-\\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?".to_owned()); + " 1\tabcde$\n 2\tfghij$\n 3\tklmno$\n 4\tpqrst$\n 5\tuvwxyz$\n 6\t^@^A^B^C^D^E^F^G^H^I$\n 7\t^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_ !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^VM-^WM-^XM-^YM-^ZM-^[M-^\\M-^]M-^^M-^_M- M-!M-\"M-#M-$M-%M-&M-\'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:M-;M-M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-UM-VM-WM-XM-YM-ZM-[M-\\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?"); } #[test] fn test_stdin_squeeze() { - let mut prog = Process::new("build/cat", ["-A".to_owned()]).unwrap(); + let mut process= Command::new("build/cat").arg("-A").spawn().unwrap(); - prog.stdin.take_unwrap().write(bytes!("\x00\x01\x02")); + process.stdin.take_unwrap().write(bytes!("\x00\x01\x02")).unwrap(); + let po = process.wait_with_output().unwrap(); + let out = str::from_utf8(po.output.as_slice()).unwrap(); - let out = str::from_utf8_owned(prog.wait_with_output().output.as_slice().to_owned()).unwrap(); - assert_eq!(out, "^@^A^B".to_owned()); + assert_eq!(out, "^@^A^B"); } #[test] fn test_stdin_number_non_blank() { - let mut prog = Process::new("build/cat", ["-b".to_owned(), "-".to_owned()]).unwrap(); + let mut process = Command::new("build/cat").arg("-b").arg("-").spawn().unwrap(); - prog.stdin.take_unwrap().write(bytes!("\na\nb\n\n\nc")); + process.stdin.take_unwrap().write(bytes!("\na\nb\n\n\nc")).unwrap(); + let po = process.wait_with_output().unwrap(); + let out = str::from_utf8(po.output.as_slice()).unwrap(); - let out = str::from_utf8_owned(prog.wait_with_output().output.as_slice().to_owned()).unwrap(); - assert_eq!(out, "\n 1\ta\n 2\tb\n\n\n 3\tc".to_owned()); + assert_eq!(out, "\n 1\ta\n 2\tb\n\n\n 3\tc"); } diff --git a/mkdir/test.rs b/mkdir/test.rs index 4c6009062..568d4af5c 100644 --- a/mkdir/test.rs +++ b/mkdir/test.rs @@ -1,4 +1,4 @@ -use std::io::process::Process; +use std::io::process::Command; use std::io::fs::rmdir; static exe: &'static str = "build/mkdir"; @@ -12,14 +12,14 @@ fn cleanup(dir: &'static str) { let d = dir.into_owned(); let p = Path::new(d.into_owned()); if p.exists() { - rmdir(&p); + rmdir(&p).unwrap(); } } #[test] fn test_mkdir_mkdir() { cleanup(test_dir1); - let prog = Process::status(exe.into_owned(), [test_dir1.into_owned()]); + let prog = Command::new(exe).arg(test_dir1).status(); let exit_success = prog.unwrap().success(); cleanup(test_dir1); assert_eq!(exit_success, true); @@ -28,13 +28,13 @@ fn test_mkdir_mkdir() { #[test] fn test_mkdir_dup_dir() { cleanup(test_dir2); - let prog = Process::status(exe.into_owned(), [test_dir2.into_owned()]); + let prog = Command::new(exe).arg(test_dir2).status(); let exit_success = prog.unwrap().success(); if !exit_success { cleanup(test_dir2); fail!(); } - let prog2 = Process::status(exe.into_owned(), [test_dir2.into_owned()]); + let prog2 = Command::new(exe).arg(test_dir2).status(); let exit_success2 = prog2.unwrap().success(); cleanup(test_dir2); assert_eq!(exit_success2, false); @@ -43,7 +43,7 @@ fn test_mkdir_dup_dir() { #[test] fn test_mkdir_mode() { cleanup(test_dir3); - let prog = Process::status(exe.into_owned(), ["-m".to_owned(), "755".to_owned(), test_dir3.into_owned()]); + let prog = Command::new(exe).arg("-m").arg("755").arg(test_dir3).status(); let exit_success = prog.unwrap().success(); cleanup(test_dir3); assert_eq!(exit_success, true); @@ -52,7 +52,7 @@ fn test_mkdir_mode() { #[test] fn test_mkdir_parent() { cleanup(test_dir4); - let prog = Process::status(exe.into_owned(), ["-p".to_owned(), test_dir4.into_owned()]); + let prog = Command::new(exe).arg("-p").arg(test_dir4).status(); let exit_success = prog.unwrap().success(); cleanup(test_dir4); assert_eq!(exit_success, true); @@ -61,7 +61,7 @@ fn test_mkdir_parent() { #[test] fn test_mkdir_no_parent() { cleanup(test_dir5); - let prog = Process::status(exe.into_owned(), [test_dir5.into_owned()]); + let prog = Command::new(exe).arg(test_dir5).status(); let exit_success = prog.unwrap().success(); cleanup(test_dir5); assert_eq!(exit_success, false); diff --git a/seq/test.rs b/seq/test.rs index 3e4f77cbf..cdf76c198 100644 --- a/seq/test.rs +++ b/seq/test.rs @@ -1,30 +1,30 @@ -use std::io::process::Process; +use std::io::process::Command; use std::str; #[test] fn test_count_up() { - let p = Process::output("build/seq", ["10".to_owned()]).unwrap(); - let out = str::from_utf8(p.output.as_slice().to_owned()).unwrap().into_owned(); - assert_eq!(out, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n".to_owned()); + let p = Command::new("build/seq").args(["10"]).output().unwrap(); + let out = str::from_utf8(p.output.as_slice()).unwrap(); + assert_eq!(out, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"); } #[test] fn test_count_down() { - let p = Process::output("build/seq", ["--".to_owned(), "5".to_owned(), "-1".to_owned(), "1".to_owned()]).unwrap(); - let out = str::from_utf8(p.output.as_slice().to_owned()).unwrap().into_owned(); - assert_eq!(out, "5\n4\n3\n2\n1\n".to_owned()); + let p = Command::new("build/seq").args(["--", "5", "-1", "1"]).output().unwrap(); + let out = str::from_utf8(p.output.as_slice()).unwrap(); + assert_eq!(out, "5\n4\n3\n2\n1\n"); } #[test] fn test_separator_and_terminator() { - let p = Process::output("build/seq", ["-s".to_owned(), ",".to_owned(), "-t".to_owned(), "!".to_owned(), "2".to_owned(), "6".to_owned()]).unwrap(); - let out = str::from_utf8(p.output.as_slice().to_owned()).unwrap().into_owned(); - assert_eq!(out, "2,3,4,5,6!".to_owned()); + let p = Command::new("build/seq").args(["-s", ",", "-t", "!", "2", "6"]).output().unwrap(); + let out = str::from_utf8(p.output.as_slice()).unwrap(); + assert_eq!(out, "2,3,4,5,6!"); } #[test] fn test_equalize_widths() { - let p = Process::output("build/seq", ["-w".to_owned(), "5".to_owned(), "10".to_owned()]).unwrap(); - let out = str::from_utf8(p.output.as_slice().to_owned()).unwrap().into_owned(); - assert_eq!(out, "05\n06\n07\n08\n09\n10\n".to_owned()); + let p = Command::new("build/seq").args(["-w", "5", "10"]).output().unwrap(); + let out = str::from_utf8(p.output.as_slice()).unwrap(); + assert_eq!(out, "05\n06\n07\n08\n09\n10\n"); } diff --git a/truncate/test.rs b/truncate/test.rs index 01777822b..8de1adbb7 100644 --- a/truncate/test.rs +++ b/truncate/test.rs @@ -1,5 +1,5 @@ use std::io; -use std::io::process::Process; +use std::io::process::Command; static PROG: &'static str = "build/truncate"; static TFILE1: &'static str = "truncate_test_1"; @@ -15,27 +15,27 @@ fn make_file(name: &str) -> io::File { #[test] fn test_increase_file_size() { let mut file = make_file(TFILE1); - if !Process::status(PROG, ["-s".to_owned(), "+5K".to_owned(), TFILE1.to_owned()]).unwrap().success() { + if !Command::new(PROG).args(["-s", "+5K", TFILE1]).status().unwrap().success() { fail!(); } - file.seek(0, io::SeekEnd); + file.seek(0, io::SeekEnd).unwrap(); if file.tell().unwrap() != 5 * 1024 { fail!(); } - io::fs::unlink(&Path::new(TFILE1)); + io::fs::unlink(&Path::new(TFILE1)).unwrap(); } #[test] fn test_decrease_file_size() { let mut file = make_file(TFILE2); - file.write(bytes!("1234567890")); - if !Process::status(PROG, ["--size=-4".to_owned(), TFILE2.to_owned()]).unwrap().success() { + file.write(bytes!("1234567890")).unwrap(); + if !Command::new(PROG).args(["--size=-4", TFILE2]).status().unwrap().success() { fail!(); } - file.seek(0, io::SeekEnd); + file.seek(0, io::SeekEnd).unwrap(); if file.tell().unwrap() != 6 { println!("{}", file.tell()); fail!(); } - io::fs::unlink(&Path::new(TFILE2)); + io::fs::unlink(&Path::new(TFILE2)).unwrap(); }