test-lib: reorder and include GIT-BUILD-OPTIONS a lot earlier
[git] / t / test-lib.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
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.
9 #
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.
14 #
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/ .
17
18 # if --tee was passed, write the output not only to the terminal, but
19 # additionally to the file test-results/$BASENAME.out, too.
20 case "$GIT_TEST_TEE_STARTED, $* " in
21 done,*)
22         # do not redirect again
23         ;;
24 *' --tee '*|*' --va'*)
25         mkdir -p test-results
26         BASE=test-results/$(basename "$0" .sh)
27         (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
28          echo $? > $BASE.exit) | tee $BASE.out
29         test "$(cat $BASE.exit)" = 0
30         exit
31         ;;
32 esac
33
34 # Keep the original TERM for say_color
35 ORIGINAL_TERM=$TERM
36
37 # Test the binaries we have just built.  The tests are kept in
38 # t/ subdirectory and are run in 'trash directory' subdirectory.
39 if test -z "$TEST_DIRECTORY"
40 then
41         # We allow tests to override this, in case they want to run tests
42         # outside of t/, e.g. for running tests on the test library
43         # itself.
44         TEST_DIRECTORY=$(pwd)
45 fi
46 if test -z "$TEST_OUTPUT_DIRECTORY"
47 then
48         # Similarly, override this to store the test-results subdir
49         # elsewhere
50         TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
51 fi
52 GIT_BUILD_DIR="$TEST_DIRECTORY"/..
53
54 . "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
55 export PERL_PATH SHELL_PATH
56
57 # For repeatability, reset the environment to known value.
58 LANG=C
59 LC_ALL=C
60 PAGER=cat
61 TZ=UTC
62 TERM=dumb
63 export LANG LC_ALL PAGER TERM TZ
64 EDITOR=:
65 # A call to "unset" with no arguments causes at least Solaris 10
66 # /usr/xpg4/bin/sh and /bin/ksh to bail out.  So keep the unsets
67 # deriving from the command substitution clustered with the other
68 # ones.
69 unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
70         my @env = keys %ENV;
71         my $ok = join("|", qw(
72                 TRACE
73                 DEBUG
74                 USE_LOOKUP
75                 TEST
76                 .*_TEST
77                 PROVE
78                 VALGRIND
79                 PERF_AGGREGATING_LATER
80         ));
81         my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
82         print join("\n", @vars);
83 ')
84 GIT_AUTHOR_EMAIL=author@example.com
85 GIT_AUTHOR_NAME='A U Thor'
86 GIT_COMMITTER_EMAIL=committer@example.com
87 GIT_COMMITTER_NAME='C O Mitter'
88 GIT_MERGE_VERBOSITY=5
89 GIT_MERGE_AUTOEDIT=no
90 export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
91 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
92 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
93 export EDITOR
94
95 # Protect ourselves from common misconfiguration to export
96 # CDPATH into the environment
97 unset CDPATH
98
99 unset GREP_OPTIONS
100
101 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
102         1|2|true)
103                 echo "* warning: Some tests will not work if GIT_TRACE" \
104                         "is set as to trace on STDERR ! *"
105                 echo "* warning: Please set GIT_TRACE to something" \
106                         "other than 1, 2 or true ! *"
107                 ;;
108 esac
109
110 # Convenience
111 #
112 # A regexp to match 5 and 40 hexdigits
113 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
114 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
115
116 # Zero SHA-1
117 _z40=0000000000000000000000000000000000000000
118
119 # Line feed
120 LF='
121 '
122
123 export _x05 _x40 _z40 LF
124
125 # Each test should start with something like this, after copyright notices:
126 #
127 # test_description='Description of this test...
128 # This test checks if command xyzzy does the right thing...
129 # '
130 # . ./test-lib.sh
131 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
132                 TERM=$ORIGINAL_TERM &&
133                 export TERM &&
134                 [ -t 1 ] &&
135                 tput bold >/dev/null 2>&1 &&
136                 tput setaf 1 >/dev/null 2>&1 &&
137                 tput sgr0 >/dev/null 2>&1
138         ) &&
139         color=t
140
141 while test "$#" -ne 0
142 do
143         case "$1" in
144         -d|--d|--de|--deb|--debu|--debug)
145                 debug=t; shift ;;
146         -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
147                 immediate=t; shift ;;
148         -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
149                 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
150         -h|--h|--he|--hel|--help)
151                 help=t; shift ;;
152         -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
153                 verbose=t; shift ;;
154         -q|--q|--qu|--qui|--quie|--quiet)
155                 # Ignore --quiet under a TAP::Harness. Saying how many tests
156                 # passed without the ok/not ok details is always an error.
157                 test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
158         --with-dashes)
159                 with_dashes=t; shift ;;
160         --no-color)
161                 color=; shift ;;
162         --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
163                 valgrind=t; verbose=t; shift ;;
164         --tee)
165                 shift ;; # was handled already
166         --root=*)
167                 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
168                 shift ;;
169         *)
170                 echo "error: unknown test option '$1'" >&2; exit 1 ;;
171         esac
172 done
173
174 if test -n "$color"; then
175         say_color () {
176                 (
177                 TERM=$ORIGINAL_TERM
178                 export TERM
179                 case "$1" in
180                         error) tput bold; tput setaf 1;; # bold red
181                         skip)  tput bold; tput setaf 2;; # bold green
182                         pass)  tput setaf 2;;            # green
183                         info)  tput setaf 3;;            # brown
184                         *) test -n "$quiet" && return;;
185                 esac
186                 shift
187                 printf "%s" "$*"
188                 tput sgr0
189                 echo
190                 )
191         }
192 else
193         say_color() {
194                 test -z "$1" && test -n "$quiet" && return
195                 shift
196                 echo "$*"
197         }
198 fi
199
200 error () {
201         say_color error "error: $*"
202         GIT_EXIT_OK=t
203         exit 1
204 }
205
206 say () {
207         say_color info "$*"
208 }
209
210 test "${test_description}" != "" ||
211 error "Test script did not set test_description."
212
213 if test "$help" = "t"
214 then
215         echo "$test_description"
216         exit 0
217 fi
218
219 exec 5>&1
220 exec 6<&0
221 if test "$verbose" = "t"
222 then
223         exec 4>&2 3>&1
224 else
225         exec 4>/dev/null 3>/dev/null
226 fi
227
228 test_failure=0
229 test_count=0
230 test_fixed=0
231 test_broken=0
232 test_success=0
233
234 test_external_has_tap=0
235
236 die () {
237         code=$?
238         if test -n "$GIT_EXIT_OK"
239         then
240                 exit $code
241         else
242                 echo >&5 "FATAL: Unexpected exit with code $code"
243                 exit 1
244         fi
245 }
246
247 GIT_EXIT_OK=
248 trap 'die' EXIT
249
250 # The user-facing functions are loaded from a separate file so that
251 # test_perf subshells can have them too
252 . "$TEST_DIRECTORY/test-lib-functions.sh"
253
254 # You are not expected to call test_ok_ and test_failure_ directly, use
255 # the text_expect_* functions instead.
256
257 test_ok_ () {
258         test_success=$(($test_success + 1))
259         say_color "" "ok $test_count - $@"
260 }
261
262 test_failure_ () {
263         test_failure=$(($test_failure + 1))
264         say_color error "not ok - $test_count $1"
265         shift
266         echo "$@" | sed -e 's/^/#       /'
267         test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
268 }
269
270 test_known_broken_ok_ () {
271         test_fixed=$(($test_fixed+1))
272         say_color "" "ok $test_count - $@ # TODO known breakage"
273 }
274
275 test_known_broken_failure_ () {
276         test_broken=$(($test_broken+1))
277         say_color skip "not ok $test_count - $@ # TODO known breakage"
278 }
279
280 test_debug () {
281         test "$debug" = "" || eval "$1"
282 }
283
284 test_eval_ () {
285         # This is a separate function because some tests use
286         # "return" to end a test_expect_success block early.
287         eval </dev/null >&3 2>&4 "$*"
288 }
289
290 test_run_ () {
291         test_cleanup=:
292         expecting_failure=$2
293         test_eval_ "$1"
294         eval_ret=$?
295
296         if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
297         then
298                 test_eval_ "$test_cleanup"
299         fi
300         if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
301                 echo ""
302         fi
303         return "$eval_ret"
304 }
305
306 test_skip () {
307         test_count=$(($test_count+1))
308         to_skip=
309         for skp in $GIT_SKIP_TESTS
310         do
311                 case $this_test.$test_count in
312                 $skp)
313                         to_skip=t
314                         break
315                 esac
316         done
317         if test -z "$to_skip" && test -n "$test_prereq" &&
318            ! test_have_prereq "$test_prereq"
319         then
320                 to_skip=t
321         fi
322         case "$to_skip" in
323         t)
324                 of_prereq=
325                 if test "$missing_prereq" != "$test_prereq"
326                 then
327                         of_prereq=" of $test_prereq"
328                 fi
329
330                 say_color skip >&3 "skipping test: $@"
331                 say_color skip "ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
332                 : true
333                 ;;
334         *)
335                 false
336                 ;;
337         esac
338 }
339
340 # stub; perf-lib overrides it
341 test_at_end_hook_ () {
342         :
343 }
344
345 test_done () {
346         GIT_EXIT_OK=t
347
348         if test -z "$HARNESS_ACTIVE"; then
349                 test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
350                 mkdir -p "$test_results_dir"
351                 test_results_path="$test_results_dir/${0%.sh}-$$.counts"
352
353                 cat >>"$test_results_path" <<-EOF
354                 total $test_count
355                 success $test_success
356                 fixed $test_fixed
357                 broken $test_broken
358                 failed $test_failure
359
360                 EOF
361         fi
362
363         if test "$test_fixed" != 0
364         then
365                 say_color pass "# fixed $test_fixed known breakage(s)"
366         fi
367         if test "$test_broken" != 0
368         then
369                 say_color error "# still have $test_broken known breakage(s)"
370                 msg="remaining $(($test_count-$test_broken)) test(s)"
371         else
372                 msg="$test_count test(s)"
373         fi
374         case "$test_failure" in
375         0)
376                 # Maybe print SKIP message
377                 [ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"
378
379                 if test $test_external_has_tap -eq 0; then
380                         say_color pass "# passed all $msg"
381                         say "1..$test_count$skip_all"
382                 fi
383
384                 test -d "$remove_trash" &&
385                 cd "$(dirname "$remove_trash")" &&
386                 rm -rf "$(basename "$remove_trash")"
387
388                 test_at_end_hook_
389
390                 exit 0 ;;
391
392         *)
393                 if test $test_external_has_tap -eq 0; then
394                         say_color error "# failed $test_failure among $msg"
395                         say "1..$test_count"
396                 fi
397
398                 exit 1 ;;
399
400         esac
401 }
402
403 if test -n "$valgrind"
404 then
405         make_symlink () {
406                 test -h "$2" &&
407                 test "$1" = "$(readlink "$2")" || {
408                         # be super paranoid
409                         if mkdir "$2".lock
410                         then
411                                 rm -f "$2" &&
412                                 ln -s "$1" "$2" &&
413                                 rm -r "$2".lock
414                         else
415                                 while test -d "$2".lock
416                                 do
417                                         say "Waiting for lock on $2."
418                                         sleep 1
419                                 done
420                         fi
421                 }
422         }
423
424         make_valgrind_symlink () {
425                 # handle only executables, unless they are shell libraries that
426                 # need to be in the exec-path.  We will just use "#!" as a
427                 # guess for a shell-script, since we have no idea what the user
428                 # may have configured as the shell path.
429                 test -x "$1" ||
430                 test "#!" = "$(head -c 2 <"$1")" ||
431                 return;
432
433                 base=$(basename "$1")
434                 symlink_target=$GIT_BUILD_DIR/$base
435                 # do not override scripts
436                 if test -x "$symlink_target" &&
437                     test ! -d "$symlink_target" &&
438                     test "#!" != "$(head -c 2 < "$symlink_target")"
439                 then
440                         symlink_target=../valgrind.sh
441                 fi
442                 case "$base" in
443                 *.sh|*.perl)
444                         symlink_target=../unprocessed-script
445                 esac
446                 # create the link, or replace it if it is out of date
447                 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
448         }
449
450         # override all git executables in TEST_DIRECTORY/..
451         GIT_VALGRIND=$TEST_DIRECTORY/valgrind
452         mkdir -p "$GIT_VALGRIND"/bin
453         for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/test-*
454         do
455                 make_valgrind_symlink $file
456         done
457         # special-case the mergetools loadables
458         make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
459         OLDIFS=$IFS
460         IFS=:
461         for path in $PATH
462         do
463                 ls "$path"/git-* 2> /dev/null |
464                 while read file
465                 do
466                         make_valgrind_symlink "$file"
467                 done
468         done
469         IFS=$OLDIFS
470         PATH=$GIT_VALGRIND/bin:$PATH
471         GIT_EXEC_PATH=$GIT_VALGRIND/bin
472         export GIT_VALGRIND
473 elif test -n "$GIT_TEST_INSTALLED" ; then
474         GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)  ||
475         error "Cannot run git from $GIT_TEST_INSTALLED."
476         PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH
477         GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
478 else # normal case, use ../bin-wrappers only unless $with_dashes:
479         git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
480         if ! test -x "$git_bin_dir/git" ; then
481                 if test -z "$with_dashes" ; then
482                         say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
483                 fi
484                 with_dashes=t
485         fi
486         PATH="$git_bin_dir:$PATH"
487         GIT_EXEC_PATH=$GIT_BUILD_DIR
488         if test -n "$with_dashes" ; then
489                 PATH="$GIT_BUILD_DIR:$PATH"
490         fi
491 fi
492 GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
493 unset GIT_CONFIG
494 GIT_CONFIG_NOSYSTEM=1
495 GIT_ATTR_NOSYSTEM=1
496 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
497
498 if test -z "$GIT_TEST_CMP"
499 then
500         if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
501         then
502                 GIT_TEST_CMP="$DIFF -c"
503         else
504                 GIT_TEST_CMP="$DIFF -u"
505         fi
506 fi
507
508 GITPERLLIB="$GIT_BUILD_DIR"/perl/blib/lib:"$GIT_BUILD_DIR"/perl/blib/arch/auto/Git
509 export GITPERLLIB
510 test -d "$GIT_BUILD_DIR"/templates/blt || {
511         error "You haven't built things yet, have you?"
512 }
513
514 if test -z "$GIT_TEST_INSTALLED" && test -z "$NO_PYTHON"
515 then
516         GITPYTHONLIB="$GIT_BUILD_DIR/git_remote_helpers/build/lib"
517         export GITPYTHONLIB
518         test -d "$GIT_BUILD_DIR"/git_remote_helpers/build || {
519                 error "You haven't built git_remote_helpers yet, have you?"
520         }
521 fi
522
523 if ! test -x "$GIT_BUILD_DIR"/test-chmtime; then
524         echo >&2 'You need to build test-chmtime:'
525         echo >&2 'Run "make test-chmtime" in the source (toplevel) directory'
526         exit 1
527 fi
528
529 # Test repository
530 test="trash directory.$(basename "$0" .sh)"
531 test -n "$root" && test="$root/$test"
532 case "$test" in
533 /*) TRASH_DIRECTORY="$test" ;;
534  *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$test" ;;
535 esac
536 test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
537 rm -fr "$test" || {
538         GIT_EXIT_OK=t
539         echo >&5 "FATAL: Cannot prepare test area"
540         exit 1
541 }
542
543 HOME="$TRASH_DIRECTORY"
544 export HOME
545
546 if test -z "$TEST_NO_CREATE_REPO"; then
547         test_create_repo "$test"
548 else
549         mkdir -p "$test"
550 fi
551 # Use -P to resolve symlinks in our working directory so that the cwd
552 # in subprocesses like git equals our $PWD (for pathname comparisons).
553 cd -P "$test" || exit 1
554
555 this_test=${0##*/}
556 this_test=${this_test%%-*}
557 for skp in $GIT_SKIP_TESTS
558 do
559         case "$this_test" in
560         $skp)
561                 say_color skip >&3 "skipping test $this_test altogether"
562                 skip_all="skip all tests in $this_test"
563                 test_done
564         esac
565 done
566
567 # Provide an implementation of the 'yes' utility
568 yes () {
569         if test $# = 0
570         then
571                 y=y
572         else
573                 y="$*"
574         fi
575
576         while echo "$y"
577         do
578                 :
579         done
580 }
581
582 # Fix some commands on Windows
583 case $(uname -s) in
584 *MINGW*)
585         # Windows has its own (incompatible) sort and find
586         sort () {
587                 /usr/bin/sort "$@"
588         }
589         find () {
590                 /usr/bin/find "$@"
591         }
592         sum () {
593                 md5sum "$@"
594         }
595         # git sees Windows-style pwd
596         pwd () {
597                 builtin pwd -W
598         }
599         # no POSIX permissions
600         # backslashes in pathspec are converted to '/'
601         # exec does not inherit the PID
602         test_set_prereq MINGW
603         test_set_prereq SED_STRIPS_CR
604         ;;
605 *CYGWIN*)
606         test_set_prereq POSIXPERM
607         test_set_prereq EXECKEEPSPID
608         test_set_prereq NOT_MINGW
609         test_set_prereq SED_STRIPS_CR
610         ;;
611 *)
612         test_set_prereq POSIXPERM
613         test_set_prereq BSLASHPSPEC
614         test_set_prereq EXECKEEPSPID
615         test_set_prereq NOT_MINGW
616         ;;
617 esac
618
619 ( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
620 test -z "$NO_PERL" && test_set_prereq PERL
621 test -z "$NO_PYTHON" && test_set_prereq PYTHON
622 test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
623 test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
624
625 # Can we rely on git's output in the C locale?
626 if test -n "$GETTEXT_POISON"
627 then
628         GIT_GETTEXT_POISON=YesPlease
629         export GIT_GETTEXT_POISON
630         test_set_prereq GETTEXT_POISON
631 else
632         test_set_prereq C_LOCALE_OUTPUT
633 fi
634
635 # Use this instead of test_cmp to compare files that contain expected and
636 # actual output from git commands that can be translated.  When running
637 # under GETTEXT_POISON this pretends that the command produced expected
638 # results.
639 test_i18ncmp () {
640         test -n "$GETTEXT_POISON" || test_cmp "$@"
641 }
642
643 # Use this instead of "grep expected-string actual" to see if the
644 # output from a git command that can be translated either contains an
645 # expected string, or does not contain an unwanted one.  When running
646 # under GETTEXT_POISON this pretends that the command produced expected
647 # results.
648 test_i18ngrep () {
649         if test -n "$GETTEXT_POISON"
650         then
651             : # pretend success
652         elif test "x!" = "x$1"
653         then
654                 shift
655                 ! grep "$@"
656         else
657                 grep "$@"
658         fi
659 }
660
661 # test whether the filesystem supports symbolic links
662 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
663 rm -f y
664
665 # When the tests are run as root, permission tests will report that
666 # things are writable when they shouldn't be.
667 test -w / || test_set_prereq SANITY