test-lib.sh 21.6 KB
Newer Older
1
# Test framework for git.  See t/README for usage.
2 3 4
#
# Copyright (c) 2005 Junio C Hamano
#
5 6 7 8 9 10 11 12 13 14 15 16
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/ .
17

18 19 20
# Keep the original TERM for say_color
ORIGINAL_TERM=$TERM

21 22 23 24 25 26 27 28
# Test the binaries we have just built.  The tests are kept in
# t/ subdirectory and are run in 'trash directory' subdirectory.
if test -z "$TEST_DIRECTORY"
then
	# We allow tests to override this, in case they want to run tests
	# outside of t/, e.g. for running tests on the test library
	# itself.
	TEST_DIRECTORY=$(pwd)
29 30 31 32
else
	# ensure that TEST_DIRECTORY is an absolute path so that it
	# is valid even if the current working directory is changed
	TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
33 34 35 36 37 38 39 40 41
fi
if test -z "$TEST_OUTPUT_DIRECTORY"
then
	# Similarly, override this to store the test-results subdir
	# elsewhere
	TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
fi
GIT_BUILD_DIR="$TEST_DIRECTORY"/..

42 43 44 45 46 47 48 49 50
################################################################
# It appears that people try to run tests without building...
"$GIT_BUILD_DIR/git" >/dev/null
if test $? != 1
then
	echo >&2 'error: you do not seem to have built git yet.'
	exit 1
fi

51 52 53
. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
export PERL_PATH SHELL_PATH

54 55 56 57 58 59 60
# if --tee was passed, write the output not only to the terminal, but
# additionally to the file test-results/$BASENAME.out, too.
case "$GIT_TEST_TEE_STARTED, $* " in
done,*)
	# do not redirect again
	;;
*' --tee '*|*' --va'*)
61 62
	mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
	BASE="$TEST_OUTPUT_DIRECTORY/test-results/$(basename "$0" .sh)"
63 64 65 66 67 68 69
	(GIT_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1;
	 echo $? > $BASE.exit) | tee $BASE.out
	test "$(cat $BASE.exit)" = 0
	exit
	;;
esac

70 71
# For repeatability, reset the environment to known value.
LANG=C
72
LC_ALL=C
73
PAGER=cat
74
TZ=UTC
75 76
TERM=dumb
export LANG LC_ALL PAGER TERM TZ
77
EDITOR=:
78 79 80 81
# A call to "unset" with no arguments causes at least Solaris 10
# /usr/xpg4/bin/sh and /bin/ksh to bail out.  So keep the unsets
# deriving from the command substitution clustered with the other
# ones.
82
unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
83
	my @env = keys %ENV;
84 85 86 87 88 89 90 91
	my $ok = join("|", qw(
		TRACE
		DEBUG
		USE_LOOKUP
		TEST
		.*_TEST
		PROVE
		VALGRIND
92
		UNZIP
93
		PERF_
94
		CURL_VERBOSE
95 96
	));
	my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
97 98
	print join("\n", @vars);
')
J
Jeff King 已提交
99
unset XDG_CONFIG_HOME
100
unset GITPERLLIB
101 102 103 104
GIT_AUTHOR_EMAIL=author@example.com
GIT_AUTHOR_NAME='A U Thor'
GIT_COMMITTER_EMAIL=committer@example.com
GIT_COMMITTER_NAME='C O Mitter'
105
GIT_MERGE_VERBOSITY=5
106 107
GIT_MERGE_AUTOEDIT=no
export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
108 109
export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
110
export EDITOR
111

112 113 114 115
# Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
GIT_TRACE_BARE=1
export GIT_TRACE_BARE

116 117 118 119 120 121
if test -n "${TEST_GIT_INDEX_VERSION:+isset}"
then
	GIT_INDEX_VERSION="$TEST_GIT_INDEX_VERSION"
	export GIT_INDEX_VERSION
fi

122 123
# Add libc MALLOC and MALLOC_PERTURB test
# only if we are not executing the test with valgrind
J
Junio C Hamano 已提交
124
if expr " $GIT_TEST_OPTS " : ".* --valgrind " >/dev/null ||
125
   test -n "$TEST_NO_MALLOC_CHECK"
J
Junio C Hamano 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
then
	setup_malloc_check () {
		: nothing
	}
	teardown_malloc_check () {
		: nothing
	}
