1 # Test function library from Git with modifications.
3 # Modifications Copyright (C) 2016,2017,2021 Kyle J. McKay
7 # * Many "GIT_..." variables removed -- some were kept as TESTLIB_..." instead
8 # (Except "GIT_PATH" is new and is the full path to a "git" executable)
10 # * IMPORTANT: test-lib-functions.sh SHOULD NOT EXECUTE ANY CODE! A new
11 # function "test_lib_functions_init" has been added that will be called
12 # and MUST contain any lines of code to be executed. This will ALWAYS
13 # be the LAST function defined in this file for easy locatability.
15 # * Added test_tolerate_failure and $LINENO support unctions
17 # * Anything related to valgrind or perf has been stripped out
19 # * Many other minor changes and efficiencies
21 # Library of functions shared by all tests scripts, included by
24 # Copyright (C) 2005 Junio C Hamano
26 # This program is free software: you can redistribute it and/or modify
27 # it under the terms of the GNU General Public License as published by
28 # the Free Software Foundation, either version 2 of the License, or
29 # (at your option) any later version.
31 # This program is distributed in the hope that it will be useful,
32 # but WITHOUT ANY WARRANTY; without even the implied warranty of
33 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 # GNU General Public License for more details.
36 # You should have received a copy of the GNU General Public License
37 # along with this program. If not, see https://www.gnu.org/licenses/ .
40 ## IMPORTANT: THIS FILE MUST NOT CONTAIN ANYTHING OTHER THAN FUNCTION
41 ## DEFINITION!!! INITIALIZATION GOES IN THE LAST FUNCTION
42 ## DEFINED IN THIS FILE "test_lib_functions_init" IF REQUIRED!
45 # The semantics of the editor variables are that of invoking
46 # sh -c "$EDITOR \"$@\"" files ...
48 # If our trash directory contains shell metacharacters, they will be
49 # interpreted if we just set $EDITOR directly, so do a little dance with
50 # environment variables to work around this.
52 # In particular, quoting isn't enough, as the path may contain the same quote
57 EDITOR
='"$FAKE_EDITOR"'
64 if (n == 0) return "RESET";
65 if (n == 1) return "BOLD";
66 if (n == 30) return "BLACK";
67 if (n == 31) return "RED";
68 if (n == 32) return "GREEN";
69 if (n == 33) return "YELLOW";
70 if (n == 34) return "BLUE";
71 if (n == 35) return "MAGENTA";
72 if (n == 36) return "CYAN";
73 if (n == 37) return "WHITE";
74 if (n == 40) return "BLACK";
75 if (n == 41) return "BRED";
76 if (n == 42) return "BGREEN";
77 if (n == 43) return "BYELLOW";
78 if (n == 44) return "BBLUE";
79 if (n == 45) return "BMAGENTA";
80 if (n == 46) return "BCYAN";
81 if (n == 47) return "BWHITE";
84 while (match($0, /\033\[[0-9;]*m/) != 0) {
85 printf "%s<", substr($0, 1, RSTART-1);
86 codes = substr($0, RSTART+2, RLENGTH-3);
87 if (length(codes) == 0)
90 n = split(codes, ary, ";");
92 for (i = 1; i <= n; i++) {
93 printf "%s%s", sep, name(ary[i]);
98 $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
130 sed -e 's/$/Q/' |
tr Q
'\015'
134 tr '\015' Q |
sed -e 's/Q$//'
137 # In some bourne shell implementations, the "unset" builtin returns
138 # nonzero status when a variable to be unset was not set in the first
141 # Use sane_unset when that should not be considered an error.
144 { "unset" "$@"; } >/dev
/null
2>&1 ||
:
147 test_asv_cache_lno
() {
150 if [ "$1" != "-" ]; then
151 test_asv_cache_lno
"$callerlno" - <<EOT
155 while read _tac_id _tac_hash _tac_value
; do
156 case "$_tac_id" in "#"*|
"") continue; esac
159 eval "test_asvdb_${_tac_id}_$_tac_hash=\"\$_tac_value\""
162 die
"${0##*/}:${callerlno:+$callerlno:}" \
163 "invalid test_asv_cache hash algorithm '$_tac_hash'"
169 test_asv_cache_lno
"" "$@"
171 alias test_asv_cache
='test_asv_cache_lno "$LINENO"' >/dev
/null
2>&1 ||
:
176 _asvhash
="${3:-$test_hash_algo}"
177 : "${_asvhash:=sha1}"
178 case "$_asvhash" in sha1|sha256
);;*)
179 die
"${0##*/}:${callerlno:+$callerlno:} invalid test_v_asv hash algorithm '$_asvhash'"
181 eval "_tac_set=\${test_asvdb_${2}_$_asvhash+set}"
182 test "$_tac_set" = "set" ||
183 die
"${0##*/}:${callerlno:+$callerlno:}" \
184 "missing test_asv_cache value for id \"$2\" hash algorithm $_asvhash"
185 test -z "$1" ||
eval "$1=\"\$test_asvdb_${2}_$_asvhash\""
188 test_v_asv_lno
"" "$@"
190 alias test_v_asv
='test_v_asv_lno "$LINENO"' >/dev
/null
2>&1 ||
:
195 _asvhash
="${2:-$test_hash_algo}"
196 : "${_asvhash:=sha1}"
197 case "$_asvhash" in sha1|sha256
);;*)
198 die
"${0##*/}:${callerlno:+$callerlno:} invalid test_asv hash algorithm '$_asvhash'"
200 eval "_tac_set=\${test_asvdb_${1}_$_asvhash+set}"
201 test "$_tac_set" = "set" ||
202 die
"${0##*/}:${callerlno:+$callerlno:}" \
203 "missing test_asv_cache value for id \"$1\" hash algorithm $_asvhash"
204 eval "printf '%s\n' \"\$test_asvdb_${1}_$_asvhash\""
209 alias test_asv
='test_asv_lno "$LINENO"' >/dev
/null
2>&1 ||
:
211 test_v_git_mt_lno
() {
214 case "$2" in blob|tree|null
);;*)
215 die
"${0##*/}:${callerlno:+$callerlno:} invalid test_v_git_mt object type '$2'"
217 _mthash
="${3:-$test_hash_algo}"
219 case "$_mthash" in sha1|sha256
);;*)
220 die
"${0##*/}:${callerlno:+$callerlno:} invalid test_v_git_mt hash algorithm '$_mthash'"
226 sha1
) _mthashval
="0000000000000000000000000000000000000000";;
227 sha256
) _mthashval
="0000000000000000000000000000000000000000000000000000000000000000";;
231 sha1
) _mthashval
="e69de29bb2d1d6434b8b29ae775ad8c2e48c5391";;
232 sha256
) _mthashval
="473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813";;
236 sha1
) _mthashval
="4b825dc642cb6eb9a060e54bf8d69288fbee4904";;
237 sha256
) _mthashval
="6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321";;
240 test -z "$1" ||
eval "$1=\"\$_mthashval\""
243 test_v_git_mt_lno
"" "$@"
245 alias test_v_git_mt
='test_v_git_mt_lno "$LINENO"' >/dev
/null
2>&1 ||
:
247 test_set_hash_algo_lno
() {
250 case "$1" in sha1|sha256
);;*)
251 die
"${0##*/}:${callerlno:+$callerlno:} invalid test_set_hash_algo hash algorithm '$1'"
253 test "$1" != "sha256" ||
test -n "$test_git229_plus" ||
254 die
"${0##*/}:${callerlno:+$callerlno:}" \
255 "test_set_hash_algo sha256 requires Git 2.29.0 or later" \
256 "but found $git_version"
258 GIT_DEFAULT_HASH
="$test_hash_algo" && export GIT_DEFAULT_HASH
260 test_set_hash_algo
() {
261 test_set_hash_algo_lno
"" "$@"
263 alias test_set_hash_algo
='test_set_hash_algo_lno "$LINENO"' >/dev
/null
2>&1 ||
:
265 # Protect against breaking in the future when Git changes its
266 # nearly two decades old defaults. The `-c` option first appeared
267 # in Git 1.7.2 (2010-07-21). That means this test framework
268 # requires at least Git 1.7.2. Since TopGit requires at least
269 # Git 1.9.2 that's not a problem. If Git is at least version 2.29.0
270 # pass a --object-format=$test_hash_algo option as the first `git init`
273 test -z "$test_git229_plus" ||
274 set -- --object-format="${test_hash_algo:-sha1}" "$@"
275 git
-c init.defaultBranch
=master init
"$@"
279 if test -z "${test_tick:+set}"
283 test_tick
=$
(($test_tick + 60))
285 GIT_COMMITTER_DATE
="$test_tick -0700"
286 GIT_AUTHOR_DATE
="$test_tick -0700"
287 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
290 # Stop execution and start a shell. This is useful for debugging tests and
291 # only makes sense together with "-v".
293 # Be sure to remove all invocations of this command before submitting.
296 if test "$verbose" = t
; then
297 "$SHELL_PATH" <&6 >&3 2>&4
299 error
>&5 "test_pause requires --verbose"
303 test_check_tag_ok_
() {
304 case "$1" in ""|
*"^"*|
*[?
*:~
]*|
*"["*|
*"@{"*|
*".."*|
*"//"*|
*"\\"*) return 1; esac
308 test_check_one_tag_
() {
310 test_check_tag_ok_
"$1"
313 # Call test_commit with the arguments "<message> [<file> [<contents> [<tag>]]]"
315 # This will commit a file with the given contents and the given commit
316 # message, and tag the resulting commit with the given tag name.
318 # <file> defaults to "<message>.t", <contents> and <tag> default to
319 # "<message>". If the (possibly default) value for <tag> ends up being
320 # empty or contains any whitespace or invalid ref name characters the tag will
323 # <file>, <contents>, and <tag> all default to <message>.
344 printf '%s\n' "${3-$1}" > "$file" &&
350 git commit
$signoff -m "$1" &&
351 if test_check_one_tag_
${4-$1}
357 # Call test_merge with the arguments "<message> [<opt>...] <commit>", where
358 # <commit> can be a tag pointing to the commit-to-merge, but automatically skip
359 # the tag if <message> is not tagish and supply --allow-unrelated-histories
360 # when running Git >= 2.9.
364 git merge
$test_auh -m "$@" &&
365 if test_check_one_tag_
$1
371 # This function helps systems where core.filemode=false is set.
372 # Use it instead of plain 'chmod +x' to set or unset the executable bit
373 # of a file in the working directory and add it to the index.
377 git update-index
--add "--chmod=$@"
380 # Unset a configuration variable, but don't fail if it doesn't exist.
389 eval git
"${config_dir:+-C \"\$config_dir\"}" config
--unset-all '"$@"'
391 case "$config_status" in
392 5) # ok, nothing to unset
396 return $config_status
399 # Set git config, automatically unsetting it after the test is over.
408 eval test_when_finished test_unconfig
"${config_dir:+-C \"\$config_dir\"}" '"$1"' &&
409 eval git
"${config_dir:+-C \"\$config_dir\"}" config
'"$@"'
412 test_config_global
() {
413 test_when_finished test_unconfig
--global "$1" &&
414 git config
--global "$@"
419 echo "#!${2-"$SHELL_PATH"}" &&
425 # Use test_set_prereq to tell that a particular prerequisite is available.
426 # The prerequisite can later be checked for in two ways:
428 # - Explicitly using test_have_prereq.
430 # - Implicitly by specifying the prerequisite tag in the calls to
431 # test_expect_{success,failure,code}.
433 # The single parameter is the prerequisite tag (a simple word, in all
434 # capital letters by convention).
437 satisfied_prereq
="$satisfied_prereq$1 "
440 # Usage: test_lazy_prereq PREREQ 'script'
442 lazily_testable_prereq
="$lazily_testable_prereq$1 "
443 eval test_prereq_lazily_
$1=\
$2
446 test_ensure_git_dir_
() {
447 git rev-parse
--git-dir >/dev
/null
2>&1 ||
448 git_init
--quiet --template="$EMPTY_DIRECTORY" >/dev
/null
2>&1 ||
449 fatal
"cannot run git init"
452 test_run_lazy_prereq_
() {
454 test_ensure_temp_dir_ "test_run_lazy_prereq_" "prereq-test-dir" &&
456 cd "$TRASHTMP_DIRECTORY/prereq-test-dir" &&'"$2"'
458 say
>&3 "checking prerequisite: $1"
462 rm -rf "$TRASHTMP_DIRECTORY/prereq-test-dir"
463 if test "$eval_ret" = 0; then
464 say
>&3 "prerequisite $1 ok"
466 say
>&3 "prerequisite $1 not satisfied"
472 # prerequisites can be concatenated with ',' or whitespace
487 case "$prerequisite" in
490 prerequisite
=${prerequisite#!}
496 case " $lazily_tested_prereq " in
500 case " $lazily_testable_prereq " in
502 eval "script=\$test_prereq_lazily_$prerequisite" &&
503 if test_run_lazy_prereq_
"$prerequisite" "$script"
505 test_set_prereq
$prerequisite
507 lazily_tested_prereq
="$lazily_tested_prereq$prerequisite "
512 total_prereq
=$
(($total_prereq + 1))
513 satisfied_this_prereq
=
514 if test "$prerequisite" = "LASTOK"
516 if test -n "$test_last_subtest_ok"
518 satisfied_this_prereq
=t
520 elif test "$prerequisite" = "GITSHA1"
522 if test "${test_hash_algo:-sha1}" = "sha1"
524 satisfied_this_prereq
=t
527 case "$satisfied_prereq" in
529 satisfied_this_prereq
=t
533 case "$satisfied_this_prereq,$negative_prereq" in
535 ok_prereq
=$
(($ok_prereq + 1))
538 # Keep a list of missing prerequisites; restore
539 # the negative marker if necessary.
540 prerequisite
=${negative_prereq:+!}$prerequisite
541 if test -z "$missing_prereq"
543 missing_prereq
=$prerequisite
545 missing_prereq
="$missing_prereq, $prerequisite"
550 test $total_prereq = $ok_prereq
553 test_declared_prereq
() {
554 case " $test_prereq " in
562 test_verify_prereq
() {
563 test -z "$test_prereq" ||
564 test "x$test_prereq" = "x${test_prereq#*[!A-Z0-9_ !]}" ||
565 error
"bug in the test script: '$test_prereq' does not look like a prereq"
568 _test_set_test_prereq
() {
578 while test "$#" != "0"
580 test_prereq_fmt
="${test_prereq_fmt:+$test_prereq_fmt, }$1"
585 test_expect_failure_lno
() {
589 test "$#" = 3 && { _test_set_test_prereq
"$1"; shift; } || test_prereq
=
591 error
"bug in the test script: not 2 or 3 parameters to test-expect-failure"
593 set -- "$1" "$test_script_"
598 say
>&3 "checking known breakage: $2"
599 if test_run_
"$2" expecting_failure
601 test_known_broken_ok_
"$1"
602 test_last_subtest_ok
=1
604 test_known_broken_failure_
"$1"
605 test_last_subtest_ok
=
611 test_expect_failure
() {
612 test_expect_failure_lno
"" "$@"
614 alias test_expect_failure
='test_expect_failure_lno "$LINENO"' >/dev
/null
2>&1 ||
:
616 if test -n "$TESTLIB_NO_TOLERATE"; then
617 test_tolerate_failure_lno
() { test_expect_success_lno
"$@"; }
619 test_tolerate_failure_lno
() {
623 test "$#" = 3 && { _test_set_test_prereq
"$1"; shift; } || test_prereq
=
625 error
"bug in the test script: not 2 or 3 parameters to test-tolerate-failure"
627 set -- "$1" "$test_script_"
632 say
>&3 "checking possible breakage: $2"
633 if test_run_
"$2" tolerating_failure
635 test_possibly_broken_ok_
"$1"
636 test_last_subtest_ok
=1
638 test_possibly_broken_failure_
"$1"
639 test_last_subtest_ok
=
646 test_tolerate_failure
() {
647 test_tolerate_failure_lno
"" "$@"
649 alias test_tolerate_failure
='test_tolerate_failure_lno "$LINENO"' >/dev
/null
2>&1 ||
:
651 test_expect_success_lno
() {
655 test "$#" = 3 && { _test_set_test_prereq
"$1"; shift; } || test_prereq
=
657 error
"bug in the test script: not 2 or 3 parameters to test-expect-success"
659 set -- "$1" "$test_script_"
664 say
>&3 "expecting success: $2"
668 test_last_subtest_ok
=1
670 test_failure_
"$callerlno" "$@"
671 test_last_subtest_ok
=
677 test_expect_success
() {
678 test_expect_success_lno
"" "$@"
680 alias test_expect_success
='test_expect_success_lno "$LINENO"' >/dev
/null
2>&1 ||
:
682 # test_external runs external test scripts that provide continuous
683 # test output about their progress, and succeeds/fails on
684 # zero/non-zero exit code. It outputs the test output on stdout even
685 # in non-verbose mode, and announces the external script with "# run
686 # <n>: ..." before running it. When providing relative paths, keep in
687 # mind that all scripts run in "trash directory".
688 # Usage: test_external description command arguments...
689 # Example: test_external 'Awk API' awk -f ../path/to/test.awk
690 test_external_lno
() {
693 test_count
=$
(($test_count+1))
694 test "$#" = 4 && { _test_set_test_prereq
"$1"; shift; } || test_prereq
=
696 error
>&5 "bug in the test script: not 3 or 4 parameters to test_external"
701 test_external_skipped
=1
702 if ! test_skip
"$descr" "$@"
704 # Announce the script to reduce confusion about the
705 # test output that follows.
706 say_color
"" "# run $test_count: $descr ($*)"
707 # Export TEST_DIRECTORY, TRASH_DIRECTORY, TRASHTMP_DIRECTORY
708 # and TESTLIB_TEST_LONG to be able to use them in script
709 export TEST_DIRECTORY TRASH_DIRECTORY TRASHTMP_DIRECTORY TESTLIB_TEST_LONG
710 # Run command; redirect its stderr to &4 as in
711 # test_run_, but keep its stdout on our stdout even in
716 if test $test_external_has_tap -eq 0; then
719 say_color
"" "# test_external test $descr was ok"
720 test_success
=$
(($test_success + 1))
722 test_last_subtest_ok
=1
724 if test $test_external_has_tap -eq 0; then
725 test_failure_
"$callerlno" "$descr" "$@"
727 say_color error
"# test_external test $descr failed: $@"
728 test_failure
=$
(($test_failure + 1))
730 test_last_subtest_ok
=
732 test_external_skipped
=
737 test_external_lno
"" "$@"
739 alias test_external
='test_external_lno "$LINENO"' >/dev
/null
2>&1 ||
:
741 # Like test_external, but in addition tests that the command generated
742 # no output on stderr.
743 test_external_without_stderr_lno
() {
746 # The temporary file has no (and must have no) security
749 stderr
="$tmp/git-external-stderr.$$.tmp"
750 test_external_lno
"$callerlno" "$@" 4> "$stderr"
751 test -f "$stderr" || error
"Internal error: $stderr disappeared."
752 descr
="no stderr: $1"
754 test_count
=$
(($test_count+1))
755 say
>&3 "# expecting no stderr from previous command"
756 if test -n "$test_external_skipped" ||
test ! -s "$stderr"
760 if test $test_external_has_tap -eq 0; then
763 say_color
"" "# test_external_without_stderr test $descr was ok"
764 test_success
=$
(($test_success + 1))
767 test_last_subtest_ok
=
768 if test "$verbose" = t
770 output
=$
(echo; echo "# Stderr is:"; cat "$stderr")
774 # rm first in case test_failure exits.
776 if test $test_external_has_tap -eq 0; then
777 test_failure_
"$callerlno" "$descr" "$@" "$output"
779 say_color error
"# test_external_without_stderr test $descr failed: $@: $output"
780 test_failure
=$
(($test_failure + 1))
785 test_external_without_stderr
() {
786 test_external_without_stderr_lno
"" "$@"
788 alias test_external_without_stderr
='test_external_without_stderr_lno "$LINENO"' >/dev
/null
2>&1 ||
:
790 # debugging-friendly alternatives to "test [-f|-d|-e]"
791 # The commands test the existence or non-existence of $1. $2 can be
792 # given to provide a more precise diagnosis.
793 test_path_is_file
() {
796 echo "File $1 doesn't exist. $2"
804 echo "Directory $1 doesn't exist. $2"
809 # Check if the directory exists and is empty as expected, barf otherwise.
810 test_dir_is_empty
() {
811 test_path_is_dir
"$1" &&
812 if test -n "$(ls -a1 "$1" | grep -E -v '^\.\.?$')"
814 echo "Directory '$1' is not empty, it contains:"
820 test_path_is_missing
() {
837 wc_vals_
="$(command wc "$@
")" &&
843 unset_ wc_ wc_vals_
&&
847 # test_line_count checks that a file has the number of lines it
848 # ought to. For example:
850 # test_expect_success 'produce exactly one line of output' '
851 # do something >output &&
852 # test_line_count = 1 output
855 # is like "test $(wc -l <output) = 1" except that it passes the
856 # output through when the number of lines is wrong.
861 error
"bug in the test script: not 3 parameters to test_line_count"
862 elif ! test $
(wc -l <"$3") "$1" "$2"
864 echo "test_line_count: line count for $3 !$1 $2"
870 # Returns success if a comma separated string of keywords ($1) contains a
871 # given keyword ($2).
873 # `list_contains "foo,bar" bar` returns 0
874 # `list_contains "foo" bar` returns 1
885 # This is not among top-level (test_expect_success | test_expect_failure)
886 # but is a prefix that can be used in the test script, like:
888 # test_expect_success 'complain and die' '
890 # do something else &&
891 # test_must_fail git checkout ../outerspace
894 # Writing this as "! git checkout ../outerspace" is wrong, because
895 # the failure could be due to a segv. We want a controlled failure.
910 if test $exit_code -eq 0 && ! list_contains
"$_test_ok" success
912 echo >&2 "test_must_fail: command succeeded: $*"
914 elif test_match_signal
13 $exit_code && list_contains
"$_test_ok" sigpipe
917 elif test $exit_code -gt 129 && test $exit_code -le 192
919 echo >&2 "test_must_fail: died by signal $(($exit_code - 128)): $*"
921 elif test $exit_code -eq 127
923 echo >&2 "test_must_fail: command not found: $*"
925 elif test $exit_code -eq 126
927 echo >&2 "test_must_fail: command not executable: $*"
933 # Similar to test_must_fail, but tolerates success, too. This is
934 # meant to be used in contexts like:
936 # test_expect_success 'some command works without configuration' '
937 # test_might_fail git config --unset all.configuration &&
941 # Writing "git config --unset all.configuration || :" would be wrong,
942 # because we want to notice if it fails due to segv.
945 test_must_fail ok
=success
"$@"
948 # Similar to test_must_fail and test_might_fail, but check that a
949 # given command exited with a given exit code. Meant to be used as:
951 # test_expect_success 'Merge with d/f conflicts' '
952 # test_expect_code 1 git merge "merge msg" B master
961 if test $exit_code = $want_code
966 echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
970 # test_cmp is a helper function to compare actual and expected output.
971 # You can use it like:
973 # test_expect_success 'foo works' '
974 # echo expected >expected &&
976 # test_cmp expected actual
979 # This could be written as either "cmp" or "diff -u", but:
980 # - cmp's output is not nearly as easy to read as diff -u
981 # - not all diff versions understand "-u"
984 $TESTLIB_TEST_CMP "$@"
987 # test_cmp_bin - helper to compare binary files
993 # Use git diff --no-index for the diff (with a few supporting options)
995 git
--git-dir="$EMPTY_DIRECTORY" --no-pager -c core.abbrev
=16 diff --no-color --exit-code --no-prefix --no-index "$@"
998 # Call any command "$@" but be more verbose about its
999 # failure. This is handy for commands like "test" which do
1000 # not output anything when they fail.
1003 echo >&2 "command failed: $(git rev-parse --sq-quote "$@
")"
1007 # Check if the file expected to be empty is indeed empty, and barfs
1010 test_must_be_empty
() {
1013 echo "'$1' is not empty, it contains:"
1019 # Tests that its two parameters refer to the same revision
1021 test_ensure_temp_dir_
"test_cmp_rev"
1022 git rev-parse
--verify "$1" -- >"$TRASHTMP_DIRECTORY/expect.rev" &&
1023 git rev-parse
--verify "$2" -- >"$TRASHTMP_DIRECTORY/actual.rev" &&
1024 test_cmp
"$TRASHTMP_DIRECTORY/expect.rev" "$TRASHTMP_DIRECTORY/actual.rev"
1027 # Print a sequence of integers in increasing order, either with
1028 # two arguments (start and end):
1030 # test_seq 1 5 -- outputs 1 2 3 4 5 one line at a time
1032 # or with one argument (end), in which case it starts counting
1039 *) error
"bug in the test script: not 1 or 2 parameters to test_seq" ;;
1041 test_seq_counter__
=$1
1042 while test "$test_seq_counter__" -le "$2"
1044 echo "$test_seq_counter__"
1045 test_seq_counter__
=$
(( $test_seq_counter__ + 1 ))
1049 # This function can be used to schedule some commands to be run
1050 # unconditionally at the end of the test to restore sanity:
1052 # test_expect_success 'test core.capslock' '
1053 # git config core.capslock true &&
1054 # test_when_finished git config --unset core.capslock &&
1058 # That would be roughly equivalent to
1060 # test_expect_success 'test core.capslock' '
1061 # git config core.capslock true &&
1063 # git config --unset core.capslock
1066 # except that the greeting and config --unset must both succeed for
1069 # Note that under --immediate mode, no clean-up is done to help diagnose
1072 test_when_finished
() {
1073 test z
"$*" != z
&& test -z "$linting" ||
return 0
1074 test_ensure_temp_dir_
"test_when_finished"
1075 twf_script_
="$TRASHTMP_DIRECTORY/test_when_finished_${test_count:-0}.sh"
1077 for twf_arg_
in "$@"; do
1079 case "$twf_arg_" in [A-Za-z_
]*)
1080 if test z
"${twf_arg_%%[!A-Za-z_0-9]*}" = z
"$twf_arg_"
1082 twf_arg_sq_
="$twf_arg_"
1084 else case "$twf_arg_" in *=*)
1085 twf_var_
="${twf_arg_%%=*}"
1086 if test z
"${twf_var_%%[!A-Za-z_0-9]*}" = z
"$twf_var_"
1088 test_quotevar_
3 twf_arg_sq_
"${twf_arg_#*=}"
1089 twf_arg_sq_
="$twf_var_=$twf_arg_sq_"
1094 test z
"$twf_dq_" = z || test_quotevar_ twf_arg_ twf_arg_sq_
1095 twf_cmd_
="${twf_cmd_:+$twf_cmd_ }$twf_arg_sq_"
1097 printf '{ %s\n} && (exit "$eval_ret"); eval_ret=$?\n' "$twf_cmd_" >>"$twf_script_"
1100 # clear out any test_when_finished items scheduled so far in this subtest
1101 test_clear_when_finished
() {
1102 tcwf_script_
="$TRASHTMP_DIRECTORY/test_when_finished_${test_count:-0}.sh"
1103 ! test -e "$tcwf_script_" ||
{
1104 rm -f "$tcwf_script_" &&
1105 ! test -e "$tcwf_script_"
1109 # Most tests can use the created repository, but some may need to create more.
1110 # Usage: test_create_repo <directory>
1111 test_create_repo
() {
1113 error
"bug in the test script: not 1 parameter to test-create-repo"
1117 cd "$repo" || error
"Cannot setup test environment"
1118 git_init
--quiet "--template=$EMPTY_DIRECTORY" >&3 2>&4 ||
1119 error
"cannot run git init -- have you built things yet?"
1120 ! [ -e .git
/hooks
] ||
mv .git
/hooks .git
/hooks-disabled
1124 # This function helps on symlink challenged file systems when it is not
1125 # important that the file system entry is a symbolic link.
1126 # Use test_ln_s_add instead of "ln -s x y && git add y" to add a
1127 # symbolic link entry y to the index.
1130 if test_have_prereq SYMLINKS
1133 git update-index
--add "$2"
1135 printf '%s' "$1" >"$2" &&
1136 ln_s_obj
=$
(git hash-object
-w "$2") &&
1137 git update-index
--add --cacheinfo 120000 $ln_s_obj "$2" &&
1138 # pick up stat info from the file
1139 git update-index
"$2"
1143 # This function writes out its parameters, one per line
1144 test_write_lines
() {
1149 { "unset" -f awk; } >/dev
/null
2>&1 ||
:
1150 "exec" "$AWK_PATH" "$@"
1154 { "unset" -f git
; } >/dev
/null
2>&1 ||
:
1155 "exec" "$GIT_PATH" "$@"
1159 : "${callerlno:=$1}"
1161 perlerr_
() { return 70; } # EX_SOFTWARE
1162 perlerr_ || die
"${0##*/}:${callerlno:+$callerlno:} test suite attempted to use perl"
1163 exit 70 # EX_SOFTWARE
1168 alias perl
='perl_lno "$LINENO"' >/dev
/null
2>&1 ||
:
1170 # Given a variable $1, normalize the value of it to one of "true",
1171 # "false", or "auto" and store the result to it.
1173 # test_tristate TESTLIB_TEST_FLIBITY
1175 # A variable set to an empty string is set to 'false'.
1176 # A variable set to 'false' or 'auto' keeps its value.
1177 # Anything else is set to 'true'.
1178 # An unset variable defaults to 'auto'.
1180 # The last rule is to allow people to set the variable to an empty
1181 # string and export it to decline testing the particular feature
1182 # for versions both before and after this change. We used to treat
1183 # both unset and empty variable as a signal for "do not test" and
1184 # took any non-empty string as "please test".
1187 if eval "test \"z\${$1+set}\" = \"zset\""
1190 eval "set -- \"\$1\" \"\$$1\""
1191 case "$2" in [-+]0?
*|
0?
*)
1192 set -- "$1" "${2#[-+]}"
1193 set -- "$1" "$2" "${2%%[!0]*}"
1194 set -- "$1" "${2#"$3"}"
1199 ""|
[Ff
][Aa
][Ll
][Ss
][Ee
]|
[Oo
][Ff
][Ff
]|
[Nn
][Oo
]|
[-+]0|
0)
1202 # Git is actually more picky than this in that
1203 # only true/on/yes/(int)!=0 qualify else an err
1204 # but the original code treated those as true
1213 # Exit the test suite, either by skipping all remaining tests or by
1214 # exiting with an error. If "$1" is "auto", we then we assume we were
1215 # opportunistically trying to set up some tests and we skip. If it is
1216 # "true", then we report a failure.
1218 # The error/skip message should be given by $2.
1220 test_skip_or_die
() {
1230 error
"BUG: test tristate is '$1' (real error: $2)"
1234 # The following mingw_* functions obey POSIX shell syntax, but are actually
1235 # bash scripts, and are meant to be used only with bash on Windows.
1237 # A test_cmp function that treats LF and CRLF equal and avoids forking
1238 # diff when possible.
1240 # Read text into shell variables and compare them. If the results
1241 # are different, use regular diff to report the difference.
1242 test_cmp_a
= test_cmp_b
=
1244 # When text came from stdin (one argument is '-') we must feed it
1248 # Since it is difficult to detect the difference between an
1249 # empty input file and a failure to read the files, we go straight
1250 # to diff if one of the inputs is empty.
1251 if test -s "$1" && test -s "$2"
1253 # regular case: both files non-empty
1254 mingw_read_file_strip_cr_ test_cmp_a
<"$1"
1255 mingw_read_file_strip_cr_ test_cmp_b
<"$2"
1256 elif test -s "$1" && test "$2" = -
1258 # read 2nd file from stdin
1259 mingw_read_file_strip_cr_ test_cmp_a
<"$1"
1260 mingw_read_file_strip_cr_ test_cmp_b
1261 stdin_for_diff
='<<<"$test_cmp_b"'
1262 elif test "$1" = - && test -s "$2"
1264 # read 1st file from stdin
1265 mingw_read_file_strip_cr_ test_cmp_a
1266 mingw_read_file_strip_cr_ test_cmp_b
<"$2"
1267 stdin_for_diff
='<<<"$test_cmp_a"'
1269 test -n "$test_cmp_a" &&
1270 test -n "$test_cmp_b" &&
1271 test "$test_cmp_a" = "$test_cmp_b" ||
1272 eval "diff -u \"\$@\" $stdin_for_diff"
1275 # $1 is the name of the shell variable to fill in
1276 mingw_read_file_strip_cr_
() {
1277 # Read line-wise using LF as the line separator
1278 # and use IFS to strip CR.
1282 if IFS
=$
'\r' read -r -d $
'\n' line_
1287 # we get here at EOF, but also if the last line
1288 # was not terminated by LF; in the latter case,
1289 # some text was read
1296 eval "$1=\$$1\$line_"
1300 # Like "env FOO=BAR some-program", but run inside a subshell, which means
1301 # it also works for shell functions (though those functions cannot impact
1302 # the environment outside of the test_env invocation).
1309 eval "${1%%=*}=\${1#*=}"
1310 eval "export ${1%%=*}"
1322 # Returns true if the numeric exit code in "$2" represents the expected signal
1323 # in "$1". Signals should be given numerically.
1324 test_match_signal
() {
1325 if test "$2" = "$((128 + $1))"
1329 elif test "$2" = "$((256 + $1))"
1337 # Read up to "$1" bytes (or to EOF) from stdin and write them to stdout.
1339 dd bs
=1 count
="$1" 2>/dev
/null
1343 # THIS SHOULD ALWAYS BE THE LAST FUNCTION DEFINED IN THIS FILE
1345 # Any client that sources this file should immediately execute this function
1348 # THERE SHOULD NOT BE ANY DIRECTLY EXECUTED LINES OF CODE IN THIS FILE
1350 test_lib_functions_init
() {
1351 satisfied_prereq
=" "
1352 lazily_testable_prereq
= lazily_tested_prereq
=