submodule init: redirect stdout to stderr
[gitweb.git] / t / test-lib-functions.sh
index 6dffb8bcde83b82fa40b05dd8ee815f4e6a3cdd3..8d99eb303fd62a1c179ab31f471a3376898586b0 100644 (file)
@@ -145,6 +145,14 @@ test_pause () {
        fi
 }
 
+# Wrap git in gdb. Adding this to a command can make it easier to
+# understand what is going on in a failing test.
+#
+# Example: "debug git checkout master".
+debug () {
+        GIT_TEST_GDB=1 "$@"
+}
+
 # Call test_commit with the arguments "<message> [<file> [<contents> [<tag>]]]"
 #
 # This will commit a file with the given contents and the given commit
@@ -561,6 +569,21 @@ test_line_count () {
        fi
 }
 
+# Returns success if a comma separated string of keywords ($1) contains a
+# given keyword ($2).
+# Examples:
+# `list_contains "foo,bar" bar` returns 0
+# `list_contains "foo" bar` returns 1
+
+list_contains () {
+       case ",$1," in
+       *,$2,*)
+               return 0
+               ;;
+       esac
+       return 1
+}
+
 # This is not among top-level (test_expect_success | test_expect_failure)
 # but is a prefix that can be used in the test script, like:
 #
@@ -574,18 +597,34 @@ test_line_count () {
 # the failure could be due to a segv.  We want a controlled failure.
 
 test_must_fail () {
+       case "$1" in
+       ok=*)
+               _test_ok=${1#ok=}
+               shift
+               ;;
+       *)
+               _test_ok=
+               ;;
+       esac
        "$@"
        exit_code=$?
-       if test $exit_code = 0; then
+       if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
+       then
                echo >&2 "test_must_fail: command succeeded: $*"
                return 1
-       elif test $exit_code -gt 129 && test $exit_code -le 192; then
-               echo >&2 "test_must_fail: died by signal: $*"
+       elif test $exit_code -eq 141 && list_contains "$_test_ok" sigpipe
+       then
+               return 0
+       elif test $exit_code -gt 129 && test $exit_code -le 192
+       then
+               echo >&2 "test_must_fail: died by signal $(($exit_code - 128)): $*"
                return 1
-       elif test $exit_code = 127; then
+       elif test $exit_code -eq 127
+       then
                echo >&2 "test_must_fail: command not found: $*"
                return 1
-       elif test $exit_code = 126; then
+       elif test $exit_code -eq 126
+       then
                echo >&2 "test_must_fail: valgrind error: $*"
                return 1
        fi
@@ -604,16 +643,7 @@ test_must_fail () {
 # because we want to notice if it fails due to segv.
 
 test_might_fail () {
-       "$@"
-       exit_code=$?
-       if test $exit_code -gt 129 && test $exit_code -le 192; then
-               echo >&2 "test_might_fail: died by signal: $*"
-               return 1
-       elif test $exit_code = 127; then
-               echo >&2 "test_might_fail: command not found: $*"
-               return 1
-       fi
-       return 0
+       test_must_fail ok=success "$@"
 }
 
 # Similar to test_must_fail and test_might_fail, but check that a