tomb

the crypto undertaker
git clone git://parazyd.org/tomb.git
Log | Files | Refs | README | LICENSE

sharness (22676B)


      1 #!/bin/sh
      2 #
      3 # Copyright (c) 2011-2012 Mathias Lafeldt
      4 # Copyright (c) 2005-2012 Git project
      5 # Copyright (c) 2005-2012 Junio C Hamano
      6 #
      7 # This program is free software: you can redistribute it and/or modify
      8 # it under the terms of the GNU General Public License as published by
      9 # the Free Software Foundation, either version 2 of the License, or
     10 # (at your option) any later version.
     11 #
     12 # This program is distributed in the hope that it will be useful,
     13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 # GNU General Public License for more details.
     16 #
     17 # You should have received a copy of the GNU General Public License
     18 # along with this program.  If not, see http://www.gnu.org/licenses/ .
     19 
     20 # Public: Current version of Sharness.
     21 SHARNESS_VERSION="1.0.0"
     22 export SHARNESS_VERSION
     23 
     24 # Public: The file extension for tests.  By default, it is set to "t".
     25 : ${SHARNESS_TEST_EXTENSION:=t}
     26 export SHARNESS_TEST_EXTENSION
     27 
     28 #  Reset TERM to original terminal if found, otherwise save orignal TERM
     29 [ "x" = "x$SHARNESS_ORIG_TERM" ] &&
     30 		SHARNESS_ORIG_TERM="$TERM" ||
     31 		TERM="$SHARNESS_ORIG_TERM"
     32 # Public: The unsanitized TERM under which sharness is originally run
     33 export SHARNESS_ORIG_TERM
     34 
     35 # Export SHELL_PATH
     36 : ${SHELL_PATH:=$SHELL}
     37 export SHELL_PATH
     38 
     39 # For repeatability, reset the environment to a known state.
     40 # TERM is sanitized below, after saving color control sequences.
     41 LANG=C
     42 LC_ALL=C
     43 PAGER=cat
     44 TZ=UTC
     45 EDITOR=:
     46 export LANG LC_ALL PAGER TZ EDITOR
     47 unset VISUAL CDPATH GREP_OPTIONS
     48 
     49 # Line feed
     50 LF='
     51 '
     52 
     53 [ "x$TERM" != "xdumb" ] && (
     54 		[ -t 1 ] &&
     55 		tput bold >/dev/null 2>&1 &&
     56 		tput setaf 1 >/dev/null 2>&1 &&
     57 		tput sgr0 >/dev/null 2>&1
     58 	) &&
     59 	color=t
     60 
     61 while test "$#" -ne 0; do
     62 	case "$1" in
     63 	-d|--d|--de|--deb|--debu|--debug)
     64 		debug=t; shift ;;
     65 	-i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
     66 		immediate=t; shift ;;
     67 	-l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
     68 		TEST_LONG=t; export TEST_LONG; shift ;;
     69 	--in|--int|--inte|--inter|--intera|--interac|--interact|--interacti|--interactiv|--interactive|--interactive-|--interactive-t|--interactive-te|--interactive-tes|--interactive-test|--interactive-tests):
     70 		TEST_INTERACTIVE=t; export TEST_INTERACTIVE; verbose=t; shift ;;
     71 	-h|--h|--he|--hel|--help)
     72 		help=t; shift ;;
     73 	-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
     74 		verbose=t; shift ;;
     75 	-q|--q|--qu|--qui|--quie|--quiet)
     76 		# Ignore --quiet under a TAP::Harness. Saying how many tests
     77 		# passed without the ok/not ok details is always an error.
     78 		test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
     79 	--chain-lint)
     80 		chain_lint=t; shift ;;
     81 	--no-chain-lint)
     82 		chain_lint=; shift ;;
     83 	--no-color)
     84 		color=; shift ;;
     85 	--root=*)
     86 		root=$(expr "z$1" : 'z[^=]*=\(.*\)')
     87 		shift ;;
     88 	*)
     89 		echo "error: unknown test option '$1'" >&2; exit 1 ;;
     90 	esac
     91 done
     92 
     93 if test -n "$color"; then
     94 	# Save the color control sequences now rather than run tput
     95 	# each time say_color() is called.  This is done for two
     96 	# reasons:
     97 	#   * TERM will be changed to dumb
     98 	#   * HOME will be changed to a temporary directory and tput
     99 	#     might need to read ~/.terminfo from the original HOME
    100 	#     directory to get the control sequences
    101 	# Note:  This approach assumes the control sequences don't end
    102 	# in a newline for any terminal of interest (command
    103 	# substitutions strip trailing newlines).  Given that most
    104 	# (all?) terminals in common use are related to ECMA-48, this
    105 	# shouldn't be a problem.
    106 	say_color_error=$(tput bold; tput setaf 1) # bold red
    107 	say_color_skip=$(tput setaf 4) # blue
    108 	say_color_warn=$(tput setaf 3) # brown/yellow
    109 	say_color_pass=$(tput setaf 2) # green
    110 	say_color_info=$(tput setaf 6) # cyan
    111 	say_color_reset=$(tput sgr0)
    112 	say_color_="" # no formatting for normal text
    113 	say_color() {
    114 		test -z "$1" && test -n "$quiet" && return
    115 		eval "say_color_color=\$say_color_$1"
    116 		shift
    117 		printf "%s\\n" "$say_color_color$*$say_color_reset"
    118 	}
    119 else
    120 	say_color() {
    121 		test -z "$1" && test -n "$quiet" && return
    122 		shift
    123 		printf "%s\n" "$*"
    124 	}
    125 fi
    126 
    127 TERM=dumb
    128 export TERM
    129 
    130 error() {
    131 	say_color error "error: $*"
    132 	EXIT_OK=t
    133 	exit 1
    134 }
    135 
    136 say() {
    137 	say_color info "$*"
    138 }
    139 
    140 test -n "$test_description" || error "Test script did not set test_description."
    141 
    142 if test "$help" = "t"; then
    143 	echo "$test_description"
    144 	exit 0
    145 fi
    146 
    147 exec 5>&1
    148 exec 6<&0
    149 if test "$verbose" = "t"; then
    150 	exec 4>&2 3>&1
    151 else
    152 	exec 4>/dev/null 3>/dev/null
    153 fi
    154 
    155 test_failure=0
    156 test_count=0
    157 test_fixed=0
    158 test_broken=0
    159 test_success=0
    160 
    161 die() {
    162 	code=$?
    163 	if test -n "$EXIT_OK"; then
    164 		exit $code
    165 	else
    166 		echo >&5 "FATAL: Unexpected exit with code $code"
    167 		exit 1
    168 	fi
    169 }
    170 
    171 EXIT_OK=
    172 trap 'die' EXIT
    173 
    174 # Public: Define that a test prerequisite is available.
    175 #
    176 # The prerequisite can later be checked explicitly using test_have_prereq or
    177 # implicitly by specifying the prerequisite name in calls to test_expect_success
    178 # or test_expect_failure.
    179 #
    180 # $1 - Name of prerequiste (a simple word, in all capital letters by convention)
    181 #
    182 # Examples
    183 #
    184 #   # Set PYTHON prerequisite if interpreter is available.
    185 #   command -v python >/dev/null && test_set_prereq PYTHON
    186 #
    187 #   # Set prerequisite depending on some variable.
    188 #   test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
    189 #
    190 # Returns nothing.
    191 test_set_prereq() {
    192 	satisfied_prereq="$satisfied_prereq$1 "
    193 }
    194 satisfied_prereq=" "
    195 
    196 # Public: Check if one or more test prerequisites are defined.
    197 #
    198 # The prerequisites must have previously been set with test_set_prereq.
    199 # The most common use of this is to skip all the tests if some essential
    200 # prerequisite is missing.
    201 #
    202 # $1 - Comma-separated list of test prerequisites.
    203 #
    204 # Examples
    205 #
    206 #   # Skip all remaining tests if prerequisite is not set.
    207 #   if ! test_have_prereq PERL; then
    208 #       skip_all='skipping perl interface tests, perl not available'
    209 #       test_done
    210 #   fi
    211 #
    212 # Returns 0 if all prerequisites are defined or 1 otherwise.
    213 test_have_prereq() {
    214 	# prerequisites can be concatenated with ','
    215 	save_IFS=$IFS
    216 	IFS=,
    217 	set -- $*
    218 	IFS=$save_IFS
    219 
    220 	total_prereq=0
    221 	ok_prereq=0
    222 	missing_prereq=
    223 
    224 	for prerequisite; do
    225 		case "$prerequisite" in
    226 		!*)
    227 			negative_prereq=t
    228 			prerequisite=${prerequisite#!}
    229 			;;
    230 		*)
    231 			negative_prereq=
    232 		esac
    233 
    234 		total_prereq=$(($total_prereq + 1))
    235 		case "$satisfied_prereq" in
    236 		*" $prerequisite "*)
    237 			satisfied_this_prereq=t
    238 			;;
    239 		*)
    240 			satisfied_this_prereq=
    241 		esac
    242 
    243 		case "$satisfied_this_prereq,$negative_prereq" in
    244 		t,|,t)
    245 			ok_prereq=$(($ok_prereq + 1))
    246 			;;
    247 		*)
    248 			# Keep a list of missing prerequisites; restore
    249 			# the negative marker if necessary.
    250 			prerequisite=${negative_prereq:+!}$prerequisite
    251 			if test -z "$missing_prereq"; then
    252 				missing_prereq=$prerequisite
    253 			else
    254 				missing_prereq="$prerequisite,$missing_prereq"
    255 			fi
    256 		esac
    257 	done
    258 
    259 	test $total_prereq = $ok_prereq
    260 }
    261 
    262 # You are not expected to call test_ok_ and test_failure_ directly, use
    263 # the text_expect_* functions instead.
    264 
    265 test_ok_() {
    266 	test_success=$(($test_success + 1))
    267 	say_color "" "ok $test_count - $@"
    268 }
    269 
    270 test_failure_() {
    271 	test_failure=$(($test_failure + 1))
    272 	say_color error "not ok $test_count - $1"
    273 	shift
    274 	echo "$@" | sed -e 's/^/#	/'
    275 	test "$immediate" = "" || { EXIT_OK=t; exit 1; }
    276 }
    277 
    278 test_known_broken_ok_() {
    279 	test_fixed=$(($test_fixed + 1))
    280 	say_color error "ok $test_count - $@ # TODO known breakage vanished"
    281 }
    282 
    283 test_known_broken_failure_() {
    284 	test_broken=$(($test_broken + 1))
    285 	say_color warn "not ok $test_count - $@ # TODO known breakage"
    286 }
    287 
    288 # Public: Execute commands in debug mode.
    289 #
    290 # Takes a single argument and evaluates it only when the test script is started
    291 # with --debug. This is primarily meant for use during the development of test
    292 # scripts.
    293 #
    294 # $1 - Commands to be executed.
    295 #
    296 # Examples
    297 #
    298 #   test_debug "cat some_log_file"
    299 #
    300 # Returns the exit code of the last command executed in debug mode or 0
    301 #   otherwise.
    302 test_debug() {
    303 	test "$debug" = "" || eval "$1"
    304 }
    305 
    306 # Public: Stop execution and start a shell.
    307 #
    308 # This is useful for debugging tests and only makes sense together with "-v".
    309 # Be sure to remove all invocations of this command before submitting.
    310 test_pause() {
    311 	if test "$verbose" = t; then
    312 		"$SHELL_PATH" <&6 >&3 2>&4
    313 	else
    314 		error >&5 "test_pause requires --verbose"
    315 	fi
    316 }
    317 
    318 test_eval_() {
    319 	# This is a separate function because some tests use
    320 	# "return" to end a test_expect_success block early.
    321 	case ",$test_prereq," in
    322 	*,INTERACTIVE,*)
    323 		eval "$*"
    324 		;;
    325 	*)
    326 		eval </dev/null >&3 2>&4 "$*"
    327 		;;
    328 	esac
    329 }
    330 
    331 test_run_() {
    332 	test_cleanup=:
    333 	expecting_failure=$2
    334 	test_eval_ "$1"
    335 	eval_ret=$?
    336 
    337 	if test "$chain_lint" = "t"; then
    338 		test_eval_ "(exit 117) && $1"
    339 		if test "$?" != 117; then
    340 			error "bug in the test script: broken &&-chain: $1"
    341 		fi
    342 	fi
    343 
    344 	if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"; then
    345 		test_eval_ "$test_cleanup"
    346 	fi
    347 	if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
    348 		echo ""
    349 	fi
    350 	return "$eval_ret"
    351 }
    352 
    353 test_skip_() {
    354 	test_count=$(($test_count + 1))
    355 	to_skip=
    356 	for skp in $SKIP_TESTS; do
    357 		case $this_test.$test_count in
    358 		$skp)
    359 			to_skip=t
    360 			break
    361 		esac
    362 	done
    363 	if test -z "$to_skip" && test -n "$test_prereq" && ! test_have_prereq "$test_prereq"; then
    364 		to_skip=t
    365 	fi
    366 	case "$to_skip" in
    367 	t)
    368 		of_prereq=
    369 		if test "$missing_prereq" != "$test_prereq"; then
    370 			of_prereq=" of $test_prereq"
    371 		fi
    372 
    373 		say_color skip >&3 "skipping test: $@"
    374 		say_color skip "ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
    375 		: true
    376 		;;
    377 	*)
    378 		false
    379 		;;
    380 	esac
    381 }
    382 
    383 # Public: Run test commands and expect them to succeed.
    384 #
    385 # When the test passed, an "ok" message is printed and the number of successful
    386 # tests is incremented. When it failed, a "not ok" message is printed and the
    387 # number of failed tests is incremented.
    388 #
    389 # With --immediate, exit test immediately upon the first failed test.
    390 #
    391 # Usually takes two arguments:
    392 # $1 - Test description
    393 # $2 - Commands to be executed.
    394 #
    395 # With three arguments, the first will be taken to be a prerequisite:
    396 # $1 - Comma-separated list of test prerequisites. The test will be skipped if
    397 #      not all of the given prerequisites are set. To negate a prerequisite,
    398 #      put a "!" in front of it.
    399 # $2 - Test description
    400 # $3 - Commands to be executed.
    401 #
    402 # Examples
    403 #
    404 #   test_expect_success \
    405 #       'git-write-tree should be able to write an empty tree.' \
    406 #       'tree=$(git-write-tree)'
    407 #
    408 #   # Test depending on one prerequisite.
    409 #   test_expect_success TTY 'git --paginate rev-list uses a pager' \
    410 #       ' ... '
    411 #
    412 #   # Multiple prerequisites are separated by a comma.
    413 #   test_expect_success PERL,PYTHON 'yo dawg' \
    414 #       ' test $(perl -E 'print eval "1 +" . qx[python -c "print 2"]') == "4" '
    415 #
    416 # Returns nothing.
    417 test_expect_success() {
    418 	test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
    419 	test "$#" = 2 || error "bug in the test script: not 2 or 3 parameters to test_expect_success"
    420 	export test_prereq
    421 	if ! test_skip_ "$@"; then
    422 		say >&3 "expecting success: $2"
    423 		if test_run_ "$2"; then
    424 			test_ok_ "$1"
    425 		else
    426 			test_failure_ "$@"
    427 		fi
    428 	fi
    429 	echo >&3 ""
    430 }
    431 
    432 # Public: Run test commands and expect them to fail. Used to demonstrate a known
    433 # breakage.
    434 #
    435 # This is NOT the opposite of test_expect_success, but rather used to mark a
    436 # test that demonstrates a known breakage.
    437 #
    438 # When the test passed, an "ok" message is printed and the number of fixed tests
    439 # is incremented. When it failed, a "not ok" message is printed and the number
    440 # of tests still broken is incremented.
    441 #
    442 # Failures from these tests won't cause --immediate to stop.
    443 #
    444 # Usually takes two arguments:
    445 # $1 - Test description
    446 # $2 - Commands to be executed.
    447 #
    448 # With three arguments, the first will be taken to be a prerequisite:
    449 # $1 - Comma-separated list of test prerequisites. The test will be skipped if
    450 #      not all of the given prerequisites are set. To negate a prerequisite,
    451 #      put a "!" in front of it.
    452 # $2 - Test description
    453 # $3 - Commands to be executed.
    454 #
    455 # Returns nothing.
    456 test_expect_failure() {
    457 	test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
    458 	test "$#" = 2 || error "bug in the test script: not 2 or 3 parameters to test_expect_failure"
    459 	export test_prereq
    460 	if ! test_skip_ "$@"; then
    461 		say >&3 "checking known breakage: $2"
    462 		if test_run_ "$2" expecting_failure; then
    463 			test_known_broken_ok_ "$1"
    464 		else
    465 			test_known_broken_failure_ "$1"
    466 		fi
    467 	fi
    468 	echo >&3 ""
    469 }
    470 
    471 # Public: Run command and ensure that it fails in a controlled way.
    472 #
    473 # Use it instead of "! <command>". For example, when <command> dies due to a
    474 # segfault, test_must_fail diagnoses it as an error, while "! <command>" would
    475 # mistakenly be treated as just another expected failure.
    476 #
    477 # This is one of the prefix functions to be used inside test_expect_success or
    478 # test_expect_failure.
    479 #
    480 # $1.. - Command to be executed.
    481 #
    482 # Examples
    483 #
    484 #   test_expect_success 'complain and die' '
    485 #       do something &&
    486 #       do something else &&
    487 #       test_must_fail git checkout ../outerspace
    488 #   '
    489 #
    490 # Returns 1 if the command succeeded (exit code 0).
    491 # Returns 1 if the command died by signal (exit codes 130-192)
    492 # Returns 1 if the command could not be found (exit code 127).
    493 # Returns 0 otherwise.
    494 test_must_fail() {
    495 	"$@"
    496 	exit_code=$?
    497 	if test $exit_code = 0; then
    498 		echo >&2 "test_must_fail: command succeeded: $*"
    499 		return 1
    500 	elif test $exit_code -gt 129 -a $exit_code -le 192; then
    501 		echo >&2 "test_must_fail: died by signal: $*"
    502 		return 1
    503 	elif test $exit_code = 127; then
    504 		echo >&2 "test_must_fail: command not found: $*"
    505 		return 1
    506 	fi
    507 	return 0
    508 }
    509 
    510 # Public: Run command and ensure that it succeeds or fails in a controlled way.
    511 #
    512 # Similar to test_must_fail, but tolerates success too. Use it instead of
    513 # "<command> || :" to catch failures caused by a segfault, for instance.
    514 #
    515 # This is one of the prefix functions to be used inside test_expect_success or
    516 # test_expect_failure.
    517 #
    518 # $1.. - Command to be executed.
    519 #
    520 # Examples
    521 #
    522 #   test_expect_success 'some command works without configuration' '
    523 #       test_might_fail git config --unset all.configuration &&
    524 #       do something
    525 #   '
    526 #
    527 # Returns 1 if the command died by signal (exit codes 130-192)
    528 # Returns 1 if the command could not be found (exit code 127).
    529 # Returns 0 otherwise.
    530 test_might_fail() {
    531 	"$@"
    532 	exit_code=$?
    533 	if test $exit_code -gt 129 -a $exit_code -le 192; then
    534 		echo >&2 "test_might_fail: died by signal: $*"
    535 		return 1
    536 	elif test $exit_code = 127; then
    537 		echo >&2 "test_might_fail: command not found: $*"
    538 		return 1
    539 	fi
    540 	return 0
    541 }
    542 
    543 # Public: Run command and ensure it exits with a given exit code.
    544 #
    545 # This is one of the prefix functions to be used inside test_expect_success or
    546 # test_expect_failure.
    547 #
    548 # $1   - Expected exit code.
    549 # $2.. - Command to be executed.
    550 #
    551 # Examples
    552 #
    553 #   test_expect_success 'Merge with d/f conflicts' '
    554 #       test_expect_code 1 git merge "merge msg" B master
    555 #   '
    556 #
    557 # Returns 0 if the expected exit code is returned or 1 otherwise.
    558 test_expect_code() {
    559 	want_code=$1
    560 	shift
    561 	"$@"
    562 	exit_code=$?
    563 	if test $exit_code = $want_code; then
    564 		return 0
    565 	fi
    566 
    567 	echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
    568 	return 1
    569 }
    570 
    571 # Public: Compare two files to see if expected output matches actual output.
    572 #
    573 # The TEST_CMP variable defines the command used for the comparision; it
    574 # defaults to "diff -u". Only when the test script was started with --verbose,
    575 # will the command's output, the diff, be printed to the standard output.
    576 #
    577 # This is one of the prefix functions to be used inside test_expect_success or
    578 # test_expect_failure.
    579 #
    580 # $1 - Path to file with expected output.
    581 # $2 - Path to file with actual output.
    582 #
    583 # Examples
    584 #
    585 #   test_expect_success 'foo works' '
    586 #       echo expected >expected &&
    587 #       foo >actual &&
    588 #       test_cmp expected actual
    589 #   '
    590 #
    591 # Returns the exit code of the command set by TEST_CMP.
    592 test_cmp() {
    593 	${TEST_CMP:-diff -u} "$@"
    594 }
    595 
    596 # Public: portably print a sequence of numbers.
    597 #
    598 # seq is not in POSIX and GNU seq might not be available everywhere,
    599 # so it is nice to have a seq implementation, even a very simple one.
    600 #
    601 # $1 - Starting number.
    602 # $2 - Ending number.
    603 #
    604 # Examples
    605 #
    606 #   test_expect_success 'foo works 10 times' '
    607 #       for i in $(test_seq 1 10)
    608 #       do
    609 #           foo || return
    610 #       done
    611 #   '
    612 #
    613 # Returns 0 if all the specified numbers can be displayed.
    614 test_seq() {
    615 	i="$1"
    616 	j="$2"
    617 	while test "$i" -le "$j"
    618 	do
    619 		echo "$i" || return
    620 		i=$(expr "$i" + 1)
    621 	done
    622 }
    623 
    624 # Public: Check if the file expected to be empty is indeed empty, and barfs
    625 # otherwise.
    626 #
    627 # $1 - File to check for emptyness.
    628 #
    629 # Returns 0 if file is empty, 1 otherwise.
    630 test_must_be_empty() {
    631 	if test -s "$1"
    632 	then
    633 		echo "'$1' is not empty, it contains:"
    634 		cat "$1"
    635 		return 1
    636 	fi
    637 }
    638 
    639 # Public: Schedule cleanup commands to be run unconditionally at the end of a
    640 # test.
    641 #
    642 # If some cleanup command fails, the test will not pass. With --immediate, no
    643 # cleanup is done to help diagnose what went wrong.
    644 #
    645 # This is one of the prefix functions to be used inside test_expect_success or
    646 # test_expect_failure.
    647 #
    648 # $1.. - Commands to prepend to the list of cleanup commands.
    649 #
    650 # Examples
    651 #
    652 #   test_expect_success 'test core.capslock' '
    653 #       git config core.capslock true &&
    654 #       test_when_finished "git config --unset core.capslock" &&
    655 #       do_something
    656 #   '
    657 #
    658 # Returns the exit code of the last cleanup command executed.
    659 test_when_finished() {
    660 	test_cleanup="{ $*
    661 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
    662 }
    663 
    664 # Public: Schedule cleanup commands to be run unconditionally when all tests
    665 # have run.
    666 #
    667 # This can be used to clean up things like test databases. It is not needed to
    668 # clean up temporary files, as test_done already does that.
    669 #
    670 # Examples:
    671 #
    672 #   cleanup mysql -e "DROP DATABASE mytest"
    673 #
    674 # Returns the exit code of the last cleanup command executed.
    675 final_cleanup=
    676 cleanup() {
    677 	final_cleanup="{ $*
    678 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $final_cleanup"
    679 }
    680 
    681 # Public: Summarize test results and exit with an appropriate error code.
    682 #
    683 # Must be called at the end of each test script.
    684 #
    685 # Can also be used to stop tests early and skip all remaining tests. For this,
    686 # set skip_all to a string explaining why the tests were skipped before calling
    687 # test_done.
    688 #
    689 # Examples
    690 #
    691 #   # Each test script must call test_done at the end.
    692 #   test_done
    693 #
    694 #   # Skip all remaining tests if prerequisite is not set.
    695 #   if ! test_have_prereq PERL; then
    696 #       skip_all='skipping perl interface tests, perl not available'
    697 #       test_done
    698 #   fi
    699 #
    700 # Returns 0 if all tests passed or 1 if there was a failure.
    701 test_done() {
    702 	EXIT_OK=t
    703 
    704 	if test -z "$HARNESS_ACTIVE"; then
    705 		test_results_dir="$SHARNESS_TEST_DIRECTORY/test-results"
    706 		mkdir -p "$test_results_dir"
    707 		test_results_path="$test_results_dir/$this_test.$$.counts"
    708 
    709 		cat >>"$test_results_path" <<-EOF
    710 		total $test_count
    711 		success $test_success
    712 		fixed $test_fixed
    713 		broken $test_broken
    714 		failed $test_failure
    715 
    716 		EOF
    717 	fi
    718 
    719 	if test "$test_fixed" != 0; then
    720 		say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
    721 	fi
    722 	if test "$test_broken" != 0; then
    723 		say_color warn "# still have $test_broken known breakage(s)"
    724 	fi
    725 	if test "$test_broken" != 0 || test "$test_fixed" != 0; then
    726 		test_remaining=$(( $test_count - $test_broken - $test_fixed ))
    727 		msg="remaining $test_remaining test(s)"
    728 	else
    729 		test_remaining=$test_count
    730 		msg="$test_count test(s)"
    731 	fi
    732 
    733 	case "$test_failure" in
    734 	0)
    735 		# Maybe print SKIP message
    736 		if test -n "$skip_all" && test $test_count -gt 0; then
    737 			error "Can't use skip_all after running some tests"
    738 		fi
    739 		[ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"
    740 
    741 		if test $test_remaining -gt 0; then
    742 			say_color pass "# passed all $msg"
    743 		fi
    744 		say "1..$test_count$skip_all"
    745 
    746 		test_eval_ "$final_cleanup"
    747 
    748 		test -d "$remove_trash" &&
    749 		cd "$(dirname "$remove_trash")" &&
    750 		rm -rf "$(basename "$remove_trash")"
    751 
    752 		exit 0 ;;
    753 
    754 	*)
    755 		say_color error "# failed $test_failure among $msg"
    756 		say "1..$test_count"
    757 
    758 		exit 1 ;;
    759 
    760 	esac
    761 }
    762 
    763 # Public: Root directory containing tests. Tests can override this variable,
    764 # e.g. for testing Sharness itself.
    765 : ${SHARNESS_TEST_DIRECTORY:=$(pwd)}
    766 export SHARNESS_TEST_DIRECTORY
    767 
    768 # Public: Source directory of test code and sharness library.
    769 # This directory may be different from the directory in which tests are
    770 # being run.
    771 : ${SHARNESS_TEST_SRCDIR:=$(cd $(dirname $0) && pwd)}
    772 export SHARNESS_TEST_SRCDIR
    773 
    774 # Public: Build directory that will be added to PATH. By default, it is set to
    775 # the parent directory of SHARNESS_TEST_DIRECTORY.
    776 : ${SHARNESS_BUILD_DIRECTORY:="$SHARNESS_TEST_DIRECTORY/.."}
    777 PATH="$SHARNESS_BUILD_DIRECTORY:$PATH"
    778 export PATH SHARNESS_BUILD_DIRECTORY
    779 
    780 # Public: Path to test script currently executed.
    781 SHARNESS_TEST_FILE="$0"
    782 export SHARNESS_TEST_FILE
    783 
    784 # Prepare test area.
    785 SHARNESS_TRASH_DIRECTORY="trash directory.$(basename "$SHARNESS_TEST_FILE" ".$SHARNESS_TEST_EXTENSION")"
    786 test -n "$root" && SHARNESS_TRASH_DIRECTORY="$root/$SHARNESS_TRASH_DIRECTORY"
    787 case "$SHARNESS_TRASH_DIRECTORY" in
    788 /*) ;; # absolute path is good
    789  *) SHARNESS_TRASH_DIRECTORY="$SHARNESS_TEST_DIRECTORY/$SHARNESS_TRASH_DIRECTORY" ;;
    790 esac
    791 test "$debug" = "t" || remove_trash="$SHARNESS_TRASH_DIRECTORY"
    792 rm -rf "$SHARNESS_TRASH_DIRECTORY" || {
    793 	EXIT_OK=t
    794 	echo >&5 "FATAL: Cannot prepare test area"
    795 	exit 1
    796 }
    797 
    798 
    799 #
    800 #  Load any extensions in $srcdir/sharness.d/*.sh
    801 #
    802 if test -d "${SHARNESS_TEST_SRCDIR}/sharness.d"
    803 then
    804 	for file in "${SHARNESS_TEST_SRCDIR}"/sharness.d/*.sh
    805 	do
    806 		# Ensure glob was not an empty match:
    807 		test -e "${file}" || break
    808 
    809 		if test -n "$debug"
    810 		then
    811 			echo >&5 "sharness: loading extensions from ${file}"
    812 		fi
    813 		. "${file}"
    814 		if test $? != 0
    815 		then
    816 			echo >&5 "sharness: Error loading ${file}. Aborting."
    817 			exit 1
    818 		fi
    819 	done
    820 fi
    821 
    822 # Public: Empty trash directory, the test area, provided for each test. The HOME
    823 # variable is set to that directory too.
    824 export SHARNESS_TRASH_DIRECTORY
    825 
    826 HOME="$SHARNESS_TRASH_DIRECTORY"
    827 export HOME
    828 
    829 mkdir -p "$SHARNESS_TRASH_DIRECTORY" || exit 1
    830 # Use -P to resolve symlinks in our working directory so that the cwd
    831 # in subprocesses like git equals our $PWD (for pathname comparisons).
    832 cd -P "$SHARNESS_TRASH_DIRECTORY" || exit 1
    833 
    834 this_test=${SHARNESS_TEST_FILE##*/}
    835 this_test=${this_test%.$SHARNESS_TEST_EXTENSION}
    836 for skp in $SKIP_TESTS; do
    837 	case "$this_test" in
    838 	$skp)
    839 		say_color info >&3 "skipping test $this_test altogether"
    840 		skip_all="skip all tests in $this_test"
    841 		test_done
    842 	esac
    843 done
    844 
    845 test -n "$TEST_LONG" && test_set_prereq EXPENSIVE
    846 test -n "$TEST_INTERACTIVE" && test_set_prereq INTERACTIVE
    847 
    848 # Make sure this script ends with code 0
    849 :
    850 
    851 # vi: set ts=4 sw=4 noet :