README_DOCS.rst: update tg prev and tg next usage summary
[topgit/pro.git] / t / test-lib-functions.sh
blob4f2bc8d8c7956086b3dee81722594ef61f9bbe5c
1 # Test function library from Git with modifications.
3 # Modifications Copyright (C) 2016,2017,2021 Kyle J. McKay
4 # All rights reserved
5 # Modifications made:
7 # * Many "GIT_..." variables removed -- some were kept as TESTLIB_..." instead
8 # (Except "GIT_PATH" is new and is the full path to a "git" executable)
10 # * IMPORTANT: test-lib-functions.sh SHOULD NOT EXECUTE ANY CODE! A new
11 # function "test_lib_functions_init" has been added that will be called
12 # and MUST contain any lines of code to be executed. This will ALWAYS
13 # be the LAST function defined in this file for easy locatability.
15 # * Added test_tolerate_failure and $LINENO support unctions
17 # * Anything related to valgrind or perf has been stripped out
19 # * Many other minor changes and efficiencies
21 # Library of functions shared by all tests scripts, included by
22 # test-lib.sh.
24 # Copyright (C) 2005 Junio C Hamano
26 # This program is free software: you can redistribute it and/or modify
27 # it under the terms of the GNU General Public License as published by
28 # the Free Software Foundation, either version 2 of the License, or
29 # (at your option) any later version.
31 # This program is distributed in the hope that it will be useful,
32 # but WITHOUT ANY WARRANTY; without even the implied warranty of
33 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 # GNU General Public License for more details.
36 # You should have received a copy of the GNU General Public License
37 # along with this program. If not, see http://www.gnu.org/licenses/ .
40 ## IMPORTANT: THIS FILE MUST NOT CONTAIN ANYTHING OTHER THAN FUNCTION
41 ## DEFINITION!!! INITIALIZATION GOES IN THE LAST FUNCTION
42 ## DEFINED IN THIS FILE "test_lib_functions_init" IF REQUIRED!
45 # The semantics of the editor variables are that of invoking
46 # sh -c "$EDITOR \"$@\"" files ...
48 # If our trash directory contains shell metacharacters, they will be
49 # interpreted if we just set $EDITOR directly, so do a little dance with
50 # environment variables to work around this.
52 # In particular, quoting isn't enough, as the path may contain the same quote
53 # that we're using.
54 test_set_editor() {
55 FAKE_EDITOR="$1"
56 export FAKE_EDITOR
57 EDITOR='"$FAKE_EDITOR"'
58 export EDITOR
61 test_decode_color() {
62 awk '
63 function name(n) {
64 if (n == 0) return "RESET";
65 if (n == 1) return "BOLD";
66 if (n == 30) return "BLACK";
67 if (n == 31) return "RED";
68 if (n == 32) return "GREEN";
69 if (n == 33) return "YELLOW";
70 if (n == 34) return "BLUE";
71 if (n == 35) return "MAGENTA";
72 if (n == 36) return "CYAN";
73 if (n == 37) return "WHITE";
74 if (n == 40) return "BLACK";
75 if (n == 41) return "BRED";
76 if (n == 42) return "BGREEN";
77 if (n == 43) return "BYELLOW";
78 if (n == 44) return "BBLUE";
79 if (n == 45) return "BMAGENTA";
80 if (n == 46) return "BCYAN";
81 if (n == 47) return "BWHITE";
84 while (match($0, /\033\[[0-9;]*m/) != 0) {
85 printf "%s<", substr($0, 1, RSTART-1);
86 codes = substr($0, RSTART+2, RLENGTH-3);
87 if (length(codes) == 0)
88 printf "%s", name(0)
89 else {
90 n = split(codes, ary, ";");
91 sep = "";
92 for (i = 1; i <= n; i++) {
93 printf "%s%s", sep, name(ary[i]);
94 sep = ";"
97 printf ">";
98 $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
100 print
105 lf_to_nul() {
106 tr '\012' '\000'
109 nul_to_q() {
110 tr '\000' Q
113 q_to_nul() {
114 tr Q '\000'
117 q_to_cr() {
118 tr Q '\015'
121 q_to_tab() {
122 tr Q '\011'
125 qz_to_tab_space() {
126 tr QZ '\011\040'
129 append_cr() {
130 sed -e 's/$/Q/' | tr Q '\015'
133 remove_cr() {
134 tr '\015' Q | sed -e 's/Q$//'
137 # In some bourne shell implementations, the "unset" builtin returns
138 # nonzero status when a variable to be unset was not set in the first
139 # place.
141 # Use sane_unset when that should not be considered an error.
143 sane_unset() {
144 { "unset" "$@"; } >/dev/null 2>&1 || :
147 test_asv_cache_lno() {
148 : "${callerlno:=$1}"
149 shift
150 if [ "$1" != "-" ]; then
151 test_asv_cache_lno "$callerlno" - <<EOT
154 else
155 while read _tac_id _tac_hash _tac_value; do
156 case "$_tac_id" in "#"*|"") continue; esac
157 case "$_tac_hash" in
158 sha1|sha256)
159 eval "test_asvdb_${_tac_id}_$_tac_hash=\"\$_tac_value\""
162 die "${0##*/}:${callerlno:+$callerlno:}" \
163 "invalid test_asv_cache hash algorithm '$_tac_hash'"
164 esac
165 done
168 test_asv_cache() {
169 test_asv_cache_lno "" "$@"
171 alias test_asv_cache='test_asv_cache_lno "$LINENO"' >/dev/null 2>&1 || :
173 test_v_asv_lno() {
174 : "${callerlno:=$1}"
175 shift
176 _asvhash="${3:-$test_hash_algo}"
177 : "${_asvhash:=sha1}"
178 case "$_asvhash" in sha1|sha256);;*)
179 die "${0##*/}:${callerlno:+$callerlno:} invalid test_v_asv hash algorithm '$_asvhash'"
180 esac
181 eval "_tac_set=\${test_asvdb_${2}_$_asvhash+set}"
182 test "$_tac_set" = "set" ||
183 die "${0##*/}:${callerlno:+$callerlno:}" \
184 "missing test_asv_cache value for id \"$2\" hash algorithm $_asvhash"
185 test -z "$1" || eval "$1=\"\$test_asvdb_${2}_$_asvhash\""
187 test_v_asv() {
188 test_v_asv_lno "" "$@"
190 alias test_v_asv='test_v_asv_lno "$LINENO"' >/dev/null 2>&1 || :
192 test_asv_lno() {
193 : "${callerlno:=$1}"
194 shift
195 _asvhash="${2:-$test_hash_algo}"
196 : "${_asvhash:=sha1}"
197 case "$_asvhash" in sha1|sha256);;*)
198 die "${0##*/}:${callerlno:+$callerlno:} invalid test_asv hash algorithm '$_asvhash'"
199 esac
200 eval "_tac_set=\${test_asvdb_${1}_$_asvhash+set}"
201 test "$_tac_set" = "set" ||
202 die "${0##*/}:${callerlno:+$callerlno:}" \
203 "missing test_asv_cache value for id \"$1\" hash algorithm $_asvhash"
204 eval "printf '%s\n' \"\$test_asvdb_${1}_$_asvhash\""
206 test_asv() {
207 test_asv_lno "" "$@"
209 alias test_asv='test_asv_lno "$LINENO"' >/dev/null 2>&1 || :
211 test_v_git_mt_lno() {
212 : "${callerlno:=$1}"
213 shift
214 case "$2" in blob|tree|null);;*)
215 die "${0##*/}:${callerlno:+$callerlno:} invalid test_v_git_mt object type '$2'"
216 esac
217 _mthash="${3:-$test_hash_algo}"
218 : "${_mthash:=sha1}"
219 case "$_mthash" in sha1|sha256);;*)
220 die "${0##*/}:${callerlno:+$callerlno:} invalid test_v_git_mt hash algorithm '$_mthash'"
221 esac
222 _mthashval=
223 case "$2" in
224 null)
225 case "$_mthash" in
226 sha1) _mthashval="0000000000000000000000000000000000000000";;
227 sha256) _mthashval="0000000000000000000000000000000000000000000000000000000000000000";;
228 esac;;
229 blob)
230 case "$_mthash" in
231 sha1) _mthashval="e69de29bb2d1d6434b8b29ae775ad8c2e48c5391";;
232 sha256) _mthashval="473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813";;
233 esac;;
234 tree)
235 case "$_mthash" in
236 sha1) _mthashval="4b825dc642cb6eb9a060e54bf8d69288fbee4904";;
237 sha256) _mthashval="6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321";;
238 esac;;
239 esac
240 test -z "$1" || eval "$1=\"\$_mthashval\""
242 test_v_git_mt() {
243 test_v_git_mt_lno "" "$@"
245 alias test_v_git_mt='test_v_git_mt_lno "$LINENO"' >/dev/null 2>&1 || :
247 test_set_hash_algo_lno() {
248 : "${callerlno:=$1}"
249 shift
250 case "$1" in sha1|sha256);;*)
251 die "${0##*/}:${callerlno:+$callerlno:} invalid test_set_hash_algo hash algorithm '$1'"
252 esac
253 test "$1" != "sha256" || test -n "$test_git229_plus" ||
254 die "${0##*/}:${callerlno:+$callerlno:}" \
255 "test_set_hash_algo sha256 requires Git 2.29.0 or later" \
256 "but found $git_version"
257 test_hash_algo="$1"
258 GIT_DEFAULT_HASH="$test_hash_algo" && export GIT_DEFAULT_HASH
260 test_set_hash_algo() {
261 test_set_hash_algo_lno "" "$@"
263 alias test_set_hash_algo='test_set_hash_algo_lno "$LINENO"' >/dev/null 2>&1 || :
265 # Protect against breaking in the future when Git changes its
266 # nearly two decades old defaults. The `-c` option first appeared
267 # in Git 1.7.2 (2010-07-21). That means this test framework
268 # requires at least Git 1.7.2. Since TopGit requires at least
269 # Git 1.9.2 that's not a problem. If Git is at least version 2.29.0
270 # pass a --object-format=$test_hash_algo option as the first `git init`
271 # option.
272 git_init() {
273 test -z "$test_git229_plus" ||
274 set -- --object-format="${test_hash_algo:-sha1}" "$@"
275 git -c init.defaultBranch=master init "$@"
278 test_tick() {
279 if test -z "${test_tick:+set}"
280 then
281 test_tick=1112911993
282 else
283 test_tick=$(($test_tick + 60))
285 GIT_COMMITTER_DATE="$test_tick -0700"
286 GIT_AUTHOR_DATE="$test_tick -0700"
287 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
290 # Stop execution and start a shell. This is useful for debugging tests and
291 # only makes sense together with "-v".
293 # Be sure to remove all invocations of this command before submitting.
295 test_pause() {
296 if test "$verbose" = t; then
297 "$SHELL_PATH" <&6 >&3 2>&4
298 else
299 error >&5 "test_pause requires --verbose"
303 test_check_tag_ok_() {
304 case "$1" in ""|*"^"*|*[?*:~]*|*"["*|*"@{"*|*".."*|*"//"*|*"\\"*) return 1; esac
305 return 0
308 test_check_one_tag_() {
309 test $# -eq 1 &&
310 test_check_tag_ok_ "$1"
313 # Call test_commit with the arguments "<message> [<file> [<contents> [<tag>]]]"
315 # This will commit a file with the given contents and the given commit
316 # message, and tag the resulting commit with the given tag name.
318 # <file> defaults to "<message>.t", <contents> and <tag> default to
319 # "<message>". If the (possibly default) value for <tag> ends up being
320 # empty or contains any whitespace or invalid ref name characters the tag will
321 # be omitted.
323 # <file>, <contents>, and <tag> all default to <message>.
325 test_commit() {
326 notick= &&
327 signoff= &&
328 while test $# != 0
330 case "$1" in
331 --notick)
332 notick=yes
334 --signoff)
335 signoff="$1"
338 break
340 esac
341 shift
342 done &&
343 file=${2:-"$1.t"} &&
344 printf '%s\n' "${3-$1}" > "$file" &&
345 git add "$file" &&
346 if test -z "$notick"
347 then
348 test_tick
349 fi &&
350 git commit $signoff -m "$1" &&
351 if test_check_one_tag_ ${4-$1}
352 then
353 git tag ${4-$1}
357 # Call test_merge with the arguments "<message> [<opt>...] <commit>", where
358 # <commit> can be a tag pointing to the commit-to-merge, but automatically skip
359 # the tag if <message> is not tagish and supply --allow-unrelated-histories
360 # when running Git >= 2.9.
362 test_merge() {
363 test_tick &&
364 git merge $test_auh -m "$@" &&
365 if test_check_one_tag_ $1
366 then
367 git tag $1
371 # This function helps systems where core.filemode=false is set.
372 # Use it instead of plain 'chmod +x' to set or unset the executable bit
373 # of a file in the working directory and add it to the index.
375 test_chmod() {
376 chmod "$@" &&
377 git update-index --add "--chmod=$@"
380 # Unset a configuration variable, but don't fail if it doesn't exist.
381 test_unconfig() {
382 config_dir=
383 if test "$1" = -C
384 then
385 shift
386 config_dir=$1
387 shift
389 eval git "${config_dir:+-C \"\$config_dir\"}" config --unset-all '"$@"'
390 config_status=$?
391 case "$config_status" in
392 5) # ok, nothing to unset
393 config_status=0
395 esac
396 return $config_status
399 # Set git config, automatically unsetting it after the test is over.
400 test_config() {
401 config_dir=
402 if test "$1" = -C
403 then
404 shift
405 config_dir="$1"
406 shift
408 eval test_when_finished test_unconfig "${config_dir:+-C \"\$config_dir\"}" '"$1"' &&
409 eval git "${config_dir:+-C \"\$config_dir\"}" config '"$@"'
412 test_config_global() {
413 test_when_finished test_unconfig --global "$1" &&
414 git config --global "$@"
417 write_script() {
419 echo "#!${2-"$SHELL_PATH"}" &&
421 } >"$1" &&
422 chmod +x "$1"
425 # Use test_set_prereq to tell that a particular prerequisite is available.
426 # The prerequisite can later be checked for in two ways:
428 # - Explicitly using test_have_prereq.
430 # - Implicitly by specifying the prerequisite tag in the calls to
431 # test_expect_{success,failure,code}.
433 # The single parameter is the prerequisite tag (a simple word, in all
434 # capital letters by convention).
436 test_set_prereq() {
437 satisfied_prereq="$satisfied_prereq$1 "
440 # Usage: test_lazy_prereq PREREQ 'script'
441 test_lazy_prereq() {
442 lazily_testable_prereq="$lazily_testable_prereq$1 "
443 eval test_prereq_lazily_$1=\$2
446 test_ensure_git_dir_() {
447 git rev-parse --git-dir >/dev/null 2>&1 ||
448 git_init --quiet --template="$EMPTY_DIRECTORY" >/dev/null 2>&1 ||
449 fatal "cannot run git init"
452 test_run_lazy_prereq_() {
453 script='
454 test_ensure_temp_dir_ "test_run_lazy_prereq_" "prereq-test-dir" &&
456 cd "$TRASHTMP_DIRECTORY/prereq-test-dir" &&'"$2"'
458 say >&3 "checking prerequisite: $1"
459 say >&3 "$script"
460 test_eval_ "$script"
461 eval_ret=$?
462 rm -rf "$TRASHTMP_DIRECTORY/prereq-test-dir"
463 if test "$eval_ret" = 0; then
464 say >&3 "prerequisite $1 ok"
465 else
466 say >&3 "prerequisite $1 not satisfied"
468 return $eval_ret
471 test_have_prereq() {
472 # prerequisites can be concatenated with ',' or whitespace
473 save_IFS="$IFS"
474 _tab=' '
475 _nl='
477 IFS=", $_tab$_nl"
478 set -- $*
479 IFS="$save_IFS"
481 total_prereq=0
482 ok_prereq=0
483 missing_prereq=
485 for prerequisite
487 case "$prerequisite" in
489 negative_prereq=t
490 prerequisite=${prerequisite#!}
493 negative_prereq=
494 esac
496 case " $lazily_tested_prereq " in
497 *" $prerequisite "*)
500 case " $lazily_testable_prereq " in
501 *" $prerequisite "*)
502 eval "script=\$test_prereq_lazily_$prerequisite" &&
503 if test_run_lazy_prereq_ "$prerequisite" "$script"
504 then
505 test_set_prereq $prerequisite
507 lazily_tested_prereq="$lazily_tested_prereq$prerequisite "
508 esac
510 esac
512 total_prereq=$(($total_prereq + 1))
513 satisfied_this_prereq=
514 if test "$prerequisite" = "LASTOK"
515 then
516 if test -n "$test_last_subtest_ok"
517 then
518 satisfied_this_prereq=t
520 elif test "$prerequisite" = "GITSHA1"
521 then
522 if test "${test_hash_algo:-sha1}" = "sha1"
523 then
524 satisfied_this_prereq=t
526 else
527 case "$satisfied_prereq" in
528 *" $prerequisite "*)
529 satisfied_this_prereq=t
530 esac
533 case "$satisfied_this_prereq,$negative_prereq" in
534 t,|,t)
535 ok_prereq=$(($ok_prereq + 1))
538 # Keep a list of missing prerequisites; restore
539 # the negative marker if necessary.
540 prerequisite=${negative_prereq:+!}$prerequisite
541 if test -z "$missing_prereq"
542 then
543 missing_prereq=$prerequisite
544 else
545 missing_prereq="$missing_prereq, $prerequisite"
547 esac
548 done
550 test $total_prereq = $ok_prereq
553 test_declared_prereq() {
554 case " $test_prereq " in
555 *" $1 "*)
556 return 0
558 esac
559 return 1
562 test_verify_prereq() {
563 test -z "$test_prereq" ||
564 test "x$test_prereq" = "x${test_prereq#*[!A-Z0-9_ !]}" ||
565 error "bug in the test script: '$test_prereq' does not look like a prereq"
568 _test_set_test_prereq() {
569 test_prereq_fmt=
570 save_IFS="$IFS"
571 _tab=' '
572 _nl='
574 IFS=", $_tab$_nl"
575 set -- $*
576 IFS="$save_IFS"
577 test_prereq="$*"
578 while test "$#" != "0"
580 test_prereq_fmt="${test_prereq_fmt:+$test_prereq_fmt, }$1"
581 shift
582 done
585 test_expect_failure_lno() {
586 callerlno="$1"
587 shift
588 test_start_
589 test "$#" = 3 && { _test_set_test_prereq "$1"; shift; } || test_prereq=
590 test "$#" = 2 ||
591 error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
592 test_get_ "$2"
593 set -- "$1" "$test_script_"
594 test_verify_prereq
595 export test_prereq
596 if ! test_skip "$@"
597 then
598 say >&3 "checking known breakage: $2"
599 if test_run_ "$2" expecting_failure
600 then
601 test_known_broken_ok_ "$1"
602 test_last_subtest_ok=1
603 else
604 test_known_broken_failure_ "$1"
605 test_last_subtest_ok=
608 test_finish_
609 unset_ callerlno
611 test_expect_failure() {
612 test_expect_failure_lno "" "$@"
614 alias test_expect_failure='test_expect_failure_lno "$LINENO"' >/dev/null 2>&1 || :
616 if test -n "$TESTLIB_NO_TOLERATE"; then
617 test_tolerate_failure_lno() { test_expect_success_lno "$@"; }
618 else
619 test_tolerate_failure_lno() {
620 callerlno="$1"
621 shift
622 test_start_
623 test "$#" = 3 && { _test_set_test_prereq "$1"; shift; } || test_prereq=
624 test "$#" = 2 ||
625 error "bug in the test script: not 2 or 3 parameters to test-tolerate-failure"
626 test_get_ "$2"
627 set -- "$1" "$test_script_"
628 test_verify_prereq
629 export test_prereq
630 if ! test_skip "$@"
631 then
632 say >&3 "checking possible breakage: $2"
633 if test_run_ "$2" tolerating_failure
634 then
635 test_possibly_broken_ok_ "$1"
636 test_last_subtest_ok=1
637 else
638 test_possibly_broken_failure_ "$1"
639 test_last_subtest_ok=
642 test_finish_
643 unset_ callerlno
646 test_tolerate_failure() {
647 test_tolerate_failure_lno "" "$@"
649 alias test_tolerate_failure='test_tolerate_failure_lno "$LINENO"' >/dev/null 2>&1 || :
651 test_expect_success_lno() {
652 callerlno="$1"
653 shift
654 test_start_
655 test "$#" = 3 && { _test_set_test_prereq "$1"; shift; } || test_prereq=
656 test "$#" = 2 ||
657 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
658 test_get_ "$2"
659 set -- "$1" "$test_script_"
660 test_verify_prereq
661 export test_prereq
662 if ! test_skip "$@"
663 then
664 say >&3 "expecting success: $2"
665 if test_run_ "$2"
666 then
667 test_ok_ "$1"
668 test_last_subtest_ok=1
669 else
670 test_failure_ "$callerlno" "$@"
671 test_last_subtest_ok=
674 test_finish_
675 unset_ callerlno
677 test_expect_success() {
678 test_expect_success_lno "" "$@"
680 alias test_expect_success='test_expect_success_lno "$LINENO"' >/dev/null 2>&1 || :
682 # test_external runs external test scripts that provide continuous
683 # test output about their progress, and succeeds/fails on
684 # zero/non-zero exit code. It outputs the test output on stdout even
685 # in non-verbose mode, and announces the external script with "# run
686 # <n>: ..." before running it. When providing relative paths, keep in
687 # mind that all scripts run in "trash directory".
688 # Usage: test_external description command arguments...
689 # Example: test_external 'Awk API' awk -f ../path/to/test.awk
690 test_external_lno() {
691 callerlno="$1"
692 shift
693 test_count=$(($test_count+1))
694 test "$#" = 4 && { _test_set_test_prereq "$1"; shift; } || test_prereq=
695 test "$#" = 3 ||
696 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
697 descr="$1"
698 shift
699 test_verify_prereq
700 export test_prereq
701 test_external_skipped=1
702 if ! test_skip "$descr" "$@"
703 then
704 # Announce the script to reduce confusion about the
705 # test output that follows.
706 say_color "" "# run $test_count: $descr ($*)"
707 # Export TEST_DIRECTORY, TRASH_DIRECTORY, TRASHTMP_DIRECTORY
708 # and TESTLIB_TEST_LONG to be able to use them in script
709 export TEST_DIRECTORY TRASH_DIRECTORY TRASHTMP_DIRECTORY TESTLIB_TEST_LONG
710 # Run command; redirect its stderr to &4 as in
711 # test_run_, but keep its stdout on our stdout even in
712 # non-verbose mode.
713 "$@" 2>&4
714 if test "$?" = 0
715 then
716 if test $test_external_has_tap -eq 0; then
717 test_ok_ "$descr"
718 else
719 say_color "" "# test_external test $descr was ok"
720 test_success=$(($test_success + 1))
722 test_last_subtest_ok=1
723 else
724 if test $test_external_has_tap -eq 0; then
725 test_failure_ "$callerlno" "$descr" "$@"
726 else
727 say_color error "# test_external test $descr failed: $@"
728 test_failure=$(($test_failure + 1))
730 test_last_subtest_ok=
732 test_external_skipped=
734 unset_ callerlno
736 test_external() {
737 test_external_lno "" "$@"
739 alias test_external='test_external_lno "$LINENO"' >/dev/null 2>&1 || :
741 # Like test_external, but in addition tests that the command generated
742 # no output on stderr.
743 test_external_without_stderr_lno() {
744 callerlno="$1"
745 shift
746 # The temporary file has no (and must have no) security
747 # implications.
748 tmp=${TMPDIR:-/tmp}
749 stderr="$tmp/git-external-stderr.$$.tmp"
750 test_external_lno "$callerlno" "$@" 4> "$stderr"
751 test -f "$stderr" || error "Internal error: $stderr disappeared."
752 descr="no stderr: $1"
753 shift
754 test_count=$(($test_count+1))
755 say >&3 "# expecting no stderr from previous command"
756 if test -n "$test_external_skipped" || test ! -s "$stderr"
757 then
758 rm "$stderr"
760 if test $test_external_has_tap -eq 0; then
761 test_ok_ "$descr"
762 else
763 say_color "" "# test_external_without_stderr test $descr was ok"
764 test_success=$(($test_success + 1))
766 else
767 test_last_subtest_ok=
768 if test "$verbose" = t
769 then
770 output=$(echo; echo "# Stderr is:"; cat "$stderr")
771 else
772 output=
774 # rm first in case test_failure exits.
775 rm "$stderr"
776 if test $test_external_has_tap -eq 0; then
777 test_failure_ "$callerlno" "$descr" "$@" "$output"
778 else
779 say_color error "# test_external_without_stderr test $descr failed: $@: $output"
780 test_failure=$(($test_failure + 1))
783 unset_ callerlno
785 test_external_without_stderr() {
786 test_external_without_stderr_lno "" "$@"
788 alias test_external_without_stderr='test_external_without_stderr_lno "$LINENO"' >/dev/null 2>&1 || :
790 # debugging-friendly alternatives to "test [-f|-d|-e]"
791 # The commands test the existence or non-existence of $1. $2 can be
792 # given to provide a more precise diagnosis.
793 test_path_is_file() {
794 if ! test -f "$1"
795 then
796 echo "File $1 doesn't exist. $2"
797 false
801 test_path_is_dir() {
802 if ! test -d "$1"
803 then
804 echo "Directory $1 doesn't exist. $2"
805 false
809 # Check if the directory exists and is empty as expected, barf otherwise.
810 test_dir_is_empty() {
811 test_path_is_dir "$1" &&
812 if test -n "$(ls -a1 "$1" | grep -E -v '^\.\.?$')"
813 then
814 echo "Directory '$1' is not empty, it contains:"
815 ls -la "$1"
816 return 1
820 test_path_is_missing() {
821 if test -e "$1"
822 then
823 echo "Path exists:"
824 ls -ld "$1"
825 if test $# -ge 1
826 then
827 echo "$*"
829 false
833 wc() {
834 wc_=0 &&
837 wc_vals_="$(command wc "$@")" &&
838 set -- $wc_vals_ &&
839 echo "$*"
840 } || wc_=$?
841 } &&
842 set -- "$wc_" &&
843 unset_ wc_ wc_vals_ &&
844 return $1
847 # test_line_count checks that a file has the number of lines it
848 # ought to. For example:
850 # test_expect_success 'produce exactly one line of output' '
851 # do something >output &&
852 # test_line_count = 1 output
855 # is like "test $(wc -l <output) = 1" except that it passes the
856 # output through when the number of lines is wrong.
858 test_line_count() {
859 if test $# != 3
860 then
861 error "bug in the test script: not 3 parameters to test_line_count"
862 elif ! test $(wc -l <"$3") "$1" "$2"
863 then
864 echo "test_line_count: line count for $3 !$1 $2"
865 cat "$3"
866 return 1
870 # Returns success if a comma separated string of keywords ($1) contains a
871 # given keyword ($2).
872 # Examples:
873 # `list_contains "foo,bar" bar` returns 0
874 # `list_contains "foo" bar` returns 1
876 list_contains() {
877 case ",$1," in
878 *,$2,*)
879 return 0
881 esac
882 return 1
885 # This is not among top-level (test_expect_success | test_expect_failure)
886 # but is a prefix that can be used in the test script, like:
888 # test_expect_success 'complain and die' '
889 # do something &&
890 # do something else &&
891 # test_must_fail git checkout ../outerspace
894 # Writing this as "! git checkout ../outerspace" is wrong, because
895 # the failure could be due to a segv. We want a controlled failure.
897 test_must_fail() {
898 case "$1" in
899 ok=*)
900 _test_ok=${1#ok=}
901 shift
904 _test_ok=
906 esac
907 exit_code=0
908 "$@" ||
909 exit_code=$?
910 if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
911 then
912 echo >&2 "test_must_fail: command succeeded: $*"
913 return 1
914 elif test_match_signal 13 $exit_code && list_contains "$_test_ok" sigpipe
915 then
916 return 0
917 elif test $exit_code -gt 129 && test $exit_code -le 192
918 then
919 echo >&2 "test_must_fail: died by signal $(($exit_code - 128)): $*"
920 return 1
921 elif test $exit_code -eq 127
922 then
923 echo >&2 "test_must_fail: command not found: $*"
924 return 1
925 elif test $exit_code -eq 126
926 then
927 echo >&2 "test_must_fail: command not executable: $*"
928 return 1
930 return 0
933 # Similar to test_must_fail, but tolerates success, too. This is
934 # meant to be used in contexts like:
936 # test_expect_success 'some command works without configuration' '
937 # test_might_fail git config --unset all.configuration &&
938 # do something
941 # Writing "git config --unset all.configuration || :" would be wrong,
942 # because we want to notice if it fails due to segv.
944 test_might_fail() {
945 test_must_fail ok=success "$@"
948 # Similar to test_must_fail and test_might_fail, but check that a
949 # given command exited with a given exit code. Meant to be used as:
951 # test_expect_success 'Merge with d/f conflicts' '
952 # test_expect_code 1 git merge "merge msg" B master
955 test_expect_code() {
956 want_code=$1 &&
957 shift &&
958 exit_code=0 &&
959 "$@" ||
960 exit_code=$?
961 if test $exit_code = $want_code
962 then
963 return 0
966 echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
967 return 1
970 # test_cmp is a helper function to compare actual and expected output.
971 # You can use it like:
973 # test_expect_success 'foo works' '
974 # echo expected >expected &&
975 # foo >actual &&
976 # test_cmp expected actual
979 # This could be written as either "cmp" or "diff -u", but:
980 # - cmp's output is not nearly as easy to read as diff -u
981 # - not all diff versions understand "-u"
983 test_cmp() {
984 $TESTLIB_TEST_CMP "$@"
987 # test_cmp_bin - helper to compare binary files
989 test_cmp_bin() {
990 cmp "$@"
993 # Use git diff --no-index for the diff (with a few supporting options)
994 test_diff() {
995 git --git-dir="$EMPTY_DIRECTORY" --no-pager -c core.abbrev=16 diff --no-color --exit-code --no-prefix --no-index "$@"
998 # Call any command "$@" but be more verbose about its
999 # failure. This is handy for commands like "test" which do
1000 # not output anything when they fail.
1001 verbose() {
1002 "$@" && return 0
1003 echo >&2 "command failed: $(git rev-parse --sq-quote "$@")"
1004 return 1
1007 # Check if the file expected to be empty is indeed empty, and barfs
1008 # otherwise.
1010 test_must_be_empty() {
1011 if test -s "$1"
1012 then
1013 echo "'$1' is not empty, it contains:"
1014 cat "$1"
1015 return 1
1019 # Tests that its two parameters refer to the same revision
1020 test_cmp_rev() {
1021 test_ensure_temp_dir_ "test_cmp_rev"
1022 git rev-parse --verify "$1" -- >"$TRASHTMP_DIRECTORY/expect.rev" &&
1023 git rev-parse --verify "$2" -- >"$TRASHTMP_DIRECTORY/actual.rev" &&
1024 test_cmp "$TRASHTMP_DIRECTORY/expect.rev" "$TRASHTMP_DIRECTORY/actual.rev"
1027 # Print a sequence of integers in increasing order, either with
1028 # two arguments (start and end):
1030 # test_seq 1 5 -- outputs 1 2 3 4 5 one line at a time
1032 # or with one argument (end), in which case it starts counting
1033 # from 1.
1035 test_seq() {
1036 case $# in
1037 1) set 1 "$@" ;;
1038 2) ;;
1039 *) error "bug in the test script: not 1 or 2 parameters to test_seq" ;;
1040 esac
1041 test_seq_counter__=$1
1042 while test "$test_seq_counter__" -le "$2"
1044 echo "$test_seq_counter__"
1045 test_seq_counter__=$(( $test_seq_counter__ + 1 ))
1046 done
1049 # This function can be used to schedule some commands to be run
1050 # unconditionally at the end of the test to restore sanity:
1052 # test_expect_success 'test core.capslock' '
1053 # git config core.capslock true &&
1054 # test_when_finished git config --unset core.capslock &&
1055 # hello world
1058 # That would be roughly equivalent to
1060 # test_expect_success 'test core.capslock' '
1061 # git config core.capslock true &&
1062 # hello world
1063 # git config --unset core.capslock
1066 # except that the greeting and config --unset must both succeed for
1067 # the test to pass.
1069 # Note that under --immediate mode, no clean-up is done to help diagnose
1070 # what went wrong.
1072 test_when_finished() {
1073 test z"$*" != z && test -z "$linting" || return 0
1074 test_ensure_temp_dir_ "test_when_finished"
1075 twf_script_="$TRASHTMP_DIRECTORY/test_when_finished_${test_count:-0}.sh"
1076 twf_cmd_=
1077 for twf_arg_ in "$@"; do
1078 twf_dq_=1
1079 case "$twf_arg_" in [A-Za-z_]*)
1080 if test z"${twf_arg_%%[!A-Za-z_0-9]*}" = z"$twf_arg_"
1081 then
1082 twf_arg_sq_="$twf_arg_"
1083 twf_dq_=
1084 else case "$twf_arg_" in *=*)
1085 twf_var_="${twf_arg_%%=*}"
1086 if test z"${twf_var_%%[!A-Za-z_0-9]*}" = z"$twf_var_"
1087 then
1088 test_quotevar_ 3 twf_arg_sq_ "${twf_arg_#*=}"
1089 twf_arg_sq_="$twf_var_=$twf_arg_sq_"
1090 twf_dq_=
1092 esac; fi
1093 esac
1094 test z"$twf_dq_" = z || test_quotevar_ twf_arg_ twf_arg_sq_
1095 twf_cmd_="${twf_cmd_:+$twf_cmd_ }$twf_arg_sq_"
1096 done
1097 printf '{ %s\n} && (exit "$eval_ret"); eval_ret=$?\n' "$twf_cmd_" >>"$twf_script_"
1100 # clear out any test_when_finished items scheduled so far in this subtest
1101 test_clear_when_finished() {
1102 tcwf_script_="$TRASHTMP_DIRECTORY/test_when_finished_${test_count:-0}.sh"
1103 ! test -e "$tcwf_script_" || {
1104 rm -f "$tcwf_script_" &&
1105 ! test -e "$tcwf_script_"
1109 # Most tests can use the created repository, but some may need to create more.
1110 # Usage: test_create_repo <directory>
1111 test_create_repo() {
1112 test "$#" = 1 ||
1113 error "bug in the test script: not 1 parameter to test-create-repo"
1114 repo="$1"
1115 mkdir -p "$repo"
1117 cd "$repo" || error "Cannot setup test environment"
1118 git_init --quiet "--template=$EMPTY_DIRECTORY" >&3 2>&4 ||
1119 error "cannot run git init -- have you built things yet?"
1120 ! [ -e .git/hooks ] || mv .git/hooks .git/hooks-disabled
1121 ) || exit
1124 # This function helps on symlink challenged file systems when it is not
1125 # important that the file system entry is a symbolic link.
1126 # Use test_ln_s_add instead of "ln -s x y && git add y" to add a
1127 # symbolic link entry y to the index.
1129 test_ln_s_add() {
1130 if test_have_prereq SYMLINKS
1131 then
1132 ln -s "$1" "$2" &&
1133 git update-index --add "$2"
1134 else
1135 printf '%s' "$1" >"$2" &&
1136 ln_s_obj=$(git hash-object -w "$2") &&
1137 git update-index --add --cacheinfo 120000 $ln_s_obj "$2" &&
1138 # pick up stat info from the file
1139 git update-index "$2"
1143 # This function writes out its parameters, one per line
1144 test_write_lines() {
1145 printf '%s\n' "$@"
1148 awk() (
1149 { "unset" -f awk; } >/dev/null 2>&1 || :
1150 "exec" "$AWK_PATH" "$@"
1153 git() (
1154 { "unset" -f git; } >/dev/null 2>&1 || :
1155 "exec" "$GIT_PATH" "$@"
1158 perl_lno() (
1159 : "${callerlno:=$1}"
1160 shift
1161 perlerr_() { return 70; } # EX_SOFTWARE
1162 perlerr_ || die "${0##*/}:${callerlno:+$callerlno:} test suite attempted to use perl"
1163 exit 70 # EX_SOFTWARE
1165 perl() {
1166 perl_lno "" "$@"
1168 alias perl='perl_lno "$LINENO"' >/dev/null 2>&1 || :
1170 # Given a variable $1, normalize the value of it to one of "true",
1171 # "false", or "auto" and store the result to it.
1173 # test_tristate TESTLIB_TEST_FLIBITY
1175 # A variable set to an empty string is set to 'false'.
1176 # A variable set to 'false' or 'auto' keeps its value.
1177 # Anything else is set to 'true'.
1178 # An unset variable defaults to 'auto'.
1180 # The last rule is to allow people to set the variable to an empty
1181 # string and export it to decline testing the particular feature
1182 # for versions both before and after this change. We used to treat
1183 # both unset and empty variable as a signal for "do not test" and
1184 # took any non-empty string as "please test".
1186 test_tristate() {
1187 if eval "test \"z\${$1+set}\" = \"zset\""
1188 then
1189 # explicitly set
1190 eval "set -- \"\$1\" \"\$$1\""
1191 case "$2" in [-+]0?*|0?*)
1192 set -- "$1" "${2#[-+]}"
1193 set -- "$1" "$2" "${2%%[!0]*}"
1194 set -- "$1" "${2#"$3"}"
1195 esac
1196 case "$2" in
1197 [Aa][Uu][Tt][Oo])
1198 set "$1" "auto";;
1199 ""|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|[Nn][Oo]|[-+]0|0)
1200 set "$1" "false";;
1202 # Git is actually more picky than this in that
1203 # only true/on/yes/(int)!=0 qualify else an err
1204 # but the original code treated those as true
1205 set "$1" "true";;
1206 esac
1207 eval "$1=\"\$2\""
1208 else
1209 eval "$1=\"auto\""
1213 # Exit the test suite, either by skipping all remaining tests or by
1214 # exiting with an error. If "$1" is "auto", we then we assume we were
1215 # opportunistically trying to set up some tests and we skip. If it is
1216 # "true", then we report a failure.
1218 # The error/skip message should be given by $2.
1220 test_skip_or_die() {
1221 case "$1" in
1222 auto)
1223 skip_all=$2
1224 test_done
1226 true)
1227 error "$2"
1230 error "BUG: test tristate is '$1' (real error: $2)"
1231 esac
1234 # The following mingw_* functions obey POSIX shell syntax, but are actually
1235 # bash scripts, and are meant to be used only with bash on Windows.
1237 # A test_cmp function that treats LF and CRLF equal and avoids forking
1238 # diff when possible.
1239 mingw_test_cmp() {
1240 # Read text into shell variables and compare them. If the results
1241 # are different, use regular diff to report the difference.
1242 test_cmp_a= test_cmp_b=
1244 # When text came from stdin (one argument is '-') we must feed it
1245 # to diff.
1246 stdin_for_diff=
1248 # Since it is difficult to detect the difference between an
1249 # empty input file and a failure to read the files, we go straight
1250 # to diff if one of the inputs is empty.
1251 if test -s "$1" && test -s "$2"
1252 then
1253 # regular case: both files non-empty
1254 mingw_read_file_strip_cr_ test_cmp_a <"$1"
1255 mingw_read_file_strip_cr_ test_cmp_b <"$2"
1256 elif test -s "$1" && test "$2" = -
1257 then
1258 # read 2nd file from stdin
1259 mingw_read_file_strip_cr_ test_cmp_a <"$1"
1260 mingw_read_file_strip_cr_ test_cmp_b
1261 stdin_for_diff='<<<"$test_cmp_b"'
1262 elif test "$1" = - && test -s "$2"
1263 then
1264 # read 1st file from stdin
1265 mingw_read_file_strip_cr_ test_cmp_a
1266 mingw_read_file_strip_cr_ test_cmp_b <"$2"
1267 stdin_for_diff='<<<"$test_cmp_a"'
1269 test -n "$test_cmp_a" &&
1270 test -n "$test_cmp_b" &&
1271 test "$test_cmp_a" = "$test_cmp_b" ||
1272 eval "diff -u \"\$@\" $stdin_for_diff"
1275 # $1 is the name of the shell variable to fill in
1276 mingw_read_file_strip_cr_() {
1277 # Read line-wise using LF as the line separator
1278 # and use IFS to strip CR.
1279 line_=
1280 while :
1282 if IFS=$'\r' read -r -d $'\n' line_
1283 then
1284 # good
1285 line_=$line_$'\n'
1286 else
1287 # we get here at EOF, but also if the last line
1288 # was not terminated by LF; in the latter case,
1289 # some text was read
1290 if test -z "$line_"
1291 then
1292 # EOF, really
1293 break
1296 eval "$1=\$$1\$line_"
1297 done
1300 # Like "env FOO=BAR some-program", but run inside a subshell, which means
1301 # it also works for shell functions (though those functions cannot impact
1302 # the environment outside of the test_env invocation).
1303 test_env() {
1305 while test $# -gt 0
1307 case "$1" in
1308 *=*)
1309 eval "${1%%=*}=\${1#*=}"
1310 eval "export ${1%%=*}"
1311 shift
1314 "$@"
1315 exit
1317 esac
1318 done
1322 # Returns true if the numeric exit code in "$2" represents the expected signal
1323 # in "$1". Signals should be given numerically.
1324 test_match_signal() {
1325 if test "$2" = "$((128 + $1))"
1326 then
1327 # POSIX
1328 return 0
1329 elif test "$2" = "$((256 + $1))"
1330 then
1331 # ksh
1332 return 0
1334 return 1
1337 # Read up to "$1" bytes (or to EOF) from stdin and write them to stdout.
1338 test_copy_bytes() {
1339 dd bs=1 count="$1" 2>/dev/null
1343 # THIS SHOULD ALWAYS BE THE LAST FUNCTION DEFINED IN THIS FILE
1345 # Any client that sources this file should immediately execute this function
1346 # afterwards.
1348 # THERE SHOULD NOT BE ANY DIRECTLY EXECUTED LINES OF CODE IN THIS FILE
1350 test_lib_functions_init() {
1351 satisfied_prereq=" "
1352 lazily_testable_prereq= lazily_tested_prereq=