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 # 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
22 # do not redirect again
24 *' --tee '*|*' --va'*)
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
34 # Keep the original TERM for say_color
37 # For repeatability, reset the environment to known value.
43 export LANG LC_ALL PAGER TERM TZ
44 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
46 # Protect ourselves from common misconfiguration to export
47 # CDPATH into the environment
52 case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
54 echo "* warning: Some tests will not work if GIT_TRACE" \
55 "is set as to trace on STDERR ! *"
56 echo "* warning: Please set GIT_TRACE to something" \
57 "other than 1, 2 or true ! *"
63 # A regexp to match 5 and 40 hexdigits
64 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
65 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
67 _x04='[0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
68 _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
70 # Each test should start with something like this, after copyright notices:
72 # test_description='Description of this test...
73 # This test checks if command xyzzy does the right thing...
76 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
77 TERM=$ORIGINAL_TERM &&
80 tput bold >/dev/null 2>&1 &&
81 tput setaf 1 >/dev/null 2>&1 &&
82 tput sgr0 >/dev/null 2>&1
89 -d|--d|--de|--deb|--debu|--debug)
91 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
93 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
94 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
95 -h|--h|--he|--hel|--help)
97 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
99 -q|--q|--qu|--qui|--quie|--quiet)
102 with_dashes=t; shift ;;
108 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
109 valgrind=t; verbose=t; shift ;;
111 shift ;; # was handled already
113 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
116 echo "error: unknown test option '$1'" >&2; exit 1 ;;
120 if test -n "$color"; then
126 error) tput bold; tput setaf 1;; # bold red
127 skip) tput bold; tput setaf 2;; # bold green
128 pass) tput setaf 2;; # green
129 info) tput setaf 3;; # brown
130 *) test -n "$quiet" && return;;
140 test -z "$1" && test -n "$quiet" && return
148 say_color error "error: $*"
157 test "${test_description}" != "" ||
158 error "Test script did not set test_description."
160 if test "$help" = "t"
162 echo "Tests ${test_description}"
166 echo "Testing ${test_description}"
169 if test "$verbose" = "t"
173 exec 4>/dev/null 3>/dev/null
184 if test -n "$GIT_EXIT_OK"
188 echo >&5 "FATAL: Unexpected exit with code $code"
196 test_decode_color () {
197 sed -e 's/.\[1m/<WHITE>/g' \
198 -e 's/.\[31m/<RED>/g' \
199 -e 's/.\[32m/<GREEN>/g' \
200 -e 's/.\[33m/<YELLOW>/g' \
201 -e 's/.\[34m/<BLUE>/g' \
202 -e 's/.\[35m/<MAGENTA>/g' \
203 -e 's/.\[36m/<CYAN>/g' \
204 -e 's/.\[m/<RESET>/g'
216 sed -e 's/$/Q/' | tr Q '\015'
220 tr '\015' Q | sed -e 's/Q$//'
223 # Notmuch helper functions
224 increment_mtime_amount=0
229 increment_mtime_amount=$((increment_mtime_amount + 1))
230 touch -d "+${increment_mtime_amount} seconds" "$dir"
233 # Generate a new message in the mail directory, with a unique message
234 # ID and subject. The message is not added to the index.
236 # After this function returns, the filename of the generated message
237 # is available as $gen_msg_filename and the message ID is available as
240 # This function supports named parameters with the bash syntax for
241 # assigning a value to an associative array ([name]=value). The
242 # supported parameters are:
244 # [dir]=directory/of/choice
246 # Generate the message in directory 'directory/of/choice' within
247 # the mail store. The directory will be created if necessary.
251 # Text to use as the body of the email message
253 # '[from]="Some User <user@example.com>"'
254 # '[to]="Some User <user@example.com>"'
255 # '[subject]="Subject of email message"'
256 # '[date]="RFC 822 Date"'
258 # Values for email headers. If not provided, default values will
259 # be generated instead.
261 # '[cc]="Some User <user@example.com>"'
262 # [reply-to]=some-address
263 # [in-reply-to]=<message-id>
264 # [references]=<message-id>
265 # [content-type]=content-type-specification
266 # '[header]=full header line, including keyword'
268 # Additional values for email headers. If these are not provided
269 # then the relevant headers will simply not appear in the
274 # Controls the message-id of the created message.
280 # This is our (bash-specific) magic for doing named parameters
281 local -A template="($@)"
282 local additional_headers
284 gen_msg_cnt=$((gen_msg_cnt + 1))
285 gen_msg_name=msg-$(printf "%03d" $gen_msg_cnt)
287 if [ -z "${template[id]}" ]; then
288 gen_msg_id="${gen_msg_name}@notmuch-test-suite"
290 gen_msg_id="${template[id]}"
293 if [ -z "${template[dir]}" ]; then
294 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
296 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
297 mkdir -p "$(dirname "$gen_msg_filename")"
300 if [ -z "${template[body]}" ]; then
301 template[body]="This is just a test message (#${gen_msg_cnt})"
304 if [ -z "${template[from]}" ]; then
305 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
308 if [ -z "${template[to]}" ]; then
309 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
312 if [ -z "${template[subject]}" ]; then
313 template[subject]="Test message #${gen_msg_cnt}"
316 if [ -z "${template[date]}" ]; then
317 template[date]="Tue, 05 Jan 2001 15:43:57 -0000"
320 additional_headers=""
321 if [ ! -z "${template[header]}" ]; then
322 additional_headers="${template[header]}
323 ${additional_headers}"
326 if [ ! -z "${template[reply-to]}" ]; then
327 additional_headers="Reply-To: ${template[reply-to]}
328 ${additional_headers}"
331 if [ ! -z "${template[in-reply-to]}" ]; then
332 additional_headers="In-Reply-To: ${template[in-reply-to]}
333 ${additional_headers}"
336 if [ ! -z "${template[cc]}" ]; then
337 additional_headers="Cc: ${template[cc]}
338 ${additional_headers}"
341 if [ ! -z "${template[references]}" ]; then
342 additional_headers="References: ${template[references]}
343 ${additional_headers}"
346 if [ ! -z "${template[content-type]}" ]; then
347 additional_headers="Content-Type: ${template[content-type]}
348 ${additional_headers}"
352 cat <<EOF >"$gen_msg_filename"
353 From: ${template[from]}
355 Message-Id: <${gen_msg_id}>
356 Subject: ${template[subject]}
357 Date: ${template[date]}
358 ${additional_headers}
362 # Ensure that the mtime of the containing directory is updated
363 increment_mtime "$(dirname "${gen_msg_filename}")"
366 # Generate a new message and add it to the index.
368 # All of the arguments and return values supported by generate_message
369 # are also supported here, so see that function for details.
372 generate_message "$@" &&
373 notmuch new > /dev/null
376 test_begin_subtest ()
378 test_subtest_name="$1"
381 # Pass test if two arguments match
383 # Note: Unlike all other test_expect_* functions, this function does
384 # not accept a test name. Instead, the caller should call
385 # test_begin_subtest before calling this function in order to set the
389 test "$#" = 3 && { prereq=$1; shift; } || prereq=
391 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
397 say >&3 "expecting success: diff $output $expected"
398 if [ "$output" = "$expected" ]; then
399 test_ok_ "$test_subtest_name"
401 testname=$this_test.$test_count
402 echo "$expected" > $testname.expected
403 echo "$output" > $testname.output
404 test_failure_ "$test_subtest_name" "$(diff -u $testname.expected $testname.output)"
410 TEST_DIR=$(pwd)/test.$$
411 MAIL_DIR=${TEST_DIR}/mail
412 export NOTMUCH_CONFIG=${TEST_DIR}/notmuch-config
417 notmuch new | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
420 notmuch_search_sanitize ()
422 sed -r -e 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
425 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
426 notmuch_show_sanitize ()
428 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
431 # End of notmuch helper functions
433 # Use test_set_prereq to tell that a particular prerequisite is available.
434 # The prerequisite can later be checked for in two ways:
436 # - Explicitly using test_have_prereq.
438 # - Implicitly by specifying the prerequisite tag in the calls to
439 # test_expect_{success,failure,code}.
441 # The single parameter is the prerequisite tag (a simple word, in all
442 # capital letters by convention).
445 satisfied="$satisfied$1 "
449 test_have_prereq () {
458 # You are not expected to call test_ok_ and test_failure_ directly, use
459 # the text_expect_* functions instead.
462 test_success=$(($test_success + 1))
463 say_color pass "%-6s" "PASS"
468 test_failure=$(($test_failure + 1))
469 say_color error "%-6s" "FAIL"
472 echo "$@" | sed -e 's/^/ /'
473 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
476 test_known_broken_ok_ () {
477 test_fixed=$(($test_fixed+1))
478 say_color pass "%-6s" "FIXED"
482 test_known_broken_failure_ () {
483 test_broken=$(($test_broken+1))
484 say_color pass "%-6s" "BROKEN"
489 test "$debug" = "" || eval "$1"
496 eval >&3 2>&4 "$test_cleanup"
501 test_count=$(($test_count+1))
503 for skp in $GIT_SKIP_TESTS
505 case $this_test.$test_count in
510 if test -z "$to_skip" && test -n "$prereq" &&
511 ! test_have_prereq "$prereq"
517 say_color skip >&3 "skipping test: $@"
518 say_color skip "%-6s" "SKIP"
528 test_expect_failure () {
529 test "$#" = 3 && { prereq=$1; shift; } || prereq=
531 error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
534 say >&3 "checking known breakage: $2"
536 if [ "$?" = 0 -a "$eval_ret" = 0 ]
538 test_known_broken_ok_ "$1"
540 test_known_broken_failure_ "$1"
546 test_expect_success () {
547 test "$#" = 3 && { prereq=$1; shift; } || prereq=
549 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
552 say >&3 "expecting success: $2"
554 if [ "$?" = 0 -a "$eval_ret" = 0 ]
564 test_expect_code () {
565 test "$#" = 4 && { prereq=$1; shift; } || prereq=
567 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
570 say >&3 "expecting exit code $1: $3"
572 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
582 # test_external runs external test scripts that provide continuous
583 # test output about their progress, and succeeds/fails on
584 # zero/non-zero exit code. It outputs the test output on stdout even
585 # in non-verbose mode, and announces the external script with "* run
586 # <n>: ..." before running it. When providing relative paths, keep in
587 # mind that all scripts run in "trash directory".
588 # Usage: test_external description command arguments...
589 # Example: test_external 'Perl API' perl ../path/to/test.pl
591 test "$#" = 4 && { prereq=$1; shift; } || prereq=
593 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
596 if ! test_skip "$descr" "$@"
598 # Announce the script to reduce confusion about the
599 # test output that follows.
600 say_color "" " run $test_count: $descr ($*)"
601 # Run command; redirect its stderr to &4 as in
602 # test_run_, but keep its stdout on our stdout even in
609 test_failure_ "$descr" "$@"
614 # Like test_external, but in addition tests that the command generated
615 # no output on stderr.
616 test_external_without_stderr () {
617 # The temporary file has no (and must have no) security
619 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
620 stderr="$tmp/git-external-stderr.$$.tmp"
621 test_external "$@" 4> "$stderr"
622 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
623 descr="no stderr: $1"
625 say >&3 "expecting no stderr from previous command"
626 if [ ! -s "$stderr" ]; then
630 if [ "$verbose" = t ]; then
631 output=`echo; echo Stderr is:; cat "$stderr"`
635 # rm first in case test_failure exits.
637 test_failure_ "$descr" "$@" "$output"
641 # This is not among top-level (test_expect_success | test_expect_failure)
642 # but is a prefix that can be used in the test script, like:
644 # test_expect_success 'complain and die' '
646 # do something else &&
647 # test_must_fail git checkout ../outerspace
650 # Writing this as "! git checkout ../outerspace" is wrong, because
651 # the failure could be due to a segv. We want a controlled failure.
655 test $? -gt 0 -a $? -le 129 -o $? -gt 192
658 # test_cmp is a helper function to compare actual and expected output.
659 # You can use it like:
661 # test_expect_success 'foo works' '
662 # echo expected >expected &&
664 # test_cmp expected actual
667 # This could be written as either "cmp" or "diff -u", but:
668 # - cmp's output is not nearly as easy to read as diff -u
669 # - not all diff versions understand "-u"
675 # This function can be used to schedule some commands to be run
676 # unconditionally at the end of the test to restore sanity:
678 # test_expect_success 'test core.capslock' '
679 # git config core.capslock true &&
680 # test_when_finished "git config --unset core.capslock" &&
684 # That would be roughly equivalent to
686 # test_expect_success 'test core.capslock' '
687 # git config core.capslock true &&
689 # git config --unset core.capslock
692 # except that the greeting and config --unset must both succeed for
695 test_when_finished () {
697 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
702 test_results_dir="$TEST_DIRECTORY/test-results"
703 mkdir -p "$test_results_dir"
704 test_results_path="$test_results_dir/${0%.sh}-$$"
706 echo "total $test_count" >> $test_results_path
707 echo "success $test_success" >> $test_results_path
708 echo "fixed $test_fixed" >> $test_results_path
709 echo "broken $test_broken" >> $test_results_path
710 echo "failed $test_failure" >> $test_results_path
711 echo "" >> $test_results_path
715 if [ "$test_failure" = "0" ]; then
727 while [ -n "$dir" ]; do
729 if [ -x "$bin" ]; then
733 dir="$(dirname "$dir")"
734 if [ "$dir" = "/" ]; then
740 # Test the binaries we have just built. The tests are kept in
741 # test/ subdirectory and are run in 'trash directory' subdirectory.
742 TEST_DIRECTORY=$(pwd)
743 # FIXME: Only the normal case bellow is updated to notmuch
744 if test -n "$valgrind"
748 test "$1" = "$(readlink "$2")" || {
756 while test -d "$2".lock
758 say "Waiting for lock on $2."
765 make_valgrind_symlink () {
766 # handle only executables
767 test -x "$1" || return
769 base=$(basename "$1")
770 symlink_target=$TEST_DIRECTORY/../$base
771 # do not override scripts
772 if test -x "$symlink_target" &&
773 test ! -d "$symlink_target" &&
774 test "#!" != "$(head -c 2 < "$symlink_target")"
776 symlink_target=../valgrind.sh
780 symlink_target=../unprocessed-script
782 # create the link, or replace it if it is out of date
783 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
786 # override all git executables in TEST_DIRECTORY/..
787 GIT_VALGRIND=$TEST_DIRECTORY/valgrind
788 mkdir -p "$GIT_VALGRIND"/bin
789 for file in $TEST_DIRECTORY/../git* $TEST_DIRECTORY/../test-*
791 make_valgrind_symlink $file
797 ls "$path"/git-* 2> /dev/null |
800 make_valgrind_symlink "$file"
804 PATH=$GIT_VALGRIND/bin:$PATH
805 GIT_EXEC_PATH=$GIT_VALGRIND/bin
807 elif test -n "$GIT_TEST_INSTALLED" ; then
808 GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path) ||
809 error "Cannot run git from $GIT_TEST_INSTALLED."
810 PATH=$GIT_TEST_INSTALLED:$TEST_DIRECTORY/..:$PATH
811 GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
813 notmuch_path=`find_notmuch_path "$TEST_DIRECTORY"`
814 test -n "$notmuch_path" && PATH="$notmuch_path:$PATH"
819 test="tmp.$(basename "$0" .sh)"
820 test -n "$root" && test="$root/$test"
822 /*) TMP_DIRECTORY="$test" ;;
823 *) TMP_DIRECTORY="$TEST_DIRECTORY/$test" ;;
825 test ! -z "$debug" || remove_tmp=$TMP_DIRECTORY
828 echo >&5 "FATAL: Cannot prepare test area"
832 MAIL_DIR="${TMP_DIRECTORY}/mail"
833 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
838 cat <<EOF >"${NOTMUCH_CONFIG}"
843 name=Notmuch Test Suite
844 primary_email=test_suite@notmuchmail.org
845 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
849 # Use -P to resolve symlinks in our working directory so that the cwd
850 # in subprocesses like git equals our $PWD (for pathname comparisons).
851 cd -P "$test" || error "Cannot setup test environment"
854 this_test=${this_test%%-*}
855 for skp in $GIT_SKIP_TESTS
858 for skp in $GIT_SKIP_TESTS
867 say_color skip >&3 "skipping test $this_test altogether"
868 say_color skip "skip all tests in $this_test"
873 # Provide an implementation of the 'yes' utility
888 # Fix some commands on Windows
891 # Windows has its own (incompatible) sort and find
901 # git sees Windows-style pwd
905 # no POSIX permissions
906 # backslashes in pathspec are converted to '/'
907 # exec does not inherit the PID
910 test_set_prereq POSIXPERM
911 test_set_prereq BSLASHPSPEC
912 test_set_prereq EXECKEEPSPID
916 test -z "$NO_PERL" && test_set_prereq PERL
917 test -z "$NO_PYTHON" && test_set_prereq PYTHON
919 # test whether the filesystem supports symbolic links
920 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS