Restore `stg sink --nopush` capability
[stgit.git] / t / test-lib.sh
blobcc8dde0cd77f80e0ca44eaf93e268e6a1f78a8f0
1 # Test framework for git, adapted for StGit. See t/README for usage.
3 # Copyright (c) 2005 Junio C Hamano
4 # Copyright (c) 2006 Yann Dirson - tuning for stgit
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see http://www.gnu.org/licenses/ .
19 # Test the binaries we have just built. The tests are kept in
20 # t/ subdirectory and are run in 'trash directory' subdirectory.
21 if test -z "$TEST_DIRECTORY"
22 then
23 # We allow tests to override this, in case they want to run tests
24 # outside of t/, e.g. for running tests on the test library
25 # itself.
26 TEST_DIRECTORY=$(pwd)
27 else
28 # ensure that TEST_DIRECTORY is an absolute path so that it
29 # is valid even if the current working directory is changed
30 TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
32 if test -z "$TEST_OUTPUT_DIRECTORY"
33 then
34 # Similarly, override this to store the test-results subdir
35 # elsewhere
36 TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
38 STG_ROOT=$(cd "$TEST_DIRECTORY"/.. && pwd) || exit 1
39 export STG_ROOT
41 PERL_PATH=${PERL:-perl}
42 SHELL_PATH=${SHELL_PATH:-/bin/sh}
43 TEST_SHELL_PATH=${TEST_SHELL_PATH:-$SHELL_PATH}
44 export PERL_PATH SHELL_PATH
46 STGIT_DEBUG_LEVEL=0
48 # Parse options while taking care to leave $@ intact, so we will still
49 # have all the original command line options when executing the test
50 # script again for '--tee' and '--verbose-log' below.
51 store_arg_to=
52 prev_opt=
53 for opt
55 if test -n "$store_arg_to"
56 then
57 eval $store_arg_to=\$opt
58 store_arg_to=
59 prev_opt=
60 continue
63 case "$opt" in
64 -d|--d|--de|--deb|--debu|--debug)
65 debug=t ;;
66 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
67 immediate=t ;;
68 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
69 GIT_TEST_LONG=t; export GIT_TEST_LONG ;;
70 -r)
71 store_arg_to=run_list
73 --run=*)
74 run_list=${opt#--*=} ;;
75 -h|--h|--he|--hel|--help)
76 help=t ;;
77 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
78 STGIT_DEBUG_LEVEL=1
79 verbose=t ;;
80 --verbose-only=*)
81 verbose_only=${opt#--*=}
83 -q|--q|--qu|--qui|--quie|--quiet)
84 # Ignore --quiet under a TAP::Harness. Saying how many tests
85 # passed without the ok/not ok details is always an error.
86 test -z "$HARNESS_ACTIVE" && quiet=t ;;
87 --no-color)
88 color= ;;
89 --tee)
90 tee=t ;;
91 --root=*)
92 root=${opt#--*=} ;;
93 -x)
94 trace=t ;;
95 -V|--verbose-log)
96 verbose_log=t
97 tee=t
99 --write-junit-xml)
100 write_junit_xml=t
102 --stress)
103 stress=t ;;
104 --stress=*)
105 echo "error: --stress does not accept an argument: '$opt'" >&2
106 echo "did you mean --stress-jobs=${opt#*=} or --stress-limit=${opt#*=}?" >&2
107 exit 1
109 --stress-jobs=*)
110 stress=t;
111 stress=${opt#--*=}
112 case "$stress" in
113 *[!0-9]*|0*|"")
114 echo "error: --stress-jobs=<N> requires the number of jobs to run" >&2
115 exit 1
117 *) # Good.
119 esac
121 --stress-limit=*)
122 stress=t;
123 stress_limit=${opt#--*=}
124 case "$stress_limit" in
125 *[!0-9]*|0*|"")
126 echo "error: --stress-limit=<N> requires the number of repetitions" >&2
127 exit 1
129 *) # Good.
131 esac
134 echo "error: unknown test option '$opt'" >&2; exit 1 ;;
135 esac
137 prev_opt=$opt
138 done
139 if test -n "$store_arg_to"
140 then
141 echo "error: $prev_opt requires an argument" >&2
142 exit 1
145 if test -n "$stress"
146 then
147 STGIT_DEBUG_LEVEL=1
148 verbose=t
149 trace=t
150 immediate=t
153 TEST_STRESS_JOB_SFX="${GIT_TEST_STRESS_JOB_NR:+.stress-$GIT_TEST_STRESS_JOB_NR}"
154 TEST_NAME="$(basename "$0" .sh)"
155 TEST_NUMBER="${TEST_NAME%%-*}"
156 TEST_NUMBER="${TEST_NUMBER#t}"
157 TEST_RESULTS_DIR="$TEST_OUTPUT_DIRECTORY/test-results"
158 TEST_RESULTS_BASE="$TEST_RESULTS_DIR/$TEST_NAME$TEST_STRESS_JOB_SFX"
159 TRASH_DIRECTORY="trash directory.$TEST_NAME$TEST_STRESS_JOB_SFX"
160 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
161 case "$TRASH_DIRECTORY" in
162 /*) ;; # absolute path is good
163 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
164 esac
166 # If --stress was passed, run this test repeatedly in several parallel loops.
167 if test "$GIT_TEST_STRESS_STARTED" = "done"
168 then
169 : # Don't stress test again.
170 elif test -n "$stress"
171 then
172 if test "$stress" != t
173 then
174 job_count=$stress
175 elif test -n "$GIT_TEST_STRESS_LOAD"
176 then
177 job_count="$GIT_TEST_STRESS_LOAD"
178 elif job_count=$(getconf _NPROCESSORS_ONLN 2>/dev/null) &&
179 test -n "$job_count"
180 then
181 job_count=$((2 * $job_count))
182 else
183 job_count=8
186 mkdir -p "$TEST_RESULTS_DIR"
187 stressfail="$TEST_RESULTS_BASE.stress-failed"
188 rm -f "$stressfail"
190 stress_exit=0
191 trap '
192 kill $job_pids 2>/dev/null
193 wait
194 stress_exit=1
195 ' TERM INT HUP
197 job_pids=
198 job_nr=0
199 while test $job_nr -lt "$job_count"
202 GIT_TEST_STRESS_STARTED=done
203 GIT_TEST_STRESS_JOB_NR=$job_nr
204 export GIT_TEST_STRESS_STARTED GIT_TEST_STRESS_JOB_NR
206 trap '
207 kill $test_pid 2>/dev/null
208 wait
209 exit 1
210 ' TERM INT
212 cnt=1
213 while ! test -e "$stressfail" &&
214 { test -z "$stress_limit" ||
215 test $cnt -le $stress_limit ; }
217 $TEST_SHELL_PATH "$0" "$@" >"$TEST_RESULTS_BASE.stress-$job_nr.out" 2>&1 &
218 test_pid=$!
220 if wait $test_pid
221 then
222 printf "OK %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
223 else
224 echo $GIT_TEST_STRESS_JOB_NR >>"$stressfail"
225 printf "FAIL %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
227 cnt=$(($cnt + 1))
228 done
230 job_pids="$job_pids $!"
231 job_nr=$(($job_nr + 1))
232 done
234 wait
236 if test -f "$stressfail"
237 then
238 stress_exit=1
239 echo "Log(s) of failed test run(s):"
240 for failed_job_nr in $(sort -n "$stressfail")
242 echo "Contents of '$TEST_RESULTS_BASE.stress-$failed_job_nr.out':"
243 cat "$TEST_RESULTS_BASE.stress-$failed_job_nr.out"
244 done
245 rm -rf "$TRASH_DIRECTORY.stress-failed"
246 # Move the last one.
247 mv "$TRASH_DIRECTORY.stress-$failed_job_nr" "$TRASH_DIRECTORY.stress-failed"
250 exit $stress_exit
253 # if --tee was passed, write the output not only to the terminal, but
254 # additionally to the file test-results/$BASENAME.out, too.
255 if test "$GIT_TEST_TEE_STARTED" = "done"
256 then
257 : # do not redirect again
258 elif test -n "$tee"
259 then
260 mkdir -p "$TEST_RESULTS_DIR"
262 # Make this filename available to the sub-process in case it is using
263 # --verbose-log.
264 GIT_TEST_TEE_OUTPUT_FILE=$TEST_RESULTS_BASE.out
265 export GIT_TEST_TEE_OUTPUT_FILE
267 # Truncate before calling "tee -a" to get rid of the results
268 # from any previous runs.
269 >"$GIT_TEST_TEE_OUTPUT_FILE"
271 (GIT_TEST_TEE_STARTED=done ${TEST_SHELL_PATH} "$0" "$@" 2>&1;
272 echo $? >"$TEST_RESULTS_BASE.exit") | tee -a "$GIT_TEST_TEE_OUTPUT_FILE"
273 test "$(cat "$TEST_RESULTS_BASE.exit")" = 0
274 exit
277 if test -n "$trace" && test -n "$test_untraceable"
278 then
279 # '-x' tracing requested, but this test script can't be reliably
280 # traced, unless it is run with a Bash version supporting
281 # BASH_XTRACEFD (introduced in Bash v4.1).
283 # Perform this version check _after_ the test script was
284 # potentially re-executed with $TEST_SHELL_PATH for '--tee' or
285 # '--verbose-log', so the right shell is checked and the
286 # warning is issued only once.
287 if test -n "$BASH_VERSION" && eval '
288 test ${BASH_VERSINFO[0]} -gt 4 || {
289 test ${BASH_VERSINFO[0]} -eq 4 &&
290 test ${BASH_VERSINFO[1]} -ge 1
293 then
294 : Executed by a Bash version supporting BASH_XTRACEFD. Good.
295 else
296 echo >&2 "warning: ignoring -x; '$0' is untraceable without BASH_XTRACEFD"
297 trace=
300 if test -n "$trace" && test -z "$verbose_log"
301 then
302 STGIT_DEBUG_LEVEL=1
303 verbose=t
306 export STGIT_DEBUG_LEVEL
308 # For repeatability, reset the environment to known value.
309 # TERM is sanitized below, after saving color control sequences.
310 LANG=${STG_TEST_LANG:-C}
311 LC_ALL=$LANG
312 PAGER=cat
313 TZ=UTC
314 export LANG LC_ALL PAGER TZ
315 EDITOR=:
317 # A call to "unset" with no arguments causes at least Solaris 10
318 # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
319 # deriving from the command substitution clustered with the other
320 # ones.
321 unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
322 my @env = keys %ENV;
323 my $ok = join("|", qw(
324 TRACE
325 DEBUG
326 TEST
327 .*_TEST
328 PROVE
329 VALGRIND
330 UNZIP
331 PERF_
332 CURL_VERBOSE
333 TRACE_CURL
335 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
336 print join("\n", @vars);
338 unset XDG_CACHE_HOME
339 unset XDG_CONFIG_HOME
340 unset GITPERLLIB
341 TEST_AUTHOR_LOCALNAME=author
342 TEST_AUTHOR_DOMAIN=example.com
343 GIT_AUTHOR_EMAIL=${TEST_AUTHOR_LOCALNAME}@${TEST_AUTHOR_DOMAIN}
344 GIT_AUTHOR_NAME='A Ú Thor'
345 TEST_COMMITTER_LOCALNAME=committer
346 TEST_COMMITTER_DOMAIN=example.com
347 GIT_COMMITTER_EMAIL=${TEST_COMMITTER_LOCALNAME}@${TEST_COMMITTER_DOMAIN}
348 GIT_COMMITTER_NAME='C Ó Mitter'
349 GIT_MERGE_VERBOSITY=5
350 GIT_MERGE_AUTOEDIT=no
351 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
352 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
353 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
354 export EDITOR
356 # Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
357 GIT_TRACE_BARE=1
358 export GIT_TRACE_BARE
360 # Use specific version of the index file format
361 if test -n "${GIT_TEST_INDEX_VERSION:+isset}"
362 then
363 GIT_INDEX_VERSION="$GIT_TEST_INDEX_VERSION"
364 export GIT_INDEX_VERSION
367 # Protect ourselves from common misconfiguration to export
368 # CDPATH into the environment
369 unset CDPATH
371 unset GREP_OPTIONS
372 unset UNZIP
374 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
375 1|2|true)
376 GIT_TRACE=4
378 esac
380 # Convenience
382 # A regexp to match 5, 35 and 40 hexdigits
383 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
384 _x35="$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
385 _x40="$_x35$_x05"
387 # Zero SHA-1
388 _z40=0000000000000000000000000000000000000000
390 OID_REGEX="$_x40"
391 ZERO_OID=$_z40
392 EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
393 EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
395 # Line feed
396 LF='
399 # Single quote
400 SQ=\'
402 # UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
403 # when case-folding filenames
404 u200c=$(printf '\342\200\214')
406 export _x05 _x35 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB ZERO_OID OID_REGEX
408 # Each test should start with something like this, after copyright notices:
410 # test_description='Description of this test...
411 # This test checks if command xyzzy does the right thing...
413 # . ./test-lib.sh
414 test "x$TERM" != "xdumb" && (
415 test -t 1 &&
416 tput bold >/dev/null 2>&1 &&
417 tput setaf 1 >/dev/null 2>&1 &&
418 tput sgr0 >/dev/null 2>&1
419 ) &&
420 color=t
422 if test -n "$color"
423 then
424 # Save the color control sequences now rather than run tput
425 # each time say_color() is called. This is done for two
426 # reasons:
427 # * TERM will be changed to dumb
428 # * HOME will be changed to a temporary directory and tput
429 # might need to read ~/.terminfo from the original HOME
430 # directory to get the control sequences
431 # Note: This approach assumes the control sequences don't end
432 # in a newline for any terminal of interest (command
433 # substitutions strip trailing newlines). Given that most
434 # (all?) terminals in common use are related to ECMA-48, this
435 # shouldn't be a problem.
436 say_color_error=$(tput bold; tput setaf 1) # bold red
437 say_color_skip=$(tput setaf 4) # blue
438 say_color_warn=$(tput setaf 3) # brown/yellow
439 say_color_pass=$(tput setaf 2) # green
440 say_color_info=$(tput setaf 6) # cyan
441 say_color_reset=$(tput sgr0)
442 say_color_="" # no formatting for normal text
443 say_color () {
444 test -z "$1" && test -n "$quiet" && return
445 eval "say_color_color=\$say_color_$1"
446 shift
447 printf "%s\\n" "$say_color_color$*$say_color_reset"
449 else
450 say_color() {
451 test -z "$1" && test -n "$quiet" && return
452 shift
453 printf "%s\n" "$*"
457 TERM=dumb
458 export TERM
460 error () {
461 say_color error "error: $*"
462 finalize_junit_xml
463 GIT_EXIT_OK=t
464 exit 1
467 BUG () {
468 error >&7 "bug in the test script: $*"
471 say () {
472 say_color info "$*"
475 if test -n "$HARNESS_ACTIVE"
476 then
477 if test "$verbose" = t || test -n "$verbose_only"
478 then
479 printf 'Bail out! %s\n' \
480 'verbose mode forbidden under TAP harness; try --verbose-log'
481 exit 1
485 test "${test_description}" != "" ||
486 error "Test script did not set test_description."
488 if test "$help" = "t"
489 then
490 printf '%s\n' "$test_description"
491 exit 0
494 exec 5>&1
495 exec 6<&0
496 exec 7>&2
497 if test "$verbose_log" = "t"
498 then
499 exec 3>>"$GIT_TEST_TEE_OUTPUT_FILE" 4>&3
500 elif test "$verbose" = "t"
501 then
502 exec 4>&2 3>&1
503 else
504 exec 4>/dev/null 3>/dev/null
507 # Send any "-x" output directly to stderr to avoid polluting tests
508 # which capture stderr. We can do this unconditionally since it
509 # has no effect if tracing isn't turned on.
511 # Note that this sets up the trace fd as soon as we assign the variable, so it
512 # must come after the creation of descriptor 4 above. Likewise, we must never
513 # unset this, as it has the side effect of closing descriptor 4, which we
514 # use to show verbose tests to the user.
516 # Note also that we don't need or want to export it. The tracing is local to
517 # this shell, and we would not want to influence any shells we exec.
518 BASH_XTRACEFD=4
520 test_failure=0
521 test_count=0
522 test_fixed=0
523 test_broken=0
524 test_success=0
526 test_external_has_tap=0
528 die () {
529 code=$?
530 # This is responsible for running the atexit commands even when a
531 # test script run with '--immediate' fails, or when the user hits
532 # ctrl-C, i.e. when 'test_done' is not invoked at all.
533 test_atexit_handler || code=$?
534 if test -n "$GIT_EXIT_OK"
535 then
536 exit $code
537 else
538 echo >&5 "FATAL: Unexpected exit with code $code"
539 exit 1
543 GIT_EXIT_OK=
544 trap 'die' EXIT
545 # Disable '-x' tracing, because with some shells, notably dash, it
546 # prevents running the cleanup commands when a test script run with
547 # '--verbose-log -x' is interrupted.
548 trap '{ code=$?; set +x; } 2>/dev/null; exit $code' INT TERM HUP
550 # The user-facing functions are loaded from a separate file so that
551 # test_perf subshells can have them too
552 . "$TEST_DIRECTORY/test-lib-functions.sh"
554 # You are not expected to call test_ok_ and test_failure_ directly, use
555 # the test_expect_* functions instead.
557 test_ok_ () {
558 if test -n "$write_junit_xml"
559 then
560 write_junit_xml_testcase "$*"
562 test_success=$(($test_success + 1))
563 say_color "" "ok $test_count - $@"
566 test_failure_ () {
567 if test -n "$write_junit_xml"
568 then
569 junit_insert="<failure message=\"not ok $test_count -"
570 junit_insert="$junit_insert $(xml_attr_encode "$1")\">"
571 junit_insert="$junit_insert $(xml_attr_encode \
572 "$(if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
573 then
574 test-tool path-utils skip-n-bytes \
575 "$GIT_TEST_TEE_OUTPUT_FILE" $GIT_TEST_TEE_OFFSET
576 else
577 printf '%s\n' "$@" | sed 1d
578 fi)")"
579 junit_insert="$junit_insert</failure>"
580 if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
581 then
582 junit_insert="$junit_insert<system-err>$(xml_attr_encode \
583 "$(cat "$GIT_TEST_TEE_OUTPUT_FILE")")</system-err>"
585 write_junit_xml_testcase "$1" " $junit_insert"
587 test_failure=$(($test_failure + 1))
588 say_color error "not ok $test_count - $1"
589 shift
590 printf '%s\n' "$*" | sed -e 's/^/# /'
591 test "$immediate" = "" || { finalize_junit_xml; GIT_EXIT_OK=t; exit 1; }
594 test_known_broken_ok_ () {
595 if test -n "$write_junit_xml"
596 then
597 write_junit_xml_testcase "$* (breakage fixed)"
599 test_fixed=$(($test_fixed+1))
600 say_color error "ok $test_count - $@ # TODO known breakage vanished"
603 test_known_broken_failure_ () {
604 if test -n "$write_junit_xml"
605 then
606 write_junit_xml_testcase "$* (known breakage)"
608 test_broken=$(($test_broken+1))
609 say_color warn "not ok $test_count - $@ # TODO known breakage"
612 test_debug () {
613 test "$debug" = "" || eval "$1"
616 match_pattern_list () {
617 arg="$1"
618 shift
619 test -z "$*" && return 1
620 for pattern_
622 case "$arg" in
623 $pattern_)
624 return 0
625 esac
626 done
627 return 1
630 match_test_selector_list () {
631 title="$1"
632 shift
633 arg="$1"
634 shift
635 test -z "$1" && return 0
637 # Both commas and whitespace are accepted as separators.
638 OLDIFS=$IFS
639 IFS=' ,'
640 set -- $1
641 IFS=$OLDIFS
643 # If the first selector is negative we include by default.
644 include=
645 case "$1" in
646 !*) include=t ;;
647 esac
649 for selector
651 orig_selector=$selector
653 positive=t
654 case "$selector" in
656 positive=
657 selector=${selector##?}
659 esac
661 test -z "$selector" && continue
663 case "$selector" in
664 *-*)
665 if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
666 then
667 echo "error: $title: invalid non-numeric in range" \
668 "start: '$orig_selector'" >&2
669 exit 1
671 if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
672 then
673 echo "error: $title: invalid non-numeric in range" \
674 "end: '$orig_selector'" >&2
675 exit 1
679 if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
680 then
681 echo "error: $title: invalid non-numeric in test" \
682 "selector: '$orig_selector'" >&2
683 exit 1
685 esac
687 # Short cut for "obvious" cases
688 test -z "$include" && test -z "$positive" && continue
689 test -n "$include" && test -n "$positive" && continue
691 case "$selector" in
693 if test $arg -le ${selector#-}
694 then
695 include=$positive
699 if test $arg -ge ${selector%-}
700 then
701 include=$positive
704 *-*)
705 if test ${selector%%-*} -le $arg \
706 && test $arg -le ${selector#*-}
707 then
708 include=$positive
712 if test $arg -eq $selector
713 then
714 include=$positive
717 esac
718 done
720 test -n "$include"
723 maybe_teardown_verbose () {
724 test -z "$verbose_only" && return
725 exec 4>/dev/null 3>/dev/null
726 verbose=
729 last_verbose=t
730 maybe_setup_verbose () {
731 test -z "$verbose_only" && return
732 if match_pattern_list $test_count $verbose_only
733 then
734 exec 4>&2 3>&1
735 # Emit a delimiting blank line when going from
736 # non-verbose to verbose. Within verbose mode the
737 # delimiter is printed by test_expect_*. The choice
738 # of the initial $last_verbose is such that before
739 # test 1, we do not print it.
740 test -z "$last_verbose" && echo >&3 ""
741 verbose=t
742 else
743 exec 4>/dev/null 3>/dev/null
744 verbose=
746 last_verbose=$verbose
749 want_trace () {
750 test "$trace" = t && {
751 test "$verbose" = t || test "$verbose_log" = t
755 # This is a separate function because some tests use
756 # "return" to end a test_expect_success block early
757 # (and we want to make sure we run any cleanup like
758 # "set +x").
759 test_eval_inner_ () {
760 # Do not add anything extra (including LF) after '$*'
761 eval "
762 want_trace && set -x
766 test_eval_ () {
767 # If "-x" tracing is in effect, then we want to avoid polluting stderr
768 # with non-test commands. But once in "set -x" mode, we cannot prevent
769 # the shell from printing the "set +x" to turn it off (nor the saving
770 # of $? before that). But we can make sure that the output goes to
771 # /dev/null.
773 # There are a few subtleties here:
775 # - we have to redirect descriptor 4 in addition to 2, to cover
776 # BASH_XTRACEFD
778 # - the actual eval has to come before the redirection block (since
779 # it needs to see descriptor 4 to set up its stderr)
781 # - likewise, any error message we print must be outside the block to
782 # access descriptor 4
784 # - checking $? has to come immediately after the eval, but it must
785 # be _inside_ the block to avoid polluting the "set -x" output
788 test_eval_inner_ "$@" </dev/null >&3 2>&4
790 test_eval_ret_=$?
791 if want_trace
792 then
793 set +x
795 } 2>/dev/null 4>&2
797 if test "$test_eval_ret_" != 0 && want_trace
798 then
799 say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
801 return $test_eval_ret_
804 test_run_ () {
805 test_cleanup=:
806 expecting_failure=$2
808 test_eval_ "$1"
809 eval_ret=$?
811 if test -z "$immediate" || test $eval_ret = 0 ||
812 test -n "$expecting_failure" && test "$test_cleanup" != ":"
813 then
814 test_eval_ "$test_cleanup"
816 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
817 then
818 echo ""
820 return "$eval_ret"
823 test_start_ () {
824 test_count=$(($test_count+1))
825 maybe_setup_verbose
826 if test -n "$write_junit_xml"
827 then
828 junit_start=$(test-tool date getnanos)
832 test_finish_ () {
833 echo >&3 ""
834 maybe_teardown_verbose
835 if test -n "$GIT_TEST_TEE_OFFSET"
836 then
837 GIT_TEST_TEE_OFFSET=$(test-tool path-utils file-size \
838 "$GIT_TEST_TEE_OUTPUT_FILE")
842 test_skip () {
843 to_skip=
844 skipped_reason=
845 if match_pattern_list $this_test.$test_count $STG_SKIP_TESTS
846 then
847 to_skip=t
848 skipped_reason="STG_SKIP_TESTS"
850 if test -z "$to_skip" && test -n "$run_list" &&
851 ! match_test_selector_list '--run' $test_count "$run_list"
852 then
853 to_skip=t
854 skipped_reason="--run"
856 if test -z "$to_skip" && test -n "$test_prereq" &&
857 ! test_have_prereq "$test_prereq"
858 then
859 to_skip=t
861 of_prereq=
862 if test "$missing_prereq" != "$test_prereq"
863 then
864 of_prereq=" of $test_prereq"
866 skipped_reason="missing $missing_prereq${of_prereq}"
869 case "$to_skip" in
871 if test -n "$write_junit_xml"
872 then
873 message="$(xml_attr_encode "$skipped_reason")"
874 write_junit_xml_testcase "$1" \
875 " <skipped message=\"$message\" />"
878 say_color skip >&3 "skipping test: $@"
879 say_color skip "ok $test_count # skip $1 ($skipped_reason)"
880 : true
883 false
885 esac
888 # stub; perf-lib overrides it
889 test_at_end_hook_ () {
893 write_junit_xml () {
894 case "$1" in
895 --truncate)
896 >"$junit_xml_path"
897 junit_have_testcase=
898 shift
900 esac
901 printf '%s\n' "$@" >>"$junit_xml_path"
904 xml_attr_encode () {
905 printf '%s\n' "$@" | test-tool xml-encode
908 write_junit_xml_testcase () {
909 junit_attrs="name=\"$(xml_attr_encode "$this_test.$test_count $1")\""
910 shift
911 junit_attrs="$junit_attrs classname=\"$this_test\""
912 junit_attrs="$junit_attrs time=\"$(test-tool \
913 date getnanos $junit_start)\""
914 write_junit_xml "$(printf '%s\n' \
915 " <testcase $junit_attrs>" "$@" " </testcase>")"
916 junit_have_testcase=t
919 finalize_junit_xml () {
920 if test -n "$write_junit_xml" && test -n "$junit_xml_path"
921 then
922 test -n "$junit_have_testcase" || {
923 junit_start=$(test-tool date getnanos)
924 write_junit_xml_testcase "all tests skipped"
927 # adjust the overall time
928 junit_time=$(test-tool date getnanos $junit_suite_start)
929 sed -e "s/\(<testsuite.*\) time=\"[^\"]*\"/\1/" \
930 -e "s/<testsuite [^>]*/& time=\"$junit_time\"/" \
931 <"$junit_xml_path" >"$junit_xml_path.new"
932 mv "$junit_xml_path.new" "$junit_xml_path"
934 write_junit_xml " </testsuite>" "</testsuites>"
935 write_junit_xml=
939 test_atexit_cleanup=:
940 test_atexit_handler () {
941 # In a succeeding test script 'test_atexit_handler' is invoked
942 # twice: first from 'test_done', then from 'die' in the trap on
943 # EXIT.
944 # This condition and resetting 'test_atexit_cleanup' below makes
945 # sure that the registered cleanup commands are run only once.
946 test : != "$test_atexit_cleanup" || return 0
948 test_eval_ "$test_atexit_cleanup"
949 test_atexit_cleanup=:
952 test_done () {
953 GIT_EXIT_OK=t
955 # Run the atexit commands _before_ the trash directory is
956 # removed, so the commands can access pidfiles and socket files.
957 test_atexit_handler
959 finalize_junit_xml
961 if test -z "$HARNESS_ACTIVE"
962 then
963 mkdir -p "$TEST_RESULTS_DIR"
965 cat >"$TEST_RESULTS_BASE.counts" <<-EOF
966 total $test_count
967 success $test_success
968 fixed $test_fixed
969 broken $test_broken
970 failed $test_failure
975 if test "$test_fixed" != 0
976 then
977 say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
979 if test "$test_broken" != 0
980 then
981 say_color warn "# still have $test_broken known breakage(s)"
983 if test "$test_broken" != 0 || test "$test_fixed" != 0
984 then
985 test_remaining=$(( $test_count - $test_broken - $test_fixed ))
986 msg="remaining $test_remaining test(s)"
987 else
988 test_remaining=$test_count
989 msg="$test_count test(s)"
991 case "$test_failure" in
993 if test $test_external_has_tap -eq 0
994 then
995 if test $test_remaining -gt 0
996 then
997 say_color pass "# passed all $msg"
1000 # Maybe print SKIP message
1001 test -z "$skip_all" || skip_all="# SKIP $skip_all"
1002 case "$test_count" in
1004 say "1..$test_count${skip_all:+ $skip_all}"
1007 test -z "$skip_all" ||
1008 say_color warn "$skip_all"
1009 say "1..$test_count"
1011 esac
1014 if test -z "$debug"
1015 then
1016 test -d "$TRASH_DIRECTORY" ||
1017 error "Tests passed but trash directory already removed before test cleanup; aborting"
1019 cd "$TRASH_DIRECTORY/.." &&
1020 rm -fr "$TRASH_DIRECTORY" || {
1021 # try again in a bit
1022 sleep 5;
1023 rm -fr "$TRASH_DIRECTORY"
1024 } ||
1025 error "Tests passed but test cleanup failed; aborting"
1027 test_at_end_hook_
1029 exit 0 ;;
1032 if test $test_external_has_tap -eq 0
1033 then
1034 say_color error "# failed $test_failure among $msg"
1035 say "1..$test_count"
1038 exit 1 ;;
1040 esac
1043 if test -n "$STG_TEST_INSTALLED"
1044 then
1045 PATH=$STG_TEST_INSTALLED:$PATH
1046 else
1047 PATH="$TEST_DIRECTORY/test-bin:$PATH"
1048 PYTHONPATH="$STG_ROOT":"$PYTHONPATH"
1050 GIT_EXEC_PATH=$(git --exec-path) || error "Cannot run git"
1051 unset GIT_TEMPLATE_DIR
1052 GIT_CONFIG_NOSYSTEM=1
1053 GIT_ATTR_NOSYSTEM=1
1054 export PATH GIT_EXEC_PATH PYTHONPATH GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
1056 if test -z "$GIT_TEST_CMP"
1057 then
1058 DIFF="${DIFF:-diff}"
1059 if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
1060 then
1061 GIT_TEST_CMP="$DIFF -c"
1062 else
1063 GIT_TEST_CMP="$DIFF -u"
1067 # Test repository
1068 rm -fr "$TRASH_DIRECTORY" || {
1069 GIT_EXIT_OK=t
1070 echo >&5 "FATAL: Cannot prepare test area"
1071 exit 1
1074 HOME="$TRASH_DIRECTORY"
1075 GNUPGHOME="$HOME/gnupg-home-not-used"
1076 export HOME GNUPGHOME
1078 if test -z "$TEST_NO_CREATE_REPO"
1079 then
1080 test_create_repo "$TRASH_DIRECTORY"
1081 else
1082 mkdir -p "$TRASH_DIRECTORY"
1085 # Use -P to resolve symlinks in our working directory so that the cwd
1086 # in subprocesses like git equals our $PWD (for pathname comparisons).
1087 cd -P "$TRASH_DIRECTORY" || exit 1
1089 this_test=${0##*/}
1090 this_test=${this_test%%-*}
1091 if match_pattern_list "$this_test" $STG_SKIP_TESTS
1092 then
1093 say_color info >&3 "skipping test $this_test altogether"
1094 skip_all="skip all tests in $this_test"
1095 test_done
1098 if test -n "$write_junit_xml"
1099 then
1100 junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
1101 mkdir -p "$junit_xml_dir"
1102 junit_xml_base=${0##*/}
1103 junit_xml_path="$junit_xml_dir/TEST-${junit_xml_base%.sh}.xml"
1104 junit_attrs="name=\"${junit_xml_base%.sh}\""
1105 junit_attrs="$junit_attrs timestamp=\"$(TZ=UTC \
1106 date +%Y-%m-%dT%H:%M:%S)\""
1107 write_junit_xml --truncate "<testsuites>" " <testsuite $junit_attrs>"
1108 junit_suite_start=$(test-tool date getnanos)
1109 if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
1110 then
1111 GIT_TEST_TEE_OFFSET=0
1115 # Provide an implementation of the 'yes' utility; the upper bound
1116 # limit is there to help Windows that cannot stop this loop from
1117 # wasting cycles when the downstream stops reading, so do not be
1118 # tempted to turn it into an infinite loop. cf. 6129c930 ("test-lib:
1119 # limit the output of the yes utility", 2016-02-02)
1120 yes () {
1121 if test $# = 0
1122 then
1124 else
1125 y="$*"
1129 while test $i -lt 99
1131 echo "$y"
1132 i=$(($i+1))
1133 done
1136 # Fix some commands on Windows, and other OS-specific things
1137 uname_s=$(uname -s)
1138 case $uname_s in
1139 *MINGW*)
1140 # Windows has its own (incompatible) sort and find
1141 sort () {
1142 /usr/bin/sort "$@"
1144 find () {
1145 /usr/bin/find "$@"
1147 # git sees Windows-style pwd
1148 pwd () {
1149 builtin pwd -W
1151 # no POSIX permissions
1152 # backslashes in pathspec are converted to '/'
1153 # exec does not inherit the PID
1154 test_set_prereq MINGW
1155 test_set_prereq NATIVE_CRLF
1156 test_set_prereq SED_STRIPS_CR
1157 test_set_prereq GREP_STRIPS_CR
1158 GIT_TEST_CMP=mingw_test_cmp
1160 *CYGWIN*)
1161 test_set_prereq POSIXPERM
1162 test_set_prereq EXECKEEPSPID
1163 test_set_prereq CYGWIN
1164 test_set_prereq SED_STRIPS_CR
1165 test_set_prereq GREP_STRIPS_CR
1167 FreeBSD)
1168 test_set_prereq REGEX_ILLSEQ
1169 test_set_prereq POSIXPERM
1170 test_set_prereq BSLASHPSPEC
1171 test_set_prereq EXECKEEPSPID
1174 test_set_prereq POSIXPERM
1175 test_set_prereq BSLASHPSPEC
1176 test_set_prereq EXECKEEPSPID
1178 esac
1180 test_lazy_prereq QUILT '
1181 # test whether quilt is installed
1182 quilt --version 2>/dev/null >/dev/null