else
	setup_malloc_check () {
		MALLOC_CHECK_=3	MALLOC_PERTURB_=165
		export MALLOC_CHECK_ MALLOC_PERTURB_
	}
	teardown_malloc_check () {
		unset MALLOC_CHECK_ MALLOC_PERTURB_
	}
fi
142

143 144 145 146
# Protect ourselves from common misconfiguration to export
# CDPATH into the environment
unset CDPATH

B
Bert Wesarg 已提交
147
unset GREP_OPTIONS
148
unset UNZIP
B
Bert Wesarg 已提交
149

150
case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
151 152 153 154 155 156
1|2|true)
	echo "* warning: Some tests will not work if GIT_TRACE" \
		"is set as to trace on STDERR ! *"
	echo "* warning: Please set GIT_TRACE to something" \
		"other than 1, 2 or true ! *"
	;;
157 158
esac

159 160 161 162 163 164
# Convenience
#
# A regexp to match 5 and 40 hexdigits
_x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
_x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"

J
Junio C Hamano 已提交
165 166 167
# Zero SHA-1
_z40=0000000000000000000000000000000000000000

168 169 170 171
# Line feed
LF='
'

172 173 174 175 176
# UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
# when case-folding filenames
u200c=$(printf '\342\200\214')

export _x05 _x40 _z40 LF u200c
177

178 179 180 181 182 183
# Each test should start with something like this, after copyright notices:
#
# test_description='Description of this test...
# This test checks if command xyzzy does the right thing...
# '
# . ./test-lib.sh
184 185 186 187 188 189 190 191
[ "x$ORIGINAL_TERM" != "xdumb" ] && (
		TERM=$ORIGINAL_TERM &&
		export TERM &&
		[ -t 1 ] &&
		tput bold >/dev/null 2>&1 &&
		tput setaf 1 >/dev/null 2>&1 &&
		tput sgr0 >/dev/null 2>&1
	) &&
192
	color=t
193 194 195 196 197 198

while test "$#" -ne 0
do
	case "$1" in
	-d|--d|--de|--deb|--debu|--debug)
		debug=t; shift ;;
J
Junio C Hamano 已提交
199 200
	-i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
		immediate=t; shift ;;
201
	-l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
J
Junio C Hamano 已提交
202
		GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
203 204 205 206 207 208 209 210
	-r)
		shift; test "$#" -ne 0 || {
			echo 'error: -r requires an argument' >&2;
			exit 1;
		}
		run_list=$1; shift ;;
	--run=*)
		run_list=$(expr "z$1" : 'z[^=]*=\(.*\)'); shift ;;
211
	-h|--h|--he|--hel|--help)
212
		help=t; shift ;;
213 214
	-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
		verbose=t; shift ;;
215 216 217
	--verbose-only=*)
		verbose_only=$(expr "z$1" : 'z[^=]*=\(.*\)')
		shift ;;
218
	-q|--q|--qu|--qui|--quie|--quiet)
219 220 221
		# Ignore --quiet under a TAP::Harness. Saying how many tests
		# passed without the ok/not ok details is always an error.
		test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
222 223
	--with-dashes)
		with_dashes=t; shift ;;
224
	--no-color)
225
		color=; shift ;;
226
	--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
227 228 229 230 231
		valgrind=memcheck
		shift ;;
	--valgrind=*)
		valgrind=$(expr "z$1" : 'z[^=]*=\(.*\)')
		shift ;;
232 233 234
	--valgrind-only=*)
		valgrind_only=$(expr "z$1" : 'z[^=]*=\(.*\)')
		shift ;;
235 236
	--tee)
		shift ;; # was handled already
237 238 239
	--root=*)
		root=$(expr "z$1" : 'z[^=]*=\(.*\)')
		shift ;;
240
	*)
241
		echo "error: unknown test option '$1'" >&2; exit 1 ;;
242 243 244
	esac
done

245
if test -n "$valgrind_only"
246 247 248 249 250 251 252
then
	test -z "$valgrind" && valgrind=memcheck
	test -z "$verbose" && verbose_only="$valgrind_only"
elif test -n "$valgrind"
then
	verbose=t
fi
253

254 255
if test -n "$color"
then
256
	say_color () {
257 258 259
		(
		TERM=$ORIGINAL_TERM
		export TERM
260
		case "$1" in
261 262 263
		error)
			tput bold; tput setaf 1;; # bold red
		skip)
