testlib: cache generic test setup for multiple test runs
[topgit/pro.git] / t / test-lib-main.sh
blob4476c1db22260121ef2eafe58a42d1e847a9b052
1 # Test framework from Git with modifications.
3 # Modifications Copyright (C) 2016 Kyle J. McKay
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_my_dir, vcmp, test_possibly_broken_ok_ and
15 # 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 whats_my_dir() (
55 # determine script's location and name
56 myname="$0"
57 while [ -L "$myname" ]; do
58 oldname="$myname"
59 myname="$(readlink "$myname")"
60 case "$myname" in /*) :;; *)
61 myname="$(dirname "$oldname")/$myname"
62 esac
63 done
64 mydir="$(cd "$(dirname "$myname")" && pwd -P)"
65 printf '%s\n' "$mydir"
68 vcmp() (
69 # Compare $1 to $2 each of which must match \d+(\.\d+)*
70 # An empty string ('') for $1 or $2 is treated like 0
71 # Outputs:
72 # -1 if $1 < $2
73 # 0 if $1 = $2
74 # 1 if $1 > $2
75 # Note that $(vcmp 1.8 1.8.0.0.0.0) correctly outputs 0.
76 while
77 _a="${1%%.*}"
78 _b="${2%%.*}"
79 [ -n "$_a" -o -n "$_b" ]
81 if [ "${_a:-0}" -lt "${_b:-0}" ]; then
82 echo -1
83 return
84 elif [ "${_a:-0}" -gt "${_b:-0}" ]; then
85 echo 1
86 return
88 _a2="${1#$_a}"
89 _b2="${2#$_b}"
90 set -- "${_a2#.}" "${_b2#.}"
91 done
92 echo 0
95 error() {
96 say_color error "error: $*"
97 TESTLIB_EXIT_OK=t
98 exit 1
101 say() {
102 say_color info "$*"
105 die() {
106 code=$?
107 if test -n "$TESTLIB_EXIT_OK"
108 then
109 exit $code
110 else
111 echo >&5 "FATAL: Unexpected exit with code $code"
112 exit 1
116 # You are not expected to call test_ok_ and test_failure_ directly, use
117 # the test_expect_* functions instead.
119 test_ok_() {
120 test_success=$(($test_success + 1))
121 say_color "" "ok $test_count - $@"
124 test_failure_() {
125 test_failure=$(($test_failure + 1))
126 say_color error "not ok $test_count - $1"
127 shift
128 printf '%s\n' "$*" | sed -e 's/^/# /'
129 test "$immediate" = "" || { TESTLIB_EXIT_OK=t; exit 1; }
132 test_known_broken_ok_() {
133 test_fixed=$(($test_fixed + 1))
134 say_color error "ok $test_count - $@ # TODO known breakage vanished"
137 test_known_broken_failure_() {
138 test_broken=$(($test_broken + 1))
139 say_color warn "not ok $test_count - $@ # TODO known breakage"
142 test_possibly_broken_ok_() {
143 test_success=$(($test_success + 1))
144 say_color "" "ok $test_count - $@"
147 test_possibly_broken_failure_() {
148 test_broken=$(($test_broken + 1))
149 say_color warn "not ok $test_count - $@ # TODO tolerated breakage"
152 test_debug() {
153 test "$debug" = "" || test $# -eq 0 || test -z "$*" || "$@"
156 match_pattern_list() {
157 arg="$1"
158 shift
159 test -z "$*" && return 1
160 for pattern_
162 case "$arg" in
163 $pattern_)
164 return 0
165 esac
166 done
167 return 1
170 match_test_selector_list() {
171 title="$1"
172 shift
173 arg="$1"
174 shift
175 test -z "$1" && return 0
177 # Both commas and whitespace are accepted as separators.
178 OLDIFS=$IFS
179 IFS=' ,'
180 set -- $1
181 IFS=$OLDIFS
183 # If the first selector is negative we include by default.
184 include=
185 case "$1" in
186 !*) include=t ;;
187 esac
189 for selector
191 orig_selector=$selector
193 positive=t
194 case "$selector" in
196 positive=
197 selector=${selector##?}
199 esac
201 test -z "$selector" && continue
203 case "$selector" in
204 *-*)
205 if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
206 then
207 echo "error: $title: invalid non-numeric in range" \
208 "start: '$orig_selector'" >&2
209 exit 1
211 if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
212 then
213 echo "error: $title: invalid non-numeric in range" \
214 "end: '$orig_selector'" >&2
215 exit 1
219 if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
220 then
221 echo "error: $title: invalid non-numeric in test" \
222 "selector: '$orig_selector'" >&2
223 exit 1
225 esac
227 # Short cut for "obvious" cases
228 test -z "$include" && test -z "$positive" && continue
229 test -n "$include" && test -n "$positive" && continue
231 case "$selector" in
233 if test $arg -le ${selector#-}
234 then
235 include=$positive
239 if test $arg -ge ${selector%-}
240 then
241 include=$positive
244 *-*)
245 if test ${selector%%-*} -le $arg \
246 && test $arg -le ${selector#*-}
247 then
248 include=$positive
252 if test $arg -eq $selector
253 then
254 include=$positive
257 esac
258 done
260 test -n "$include"
263 maybe_teardown_verbose() {
264 test -z "$verbose_only" && return
265 exec 4>/dev/null 3>/dev/null
266 verbose=
269 maybe_setup_verbose() {
270 test -z "$verbose_only" && return
271 if match_pattern_list $test_count $verbose_only
272 then
273 exec 4>&2 3>&1
274 # Emit a delimiting blank line when going from
275 # non-verbose to verbose. Within verbose mode the
276 # delimiter is printed by test_expect_*. The choice
277 # of the initial $last_verbose is such that before
278 # test 1, we do not print it.
279 test -z "$last_verbose" && echo >&3 ""
280 verbose=t
281 else
282 exec 4>/dev/null 3>/dev/null
283 verbose=
285 last_verbose=$verbose
288 want_trace() {
289 test "$trace" = t && test "$verbose" = t
292 # This is a separate function because some tests use
293 # "return" to end a test_expect_success block early
294 # (and we want to make sure we run any cleanup like
295 # "set +x").
296 test_eval_inner_() {
297 # Do not add anything extra (including LF) after '$*'
298 eval "
299 want_trace && set -x
303 test_eval_() {
304 # We run this block with stderr redirected to avoid extra cruft
305 # during a "-x" trace. Once in "set -x" mode, we cannot prevent
306 # the shell from printing the "set +x" to turn it off (nor the saving
307 # of $? before that). But we can make sure that the output goes to
308 # /dev/null.
310 # The test itself is run with stderr put back to &4 (so either to
311 # /dev/null, or to the original stderr if --verbose was used).
313 test_eval_inner_ "$@" </dev/null >&3 2>&4
314 test_eval_ret_=$?
315 if want_trace
316 then
317 set +x
318 if test "$test_eval_ret_" != 0
319 then
320 say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
323 } 2>/dev/null
324 return $test_eval_ret_
327 test_run_() {
328 test_cleanup=:
329 expecting_failure=$2
331 if test "${TESTLIB_TEST_CHAIN_LINT:-1}" != 0; then
332 # turn off tracing for this test-eval, as it simply creates
333 # confusing noise in the "-x" output
334 trace_tmp=$trace
335 trace=
336 # 117 is magic because it is unlikely to match the exit
337 # code of other programs
338 test_eval_ "(exit 117) && $1"
339 if test "$?" != 117; then
340 error "bug in the test script: broken &&-chain: $1"
342 trace=$trace_tmp
345 test_eval_ "$1"
346 eval_ret=$?
348 if test -z "$immediate" || test $eval_ret = 0 ||
349 test -n "$expecting_failure" && test "${test_cleanup:-:}" != ":"
350 then
351 test_eval_ "$test_cleanup"
353 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
354 then
355 echo ""
357 return "$eval_ret"
360 test_start_() {
361 test_count=$(($test_count+1))
362 maybe_setup_verbose
365 test_finish_() {
366 echo >&3 ""
367 maybe_teardown_verbose
370 test_skip() {
371 to_skip=
372 skipped_reason=
373 if match_pattern_list $this_test.$test_count $TESTLIB_SKIP_TESTS
374 then
375 to_skip=t
376 skipped_reason="TESTLIB_SKIP_TESTS"
378 if test -z "$to_skip" && test -n "$test_prereq" &&
379 ! test_have_prereq "$test_prereq"
380 then
381 to_skip=t
383 of_prereq=
384 if test "$missing_prereq" != "$test_prereq"
385 then
386 of_prereq=" of $test_prereq"
388 skipped_reason="missing $missing_prereq${of_prereq}"
390 if test -z "$to_skip" && test -n "$run_list" &&
391 ! match_test_selector_list '--run' $test_count "$run_list"
392 then
393 to_skip=t
394 skipped_reason="--run"
397 case "$to_skip" in
399 say_color skip >&3 "skipping test: $@"
400 say_color skip "ok $test_count # skip $1 ($skipped_reason)"
401 : true
404 false
406 esac
409 # stub; runs at end of each successful test
410 test_at_end_hook_() {
414 test_done() {
415 TESTLIB_EXIT_OK=t
417 if test -z "$HARNESS_ACTIVE"
418 then
419 test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
420 mkdir -p "$test_results_dir"
421 base=${0##*/}
422 test_results_path="$test_results_dir/${base%.sh}.counts"
424 cat >"$test_results_path" <<-EOF
425 total $test_count
426 success $test_success
427 fixed $test_fixed
428 broken $test_broken
429 failed $test_failure
434 if test "$test_fixed" != 0
435 then
436 say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
438 if test "$test_broken" != 0
439 then
440 say_color warn "# still have $test_broken known breakage(s)"
442 if test "$test_broken" != 0 || test "$test_fixed" != 0
443 then
444 test_remaining=$(( $test_count - $test_broken - $test_fixed ))
445 msg="remaining $test_remaining test(s)"
446 else
447 test_remaining=$test_count
448 msg="$test_count test(s)"
450 case "$test_failure" in
452 # Maybe print SKIP message
453 if test -n "$skip_all" && test $test_count -gt 0
454 then
455 error "Can't use skip_all after running some tests"
457 test -z "$skip_all" || skip_all=" # SKIP $skip_all"
459 if test $test_external_has_tap -eq 0
460 then
461 if test $test_remaining -gt 0
462 then
463 say_color pass "# passed all $msg"
465 say "1..$test_count$skip_all"
468 test -d "$remove_trash" &&
469 cd "$(dirname "$remove_trash")" &&
470 rm -rf "$(basename "$remove_trash")"
472 test_at_end_hook_
474 exit 0 ;;
477 if test $test_external_has_tap -eq 0
478 then
479 say_color error "# failed $test_failure among $msg"
480 say "1..$test_count"
483 exit 1 ;;
485 esac
488 # Provide an implementation of the 'yes' utility
489 yes() {
490 if test $# = 0
491 then
493 else
494 y="$*"
498 while test $i -lt 99
500 echo "$y"
501 i=$(($i+1))
502 done
505 run_with_limited_cmdline() {
506 (ulimit -s 128 && "$@")
511 ## Note that the following functions have bodies that are NOT indented
512 ## to assist with readability
516 # This function is called with all the test args and must perform all
517 # initialization that involves variables and is not specific to "$0"
518 # or "$test_description" in any way. This function may only be called
519 # once per run of the entire test suite.
520 test_lib_main_init_generic() {
521 # Begin test_lib_main_init_generic
524 ! [ -f ../TG-BUILD-SETTINGS ] || . ../TG-BUILD-SETTINGS
525 ! [ -f TG-TEST-SETTINGS ] || . ./TG-TEST-SETTINGS
527 : "${SHELL_PATH:=/bin/sh}"
528 : "${DIFF:=diff}"
529 : "${GIT_PATH:=$(cmd_path git)}"
530 : "${PERL_PATH:=$(cmd_path perl || :)}"
531 TESTLIB_DIRECTORY="$(whats_my_dir)"
533 # Test the binaries we have just built. The tests are kept in
534 # t/ subdirectory and are run in 'trash directory' subdirectory.
535 if test -z "$TEST_DIRECTORY"
536 then
537 # We allow tests to override this, in case they want to run tests
538 # outside of t/, e.g. for running tests on the test library
539 # itself.
540 TEST_DIRECTORY="$TESTLIB_DIRECTORY"
541 else
542 # ensure that TEST_DIRECTORY is an absolute path so that it
543 # is valid even if the current working directory is changed
544 TEST_DIRECTORY="$(cd "$TEST_DIRECTORY" && pwd)" || exit 1
546 if test -z "$TEST_OUTPUT_DIRECTORY"
547 then
548 # Similarly, override this to store the test-results subdir
549 # elsewhere
550 TEST_OUTPUT_DIRECTORY="$TEST_DIRECTORY"
552 [ -d "$TESTLIB_DIRECTORY"/empty ] || {
553 mkdir "$TESTLIB_DIRECTORY/empty"
554 chmod a-w "$TESTLIB_DIRECTORY/empty"
556 EMPTY_DIRECTORY="$TESTLIB_DIRECTORY/empty"
558 ################################################################
559 # It appears that people try to run tests with missing perl or git...
560 git_version="$("$GIT_PATH" --version 2>&1)" ||
561 fatal 'error: you do not seem to have git available?'
562 case "$git_version" in [Gg][Ii][Tt]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn]\ [0-9]*) :;; *)
563 fatal "error: git --version returned bogus value: $git_version"
564 esac
565 #"$PERL_PATH" --version >/dev/null 2>&1 ||
566 # fatal 'error: you do not seem to have perl available?'
568 # if --tee was passed, write the output not only to the terminal, but
569 # additionally to the file test-results/$BASENAME.out, too.
570 case "$TESTLIB_TEST_TEE_STARTED, $* " in
571 done,*)
572 # do not redirect again
574 *' --tee '*|*' --verbose-log '*)
575 mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
576 BASE="$TEST_OUTPUT_DIRECTORY/test-results/$(basename "$0" .sh)"
578 # Make this filename available to the sub-process in case it is using
579 # --verbose-log.
580 TESTLIB_TEST_TEE_OUTPUT_FILE=$BASE.out
581 export TESTLIB_TEST_TEE_OUTPUT_FILE
583 # Truncate before calling "tee -a" to get rid of the results
584 # from any previous runs.
585 >"$TESTLIB_TEST_TEE_OUTPUT_FILE"
587 (TESTLIB_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1;
588 echo $? >"$BASE.exit") | tee -a "$TESTLIB_TEST_TEE_OUTPUT_FILE"
589 test "$(cat "$BASE.exit")" = 0
590 exit
592 esac
594 # For repeatability, reset the environment to known value.
595 # TERM is sanitized below, after saving color control sequences.
596 LANG=C
597 LC_ALL=C
598 PAGER=cat
599 TZ=UTC
600 export LANG LC_ALL PAGER TZ
601 EDITOR=:
602 # A call to "unset" with no arguments causes at least Solaris 10
603 # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
604 # deriving from the command substitution clustered with the other
605 # ones.
606 unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
607 my @env = keys %ENV;
608 my $ok = join("|", qw(
609 TRACE
610 DEBUG
611 USE_LOOKUP
612 TEST
613 .*_TEST
614 MINIMUM_VERSION
615 PATH
616 PROVE
617 UNZIP
618 PERF_
619 CURL_VERBOSE
620 TRACE_CURL
622 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
623 print join("\n", @vars);
625 unset XDG_CONFIG_HOME
626 unset GITPERLLIB
627 GIT_AUTHOR_NAME='Te s t'
628 GIT_AUTHOR_EMAIL=test@example.net
629 GIT_COMMITTER_NAME='Fra mewor k'
630 GIT_COMMITTER_EMAIL=framework@example.org
631 GIT_MERGE_VERBOSITY=5
632 GIT_MERGE_AUTOEDIT=no
633 GIT_TEMPLATE_DIR="$EMPTY_DIRECTORY"
634 GIT_CONFIG_NOSYSTEM=1
635 GIT_ATTR_NOSYSTEM=1
636 export PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
637 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
638 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
639 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
640 export EDITOR
642 # Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
643 GIT_TRACE_BARE=1
644 export GIT_TRACE_BARE
646 # Protect ourselves from common misconfiguration to export
647 # CDPATH into the environment
648 unset CDPATH
650 unset GREP_OPTIONS
651 unset UNZIP
653 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
654 1|2|true)
655 GIT_TRACE=4
657 esac
659 # Convenience
661 # A regexp to match 5 and 40 hexdigits
662 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
663 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
665 # Zero SHA-1
666 _z40=0000000000000000000000000000000000000000
668 EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
669 EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
671 # Line feed
672 LF='
675 # UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
676 # when case-folding filenames
677 u200c=$(printf '\342\200\214')
679 export _x05 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB
681 test "x$TERM" != "xdumb" && (
682 { test -n "$TESTLIB_FORCETTY" || test -t 1; } &&
683 tput bold >/dev/null 2>&1 &&
684 tput setaf 1 >/dev/null 2>&1 &&
685 tput sgr0 >/dev/null 2>&1
686 ) &&
687 color=t
689 while test "$#" -ne 0
691 case "$1" in
692 -d|--d|--de|--deb|--debu|--debug)
693 debug=t; shift ;;
694 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
695 immediate=t; shift ;;
696 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
697 TESTLIB_TEST_LONG=t; export TESTLIB_TEST_LONG; shift ;;
699 shift; test "$#" -ne 0 || {
700 echo 'error: -r requires an argument' >&2;
701 exit 1;
703 run_list=$1; shift ;;
704 --run=*)
705 run_list=${1#--*=}; shift ;;
706 -h|--h|--he|--hel|--help)
707 help=t; shift ;;
708 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
709 verbose=t; shift ;;
710 --verbose-only=*)
711 verbose_only=${1#--*=}
712 shift ;;
713 -q|--q|--qu|--qui|--quie|--quiet)
714 # Ignore --quiet under a TAP::Harness. Saying how many tests
715 # passed without the ok/not ok details is always an error.
716 test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
717 --no-color)
718 color=; shift ;;
719 --tee)
720 shift ;; # was handled already
721 --root=*)
722 root=${1#--*=}
723 shift ;;
724 --chain-lint)
725 TESTLIB_TEST_CHAIN_LINT=1
726 shift ;;
727 --no-chain-lint)
728 TESTLIB_TEST_CHAIN_LINT=0
729 shift ;;
731 trace=t
732 verbose=t
733 shift ;;
734 --verbose-log)
735 verbose_log=t
736 shift ;;
738 echo "error: unknown test option '$1'" >&2; exit 1 ;;
739 esac
740 done
742 if test -n "$color"
743 then
744 # Save the color control sequences now rather than run tput
745 # each time say_color() is called. This is done for two
746 # reasons:
747 # * TERM will be changed to dumb
748 # * HOME will be changed to a temporary directory and tput
749 # might need to read ~/.terminfo from the original HOME
750 # directory to get the control sequences
751 # Note: This approach assumes the control sequences don't end
752 # in a newline for any terminal of interest (command
753 # substitutions strip trailing newlines). Given that most
754 # (all?) terminals in common use are related to ECMA-48, this
755 # shouldn't be a problem.
756 say_color_error=$(tput bold; tput setaf 1) # bold red
757 say_color_skip=$(tput setaf 4) # blue
758 say_color_warn=$(tput setaf 3) # brown/yellow
759 say_color_pass=$(tput setaf 2) # green
760 say_color_info=$(tput setaf 6) # cyan
761 say_color_reset=$(tput sgr0)
762 say_color_="" # no formatting for normal text
765 TERM=dumb
766 export TERM
768 # Send any "-x" output directly to stderr to avoid polluting tests
769 # which capture stderr. We can do this unconditionally since it
770 # has no effect if tracing isn't turned on.
772 # Note that this sets up the trace fd as soon as we assign the variable, so it
773 # must come after the creation of descriptor 4 above. Likewise, we must never
774 # unset this, as it has the side effect of closing descriptor 4, which we
775 # use to show verbose tests to the user.
777 # Note also that we don't need or want to export it. The tracing is local to
778 # this shell, and we would not want to influence any shells we exec.
779 BASH_XTRACEFD=4
781 test_failure=0
782 test_count=0
783 test_fixed=0
784 test_broken=0
785 test_success=0
787 test_external_has_tap=0
789 # The user-facing functions are loaded from a separate file
790 . "$TEST_DIRECTORY/test-lib-functions.sh"
791 test_lib_functions_init
793 last_verbose=t
795 if [ -n "$TG_TEST_INSTALLED" ]; then
796 [ -n "$(cmd_path tg || :)" ] ||
797 fatal 'error: TG_TEST_INSTALLED set but no tg found in $PATH!'
798 else
799 tg_bin_dir="$(cd "$TESTLIB_DIRECTORY/../bin-wrappers" 2>/dev/null && pwd -P || :)"
800 [ -x "$tg_bin_dir/tg" ] ||
801 fatal 'error: no ../bin-wrappers/tg executable found!'
802 PATH="$tg_bin_dir:$PATH"
804 tg_version="$(tg --version)" ||
805 fatal 'error: tg --version failed!'
806 case "$tg_version" in [Tt][Oo][Pp][Gg][Ii][Tt]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn]\ [0-9]*) :;; *)
807 fatal "error: tg --version returned bogus value: $tg_version"
808 esac
810 if [ -n "$GIT_MINIMUM_VERSION" ] && [ -n "$git_version" ]; then
811 git_vernum="$(sed -ne '1s/^[^0-9]*\([0-9][0-9]*\(\.[0-9][0-9]*\)*\).*$/\1/p' <<-EOT
812 $git_version
815 [ "$(vcmp "$git_vernum" $GIT_MINIMUM_VERSION)" -ge 0 ] ||
816 fatal "git version >= $GIT_MINIMUM_VERSION required but found git version $git_vernum instead"
819 if test -z "$TESTLIB_TEST_CMP"
820 then
821 if test -n "$TESTLIB_TEST_CMP_USE_COPIED_CONTEXT"
822 then
823 TESTLIB_TEST_CMP="$DIFF -c"
824 else
825 TESTLIB_TEST_CMP="$DIFF -u"
829 # Fix some commands on Windows
830 uname_s="$(uname -s)"
831 case $uname_s in
832 *MINGW*)
833 # no POSIX permissions
834 # backslashes in pathspec are converted to '/'
835 # exec does not inherit the PID
836 test_set_prereq MINGW
837 test_set_prereq NATIVE_CRLF
838 test_set_prereq SED_STRIPS_CR
839 test_set_prereq GREP_STRIPS_CR
840 TESTLIB_TEST_CMP=mingw_test_cmp
842 *CYGWIN*)
843 test_set_prereq POSIXPERM
844 test_set_prereq EXECKEEPSPID
845 test_set_prereq CYGWIN
846 test_set_prereq SED_STRIPS_CR
847 test_set_prereq GREP_STRIPS_CR
850 test_set_prereq POSIXPERM
851 test_set_prereq BSLASHPSPEC
852 test_set_prereq EXECKEEPSPID
854 esac
856 ( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
858 test_lazy_prereq PIPE '
859 # test whether the filesystem supports FIFOs
860 case "$uname_s" in
861 CYGWIN*|MINGW*)
862 false
865 rm -f testfifo && mkfifo testfifo
867 esac
870 test_lazy_prereq SYMLINKS '
871 # test whether the filesystem supports symbolic links
872 ln -s x y && test -h y
875 test_lazy_prereq FILEMODE '
876 test "$(git config --bool core.filemode)" = true
879 test_lazy_prereq CASE_INSENSITIVE_FS '
880 echo good >CamelCase &&
881 echo bad >camelcase &&
882 test "$(cat CamelCase)" != good
885 test_lazy_prereq UTF8_NFD_TO_NFC '
886 # check whether FS converts nfd unicode to nfc
887 auml=$(printf "\303\244")
888 aumlcdiar=$(printf "\141\314\210")
889 >"$auml" &&
890 case "$(echo *)" in
891 "$aumlcdiar")
892 true ;;
894 false ;;
895 esac
898 test_lazy_prereq AUTOIDENT '
899 sane_unset GIT_AUTHOR_NAME &&
900 sane_unset GIT_AUTHOR_EMAIL &&
901 git var GIT_AUTHOR_IDENT
904 test_lazy_prereq EXPENSIVE '
905 test -n "$TESTLIB_TEST_LONG"
908 test_lazy_prereq USR_BIN_TIME '
909 test -x /usr/bin/time
912 test_lazy_prereq NOT_ROOT '
913 uid=$(id -u) &&
914 test "$uid" != 0
917 # SANITY is about "can you correctly predict what the filesystem would
918 # do by only looking at the permission bits of the files and
919 # directories?" A typical example of !SANITY is running the test
920 # suite as root, where a test may expect "chmod -r file && cat file"
921 # to fail because file is supposed to be unreadable after a successful
922 # chmod. In an environment (i.e. combination of what filesystem is
923 # being used and who is running the tests) that lacks SANITY, you may
924 # be able to delete or create a file when the containing directory
925 # doesn't have write permissions, or access a file even if the
926 # containing directory doesn't have read or execute permissions.
928 test_lazy_prereq SANITY '
929 mkdir SANETESTD.1 SANETESTD.2 &&
931 chmod +w SANETESTD.1 SANETESTD.2 &&
932 >SANETESTD.1/x 2>SANETESTD.2/x &&
933 chmod -w SANETESTD.1 &&
934 chmod -r SANETESTD.1/x &&
935 chmod -rx SANETESTD.2 ||
936 error "bug in test sript: cannot prepare SANETESTD"
938 ! test -r SANETESTD.1/x &&
939 ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
940 status=$?
942 chmod +rwx SANETESTD.1 SANETESTD.2 &&
943 rm -rf SANETESTD.1 SANETESTD.2 ||
944 error "bug in test sript: cannot clean SANETESTD"
945 return $status
948 test_lazy_prereq CMDLINE_LIMIT 'run_with_limited_cmdline true'
951 # End test_lib_main_init_generic
955 # This function is guaranteed to always be called for every single test.
956 # Only put things in this function that MUST be done per-test, function
957 # definitions and sourcing other files generally DO NOT QUALIFY (there can
958 # be exceptions).
959 test_lib_main_init_specific() {
960 # Begin test_lib_main_init_specific
963 # Ignore --quiet under a TAP::Harness. Saying how many tests
964 # passed without the ok/not ok details is always an error.
965 test -n "$HARNESS_ACTIVE" && unset quiet
967 if test -n "$color"
968 then
969 say_color() {
970 test -z "$1" && test -n "$quiet" && return
971 eval "say_color_color=\$say_color_$1"
972 shift
973 printf "%s\\n" "$say_color_color$*$say_color_reset"
975 else
976 say_color() {
977 test -z "$1" && test -n "$quiet" && return
978 shift
979 printf "%s\n" "$*"
983 if test -n "$HARNESS_ACTIVE"
984 then
985 if test "$verbose" = t || test -n "$verbose_only"
986 then
987 printf 'Bail out! %s\n' \
988 'verbose mode forbidden under TAP harness; try --verbose-log'
989 exit 1
993 test "${test_description}" != "" ||
994 error "Test script did not set test_description."
996 if test "$help" = "t"
997 then
998 printf '%s\n' "$test_description"
999 exit 0
1002 exec 5>&1
1003 exec 6<&0
1004 if test "$verbose_log" = "t"
1005 then
1006 exec 3>>"$TESTLIB_TEST_TEE_OUTPUT_FILE" 4>&3
1007 elif test "$verbose" = "t"
1008 then
1009 exec 4>&2 3>&1
1010 else
1011 exec 4>/dev/null 3>/dev/null
1014 TESTLIB_EXIT_OK=
1015 trap 'die' EXIT
1016 trap 'exit $?' HUP INT QUIT ABRT PIPE TERM
1018 # Test repository
1019 TRASH_DIRECTORY="trash directory.$(basename "$0" .sh)"
1020 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
1021 case "$TRASH_DIRECTORY" in
1022 /*) ;; # absolute path is good
1023 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
1024 esac
1025 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
1026 ! [ -e "$TRASH_DIRECTORY" ] || rm -fr "$TRASH_DIRECTORY" || {
1027 TESTLIB_EXIT_OK=t
1028 echo >&5 "FATAL: Cannot prepare test area"
1029 exit 1
1032 HOME="$TRASH_DIRECTORY"
1033 GNUPGHOME="$HOME/gnupg-home-not-used"
1034 export HOME GNUPGHOME
1036 if test -z "$TEST_NO_CREATE_REPO"
1037 then
1038 test_create_repo "$TRASH_DIRECTORY"
1039 else
1040 mkdir -p "$TRASH_DIRECTORY"
1042 # Use -P to resolve symlinks in our working directory so that the cwd
1043 # in subprocesses like tg equals our $PWD (for pathname comparisons).
1044 cd -P "$TRASH_DIRECTORY" || exit 1
1046 this_test=${0##*/}
1047 this_test=${this_test%%-*}
1048 if match_pattern_list "$this_test" $TESTLIB_SKIP_TESTS
1049 then
1050 say_color info >&3 "skipping test $this_test altogether"
1051 skip_all="skip all tests in $this_test"
1052 test_done
1055 # Fix some commands on Windows
1056 case "$uname_s" in
1057 *MINGW*)
1058 # Windows has its own (incompatible) sort and find
1059 sort() {
1060 /usr/bin/sort "$@"
1062 find() {
1063 /usr/bin/find "$@"
1065 sum() {
1066 md5sum "$@"
1068 # git sees Windows-style pwd
1069 pwd() {
1070 builtin pwd -W
1073 esac
1076 # End test_lib_main_init_specific
1081 # THIS SHOULD ALWAYS BE THE LAST FUNCTION DEFINED IN THIS FILE
1083 # Any client that sources this file should immediately execute this function
1084 # afterwards with the command line arguments
1086 # THERE SHOULD NOT BE ANY DIRECTLY EXECUTED LINES OF CODE IN THIS FILE
1088 test_lib_main_init() {
1090 test_lib_main_init_generic "$@"
1091 test_lib_main_init_specific "$@"