diff options
Diffstat (limited to 't/test-lib-functions.sh')
-rw-r--r-- | t/test-lib-functions.sh | 563 |
1 files changed, 444 insertions, 119 deletions
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index f233522f43..b823c14027 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -32,11 +32,6 @@ test_set_editor () { export EDITOR } -test_set_index_version () { - GIT_INDEX_VERSION="$1" - export GIT_INDEX_VERSION -} - test_decode_color () { awk ' function name(n) { @@ -116,13 +111,6 @@ remove_cr () { tr '\015' Q | sed -e 's/Q$//' } -# Generate an output of $1 bytes of all zeroes (NULs, not ASCII zeroes). -# If $1 is 'infinity', output forever or until the receiving pipe stops reading, -# whichever comes first. -generate_zero_bytes () { - test-tool genzeros "$@" -} - # In some bourne shell implementations, the "unset" builtin returns # nonzero status when a variable to be unset was not set in the first # place. @@ -178,34 +166,60 @@ debug () { GIT_DEBUGGER="${GIT_DEBUGGER}" "$@" <&6 >&5 2>&7 } -# Call test_commit with the arguments -# [-C <directory>] <message> [<file> [<contents> [<tag>]]]" +# Usage: test_commit [options] <message> [<file> [<contents> [<tag>]]] +# -C <dir>: +# Run all git commands in directory <dir> +# --notick +# Do not call test_tick before making a commit +# --append +# Use "echo >>" instead of "echo >" when writing "<contents>" to +# "<file>" +# --signoff +# Invoke "git commit" with --signoff +# --author <author> +# Invoke "git commit" with --author <author> # # This will commit a file with the given contents and the given commit # message, and tag the resulting commit with the given tag name. # # <file>, <contents>, and <tag> all default to <message>. -# -# If the first argument is "-C", the second argument is used as a path for -# the git invocations. test_commit () { notick= && + append= && + author= && signoff= && indir= && + no_tag= && while test $# != 0 do case "$1" in --notick) notick=yes ;; + --append) + append=yes + ;; + --author) + author="$2" + shift + ;; --signoff) signoff="$1" ;; + --date) + notick=yes + GIT_COMMITTER_DATE="$2" + GIT_AUTHOR_DATE="$2" + shift + ;; -C) indir="$2" shift ;; + --no-tag) + no_tag=yes + ;; *) break ;; @@ -214,23 +228,158 @@ test_commit () { done && indir=${indir:+"$indir"/} && file=${2:-"$1.t"} && - echo "${3-$1}" > "$indir$file" && + if test -n "$append" + then + echo "${3-$1}" >>"$indir$file" + else + echo "${3-$1}" >"$indir$file" + fi && 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}" + git ${indir:+ -C "$indir"} commit \ + ${author:+ --author "$author"} \ + $signoff -m "$1" && + if test -z "$no_tag" + then + git ${indir:+ -C "$indir"} tag "${4:-$1}" + fi } # Call test_merge with the arguments "<message> <commit>", where <commit> # can be a tag pointing to the commit-to-merge. test_merge () { + label="$1" && + shift && test_tick && - git merge -m "$1" "$2" && - git tag "$1" + git merge -m "$label" "$@" && + git tag "$label" +} + +# Efficiently create <nr> commits, each with a unique number (from 1 to <nr> +# by default) in the commit message. +# +# Usage: test_commit_bulk [options] <nr> +# -C <dir>: +# Run all git commands in directory <dir> +# --ref=<n>: +# ref on which to create commits (default: HEAD) +# --start=<n>: +# number commit messages from <n> (default: 1) +# --message=<msg>: +# use <msg> as the commit mesasge (default: "commit %s") +# --filename=<fn>: +# modify <fn> in each commit (default: %s.t) +# --contents=<string>: +# place <string> in each file (default: "content %s") +# --id=<string>: +# shorthand to use <string> and %s in message, filename, and contents +# +# The message, filename, and contents strings are evaluated by printf, with the +# first "%s" replaced by the current commit number. So you can do: +# +# test_commit_bulk --filename=file --contents="modification %s" +# +# to have every commit touch the same file, but with unique content. +# +test_commit_bulk () { + tmpfile=.bulk-commit.input + indir=. + ref=HEAD + n=1 + message='commit %s' + filename='%s.t' + contents='content %s' + while test $# -gt 0 + do + case "$1" in + -C) + indir=$2 + shift + ;; + --ref=*) + ref=${1#--*=} + ;; + --start=*) + n=${1#--*=} + ;; + --message=*) + message=${1#--*=} + ;; + --filename=*) + filename=${1#--*=} + ;; + --contents=*) + contents=${1#--*=} + ;; + --id=*) + message="${1#--*=} %s" + filename="${1#--*=}-%s.t" + contents="${1#--*=} %s" + ;; + -*) + BUG "invalid test_commit_bulk option: $1" + ;; + *) + break + ;; + esac + shift + done + total=$1 + + add_from= + if git -C "$indir" rev-parse --quiet --verify "$ref" + then + add_from=t + fi + + while test "$total" -gt 0 + do + test_tick && + echo "commit $ref" + printf 'author %s <%s> %s\n' \ + "$GIT_AUTHOR_NAME" \ + "$GIT_AUTHOR_EMAIL" \ + "$GIT_AUTHOR_DATE" + printf 'committer %s <%s> %s\n' \ + "$GIT_COMMITTER_NAME" \ + "$GIT_COMMITTER_EMAIL" \ + "$GIT_COMMITTER_DATE" + echo "data <<EOF" + printf "$message\n" $n + echo "EOF" + if test -n "$add_from" + then + echo "from $ref^0" + add_from= + fi + printf "M 644 inline $filename\n" $n + echo "data <<EOF" + printf "$contents\n" $n + echo "EOF" + echo + n=$((n + 1)) + total=$((total - 1)) + done >"$tmpfile" + + git -C "$indir" \ + -c fastimport.unpacklimit=0 \ + fast-import <"$tmpfile" || return 1 + + # This will be left in place on failure, which may aid debugging. + rm -f "$tmpfile" + + # If we updated HEAD, then be nice and update the index and working + # tree, too. + if test "$ref" = "HEAD" + then + git -C "$indir" checkout -f HEAD || return 1 + fi + } # This function helps systems where core.filemode=false is set. @@ -242,9 +391,14 @@ test_chmod () { git update-index --add "--chmod=$@" } -# Get the modebits from a file. +# Get the modebits from a file or directory, ignoring the setgid bit (g+s). +# This bit is inherited by subdirectories at their creation. So we remove it +# from the returning string to prevent callers from having to worry about the +# state of the bit in the test directory. +# test_modebits () { - ls -l "$1" | sed -e 's|^\(..........\).*|\1|' + ls -ld "$1" | sed -e 's|^\(..........\).*|\1|' \ + -e 's|^\(......\)S|\1-|' -e 's|^\(......\)s|\1x|' } # Unset a configuration variable, but don't fail if it doesn't exist. @@ -298,7 +452,7 @@ write_script () { # - Explicitly using test_have_prereq. # # - Implicitly by specifying the prerequisite tag in the calls to -# test_expect_{success,failure,code}. +# test_expect_{success,failure} and test_external{,_without_stderr}. # # The single parameter is the prerequisite tag (a simple word, in all # capital letters by convention). @@ -309,6 +463,26 @@ test_unset_prereq () { } test_set_prereq () { + if test -n "$GIT_TEST_FAIL_PREREQS_INTERNAL" + then + case "$1" in + # The "!" case is handled below with + # test_unset_prereq() + !*) + ;; + # (Temporary?) whitelist of things we can't easily + # pretend not to support + SYMLINKS) + ;; + # Inspecting whether GIT_TEST_FAIL_PREREQS is on + # should be unaffected. + FAIL_PREREQS) + ;; + *) + return + esac + fi + case "$1" in !*) test_unset_prereq "${1#!}" @@ -329,15 +503,15 @@ test_lazy_prereq () { test_run_lazy_prereq_ () { script=' -mkdir -p "$TRASH_DIRECTORY/prereq-test-dir" && +mkdir -p "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" && ( - cd "$TRASH_DIRECTORY/prereq-test-dir" &&'"$2"' + cd "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" &&'"$2"' )' say >&3 "checking prerequisite: $1" say >&3 "$script" test_eval_ "$script" eval_ret=$? - rm -rf "$TRASH_DIRECTORY/prereq-test-dir" + rm -rf "$TRASH_DIRECTORY/prereq-test-dir-$1" if test "$eval_ret" = 0; then say >&3 "prerequisite $1 ok" else @@ -437,7 +611,7 @@ test_expect_failure () { export test_prereq if ! test_skip "$@" then - say >&3 "checking known breakage: $2" + say >&3 "checking known breakage of $TEST_NUMBER.$test_count '$1': $2" if test_run_ "$2" expecting_failure then test_known_broken_ok_ "$1" @@ -457,7 +631,7 @@ test_expect_success () { export test_prereq if ! test_skip "$@" then - say >&3 "expecting success: $2" + say >&3 "expecting success of $TEST_NUMBER.$test_count '$1': $2" if test_run_ "$2" then test_ok_ "$1" @@ -556,34 +730,37 @@ test_external_without_stderr () { } # debugging-friendly alternatives to "test [-f|-d|-e]" -# The commands test the existence or non-existence of $1. $2 can be -# given to provide a more precise diagnosis. +# The commands test the existence or non-existence of $1 test_path_is_file () { + test "$#" -ne 1 && BUG "1 param" if ! test -f "$1" then - echo "File $1 doesn't exist. $2" + echo "File $1 doesn't exist" false fi } test_path_is_dir () { + test "$#" -ne 1 && BUG "1 param" if ! test -d "$1" then - echo "Directory $1 doesn't exist. $2" + echo "Directory $1 doesn't exist" false fi } test_path_exists () { + test "$#" -ne 1 && BUG "1 param" if ! test -e "$1" then - echo "Path $1 doesn't exist. $2" + echo "Path $1 doesn't exist" false fi } # Check if the directory exists and is empty as expected, barf otherwise. test_dir_is_empty () { + test "$#" -ne 1 && BUG "1 param" test_path_is_dir "$1" && if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')" then @@ -595,6 +772,7 @@ test_dir_is_empty () { # Check if the file exists and has a size greater than zero test_file_not_empty () { + test "$#" = 2 && BUG "2 param" if ! test -s "$1" then echo "'$1' is not a non-empty file." @@ -603,6 +781,7 @@ test_file_not_empty () { } test_path_is_missing () { + test "$#" -ne 1 && BUG "1 param" if test -e "$1" then echo "Path exists:" @@ -638,6 +817,11 @@ test_line_count () { fi } +test_file_size () { + test "$#" -ne 1 && BUG "1 param" + test-tool path-utils file-size "$1" +} + # Returns success if a comma separated string of keywords ($1) contains a # given keyword ($2). # Examples: @@ -653,6 +837,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: # @@ -672,6 +887,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 @@ -683,6 +909,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 @@ -759,8 +990,9 @@ test_expect_code () { # - cmp's output is not nearly as easy to read as diff -u # - not all diff versions understand "-u" -test_cmp() { - $GIT_TEST_CMP "$@" +test_cmp () { + test "$#" -ne 2 && BUG "2 param" + eval "$GIT_TEST_CMP" '"$@"' } # Check that the given config key has the expected value. @@ -772,7 +1004,7 @@ test_cmp() { # # test_cmp_config foo core.bar # -test_cmp_config() { +test_cmp_config () { local GD && if test "$1" = "-C" then @@ -788,23 +1020,14 @@ test_cmp_config() { # test_cmp_bin - helper to compare binary files -test_cmp_bin() { +test_cmp_bin () { + test "$#" -ne 2 && BUG "2 param" cmp "$@" } -# Use this instead of test_cmp to compare files that contain expected and -# actual output from git commands that can be translated. When running -# under GIT_TEST_GETTEXT_POISON this pretends that the command produced expected -# results. -test_i18ncmp () { - ! test_have_prereq C_LOCALE_OUTPUT || test_cmp "$@" -} - -# Use this instead of "grep expected-string actual" to see if the -# output from a git command that can be translated either contains an -# expected string, or does not contain an unwanted one. When running -# under GIT_TEST_GETTEXT_POISON this pretends that the command produced expected -# results. +# Wrapper for grep which used to be used for +# GIT_TEST_GETTEXT_POISON=false. Only here as a shim for other +# in-flight changes. Should not be used and will be removed soon. test_i18ngrep () { eval "last_arg=\${$#}" @@ -817,12 +1040,6 @@ test_i18ngrep () { BUG "too few parameters to test_i18ngrep" fi - if test_have_prereq !C_LOCALE_OUTPUT - then - # pretend success - return 0 - fi - if test "x!" = "x$1" then shift @@ -858,6 +1075,7 @@ verbose () { # otherwise. test_must_be_empty () { + test "$#" -ne 1 && BUG "1 param" test_path_is_file "$1" && if test -s "$1" then @@ -867,19 +1085,30 @@ test_must_be_empty () { fi } -# Tests that its two parameters refer to the same revision +# Tests that its two parameters refer to the same revision, or if '!' is +# provided first, that its other two parameters refer to different +# revisions. test_cmp_rev () { + local op='=' wrong_result=different + + if test $# -ge 1 && test "x$1" = 'x!' + then + op='!=' + wrong_result='the same' + shift + fi if test $# != 2 then - error "bug in the test script: test_cmp_rev requires two revisions, but got $#" + BUG "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" + r2=$(git rev-parse --verify "$2") || return 1 + + if ! test "$r1" "$op" "$r2" then cat >&4 <<-EOF - error: two revisions point to different objects: + error: two revisions point to $wrong_result objects: '$1': $r1 '$2': $r2 EOF @@ -981,7 +1210,7 @@ test_atexit () { # doing so on Bash is better than nothing (the test will # silently pass on other shells). test "${BASH_SUBSHELL-0}" = 0 || - error "bug in test script: test_atexit does nothing in a subshell" + BUG "test_atexit does nothing in a subshell" test_atexit_cleanup="{ $* } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup" } @@ -995,7 +1224,9 @@ test_create_repo () { mkdir -p "$repo" ( cd "$repo" || error "Cannot setup test environment" - "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" init \ + "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" -c \ + init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \ + 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 @@ -1030,62 +1261,48 @@ perl () { command "$PERL_PATH" "$@" 2>&7 } 7>&2 2>&4 -# Is the value one of the various ways to spell a boolean true/false? -test_normalize_bool () { - git -c magic.variable="$1" config --bool magic.variable 2>/dev/null -} - -# Given a variable $1, normalize the value of it to one of "true", -# "false", or "auto" and store the result to it. +# Given the name of an environment variable with a bool value, normalize +# its value to a 0 (true) or 1 (false or empty string) return code. # -# test_tristate GIT_TEST_HTTPD +# test_bool_env GIT_TEST_HTTPD <default-value> # -# A variable set to an empty string is set to 'false'. -# A variable set to 'false' or 'auto' keeps its value. -# Anything else is set to 'true'. -# An unset variable defaults to 'auto'. -# -# The last rule is to allow people to set the variable to an empty -# string and export it to decline testing the particular feature -# for versions both before and after this change. We used to treat -# both unset and empty variable as a signal for "do not test" and -# took any non-empty string as "please test". +# Return with code corresponding to the given default value if the variable +# is unset. +# Abort the test script if either the value of the variable or the default +# are not valid bool values. -test_tristate () { - if eval "test x\"\${$1+isset}\" = xisset" +test_bool_env () { + if test $# != 2 then - # explicitly set - eval " - case \"\$$1\" in - '') $1=false ;; - auto) ;; - *) $1=\$(test_normalize_bool \$$1 || echo true) ;; - esac - " - else - eval "$1=auto" + BUG "test_bool_env requires two parameters (variable name and default value)" fi + + git env--helper --type=bool --default="$2" --exit-code "$1" + ret=$? + case $ret in + 0|1) # unset or valid bool value + ;; + *) # invalid bool value or something unexpected + error >&7 "test_bool_env requires bool values both for \$$1 and for the default fallback" + ;; + esac + return $ret } # Exit the test suite, either by skipping all remaining tests or by -# exiting with an error. If "$1" is "auto", we then we assume we were -# opportunistically trying to set up some tests and we skip. If it is -# "true", then we report a failure. +# exiting with an error. If our prerequisite variable $1 falls back +# on a default assume we were opportunistically trying to set up some +# tests and we skip. If it is explicitly "true", then we report a failure. # # The error/skip message should be given by $2. # test_skip_or_die () { - case "$1" in - auto) + if ! test_bool_env "$1" false + then skip_all=$2 test_done - ;; - true) - error "$2" - ;; - *) - error "BUG: test tristate is '$1' (real error: $2)" - esac + fi + error "$2" } # The following mingw_* functions obey POSIX shell syntax, but are actually @@ -1220,14 +1437,22 @@ nongit () { ) } 7>&2 2>&4 -# convert stdin to pktline representation; note that empty input becomes an -# empty packet, not a flush packet (for that you can just print 0000 yourself). -packetize() { - cat >packetize.tmp && - len=$(wc -c <packetize.tmp) && - printf '%04x%s' "$(($len + 4))" && - cat packetize.tmp && - rm -f packetize.tmp +# convert function arguments or stdin (if not arguments given) to pktline +# representation. If multiple arguments are given, they are separated by +# whitespace and put in a single packet. Note that data containing NULs must be +# given on stdin, and that empty input becomes an empty packet, not a flush +# packet (for that you can just print 0000 yourself). +packetize () { + if test $# -gt 0 + then + packet="$*" + printf '%04x%s' "$((4 + ${#packet}))" "$packet" + else + perl -e ' + my $packet = do { local $/; <STDIN> }; + printf "%04x%s", 4 + length($packet), $packet; + ' + fi } # Parse the input as a series of pktlines, writing the result to stdout. @@ -1267,9 +1492,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 @@ -1318,7 +1541,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. @@ -1329,6 +1562,13 @@ test_oid () { eval "printf '%s' \"\${$var}\"" } +# Insert a slash into an object ID so it can be used to reference a location +# under ".git/objects". For example, "deadbeef..." becomes "de/adbeef..". +test_oid_to_path () { + local basename=${1#??} + echo "${1%$basename}/$basename" +} + # Choose a port number based on the test script's number and store it in # the given variable name, unless that variable already contains a number. test_set_port () { @@ -1367,3 +1607,88 @@ test_set_port () { port=$(($port + ${GIT_TEST_STRESS_JOB_NR:-0})) eval $var=$port } + +# Tests for the hidden file attribute on Windows +test_path_is_hidden () { + test_have_prereq MINGW || + BUG "test_path_is_hidden can only be used on Windows" + + # Use the output of `attrib`, ignore the absolute path + 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 +} + +# Check that the given command was invoked as part of the +# trace2-format trace on stdin. +# +# test_region [!] <category> <label> git <command> <args>... +# +# For example, to look for trace2_region_enter("index", "do_read_index", repo) +# in an invocation of "git checkout HEAD~1", run +# +# GIT_TRACE2_EVENT="$(pwd)/trace.txt" GIT_TRACE2_EVENT_NESTING=10 \ +# git checkout HEAD~1 && +# test_region index do_read_index <trace.txt +# +# If the first parameter passed is !, this instead checks that +# the given region was not entered. +# +test_region () { + local expect_exit=0 + if test "$1" = "!" + then + expect_exit=1 + shift + fi + + grep -e '"region_enter".*"category":"'"$1"'","label":"'"$2"\" "$3" + exitcode=$? + + if test $exitcode != $expect_exit + then + return 1 + fi + + grep -e '"region_leave".*"category":"'"$1"'","label":"'"$2"\" "$3" + exitcode=$? + + if test $exitcode != $expect_exit + then + return 1 + fi + + return 0 +} |