A
Adam Spiers 已提交
264
			tput setaf 4;; # blue
265 266
		warn)
			tput setaf 3;; # brown/yellow
267
		pass)
268
			tput setaf 2;; # green
269
		info)
270
			tput setaf 6;; # cyan
271 272
		*)
			test -n "$quiet" && return;;
273 274
		esac
		shift
275
		printf "%s" "$*"
276
		tput sgr0
277
		echo
278
		)
279 280 281
	}
else
	say_color() {
282
		test -z "$1" && test -n "$quiet" && return
283
		shift
284
		printf "%s\n" "$*"
285 286 287 288 289
	}
fi

error () {
	say_color error "error: $*"
290
	GIT_EXIT_OK=t
291 292 293 294 295 296 297
	exit 1
}

say () {
	say_color info "$*"
}

298 299 300 301 302
test "${test_description}" != "" ||
error "Test script did not set test_description."

if test "$help" = "t"
then
303
	printf '%s\n' "$test_description"
304 305 306
	exit 0
fi

307
exec 5>&1
J
Jeff King 已提交
308
exec 6<&0
309 310 311 312 313 314 315 316 317
if test "$verbose" = "t"
then
	exec 4>&2 3>&1
else
	exec 4>/dev/null 3>/dev/null
fi

test_failure=0
test_count=0
J
Junio C Hamano 已提交
318 319
test_fixed=0
test_broken=0
320
test_success=0
321

322 323
test_external_has_tap=0

324
die () {
325 326 327 328 329 330 331 332
	code=$?
	if test -n "$GIT_EXIT_OK"
	then
		exit $code
	else
		echo >&5 "FATAL: Unexpected exit with code $code"
		exit 1
	fi
333 334
}

335
GIT_EXIT_OK=
336
trap 'die' EXIT
337

338 339
# The user-facing functions are loaded from a separate file so that
# test_perf subshells can have them too
340
. "$TEST_DIRECTORY/test-lib-functions.sh"
341

J
Junio C Hamano 已提交
342
# You are not expected to call test_ok_ and test_failure_ directly, use
T
Torstein Hegge 已提交
343
# the test_expect_* functions instead.
J
Junio C Hamano 已提交
344 345

test_ok_ () {
346
	test_success=$(($test_success + 1))
347
	say_color "" "ok $test_count - $@"
348 349
}

J
Junio C Hamano 已提交
350
test_failure_ () {
351
	test_failure=$(($test_failure + 1))
352
	say_color error "not ok $test_count - $1"
353
	shift
354
	printf '%s\n' "$*" | sed -e 's/^/#	/'
355
	test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
J
Junio C Hamano 已提交
356 357
}

J
Junio C Hamano 已提交
358 359
test_known_broken_ok_ () {
	test_fixed=$(($test_fixed+1))
360
	say_color error "ok $test_count - $@ # TODO known breakage vanished"
J
Junio C Hamano 已提交
361 362 363 364
}

test_known_broken_failure_ () {
	test_broken=$(($test_broken+1))
365
	say_color warn "not ok $test_count - $@ # TODO known breakage"
J
Junio C Hamano 已提交
366
}
J
Junio C Hamano 已提交
367 368

test_debug () {
369
	test "$debug" = "" || eval "$1"
370 371
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385
match_pattern_list () {
	arg="$1"
	shift
	test -z "$*" && return 1
	for pattern_
	do
		case "$arg" in
		$pattern_)
			return 0
		esac
	done
	return 1
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
match_test_selector_list () {
	title="$1"
	shift
	arg="$1"
	shift
	test -z "$1" && return 0

	# Both commas and whitespace are accepted as separators.
	OLDIFS=$IFS
	IFS=' 	,'
	set -- $1
	IFS=$OLDIFS

	# If the first selector is negative we include by default.
	include=
	case "$1" in
		!*) include=t ;;
	esac

	for selector
	do
		orig_selector=$selector

		positive=t
		case "$selector" in
			!*)
				positive=
				selector=${selector##?}
				;;
		esac

		test -z "$selector" && continue

		case "$selector" in
			*-*)
				if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
				then
					echo "error: $title: invalid non-numeric in range" \
						"start: '$orig_selector'" >&2
					exit 1
				fi
				if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
				then
					echo "error: $title: invalid non-numeric in range" \
						"end: '$orig_selector'" >&2
					exit 1
				fi
				;;
			*)
				if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
				then
					echo "error: $title: invalid non-numeric in test" \
						"selector: '$orig_selector'" >&2
					exit 1
				fi
		esac

		# Short cut for "obvious" cases
		test -z "$include" && test -z "$positive" && continue
		test -n "$include" && test -n "$positive" && continue

		case "$selector" in
			-*)
				if test $arg -le ${selector#-}
				then
					include=$positive
				fi
				;;
			*-)
				if test $arg -ge ${selector%-}
				then
					include=$positive
				fi
				;;
			*-*)
				if test ${selector%%-*} -le $arg \
					&& test $arg -le ${selector#*-}
				then
					include=$positive
				fi
				;;
			*)
				if test $arg -eq $selector
				then
					include=$positive
				fi
				;;
		esac
	done

	test -n "$include"
}

