PageRenderTime 49ms CodeModel.GetById 10ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/test-lib.sh

https://code.google.com/
Shell | 1007 lines | 704 code | 106 blank | 197 comment | 100 complexity | 3dd93e97b58820ba52c99b38b6558490 MD5 | raw file
   1#!/bin/sh
   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# if --tee was passed, write the output not only to the terminal, but
  19# additionally to the file test-results/$BASENAME.out, too.
  20
  21test_name=$(basename "$0" .sh)
  22
  23case "$GIT_TEST_TEE_STARTED, $* " in
  24done,*)
  25	# do not redirect again
  26	;;
  27*' --tee '*|*' --va'*)
  28	mkdir -p test-results
  29	BASE=test-results/$test_name
  30	(GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
  31	 echo $? > $BASE.exit) | tee $BASE.out
  32	test "$(cat $BASE.exit)" = 0
  33	exit
  34	;;
  35esac
  36
  37# Keep the original TERM for say_color
  38ORIGINAL_TERM=$TERM
  39
  40# For repeatability, reset the environment to known value.
  41LANG=C
  42LC_ALL=C
  43PAGER=cat
  44TZ=UTC
  45TERM=dumb
  46export LANG LC_ALL PAGER TERM TZ
  47EDITOR=:
  48export EDITOR
  49
  50#
  51#  If SHELL_PATH is not set, use a default of /bin/sh
  52#
  53SHELL_PATH=${SHELL_PATH:-/bin/sh}
  54export SHELL_PATH
  55
  56unset VISUAL
  57#
  58#  Pdsh variables
  59#
  60unset WCOLL
  61unset PDSH_RCMD_TYPE
  62unset PDSH_MISC_MODULES
  63unset PDSH_MODULE_DIR
  64unset DSHPATH
  65unset FANOUT
  66unset PDSH_GENDERS_FILE
  67unset PDSH_GENDERS_DIR
  68unset PDSH_SSH_ARGS
  69unset PDSH_SSH_ARGS_APPEND
  70unset SLUMR_JOBID
  71unset PBS_JOBID
  72
  73#
  74#  PDSH_BUILD_DIR and PDSH_SRC_DIR are set to build and src paths
  75#
  76if test -z "$PDSH_BUILD_DIR"; then
  77    if test -z "${builddir}"; then
  78	    PDSH_BUILD_DIR=$(pwd)/..
  79    else
  80	    PDSH_BUILD_DIR=$(cd ${builddir} && pwd)/..
  81	fi
  82	export PDSH_BUILD_DIR
  83fi
  84#
  85if test -z "$PDSH_SRC_DIR"; then
  86    if test -z "$srcdir"; then
  87	    PDSH_SRC_DIR=$(pwd)/..
  88    else
  89	    PDSH_SRC_DIR=$(cd ${srcdir} && pwd)/..
  90	fi
  91	export PDSH_SRC_DIR
  92fi
  93
  94# Protect ourselves from common misconfiguration to export
  95# CDPATH into the environment
  96unset CDPATH
  97
  98unset GREP_OPTIONS
  99
 100# Convenience
 101#
 102# A regexp to match 5 and 40 hexdigits
 103_x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
 104_x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
 105
 106# Each test should start with something like this, after copyright notices:
 107#
 108# test_description='Description of this test...
 109# This test checks if command xyzzy does the right thing...
 110# '
 111# . ./test-lib.sh
 112[ "x$ORIGINAL_TERM" != "xdumb" ] && (
 113		TERM=$ORIGINAL_TERM &&
 114		export TERM &&
 115		[ -t 1 ] &&
 116		tput bold >/dev/null 2>&1 &&
 117		tput setaf 1 >/dev/null 2>&1 &&
 118		tput sgr0 >/dev/null 2>&1
 119	) &&
 120	color=t
 121
 122while test "$#" -ne 0
 123do
 124	case "$1" in
 125	-d|--d|--de|--deb|--debu|--debug)
 126		debug=t; shift ;;
 127	-i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
 128		immediate=t; shift ;;
 129	-l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
 130		PDSH_TEST_LONG=t; export PDSH_TEST_LONG; shift ;;
 131	-h|--h|--he|--hel|--help)
 132		help=t; shift ;;
 133	-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
 134		verbose=t; shift ;;
 135	-q|--q|--qu|--qui|--quie|--quiet)
 136		# Ignore --quiet under a TAP::Harness. Saying how many tests
 137		# passed without the ok/not ok details is always an error.
 138		test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
 139	--with-dashes)
 140		with_dashes=t; shift ;;
 141	--no-color)
 142		color=; shift ;;
 143	--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
 144		valgrind=t; verbose=t; shift ;;
 145	--tee)
 146		shift ;; # was handled already
 147	--root=*)
 148		root=$(expr "z$1" : 'z[^=]*=\(.*\)')
 149		shift ;;
 150	*)
 151		echo "error: unknown test option '$1'" >&2; exit 1 ;;
 152	esac
 153done
 154
 155if test -n "$color"; then
 156	say_color () {
 157		(
 158		TERM=$ORIGINAL_TERM
 159		export TERM
 160		case "$1" in
 161			error) tput bold; tput setaf 1;; # bold red
 162			skip)  tput bold; tput setaf 2;; # bold green
 163			pass)  tput setaf 2;;            # green
 164			info)  tput setaf 3;;            # brown
 165			*) test -n "$quiet" && return;;
 166		esac
 167		shift
 168		printf "%s" "$*"
 169		tput sgr0
 170		echo
 171		)
 172	}
 173else
 174	say_color() {
 175		test -z "$1" && test -n "$quiet" && return
 176		shift
 177		echo "$*"
 178	}
 179fi
 180
 181error () {
 182	say_color error "error: $*"
 183	GIT_EXIT_OK=t
 184	exit 1
 185}
 186
 187say () {
 188	say_color info "$*"
 189}
 190
 191test "${test_description}" != "" ||
 192error "Test script did not set test_description."
 193
 194if test "$help" = "t"
 195then
 196	echo "$test_description"
 197	exit 0
 198fi
 199
 200exec 5>&1
 201if test "$verbose" = "t"
 202then
 203	exec 4>&2 3>&1
 204else
 205	exec 4>/dev/null 3>/dev/null
 206fi
 207
 208test_failure=0
 209test_count=0
 210test_fixed=0
 211test_broken=0
 212test_success=0
 213
 214test_external_has_tap=0
 215
 216die () {
 217	code=$?
 218	if test -n "$GIT_EXIT_OK"
 219	then
 220		exit $code
 221	else
 222		echo >&5 "FATAL: Unexpected exit with code $code"
 223		exit 1
 224	fi
 225}
 226
 227GIT_EXIT_OK=
 228trap 'die' EXIT
 229
 230# The semantics of the editor variables are that of invoking
 231# sh -c "$EDITOR \"$@\"" files ...
 232#
 233# If our trash directory contains shell metacharacters, they will be
 234# interpreted if we just set $EDITOR directly, so do a little dance with
 235# environment variables to work around this.
 236#
 237# In particular, quoting isn't enough, as the path may contain the same quote
 238# that we're using.
 239test_set_editor () {
 240	FAKE_EDITOR="$1"
 241	export FAKE_EDITOR
 242	EDITOR='"$FAKE_EDITOR"'
 243	export EDITOR
 244}
 245
 246test_decode_color () {
 247	sed	-e 's/.\[1m/<WHITE>/g' \
 248		-e 's/.\[31m/<RED>/g' \
 249		-e 's/.\[32m/<GREEN>/g' \
 250		-e 's/.\[33m/<YELLOW>/g' \
 251		-e 's/.\[34m/<BLUE>/g' \
 252		-e 's/.\[35m/<MAGENTA>/g' \
 253		-e 's/.\[36m/<CYAN>/g' \
 254		-e 's/.\[m/<RESET>/g'
 255}
 256
 257q_to_nul () {
 258	perl -pe 'y/Q/\000/'
 259}
 260
 261q_to_cr () {
 262	tr Q '\015'
 263}
 264
 265q_to_tab () {
 266	tr Q '\011'
 267}
 268
 269append_cr () {
 270	sed -e 's/$/Q/' | tr Q '\015'
 271}
 272
 273remove_cr () {
 274	tr '\015' Q | sed -e 's/Q$//'
 275}
 276
 277test_tick () {
 278	if test -z "${test_tick+set}"
 279	then
 280		test_tick=1112911993
 281	else
 282		test_tick=$(($test_tick + 60))
 283	fi
 284	GIT_COMMITTER_DATE="$test_tick -0700"
 285	GIT_AUTHOR_DATE="$test_tick -0700"
 286	export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
 287}
 288
 289# Use test_set_prereq to tell that a particular prerequisite is available.
 290# The prerequisite can later be checked for in two ways:
 291#
 292# - Explicitly using test_have_prereq.
 293#
 294# - Implicitly by specifying the prerequisite tag in the calls to
 295#   test_expect_{success,failure,code}.
 296#
 297# The single parameter is the prerequisite tag (a simple word, in all
 298# capital letters by convention).
 299
 300test_set_prereq () {
 301	satisfied="$satisfied$1 "
 302}
 303satisfied=" "
 304
 305test_have_prereq () {
 306	# prerequisites can be concatenated with ','
 307	save_IFS=$IFS
 308	IFS=,
 309	set -- $*
 310	IFS=$save_IFS
 311
 312	total_prereq=0
 313	ok_prereq=0
 314	missing_prereq=
 315
 316	for prerequisite
 317	do
 318		total_prereq=$(($total_prereq + 1))
 319		case $satisfied in
 320		*" $prerequisite "*)
 321			ok_prereq=$(($ok_prereq + 1))
 322			;;
 323		*)
 324			# Keep a list of missing prerequisites
 325			if test -z "$missing_prereq"
 326			then
 327				missing_prereq=$prerequisite
 328			else
 329				missing_prereq="$prerequisite,$missing_prereq"
 330			fi
 331		esac
 332	done
 333
 334	test $total_prereq = $ok_prereq
 335}
 336
 337# You are not expected to call test_ok_ and test_failure_ directly, use
 338# the text_expect_* functions instead.
 339
 340test_ok_ () {
 341	test_success=$(($test_success + 1))
 342	say_color pass "ok $test_count - $@"
 343}
 344
 345test_failure_ () {
 346	test_failure=$(($test_failure + 1))
 347	say_color error "not ok - $test_count $1"
 348	shift
 349	echo "$@" | sed -e 's/^/#	/'
 350	test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
 351}
 352
 353test_known_broken_ok_ () {
 354	test_fixed=$(($test_fixed+1))
 355	say_color pass "ok $test_count - $@ # TODO known breakage"
 356}
 357
 358test_known_broken_failure_ () {
 359	test_broken=$(($test_broken+1))
 360	say_color skip "not ok $test_count - $@ # TODO known breakage"
 361}
 362
 363test_debug () {
 364	test "$debug" = "" || eval "$1"
 365}
 366
 367test_run_ () {
 368	test_cleanup=:
 369	eval >&3 2>&4 "$1"
 370	eval_ret=$?
 371	eval >&3 2>&4 "$test_cleanup"
 372	if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
 373		echo ""
 374	fi
 375	return 0
 376}
 377
 378test_skip () {
 379	test_count=$(($test_count+1))
 380	to_skip=
 381	for skp in $PDSH_SKIP_TESTS
 382	do
 383		case $this_test.$test_count in
 384		$skp)
 385			to_skip=t
 386			break
 387		esac
 388	done
 389	if test -z "$to_skip" && test -n "$prereq" &&
 390	   ! test_have_prereq "$prereq"
 391	then
 392		to_skip=t
 393	fi
 394	case "$to_skip" in
 395	t)
 396		of_prereq=
 397		if test "$missing_prereq" != "$prereq"
 398		then
 399			of_prereq=" of $prereq"
 400		fi
 401
 402		say_color skip >&3 "skipping test: $@"
 403		say_color skip "ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
 404		: true
 405		;;
 406	*)
 407		false
 408		;;
 409	esac
 410}
 411
 412test_expect_failure () {
 413	test "$#" = 3 && { prereq=$1; shift; } || prereq=
 414	test "$#" = 2 ||
 415	error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
 416	if ! test_skip "$@"
 417	then
 418		say >&3 "checking known breakage: $2"
 419		test_run_ "$2"
 420		if [ "$?" = 0 -a "$eval_ret" = 0 ]
 421		then
 422			test_known_broken_ok_ "$1"
 423		else
 424			test_known_broken_failure_ "$1"
 425		fi
 426	fi
 427	echo >&3 ""
 428}
 429
 430test_expect_success () {
 431	test "$#" = 3 && { prereq=$1; shift; } || prereq=
 432	test "$#" = 2 ||
 433	error "bug in the test script: not 2 or 3 parameters to test-expect-success"
 434	if ! test_skip "$@"
 435	then
 436		say >&3 "expecting success: $2"
 437		test_run_ "$2"
 438		if [ "$?" = 0 -a "$eval_ret" = 0 ]
 439		then
 440			test_ok_ "$1"
 441		else
 442			test_failure_ "$@"
 443		fi
 444	fi
 445	echo >&3 ""
 446}
 447
 448
 449# Similar to test_must_fail and test_might_fail, but check that a
 450# given command exited with a given exit code. Meant to be used as:
 451#
 452#	test_expect_success 'Merge with d/f conflicts' '
 453#		test_expect_code 1 git merge "merge msg" B master
 454#	'
 455
 456test_expect_code () {
 457	want_code=$1
 458	shift
 459	"$@"
 460	exit_code=$?
 461	if test $exit_code = $want_code
 462	then
 463		echo >&2 "test_expect_code: command exited with $exit_code: $*"
 464		return 0
 465	else
 466		echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
 467		return 1
 468	fi
 469}
 470
 471# test_external runs external test scripts that provide continuous
 472# test output about their progress, and succeeds/fails on
 473# zero/non-zero exit code.  It outputs the test output on stdout even
 474# in non-verbose mode, and announces the external script with "# run
 475# <n>: ..." before running it.  When providing relative paths, keep in
 476# mind that all scripts run in "trash directory".
 477# Usage: test_external description command arguments...
 478# Example: test_external 'Perl API' perl ../path/to/test.pl
 479test_external () {
 480	test "$#" = 4 && { prereq=$1; shift; } || prereq=
 481	test "$#" = 3 ||
 482	error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
 483	descr="$1"
 484	shift
 485	if ! test_skip "$descr" "$@"
 486	then
 487		# Announce the script to reduce confusion about the
 488		# test output that follows.
 489		say_color "" "# run $test_count: $descr ($*)"
 490		# Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG
 491		# to be able to use them in script
 492		export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG
 493		# Run command; redirect its stderr to &4 as in
 494		# test_run_, but keep its stdout on our stdout even in
 495		# non-verbose mode.
 496		"$@" 2>&4
 497		if [ "$?" = 0 ]
 498		then
 499			if test $test_external_has_tap -eq 0; then
 500				test_ok_ "$descr"
 501			else
 502				say_color "" "# test_external test $descr was ok"
 503				test_success=$(($test_success + 1))
 504			fi
 505		else
 506			if test $test_external_has_tap -eq 0; then
 507				test_failure_ "$descr" "$@"
 508			else
 509				say_color error "# test_external test $descr failed: $@"
 510				test_failure=$(($test_failure + 1))
 511			fi
 512		fi
 513	fi
 514}
 515
 516# Like test_external, but in addition tests that the command generated
 517# no output on stderr.
 518test_external_without_stderr () {
 519	# The temporary file has no (and must have no) security
 520	# implications.
 521	tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
 522	stderr="$tmp/git-external-stderr.$$.tmp"
 523	test_external "$@" 4> "$stderr"
 524	[ -f "$stderr" ] || error "Internal error: $stderr disappeared."
 525	descr="no stderr: $1"
 526	shift
 527	say >&3 "# expecting no stderr from previous command"
 528	if [ ! -s "$stderr" ]; then
 529		rm "$stderr"
 530
 531		if test $test_external_has_tap -eq 0; then
 532			test_ok_ "$descr"
 533		else
 534			say_color "" "# test_external_without_stderr test $descr was ok"
 535			test_success=$(($test_success + 1))
 536		fi
 537	else
 538		if [ "$verbose" = t ]; then
 539			output=`echo; echo "# Stderr is:"; cat "$stderr"`
 540		else
 541			output=
 542		fi
 543		# rm first in case test_failure exits.
 544		rm "$stderr"
 545		if test $test_external_has_tap -eq 0; then
 546			test_failure_ "$descr" "$@" "$output"
 547		else
 548			say_color error "# test_external_without_stderr test $descr failed: $@: $output"
 549			test_failure=$(($test_failure + 1))
 550		fi
 551	fi
 552}
 553
 554# debugging-friendly alternatives to "test [-f|-d|-e]"
 555# The commands test the existence or non-existence of $1. $2 can be
 556# given to provide a more precise diagnosis.
 557test_path_is_file () {
 558	if ! [ -f "$1" ]
 559	then
 560		echo "File $1 doesn't exist. $*"
 561		false
 562	fi
 563}
 564
 565test_path_is_dir () {
 566	if ! [ -d "$1" ]
 567	then
 568		echo "Directory $1 doesn't exist. $*"
 569		false
 570	fi
 571}
 572
 573test_path_is_missing () {
 574	if [ -e "$1" ]
 575	then
 576		echo "Path exists:"
 577		ls -ld "$1"
 578		if [ $# -ge 1 ]; then
 579			echo "$*"
 580		fi
 581		false
 582	fi
 583}
 584
 585
 586# This is not among top-level (test_expect_success | test_expect_failure)
 587# but is a prefix that can be used in the test script, like:
 588#
 589#	test_expect_success 'complain and die' '
 590#           do something &&
 591#           do something else &&
 592#	    test_must_fail git checkout ../outerspace
 593#	'
 594#
 595# Writing this as "! git checkout ../outerspace" is wrong, because
 596# the failure could be due to a segv.  We want a controlled failure.
 597
 598test_must_fail () {
 599	"$@"
 600	exit_code=$?
 601	if test $exit_code = 0; then
 602		echo >&2 "test_must_fail: command succeeded: $*"
 603		return 1
 604	elif test $exit_code -gt 129 -a $exit_code -le 192; then
 605		echo >&2 "test_must_fail: died by signal: $*"
 606		return 1
 607	elif test $exit_code = 127; then
 608		echo >&2 "test_must_fail: command not found: $*"
 609		return 1
 610	fi
 611	return 0
 612}
 613
 614# Similar to test_must_fail, but tolerates success, too.  This is
 615# meant to be used in contexts like:
 616#
 617#	test_expect_success 'some command works without configuration' '
 618#		test_might_fail git config --unset all.configuration &&
 619#		do something
 620#	'
 621#
 622# Writing "git config --unset all.configuration || :" would be wrong,
 623# because we want to notice if it fails due to segv.
 624
 625test_might_fail () {
 626	"$@"
 627	exit_code=$?
 628	if test $exit_code -gt 129 -a $exit_code -le 192; then
 629		echo >&2 "test_might_fail: died by signal: $*"
 630		return 1
 631	elif test $exit_code = 127; then
 632		echo >&2 "test_might_fail: command not found: $*"
 633		return 1
 634	fi
 635	return 0
 636}
 637
 638# test_cmp is a helper function to compare actual and expected output.
 639# You can use it like:
 640#
 641#	test_expect_success 'foo works' '
 642#		echo expected >expected &&
 643#		foo >actual &&
 644#		test_cmp expected actual
 645#	'
 646#
 647# This could be written as either "cmp" or "diff -u", but:
 648# - cmp's output is not nearly as easy to read as diff -u
 649# - not all diff versions understand "-u"
 650
 651test_cmp() {
 652	$GIT_TEST_CMP "$@"
 653}
 654
 655# This function can be used to schedule some commands to be run
 656# unconditionally at the end of the test to restore sanity:
 657#
 658#	test_expect_success 'test core.capslock' '
 659#		git config core.capslock true &&
 660#		test_when_finished "git config --unset core.capslock" &&
 661#		hello world
 662#	'
 663#
 664# That would be roughly equivalent to
 665#
 666#	test_expect_success 'test core.capslock' '
 667#		git config core.capslock true &&
 668#		hello world
 669#		git config --unset core.capslock
 670#	'
 671#
 672# except that the greeting and config --unset must both succeed for
 673# the test to pass.
 674
 675test_when_finished () {
 676	test_cleanup="{ $*
 677		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
 678}
 679
 680#
 681#  pdsh convenience functions
 682#
 683test_output_is_expected() {
 684	OUTPUT="$1"
 685	EXPECTED="$2"
 686	if ! test "$OUTPUT" = "$EXPECTED"; then
 687		say_color error "Error: Output \'$OUTPUT\' != \'$EXPECTED\'"
 688		false
 689	fi
 690}
 691
 692
 693# Most tests can use the created repository, but some may need to create more.
 694# Usage: test_create_repo <directory>
 695test_create_repo () {
 696        test "$#" = 1 ||
 697        error "bug in the test script: not 1 parameter to test-create-repo"
 698        repo="$1"
 699        mkdir -p "$repo"
 700}
 701
 702test_done () {
 703	GIT_EXIT_OK=t
 704
 705	if test -z "$HARNESS_ACTIVE"; then
 706		test_results_dir="$TEST_DIRECTORY/test-results"
 707		mkdir -p "$test_results_dir"
 708		test_results_path="$test_results_dir/$test_name-$$.counts"
 709
 710		echo "total $test_count" >> $test_results_path
 711		echo "success $test_success" >> $test_results_path
 712		echo "fixed $test_fixed" >> $test_results_path
 713		echo "broken $test_broken" >> $test_results_path
 714		echo "failed $test_failure" >> $test_results_path
 715		echo "" >> $test_results_path
 716	fi
 717
 718	if test "$test_fixed" != 0
 719	then
 720		say_color pass "# fixed $test_fixed known breakage(s)"
 721	fi
 722	if test "$test_broken" != 0
 723	then
 724		say_color error "# still have $test_broken known breakage(s)"
 725		msg="remaining $(($test_count-$test_broken)) test(s)"
 726	else
 727		msg="$test_count test(s)"
 728	fi
 729	case "$test_failure" in
 730	0)
 731		# Maybe print SKIP message
 732		[ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"
 733
 734		if test $test_external_has_tap -eq 0; then
 735			say_color pass "# passed all $msg"
 736			say "1..$test_count$skip_all"
 737		fi
 738
 739		test -d "$remove_trash" &&
 740		cd "$(dirname "$remove_trash")" &&
 741		rm -rf "$(basename "$remove_trash")"
 742
 743		exit 0 ;;
 744
 745	*)
 746		if test $test_external_has_tap -eq 0; then
 747			say_color error "# failed $test_failure among $msg"
 748			say "1..$test_count"
 749		fi
 750
 751		exit 1 ;;
 752
 753	esac
 754}
 755
 756# Test the binaries we have just built.  The tests are kept in
 757# t/ subdirectory and are run in 'trash directory' subdirectory.
 758if test -z "$TEST_DIRECTORY"
 759then
 760	# We allow tests to override this, in case they want to run tests
 761	# outside of t/, e.g. for running tests on the test library
 762	# itself.
 763	TEST_DIRECTORY=$(pwd)
 764fi
 765
 766if test -n "$valgrind"
 767then
 768	make_symlink () {
 769		test -h "$2" &&
 770		test "$1" = "$(readlink "$2")" || {
 771			# be super paranoid
 772			if mkdir "$2".lock
 773			then
 774				rm -f "$2" &&
 775				ln -s "$1" "$2" &&
 776				rm -r "$2".lock
 777			else
 778				while test -d "$2".lock
 779				do
 780					say "Waiting for lock on $2."
 781					sleep 1
 782				done
 783			fi
 784		}
 785	}
 786
 787	make_valgrind_symlink () {
 788		# handle only executables
 789		test -x "$1" || return
 790
 791		base=$(basename "$1")
 792		symlink_target=$GIT_BUILD_DIR/$base
 793		# do not override scripts
 794		if test -x "$symlink_target" &&
 795		    test ! -d "$symlink_target" &&
 796		    test "#!" != "$(head -c 2 < "$symlink_target")"
 797		then
 798			symlink_target=../valgrind.sh
 799		fi
 800		case "$base" in
 801		*.sh|*.perl)
 802			symlink_target=../unprocessed-script
 803		esac
 804		# create the link, or replace it if it is out of date
 805		make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
 806	}
 807
 808	# override all executables in TEST_DIRECTORY/..
 809	GIT_VALGRIND=$TEST_DIRECTORY/valgrind
 810	mkdir -p "$GIT_VALGRIND"/bin
 811	make_valgrind_symlink $PDSH_BUILD_DIR/src/pdsh/pdsh
 812	IFS=$OLDIFS
 813	PATH=$GIT_VALGRIND/bin:$PATH
 814	export GIT_VALGRIND
 815elif test -n "$PDSH_TEST_INSTALLED" ; then
 816	PATH=$PDSH_TEST_INSTALLED:$PDSH_BUILD_DIR/src/pdsh:$PATH
 817	GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
 818else # normal case, use ../bin-wrappers only unless $with_dashes:
 819	pdsh_path=$PDSH_BUILD_DIR/src/pdsh
 820	dshbak_path=$PDSH_SRC_DIR/scripts
 821	test -n "$dshbak_path" && PATH="$dshbak_path:$PATH"
 822	test -n "$pdsh_path" && PATH="$pdsh_path:$PATH"
 823fi
 824export PATH
 825
 826if test -z "$GIT_TEST_CMP"
 827then
 828	if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
 829	then
 830		GIT_TEST_CMP="diff -c"
 831	else
 832		GIT_TEST_CMP="diff -u"
 833	fi
 834fi
 835
 836test="trash-directory.$test_name"
 837test -n "$root" && test="$root/$test"
 838case "$test" in
 839/*) TRASH_DIRECTORY="$test" ;;
 840 *) TRASH_DIRECTORY="$TEST_DIRECTORY/$test" ;;
 841esac
 842test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
 843rm -fr "$test" || {
 844        GIT_EXIT_OK=t
 845        echo >&5 "FATAL: Cannot prepare test area"
 846        exit 1
 847}
 848
 849test_create_repo "$test"
 850
 851# Use -P to resolve symlinks in our working directory so that the cwd
 852# in subprocesses like git equals our $PWD (for pathname comparisons).
 853cd -P "$test" || exit 1
 854
 855HOME=$(pwd)
 856export HOME
 857
 858this_test=${0##*/}
 859this_test=${this_test%%-*}
 860for skp in $PDSH_SKIP_TESTS
 861do
 862	case "$this_test" in
 863	$skp)
 864		say_color skip >&3 "skipping test $this_test altogether"
 865		skip_all="skip all tests in $this_test"
 866		test_done
 867	esac
 868done
 869
 870# Provide an implementation of the 'yes' utility
 871yes () {
 872	if test $# = 0
 873	then
 874		y=y
 875	else
 876		y="$*"
 877	fi
 878
 879	while echo "$y"
 880	do
 881		:
 882	done
 883}
 884
 885# Fix some commands on Windows
 886case $(uname -s) in
 887*MINGW*)
 888	# Windows has its own (incompatible) sort and find
 889	sort () {
 890		/usr/bin/sort "$@"
 891	}
 892	find () {
 893		/usr/bin/find "$@"
 894	}
 895	sum () {
 896		md5sum "$@"
 897	}
 898	# git sees Windows-style pwd
 899	pwd () {
 900		builtin pwd -W
 901	}
 902	# no POSIX permissions
 903	# backslashes in pathspec are converted to '/'
 904	# exec does not inherit the PID
 905	;;
 906*)
 907	test_set_prereq POSIXPERM
 908	test_set_prereq BSLASHPSPEC
 909	test_set_prereq EXECKEEPSPID
 910	;;
 911esac
 912
 913test -z "$NO_PERL" && test_set_prereq PERL
 914test -z "$NO_PYTHON" && test_set_prereq PYTHON
 915
 916# test whether the filesystem supports symbolic links
 917ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
 918rm -f y
 919
 920# When the tests are run as root, permission tests will report that
 921# things are writable when they shouldn't be.
 922#
 923# Additionally, for pdsh, some tests require non-root, esp those
 924#  that use PDSH_MODULE_DIR, which doesn't work when run setuid or
 925#  as root
 926test -w / || test_set_prereq SANITY
 927test "$USER" = "root" || test_set_prereq NOTROOT
 928
 929#  Set some prereqs for common commands
 930#
 931test "$(expr 9 - 2)" = "7" &&      test_set_prereq EXPR
 932
 933#  Function to generate a random number
 934# 
 935random() { 
 936   R=$RANDOM
 937   if test -z "$R"; then
 938      if test -r /dev/urandom; then
 939         R=$(dd if=/dev/urandom count=1 2>/dev/null | cksum | cut -d' ' -f1)
 940      else
 941         R=$( (echo $$; ps; date +%s) 2>&1 | cksum | cut -d' ' -f1)
 942      fi
 943   fi
 944   if test -n "${1}"; then
 945      R=$(expr $R % $1)
 946   fi
 947   echo $R
 948}
 949
 950#  Shell implementation of seq(1)
 951#
 952seq() {
 953	if [ $# -eq 1 ]; then
 954		i=1
 955		end=$1
 956	else
 957		i=$1
 958		end=$2
 959	fi
 960	while [ $i -le $end ]; do
 961		echo $i
 962		i=$((i+1))
 963	done
 964}
 965
 966
 967#
 968#  Ensure that pdsh has been built.
 969#
 970if ! test -x $PDSH_BUILD_DIR/src/pdsh/pdsh; then
 971  say_color error 'Can not find a pdsh binary to test'
 972  say_color error 'Do you need to run the build system?'
 973  exit 1
 974fi
 975
 976#
 977# If the pdsh build directory owner and the pdsh binary have
 978#  different ownership, abort the test because pdsh will not
 979#  be able to load any modules, and almost no tests will work.
 980#
 981path_owner() { ls -ld $1 | awk '{print $3}'; }
 982pdsh_owner=$(path_owner $PDSH_BUILD_DIR/src/pdsh/pdsh)
 983builddir_owner=$(path_owner $PDSH_BUILD_DIR/src)
 984if test "$pdsh_owner" != "$builddir_owner"; then
 985  say_color error 'Build directory owner and pdsh binary owner are different'
 986  say_color error 'The testsuite will not work in this configuration'
 987  exit 1
 988fi
 989
 990#
 991#  Set loaded modules as prereqs
 992#
 993for mod in $(pdsh -L 2>&1 | \
 994                 sed -n 's/^Module: *\(.*\)\/\(.*\)/\1_\2/p' | \
 995                 tr a-z A-Z); do
 996	test_set_prereq MOD_${mod}
 997done
 998
 999if [ -n "$PDSH_TEST_LONG" ]; then
1000	test_set_prereq LONGTESTS
1001fi
1002
1003if pdsh -V | head -1 | grep -qv +static-modules; then
1004	test_set_prereq DYNAMIC_MODULES
1005fi
1006
1007