]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - t/test-lib.sh
The fifth batch
[thirdparty/git.git] / t / test-lib.sh
... / ...
CommitLineData
1# Test framework for git. See t/README for usage.
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see https://www.gnu.org/licenses/ .
17
18# Test the binaries we have just built. The tests are kept in
19# t/ subdirectory and are run in 'trash directory' subdirectory.
20if test -z "$TEST_DIRECTORY"
21then
22 # ensure that TEST_DIRECTORY is an absolute path so that it
23 # is valid even if the current working directory is changed
24 TEST_DIRECTORY=$(pwd)
25else
26 # The TEST_DIRECTORY will always be the path to the "t"
27 # directory in the git.git checkout. This is overridden by
28 # e.g. t/lib-subtest.sh, but only because its $(pwd) is
29 # different. Those tests still set "$TEST_DIRECTORY" to the
30 # same path.
31 #
32 # See use of "$GIT_BUILD_DIR" and "$TEST_DIRECTORY" below for
33 # hard assumptions about "$GIT_BUILD_DIR/t" existing and being
34 # the "$TEST_DIRECTORY", and e.g. "$TEST_DIRECTORY/helper"
35 # needing to exist.
36 TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
37fi
38GIT_BUILD_DIR="${GIT_BUILD_DIR:-${TEST_DIRECTORY%/t}}"
39if test "$TEST_DIRECTORY" = "$GIT_BUILD_DIR"
40then
41 echo "PANIC: Running in a $TEST_DIRECTORY that doesn't end in '/t'?" >&2
42 exit 1
43fi
44if test -f "$GIT_BUILD_DIR/GIT-BUILD-DIR"
45then
46 GIT_BUILD_DIR="$(cat "$GIT_BUILD_DIR/GIT-BUILD-DIR")" || exit 1
47 # On Windows, we must convert Windows paths lest they contain a colon
48 case "$(uname -s)" in
49 *MINGW*)
50 GIT_BUILD_DIR="$(cygpath -au "$GIT_BUILD_DIR")"
51 ;;
52 esac
53fi
54
55# Prepend a string to a VAR using an arbitrary ":" delimiter, not
56# adding the delimiter if VAR or VALUE is empty. I.e. a generalized:
57#
58# VAR=$1${VAR:+${1:+$2}$VAR}
59#
60# Usage (using ":" as the $2 delimiter):
61#
62# prepend_var VAR : VALUE
63prepend_var () {
64 eval "$1=\"$3\${$1:+${3:+$2}\$$1}\""
65}
66
67# If [AL]SAN is in effect we want to abort so that we notice
68# problems. The GIT_SAN_OPTIONS variable can be used to set common
69# defaults shared between [AL]SAN_OPTIONS.
70prepend_var GIT_SAN_OPTIONS : abort_on_error=1
71prepend_var GIT_SAN_OPTIONS : strip_path_prefix="$GIT_BUILD_DIR/"
72
73# If we were built with ASAN, it may complain about leaks
74# of program-lifetime variables. Disable it by default to lower
75# the noise level. This needs to happen at the start of the script,
76# before we even do our "did we build git yet" check (since we don't
77# want that one to complain to stderr).
78prepend_var ASAN_OPTIONS : $GIT_SAN_OPTIONS
79prepend_var ASAN_OPTIONS : detect_leaks=0
80export ASAN_OPTIONS
81
82prepend_var LSAN_OPTIONS : $GIT_SAN_OPTIONS
83prepend_var LSAN_OPTIONS : exitcode=0
84prepend_var LSAN_OPTIONS : fast_unwind_on_malloc=0
85export LSAN_OPTIONS
86
87prepend_var UBSAN_OPTIONS : $GIT_SAN_OPTIONS
88export UBSAN_OPTIONS
89
90# The TEST_OUTPUT_DIRECTORY will be overwritten via GIT-BUILD-OPTIONS. So in
91# case the caller has manually set up this variable via the environment we must
92# make sure to not overwrite that value, and thus we save it into
93# TEST_OUTPUT_DIRECTORY_OVERRIDE here.
94if test -n "$TEST_OUTPUT_DIRECTORY" && test -z "$TEST_OUTPUT_DIRECTORY_OVERRIDE"
95then
96 TEST_OUTPUT_DIRECTORY_OVERRIDE=$TEST_OUTPUT_DIRECTORY
97fi
98
99if test ! -f "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
100then
101 echo >&2 'error: GIT-BUILD-OPTIONS missing (has Git been built?).'
102 exit 1
103fi
104. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
105export PERL_PATH SHELL_PATH
106
107if test -z "$TEST_OUTPUT_DIRECTORY"
108then
109 # Similarly, override this to store the test-results subdir
110 # elsewhere
111 TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
112fi
113
114# In t0000, we need to override test directories of nested testcases. In case
115# the developer has TEST_OUTPUT_DIRECTORY part of his build options, then we'd
116# reset this value to instead contain what the developer has specified. We thus
117# have this knob to allow overriding the directory.
118if test -n "${TEST_OUTPUT_DIRECTORY_OVERRIDE}"
119then
120 TEST_OUTPUT_DIRECTORY="${TEST_OUTPUT_DIRECTORY_OVERRIDE}"
121fi
122
123# Disallow the use of abbreviated options in the test suite by default
124if test -z "${GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS}"
125then
126 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true
127 export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
128fi
129
130# Explicitly set the default branch name for testing, to avoid the
131# transitory "git init" warning under --verbose.
132: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
133export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
134
135################################################################
136# It appears that people try to run tests without building...
137"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null
138if test $? != 1
139then
140 if test -n "$GIT_TEST_INSTALLED"
141 then
142 echo >&2 "error: there is no working Git at '$GIT_TEST_INSTALLED'"
143 else
144 echo >&2 'error: you do not seem to have built git yet.'
145 fi
146 exit 1
147fi
148
149store_arg_to=
150opt_required_arg=
151# $1: option string
152# $2: name of the var where the arg will be stored
153mark_option_requires_arg () {
154 if test -n "$opt_required_arg"
155 then
156 echo "error: options that require args cannot be bundled" \
157 "together: '$opt_required_arg' and '$1'" >&2
158 exit 1
159 fi
160 opt_required_arg=$1
161 store_arg_to=$2
162}
163
164# These functions can be overridden e.g. to output JUnit XML
165start_test_output () { :; }
166start_test_case_output () { :; }
167finalize_test_case_output () { :; }
168finalize_test_output () { :; }
169
170parse_option () {
171 local opt="$1"
172
173 case "$opt" in
174 -d|--d|--de|--deb|--debu|--debug)
175 debug=t ;;
176 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
177 immediate=t ;;
178 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
179 GIT_TEST_LONG=t; export GIT_TEST_LONG ;;
180 -r)
181 mark_option_requires_arg "$opt" run_list
182 ;;
183 --run=*)
184 run_list=${opt#--*=} ;;
185 -h|--h|--he|--hel|--help)
186 help=t ;;
187 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
188 verbose=t ;;
189 --verbose-only=*)
190 verbose_only=${opt#--*=}
191 ;;
192 -q|--q|--qu|--qui|--quie|--quiet)
193 # Ignore --quiet under a TAP::Harness. Saying how many tests
194 # passed without the ok/not ok details is always an error.
195 test -z "$HARNESS_ACTIVE" && quiet=t ;;
196 --with-dashes)
197 with_dashes=t ;;
198 --no-bin-wrappers)
199 no_bin_wrappers=t ;;
200 --no-color)
201 color= ;;
202 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
203 valgrind=memcheck
204 tee=t
205 ;;
206 --valgrind=*)
207 valgrind=${opt#--*=}
208 tee=t
209 ;;
210 --valgrind-only=*)
211 valgrind_only=${opt#--*=}
212 tee=t
213 ;;
214 --tee)
215 tee=t ;;
216 --root=*)
217 root=${opt#--*=} ;;
218 --chain-lint)
219 GIT_TEST_CHAIN_LINT=1 ;;
220 --no-chain-lint)
221 GIT_TEST_CHAIN_LINT=0 ;;
222 -x)
223 trace=t ;;
224 -V|--verbose-log)
225 verbose_log=t
226 tee=t
227 ;;
228 --write-junit-xml)
229 . "$TEST_DIRECTORY/test-lib-junit.sh"
230 ;;
231 --github-workflow-markup)
232 . "$TEST_DIRECTORY/test-lib-github-workflow-markup.sh"
233 ;;
234 --stress)
235 stress=t ;;
236 --stress=*)
237 echo "error: --stress does not accept an argument: '$opt'" >&2
238 echo "did you mean --stress-jobs=${opt#*=} or --stress-limit=${opt#*=}?" >&2
239 exit 1
240 ;;
241 --stress-jobs=*)
242 stress=t;
243 stress_jobs=${opt#--*=}
244 case "$stress_jobs" in
245 *[!0-9]*|0*|"")
246 echo "error: --stress-jobs=<N> requires the number of jobs to run" >&2
247 exit 1
248 ;;
249 *) # Good.
250 ;;
251 esac
252 ;;
253 --stress-limit=*)
254 stress=t;
255 stress_limit=${opt#--*=}
256 case "$stress_limit" in
257 *[!0-9]*|0*|"")
258 echo "error: --stress-limit=<N> requires the number of repetitions" >&2
259 exit 1
260 ;;
261 *) # Good.
262 ;;
263 esac
264 ;;
265 --invert-exit-code)
266 invert_exit_code=t
267 ;;
268 *)
269 echo "error: unknown test option '$opt'" >&2; exit 1 ;;
270 esac
271}
272
273# Parse options while taking care to leave $@ intact, so we will still
274# have all the original command line options when executing the test
275# script again for '--tee' and '--verbose-log' later.
276for opt
277do
278 if test -n "$store_arg_to"
279 then
280 eval $store_arg_to=\$opt
281 store_arg_to=
282 opt_required_arg=
283 continue
284 fi
285
286 case "$opt" in
287 --*|-?)
288 parse_option "$opt" ;;
289 -?*)
290 # bundled short options must be fed separately to parse_option
291 opt=${opt#-}
292 while test -n "$opt"
293 do
294 extra=${opt#?}
295 this=${opt%$extra}
296 opt=$extra
297 parse_option "-$this"
298 done
299 ;;
300 *)
301 echo "error: unknown test option '$opt'" >&2; exit 1 ;;
302 esac
303done
304if test -n "$store_arg_to"
305then
306 echo "error: $opt_required_arg requires an argument" >&2
307 exit 1
308fi
309
310if test -n "$valgrind_only"
311then
312 test -z "$valgrind" && valgrind=memcheck
313 test -z "$verbose" && verbose_only="$valgrind_only"
314elif test -n "$valgrind"
315then
316 test -z "$verbose_log" && verbose=t
317fi
318
319if test -n "$stress"
320then
321 verbose=t
322 trace=t
323 immediate=t
324fi
325
326TEST_STRESS_JOB_SFX="${GIT_TEST_STRESS_JOB_NR:+.stress-$GIT_TEST_STRESS_JOB_NR}"
327TEST_NAME="$(basename "$0" .sh)"
328TEST_NUMBER="${TEST_NAME%%-*}"
329TEST_NUMBER="${TEST_NUMBER#t}"
330TEST_RESULTS_DIR="$TEST_OUTPUT_DIRECTORY/test-results"
331TEST_RESULTS_BASE="$TEST_RESULTS_DIR/$TEST_NAME$TEST_STRESS_JOB_SFX"
332TEST_RESULTS_SAN_FILE_PFX=trace
333TEST_RESULTS_SAN_DIR_SFX=leak
334TEST_RESULTS_SAN_FILE=
335TEST_RESULTS_SAN_DIR="$TEST_RESULTS_BASE.$TEST_RESULTS_SAN_DIR_SFX"
336TRASH_DIRECTORY="trash directory.$TEST_NAME$TEST_STRESS_JOB_SFX"
337test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
338case "$TRASH_DIRECTORY" in
339/*) ;; # absolute path is good
340 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
341esac
342
343# If --stress was passed, run this test repeatedly in several parallel loops.
344if test "$GIT_TEST_STRESS_STARTED" = "done"
345then
346 : # Don't stress test again.
347elif test -n "$stress"
348then
349 if test -n "$stress_jobs"
350 then
351 job_count=$stress_jobs
352 elif test -n "$GIT_TEST_STRESS_LOAD"
353 then
354 job_count="$GIT_TEST_STRESS_LOAD"
355 elif job_count=$(getconf _NPROCESSORS_ONLN 2>/dev/null) &&
356 test -n "$job_count"
357 then
358 job_count=$((2 * $job_count))
359 else
360 job_count=8
361 fi
362
363 mkdir -p "$TEST_RESULTS_DIR"
364 stressfail="$TEST_RESULTS_BASE.stress-failed"
365 rm -f "$stressfail"
366
367 stress_exit=0
368 trap '
369 kill $job_pids 2>/dev/null
370 wait
371 stress_exit=1
372 ' TERM INT HUP
373
374 job_pids=
375 job_nr=0
376 while test $job_nr -lt "$job_count"
377 do
378 (
379 GIT_TEST_STRESS_STARTED=done
380 GIT_TEST_STRESS_JOB_NR=$job_nr
381 export GIT_TEST_STRESS_STARTED GIT_TEST_STRESS_JOB_NR
382
383 trap '
384 kill $test_pid 2>/dev/null
385 wait
386 exit 1
387 ' TERM INT
388
389 cnt=1
390 while ! test -e "$stressfail" &&
391 { test -z "$stress_limit" ||
392 test $cnt -le $stress_limit ; }
393 do
394 $TEST_SHELL_PATH "$0" "$@" >"$TEST_RESULTS_BASE.stress-$job_nr.out" 2>&1 &
395 test_pid=$!
396
397 if wait $test_pid
398 then
399 printf "OK %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
400 else
401 echo $GIT_TEST_STRESS_JOB_NR >>"$stressfail"
402 printf "FAIL %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
403 fi
404 cnt=$(($cnt + 1))
405 done
406 ) &
407 job_pids="$job_pids $!"
408 job_nr=$(($job_nr + 1))
409 done
410
411 wait
412
413 if test -f "$stressfail"
414 then
415 stress_exit=1
416 echo "Log(s) of failed test run(s):"
417 for failed_job_nr in $(sort -n "$stressfail")
418 do
419 echo "Contents of '$TEST_RESULTS_BASE.stress-$failed_job_nr.out':"
420 cat "$TEST_RESULTS_BASE.stress-$failed_job_nr.out"
421 done
422 rm -rf "$TRASH_DIRECTORY.stress-failed"
423 # Move the last one.
424 mv "$TRASH_DIRECTORY.stress-$failed_job_nr" "$TRASH_DIRECTORY.stress-failed"
425 fi
426
427 exit $stress_exit
428fi
429
430# if --tee was passed, write the output not only to the terminal, but
431# additionally to the file test-results/$BASENAME.out, too.
432if test "$GIT_TEST_TEE_STARTED" = "done"
433then
434 : # do not redirect again
435elif test -n "$tee"
436then
437 mkdir -p "$TEST_RESULTS_DIR"
438
439 # Make this filename available to the sub-process in case it is using
440 # --verbose-log.
441 GIT_TEST_TEE_OUTPUT_FILE=$TEST_RESULTS_BASE.out
442 export GIT_TEST_TEE_OUTPUT_FILE
443
444 # Truncate before calling "tee -a" to get rid of the results
445 # from any previous runs.
446 >"$GIT_TEST_TEE_OUTPUT_FILE"
447
448 (GIT_TEST_TEE_STARTED=done ${TEST_SHELL_PATH} "$0" "$@" 2>&1;
449 echo $? >"$TEST_RESULTS_BASE.exit") | tee -a "$GIT_TEST_TEE_OUTPUT_FILE"
450 test "$(cat "$TEST_RESULTS_BASE.exit")" = 0
451 exit
452fi
453
454if test -n "$trace" && test -n "$test_untraceable"
455then
456 # '-x' tracing requested, but this test script can't be reliably
457 # traced, unless it is run with a Bash version supporting
458 # BASH_XTRACEFD (introduced in Bash v4.1).
459 #
460 # Perform this version check _after_ the test script was
461 # potentially re-executed with $TEST_SHELL_PATH for '--tee' or
462 # '--verbose-log', so the right shell is checked and the
463 # warning is issued only once.
464 if test -n "$BASH_VERSION" && eval '
465 test ${BASH_VERSINFO[0]} -gt 4 || {
466 test ${BASH_VERSINFO[0]} -eq 4 &&
467 test ${BASH_VERSINFO[1]} -ge 1
468 }
469 '
470 then
471 : Executed by a Bash version supporting BASH_XTRACEFD. Good.
472 else
473 echo >&2 "# warning: ignoring -x; '$0' is untraceable without BASH_XTRACEFD"
474 trace=
475 fi
476fi
477if test -n "$trace" && test -z "$verbose_log"
478then
479 verbose=t
480fi
481
482# Since bash 5.0, checkwinsize is enabled by default which does
483# update the COLUMNS variable every time a non-builtin command
484# completes, even for non-interactive shells.
485# Disable that since we are aiming for repeatability.
486test -n "$BASH_VERSION" && shopt -u checkwinsize 2>/dev/null
487
488# For repeatability, reset the environment to known value.
489# TERM is sanitized below, after saving color control sequences.
490LANG=C
491LC_ALL=C
492PAGER=cat
493TZ=UTC
494COLUMNS=80
495export LANG LC_ALL PAGER TZ COLUMNS
496EDITOR=:
497
498# A call to "unset" with no arguments causes at least Solaris 10
499# /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
500# deriving from the command substitution clustered with the other
501# ones.
502unset VISUAL EMAIL LANGUAGE $(env | sed -n \
503 -e '/^GIT_TRACE/d' \
504 -e '/^GIT_DEBUG/d' \
505 -e '/^GIT_TEST/d' \
506 -e '/^GIT_.*_TEST/d' \
507 -e '/^GIT_PROVE/d' \
508 -e '/^GIT_VALGRIND/d' \
509 -e '/^GIT_UNZIP/d' \
510 -e '/^GIT_PERF_/d' \
511 -e '/^GIT_CURL_VERBOSE/d' \
512 -e '/^GIT_TRACE_CURL/d' \
513 -e '/^GIT_BUILD_DIR/d' \
514 -e 's/^\(GIT_[^=]*\)=.*/\1/p'
515)
516unset XDG_CACHE_HOME
517unset XDG_CONFIG_HOME
518unset GITPERLLIB
519unset GIT_TRACE2_PARENT_NAME
520unset GIT_TRACE2_PARENT_SID
521TEST_AUTHOR_LOCALNAME=author
522TEST_AUTHOR_DOMAIN=example.com
523GIT_AUTHOR_EMAIL=${TEST_AUTHOR_LOCALNAME}@${TEST_AUTHOR_DOMAIN}
524GIT_AUTHOR_NAME='A U Thor'
525GIT_AUTHOR_DATE='1112354055 +0200'
526TEST_COMMITTER_LOCALNAME=committer
527TEST_COMMITTER_DOMAIN=example.com
528GIT_COMMITTER_EMAIL=${TEST_COMMITTER_LOCALNAME}@${TEST_COMMITTER_DOMAIN}
529GIT_COMMITTER_NAME='C O Mitter'
530GIT_COMMITTER_DATE='1112354055 +0200'
531GIT_MERGE_VERBOSITY=5
532GIT_MERGE_AUTOEDIT=no
533export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
534export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
535export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
536export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
537export EDITOR
538
539GIT_DEFAULT_HASH="${GIT_TEST_DEFAULT_HASH:-sha1}"
540export GIT_DEFAULT_HASH
541GIT_DEFAULT_REF_FORMAT="${GIT_TEST_DEFAULT_REF_FORMAT:-files}"
542export GIT_DEFAULT_REF_FORMAT
543
544# Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
545GIT_TRACE_BARE=1
546export GIT_TRACE_BARE
547
548# Some tests scan the GIT_TRACE2_EVENT feed for events, but the
549# default depth is 2, which frequently causes issues when the
550# events are wrapped in new regions. Set it to a sufficiently
551# large depth to avoid custom changes in the test suite.
552GIT_TRACE2_EVENT_NESTING=100
553export GIT_TRACE2_EVENT_NESTING
554
555# Use specific version of the index file format
556if test -n "${GIT_TEST_INDEX_VERSION:+isset}"
557then
558 GIT_INDEX_VERSION="$GIT_TEST_INDEX_VERSION"
559 export GIT_INDEX_VERSION
560fi
561
562if test -n "$GIT_TEST_PERL_FATAL_WARNINGS"
563then
564 GIT_PERL_FATAL_WARNINGS=1
565 export GIT_PERL_FATAL_WARNINGS
566fi
567
568case $GIT_TEST_FSYNC in
569'')
570 GIT_TEST_FSYNC=0
571 export GIT_TEST_FSYNC
572 ;;
573esac
574
575# Protect ourselves from common misconfiguration to export
576# CDPATH into the environment
577unset CDPATH
578
579unset GREP_OPTIONS
580unset UNZIP
581
582case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
5831|2|true)
584 GIT_TRACE=4
585 ;;
586esac
587
588# Line feed
589LF='
590'
591
592# Single quote
593SQ=\'
594
595# UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
596# when case-folding filenames
597u200c=$(printf '\342\200\214')
598
599export _x05 _x35 LF u200c EMPTY_TREE EMPTY_BLOB ZERO_OID OID_REGEX
600
601test "x$TERM" != "xdumb" && (
602 test -t 1 &&
603 tput bold >/dev/null 2>&1 &&
604 tput setaf 1 >/dev/null 2>&1 &&
605 tput sgr0 >/dev/null 2>&1
606 ) &&
607 color=t
608
609if test -n "$color"
610then
611 # Save the color control sequences now rather than run tput
612 # each time say_color() is called. This is done for two
613 # reasons:
614 # * TERM will be changed to dumb
615 # * HOME will be changed to a temporary directory and tput
616 # might need to read ~/.terminfo from the original HOME
617 # directory to get the control sequences
618 # Note: This approach assumes the control sequences don't end
619 # in a newline for any terminal of interest (command
620 # substitutions strip trailing newlines). Given that most
621 # (all?) terminals in common use are related to ECMA-48, this
622 # shouldn't be a problem.
623 say_color_error=$(tput bold; tput setaf 1) # bold red
624 say_color_skip=$(tput setaf 4) # blue
625 say_color_warn=$(tput setaf 3) # brown/yellow
626 say_color_pass=$(tput setaf 2) # green
627 say_color_info=$(tput setaf 6) # cyan
628 say_color_reset=$(tput sgr0)
629 say_color_="" # no formatting for normal text
630 say_color () {
631 test -z "$1" && test -n "$quiet" && return
632 eval "say_color_color=\$say_color_$1"
633 shift
634 printf "%s\\n" "$say_color_color$*$say_color_reset"
635 }
636else
637 say_color() {
638 test -z "$1" && test -n "$quiet" && return
639 shift
640 printf "%s\n" "$*"
641 }
642fi
643
644USER_TERM="$TERM"
645TERM=dumb
646export TERM USER_TERM
647
648# What is written by tests to stdout and stderr is sent to different places
649# depending on the test mode (e.g. /dev/null in non-verbose mode, piped to tee
650# with --tee option, etc.). We save the original stdin to FD #6 and stdout and
651# stderr to #5 and #7, so that the test framework can use them (e.g. for
652# printing errors within the test framework) independently of the test mode.
653exec 5>&1
654exec 6<&0
655exec 7>&2
656
657_error_exit () {
658 finalize_test_output
659 GIT_EXIT_OK=t
660 exit 1
661}
662
663error () {
664 say_color error "error: $*"
665 _error_exit
666}
667
668BUG () {
669 error >&7 "bug in the test script: $*"
670}
671
672BAIL_OUT () {
673 test $# -ne 1 && BUG "1 param"
674
675 # Do not change "Bail out! " string. It's part of TAP syntax:
676 # https://testanything.org/tap-specification.html
677 local bail_out="Bail out! "
678 local message="$1"
679
680 say_color >&5 error $bail_out "$message"
681 _error_exit
682}
683
684say () {
685 say_color info "$*"
686}
687
688if test -n "$HARNESS_ACTIVE"
689then
690 if test "$verbose" = t || test -n "$verbose_only"
691 then
692 BAIL_OUT 'verbose mode forbidden under TAP harness; try --verbose-log'
693 fi
694fi
695
696test "${test_description}" != "" ||
697error "Test script did not set test_description."
698
699if test "$help" = "t"
700then
701 printf '%s\n' "$test_description"
702 exit 0
703fi
704
705if test "$verbose_log" = "t"
706then
707 exec 3>>"$GIT_TEST_TEE_OUTPUT_FILE" 4>&3
708elif test "$verbose" = "t"
709then
710 exec 4>&2 3>&2
711else
712 exec 4>/dev/null 3>/dev/null
713fi
714
715# Send any "-x" output directly to stderr to avoid polluting tests
716# which capture stderr. We can do this unconditionally since it
717# has no effect if tracing isn't turned on.
718#
719# Note that this sets up the trace fd as soon as we assign the variable, so it
720# must come after the creation of descriptor 4 above. Likewise, we must never
721# unset this, as it has the side effect of closing descriptor 4, which we
722# use to show verbose tests to the user.
723#
724# Note also that we don't need or want to export it. The tracing is local to
725# this shell, and we would not want to influence any shells we exec.
726BASH_XTRACEFD=4
727
728test_failure=0
729test_count=0
730test_fixed=0
731test_broken=0
732test_success=0
733
734test_missing_prereq=
735
736test_external_has_tap=0
737
738die () {
739 code=$?
740 # This is responsible for running the atexit commands even when a
741 # test script run with '--immediate' fails, or when the user hits
742 # ctrl-C, i.e. when 'test_done' is not invoked at all.
743 test_atexit_handler || code=$?
744 if test -n "$GIT_EXIT_OK"
745 then
746 exit $code
747 else
748 echo >&5 "FATAL: Unexpected exit with code $code"
749 exit 1
750 fi
751}
752
753GIT_EXIT_OK=
754trap 'die' EXIT
755# Disable '-x' tracing, because with some shells, notably dash, it
756# prevents running the cleanup commands when a test script run with
757# '--verbose-log -x' is interrupted.
758trap '{ code=$?; set +x; } 2>/dev/null; exit $code' INT TERM HUP
759
760# The user-facing functions are loaded from a separate file so that
761# test_perf subshells can have them too
762. "$TEST_DIRECTORY/test-lib-functions.sh"
763
764# You are not expected to call test_ok_ and test_failure_ directly, use
765# the test_expect_* functions instead.
766
767test_ok_ () {
768 test_success=$(($test_success + 1))
769 say_color "" "ok $test_count - $@"
770 finalize_test_case_output ok "$@"
771}
772
773_invert_exit_code_failure_end_blurb () {
774 say_color warn "# faked up failures as TODO & now exiting with 0 due to --invert-exit-code"
775}
776
777test_failure_ () {
778 failure_label=$1
779 test_failure=$(($test_failure + 1))
780 local pfx=""
781 if test -n "$invert_exit_code" # && test -n "$HARNESS_ACTIVE"
782 then
783 pfx="# TODO induced breakage (--invert-exit-code):"
784 fi
785 say_color error "not ok $test_count - ${pfx:+$pfx }$1"
786 shift
787 printf '%s\n' "$*" | sed -e 's/^/# /'
788 if test -n "$immediate"
789 then
790 say_color error "1..$test_count"
791 if test -n "$invert_exit_code"
792 then
793 finalize_test_output
794 _invert_exit_code_failure_end_blurb
795 GIT_EXIT_OK=t
796 exit 0
797 fi
798 check_test_results_san_file_ "$test_failure"
799 _error_exit
800 fi
801 finalize_test_case_output failure "$failure_label" "$@"
802}
803
804test_known_broken_ok_ () {
805 test_fixed=$(($test_fixed+1))
806 say_color error "ok $test_count - $1 # TODO known breakage vanished"
807 finalize_test_case_output fixed "$1"
808}
809
810test_known_broken_failure_ () {
811 test_broken=$(($test_broken+1))
812 say_color warn "not ok $test_count - $1 # TODO known breakage"
813 finalize_test_case_output broken "$1"
814}
815
816test_debug () {
817 test "$debug" = "" || eval "$1"
818}
819
820match_pattern_list () {
821 arg="$1"
822 shift
823 test -z "$*" && return 1
824 # We need to use "$*" to get field-splitting, but we want to
825 # disable globbing, since we are matching against an arbitrary
826 # $arg, not what's in the filesystem. Using "set -f" accomplishes
827 # that, but we must do it in a subshell to avoid impacting the
828 # rest of the script. The exit value of the subshell becomes
829 # the function's return value.
830 (
831 set -f
832 for pattern_ in $*
833 do
834 case "$arg" in
835 $pattern_)
836 exit 0
837 ;;
838 esac
839 done
840 exit 1
841 )
842}
843
844match_test_selector_list () {
845 operation="$1"
846 shift
847 title="$1"
848 shift
849 arg="$1"
850 shift
851 test -z "$1" && return 0
852
853 # Commas are accepted as separators.
854 OLDIFS=$IFS
855 IFS=','
856 set -- $1
857 IFS=$OLDIFS
858
859 # If the first selector is negative we include by default.
860 include=
861 case "$1" in
862 !*) include=t ;;
863 esac
864
865 for selector
866 do
867 orig_selector=$selector
868
869 positive=t
870 case "$selector" in
871 !*)
872 positive=
873 selector=${selector##?}
874 ;;
875 esac
876
877 test -z "$selector" && continue
878
879 case "$selector" in
880 *-*)
881 if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
882 then
883 echo "error: $operation: invalid non-numeric in range" \
884 "start: '$orig_selector'" >&2
885 exit 1
886 fi
887 if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
888 then
889 echo "error: $operation: invalid non-numeric in range" \
890 "end: '$orig_selector'" >&2
891 exit 1
892 fi
893 ;;
894 *)
895 if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
896 then
897 case "$title" in *${selector}*)
898 include=$positive
899 ;;
900 esac
901 continue
902 fi
903 esac
904
905 # Short cut for "obvious" cases
906 test -z "$include" && test -z "$positive" && continue
907 test -n "$include" && test -n "$positive" && continue
908
909 case "$selector" in
910 -*)
911 if test $arg -le ${selector#-}
912 then
913 include=$positive
914 fi
915 ;;
916 *-)
917 if test $arg -ge ${selector%-}
918 then
919 include=$positive
920 fi
921 ;;
922 *-*)
923 if test ${selector%%-*} -le $arg \
924 && test $arg -le ${selector#*-}
925 then
926 include=$positive
927 fi
928 ;;
929 *)
930 if test $arg -eq $selector
931 then
932 include=$positive
933 fi
934 ;;
935 esac
936 done
937
938 test -n "$include"
939}
940
941maybe_teardown_verbose () {
942 test -z "$verbose_only" && return
943 exec 4>/dev/null 3>/dev/null
944 verbose=
945}
946
947last_verbose=t
948maybe_setup_verbose () {
949 test -z "$verbose_only" && return
950 if match_pattern_list $test_count "$verbose_only"
951 then
952 exec 4>&2 3>&2
953 # Emit a delimiting blank line when going from
954 # non-verbose to verbose. Within verbose mode the
955 # delimiter is printed by test_expect_*. The choice
956 # of the initial $last_verbose is such that before
957 # test 1, we do not print it.
958 test -z "$last_verbose" && echo >&3 ""
959 verbose=t
960 else
961 exec 4>/dev/null 3>/dev/null
962 verbose=
963 fi
964 last_verbose=$verbose
965}
966
967maybe_teardown_valgrind () {
968 test -z "$GIT_VALGRIND" && return
969 GIT_VALGRIND_ENABLED=
970}
971
972maybe_setup_valgrind () {
973 test -z "$GIT_VALGRIND" && return
974 if test -z "$valgrind_only"
975 then
976 GIT_VALGRIND_ENABLED=t
977 return
978 fi
979 GIT_VALGRIND_ENABLED=
980 if match_pattern_list $test_count "$valgrind_only"
981 then
982 GIT_VALGRIND_ENABLED=t
983 fi
984}
985
986trace_level_=0
987want_trace () {
988 test "$trace" = t && {
989 test "$verbose" = t || test "$verbose_log" = t
990 }
991}
992
993# This is a separate function because some tests use
994# "return" to end a test_expect_success block early
995# (and we want to make sure we run any cleanup like
996# "set +x").
997test_eval_inner_ () {
998 eval "$*"
999}
1000
1001test_eval_ () {
1002 # If "-x" tracing is in effect, then we want to avoid polluting stderr
1003 # with non-test commands. But once in "set -x" mode, we cannot prevent
1004 # the shell from printing the "set +x" to turn it off (nor the saving
1005 # of $? before that). But we can make sure that the output goes to
1006 # /dev/null.
1007 #
1008 # There are a few subtleties here:
1009 #
1010 # - we have to redirect descriptor 4 in addition to 2, to cover
1011 # BASH_XTRACEFD
1012 #
1013 # - the actual eval has to come before the redirection block (since
1014 # it needs to see descriptor 4 to set up its stderr)
1015 #
1016 # - likewise, any error message we print must be outside the block to
1017 # access descriptor 4
1018 #
1019 # - checking $? has to come immediately after the eval, but it must
1020 # be _inside_ the block to avoid polluting the "set -x" output
1021 #
1022
1023 # Do not add anything extra (including LF) after '$*'
1024 test_eval_inner_ </dev/null >&3 2>&4 "
1025 want_trace && trace_level_=$(($trace_level_+1)) && set -x
1026 $*"
1027 {
1028 test_eval_ret_=$?
1029 if want_trace
1030 then
1031 test 1 = $trace_level_ && set +x
1032 trace_level_=$(($trace_level_-1))
1033 fi
1034 } 2>/dev/null 4>&2
1035
1036 if test "$test_eval_ret_" != 0 && want_trace
1037 then
1038 say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
1039 fi
1040 return $test_eval_ret_
1041}
1042
1043fail_117 () {
1044 return 117
1045}
1046
1047test_run_ () {
1048 test_cleanup=:
1049 expecting_failure=$2
1050
1051 if test "${GIT_TEST_CHAIN_LINT:-1}" != 0; then
1052 # 117 is magic because it is unlikely to match the exit
1053 # code of other programs
1054 test_eval_inner_ "fail_117 && $1" </dev/null >&3 2>&4
1055 if test $? != 117
1056 then
1057 BUG "broken &&-chain: $1"
1058 fi
1059 fi
1060
1061 setup_malloc_check
1062 test_eval_ "$1"
1063 eval_ret=$?
1064 teardown_malloc_check
1065
1066 if test -z "$immediate" || test $eval_ret = 0 ||
1067 test -n "$expecting_failure" && test "$test_cleanup" != ":"
1068 then
1069 setup_malloc_check
1070 test_eval_ "$test_cleanup"
1071 teardown_malloc_check
1072 fi
1073 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
1074 then
1075 echo ""
1076 fi
1077 return "$eval_ret"
1078}
1079
1080test_start_ () {
1081 test_count=$(($test_count+1))
1082 maybe_setup_verbose
1083 maybe_setup_valgrind
1084 start_test_case_output "$@"
1085}
1086
1087test_finish_ () {
1088 echo >&3 ""
1089 maybe_teardown_valgrind
1090 maybe_teardown_verbose
1091 if test -n "$GIT_TEST_TEE_OFFSET"
1092 then
1093 GIT_TEST_TEE_OFFSET=$(test-tool path-utils file-size \
1094 "$GIT_TEST_TEE_OUTPUT_FILE")
1095 fi
1096}
1097
1098test_skip () {
1099 to_skip=
1100 skipped_reason=
1101 if match_pattern_list $this_test.$test_count "$GIT_SKIP_TESTS"
1102 then
1103 to_skip=t
1104 skipped_reason="GIT_SKIP_TESTS"
1105 fi
1106 if test -z "$to_skip" && test -n "$run_list" &&
1107 ! match_test_selector_list '--run' "$1" $test_count "$run_list"
1108 then
1109 to_skip=t
1110 skipped_reason="--run"
1111 fi
1112 if test -z "$to_skip" && test -n "$test_prereq" &&
1113 ! test_have_prereq "$test_prereq"
1114 then
1115 to_skip=t
1116
1117 of_prereq=
1118 if test "$missing_prereq" != "$test_prereq"
1119 then
1120 of_prereq=" of $test_prereq"
1121 fi
1122 skipped_reason="missing $missing_prereq${of_prereq}"
1123
1124 # Keep a list of all the missing prereq for result aggregation
1125 if test -z "$missing_prereq"
1126 then
1127 test_missing_prereq=$missing_prereq
1128 else
1129 test_missing_prereq="$test_missing_prereq,$missing_prereq"
1130 fi
1131 fi
1132
1133 case "$to_skip" in
1134 t)
1135
1136 say_color skip "ok $test_count # skip $1 ($skipped_reason)"
1137 : true
1138 finalize_test_case_output skip "$@"
1139 ;;
1140 *)
1141 false
1142 ;;
1143 esac
1144}
1145
1146# stub; perf-lib overrides it
1147test_at_end_hook_ () {
1148 :
1149}
1150
1151test_atexit_cleanup=:
1152test_atexit_handler () {
1153 # In a succeeding test script 'test_atexit_handler' is invoked
1154 # twice: first from 'test_done', then from 'die' in the trap on
1155 # EXIT.
1156 # This condition and resetting 'test_atexit_cleanup' below makes
1157 # sure that the registered cleanup commands are run only once.
1158 test : != "$test_atexit_cleanup" || return 0
1159
1160 setup_malloc_check
1161 test_eval_ "$test_atexit_cleanup"
1162 test_atexit_cleanup=:
1163 teardown_malloc_check
1164}
1165
1166check_test_results_san_file_has_entries_ () {
1167 test -z "$TEST_RESULTS_SAN_FILE" && return 1
1168
1169 # Lines marked with DEDUP_TOKEN show unique leaks. We only care that we
1170 # found at least one.
1171 #
1172 # But also suppress any false positives caused by bugs or races in the
1173 # sanitizer itself.
1174 grep -s ^DEDUP_TOKEN "$TEST_RESULTS_SAN_FILE".* |
1175 grep -qv sanitizer::GetThreadStackTopAndBottom
1176}
1177
1178check_test_results_san_file_ () {
1179 if ! check_test_results_san_file_has_entries_
1180 then
1181 return
1182 fi &&
1183 say_color error "$(cat "$TEST_RESULTS_SAN_FILE".*)" &&
1184
1185 if test "$test_failure" = 0
1186 then
1187 say "Our logs revealed a memory leak, exit non-zero!" &&
1188 invert_exit_code=t
1189 else
1190 say "Our logs revealed a memory leak..."
1191 fi
1192}
1193
1194test_done () {
1195 # Run the atexit commands _before_ the trash directory is
1196 # removed, so the commands can access pidfiles and socket files.
1197 test_atexit_handler
1198
1199 finalize_test_output
1200
1201 if test -z "$HARNESS_ACTIVE"
1202 then
1203 mkdir -p "$TEST_RESULTS_DIR"
1204
1205 cat >"$TEST_RESULTS_BASE.counts" <<-EOF
1206 total $test_count
1207 success $test_success
1208 fixed $test_fixed
1209 broken $test_broken
1210 failed $test_failure
1211 missing_prereq $test_missing_prereq
1212
1213 EOF
1214 fi
1215
1216 if test "$test_fixed" != 0
1217 then
1218 say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
1219 fi
1220 if test "$test_broken" != 0
1221 then
1222 say_color warn "# still have $test_broken known breakage(s)"
1223 fi
1224 if test "$test_broken" != 0 || test "$test_fixed" != 0
1225 then
1226 test_remaining=$(( $test_count - $test_broken - $test_fixed ))
1227 msg="remaining $test_remaining test(s)"
1228 else
1229 test_remaining=$test_count
1230 msg="$test_count test(s)"
1231 fi
1232 case "$test_failure" in
1233 0)
1234 if test $test_remaining -gt 0
1235 then
1236 say_color pass "# passed all $msg"
1237 fi
1238
1239 # Maybe print SKIP message
1240 test -z "$skip_all" || skip_all="# SKIP $skip_all"
1241 case "$test_count" in
1242 0)
1243 say "1..$test_count${skip_all:+ $skip_all}"
1244 ;;
1245 *)
1246 test -z "$skip_all" ||
1247 say_color warn "$skip_all"
1248 say "1..$test_count"
1249 ;;
1250 esac
1251
1252 if test -n "$stress" && test -n "$invert_exit_code"
1253 then
1254 # We're about to move our "$TRASH_DIRECTORY"
1255 # to "$TRASH_DIRECTORY.stress-failed" if
1256 # --stress is combined with
1257 # --invert-exit-code.
1258 say "with --stress and --invert-exit-code we're not removing '$TRASH_DIRECTORY'"
1259 elif test -z "$debug" && test -n "$remove_trash"
1260 then
1261 test -d "$TRASH_DIRECTORY" ||
1262 error "Tests passed but trash directory already removed before test cleanup; aborting"
1263
1264 cd "$TRASH_DIRECTORY/.." &&
1265 rm -fr "$TRASH_DIRECTORY" || {
1266 # try again in a bit
1267 sleep 5;
1268 rm -fr "$TRASH_DIRECTORY"
1269 } ||
1270 error "Tests passed but test cleanup failed; aborting"
1271 fi
1272
1273 check_test_results_san_file_ "$test_failure"
1274
1275 if test "$test_fixed" != 0
1276 then
1277 if test -z "$invert_exit_code"
1278 then
1279 GIT_EXIT_OK=t
1280 exit 1
1281 fi
1282 elif test -z "$skip_all" && test -n "$invert_exit_code"
1283 then
1284 say_color warn "# faking up non-zero exit with --invert-exit-code"
1285 GIT_EXIT_OK=t
1286 exit 1
1287 fi
1288
1289 test_at_end_hook_
1290
1291 GIT_EXIT_OK=t
1292 exit 0 ;;
1293
1294 *)
1295 say_color error "# failed $test_failure among $msg"
1296 say "1..$test_count"
1297
1298 check_test_results_san_file_ "$test_failure"
1299
1300 if test -n "$invert_exit_code"
1301 then
1302 _invert_exit_code_failure_end_blurb
1303 GIT_EXIT_OK=t
1304 exit 0
1305 fi
1306
1307 GIT_EXIT_OK=t
1308 exit 1 ;;
1309
1310 esac
1311}
1312
1313if test -n "$valgrind"
1314then
1315 make_symlink () {
1316 test -h "$2" &&
1317 test "$1" = "$(readlink "$2")" || {
1318 # be super paranoid
1319 if mkdir "$2".lock
1320 then
1321 rm -f "$2" &&
1322 ln -s "$1" "$2" &&
1323 rm -r "$2".lock
1324 else
1325 while test -d "$2".lock
1326 do
1327 say "Waiting for lock on $2."
1328 sleep 1
1329 done
1330 fi
1331 }
1332 }
1333
1334 make_valgrind_symlink () {
1335 # handle only executables, unless they are shell libraries that
1336 # need to be in the exec-path.
1337 test -x "$1" ||
1338 test "# " = "$(test_copy_bytes 2 <"$1")" ||
1339 return;
1340
1341 base=$(basename "$1")
1342 case "$base" in
1343 test-*)
1344 symlink_target="$GIT_BUILD_DIR/t/helper/$base"
1345 ;;
1346 *)
1347 symlink_target="$GIT_BUILD_DIR/$base"
1348 ;;
1349 esac
1350 # do not override scripts
1351 if test -x "$symlink_target" &&
1352 test ! -d "$symlink_target" &&
1353 test "#!" != "$(test_copy_bytes 2 <"$symlink_target")"
1354 then
1355 symlink_target=../valgrind.sh
1356 fi
1357 case "$base" in
1358 *.sh|*.perl)
1359 symlink_target=../unprocessed-script
1360 esac
1361 # create the link, or replace it if it is out of date
1362 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
1363 }
1364
1365 # override all git executables in TEST_DIRECTORY/..
1366 GIT_VALGRIND=$TEST_DIRECTORY/valgrind
1367 mkdir -p "$GIT_VALGRIND"/bin
1368 for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/t/helper/test-*
1369 do
1370 make_valgrind_symlink $file
1371 done
1372 # special-case the mergetools loadables
1373 make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
1374 OLDIFS=$IFS
1375 IFS=:
1376 for path in $PATH
1377 do
1378 ls "$path"/git-* 2> /dev/null |
1379 while read file
1380 do
1381 make_valgrind_symlink "$file"
1382 done
1383 done
1384 IFS=$OLDIFS
1385 PATH=$GIT_VALGRIND/bin:$PATH
1386 GIT_EXEC_PATH=$GIT_VALGRIND/bin
1387 export GIT_VALGRIND
1388 GIT_VALGRIND_MODE="$valgrind"
1389 export GIT_VALGRIND_MODE
1390 GIT_VALGRIND_ENABLED=t
1391 test -n "$valgrind_only" && GIT_VALGRIND_ENABLED=
1392 export GIT_VALGRIND_ENABLED
1393elif test -n "$GIT_TEST_INSTALLED"
1394then
1395 GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path) ||
1396 error "Cannot run git from $GIT_TEST_INSTALLED."
1397 PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR/t/helper:$PATH
1398 GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
1399else # normal case, use ../bin-wrappers only unless $with_dashes:
1400 if test -n "$no_bin_wrappers"
1401 then
1402 with_dashes=t
1403 else
1404 git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
1405 if ! test -x "$git_bin_dir/git"
1406 then
1407 if test -z "$with_dashes"
1408 then
1409 say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
1410 fi
1411 with_dashes=t
1412 fi
1413 PATH="$git_bin_dir:$PATH"
1414 fi
1415 GIT_EXEC_PATH=$GIT_BUILD_DIR
1416 if test -n "$with_dashes"
1417 then
1418 PATH="$GIT_BUILD_DIR:$GIT_BUILD_DIR/t/helper:$PATH"
1419 fi
1420fi
1421GIT_TEMPLATE_DIR="$GIT_TEST_TEMPLATE_DIR"
1422GIT_CONFIG_NOSYSTEM=1
1423GIT_ATTR_NOSYSTEM=1
1424GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/.."
1425export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM GIT_CEILING_DIRECTORIES
1426
1427# Add libc MALLOC and MALLOC_PERTURB test only if we are not executing
1428# the test with valgrind and have not compiled with conflict SANITIZE
1429# options.
1430if test -n "$valgrind" ||
1431 test -n "$SANITIZE_ADDRESS" ||
1432 test -n "$SANITIZE_LEAK" ||
1433 test -n "$TEST_NO_MALLOC_CHECK"
1434then
1435 setup_malloc_check () {
1436 : nothing
1437 }
1438 teardown_malloc_check () {
1439 : nothing
1440 }
1441else
1442 _USE_GLIBC_TUNABLES=
1443 _USE_GLIBC_PRELOAD=libc_malloc_debug.so.0
1444 if _GLIBC_VERSION=$(getconf GNU_LIBC_VERSION 2>/dev/null) &&
1445 _GLIBC_VERSION=${_GLIBC_VERSION#"glibc "} &&
1446 expr 2.34 \<= "$_GLIBC_VERSION" >/dev/null &&
1447 stderr=$(LD_PRELOAD=$_USE_GLIBC_PRELOAD git version 2>&1 >/dev/null) &&
1448 test -z "$stderr"
1449 then
1450 _USE_GLIBC_TUNABLES=YesPlease
1451 fi
1452 setup_malloc_check () {
1453 local g
1454 local t
1455 MALLOC_CHECK_=3 MALLOC_PERTURB_=165
1456 export MALLOC_CHECK_ MALLOC_PERTURB_
1457 if test -n "$_USE_GLIBC_TUNABLES"
1458 then
1459 g=
1460 LD_PRELOAD=$_USE_GLIBC_PRELOAD
1461 for t in \
1462 glibc.malloc.check=1 \
1463 glibc.malloc.perturb=165
1464 do
1465 g="${g#:}:$t"
1466 done
1467 GLIBC_TUNABLES=$g
1468 export LD_PRELOAD GLIBC_TUNABLES
1469 fi
1470 }
1471 teardown_malloc_check () {
1472 unset MALLOC_CHECK_ MALLOC_PERTURB_
1473 unset LD_PRELOAD GLIBC_TUNABLES
1474 }
1475fi
1476
1477if test -z "$GIT_TEST_CMP"
1478then
1479 if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
1480 then
1481 GIT_TEST_CMP="$DIFF -c"
1482 else
1483 GIT_TEST_CMP="$DIFF -u"
1484 fi
1485fi
1486
1487GITPERLLIB="$GIT_TEST_GITPERLLIB"
1488export GITPERLLIB
1489test -d "$GIT_TEMPLATE_DIR" || {
1490 BAIL_OUT "You haven't built things yet, have you?"
1491}
1492
1493if ! test -x "$GIT_BUILD_DIR"/t/helper/test-tool$X
1494then
1495 BAIL_OUT 'You need to build test-tool; Run "make t/helper/test-tool" in the source (toplevel) directory'
1496fi
1497
1498# Are we running this test at all?
1499remove_trash=
1500this_test=${0##*/}
1501this_test=${this_test%%-*}
1502if match_pattern_list "$this_test" "$GIT_SKIP_TESTS"
1503then
1504 say_color info >&3 "skipping test $this_test altogether"
1505 skip_all="skip all tests in $this_test"
1506 test_done
1507fi
1508
1509if test -n "$SANITIZE_LEAK"
1510then
1511 rm -rf "$TEST_RESULTS_SAN_DIR"
1512 if ! mkdir -p "$TEST_RESULTS_SAN_DIR"
1513 then
1514 BAIL_OUT "cannot create $TEST_RESULTS_SAN_DIR"
1515 fi &&
1516 TEST_RESULTS_SAN_FILE="$TEST_RESULTS_SAN_DIR/$TEST_RESULTS_SAN_FILE_PFX"
1517
1518 # Don't litter *.leak dirs if there was nothing to report
1519 test_atexit "rmdir \"$TEST_RESULTS_SAN_DIR\" 2>/dev/null || :"
1520
1521 prepend_var LSAN_OPTIONS : dedup_token_length=9999
1522 prepend_var LSAN_OPTIONS : log_exe_name=1
1523 prepend_var LSAN_OPTIONS : log_path="'$TEST_RESULTS_SAN_FILE'"
1524 export LSAN_OPTIONS
1525fi
1526
1527if test -z "$PERL_PATH"
1528then
1529 case "${GIT_TEST_CHAIN_LINT:-unset}" in
1530 unset)
1531 GIT_TEST_CHAIN_LINT=0
1532 ;;
1533 0)
1534 # The user has explicitly disabled the chain linter, so we
1535 # don't have anything to worry about.
1536 ;;
1537 *)
1538 BAIL_OUT 'You need Perl for the chain linter'
1539 ;;
1540 esac
1541fi
1542
1543if test "${GIT_TEST_CHAIN_LINT:-1}" != 0 &&
1544 test "${GIT_TEST_EXT_CHAIN_LINT:-1}" != 0
1545then
1546 "$PERL_PATH" "$TEST_DIRECTORY/chainlint.pl" "$0" ||
1547 BUG "lint error (see 'LINT' annotations above)"
1548fi
1549
1550# Last-minute variable setup
1551USER_HOME="$HOME"
1552HOME="$TRASH_DIRECTORY"
1553GNUPGHOME="$HOME/gnupg-home-not-used"
1554export HOME GNUPGHOME USER_HOME
1555
1556# "rm -rf" existing trash directory, even if a previous run left it
1557# with bad permissions.
1558remove_trash_directory () {
1559 dir="$1"
1560 if ! rm -rf "$dir" 2>/dev/null
1561 then
1562 chmod -R u+rwx "$dir"
1563 rm -rf "$dir"
1564 fi
1565 ! test -d "$dir"
1566}
1567
1568# Test repository
1569remove_trash_directory "$TRASH_DIRECTORY" || {
1570 BAIL_OUT 'cannot prepare test area'
1571}
1572
1573remove_trash=t
1574if test -z "$TEST_NO_CREATE_REPO"
1575then
1576 git init \
1577 ${TEST_CREATE_REPO_NO_TEMPLATE:+--template=} \
1578 "$TRASH_DIRECTORY" >&3 2>&4 ||
1579 error "cannot run git init"
1580else
1581 mkdir -p "$TRASH_DIRECTORY"
1582fi
1583
1584# Use -P to resolve symlinks in our working directory so that the cwd
1585# in subprocesses like git equals our $PWD (for pathname comparisons).
1586cd -P "$TRASH_DIRECTORY" || BAIL_OUT "cannot cd -P to \"$TRASH_DIRECTORY\""
1587TRASH_DIRECTORY=$(pwd)
1588HOME="$TRASH_DIRECTORY"
1589
1590start_test_output "$0"
1591
1592# Convenience
1593# A regexp to match 5 and 35 hexdigits
1594_x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
1595_x35="$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
1596
1597test_oid_init
1598
1599ZERO_OID=$(test_oid zero)
1600OID_REGEX=$(echo $ZERO_OID | sed -e 's/0/[0-9a-f]/g')
1601OIDPATH_REGEX=$(test_oid_to_path $ZERO_OID | sed -e 's/0/[0-9a-f]/g')
1602EMPTY_TREE=$(test_oid empty_tree)
1603EMPTY_BLOB=$(test_oid empty_blob)
1604
1605# Provide an implementation of the 'yes' utility; the upper bound
1606# limit is there to help Windows that cannot stop this loop from
1607# wasting cycles when the downstream stops reading, so do not be
1608# tempted to turn it into an infinite loop. cf. 6129c930 ("test-lib:
1609# limit the output of the yes utility", 2016-02-02)
1610yes () {
1611 if test $# = 0
1612 then
1613 y=y
1614 else
1615 y="$*"
1616 fi
1617
1618 i=0
1619 while test $i -lt 99
1620 do
1621 echo "$y"
1622 i=$(($i+1))
1623 done
1624}
1625
1626# The GIT_TEST_FAIL_PREREQS code hooks into test_set_prereq(), and
1627# thus needs to be set up really early, and set an internal variable
1628# for convenience so the hot test_set_prereq() codepath doesn't need
1629# to call "test-tool env-helper" (via test_bool_env). Only do that work
1630# if needed by seeing if GIT_TEST_FAIL_PREREQS is set at all.
1631GIT_TEST_FAIL_PREREQS_INTERNAL=
1632if test -n "$GIT_TEST_FAIL_PREREQS"
1633then
1634 if test_bool_env GIT_TEST_FAIL_PREREQS false
1635 then
1636 GIT_TEST_FAIL_PREREQS_INTERNAL=true
1637 test_set_prereq FAIL_PREREQS
1638 fi
1639else
1640 test_lazy_prereq FAIL_PREREQS '
1641 test_bool_env GIT_TEST_FAIL_PREREQS false
1642 '
1643fi
1644
1645# Fix some commands on Windows, and other OS-specific things
1646uname_s=$(uname -s)
1647case $uname_s in
1648Darwin)
1649 test_set_prereq MACOS
1650 test_set_prereq POSIXPERM
1651 test_set_prereq BSLASHPSPEC
1652 test_set_prereq EXECKEEPSPID
1653 ;;
1654*MINGW*)
1655 # Windows has its own (incompatible) sort and find
1656 sort () {
1657 /usr/bin/sort "$@"
1658 }
1659 find () {
1660 /usr/bin/find "$@"
1661 }
1662 # git sees Windows-style pwd
1663 pwd () {
1664 builtin pwd -W
1665 }
1666 # no POSIX permissions
1667 # backslashes in pathspec are converted to '/'
1668 # exec does not inherit the PID
1669 test_set_prereq MINGW
1670 test_set_prereq NATIVE_CRLF
1671 test_set_prereq SED_STRIPS_CR
1672 test_set_prereq GREP_STRIPS_CR
1673 test_set_prereq WINDOWS
1674 GIT_TEST_CMP="GIT_DIR=/dev/null git diff --no-index --ignore-cr-at-eol --"
1675 ;;
1676*CYGWIN*)
1677 test_set_prereq POSIXPERM
1678 test_set_prereq EXECKEEPSPID
1679 test_set_prereq CYGWIN
1680 test_set_prereq SED_STRIPS_CR
1681 test_set_prereq GREP_STRIPS_CR
1682 test_set_prereq WINDOWS
1683 ;;
1684*)
1685 test_set_prereq POSIXPERM
1686 test_set_prereq BSLASHPSPEC
1687 test_set_prereq EXECKEEPSPID
1688 ;;
1689esac
1690
1691# Detect arches where a few things don't work
1692uname_m=$(uname -m)
1693case $uname_m in
1694parisc* | hppa*)
1695 test_set_prereq HPPA
1696 ;;
1697esac
1698
1699case "$GIT_DEFAULT_REF_FORMAT" in
1700files)
1701 test_set_prereq REFFILES;;
1702reftable)
1703 test_set_prereq REFTABLE;;
1704*)
1705 echo 2>&1 "error: unknown ref format $GIT_DEFAULT_REF_FORMAT"
1706 exit 1
1707 ;;
1708esac
1709
1710( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
1711test -z "$NO_CURL" && test_set_prereq LIBCURL
1712test -z "$NO_GITWEB" && test_set_prereq GITWEB
1713test -z "$NO_ICONV" && test_set_prereq ICONV
1714test -z "$NO_PERL" && test_set_prereq PERL
1715test -z "$NO_PTHREADS" && test_set_prereq PTHREADS
1716test -z "$NO_PYTHON" && test_set_prereq PYTHON
1717test -n "$USE_LIBPCRE2" && test_set_prereq PCRE
1718test -n "$USE_LIBPCRE2" && test_set_prereq LIBPCRE2
1719test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
1720test -n "$SANITIZE_LEAK" && test_set_prereq SANITIZE_LEAK
1721test -n "$GIT_VALGRIND_ENABLED" && test_set_prereq VALGRIND
1722test -n "$PERL_PATH" && test_set_prereq PERL_TEST_HELPERS
1723
1724if test -z "$GIT_TEST_CHECK_CACHE_TREE"
1725then
1726 GIT_TEST_CHECK_CACHE_TREE=true
1727 export GIT_TEST_CHECK_CACHE_TREE
1728fi
1729
1730test_lazy_prereq PIPE '
1731 # test whether the filesystem supports FIFOs
1732 test_have_prereq !MINGW,!CYGWIN &&
1733 rm -f testfifo && mkfifo testfifo
1734'
1735
1736test_lazy_prereq SYMLINKS '
1737 # test whether the filesystem supports symbolic links
1738 ln -s x y && test -h y
1739'
1740
1741test_lazy_prereq SYMLINKS_WINDOWS '
1742 # test whether symbolic links are enabled on Windows
1743 test_have_prereq MINGW &&
1744 cmd //c "mklink y x" &> /dev/null && test -h y
1745'
1746
1747test_lazy_prereq FILEMODE '
1748 test "$(git config --bool core.filemode)" = true
1749'
1750
1751test_lazy_prereq CASE_INSENSITIVE_FS '
1752 echo good >CamelCase &&
1753 echo bad >camelcase &&
1754 test "$(cat CamelCase)" != good
1755'
1756
1757test_lazy_prereq FUNNYNAMES '
1758 test_have_prereq !MINGW &&
1759 touch -- \
1760 "FUNNYNAMES tab embedded" \
1761 "FUNNYNAMES \"quote embedded\"" \
1762 "FUNNYNAMES newline
1763embedded" 2>/dev/null &&
1764 rm -- \
1765 "FUNNYNAMES tab embedded" \
1766 "FUNNYNAMES \"quote embedded\"" \
1767 "FUNNYNAMES newline
1768embedded" 2>/dev/null
1769'
1770
1771test_lazy_prereq UTF8_NFD_TO_NFC '
1772 # check whether FS converts nfd unicode to nfc
1773 auml=$(printf "\303\244")
1774 aumlcdiar=$(printf "\141\314\210")
1775 >"$auml" &&
1776 test -f "$aumlcdiar"
1777'
1778
1779test_lazy_prereq AUTOIDENT '
1780 sane_unset GIT_AUTHOR_NAME &&
1781 sane_unset GIT_AUTHOR_EMAIL &&
1782 git var GIT_AUTHOR_IDENT
1783'
1784
1785test_lazy_prereq EXPENSIVE '
1786 test -n "$GIT_TEST_LONG"
1787'
1788
1789test_lazy_prereq EXPENSIVE_ON_WINDOWS '
1790 test_have_prereq EXPENSIVE || test_have_prereq !MINGW,!CYGWIN
1791'
1792
1793test_lazy_prereq USR_BIN_TIME '
1794 test -x /usr/bin/time
1795'
1796
1797test_lazy_prereq NOT_ROOT '
1798 uid=$(id -u) &&
1799 test "$uid" != 0
1800'
1801
1802test_lazy_prereq JGIT '
1803 jgit --version
1804'
1805
1806# SANITY is about "can you correctly predict what the filesystem would
1807# do by only looking at the permission bits of the files and
1808# directories?" A typical example of !SANITY is running the test
1809# suite as root, where a test may expect "chmod -r file && cat file"
1810# to fail because file is supposed to be unreadable after a successful
1811# chmod. In an environment (i.e. combination of what filesystem is
1812# being used and who is running the tests) that lacks SANITY, you may
1813# be able to delete or create a file when the containing directory
1814# doesn't have write permissions, or access a file even if the
1815# containing directory doesn't have read or execute permissions.
1816
1817test_lazy_prereq SANITY '
1818 mkdir SANETESTD.1 SANETESTD.2 &&
1819
1820 chmod +w SANETESTD.1 SANETESTD.2 &&
1821 >SANETESTD.1/x 2>SANETESTD.2/x &&
1822 chmod -w SANETESTD.1 &&
1823 chmod -r SANETESTD.1/x &&
1824 chmod -rx SANETESTD.2 ||
1825 BUG "cannot prepare SANETESTD"
1826
1827 ! test -r SANETESTD.1/x &&
1828 ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
1829 status=$?
1830
1831 chmod +rwx SANETESTD.1 SANETESTD.2 &&
1832 rm -rf SANETESTD.1 SANETESTD.2 ||
1833 BUG "cannot clean SANETESTD"
1834 return $status
1835'
1836
1837test FreeBSD != $uname_s || GIT_UNZIP=${GIT_UNZIP:-/usr/local/bin/unzip}
1838GIT_UNZIP=${GIT_UNZIP:-unzip}
1839test_lazy_prereq UNZIP '
1840 "$GIT_UNZIP" -v
1841 test $? -ne 127
1842'
1843
1844run_with_limited_cmdline () {
1845 (ulimit -s 128 && "$@")
1846}
1847
1848test_lazy_prereq CMDLINE_LIMIT '
1849 test_have_prereq !HPPA,!MINGW,!CYGWIN &&
1850 run_with_limited_cmdline true
1851'
1852
1853run_with_limited_stack () {
1854 (ulimit -s 128 && "$@")
1855}
1856
1857test_lazy_prereq ULIMIT_STACK_SIZE '
1858 test_have_prereq !HPPA,!MINGW,!CYGWIN &&
1859 run_with_limited_stack true
1860'
1861
1862run_with_limited_open_files () {
1863 (ulimit -n 32 && "$@")
1864}
1865
1866test_lazy_prereq ULIMIT_FILE_DESCRIPTORS '
1867 test_have_prereq !MINGW,!CYGWIN &&
1868 run_with_limited_open_files true
1869'
1870
1871build_option () {
1872 git version --build-options |
1873 sed -ne "s/^$1: //p"
1874}
1875
1876test_lazy_prereq SIZE_T_IS_64BIT '
1877 test 8 -eq "$(build_option sizeof-size_t)"
1878'
1879
1880test_lazy_prereq LONG_IS_64BIT '
1881 test 8 -le "$(build_option sizeof-long)"
1882'
1883
1884test_lazy_prereq TIME_IS_64BIT 'test-tool date is64bit'
1885test_lazy_prereq TIME_T_IS_64BIT 'test-tool date time_t-is64bit'
1886
1887test_lazy_prereq CURL '
1888 curl --version
1889'
1890
1891test_lazy_prereq WITH_BREAKING_CHANGES '
1892 test -n "$WITH_BREAKING_CHANGES"
1893'
1894
1895test_lazy_prereq WITHOUT_BREAKING_CHANGES '
1896 # Signal that this prereq should not be used.
1897 exit 125
1898'
1899
1900# SHA1 is a test if the hash algorithm in use is SHA-1. This is both for tests
1901# which will not work with other hash algorithms and tests that work but don't
1902# test anything meaningful (e.g. special values which cause short collisions).
1903test_lazy_prereq SHA1 '
1904 case "$GIT_DEFAULT_HASH" in
1905 sha1) true ;;
1906 "") test $(git hash-object /dev/null) = e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 ;;
1907 *) false ;;
1908 esac
1909'
1910
1911test_lazy_prereq DEFAULT_REPO_FORMAT '
1912 test_have_prereq SHA1,REFFILES
1913'
1914
1915# Ensure that no test accidentally triggers a Git command
1916# that runs the actual maintenance scheduler, affecting a user's
1917# system permanently.
1918# Tests that verify the scheduler integration must set this locally
1919# to avoid errors.
1920GIT_TEST_MAINT_SCHEDULER="none:exit 1"
1921export GIT_TEST_MAINT_SCHEDULER
1922
1923# Does this platform support `git fsmonitor--daemon`
1924#
1925test_lazy_prereq FSMONITOR_DAEMON '
1926 git version --build-options >output &&
1927 grep "feature: fsmonitor--daemon" output
1928'