479 480 481 482 483 484 485 486 487
maybe_teardown_verbose () {
	test -z "$verbose_only" && return
	exec 4>/dev/null 3>/dev/null
	verbose=
}

last_verbose=t
maybe_setup_verbose () {
	test -z "$verbose_only" && return
488
	if match_pattern_list $test_count $verbose_only
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
	then
		exec 4>&2 3>&1
		# Emit a delimiting blank line when going from
		# non-verbose to verbose.  Within verbose mode the
		# delimiter is printed by test_expect_*.  The choice
		# of the initial $last_verbose is such that before
		# test 1, we do not print it.
		test -z "$last_verbose" && echo >&3 ""
		verbose=t
	else
		exec 4>/dev/null 3>/dev/null
		verbose=
	fi
	last_verbose=$verbose
}

505 506 507 508 509 510 511
maybe_teardown_valgrind () {
	test -z "$GIT_VALGRIND" && return
	GIT_VALGRIND_ENABLED=
}

maybe_setup_valgrind () {
	test -z "$GIT_VALGRIND" && return
512
	if test -z "$valgrind_only"
513 514 515 516 517 518 519 520 521 522 523
	then
		GIT_VALGRIND_ENABLED=t
		return
	fi
	GIT_VALGRIND_ENABLED=
	if match_pattern_list $test_count $valgrind_only
	then
		GIT_VALGRIND_ENABLED=t
	fi
}

524 525 526
test_eval_ () {
	# This is a separate function because some tests use
	# "return" to end a test_expect_success block early.
J
Jeff King 已提交
527
	eval </dev/null >&3 2>&4 "$*"
528 529
}

530
test_run_ () {
531
	test_cleanup=:
532
	expecting_failure=$2
533
	setup_malloc_check
534
	test_eval_ "$1"
535
	eval_ret=$?
536
	teardown_malloc_check
537 538 539

	if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
	then
J
Junio C Hamano 已提交
540
		setup_malloc_check
541
		test_eval_ "$test_cleanup"
J
Junio C Hamano 已提交
542
		teardown_malloc_check
543
	fi
544 545
	if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
	then
546 547
		echo ""
	fi
548
	return "$eval_ret"
549 550
}

551
test_start_ () {
552
	test_count=$(($test_count+1))
553
	maybe_setup_verbose
554
	maybe_setup_valgrind
555 556 557 558
}

test_finish_ () {
	echo >&3 ""
559
	maybe_teardown_valgrind
560
	maybe_teardown_verbose
561 562 563
}

test_skip () {
564
	to_skip=
565
	skipped_reason=
566 567 568
	if match_pattern_list $this_test.$test_count $GIT_SKIP_TESTS
	then
		to_skip=t
569
		skipped_reason="GIT_SKIP_TESTS"
570
	fi
571 572
	if test -z "$to_skip" && test -n "$test_prereq" &&
	   ! test_have_prereq "$test_prereq"
573 574
	then
		to_skip=t
575

576
		of_prereq=
577
		if test "$missing_prereq" != "$test_prereq"
578
		then
579
			of_prereq=" of $test_prereq"
580
		fi
581 582
		skipped_reason="missing $missing_prereq${of_prereq}"
	fi
583 584 585 586 587 588
	if test -z "$to_skip" && test -n "$run_list" &&
		! match_test_selector_list '--run' $test_count "$run_list"
	then
		to_skip=t
		skipped_reason="--run"
	fi
589

590 591
	case "$to_skip" in
	t)
592
		say_color skip >&3 "skipping test: $@"
593
		say_color skip "ok $test_count # skip $1 ($skipped_reason)"
