From 810936ac4226dae4aab41626418a2e1f61e3b20b Mon Sep 17 00:00:00 2001 From: Arcterus Date: Mon, 3 Feb 2014 22:54:57 -0800 Subject: [PATCH] truncate: added simple tests --- Makefile | 1 + truncate/test.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 truncate/test.rs diff --git a/Makefile b/Makefile index caf18eea9..f03f3067e 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,7 @@ TEST_PROGS := \ cat \ mkdir \ seq \ + truncate \ TEST ?= $(TEST_PROGS) diff --git a/truncate/test.rs b/truncate/test.rs new file mode 100644 index 000000000..2562ab758 --- /dev/null +++ b/truncate/test.rs @@ -0,0 +1,40 @@ +use std::{run, io}; + +static PROG: &'static str = "build/truncate"; +static TESTNAME: &'static str = "THISISARANDOMFILENAME"; + +fn make_file() -> io::File { + while Path::new(TESTNAME).exists() { io::timer::sleep(1000); } + match io::File::create(&Path::new(TESTNAME)) { + Some(f) => f, + None => fail!() + } +} + +#[test] +fn test_increase_file_size() { + let mut file = make_file(); + if !run::process_status(PROG, [~"-s", ~"+5K", TESTNAME.to_owned()]).unwrap().success() { + fail!(); + } + file.seek(0, io::SeekEnd); + if file.tell() != 5 * 1024 { + fail!(); + } + io::fs::unlink(&Path::new(TESTNAME)); +} + +#[test] +fn test_decrease_file_size() { + let mut file = make_file(); + file.write(bytes!("1234567890")); + if !run::process_status(PROG, [~"--size=-4", TESTNAME.to_owned()]).unwrap().success() { + fail!(); + } + file.seek(0, io::SeekEnd); + if file.tell() != 6 { + println!("{}", file.tell()); + fail!(); + } + io::fs::unlink(&Path::new(TESTNAME)); +}