Add tests for head

This exposed one issue in head.c -- printf was not flushing and
file names could appear after file contents instead of before.

The issue is fixed by calling xflush after xprintf.
This commit is contained in:
Timothy Elliott 2012-02-10 21:59:57 -08:00
parent 365bda87f4
commit 270366f582
2 changed files with 19 additions and 1 deletions

17
scripts/test/head.test Normal file
View File

@ -0,0 +1,17 @@
#!/bin/bash
[ -f testing.sh ] && . testing.sh
#testing "name" "command" "result" "infile" "stdin"
testing "head, stdin" "head -n 1 && echo yes" "one\nyes\n" "" "one\ntwo"
testing "head, stdin via -" "head -n 1 - && echo yes" "one\nyes\n" "" "one\ntwo"
testing "head, file" "head input -n 1 && echo yes" "one\nyes\n" "one\ntwo" ""
testing "head, default lines" "head" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" "" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12"
echo "foo
bar
baz" > file1
testing "head, multiple files" "head -n 2 input file1" "==> input <==\none\ntwo\n\n==> file1 <==\nfoo\nbar\n" "one\ntwo\nthree\n" ""
rm file1

View File

@ -35,8 +35,9 @@ static void do_head(int fd, char *name)
if (toys.optc > 1) {
// Print an extra newline for all but the first file
if (TT.file_no++) printf("\n");
if (TT.file_no++) xprintf("\n");
xprintf("==> %s <==\n", name);
xflush();
}
while (lines) {