test-lib-functions-tg.sh: introduce TopGit-specific test functions library
[topgit/pro.git] / t / test-lib-main.sh
blobe3c1c7d451de2a91dc068acf7f7e9622269d9060
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
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)
58 getcmd() {
59 [ -n "$1" ] || return 1
60 eval "$1=" >/dev/null 2>&1 || return 1
61 [ -n "$2" ] || return 1
62 _getcmd_vn="$1"
63 shift
64 _getcmd_ec=0
65 _getcmd_result="$(ec=0; ("$@") || ec=$?; echo Z; exit $ec)" || _getcmd_ec=$?
66 eval "$_getcmd_vn=\"\${_getcmd_result%Z}\""
67 return $_getcmd_ec
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
73 whats_the_dir() {
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
77 set -- "$3" "$4" "$1"
78 else
79 set -- "$2" "$3" "$1"
81 elif [ "z$1" = "z--" ]; then
82 shift
84 case "$1" in *"/"*);;*) set -- "./$1" "$2" "$3"; esac
85 while [ -L "$1" ]; do
86 set -- "$(readlink "$1")" "$2" "$3" "$1"
87 case "$1" in "/"*);;*)
88 set -- "${4%/*}/$1" "$2" "$3"
89 esac
90 done
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
96 set -- "$PWD" "$2"
97 else
98 set -- "$(cd "$1" && pwd $3)" "$2"
101 eval "$2=\"$1\""
104 vcmp() {
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.]*}"
116 while
117 vcmp_a_="${1%%.*}"
118 vcmp_b_="${3%%.*}"
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
124 return 1
125 elif [ "${vcmp_a_:-0}" -gt "${vcmp_b_:-0}" ]; then
126 unset vcmp_a_ vcmp_b_
127 case "$2" in ">"|">="|"!=") return 0; esac
128 return 1;
130 vcmp_a_="${1#$vcmp_a_}"
131 vcmp_b_="${3#$vcmp_b_}"
132 set -- "${vcmp_a_#.}" "$2" "${vcmp_b_#.}"
133 done
134 unset vcmp_a_ vcmp_b_
135 case "$2" in "="|"=="|"<="|">=") return 0; esac
136 return 1
139 vcmp "$@"
141 error() {
142 say_color error "${LF}error: $*" >&7
143 printf '%s\n' "Bail out! ${0##*/}:${callerlno:+$callerlno:} error: $*" >&5
144 TESTLIB_EXIT_OK=t
145 [ -z "$TESTLIB_TEST_PARENT_INT_ON_ERROR" ] || kill -INT $PPID || :
146 kill -USR1 $$
147 exit 1
150 say() {
151 say_color info "$@"
154 say_tap() {
155 say_color_tap info "$@"
158 _die() {
159 code=$?
160 if test -n "$TESTLIB_EXIT_OK"
161 then
162 exit $code
163 else
164 echo >&5 "FATAL: Unexpected exit with code $code"
165 exit 1
168 die() { _die "$@"; }
170 # You are not expected to call test_ok_ and test_failure_ directly, use
171 # the test_expect_* functions instead.
173 test_ok_() {
174 test_success=$(($test_success + 1))
175 say_color_tap "" "ok $test_count - $@"
178 test_failure_() {
179 test_failure=$(($test_failure + 1))
180 tlno="$1"
181 shift
182 say_color_tap error "not ok $test_count - $1"
183 shift
184 printf '%s\n' "$(printf '%s\n' "failed: ${0##*/}${tlno:+:$tlno}$LF$*")" |
185 sed -n -e '
187 :loop
188 s/\([^ ]\)/\1/
189 t first
190 b continue
191 :first
194 b rest
195 :continue
197 b loop
199 :rest
200 s/^/# /
202 $ i\
205 test "$immediate" = "" || { TESTLIB_EXIT_OK=t; exit 1; }
208 test_known_broken_ok_() {
209 test_fixed=$(($test_fixed + 1))
210 say_color_tap warn "ok $test_count - $@ # TODO known breakage vanished"
213 test_known_broken_failure_() {
214 test_broken=$(($test_broken + 1))
215 say_color_tap warn "not ok $test_count - $@ # TODO known breakage"
218 test_possibly_broken_ok_() {
219 test_success=$(($test_success + 1))
220 say_color_tap "" "ok $test_count - $@"
223 test_possibly_broken_failure_() {
224 test_broken=$(($test_broken + 1))
225 say_color_tap warn "not ok $test_count - $@ # TODO tolerated breakage"
228 test_debug() {
229 test "$debug" = "" || test $# -eq 0 || test -z "$*" || { "$@"; } >&7 2>&1
232 match_pattern_list() {
233 arg="$1"
234 shift
235 test -z "$*" && return 1
236 for pattern_
238 case "$arg" in
239 $pattern_)
240 return 0
241 esac
242 done
243 return 1
246 match_test_selector_list() {
247 title="$1"
248 shift
249 arg="$1"
250 shift
251 test -z "$1" && return 0
253 # Both commas and whitespace are accepted as separators.
254 OLDIFS=$IFS
255 IFS=' ,'
256 set -- $1
257 IFS=$OLDIFS
259 # If the first selector is negative we include by default.
260 include=
261 case "$1" in
262 !*) include=t ;;
263 esac
265 for selector
267 orig_selector=$selector
269 positive=t
270 case "$selector" in
272 positive=
273 selector=${selector##?}
275 esac
277 test -z "$selector" && continue
279 case "$selector" in
280 *-*)
281 if x_="${selector%%-*}" && test "z$x_" != "z${x_#*[!0-9]}"
282 then
283 echo "error: $title: invalid non-numeric in range" \
284 "start: '$orig_selector'" >&2
285 exit 1
287 if x_="${selector#*-}" && test "z$x_" != "z${x_#*[!0-9]}"
288 then
289 echo "error: $title: invalid non-numeric in range" \
290 "end: '$orig_selector'" >&2
291 exit 1
293 unset x_
296 if test "z$selector" != "z${selector#*[!0-9]}"
297 then
298 echo "error: $title: invalid non-numeric in test" \
299 "selector: '$orig_selector'" >&2
300 exit 1
302 esac
304 # Short cut for "obvious" cases
305 test -z "$include" && test -z "$positive" && continue
306 test -n "$include" && test -n "$positive" && continue
308 case "$selector" in
310 if test $arg -le ${selector#-}
311 then
312 include=$positive
316 if test $arg -ge ${selector%-}
317 then
318 include=$positive
321 *-*)
322 if test ${selector%%-*} -le $arg \
323 && test $arg -le ${selector#*-}
324 then
325 include=$positive
329 if test $arg -eq $selector
330 then
331 include=$positive
334 esac
335 done
337 test -n "$include"
340 maybe_teardown_verbose() {
341 test -z "$verbose_only" && return
342 exec 4>/dev/null 3>/dev/null
343 verbose=
346 maybe_setup_verbose() {
347 test -z "$verbose_only" && return
348 if match_pattern_list $test_count $verbose_only
349 then
350 if test "$verbose_log" = "t"
351 then
352 exec 3>>"$TESTLIB_TEST_TEE_OUTPUT_FILE" 4>&3
353 else
354 exec 4>&2 3>&1
356 # Emit a delimiting blank line when going from
357 # non-verbose to verbose. Within verbose mode the
358 # delimiter is printed by test_expect_*. The choice
359 # of the initial $last_verbose is such that before
360 # test 1, we do not print it.
361 test -z "$last_verbose" && echo >&3 ""
362 verbose=t
363 else
364 exec 4>/dev/null 3>/dev/null
365 verbose=
367 last_verbose=$verbose
370 want_trace() {
371 test "$trace" = t && test "$verbose" = t
374 # This is a separate function because some tests use
375 # "return" to end a test_expect_success block early
376 # (and we want to make sure we run any cleanup like
377 # "set +x").
378 test_eval_inner_() (
379 # Do not add anything extra (including LF) after '$*'
380 eval "
381 set -e
382 test_subshell_active_=t
383 ! want_trace || ! set -x && ! :
387 # Same thing as test_eval_inner_ but without the subshell
388 test_eval_inner_no_subshell_() {
389 # Do not add anything extra (including LF) after '$*'
390 eval "
391 ! want_trace || ! set -x && ! :
395 test_eval_ss_() {
396 # We run this block with stderr redirected to avoid extra cruft
397 # during a "-x" trace. Once in "set -x" mode, we cannot prevent
398 # the shell from printing the "set +x" to turn it off (nor the saving
399 # of $? before that). But we can make sure that the output goes to
400 # /dev/null.
402 # The test itself is run with stderr put back to &4 (so either to
403 # /dev/null, or to the original stderr if --verbose was used).
405 test_eval_ss_="$1"
406 shift
407 if test "${test_eval_ss_:-0}" = "0"
408 then
409 test_eval_inner_no_subshell_ "$@" </dev/null >&3 2>&4
410 else
411 test_eval_inner_ "$@" </dev/null >&3 2>&4
413 test_eval_ret_=$?
414 if want_trace
415 then
416 set +x
417 if test "$test_eval_ret_" != 0
418 then
419 say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
422 } 2>/dev/null
423 return $test_eval_ret_
426 # Calls the real test_eval_ss_ with !"$TESTLIB_TEST_NO_SUBSHELL" as first arg
427 test_eval_() {
428 if test -n "$TESTLIB_TEST_NO_SUBSHELL"
429 then
430 test_eval_ss_ "0" "$@"
431 else
432 test_eval_ss_ "1" "$@"
436 # If "$1" = "-" read the script from stdin but ONLY if stdin is NOT a tty
437 # Store the test script in test_script_
438 test_get_() {
439 if test "x$1" = "x-"
440 then
441 ! test -t 0 || error "test script is '-' but STDIN is a tty"
442 test_script_="$(cat)"
443 test -n "$test_script_" || error "test script is '-' but STDIN is empty"
444 test_script_="$LF$test_script_$LF"
445 else
446 test_script_="$1"
450 fail_() {
451 return ${1:-1}
454 test_run_() {
455 test_cleanup=:
456 test_subshell_active_=
457 expecting_failure=$2
458 linting=
460 if test "${TESTLIB_TEST_CHAIN_LINT:-1}" != 0; then
461 # turn off tracing for this test-eval, as it simply creates
462 # confusing noise in the "-x" output
463 trace_tmp=$trace
464 trace=
465 linting=t
466 # 117 is magic because it is unlikely to match the exit
467 # code of other programs
468 test_eval_ss_ "1" "fail_ 117 && $1${LF}fail_ \$?"
469 if test "$?" != 117; then
470 error "bug in the test script: broken &&-chain: $1"
472 trace=$trace_tmp
473 linting=
476 test_eval_ "$1"
477 eval_ret=$?
479 if test -z "$immediate" || test $eval_ret = 0 ||
480 test -n "$expecting_failure" && test "${test_cleanup:-:}" != ":"
481 then
482 test_eval_ "$test_cleanup"
484 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
485 then
486 echo ""
488 return "$eval_ret"
491 test_start_() {
492 test_count=$(($test_count+1))
493 maybe_setup_verbose
496 test_finish_() {
497 echo >&3 ""
498 maybe_teardown_verbose
501 test_skip() {
502 to_skip=
503 skipped_reason=
504 if match_pattern_list $this_test.$test_count $TESTLIB_SKIP_TESTS
505 then
506 to_skip=t
507 skipped_reason="TESTLIB_SKIP_TESTS"
509 if test -z "$to_skip" && test -n "$test_prereq" &&
510 ! test_have_prereq "$test_prereq"
511 then
512 to_skip=t
514 of_prereq=
515 if test "$missing_prereq" != "$test_prereq"
516 then
517 of_prereq=" of $test_prereq"
519 skipped_reason="missing $missing_prereq${of_prereq}"
521 if test -z "$to_skip" && test -n "$run_list" &&
522 ! match_test_selector_list '--run' $test_count "$run_list"
523 then
524 to_skip=t
525 skipped_reason="--run"
528 case "$to_skip" in
530 say_color skip >&3 "skipping test: $@"
531 say_color_tap skip "ok $test_count # skip $1 ($skipped_reason)"
532 : true
535 false
537 esac
540 # stub; runs at end of each successful test
541 test_at_end_hook_() {
545 test_done() {
546 TESTLIB_EXIT_OK=t
548 if test -z "$HARNESS_ACTIVE"
549 then
550 test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
551 mkdir -p "$test_results_dir"
552 base=${0##*/}
553 test_results_path="$test_results_dir/${base%.sh}.counts"
555 cat >"$test_results_path" <<-EOF
556 total $test_count
557 success $test_success
558 fixed $test_fixed
559 broken $test_broken
560 failed $test_failure
565 if test "$test_fixed" != 0
566 then
567 say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
569 if test "$test_broken" != 0
570 then
571 say_color warn "# still have $test_broken known breakage(s)"
573 if test "$test_broken" != 0 || test "$test_fixed" != 0
574 then
575 test_remaining=$(( $test_count - $test_broken - $test_fixed ))
576 msg="remaining $test_remaining test(s)"
577 else
578 test_remaining=$test_count
579 msg="$test_count test(s)"
581 case "$test_failure" in
583 # Maybe print SKIP message
584 if test -n "$skip_all" && test $test_count -gt 0
585 then
586 error "Can't use skip_all after running some tests"
588 test -z "$skip_all" || skip_all=" # SKIP $skip_all"
590 if test $test_external_has_tap -eq 0
591 then
592 if test $test_remaining -gt 0
593 then
594 say_color pass "# passed all $msg"
596 test -n "$test_wrote_plan_count" || say_tap "1..$test_count$skip_all"
598 if test -n "$test_wrote_plan_count" && test "$test_wrote_plan_count" -ne "$test_count"
599 then
600 say_color error "# plan count of $test_wrote_plan_count does not match run count of $test_count"
601 exit 1
604 test -n "$remove_trash" &&
605 test -d "$remove_trash" &&
606 cd "${remove_trash%/*}" &&
607 test_done_td_="${remove_trash##*/}" &&
608 test -e "$test_done_td_" &&
609 rm -rf "$test_done_td_" &&
611 ! test -e "$test_done_td_" || {
612 chmod -R u+w "$test_done_td_" &&
613 rm -rf "$test_done_td_"
617 test_at_end_hook_
619 exit 0 ;;
622 if test $test_external_has_tap -eq 0
623 then
624 say_color error "# failed $test_failure among $msg"
625 test -n "$test_wrote_plan_count" || say_tap "1..$test_count"
627 if test -n "$test_wrote_plan_count" && test "$test_wrote_plan_count" -ne "$test_count"
628 then
629 say_color error "# plan count of $test_wrote_plan_count does not match run count of $test_count"
632 test -z "$HARNESS_ACTIVE" || exit 0
633 exit 1 ;;
635 esac
638 test_plan() {
639 test -n "$1" && test "z$1" = "z${1#*[!0-9]}" || fatal "invalid test_plan argument: $1"
640 test "$1" -eq 0 || test -z "$2" || fatal "invalid test_plan arguments: $*"
641 if test "$1" -eq 0; then
642 skip_all="${2:-skip all tests in $this_test}"
643 test_done
645 test $test_external_has_tap -ne 0 || say_tap "1..$1"
646 test_wrote_plan_count="$1"
649 # Provide an implementation of the 'yes' utility
650 yes() {
651 if test $# = 0
652 then
654 else
655 y="$*"
659 while test $i -lt 99
661 echo "$y"
662 i=$(($i+1))
663 done
666 run_with_limited_cmdline() {
667 (ulimit -s 128 && "$@")
672 ## Note that the following functions have bodies that are NOT indented
673 ## to assist with readability
677 test_lib_main_init_tee() {
678 # Begin test_lib_main_init_tee
681 # if --tee was passed, write the output not only to the terminal, but
682 # additionally to the file test-results/$BASENAME.out, too.
683 case "$TESTLIB_TEST_TEE_STARTED, $* " in
684 done,*)
685 # do not redirect again
687 *' --tee '*|*' --verbose-log '*)
688 mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
689 BASE="$TEST_OUTPUT_DIRECTORY/test-results/${0##*/}"
690 BASE="${BASE%.sh}"
692 # Make this filename available to the sub-process in case it is using
693 # --verbose-log.
694 TESTLIB_TEST_TEE_OUTPUT_FILE=$BASE.out
695 export TESTLIB_TEST_TEE_OUTPUT_FILE
697 # Truncate before calling "tee -a" to get rid of the results
698 # from any previous runs.
699 >"$TESTLIB_TEST_TEE_OUTPUT_FILE"
700 >"$BASE.exit"
702 (ec=0; TESTLIB_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1 || ec=$?
703 echo $ec >"$BASE.exit") | tee -a "$TESTLIB_TEST_TEE_OUTPUT_FILE"
704 exitcode="$(cat "$BASE.exit" 2>/dev/null)" || :
705 exit ${exitcode:-1}
707 esac
710 # End test_lib_main_init_tee
714 test_lib_main_init_funcs() {
715 # Begin test_lib_main_init_funcs
718 [ -z "$test_lib_main_init_funcs_done" ] || return 0
720 if test -n "$color"
721 then
722 say_color() {
723 test -z "$1" && test -n "$quiet" && return
724 eval "say_color_color=\$say_color_$1"
725 shift
726 _sfc=
727 _sms="$*"
728 if test -n "$HARNESS_ACTIVE"
729 then
730 case "$_sms" in '#'*)
731 _sfc='#'
732 _sms="${_sms#?}"
733 esac
735 printf '%s\n' "$_sfc$say_color_color$_sms$say_color_reset"
737 else
738 say_color() {
739 test -z "$1" && test -n "$quiet" && return
740 shift
741 printf '%s\n' "$*"
745 # Just like say_color except if HARNESS_ACTIVE it's ALWAYS output and WITHOUT color
746 say_color_tap() {
747 if test -n "$HARNESS_ACTIVE"
748 then
749 shift
750 printf '%s\n' "$*"
751 else
752 say_color "$@"
757 # Fix some commands on Windows
758 case "${UNAME_S:=$(uname -s)}" in
759 *MINGW*)
760 # Windows has its own (incompatible) sort and find
761 sort() {
762 /usr/bin/sort "$@"
764 find() {
765 /usr/bin/find "$@"
767 sum() {
768 md5sum "$@"
770 # git sees Windows-style pwd
771 pwd() {
772 builtin pwd -W
775 esac
777 test_lib_main_init_funcs_done=1
780 # End test_lib_main_init_funcs
784 # This function is called with all the test args and must perform all
785 # initialization that involves variables and is not specific to "$0"
786 # or "$test_description" in any way. This function may only be called
787 # once per run of the entire test suite.
788 test_lib_main_init_generic() {
789 # Begin test_lib_main_init_generic
791 [ -n "$TESTLIB_DIRECTORY" ] || whats_the_dir -L -- "${TEST_DIRECTORY:-.}/test-lib.sh" TESTLIB_DIRECTORY
792 [ -f "$TESTLIB_DIRECTORY/test-lib.sh" ] && [ -f "$TESTLIB_DIRECTORY/test-lib-main.sh" ] &&
793 [ -f "$TESTLIB_DIRECTORY/test-lib-functions.sh" ] &&
794 [ -f "$TESTLIB_DIRECTORY/test-lib-functions-tg.sh" ] ||
795 fatal "error: invalid TESTLIB_DIRECTORY: $TESTLIB_DIRECTORY"
796 export TESTLIB_DIRECTORY
798 ! [ -f "$TESTLIB_DIRECTORY/../TG-BUILD-SETTINGS" ] || . "$TESTLIB_DIRECTORY/../TG-BUILD-SETTINGS"
799 ! [ -f "$TESTLIB_DIRECTORY/TG-TEST-SETTINGS" ] || . "$TESTLIB_DIRECTORY/TG-TEST-SETTINGS"
801 : "${SHELL_PATH:=/bin/sh}"
802 : "${DIFF:=diff}"
803 : "${GIT_PATH:=$(cmd_path git)}"
804 : "${PERL_PATH:=$(cmd_path perl || :)}"
806 # Test the binaries we have just built. The tests are kept in
807 # t/ subdirectory and are run in 'trash directory' subdirectory.
808 if test -z "$TEST_DIRECTORY"
809 then
810 # We allow tests to override this, in case they want to run tests
811 # outside of t/, e.g. for running tests on the test library
812 # itself.
813 TEST_DIRECTORY="$TESTLIB_DIRECTORY"
814 else
815 # ensure that TEST_DIRECTORY is an absolute path so that it
816 # is valid even if the current working directory is changed
817 TEST_DIRECTORY="$(cd "$TEST_DIRECTORY" && pwd)" || exit 1
819 if test -z "$TEST_OUTPUT_DIRECTORY"
820 then
821 # Similarly, override this to store the test-results subdir
822 # elsewhere
823 TEST_OUTPUT_DIRECTORY="$TEST_DIRECTORY"
825 [ -d "$TESTLIB_DIRECTORY"/empty ] || {
826 mkdir "$TESTLIB_DIRECTORY/empty" || :
827 chmod a-w "$TESTLIB_DIRECTORY/empty" || :
828 test -d "$TESTLIB_DIRECTORY"/empty ||
829 fatal "error: could not make empty directory: '$TESTLIB_DIRECTORY/empty'"
831 EMPTY_DIRECTORY="$TESTLIB_DIRECTORY/empty"
832 export TEST_DIRECTORY TEST_OUTPUT_DIRECTORY EMPTY_DIRECTORY
833 GIT_CEILING_DIRECTORIES="$TESTLIB_DIRECTORY"
834 [ "$TESTLIB_DIRECTORY" = "$TEST_DIRECTORY" ] ||
835 GIT_CEILING_DIRECTORIES="$TEST_DIRECTORY:$GIT_CEILING_DIRECTORIES"
836 [ "$TESTLIB_DIRECTORY" = "$TEST_OUTPUT_DIRECTORY" ] ||
837 GIT_CEILING_DIRECTORIES="$TEST_OUTPUT_DIRECTORY:$GIT_CEILING_DIRECTORIES"
838 export GIT_CEILING_DIRECTORIES
840 ################################################################
841 # It appears that people try to run tests with missing perl or git...
842 git_version="$("$GIT_PATH" --version 2>&1)" ||
843 fatal 'error: you do not seem to have git available?'
844 case "$git_version" in [Gg][Ii][Tt]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn]\ [0-9]*);;*)
845 fatal "error: git --version returned bogus value: $git_version"
846 esac
847 #"$PERL_PATH" --version >/dev/null 2>&1 ||
848 # fatal 'error: you do not seem to have perl available?'
850 test_lib_main_init_tee "$@"
852 # For repeatability, reset the environment to known value.
853 # TERM is sanitized below, after saving color control sequences.
854 LANG=C
855 LC_ALL=C
856 PAGER=cat
857 TZ=UTC
858 export LANG LC_ALL PAGER TZ
859 EDITOR=:
860 # A call to "unset" with no arguments causes at least Solaris 10
861 # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
862 # deriving from the command substitution clustered with the other
863 # ones.
864 unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
865 my @env = keys %ENV;
866 my $ok = join("|", qw(
867 TRACE
868 DEBUG
869 USE_LOOKUP
870 TEST
871 .*_TEST
872 MINIMUM_VERSION
873 PATH
874 PROVE
875 UNZIP
876 PERF_
877 CURL_VERBOSE
878 TRACE_CURL
879 CEILING_DIRECTORIES
881 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
882 print join("\n", @vars);
884 unset XDG_CONFIG_HOME
885 unset GITPERLLIB
886 GIT_AUTHOR_NAME='Te s t'
887 GIT_AUTHOR_EMAIL=test@example.net
888 GIT_COMMITTER_NAME='Fra mewor k'
889 GIT_COMMITTER_EMAIL=framework@example.org
890 GIT_MERGE_VERBOSITY=5
891 GIT_MERGE_AUTOEDIT=no
892 GIT_TEMPLATE_DIR="$EMPTY_DIRECTORY"
893 GIT_CONFIG_NOSYSTEM=1
894 GIT_ATTR_NOSYSTEM=1
895 export PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
896 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
897 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
898 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
899 export EDITOR
901 # Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
902 GIT_TRACE_BARE=1
903 export GIT_TRACE_BARE
905 # Protect ourselves from common misconfiguration to export
906 # CDPATH into the environment
907 unset CDPATH
909 unset GREP_OPTIONS
910 unset UNZIP
912 case "$GIT_TRACE" in 1|2|[Tt][Rr][Uu][Ee])
913 GIT_TRACE=4
915 esac
917 # Convenience
919 # A regexp to match 5 and 40 hexdigits
920 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
921 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
923 # Zero SHA-1
924 _z40=0000000000000000000000000000000000000000
926 EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
927 EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
929 # Line feed
930 LF='
933 # UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
934 # when case-folding filenames
935 u200c="$(printf '\342\200\214')"
937 export _x05 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB
939 while test "$#" -ne 0
941 case "$1" in
942 -d|--d|--de|--deb|--debu|--debug)
943 debug=t; shift ;;
944 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
945 immediate=t; shift ;;
946 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests|\
947 --ex|--exp|--expe|--expen|--expens|--expensi|--expensiv|--expensive)
948 TESTLIB_TEST_LONG=t; export TESTLIB_TEST_LONG; shift ;;
950 shift; test "$#" -ne 0 || {
951 echo 'error: -r requires an argument' >&2;
952 exit 1;
954 run_list=$1; shift ;;
955 --run=*)
956 run_list=${1#--*=}; shift ;;
957 -h|--h|--he|--hel|--help)
958 help=t; shift ;;
959 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
960 verbose=t; shift ;;
961 --verbose-only=*)
962 verbose_only=${1#--*=}
963 shift ;;
964 -q|--q|--qu|--qui|--quie|--quiet)
965 quiet=t; shift ;;
966 --color)
967 color=t; shift ;;
968 --no-color)
969 color=; shift ;;
970 --tee)
971 shift ;; # was handled already
972 --root=*)
973 root=${1#--*=}
974 shift ;;
975 --chain-lint)
976 TESTLIB_TEST_CHAIN_LINT=1
977 shift ;;
978 --no-chain-lint)
979 TESTLIB_TEST_CHAIN_LINT=0
980 shift ;;
981 -x|--x|--xt|--xtr|--xtra|--xtrac|--xtrace)
982 trace=t
983 verbose=t
984 shift ;;
985 --verbose-log)
986 verbose_log=t
987 shift ;;
989 echo "error: unknown test option '$1'" >&2; exit 1 ;;
990 esac
991 done
993 test "x${color+set}" != "xset" &&
994 test "x$TERM" != "xdumb" && (
995 { test -n "$TESTLIB_FORCETTY" || test -t 1; } &&
996 tput bold >/dev/null 2>&1 &&
997 tput setaf 1 >/dev/null 2>&1 &&
998 tput sgr0 >/dev/null 2>&1
999 ) &&
1000 color=t
1001 if test -n "$color"
1002 then
1003 # Save the color control sequences now rather than run tput
1004 # each time say_color() is called. This is done for two
1005 # reasons:
1006 # * TERM will be changed to dumb
1007 # * HOME will be changed to a temporary directory and tput
1008 # might need to read ~/.terminfo from the original HOME
1009 # directory to get the control sequences
1010 getcmd say_color_error eval 'tput setaf 1' # red
1011 getcmd say_color_skip eval 'tput bold; tput setaf 5' # bold blue
1012 getcmd say_color_warn eval 'tput setaf 3' # brown/yellow
1013 getcmd say_color_pass eval 'tput setaf 2' # green
1014 getcmd say_color_info eval 'tput setaf 6' # cyan
1015 getcmd say_color_reset eval 'tput sgr0'
1016 say_color_="" # no formatting for normal text
1019 TERM=dumb
1020 export TERM
1022 # Send any "-x" output directly to stderr to avoid polluting tests
1023 # which capture stderr. We can do this unconditionally since it
1024 # has no effect if tracing isn't turned on.
1026 # Note that this sets up the trace fd as soon as we assign the variable, so it
1027 # must come after the creation of descriptor 4 above. Likewise, we must never
1028 # unset this, as it has the side effect of closing descriptor 4, which we
1029 # use to show verbose tests to the user.
1031 # Note also that we don't need or want to export it. The tracing is local to
1032 # this shell, and we would not want to influence any shells we exec.
1033 BASH_XTRACEFD=4
1035 test_failure=0
1036 test_count=0
1037 test_fixed=0
1038 test_broken=0
1039 test_success=0
1041 test_external_has_tap=0
1043 # The user-facing functions are loaded from a separate file
1044 . "$TESTLIB_DIRECTORY/test-lib-functions-tg.sh"
1045 . "$TESTLIB_DIRECTORY/test-lib-functions.sh"
1046 test_lib_functions_init
1047 test_lib_functions_tg_init
1049 last_verbose=t
1051 [ -d "$TEST_DIRECTORY/helper" ] && PATH="$TEST_DIRECTORY/helper:$PATH"
1052 if [ -n "$TG_TEST_INSTALLED" ]; then
1053 TG_FULL_PATH="$(cmd_path tg)" && [ -n "$TG_FULL_PATH" ] ||
1054 fatal 'error: TG_TEST_INSTALLED set but no tg found in $PATH!'
1055 else
1056 tg_bin_dir="$(cd "$TESTLIB_DIRECTORY/../bin-wrappers" 2>/dev/null && pwd -P || :)"
1057 [ -x "$tg_bin_dir/tg" ] ||
1058 fatal 'error: no ../bin-wrappers/tg executable found!'
1059 PATH="$tg_bin_dir:$PATH"
1060 TG_FULL_PATH="$tg_bin_dir/tg"
1062 export TG_FULL_PATH
1063 tg_version="$(tg --version)" ||
1064 fatal 'error: tg --version failed!'
1065 case "$tg_version" in [Tt][Oo][Pp][Gg][Ii][Tt]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn]\ [0-9]*);;*)
1066 fatal "error: tg --version returned bogus value: $tg_version"
1067 esac
1069 vcmp "$git_version" '>=' "$GIT_MINIMUM_VERSION" ||
1070 fatal "git version >= $GIT_MINIMUM_VERSION required but found \"$git_version\" instead"
1072 if test -z "$TESTLIB_TEST_CMP"
1073 then
1074 if test -n "$TESTLIB_TEST_CMP_USE_COPIED_CONTEXT"
1075 then
1076 TESTLIB_TEST_CMP="$DIFF -c"
1077 else
1078 TESTLIB_TEST_CMP="$DIFF -u"
1082 # Fix some commands on Windows
1083 case "${UNAME_S:=$(uname -s)}" in
1084 *MINGW*)
1085 # no POSIX permissions
1086 # backslashes in pathspec are converted to '/'
1087 # exec does not inherit the PID
1088 test_set_prereq MINGW
1089 test_set_prereq NATIVE_CRLF
1090 test_set_prereq SED_STRIPS_CR
1091 test_set_prereq GREP_STRIPS_CR
1092 TESTLIB_TEST_CMP=mingw_test_cmp
1094 *CYGWIN*)
1095 test_set_prereq POSIXPERM
1096 test_set_prereq EXECKEEPSPID
1097 test_set_prereq CYGWIN
1098 test_set_prereq SED_STRIPS_CR
1099 test_set_prereq GREP_STRIPS_CR
1102 test_set_prereq POSIXPERM
1103 test_set_prereq BSLASHPSPEC
1104 test_set_prereq EXECKEEPSPID
1106 esac
1108 ( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
1110 test_lib_main_init_funcs
1112 test_lazy_prereq PIPE '
1113 # test whether the filesystem supports FIFOs
1114 case "${UNAME_S:=$(uname -s)}" in
1115 CYGWIN*|MINGW*)
1116 false
1119 rm -f testfifo && mkfifo testfifo
1121 esac
1124 test_lazy_prereq SYMLINKS '
1125 # test whether the filesystem supports symbolic links
1126 ln -s x y && test -h y
1129 test_lazy_prereq FILEMODE '
1130 test "$(git config --bool core.filemode)" = true
1133 test_lazy_prereq CASE_INSENSITIVE_FS '
1134 echo good >CamelCase &&
1135 echo bad >camelcase &&
1136 test "$(cat CamelCase)" != good
1139 test_lazy_prereq UTF8_NFD_TO_NFC '
1140 # check whether FS converts nfd unicode to nfc
1141 auml="$(printf "\303\244")"
1142 aumlcdiar="$(printf "\141\314\210")"
1143 >"$auml" &&
1144 case "$(echo *)" in
1145 "$aumlcdiar")
1146 true ;;
1148 false ;;
1149 esac
1152 test_lazy_prereq AUTOIDENT '
1153 sane_unset GIT_AUTHOR_NAME &&
1154 sane_unset GIT_AUTHOR_EMAIL &&
1155 git var GIT_AUTHOR_IDENT
1158 test_lazy_prereq EXPENSIVE '
1159 test -n "$TESTLIB_TEST_LONG"
1162 test_lazy_prereq USR_BIN_TIME '
1163 test -x /usr/bin/time
1166 test_lazy_prereq NOT_ROOT '
1167 uid="$(id -u)" &&
1168 test "$uid" != 0
1171 # SANITY is about "can you correctly predict what the filesystem would
1172 # do by only looking at the permission bits of the files and
1173 # directories?" A typical example of !SANITY is running the test
1174 # suite as root, where a test may expect "chmod -r file && cat file"
1175 # to fail because file is supposed to be unreadable after a successful
1176 # chmod. In an environment (i.e. combination of what filesystem is
1177 # being used and who is running the tests) that lacks SANITY, you may
1178 # be able to delete or create a file when the containing directory
1179 # doesn't have write permissions, or access a file even if the
1180 # containing directory doesn't have read or execute permissions.
1182 test_lazy_prereq SANITY '
1183 mkdir SANETESTD.1 SANETESTD.2 &&
1185 chmod +w SANETESTD.1 SANETESTD.2 &&
1186 >SANETESTD.1/x 2>SANETESTD.2/x &&
1187 chmod -w SANETESTD.1 &&
1188 chmod -r SANETESTD.1/x &&
1189 chmod -rx SANETESTD.2 ||
1190 error "bug in test sript: cannot prepare SANETESTD"
1192 ! test -r SANETESTD.1/x &&
1193 ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
1194 status=$?
1196 chmod +rwx SANETESTD.1 SANETESTD.2 &&
1197 rm -rf SANETESTD.1 SANETESTD.2 ||
1198 error "bug in test sript: cannot clean SANETESTD"
1199 return $status
1202 test_lazy_prereq CMDLINE_LIMIT 'run_with_limited_cmdline true'
1205 # End test_lib_main_init_generic
1209 # This function is guaranteed to always be called for every single test.
1210 # Only put things in this function that MUST be done per-test, function
1211 # definitions and sourcing other files generally DO NOT QUALIFY (there can
1212 # be exceptions).
1213 test_lib_main_init_specific() {
1214 # Begin test_lib_main_init_specific
1217 # original stdin is on 6, stdout on 5 and stderr on 7
1218 exec 5>&1 6<&0 7>&2
1220 test_lib_main_init_funcs
1222 if test -n "$HARNESS_ACTIVE"
1223 then
1224 if test "$verbose" = t || test -n "$verbose_only" && test -z "$verbose_log$TESTLIB_OVERRIDE"
1225 then
1226 printf 'Bail out! %s\n' \
1227 'verbose mode forbidden under TAP harness; use --verbose-log'
1228 exit 1
1232 test "${test_description}" != "" ||
1233 error "Test script did not set test_description."
1235 if test "$help" = "t"
1236 then
1237 printf '%s\n' "$(printf '%s\n' "$test_description")" |
1238 sed -n -e '
1240 :loop
1241 s/\([^ ]\)/\1/
1242 t rest
1244 b loop
1246 :rest
1249 exit 0
1252 if test "$verbose_log" = "t"
1253 then
1254 exec 3>>"$TESTLIB_TEST_TEE_OUTPUT_FILE" 4>&3
1255 elif test "$verbose" = "t"
1256 then
1257 exec 4>&2 3>&1
1258 else
1259 exec 4>/dev/null 3>/dev/null
1262 TESTLIB_EXIT_OK=
1263 trap '_die' EXIT
1264 trap 'exit $?' HUP INT QUIT ABRT PIPE TERM
1265 trap 'TESTLIB_EXIT_OK=t; exit 1' USR1
1267 # Test repository
1268 TRASH_DIRECTORY="trash directory.${0##*/}"
1269 TRASH_DIRECTORY="${TRASH_DIRECTORY%.sh}"
1270 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
1271 test -n "$root" && GIT_CEILING_DIRECTORIES="$root:$GIT_CEILING_DIRECTORIES"
1272 case "$TRASH_DIRECTORY" in
1273 /*) ;; # absolute path is good
1274 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
1275 esac
1276 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
1277 ! test -e "$TRASH_DIRECTORY" || {
1278 rm -rf "$TRASH_DIRECTORY" &&
1279 ! test -e "$TRASH_DIRECTORY" || {
1280 chmod -R u+w "$TRASH_DIRECTORY" &&
1281 rm -rf "$TRASH_DIRECTORY" &&
1282 ! test -e "$TRASH_DIRECTORY"
1284 } || {
1285 TESTLIB_EXIT_OK=t
1286 echo >&5 "FATAL: Cannot prepare test area"
1287 exit 1
1290 HOME="$TRASH_DIRECTORY"
1291 GNUPGHOME="$HOME/gnupg-home-not-used"
1292 export HOME GNUPGHOME
1294 if test -z "$TEST_NO_CREATE_REPO"
1295 then
1296 test_create_repo "$TRASH_DIRECTORY"
1297 else
1298 mkdir -p "$TRASH_DIRECTORY"
1300 # Use -P to resolve symlinks in our working directory so that the cwd
1301 # in subprocesses like tg equals our $PWD (for pathname comparisons).
1302 cd -P "$TRASH_DIRECTORY" || exit 1
1304 this_test=${0##*/}
1305 this_test=${this_test%%-*}
1306 test_wrote_plan_count=
1307 test_last_subtest_ok=1
1308 if match_pattern_list "$this_test" $TESTLIB_SKIP_TESTS
1309 then
1310 say_color info >&3 "skipping test $this_test altogether"
1311 skip_all="skip all tests in $this_test"
1312 test_done
1316 # End test_lib_main_init_specific
1321 # THIS SHOULD ALWAYS BE THE LAST FUNCTION DEFINED IN THIS FILE
1323 # Any client that sources this file should immediately execute this function
1324 # afterwards with the command line arguments
1326 # THERE SHOULD NOT BE ANY DIRECTLY EXECUTED LINES OF CODE IN THIS FILE
1328 test_lib_main_init() {
1330 test_lib_main_init_generic "$@"
1331 test_lib_main_init_specific "$@"