# HG changeset patch # User Timothy Elliott # Date 1328939997 28800 # Node ID f4cc0a2aa440ed7c42298d5f685c4e452eb93b4c # Parent d8ff3b0980cff133f7424deb7e02c6ef79658151 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. diff -r d8ff3b0980cf -r f4cc0a2aa440 scripts/test/head.test --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/test/head.test Fri Feb 10 21:59:57 2012 -0800 @@ -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 -r d8ff3b0980cf -r f4cc0a2aa440 toys/head.c --- a/toys/head.c Sat Feb 11 13:42:24 2012 -0600 +++ b/toys/head.c Fri Feb 10 21:59:57 2012 -0800 @@ -35,8 +35,9 @@ 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) {