summaryrefslogtreecommitdiff
path: root/t/test-lib-functions.sh
diff options
context:
space:
mode:
Diffstat (limited to 't/test-lib-functions.sh')
-rw-r--r--t/test-lib-functions.sh112
1 files changed, 106 insertions, 6 deletions
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 3103be8a32..8d59b90348 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -798,6 +798,37 @@ list_contains () {
return 1
}
+# Returns success if the arguments indicate that a command should be
+# accepted by test_must_fail(). If the command is run with env, the env
+# and its corresponding variable settings will be stripped before we
+# test the command being run.
+test_must_fail_acceptable () {
+ if test "$1" = "env"
+ then
+ shift
+ while test $# -gt 0
+ do
+ case "$1" in
+ *?=*)
+ shift
+ ;;
+ *)
+ break
+ ;;
+ esac
+ done
+ fi
+
+ case "$1" in
+ git|__git*|test-tool|test_terminal)
+ return 0
+ ;;
+ *)
+ return 1
+ ;;
+ esac
+}
+
# 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:
#
@@ -817,6 +848,17 @@ list_contains () {
# Multiple signals can be specified as a comma separated list.
# Currently recognized signal names are: sigpipe, success.
# (Don't use 'success', use 'test_might_fail' instead.)
+#
+# Do not use this to run anything but "git" and other specific testable
+# commands (see test_must_fail_acceptable()). We are not in the
+# business of vetting system supplied commands -- in other words, this
+# is wrong:
+#
+# test_must_fail grep pattern output
+#
+# Instead use '!':
+#
+# ! grep pattern output
test_must_fail () {
case "$1" in
@@ -828,6 +870,11 @@ test_must_fail () {
_test_ok=
;;
esac
+ if ! test_must_fail_acceptable "$@"
+ then
+ echo >&7 "test_must_fail: only 'git' is allowed: $*"
+ return 1
+ fi
"$@" 2>&7
exit_code=$?
if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
@@ -905,7 +952,13 @@ test_expect_code () {
# - not all diff versions understand "-u"
test_cmp() {
- eval "$GIT_TEST_CMP" '"$@"'
+ test $# -eq 2 || BUG "test_cmp requires two arguments"
+ if ! eval "$GIT_TEST_CMP" '"$@"'
+ then
+ test "x$1" = x- || test -e "$1" || BUG "test_cmp '$1' missing"
+ test "x$2" = x- || test -e "$2" || BUG "test_cmp '$2' missing"
+ return 1
+ fi
}
# Check that the given config key has the expected value.
@@ -934,7 +987,13 @@ test_cmp_config() {
# test_cmp_bin - helper to compare binary files
test_cmp_bin() {
- cmp "$@"
+ test $# -eq 2 || BUG "test_cmp_bin requires two arguments"
+ if ! cmp "$@"
+ then
+ test "x$1" = x- || test -e "$1" || BUG "test_cmp_bin '$1' missing"
+ test "x$2" = x- || test -e "$2" || BUG "test_cmp_bin '$2' missing"
+ return 1
+ fi
}
# Use this instead of test_cmp to compare files that contain expected and
@@ -1417,9 +1476,7 @@ test_set_hash () {
# Detect the hash algorithm in use.
test_detect_hash () {
- # Currently we only support SHA-1, but in the future this function will
- # actually detect the algorithm in use.
- test_hash_algo='sha1'
+ test_hash_algo="${GIT_TEST_DEFAULT_HASH:-sha1}"
}
# Load common hash metadata and common placeholder object IDs for use with
@@ -1468,7 +1525,17 @@ test_oid_cache () {
# Look up a per-hash value based on a key ($1). The value must have been loaded
# by test_oid_init or test_oid_cache.
test_oid () {
- local var="test_oid_${test_hash_algo}_$1" &&
+ local algo="${test_hash_algo}" &&
+
+ case "$1" in
+ --hash=*)
+ algo="${1#--hash=}" &&
+ shift;;
+ *)
+ ;;
+ esac &&
+
+ local var="test_oid_${algo}_$1" &&
# If the variable is unset, we must be missing an entry for this
# key-hash pair, so exit with an error.
@@ -1561,3 +1628,36 @@ test_path_is_hidden () {
case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
return 1
}
+
+# Check that the given command was invoked as part of the
+# trace2-format trace on stdin.
+#
+# test_subcommand [!] <command> <args>... < <trace>
+#
+# For example, to look for an invocation of "git upload-pack
+# /path/to/repo"
+#
+# GIT_TRACE2_EVENT=event.log git fetch ... &&
+# test_subcommand git upload-pack "$PATH" <event.log
+#
+# If the first parameter passed is !, this instead checks that
+# the given command was not called.
+#
+test_subcommand () {
+ local negate=
+ if test "$1" = "!"
+ then
+ negate=t
+ shift
+ fi
+
+ local expr=$(printf '"%s",' "$@")
+ expr="${expr%,}"
+
+ if test -n "$negate"
+ then
+ ! grep "\[$expr\]"
+ else
+ grep "\[$expr\]"
+ fi
+}