]> git.ipfire.org Git - thirdparty/git.git/blobdiff - t/test-lib-functions.sh
Merge branch 'ak/corrected-commit-date'
[thirdparty/git.git] / t / test-lib-functions.sh
index 3ad712c3accd64b270cb69d4e08c57e45561f5ba..05dc2cc6be8eed02f7aa5e49c94117287085ae4f 100644 (file)
@@ -178,19 +178,28 @@ 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= &&
        while test $# != 0
@@ -199,6 +208,13 @@ test_commit () {
                --notick)
                        notick=yes
                        ;;
+               --append)
+                       append=yes
+                       ;;
+               --author)
+                       author="$2"
+                       shift
+                       ;;
                --signoff)
                        signoff="$1"
                        ;;
@@ -220,13 +236,20 @@ 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"} commit \
+           ${author:+ --author "$author"} \
+           $signoff -m "$1" &&
        git ${indir:+ -C "$indir"} tag "${4:-$1}"
 }
 
@@ -373,9 +396,14 @@ test_chmod () {
        git update-index --add "--chmod=$@"
 }
 
-# Get the modebits from a file or directory.
+# 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 -ld "$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.
@@ -994,19 +1022,16 @@ test_cmp_bin () {
        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.
+# Wrapper for test_cmp 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_i18ncmp () {
-       ! test_have_prereq C_LOCALE_OUTPUT || test_cmp "$@"
+       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=\${$#}"
 
@@ -1019,12 +1044,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
@@ -1661,3 +1680,45 @@ test_subcommand () {
                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
+}