Skip to content

Commit fb3340a

Browse files
jrngitster
authored andcommitted
test-lib: introduce test_line_count to measure files
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 45b983b 3 hi.c on failure. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2dec68c commit fb3340a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

t/README

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,10 @@ library for your script to use.
500500
<expected> file. This behaves like "cmp" but produces more
501501
helpful output when the test is run with "-v" option.
502502

503+
- test_line_count (= | -lt | -ge | ...) <length> <file>
504+
505+
Check whether a file has the length it is expected to.
506+
503507
- test_path_is_file <file> [<diagnosis>]
504508
test_path_is_dir <dir> [<diagnosis>]
505509
test_path_is_missing <path> [<diagnosis>]

t/test-lib.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,28 @@ test_path_is_missing () {
598598
fi
599599
}
600600

601+
# test_line_count checks that a file has the number of lines it
602+
# ought to. For example:
603+
#
604+
# test_expect_success 'produce exactly one line of output' '
605+
# do something >output &&
606+
# test_line_count = 1 output
607+
# '
608+
#
609+
# is like "test $(wc -l <output) = 1" except that it passes the
610+
# output through when the number of lines is wrong.
611+
612+
test_line_count () {
613+
if test $# != 3
614+
then
615+
error "bug in the test script: not 3 parameters to test_line_count"
616+
elif ! test $(wc -l <"$3") "$1" "$2"
617+
then
618+
echo "test_line_count: line count for $3 !$1 $2"
619+
cat "$3"
620+
return 1
621+
fi
622+
}
601623

602624
# This is not among top-level (test_expect_success | test_expect_failure)
603625
# but is a prefix that can be used in the test script, like:

0 commit comments

Comments
 (0)