1 # Test framework from Git with modifications.
3 # Modifications Copyright (C) 2016,2017 Kyle J. McKay. All rights reserved.
6 # * Many "GIT_..." variables removed -- some were kept as TESTLIB_..." instead
7 # (Except "GIT_PATH" is new and is the full path to a "git" executable)
9 # * IMPORTANT: test-lib-main.sh SHOULD NOT EXECUTE ANY CODE! A new
10 # function "test_lib_main_init" has been added that will be called
11 # and MUST contain any lines of code to be executed. This will ALWAYS
12 # be the LAST function defined in this file for easy locatability.
14 # * Added cmd_path, _?fatal, whats_the_dir, vcmp, getcmd, say_tap, say_color_tap,
15 # fail_, test_possibly_broken_ok_ and test_possibly_broken_failure_ functions
17 # * Anything related to valgrind or perf has been stripped out
19 # * Many other minor changes
21 # Copyright (C) 2005 Junio C Hamano
23 # This program is free software: you can redistribute it and/or modify
24 # it under the terms of the GNU General Public License as published by
25 # the Free Software Foundation, either version 2 of the License, or
26 # (at your option) any later version.
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 # GNU General Public License for more details.
33 # You should have received a copy of the GNU General Public License
34 # along with this program. If not, see http://www.gnu.org/licenses/ .
37 ## IMPORTANT: THIS FILE MUST NOT CONTAIN ANYTHING OTHER THAN FUNCTION
38 ## DEFINITION!!! INITIALIZATION GOES IN THE LAST FUNCTION
39 ## DEFINED IN THIS FILE "test_lib_main_init" AS REQUIRED!
43 { "unset" -f command unset unalias "$1"; } >/dev
/null
2>&1 ||
:
44 { "unalias" -a ||
unalias -m "*"; } >/dev
/null
2>&1 ||
:
49 printf '%s\n' "$*" >&2
53 fatal
() { _fatal
"$@"; }
55 # usage: cmdget <varname> <cmd> [<arg>...]
56 # return code is that of <cmd> [<arg...]
57 # <varname> is set to VERBATIM <cmd> output (except NULs may not be handled)
59 [ -n "$1" ] ||
return 1
60 eval "$1=" >/dev
/null
2>&1 ||
return 1
61 [ -n "$2" ] ||
return 1
65 _getcmd_result
="$(ec=0; ("$@
") || ec=$?; echo Z; exit $ec)" || _getcmd_ec
=$?
66 eval "$_getcmd_vn=\"\${_getcmd_result%Z}\""
70 # usage: whats_the_dir [-P | -L] [--] path-to-something varname
71 # determine path-to-something's directory and store it into varname
72 # without "-P" or "-L" a relative dirname may be returned
74 # determine "$1"'s directory and store it into the var name passed as "$2"
75 if [ "z$1" = "z-P" -o "z$1" = "z-L" ]; then
76 if [ "z$2" = "z--" ]; then
81 elif [ "z$1" = "z--" ]; then
84 case "$1" in *"/"*);;*) set -- "./$1" "$2" "$3"; esac
86 set -- "$(readlink "$1")" "$2" "$3" "$1"
87 case "$1" in "/"*);;*)
88 set -- "${4%/*}/$1" "$2" "$3"
91 set -- "${1%/*}" "$2" "$3"
92 if [ "z$3" != "z" ] && [ -d "$1" ] &&
93 ! case "$1" in [!/]*|
*"/./"*|
*"/."|
*"/../"*|
*"/..") ! :; esac; then
94 [ "z$3" = "z-P" ] ||
set -- "$1" "$2"
95 if [ "z$3" = "z" -a \
( "z$1" = "z." -o "z$1" = "z$PWD" \
) ]; then
98 set -- "$(cd "$1" && pwd $3)" "$2"
105 # Compare $1 to $3 each of which must match ^[^0-9]*\d*(\.\d*)*.*$
106 # where only the "\d*" parts in the regex participate in the comparison
107 # Since EVERY string matches that regex this function is easy to use
108 # An empty string ('') for $1 or $3 or any "\d*" part is treated as 0
109 # $2 is a compare op '<', '<=', '=', '==', '!=', '>=', '>'
110 # Return code is 0 for true, 1 for false (or unknown compare op)
111 # There is NO difference in behavior between '=' and '=='
112 # Note that "vcmp 1.8 == 1.8.0.0.0.0" correctly returns 0
113 set -- "$1" "$2" "$3" "${1%%[0-9]*}" "${3%%[0-9]*}"
114 set -- "${1#"$4"}" "$2" "${3#"$5"}"
115 set -- "${1%%[!0-9.]*}" "$2" "${3%%[!0-9.]*}"
119 [ "z$vcmp_a_" != "z" -o "z$vcmp_b_" != "z" ]
121 if [ "${vcmp_a_:-0}" -lt "${vcmp_b_:-0}" ]; then
122 unset_ vcmp_a_ vcmp_b_
123 case "$2" in "<"|
"<="|
"!=") return 0; esac
125 elif [ "${vcmp_a_:-0}" -gt "${vcmp_b_:-0}" ]; then
126 unset_ vcmp_a_ vcmp_b_
127 case "$2" in ">"|
">="|
"!=") return 0; esac
130 vcmp_a_
="${1#$vcmp_a_}"
131 vcmp_b_
="${3#$vcmp_b_}"
132 set -- "${vcmp_a_#.}" "$2" "${vcmp_b_#.}"
134 unset_ vcmp_a_ vcmp_b_
135 case "$2" in "="|
"=="|
"<="|
">=") return 0; esac
144 say_color error
"${LF}error: $*" >&7
145 printf '%s\n' "Bail out! ${0##*/}:${callerlno:+$callerlno:} error: $*" >&5
146 test_results_dir
="${TEST_OUTPUT_DIRECTORY:-.}/test-results"
147 mkdir
-p "$test_results_dir"
148 printf '%s\n' "Bail out! ${0##*/}:${callerlno:+$callerlno:} error: $*" >>"$test_results_dir/bailout"
150 [ -z "$TESTLIB_TEST_PARENT_INT_ON_ERROR" ] ||
{
152 perl
-e "kill(-2, getpgrp($TESTLIB_TEST_PARENT_INT_ON_ERROR), getpgrp($PPID), getpgrp($$), getpgrp(0))" ||
:
153 kill -INT $TESTLIB_TEST_PARENT_INT_ON_ERROR $PPID ||
:
161 alias error
='error_lno "$LINENO"' >/dev
/null
2>&1 ||
:
168 say_color_tap info
"$@"
172 code
=${EXITCODE_:-$?}
173 if test -n "$TESTLIB_EXIT_OK"
178 msg
="${msg:+($code) }$msg"
179 [ -n "$msg" ] || msg
=" Unexpected exit with code $code"
180 echo >&5 "FATAL:$msg"
186 # You are not expected to call test_ok_ and test_failure_ directly, use
187 # the test_expect_* functions instead.
190 test_success
=$
(($test_success + 1))
191 say_color_tap
"" "ok $test_count - $@"
195 test_failure
=$
(($test_failure + 1))
198 say_color_tap error
"not ok $test_count - $1"
199 if test z
"${TESTLIB_TEST_TAP_ONLY:-0}" != z
"0"; then
201 say_color_tap
"" "# failed: ${0##*/}${tlno:+:$tlno}: $test_count - $1"
203 ttit
="$test_count - $1"
205 printf '%s\n' "$(printf '%s\n' "failed
: ${0##*/}${tlno:+:$tlno}: $ttit$LF$
*")" |
227 test "$immediate" = "" ||
{ TESTLIB_EXIT_OK
=t
; exit 1; }
230 test_known_broken_ok_
() {
231 test_fixed
=$
(($test_fixed + 1))
232 say_color_tap warn
"ok $test_count - $@ # TODO known breakage vanished"
235 test_known_broken_failure_
() {
236 test_broken
=$
(($test_broken + 1))
237 say_color_tap warn
"not ok $test_count - $@ # TODO known breakage"
240 test_possibly_broken_ok_
() {
241 test_success
=$
(($test_success + 1))
242 say_color_tap
"" "ok $test_count - $@"
245 test_possibly_broken_failure_
() {
246 test_broken
=$
(($test_broken + 1))
247 say_color_tap warn
"not ok $test_count - $@ # TODO tolerated breakage"
251 test "$debug" = "" ||
test $# -eq 0 ||
test -z "$*" ||
{ "$@"; } >&7 2>&1
254 match_pattern_list
() {
257 test -z "$*" && return 1
268 match_test_selector_list
() {
273 test -z "$1" && return 0
275 # Both commas and whitespace are accepted as separators.
281 # If the first selector is negative we include by default.
289 orig_selector
=$selector
295 selector
=${selector##?}
299 test -z "$selector" && continue
303 if x_
="${selector%%-*}" && test "z$x_" != "z${x_#*[!0-9]}"
305 echo "error: $title: invalid non-numeric in range" \
306 "start: '$orig_selector'" >&2
309 if x_
="${selector#*-}" && test "z$x_" != "z${x_#*[!0-9]}"
311 echo "error: $title: invalid non-numeric in range" \
312 "end: '$orig_selector'" >&2
318 if test "z$selector" != "z${selector#*[!0-9]}"
320 echo "error: $title: invalid non-numeric in test" \
321 "selector: '$orig_selector'" >&2
326 # Short cut for "obvious" cases
327 test -z "$include" && test -z "$positive" && continue
328 test -n "$include" && test -n "$positive" && continue
332 if test $arg -le ${selector#-}
338 if test $arg -ge ${selector%-}
344 if test ${selector%%-*} -le $arg \
345 && test $arg -le ${selector#*-}
351 if test $arg -eq $selector
362 maybe_teardown_verbose
() {
363 test -z "$verbose_list" && return
364 exec 4>/dev
/null
3>/dev
/null
368 maybe_setup_verbose
() {
369 test -z "$verbose_list" && return
370 if match_test_selector_list
'--verbose-only' $test_count "$verbose_list"
372 if test "$verbose_log" = "t"
374 exec 3>>"$TESTLIB_TEST_TEE_OUTPUT_FILE" 4>&3
378 # Emit a delimiting blank line when going from
379 # non-verbose to verbose. Within verbose mode the
380 # delimiter is printed by test_expect_*. The choice
381 # of the initial $last_verbose is such that before
382 # test 1, we do not print it.
383 test -z "$last_verbose" && echo >&3 ""
386 exec 4>/dev
/null
3>/dev
/null
389 last_verbose
=$verbose
401 test "$trace" = t
&& test "$verbose" = t
404 # This is a separate function because some tests use
405 # "return" to end a test_expect_success block early
406 # (and we want to make sure we run any cleanup like
409 # Do not add anything extra (including LF) after '$*'
412 test_subshell_active_=t
413 ! want_trace || ! set -x && ! :
417 # Same thing as test_eval_inner_ but without the subshell
418 test_eval_inner_no_subshell_
() {
419 # Do not add anything extra (including LF) after '$*'
421 ! want_trace || ! set -x && ! :
426 # We run this block with stderr redirected to avoid extra cruft
427 # during a "-x" trace. Once in "set -x" mode, we cannot prevent
428 # the shell from printing the "set +x" to turn it off (nor the saving
429 # of $? before that). But we can make sure that the output goes to
432 # The test itself is run with stderr put back to &4 (so either to
433 # /dev/null, or to the original stderr if --verbose was used).
437 if test "${test_eval_ss_:-0}" = "0"
439 test_eval_inner_no_subshell_
"$@" </dev
/null
>&3 2>&4
441 test_eval_inner_
"$@" </dev
/null
>&3 2>&4
447 if test "$test_eval_ret_" != 0
449 say_color error
>&4 "error: last command exited with \$?=$test_eval_ret_"
453 return $test_eval_ret_
456 # Calls the real test_eval_ss_ with !"$TESTLIB_TEST_NO_SUBSHELL" as first arg
458 if test -n "$TESTLIB_TEST_NO_SUBSHELL"
460 test_eval_ss_
"0" "$@"
462 test_eval_ss_
"1" "$@"
466 # If "$1" = "-" read the script from stdin but ONLY if stdin is NOT a tty
467 # Store the test script in test_script_
471 ! test -t 0 || error
"test script is '-' but STDIN is a tty"
472 test_script_
="$(cat)"
473 test -n "$test_script_" || error
"test script is '-' but STDIN is empty"
474 test_script_
="$LF$test_script_$LF"
480 # protects against use of "return" in test_when_finished scripts
486 test z
"$2" = "z" ||
set +e
491 test_cleanup
="$TRASHTMP_DIRECTORY/test_when_finished_${test_count:-0}.sh"
492 ! test -e "$test_cleanup" ||
{
493 rm -f "$test_cleanup" &&
494 ! test -e "$test_cleanup" ||
495 error
"FATAL: Cannot prepare test area"
497 test_subshell_active_
=
501 if test "${TESTLIB_TEST_CHAIN_LINT:-1}" != 0; then
502 # turn off tracing for this test-eval, as it simply creates
503 # confusing noise in the "-x" output
507 # 117 is magic because it is unlikely to match the exit
508 # code of other programs
509 test_eval_ss_
"1" "fail_ 117 && $1${LF}fail_ \$? 1"
510 if test "$?" != 117; then
511 error
"bug in the test script: broken &&-chain: $1"
520 if test -z "$immediate" ||
test $eval_ret = 0 ||
521 test -n "$expecting_failure" && test -s "$test_cleanup"
523 test_eval_ss_
"0" 'test_do_script_ "$test_cleanup" && (exit $eval_ret); eval_ret=$?'
525 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
533 test_count
=$
(($test_count+1))
538 test z
"$to_skip" = z
"q" ||
echo >&3 ""
539 maybe_teardown_verbose
545 if match_pattern_list
$this_test.
$test_count $TESTLIB_SKIP_TESTS
548 skipped_reason
="TESTLIB_SKIP_TESTS"
550 if test -z "$to_skip" && test -n "$test_prereq" &&
551 ! test_have_prereq
"$test_prereq"
556 if test "$missing_prereq" != "$test_prereq"
558 of_prereq
=" of $test_prereq"
560 skipped_reason
="missing $missing_prereq${of_prereq}"
562 if test -z "$to_skip" && test -n "$run_list" &&
563 ! match_test_selector_list
'--run' $test_count "$run_list"
566 skipped_reason
="--run"
571 if test z
"$runquiet" = z ||
test z
"$skipped_reason" != z
"--run"; then
572 say_color skip
>&3 "skipping test: $@"
573 say_color_tap skip
"ok $test_count # skip $1 ($skipped_reason)"
585 # stub; runs at end of each successful test
586 test_at_end_hook_
() {
590 # returns 1 if counts do not match
591 # $1 is extra message, if any
592 test_done_write_plan_
() {
593 if test $test_external_has_tap -eq 0
596 test -z "$test_called_test_plan" &&
599 test z
"$test_count" != z
"0"
602 say_color_tap warn
"# please add test_plan call"
604 test -n "$test_wrote_plan_count" || say_tap
"1..$test_count$1"
606 if test -n "$test_wrote_plan_count" && test "$test_wrote_plan_count" -ne "$test_count"
608 say_color_tap error
"# $this_test plan count of $test_wrote_plan_count does not match run count of $test_count"
617 if test -z "$HARNESS_ACTIVE"
619 test_results_dir
="${TEST_OUTPUT_DIRECTORY:-.}/test-results"
620 mkdir
-p "$test_results_dir"
622 test_results_path
="$test_results_dir/${base%.sh}.counts"
624 cat >"$test_results_path" <<-EOF
626 success $test_success
634 if test "$test_fixed" != 0
636 say_color_tap error
"# $this_test $test_fixed known breakage(s) vanished; please update test(s)"
638 if test "$test_broken" != 0
640 say_color_tap warn
"# $this_test still have $test_broken known breakage(s)"
642 if test "$test_broken" != 0 ||
test "$test_fixed" != 0
644 test_remaining
=$
(( $test_count - $test_broken - $test_fixed ))
645 msg
="remaining $test_remaining test(s)"
647 test_remaining
=$test_count
648 msg
="$test_count test(s)"
650 case "$test_failure" in
652 # Maybe print SKIP message
653 if test -n "$skip_all" && test $test_count -gt 0
655 error
"Can't use skip_all after running some tests"
657 test -z "$skip_all" || skip_all
=" # SKIP $skip_all"
659 if test $test_external_has_tap -eq 0
661 if test $test_remaining -gt 0
663 say_color_tap pass
"# $this_test passed all $msg"
666 test_done_write_plan_
"$skip_all" ||
exit 1
668 test -n "$remove_trash" &&
669 test -d "$remove_trash" &&
670 cd "${remove_trash%/*}" &&
671 test_done_td_
="${remove_trash##*/}" &&
672 test -e "$test_done_td_" &&
673 { rm -rf "$test_done_td_" ||
:; } >/dev
/null
2>&1 &&
675 ! test -e "$test_done_td_" ||
{
676 chmod -R u
+rw
"$test_done_td_" &&
677 rm -rf "$test_done_td_"
680 test -n "$remove_trashtmp" &&
681 test -d "$remove_trashtmp" &&
682 cd "${remove_trashtmp%/*}" &&
683 test_done_td_
="${remove_trashtmp##*/}" &&
684 test -e "$test_done_td_" &&
685 { rm -rf "$test_done_td_" ||
:; } >/dev
/null
2>&1 &&
687 ! test -e "$test_done_td_" ||
{
688 chmod -R u
+rw
"$test_done_td_" &&
689 rm -rf "$test_done_td_"
698 if test $test_external_has_tap -eq 0
700 say_color_tap error
"# $this_test failed $test_failure among $msg"
702 test_done_write_plan_ ||
:
704 test -z "$HARNESS_ACTIVE" ||
exit 0
711 test z
"$test_called_test_plan" = z || fatal
"test_plan may not be called more than once"
712 test_called_test_plan
=1
713 test z
"$*" != z
"?" ||
return 0
714 test -n "$1" && test "z$1" = "z${1#*[!0-9]}" || fatal
"invalid test_plan argument: $1"
715 test "$1" -eq 0 ||
test z
"$1" = z
"$*" || fatal
"invalid test_plan arguments: $*"
716 if test "$1" -eq 0; then
719 skip_all
="${msg:-skip all tests in $this_test}"
722 if test z
"$runquiet" = z
; then
723 test $test_external_has_tap -ne 0 || say_tap
"1..$1"
724 test_wrote_plan_count
="$1"
728 # Provide an implementation of the 'yes' utility
745 run_with_limited_cmdline
() {
746 (ulimit -s 128 && "$@")
749 # internal use function
750 test_ensure_temp_dir_
() {
751 test z
"$TRASHTMP_DIRECTORY" != z ||
752 fatal
"${1:+$1 called but }TRASHTMP_DIRECTORY is not set yet"
753 test -d "$TRASHTMP_DIRECTORY" ||
{
754 mkdir
"$TRASHTMP_DIRECTORY" &&
755 test -d "$TRASHTMP_DIRECTORY"
756 } || fatal
"could not create temp directory \"$TRASHTMP_DIRECTORY\""
758 test -d "$TRASHTMP_DIRECTORY/$2" ||
{
759 mkdir
"$TRASHTMP_DIRECTORY/$2" &&
760 test -d "$TRASHTMP_DIRECTORY/$2"
761 } || fatal
"could not create temp subdirectory \"$TRASHTMP_DIRECTORY/$2\""
764 # test_get_temp [-d] [<name>]
765 # creates a new temporary file (or directory with -d) in the
766 # temporary directory $TRASHTMP_DIRECTORY with pattern prefix NAME
768 test_ensure_temp_dir_
"test_get_temp"
769 test z
"$1" != z
"-d" ||
set -- "$2" "$1"
770 mktemp
$2 "$TRASHTMP_DIRECTORY/${1:+$1.}XXXXXX"
775 ## Note that the following functions have bodies that are NOT indented
776 ## to assist with readability
780 test_lib_main_init_tee
() {
781 # Begin test_lib_main_init_tee
784 # if --tee was passed, write the output not only to the terminal, but
785 # additionally to the file test-results/$BASENAME.out, too.
786 case "$TESTLIB_TEST_TEE_STARTED, $* " in
788 # do not redirect again
790 *' --tee '*|
*' --verbose-log '*)
791 mkdir
-p "$TEST_OUTPUT_DIRECTORY/test-results"
792 BASE
="$TEST_OUTPUT_DIRECTORY/test-results/${0##*/}"
795 # Make this filename available to the sub-process in case it is using
797 TESTLIB_TEST_TEE_OUTPUT_FILE
=$BASE.out
798 export TESTLIB_TEST_TEE_OUTPUT_FILE
800 # Truncate before calling "tee -a" to get rid of the results
801 # from any previous runs.
802 >"$TESTLIB_TEST_TEE_OUTPUT_FILE"
805 (ec
=0; TESTLIB_TEST_TEE_STARTED
=done ${SHELL_PATH} "$0" "$@" 2>&1 || ec
=$?
806 echo $ec >"$BASE.exit") |
tee -a "$TESTLIB_TEST_TEE_OUTPUT_FILE"
807 exitcode
="$(cat "$BASE.
exit" 2>/dev/null)" ||
:
813 # End test_lib_main_init_tee
817 test_lib_main_init_funcs
() {
818 # Begin test_lib_main_init_funcs
821 [ -z "$test_lib_main_init_funcs_done" ] ||
return 0
823 if test z
"$color" != z
826 test z
"$1" != z ||
test z
"${TESTLIB_TEST_TAP_ONLY:-0}" = z
"0" ||
return 0
828 test -z "$1" && test -n "$quiet" && test -z "$verbose" && return
829 eval "say_color_color=\$say_color_$1"
833 if test -n "$HARNESS_ACTIVE"
835 case "$_sms" in '#'*)
840 printf '%s\n' "$_sfc$say_color_color$_sms$say_color_reset"
844 test z
"$1" != z ||
test z
"${TESTLIB_TEST_TAP_ONLY:-0}" = z
"0" ||
return 0
846 test -z "$1" && test -n "$quiet" && test -z "$verbose" && return
853 say_color
() { say_color_
"" "$@"; }
855 # Just like say_color except if HARNESS_ACTIVE it's ALWAYS output and WITHOUT color
857 if test -n "$HARNESS_ACTIVE"
867 # Fix some commands on Windows
868 case "${UNAME_S:=$(uname -s)}" in
870 # Windows has its own (incompatible) sort and find
880 # git sees Windows-style pwd
887 test_lib_main_init_funcs_done
=1
890 # End test_lib_main_init_funcs
894 # This function is called with all the test args and must perform all
895 # initialization that involves variables and is not specific to "$0"
896 # or "$test_description" in any way. This function may only be called
897 # once per run of the entire test suite.
898 test_lib_main_init_generic
() {
899 # Begin test_lib_main_init_generic
901 [ -n "$TESTLIB_DIRECTORY" ] || whats_the_dir
-L -- "${TEST_DIRECTORY:-.}/test-lib.sh" TESTLIB_DIRECTORY
902 [ -f "$TESTLIB_DIRECTORY/test-lib.sh" ] && [ -f "$TESTLIB_DIRECTORY/test-lib-main.sh" ] &&
903 [ -f "$TESTLIB_DIRECTORY/test-lib-functions.sh" ] &&
904 [ -f "$TESTLIB_DIRECTORY/test-lib-functions-tg.sh" ] ||
905 fatal
"error: invalid TESTLIB_DIRECTORY: $TESTLIB_DIRECTORY"
906 export TESTLIB_DIRECTORY
908 ! [ -f "$TESTLIB_DIRECTORY/../TG-BUILD-SETTINGS" ] || .
"$TESTLIB_DIRECTORY/../TG-BUILD-SETTINGS"
909 ! [ -f "$TESTLIB_DIRECTORY/TG-TEST-SETTINGS" ] || .
"$TESTLIB_DIRECTORY/TG-TEST-SETTINGS"
911 : "${SHELL_PATH:=/bin/sh}"
913 : "${GIT_PATH:=$(cmd_path git)}"
914 : "${PERL_PATH:=$(cmd_path perl || :)}"
916 # Test the binaries we have just built. The tests are kept in
917 # t/ subdirectory and are run in 'trash directory' subdirectory.
918 if test -z "$TEST_DIRECTORY"
920 # We allow tests to override this, in case they want to run tests
921 # outside of t/, e.g. for running tests on the test library
923 TEST_DIRECTORY
="$TESTLIB_DIRECTORY"
925 # ensure that TEST_DIRECTORY is an absolute path so that it
926 # is valid even if the current working directory is changed
927 TEST_DIRECTORY
="$(cd "$TEST_DIRECTORY" && pwd)" ||
exit 1
929 if test -z "$TEST_HELPER_DIRECTORY" && test -d "$TEST_DIRECTORY/helper"
931 TEST_HELPER_DIRECTORY
="$TEST_DIRECTORY/helper"
933 if test -z "$TEST_OUTPUT_DIRECTORY"
935 # Similarly, override this to store the test-results subdir
937 TEST_OUTPUT_DIRECTORY
="$TEST_DIRECTORY"
939 [ -d "$TESTLIB_DIRECTORY"/empty
] ||
{
940 mkdir
"$TESTLIB_DIRECTORY/empty" ||
:
941 chmod a-w
"$TESTLIB_DIRECTORY/empty" ||
:
942 test -d "$TESTLIB_DIRECTORY"/empty ||
943 fatal
"error: could not make empty directory: '$TESTLIB_DIRECTORY/empty'"
945 EMPTY_DIRECTORY
="$TESTLIB_DIRECTORY/empty"
946 export TEST_DIRECTORY TEST_HELPER_DIRECTORY TEST_OUTPUT_DIRECTORY EMPTY_DIRECTORY
947 GIT_CEILING_DIRECTORIES
="$TESTLIB_DIRECTORY"
948 [ "$TESTLIB_DIRECTORY" = "$TEST_DIRECTORY" ] ||
949 GIT_CEILING_DIRECTORIES
="$TEST_DIRECTORY:$GIT_CEILING_DIRECTORIES"
950 [ "$TESTLIB_DIRECTORY" = "$TEST_OUTPUT_DIRECTORY" ] ||
951 GIT_CEILING_DIRECTORIES
="$TEST_OUTPUT_DIRECTORY:$GIT_CEILING_DIRECTORIES"
952 export GIT_CEILING_DIRECTORIES
954 ################################################################
955 # It appears that people try to run tests with missing perl or git...
956 git_version
="$("$GIT_PATH" --version 2>&1)" ||
957 fatal
'error: you do not seem to have git available?'
958 case "$git_version" in [Gg
][Ii
][Tt
]\
[Vv
][Ee
][Rr
][Ss
][Ii
][Oo
][Nn
]\
[0-9]*);;*)
959 fatal
"error: git --version returned bogus value: $git_version"
961 ! vcmp
"$git_version" '>=' "2.9" || test_auh
="--allow-unrelated-histories"
962 #"$PERL_PATH" --version >/dev/null 2>&1 ||
963 # fatal 'error: you do not seem to have perl available?'
965 test_lib_main_init_tee
"$@"
967 # For repeatability, reset the environment to known value.
968 # TERM is sanitized below, after saving color control sequences.
973 export LANG LC_ALL PAGER TZ
975 # A call to "unset" with no arguments causes at least Solaris 10
976 # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
977 # deriving from the command substitution clustered with the other
979 unset_ VISUAL EMAIL LANGUAGE COLUMNS $
("$PERL_PATH" -e '
981 my $ok = join("|", qw(
996 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
997 print join("\n", @vars);
999 unset_ XDG_CONFIG_HOME
1001 GIT_AUTHOR_NAME
='Te s t (Author)'
1002 GIT_AUTHOR_EMAIL
=test@example.net
1003 GIT_COMMITTER_NAME
='Fra mewor k (Committer)'
1004 GIT_COMMITTER_EMAIL
=framework@example.org
1005 GIT_MERGE_VERBOSITY
=5
1006 GIT_MERGE_AUTOEDIT
=no
1007 GIT_TEMPLATE_DIR
="$EMPTY_DIRECTORY"
1008 GIT_CONFIG_NOSYSTEM
=1
1010 export PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
1011 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
1012 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
1013 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
1016 # Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
1018 export GIT_TRACE_BARE
1020 # Protect ourselves from common misconfiguration to export
1021 # CDPATH into the environment
1027 case "$GIT_TRACE" in 1|
2|
[Tt
][Rr
][Uu
][Ee
])
1034 # A regexp to match 5 and 40 hexdigits
1035 _x05
='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
1036 _x40
="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
1039 _z40
=0000000000000000000000000000000000000000
1041 EMPTY_TREE
=4b825dc642cb6eb9a060e54bf8d69288fbee4904
1042 EMPTY_BLOB
=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
1048 # UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
1049 # when case-folding filenames
1050 u200c
="$(printf '\342\200\214')"
1052 export _x05 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB
1054 while test "$#" -ne 0
1057 -d|
--d|
--de|
--deb|
--debu|
--debug)
1059 -i|
--i|
--im|
--imm|
--imme|
--immed|
--immedi|
--immedia|
--immediat|
--immediate)
1060 immediate
=t
; shift ;;
1061 -l|
--l|
--lo|
--lon|
--long|
--long-|
--long-t|
--long-te|
--long-tes|
--long-test|
--long-tests|\
1062 --ex|
--exp|
--expe|
--expen|
--expens|
--expensi|
--expensiv|
--expensive)
1063 TESTLIB_TEST_LONG
=t
; export TESTLIB_TEST_LONG
; shift ;;
1065 shift; test "$#" -ne 0 ||
{
1066 echo 'error: -r requires an argument' >&2;
1069 run_list
=$1; shift ;;
1071 run_list
=${1#--*=}; shift ;;
1072 -h|
--h|
--he|
--hel|
--help)
1074 -v|
--v|
--ve|
--ver|
--verb|
--verbo|
--verbos|
--verbose)
1077 verbose_list
=${1#--*=}
1079 -q|
--q|
--qu|
--qui|
--quie|
--quiet)
1084 TESTLIB_TEST_TAP_ONLY
=1; shift;;
1086 TESTLIB_TEST_TAP_ONLY
=0; shift;;
1088 color
="--color"; shift ;;
1092 shift ;; # was handled already
1097 TESTLIB_TEST_CHAIN_LINT
=1
1100 TESTLIB_TEST_CHAIN_LINT
=0
1102 -x|
--x|
--xt|
--xtr|
--xtra|
--xtrac|
--xtrace)
1110 echo "error: unknown test option '$1'" >&2; exit 1 ;;
1114 test z
"$run_list" = z ||
test z
"$quiet" != z || quiet
=T
1115 test z
"$quiet" != z
"0" || quiet
=
1116 test z
"$quiet" = z ||
test z
"$run_list" = z ||
test z
"$HARNESS_ACTIVE" != z || runquiet
=t
1117 test z
"$quiet" != z
"T" || quiet
=
1119 test "x${color+set}" != "xset" &&
1120 test "x$TERM" != "xdumb" && (
1121 { test -n "$TESTLIB_FORCETTY" ||
test -t 1; } &&
1122 tput bold
>/dev
/null
2>&1 &&
1123 tput setaf
1 >/dev
/null
2>&1 &&
1124 tput sgr0
>/dev
/null
2>&1
1127 if test z
"$color" != z
1129 # Save the color control sequences now rather than run tput
1130 # each time say_color() is called. This is done for two
1132 # * TERM will be changed to dumb
1133 # * HOME will be changed to a temporary directory and tput
1134 # might need to read ~/.terminfo from the original HOME
1135 # directory to get the control sequences
1136 getcmd say_color_error
eval 'tput setaf 1' # red
1137 getcmd say_color_skip
eval 'tput bold; tput setaf 5' # bold blue
1138 getcmd say_color_warn
eval 'tput setaf 3' # brown/yellow
1139 getcmd say_color_pass
eval 'tput setaf 2' # green
1140 getcmd say_color_info
eval 'tput setaf 6' # cyan
1141 getcmd say_color_reset
eval 'tput sgr0'
1142 say_color_
="" # no formatting for normal text
1154 test_external_has_tap
=0
1156 # The user-facing functions are loaded from a separate file
1157 .
"$TESTLIB_DIRECTORY/test-lib-functions-tg.sh"
1158 .
"$TESTLIB_DIRECTORY/test-lib-functions.sh"
1159 test_lib_functions_init
1160 test_lib_functions_tg_init
1163 : "${TESTLIB_SHELL_HAS_SHOPT=$(command -v shopt)}"
1167 [ -n "$TEST_HELPER_DIRECTORY" ] && [ -d "$TEST_HELPER_DIRECTORY" ] && PATH
="$TEST_HELPER_DIRECTORY:$PATH" ||
:
1168 if [ -n "$TG_TEST_INSTALLED" ]; then
1169 TG_TEST_FULL_PATH
="$(cmd_path tg)" && [ -n "$TG_TEST_FULL_PATH" ] ||
1170 fatal
'error: TG_TEST_INSTALLED set but no tg found in $PATH!'
1172 tg_bin_dir
="$(cd "$TESTLIB_DIRECTORY/..
/bin-wrappers
" 2>/dev/null && pwd -P || :)"
1173 [ -x "$tg_bin_dir/tg" ] ||
1174 fatal
'error: no ../bin-wrappers/tg executable found!'
1175 PATH
="$tg_bin_dir:$PATH"
1176 TG_TEST_FULL_PATH
="$tg_bin_dir/tg"
1177 test -f "$TESTLIB_DIRECTORY/TG-TEST-SETTINGS" ||
1178 echo 'warning: no TG-TEST-SETTINGS file found (run `make settings`)' >&2
1180 export TG_TEST_FULL_PATH
1181 tg_version
="$(tg --version)" ||
1182 fatal
'error: tg --version failed!'
1183 case "$tg_version" in [Tt
][Oo
][Pp
][Gg
][Ii
][Tt
]\
[Vv
][Ee
][Rr
][Ss
][Ii
][Oo
][Nn
]\
[0-9]*);;*)
1184 fatal
"error: tg --version returned bogus value: $tg_version"
1187 vcmp
"$git_version" '>=' "$GIT_MINIMUM_VERSION" ||
1188 fatal
"git version >= $GIT_MINIMUM_VERSION required but found \"$git_version\" instead"
1190 if test -z "$TESTLIB_TEST_CMP"
1192 if test -n "$TESTLIB_TEST_CMP_USE_COPIED_CONTEXT"
1194 TESTLIB_TEST_CMP
="$DIFF -c"
1196 TESTLIB_TEST_CMP
="$DIFF -u"
1200 # Fix some commands on Windows
1201 case "${UNAME_S:=$(uname -s)}" in
1203 # no POSIX permissions
1204 # backslashes in pathspec are converted to '/'
1205 # exec does not inherit the PID
1206 test_set_prereq MINGW
1207 test_set_prereq NATIVE_CRLF
1208 test_set_prereq SED_STRIPS_CR
1209 test_set_prereq GREP_STRIPS_CR
1210 TESTLIB_TEST_CMP
=mingw_test_cmp
1213 test_set_prereq POSIXPERM
1214 test_set_prereq EXECKEEPSPID
1215 test_set_prereq CYGWIN
1216 test_set_prereq SED_STRIPS_CR
1217 test_set_prereq GREP_STRIPS_CR
1220 test_set_prereq POSIXPERM
1221 test_set_prereq BSLASHPSPEC
1222 test_set_prereq EXECKEEPSPID
1226 ( COLUMNS
=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
1228 test_lib_main_init_funcs
1230 test_lazy_prereq PIPE
'
1231 # test whether the filesystem supports FIFOs
1232 case "${UNAME_S:=$(uname -s)}" in
1237 rm -f testfifo && mkfifo testfifo
1242 test_lazy_prereq SYMLINKS
'
1243 # test whether the filesystem supports symbolic links
1244 ln -s x y && test -h y
1247 test_lazy_prereq FILEMODE
'
1248 test_ensure_git_dir_ &&
1249 test "$(git config --bool core.filemode)" = true
1252 test_lazy_prereq CASE_INSENSITIVE_FS
'
1253 echo good >CamelCase &&
1254 echo bad >camelcase &&
1255 test "$(cat CamelCase)" != good
1258 test_lazy_prereq UTF8_NFD_TO_NFC
'
1259 # check whether FS converts nfd unicode to nfc
1260 auml="$(printf "\303\244")"
1261 aumlcdiar="$(printf "\141\314\210")"
1271 test_lazy_prereq AUTOIDENT
'
1272 test_ensure_git_dir_ &&
1273 sane_unset GIT_AUTHOR_NAME &&
1274 sane_unset GIT_AUTHOR_EMAIL &&
1275 git var GIT_AUTHOR_IDENT
1278 test_lazy_prereq EXPENSIVE
'
1279 test -n "$TESTLIB_TEST_LONG"
1282 test_lazy_prereq USR_BIN_TIME
'
1283 test -x /usr/bin/time
1286 test_lazy_prereq NOT_ROOT
'
1291 # SANITY is about "can you correctly predict what the filesystem would
1292 # do by only looking at the permission bits of the files and
1293 # directories?" A typical example of !SANITY is running the test
1294 # suite as root, where a test may expect "chmod -r file && cat file"
1295 # to fail because file is supposed to be unreadable after a successful
1296 # chmod. In an environment (i.e. combination of what filesystem is
1297 # being used and who is running the tests) that lacks SANITY, you may
1298 # be able to delete or create a file when the containing directory
1299 # doesn't have write permissions, or access a file even if the
1300 # containing directory doesn't have read or execute permissions.
1302 test_lazy_prereq SANITY
'
1303 mkdir SANETESTD.1 SANETESTD.2 &&
1305 chmod +w SANETESTD.1 SANETESTD.2 &&
1306 >SANETESTD.1/x 2>SANETESTD.2/x &&
1307 chmod -w SANETESTD.1 &&
1308 chmod -r SANETESTD.1/x &&
1309 chmod -rx SANETESTD.2 ||
1310 error "bug in test sript: cannot prepare SANETESTD"
1312 ! test -r SANETESTD.1/x &&
1313 ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
1316 chmod +rwx SANETESTD.1 SANETESTD.2 &&
1317 rm -rf SANETESTD.1 SANETESTD.2 ||
1318 error "bug in test sript: cannot clean SANETESTD"
1322 test_lazy_prereq CMDLINE_LIMIT
'run_with_limited_cmdline true'
1325 # End test_lib_main_init_generic
1329 # This function is guaranteed to always be called for every single test.
1330 # Only put things in this function that MUST be done per-test, function
1331 # definitions and sourcing other files generally DO NOT QUALIFY (there can
1333 test_lib_main_init_specific
() {
1334 # Begin test_lib_main_init_specific
1337 # original stdin is on 6, stdout on 5 and stderr on 7
1340 test_lib_main_init_funcs
1342 if test -n "$HARNESS_ACTIVE"
1344 if test "$verbose" = t ||
test -n "$verbose_list" && test -z "$verbose_log$TESTLIB_OVERRIDE"
1346 printf 'Bail out! %s\n' \
1347 'verbose mode forbidden under TAP harness; use --verbose-log'
1352 test z
"${TESTLIB_TEST_TAP_ONLY:-0}" = z
"0" ||
test -z "$verbose$verbose_list" ||
1353 test -n "$verbose_log$TESTLIB_OVERRIDE" ||
{
1354 if test z
"$TESTLIB_TEST_TAP_ONLY" = z
"-1"; then
1355 unset_ TESTLIB_TEST_TAP_ONLY
1356 say_color
"" "# auto-deactivating TESTLIB_TEST_TAP_ONLY=-1 in verbose mode"
1358 printf 'Bail out! %s\n' \
1359 'verbose mode forbidden with TESTLIB_TEST_TAP_ONLY; use --verbose-log'
1364 test "${test_description}" != "" ||
1365 error
"Test script did not set test_description."
1367 if test "$help" = "t"
1369 printf '%s\n' "$(printf '%s\n' "$test_description")" |
1384 if test "$verbose_log" = "t"
1386 exec 3>>"$TESTLIB_TEST_TEE_OUTPUT_FILE" 4>&3
1387 elif test "$verbose" = "t"
1391 exec 4>/dev
/null
3>/dev
/null
1394 # Send any "-x" output directly to stderr to avoid polluting tests
1395 # which capture stderr. We can do this unconditionally since it
1396 # has no effect if tracing isn't turned on.
1398 # Note that this sets up the trace fd as soon as we assign the variable, so it
1399 # must come after the creation of descriptor 4 above. Likewise, we must never
1400 # unset this, as it has the side effect of closing descriptor 4, which we
1401 # use to show verbose tests to the user.
1403 # Note also that we don't need or want to export it. The tracing is local to
1404 # this shell, and we would not want to influence any shells we exec.
1409 trap 'trapexit_ 129' HUP
1410 trap 'trapexit_ 130' INT
1411 trap 'trapexit_ 131' QUIT
1412 trap 'trapexit_ 134' ABRT
1413 trap 'trapexit_ 141' PIPE
1414 trap 'trapexit_ 143' TERM
1415 trap 'TESTLIB_EXIT_OK=t; trapexit_ 1' USR1
1418 TRASH_DIRECTORY
="${0%.sh}"
1419 test z
"${TRASH_DIRECTORY##*/}" != z
"sh" || TRASH_DIRECTORY
="xsh"
1420 TRASH_DIRECTORY
="trash directory.${TRASH_DIRECTORY##*/}"
1421 TRASHTMP_DIRECTORY
="${0%.sh}"
1422 test z
"${TRASHTMP_DIRECTORY##*/}" != z
"sh" || TRASHTMP_DIRECTORY
="xsh"
1423 TRASHTMP_DIRECTORY
="trash tmp directory.${TRASHTMP_DIRECTORY##*/}"
1424 test -n "$root" && TRASH_DIRECTORY
="$root/$TRASH_DIRECTORY"
1425 test -n "$root" && TRASHTMP_DIRECTORY
="$root/$TRASHTMP_DIRECTORY"
1426 test -n "$root" && GIT_CEILING_DIRECTORIES
="$root:$GIT_CEILING_DIRECTORIES"
1427 case "$TRASH_DIRECTORY" in
1428 /*) ;; # absolute path is good
1429 *) TRASH_DIRECTORY
="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY"
1430 TRASHTMP_DIRECTORY
="$TEST_OUTPUT_DIRECTORY/$TRASHTMP_DIRECTORY" ;;
1432 test ! -z "$debug" || remove_trash
="$TRASH_DIRECTORY"
1433 test ! -z "$debug" || remove_trashtmp
="$TRASHTMP_DIRECTORY"
1434 ! test -e "$TRASH_DIRECTORY" ||
{
1435 { rm -rf "$TRASH_DIRECTORY" ||
:; } >/dev
/null
2>&1 &&
1436 ! test -e "$TRASH_DIRECTORY" ||
{
1437 chmod -R u
+rw
"$TRASH_DIRECTORY" &&
1438 rm -rf "$TRASH_DIRECTORY" &&
1439 ! test -e "$TRASH_DIRECTORY"
1442 ! test -e "$TRASHTMP_DIRECTORY" ||
{
1443 { rm -rf "$TRASHTMP_DIRECTORY" ||
:; } >/dev
/null
2>&1 &&
1444 ! test -e "$TRASHTMP_DIRECTORY" ||
{
1445 chmod -R u
+rw
"$TRASHTMP_DIRECTORY" &&
1446 rm -rf "$TRASHTMP_DIRECTORY" &&
1447 ! test -e "$TRASHTMP_DIRECTORY"
1451 echo >&5 "FATAL: Cannot prepare test area"
1455 HOME
="$TRASH_DIRECTORY"
1456 GNUPGHOME
="$HOME/gnupg-home-not-used"
1457 export HOME GNUPGHOME
1459 if test -z "$TEST_NO_CREATE_REPO"
1461 test_create_repo
"$TRASH_DIRECTORY"
1463 mkdir
-p "$TRASH_DIRECTORY"
1465 # $TRASHTMP_DIRECTORY is created on-demand only
1467 # Use -P to resolve symlinks in our working directory so that the cwd
1468 # in subprocesses like tg equals our $PWD (for pathname comparisons).
1469 cd -P "$TRASH_DIRECTORY" ||
exit 1
1472 this_test
=${this_test%%-*}
1473 test_called_test_plan
=
1474 test_wrote_plan_count
=
1475 test_last_subtest_ok
=1
1476 if match_pattern_list
"$this_test" $TESTLIB_SKIP_TESTS
1478 say_color info
>&3 "skipping test $this_test altogether"
1479 skip_all
="skip all tests in $this_test"
1483 if test z
"${TESTLIB_SHELL_HAS_SHOPT=$(command -v shopt)}" = z
"shopt"
1485 shopt -s expand_aliases
>/dev
/null
2>&1 ||
:
1488 # End test_lib_main_init_specific
1493 # THIS SHOULD ALWAYS BE THE LAST FUNCTION DEFINED IN THIS FILE
1495 # Any client that sources this file should immediately execute this function
1496 # afterwards with the command line arguments
1498 # THERE SHOULD NOT BE ANY DIRECTLY EXECUTED LINES OF CODE IN THIS FILE
1500 test_lib_main_init
() {
1502 test_lib_main_init_generic
"$@"
1503 test_lib_main_init_specific
"$@"