diff --git a/Makefile b/Makefile index 0f1900e85..6f4651d70 100644 --- a/Makefile +++ b/Makefile @@ -22,12 +22,16 @@ ENABLE_STRIP := endif # Install directories -PREFIX ?= /usr/local -BINDIR ?= /bin +PREFIX ?= /usr/local +BINDIR ?= /bin -BASEDIR ?= . +# This won't support any directory with spaces in its name, but you can just +# make a symlink without spaces that points to the directory. +BASEDIR ?= $(shell pwd) SRCDIR := $(BASEDIR)/src BUILDDIR := $(BASEDIR)/build +TESTDIR := $(BASEDIR)/test +TEMPDIR := $(BASEDIR)/tmp # Possible programs PROGS := \ @@ -178,11 +182,14 @@ endef # Test exe built rules define TEST_BUILD -test_$(1): tmp/$(1)_test $(BUILDDIR)/$(1) - $(call command,$$<) +test_$(1): $(TEMPDIR)/$(1)/$(1)_test $(BUILDDIR)/$(1) + $(call command,cp $(BUILDDIR)/$(1) $(TEMPDIR)/$(1) && cd $(TEMPDIR)/$(1) && $$<) -tmp/$(1)_test: $(SRCDIR)/$(1)/test.rs +$(TEMPDIR)/$(1)/$(1)_test: $(TESTDIR)/$(1).rs | $(TEMPDIR)/$(1) $(call command,$(RUSTC) $(RUSTCFLAGS) --test -o $$@ $$<) + +$(TEMPDIR)/$(1): | $(TEMPDIR) + $(call command,cp -r $(TESTDIR)/fixtures/$(1) $$@ || mkdir $$@) endef # Main rules @@ -220,18 +227,18 @@ deps: $(BUILDDIR)/.rust-crypto $(SRCDIR)/cksum/crc_table.rs crates: echo $(EXES) -test: tmp $(addprefix test_,$(TESTS)) - $(RM) -rf tmp +test: $(TEMPDIR) $(addprefix test_,$(TESTS)) + $(RM) -rf $(TEMPDIR) clean: - $(RM) -rf $(BUILDDIR) tmp + $(RM) -rf $(BUILDDIR) $(TEMPDIR) $(BUILDDIR): git submodule update --init mkdir -p $(BUILDDIR)/gen -tmp: - mkdir tmp +$(TEMPDIR): + mkdir $(TEMPDIR) install: $(addprefix $(BUILDDIR)/,$(INSTALLEES)) mkdir -p $(DESTDIR)$(PREFIX)$(BINDIR) diff --git a/src/cat/test.rs b/test/cat.rs similarity index 82% rename from src/cat/test.rs rename to test/cat.rs index 2b0e3e417..ba7248858 100644 --- a/src/cat/test.rs +++ b/test/cat.rs @@ -1,11 +1,13 @@ use std::io::process::Command; use std::str; +static PROGNAME: &'static str = "./cat"; + #[test] fn test_output_multi_files_print_all_chars() { - let po = match Command::new("build/cat") - .arg("src/cat/fixtures/alpha.txt") - .arg("src/cat/fixtures/256.txt") + let po = match Command::new(PROGNAME) + .arg("alpha.txt") + .arg("256.txt") .arg("-A") .arg("-n").output() { @@ -20,7 +22,7 @@ fn test_output_multi_files_print_all_chars() { #[test] fn test_stdin_squeeze() { - let mut process= Command::new("build/cat").arg("-A").spawn().unwrap(); + let mut process= Command::new(PROGNAME).arg("-A").spawn().unwrap(); process.stdin.take_unwrap().write(b"\x00\x01\x02").unwrap(); let po = process.wait_with_output().unwrap(); @@ -31,7 +33,7 @@ fn test_stdin_squeeze() { #[test] fn test_stdin_number_non_blank() { - let mut process = Command::new("build/cat").arg("-b").arg("-").spawn().unwrap(); + let mut process = Command::new(PROGNAME).arg("-b").arg("-").spawn().unwrap(); process.stdin.take_unwrap().write(b"\na\nb\n\n\nc").unwrap(); let po = process.wait_with_output().unwrap(); diff --git a/src/cat/fixtures/256.txt b/test/fixtures/cat/256.txt similarity index 100% rename from src/cat/fixtures/256.txt rename to test/fixtures/cat/256.txt diff --git a/src/cat/fixtures/alpha.txt b/test/fixtures/cat/alpha.txt similarity index 100% rename from src/cat/fixtures/alpha.txt rename to test/fixtures/cat/alpha.txt diff --git a/src/nl/fixtures/joinblanklines.txt b/test/fixtures/nl/joinblanklines.txt similarity index 100% rename from src/nl/fixtures/joinblanklines.txt rename to test/fixtures/nl/joinblanklines.txt diff --git a/src/nl/fixtures/section.txt b/test/fixtures/nl/section.txt similarity index 100% rename from src/nl/fixtures/section.txt rename to test/fixtures/nl/section.txt diff --git a/src/nl/fixtures/simple.txt b/test/fixtures/nl/simple.txt similarity index 100% rename from src/nl/fixtures/simple.txt rename to test/fixtures/nl/simple.txt diff --git a/src/mkdir/test.rs b/test/mkdir.rs similarity index 82% rename from src/mkdir/test.rs rename to test/mkdir.rs index 2ed17930d..28a525ffd 100644 --- a/src/mkdir/test.rs +++ b/test/mkdir.rs @@ -1,12 +1,12 @@ use std::io::process::Command; use std::io::fs::rmdir; -static exe: &'static str = "build/mkdir"; -static test_dir1: &'static str = "tmp/mkdir_test1"; -static test_dir2: &'static str = "tmp/mkdir_test2"; -static test_dir3: &'static str = "tmp/mkdir_test3"; -static test_dir4: &'static str = "tmp/mkdir_test4/mkdir_test4_1"; -static test_dir5: &'static str = "tmp/mkdir_test5/mkdir_test5_1"; +static exe: &'static str = "./mkdir"; +static test_dir1: &'static str = "mkdir_test1"; +static test_dir2: &'static str = "mkdir_test2"; +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 cleanup(dir: &'static str) { let d = dir.into_string(); diff --git a/src/nl/test.rs b/test/nl.rs similarity index 76% rename from src/nl/test.rs rename to test/nl.rs index dc1e41de9..21ad94b77 100644 --- a/src/nl/test.rs +++ b/test/nl.rs @@ -1,10 +1,11 @@ use std::io::process::Command; use std::str; +static PROGNAME: &'static str = "./nl"; + #[test] fn test_stdin_nonewline() { - - let mut process = Command::new("build/nl").spawn().unwrap(); + let mut process = Command::new(PROGNAME).spawn().unwrap(); process.stdin.take_unwrap().write(b"No Newline").unwrap(); let po = process.wait_with_output().unwrap(); let out = str::from_utf8(po.output.as_slice()).unwrap(); @@ -13,8 +14,7 @@ fn test_stdin_nonewline() { } #[test] fn test_stdin_newline() { - - let mut process = Command::new("build/nl").arg("-s").arg("-") + let mut process = Command::new(PROGNAME).arg("-s").arg("-") .arg("-w").arg("1").spawn().unwrap(); process.stdin.take_unwrap().write(b"Line One\nLine Two\n").unwrap(); @@ -26,8 +26,8 @@ fn test_stdin_newline() { #[test] fn test_padding_without_overflow() { - let po = Command::new("build/nl").arg("-i").arg("1000").arg("-s").arg("x") - .arg("-n").arg("rz").arg("src/nl/fixtures/simple.txt").output().unwrap(); + let po = Command::new(PROGNAME).arg("-i").arg("1000").arg("-s").arg("x") + .arg("-n").arg("rz").arg("simple.txt").output().unwrap(); let out = str::from_utf8(po.output.as_slice()).unwrap(); assert_eq!(out, "000001xL1\n001001xL2\n002001xL3\n003001xL4\n004001xL5\n005001xL6\n006001xL7\n007001xL8\n008001xL9\n009001xL10\n010001xL11\n011001xL12\n012001xL13\n013001xL14\n014001xL15\n"); @@ -35,9 +35,9 @@ fn test_padding_without_overflow() { #[test] fn test_padding_with_overflow() { - let po = Command::new("build/nl").arg("-i").arg("1000").arg("-s").arg("x") + let po = Command::new(PROGNAME).arg("-i").arg("1000").arg("-s").arg("x") .arg("-n").arg("rz").arg("-w").arg("4") - .arg("src/nl/fixtures/simple.txt").output().unwrap(); + .arg("simple.txt").output().unwrap(); let out = str::from_utf8(po.output.as_slice()).unwrap(); assert_eq!(out, "0001xL1\n1001xL2\n2001xL3\n3001xL4\n4001xL5\n5001xL6\n6001xL7\n7001xL8\n8001xL9\n9001xL10\n10001xL11\n11001xL12\n12001xL13\n13001xL14\n14001xL15\n"); @@ -47,15 +47,15 @@ fn test_padding_with_overflow() { fn test_sections_and_styles() { for &(fixture, output) in [ ( - "src/nl/fixtures/section.txt", + "section.txt", "\nHEADER1\nHEADER2\n\n1 |BODY1\n2 |BODY2\n\nFOOTER1\nFOOTER2\n\nNEXTHEADER1\nNEXTHEADER2\n\n1 |NEXTBODY1\n2 |NEXTBODY2\n\nNEXTFOOTER1\nNEXTFOOTER2\n" ), ( - "src/nl/fixtures/joinblanklines.txt", + "joinblanklines.txt", "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 po = Command::new("build/nl").arg("-s").arg("|").arg("-n").arg("ln") + let po = Command::new(PROGNAME).arg("-s").arg("|").arg("-n").arg("ln") .arg("-w").arg("3").arg("-b").arg("a").arg("-l").arg("5") .arg(fixture).output().unwrap(); assert_eq!(str::from_utf8(po.output.as_slice()).unwrap(), output); diff --git a/src/seq/test.rs b/test/seq.rs similarity index 62% rename from src/seq/test.rs rename to test/seq.rs index cdf76c198..8f6569ca8 100644 --- a/src/seq/test.rs +++ b/test/seq.rs @@ -1,30 +1,32 @@ use std::io::process::Command; use std::str; +static PROGNAME: &'static str = "./seq"; + #[test] fn test_count_up() { - let p = Command::new("build/seq").args(["10"]).output().unwrap(); + let p = Command::new(PROGNAME).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 = Command::new("build/seq").args(["--", "5", "-1", "1"]).output().unwrap(); + let p = Command::new(PROGNAME).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 = Command::new("build/seq").args(["-s", ",", "-t", "!", "2", "6"]).output().unwrap(); + let p = Command::new(PROGNAME).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 = Command::new("build/seq").args(["-w", "5", "10"]).output().unwrap(); + let p = Command::new(PROGNAME).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/src/tr/test.rs b/test/tr.rs similarity index 89% rename from src/tr/test.rs rename to test/tr.rs index b39c29ecb..562fee81a 100644 --- a/src/tr/test.rs +++ b/test/tr.rs @@ -1,7 +1,9 @@ use std::io::process::Command; +static PROGNAME: &'static str = "./tr"; + fn run(input: &str, args: &[&'static str]) -> Vec { - let mut process = Command::new("build/tr").args(args).spawn().unwrap(); + let mut process = Command::new(PROGNAME).args(args).spawn().unwrap(); process.stdin.take_unwrap().write_str(input).unwrap(); diff --git a/src/truncate/test.rs b/test/truncate.rs similarity index 79% rename from src/truncate/test.rs rename to test/truncate.rs index d549724eb..23ab29af1 100644 --- a/src/truncate/test.rs +++ b/test/truncate.rs @@ -1,7 +1,7 @@ use std::io; use std::io::process::Command; -static PROG: &'static str = "build/truncate"; +static PROGNAME: &'static str = "./truncate"; static TFILE1: &'static str = "truncate_test_1"; static TFILE2: &'static str = "truncate_test_2"; @@ -15,7 +15,7 @@ fn make_file(name: &str) -> io::File { #[test] fn test_increase_file_size() { let mut file = make_file(TFILE1); - if !Command::new(PROG).args(["-s", "+5K", TFILE1]).status().unwrap().success() { + if !Command::new(PROGNAME).args(["-s", "+5K", TFILE1]).status().unwrap().success() { fail!(); } file.seek(0, io::SeekEnd).unwrap(); @@ -29,7 +29,7 @@ fn test_increase_file_size() { fn test_decrease_file_size() { let mut file = make_file(TFILE2); file.write(b"1234567890").unwrap(); - if !Command::new(PROG).args(["--size=-4", TFILE2]).status().unwrap().success() { + if !Command::new(PROGNAME).args(["--size=-4", TFILE2]).status().unwrap().success() { fail!(); } file.seek(0, io::SeekEnd).unwrap();