summaryrefslogtreecommitdiff
path: root/t/test-lib.sh
diff options
context:
space:
mode:
Diffstat (limited to 't/test-lib.sh')
-rw-r--r--t/test-lib.sh356
1 files changed, 226 insertions, 130 deletions
diff --git a/t/test-lib.sh b/t/test-lib.sh
index c5e914a..79e8a33 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+# Test framework for git. See t/README for usage.
#
# Copyright (c) 2005 Junio C Hamano
#
@@ -26,6 +26,10 @@ then
# outside of t/, e.g. for running tests on the test library
# itself.
TEST_DIRECTORY=$(pwd)
+else
+ # ensure that TEST_DIRECTORY is an absolute path so that it
+ # is valid even if the current working directory is changed
+ TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
fi
if test -z "$TEST_OUTPUT_DIRECTORY"
then
@@ -87,6 +91,7 @@ unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
VALGRIND
UNZIP
PERF_
+ CURL_VERBOSE
));
my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
print join("\n", @vars);
@@ -104,6 +109,16 @@ export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
export EDITOR
+# Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
+GIT_TRACE_BARE=1
+export GIT_TRACE_BARE
+
+if test -n "${TEST_GIT_INDEX_VERSION:+isset}"
+then
+ GIT_INDEX_VERSION="$TEST_GIT_INDEX_VERSION"
+ export GIT_INDEX_VERSION
+fi
+
# Add libc MALLOC and MALLOC_PERTURB test
# only if we are not executing the test with valgrind
if expr " $GIT_TEST_OPTS " : ".* --valgrind " >/dev/null ||
@@ -154,7 +169,11 @@ _z40=0000000000000000000000000000000000000000
LF='
'
-export _x05 _x40 _z40 LF
+# UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
+# when case-folding filenames
+u200c=$(printf '\342\200\214')
+
+export _x05 _x40 _z40 LF u200c
# Each test should start with something like this, after copyright notices:
#
@@ -181,6 +200,14 @@ do
immediate=t; shift ;;
-l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
+ -r)
+ shift; test "$#" -ne 0 || {
+ echo 'error: -r requires an argument' >&2;
+ exit 1;
+ }
+ run_list=$1; shift ;;
+ --run=*)
+ run_list=$(expr "z$1" : 'z[^=]*=\(.*\)'); shift ;;
-h|--h|--he|--hel|--help)
help=t; shift ;;
-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
@@ -205,29 +232,21 @@ do
--valgrind-only=*)
valgrind_only=$(expr "z$1" : 'z[^=]*=\(.*\)')
shift ;;
- --valgrind-parallel=*)
- valgrind_parallel=$(expr "z$1" : 'z[^=]*=\(.*\)')
- shift ;;
- --valgrind-only-stride=*)
- valgrind_only_stride=$(expr "z$1" : 'z[^=]*=\(.*\)')
- shift ;;
- --valgrind-only-offset=*)
- valgrind_only_offset=$(expr "z$1" : 'z[^=]*=\(.*\)')
- shift ;;
--tee)
shift ;; # was handled already
--root=*)
root=$(expr "z$1" : 'z[^=]*=\(.*\)')
shift ;;
- --statusprefix=*)
- statusprefix=$(expr "z$1" : 'z[^=]*=\(.*\)')
+ -x)
+ trace=t
+ verbose=t
shift ;;
*)
echo "error: unknown test option '$1'" >&2; exit 1 ;;
esac
done
-if test -n "$valgrind_only" || test -n "$valgrind_only_stride"
+if test -n "$valgrind_only"
then
test -z "$valgrind" && valgrind=memcheck
test -z "$verbose" && verbose_only="$valgrind_only"
@@ -285,7 +304,7 @@ error "Test script did not set test_description."
if test "$help" = "t"
then
- echo "$test_description"
+ printf '%s\n' "$test_description"
exit 0
fi
@@ -329,25 +348,25 @@ trap 'die' EXIT
test_ok_ () {
test_success=$(($test_success + 1))
- say_color "" "${statusprefix}ok $test_count - $@"
+ say_color "" "ok $test_count - $@"
}
test_failure_ () {
test_failure=$(($test_failure + 1))
- say_color error "${statusprefix}not ok $test_count - $1"
+ say_color error "not ok $test_count - $1"
shift
- echo "$@" | sed -e 's/^/# /'
+ printf '%s\n' "$*" | sed -e 's/^/# /'
test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
}
test_known_broken_ok_ () {
test_fixed=$(($test_fixed+1))
- say_color error "${statusprefix}ok $test_count - $@ # TODO known breakage vanished"
+ say_color error "ok $test_count - $@ # TODO known breakage vanished"
}
test_known_broken_failure_ () {
test_broken=$(($test_broken+1))
- say_color warn "${statusprefix}not ok $test_count - $@ # TODO known breakage"
+ say_color warn "not ok $test_count - $@ # TODO known breakage"
}
test_debug () {
@@ -368,6 +387,99 @@ match_pattern_list () {
return 1
}
+match_test_selector_list () {
+ title="$1"
+ shift
+ arg="$1"
+ shift
+ test -z "$1" && return 0
+
+ # Both commas and whitespace are accepted as separators.
+ OLDIFS=$IFS
+ IFS=' ,'
+ set -- $1
+ IFS=$OLDIFS
+
+ # If the first selector is negative we include by default.
+ include=
+ case "$1" in
+ !*) include=t ;;
+ esac
+
+ for selector
+ do
+ orig_selector=$selector
+
+ positive=t
+ case "$selector" in
+ !*)
+ positive=
+ selector=${selector##?}
+ ;;
+ esac
+
+ test -z "$selector" && continue
+
+ case "$selector" in
+ *-*)
+ if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
+ then
+ echo "error: $title: invalid non-numeric in range" \
+ "start: '$orig_selector'" >&2
+ exit 1
+ fi
+ if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
+ then
+ echo "error: $title: invalid non-numeric in range" \
+ "end: '$orig_selector'" >&2
+ exit 1
+ fi
+ ;;
+ *)
+ if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
+ then
+ echo "error: $title: invalid non-numeric in test" \
+ "selector: '$orig_selector'" >&2
+ exit 1
+ fi
+ esac
+
+ # Short cut for "obvious" cases
+ test -z "$include" && test -z "$positive" && continue
+ test -n "$include" && test -n "$positive" && continue
+
+ case "$selector" in
+ -*)
+ if test $arg -le ${selector#-}
+ then
+ include=$positive
+ fi
+ ;;
+ *-)
+ if test $arg -ge ${selector%-}
+ then
+ include=$positive
+ fi
+ ;;
+ *-*)
+ if test ${selector%%-*} -le $arg \
+ && test $arg -le ${selector#*-}
+ then
+ include=$positive
+ fi
+ ;;
+ *)
+ if test $arg -eq $selector
+ then
+ include=$positive
+ fi
+ ;;
+ esac
+ done
+
+ test -n "$include"
+}
+
maybe_teardown_verbose () {
test -z "$verbose_only" && return
exec 4>/dev/null 3>/dev/null
@@ -377,9 +489,7 @@ maybe_teardown_verbose () {
last_verbose=t
maybe_setup_verbose () {
test -z "$verbose_only" && return
- if match_pattern_list $test_count $verbose_only ||
- { test -n "$valgrind_only_stride" &&
- expr $test_count "%" $valgrind_only_stride - $valgrind_only_offset = 0 >/dev/null; }
+ if match_pattern_list $test_count $verbose_only
then
exec 4>&2 3>&1
# Emit a delimiting blank line when going from
@@ -403,7 +513,7 @@ maybe_teardown_valgrind () {
maybe_setup_valgrind () {
test -z "$GIT_VALGRIND" && return
- if test -z "$valgrind_only" && test -z "$valgrind_only_stride"
+ if test -z "$valgrind_only"
then
GIT_VALGRIND_ENABLED=t
return
@@ -412,17 +522,42 @@ maybe_setup_valgrind () {
if match_pattern_list $test_count $valgrind_only
then
GIT_VALGRIND_ENABLED=t
- elif test -n "$valgrind_only_stride" &&
- expr $test_count "%" $valgrind_only_stride - $valgrind_only_offset = 0 >/dev/null
- then
- GIT_VALGRIND_ENABLED=t
fi
}
+# This is a separate function because some tests use
+# "return" to end a test_expect_success block early
+# (and we want to make sure we run any cleanup like
+# "set +x").
+test_eval_inner_ () {
+ # Do not add anything extra (including LF) after '$*'
+ eval "
+ test \"$trace\" = t && set -x
+ $*"
+}
+
test_eval_ () {
- # This is a separate function because some tests use
- # "return" to end a test_expect_success block early.
- eval </dev/null >&3 2>&4 "$*"
+ # We run this block with stderr redirected to avoid extra cruft
+ # during a "-x" trace. Once in "set -x" mode, we cannot prevent
+ # the shell from printing the "set +x" to turn it off (nor the saving
+ # of $? before that). But we can make sure that the output goes to
+ # /dev/null.
+ #
+ # The test itself is run with stderr put back to &4 (so either to
+ # /dev/null, or to the original stderr if --verbose was used).
+ {
+ test_eval_inner_ "$@" </dev/null >&3 2>&4
+ test_eval_ret_=$?
+ if test "$trace" = t
+ then
+ set +x
+ if test "$test_eval_ret_" != 0
+ then
+ say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
+ fi
+ fi
+ } 2>/dev/null
+ return $test_eval_ret_
}
test_run_ () {
@@ -433,7 +568,8 @@ test_run_ () {
eval_ret=$?
teardown_malloc_check
- if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
+ if test -z "$immediate" || test $eval_ret = 0 ||
+ test -n "$expecting_failure" && test "$test_cleanup" != ":"
then
setup_malloc_check
test_eval_ "$test_cleanup"
@@ -460,25 +596,35 @@ test_finish_ () {
test_skip () {
to_skip=
+ skipped_reason=
if match_pattern_list $this_test.$test_count $GIT_SKIP_TESTS
then
to_skip=t
+ skipped_reason="GIT_SKIP_TESTS"
fi
if test -z "$to_skip" && test -n "$test_prereq" &&
! test_have_prereq "$test_prereq"
then
to_skip=t
- fi
- case "$to_skip" in
- t)
+
of_prereq=
if test "$missing_prereq" != "$test_prereq"
then
of_prereq=" of $test_prereq"
fi
+ skipped_reason="missing $missing_prereq${of_prereq}"
+ fi
+ if test -z "$to_skip" && test -n "$run_list" &&
+ ! match_test_selector_list '--run' $test_count "$run_list"
+ then
+ to_skip=t
+ skipped_reason="--run"
+ fi
- say_color skip >&3 "${statusprefix}skipping test: $@"
- say_color skip "${statusprefix}ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
+ case "$to_skip" in
+ t)
+ say_color skip >&3 "skipping test: $@"
+ say_color skip "ok $test_count # skip $1 ($skipped_reason)"
: true
;;
*)
@@ -495,8 +641,6 @@ test_at_end_hook_ () {
test_done () {
GIT_EXIT_OK=t
- # Note: t0000 relies on $HARNESS_ACTIVE disabling the .counts
- # output file
if test -z "$HARNESS_ACTIVE"
then
test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
@@ -516,11 +660,11 @@ test_done () {
if test "$test_fixed" != 0
then
- say_color error "${statusprefix}# $test_fixed known breakage(s) vanished; please update test(s)"
+ say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
fi
if test "$test_broken" != 0
then
- say_color warn "${statusprefix}# still have $test_broken known breakage(s)"
+ say_color warn "# still have $test_broken known breakage(s)"
fi
if test "$test_broken" != 0 || test "$test_fixed" != 0
then
@@ -543,9 +687,9 @@ test_done () {
then
if test $test_remaining -gt 0
then
- say_color pass "${statusprefix}# passed all $msg"
+ say_color pass "# passed all $msg"
fi
- say "${statusprefix}1..$test_count$skip_all"
+ say "1..$test_count$skip_all"
fi
test -d "$remove_trash" &&
@@ -559,8 +703,8 @@ test_done () {
*)
if test $test_external_has_tap -eq 0
then
- say_color error "${statusprefix}# failed $test_failure among $msg"
- say "${statusprefix}1..$test_count"
+ say_color error "# failed $test_failure among $msg"
+ say "1..$test_count"
fi
exit 1 ;;
@@ -568,9 +712,6 @@ test_done () {
esac
}
-
-# Set up a directory that we can put in PATH which redirects all git
-# calls to 'valgrind git ...'.
if test -n "$valgrind"
then
make_symlink () {
@@ -594,11 +735,9 @@ then
make_valgrind_symlink () {
# handle only executables, unless they are shell libraries that
- # need to be in the exec-path. We will just use "#!" as a
- # guess for a shell-script, since we have no idea what the user
- # may have configured as the shell path.
+ # need to be in the exec-path.
test -x "$1" ||
- test "#!" = "$(head -c 2 <"$1")" ||
+ test "# " = "$(head -c 2 <"$1")" ||
return;
base=$(basename "$1")
@@ -618,42 +757,33 @@ then
make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
}
- # In the case of --valgrind-parallel, we only need to do the
- # wrapping once, in the main script. The worker children all
- # have $valgrind_only_stride set, so we can skip based on that.
- if test -z "$valgrind_only_stride"
- then
- # override all git executables in TEST_DIRECTORY/..
- GIT_VALGRIND=$TEST_DIRECTORY/valgrind
- mkdir -p "$GIT_VALGRIND"/bin
- for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/test-*
- do
- make_valgrind_symlink $file
- done
- # special-case the mergetools loadables
- make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
- OLDIFS=$IFS
- IFS=:
- for path in $PATH
+ # override all git executables in TEST_DIRECTORY/..
+ GIT_VALGRIND=$TEST_DIRECTORY/valgrind
+ mkdir -p "$GIT_VALGRIND"/bin
+ for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/test-*
+ do
+ make_valgrind_symlink $file
+ done
+ # special-case the mergetools loadables
+ make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
+ OLDIFS=$IFS
+ IFS=:
+ for path in $PATH
+ do
+ ls "$path"/git-* 2> /dev/null |
+ while read file
do
- ls "$path"/git-* 2> /dev/null |
- while read file
- do
- make_valgrind_symlink "$file"
- done
+ make_valgrind_symlink "$file"
done
- IFS=$OLDIFS
- fi
+ done
+ IFS=$OLDIFS
PATH=$GIT_VALGRIND/bin:$PATH
GIT_EXEC_PATH=$GIT_VALGRIND/bin
export GIT_VALGRIND
GIT_VALGRIND_MODE="$valgrind"
export GIT_VALGRIND_MODE
GIT_VALGRIND_ENABLED=t
- if test -n "$valgrind_only" || test -n "$valgrind_only_stride"
- then
- GIT_VALGRIND_ENABLED=
- fi
+ test -n "$valgrind_only" && GIT_VALGRIND_ENABLED=
export GIT_VALGRIND_ENABLED
elif test -n "$GIT_TEST_INSTALLED"
then
@@ -679,7 +809,6 @@ else # normal case, use ../bin-wrappers only unless $with_dashes:
fi
fi
GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
-unset GIT_CONFIG
GIT_CONFIG_NOSYSTEM=1
GIT_ATTR_NOSYSTEM=1
export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
@@ -700,15 +829,6 @@ test -d "$GIT_BUILD_DIR"/templates/blt || {
error "You haven't built things yet, have you?"
}
-if test -z "$GIT_TEST_INSTALLED" && test -z "$NO_PYTHON"
-then
- GITPYTHONLIB="$GIT_BUILD_DIR/git_remote_helpers/build/lib"
- export GITPYTHONLIB
- test -d "$GIT_BUILD_DIR"/git_remote_helpers/build || {
- error "You haven't built git_remote_helpers yet, have you?"
- }
-fi
-
if ! test -x "$GIT_BUILD_DIR"/test-chmtime
then
echo >&2 'You need to build test-chmtime:'
@@ -731,7 +851,8 @@ rm -fr "$TRASH_DIRECTORY" || {
}
HOME="$TRASH_DIRECTORY"
-export HOME
+GNUPGHOME="$HOME/gnupg-home-not-used"
+export HOME GNUPGHOME
if test -z "$TEST_NO_CREATE_REPO"
then
@@ -739,41 +860,6 @@ then
else
mkdir -p "$TRASH_DIRECTORY"
fi
-
-# Gross hack to spawn N sub-instances of the tests in parallel, and
-# summarize the results. Note that if this is enabled, the script
-# terminates at the end of this 'if' block.
-if test -n "$valgrind_parallel"
-then
- for i in $(test_seq 1 $valgrind_parallel)
- do
- root="$TRASH_DIRECTORY/vgparallel-$i"
- mkdir "$root"
- TEST_OUTPUT_DIRECTORY="$root" \
- ${SHELL_PATH} "$0" \
- --root="$root" --statusprefix="[$i] " \
- --valgrind="$valgrind" \
- --valgrind-only-stride="$valgrind_parallel" \
- --valgrind-only-offset="$i" &
- pids="$pids $!"
- done
- trap "kill $pids" INT TERM HUP
- wait $pids
- trap - INT TERM HUP
- for i in $(test_seq 1 $valgrind_parallel)
- do
- root="$TRASH_DIRECTORY/vgparallel-$i"
- eval "$(cat "$root/test-results/$(basename "$0" .sh)"-*.counts |
- sed 's/^\([a-z][a-z]*\) \([0-9][0-9]*\)/inner_\1=\2/')"
- test_count=$(expr $test_count + $inner_total)
- test_success=$(expr $test_success + $inner_success)
- test_fixed=$(expr $test_fixed + $inner_fixed)
- test_broken=$(expr $test_broken + $inner_broken)
- test_failure=$(expr $test_failure + $inner_failed)
- done
- test_done
-fi
-
# Use -P to resolve symlinks in our working directory so that the cwd
# in subprocesses like git equals our $PWD (for pathname comparisons).
cd -P "$TRASH_DIRECTORY" || exit 1
@@ -823,14 +909,14 @@ case $(uname -s) in
# backslashes in pathspec are converted to '/'
# exec does not inherit the PID
test_set_prereq MINGW
- test_set_prereq NOT_CYGWIN
+ test_set_prereq NATIVE_CRLF
test_set_prereq SED_STRIPS_CR
test_set_prereq GREP_STRIPS_CR
+ GIT_TEST_CMP=mingw_test_cmp
;;
*CYGWIN*)
test_set_prereq POSIXPERM
test_set_prereq EXECKEEPSPID
- test_set_prereq NOT_MINGW
test_set_prereq CYGWIN
test_set_prereq SED_STRIPS_CR
test_set_prereq GREP_STRIPS_CR
@@ -839,8 +925,6 @@ case $(uname -s) in
test_set_prereq POSIXPERM
test_set_prereq BSLASHPSPEC
test_set_prereq EXECKEEPSPID
- test_set_prereq NOT_MINGW
- test_set_prereq NOT_CYGWIN
;;
esac
@@ -903,6 +987,10 @@ test_lazy_prereq SYMLINKS '
ln -s x y && test -h y
'
+test_lazy_prereq FILEMODE '
+ test "$(git config --bool core.filemode)" = true
+'
+
test_lazy_prereq CASE_INSENSITIVE_FS '
echo good >CamelCase &&
echo bad >camelcase &&
@@ -928,6 +1016,14 @@ test_lazy_prereq AUTOIDENT '
git var GIT_AUTHOR_IDENT
'
+test_lazy_prereq EXPENSIVE '
+ test -n "$GIT_TEST_LONG"
+'
+
+test_lazy_prereq USR_BIN_TIME '
+ test -x /usr/bin/time
+'
+
# When the tests are run as root, permission tests will report that
# things are writable when they shouldn't be.
test -w / || test_set_prereq SANITY