1 # Library of functions shared by all tests scripts, included by
4 # Copyright (c) 2005 Junio C Hamano
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see http://www.gnu.org/licenses/ .
19 # The semantics of the editor variables are that of invoking
20 # sh -c "$EDITOR \"$@\"" files ...
22 # If our trash directory contains shell metacharacters, they will be
23 # interpreted if we just set $EDITOR directly, so do a little dance with
24 # environment variables to work around this.
26 # In particular, quoting isn't enough, as the path may contain the same quote
31 EDITOR='"$FAKE_EDITOR"'
35 test_decode_color () {
38 if (n == 0) return "RESET";
39 if (n == 1) return "BOLD";
40 if (n == 30) return "BLACK";
41 if (n == 31) return "RED";
42 if (n == 32) return "GREEN";
43 if (n == 33) return "YELLOW";
44 if (n == 34) return "BLUE";
45 if (n == 35) return "MAGENTA";
46 if (n == 36) return "CYAN";
47 if (n == 37) return "WHITE";
48 if (n == 40) return "BLACK";
49 if (n == 41) return "BRED";
50 if (n == 42) return "BGREEN";
51 if (n == 43) return "BYELLOW";
52 if (n == 44) return "BBLUE";
53 if (n == 45) return "BMAGENTA";
54 if (n == 46) return "BCYAN";
55 if (n == 47) return "BWHITE";
58 while (match($0, /\033\[[0-9;]*m/) != 0) {
59 printf "%s<", substr($0, 1, RSTART-1);
60 codes = substr($0, RSTART+2, RLENGTH-3);
61 if (length(codes) == 0)
64 n = split(codes, ary, ";");
66 for (i = 1; i <= n; i++) {
67 printf "%s%s", sep, name(ary[i]);
72 $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
100 sed -e 's/$/Q/' | tr Q '\015'
104 tr '\015' Q | sed -e 's/Q$//'
107 # In some bourne shell implementations, the "unset" builtin returns
108 # nonzero status when a variable to be unset was not set in the first
111 # Use sane_unset when that should not be considered an error.
119 if test -z "${test_tick+set}"
123 test_tick=$(($test_tick + 60))
125 GIT_COMMITTER_DATE="$test_tick -0700"
126 GIT_AUTHOR_DATE="$test_tick -0700"
127 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
130 # Stop execution and start a shell. This is useful for debugging tests and
131 # only makes sense together with "-v".
133 # Be sure to remove all invocations of this command before submitting.
136 if test "$verbose" = t; then
137 "$SHELL_PATH" <&6 >&3 2>&4
139 error >&5 "test_pause requires --verbose"
143 # Call test_commit with the arguments "<message> [<file> [<contents> [<tag>]]]"
145 # This will commit a file with the given contents and the given commit
146 # message, and tag the resulting commit with the given tag name.
148 # <file>, <contents>, and <tag> all default to <message>.
169 echo "${3-$1}" > "$file" &&
175 git commit $signoff -m "$1" &&
179 # Call test_merge with the arguments "<message> <commit>", where <commit>
180 # can be a tag pointing to the commit-to-merge.
184 git merge -m "$1" "$2" &&
188 # This function helps systems where core.filemode=false is set.
189 # Use it instead of plain 'chmod +x' to set or unset the executable bit
190 # of a file in the working directory and add it to the index.
194 git update-index --add "--chmod=$@"
197 # Unset a configuration variable, but don't fail if it doesn't exist.
199 git config --unset-all "$@"
201 case "$config_status" in
202 5) # ok, nothing to unset
206 return $config_status
209 # Set git config, automatically unsetting it after the test is over.
211 test_when_finished "test_unconfig '$1'" &&
215 test_config_global () {
216 test_when_finished "test_unconfig --global '$1'" &&
217 git config --global "$@"
222 echo "#!${2-"$SHELL_PATH"}" &&
228 # Use test_set_prereq to tell that a particular prerequisite is available.
229 # The prerequisite can later be checked for in two ways:
231 # - Explicitly using test_have_prereq.
233 # - Implicitly by specifying the prerequisite tag in the calls to
234 # test_expect_{success,failure,code}.
236 # The single parameter is the prerequisite tag (a simple word, in all
237 # capital letters by convention).
240 satisfied_prereq="$satisfied_prereq$1 "
243 lazily_testable_prereq= lazily_tested_prereq=
245 # Usage: test_lazy_prereq PREREQ 'script'
246 test_lazy_prereq () {
247 lazily_testable_prereq="$lazily_testable_prereq$1 "
248 eval test_prereq_lazily_$1=\$2
251 test_run_lazy_prereq_ () {
253 mkdir -p "$TRASH_DIRECTORY/prereq-test-dir" &&
255 cd "$TRASH_DIRECTORY/prereq-test-dir" &&'"$2"'
257 say >&3 "checking prerequisite: $1"
261 rm -rf "$TRASH_DIRECTORY/prereq-test-dir"
262 if test "$eval_ret" = 0; then
263 say >&3 "prerequisite $1 ok"
265 say >&3 "prerequisite $1 not satisfied"
270 test_have_prereq () {
271 # prerequisites can be concatenated with ','
283 case "$prerequisite" in
286 prerequisite=${prerequisite#!}
292 case " $lazily_tested_prereq " in
296 case " $lazily_testable_prereq " in
298 eval "script=\$test_prereq_lazily_$prerequisite" &&
299 if test_run_lazy_prereq_ "$prerequisite" "$script"
301 test_set_prereq $prerequisite
303 lazily_tested_prereq="$lazily_tested_prereq$prerequisite "
308 total_prereq=$(($total_prereq + 1))
309 case "$satisfied_prereq" in
311 satisfied_this_prereq=t
314 satisfied_this_prereq=
317 case "$satisfied_this_prereq,$negative_prereq" in
319 ok_prereq=$(($ok_prereq + 1))
322 # Keep a list of missing prerequisites; restore
323 # the negative marker if necessary.
324 prerequisite=${negative_prereq:+!}$prerequisite
325 if test -z "$missing_prereq"
327 missing_prereq=$prerequisite
329 missing_prereq="$prerequisite,$missing_prereq"
334 test $total_prereq = $ok_prereq
337 test_declared_prereq () {
338 case ",$test_prereq," in
346 test_expect_failure () {
348 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
350 error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
354 say >&3 "checking known breakage: $2"
355 if test_run_ "$2" expecting_failure
357 test_known_broken_ok_ "$1"
359 test_known_broken_failure_ "$1"
365 test_expect_success () {
367 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
369 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
373 say >&3 "expecting success: $2"
384 # test_external runs external test scripts that provide continuous
385 # test output about their progress, and succeeds/fails on
386 # zero/non-zero exit code. It outputs the test output on stdout even
387 # in non-verbose mode, and announces the external script with "# run
388 # <n>: ..." before running it. When providing relative paths, keep in
389 # mind that all scripts run in "trash directory".
390 # Usage: test_external description command arguments...
391 # Example: test_external 'Perl API' perl ../path/to/test.pl
393 test "$#" = 4 && { test_prereq=$1; shift; } || test_prereq=
395 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
399 if ! test_skip "$descr" "$@"
401 # Announce the script to reduce confusion about the
402 # test output that follows.
403 say_color "" "# run $test_count: $descr ($*)"
404 # Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG
405 # to be able to use them in script
406 export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG
407 # Run command; redirect its stderr to &4 as in
408 # test_run_, but keep its stdout on our stdout even in
413 if test $test_external_has_tap -eq 0; then
416 say_color "" "# test_external test $descr was ok"
417 test_success=$(($test_success + 1))
420 if test $test_external_has_tap -eq 0; then
421 test_failure_ "$descr" "$@"
423 say_color error "# test_external test $descr failed: $@"
424 test_failure=$(($test_failure + 1))
430 # Like test_external, but in addition tests that the command generated
431 # no output on stderr.
432 test_external_without_stderr () {
433 # The temporary file has no (and must have no) security
436 stderr="$tmp/git-external-stderr.$$.tmp"
437 test_external "$@" 4> "$stderr"
438 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
439 descr="no stderr: $1"
441 say >&3 "# expecting no stderr from previous command"
442 if [ ! -s "$stderr" ]; then
445 if test $test_external_has_tap -eq 0; then
448 say_color "" "# test_external_without_stderr test $descr was ok"
449 test_success=$(($test_success + 1))
452 if [ "$verbose" = t ]; then
453 output=`echo; echo "# Stderr is:"; cat "$stderr"`
457 # rm first in case test_failure exits.
459 if test $test_external_has_tap -eq 0; then
460 test_failure_ "$descr" "$@" "$output"
462 say_color error "# test_external_without_stderr test $descr failed: $@: $output"
463 test_failure=$(($test_failure + 1))
468 # debugging-friendly alternatives to "test [-f|-d|-e]"
469 # The commands test the existence or non-existence of $1. $2 can be
470 # given to provide a more precise diagnosis.
471 test_path_is_file () {
474 echo "File $1 doesn't exist. $*"
479 test_path_is_dir () {
482 echo "Directory $1 doesn't exist. $*"
487 test_path_is_missing () {
492 if [ $# -ge 1 ]; then
499 # test_line_count checks that a file has the number of lines it
500 # ought to. For example:
502 # test_expect_success 'produce exactly one line of output' '
503 # do something >output &&
504 # test_line_count = 1 output
507 # is like "test $(wc -l <output) = 1" except that it passes the
508 # output through when the number of lines is wrong.
513 error "bug in the test script: not 3 parameters to test_line_count"
514 elif ! test $(wc -l <"$3") "$1" "$2"
516 echo "test_line_count: line count for $3 !$1 $2"
522 # This is not among top-level (test_expect_success | test_expect_failure)
523 # but is a prefix that can be used in the test script, like:
525 # test_expect_success 'complain and die' '
527 # do something else &&
528 # test_must_fail git checkout ../outerspace
531 # Writing this as "! git checkout ../outerspace" is wrong, because
532 # the failure could be due to a segv. We want a controlled failure.
537 if test $exit_code = 0; then
538 echo >&2 "test_must_fail: command succeeded: $*"
540 elif test $exit_code -gt 129 -a $exit_code -le 192; then
541 echo >&2 "test_must_fail: died by signal: $*"
543 elif test $exit_code = 127; then
544 echo >&2 "test_must_fail: command not found: $*"
546 elif test $exit_code = 126; then
547 echo >&2 "test_must_fail: valgrind error: $*"
553 # Similar to test_must_fail, but tolerates success, too. This is
554 # meant to be used in contexts like:
556 # test_expect_success 'some command works without configuration' '
557 # test_might_fail git config --unset all.configuration &&
561 # Writing "git config --unset all.configuration || :" would be wrong,
562 # because we want to notice if it fails due to segv.
567 if test $exit_code -gt 129 -a $exit_code -le 192; then
568 echo >&2 "test_might_fail: died by signal: $*"
570 elif test $exit_code = 127; then
571 echo >&2 "test_might_fail: command not found: $*"
577 # Similar to test_must_fail and test_might_fail, but check that a
578 # given command exited with a given exit code. Meant to be used as:
580 # test_expect_success 'Merge with d/f conflicts' '
581 # test_expect_code 1 git merge "merge msg" B master
584 test_expect_code () {
589 if test $exit_code = $want_code
594 echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
598 # test_cmp is a helper function to compare actual and expected output.
599 # You can use it like:
601 # test_expect_success 'foo works' '
602 # echo expected >expected &&
604 # test_cmp expected actual
607 # This could be written as either "cmp" or "diff -u", but:
608 # - cmp's output is not nearly as easy to read as diff -u
609 # - not all diff versions understand "-u"
615 # Check if the file expected to be empty is indeed empty, and barfs
618 test_must_be_empty () {
621 echo "'$1' is not empty, it contains:"
627 # Tests that its two parameters refer to the same revision
629 git rev-parse --verify "$1" >expect.rev &&
630 git rev-parse --verify "$2" >actual.rev &&
631 test_cmp expect.rev actual.rev
634 # Print a sequence of numbers or letters in increasing order. This is
635 # similar to GNU seq(1), but the latter might not be available
636 # everywhere (and does not do letters). It may be used like:
638 # for i in `test_seq 100`; do
639 # for j in `test_seq 10 20`; do
640 # for k in `test_seq a z`; do
650 *) error "bug in the test script: not 1 or 2 parameters to test_seq" ;;
652 perl -le 'print for $ARGV[0]..$ARGV[1]' -- "$@"
655 # This function can be used to schedule some commands to be run
656 # unconditionally at the end of the test to restore sanity:
658 # test_expect_success 'test core.capslock' '
659 # git config core.capslock true &&
660 # test_when_finished "git config --unset core.capslock" &&
664 # That would be roughly equivalent to
666 # test_expect_success 'test core.capslock' '
667 # git config core.capslock true &&
669 # git config --unset core.capslock
672 # except that the greeting and config --unset must both succeed for
675 # Note that under --immediate mode, no clean-up is done to help diagnose
678 test_when_finished () {
680 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
683 # Most tests can use the created repository, but some may need to create more.
684 # Usage: test_create_repo <directory>
685 test_create_repo () {
687 error "bug in the test script: not 1 parameter to test-create-repo"
691 cd "$repo" || error "Cannot setup test environment"
692 "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
693 error "cannot run git init -- have you built things yet?"
694 mv .git/hooks .git/hooks-disabled
698 # This function helps on symlink challenged file systems when it is not
699 # important that the file system entry is a symbolic link.
700 # Use test_ln_s_add instead of "ln -s x y && git add y" to add a
701 # symbolic link entry y to the index.
704 if test_have_prereq SYMLINKS
707 git update-index --add "$2"
709 printf '%s' "$1" >"$2" &&
710 ln_s_obj=$(git hash-object -w "$2") &&
711 git update-index --add --cacheinfo 120000 $ln_s_obj "$2"
716 command "$PERL_PATH" "$@"
719 # The following mingw_* functions obey POSIX shell syntax, but are actually
720 # bash scripts, and are meant to be used only with bash on Windows.
722 # A test_cmp function that treats LF and CRLF equal and avoids to fork
723 # diff when possible.
725 # Read text into shell variables and compare them. If the results
726 # are different, use regular diff to report the difference.
727 local test_cmp_a= test_cmp_b=
729 # When text came from stdin (one argument is '-') we must feed it
731 local stdin_for_diff=
733 # Since it is difficult to detect the difference between an
734 # empty input file and a failure to read the files, we go straight
735 # to diff if one of the inputs is empty.
736 if test -s "$1" && test -s "$2"
738 # regular case: both files non-empty
739 mingw_read_file_strip_cr_ test_cmp_a <"$1"
740 mingw_read_file_strip_cr_ test_cmp_b <"$2"
741 elif test -s "$1" && test "$2" = -
743 # read 2nd file from stdin
744 mingw_read_file_strip_cr_ test_cmp_a <"$1"
745 mingw_read_file_strip_cr_ test_cmp_b
746 stdin_for_diff='<<<"$test_cmp_b"'
747 elif test "$1" = - && test -s "$2"
749 # read 1st file from stdin
750 mingw_read_file_strip_cr_ test_cmp_a
751 mingw_read_file_strip_cr_ test_cmp_b <"$2"
752 stdin_for_diff='<<<"$test_cmp_a"'
754 test -n "$test_cmp_a" &&
755 test -n "$test_cmp_b" &&
756 test "$test_cmp_a" = "$test_cmp_b" ||
757 eval "diff -u \"\$@\" $stdin_for_diff"
760 # $1 is the name of the shell variable to fill in
761 mingw_read_file_strip_cr_ () {
762 # Read line-wise using LF as the line separator
763 # and use IFS to strip CR.
767 if IFS=$'\r' read -r -d $'\n' line
772 # we get here at EOF, but also if the last line
773 # was not terminated by LF; in the latter case,