Tests redirect to err file when grepping stderr
[stgit.git] / t / test-lib.sh
blob309cc0ff3e4947974335cd8b684e208cac97d4fd
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 # In t0000, we need to override test directories of nested testcases. In case
49 # the developer has TEST_OUTPUT_DIRECTORY part of his build options, then we'd
50 # reset this value to instead contain what the developer has specified. We thus
51 # have this knob to allow overriding the directory.
52 if test -n "${TEST_OUTPUT_DIRECTORY_OVERRIDE}"
53 then
54 TEST_OUTPUT_DIRECTORY="${TEST_OUTPUT_DIRECTORY_OVERRIDE}"
57 # Explicitly set the default branch name for testing, to avoid the
58 # transitory "git init" warning under --verbose.
59 : ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
60 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
62 store_arg_to=
63 opt_required_arg=
64 # $1: option string
65 # $2: name of the var where the arg will be stored
66 mark_option_requires_arg () {
67 if test -n "$opt_required_arg"
68 then
69 echo "error: options that require args cannot be bundled" \
70 "together: '$opt_required_arg' and '$1'" >&2
71 exit 1
73 opt_required_arg=$1
74 store_arg_to=$2
77 parse_option () {
78 local opt="$1"
80 case "$opt" in
81 -d|--d|--de|--deb|--debu|--debug)
82 debug=t ;;
83 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
84 immediate=t ;;
85 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
86 GIT_TEST_LONG=t; export GIT_TEST_LONG ;;
87 -r)
88 mark_option_requires_arg "$opt" run_list
90 --run=*)
91 run_list=${opt#--*=} ;;
92 -h|--h|--he|--hel|--help)
93 help=t ;;
94 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
95 STGIT_DEBUG_LEVEL=1
96 verbose=t ;;
97 --verbose-only=*)
98 verbose_only=${opt#--*=}
100 -q|--q|--qu|--qui|--quie|--quiet)
101 # Ignore --quiet under a TAP::Harness. Saying how many tests
102 # passed without the ok/not ok details is always an error.
103 test -z "$HARNESS_ACTIVE" && quiet=t ;;
104 --no-color)
105 color= ;;
106 --tee)
107 tee=t ;;
108 --root=*)
109 root=${opt#--*=} ;;
111 trace=t ;;
112 -V|--verbose-log)
113 verbose_log=t
114 tee=t
116 --write-junit-xml)
117 write_junit_xml=t
119 --stress)
120 stress=t ;;
121 --stress=*)
122 echo "error: --stress does not accept an argument: '$opt'" >&2
123 echo "did you mean --stress-jobs=${opt#*=} or --stress-limit=${opt#*=}?" >&2
124 exit 1
126 --stress-jobs=*)
127 stress=t;
128 stress_jobs=${opt#--*=}
129 case "$stress_jobs" in
130 *[!0-9]*|0*|"")
131 echo "error: --stress-jobs=<N> requires the number of jobs to run" >&2
132 exit 1
134 *) # Good.
136 esac
138 --stress-limit=*)
139 stress=t;
140 stress_limit=${opt#--*=}
141 case "$stress_limit" in
142 *[!0-9]*|0*|"")
143 echo "error: --stress-limit=<N> requires the number of repetitions" >&2
144 exit 1
146 *) # Good.
148 esac
151 echo "error: unknown test option '$opt'" >&2; exit 1 ;;
152 esac
155 # Parse options while taking care to leave $@ intact, so we will still
156 # have all the original command line options when executing the test
157 # script again for '--tee' and '--verbose-log' later.
158 for opt
160 if test -n "$store_arg_to"
161 then
162 eval $store_arg_to=\$opt
163 store_arg_to=
164 opt_required_arg=
165 continue
168 case "$opt" in
169 --*|-?)
170 parse_option "$opt" ;;
171 -?*)
172 # bundled short options must be fed separately to parse_option
173 opt=${opt#-}
174 while test -n "$opt"
176 extra=${opt#?}
177 this=${opt%$extra}
178 opt=$extra
179 parse_option "-$this"
180 done
183 echo "error: unknown test option '$opt'" >&2; exit 1 ;;
184 esac
185 done
186 if test -n "$store_arg_to"
187 then
188 echo "error: $opt_required_arg requires an argument" >&2
189 exit 1
192 if test -n "$stress"
193 then
194 STGIT_DEBUG_LEVEL=1
195 verbose=t
196 trace=t
197 immediate=t
200 TEST_STRESS_JOB_SFX="${GIT_TEST_STRESS_JOB_NR:+.stress-$GIT_TEST_STRESS_JOB_NR}"
201 TEST_NAME="$(basename "$0" .sh)"
202 TEST_NUMBER="${TEST_NAME%%-*}"
203 TEST_NUMBER="${TEST_NUMBER#t}"
204 TEST_RESULTS_DIR="$TEST_OUTPUT_DIRECTORY/test-results"
205 TEST_RESULTS_BASE="$TEST_RESULTS_DIR/$TEST_NAME$TEST_STRESS_JOB_SFX"
206 TRASH_DIRECTORY="trash directory.$TEST_NAME$TEST_STRESS_JOB_SFX"
207 test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
208 case "$TRASH_DIRECTORY" in
209 /*) ;; # absolute path is good
210 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
211 esac
213 # If --stress was passed, run this test repeatedly in several parallel loops.
214 if test "$GIT_TEST_STRESS_STARTED" = "done"
215 then
216 : # Don't stress test again.
217 elif test -n "$stress"
218 then
219 if test -n "$stress_jobs"
220 then
221 job_count=$stress_jobs
222 elif test -n "$GIT_TEST_STRESS_LOAD"
223 then
224 job_count="$GIT_TEST_STRESS_LOAD"
225 elif job_count=$(getconf _NPROCESSORS_ONLN 2>/dev/null) &&
226 test -n "$job_count"
227 then
228 job_count=$((2 * $job_count))
229 else
230 job_count=8
233 mkdir -p "$TEST_RESULTS_DIR"
234 stressfail="$TEST_RESULTS_BASE.stress-failed"
235 rm -f "$stressfail"
237 stress_exit=0
238 trap '
239 kill $job_pids 2>/dev/null
240 wait
241 stress_exit=1
242 ' TERM INT HUP
244 job_pids=
245 job_nr=0
246 while test $job_nr -lt "$job_count"
249 GIT_TEST_STRESS_STARTED=done
250 GIT_TEST_STRESS_JOB_NR=$job_nr
251 export GIT_TEST_STRESS_STARTED GIT_TEST_STRESS_JOB_NR
253 trap '
254 kill $test_pid 2>/dev/null
255 wait
256 exit 1
257 ' TERM INT
259 cnt=1
260 while ! test -e "$stressfail" &&
261 { test -z "$stress_limit" ||
262 test $cnt -le $stress_limit ; }
264 $TEST_SHELL_PATH "$0" "$@" >"$TEST_RESULTS_BASE.stress-$job_nr.out" 2>&1 &
265 test_pid=$!
267 if wait $test_pid
268 then
269 printf "OK %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
270 else
271 echo $GIT_TEST_STRESS_JOB_NR >>"$stressfail"
272 printf "FAIL %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
274 cnt=$(($cnt + 1))
275 done
277 job_pids="$job_pids $!"
278 job_nr=$(($job_nr + 1))
279 done
281 wait
283 if test -f "$stressfail"
284 then
285 stress_exit=1
286 echo "Log(s) of failed test run(s):"
287 for failed_job_nr in $(sort -n "$stressfail")
289 echo "Contents of '$TEST_RESULTS_BASE.stress-$failed_job_nr.out':"
290 cat "$TEST_RESULTS_BASE.stress-$failed_job_nr.out"
291 done
292 rm -rf "$TRASH_DIRECTORY.stress-failed"
293 # Move the last one.
294 mv "$TRASH_DIRECTORY.stress-$failed_job_nr" "$TRASH_DIRECTORY.stress-failed"
297 exit $stress_exit
300 # if --tee was passed, write the output not only to the terminal, but
301 # additionally to the file test-results/$BASENAME.out, too.
302 if test "$GIT_TEST_TEE_STARTED" = "done"
303 then
304 : # do not redirect again
305 elif test -n "$tee"
306 then
307 mkdir -p "$TEST_RESULTS_DIR"
309 # Make this filename available to the sub-process in case it is using
310 # --verbose-log.
311 GIT_TEST_TEE_OUTPUT_FILE=$TEST_RESULTS_BASE.out
312 export GIT_TEST_TEE_OUTPUT_FILE
314 # Truncate before calling "tee -a" to get rid of the results
315 # from any previous runs.
316 >"$GIT_TEST_TEE_OUTPUT_FILE"
318 (GIT_TEST_TEE_STARTED=done ${TEST_SHELL_PATH} "$0" "$@" 2>&1;
319 echo $? >"$TEST_RESULTS_BASE.exit") | tee -a "$GIT_TEST_TEE_OUTPUT_FILE"
320 test "$(cat "$TEST_RESULTS_BASE.exit")" = 0
321 exit
324 if test -n "$trace" && test -n "$test_untraceable"
325 then
326 # '-x' tracing requested, but this test script can't be reliably
327 # traced, unless it is run with a Bash version supporting
328 # BASH_XTRACEFD (introduced in Bash v4.1).
330 # Perform this version check _after_ the test script was
331 # potentially re-executed with $TEST_SHELL_PATH for '--tee' or
332 # '--verbose-log', so the right shell is checked and the
333 # warning is issued only once.
334 if test -n "$BASH_VERSION" && eval '
335 test ${BASH_VERSINFO[0]} -gt 4 || {
336 test ${BASH_VERSINFO[0]} -eq 4 &&
337 test ${BASH_VERSINFO[1]} -ge 1
340 then
341 : Executed by a Bash version supporting BASH_XTRACEFD. Good.
342 else
343 echo >&2 "warning: ignoring -x; '$0' is untraceable without BASH_XTRACEFD"
344 trace=
347 if test -n "$trace" && test -z "$verbose_log"
348 then
349 STGIT_DEBUG_LEVEL=1
350 verbose=t
353 # Since bash 5.0, checkwinsize is enabled by default which does
354 # update the COLUMNS variable every time a non-builtin command
355 # completes, even for non-interactive shells.
356 # Disable that since we are aiming for repeatability.
357 test -n "$BASH_VERSION" && shopt -u checkwinsize 2>/dev/null
359 export STGIT_DEBUG_LEVEL
361 # For repeatability, reset the environment to known value.
362 # TERM is sanitized below, after saving color control sequences.
363 LANG=${STG_TEST_LANG:-C}
364 LC_ALL=$LANG
365 PAGER=cat
366 TZ=UTC
367 COLUMNS=80
368 export LANG LC_ALL PAGER TZ COLUMNS
369 EDITOR=:
371 # A call to "unset" with no arguments causes at least Solaris 10
372 # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
373 # deriving from the command substitution clustered with the other
374 # ones.
375 unset VISUAL EMAIL LANGUAGE $("$PERL_PATH" -e '
376 my @env = keys %ENV;
377 my $ok = join("|", qw(
378 TRACE
379 DEBUG
380 TEST
381 .*_TEST
382 PROVE
383 VALGRIND
384 UNZIP
385 PERF_
386 CURL_VERBOSE
387 TRACE_CURL
389 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
390 print join("\n", @vars);
392 unset XDG_CACHE_HOME
393 unset XDG_CONFIG_HOME
394 unset GITPERLLIB
395 TEST_AUTHOR_LOCALNAME=author
396 TEST_AUTHOR_DOMAIN=example.com
397 GIT_AUTHOR_EMAIL=${TEST_AUTHOR_LOCALNAME}@${TEST_AUTHOR_DOMAIN}
398 GIT_AUTHOR_NAME='A Ú Thor'
399 TEST_COMMITTER_LOCALNAME=committer
400 TEST_COMMITTER_DOMAIN=example.com
401 GIT_COMMITTER_EMAIL=${TEST_COMMITTER_LOCALNAME}@${TEST_COMMITTER_DOMAIN}
402 GIT_COMMITTER_NAME='C Ó Mitter'
403 GIT_MERGE_VERBOSITY=5
404 GIT_MERGE_AUTOEDIT=no
405 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
406 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
407 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
408 export EDITOR
410 GIT_DEFAULT_HASH="${GIT_TEST_DEFAULT_HASH:-sha1}"
411 export GIT_DEFAULT_HASH
412 GIT_TEST_MERGE_ALGORITHM="${GIT_TEST_MERGE_ALGORITHM:-ort}"
413 export GIT_TEST_MERGE_ALGORITHM
415 # Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
416 GIT_TRACE_BARE=1
417 export GIT_TRACE_BARE
419 # Use specific version of the index file format
420 if test -n "${GIT_TEST_INDEX_VERSION:+isset}"
421 then
422 GIT_INDEX_VERSION="$GIT_TEST_INDEX_VERSION"
423 export GIT_INDEX_VERSION
426 if test -n "$GIT_TEST_PERL_FATAL_WARNINGS"
427 then
428 GIT_PERL_FATAL_WARNINGS=1
429 export GIT_PERL_FATAL_WARNINGS
432 # Protect ourselves from common misconfiguration to export
433 # CDPATH into the environment
434 unset CDPATH
436 unset GREP_OPTIONS
437 unset UNZIP
439 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
440 1|2|true)
441 GIT_TRACE=4
443 esac
445 # Line feed
446 LF='
449 # Single quote
450 SQ=\'
452 # UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
453 # when case-folding filenames
454 u200c=$(printf '\342\200\214')
456 export _x05 _x35 LF u200c EMPTY_TREE EMPTY_BLOB ZERO_OID OID_REGEX
458 # Each test should start with something like this, after copyright notices:
460 # test_description='Description of this test...
461 # This test checks if command xyzzy does the right thing...
463 # . ./test-lib.sh
464 test "x$TERM" != "xdumb" && (
465 test -t 1 &&
466 tput bold >/dev/null 2>&1 &&
467 tput setaf 1 >/dev/null 2>&1 &&
468 tput sgr0 >/dev/null 2>&1
469 ) &&
470 color=t
472 if test -n "$color"
473 then
474 # Save the color control sequences now rather than run tput
475 # each time say_color() is called. This is done for two
476 # reasons:
477 # * TERM will be changed to dumb
478 # * HOME will be changed to a temporary directory and tput
479 # might need to read ~/.terminfo from the original HOME
480 # directory to get the control sequences
481 # Note: This approach assumes the control sequences don't end
482 # in a newline for any terminal of interest (command
483 # substitutions strip trailing newlines). Given that most
484 # (all?) terminals in common use are related to ECMA-48, this
485 # shouldn't be a problem.
486 say_color_error=$(tput bold; tput setaf 1) # bold red
487 say_color_skip=$(tput setaf 4) # blue
488 say_color_warn=$(tput setaf 3) # brown/yellow
489 say_color_pass=$(tput setaf 2) # green
490 say_color_info=$(tput setaf 6) # cyan
491 say_color_reset=$(tput sgr0)
492 say_color_="" # no formatting for normal text
493 say_color () {
494 test -z "$1" && test -n "$quiet" && return
495 eval "say_color_color=\$say_color_$1"
496 shift
497 printf "%s\\n" "$say_color_color$*$say_color_reset"
499 else
500 say_color() {
501 test -z "$1" && test -n "$quiet" && return
502 shift
503 printf "%s\n" "$*"
507 USER_TERM="$TERM"
508 TERM=dumb
509 export TERM USER_TERM
511 _error_exit () {
512 finalize_junit_xml
513 GIT_EXIT_OK=t
514 exit 1
517 error () {
518 say_color error "error: $*"
519 _error_exit
522 BUG () {
523 error >&7 "bug in the test script: $*"
526 BAIL_OUT () {
527 test $# -ne 1 && BUG "1 param"
529 # Do not change "Bail out! " string. It's part of TAP syntax:
530 # https://testanything.org/tap-specification.html
531 local bail_out="Bail out! "
532 local message="$1"
534 say_color error $bail_out "$message"
535 _error_exit
538 say () {
539 say_color info "$*"
542 if test -n "$HARNESS_ACTIVE"
543 then
544 if test "$verbose" = t || test -n "$verbose_only"
545 then
546 BAIL_OUT 'verbose mode forbidden under TAP harness; try --verbose-log'
550 test "${test_description}" != "" ||
551 error "Test script did not set test_description."
553 if test "$help" = "t"
554 then
555 printf '%s\n' "$test_description"
556 exit 0
559 exec 5>&1
560 exec 6<&0
561 exec 7>&2
562 if test "$verbose_log" = "t"
563 then
564 exec 3>>"$GIT_TEST_TEE_OUTPUT_FILE" 4>&3
565 elif test "$verbose" = "t"
566 then
567 exec 4>&2 3>&1
568 else
569 exec 4>/dev/null 3>/dev/null
572 # Send any "-x" output directly to stderr to avoid polluting tests
573 # which capture stderr. We can do this unconditionally since it
574 # has no effect if tracing isn't turned on.
576 # Note that this sets up the trace fd as soon as we assign the variable, so it
577 # must come after the creation of descriptor 4 above. Likewise, we must never
578 # unset this, as it has the side effect of closing descriptor 4, which we
579 # use to show verbose tests to the user.
581 # Note also that we don't need or want to export it. The tracing is local to
582 # this shell, and we would not want to influence any shells we exec.
583 BASH_XTRACEFD=4
585 test_failure=0
586 test_count=0
587 test_fixed=0
588 test_broken=0
589 test_success=0
591 test_external_has_tap=0
593 die () {
594 code=$?
595 # This is responsible for running the atexit commands even when a
596 # test script run with '--immediate' fails, or when the user hits
597 # ctrl-C, i.e. when 'test_done' is not invoked at all.
598 test_atexit_handler || code=$?
599 if test -n "$GIT_EXIT_OK"
600 then
601 exit $code
602 else
603 echo >&5 "FATAL: Unexpected exit with code $code"
604 exit 1
608 GIT_EXIT_OK=
609 trap 'die' EXIT
610 # Disable '-x' tracing, because with some shells, notably dash, it
611 # prevents running the cleanup commands when a test script run with
612 # '--verbose-log -x' is interrupted.
613 trap '{ code=$?; set +x; } 2>/dev/null; exit $code' INT TERM HUP
615 # The user-facing functions are loaded from a separate file so that
616 # test_perf subshells can have them too
617 . "$TEST_DIRECTORY/test-lib-functions.sh"
619 # You are not expected to call test_ok_ and test_failure_ directly, use
620 # the test_expect_* functions instead.
622 test_ok_ () {
623 if test -n "$write_junit_xml"
624 then
625 write_junit_xml_testcase "$*"
627 test_success=$(($test_success + 1))
628 say_color "" "ok $test_count - $@"
631 test_failure_ () {
632 if test -n "$write_junit_xml"
633 then
634 junit_insert="<failure message=\"not ok $test_count -"
635 junit_insert="$junit_insert $(xml_attr_encode "$1")\">"
636 junit_insert="$junit_insert $(xml_attr_encode \
637 "$(if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
638 then
639 test-tool path-utils skip-n-bytes \
640 "$GIT_TEST_TEE_OUTPUT_FILE" $GIT_TEST_TEE_OFFSET
641 else
642 printf '%s\n' "$@" | sed 1d
643 fi)")"
644 junit_insert="$junit_insert</failure>"
645 if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
646 then
647 junit_insert="$junit_insert<system-err>$(xml_attr_encode \
648 "$(cat "$GIT_TEST_TEE_OUTPUT_FILE")")</system-err>"
650 write_junit_xml_testcase "$1" " $junit_insert"
652 test_failure=$(($test_failure + 1))
653 say_color error "not ok $test_count - $1"
654 shift
655 printf '%s\n' "$*" | sed -e 's/^/# /'
656 test "$immediate" = "" || _error_exit
659 test_known_broken_ok_ () {
660 if test -n "$write_junit_xml"
661 then
662 write_junit_xml_testcase "$* (breakage fixed)"
664 test_fixed=$(($test_fixed+1))
665 say_color error "ok $test_count - $@ # TODO known breakage vanished"
668 test_known_broken_failure_ () {
669 if test -n "$write_junit_xml"
670 then
671 write_junit_xml_testcase "$* (known breakage)"
673 test_broken=$(($test_broken+1))
674 say_color warn "not ok $test_count - $@ # TODO known breakage"
677 test_debug () {
678 test "$debug" = "" || eval "$1"
681 match_pattern_list () {
682 arg="$1"
683 shift
684 test -z "$*" && return 1
685 # We need to use "$*" to get field-splitting, but we want to
686 # disable globbing, since we are matching against an arbitrary
687 # $arg, not what's in the filesystem. Using "set -f" accomplishes
688 # that, but we must do it in a subshell to avoid impacting the
689 # rest of the script. The exit value of the subshell becomes
690 # the function's return value.
692 set -f
693 for pattern_ in $*
695 case "$arg" in
696 $pattern_)
697 exit 0
699 esac
700 done
701 exit 1
705 match_test_selector_list () {
706 operation="$1"
707 shift
708 title="$1"
709 shift
710 arg="$1"
711 shift
712 test -z "$1" && return 0
714 # Commas are accepted as separators.
715 OLDIFS=$IFS
716 IFS=','
717 set -- $1
718 IFS=$OLDIFS
720 # If the first selector is negative we include by default.
721 include=
722 case "$1" in
723 !*) include=t ;;
724 esac
726 for selector
728 orig_selector=$selector
730 positive=t
731 case "$selector" in
733 positive=
734 selector=${selector##?}
736 esac
738 test -z "$selector" && continue
740 case "$selector" in
741 *-*)
742 if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
743 then
744 echo "error: $operation: invalid non-numeric in range" \
745 "start: '$orig_selector'" >&2
746 exit 1
748 if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
749 then
750 echo "error: $operation: invalid non-numeric in range" \
751 "end: '$orig_selector'" >&2
752 exit 1
756 if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
757 then
758 case "$title" in *${selector}*)
759 include=$positive
761 esac
762 continue
764 esac
766 # Short cut for "obvious" cases
767 test -z "$include" && test -z "$positive" && continue
768 test -n "$include" && test -n "$positive" && continue
770 case "$selector" in
772 if test $arg -le ${selector#-}
773 then
774 include=$positive
778 if test $arg -ge ${selector%-}
779 then
780 include=$positive
783 *-*)
784 if test ${selector%%-*} -le $arg \
785 && test $arg -le ${selector#*-}
786 then
787 include=$positive
791 if test $arg -eq $selector
792 then
793 include=$positive
796 esac
797 done
799 test -n "$include"
802 maybe_teardown_verbose () {
803 test -z "$verbose_only" && return
804 exec 4>/dev/null 3>/dev/null
805 verbose=
808 last_verbose=t
809 maybe_setup_verbose () {
810 test -z "$verbose_only" && return
811 if match_pattern_list $test_count "$verbose_only"
812 then
813 exec 4>&2 3>&1
814 # Emit a delimiting blank line when going from
815 # non-verbose to verbose. Within verbose mode the
816 # delimiter is printed by test_expect_*. The choice
817 # of the initial $last_verbose is such that before
818 # test 1, we do not print it.
819 test -z "$last_verbose" && echo >&3 ""
820 verbose=t
821 else
822 exec 4>/dev/null 3>/dev/null
823 verbose=
825 last_verbose=$verbose
828 trace_level_=0
829 want_trace () {
830 test "$trace" = t && {
831 test "$verbose" = t || test "$verbose_log" = t
835 # This is a separate function because some tests use
836 # "return" to end a test_expect_success block early
837 # (and we want to make sure we run any cleanup like
838 # "set +x").
839 test_eval_inner_ () {
840 # Do not add anything extra (including LF) after '$*'
841 eval "
842 want_trace && trace_level_=$(($trace_level_+1)) && set -x
846 test_eval_ () {
847 # If "-x" tracing is in effect, then we want to avoid polluting stderr
848 # with non-test commands. But once in "set -x" mode, we cannot prevent
849 # the shell from printing the "set +x" to turn it off (nor the saving
850 # of $? before that). But we can make sure that the output goes to
851 # /dev/null.
853 # There are a few subtleties here:
855 # - we have to redirect descriptor 4 in addition to 2, to cover
856 # BASH_XTRACEFD
858 # - the actual eval has to come before the redirection block (since
859 # it needs to see descriptor 4 to set up its stderr)
861 # - likewise, any error message we print must be outside the block to
862 # access descriptor 4
864 # - checking $? has to come immediately after the eval, but it must
865 # be _inside_ the block to avoid polluting the "set -x" output
868 test_eval_inner_ "$@" </dev/null >&3 2>&4
870 test_eval_ret_=$?
871 if want_trace
872 then
873 test 1 = $trace_level_ && set +x
874 trace_level_=$(($trace_level_-1))
876 } 2>/dev/null 4>&2
878 if test "$test_eval_ret_" != 0 && want_trace
879 then
880 say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
882 return $test_eval_ret_
885 test_run_ () {
886 test_cleanup=:
887 expecting_failure=$2
889 test_eval_ "$1"
890 eval_ret=$?
892 if test -z "$immediate" || test $eval_ret = 0 ||
893 test -n "$expecting_failure" && test "$test_cleanup" != ":"
894 then
895 test_eval_ "$test_cleanup"
897 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
898 then
899 echo ""
901 return "$eval_ret"
904 test_start_ () {
905 test_count=$(($test_count+1))
906 maybe_setup_verbose
907 if test -n "$write_junit_xml"
908 then
909 junit_start=$(test-tool date getnanos)
913 test_finish_ () {
914 echo >&3 ""
915 maybe_teardown_verbose
916 if test -n "$GIT_TEST_TEE_OFFSET"
917 then
918 GIT_TEST_TEE_OFFSET=$(test-tool path-utils file-size \
919 "$GIT_TEST_TEE_OUTPUT_FILE")
923 test_skip () {
924 to_skip=
925 skipped_reason=
926 if match_pattern_list $this_test.$test_count $STG_SKIP_TESTS
927 then
928 to_skip=t
929 skipped_reason="STG_SKIP_TESTS"
931 if test -z "$to_skip" && test -n "$run_list" &&
932 ! match_test_selector_list '--run' "$1" $test_count "$run_list"
933 then
934 to_skip=t
935 skipped_reason="--run"
937 if test -z "$to_skip" && test -n "$test_prereq" &&
938 ! test_have_prereq "$test_prereq"
939 then
940 to_skip=t
942 of_prereq=
943 if test "$missing_prereq" != "$test_prereq"
944 then
945 of_prereq=" of $test_prereq"
947 skipped_reason="missing $missing_prereq${of_prereq}"
950 case "$to_skip" in
952 if test -n "$write_junit_xml"
953 then
954 message="$(xml_attr_encode "$skipped_reason")"
955 write_junit_xml_testcase "$1" \
956 " <skipped message=\"$message\" />"
959 say_color skip "ok $test_count # skip $1 ($skipped_reason)"
960 : true
963 false
965 esac
968 # stub; perf-lib overrides it
969 test_at_end_hook_ () {
973 write_junit_xml () {
974 case "$1" in
975 --truncate)
976 >"$junit_xml_path"
977 junit_have_testcase=
978 shift
980 esac
981 printf '%s\n' "$@" >>"$junit_xml_path"
984 xml_attr_encode () {
985 printf '%s\n' "$@" | test-tool xml-encode
988 write_junit_xml_testcase () {
989 junit_attrs="name=\"$(xml_attr_encode "$this_test.$test_count $1")\""
990 shift
991 junit_attrs="$junit_attrs classname=\"$this_test\""
992 junit_attrs="$junit_attrs time=\"$(test-tool \
993 date getnanos $junit_start)\""
994 write_junit_xml "$(printf '%s\n' \
995 " <testcase $junit_attrs>" "$@" " </testcase>")"
996 junit_have_testcase=t
999 finalize_junit_xml () {
1000 if test -n "$write_junit_xml" && test -n "$junit_xml_path"
1001 then
1002 test -n "$junit_have_testcase" || {
1003 junit_start=$(test-tool date getnanos)
1004 write_junit_xml_testcase "all tests skipped"
1007 # adjust the overall time
1008 junit_time=$(test-tool date getnanos $junit_suite_start)
1009 sed -e "s/\(<testsuite.*\) time=\"[^\"]*\"/\1/" \
1010 -e "s/<testsuite [^>]*/& time=\"$junit_time\"/" \
1011 -e '/^ *<\/testsuite/d' \
1012 <"$junit_xml_path" >"$junit_xml_path.new"
1013 mv "$junit_xml_path.new" "$junit_xml_path"
1015 write_junit_xml " </testsuite>" "</testsuites>"
1016 write_junit_xml=
1020 test_atexit_cleanup=:
1021 test_atexit_handler () {
1022 # In a succeeding test script 'test_atexit_handler' is invoked
1023 # twice: first from 'test_done', then from 'die' in the trap on
1024 # EXIT.
1025 # This condition and resetting 'test_atexit_cleanup' below makes
1026 # sure that the registered cleanup commands are run only once.
1027 test : != "$test_atexit_cleanup" || return 0
1029 test_eval_ "$test_atexit_cleanup"
1030 test_atexit_cleanup=:
1033 test_done () {
1034 GIT_EXIT_OK=t
1036 # Run the atexit commands _before_ the trash directory is
1037 # removed, so the commands can access pidfiles and socket files.
1038 test_atexit_handler
1040 finalize_junit_xml
1042 if test -z "$HARNESS_ACTIVE"
1043 then
1044 mkdir -p "$TEST_RESULTS_DIR"
1046 cat >"$TEST_RESULTS_BASE.counts" <<-EOF
1047 total $test_count
1048 success $test_success
1049 fixed $test_fixed
1050 broken $test_broken
1051 failed $test_failure
1056 if test "$test_fixed" != 0
1057 then
1058 say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
1060 if test "$test_broken" != 0
1061 then
1062 say_color warn "# still have $test_broken known breakage(s)"
1064 if test "$test_broken" != 0 || test "$test_fixed" != 0
1065 then
1066 test_remaining=$(( $test_count - $test_broken - $test_fixed ))
1067 msg="remaining $test_remaining test(s)"
1068 else
1069 test_remaining=$test_count
1070 msg="$test_count test(s)"
1072 case "$test_failure" in
1074 if test $test_external_has_tap -eq 0
1075 then
1076 if test $test_remaining -gt 0
1077 then
1078 say_color pass "# passed all $msg"
1081 # Maybe print SKIP message
1082 test -z "$skip_all" || skip_all="# SKIP $skip_all"
1083 case "$test_count" in
1085 say "1..$test_count${skip_all:+ $skip_all}"
1088 test -z "$skip_all" ||
1089 say_color warn "$skip_all"
1090 say "1..$test_count"
1092 esac
1095 if test -z "$debug" && test -n "$remove_trash"
1096 then
1097 test -d "$TRASH_DIRECTORY" ||
1098 error "Tests passed but trash directory already removed before test cleanup; aborting"
1100 cd "$TRASH_DIRECTORY/.." &&
1101 rm -fr "$TRASH_DIRECTORY" || {
1102 # try again in a bit
1103 sleep 5;
1104 rm -fr "$TRASH_DIRECTORY"
1105 } ||
1106 error "Tests passed but test cleanup failed; aborting"
1108 test_at_end_hook_
1110 exit 0 ;;
1113 if test $test_external_has_tap -eq 0
1114 then
1115 say_color error "# failed $test_failure among $msg"
1116 say "1..$test_count"
1119 exit 1 ;;
1121 esac
1124 if test -n "$STG_TEST_INSTALLED"
1125 then
1126 PATH=$STG_TEST_INSTALLED:$PATH
1127 else
1128 PATH="$TEST_DIRECTORY/test-bin:$PATH"
1129 PYTHONPATH="$STG_ROOT":"$PYTHONPATH"
1131 GIT_EXEC_PATH=$(git --exec-path) || error "Cannot run git"
1132 unset GIT_TEMPLATE_DIR
1133 GIT_CONFIG_NOSYSTEM=1
1134 GIT_ATTR_NOSYSTEM=1
1135 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/.."
1136 export PATH GIT_EXEC_PATH GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM GIT_CEILING_DIRECTORIES PYTHONPATH
1138 if test -z "$GIT_TEST_CMP"
1139 then
1140 DIFF="${DIFF:-diff}"
1141 if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
1142 then
1143 GIT_TEST_CMP="$DIFF -c"
1144 else
1145 GIT_TEST_CMP="$DIFF -u"
1149 # Are we running this test at all?
1150 remove_trash=
1151 this_test=${0##*/}
1152 this_test=${this_test%%-*}
1153 if match_pattern_list "$this_test" "$STG_SKIP_TESTS"
1154 then
1155 say_color info >&3 "skipping test $this_test altogether"
1156 skip_all="skip all tests in $this_test"
1157 test_done
1160 # skip non-whitelisted tests when compiled with SANITIZE=leak
1161 if test -n "$SANITIZE_LEAK"
1162 then
1163 if test_bool_env GIT_TEST_PASSING_SANITIZE_LEAK false
1164 then
1165 # We need to see it in "git env--helper" (via
1166 # test_bool_env)
1167 export TEST_PASSES_SANITIZE_LEAK
1169 if ! test_bool_env TEST_PASSES_SANITIZE_LEAK false
1170 then
1171 skip_all="skipping $this_test under GIT_TEST_PASSING_SANITIZE_LEAK=true"
1172 test_done
1175 elif test_bool_env GIT_TEST_PASSING_SANITIZE_LEAK false
1176 then
1177 BAIL_OUT "GIT_TEST_PASSING_SANITIZE_LEAK=true has no effect except when compiled with SANITIZE=leak"
1180 # Last-minute variable setup
1181 USER_HOME="$HOME"
1182 HOME="$TRASH_DIRECTORY"
1183 GNUPGHOME="$HOME/gnupg-home-not-used"
1184 export HOME GNUPGHOME USER_HOME
1186 # "rm -rf" existing trash directory, even if a previous run left it
1187 # with bad permissions.
1188 remove_trash_directory () {
1189 dir="$1"
1190 if ! rm -rf "$dir" 2>/dev/null
1191 then
1192 chmod -R u+rwx "$dir"
1193 rm -rf "$dir"
1195 ! test -d "$dir"
1198 # Test repository
1199 remove_trash_directory "$TRASH_DIRECTORY" || {
1200 GIT_EXIT_OK=t
1201 echo >&5 "FATAL: Cannot prepare test area"
1202 exit 1
1205 remove_trash=t
1206 if test -z "$TEST_NO_CREATE_REPO"
1207 then
1208 test_create_repo "$TRASH_DIRECTORY"
1209 else
1210 mkdir -p "$TRASH_DIRECTORY"
1213 # Use -P to resolve symlinks in our working directory so that the cwd
1214 # in subprocesses like git equals our $PWD (for pathname comparisons).
1215 cd -P "$TRASH_DIRECTORY" || exit 1
1217 if test -n "$write_junit_xml"
1218 then
1219 junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
1220 mkdir -p "$junit_xml_dir"
1221 junit_xml_base=${0##*/}
1222 junit_xml_path="$junit_xml_dir/TEST-${junit_xml_base%.sh}.xml"
1223 junit_attrs="name=\"${junit_xml_base%.sh}\""
1224 junit_attrs="$junit_attrs timestamp=\"$(TZ=UTC \
1225 date +%Y-%m-%dT%H:%M:%S)\""
1226 write_junit_xml --truncate "<testsuites>" " <testsuite $junit_attrs>"
1227 junit_suite_start=$(test-tool date getnanos)
1228 if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
1229 then
1230 GIT_TEST_TEE_OFFSET=0
1234 # Convenience
1235 # A regexp to match 5 and 35 hexdigits
1236 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
1237 _x35="$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
1239 test_oid_init
1241 ZERO_OID=$(test_oid zero)
1242 OID_REGEX=$(echo $ZERO_OID | sed -e 's/0/[0-9a-f]/g')
1243 OIDPATH_REGEX=$(test_oid_to_path $ZERO_OID | sed -e 's/0/[0-9a-f]/g')
1244 EMPTY_TREE=$(test_oid empty_tree)
1245 EMPTY_BLOB=$(test_oid empty_blob)
1247 # Provide an implementation of the 'yes' utility; the upper bound
1248 # limit is there to help Windows that cannot stop this loop from
1249 # wasting cycles when the downstream stops reading, so do not be
1250 # tempted to turn it into an infinite loop. cf. 6129c930 ("test-lib:
1251 # limit the output of the yes utility", 2016-02-02)
1252 yes () {
1253 if test $# = 0
1254 then
1256 else
1257 y="$*"
1261 while test $i -lt 99
1263 echo "$y"
1264 i=$(($i+1))
1265 done
1268 # The GIT_TEST_FAIL_PREREQS code hooks into test_set_prereq(), and
1269 # thus needs to be set up really early, and set an internal variable
1270 # for convenience so the hot test_set_prereq() codepath doesn't need
1271 # to call "git env--helper" (via test_bool_env). Only do that work
1272 # if needed by seeing if GIT_TEST_FAIL_PREREQS is set at all.
1273 GIT_TEST_FAIL_PREREQS_INTERNAL=
1274 if test -n "$GIT_TEST_FAIL_PREREQS"
1275 then
1276 if test_bool_env GIT_TEST_FAIL_PREREQS false
1277 then
1278 GIT_TEST_FAIL_PREREQS_INTERNAL=true
1279 test_set_prereq FAIL_PREREQS
1281 else
1282 test_lazy_prereq FAIL_PREREQS '
1283 test_bool_env GIT_TEST_FAIL_PREREQS false
1287 # Fix some commands on Windows, and other OS-specific things
1288 uname_s=$(uname -s)
1289 case $uname_s in
1290 *MINGW*)
1291 # Windows has its own (incompatible) sort and find
1292 sort () {
1293 /usr/bin/sort "$@"
1295 find () {
1296 /usr/bin/find "$@"
1298 # git sees Windows-style pwd
1299 pwd () {
1300 builtin pwd -W
1302 # no POSIX permissions
1303 # backslashes in pathspec are converted to '/'
1304 # exec does not inherit the PID
1305 test_set_prereq MINGW
1306 test_set_prereq NATIVE_CRLF
1307 test_set_prereq SED_STRIPS_CR
1308 test_set_prereq GREP_STRIPS_CR
1309 test_set_prereq WINDOWS
1310 GIT_TEST_CMP=mingw_test_cmp
1312 *CYGWIN*)
1313 test_set_prereq POSIXPERM
1314 test_set_prereq EXECKEEPSPID
1315 test_set_prereq CYGWIN
1316 test_set_prereq SED_STRIPS_CR
1317 test_set_prereq GREP_STRIPS_CR
1318 test_set_prereq WINDOWS
1321 test_set_prereq POSIXPERM
1322 test_set_prereq BSLASHPSPEC
1323 test_set_prereq EXECKEEPSPID
1325 esac
1327 test_lazy_prereq QUILT '
1328 # test whether quilt is installed
1329 quilt --version 2>/dev/null >/dev/null