594 595 596 597 598 599 600 601
		: true
		;;
	*)
		false
		;;
	esac
}

602 603 604 605 606
# stub; perf-lib overrides it
test_at_end_hook_ () {
	:
}

607
test_done () {
608
	GIT_EXIT_OK=t
609

610 611
	if test -z "$HARNESS_ACTIVE"
	then
612
		test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
613
		mkdir -p "$test_results_dir"
614 615
		base=${0##*/}
		test_results_path="$test_results_dir/${base%.sh}-$$.counts"
616

617 618 619 620 621 622 623 624
		cat >>"$test_results_path" <<-EOF
		total $test_count
		success $test_success
		fixed $test_fixed
		broken $test_broken
		failed $test_failure

		EOF
625
	fi
J
Junio C Hamano 已提交
626 627 628

	if test "$test_fixed" != 0
	then
629
		say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
J
Junio C Hamano 已提交
630 631 632
	fi
	if test "$test_broken" != 0
	then
633
		say_color warn "# still have $test_broken known breakage(s)"
634 635 636 637 638
	fi
	if test "$test_broken" != 0 || test "$test_fixed" != 0
	then
		test_remaining=$(( $test_count - $test_broken - $test_fixed ))
		msg="remaining $test_remaining test(s)"
639
	else
640
		test_remaining=$test_count
641
		msg="$test_count test(s)"
J
Junio C Hamano 已提交
642
	fi
643
	case "$test_failure" in
644
	0)
645
		# Maybe print SKIP message
646 647 648 649
		if test -n "$skip_all" && test $test_count -gt 0
		then
			error "Can't use skip_all after running some tests"
		fi
650 651
		[ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"

652 653
		if test $test_external_has_tap -eq 0
		then
654
			if test $test_remaining -gt 0
655
			then
656
				say_color pass "# passed all $msg"
657
			fi
658
			say "1..$test_count$skip_all"
659
		fi
J
Johannes Schindelin 已提交
660 661 662 663 664

		test -d "$remove_trash" &&
		cd "$(dirname "$remove_trash")" &&
		rm -rf "$(basename "$remove_trash")"

665 666
		test_at_end_hook_

667 668 669
		exit 0 ;;

	*)
670 671
		if test $test_external_has_tap -eq 0
		then
672 673
			say_color error "# failed $test_failure among $msg"
			say "1..$test_count"
674
		fi
675

676 677 678 679 680
		exit 1 ;;

	esac
}

681
if test -n "$valgrind"
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
then
	make_symlink () {
		test -h "$2" &&
		test "$1" = "$(readlink "$2")" || {
			# be super paranoid
			if mkdir "$2".lock
			then
				rm -f "$2" &&
				ln -s "$1" "$2" &&
				rm -r "$2".lock
			else
				while test -d "$2".lock
				do
					say "Waiting for lock on $2."
					sleep 1
				done
			fi
		}
	}

	make_valgrind_symlink () {
703
		# handle only executables, unless they are shell libraries that
704
		# need to be in the exec-path.
705
		test -x "$1" ||
706
		test "# " = "$(head -c 2 <"$1")" ||
707
		return;
708 709

		base=$(basename "$1")
710
		symlink_target=$GIT_BUILD_DIR/$base
711 712 713 714 715 716 717
		# do not override scripts
		if test -x "$symlink_target" &&
		    test ! -d "$symlink_target" &&
		    test "#!" != "$(head -c 2 < "$symlink_target")"
		then
			symlink_target=../valgrind.sh
		fi
718 719 720 721
		case "$base" in
		*.sh|*.perl)
			symlink_target=../unprocessed-script
		esac
722 723 724 725
		# create the link, or replace it if it is out of date
		make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
	}

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	# override all git executables in TEST_DIRECTORY/..
	GIT_VALGRIND=$TEST_DIRECTORY/valgrind
	mkdir -p "$GIT_VALGRIND"/bin
	for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/test-*
	do
		make_valgrind_symlink $file
	done
	# special-case the mergetools loadables
	make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
	OLDIFS=$IFS
	IFS=:
	for path in $PATH
	do
		ls "$path"/git-* 2> /dev/null |
		while read file
741
		do
742
			make_valgrind_symlink "$file"
743
		done
744 745
	done
	IFS=$OLDIFS
