Update wiki links to the new short URL
[aur.git] / test / sharness.sh
blobace5a5b55e6653569761cf7bbd6d8dd73e20ae53
1 # Sharness test framework.
3 # Copyright (c) 2011-2012 Mathias Lafeldt
4 # Copyright (c) 2005-2012 Git project
5 # Copyright (c) 2005-2012 Junio C Hamano
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.
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.
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/ .
20 # Public: Current version of Sharness.
21 SHARNESS_VERSION="1.0.0"
22 export SHARNESS_VERSION
24 # Public: The file extension for tests. By default, it is set to "t".
25 : "${SHARNESS_TEST_EXTENSION:=t}"
26 export SHARNESS_TEST_EXTENSION
28 # Public: Root directory containing tests. Tests can override this variable,
29 # e.g. for testing Sharness itself.
30 if test -z "$SHARNESS_TEST_DIRECTORY"
31 then
32 SHARNESS_TEST_DIRECTORY=$(pwd)
33 else
34 # ensure that SHARNESS_TEST_DIRECTORY is an absolute path so that it
35 # is valid even if the current working directory is changed
36 SHARNESS_TEST_DIRECTORY=$(cd "$SHARNESS_TEST_DIRECTORY" && pwd) || exit 1
38 export SHARNESS_TEST_DIRECTORY
40 # Reset TERM to original terminal if found, otherwise save original TERM
41 [ "x" = "x$SHARNESS_ORIG_TERM" ] &&
42 SHARNESS_ORIG_TERM="$TERM" ||
43 TERM="$SHARNESS_ORIG_TERM"
44 # Public: The unsanitized TERM under which sharness is originally run
45 export SHARNESS_ORIG_TERM
47 # Export SHELL_PATH
48 : "${SHELL_PATH:=$SHELL}"
49 export SHELL_PATH
51 # if --tee was passed, write the output not only to the terminal, but
52 # additionally to the file test-results/$BASENAME.out, too.
53 case "$SHARNESS_TEST_TEE_STARTED, $* " in
54 done,*)
55 # do not redirect again
57 *' --tee '*|*' --verbose-log '*)
58 mkdir -p "$SHARNESS_TEST_DIRECTORY/test-results"
59 BASE="$SHARNESS_TEST_DIRECTORY/test-results/$(basename "$0" ".$SHARNESS_TEST_EXTENSION")"
61 # Make this filename available to the sub-process in case it is using
62 # --verbose-log.
63 SHARNESS_TEST_TEE_OUTPUT_FILE="$BASE.out"
64 export SHARNESS_TEST_TEE_OUTPUT_FILE
66 # Truncate before calling "tee -a" to get rid of the results
67 # from any previous runs.
68 : >"$SHARNESS_TEST_TEE_OUTPUT_FILE"
70 (SHARNESS_TEST_TEE_STARTED="done" ${SHELL_PATH} "$0" "$@" 2>&1;
71 echo $? >"$BASE.exit") | tee -a "$SHARNESS_TEST_TEE_OUTPUT_FILE"
72 test "$(cat "$BASE.exit")" = 0
73 exit
75 esac
77 # For repeatability, reset the environment to a known state.
78 # TERM is sanitized below, after saving color control sequences.
79 LANG=C
80 LC_ALL=C
81 PAGER="cat"
82 TZ=UTC
83 EDITOR=:
84 export LANG LC_ALL PAGER TZ EDITOR
85 unset VISUAL CDPATH GREP_OPTIONS
87 [ "x$TERM" != "xdumb" ] && (
88 [ -t 1 ] &&
89 tput bold >/dev/null 2>&1 &&
90 tput setaf 1 >/dev/null 2>&1 &&
91 tput sgr0 >/dev/null 2>&1
92 ) &&
93 color=t
95 while test "$#" -ne 0; do
96 case "$1" in
97 -d|--d|--de|--deb|--debu|--debug)
98 debug=t; shift ;;
99 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
100 immediate=t; shift ;;
101 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
102 TEST_LONG=t; export TEST_LONG; shift ;;
103 --in|--int|--inte|--inter|--intera|--interac|--interact|--interacti|--interactiv|--interactive|--interactive-|--interactive-t|--interactive-te|--interactive-tes|--interactive-test|--interactive-tests):
104 TEST_INTERACTIVE=t; export TEST_INTERACTIVE; verbose=t; shift ;;
105 -h|--h|--he|--hel|--help)
106 help=t; shift ;;
107 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
108 verbose=t; shift ;;
109 -q|--q|--qu|--qui|--quie|--quiet)
110 # Ignore --quiet under a TAP::Harness. Saying how many tests
111 # passed without the ok/not ok details is always an error.
112 test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
113 --chain-lint)
114 chain_lint=t; shift ;;
115 --no-chain-lint)
116 chain_lint=; shift ;;
117 --no-color)
118 color=; shift ;;
119 --tee)
120 shift ;; # was handled already
121 --root=*)
122 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
123 shift ;;
124 --verbose-log)
125 verbose_log=t
126 shift ;;
128 echo "error: unknown test option '$1'" >&2; exit 1 ;;
129 esac
130 done
132 if test -n "$color"; then
133 # Save the color control sequences now rather than run tput
134 # each time say_color() is called. This is done for two
135 # reasons:
136 # * TERM will be changed to dumb
137 # * HOME will be changed to a temporary directory and tput
138 # might need to read ~/.terminfo from the original HOME
139 # directory to get the control sequences
140 # Note: This approach assumes the control sequences don't end
141 # in a newline for any terminal of interest (command
142 # substitutions strip trailing newlines). Given that most
143 # (all?) terminals in common use are related to ECMA-48, this
144 # shouldn't be a problem.
145 say_color_error=$(tput bold; tput setaf 1) # bold red
146 say_color_skip=$(tput setaf 4) # blue
147 say_color_warn=$(tput setaf 3) # brown/yellow
148 say_color_pass=$(tput setaf 2) # green
149 say_color_info=$(tput setaf 6) # cyan
150 say_color_reset=$(tput sgr0)
151 say_color_raw="" # no formatting for normal text
152 say_color() {
153 test -z "$1" && test -n "$quiet" && return
154 case "$1" in
155 error) say_color_color=$say_color_error ;;
156 skip) say_color_color=$say_color_skip ;;
157 warn) say_color_color=$say_color_warn ;;
158 pass) say_color_color=$say_color_pass ;;
159 info) say_color_color=$say_color_info ;;
160 *) say_color_color=$say_color_raw ;;
161 esac
162 shift
163 printf '%s%s%s\n' "$say_color_color" "$*" "$say_color_reset"
165 else
166 say_color() {
167 test -z "$1" && test -n "$quiet" && return
168 shift
169 printf '%s\n' "$*"
173 TERM=dumb
174 export TERM
176 error() {
177 say_color error "error: $*"
178 EXIT_OK=t
179 exit 1
182 say() {
183 say_color info "$*"
186 test -n "${test_description:-}" || error "Test script did not set test_description."
188 if test "$help" = "t"; then
189 echo "$test_description"
190 exit 0
193 exec 5>&1
194 exec 6<&0
195 if test "$verbose_log" = "t"
196 then
197 exec 3>>"$SHARNESS_TEST_TEE_OUTPUT_FILE" 4>&3
198 elif test "$verbose" = "t"
199 then
200 exec 4>&2 3>&1
201 else
202 exec 4>/dev/null 3>/dev/null
205 test_failure=0
206 test_count=0
207 test_fixed=0
208 test_broken=0
209 test_success=0
211 die() {
212 code=$?
213 if test -n "$EXIT_OK"; then
214 exit $code
215 else
216 echo >&5 "FATAL: Unexpected exit with code $code"
217 exit 1
221 EXIT_OK=
222 trap 'die' EXIT
224 # Public: Define that a test prerequisite is available.
226 # The prerequisite can later be checked explicitly using test_have_prereq or
227 # implicitly by specifying the prerequisite name in calls to test_expect_success
228 # or test_expect_failure.
230 # $1 - Name of prerequisite (a simple word, in all capital letters by convention)
232 # Examples
234 # # Set PYTHON prerequisite if interpreter is available.
235 # command -v python >/dev/null && test_set_prereq PYTHON
237 # # Set prerequisite depending on some variable.
238 # test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
240 # Returns nothing.
241 test_set_prereq() {
242 satisfied_prereq="$satisfied_prereq$1 "
244 satisfied_prereq=" "
246 # Public: Check if one or more test prerequisites are defined.
248 # The prerequisites must have previously been set with test_set_prereq.
249 # The most common use of this is to skip all the tests if some essential
250 # prerequisite is missing.
252 # $1 - Comma-separated list of test prerequisites.
254 # Examples
256 # # Skip all remaining tests if prerequisite is not set.
257 # if ! test_have_prereq PERL; then
258 # skip_all='skipping perl interface tests, perl not available'
259 # test_done
260 # fi
262 # Returns 0 if all prerequisites are defined or 1 otherwise.
263 test_have_prereq() {
264 # prerequisites can be concatenated with ','
265 save_IFS=$IFS
266 IFS=,
267 set -- $@
268 IFS=$save_IFS
270 total_prereq=0
271 ok_prereq=0
272 missing_prereq=
274 for prerequisite; do
275 case "$prerequisite" in
277 negative_prereq=t
278 prerequisite=${prerequisite#!}
281 negative_prereq=
282 esac
284 total_prereq=$((total_prereq + 1))
285 case "$satisfied_prereq" in
286 *" $prerequisite "*)
287 satisfied_this_prereq=t
290 satisfied_this_prereq=
291 esac
293 case "$satisfied_this_prereq,$negative_prereq" in
294 t,|,t)
295 ok_prereq=$((ok_prereq + 1))
298 # Keep a list of missing prerequisites; restore
299 # the negative marker if necessary.
300 prerequisite=${negative_prereq:+!}$prerequisite
301 if test -z "$missing_prereq"; then
302 missing_prereq=$prerequisite
303 else
304 missing_prereq="$prerequisite,$missing_prereq"
306 esac
307 done
309 test $total_prereq = $ok_prereq
312 # You are not expected to call test_ok_ and test_failure_ directly, use
313 # the text_expect_* functions instead.
315 test_ok_() {
316 test_success=$((test_success + 1))
317 say_color "" "ok $test_count - $*"
320 test_failure_() {
321 test_failure=$((test_failure + 1))
322 say_color error "not ok $test_count - $1"
323 shift
324 echo "$@" | sed -e 's/^/# /'
325 test "$immediate" = "" || { EXIT_OK=t; exit 1; }
328 test_known_broken_ok_() {
329 test_fixed=$((test_fixed + 1))
330 say_color error "ok $test_count - $* # TODO known breakage vanished"
333 test_known_broken_failure_() {
334 test_broken=$((test_broken + 1))
335 say_color warn "not ok $test_count - $* # TODO known breakage"
338 # Public: Execute commands in debug mode.
340 # Takes a single argument and evaluates it only when the test script is started
341 # with --debug. This is primarily meant for use during the development of test
342 # scripts.
344 # $1 - Commands to be executed.
346 # Examples
348 # test_debug "cat some_log_file"
350 # Returns the exit code of the last command executed in debug mode or 0
351 # otherwise.
352 test_debug() {
353 test "$debug" = "" || eval "$1"
356 # Public: Stop execution and start a shell.
358 # This is useful for debugging tests and only makes sense together with "-v".
359 # Be sure to remove all invocations of this command before submitting.
360 test_pause() {
361 if test "$verbose" = t; then
362 "$SHELL_PATH" <&6 >&3 2>&4
363 else
364 error >&5 "test_pause requires --verbose"
368 test_eval_() {
369 # This is a separate function because some tests use
370 # "return" to end a test_expect_success block early.
371 case ",$test_prereq," in
372 *,INTERACTIVE,*)
373 eval "$*"
376 eval </dev/null >&3 2>&4 "$*"
378 esac
381 test_run_() {
382 test_cleanup=:
383 expecting_failure=$2
384 test_eval_ "$1"
385 eval_ret=$?
387 if test "$chain_lint" = "t"; then
388 test_eval_ "(exit 117) && $1"
389 if test "$?" != 117; then
390 error "bug in the test script: broken &&-chain: $1"
394 if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"; then
395 test_eval_ "$test_cleanup"
397 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
398 echo ""
400 return "$eval_ret"
403 test_skip_() {
404 test_count=$((test_count + 1))
405 to_skip=
406 for skp in $SKIP_TESTS; do
407 case $this_test.$test_count in
408 $skp)
409 to_skip=t
410 break
411 esac
412 done
413 if test -z "$to_skip" && test -n "$test_prereq" && ! test_have_prereq "$test_prereq"; then
414 to_skip=t
416 case "$to_skip" in
418 of_prereq=
419 if test "$missing_prereq" != "$test_prereq"; then
420 of_prereq=" of $test_prereq"
423 say_color skip >&3 "skipping test: $*"
424 say_color skip "ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
425 : true
428 false
430 esac
433 # Public: Run test commands and expect them to succeed.
435 # When the test passed, an "ok" message is printed and the number of successful
436 # tests is incremented. When it failed, a "not ok" message is printed and the
437 # number of failed tests is incremented.
439 # With --immediate, exit test immediately upon the first failed test.
441 # Usually takes two arguments:
442 # $1 - Test description
443 # $2 - Commands to be executed.
445 # With three arguments, the first will be taken to be a prerequisite:
446 # $1 - Comma-separated list of test prerequisites. The test will be skipped if
447 # not all of the given prerequisites are set. To negate a prerequisite,
448 # put a "!" in front of it.
449 # $2 - Test description
450 # $3 - Commands to be executed.
452 # Examples
454 # test_expect_success \
455 # 'git-write-tree should be able to write an empty tree.' \
456 # 'tree=$(git-write-tree)'
458 # # Test depending on one prerequisite.
459 # test_expect_success TTY 'git --paginate rev-list uses a pager' \
460 # ' ... '
462 # # Multiple prerequisites are separated by a comma.
463 # test_expect_success PERL,PYTHON 'yo dawg' \
464 # ' test $(perl -E 'print eval "1 +" . qx[python -c "print 2"]') == "4" '
466 # Returns nothing.
467 test_expect_success() {
468 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
469 test "$#" = 2 || error "bug in the test script: not 2 or 3 parameters to test_expect_success"
470 export test_prereq
471 if ! test_skip_ "$@"; then
472 say >&3 "expecting success: $2"
473 if test_run_ "$2"; then
474 test_ok_ "$1"
475 else
476 test_failure_ "$@"
479 echo >&3 ""
482 # Public: Run test commands and expect them to fail. Used to demonstrate a known
483 # breakage.
485 # This is NOT the opposite of test_expect_success, but rather used to mark a
486 # test that demonstrates a known breakage.
488 # When the test passed, an "ok" message is printed and the number of fixed tests
489 # is incremented. When it failed, a "not ok" message is printed and the number
490 # of tests still broken is incremented.
492 # Failures from these tests won't cause --immediate to stop.
494 # Usually takes two arguments:
495 # $1 - Test description
496 # $2 - Commands to be executed.
498 # With three arguments, the first will be taken to be a prerequisite:
499 # $1 - Comma-separated list of test prerequisites. The test will be skipped if
500 # not all of the given prerequisites are set. To negate a prerequisite,
501 # put a "!" in front of it.
502 # $2 - Test description
503 # $3 - Commands to be executed.
505 # Returns nothing.
506 test_expect_failure() {
507 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
508 test "$#" = 2 || error "bug in the test script: not 2 or 3 parameters to test_expect_failure"
509 export test_prereq
510 if ! test_skip_ "$@"; then
511 say >&3 "checking known breakage: $2"
512 if test_run_ "$2" expecting_failure; then
513 test_known_broken_ok_ "$1"
514 else
515 test_known_broken_failure_ "$1"
518 echo >&3 ""
521 # Public: Run test commands and expect anything from them. Used when a
522 # test is not stable or not finished for some reason.
524 # When the test passed, an "ok" message is printed, but the number of
525 # fixed tests is not incremented.
527 # When it failed, a "not ok ... # TODO known breakage" message is
528 # printed, and the number of tests still broken is incremented.
530 # Failures from these tests won't cause --immediate to stop.
532 # Usually takes two arguments:
533 # $1 - Test description
534 # $2 - Commands to be executed.
536 # With three arguments, the first will be taken to be a prerequisite:
537 # $1 - Comma-separated list of test prerequisites. The test will be skipped if
538 # not all of the given prerequisites are set. To negate a prerequisite,
539 # put a "!" in front of it.
540 # $2 - Test description
541 # $3 - Commands to be executed.
543 # Returns nothing.
544 test_expect_unstable() {
545 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
546 test "$#" = 2 || error "bug in the test script: not 2 or 3 parameters to test_expect_unstable"
547 export test_prereq
548 if ! test_skip_ "$@"; then
549 say >&3 "checking unstable test: $2"
550 if test_run_ "$2" unstable; then
551 test_ok_ "$1"
552 else
553 test_known_broken_failure_ "$1"
556 echo >&3 ""
559 # Public: Run command and ensure that it fails in a controlled way.
561 # Use it instead of "! <command>". For example, when <command> dies due to a
562 # segfault, test_must_fail diagnoses it as an error, while "! <command>" would
563 # mistakenly be treated as just another expected failure.
565 # This is one of the prefix functions to be used inside test_expect_success or
566 # test_expect_failure.
568 # $1.. - Command to be executed.
570 # Examples
572 # test_expect_success 'complain and die' '
573 # do something &&
574 # do something else &&
575 # test_must_fail git checkout ../outerspace
578 # Returns 1 if the command succeeded (exit code 0).
579 # Returns 1 if the command died by signal (exit codes 130-192)
580 # Returns 1 if the command could not be found (exit code 127).
581 # Returns 0 otherwise.
582 test_must_fail() {
583 "$@"
584 exit_code=$?
585 if test $exit_code = 0; then
586 echo >&2 "test_must_fail: command succeeded: $*"
587 return 1
588 elif test $exit_code -gt 129 -a $exit_code -le 192; then
589 echo >&2 "test_must_fail: died by signal: $*"
590 return 1
591 elif test $exit_code = 127; then
592 echo >&2 "test_must_fail: command not found: $*"
593 return 1
595 return 0
598 # Public: Run command and ensure that it succeeds or fails in a controlled way.
600 # Similar to test_must_fail, but tolerates success too. Use it instead of
601 # "<command> || :" to catch failures caused by a segfault, for instance.
603 # This is one of the prefix functions to be used inside test_expect_success or
604 # test_expect_failure.
606 # $1.. - Command to be executed.
608 # Examples
610 # test_expect_success 'some command works without configuration' '
611 # test_might_fail git config --unset all.configuration &&
612 # do something
615 # Returns 1 if the command died by signal (exit codes 130-192)
616 # Returns 1 if the command could not be found (exit code 127).
617 # Returns 0 otherwise.
618 test_might_fail() {
619 "$@"
620 exit_code=$?
621 if test $exit_code -gt 129 -a $exit_code -le 192; then
622 echo >&2 "test_might_fail: died by signal: $*"
623 return 1
624 elif test $exit_code = 127; then
625 echo >&2 "test_might_fail: command not found: $*"
626 return 1
628 return 0
631 # Public: Run command and ensure it exits with a given exit code.
633 # This is one of the prefix functions to be used inside test_expect_success or
634 # test_expect_failure.
636 # $1 - Expected exit code.
637 # $2.. - Command to be executed.
639 # Examples
641 # test_expect_success 'Merge with d/f conflicts' '
642 # test_expect_code 1 git merge "merge msg" B master
645 # Returns 0 if the expected exit code is returned or 1 otherwise.
646 test_expect_code() {
647 want_code=$1
648 shift
649 "$@"
650 exit_code=$?
651 if test "$exit_code" = "$want_code"; then
652 return 0
655 echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
656 return 1
659 # Public: Compare two files to see if expected output matches actual output.
661 # The TEST_CMP variable defines the command used for the comparison; it
662 # defaults to "diff -u". Only when the test script was started with --verbose,
663 # will the command's output, the diff, be printed to the standard output.
665 # This is one of the prefix functions to be used inside test_expect_success or
666 # test_expect_failure.
668 # $1 - Path to file with expected output.
669 # $2 - Path to file with actual output.
671 # Examples
673 # test_expect_success 'foo works' '
674 # echo expected >expected &&
675 # foo >actual &&
676 # test_cmp expected actual
679 # Returns the exit code of the command set by TEST_CMP.
680 test_cmp() {
681 ${TEST_CMP:-diff -u} "$@"
684 # Public: portably print a sequence of numbers.
686 # seq is not in POSIX and GNU seq might not be available everywhere,
687 # so it is nice to have a seq implementation, even a very simple one.
689 # $1 - Starting number.
690 # $2 - Ending number.
692 # Examples
694 # test_expect_success 'foo works 10 times' '
695 # for i in $(test_seq 1 10)
696 # do
697 # foo || return
698 # done
701 # Returns 0 if all the specified numbers can be displayed.
702 test_seq() {
703 i="$1"
704 j="$2"
705 while test "$i" -le "$j"
707 echo "$i" || return
708 i=$(("$i" + 1))
709 done
712 # Public: Check if the file expected to be empty is indeed empty, and barfs
713 # otherwise.
715 # $1 - File to check for emptiness.
717 # Returns 0 if file is empty, 1 otherwise.
718 test_must_be_empty() {
719 if test -s "$1"
720 then
721 echo "'$1' is not empty, it contains:"
722 cat "$1"
723 return 1
727 # debugging-friendly alternatives to "test [-f|-d|-e]"
728 # The commands test the existence or non-existence of $1. $2 can be
729 # given to provide a more precise diagnosis.
730 test_path_is_file () {
731 if ! test -f "$1"
732 then
733 echo "File $1 doesn't exist. $2"
734 false
738 test_path_is_dir () {
739 if ! test -d "$1"
740 then
741 echo "Directory $1 doesn't exist. $2"
742 false
746 # Check if the directory exists and is empty as expected, barf otherwise.
747 test_dir_is_empty () {
748 test_path_is_dir "$1" &&
749 if test -n "$(find "$1" -mindepth 1 -maxdepth 1)"
750 then
751 echo "Directory '$1' is not empty, it contains:"
752 ls -la "$1"
753 return 1
757 # Public: Schedule cleanup commands to be run unconditionally at the end of a
758 # test.
760 # If some cleanup command fails, the test will not pass. With --immediate, no
761 # cleanup is done to help diagnose what went wrong.
763 # This is one of the prefix functions to be used inside test_expect_success or
764 # test_expect_failure.
766 # $1.. - Commands to prepend to the list of cleanup commands.
768 # Examples
770 # test_expect_success 'test core.capslock' '
771 # git config core.capslock true &&
772 # test_when_finished "git config --unset core.capslock" &&
773 # do_something
776 # Returns the exit code of the last cleanup command executed.
777 test_when_finished() {
778 test_cleanup="{ $*
779 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
782 # Public: Schedule cleanup commands to be run unconditionally when all tests
783 # have run.
785 # This can be used to clean up things like test databases. It is not needed to
786 # clean up temporary files, as test_done already does that.
788 # Examples:
790 # cleanup mysql -e "DROP DATABASE mytest"
792 # Returns the exit code of the last cleanup command executed.
793 final_cleanup=
794 cleanup() {
795 final_cleanup="{ $*
796 } && (exit \"\$eval_ret\"); eval_ret=\$?; $final_cleanup"
799 # Public: Summarize test results and exit with an appropriate error code.
801 # Must be called at the end of each test script.
803 # Can also be used to stop tests early and skip all remaining tests. For this,
804 # set skip_all to a string explaining why the tests were skipped before calling
805 # test_done.
807 # Examples
809 # # Each test script must call test_done at the end.
810 # test_done
812 # # Skip all remaining tests if prerequisite is not set.
813 # if ! test_have_prereq PERL; then
814 # skip_all='skipping perl interface tests, perl not available'
815 # test_done
816 # fi
818 # Returns 0 if all tests passed or 1 if there was a failure.
819 test_done() {
820 EXIT_OK=t
822 if test -z "$HARNESS_ACTIVE"; then
823 test_results_dir="$SHARNESS_TEST_DIRECTORY/test-results"
824 mkdir -p "$test_results_dir"
825 test_results_path="$test_results_dir/$this_test.$$.counts"
827 cat >>"$test_results_path" <<-EOF
828 total $test_count
829 success $test_success
830 fixed $test_fixed
831 broken $test_broken
832 failed $test_failure
837 if test "$test_fixed" != 0; then
838 say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
840 if test "$test_broken" != 0; then
841 say_color warn "# still have $test_broken known breakage(s)"
843 if test "$test_broken" != 0 || test "$test_fixed" != 0; then
844 test_remaining=$((test_count - test_broken - test_fixed))
845 msg="remaining $test_remaining test(s)"
846 else
847 test_remaining=$test_count
848 msg="$test_count test(s)"
851 case "$test_failure" in
853 # Maybe print SKIP message
854 if test -n "$skip_all" && test $test_count -gt 0; then
855 error "Can't use skip_all after running some tests"
857 [ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"
859 if test $test_remaining -gt 0; then
860 say_color pass "# passed all $msg"
862 say "1..$test_count$skip_all"
864 test_eval_ "$final_cleanup"
866 test -d "$remove_trash" &&
867 cd "$(dirname "$remove_trash")" &&
868 rm -rf "$(basename "$remove_trash")"
870 exit 0 ;;
873 say_color error "# failed $test_failure among $msg"
874 say "1..$test_count"
876 exit 1 ;;
878 esac
881 # Public: Source directory of test code and sharness library.
882 # This directory may be different from the directory in which tests are
883 # being run.
884 : "${SHARNESS_TEST_SRCDIR:=$(cd "$(dirname "$0")" && pwd)}"
885 export SHARNESS_TEST_SRCDIR
887 # Public: Build directory that will be added to PATH. By default, it is set to
888 # the parent directory of SHARNESS_TEST_DIRECTORY.
889 : "${SHARNESS_BUILD_DIRECTORY:="$SHARNESS_TEST_DIRECTORY/.."}"
890 PATH="$SHARNESS_BUILD_DIRECTORY:$PATH"
891 export PATH SHARNESS_BUILD_DIRECTORY
893 # Public: Path to test script currently executed.
894 SHARNESS_TEST_FILE="$0"
895 export SHARNESS_TEST_FILE
897 # Prepare test area.
898 SHARNESS_TRASH_DIRECTORY="trash directory.$(basename "$SHARNESS_TEST_FILE" ".$SHARNESS_TEST_EXTENSION")"
899 test -n "$root" && SHARNESS_TRASH_DIRECTORY="$root/$SHARNESS_TRASH_DIRECTORY"
900 case "$SHARNESS_TRASH_DIRECTORY" in
901 /*) ;; # absolute path is good
902 *) SHARNESS_TRASH_DIRECTORY="$SHARNESS_TEST_DIRECTORY/$SHARNESS_TRASH_DIRECTORY" ;;
903 esac
904 test "$debug" = "t" || remove_trash="$SHARNESS_TRASH_DIRECTORY"
905 rm -rf "$SHARNESS_TRASH_DIRECTORY" || {
906 EXIT_OK=t
907 echo >&5 "FATAL: Cannot prepare test area"
908 exit 1
913 # Load any extensions in $srcdir/sharness.d/*.sh
915 if test -d "${SHARNESS_TEST_SRCDIR}/sharness.d"
916 then
917 for file in "${SHARNESS_TEST_SRCDIR}"/sharness.d/*.sh
919 # Ensure glob was not an empty match:
920 test -e "${file}" || break
922 if test -n "$debug"
923 then
924 echo >&5 "sharness: loading extensions from ${file}"
926 . "${file}"
927 if test $? != 0
928 then
929 echo >&5 "sharness: Error loading ${file}. Aborting."
930 exit 1
932 done
935 # Public: Empty trash directory, the test area, provided for each test. The HOME
936 # variable is set to that directory too.
937 export SHARNESS_TRASH_DIRECTORY
939 HOME="$SHARNESS_TRASH_DIRECTORY"
940 export HOME
942 mkdir -p "$SHARNESS_TRASH_DIRECTORY" || exit 1
943 # Use -P to resolve symlinks in our working directory so that the cwd
944 # in subprocesses like git equals our $PWD (for pathname comparisons).
945 cd -P "$SHARNESS_TRASH_DIRECTORY" || exit 1
947 this_test=${SHARNESS_TEST_FILE##*/}
948 this_test=${this_test%.$SHARNESS_TEST_EXTENSION}
949 for skp in $SKIP_TESTS; do
950 case "$this_test" in
951 $skp)
952 say_color info >&3 "skipping test $this_test altogether"
953 skip_all="skip all tests in $this_test"
954 test_done
955 esac
956 done
958 test -n "$TEST_LONG" && test_set_prereq EXPENSIVE
959 test -n "$TEST_INTERACTIVE" && test_set_prereq INTERACTIVE
961 # Make sure this script ends with code 0
964 # vi: set ts=4 sw=4 noet :