Skip to content

Commit cdff1bb

Browse files
sgngitster
authored andcommitted
test-lib-functions: introduce test_stdout_line_count
In some tests, we're checking the number of lines in output of some commands, including but not limited to Git's command. We're doing the check by running those commands in the left side of a pipe, thus losing the exit status code of those commands. Meanwhile, we really want to check the exit status code of Git's command. Let's write the output of those commands to a temporary file, and use test_line_count separately in order to check exit status code of those commands properly. Signed-off-by: Đoàn Trần Công Danh <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 670b81a commit cdff1bb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

t/test-lib-functions.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,32 @@ test_line_count () {
845845
fi
846846
}
847847

848+
# SYNOPSIS:
849+
# test_stdout_line_count <bin-ops> <value> <cmd> [<args>...]
850+
#
851+
# test_stdout_line_count checks that the output of a command has the number
852+
# of lines it ought to. For example:
853+
#
854+
# test_stdout_line_count = 3 git ls-files -u
855+
# test_stdout_line_count -gt 10 ls
856+
test_stdout_line_count () {
857+
local ops val trashdir &&
858+
if test "$#" -le 3
859+
then
860+
BUG "expect 3 or more arguments"
861+
fi &&
862+
ops="$1" &&
863+
val="$2" &&
864+
shift 2 &&
865+
if ! trashdir="$(git rev-parse --git-dir)/trash"; then
866+
BUG "expect to be run inside a worktree"
867+
fi &&
868+
mkdir -p "$trashdir" &&
869+
"$@" >"$trashdir/output" &&
870+
test_line_count "$ops" "$val" "$trashdir/output"
871+
}
872+
873+
848874
test_file_size () {
849875
test "$#" -ne 1 && BUG "1 param"
850876
test-tool path-utils file-size "$1"

0 commit comments

Comments
 (0)