746 747 748
	PATH=$GIT_VALGRIND/bin:$PATH
	GIT_EXEC_PATH=$GIT_VALGRIND/bin
	export GIT_VALGRIND
749 750
	GIT_VALGRIND_MODE="$valgrind"
	export GIT_VALGRIND_MODE
751
	GIT_VALGRIND_ENABLED=t
752
	test -n "$valgrind_only" && GIT_VALGRIND_ENABLED=
753
	export GIT_VALGRIND_ENABLED
754 755
elif test -n "$GIT_TEST_INSTALLED"
then
756 757
	GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)  ||
	error "Cannot run git from $GIT_TEST_INSTALLED."
758
	PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH
759 760
	GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
else # normal case, use ../bin-wrappers only unless $with_dashes:
761
	git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
762 763 764 765
	if ! test -x "$git_bin_dir/git"
	then
		if test -z "$with_dashes"
		then
766 767 768 769 770
			say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
		fi
		with_dashes=t
	fi
	PATH="$git_bin_dir:$PATH"
771
	GIT_EXEC_PATH=$GIT_BUILD_DIR
772 773
	if test -n "$with_dashes"
	then
774
		PATH="$GIT_BUILD_DIR:$PATH"
775
	fi
776
fi
777
GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
J
Jeff King 已提交
778
GIT_CONFIG_NOSYSTEM=1
779
GIT_ATTR_NOSYSTEM=1
780
export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
781

782 783 784 785 786 787 788 789 790 791
if test -z "$GIT_TEST_CMP"
then
	if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
	then
		GIT_TEST_CMP="$DIFF -c"
	else
		GIT_TEST_CMP="$DIFF -u"
	fi
fi

792
GITPERLLIB="$GIT_BUILD_DIR"/perl/blib/lib:"$GIT_BUILD_DIR"/perl/blib/arch/auto/Git
793
export GITPERLLIB
794
test -d "$GIT_BUILD_DIR"/templates/blt || {
795 796
	error "You haven't built things yet, have you?"
}
797

798 799
if ! test -x "$GIT_BUILD_DIR"/test-chmtime
then
E
Eric Wong 已提交
800 801 802 803 804
	echo >&2 'You need to build test-chmtime:'
	echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
	exit 1
fi

805
# Test repository
J
Jeff King 已提交
806 807 808 809 810
TRASH_DIRECTORY="trash directory.$(basename "$0" .sh)"
test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
case "$TRASH_DIRECTORY" in
/*) ;; # absolute path is good
 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
811
esac
812
test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
813
rm -fr "$TRASH_DIRECTORY" || {
814
	GIT_EXIT_OK=t
815 816 817 818
	echo >&5 "FATAL: Cannot prepare test area"
	exit 1
}

819 820 821
HOME="$TRASH_DIRECTORY"
export HOME

822 823
if test -z "$TEST_NO_CREATE_REPO"
then
824
	test_create_repo "$TRASH_DIRECTORY"
825
else
826
	mkdir -p "$TRASH_DIRECTORY"
827
fi
828 829
# Use -P to resolve symlinks in our working directory so that the cwd
# in subprocesses like git equals our $PWD (for pathname comparisons).
830
cd -P "$TRASH_DIRECTORY" || exit 1
831

832 833
this_test=${0##*/}
this_test=${this_test%%-*}
834 835 836 837 838 839
if match_pattern_list "$this_test" $GIT_SKIP_TESTS
then
	say_color info >&3 "skipping test $this_test altogether"
	skip_all="skip all tests in $this_test"
	test_done
fi
840

841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
# Provide an implementation of the 'yes' utility
yes () {
	if test $# = 0
	then
		y=y
	else
		y="$*"
	fi

	while echo "$y"
	do
		:
	done
}

856 857 858 859 860 861 862 863 864 865
# Fix some commands on Windows
case $(uname -s) in
*MINGW*)
	# Windows has its own (incompatible) sort and find
	sort () {
		/usr/bin/sort "$@"
	}
	find () {
		/usr/bin/find "$@"
	}
866 867 868
	sum () {
		md5sum "$@"
	}
869 870 871 872
	# git sees Windows-style pwd
	pwd () {
		builtin pwd -W
	}
873
	# no POSIX permissions
874
	# backslashes in pathspec are converted to '/'
875
	# exec does not inherit the PID
876
	test_set_prereq MINGW
877
	test_set_prereq NOT_CYGWIN
878
	test_set_prereq SED_STRIPS_CR
