t*.sh: remove GIT_CEILING_DIRECTORIES settings
[topgit/pro.git] / t / test-lib-main.sh
blob22a1a9b91c1f274b60e7ad4a636b5f23716fde36
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 if test "$verbose_log" = "t"
349 then
350 exec 3>>"$TESTLIB_TEST_TEE_OUTPUT_FILE" 4>&3
351 else
352 exec 4>&2 3>&1
354 # Emit a delimiting blank line when going from
355 # non-verbose to verbose. Within verbose mode the
356 # delimiter is printed by test_expect_*. The choice
357 # of the initial $last_verbose is such that before
358 # test 1, we do not print it.
359 test -z "$last_verbose" && echo >&3 ""
360 verbose=t
361 else
362 exec 4>/dev/null 3>/dev/null
363 verbose=
365 last_verbose=$verbose
368 want_trace() {
369 test "$trace" = t && test "$verbose" = t
372 # This is a separate function because some tests use
373 # "return" to end a test_expect_success block early
374 # (and we want to make sure we run any cleanup like
375 # "set +x").
376 test_eval_inner_() (
377 # Do not add anything extra (including LF) after '$*'
378 eval "
379 set -e
380 test_subshell_active_=t
381 ! want_trace || ! set -x && ! :
385 # Same thing as test_eval_inner_ but without the subshell
386 test_eval_inner_no_subshell_() {
387 # Do not add anything extra (including LF) after '$*'
388 eval "
389 ! want_trace || ! set -x && ! :
393 test_eval_ss_() {
394 # We run this block with stderr redirected to avoid extra cruft
395 # during a "-x" trace. Once in "set -x" mode, we cannot prevent
396 # the shell from printing the "set +x" to turn it off (nor the saving
397 # of $? before that). But we can make sure that the output goes to
398 # /dev/null.
400 # The test itself is run with stderr put back to &4 (so either to
401 # /dev/null, or to the original stderr if --verbose was used).
403 test_eval_ss_="$1"
404 shift
405 if test "${test_eval_ss_:-0}" = "0"
406 then
407 test_eval_inner_no_subshell_ "$@" </dev/null >&3 2>&4
408 else
409 test_eval_inner_ "$@" </dev/null >&3 2>&4
411 test_eval_ret_=$?
412 if want_trace
413 then
414 set +x
415 if test "$test_eval_ret_" != 0
416 then
417 say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
420 } 2>/dev/null
421 return $test_eval_ret_
424 # Calls the real test_eval_ss_ with !"$TESTLIB_TEST_NO_SUBSHELL" as first arg
425 test_eval_() {
426 if test -n "$TESTLIB_TEST_NO_SUBSHELL"
427 then
428 test_eval_ss_ "0" "$@"
429 else
430 test_eval_ss_ "1" "$@"
434 # If "$1" = "-" read the script from stdin but ONLY if stdin is NOT a tty
435 # Store the test script in test_script_
436 test_get_() {
437 if test "x$1" = "x-"
438 then
439 ! test -t 0 || error "test script is '-' but STDIN is a tty"
440 test_script_="$(cat)"
441 test -n "$test_script_" || error "test script is '-' but STDIN is empty"
442 test_script_="$LF$test_script_$LF"
443 else
444 test_script_="$1"
448 fail_() {
449 return ${1:-1}
452 test_run_() {
453 test_cleanup=:
454 test_subshell_active_=
455 expecting_failure=$2
456 linting=
458 if test "${TESTLIB_TEST_CHAIN_LINT:-1}" != 0; then
459 # turn off tracing for this test-eval, as it simply creates
460 # confusing noise in the "-x" output
461 trace_tmp=$trace
462 trace=
463 linting=t
464 # 117 is magic because it is unlikely to match the exit
465 # code of other programs
466 test_eval_ss_ "1" "fail_ 117 && $1${LF}fail_ \$?"
467 if test "$?" != 117; then
468 error "bug in the test script: broken &&-chain: $1"
470 trace=$trace_tmp
471 linting=
474 test_eval_ "$1"
475 eval_ret=$?
477 if test -z "$immediate" || test $eval_ret = 0 ||
478 test -n "$expecting_failure" && test "${test_cleanup:-:}" != ":"
479 then
480 test_eval_ "$test_cleanup"
482 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
483 then
484 echo ""
486 return "$eval_ret"
489 test_start_() {
490 test_count=$(($test_count+1))
491 maybe_setup_verbose
494 test_finish_() {
495 echo >&3 ""
496 maybe_teardown_verbose
499 test_skip() {
500 to_skip=
501 skipped_reason=
502 if match_pattern_list $this_test.$test_count $TESTLIB_SKIP_TESTS
503 then
504 to_skip=t
505 skipped_reason="TESTLIB_SKIP_TESTS"
507 if test -z "$to_skip" && test -n "$test_prereq" &&
508 ! test_have_prereq "$test_prereq"
509 then
510 to_skip=t
512 of_prereq=
513 if test "$missing_prereq" != "$test_prereq"
514 then
515 of_prereq=" of $test_prereq"
517 skipped_reason="missing $missing_prereq${of_prereq}"
519 if test -z "$to_skip" && test -n "$run_list" &&
520 ! match_test_selector_list '--run' $test_count "$run_list"
521 then
522 to_skip=t
523 skipped_reason="--run"
526 case "$to_skip" in
528 say_color skip >&3 "skipping test: $@"
529 say_color_tap skip "ok $test_count # skip $1 ($skipped_reason)"
530 : true
533 false
535 esac
538 # stub; runs at end of each successful test
539 test_at_end_hook_() {
543 test_done() {
544 TESTLIB_EXIT_OK=t
546 if test -z "$HARNESS_ACTIVE"
547 then
548 test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
549 mkdir -p "$test_results_dir"
550 base=${0##*/}
551 test_results_path="$test_results_dir/${base%.sh}.counts"
553 cat >"$test_results_path" <<-EOF
554 total $test_count
555 success $test_success
556 fixed $test_fixed
557 broken $test_broken
558 failed $test_failure
563 if test "$test_fixed" != 0
564 then
565 say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
567 if test "$test_broken" != 0
568 then
569 say_color warn "# still have $test_broken known breakage(s)"
571 if test "$test_broken" != 0 || test "$test_fixed" != 0
572 then
573 test_remaining=$(( $test_count - $test_broken - $test_fixed ))
574 msg="remaining $test_remaining test(s)"
575 else
576 test_remaining=$test_count
577 msg="$test_count test(s)"
579 case "$test_failure" in
581 # Maybe print SKIP message
582 if test -n "$skip_all" && test $test_count -gt 0
583 then
584 error "Can't use skip_all after running some tests"
586 test -z "$skip_all" || skip_all=" # SKIP $skip_all"
588 if test $test_external_has_tap -eq 0
589 then
590 if test $test_remaining -gt 0
591 then
592 say_color pass "# passed all $msg"
594 test -n "$test_wrote_plan_count" || say_tap "1..$test_count$skip_all"
596 if test -n "$test_wrote_plan_count" && test "$test_wrote_plan_count" -ne "$test_count"
597 then
598 say_color error "# plan count of $test_wrote_plan_count does not match run count of $test_count"
599 exit 1
602 test -n "$remove_trash" &&
603 test -d "$remove_trash" &&
604 cd "${remove_trash%/*}" &&
605 test_done_td_="${remove_trash##*/}" &&
606 test -e "$test_done_td_" &&
607 rm -rf "$test_done_td_" &&
609 ! test -e "$test_done_td_" || {
610 chmod -R u+w "$test_done_td_" &&
611 rm -rf "$test_done_td_"
615 test_at_end_hook_
617 exit 0 ;;
620 if test $test_external_has_tap -eq 0
621 then
622 say_color error "# failed $test_failure among $msg"
623 test -n "$test_wrote_plan_count" || say_tap "1..$test_count"
625 if test -n "$test_wrote_plan_count" && test "$test_wrote_plan_count" -ne "$test_count"
626 then
627 say_color error "# plan count of $test_wrote_plan_count does not match run count of $test_count"
630 exit 1 ;;
632 esac
635 test_plan() {
636 test -n "$1" && test "z$1" = "z${1#*[!0-9]}" || fatal "invalid test_plan argument: $1"
637 test "$1" -eq 0 || test -z "$2" || fatal "invalid test_plan arguments: $*"
638 if test "$1" -eq 0; then
639 skip_all="${2:-skip all tests in $this_test}"
640 test_done
642 test $test_external_has_tap -ne 0 || say_tap "1..$1"
643 test_wrote_plan_count="$1"
646 # Provide an implementation of the 'yes' utility
647 yes() {
648 if test $# = 0
649 then
651 else
652 y="$*"
656 while test $i -lt 99
658 echo "$y"
659 i=$(($i+1))
660 done
663 run_with_limited_cmdline() {
664 (ulimit -s 128 && "$@")
669 ## Note that the following functions have bodies that are NOT indented
670 ## to assist with readability
674 test_lib_main_init_tee() {
675 # Begin test_lib_main_init_tee
678 # if --tee was passed, write the output not only to the terminal, but
679 # additionally to the file test-results/$BASENAME.out, too.
680 case "$TESTLIB_TEST_TEE_STARTED, $* " in
681 done,*)
682 # do not redirect again
684 *' --tee '*|*' --verbose-log '*)
685 mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
686 BASE="$TEST_OUTPUT_DIRECTORY/test-results/${0##*/}"
687 BASE="${BASE%.sh}"
689 # Make this filename available to the sub-process in case it is using
690 # --verbose-log.
691 TESTLIB_TEST_TEE_OUTPUT_FILE=$BASE.out
692 export TESTLIB_TEST_TEE_OUTPUT_FILE
694 # Truncate before calling "tee -a" to get rid of the results
695 # from any previous runs.
696 >"$TESTLIB_TEST_TEE_OUTPUT_FILE"
697 >"$BASE.exit"
699 (ec=0; TESTLIB_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1 || ec=$?
700 echo $ec >"$BASE.exit") | tee -a "$TESTLIB_TEST_TEE_OUTPUT_FILE"
701 exitcode="$(cat "$BASE.exit" 2>/dev/null)" || :
702 exit ${exitcode:-1}
704 esac
707 # End test_lib_main_init_tee
711 test_lib_main_init_funcs() {
712 # Begin test_lib_main_init_funcs
715 [ -z "$test_lib_main_init_funcs_done" ] || return 0
717 if test -n "$color"
718 then
719 say_color() {
720 test -z "$1" && test -n "$quiet" && return
721 eval "say_color_color=\$say_color_$1"
722 shift
723 _sfc=
724 _sms="$*"
725 if test -n "$HARNESS_ACTIVE"
726 then
727 case "$_sms" in '#'*)
728 _sfc='#'
729 _sms="${_sms#?}"
730 esac
732 printf '%s\n' "$_sfc$say_color_color$_sms$say_color_reset"
734 else
735 say_color() {
736 test -z "$1" && test -n "$quiet" && return
737 shift
738 printf '%s\n' "$*"
742 # Just like say_color except if HARNESS_ACTIVE it's ALWAYS output and WITHOUT color
743 say_color_tap() {
744 if test -n "$HARNESS_ACTIVE"
745 then
746 shift
747 printf '%s\n' "$*"
748 else
749 say_color "$@"
754 # Fix some commands on Windows
755 case "${UNAME_S:=$(uname -s)}" in
756 *MINGW*)
757 # Windows has its own (incompatible) sort and find
758 sort() {
759 /usr/bin/sort "$@"
761 find() {
762 /usr/bin/find "$@"
764 sum() {
765 md5sum "$@"
767 # git sees Windows-style pwd
768 pwd() {
769 builtin pwd -W
772 esac
774 test_lib_main_init_funcs_done=1
777 # End test_lib_main_init_funcs
781 # This function is called with all the test args and must perform all
782 # initialization that involves variables and is not specific to "$0"
783 # or "$test_description" in any way. This function may only be called
784 # once per run of the entire test suite.
785 test_lib_main_init_generic() {
786 # Begin test_lib_main_init_generic
788 [ -n "$TESTLIB_DIRECTORY" ] || whats_the_dir -L -- "${TEST_DIRECTORY:-.}/test-lib.sh" TESTLIB_DIRECTORY
789 [ -f "$TESTLIB_DIRECTORY/test-lib.sh" ] && [ -f "$TESTLIB_DIRECTORY/test-lib-main.sh" ] &&
790 [ -f "$TESTLIB_DIRECTORY/test-lib-functions.sh" ] ||
791 fatal "error: invalid TESTLIB_DIRECOTRY: $TESTLIB_DIRECTORY"
792 export TESTLIB_DIRECTORY
794 ! [ -f "$TESTLIB_DIRECTORY/../TG-BUILD-SETTINGS" ] || . "$TESTLIB_DIRECTORY/../TG-BUILD-SETTINGS"
795 ! [ -f "$TESTLIB_DIRECTORY/TG-TEST-SETTINGS" ] || . "$TESTLIB_DIRECTORY/TG-TEST-SETTINGS"
797 : "${SHELL_PATH:=/bin/sh}"
798 : "${DIFF:=diff}"
799 : "${GIT_PATH:=$(cmd_path git)}"
800 : "${PERL_PATH:=$(cmd_path perl || :)}"
802 # Test the binaries we have just built. The tests are kept in
803 # t/ subdirectory and are run in 'trash directory' subdirectory.
804 if test -z "$TEST_DIRECTORY"
805 then
806 # We allow tests to override this, in case they want to run tests
807 # outside of t/, e.g. for running tests on the test library
808 # itself.
809 TEST_DIRECTORY="$TESTLIB_DIRECTORY"
810 else
811 # ensure that TEST_DIRECTORY is an absolute path so that it
812 # is valid even if the current working directory is changed
813 TEST_DIRECTORY="$(cd "$TEST_DIRECTORY" && pwd)" || exit 1
815 if test -z "$TEST_OUTPUT_DIRECTORY"
816 then
817 # Similarly, override this to store the test-results subdir
818 # elsewhere
819 TEST_OUTPUT_DIRECTORY="$TEST_DIRECTORY"
821 [ -d "$TESTLIB_DIRECTORY"/empty ] || {
822 mkdir "$TESTLIB_DIRECTORY/empty" || :
823 chmod a-w "$TESTLIB_DIRECTORY/empty" || :
824 test -d "$TESTLIB_DIRECTORY"/empty ||
825 fatal "error: could not make empty directory: '$TESTLIB_DIRECTORY/empty'"
827 EMPTY_DIRECTORY="$TESTLIB_DIRECTORY/empty"
828 export TEST_DIRECTORY TEST_OUTPUT_DIRECTORY EMPTY_DIRECTORY
829 GIT_CEILING_DIRECTORIES="$TESTLIB_DIRECTORY"
830 [ "$TESTLIB_DIRECTORY" = "$TEST_DIRECTORY" ] ||
831 GIT_CEILING_DIRECTORIES="$TEST_DIRECTORY:$GIT_CEILING_DIRECTORIES"
832 [ "$TESTLIB_DIRECTORY" = "$TEST_OUTPUT_DIRECTORY" ] ||
833 GIT_CEILING_DIRECTORIES="$TEST_OUTPUT_DIRECTORY:$GIT_CEILING_DIRECTORIES"
834 export GIT_CEILING_DIRECTORIES
836 ################################################################
837 # It appears that people try to run tests with missing perl or git...
838 git_version="$("$GIT_PATH" --version 2>&1)" ||
839 fatal 'error: you do not seem to have git available?'
840 case "$git_version" in [Gg][Ii][Tt]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn]\ [0-9]*);;*)
841 fatal "error: git --version returned bogus value: $git_version"
842 esac
843 #"$PERL_PATH" --version >/dev/null 2>&1 ||
844 # fatal 'error: you do not seem to have perl available?'
846 test_lib_main_init_tee "$@"
848 # For repeatability, reset the environment to known value.
849 # TERM is sanitized below, after saving color control sequences.
850 LANG=C
851 LC_ALL=C
852 PAGER=cat
853 TZ=UTC
854 export LANG LC_ALL PAGER TZ
855 EDITOR=:
856 # A call to "unset" with no arguments causes at least Solaris 10
857 # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
858 # deriving from the command substitution clustered with the other
859 # ones.
860 unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
861 my @env = keys %ENV;
862 my $ok = join("|", qw(
863 TRACE
864 DEBUG
865 USE_LOOKUP
866 TEST
867 .*_TEST
868 MINIMUM_VERSION
869 PATH
870 PROVE
871 UNZIP
872 PERF_
873 CURL_VERBOSE
874 TRACE_CURL
875 CEILING_DIRECTORIES
877 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
878 print join("\n", @vars);
880 unset XDG_CONFIG_HOME
881 unset GITPERLLIB
882 GIT_AUTHOR_NAME='Te s t'
883 GIT_AUTHOR_EMAIL=test@example.net
884 GIT_COMMITTER_NAME='Fra mewor k'
885 GIT_COMMITTER_EMAIL=framework@example.org
886 GIT_MERGE_VERBOSITY=5
887 GIT_MERGE_AUTOEDIT=no
888 GIT_TEMPLATE_DIR="$EMPTY_DIRECTORY"
889 GIT_CONFIG_NOSYSTEM=1
890 GIT_ATTR_NOSYSTEM=1
891 export PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
892 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
893 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
894 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
895 export EDITOR
897 # Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
898 GIT_TRACE_BARE=1
899 export GIT_TRACE_BARE
901 # Protect ourselves from common misconfiguration to export
902 # CDPATH into the environment
903 unset CDPATH
905 unset GREP_OPTIONS
906 unset UNZIP
908 case "$GIT_TRACE" in 1|2|[Tt][Rr][Uu][Ee])
909 GIT_TRACE=4
911 esac
913 # Convenience
915 # A regexp to match 5 and 40 hexdigits
916 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
917 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
919 # Zero SHA-1
920 _z40=0000000000000000000000000000000000000000
922 EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
923 EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
925 # Line feed
926 LF='
929 # UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
930 # when case-folding filenames
931 u200c="$(printf '\342\200\214')"
933 export _x05 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB
935 while test "$#" -ne 0
937 case "$1" in
938 -d|--d|--de|--deb|--debu|--debug)
939 debug=t; shift ;;
940 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
941 immediate=t; shift ;;
942 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests|\
943 --ex|--exp|--expe|--expen|--expens|--expensi|--expensiv|--expensive)
944 TESTLIB_TEST_LONG=t; export TESTLIB_TEST_LONG; shift ;;
946 shift; test "$#" -ne 0 || {
947 echo 'error: -r requires an argument' >&2;
948 exit 1;
950 run_list=$1; shift ;;
951 --run=*)
952 run_list=${1#--*=}; shift ;;
953 -h|--h|--he|--hel|--help)
954 help=t; shift ;;
955 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
956 verbose=t; shift ;;
957 --verbose-only=*)
958 verbose_only=${1#--*=}
959 shift ;;
960 -q|--q|--qu|--qui|--quie|--quiet)
961 quiet=t; shift ;;
962 --color)
963 color=t; shift ;;
964 --no-color)
965 color=; shift ;;
966 --tee)
967 shift ;; # was handled already
968 --root=*)
969 root=${1#--*=}
970 shift ;;
971 --chain-lint)
972 TESTLIB_TEST_CHAIN_LINT=1
973 shift ;;
974 --no-chain-lint)
975 TESTLIB_TEST_CHAIN_LINT=0
976 shift ;;
977 -x|--x|--xt|--xtr|--xtra|--xtrac|--xtrace)
978 trace=t
979 verbose=t
980 shift ;;
981 --verbose-log)
982 verbose_log=t
983 shift ;;
985 echo "error: unknown test option '$1'" >&2; exit 1 ;;
986 esac
987 done
989 test "x${color+set}" != "xset" &&
990 test "x$TERM" != "xdumb" && (
991 { test -n "$TESTLIB_FORCETTY" || test -t 1; } &&
992 tput bold >/dev/null 2>&1 &&
993 tput setaf 1 >/dev/null 2>&1 &&
994 tput sgr0 >/dev/null 2>&1
995 ) &&
996 color=t
997 if test -n "$color"
998 then
999 # Save the color control sequences now rather than run tput
1000 # each time say_color() is called. This is done for two
1001 # reasons:
1002 # * TERM will be changed to dumb
1003 # * HOME will be changed to a temporary directory and tput
1004 # might need to read ~/.terminfo from the original HOME
1005 # directory to get the control sequences
1006 getcmd say_color_error eval 'tput setaf 1' # red
1007 getcmd say_color_skip eval 'tput bold; tput setaf 5' # bold blue
1008 getcmd say_color_warn eval 'tput setaf 3' # brown/yellow
1009 getcmd say_color_pass eval 'tput setaf 2' # green
1010 getcmd say_color_info eval 'tput setaf 6' # cyan
1011 getcmd say_color_reset eval 'tput sgr0'
1012 say_color_="" # no formatting for normal text
1015 TERM=dumb
1016 export TERM
1018 # Send any "-x" output directly to stderr to avoid polluting tests
1019 # which capture stderr. We can do this unconditionally since it
1020 # has no effect if tracing isn't turned on.
1022 # Note that this sets up the trace fd as soon as we assign the variable, so it
1023 # must come after the creation of descriptor 4 above. Likewise, we must never
1024 # unset this, as it has the side effect of closing descriptor 4, which we
1025 # use to show verbose tests to the user.
1027 # Note also that we don't need or want to export it. The tracing is local to
1028 # this shell, and we would not want to influence any shells we exec.
1029 BASH_XTRACEFD=4
1031 test_failure=0
1032 test_count=0
1033 test_fixed=0
1034 test_broken=0
1035 test_success=0
1037 test_external_has_tap=0
1039 # The user-facing functions are loaded from a separate file
1040 . "$TESTLIB_DIRECTORY/test-lib-functions.sh"
1041 test_lib_functions_init
1043 last_verbose=t
1045 if [ -n "$TG_TEST_INSTALLED" ]; then
1046 [ -n "$(cmd_path tg || :)" ] ||
1047 fatal 'error: TG_TEST_INSTALLED set but no tg found in $PATH!'
1048 else
1049 tg_bin_dir="$(cd "$TESTLIB_DIRECTORY/../bin-wrappers" 2>/dev/null && pwd -P || :)"
1050 [ -x "$tg_bin_dir/tg" ] ||
1051 fatal 'error: no ../bin-wrappers/tg executable found!'
1052 PATH="$tg_bin_dir:$PATH"
1054 tg_version="$(tg --version)" ||
1055 fatal 'error: tg --version failed!'
1056 case "$tg_version" in [Tt][Oo][Pp][Gg][Ii][Tt]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn]\ [0-9]*);;*)
1057 fatal "error: tg --version returned bogus value: $tg_version"
1058 esac
1060 vcmp "$git_version" '>=' "$GIT_MINIMUM_VERSION" ||
1061 fatal "git version >= $GIT_MINIMUM_VERSION required but found \"$git_version\" instead"
1063 if test -z "$TESTLIB_TEST_CMP"
1064 then
1065 if test -n "$TESTLIB_TEST_CMP_USE_COPIED_CONTEXT"
1066 then
1067 TESTLIB_TEST_CMP="$DIFF -c"
1068 else
1069 TESTLIB_TEST_CMP="$DIFF -u"
1073 # Fix some commands on Windows
1074 case "${UNAME_S:=$(uname -s)}" in
1075 *MINGW*)
1076 # no POSIX permissions
1077 # backslashes in pathspec are converted to '/'
1078 # exec does not inherit the PID
1079 test_set_prereq MINGW
1080 test_set_prereq NATIVE_CRLF
1081 test_set_prereq SED_STRIPS_CR
1082 test_set_prereq GREP_STRIPS_CR
1083 TESTLIB_TEST_CMP=mingw_test_cmp
1085 *CYGWIN*)
1086 test_set_prereq POSIXPERM
1087 test_set_prereq EXECKEEPSPID
1088 test_set_prereq CYGWIN
1089 test_set_prereq SED_STRIPS_CR
1090 test_set_prereq GREP_STRIPS_CR
1093 test_set_prereq POSIXPERM
1094 test_set_prereq BSLASHPSPEC
1095 test_set_prereq EXECKEEPSPID
1097 esac
1099 ( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
1101 test_lib_main_init_funcs
1103 test_lazy_prereq PIPE '
1104 # test whether the filesystem supports FIFOs
1105 case "${UNAME_S:=$(uname -s)}" in
1106 CYGWIN*|MINGW*)
1107 false
1110 rm -f testfifo && mkfifo testfifo
1112 esac
1115 test_lazy_prereq SYMLINKS '
1116 # test whether the filesystem supports symbolic links
1117 ln -s x y && test -h y
1120 test_lazy_prereq FILEMODE '
1121 test "$(git config --bool core.filemode)" = true
1124 test_lazy_prereq CASE_INSENSITIVE_FS '
1125 echo good >CamelCase &&
1126 echo bad >camelcase &&
1127 test "$(cat CamelCase)" != good
1130 test_lazy_prereq UTF8_NFD_TO_NFC '
1131 # check whether FS converts nfd unicode to nfc
1132 auml="$(printf "\303\244")"
1133 aumlcdiar="$(printf "\141\314\210")"
1134 >"$auml" &&
1135 case "$(echo *)" in
1136 "$aumlcdiar")
1137 true ;;
1139 false ;;
1140 esac
1143 test_lazy_prereq AUTOIDENT '
1144 sane_unset GIT_AUTHOR_NAME &&
1145 sane_unset GIT_AUTHOR_EMAIL &&
1146 git var GIT_AUTHOR_IDENT
1149 test_lazy_prereq EXPENSIVE '
1150 test -n "$TESTLIB_TEST_LONG"
1153 test_lazy_prereq USR_BIN_TIME '
1154 test -x /usr/bin/time
1157 test_lazy_prereq NOT_ROOT '
1158 uid="$(id -u)" &&
1159 test "$uid" != 0
1162 # SANITY is about "can you correctly predict what the filesystem would
1163 # do by only looking at the permission bits of the files and
1164 # directories?" A typical example of !SANITY is running the test
1165 # suite as root, where a test may expect "chmod -r file && cat file"
1166 # to fail because file is supposed to be unreadable after a successful
1167 # chmod. In an environment (i.e. combination of what filesystem is
1168 # being used and who is running the tests) that lacks SANITY, you may
1169 # be able to delete or create a file when the containing directory
1170 # doesn't have write permissions, or access a file even if the
1171 # containing directory doesn't have read or execute permissions.
1173 test_lazy_prereq SANITY '
1174 mkdir SANETESTD.1 SANETESTD.2 &&
1176 chmod +w SANETESTD.1 SANETESTD.2 &&
1177 >SANETESTD.1/x 2>SANETESTD.2/x &&
1178 chmod -w SANETESTD.1 &&
1179 chmod -r SANETESTD.1/x &&
1180 chmod -rx SANETESTD.2 ||
1181 error "bug in test sript: cannot prepare SANETESTD"
1183 ! test -r SANETESTD.1/x &&
1184 ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
1185 status=$?
1187 chmod +rwx SANETESTD.1 SANETESTD.2 &&
1188 rm -rf SANETESTD.1 SANETESTD.2 ||
1189 error "bug in test sript: cannot clean SANETESTD"
1190 return $status
1193 test_lazy_prereq CMDLINE_LIMIT 'run_with_limited_cmdline true'
1196 # End test_lib_main_init_generic
1200 # This function is guaranteed to always be called for every single test.
1201 # Only put things in this function that MUST be done per-test, function
1202 # definitions and sourcing other files generally DO NOT QUALIFY (there can
1203 # be exceptions).
1204 test_lib_main_init_specific() {
1205 # Begin test_lib_main_init_specific
1208 # original stdin is on 6, stdout on 5 and stderr on 7
1209 exec 5>&1 6<&0 7>&2
1211 test_lib_main_init_funcs
1213 if test -n "$HARNESS_ACTIVE"
1214 then
1215 if test "$verbose" = t || test -n "$verbose_only" && test -z "$verbose_log$TESTLIB_OVERRIDE"
1216 then
1217 printf 'Bail out! %s\n' \
1218 'verbose mode forbidden under TAP harness; use --verbose-log'
1219 exit 1
1223 test "${test_description}" != "" ||
1224 error "Test script did not set test_description."
1226 if test "$help" = "t"
1227 then
1228 printf '%s\n' "$(printf '%s\n' "$test_description")" |
1229 sed -n -e '
1231 :loop
1232 s/\([^ ]\)/\1/
1233 t rest
1235 b loop
1237 :rest
1240 exit 0
1243 if test "$verbose_log" = "t"
1244 then
1245 exec 3>>"$TESTLIB_TEST_TEE_OUTPUT_FILE" 4>&3
1246 elif test "$verbose" = "t"
1247 then
1248 exec 4>&2 3>&1
1249 else
1250 exec 4>/dev/null 3>/dev/null
1253 TESTLIB_EXIT_OK=
1254 trap 'die' EXIT
1255 trap 'exit $?' HUP INT QUIT ABRT PIPE TERM
1256 trap 'TESTLIB_EXIT_OK=t; exit 1' USR1
1258 # Test repository
1259 TRASH_DIRECTORY="trash directory.${0##*/}"
1260 TRASH_DIRECTORY="${TRASH_DIRECTORY%.sh}"
1261 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
1262 test -n "$root" && GIT_CEILING_DIRECTORIES="$root:$GIT_CEILING_DIRECTORIES"
1263 case "$TRASH_DIRECTORY" in
1264 /*) ;; # absolute path is good
1265 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
1266 esac
1267 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
1268 ! test -e "$TRASH_DIRECTORY" || {
1269 rm -rf "$TRASH_DIRECTORY" &&
1270 ! test -e "$TRASH_DIRECTORY" || {
1271 chmod -R u+w "$TRASH_DIRECTORY" &&
1272 rm -rf "$TRASH_DIRECTORY" &&
1273 ! test -e "$TRASH_DIRECTORY"
1275 } || {
1276 TESTLIB_EXIT_OK=t
1277 echo >&5 "FATAL: Cannot prepare test area"
1278 exit 1
1281 HOME="$TRASH_DIRECTORY"
1282 GNUPGHOME="$HOME/gnupg-home-not-used"
1283 export HOME GNUPGHOME
1285 if test -z "$TEST_NO_CREATE_REPO"
1286 then
1287 test_create_repo "$TRASH_DIRECTORY"
1288 else
1289 mkdir -p "$TRASH_DIRECTORY"
1291 # Use -P to resolve symlinks in our working directory so that the cwd
1292 # in subprocesses like tg equals our $PWD (for pathname comparisons).
1293 cd -P "$TRASH_DIRECTORY" || exit 1
1295 this_test=${0##*/}
1296 this_test=${this_test%%-*}
1297 test_wrote_plan_count=
1298 test_last_subtest_ok=1
1299 if match_pattern_list "$this_test" $TESTLIB_SKIP_TESTS
1300 then
1301 say_color info >&3 "skipping test $this_test altogether"
1302 skip_all="skip all tests in $this_test"
1303 test_done
1307 # End test_lib_main_init_specific
1312 # THIS SHOULD ALWAYS BE THE LAST FUNCTION DEFINED IN THIS FILE
1314 # Any client that sources this file should immediately execute this function
1315 # afterwards with the command line arguments
1317 # THERE SHOULD NOT BE ANY DIRECTLY EXECUTED LINES OF CODE IN THIS FILE
1319 test_lib_main_init() {
1321 test_lib_main_init_generic "$@"
1322 test_lib_main_init_specific "$@"