test-lib.sh 15.2 KB
Newer Older
1 2 3 4
#!/bin/sh
#
# 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 21 22 23
# 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
	;;
24
*' --tee '*|*' --va'*)
25 26 27 28 29 30 31 32 33
	mkdir -p test-results
	BASE=test-results/$(basename "$0" .sh)
	(GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
	 echo $? > $BASE.exit) | tee $BASE.out
	test "$(cat $BASE.exit)" = 0
	exit
	;;
esac

34 35 36
# Keep the original TERM for say_color
ORIGINAL_TERM=$TERM

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
# 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)
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"/..

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

57 58
# For repeatability, reset the environment to known value.
LANG=C
59
LC_ALL=C
60
PAGER=cat
61
TZ=UTC
62 63
TERM=dumb
export LANG LC_ALL PAGER TERM TZ
64
EDITOR=:
65 66 67 68
# 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.
69
unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
70
	my @env = keys %ENV;
71 72 73 74 75 76 77 78
	my $ok = join("|", qw(
		TRACE
		DEBUG
		USE_LOOKUP
		TEST
		.*_TEST
		PROVE
		VALGRIND
79
		PERF_AGGREGATING_LATER
80 81
	));
	my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
82 83
	print join("\n", @vars);
')
J
Jeff King 已提交
84
unset XDG_CONFIG_HOME
85 86 87 88
GIT_AUTHOR_EMAIL=author@example.com
GIT_AUTHOR_NAME='A U Thor'
GIT_COMMITTER_EMAIL=committer@example.com
GIT_COMMITTER_NAME='C O Mitter'
89
GIT_MERGE_VERBOSITY=5
90 91
GIT_MERGE_AUTOEDIT=no
export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
92 93
export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
94
export EDITOR
95

96 97 98 99
# Protect ourselves from common misconfiguration to export
# CDPATH into the environment
unset CDPATH

B
Bert Wesarg 已提交
100 101
unset GREP_OPTIONS

102
case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
103 104 105 106 107 108 109 110
	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 ! *"
		;;
esac

111 112 113 114 115 116
# 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 已提交
117 118 119
# Zero SHA-1
_z40=0000000000000000000000000000000000000000

120 121 122 123
# Line feed
LF='
'

124 125
export _x05 _x40 _z40 LF

126 127 128 129 130 131
# 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
132 133 134 135 136 137 138 139
[ "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
	) &&
140
	color=t
141 142 143 144 145 146

while test "$#" -ne 0
do
	case "$1" in
	-d|--d|--de|--deb|--debu|--debug)
		debug=t; shift ;;
J
Junio C Hamano 已提交
147 148
	-i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
		immediate=t; shift ;;
149
	-l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
J
Junio C Hamano 已提交
150
		GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
151
	-h|--h|--he|--hel|--help)
152
		help=t; shift ;;
153 154
	-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
		verbose=t; shift ;;
155
	-q|--q|--qu|--qui|--quie|--quiet)
156 157 158
		# 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 ;;
159 160
	--with-dashes)
		with_dashes=t; shift ;;
161
	--no-color)
162
		color=; shift ;;
163
	--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
164
		valgrind=t; verbose=t; shift ;;
165 166
	--tee)
		shift ;; # was handled already
167 168 169
	--root=*)
		root=$(expr "z$1" : 'z[^=]*=\(.*\)')
		shift ;;
170
	*)
171
		echo "error: unknown test option '$1'" >&2; exit 1 ;;
172 173 174
	esac
done

175 176
if test -n "$color"; then
	say_color () {
177 178 179
		(
		TERM=$ORIGINAL_TERM
		export TERM
180 181 182 183 184
		case "$1" in
			error) tput bold; tput setaf 1;; # bold red
			skip)  tput bold; tput setaf 2;; # bold green
			pass)  tput setaf 2;;            # green
			info)  tput setaf 3;;            # brown
185
			*) test -n "$quiet" && return;;
186 187
		esac
		shift
188
		printf "%s" "$*"
189
		tput sgr0
190
		echo
191
		)
192 193 194
	}
else
	say_color() {
195
		test -z "$1" && test -n "$quiet" && return
196
		shift
197
		echo "$*"
198 199 200 201 202
	}
