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 http://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	# We allow tests to override this, in case they want to run tests
23	# outside of t/, e.g. for running tests on the test library
24	# itself.
25	TEST_DIRECTORY=$(pwd)
26else
27	# ensure that TEST_DIRECTORY is an absolute path so that it
28	# is valid even if the current working directory is changed
29	TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
30fi
31if test -z "$TEST_OUTPUT_DIRECTORY"
32then
33	# Similarly, override this to store the test-results subdir
34	# elsewhere
35	TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
36fi
37GIT_BUILD_DIR="$TEST_DIRECTORY"/..
38
39# If we were built with ASAN, it may complain about leaks
40# of program-lifetime variables. Disable it by default to lower
41# the noise level. This needs to happen at the start of the script,
42# before we even do our "did we build git yet" check (since we don't
43# want that one to complain to stderr).
44: ${ASAN_OPTIONS=detect_leaks=0:abort_on_error=1}
45export ASAN_OPTIONS
46
47# If LSAN is in effect we _do_ want leak checking, but we still
48# want to abort so that we notice the problems.
49: ${LSAN_OPTIONS=abort_on_error=1}
50export LSAN_OPTIONS
51
52PERL_PATH=${PERL_PATH:-perl}
53export PERL_PATH SHELL_PATH
54
55GIT_TEST_INSTALLED=${GIT_TEST_INSTALLED:-$(dirname $(type -p git))}
56GIT_TEST_CHAIN_LINT=0
57DIFF='diff'
58
59# Parse options while taking care to leave $@ intact, so we will still
60# have all the original command line options when executing the test
61# script again for '--tee' and '--verbose-log' below.
62store_arg_to=
63prev_opt=
64for opt
65do
66	if test -n "$store_arg_to"
67	then
68		eval $store_arg_to=\$opt
69		store_arg_to=
70		prev_opt=
71		continue
72	fi
73
74	case "$opt" in
75	-d|--d|--de|--deb|--debu|--debug)
76		debug=t ;;
77	-i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
78		immediate=t ;;
79	-l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
80		GIT_TEST_LONG=t; export GIT_TEST_LONG ;;
81	-r)
82		store_arg_to=run_list
83		;;
84	--run=*)
85		run_list=${opt#--*=} ;;
86	-h|--h|--he|--hel|--help)
87		help=t ;;
88	-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
89		verbose=t ;;
90	--verbose-only=*)
91		verbose_only=${opt#--*=}
92		;;
93	-q|--q|--qu|--qui|--quie|--quiet)
94		# Ignore --quiet under a TAP::Harness. Saying how many tests
95		# passed without the ok/not ok details is always an error.
96		test -z "$HARNESS_ACTIVE" && quiet=t ;;
97	--with-dashes)
98		with_dashes=t ;;
99	--no-bin-wrappers)
100		no_bin_wrappers=t ;;
101	--no-color)
102		color= ;;
103	--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
104		valgrind=memcheck
105		tee=t
106		;;
107	--valgrind=*)
108		valgrind=${opt#--*=}
109		tee=t
110		;;
111	--valgrind-only=*)
112		valgrind_only=${opt#--*=}
113		tee=t
114		;;
115	--tee)
116		tee=t ;;
117	--root=*)
118		root=${opt#--*=} ;;
119	--chain-lint)
120		GIT_TEST_CHAIN_LINT=1 ;;
121	--no-chain-lint)
122		GIT_TEST_CHAIN_LINT=0 ;;
123	-x)
124		trace=t ;;
125	-V|--verbose-log)
126		verbose_log=t
127		tee=t
128		;;
129	--write-junit-xml)
130		write_junit_xml=t
131		;;
132	--stress)
133		stress=t ;;
134	--stress=*)
135		stress=${opt#--*=}
136		case "$stress" in
137		*[!0-9]*|0*|"")
138			echo "error: --stress=<N> requires the number of jobs to run" >&2
139			exit 1
140			;;
141		*)	# Good.
142			;;
143		esac
144		;;
145	--stress-limit=*)
146		stress_limit=${opt#--*=}
147		case "$stress_limit" in
148		*[!0-9]*|0*|"")
149			echo "error: --stress-limit=<N> requires the number of repetitions" >&2
150			exit 1
151			;;
152		*)	# Good.
153			;;
154		esac
155		;;
156	*)
157		echo "error: unknown test option '$opt'" >&2; exit 1 ;;
158	esac
159
160	prev_opt=$opt
161done
162if test -n "$store_arg_to"
163then
164	echo "error: $prev_opt requires an argument" >&2
165	exit 1
166fi
167
168if test -n "$valgrind_only"
169then
170	test -z "$valgrind" && valgrind=memcheck
171	test -z "$verbose" && verbose_only="$valgrind_only"
172elif test -n "$valgrind"
173then
174	test -z "$verbose_log" && verbose=t
175fi
176
177if test -n "$stress"
178then
179	verbose=t
180	trace=t
181	immediate=t
182fi
183
184TEST_STRESS_JOB_SFX="${GIT_TEST_STRESS_JOB_NR:+.stress-$GIT_TEST_STRESS_JOB_NR}"
185TEST_NAME="$(basename "$0" .sh)"
186TEST_RESULTS_DIR="$TEST_OUTPUT_DIRECTORY/test-results"
187TEST_RESULTS_BASE="$TEST_RESULTS_DIR/$TEST_NAME$TEST_STRESS_JOB_SFX"
188TRASH_DIRECTORY="trash directory.$TEST_NAME$TEST_STRESS_JOB_SFX"
189test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
190case "$TRASH_DIRECTORY" in
191/*) ;; # absolute path is good
192 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
193esac
194
195# If --stress was passed, run this test repeatedly in several parallel loops.
196if test "$GIT_TEST_STRESS_STARTED" = "done"
197then
198	: # Don't stress test again.
199elif test -n "$stress"
200then
201	if test "$stress" != t
202	then
203		job_count=$stress
204	elif test -n "$GIT_TEST_STRESS_LOAD"
205	then
206		job_count="$GIT_TEST_STRESS_LOAD"
207	elif job_count=$(getconf _NPROCESSORS_ONLN 2>/dev/null) &&
208	     test -n "$job_count"
209	then
210		job_count=$((2 * $job_count))
211	else
212		job_count=8
213	fi
214
215	mkdir -p "$TEST_RESULTS_DIR"
216	stressfail="$TEST_RESULTS_BASE.stress-failed"
217	rm -f "$stressfail"
218
219	stress_exit=0
220	trap '
221		kill $job_pids 2>/dev/null
222		wait
223		stress_exit=1
224	' TERM INT HUP
225
226	job_pids=
227	job_nr=0
228	while test $job_nr -lt "$job_count"
229	do
230		(
231			GIT_TEST_STRESS_STARTED=done
232			GIT_TEST_STRESS_JOB_NR=$job_nr
233			export GIT_TEST_STRESS_STARTED GIT_TEST_STRESS_JOB_NR
234
235			trap '
236				kill $test_pid 2>/dev/null
237				wait
238				exit 1
239			' TERM INT
240
241			cnt=1
242			while ! test -e "$stressfail" &&
243			      { test -z "$stress_limit" ||
244				test $cnt -le $stress_limit ; }
245			do
246				$TEST_SHELL_PATH "$0" "$@" >"$TEST_RESULTS_BASE.stress-$job_nr.out" 2>&1 &
247				test_pid=$!
248
249				if wait $test_pid
250				then
251					printf "OK   %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
252				else
253					echo $GIT_TEST_STRESS_JOB_NR >>"$stressfail"
254					printf "FAIL %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
255				fi
256				cnt=$(($cnt + 1))
257			done
258		) &
259		job_pids="$job_pids $!"
260		job_nr=$(($job_nr + 1))
261	done
262
263	wait
264
265	if test -f "$stressfail"
266	then
267		stress_exit=1
268		echo "Log(s) of failed test run(s):"
269		for failed_job_nr in $(sort -n "$stressfail")
270		do
271			echo "Contents of '$TEST_RESULTS_BASE.stress-$failed_job_nr.out':"
272			cat "$TEST_RESULTS_BASE.stress-$failed_job_nr.out"
273		done
274		rm -rf "$TRASH_DIRECTORY.stress-failed"
275		# Move the last one.
276		mv "$TRASH_DIRECTORY.stress-$failed_job_nr" "$TRASH_DIRECTORY.stress-failed"
277	fi
278
279	exit $stress_exit
280fi
281
282# if --tee was passed, write the output not only to the terminal, but
283# additionally to the file test-results/$BASENAME.out, too.
284if test "$GIT_TEST_TEE_STARTED" = "done"
285then
286	: # do not redirect again
287elif test -n "$tee"
288then
289	mkdir -p "$TEST_RESULTS_DIR"
290
291	# Make this filename available to the sub-process in case it is using
292	# --verbose-log.
293	GIT_TEST_TEE_OUTPUT_FILE=$TEST_RESULTS_BASE.out
294	export GIT_TEST_TEE_OUTPUT_FILE
295
296	# Truncate before calling "tee -a" to get rid of the results
297	# from any previous runs.
298	>"$GIT_TEST_TEE_OUTPUT_FILE"
299
300	(GIT_TEST_TEE_STARTED=done ${TEST_SHELL_PATH} "$0" "$@" 2>&1;
301	 echo $? >"$TEST_RESULTS_BASE.exit") | tee -a "$GIT_TEST_TEE_OUTPUT_FILE"
302	test "$(cat "$TEST_RESULTS_BASE.exit")" = 0
303	exit
304fi
305
306if test -n "$trace" && test -n "$test_untraceable"
307then
308	# '-x' tracing requested, but this test script can't be reliably
309	# traced, unless it is run with a Bash version supporting
310	# BASH_XTRACEFD (introduced in Bash v4.1).
311	#
312	# Perform this version check _after_ the test script was
313	# potentially re-executed with $TEST_SHELL_PATH for '--tee' or
314	# '--verbose-log', so the right shell is checked and the
315	# warning is issued only once.
316	if test -n "$BASH_VERSION" && eval '
317	     test ${BASH_VERSINFO[0]} -gt 4 || {
318	       test ${BASH_VERSINFO[0]} -eq 4 &&
319	       test ${BASH_VERSINFO[1]} -ge 1
320	     }
321	   '
322	then
323		: Executed by a Bash version supporting BASH_XTRACEFD.  Good.
324	else
325		echo >&2 "warning: ignoring -x; '$0' is untraceable without BASH_XTRACEFD"
326		trace=
327	fi
328fi
329if test -n "$trace" && test -z "$verbose_log"
330then
331	verbose=t
332fi
333
334# For repeatability, reset the environment to known value.
335# TERM is sanitized below, after saving color control sequences.
336LANG=C
337LC_ALL=C
338PAGER=cat
339TZ=UTC
340export LANG LC_ALL PAGER TZ
341EDITOR=:
342
343# GIT_TEST_GETTEXT_POISON should not influence git commands executed
344# during initialization of test-lib and the test repo. Back it up,
345# unset and then restore after initialization is finished.
346if test -n "$GIT_TEST_GETTEXT_POISON"
347then
348	GIT_TEST_GETTEXT_POISON_ORIG=$GIT_TEST_GETTEXT_POISON
349	unset GIT_TEST_GETTEXT_POISON
350fi
351
352# A call to "unset" with no arguments causes at least Solaris 10
353# /usr/xpg4/bin/sh and /bin/ksh to bail out.  So keep the unsets
354# deriving from the command substitution clustered with the other
355# ones.
356unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
357	my @env = keys %ENV;
358	my $ok = join("|", qw(
359		TRACE
360		DEBUG
361		TEST
362		.*_TEST
363		PROVE
364		VALGRIND
365		UNZIP
366		PERF_
367		CURL_VERBOSE
368		TRACE_CURL
369	));
370	my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
371	print join("\n", @vars);
372')
373unset XDG_CACHE_HOME
374unset XDG_CONFIG_HOME
375unset GITPERLLIB
376GIT_AUTHOR_EMAIL=author@example.com
377GIT_AUTHOR_NAME='A U Thor'
378GIT_COMMITTER_EMAIL=committer@example.com
379GIT_COMMITTER_NAME='C O Mitter'
380GIT_MERGE_VERBOSITY=5
381GIT_MERGE_AUTOEDIT=no
382export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
383export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
384export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
385export EDITOR
386
387# Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
388GIT_TRACE_BARE=1
389export GIT_TRACE_BARE
390
391check_var_migration () {
392	# the warnings and hints given from this helper depends
393	# on end-user settings, which will disrupt the self-test
394	# done on the test framework itself.
395	case "$GIT_TEST_FRAMEWORK_SELFTEST" in
396	t)	return ;;
397	esac
398
399	old_name=$1 new_name=$2
400	eval "old_isset=\${${old_name}:+isset}"
401	eval "new_isset=\${${new_name}:+isset}"
402
403	case "$old_isset,$new_isset" in
404	isset,)
405		echo >&2 "warning: $old_name is now $new_name"
406		echo >&2 "hint: set $new_name too during the transition period"
407		eval "$new_name=\$$old_name"
408		;;
409	isset,isset)
410		# do this later
411		# echo >&2 "warning: $old_name is now $new_name"
412		# echo >&2 "hint: remove $old_name"
413		;;
414	esac
415}
416
417check_var_migration GIT_FSMONITOR_TEST GIT_TEST_FSMONITOR
418check_var_migration TEST_GIT_INDEX_VERSION GIT_TEST_INDEX_VERSION
419check_var_migration GIT_FORCE_PRELOAD_TEST GIT_TEST_PRELOAD_INDEX
420
421# Use specific version of the index file format
422if test -n "${GIT_TEST_INDEX_VERSION:+isset}"
423then
424	GIT_INDEX_VERSION="$GIT_TEST_INDEX_VERSION"
425	export GIT_INDEX_VERSION
426fi
427
428# Add libc MALLOC and MALLOC_PERTURB test
429# only if we are not executing the test with valgrind
430if test -n "$valgrind" ||
431   test -n "$TEST_NO_MALLOC_CHECK"
432then
433	setup_malloc_check () {
434		: nothing
435	}
436	teardown_malloc_check () {
437		: nothing
438	}
439else
440	setup_malloc_check () {
441		MALLOC_CHECK_=3	MALLOC_PERTURB_=165
442		export MALLOC_CHECK_ MALLOC_PERTURB_
443	}
444	teardown_malloc_check () {
445		unset MALLOC_CHECK_ MALLOC_PERTURB_
446	}
447fi
448
449# Protect ourselves from common misconfiguration to export
450# CDPATH into the environment
451unset CDPATH
452
453unset GREP_OPTIONS
454unset UNZIP
455
456case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
4571|2|true)
458	GIT_TRACE=4
459	;;
460esac
461
462# Convenience
463#
464# A regexp to match 5, 35 and 40 hexdigits
465_x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
466_x35="$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
467_x40="$_x35$_x05"
468
469# Zero SHA-1
470_z40=0000000000000000000000000000000000000000
471
472OID_REGEX="$_x40"
473ZERO_OID=$_z40
474EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
475EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
476
477# Line feed
478LF='
479'
480
481# UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
482# when case-folding filenames
483u200c=$(printf '\342\200\214')
484
485export _x05 _x35 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB ZERO_OID OID_REGEX
486
487# Each test should start with something like this, after copyright notices:
488#
489# test_description='Description of this test...
490# This test checks if command xyzzy does the right thing...
491# '
492# . ./test-lib.sh
493test "x$TERM" != "xdumb" && (
494		test -t 1 &&
495		tput bold >/dev/null 2>&1 &&
496		tput setaf 1 >/dev/null 2>&1 &&
497		tput sgr0 >/dev/null 2>&1
498	) &&
499	color=t
500
501if test -n "$color"
502then
503	# Save the color control sequences now rather than run tput
504	# each time say_color() is called.  This is done for two
505	# reasons:
506	#   * TERM will be changed to dumb
507	#   * HOME will be changed to a temporary directory and tput
508	#     might need to read ~/.terminfo from the original HOME
509	#     directory to get the control sequences
510	# Note:  This approach assumes the control sequences don't end
511	# in a newline for any terminal of interest (command
512	# substitutions strip trailing newlines).  Given that most
513	# (all?) terminals in common use are related to ECMA-48, this
514	# shouldn't be a problem.
515	say_color_error=$(tput bold; tput setaf 1) # bold red
516	say_color_skip=$(tput setaf 4) # blue
517	say_color_warn=$(tput setaf 3) # brown/yellow
518	say_color_pass=$(tput setaf 2) # green
519	say_color_info=$(tput setaf 6) # cyan
520	say_color_reset=$(tput sgr0)
521	say_color_="" # no formatting for normal text
522	say_color () {
523		test -z "$1" && test -n "$quiet" && return
524		eval "say_color_color=\$say_color_$1"
525		shift
526		printf "%s\\n" "$say_color_color$*$say_color_reset"
527	}
528else
529	say_color() {
530		test -z "$1" && test -n "$quiet" && return
531		shift
532		printf "%s\n" "$*"
533	}
534fi
535
536TERM=dumb
537export TERM
538
539error () {
540	say_color error "error: $*"
541	GIT_EXIT_OK=t
542	exit 1
543}
544
545BUG () {
546	error >&7 "bug in the test script: $*"
547}
548
549say () {
550	say_color info "$*"
551}
552
553if test -n "$HARNESS_ACTIVE"
554then
555	if test "$verbose" = t || test -n "$verbose_only"
556	then
557		printf 'Bail out! %s\n' \
558		 'verbose mode forbidden under TAP harness; try --verbose-log'
559		exit 1
560	fi
561fi
562
563test "${test_description}" != "" ||
564error "Test script did not set test_description."
565
566if test "$help" = "t"
567then
568	printf '%s\n' "$test_description"
569	exit 0
570fi
571
572exec 5>&1
573exec 6<&0
574exec 7>&2
575if test "$verbose_log" = "t"
576then
577	exec 3>>"$GIT_TEST_TEE_OUTPUT_FILE" 4>&3
578elif test "$verbose" = "t"
579then
580	exec 4>&2 3>&1
581else
582	exec 4>/dev/null 3>/dev/null
583fi
584
585# Send any "-x" output directly to stderr to avoid polluting tests
586# which capture stderr. We can do this unconditionally since it
587# has no effect if tracing isn't turned on.
588#
589# Note that this sets up the trace fd as soon as we assign the variable, so it
590# must come after the creation of descriptor 4 above. Likewise, we must never
591# unset this, as it has the side effect of closing descriptor 4, which we
592# use to show verbose tests to the user.
593#
594# Note also that we don't need or want to export it. The tracing is local to
595# this shell, and we would not want to influence any shells we exec.
596BASH_XTRACEFD=4
597
598test_failure=0
599test_count=0
600test_fixed=0
601test_broken=0
602test_success=0
603
604test_external_has_tap=0
605
606die () {
607	code=$?
608	if test -n "$GIT_EXIT_OK"
609	then
610		exit $code
611	else
612		echo >&5 "FATAL: Unexpected exit with code $code"
613		exit 1
614	fi
615}
616
617GIT_EXIT_OK=
618trap 'die' EXIT
619trap 'exit $?' INT TERM HUP
620
621# The user-facing functions are loaded from a separate file so that
622# test_perf subshells can have them too
623. "$TEST_DIRECTORY/test-lib-functions.sh"
624
625# You are not expected to call test_ok_ and test_failure_ directly, use
626# the test_expect_* functions instead.
627
628test_ok_ () {
629	if test -n "$write_junit_xml"
630	then
631		write_junit_xml_testcase "$*"
632	fi
633	test_success=$(($test_success + 1))
634	say_color "" "ok $test_count - $@"
635}
636
637test_failure_ () {
638	if test -n "$write_junit_xml"
639	then
640		junit_insert="<failure message=\"not ok $test_count -"
641		junit_insert="$junit_insert $(xml_attr_encode "$1")\">"
642		junit_insert="$junit_insert $(xml_attr_encode \
643			"$(if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
644			   then
645				test-tool path-utils skip-n-bytes \
646					"$GIT_TEST_TEE_OUTPUT_FILE" $GIT_TEST_TEE_OFFSET
647			   else
648				printf '%s\n' "$@" | sed 1d
649			   fi)")"
650		junit_insert="$junit_insert</failure>"
651		if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
652		then
653			junit_insert="$junit_insert<system-err>$(xml_attr_encode \
654				"$(cat "$GIT_TEST_TEE_OUTPUT_FILE")")</system-err>"
655		fi
656		write_junit_xml_testcase "$1" "      $junit_insert"
657	fi
658	test_failure=$(($test_failure + 1))
659	say_color error "not ok $test_count - $1"
660	shift
661	printf '%s\n' "$*" | sed -e 's/^/#	/'
662	test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
663}
664
665test_known_broken_ok_ () {
666	if test -n "$write_junit_xml"
667	then
668		write_junit_xml_testcase "$* (breakage fixed)"
669	fi
670	test_fixed=$(($test_fixed+1))
671	say_color error "ok $test_count - $@ # TODO known breakage vanished"
672}
673
674test_known_broken_failure_ () {
675	if test -n "$write_junit_xml"
676	then
677		write_junit_xml_testcase "$* (known breakage)"
678	fi
679	test_broken=$(($test_broken+1))
680	say_color warn "not ok $test_count - $@ # TODO known breakage"
681}
682
683test_debug () {
684	test "$debug" = "" || eval "$1"
685}
686
687match_pattern_list () {
688	arg="$1"
689	shift
690	test -z "$*" && return 1
691	for pattern_
692	do
693		case "$arg" in
694		$pattern_)
695			return 0
696		esac
697	done
698	return 1
699}
700
701match_test_selector_list () {
702	title="$1"
703	shift
704	arg="$1"
705	shift
706	test -z "$1" && return 0
707
708	# Both commas and whitespace are accepted as separators.
709	OLDIFS=$IFS
710	IFS=' 	,'
711	set -- $1
712	IFS=$OLDIFS
713
714	# If the first selector is negative we include by default.
715	include=
716	case "$1" in
717		!*) include=t ;;
718	esac
719
720	for selector
721	do
722		orig_selector=$selector
723
724		positive=t
725		case "$selector" in
726			!*)
727				positive=
728				selector=${selector##?}
729				;;
730		esac
731
732		test -z "$selector" && continue
733
734		case "$selector" in
735			*-*)
736				if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
737				then
738					echo "error: $title: invalid non-numeric in range" \
739						"start: '$orig_selector'" >&2
740					exit 1
741				fi
742				if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
743				then
744					echo "error: $title: invalid non-numeric in range" \
745						"end: '$orig_selector'" >&2
746					exit 1
747				fi
748				;;
749			*)
750				if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
751				then
752					echo "error: $title: invalid non-numeric in test" \
753						"selector: '$orig_selector'" >&2
754					exit 1
755				fi
756		esac
757
758		# Short cut for "obvious" cases
759		test -z "$include" && test -z "$positive" && continue
760		test -n "$include" && test -n "$positive" && continue
761
762		case "$selector" in
763			-*)
764				if test $arg -le ${selector#-}
765				then
766					include=$positive
767				fi
768				;;
769			*-)
770				if test $arg -ge ${selector%-}
771				then
772					include=$positive
773				fi
774				;;
775			*-*)
776				if test ${selector%%-*} -le $arg \
777					&& test $arg -le ${selector#*-}
778				then
779					include=$positive
780				fi
781				;;
782			*)
783				if test $arg -eq $selector
784				then
785					include=$positive
786				fi
787				;;
788		esac
789	done
790
791	test -n "$include"
792}
793
794maybe_teardown_verbose () {
795	test -z "$verbose_only" && return
796	exec 4>/dev/null 3>/dev/null
797	verbose=
798}
799
800last_verbose=t
801maybe_setup_verbose () {
802	test -z "$verbose_only" && return
803	if match_pattern_list $test_count $verbose_only
804	then
805		exec 4>&2 3>&1
806		# Emit a delimiting blank line when going from
807		# non-verbose to verbose.  Within verbose mode the
808		# delimiter is printed by test_expect_*.  The choice
809		# of the initial $last_verbose is such that before
810		# test 1, we do not print it.
811		test -z "$last_verbose" && echo >&3 ""
812		verbose=t
813	else
814		exec 4>/dev/null 3>/dev/null
815		verbose=
816	fi
817	last_verbose=$verbose
818}
819
820maybe_teardown_valgrind () {
821	test -z "$GIT_VALGRIND" && return
822	GIT_VALGRIND_ENABLED=
823}
824
825maybe_setup_valgrind () {
826	test -z "$GIT_VALGRIND" && return
827	if test -z "$valgrind_only"
828	then
829		GIT_VALGRIND_ENABLED=t
830		return
831	fi
832	GIT_VALGRIND_ENABLED=
833	if match_pattern_list $test_count $valgrind_only
834	then
835		GIT_VALGRIND_ENABLED=t
836	fi
837}
838
839want_trace () {
840	test "$trace" = t && {
841		test "$verbose" = t || test "$verbose_log" = t
842	}
843}
844
845# This is a separate function because some tests use
846# "return" to end a test_expect_success block early
847# (and we want to make sure we run any cleanup like
848# "set +x").
849test_eval_inner_ () {
850	# Do not add anything extra (including LF) after '$*'
851	eval "
852		want_trace && set -x
853		$*"
854}
855
856test_eval_ () {
857	# If "-x" tracing is in effect, then we want to avoid polluting stderr
858	# with non-test commands. But once in "set -x" mode, we cannot prevent
859	# the shell from printing the "set +x" to turn it off (nor the saving
860	# of $? before that). But we can make sure that the output goes to
861	# /dev/null.
862	#
863	# There are a few subtleties here:
864	#
865	#   - we have to redirect descriptor 4 in addition to 2, to cover
866	#     BASH_XTRACEFD
867	#
868	#   - the actual eval has to come before the redirection block (since
869	#     it needs to see descriptor 4 to set up its stderr)
870	#
871	#   - likewise, any error message we print must be outside the block to
872	#     access descriptor 4
873	#
874	#   - checking $? has to come immediately after the eval, but it must
875	#     be _inside_ the block to avoid polluting the "set -x" output
876	#
877
878	test_eval_inner_ "$@" </dev/null >&3 2>&4
879	{
880		test_eval_ret_=$?
881		if want_trace
882		then
883			set +x
884		fi
885	} 2>/dev/null 4>&2
886
887	if test "$test_eval_ret_" != 0 && want_trace
888	then
889		say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
890	fi
891	return $test_eval_ret_
892}
893
894test_run_ () {
895	test_cleanup=:
896	expecting_failure=$2
897
898	if test "${GIT_TEST_CHAIN_LINT:-1}" != 0; then
899		# turn off tracing for this test-eval, as it simply creates
900		# confusing noise in the "-x" output
901		trace_tmp=$trace
902		trace=
903		# 117 is magic because it is unlikely to match the exit
904		# code of other programs
905		if $(printf '%s\n' "$1" | sed -f "$GIT_BUILD_DIR/t/chainlint.sed" | grep -q '?![A-Z][A-Z]*?!') ||
906			test "OK-117" != "$(test_eval_ "(exit 117) && $1${LF}${LF}echo OK-\$?" 3>&1)"
907		then
908			BUG "broken &&-chain or run-away HERE-DOC: $1"
909		fi
910		trace=$trace_tmp
911	fi
912
913	setup_malloc_check
914	test_eval_ "$1"
915	eval_ret=$?
916	teardown_malloc_check
917
918	if test -z "$immediate" || test $eval_ret = 0 ||
919	   test -n "$expecting_failure" && test "$test_cleanup" != ":"
920	then
921		setup_malloc_check
922		test_eval_ "$test_cleanup"
923		teardown_malloc_check
924	fi
925	if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
926	then
927		echo ""
928	fi
929	return "$eval_ret"
930}
931
932test_start_ () {
933	test_count=$(($test_count+1))
934	maybe_setup_verbose
935	maybe_setup_valgrind
936	if test -n "$write_junit_xml"
937	then
938		junit_start=$(test-tool date getnanos)
939	fi
940}
941
942test_finish_ () {
943	echo >&3 ""
944	maybe_teardown_valgrind
945	maybe_teardown_verbose
946	if test -n "$GIT_TEST_TEE_OFFSET"
947	then
948		GIT_TEST_TEE_OFFSET=$(test-tool path-utils file-size \
949			"$GIT_TEST_TEE_OUTPUT_FILE")
950	fi
951}
952
953test_skip () {
954	to_skip=
955	skipped_reason=
956	if match_pattern_list $this_test.$test_count $GIT_SKIP_TESTS
957	then
958		to_skip=t
959		skipped_reason="GIT_SKIP_TESTS"
960	fi
961	if test -z "$to_skip" && test -n "$test_prereq" &&
962	   ! test_have_prereq "$test_prereq"
963	then
964		to_skip=t
965
966		of_prereq=
967		if test "$missing_prereq" != "$test_prereq"
968		then
969			of_prereq=" of $test_prereq"
970		fi
971		skipped_reason="missing $missing_prereq${of_prereq}"
972	fi
973	if test -z "$to_skip" && test -n "$run_list" &&
974		! match_test_selector_list '--run' $test_count "$run_list"
975	then
976		to_skip=t
977		skipped_reason="--run"
978	fi
979
980	case "$to_skip" in
981	t)
982		if test -n "$write_junit_xml"
983		then
984			message="$(xml_attr_encode "$skipped_reason")"
985			write_junit_xml_testcase "$1" \
986				"      <skipped message=\"$message\" />"
987		fi
988
989		say_color skip >&3 "skipping test: $@"
990		say_color skip "ok $test_count # skip $1 ($skipped_reason)"
991		: true
992		;;
993	*)
994		false
995		;;
996	esac
997}
998
999# stub; perf-lib overrides it
1000test_at_end_hook_ () {
1001	:
1002}
1003
1004write_junit_xml () {
1005	case "$1" in
1006	--truncate)
1007		>"$junit_xml_path"
1008		junit_have_testcase=
1009		shift
1010		;;
1011	esac
1012	printf '%s\n' "$@" >>"$junit_xml_path"
1013}
1014
1015xml_attr_encode () {
1016	printf '%s\n' "$@" | test-tool xml-encode
1017}
1018
1019write_junit_xml_testcase () {
1020	junit_attrs="name=\"$(xml_attr_encode "$this_test.$test_count $1")\""
1021	shift
1022	junit_attrs="$junit_attrs classname=\"$this_test\""
1023	junit_attrs="$junit_attrs time=\"$(test-tool \
1024		date getnanos $junit_start)\""
1025	write_junit_xml "$(printf '%s\n' \
1026		"    <testcase $junit_attrs>" "$@" "    </testcase>")"
1027	junit_have_testcase=t
1028}
1029
1030test_done () {
1031	GIT_EXIT_OK=t
1032
1033	if test -n "$write_junit_xml" && test -n "$junit_xml_path"
1034	then
1035		test -n "$junit_have_testcase" || {
1036			junit_start=$(test-tool date getnanos)
1037			write_junit_xml_testcase "all tests skipped"
1038		}
1039
1040		# adjust the overall time
1041		junit_time=$(test-tool date getnanos $junit_suite_start)
1042		sed "s/<testsuite [^>]*/& time=\"$junit_time\"/" \
1043			<"$junit_xml_path" >"$junit_xml_path.new"
1044		mv "$junit_xml_path.new" "$junit_xml_path"
1045
1046		write_junit_xml "  </testsuite>" "</testsuites>"
1047	fi
1048
1049	if test -z "$HARNESS_ACTIVE"
1050	then
1051		mkdir -p "$TEST_RESULTS_DIR"
1052
1053		cat >"$TEST_RESULTS_BASE.counts" <<-EOF
1054		total $test_count
1055		success $test_success
1056		fixed $test_fixed
1057		broken $test_broken
1058		failed $test_failure
1059
1060		EOF
1061	fi
1062
1063	if test "$test_fixed" != 0
1064	then
1065		say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
1066	fi
1067	if test "$test_broken" != 0
1068	then
1069		say_color warn "# still have $test_broken known breakage(s)"
1070	fi
1071	if test "$test_broken" != 0 || test "$test_fixed" != 0
1072	then
1073		test_remaining=$(( $test_count - $test_broken - $test_fixed ))
1074		msg="remaining $test_remaining test(s)"
1075	else
1076		test_remaining=$test_count
1077		msg="$test_count test(s)"
1078	fi
1079	case "$test_failure" in
1080	0)
1081		if test $test_external_has_tap -eq 0
1082		then
1083			if test $test_remaining -gt 0
1084			then
1085				say_color pass "# passed all $msg"
1086			fi
1087
1088			# Maybe print SKIP message
1089			test -z "$skip_all" || skip_all="# SKIP $skip_all"
1090			case "$test_count" in
1091			0)
1092				say "1..$test_count${skip_all:+ $skip_all}"
1093				;;
1094			*)
1095				test -z "$skip_all" ||
1096				say_color warn "$skip_all"
1097				say "1..$test_count"
1098				;;
1099			esac
1100		fi
1101
1102		if test -z "$debug"
1103		then
1104			test -d "$TRASH_DIRECTORY" ||
1105			error "Tests passed but trash directory already removed before test cleanup; aborting"
1106
1107			cd "$TRASH_DIRECTORY/.." &&
1108			rm -fr "$TRASH_DIRECTORY" || {
1109				# try again in a bit
1110				sleep 5;
1111				rm -fr "$TRASH_DIRECTORY"
1112			} ||
1113			error "Tests passed but test cleanup failed; aborting"
1114		fi
1115		test_at_end_hook_
1116
1117		exit 0 ;;
1118
1119	*)
1120		if test $test_external_has_tap -eq 0
1121		then
1122			say_color error "# failed $test_failure among $msg"
1123			say "1..$test_count"
1124		fi
1125
1126		exit 1 ;;
1127
1128	esac
1129}
1130
1131if test -z "$GIT_TEST_CMP"
1132then
1133	if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
1134	then
1135		GIT_TEST_CMP="$DIFF -c"
1136	else
1137		GIT_TEST_CMP="$DIFF -u"
1138	fi
1139fi
1140
1141# Test repository
1142rm -fr "$TRASH_DIRECTORY" || {
1143	GIT_EXIT_OK=t
1144	echo >&5 "FATAL: Cannot prepare test area"
1145	exit 1
1146}
1147
1148HOME="$TRASH_DIRECTORY"
1149GNUPGHOME="$HOME/gnupg-home-not-used"
1150export HOME GNUPGHOME
1151
1152if test -z "$TEST_NO_CREATE_REPO"
1153then
1154	test_create_repo "$TRASH_DIRECTORY"
1155else
1156	mkdir -p "$TRASH_DIRECTORY"
1157fi
1158
1159# Use -P to resolve symlinks in our working directory so that the cwd
1160# in subprocesses like git equals our $PWD (for pathname comparisons).
1161cd -P "$TRASH_DIRECTORY" || exit 1
1162
1163this_test=${0##*/}
1164this_test=${this_test%%-*}
1165if match_pattern_list "$this_test" $GIT_SKIP_TESTS
1166then
1167	say_color info >&3 "skipping test $this_test altogether"
1168	skip_all="skip all tests in $this_test"
1169	test_done
1170fi
1171
1172if test -n "$write_junit_xml"
1173then
1174	junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
1175	mkdir -p "$junit_xml_dir"
1176	junit_xml_base=${0##*/}
1177	junit_xml_path="$junit_xml_dir/TEST-${junit_xml_base%.sh}.xml"
1178	junit_attrs="name=\"${junit_xml_base%.sh}\""
1179	junit_attrs="$junit_attrs timestamp=\"$(TZ=UTC \
1180		date +%Y-%m-%dT%H:%M:%S)\""
1181	write_junit_xml --truncate "<testsuites>" "  <testsuite $junit_attrs>"
1182	junit_suite_start=$(test-tool date getnanos)
1183	if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
1184	then
1185		GIT_TEST_TEE_OFFSET=0
1186	fi
1187fi
1188
1189# Provide an implementation of the 'yes' utility; the upper bound
1190# limit is there to help Windows that cannot stop this loop from
1191# wasting cycles when the downstream stops reading, so do not be
1192# tempted to turn it into an infinite loop. cf. 6129c930 ("test-lib:
1193# limit the output of the yes utility", 2016-02-02)
1194yes () {
1195	if test $# = 0
1196	then
1197		y=y
1198	else
1199		y="$*"
1200	fi
1201
1202	i=0
1203	while test $i -lt 99
1204	do
1205		echo "$y"
1206		i=$(($i+1))
1207	done
1208}
1209
1210# Fix some commands on Windows
1211uname_s=$(uname -s)
1212case $uname_s in
1213*MINGW*)
1214	# Windows has its own (incompatible) sort and find
1215	sort () {
1216		/usr/bin/sort "$@"
1217	}
1218	find () {
1219		/usr/bin/find "$@"
1220	}
1221	# git sees Windows-style pwd
1222	pwd () {
1223		builtin pwd -W
1224	}
1225	# no POSIX permissions
1226	# backslashes in pathspec are converted to '/'
1227	# exec does not inherit the PID
1228	test_set_prereq MINGW
1229	test_set_prereq NATIVE_CRLF
1230	test_set_prereq SED_STRIPS_CR
1231	test_set_prereq GREP_STRIPS_CR
1232	GIT_TEST_CMP=mingw_test_cmp
1233	;;
1234*CYGWIN*)
1235	test_set_prereq POSIXPERM
1236	test_set_prereq EXECKEEPSPID
1237	test_set_prereq CYGWIN
1238	test_set_prereq SED_STRIPS_CR
1239	test_set_prereq GREP_STRIPS_CR
1240	;;
1241*)
1242	test_set_prereq POSIXPERM
1243	test_set_prereq BSLASHPSPEC
1244	test_set_prereq EXECKEEPSPID
1245	;;
1246esac
1247
1248( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
1249test -z "$NO_PERL" && test_set_prereq PERL
1250test -z "$NO_PTHREADS" && test_set_prereq PTHREADS
1251test -z "$NO_PYTHON" && test_set_prereq PYTHON
1252test -n "$USE_LIBPCRE1$USE_LIBPCRE2" && test_set_prereq PCRE
1253test -n "$USE_LIBPCRE1" && test_set_prereq LIBPCRE1
1254test -n "$USE_LIBPCRE2" && test_set_prereq LIBPCRE2
1255test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
1256
1257if test -n "$GIT_TEST_GETTEXT_POISON_ORIG"
1258then
1259	GIT_TEST_GETTEXT_POISON=$GIT_TEST_GETTEXT_POISON_ORIG
1260	unset GIT_TEST_GETTEXT_POISON_ORIG
1261fi
1262
1263# Can we rely on git's output in the C locale?
1264if test -z "$GIT_TEST_GETTEXT_POISON"
1265then
1266	test_set_prereq C_LOCALE_OUTPUT
1267fi
1268
1269if test -z "$GIT_TEST_CHECK_CACHE_TREE"
1270then
1271	GIT_TEST_CHECK_CACHE_TREE=true
1272	export GIT_TEST_CHECK_CACHE_TREE
1273fi
1274
1275test_lazy_prereq PIPE '
1276	# test whether the filesystem supports FIFOs
1277	test_have_prereq !MINGW,!CYGWIN &&
1278	rm -f testfifo && mkfifo testfifo
1279'
1280
1281test_lazy_prereq SYMLINKS '
1282	# test whether the filesystem supports symbolic links
1283	ln -s x y && test -h y
1284'
1285
1286test_lazy_prereq FILEMODE '
1287	test "$(git config --bool core.filemode)" = true
1288'
1289
1290test_lazy_prereq CASE_INSENSITIVE_FS '
1291	echo good >CamelCase &&
1292	echo bad >camelcase &&
1293	test "$(cat CamelCase)" != good
1294'
1295
1296test_lazy_prereq FUNNYNAMES '
1297	test_have_prereq !MINGW &&
1298	touch -- \
1299		"FUNNYNAMES tab	embedded" \
1300		"FUNNYNAMES \"quote embedded\"" \
1301		"FUNNYNAMES newline
1302embedded" 2>/dev/null &&
1303	rm -- \
1304		"FUNNYNAMES tab	embedded" \
1305		"FUNNYNAMES \"quote embedded\"" \
1306		"FUNNYNAMES newline
1307embedded" 2>/dev/null
1308'
1309
1310test_lazy_prereq UTF8_NFD_TO_NFC '
1311	# check whether FS converts nfd unicode to nfc
1312	auml=$(printf "\303\244")
1313	aumlcdiar=$(printf "\141\314\210")
1314	>"$auml" &&
1315	test -f "$aumlcdiar"
1316'
1317
1318test_lazy_prereq AUTOIDENT '
1319	sane_unset GIT_AUTHOR_NAME &&
1320	sane_unset GIT_AUTHOR_EMAIL &&
1321	git var GIT_AUTHOR_IDENT
1322'
1323
1324test_lazy_prereq EXPENSIVE '
1325	test -n "$GIT_TEST_LONG"
1326'
1327
1328test_lazy_prereq EXPENSIVE_ON_WINDOWS '
1329	test_have_prereq EXPENSIVE || test_have_prereq !MINGW,!CYGWIN
1330'
1331
1332test_lazy_prereq USR_BIN_TIME '
1333	test -x /usr/bin/time
1334'
1335
1336test_lazy_prereq NOT_ROOT '
1337	uid=$(id -u) &&
1338	test "$uid" != 0
1339'
1340
1341test_lazy_prereq JGIT '
1342	type jgit
1343'
1344
1345# SANITY is about "can you correctly predict what the filesystem would
1346# do by only looking at the permission bits of the files and
1347# directories?"  A typical example of !SANITY is running the test
1348# suite as root, where a test may expect "chmod -r file && cat file"
1349# to fail because file is supposed to be unreadable after a successful
1350# chmod.  In an environment (i.e. combination of what filesystem is
1351# being used and who is running the tests) that lacks SANITY, you may
1352# be able to delete or create a file when the containing directory
1353# doesn't have write permissions, or access a file even if the
1354# containing directory doesn't have read or execute permissions.
1355
1356test_lazy_prereq SANITY '
1357	mkdir SANETESTD.1 SANETESTD.2 &&
1358
1359	chmod +w SANETESTD.1 SANETESTD.2 &&
1360	>SANETESTD.1/x 2>SANETESTD.2/x &&
1361	chmod -w SANETESTD.1 &&
1362	chmod -r SANETESTD.1/x &&
1363	chmod -rx SANETESTD.2 ||
1364	BUG "cannot prepare SANETESTD"
1365
1366	! test -r SANETESTD.1/x &&
1367	! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
1368	status=$?
1369
1370	chmod +rwx SANETESTD.1 SANETESTD.2 &&
1371	rm -rf SANETESTD.1 SANETESTD.2 ||
1372	BUG "cannot clean SANETESTD"
1373	return $status
1374'
1375
1376test FreeBSD != $uname_s || GIT_UNZIP=${GIT_UNZIP:-/usr/local/bin/unzip}
1377GIT_UNZIP=${GIT_UNZIP:-unzip}
1378test_lazy_prereq UNZIP '
1379	"$GIT_UNZIP" -v
1380	test $? -ne 127
1381'
1382
1383run_with_limited_cmdline () {
1384	(ulimit -s 128 && "$@")
1385}
1386
1387test_lazy_prereq CMDLINE_LIMIT '
1388	test_have_prereq !MINGW,!CYGWIN &&
1389	run_with_limited_cmdline true
1390'
1391
1392run_with_limited_stack () {
1393	(ulimit -s 128 && "$@")
1394}
1395
1396test_lazy_prereq ULIMIT_STACK_SIZE '
1397	test_have_prereq !MINGW,!CYGWIN &&
1398	run_with_limited_stack true
1399'
1400
1401build_option () {
1402	git version --build-options |
1403	sed -ne "s/^$1: //p"
1404}
1405
1406test_lazy_prereq LONG_IS_64BIT '
1407	test 8 -le "$(build_option sizeof-long)"
1408'
1409
1410test_lazy_prereq TIME_IS_64BIT 'test-tool date is64bit'
1411test_lazy_prereq TIME_T_IS_64BIT 'test-tool date time_t-is64bit'
1412
1413test_lazy_prereq CURL '
1414	curl --version
1415'
1416
1417# SHA1 is a test if the hash algorithm in use is SHA-1.  This is both for tests
1418# which will not work with other hash algorithms and tests that work but don't
1419# test anything meaningful (e.g. special values which cause short collisions).
1420test_lazy_prereq SHA1 '
1421	test $(git hash-object /dev/null) = e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
1422'
1423
1424test_lazy_prereq REBASE_P '
1425	test -z "$GIT_TEST_SKIP_REBASE_P"
1426'
1427