test-lib-main.sh: enable line numbers when using bash
[topgit/pro.git] / t / test-lib-main.sh
blobb6e4f0adfe5471b5309138ed8a2787295f909694
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_lno() {
142 : "${callerlno:=$1}"
143 shift
144 say_color error "${LF}error: $*" >&7
145 printf '%s\n' "Bail out! ${0##*/}:${callerlno:+$callerlno:} error: $*" >&5
146 TESTLIB_EXIT_OK=t
147 [ -z "$TESTLIB_TEST_PARENT_INT_ON_ERROR" ] || {
148 perl -e "kill(-2, getpgrp($TESTLIB_TEST_PARENT_INT_ON_ERROR), getpgrp($PPID))" || :
149 kill -INT $TESTLIB_TEST_PARENT_INT_ON_ERROR $PPID || :
151 kill -USR1 $$ || :
152 exit 1
154 error() {
155 error_lno "" "$@"
157 alias error='error_lno "$LINENO"' >/dev/null 2>&1 || :
159 say() {
160 say_color info "$@"
163 say_tap() {
164 say_color_tap info "$@"
167 _die() {
168 code=$?
169 if test -n "$TESTLIB_EXIT_OK"
170 then
171 exit $code
172 else
173 echo >&5 "FATAL: Unexpected exit with code $code"
174 exit 1
177 die() { _die "$@"; }
179 # You are not expected to call test_ok_ and test_failure_ directly, use
180 # the test_expect_* functions instead.
182 test_ok_() {
183 test_success=$(($test_success + 1))
184 say_color_tap "" "ok $test_count - $@"
187 test_failure_() {
188 test_failure=$(($test_failure + 1))
189 tlno="$1"
190 shift
191 say_color_tap error "not ok $test_count - $1"
192 shift
193 printf '%s\n' "$(printf '%s\n' "failed: ${0##*/}${tlno:+:$tlno}$LF$*")" |
194 sed -n -e '
196 :loop
197 s/\([^ ]\)/\1/
198 t first
199 b continue
200 :first
203 b rest
204 :continue
206 b loop
208 :rest
209 s/^/# /
211 $ i\
214 test "$immediate" = "" || { TESTLIB_EXIT_OK=t; exit 1; }
217 test_known_broken_ok_() {
218 test_fixed=$(($test_fixed + 1))
219 say_color_tap warn "ok $test_count - $@ # TODO known breakage vanished"
222 test_known_broken_failure_() {
223 test_broken=$(($test_broken + 1))
224 say_color_tap warn "not ok $test_count - $@ # TODO known breakage"
227 test_possibly_broken_ok_() {
228 test_success=$(($test_success + 1))
229 say_color_tap "" "ok $test_count - $@"
232 test_possibly_broken_failure_() {
233 test_broken=$(($test_broken + 1))
234 say_color_tap warn "not ok $test_count - $@ # TODO tolerated breakage"
237 test_debug() {
238 test "$debug" = "" || test $# -eq 0 || test -z "$*" || { "$@"; } >&7 2>&1
241 match_pattern_list() {
242 arg="$1"
243 shift
244 test -z "$*" && return 1
245 for pattern_
247 case "$arg" in
248 $pattern_)
249 return 0
250 esac
251 done
252 return 1
255 match_test_selector_list() {
256 title="$1"
257 shift
258 arg="$1"
259 shift
260 test -z "$1" && return 0
262 # Both commas and whitespace are accepted as separators.
263 OLDIFS=$IFS
264 IFS=' ,'
265 set -- $1
266 IFS=$OLDIFS
268 # If the first selector is negative we include by default.
269 include=
270 case "$1" in
271 !*) include=t ;;
272 esac
274 for selector
276 orig_selector=$selector
278 positive=t
279 case "$selector" in
281 positive=
282 selector=${selector##?}
284 esac
286 test -z "$selector" && continue
288 case "$selector" in
289 *-*)
290 if x_="${selector%%-*}" && test "z$x_" != "z${x_#*[!0-9]}"
291 then
292 echo "error: $title: invalid non-numeric in range" \
293 "start: '$orig_selector'" >&2
294 exit 1
296 if x_="${selector#*-}" && test "z$x_" != "z${x_#*[!0-9]}"
297 then
298 echo "error: $title: invalid non-numeric in range" \
299 "end: '$orig_selector'" >&2
300 exit 1
302 unset x_
305 if test "z$selector" != "z${selector#*[!0-9]}"
306 then
307 echo "error: $title: invalid non-numeric in test" \
308 "selector: '$orig_selector'" >&2
309 exit 1
311 esac
313 # Short cut for "obvious" cases
314 test -z "$include" && test -z "$positive" && continue
315 test -n "$include" && test -n "$positive" && continue
317 case "$selector" in
319 if test $arg -le ${selector#-}
320 then
321 include=$positive
325 if test $arg -ge ${selector%-}
326 then
327 include=$positive
330 *-*)
331 if test ${selector%%-*} -le $arg \
332 && test $arg -le ${selector#*-}
333 then
334 include=$positive
338 if test $arg -eq $selector
339 then
340 include=$positive
343 esac
344 done
346 test -n "$include"
349 maybe_teardown_verbose() {
350 test -z "$verbose_only" && return
351 exec 4>/dev/null 3>/dev/null
352 verbose=
355 maybe_setup_verbose() {
356 test -z "$verbose_only" && return
357 if match_pattern_list $test_count $verbose_only
358 then
359 if test "$verbose_log" = "t"
360 then
361 exec 3>>"$TESTLIB_TEST_TEE_OUTPUT_FILE" 4>&3
362 else
363 exec 4>&2 3>&1
365 # Emit a delimiting blank line when going from
366 # non-verbose to verbose. Within verbose mode the
367 # delimiter is printed by test_expect_*. The choice
368 # of the initial $last_verbose is such that before
369 # test 1, we do not print it.
370 test -z "$last_verbose" && echo >&3 ""
371 verbose=t
372 else
373 exec 4>/dev/null 3>/dev/null
374 verbose=
376 last_verbose=$verbose
379 want_trace() {
380 test "$trace" = t && test "$verbose" = t
383 # This is a separate function because some tests use
384 # "return" to end a test_expect_success block early
385 # (and we want to make sure we run any cleanup like
386 # "set +x").
387 test_eval_inner_() (
388 # Do not add anything extra (including LF) after '$*'
389 eval "
390 set -e
391 test_subshell_active_=t
392 ! want_trace || ! set -x && ! :
396 # Same thing as test_eval_inner_ but without the subshell
397 test_eval_inner_no_subshell_() {
398 # Do not add anything extra (including LF) after '$*'
399 eval "
400 ! want_trace || ! set -x && ! :
404 test_eval_ss_() {
405 # We run this block with stderr redirected to avoid extra cruft
406 # during a "-x" trace. Once in "set -x" mode, we cannot prevent
407 # the shell from printing the "set +x" to turn it off (nor the saving
408 # of $? before that). But we can make sure that the output goes to
409 # /dev/null.
411 # The test itself is run with stderr put back to &4 (so either to
412 # /dev/null, or to the original stderr if --verbose was used).
414 test_eval_ss_="$1"
415 shift
416 if test "${test_eval_ss_:-0}" = "0"
417 then
418 test_eval_inner_no_subshell_ "$@" </dev/null >&3 2>&4
419 else
420 test_eval_inner_ "$@" </dev/null >&3 2>&4
422 test_eval_ret_=$?
423 if want_trace
424 then
425 set +x
426 if test "$test_eval_ret_" != 0
427 then
428 say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
431 } 2>/dev/null
432 return $test_eval_ret_
435 # Calls the real test_eval_ss_ with !"$TESTLIB_TEST_NO_SUBSHELL" as first arg
436 test_eval_() {
437 if test -n "$TESTLIB_TEST_NO_SUBSHELL"
438 then
439 test_eval_ss_ "0" "$@"
440 else
441 test_eval_ss_ "1" "$@"
445 # If "$1" = "-" read the script from stdin but ONLY if stdin is NOT a tty
446 # Store the test script in test_script_
447 test_get_() {
448 if test "x$1" = "x-"
449 then
450 ! test -t 0 || error "test script is '-' but STDIN is a tty"
451 test_script_="$(cat)"
452 test -n "$test_script_" || error "test script is '-' but STDIN is empty"
453 test_script_="$LF$test_script_$LF"
454 else
455 test_script_="$1"
459 fail_() {
460 test z"$2" = "z" || set +e
461 return ${1:-1}
464 test_run_() {
465 test_cleanup=:
466 test_subshell_active_=
467 expecting_failure=$2
468 linting=
470 if test "${TESTLIB_TEST_CHAIN_LINT:-1}" != 0; then
471 # turn off tracing for this test-eval, as it simply creates
472 # confusing noise in the "-x" output
473 trace_tmp=$trace
474 trace=
475 linting=t
476 # 117 is magic because it is unlikely to match the exit
477 # code of other programs
478 test_eval_ss_ "1" "fail_ 117 && $1${LF}fail_ \$? 1"
479 if test "$?" != 117; then
480 error "bug in the test script: broken &&-chain: $1"
482 trace=$trace_tmp
483 linting=
486 test_eval_ "$1"
487 eval_ret=$?
489 if test -z "$immediate" || test $eval_ret = 0 ||
490 test -n "$expecting_failure" && test "${test_cleanup:-:}" != ":"
491 then
492 test_eval_ "$test_cleanup"
494 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
495 then
496 echo ""
498 return "$eval_ret"
501 test_start_() {
502 test_count=$(($test_count+1))
503 maybe_setup_verbose
506 test_finish_() {
507 echo >&3 ""
508 maybe_teardown_verbose
511 test_skip() {
512 to_skip=
513 skipped_reason=
514 if match_pattern_list $this_test.$test_count $TESTLIB_SKIP_TESTS
515 then
516 to_skip=t
517 skipped_reason="TESTLIB_SKIP_TESTS"
519 if test -z "$to_skip" && test -n "$test_prereq" &&
520 ! test_have_prereq "$test_prereq"
521 then
522 to_skip=t
524 of_prereq=
525 if test "$missing_prereq" != "$test_prereq"
526 then
527 of_prereq=" of $test_prereq"
529 skipped_reason="missing $missing_prereq${of_prereq}"
531 if test -z "$to_skip" && test -n "$run_list" &&
532 ! match_test_selector_list '--run' $test_count "$run_list"
533 then
534 to_skip=t
535 skipped_reason="--run"
538 case "$to_skip" in
540 say_color skip >&3 "skipping test: $@"
541 say_color_tap skip "ok $test_count # skip $1 ($skipped_reason)"
542 : true
545 false
547 esac
550 # stub; runs at end of each successful test
551 test_at_end_hook_() {
555 test_done() {
556 TESTLIB_EXIT_OK=t
558 if test -z "$HARNESS_ACTIVE"
559 then
560 test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
561 mkdir -p "$test_results_dir"
562 base=${0##*/}
563 test_results_path="$test_results_dir/${base%.sh}.counts"
565 cat >"$test_results_path" <<-EOF
566 total $test_count
567 success $test_success
568 fixed $test_fixed
569 broken $test_broken
570 failed $test_failure
575 if test "$test_fixed" != 0
576 then
577 say_color error "# $this_test $test_fixed known breakage(s) vanished; please update test(s)"
579 if test "$test_broken" != 0
580 then
581 say_color warn "# $this_test still have $test_broken known breakage(s)"
583 if test "$test_broken" != 0 || test "$test_fixed" != 0
584 then
585 test_remaining=$(( $test_count - $test_broken - $test_fixed ))
586 msg="remaining $test_remaining test(s)"
587 else
588 test_remaining=$test_count
589 msg="$test_count test(s)"
591 case "$test_failure" in
593 # Maybe print SKIP message
594 if test -n "$skip_all" && test $test_count -gt 0
595 then
596 error "Can't use skip_all after running some tests"
598 test -z "$skip_all" || skip_all=" # SKIP $skip_all"
600 if test $test_external_has_tap -eq 0
601 then
602 if test $test_remaining -gt 0
603 then
604 say_color pass "# $this_test passed all $msg"
606 test -n "$test_wrote_plan_count" || say_tap "1..$test_count$skip_all"
608 if test -n "$test_wrote_plan_count" && test "$test_wrote_plan_count" -ne "$test_count"
609 then
610 say_color error "# $this_test plan count of $test_wrote_plan_count does not match run count of $test_count"
611 exit 1
614 test -n "$remove_trash" &&
615 test -d "$remove_trash" &&
616 cd "${remove_trash%/*}" &&
617 test_done_td_="${remove_trash##*/}" &&
618 test -e "$test_done_td_" &&
619 rm -rf "$test_done_td_" &&
621 ! test -e "$test_done_td_" || {
622 chmod -R u+w "$test_done_td_" &&
623 rm -rf "$test_done_td_"
627 test_at_end_hook_
629 exit 0 ;;
632 if test $test_external_has_tap -eq 0
633 then
634 say_color error "# $this_test failed $test_failure among $msg"
635 test -n "$test_wrote_plan_count" || say_tap "1..$test_count"
637 if test -n "$test_wrote_plan_count" && test "$test_wrote_plan_count" -ne "$test_count"
638 then
639 say_color error "# $this_test plan count of $test_wrote_plan_count does not match run count of $test_count"
642 test -z "$HARNESS_ACTIVE" || exit 0
643 exit 1 ;;
645 esac
648 test_plan() {
649 test -n "$1" && test "z$1" = "z${1#*[!0-9]}" || fatal "invalid test_plan argument: $1"
650 test "$1" -eq 0 || test -z "$2" || fatal "invalid test_plan arguments: $*"
651 if test "$1" -eq 0; then
652 skip_all="${2:-skip all tests in $this_test}"
653 test_done
655 test $test_external_has_tap -ne 0 || say_tap "1..$1"
656 test_wrote_plan_count="$1"
659 # Provide an implementation of the 'yes' utility
660 yes() {
661 if test $# = 0
662 then
664 else
665 y="$*"
669 while test $i -lt 99
671 echo "$y"
672 i=$(($i+1))
673 done
676 run_with_limited_cmdline() {
677 (ulimit -s 128 && "$@")
682 ## Note that the following functions have bodies that are NOT indented
683 ## to assist with readability
687 test_lib_main_init_tee() {
688 # Begin test_lib_main_init_tee
691 # if --tee was passed, write the output not only to the terminal, but
692 # additionally to the file test-results/$BASENAME.out, too.
693 case "$TESTLIB_TEST_TEE_STARTED, $* " in
694 done,*)
695 # do not redirect again
697 *' --tee '*|*' --verbose-log '*)
698 mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
699 BASE="$TEST_OUTPUT_DIRECTORY/test-results/${0##*/}"
700 BASE="${BASE%.sh}"
702 # Make this filename available to the sub-process in case it is using
703 # --verbose-log.
704 TESTLIB_TEST_TEE_OUTPUT_FILE=$BASE.out
705 export TESTLIB_TEST_TEE_OUTPUT_FILE
707 # Truncate before calling "tee -a" to get rid of the results
708 # from any previous runs.
709 >"$TESTLIB_TEST_TEE_OUTPUT_FILE"
710 >"$BASE.exit"
712 (ec=0; TESTLIB_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1 || ec=$?
713 echo $ec >"$BASE.exit") | tee -a "$TESTLIB_TEST_TEE_OUTPUT_FILE"
714 exitcode="$(cat "$BASE.exit" 2>/dev/null)" || :
715 exit ${exitcode:-1}
717 esac
720 # End test_lib_main_init_tee
724 test_lib_main_init_funcs() {
725 # Begin test_lib_main_init_funcs
728 [ -z "$test_lib_main_init_funcs_done" ] || return 0
730 if test -n "$color"
731 then
732 say_color() {
733 test -z "$1" && test -n "$quiet" && return
734 eval "say_color_color=\$say_color_$1"
735 shift
736 _sfc=
737 _sms="$*"
738 if test -n "$HARNESS_ACTIVE"
739 then
740 case "$_sms" in '#'*)
741 _sfc='#'
742 _sms="${_sms#?}"
743 esac
745 printf '%s\n' "$_sfc$say_color_color$_sms$say_color_reset"
747 else
748 say_color() {
749 test -z "$1" && test -n "$quiet" && return
750 shift
751 printf '%s\n' "$*"
755 # Just like say_color except if HARNESS_ACTIVE it's ALWAYS output and WITHOUT color
756 say_color_tap() {
757 if test -n "$HARNESS_ACTIVE"
758 then
759 shift
760 printf '%s\n' "$*"
761 else
762 say_color "$@"
767 # Fix some commands on Windows
768 case "${UNAME_S:=$(uname -s)}" in
769 *MINGW*)
770 # Windows has its own (incompatible) sort and find
771 sort() {
772 /usr/bin/sort "$@"
774 find() {
775 /usr/bin/find "$@"
777 sum() {
778 md5sum "$@"
780 # git sees Windows-style pwd
781 pwd() {
782 builtin pwd -W
785 esac
787 test_lib_main_init_funcs_done=1
790 # End test_lib_main_init_funcs
794 # This function is called with all the test args and must perform all
795 # initialization that involves variables and is not specific to "$0"
796 # or "$test_description" in any way. This function may only be called
797 # once per run of the entire test suite.
798 test_lib_main_init_generic() {
799 # Begin test_lib_main_init_generic
801 [ -n "$TESTLIB_DIRECTORY" ] || whats_the_dir -L -- "${TEST_DIRECTORY:-.}/test-lib.sh" TESTLIB_DIRECTORY
802 [ -f "$TESTLIB_DIRECTORY/test-lib.sh" ] && [ -f "$TESTLIB_DIRECTORY/test-lib-main.sh" ] &&
803 [ -f "$TESTLIB_DIRECTORY/test-lib-functions.sh" ] &&
804 [ -f "$TESTLIB_DIRECTORY/test-lib-functions-tg.sh" ] ||
805 fatal "error: invalid TESTLIB_DIRECTORY: $TESTLIB_DIRECTORY"
806 export TESTLIB_DIRECTORY
808 ! [ -f "$TESTLIB_DIRECTORY/../TG-BUILD-SETTINGS" ] || . "$TESTLIB_DIRECTORY/../TG-BUILD-SETTINGS"
809 ! [ -f "$TESTLIB_DIRECTORY/TG-TEST-SETTINGS" ] || . "$TESTLIB_DIRECTORY/TG-TEST-SETTINGS"
811 : "${SHELL_PATH:=/bin/sh}"
812 : "${DIFF:=diff}"
813 : "${GIT_PATH:=$(cmd_path git)}"
814 : "${PERL_PATH:=$(cmd_path perl || :)}"
816 # Test the binaries we have just built. The tests are kept in
817 # t/ subdirectory and are run in 'trash directory' subdirectory.
818 if test -z "$TEST_DIRECTORY"
819 then
820 # We allow tests to override this, in case they want to run tests
821 # outside of t/, e.g. for running tests on the test library
822 # itself.
823 TEST_DIRECTORY="$TESTLIB_DIRECTORY"
824 else
825 # ensure that TEST_DIRECTORY is an absolute path so that it
826 # is valid even if the current working directory is changed
827 TEST_DIRECTORY="$(cd "$TEST_DIRECTORY" && pwd)" || exit 1
829 if test -z "$TEST_HELPER_DIRECTORY" && test -d "$TEST_DIRECTORY/helper"
830 then
831 TEST_HELPER_DIRECTORY="$TEST_DIRECTORY/helper"
833 if test -z "$TEST_OUTPUT_DIRECTORY"
834 then
835 # Similarly, override this to store the test-results subdir
836 # elsewhere
837 TEST_OUTPUT_DIRECTORY="$TEST_DIRECTORY"
839 [ -d "$TESTLIB_DIRECTORY"/empty ] || {
840 mkdir "$TESTLIB_DIRECTORY/empty" || :
841 chmod a-w "$TESTLIB_DIRECTORY/empty" || :
842 test -d "$TESTLIB_DIRECTORY"/empty ||
843 fatal "error: could not make empty directory: '$TESTLIB_DIRECTORY/empty'"
845 EMPTY_DIRECTORY="$TESTLIB_DIRECTORY/empty"
846 export TEST_DIRECTORY TEST_HELPER_DIRECTORY TEST_OUTPUT_DIRECTORY EMPTY_DIRECTORY
847 GIT_CEILING_DIRECTORIES="$TESTLIB_DIRECTORY"
848 [ "$TESTLIB_DIRECTORY" = "$TEST_DIRECTORY" ] ||
849 GIT_CEILING_DIRECTORIES="$TEST_DIRECTORY:$GIT_CEILING_DIRECTORIES"
850 [ "$TESTLIB_DIRECTORY" = "$TEST_OUTPUT_DIRECTORY" ] ||
851 GIT_CEILING_DIRECTORIES="$TEST_OUTPUT_DIRECTORY:$GIT_CEILING_DIRECTORIES"
852 export GIT_CEILING_DIRECTORIES
854 ################################################################
855 # It appears that people try to run tests with missing perl or git...
856 git_version="$("$GIT_PATH" --version 2>&1)" ||
857 fatal 'error: you do not seem to have git available?'
858 case "$git_version" in [Gg][Ii][Tt]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn]\ [0-9]*);;*)
859 fatal "error: git --version returned bogus value: $git_version"
860 esac
861 #"$PERL_PATH" --version >/dev/null 2>&1 ||
862 # fatal 'error: you do not seem to have perl available?'
864 test_lib_main_init_tee "$@"
866 # For repeatability, reset the environment to known value.
867 # TERM is sanitized below, after saving color control sequences.
868 LANG=C
869 LC_ALL=C
870 PAGER=cat
871 TZ=UTC
872 export LANG LC_ALL PAGER TZ
873 EDITOR=:
874 # A call to "unset" with no arguments causes at least Solaris 10
875 # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
876 # deriving from the command substitution clustered with the other
877 # ones.
878 unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
879 my @env = keys %ENV;
880 my $ok = join("|", qw(
881 TRACE
882 DEBUG
883 USE_LOOKUP
884 TEST
885 .*_TEST
886 MINIMUM_VERSION
887 PATH
888 PROVE
889 UNZIP
890 PERF_
891 CURL_VERBOSE
892 TRACE_CURL
893 CEILING_DIRECTORIES
895 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
896 print join("\n", @vars);
898 unset XDG_CONFIG_HOME
899 unset GITPERLLIB
900 GIT_AUTHOR_NAME='Te s t'
901 GIT_AUTHOR_EMAIL=test@example.net
902 GIT_COMMITTER_NAME='Fra mewor k'
903 GIT_COMMITTER_EMAIL=framework@example.org
904 GIT_MERGE_VERBOSITY=5
905 GIT_MERGE_AUTOEDIT=no
906 GIT_TEMPLATE_DIR="$EMPTY_DIRECTORY"
907 GIT_CONFIG_NOSYSTEM=1
908 GIT_ATTR_NOSYSTEM=1
909 export PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
910 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
911 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
912 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
913 export EDITOR
915 # Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
916 GIT_TRACE_BARE=1
917 export GIT_TRACE_BARE
919 # Protect ourselves from common misconfiguration to export
920 # CDPATH into the environment
921 unset CDPATH
923 unset GREP_OPTIONS
924 unset UNZIP
926 case "$GIT_TRACE" in 1|2|[Tt][Rr][Uu][Ee])
927 GIT_TRACE=4
929 esac
931 # Convenience
933 # A regexp to match 5 and 40 hexdigits
934 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
935 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
937 # Zero SHA-1
938 _z40=0000000000000000000000000000000000000000
940 EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
941 EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
943 # Line feed
944 LF='
947 # UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
948 # when case-folding filenames
949 u200c="$(printf '\342\200\214')"
951 export _x05 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB
953 while test "$#" -ne 0
955 case "$1" in
956 -d|--d|--de|--deb|--debu|--debug)
957 debug=t; shift ;;
958 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
959 immediate=t; shift ;;
960 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests|\
961 --ex|--exp|--expe|--expen|--expens|--expensi|--expensiv|--expensive)
962 TESTLIB_TEST_LONG=t; export TESTLIB_TEST_LONG; shift ;;
964 shift; test "$#" -ne 0 || {
965 echo 'error: -r requires an argument' >&2;
966 exit 1;
968 run_list=$1; shift ;;
969 --run=*)
970 run_list=${1#--*=}; shift ;;
971 -h|--h|--he|--hel|--help)
972 help=t; shift ;;
973 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
974 verbose=t; shift ;;
975 --verbose-only=*)
976 verbose_only=${1#--*=}
977 shift ;;
978 -q|--q|--qu|--qui|--quie|--quiet)
979 quiet=t; shift ;;
980 --color)
981 color=t; shift ;;
982 --no-color)
983 color=; shift ;;
984 --tee)
985 shift ;; # was handled already
986 --root=*)
987 root=${1#--*=}
988 shift ;;
989 --chain-lint)
990 TESTLIB_TEST_CHAIN_LINT=1
991 shift ;;
992 --no-chain-lint)
993 TESTLIB_TEST_CHAIN_LINT=0
994 shift ;;
995 -x|--x|--xt|--xtr|--xtra|--xtrac|--xtrace)
996 trace=t
997 verbose=t
998 shift ;;
999 --verbose-log)
1000 verbose_log=t
1001 shift ;;
1003 echo "error: unknown test option '$1'" >&2; exit 1 ;;
1004 esac
1005 done
1007 test "x${color+set}" != "xset" &&
1008 test "x$TERM" != "xdumb" && (
1009 { test -n "$TESTLIB_FORCETTY" || test -t 1; } &&
1010 tput bold >/dev/null 2>&1 &&
1011 tput setaf 1 >/dev/null 2>&1 &&
1012 tput sgr0 >/dev/null 2>&1
1013 ) &&
1014 color=t
1015 if test -n "$color"
1016 then
1017 # Save the color control sequences now rather than run tput
1018 # each time say_color() is called. This is done for two
1019 # reasons:
1020 # * TERM will be changed to dumb
1021 # * HOME will be changed to a temporary directory and tput
1022 # might need to read ~/.terminfo from the original HOME
1023 # directory to get the control sequences
1024 getcmd say_color_error eval 'tput setaf 1' # red
1025 getcmd say_color_skip eval 'tput bold; tput setaf 5' # bold blue
1026 getcmd say_color_warn eval 'tput setaf 3' # brown/yellow
1027 getcmd say_color_pass eval 'tput setaf 2' # green
1028 getcmd say_color_info eval 'tput setaf 6' # cyan
1029 getcmd say_color_reset eval 'tput sgr0'
1030 say_color_="" # no formatting for normal text
1033 TERM=dumb
1034 export TERM
1036 test_failure=0
1037 test_count=0
1038 test_fixed=0
1039 test_broken=0
1040 test_success=0
1042 test_external_has_tap=0
1044 # The user-facing functions are loaded from a separate file
1045 . "$TESTLIB_DIRECTORY/test-lib-functions-tg.sh"
1046 . "$TESTLIB_DIRECTORY/test-lib-functions.sh"
1047 test_lib_functions_init
1048 test_lib_functions_tg_init
1050 # Check for shopt
1051 : "${TESTLIB_SHELL_HAS_SHOPT=$(command -v shopt)}"
1053 last_verbose=t
1055 [ -n "$TEST_HELPER_DIRECTORY" ] && [ -d "$TEST_HELPER_DIRECTORY" ] && PATH="$TEST_HELPER_DIRECTORY:$PATH" || :
1056 if [ -n "$TG_TEST_INSTALLED" ]; then
1057 TG_TEST_FULL_PATH="$(cmd_path tg)" && [ -n "$TG_TEST_FULL_PATH" ] ||
1058 fatal 'error: TG_TEST_INSTALLED set but no tg found in $PATH!'
1059 else
1060 tg_bin_dir="$(cd "$TESTLIB_DIRECTORY/../bin-wrappers" 2>/dev/null && pwd -P || :)"
1061 [ -x "$tg_bin_dir/tg" ] ||
1062 fatal 'error: no ../bin-wrappers/tg executable found!'
1063 PATH="$tg_bin_dir:$PATH"
1064 TG_TEST_FULL_PATH="$tg_bin_dir/tg"
1066 export TG_TEST_FULL_PATH
1067 tg_version="$(tg --version)" ||
1068 fatal 'error: tg --version failed!'
1069 case "$tg_version" in [Tt][Oo][Pp][Gg][Ii][Tt]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn]\ [0-9]*);;*)
1070 fatal "error: tg --version returned bogus value: $tg_version"
1071 esac
1073 vcmp "$git_version" '>=' "$GIT_MINIMUM_VERSION" ||
1074 fatal "git version >= $GIT_MINIMUM_VERSION required but found \"$git_version\" instead"
1076 if test -z "$TESTLIB_TEST_CMP"
1077 then
1078 if test -n "$TESTLIB_TEST_CMP_USE_COPIED_CONTEXT"
1079 then
1080 TESTLIB_TEST_CMP="$DIFF -c"
1081 else
1082 TESTLIB_TEST_CMP="$DIFF -u"
1086 # Fix some commands on Windows
1087 case "${UNAME_S:=$(uname -s)}" in
1088 *MINGW*)
1089 # no POSIX permissions
1090 # backslashes in pathspec are converted to '/'
1091 # exec does not inherit the PID
1092 test_set_prereq MINGW
1093 test_set_prereq NATIVE_CRLF
1094 test_set_prereq SED_STRIPS_CR
1095 test_set_prereq GREP_STRIPS_CR
1096 TESTLIB_TEST_CMP=mingw_test_cmp
1098 *CYGWIN*)
1099 test_set_prereq POSIXPERM
1100 test_set_prereq EXECKEEPSPID
1101 test_set_prereq CYGWIN
1102 test_set_prereq SED_STRIPS_CR
1103 test_set_prereq GREP_STRIPS_CR
1106 test_set_prereq POSIXPERM
1107 test_set_prereq BSLASHPSPEC
1108 test_set_prereq EXECKEEPSPID
1110 esac
1112 ( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
1114 test_lib_main_init_funcs
1116 test_lazy_prereq PIPE '
1117 # test whether the filesystem supports FIFOs
1118 case "${UNAME_S:=$(uname -s)}" in
1119 CYGWIN*|MINGW*)
1120 false
1123 rm -f testfifo && mkfifo testfifo
1125 esac
1128 test_lazy_prereq SYMLINKS '
1129 # test whether the filesystem supports symbolic links
1130 ln -s x y && test -h y
1133 test_lazy_prereq FILEMODE '
1134 test "$(git config --bool core.filemode)" = true
1137 test_lazy_prereq CASE_INSENSITIVE_FS '
1138 echo good >CamelCase &&
1139 echo bad >camelcase &&
1140 test "$(cat CamelCase)" != good
1143 test_lazy_prereq UTF8_NFD_TO_NFC '
1144 # check whether FS converts nfd unicode to nfc
1145 auml="$(printf "\303\244")"
1146 aumlcdiar="$(printf "\141\314\210")"
1147 >"$auml" &&
1148 case "$(echo *)" in
1149 "$aumlcdiar")
1150 true ;;
1152 false ;;
1153 esac
1156 test_lazy_prereq AUTOIDENT '
1157 sane_unset GIT_AUTHOR_NAME &&
1158 sane_unset GIT_AUTHOR_EMAIL &&
1159 git var GIT_AUTHOR_IDENT
1162 test_lazy_prereq EXPENSIVE '
1163 test -n "$TESTLIB_TEST_LONG"
1166 test_lazy_prereq USR_BIN_TIME '
1167 test -x /usr/bin/time
1170 test_lazy_prereq NOT_ROOT '
1171 uid="$(id -u)" &&
1172 test "$uid" != 0
1175 # SANITY is about "can you correctly predict what the filesystem would
1176 # do by only looking at the permission bits of the files and
1177 # directories?" A typical example of !SANITY is running the test
1178 # suite as root, where a test may expect "chmod -r file && cat file"
1179 # to fail because file is supposed to be unreadable after a successful
1180 # chmod. In an environment (i.e. combination of what filesystem is
1181 # being used and who is running the tests) that lacks SANITY, you may
1182 # be able to delete or create a file when the containing directory
1183 # doesn't have write permissions, or access a file even if the
1184 # containing directory doesn't have read or execute permissions.
1186 test_lazy_prereq SANITY '
1187 mkdir SANETESTD.1 SANETESTD.2 &&
1189 chmod +w SANETESTD.1 SANETESTD.2 &&
1190 >SANETESTD.1/x 2>SANETESTD.2/x &&
1191 chmod -w SANETESTD.1 &&
1192 chmod -r SANETESTD.1/x &&
1193 chmod -rx SANETESTD.2 ||
1194 error "bug in test sript: cannot prepare SANETESTD"
1196 ! test -r SANETESTD.1/x &&
1197 ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
1198 status=$?
1200 chmod +rwx SANETESTD.1 SANETESTD.2 &&
1201 rm -rf SANETESTD.1 SANETESTD.2 ||
1202 error "bug in test sript: cannot clean SANETESTD"
1203 return $status
1206 test_lazy_prereq CMDLINE_LIMIT 'run_with_limited_cmdline true'
1209 # End test_lib_main_init_generic
1213 # This function is guaranteed to always be called for every single test.
1214 # Only put things in this function that MUST be done per-test, function
1215 # definitions and sourcing other files generally DO NOT QUALIFY (there can
1216 # be exceptions).
1217 test_lib_main_init_specific() {
1218 # Begin test_lib_main_init_specific
1221 # original stdin is on 6, stdout on 5 and stderr on 7
1222 exec 5>&1 6<&0 7>&2
1224 test_lib_main_init_funcs
1226 if test -n "$HARNESS_ACTIVE"
1227 then
1228 if test "$verbose" = t || test -n "$verbose_only" && test -z "$verbose_log$TESTLIB_OVERRIDE"
1229 then
1230 printf 'Bail out! %s\n' \
1231 'verbose mode forbidden under TAP harness; use --verbose-log'
1232 exit 1
1236 test "${test_description}" != "" ||
1237 error "Test script did not set test_description."
1239 if test "$help" = "t"
1240 then
1241 printf '%s\n' "$(printf '%s\n' "$test_description")" |
1242 sed -n -e '
1244 :loop
1245 s/\([^ ]\)/\1/
1246 t rest
1248 b loop
1250 :rest
1253 exit 0
1256 if test "$verbose_log" = "t"
1257 then
1258 exec 3>>"$TESTLIB_TEST_TEE_OUTPUT_FILE" 4>&3
1259 elif test "$verbose" = "t"
1260 then
1261 exec 4>&2 3>&1
1262 else
1263 exec 4>/dev/null 3>/dev/null
1266 # Send any "-x" output directly to stderr to avoid polluting tests
1267 # which capture stderr. We can do this unconditionally since it
1268 # has no effect if tracing isn't turned on.
1270 # Note that this sets up the trace fd as soon as we assign the variable, so it
1271 # must come after the creation of descriptor 4 above. Likewise, we must never
1272 # unset this, as it has the side effect of closing descriptor 4, which we
1273 # use to show verbose tests to the user.
1275 # Note also that we don't need or want to export it. The tracing is local to
1276 # this shell, and we would not want to influence any shells we exec.
1277 BASH_XTRACEFD=4
1279 TESTLIB_EXIT_OK=
1280 trap '_die' EXIT
1281 trap 'exit $?' HUP INT QUIT ABRT PIPE TERM
1282 trap 'TESTLIB_EXIT_OK=t; exit 1' USR1
1284 # Test repository
1285 TRASH_DIRECTORY="trash directory.${0##*/}"
1286 TRASH_DIRECTORY="${TRASH_DIRECTORY%.sh}"
1287 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
1288 test -n "$root" && GIT_CEILING_DIRECTORIES="$root:$GIT_CEILING_DIRECTORIES"
1289 case "$TRASH_DIRECTORY" in
1290 /*) ;; # absolute path is good
1291 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
1292 esac
1293 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
1294 ! test -e "$TRASH_DIRECTORY" || {
1295 rm -rf "$TRASH_DIRECTORY" &&
1296 ! test -e "$TRASH_DIRECTORY" || {
1297 chmod -R u+w "$TRASH_DIRECTORY" &&
1298 rm -rf "$TRASH_DIRECTORY" &&
1299 ! test -e "$TRASH_DIRECTORY"
1301 } || {
1302 TESTLIB_EXIT_OK=t
1303 echo >&5 "FATAL: Cannot prepare test area"
1304 exit 1
1307 HOME="$TRASH_DIRECTORY"
1308 GNUPGHOME="$HOME/gnupg-home-not-used"
1309 export HOME GNUPGHOME
1311 if test -z "$TEST_NO_CREATE_REPO"
1312 then
1313 test_create_repo "$TRASH_DIRECTORY"
1314 else
1315 mkdir -p "$TRASH_DIRECTORY"
1317 # Use -P to resolve symlinks in our working directory so that the cwd
1318 # in subprocesses like tg equals our $PWD (for pathname comparisons).
1319 cd -P "$TRASH_DIRECTORY" || exit 1
1321 this_test=${0##*/}
1322 this_test=${this_test%%-*}
1323 test_wrote_plan_count=
1324 test_last_subtest_ok=1
1325 if match_pattern_list "$this_test" $TESTLIB_SKIP_TESTS
1326 then
1327 say_color info >&3 "skipping test $this_test altogether"
1328 skip_all="skip all tests in $this_test"
1329 test_done
1332 if test z"${TESTLIB_SHELL_HAS_SHOPT=$(command -v shopt)}" = z"shopt"
1333 then
1334 shopt -s expand_aliases >/dev/null 2>&1 || :
1337 # End test_lib_main_init_specific
1342 # THIS SHOULD ALWAYS BE THE LAST FUNCTION DEFINED IN THIS FILE
1344 # Any client that sources this file should immediately execute this function
1345 # afterwards with the command line arguments
1347 # THERE SHOULD NOT BE ANY DIRECTLY EXECUTED LINES OF CODE IN THIS FILE
1349 test_lib_main_init() {
1351 test_lib_main_init_generic "$@"
1352 test_lib_main_init_specific "$@"