fi

error () {
	say_color error "error: $*"
203
	GIT_EXIT_OK=t
204 205 206 207 208 209 210
	exit 1
}

say () {
	say_color info "$*"
}

211 212 213 214 215 216 217 218 219
test "${test_description}" != "" ||
error "Test script did not set test_description."

if test "$help" = "t"
then
	echo "$test_description"
	exit 0
fi

220
exec 5>&1
J
Jeff King 已提交
221
exec 6<&0
222 223 224 225 226 227 228 229 230
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 已提交
231 232
test_fixed=0
test_broken=0
233
test_success=0
234

235 236
test_external_has_tap=0

237
die () {
238 239 240 241 242 243 244 245
	code=$?
	if test -n "$GIT_EXIT_OK"
	then
		exit $code
	else
		echo >&5 "FATAL: Unexpected exit with code $code"
		exit 1
	fi
246 247
}

248
GIT_EXIT_OK=
249
trap 'die' EXIT
250

251 252
# The user-facing functions are loaded from a separate file so that
# test_perf subshells can have them too
253
. "$TEST_DIRECTORY/test-lib-functions.sh"
254

J
Junio C Hamano 已提交
255 256 257 258
# You are not expected to call test_ok_ and test_failure_ directly, use
# the text_expect_* functions instead.

test_ok_ () {
259
	test_success=$(($test_success + 1))
260
	say_color "" "ok $test_count - $@"
261 262
}

J
Junio C Hamano 已提交
263
test_failure_ () {
264
	test_failure=$(($test_failure + 1))
265
	say_color error "not ok - $test_count $1"
266
	shift
267
	echo "$@" | sed -e 's/^/#	/'
268
	test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
J
Junio C Hamano 已提交
269 270
}

J
Junio C Hamano 已提交
271 272
test_known_broken_ok_ () {
	test_fixed=$(($test_fixed+1))
273
	say_color "" "ok $test_count - $@ # TODO known breakage"
J
Junio C Hamano 已提交
274 275 276 277
}

test_known_broken_failure_ () {
	test_broken=$(($test_broken+1))
278
	say_color skip "not ok $test_count - $@ # TODO known breakage"
J
Junio C Hamano 已提交
279
}
J
Junio C Hamano 已提交
280 281

test_debug () {
282
	test "$debug" = "" || eval "$1"
283 284
}

285 286 287
test_eval_ () {
	# This is a separate function because some tests use
	# "return" to end a test_expect_success block early.
J
Jeff King 已提交
288
	eval </dev/null >&3 2>&4 "$*"
289 290
}

291
test_run_ () {
292
	test_cleanup=:
293
	expecting_failure=$2
294
	test_eval_ "$1"
295
	eval_ret=$?
296 297 298

	if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
	then
299
		test_eval_ "$test_cleanup"
300
	fi
301 302 303
	if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
		echo ""
	fi
304
	return "$eval_ret"
305 306
}

307
test_skip () {
308
	test_count=$(($test_count+1))
309 310 311
	to_skip=
	for skp in $GIT_SKIP_TESTS
	do
312
		case $this_test.$test_count in
313 314
		$skp)
			to_skip=t
315
			break
316 317
		esac
	done
318 319
	if test -z "$to_skip" && test -n "$test_prereq" &&
	   ! test_have_prereq "$test_prereq"
320 321 322
	then
		to_skip=t
	fi
323 324
	case "$to_skip" in
	t)
325
		of_prereq=
326
		if test "$missing_prereq" != "$test_prereq"
327
		then
328
			of_prereq=" of $test_prereq"
329 330
		fi

331
		say_color skip >&3 "skipping test: $@"
332
		say_color skip "ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
333 334 335 336 337 338 339 340
		: true
		;;
	*)
		false
		;;
	esac
}

341 342 343 344 345
# stub; perf-lib overrides it
test_at_end_hook_ () {
	:
}

