]> asedeno.scripts.mit.edu Git - git.git/commitdiff
test-lib: introduce test_line_count to measure files
authorJonathan Nieder <jrnieder@gmail.com>
Sun, 31 Oct 2010 07:33:50 +0000 (02:33 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 9 Nov 2010 22:47:17 +0000 (14:47 -0800)
Some tests check their output with code like the following:

test "$(git ls-files -u B | wc -l)" -eq 3 || {
echo "BAD: should have left stages for B"
return 1
}

The verbose failure condition is used because test, unlike
diff, does not print any useful information about the
nature of the failure when it fails.

Introduce a test_line_count function to help. If used like

git ls-files -u B >output &&
test_line_count -eq 3 output

it will produce output like

test_line_count: line count for output !-eq 3
100644 b023018cabc396e7692c70bbf5784a93d3f738ab 2 hi.c
100644 45b983be36b73c0788dc9cbcb76cbb80fc7bb057 3 hi.c

on failure.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/README
t/test-lib.sh

index 2aceb67c785b40642818849d80c1f926b0897af9..1a78982da1265242d7fa5f249eaa2f67ae9d2879 100644 (file)
--- a/t/README
+++ b/t/README
@@ -500,6 +500,10 @@ library for your script to use.
    <expected> file.  This behaves like "cmp" but produces more
    helpful output when the test is run with "-v" option.
 
+ - test_line_count (= | -lt | -ge | ...) <length> <file>
+
+   Check whether a file has the length it is expected to.
+
  - test_path_is_file <file> [<diagnosis>]
    test_path_is_dir <dir> [<diagnosis>]
    test_path_is_missing <path> [<diagnosis>]
index 87308f5a9bb8086475dbc49276b8ad249f196d32..a417bdfed14bd56751aa4fd5787803b9eae9c25c 100644 (file)
@@ -598,6 +598,28 @@ test_path_is_missing () {
        fi
 }
 
+# test_line_count checks that a file has the number of lines it
+# ought to. For example:
+#
+#      test_expect_success 'produce exactly one line of output' '
+#              do something >output &&
+#              test_line_count = 1 output
+#      '
+#
+# is like "test $(wc -l <output) = 1" except that it passes the
+# output through when the number of lines is wrong.
+
+test_line_count () {
+       if test $# != 3
+       then
+               error "bug in the test script: not 3 parameters to test_line_count"
+       elif ! test $(wc -l <"$3") "$1" "$2"
+       then
+               echo "test_line_count: line count for $3 !$1 $2"
+               cat "$3"
+               return 1
+       fi
+}
 
 # This is not among top-level (test_expect_success | test_expect_failure)
 # but is a prefix that can be used in the test script, like: