aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--scripts/test/head.test17
-rw-r--r--toys/head.c3
2 files changed, 19 insertions, 1 deletions
diff --git a/scripts/test/head.test b/scripts/test/head.test
new file mode 100644
index 00000000..eeb07e00
--- /dev/null
+++ b/scripts/test/head.test
@@ -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
+
diff --git a/toys/head.c b/toys/head.c
index 87fbe62a..1d1e54a3 100644
--- a/toys/head.c
+++ b/toys/head.c
@@ -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) {