346
test_done () {
347
	GIT_EXIT_OK=t
348

349
	if test -z "$HARNESS_ACTIVE"; then
350
		test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
351 352 353
		mkdir -p "$test_results_dir"
		test_results_path="$test_results_dir/${0%.sh}-$$.counts"

354 355 356 357 358 359 360 361
		cat >>"$test_results_path" <<-EOF
		total $test_count
		success $test_success
		fixed $test_fixed
		broken $test_broken
		failed $test_failure

		EOF
362
	fi
J
Junio C Hamano 已提交
363 364 365

	if test "$test_fixed" != 0
	then
366
		say_color pass "# fixed $test_fixed known breakage(s)"
J
Junio C Hamano 已提交
367 368 369
	fi
	if test "$test_broken" != 0
	then
370
		say_color error "# still have $test_broken known breakage(s)"
371 372 373
		msg="remaining $(($test_count-$test_broken)) test(s)"
	else
		msg="$test_count test(s)"
J
Junio C Hamano 已提交
374
	fi
375
	case "$test_failure" in
376
	0)
377 378 379
		# Maybe print SKIP message
		[ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"

380 381 382 383
		if test $test_external_has_tap -eq 0; then
			say_color pass "# passed all $msg"
			say "1..$test_count$skip_all"
		fi
J
Johannes Schindelin 已提交
384 385 386 387 388

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

389 390
		test_at_end_hook_

391 392 393
		exit 0 ;;

	*)
394 395 396 397
		if test $test_external_has_tap -eq 0; then
			say_color error "# failed $test_failure among $msg"
			say "1..$test_count"
		fi
398

399 400 401 402 403
		exit 1 ;;

	esac
}

404
if test -n "$valgrind"
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
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 () {
426 427 428 429 430 431 432
		# handle only executables, unless they are shell libraries that
		# need to be in the exec-path.  We will just use "#!" as a
		# guess for a shell-script, since we have no idea what the user
		# may have configured as the shell path.
		test -x "$1" ||
		test "#!" = "$(head -c 2 <"$1")" ||
		return;
433 434

		base=$(basename "$1")
435
		symlink_target=$GIT_BUILD_DIR/$base
436 437 438 439 440 441 442
		# 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
443 444 445 446
		case "$base" in
		*.sh|*.perl)
			symlink_target=../unprocessed-script
		esac
447 448 449 450 451 452 453
		# create the link, or replace it if it is out of date
		make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
	}

	# override all git executables in TEST_DIRECTORY/..
	GIT_VALGRIND=$TEST_DIRECTORY/valgrind
	mkdir -p "$GIT_VALGRIND"/bin
454
	for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/test-*
455 456 457
	do
		make_valgrind_symlink $file
	done
458 459
	# special-case the mergetools loadables
	make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
460 461 462 463 464 465 466 467 468 469 470
	OLDIFS=$IFS
	IFS=:
	for path in $PATH
	do
		ls "$path"/git-* 2> /dev/null |
		while read file
		do
			make_valgrind_symlink "$file"
		done
	done
	IFS=$OLDIFS
471 472 473
	PATH=$GIT_VALGRIND/bin:$PATH
	GIT_EXEC_PATH=$GIT_VALGRIND/bin
	export GIT_VALGRIND
474 475 476
elif test -n "$GIT_TEST_INSTALLED" ; then
	GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)  ||
	error "Cannot run git from $GIT_TEST_INSTALLED."
477
	PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH
478 479
	GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
else # normal case, use ../bin-wrappers only unless $with_dashes:
480
	git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
481 482 483 484 485 486 487
	if ! test -x "$git_bin_dir/git" ; then
		if test -z "$with_dashes" ; then
			say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
		fi
		with_dashes=t
	fi
	PATH="$git_bin_dir:$PATH"
488
	GIT_EXEC_PATH=$GIT_BUILD_DIR
489
	if test -n "$with_dashes" ; then
490
		PATH="$GIT_BUILD_DIR:$PATH"
491
	fi
492
fi
493
GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
J
Jeff King 已提交
494 495
unset GIT_CONFIG
GIT_CONFIG_NOSYSTEM=1
496
GIT_ATTR_NOSYSTEM=1
497
export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
498

499 500 501 502 503 504 505 506 507 508
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

