test-lib-main.sh: keep same exit code with --tee
[topgit/pro.git] / t / test-lib-main.sh
blob65494ce52fbf995449ab709b8f72c98517b659c6
1 # Test framework from Git with modifications.
3 # Modifications Copyright (C) 2016,2017 Kyle J. McKay. All rights reserved.
4 # Modifications made:
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!
42 cmd_path() (
43 { "unset" -f command unset unalias "$1"; } >/dev/null 2>&1 || :
44 { "unalias" -a; } >/dev/null 2>&1 || :
45 command -v "$1"
48 fatal() {
49 printf '%s\n' "$*" >&2
50 TESTLIB_EXIT_OK=1
51 exit 1
54 # usage: cmdget <varname> <cmd> [<arg>...]
55 # return code is that of <cmd> [<arg...]
56 # <varname> is set to VERBATIM <cmd> output (except NULs may not be handled)
57 getcmd() {
58 [ -n "$1" ] || return 1
59 eval "$1=" >/dev/null 2>&1 || return 1
60 [ -n "$2" ] || return 1
61 _getcmd_vn="$1"
62 shift
63 _getcmd_ec=0
64 _getcmd_result="$(ec=0; ("$@") || ec=$?; echo Z; exit $ec)" || _getcmd_ec=$?
65 eval "$_getcmd_vn=\"\${_getcmd_result%Z}\""
66 return $_getcmd_ec
69 # usage: whats_the_dir [-P | -L] [--] path-to-something varname
70 # determine path-to-something's directory and store it into varname
71 # without "-P" or "-L" a relative dirname may be returned
72 whats_the_dir() {
73 # determine "$1"'s directory and store it into the var name passed as "$2"
74 if [ "z$1" = "z-P" -o "z$1" = "z-L" ]; then
75 if [ "z$2" = "z--" ]; then
76 set -- "$3" "$4" "$1"
77 else
78 set -- "$2" "$3" "$1"
80 elif [ "z$1" = "z--" ]; then
81 shift
83 case "$1" in *"/"*);;*) set -- "./$1" "$2" "$3"; esac
84 while [ -L "$1" ]; do
85 set -- "$(readlink "$1")" "$2" "$3" "$1"
86 case "$1" in "/"*);;*)
87 set -- "${4%/*}/$1" "$2" "$3"
88 esac
89 done
90 set -- "${1%/*}" "$2" "$3"
91 if [ "z$3" != "z" ] && [ -d "$1" ] &&
92 ! case "$1" in [!/]*|*"/./"*|*"/."|*"/../"*|*"/..") ! :; esac; then
93 [ "z$3" = "z-P" ] || set -- "$1" "$2"
94 if [ "z$3" = "z" -a \( "z$1" = "z." -o "z$1" = "z$PWD" \) ]; then
95 set -- "$PWD" "$2"
96 else
97 set -- "$(cd "$1" && pwd $3)" "$2"
100 eval "$2=\"$1\""
103 vcmp() {
104 # Compare $1 to $3 each of which must match ^[^0-9]*\d*(\.\d*)*.*$
105 # where only the "\d*" parts in the regex participate in the comparison
106 # Since EVERY string matches that regex this function is easy to use
107 # An empty string ('') for $1 or $3 or any "\d*" part is treated as 0
108 # $2 is a compare op '<', '<=', '=', '==', '!=', '>=', '>'
109 # Return code is 0 for true, 1 for false (or unknown compare op)
110 # There is NO difference in behavior between '=' and '=='
111 # Note that "vcmp 1.8 == 1.8.0.0.0.0" correctly returns 0
112 set -- "$1" "$2" "$3" "${1%%[0-9]*}" "${3%%[0-9]*}"
113 set -- "${1#"$4"}" "$2" "${3#"$5"}"
114 set -- "${1%%[!0-9.]*}" "$2" "${3%%[!0-9.]*}"
115 while
116 vcmp_a_="${1%%.*}"
117 vcmp_b_="${3%%.*}"
118 [ "z$vcmp_a_" != "z" -o "z$vcmp_b_" != "z" ]
120 if [ "${vcmp_a_:-0}" -lt "${vcmp_b_:-0}" ]; then
121 unset vcmp_a_ vcmp_b_
122 case "$2" in "<"|"<="|"!=") return 0; esac
123 return 1
124 elif [ "${vcmp_a_:-0}" -gt "${vcmp_b_:-0}" ]; then
125 unset vcmp_a_ vcmp_b_
126 case "$2" in ">"|">="|"!=") return 0; esac
127 return 1;
129 vcmp_a_="${1#$vcmp_a_}"
130 vcmp_b_="${3#$vcmp_b_}"
131 set -- "${vcmp_a_#.}" "$2" "${vcmp_b_#.}"
132 done
133 unset vcmp_a_ vcmp_b_
134 case "$2" in "="|"=="|"<="|">=") return 0; esac
135 return 1
138 vcmp "$@"
140 error() {
141 say_color error "${LF}error: $*" >&7
142 printf '%s\n' "Bail out! ${0##*/}:${callerlno:+$callerlno:} error: $*" >&5
143 TESTLIB_EXIT_OK=t
144 [ -z "$TESTLIB_TEST_PARENT_INT_ON_ERROR" ] || kill -INT $PPID || :
145 kill -USR1 $$
146 exit 1
149 say() {
150 say_color info "$@"
153 say_tap() {
154 say_color_tap info "$@"
157 die() {
158 code=$?
159 if test -n "$TESTLIB_EXIT_OK"
160 then
161 exit $code
162 else
163 echo >&5 "FATAL: Unexpected exit with code $code"
164 exit 1
168 # You are not expected to call test_ok_ and test_failure_ directly, use
169 # the test_expect_* functions instead.
171 test_ok_() {
172 test_success=$(($test_success + 1))
173 say_color_tap "" "ok $test_count - $@"
176 test_failure_() {
177 test_failure=$(($test_failure + 1))
178 tlno="$1"
179 shift
180 say_color_tap error "not ok $test_count - $1"
181 shift
182 printf '%s\n' "$(printf '%s\n' "failed: ${0##*/}${tlno:+:$tlno}$LF$*")" |
183 sed -n -e '
185 :loop
186 s/\([^ ]\)/\1/
187 t first
188 b continue
189 :first
192 b rest
193 :continue
195 b loop
197 :rest
198 s/^/# /
200 $ i\
203 test "$immediate" = "" || { TESTLIB_EXIT_OK=t; exit 1; }
206 test_known_broken_ok_() {
207 test_fixed=$(($test_fixed + 1))
208 say_color_tap warn "ok $test_count - $@ # TODO known breakage vanished"
211 test_known_broken_failure_() {
212 test_broken=$(($test_broken + 1))
213 say_color_tap warn "not ok $test_count - $@ # TODO known breakage"
216 test_possibly_broken_ok_() {
217 test_success=$(($test_success + 1))
218 say_color_tap "" "ok $test_count - $@"
221 test_possibly_broken_failure_() {
222 test_broken=$(($test_broken + 1))
223 say_color_tap warn "not ok $test_count - $@ # TODO tolerated breakage"
226 test_debug() {
227 test "$debug" = "" || test $# -eq 0 || test -z "$*" || { "$@"; } >&7 2>&1
230 match_pattern_list() {
231 arg="$1"
232 shift
233 test -z "$*" && return 1
234 for pattern_
236 case "$arg" in
237 $pattern_)
238 return 0
239 esac
240 done
241 return 1
244 match_test_selector_list() {
245 title="$1"
246 shift
247 arg="$1"
248 shift
249 test -z "$1" && return 0
251 # Both commas and whitespace are accepted as separators.
252 OLDIFS=$IFS
253 IFS=' ,'
254 set -- $1
255 IFS=$OLDIFS
257 # If the first selector is negative we include by default.
258 include=
259 case "$1" in
260 !*) include=t ;;
261 esac
263 for selector
265 orig_selector=$selector
267 positive=t
268 case "$selector" in
270 positive=
271 selector=${selector##?}
273 esac
275 test -z "$selector" && continue
277 case "$selector" in
278 *-*)
279 if x_="${selector%%-*}" && test "z$x_" != "z${x_#*[!0-9]}"
280 then
281 echo "error: $title: invalid non-numeric in range" \
282 "start: '$orig_selector'" >&2
283 exit 1
285 if x_="${selector#*-}" && test "z$x_" != "z${x_#*[!0-9]}"
286 then
287 echo "error: $title: invalid non-numeric in range" \
288 "end: '$orig_selector'" >&2
289 exit 1
291 unset x_
294 if test "z$selector" != "z${selector#*[!0-9]}"
295 then
296 echo "error: $title: invalid non-numeric in test" \
297 "selector: '$orig_selector'" >&2
298 exit 1
300 esac
302 # Short cut for "obvious" cases
303 test -z "$include" && test -z "$positive" && continue
304 test -n "$include" && test -n "$positive" && continue
306 case "$selector" in
308 if test $arg -le ${selector#-}
309 then
310 include=$positive
314 if test $arg -ge ${selector%-}
315 then
316 include=$positive
319 *-*)
320 if test ${selector%%-*} -le $arg \
321 && test $arg -le ${selector#*-}
322 then
323 include=$positive
327 if test $arg -eq $selector
328 then
329 include=$positive
332 esac
333 done
335 test -n "$include"
338 maybe_teardown_verbose() {
339 test -z "$verbose_only" && return
340 exec 4>/dev/null 3>/dev/null
341 verbose=
344 maybe_setup_verbose() {
345 test -z "$verbose_only" && return
346 if match_pattern_list $test_count $verbose_only
347 then
348 exec 4>&2 3>&1
349 # Emit a delimiting blank line when going from
350 # non-verbose to verbose. Within verbose mode the
351 # delimiter is printed by test_expect_*. The choice
352 # of the initial $last_verbose is such that before
353 # test 1, we do not print it.
354 test -z "$last_verbose" && echo >&3 ""
355 verbose=t
356 else
357 exec 4>/dev/null 3>/dev/null
358 verbose=
360 last_verbose=$verbose
363 want_trace() {
364 test "$trace" = t && test "$verbose" = t
367 # This is a separate function because some tests use
368 # "return" to end a test_expect_success block early
369 # (and we want to make sure we run any cleanup like
370 # "set +x").
371 test_eval_inner_() (
372 # Do not add anything extra (including LF) after '$*'
373 eval "
374 set -e
375 test_subshell_active_=t
376 ! want_trace || ! set -x && ! :
380 # Same thing as test_eval_inner_ but without the subshell
381 test_eval_inner_no_subshell_() {
382 # Do not add anything extra (including LF) after '$*'
383 eval "
384 ! want_trace || ! set -x && ! :
388 test_eval_ss_() {
389 # We run this block with stderr redirected to avoid extra cruft
390 # during a "-x" trace. Once in "set -x" mode, we cannot prevent
391 # the shell from printing the "set +x" to turn it off (nor the saving
392 # of $? before that). But we can make sure that the output goes to
393 # /dev/null.
395 # The test itself is run with stderr put back to &4 (so either to
396 # /dev/null, or to the original stderr if --verbose was used).
398 test_eval_ss_="$1"
399 shift
400 if test "${test_eval_ss_:-0}" = "0"
401 then
402 test_eval_inner_no_subshell_ "$@" </dev/null >&3 2>&4
403 else
404 test_eval_inner_ "$@" </dev/null >&3 2>&4
406 test_eval_ret_=$?
407 if want_trace
408 then
409 set +x
410 if test "$test_eval_ret_" != 0
411 then
412 say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
415 } 2>/dev/null
416 return $test_eval_ret_
419 # Calls the real test_eval_ss_ with !"$TESTLIB_TEST_NO_SUBSHELL" as first arg
420 test_eval_() {
421 if test -n "$TESTLIB_TEST_NO_SUBSHELL"
422 then
423 test_eval_ss_ "0" "$@"
424 else
425 test_eval_ss_ "1" "$@"
429 # If "$1" = "-" read the script from stdin but ONLY if stdin is NOT a tty
430 # Store the test script in test_script_
431 test_get_() {
432 if test "x$1" = "x-"
433 then
434 ! test -t 0 || error "test script is '-' but STDIN is a tty"
435 test_script_="$(cat)"
436 test -n "$test_script_" || error "test script is '-' but STDIN is empty"
437 test_script_="$LF$test_script_$LF"
438 else
439 test_script_="$1"
443 fail_() {
444 return ${1:-1}
447 test_run_() {
448 test_cleanup=:
449 test_subshell_active_=
450 expecting_failure=$2
451 linting=
453 if test "${TESTLIB_TEST_CHAIN_LINT:-1}" != 0; then
454 # turn off tracing for this test-eval, as it simply creates
455 # confusing noise in the "-x" output
456 trace_tmp=$trace
457 trace=
458 linting=t
459 # 117 is magic because it is unlikely to match the exit
460 # code of other programs
461 test_eval_ss_ "1" "fail_ 117 && $1${LF}fail_ \$?"
462 if test "$?" != 117; then
463 error "bug in the test script: broken &&-chain: $1"
465 trace=$trace_tmp
466 linting=
469 test_eval_ "$1"
470 eval_ret=$?
472 if test -z "$immediate" || test $eval_ret = 0 ||
473 test -n "$expecting_failure" && test "${test_cleanup:-:}" != ":"
474 then
475 test_eval_ "$test_cleanup"
477 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
478 then
479 echo ""
481 return "$eval_ret"
484 test_start_() {
485 test_count=$(($test_count+1))
486 maybe_setup_verbose
489 test_finish_() {
490 echo >&3 ""
491 maybe_teardown_verbose
494 test_skip() {
495 to_skip=
496 skipped_reason=
497 if match_pattern_list $this_test.$test_count $TESTLIB_SKIP_TESTS
498 then
499 to_skip=t
500 skipped_reason="TESTLIB_SKIP_TESTS"
502 if test -z "$to_skip" && test -n "$test_prereq" &&
503 ! test_have_prereq "$test_prereq"
504 then
505 to_skip=t
507 of_prereq=
508 if test "$missing_prereq" != "$test_prereq"
509 then
510 of_prereq=" of $test_prereq"
512 skipped_reason="missing $missing_prereq${of_prereq}"
514 if test -z "$to_skip" && test -n "$run_list" &&
515 ! match_test_selector_list '--run' $test_count "$run_list"
516 then
517 to_skip=t
518 skipped_reason="--run"
521 case "$to_skip" in
523 say_color skip >&3 "skipping test: $@"
524 say_color_tap skip "ok $test_count # skip $1 ($skipped_reason)"
525 : true
528 false
530 esac
533 # stub; runs at end of each successful test
534 test_at_end_hook_() {
538 test_done() {
539 TESTLIB_EXIT_OK=t
541 if test -z "$HARNESS_ACTIVE"
542 then
543 test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
544 mkdir -p "$test_results_dir"
545 base=${0##*/}
546 test_results_path="$test_results_dir/${base%.sh}.counts"
548 cat >"$test_results_path" <<-EOF
549 total $test_count
550 success $test_success
551 fixed $test_fixed
552 broken $test_broken
553 failed $test_failure
558 if test "$test_fixed" != 0
559 then
560 say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
562 if test "$test_broken" != 0
563 then
564 say_color warn "# still have $test_broken known breakage(s)"
566 if test "$test_broken" != 0 || test "$test_fixed" != 0
567 then
568 test_remaining=$(( $test_count - $test_broken - $test_fixed ))
569 msg="remaining $test_remaining test(s)"
570 else
571 test_remaining=$test_count
572 msg="$test_count test(s)"
574 case "$test_failure" in
576 # Maybe print SKIP message
577 if test -n "$skip_all" && test $test_count -gt 0
578 then
579 error "Can't use skip_all after running some tests"
581 test -z "$skip_all" || skip_all=" # SKIP $skip_all"
583 if test $test_external_has_tap -eq 0
584 then
585 if test $test_remaining -gt 0
586 then
587 say_color pass "# passed all $msg"
589 say_tap "1..$test_count$skip_all"
592 test -d "$remove_trash" &&
593 cd "${remove_trash%/*}" &&
594 test_done_td_="${remove_trash##*/}" &&
595 test -e "$test_done_td_" &&
596 rm -rf "$test_done_td_" &&
597 ! test -e "$test_done_td_" || {
598 chmod -R u+w "$test_done_td_" &&
599 rm -rf "$test_done_td_"
602 test_at_end_hook_
604 exit 0 ;;
607 if test $test_external_has_tap -eq 0
608 then
609 say_color error "# failed $test_failure among $msg"
610 say_tap "1..$test_count"
613 exit 1 ;;
615 esac
618 # Provide an implementation of the 'yes' utility
619 yes() {
620 if test $# = 0
621 then
623 else
624 y="$*"
628 while test $i -lt 99
630 echo "$y"
631 i=$(($i+1))
632 done
635 run_with_limited_cmdline() {
636 (ulimit -s 128 && "$@")
641 ## Note that the following functions have bodies that are NOT indented
642 ## to assist with readability
646 test_lib_main_init_tee() {
647 # Begin test_lib_main_init_tee
650 # if --tee was passed, write the output not only to the terminal, but
651 # additionally to the file test-results/$BASENAME.out, too.
652 case "$TESTLIB_TEST_TEE_STARTED, $* " in
653 done,*)
654 # do not redirect again
656 *' --tee '*|*' --verbose-log '*)
657 mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
658 BASE="$TEST_OUTPUT_DIRECTORY/test-results/${0##*/}"
659 BASE="${BASE%.sh}"
661 # Make this filename available to the sub-process in case it is using
662 # --verbose-log.
663 TESTLIB_TEST_TEE_OUTPUT_FILE=$BASE.out
664 export TESTLIB_TEST_TEE_OUTPUT_FILE
666 # Truncate before calling "tee -a" to get rid of the results
667 # from any previous runs.
668 >"$TESTLIB_TEST_TEE_OUTPUT_FILE"
669 >"$BASE.exit"
671 (ec=0; TESTLIB_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1 || ec=$?
672 echo $ec >"$BASE.exit") | tee -a "$TESTLIB_TEST_TEE_OUTPUT_FILE"
673 exitcode="$(cat "$BASE.exit" 2>/dev/null)" || :
674 exit ${exitcode:-1}
676 esac
679 # End test_lib_main_init_tee
683 test_lib_main_init_funcs() {
684 # Begin test_lib_main_init_funcs
687 [ -z "$test_lib_main_init_funcs_done" ] || return 0
689 if test -n "$color"
690 then
691 say_color() {
692 test -z "$1" && test -n "$quiet" && return
693 eval "say_color_color=\$say_color_$1"
694 shift
695 _sfc=
696 _sms="$*"
697 if test -n "$HARNESS_ACTIVE"
698 then
699 case "$_sms" in '#'*)
700 _sfc='#'
701 _sms="${_sms#?}"
702 esac
704 printf '%s\n' "$_sfc$say_color_color$_sms$say_color_reset"
706 else
707 say_color() {
708 test -z "$1" && test -n "$quiet" && return
709 shift
710 printf '%s\n' "$*"
714 # Just like say_color except if HARNESS_ACTIVE it's ALWAYS output and WITHOUT color
715 say_color_tap() {
716 if test -n "$HARNESS_ACTIVE"
717 then
718 shift
719 printf '%s\n' "$*"
720 else
721 say_color "$@"
726 # Fix some commands on Windows
727 case "${UNAME_S:=$(uname -s)}" in
728 *MINGW*)
729 # Windows has its own (incompatible) sort and find
730 sort() {
731 /usr/bin/sort "$@"
733 find() {
734 /usr/bin/find "$@"
736 sum() {
737 md5sum "$@"
739 # git sees Windows-style pwd
740 pwd() {
741 builtin pwd -W
744 esac
746 test_lib_main_init_funcs_done=1
749 # End test_lib_main_init_funcs
753 # This function is called with all the test args and must perform all
754 # initialization that involves variables and is not specific to "$0"
755 # or "$test_description" in any way. This function may only be called
756 # once per run of the entire test suite.
757 test_lib_main_init_generic() {
758 # Begin test_lib_main_init_generic
760 [ -n "$TESTLIB_DIRECTORY" ] || whats_the_dir -L -- "${TEST_DIRECTORY:-.}/test-lib.sh" TESTLIB_DIRECTORY
761 [ -f "$TESTLIB_DIRECTORY/test-lib.sh" ] && [ -f "$TESTLIB_DIRECTORY/test-lib-main.sh" ] &&
762 [ -f "$TESTLIB_DIRECTORY/test-lib-functions.sh" ] ||
763 fatal "error: invalid TESTLIB_DIRECOTRY: $TESTLIB_DIRECTORY"
764 export TESTLIB_DIRECTORY
766 ! [ -f "$TESTLIB_DIRECTORY/../TG-BUILD-SETTINGS" ] || . "$TESTLIB_DIRECTORY/../TG-BUILD-SETTINGS"
767 ! [ -f "$TESTLIB_DIRECTORY/TG-TEST-SETTINGS" ] || . "$TESTLIB_DIRECTORY/TG-TEST-SETTINGS"
769 : "${SHELL_PATH:=/bin/sh}"
770 : "${DIFF:=diff}"
771 : "${GIT_PATH:=$(cmd_path git)}"
772 : "${PERL_PATH:=$(cmd_path perl || :)}"
774 # Test the binaries we have just built. The tests are kept in
775 # t/ subdirectory and are run in 'trash directory' subdirectory.
776 if test -z "$TEST_DIRECTORY"
777 then
778 # We allow tests to override this, in case they want to run tests
779 # outside of t/, e.g. for running tests on the test library
780 # itself.
781 TEST_DIRECTORY="$TESTLIB_DIRECTORY"
782 else
783 # ensure that TEST_DIRECTORY is an absolute path so that it
784 # is valid even if the current working directory is changed
785 TEST_DIRECTORY="$(cd "$TEST_DIRECTORY" && pwd)" || exit 1
787 if test -z "$TEST_OUTPUT_DIRECTORY"
788 then
789 # Similarly, override this to store the test-results subdir
790 # elsewhere
791 TEST_OUTPUT_DIRECTORY="$TEST_DIRECTORY"
793 [ -d "$TESTLIB_DIRECTORY"/empty ] || {
794 mkdir "$TESTLIB_DIRECTORY/empty" || :
795 chmod a-w "$TESTLIB_DIRECTORY/empty" || :
796 test -d "$TESTLIB_DIRECTORY"/empty ||
797 fatal "error: could not make empty directory: '$TESTLIB_DIRECTORY/empty'"
799 EMPTY_DIRECTORY="$TESTLIB_DIRECTORY/empty"
800 export TEST_DIRECTORY TEST_OUTPUT_DIRECTORY EMPTY_DIRECTORY
802 ################################################################
803 # It appears that people try to run tests with missing perl or git...
804 git_version="$("$GIT_PATH" --version 2>&1)" ||
805 fatal 'error: you do not seem to have git available?'
806 case "$git_version" in [Gg][Ii][Tt]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn]\ [0-9]*);;*)
807 fatal "error: git --version returned bogus value: $git_version"
808 esac
809 #"$PERL_PATH" --version >/dev/null 2>&1 ||
810 # fatal 'error: you do not seem to have perl available?'
812 test_lib_main_init_tee "$@"
814 # For repeatability, reset the environment to known value.
815 # TERM is sanitized below, after saving color control sequences.
816 LANG=C
817 LC_ALL=C
818 PAGER=cat
819 TZ=UTC
820 export LANG LC_ALL PAGER TZ
821 EDITOR=:
822 # A call to "unset" with no arguments causes at least Solaris 10
823 # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
824 # deriving from the command substitution clustered with the other
825 # ones.
826 unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
827 my @env = keys %ENV;
828 my $ok = join("|", qw(
829 TRACE
830 DEBUG
831 USE_LOOKUP
832 TEST
833 .*_TEST
834 MINIMUM_VERSION
835 PATH
836 PROVE
837 UNZIP
838 PERF_
839 CURL_VERBOSE
840 TRACE_CURL
842 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
843 print join("\n", @vars);
845 unset XDG_CONFIG_HOME
846 unset GITPERLLIB
847 GIT_AUTHOR_NAME='Te s t'
848 GIT_AUTHOR_EMAIL=test@example.net
849 GIT_COMMITTER_NAME='Fra mewor k'
850 GIT_COMMITTER_EMAIL=framework@example.org
851 GIT_MERGE_VERBOSITY=5
852 GIT_MERGE_AUTOEDIT=no
853 GIT_TEMPLATE_DIR="$EMPTY_DIRECTORY"
854 GIT_CONFIG_NOSYSTEM=1
855 GIT_ATTR_NOSYSTEM=1
856 export PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
857 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
858 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
859 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
860 export EDITOR
862 # Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
863 GIT_TRACE_BARE=1
864 export GIT_TRACE_BARE
866 # Protect ourselves from common misconfiguration to export
867 # CDPATH into the environment
868 unset CDPATH
870 unset GREP_OPTIONS
871 unset UNZIP
873 case "$GIT_TRACE" in 1|2|[Tt][Rr][Uu][Ee])
874 GIT_TRACE=4
876 esac
878 # Convenience
880 # A regexp to match 5 and 40 hexdigits
881 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
882 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
884 # Zero SHA-1
885 _z40=0000000000000000000000000000000000000000
887 EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
888 EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
890 # Line feed
891 LF='
894 # UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
895 # when case-folding filenames
896 u200c="$(printf '\342\200\214')"
898 export _x05 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB
900 while test "$#" -ne 0
902 case "$1" in
903 -d|--d|--de|--deb|--debu|--debug)
904 debug=t; shift ;;
905 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
906 immediate=t; shift ;;
907 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests|\
908 --ex|--exp|--expe|--expen|--expens|--expensi|--expensiv|--expensive)
909 TESTLIB_TEST_LONG=t; export TESTLIB_TEST_LONG; shift ;;
911 shift; test "$#" -ne 0 || {
912 echo 'error: -r requires an argument' >&2;
913 exit 1;
915 run_list=$1; shift ;;
916 --run=*)
917 run_list=${1#--*=}; shift ;;
918 -h|--h|--he|--hel|--help)
919 help=t; shift ;;
920 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
921 verbose=t; shift ;;
922 --verbose-only=*)
923 verbose_only=${1#--*=}
924 shift ;;
925 -q|--q|--qu|--qui|--quie|--quiet)
926 quiet=t; shift ;;
927 --color)
928 color=t; shift ;;
929 --no-color)
930 color=; shift ;;
931 --tee)
932 shift ;; # was handled already
933 --root=*)
934 root=${1#--*=}
935 shift ;;
936 --chain-lint)
937 TESTLIB_TEST_CHAIN_LINT=1
938 shift ;;
939 --no-chain-lint)
940 TESTLIB_TEST_CHAIN_LINT=0
941 shift ;;
942 -x|--x|--xt|--xtr|--xtra|--xtrac|--xtrace)
943 trace=t
944 verbose=t
945 shift ;;
946 --verbose-log)
947 verbose_log=t
948 shift ;;
950 echo "error: unknown test option '$1'" >&2; exit 1 ;;
951 esac
952 done
954 test "x${color+set}" != "xset" &&
955 test "x$TERM" != "xdumb" && (
956 { test -n "$TESTLIB_FORCETTY" || test -t 1; } &&
957 tput bold >/dev/null 2>&1 &&
958 tput setaf 1 >/dev/null 2>&1 &&
959 tput sgr0 >/dev/null 2>&1
960 ) &&
961 color=t
962 if test -n "$color"
963 then
964 # Save the color control sequences now rather than run tput
965 # each time say_color() is called. This is done for two
966 # reasons:
967 # * TERM will be changed to dumb
968 # * HOME will be changed to a temporary directory and tput
969 # might need to read ~/.terminfo from the original HOME
970 # directory to get the control sequences
971 getcmd say_color_error eval 'tput setaf 1' # red
972 getcmd say_color_skip eval 'tput bold; tput setaf 5' # bold blue
973 getcmd say_color_warn eval 'tput setaf 3' # brown/yellow
974 getcmd say_color_pass eval 'tput setaf 2' # green
975 getcmd say_color_info eval 'tput setaf 6' # cyan
976 getcmd say_color_reset eval 'tput sgr0'
977 say_color_="" # no formatting for normal text
980 TERM=dumb
981 export TERM
983 # Send any "-x" output directly to stderr to avoid polluting tests
984 # which capture stderr. We can do this unconditionally since it
985 # has no effect if tracing isn't turned on.
987 # Note that this sets up the trace fd as soon as we assign the variable, so it
988 # must come after the creation of descriptor 4 above. Likewise, we must never
989 # unset this, as it has the side effect of closing descriptor 4, which we
990 # use to show verbose tests to the user.
992 # Note also that we don't need or want to export it. The tracing is local to
993 # this shell, and we would not want to influence any shells we exec.
994 BASH_XTRACEFD=4
996 test_failure=0
997 test_count=0
998 test_fixed=0
999 test_broken=0
1000 test_success=0
1002 test_external_has_tap=0
1004 # The user-facing functions are loaded from a separate file
1005 . "$TESTLIB_DIRECTORY/test-lib-functions.sh"
1006 test_lib_functions_init
1008 last_verbose=t
1010 if [ -n "$TG_TEST_INSTALLED" ]; then
1011 [ -n "$(cmd_path tg || :)" ] ||
1012 fatal 'error: TG_TEST_INSTALLED set but no tg found in $PATH!'
1013 else
1014 tg_bin_dir="$(cd "$TESTLIB_DIRECTORY/../bin-wrappers" 2>/dev/null && pwd -P || :)"
1015 [ -x "$tg_bin_dir/tg" ] ||
1016 fatal 'error: no ../bin-wrappers/tg executable found!'
1017 PATH="$tg_bin_dir:$PATH"
1019 tg_version="$(tg --version)" ||
1020 fatal 'error: tg --version failed!'
1021 case "$tg_version" in [Tt][Oo][Pp][Gg][Ii][Tt]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn]\ [0-9]*);;*)
1022 fatal "error: tg --version returned bogus value: $tg_version"
1023 esac
1025 vcmp "$git_version" '>=' "$GIT_MINIMUM_VERSION" ||
1026 fatal "git version >= $GIT_MINIMUM_VERSION required but found \"$git_version\" instead"
1028 if test -z "$TESTLIB_TEST_CMP"
1029 then
1030 if test -n "$TESTLIB_TEST_CMP_USE_COPIED_CONTEXT"
1031 then
1032 TESTLIB_TEST_CMP="$DIFF -c"
1033 else
1034 TESTLIB_TEST_CMP="$DIFF -u"
1038 # Fix some commands on Windows
1039 case "${UNAME_S:=$(uname -s)}" in
1040 *MINGW*)
1041 # no POSIX permissions
1042 # backslashes in pathspec are converted to '/'
1043 # exec does not inherit the PID
1044 test_set_prereq MINGW
1045 test_set_prereq NATIVE_CRLF
1046 test_set_prereq SED_STRIPS_CR
1047 test_set_prereq GREP_STRIPS_CR
1048 TESTLIB_TEST_CMP=mingw_test_cmp
1050 *CYGWIN*)
1051 test_set_prereq POSIXPERM
1052 test_set_prereq EXECKEEPSPID
1053 test_set_prereq CYGWIN
1054 test_set_prereq SED_STRIPS_CR
1055 test_set_prereq GREP_STRIPS_CR
1058 test_set_prereq POSIXPERM
1059 test_set_prereq BSLASHPSPEC
1060 test_set_prereq EXECKEEPSPID
1062 esac
1064 ( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
1066 test_lib_main_init_funcs
1068 test_lazy_prereq PIPE '
1069 # test whether the filesystem supports FIFOs
1070 case "${UNAME_S:=$(uname -s)}" in
1071 CYGWIN*|MINGW*)
1072 false
1075 rm -f testfifo && mkfifo testfifo
1077 esac
1080 test_lazy_prereq SYMLINKS '
1081 # test whether the filesystem supports symbolic links
1082 ln -s x y && test -h y
1085 test_lazy_prereq FILEMODE '
1086 test "$(git config --bool core.filemode)" = true
1089 test_lazy_prereq CASE_INSENSITIVE_FS '
1090 echo good >CamelCase &&
1091 echo bad >camelcase &&
1092 test "$(cat CamelCase)" != good
1095 test_lazy_prereq UTF8_NFD_TO_NFC '
1096 # check whether FS converts nfd unicode to nfc
1097 auml="$(printf "\303\244")"
1098 aumlcdiar="$(printf "\141\314\210")"
1099 >"$auml" &&
1100 case "$(echo *)" in
1101 "$aumlcdiar")
1102 true ;;
1104 false ;;
1105 esac
1108 test_lazy_prereq AUTOIDENT '
1109 sane_unset GIT_AUTHOR_NAME &&
1110 sane_unset GIT_AUTHOR_EMAIL &&
1111 git var GIT_AUTHOR_IDENT
1114 test_lazy_prereq EXPENSIVE '
1115 test -n "$TESTLIB_TEST_LONG"
1118 test_lazy_prereq USR_BIN_TIME '
1119 test -x /usr/bin/time
1122 test_lazy_prereq NOT_ROOT '
1123 uid="$(id -u)" &&
1124 test "$uid" != 0
1127 # SANITY is about "can you correctly predict what the filesystem would
1128 # do by only looking at the permission bits of the files and
1129 # directories?" A typical example of !SANITY is running the test
1130 # suite as root, where a test may expect "chmod -r file && cat file"
1131 # to fail because file is supposed to be unreadable after a successful
1132 # chmod. In an environment (i.e. combination of what filesystem is
1133 # being used and who is running the tests) that lacks SANITY, you may
1134 # be able to delete or create a file when the containing directory
1135 # doesn't have write permissions, or access a file even if the
1136 # containing directory doesn't have read or execute permissions.
1138 test_lazy_prereq SANITY '
1139 mkdir SANETESTD.1 SANETESTD.2 &&
1141 chmod +w SANETESTD.1 SANETESTD.2 &&
1142 >SANETESTD.1/x 2>SANETESTD.2/x &&
1143 chmod -w SANETESTD.1 &&
1144 chmod -r SANETESTD.1/x &&
1145 chmod -rx SANETESTD.2 ||
1146 error "bug in test sript: cannot prepare SANETESTD"
1148 ! test -r SANETESTD.1/x &&
1149 ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
1150 status=$?
1152 chmod +rwx SANETESTD.1 SANETESTD.2 &&
1153 rm -rf SANETESTD.1 SANETESTD.2 ||
1154 error "bug in test sript: cannot clean SANETESTD"
1155 return $status
1158 test_lazy_prereq CMDLINE_LIMIT 'run_with_limited_cmdline true'
1161 # End test_lib_main_init_generic
1165 # This function is guaranteed to always be called for every single test.
1166 # Only put things in this function that MUST be done per-test, function
1167 # definitions and sourcing other files generally DO NOT QUALIFY (there can
1168 # be exceptions).
1169 test_lib_main_init_specific() {
1170 # Begin test_lib_main_init_specific
1173 # original stdin is on 6, stdout on 5 and stderr on 7
1174 exec 5>&1 6<&0 7>&2
1176 test_lib_main_init_funcs
1178 if test -n "$HARNESS_ACTIVE"
1179 then
1180 if test "$verbose" = t || test -n "$verbose_only" && test -z "$TESTLIB_OVERRIDE"
1181 then
1182 printf 'Bail out! %s\n' \
1183 'verbose mode forbidden under TAP harness; try --verbose-log'
1184 exit 1
1188 test "${test_description}" != "" ||
1189 error "Test script did not set test_description."
1191 if test "$help" = "t"
1192 then
1193 printf '%s\n' "$(printf '%s\n' "$test_description")" |
1194 sed -n -e '
1196 :loop
1197 s/\([^ ]\)/\1/
1198 t rest
1200 b loop
1202 :rest
1205 exit 0
1208 if test "$verbose_log" = "t"
1209 then
1210 exec 3>>"$TESTLIB_TEST_TEE_OUTPUT_FILE" 4>&3
1211 elif test "$verbose" = "t"
1212 then
1213 exec 4>&2 3>&1
1214 else
1215 exec 4>/dev/null 3>/dev/null
1218 TESTLIB_EXIT_OK=
1219 trap 'die' EXIT
1220 trap 'exit $?' HUP INT QUIT ABRT PIPE TERM
1221 trap 'TESTLIB_EXIT_OK=t; exit 1' USR1
1223 # Test repository
1224 TRASH_DIRECTORY="trash directory.${0##*/}"
1225 TRASH_DIRECTORY="${TRASH_DIRECTORY%.sh}"
1226 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
1227 case "$TRASH_DIRECTORY" in
1228 /*) ;; # absolute path is good
1229 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
1230 esac
1231 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
1232 ! test -e "$TRASH_DIRECTORY" || {
1233 rm -rf "$TRASH_DIRECTORY" &&
1234 ! test -e "$TRASH_DIRECTORY" || {
1235 chmod -R u+w "$TRASH_DIRECTORY" &&
1236 rm -rf "$TRASH_DIRECTORY" &&
1237 ! test -e "$TRASH_DIRECTORY"
1239 } || {
1240 TESTLIB_EXIT_OK=t
1241 echo >&5 "FATAL: Cannot prepare test area"
1242 exit 1
1245 HOME="$TRASH_DIRECTORY"
1246 GNUPGHOME="$HOME/gnupg-home-not-used"
1247 export HOME GNUPGHOME
1249 if test -z "$TEST_NO_CREATE_REPO"
1250 then
1251 test_create_repo "$TRASH_DIRECTORY"
1252 else
1253 mkdir -p "$TRASH_DIRECTORY"
1255 # Use -P to resolve symlinks in our working directory so that the cwd
1256 # in subprocesses like tg equals our $PWD (for pathname comparisons).
1257 cd -P "$TRASH_DIRECTORY" || exit 1
1259 this_test=${0##*/}
1260 this_test=${this_test%%-*}
1261 if match_pattern_list "$this_test" $TESTLIB_SKIP_TESTS
1262 then
1263 say_color info >&3 "skipping test $this_test altogether"
1264 skip_all="skip all tests in $this_test"
1265 test_done
1269 # End test_lib_main_init_specific
1274 # THIS SHOULD ALWAYS BE THE LAST FUNCTION DEFINED IN THIS FILE
1276 # Any client that sources this file should immediately execute this function
1277 # afterwards with the command line arguments
1279 # THERE SHOULD NOT BE ANY DIRECTLY EXECUTED LINES OF CODE IN THIS FILE
1281 test_lib_main_init() {
1283 test_lib_main_init_generic "$@"
1284 test_lib_main_init_specific "$@"