Merge branch 'jc/test-say-color-avoid-echo-escape' into maint
[git.git] / t / test-lib.sh
blob489bc80fc1b1ba4066a1fd5ad6d2024d67160ba7
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see http://www.gnu.org/licenses/ .
18 # Keep the original TERM for say_color
19 ORIGINAL_TERM=$TERM
21 # Test the binaries we have just built. The tests are kept in
22 # t/ subdirectory and are run in 'trash directory' subdirectory.
23 if test -z "$TEST_DIRECTORY"
24 then
25 # We allow tests to override this, in case they want to run tests
26 # outside of t/, e.g. for running tests on the test library
27 # itself.
28 TEST_DIRECTORY=$(pwd)
30 if test -z "$TEST_OUTPUT_DIRECTORY"
31 then
32 # Similarly, override this to store the test-results subdir
33 # elsewhere
34 TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
36 GIT_BUILD_DIR="$TEST_DIRECTORY"/..
38 ################################################################
39 # It appears that people try to run tests without building...
40 "$GIT_BUILD_DIR/git" >/dev/null
41 if test $? != 1
42 then
43 echo >&2 'error: you do not seem to have built git yet.'
44 exit 1
47 . "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
48 export PERL_PATH SHELL_PATH
50 # if --tee was passed, write the output not only to the terminal, but
51 # additionally to the file test-results/$BASENAME.out, too.
52 case "$GIT_TEST_TEE_STARTED, $* " in
53 done,*)
54 # do not redirect again
56 *' --tee '*|*' --va'*)
57 mkdir -p test-results
58 BASE=test-results/$(basename "$0" .sh)
59 (GIT_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1;
60 echo $? > $BASE.exit) | tee $BASE.out
61 test "$(cat $BASE.exit)" = 0
62 exit
64 esac
66 # For repeatability, reset the environment to known value.
67 LANG=C
68 LC_ALL=C
69 PAGER=cat
70 TZ=UTC
71 TERM=dumb
72 export LANG LC_ALL PAGER TERM TZ
73 EDITOR=:
74 # A call to "unset" with no arguments causes at least Solaris 10
75 # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
76 # deriving from the command substitution clustered with the other
77 # ones.
78 unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
79 my @env = keys %ENV;
80 my $ok = join("|", qw(
81 TRACE
82 DEBUG
83 USE_LOOKUP
84 TEST
85 .*_TEST
86 PROVE
87 VALGRIND
88 PERF_AGGREGATING_LATER
89 ));
90 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
91 print join("\n", @vars);
93 unset XDG_CONFIG_HOME
94 GIT_AUTHOR_EMAIL=author@example.com
95 GIT_AUTHOR_NAME='A U Thor'
96 GIT_COMMITTER_EMAIL=committer@example.com
97 GIT_COMMITTER_NAME='C O Mitter'
98 GIT_MERGE_VERBOSITY=5
99 GIT_MERGE_AUTOEDIT=no
100 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
101 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
102 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
103 export EDITOR
105 # Add libc MALLOC and MALLOC_PERTURB test
106 # only if we are not executing the test with valgrind
107 if expr " $GIT_TEST_OPTS " : ".* --valgrind " >/dev/null ||
108 test -n "$TEST_NO_MALLOC_CHECK"
109 then
110 setup_malloc_check () {
111 : nothing
113 teardown_malloc_check () {
114 : nothing
116 else
117 setup_malloc_check () {
118 MALLOC_CHECK_=3 MALLOC_PERTURB_=165
119 export MALLOC_CHECK_ MALLOC_PERTURB_
121 teardown_malloc_check () {
122 unset MALLOC_CHECK_ MALLOC_PERTURB_
126 # Protect ourselves from common misconfiguration to export
127 # CDPATH into the environment
128 unset CDPATH
130 unset GREP_OPTIONS
132 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
133 1|2|true)
134 echo "* warning: Some tests will not work if GIT_TRACE" \
135 "is set as to trace on STDERR ! *"
136 echo "* warning: Please set GIT_TRACE to something" \
137 "other than 1, 2 or true ! *"
139 esac
141 # Convenience
143 # A regexp to match 5 and 40 hexdigits
144 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
145 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
147 # Zero SHA-1
148 _z40=0000000000000000000000000000000000000000
150 # Line feed
151 LF='
154 export _x05 _x40 _z40 LF
156 # Each test should start with something like this, after copyright notices:
158 # test_description='Description of this test...
159 # This test checks if command xyzzy does the right thing...
161 # . ./test-lib.sh
162 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
163 TERM=$ORIGINAL_TERM &&
164 export TERM &&
165 [ -t 1 ] &&
166 tput bold >/dev/null 2>&1 &&
167 tput setaf 1 >/dev/null 2>&1 &&
168 tput sgr0 >/dev/null 2>&1
169 ) &&
170 color=t
172 while test "$#" -ne 0
174 case "$1" in
175 -d|--d|--de|--deb|--debu|--debug)
176 debug=t; shift ;;
177 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
178 immediate=t; shift ;;
179 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
180 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
181 -h|--h|--he|--hel|--help)
182 help=t; shift ;;
183 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
184 verbose=t; shift ;;
185 -q|--q|--qu|--qui|--quie|--quiet)
186 # Ignore --quiet under a TAP::Harness. Saying how many tests
187 # passed without the ok/not ok details is always an error.
188 test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
189 --with-dashes)
190 with_dashes=t; shift ;;
191 --no-color)
192 color=; shift ;;
193 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
194 valgrind=t; verbose=t; shift ;;
195 --tee)
196 shift ;; # was handled already
197 --root=*)
198 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
199 shift ;;
201 echo "error: unknown test option '$1'" >&2; exit 1 ;;
202 esac
203 done
205 if test -n "$color"
206 then
207 say_color () {
209 TERM=$ORIGINAL_TERM
210 export TERM
211 case "$1" in
212 error)
213 tput bold; tput setaf 1;; # bold red
214 skip)
215 tput bold; tput setaf 2;; # bold green
216 pass)
217 tput setaf 2;; # green
218 info)
219 tput setaf 3;; # brown
221 test -n "$quiet" && return;;
222 esac
223 shift
224 printf "%s" "$*"
225 tput sgr0
226 echo
229 else
230 say_color() {
231 test -z "$1" && test -n "$quiet" && return
232 shift
233 printf "%s\n" "$*"
237 error () {
238 say_color error "error: $*"
239 GIT_EXIT_OK=t
240 exit 1
243 say () {
244 say_color info "$*"
247 test "${test_description}" != "" ||
248 error "Test script did not set test_description."
250 if test "$help" = "t"
251 then
252 echo "$test_description"
253 exit 0
256 exec 5>&1
257 exec 6<&0
258 if test "$verbose" = "t"
259 then
260 exec 4>&2 3>&1
261 else
262 exec 4>/dev/null 3>/dev/null
265 test_failure=0
266 test_count=0
267 test_fixed=0
268 test_broken=0
269 test_success=0
271 test_external_has_tap=0
273 die () {
274 code=$?
275 if test -n "$GIT_EXIT_OK"
276 then
277 exit $code
278 else
279 echo >&5 "FATAL: Unexpected exit with code $code"
280 exit 1
284 GIT_EXIT_OK=
285 trap 'die' EXIT
287 # The user-facing functions are loaded from a separate file so that
288 # test_perf subshells can have them too
289 . "$TEST_DIRECTORY/test-lib-functions.sh"
291 # You are not expected to call test_ok_ and test_failure_ directly, use
292 # the text_expect_* functions instead.
294 test_ok_ () {
295 test_success=$(($test_success + 1))
296 say_color "" "ok $test_count - $@"
299 test_failure_ () {
300 test_failure=$(($test_failure + 1))
301 say_color error "not ok - $test_count $1"
302 shift
303 echo "$@" | sed -e 's/^/# /'
304 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
307 test_known_broken_ok_ () {
308 test_fixed=$(($test_fixed+1))
309 say_color "" "ok $test_count - $@ # TODO known breakage"
312 test_known_broken_failure_ () {
313 test_broken=$(($test_broken+1))
314 say_color skip "not ok $test_count - $@ # TODO known breakage"
317 test_debug () {
318 test "$debug" = "" || eval "$1"
321 test_eval_ () {
322 # This is a separate function because some tests use
323 # "return" to end a test_expect_success block early.
324 eval </dev/null >&3 2>&4 "$*"
327 test_run_ () {
328 test_cleanup=:
329 expecting_failure=$2
330 test_eval_ "$1"
331 eval_ret=$?
333 if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
334 then
335 setup_malloc_check
336 test_eval_ "$test_cleanup"
337 teardown_malloc_check
339 if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
340 then
341 echo ""
343 return "$eval_ret"
346 test_skip () {
347 test_count=$(($test_count+1))
348 to_skip=
349 for skp in $GIT_SKIP_TESTS
351 case $this_test.$test_count in
352 $skp)
353 to_skip=t
354 break
355 esac
356 done
357 if test -z "$to_skip" && test -n "$test_prereq" &&
358 ! test_have_prereq "$test_prereq"
359 then
360 to_skip=t
362 case "$to_skip" in
364 of_prereq=
365 if test "$missing_prereq" != "$test_prereq"
366 then
367 of_prereq=" of $test_prereq"
370 say_color skip >&3 "skipping test: $@"
371 say_color skip "ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
372 : true
375 false
377 esac
380 # stub; perf-lib overrides it
381 test_at_end_hook_ () {
385 test_done () {
386 GIT_EXIT_OK=t
388 if test -z "$HARNESS_ACTIVE"
389 then
390 test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
391 mkdir -p "$test_results_dir"
392 test_results_path="$test_results_dir/${0%.sh}-$$.counts"
394 cat >>"$test_results_path" <<-EOF
395 total $test_count
396 success $test_success
397 fixed $test_fixed
398 broken $test_broken
399 failed $test_failure
404 if test "$test_fixed" != 0
405 then
406 say_color pass "# fixed $test_fixed known breakage(s)"
408 if test "$test_broken" != 0
409 then
410 say_color error "# still have $test_broken known breakage(s)"
411 msg="remaining $(($test_count-$test_broken)) test(s)"
412 else
413 msg="$test_count test(s)"
415 case "$test_failure" in
417 # Maybe print SKIP message
418 if test -n "$skip_all" && test $test_count -gt 0
419 then
420 error "Can't use skip_all after running some tests"
422 [ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"
424 if test $test_external_has_tap -eq 0
425 then
426 if test $test_count -gt 0
427 then
428 say_color pass "# passed all $msg"
430 say "1..$test_count$skip_all"
433 test -d "$remove_trash" &&
434 cd "$(dirname "$remove_trash")" &&
435 rm -rf "$(basename "$remove_trash")"
437 test_at_end_hook_
439 exit 0 ;;
442 if test $test_external_has_tap -eq 0
443 then
444 say_color error "# failed $test_failure among $msg"
445 say "1..$test_count"
448 exit 1 ;;
450 esac
453 if test -n "$valgrind"
454 then
455 make_symlink () {
456 test -h "$2" &&
457 test "$1" = "$(readlink "$2")" || {
458 # be super paranoid
459 if mkdir "$2".lock
460 then
461 rm -f "$2" &&
462 ln -s "$1" "$2" &&
463 rm -r "$2".lock
464 else
465 while test -d "$2".lock
467 say "Waiting for lock on $2."
468 sleep 1
469 done
474 make_valgrind_symlink () {
475 # handle only executables, unless they are shell libraries that
476 # need to be in the exec-path. We will just use "#!" as a
477 # guess for a shell-script, since we have no idea what the user
478 # may have configured as the shell path.
479 test -x "$1" ||
480 test "#!" = "$(head -c 2 <"$1")" ||
481 return;
483 base=$(basename "$1")
484 symlink_target=$GIT_BUILD_DIR/$base
485 # do not override scripts
486 if test -x "$symlink_target" &&
487 test ! -d "$symlink_target" &&
488 test "#!" != "$(head -c 2 < "$symlink_target")"
489 then
490 symlink_target=../valgrind.sh
492 case "$base" in
493 *.sh|*.perl)
494 symlink_target=../unprocessed-script
495 esac
496 # create the link, or replace it if it is out of date
497 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
500 # override all git executables in TEST_DIRECTORY/..
501 GIT_VALGRIND=$TEST_DIRECTORY/valgrind
502 mkdir -p "$GIT_VALGRIND"/bin
503 for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/test-*
505 make_valgrind_symlink $file
506 done
507 # special-case the mergetools loadables
508 make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
509 OLDIFS=$IFS
510 IFS=:
511 for path in $PATH
513 ls "$path"/git-* 2> /dev/null |
514 while read file
516 make_valgrind_symlink "$file"
517 done
518 done
519 IFS=$OLDIFS
520 PATH=$GIT_VALGRIND/bin:$PATH
521 GIT_EXEC_PATH=$GIT_VALGRIND/bin
522 export GIT_VALGRIND
523 elif test -n "$GIT_TEST_INSTALLED"
524 then
525 GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path) ||
526 error "Cannot run git from $GIT_TEST_INSTALLED."
527 PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH
528 GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
529 else # normal case, use ../bin-wrappers only unless $with_dashes:
530 git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
531 if ! test -x "$git_bin_dir/git"
532 then
533 if test -z "$with_dashes"
534 then
535 say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
537 with_dashes=t
539 PATH="$git_bin_dir:$PATH"
540 GIT_EXEC_PATH=$GIT_BUILD_DIR
541 if test -n "$with_dashes"
542 then
543 PATH="$GIT_BUILD_DIR:$PATH"
546 GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
547 unset GIT_CONFIG
548 GIT_CONFIG_NOSYSTEM=1
549 GIT_ATTR_NOSYSTEM=1
550 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
552 if test -z "$GIT_TEST_CMP"
553 then
554 if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
555 then
556 GIT_TEST_CMP="$DIFF -c"
557 else
558 GIT_TEST_CMP="$DIFF -u"
562 GITPERLLIB="$GIT_BUILD_DIR"/perl/blib/lib:"$GIT_BUILD_DIR"/perl/blib/arch/auto/Git
563 export GITPERLLIB
564 test -d "$GIT_BUILD_DIR"/templates/blt || {
565 error "You haven't built things yet, have you?"
568 if test -z "$GIT_TEST_INSTALLED" && test -z "$NO_PYTHON"
569 then
570 GITPYTHONLIB="$GIT_BUILD_DIR/git_remote_helpers/build/lib"
571 export GITPYTHONLIB
572 test -d "$GIT_BUILD_DIR"/git_remote_helpers/build || {
573 error "You haven't built git_remote_helpers yet, have you?"
577 if ! test -x "$GIT_BUILD_DIR"/test-chmtime
578 then
579 echo >&2 'You need to build test-chmtime:'
580 echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
581 exit 1
584 # Test repository
585 test="trash directory.$(basename "$0" .sh)"
586 test -n "$root" && test="$root/$test"
587 case "$test" in
588 /*) TRASH_DIRECTORY="$test" ;;
589 *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$test" ;;
590 esac
591 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
592 rm -fr "$test" || {
593 GIT_EXIT_OK=t
594 echo >&5 "FATAL: Cannot prepare test area"
595 exit 1
598 HOME="$TRASH_DIRECTORY"
599 export HOME
601 if test -z "$TEST_NO_CREATE_REPO"
602 then
603 test_create_repo "$test"
604 else
605 mkdir -p "$test"
607 # Use -P to resolve symlinks in our working directory so that the cwd
608 # in subprocesses like git equals our $PWD (for pathname comparisons).
609 cd -P "$test" || exit 1
611 this_test=${0##*/}
612 this_test=${this_test%%-*}
613 for skp in $GIT_SKIP_TESTS
615 case "$this_test" in
616 $skp)
617 say_color skip >&3 "skipping test $this_test altogether"
618 skip_all="skip all tests in $this_test"
619 test_done
620 esac
621 done
623 # Provide an implementation of the 'yes' utility
624 yes () {
625 if test $# = 0
626 then
628 else
629 y="$*"
632 while echo "$y"
635 done
638 # Fix some commands on Windows
639 case $(uname -s) in
640 *MINGW*)
641 # Windows has its own (incompatible) sort and find
642 sort () {
643 /usr/bin/sort "$@"
645 find () {
646 /usr/bin/find "$@"
648 sum () {
649 md5sum "$@"
651 # git sees Windows-style pwd
652 pwd () {
653 builtin pwd -W
655 # no POSIX permissions
656 # backslashes in pathspec are converted to '/'
657 # exec does not inherit the PID
658 test_set_prereq MINGW
659 test_set_prereq SED_STRIPS_CR
661 *CYGWIN*)
662 test_set_prereq POSIXPERM
663 test_set_prereq EXECKEEPSPID
664 test_set_prereq NOT_MINGW
665 test_set_prereq SED_STRIPS_CR
668 test_set_prereq POSIXPERM
669 test_set_prereq BSLASHPSPEC
670 test_set_prereq EXECKEEPSPID
671 test_set_prereq NOT_MINGW
673 esac
675 ( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
676 test -z "$NO_PERL" && test_set_prereq PERL
677 test -z "$NO_PYTHON" && test_set_prereq PYTHON
678 test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
679 test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
681 # Can we rely on git's output in the C locale?
682 if test -n "$GETTEXT_POISON"
683 then
684 GIT_GETTEXT_POISON=YesPlease
685 export GIT_GETTEXT_POISON
686 test_set_prereq GETTEXT_POISON
687 else
688 test_set_prereq C_LOCALE_OUTPUT
691 # Use this instead of test_cmp to compare files that contain expected and
692 # actual output from git commands that can be translated. When running
693 # under GETTEXT_POISON this pretends that the command produced expected
694 # results.
695 test_i18ncmp () {
696 test -n "$GETTEXT_POISON" || test_cmp "$@"
699 # Use this instead of "grep expected-string actual" to see if the
700 # output from a git command that can be translated either contains an
701 # expected string, or does not contain an unwanted one. When running
702 # under GETTEXT_POISON this pretends that the command produced expected
703 # results.
704 test_i18ngrep () {
705 if test -n "$GETTEXT_POISON"
706 then
707 : # pretend success
708 elif test "x!" = "x$1"
709 then
710 shift
711 ! grep "$@"
712 else
713 grep "$@"
717 test_lazy_prereq SYMLINKS '
718 # test whether the filesystem supports symbolic links
719 ln -s x y && test -h y
722 test_lazy_prereq CASE_INSENSITIVE_FS '
723 echo good >CamelCase &&
724 echo bad >camelcase &&
725 test "$(cat CamelCase)" != good
728 test_lazy_prereq UTF8_NFD_TO_NFC '
729 # check whether FS converts nfd unicode to nfc
730 auml=$(printf "\303\244")
731 aumlcdiar=$(printf "\141\314\210")
732 >"$auml" &&
733 case "$(echo *)" in
734 "$aumlcdiar")
735 true ;;
737 false ;;
738 esac
741 # When the tests are run as root, permission tests will report that
742 # things are writable when they shouldn't be.
743 test -w / || test_set_prereq SANITY