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 # 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
36 : ${SHELL_PATH:=$SHELL}
39 # For repeatability, reset the environment to a known state.
40 # TERM is sanitized below, after saving color control sequences.
46 export LANG LC_ALL PAGER TZ EDITOR
47 unset VISUAL CDPATH GREP_OPTIONS
53 [ "x$TERM" != "xdumb" ] && (
55 tput bold
>/dev
/null
2>&1 &&
56 tput setaf
1 >/dev
/null
2>&1 &&
57 tput sgr0
>/dev
/null
2>&1
61 while test "$#" -ne 0; do
63 -d|
--d|
--de|
--deb|
--debu|
--debug)
65 -i|
--i|
--im|
--imm|
--imme|
--immed|
--immedi|
--immedia|
--immediat|
--immediate)
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)
73 -v|
--v|
--ve|
--ver|
--verb|
--verbo|
--verbos|
--verbose)
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 ;;
80 chain_lint
=t
; shift ;;
86 root
=$
(expr "z$1" : 'z[^=]*=\(.*\)')
89 echo "error: unknown test option '$1'" >&2; exit 1 ;;
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
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
114 test -z "$1" && test -n "$quiet" && return
115 eval "say_color_color=\$say_color_$1"
117 printf "%s\\n" "$say_color_color$*$say_color_reset"
121 test -z "$1" && test -n "$quiet" && return
131 say_color error
"error: $*"
140 test -n "$test_description" || error
"Test script did not set test_description."
142 if test "$help" = "t"; then
143 echo "$test_description"
149 if test "$verbose" = "t"; then
152 exec 4>/dev
/null
3>/dev
/null
163 if test -n "$EXIT_OK"; then
166 echo >&5 "FATAL: Unexpected exit with code $code"
174 # Public: Define that a test prerequisite is available.
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.
180 # $1 - Name of prerequiste (a simple word, in all capital letters by convention)
184 # # Set PYTHON prerequisite if interpreter is available.
185 # command -v python >/dev/null && test_set_prereq PYTHON
187 # # Set prerequisite depending on some variable.
188 # test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
192 satisfied_prereq
="$satisfied_prereq$1 "
196 # Public: Check if one or more test prerequisites are defined.
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.
202 # $1 - Comma-separated list of test prerequisites.
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'
212 # Returns 0 if all prerequisites are defined or 1 otherwise.
214 # prerequisites can be concatenated with ','
225 case "$prerequisite" in
228 prerequisite
=${prerequisite#!}
234 total_prereq
=$
(($total_prereq + 1))
235 case "$satisfied_prereq" in
237 satisfied_this_prereq
=t
240 satisfied_this_prereq
=
243 case "$satisfied_this_prereq,$negative_prereq" in
245 ok_prereq
=$
(($ok_prereq + 1))
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
254 missing_prereq
="$prerequisite,$missing_prereq"
259 test $total_prereq = $ok_prereq
262 # You are not expected to call test_ok_ and test_failure_ directly, use
263 # the text_expect_* functions instead.
266 test_success
=$
(($test_success + 1))
267 say_color
"" "ok $test_count - $@"
271 test_failure
=$
(($test_failure + 1))
272 say_color error
"not ok $test_count - $1"
274 echo "$@" |
sed -e 's/^/# /'
275 test "$immediate" = "" ||
{ EXIT_OK
=t
; exit 1; }
278 test_known_broken_ok_
() {
279 test_fixed
=$
(($test_fixed + 1))
280 say_color error
"ok $test_count - $@ # TODO known breakage vanished"
283 test_known_broken_failure_
() {
284 test_broken
=$
(($test_broken + 1))
285 say_color warn
"not ok $test_count - $@ # TODO known breakage"
288 # Public: Execute commands in debug mode.
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
294 # $1 - Commands to be executed.
298 # test_debug "cat some_log_file"
300 # Returns the exit code of the last command executed in debug mode or 0
303 test "$debug" = "" ||
eval "$1"
306 # Public: Stop execution and start a shell.
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.
311 if test "$verbose" = t
; then
312 "$SHELL_PATH" <&6 >&3 2>&4
314 error
>&5 "test_pause requires --verbose"
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
326 eval </dev
/null
>&3 2>&4 "$*"
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"
344 if test -z "$immediate" ||
test $eval_ret = 0 ||
test -n "$expecting_failure"; then
345 test_eval_
"$test_cleanup"
347 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
354 test_count
=$
(($test_count + 1))
356 for skp
in $SKIP_TESTS; do
357 case $this_test.
$test_count in
363 if test -z "$to_skip" && test -n "$test_prereq" && ! test_have_prereq
"$test_prereq"; then
369 if test "$missing_prereq" != "$test_prereq"; then
370 of_prereq
=" of $test_prereq"
373 say_color skip
>&3 "skipping test: $@"
374 say_color skip
"ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
383 # Public: Run test commands and expect them to succeed.
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.
389 # With --immediate, exit test immediately upon the first failed test.
391 # Usually takes two arguments:
392 # $1 - Test description
393 # $2 - Commands to be executed.
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.
404 # test_expect_success \
405 # 'git-write-tree should be able to write an empty tree.' \
406 # 'tree=$(git-write-tree)'
408 # # Test depending on one prerequisite.
409 # test_expect_success TTY 'git --paginate rev-list uses a pager' \
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" '
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"
421 if ! test_skip_
"$@"; then
422 say
>&3 "expecting success: $2"
423 if test_run_
"$2"; then
432 # Public: Run test commands and expect them to fail. Used to demonstrate a known
435 # This is NOT the opposite of test_expect_success, but rather used to mark a
436 # test that demonstrates a known breakage.
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.
442 # Failures from these tests won't cause --immediate to stop.
444 # Usually takes two arguments:
445 # $1 - Test description
446 # $2 - Commands to be executed.
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.
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"
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"
465 test_known_broken_failure_
"$1"
471 # Public: Run command and ensure that it fails in a controlled way.
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.
477 # This is one of the prefix functions to be used inside test_expect_success or
478 # test_expect_failure.
480 # $1.. - Command to be executed.
484 # test_expect_success 'complain and die' '
486 # do something else &&
487 # test_must_fail git checkout ../outerspace
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.
497 if test $exit_code = 0; then
498 echo >&2 "test_must_fail: command succeeded: $*"
500 elif test $exit_code -gt 129 -a $exit_code -le 192; then
501 echo >&2 "test_must_fail: died by signal: $*"
503 elif test $exit_code = 127; then
504 echo >&2 "test_must_fail: command not found: $*"
510 # Public: Run command and ensure that it succeeds or fails in a controlled way.
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.
515 # This is one of the prefix functions to be used inside test_expect_success or
516 # test_expect_failure.
518 # $1.. - Command to be executed.
522 # test_expect_success 'some command works without configuration' '
523 # test_might_fail git config --unset all.configuration &&
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.
533 if test $exit_code -gt 129 -a $exit_code -le 192; then
534 echo >&2 "test_might_fail: died by signal: $*"
536 elif test $exit_code = 127; then
537 echo >&2 "test_might_fail: command not found: $*"
543 # Public: Run command and ensure it exits with a given exit code.
545 # This is one of the prefix functions to be used inside test_expect_success or
546 # test_expect_failure.
548 # $1 - Expected exit code.
549 # $2.. - Command to be executed.
553 # test_expect_success 'Merge with d/f conflicts' '
554 # test_expect_code 1 git merge "merge msg" B master
557 # Returns 0 if the expected exit code is returned or 1 otherwise.
563 if test $exit_code = $want_code; then
567 echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
571 # Public: Compare two files to see if expected output matches actual output.
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.
577 # This is one of the prefix functions to be used inside test_expect_success or
578 # test_expect_failure.
580 # $1 - Path to file with expected output.
581 # $2 - Path to file with actual output.
585 # test_expect_success 'foo works' '
586 # echo expected >expected &&
588 # test_cmp expected actual
591 # Returns the exit code of the command set by TEST_CMP.
593 ${TEST_CMP:-diff -u} "$@"
596 # Public: portably print a sequence of numbers.
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.
601 # $1 - Starting number.
602 # $2 - Ending number.
606 # test_expect_success 'foo works 10 times' '
607 # for i in $(test_seq 1 10)
613 # Returns 0 if all the specified numbers can be displayed.
617 while test "$i" -le "$j"
624 # Public: Check if the file expected to be empty is indeed empty, and barfs
627 # $1 - File to check for emptyness.
629 # Returns 0 if file is empty, 1 otherwise.
630 test_must_be_empty
() {
633 echo "'$1' is not empty, it contains:"
639 # Public: Schedule cleanup commands to be run unconditionally at the end of a
642 # If some cleanup command fails, the test will not pass. With --immediate, no
643 # cleanup is done to help diagnose what went wrong.
645 # This is one of the prefix functions to be used inside test_expect_success or
646 # test_expect_failure.
648 # $1.. - Commands to prepend to the list of cleanup commands.
652 # test_expect_success 'test core.capslock' '
653 # git config core.capslock true &&
654 # test_when_finished "git config --unset core.capslock" &&
658 # Returns the exit code of the last cleanup command executed.
659 test_when_finished
() {
661 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
664 # Public: Schedule cleanup commands to be run unconditionally when all tests
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.
672 # cleanup mysql -e "DROP DATABASE mytest"
674 # Returns the exit code of the last cleanup command executed.
678 } && (exit \"\$eval_ret\"); eval_ret=\$?; $final_cleanup"
681 # Public: Summarize test results and exit with an appropriate error code.
683 # Must be called at the end of each test script.
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
691 # # Each test script must call test_done at the end.
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'
700 # Returns 0 if all tests passed or 1 if there was a failure.
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"
709 cat >>"$test_results_path" <<-EOF
711 success $test_success
719 if test "$test_fixed" != 0; then
720 say_color error
"# $test_fixed known breakage(s) vanished; please update test(s)"
722 if test "$test_broken" != 0; then
723 say_color warn
"# still have $test_broken known breakage(s)"
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)"
729 test_remaining
=$test_count
730 msg
="$test_count test(s)"
733 case "$test_failure" in
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"
739 [ -z "$skip_all" ] || skip_all
=" # SKIP $skip_all"
741 if test $test_remaining -gt 0; then
742 say_color pass
"# passed all $msg"
744 say
"1..$test_count$skip_all"
746 test_eval_
"$final_cleanup"
748 test -d "$remove_trash" &&
749 cd "$(dirname "$remove_trash")" &&
750 rm -rf "$(basename "$remove_trash")"
755 say_color error
"# failed $test_failure among $msg"
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
768 # Public: Source directory of test code and sharness library.
769 # This directory may be different from the directory in which tests are
771 : ${SHARNESS_TEST_SRCDIR:=$(cd $(dirname $0) && pwd)}
772 export SHARNESS_TEST_SRCDIR
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
780 # Public: Path to test script currently executed.
781 SHARNESS_TEST_FILE
="$0"
782 export SHARNESS_TEST_FILE
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" ;;
791 test "$debug" = "t" || remove_trash
="$SHARNESS_TRASH_DIRECTORY"
792 rm -rf "$SHARNESS_TRASH_DIRECTORY" ||
{
794 echo >&5 "FATAL: Cannot prepare test area"
800 # Load any extensions in $srcdir/sharness.d/*.sh
802 if test -d "${SHARNESS_TEST_SRCDIR}/sharness.d"
804 for file in "${SHARNESS_TEST_SRCDIR}"/sharness.d
/*.sh
806 # Ensure glob was not an empty match:
807 test -e "${file}" ||
break
811 echo >&5 "sharness: loading extensions from ${file}"
816 echo >&5 "sharness: Error loading ${file}. Aborting."
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
826 HOME
="$SHARNESS_TRASH_DIRECTORY"
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
834 this_test
=${SHARNESS_TEST_FILE##*/}
835 this_test
=${this_test%.$SHARNESS_TEST_EXTENSION}
836 for skp
in $SKIP_TESTS; do
839 say_color info
>&3 "skipping test $this_test altogether"
840 skip_all
="skip all tests in $this_test"
845 test -n "$TEST_LONG" && test_set_prereq EXPENSIVE
846 test -n "$TEST_INTERACTIVE" && test_set_prereq INTERACTIVE
848 # Make sure this script ends with code 0
851 # vi: set ts=4 sw=4 noet :