test-lib-functions.sh [plain text]
test_set_editor () {
FAKE_EDITOR="$1"
export FAKE_EDITOR
EDITOR='"$FAKE_EDITOR"'
export EDITOR
}
test_set_index_version () {
GIT_INDEX_VERSION="$1"
export GIT_INDEX_VERSION
}
test_decode_color () {
awk '
function name(n) {
if (n == 0) return "RESET";
if (n == 1) return "BOLD";
if (n == 2) return "FAINT";
if (n == 3) return "ITALIC";
if (n == 7) return "REVERSE";
if (n == 30) return "BLACK";
if (n == 31) return "RED";
if (n == 32) return "GREEN";
if (n == 33) return "YELLOW";
if (n == 34) return "BLUE";
if (n == 35) return "MAGENTA";
if (n == 36) return "CYAN";
if (n == 37) return "WHITE";
if (n == 40) return "BLACK";
if (n == 41) return "BRED";
if (n == 42) return "BGREEN";
if (n == 43) return "BYELLOW";
if (n == 44) return "BBLUE";
if (n == 45) return "BMAGENTA";
if (n == 46) return "BCYAN";
if (n == 47) return "BWHITE";
}
{
while (match($0, /\033\[[0-9;]*m/) != 0) {
printf "%s<", substr($0, 1, RSTART-1);
codes = substr($0, RSTART+2, RLENGTH-3);
if (length(codes) == 0)
printf "%s", name(0)
else {
n = split(codes, ary, ";");
sep = "";
for (i = 1; i <= n; i++) {
printf "%s%s", sep, name(ary[i]);
sep = ";"
}
}
printf ">";
$0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
}
print
}
'
}
lf_to_nul () {
perl -pe 'y/\012/\000/'
}
nul_to_q () {
perl -pe 'y/\000/Q/'
}
q_to_nul () {
perl -pe 'y/Q/\000/'
}
q_to_cr () {
tr Q '\015'
}
q_to_tab () {
tr Q '\011'
}
qz_to_tab_space () {
tr QZ '\011\040'
}
append_cr () {
sed -e 's/$/Q/' | tr Q '\015'
}
remove_cr () {
tr '\015' Q | sed -e 's/Q$//'
}
sane_unset () {
unset "$@"
return 0
}
test_tick () {
if test -z "${test_tick+set}"
then
test_tick=1112911993
else
test_tick=$(($test_tick + 60))
fi
GIT_COMMITTER_DATE="$test_tick -0700"
GIT_AUTHOR_DATE="$test_tick -0700"
export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
}
test_pause () {
"$SHELL_PATH" <&6 >&5 2>&7
}
debug () {
case "$1" in
-d)
GIT_DEBUGGER="$2" &&
shift 2
;;
--debugger=*)
GIT_DEBUGGER="${1#*=}" &&
shift 1
;;
*)
GIT_DEBUGGER=1
;;
esac &&
GIT_DEBUGGER="${GIT_DEBUGGER}" "$@" <&6 >&5 2>&7
}
test_commit () {
notick= &&
signoff= &&
indir= &&
while test $ do
case "$1" in
--notick)
notick=yes
;;
--signoff)
signoff="$1"
;;
-C)
indir="$2"
shift
;;
*)
break
;;
esac
shift
done &&
indir=${indir:+"$indir"/} &&
file=${2:-"$1.t"} &&
echo "${3-$1}" > "$indir$file" &&
git ${indir:+ -C "$indir"} add "$file" &&
if test -z "$notick"
then
test_tick
fi &&
git ${indir:+ -C "$indir"} commit $signoff -m "$1" &&
git ${indir:+ -C "$indir"} tag "${4:-$1}"
}
test_merge () {
test_tick &&
git merge -m "$1" "$2" &&
git tag "$1"
}
test_chmod () {
chmod "$@" &&
git update-index --add "--chmod=$@"
}
test_modebits () {
ls -l "$1" | sed -e 's|^\(..........\).*|\1|'
}
test_unconfig () {
config_dir=
if test "$1" = -C
then
shift
config_dir=$1
shift
fi
git ${config_dir:+-C "$config_dir"} config --unset-all "$@"
config_status=$?
case "$config_status" in
5) config_status=0
;;
esac
return $config_status
}
test_config () {
config_dir=
if test "$1" = -C
then
shift
config_dir=$1
shift
fi
test_when_finished "test_unconfig ${config_dir:+-C '$config_dir'} '$1'" &&
git ${config_dir:+-C "$config_dir"} config "$@"
}
test_config_global () {
test_when_finished "test_unconfig --global '$1'" &&
git config --global "$@"
}
write_script () {
{
echo "#!${2-"$SHELL_PATH"}" &&
cat
} >"$1" &&
chmod +x "$1"
}
test_unset_prereq () {
! test_have_prereq "$1" ||
satisfied_prereq="${satisfied_prereq% $1 *} ${satisfied_prereq#* $1 }"
}
test_set_prereq () {
case "$1" in
!*)
test_unset_prereq "${1#!}"
;;
*)
satisfied_prereq="$satisfied_prereq$1 "
;;
esac
}
satisfied_prereq=" "
lazily_testable_prereq= lazily_tested_prereq=
test_lazy_prereq () {
lazily_testable_prereq="$lazily_testable_prereq$1 "
eval test_prereq_lazily_$1=\$2
}
test_run_lazy_prereq_ () {
script='
mkdir -p "$TRASH_DIRECTORY/prereq-test-dir" &&
(
cd "$TRASH_DIRECTORY/prereq-test-dir" &&'"$2"'
)'
say >&3 "checking prerequisite: $1"
say >&3 "$script"
test_eval_ "$script"
eval_ret=$?
rm -rf "$TRASH_DIRECTORY/prereq-test-dir"
if test "$eval_ret" = 0; then
say >&3 "prerequisite $1 ok"
else
say >&3 "prerequisite $1 not satisfied"
fi
return $eval_ret
}
test_have_prereq () {
save_IFS=$IFS
IFS=,
set -- $*
IFS=$save_IFS
total_prereq=0
ok_prereq=0
missing_prereq=
for prerequisite
do
case "$prerequisite" in
!*)
negative_prereq=t
prerequisite=${prerequisite ;;
*)
negative_prereq=
esac
case " $lazily_tested_prereq " in
*" $prerequisite "*)
;;
*)
case " $lazily_testable_prereq " in
*" $prerequisite "*)
eval "script=\$test_prereq_lazily_$prerequisite" &&
if test_run_lazy_prereq_ "$prerequisite" "$script"
then
test_set_prereq $prerequisite
fi
lazily_tested_prereq="$lazily_tested_prereq$prerequisite "
esac
;;
esac
total_prereq=$(($total_prereq + 1))
case "$satisfied_prereq" in
*" $prerequisite "*)
satisfied_this_prereq=t
;;
*)
satisfied_this_prereq=
esac
case "$satisfied_this_prereq,$negative_prereq" in
t,|,t)
ok_prereq=$(($ok_prereq + 1))
;;
*)
prerequisite=${negative_prereq:+!}$prerequisite
if test -z "$missing_prereq"
then
missing_prereq=$prerequisite
else
missing_prereq="$prerequisite,$missing_prereq"
fi
esac
done
test $total_prereq = $ok_prereq
}
test_declared_prereq () {
case ",$test_prereq," in
*,$1,*)
return 0
;;
esac
return 1
}
test_verify_prereq () {
test -z "$test_prereq" ||
expr >/dev/null "$test_prereq" : '[A-Z0-9_,!]*$' ||
BUG "'$test_prereq' does not look like a prereq"
}
test_expect_failure () {
test_start_
test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
test "$#" = 2 ||
BUG "not 2 or 3 parameters to test-expect-failure"
test_verify_prereq
export test_prereq
if ! test_skip "$@"
then
say >&3 "checking known breakage: $2"
if test_run_ "$2" expecting_failure
then
test_known_broken_ok_ "$1"
else
test_known_broken_failure_ "$1"
fi
fi
test_finish_
}
test_expect_success () {
test_start_
test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
test "$#" = 2 ||
BUG "not 2 or 3 parameters to test-expect-success"
test_verify_prereq
export test_prereq
if ! test_skip "$@"
then
say >&3 "expecting success: $2"
if test_run_ "$2"
then
test_ok_ "$1"
else
test_failure_ "$@"
fi
fi
test_finish_
}
test_external () {
test "$#" = 4 && { test_prereq=$1; shift; } || test_prereq=
test "$#" = 3 ||
BUG "not 3 or 4 parameters to test_external"
descr="$1"
shift
test_verify_prereq
export test_prereq
if ! test_skip "$descr" "$@"
then
say_color "" "# run $test_count: $descr ($*)"
export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG
"$@" 2>&4
if test "$?" = 0
then
if test $test_external_has_tap -eq 0; then
test_ok_ "$descr"
else
say_color "" "# test_external test $descr was ok"
test_success=$(($test_success + 1))
fi
else
if test $test_external_has_tap -eq 0; then
test_failure_ "$descr" "$@"
else
say_color error "# test_external test $descr failed: $@"
test_failure=$(($test_failure + 1))
fi
fi
fi
}
test_external_without_stderr () {
tmp=${TMPDIR:-/tmp}
stderr="$tmp/git-external-stderr.$$.tmp"
test_external "$@" 4> "$stderr"
test -f "$stderr" || error "Internal error: $stderr disappeared."
descr="no stderr: $1"
shift
say >&3 "# expecting no stderr from previous command"
if test ! -s "$stderr"
then
rm "$stderr"
if test $test_external_has_tap -eq 0; then
test_ok_ "$descr"
else
say_color "" "# test_external_without_stderr test $descr was ok"
test_success=$(($test_success + 1))
fi
else
if test "$verbose" = t
then
output=$(echo; echo "# Stderr is:"; cat "$stderr")
else
output=
fi
rm "$stderr"
if test $test_external_has_tap -eq 0; then
test_failure_ "$descr" "$@" "$output"
else
say_color error "# test_external_without_stderr test $descr failed: $@: $output"
test_failure=$(($test_failure + 1))
fi
fi
}
test_path_is_file () {
if ! test -f "$1"
then
echo "File $1 doesn't exist. $2"
false
fi
}
test_path_is_dir () {
if ! test -d "$1"
then
echo "Directory $1 doesn't exist. $2"
false
fi
}
test_path_exists () {
if ! test -e "$1"
then
echo "Path $1 doesn't exist. $2"
false
fi
}
test_dir_is_empty () {
test_path_is_dir "$1" &&
if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
then
echo "Directory '$1' is not empty, it contains:"
ls -la "$1"
return 1
fi
}
test_path_is_missing () {
if test -e "$1"
then
echo "Path exists:"
ls -ld "$1"
if test $ then
echo "$*"
fi
false
fi
}
test_line_count () {
if test $ then
BUG "not 3 parameters to test_line_count"
elif ! test $(wc -l <"$3") "$1" "$2"
then
echo "test_line_count: line count for $3 !$1 $2"
cat "$3"
return 1
fi
}
list_contains () {
case ",$1," in
*,$2,*)
return 0
;;
esac
return 1
}
test_must_fail () {
case "$1" in
ok=*)
_test_ok=${1 shift
;;
*)
_test_ok=
;;
esac
"$@" 2>&7
exit_code=$?
if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
then
echo >&4 "test_must_fail: command succeeded: $*"
return 1
elif test_match_signal 13 $exit_code && list_contains "$_test_ok" sigpipe
then
return 0
elif test $exit_code -gt 129 && test $exit_code -le 192
then
echo >&4 "test_must_fail: died by signal $(($exit_code - 128)): $*"
return 1
elif test $exit_code -eq 127
then
echo >&4 "test_must_fail: command not found: $*"
return 1
elif test $exit_code -eq 126
then
echo >&4 "test_must_fail: valgrind error: $*"
return 1
fi
return 0
} 7>&2 2>&4
test_might_fail () {
test_must_fail ok=success "$@" 2>&7
} 7>&2 2>&4
test_expect_code () {
want_code=$1
shift
"$@" 2>&7
exit_code=$?
if test $exit_code = $want_code
then
return 0
fi
echo >&4 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
return 1
} 7>&2 2>&4
test_cmp() {
$GIT_TEST_CMP "$@"
}
test_cmp_config() {
local GD &&
if test "$1" = "-C"
then
shift &&
GD="-C $1" &&
shift
fi &&
printf "%s\n" "$1" >expect.config &&
shift &&
git $GD config "$@" >actual.config &&
test_cmp expect.config actual.config
}
test_cmp_bin() {
cmp "$@"
}
test_i18ncmp () {
! test_have_prereq C_LOCALE_OUTPUT || test_cmp "$@"
}
test_i18ngrep () {
eval "last_arg=\${$#}"
test -f "$last_arg" ||
BUG "test_i18ngrep requires a file to read as the last parameter"
if test $ { test "x!" = "x$1" && test $ then
BUG "too few parameters to test_i18ngrep"
fi
if test_have_prereq !C_LOCALE_OUTPUT
then
return 0
fi
if test "x!" = "x$1"
then
shift
! grep "$@" && return 0
echo >&4 "error: '! grep $@' did find a match in:"
else
grep "$@" && return 0
echo >&4 "error: 'grep $@' didn't find a match in:"
fi
if test -s "$last_arg"
then
cat >&4 "$last_arg"
else
echo >&4 "<File '$last_arg' is empty>"
fi
return 1
}
verbose () {
"$@" && return 0
echo >&4 "command failed: $(git rev-parse --sq-quote "$@")"
return 1
}
test_must_be_empty () {
test_path_is_file "$1" &&
if test -s "$1"
then
echo "'$1' is not empty, it contains:"
cat "$1"
return 1
fi
}
test_cmp_rev () {
if test $ then
error "bug in the test script: test_cmp_rev requires two revisions, but got $#"
else
local r1 r2
r1=$(git rev-parse --verify "$1") &&
r2=$(git rev-parse --verify "$2") &&
if test "$r1" != "$r2"
then
cat >&4 <<-EOF
error: two revisions point to different objects:
'$1': $r1
'$2': $r2
EOF
return 1
fi
fi
}
test_seq () {
case $ 1) set 1 "$@" ;;
2) ;;
*) BUG "not 1 or 2 parameters to test_seq" ;;
esac
test_seq_counter__=$1
while test "$test_seq_counter__" -le "$2"
do
echo "$test_seq_counter__"
test_seq_counter__=$(( $test_seq_counter__ + 1 ))
done
}
test_when_finished () {
test "${BASH_SUBSHELL-0}" = 0 ||
BUG "test_when_finished does nothing in a subshell"
test_cleanup="{ $*
} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
}
test_create_repo () {
test "$#" = 1 ||
BUG "not 1 parameter to test-create-repo"
repo="$1"
mkdir -p "$repo"
(
cd "$repo" || error "Cannot setup test environment"
"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" init \
"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
error "cannot run git init -- have you built things yet?"
mv .git/hooks .git/hooks-disabled
) || exit
}
test_ln_s_add () {
if test_have_prereq SYMLINKS
then
ln -s "$1" "$2" &&
git update-index --add "$2"
else
printf '%s' "$1" >"$2" &&
ln_s_obj=$(git hash-object -w "$2") &&
git update-index --add --cacheinfo 120000 $ln_s_obj "$2" &&
git update-index "$2"
fi
}
test_write_lines () {
printf "%s\n" "$@"
}
perl () {
command "$PERL_PATH" "$@" 2>&7
} 7>&2 2>&4
test_normalize_bool () {
git -c magic.variable="$1" config --bool magic.variable 2>/dev/null
}
test_tristate () {
if eval "test x\"\${$1+isset}\" = xisset"
then
eval "
case \"\$$1\" in
'') $1=false ;;
auto) ;;
*) $1=\$(test_normalize_bool \$$1 || echo true) ;;
esac
"
else
eval "$1=auto"
fi
}
test_skip_or_die () {
case "$1" in
auto)
skip_all=$2
test_done
;;
true)
error "$2"
;;
*)
error "BUG: test tristate is '$1' (real error: $2)"
esac
}
mingw_test_cmp () {
local test_cmp_a= test_cmp_b=
local stdin_for_diff=
if test -s "$1" && test -s "$2"
then
mingw_read_file_strip_cr_ test_cmp_a <"$1"
mingw_read_file_strip_cr_ test_cmp_b <"$2"
elif test -s "$1" && test "$2" = -
then
mingw_read_file_strip_cr_ test_cmp_a <"$1"
mingw_read_file_strip_cr_ test_cmp_b
stdin_for_diff='<<<"$test_cmp_b"'
elif test "$1" = - && test -s "$2"
then
mingw_read_file_strip_cr_ test_cmp_a
mingw_read_file_strip_cr_ test_cmp_b <"$2"
stdin_for_diff='<<<"$test_cmp_a"'
fi
test -n "$test_cmp_a" &&
test -n "$test_cmp_b" &&
test "$test_cmp_a" = "$test_cmp_b" ||
eval "diff -u \"\$@\" $stdin_for_diff"
}
mingw_read_file_strip_cr_ () {
local line
while :
do
if IFS=$'\r' read -r -d $'\n' line
then
line=$line$'\n'
else
if test -z "$line"
then
break
fi
fi
eval "$1=\$$1\$line"
done
}
test_env () {
(
while test $ do
case "$1" in
*=*)
eval "${1%%=*}=\${1#*=}"
eval "export ${1%%=*}"
shift
;;
*)
"$@" 2>&7
exit
;;
esac
done
)
} 7>&2 2>&4
test_match_signal () {
if test "$2" = "$((128 + $1))"
then
return 0
elif test "$2" = "$((256 + $1))"
then
return 0
fi
return 1
}
test_copy_bytes () {
perl -e '
my $len = $ARGV[1];
while ($len > 0) {
my $s;
my $nread = sysread(STDIN, $s, $len);
die "cannot read: $!" unless defined($nread);
last unless $nread;
print $s;
$len -= $nread;
}
' - "$1"
}
nongit () {
test -d non-repo ||
mkdir non-repo ||
return 1
(
GIT_CEILING_DIRECTORIES=$(pwd) &&
export GIT_CEILING_DIRECTORIES &&
cd non-repo &&
"$@" 2>&7
)
} 7>&2 2>&4
packetize() {
cat >packetize.tmp &&
len=$(wc -c <packetize.tmp) &&
printf '%04x%s' "$(($len + 4))" &&
cat packetize.tmp &&
rm -f packetize.tmp
}
depacketize () {
perl -e '
while (read(STDIN, $len, 4) == 4) {
if ($len eq "0000") {
print "FLUSH\n";
} else {
read(STDIN, $buf, hex($len) - 4);
$buf =~ s/\0/\\0/g;
if ($buf =~ s/^[\x2\x3]//) {
print STDERR $buf;
} else {
$buf =~ s/^\x1//;
print $buf;
}
}
}
'
}
test_set_hash () {
test_hash_algo="$1"
}
test_detect_hash () {
test_hash_algo='sha1'
}
test_oid_init () {
test -n "$test_hash_algo" || test_detect_hash &&
test_oid_cache <"$TEST_DIRECTORY/oid-info/hash-info" &&
test_oid_cache <"$TEST_DIRECTORY/oid-info/oid"
}
test_oid_cache () {
local tag rest k v &&
{ test -n "$test_hash_algo" || test_detect_hash; } &&
while read tag rest
do
case $tag in
\ continue;;
?*)
;;
*)
continue;;
esac &&
k="${rest%:*}" &&
v="${rest#*:}" &&
if ! expr "$k" : '[a-z0-9][a-z0-9]*$' >/dev/null
then
BUG 'bad hash algorithm'
fi &&
eval "test_oid_${k}_$tag=\"\$v\""
done
}
test_oid () {
local var="test_oid_${test_hash_algo}_$1" &&
if eval "test -z \"\${$var+set}\""
then
BUG "undefined key '$1'"
fi &&
eval "printf '%s' \"\${$var}\""
}