509
GITPERLLIB="$GIT_BUILD_DIR"/perl/blib/lib:"$GIT_BUILD_DIR"/perl/blib/arch/auto/Git
510
export GITPERLLIB
511
test -d "$GIT_BUILD_DIR"/templates/blt || {
512 513
	error "You haven't built things yet, have you?"
}
514

J
Jeff King 已提交
515
if test -z "$GIT_TEST_INSTALLED" && test -z "$NO_PYTHON"
516
then
517
	GITPYTHONLIB="$GIT_BUILD_DIR/git_remote_helpers/build/lib"
518
	export GITPYTHONLIB
519
	test -d "$GIT_BUILD_DIR"/git_remote_helpers/build || {
520 521 522 523
		error "You haven't built git_remote_helpers yet, have you?"
	}
fi

524
if ! test -x "$GIT_BUILD_DIR"/test-chmtime; then
E
Eric Wong 已提交
525 526 527 528 529
	echo >&2 'You need to build test-chmtime:'
	echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
	exit 1
fi

530
# Test repository
J
Johannes Schindelin 已提交
531
test="trash directory.$(basename "$0" .sh)"
532 533 534
test -n "$root" && test="$root/$test"
case "$test" in
/*) TRASH_DIRECTORY="$test" ;;
535
 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$test" ;;
536
esac
537
test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
538
rm -fr "$test" || {
539
	GIT_EXIT_OK=t
540 541 542 543
	echo >&5 "FATAL: Cannot prepare test area"
	exit 1
}

544 545 546
HOME="$TRASH_DIRECTORY"
export HOME

547 548 549 550 551
if test -z "$TEST_NO_CREATE_REPO"; then
	test_create_repo "$test"
else
	mkdir -p "$test"
fi
552 553 554
# Use -P to resolve symlinks in our working directory so that the cwd
# in subprocesses like git equals our $PWD (for pathname comparisons).
cd -P "$test" || exit 1
555

556 557
this_test=${0##*/}
this_test=${this_test%%-*}
558 559
for skp in $GIT_SKIP_TESTS
do
560 561
	case "$this_test" in
	$skp)
562
		say_color skip >&3 "skipping test $this_test altogether"
563
		skip_all="skip all tests in $this_test"
564 565 566
		test_done
	esac
done
567

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
# Provide an implementation of the 'yes' utility
yes () {
	if test $# = 0
	then
		y=y
	else
		y="$*"
	fi

	while echo "$y"
	do
		:
	done
}

583 584 585 586 587 588 589 590 591 592
# 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 "$@"
	}
593 594 595
	sum () {
		md5sum "$@"
	}
596 597 598 599
	# git sees Windows-style pwd
	pwd () {
		builtin pwd -W
	}
600
	# no POSIX permissions
601
	# backslashes in pathspec are converted to '/'
602
	# exec does not inherit the PID
603
	test_set_prereq MINGW
604 605 606 607 608 609 610
	test_set_prereq SED_STRIPS_CR
	;;
*CYGWIN*)
	test_set_prereq POSIXPERM
	test_set_prereq EXECKEEPSPID
	test_set_prereq NOT_MINGW
	test_set_prereq SED_STRIPS_CR
611 612 613
	;;
*)
	test_set_prereq POSIXPERM
614
	test_set_prereq BSLASHPSPEC
615
	test_set_prereq EXECKEEPSPID
P
Pat Thoyts 已提交
616
	test_set_prereq NOT_MINGW
617 618
	;;
esac
619

620
( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
621
test -z "$NO_PERL" && test_set_prereq PERL
622
test -z "$NO_PYTHON" && test_set_prereq PYTHON
M
Michał Kiedrowicz 已提交
623
test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
624
test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
625

626
# Can we rely on git's output in the C locale?
627 628 629 630
if test -n "$GETTEXT_POISON"
then
	GIT_GETTEXT_POISON=YesPlease
	export GIT_GETTEXT_POISON
631
	test_set_prereq GETTEXT_POISON
632 633 634
else
	test_set_prereq C_LOCALE_OUTPUT
fi
635

636 637 638 639 640 641 642 643
# 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 "$@"
}

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
# 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
}

662 663 664
# test whether the filesystem supports symbolic links
ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
rm -f y
665 666 667 668

# 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