879
	test_set_prereq GREP_STRIPS_CR
880
	GIT_TEST_CMP=mingw_test_cmp
881 882 883 884 885
	;;
*CYGWIN*)
	test_set_prereq POSIXPERM
	test_set_prereq EXECKEEPSPID
	test_set_prereq NOT_MINGW
886
	test_set_prereq CYGWIN
887
	test_set_prereq SED_STRIPS_CR
888
	test_set_prereq GREP_STRIPS_CR
889 890 891
	;;
*)
	test_set_prereq POSIXPERM
892
	test_set_prereq BSLASHPSPEC
893
	test_set_prereq EXECKEEPSPID
P
Pat Thoyts 已提交
894
	test_set_prereq NOT_MINGW
895
	test_set_prereq NOT_CYGWIN
896 897
	;;
esac
898

899
( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
900
test -z "$NO_PERL" && test_set_prereq PERL
901
test -z "$NO_PYTHON" && test_set_prereq PYTHON
M
Michał Kiedrowicz 已提交
902
test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
903
test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
904

905
# Can we rely on git's output in the C locale?
906 907 908 909
if test -n "$GETTEXT_POISON"
then
	GIT_GETTEXT_POISON=YesPlease
	export GIT_GETTEXT_POISON
910
	test_set_prereq GETTEXT_POISON
911 912 913
else
	test_set_prereq C_LOCALE_OUTPUT
fi
914

915 916 917 918 919 920 921 922
# Use this instead of test_cmp to compare files that contain expected and
# actual output from git commands that can be translated.  When running
# under GETTEXT_POISON this pretends that the command produced expected
# results.
test_i18ncmp () {
	test -n "$GETTEXT_POISON" || test_cmp "$@"
}

923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
# Use this instead of "grep expected-string actual" to see if the
# output from a git command that can be translated either contains an
# expected string, or does not contain an unwanted one.  When running
# under GETTEXT_POISON this pretends that the command produced expected
# results.
test_i18ngrep () {
	if test -n "$GETTEXT_POISON"
	then
	    : # pretend success
	elif test "x!" = "x$1"
	then
		shift
		! grep "$@"
	else
		grep "$@"
	fi
}

941 942
test_lazy_prereq PIPE '
	# test whether the filesystem supports FIFOs
943 944 945 946 947 948 949 950
	case $(uname -s) in
	CYGWIN*)
		false
		;;
	*)
		rm -f testfifo && mkfifo testfifo
		;;
	esac
951 952
'

953 954 955 956
test_lazy_prereq SYMLINKS '
	# test whether the filesystem supports symbolic links
	ln -s x y && test -h y
'
957

958 959 960 961
test_lazy_prereq FILEMODE '
	test "$(git config --bool core.filemode)" = true
'

962 963 964 965 966 967
test_lazy_prereq CASE_INSENSITIVE_FS '
	echo good >CamelCase &&
	echo bad >camelcase &&
	test "$(cat CamelCase)" != good
'

968 969 970 971 972 973 974 975 976 977 978 979 980
test_lazy_prereq UTF8_NFD_TO_NFC '
	# check whether FS converts nfd unicode to nfc
	auml=$(printf "\303\244")
	aumlcdiar=$(printf "\141\314\210")
	>"$auml" &&
	case "$(echo *)" in
	"$aumlcdiar")
		true ;;
	*)
		false ;;
	esac
'

981 982 983 984 985 986
test_lazy_prereq AUTOIDENT '
	sane_unset GIT_AUTHOR_NAME &&
	sane_unset GIT_AUTHOR_EMAIL &&
	git var GIT_AUTHOR_IDENT
'

987 988 989 990
test_lazy_prereq EXPENSIVE '
	test -n "$GIT_TEST_LONG"
'

991 992 993 994
test_lazy_prereq USR_BIN_TIME '
	test -x /usr/bin/time
'

995 996 997 998 999
test_lazy_prereq NOT_ROOT '
	uid=$(id -u) &&
	test "$uid" != 0
'

1000 1001 1002
# When the tests are run as root, permission tests will report that
# things are writable when they shouldn't be.
test -w / || test_set_prereq SANITY
1003 1004 1005 1006 1007 1008

GIT_UNZIP=${GIT_UNZIP:-unzip}
test_lazy_prereq UNZIP '
	"$GIT_UNZIP" -v
	test $? -ne 127
'