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
54 # A regexp to match 5 and 40 hexdigits
55 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
56 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
58 _x04='[0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
59 _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
61 # Each test should start with something like this, after copyright notices:
63 # test_description='Description of this test...
64 # This test checks if command xyzzy does the right thing...
67 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
68 TERM=$ORIGINAL_TERM &&
71 tput bold >/dev/null 2>&1 &&
72 tput setaf 1 >/dev/null 2>&1 &&
73 tput sgr0 >/dev/null 2>&1
80 -d|--d|--de|--deb|--debu|--debug)
82 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
84 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
85 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
86 -h|--h|--he|--hel|--help)
88 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
90 -q|--q|--qu|--qui|--quie|--quiet)
93 with_dashes=t; shift ;;
99 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
100 valgrind=t; verbose=t; shift ;;
102 shift ;; # was handled already
104 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
107 echo "error: unknown test option '$1'" >&2; exit 1 ;;
111 if test -n "$color"; then
117 error) tput bold; tput setaf 1;; # bold red
118 skip) tput bold; tput setaf 2;; # bold green
119 pass) tput setaf 2;; # green
120 info) tput setaf 3;; # brown
121 *) test -n "$quiet" && return;;
131 test -z "$1" && test -n "$quiet" && return
139 say_color error "error: $*"
148 test "${test_description}" != "" ||
149 error "Test script did not set test_description."
151 if test "$help" = "t"
153 echo "Tests ${test_description}"
157 echo "Testing ${test_description}"
160 if test "$verbose" = "t"
164 exec 4>/dev/null 3>/dev/null
175 if test -n "$GIT_EXIT_OK"
179 echo >&5 "FATAL: Unexpected exit with code $code"
187 test_decode_color () {
188 sed -e 's/.\[1m/<WHITE>/g' \
189 -e 's/.\[31m/<RED>/g' \
190 -e 's/.\[32m/<GREEN>/g' \
191 -e 's/.\[33m/<YELLOW>/g' \
192 -e 's/.\[34m/<BLUE>/g' \
193 -e 's/.\[35m/<MAGENTA>/g' \
194 -e 's/.\[36m/<CYAN>/g' \
195 -e 's/.\[m/<RESET>/g'
207 sed -e 's/$/Q/' | tr Q '\015'
211 tr '\015' Q | sed -e 's/Q$//'
214 # Notmuch helper functions
215 increment_mtime_amount=0
220 increment_mtime_amount=$((increment_mtime_amount + 1))
221 touch -d "+${increment_mtime_amount} seconds" "$dir"
224 # Generate a new message in the mail directory, with a unique message
225 # ID and subject. The message is not added to the index.
227 # After this function returns, the filename of the generated message
228 # is available as $gen_msg_filename and the message ID is available as
231 # This function supports named parameters with the bash syntax for
232 # assigning a value to an associative array ([name]=value). The
233 # supported parameters are:
235 # [dir]=directory/of/choice
237 # Generate the message in directory 'directory/of/choice' within
238 # the mail store. The directory will be created if necessary.
242 # Text to use as the body of the email message
244 # '[from]="Some User <user@example.com>"'
245 # '[to]="Some User <user@example.com>"'
246 # '[subject]="Subject of email message"'
247 # '[date]="RFC 822 Date"'
249 # Values for email headers. If not provided, default values will
250 # be generated instead.
252 # '[cc]="Some User <user@example.com>"'
253 # [reply-to]=some-address
254 # [in-reply-to]=<message-id>
255 # [references]=<message-id>
256 # [content-type]=content-type-specification
257 # '[header]=full header line, including keyword'
259 # Additional values for email headers. If these are not provided
260 # then the relevant headers will simply not appear in the
265 # Controls the message-id of the created message.
271 # This is our (bash-specific) magic for doing named parameters
272 local -A template="($@)"
273 local additional_headers
275 gen_msg_cnt=$((gen_msg_cnt + 1))
276 gen_msg_name=msg-$(printf "%03d" $gen_msg_cnt)
278 if [ -z "${template[id]}" ]; then
279 gen_msg_id="${gen_msg_name}@notmuch-test-suite"
281 gen_msg_id="${template[id]}"
284 if [ -z "${template[dir]}" ]; then
285 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
287 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
288 mkdir -p "$(dirname "$gen_msg_filename")"
291 if [ -z "${template[body]}" ]; then
292 template[body]="This is just a test message (#${gen_msg_cnt})"
295 if [ -z "${template[from]}" ]; then
296 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
299 if [ -z "${template[to]}" ]; then
300 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
303 if [ -z "${template[subject]}" ]; then
304 template[subject]="Test message #${gen_msg_cnt}"
307 if [ -z "${template[date]}" ]; then
308 template[date]="Tue, 05 Jan 2001 15:43:57 -0000"
311 additional_headers=""
312 if [ ! -z "${template[header]}" ]; then
313 additional_headers="${template[header]}
314 ${additional_headers}"
317 if [ ! -z "${template[reply-to]}" ]; then
318 additional_headers="Reply-To: ${template[reply-to]}
319 ${additional_headers}"
322 if [ ! -z "${template[in-reply-to]}" ]; then
323 additional_headers="In-Reply-To: ${template[in-reply-to]}
324 ${additional_headers}"
327 if [ ! -z "${template[cc]}" ]; then
328 additional_headers="Cc: ${template[cc]}
329 ${additional_headers}"
332 if [ ! -z "${template[references]}" ]; then
333 additional_headers="References: ${template[references]}
334 ${additional_headers}"
337 if [ ! -z "${template[content-type]}" ]; then
338 additional_headers="Content-Type: ${template[content-type]}
339 ${additional_headers}"
343 cat <<EOF >"$gen_msg_filename"
344 From: ${template[from]}
346 Message-Id: <${gen_msg_id}>
347 Subject: ${template[subject]}
348 Date: ${template[date]}
349 ${additional_headers}
353 # Ensure that the mtime of the containing directory is updated
354 increment_mtime "$(dirname "${gen_msg_filename}")"
357 # Generate a new message and add it to the index.
359 # All of the arguments and return values supported by generate_message
360 # are also supported here, so see that function for details.
363 generate_message "$@" &&
364 notmuch new > /dev/null
367 test_begin_subtest ()
369 test_subtest_name="$1"
372 # Pass test if two arguments match
374 # Note: Unlike all other test_expect_* functions, this function does
375 # not accept a test name. Instead, the caller should call
376 # test_begin_subtest before calling this function in order to set the
380 test "$#" = 3 && { prereq=$1; shift; } || prereq=
382 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
388 say >&3 "expecting success: diff $output $expected"
389 if [ "$output" = "$expected" ]; then
390 test_ok_ "$test_subtest_name"
392 testname=$this_test.$test_count
393 echo "$expected" > $testname.expected
394 echo "$output" > $testname.output
395 test_failure_ "$test_subtest_name" "$(diff -u $testname.expected $testname.output)"
401 TEST_DIR=$(pwd)/test.$$
402 MAIL_DIR=${TEST_DIR}/mail
403 export NOTMUCH_CONFIG=${TEST_DIR}/notmuch-config
408 notmuch new | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
411 notmuch_search_sanitize ()
413 sed -r -e 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
416 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
417 notmuch_show_sanitize ()
419 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
422 # End of notmuch helper functions
424 # Use test_set_prereq to tell that a particular prerequisite is available.
425 # The prerequisite can later be checked for in two ways:
427 # - Explicitly using test_have_prereq.
429 # - Implicitly by specifying the prerequisite tag in the calls to
430 # test_expect_{success,failure,code}.
432 # The single parameter is the prerequisite tag (a simple word, in all
433 # capital letters by convention).
436 satisfied="$satisfied$1 "
440 test_have_prereq () {
449 # You are not expected to call test_ok_ and test_failure_ directly, use
450 # the text_expect_* functions instead.
453 test_success=$(($test_success + 1))
454 say_color pass "%-6s" "PASS"
459 test_failure=$(($test_failure + 1))
460 say_color error "%-6s" "FAIL"
463 echo "$@" | sed -e 's/^/ /'
464 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
467 test_known_broken_ok_ () {
468 test_fixed=$(($test_fixed+1))
469 say_color pass "%-6s" "FIXED"
473 test_known_broken_failure_ () {
474 test_broken=$(($test_broken+1))
475 say_color pass "%-6s" "BROKEN"
480 test "$debug" = "" || eval "$1"
487 eval >&3 2>&4 "$test_cleanup"
492 test_count=$(($test_count+1))
494 for skp in $NOTMUCH_SKIP_TESTS
496 case $this_test.$test_count in
501 if test -z "$to_skip" && test -n "$prereq" &&
502 ! test_have_prereq "$prereq"
508 say_color skip >&3 "skipping test: $@"
509 say_color skip "%-6s" "SKIP"
519 test_expect_failure () {
520 test "$#" = 3 && { prereq=$1; shift; } || prereq=
522 error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
525 say >&3 "checking known breakage: $2"
527 if [ "$?" = 0 -a "$eval_ret" = 0 ]
529 test_known_broken_ok_ "$1"
531 test_known_broken_failure_ "$1"
537 test_expect_success () {
538 test "$#" = 3 && { prereq=$1; shift; } || prereq=
540 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
543 say >&3 "expecting success: $2"
545 if [ "$?" = 0 -a "$eval_ret" = 0 ]
555 test_expect_code () {
556 test "$#" = 4 && { prereq=$1; shift; } || prereq=
558 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
561 say >&3 "expecting exit code $1: $3"
563 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
573 # test_external runs external test scripts that provide continuous
574 # test output about their progress, and succeeds/fails on
575 # zero/non-zero exit code. It outputs the test output on stdout even
576 # in non-verbose mode, and announces the external script with "* run
577 # <n>: ..." before running it. When providing relative paths, keep in
578 # mind that all scripts run in "trash directory".
579 # Usage: test_external description command arguments...
580 # Example: test_external 'Perl API' perl ../path/to/test.pl
582 test "$#" = 4 && { prereq=$1; shift; } || prereq=
584 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
587 if ! test_skip "$descr" "$@"
589 # Announce the script to reduce confusion about the
590 # test output that follows.
591 say_color "" " run $test_count: $descr ($*)"
592 # Run command; redirect its stderr to &4 as in
593 # test_run_, but keep its stdout on our stdout even in
600 test_failure_ "$descr" "$@"
605 # Like test_external, but in addition tests that the command generated
606 # no output on stderr.
607 test_external_without_stderr () {
608 # The temporary file has no (and must have no) security
610 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
611 stderr="$tmp/git-external-stderr.$$.tmp"
612 test_external "$@" 4> "$stderr"
613 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
614 descr="no stderr: $1"
616 say >&3 "expecting no stderr from previous command"
617 if [ ! -s "$stderr" ]; then
621 if [ "$verbose" = t ]; then
622 output=`echo; echo Stderr is:; cat "$stderr"`
626 # rm first in case test_failure exits.
628 test_failure_ "$descr" "$@" "$output"
632 # This is not among top-level (test_expect_success | test_expect_failure)
633 # but is a prefix that can be used in the test script, like:
635 # test_expect_success 'complain and die' '
637 # do something else &&
638 # test_must_fail git checkout ../outerspace
641 # Writing this as "! git checkout ../outerspace" is wrong, because
642 # the failure could be due to a segv. We want a controlled failure.
646 test $? -gt 0 -a $? -le 129 -o $? -gt 192
649 # test_cmp is a helper function to compare actual and expected output.
650 # You can use it like:
652 # test_expect_success 'foo works' '
653 # echo expected >expected &&
655 # test_cmp expected actual
658 # This could be written as either "cmp" or "diff -u", but:
659 # - cmp's output is not nearly as easy to read as diff -u
660 # - not all diff versions understand "-u"
666 # This function can be used to schedule some commands to be run
667 # unconditionally at the end of the test to restore sanity:
669 # test_expect_success 'test core.capslock' '
670 # git config core.capslock true &&
671 # test_when_finished "git config --unset core.capslock" &&
675 # That would be roughly equivalent to
677 # test_expect_success 'test core.capslock' '
678 # git config core.capslock true &&
680 # git config --unset core.capslock
683 # except that the greeting and config --unset must both succeed for
686 test_when_finished () {
688 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
693 test_results_dir="$TEST_DIRECTORY/test-results"
694 mkdir -p "$test_results_dir"
695 test_results_path="$test_results_dir/${0%.sh}-$$"
697 echo "total $test_count" >> $test_results_path
698 echo "success $test_success" >> $test_results_path
699 echo "fixed $test_fixed" >> $test_results_path
700 echo "broken $test_broken" >> $test_results_path
701 echo "failed $test_failure" >> $test_results_path
702 echo "" >> $test_results_path
706 if [ "$test_failure" = "0" ]; then
718 while [ -n "$dir" ]; do
720 if [ -x "$bin" ]; then
724 dir="$(dirname "$dir")"
725 if [ "$dir" = "/" ]; then
731 # Test the binaries we have just built. The tests are kept in
732 # test/ subdirectory and are run in 'trash directory' subdirectory.
733 TEST_DIRECTORY=$(pwd)
734 if test -n "$valgrind"
738 test "$1" = "$(readlink "$2")" || {
746 while test -d "$2".lock
748 say "Waiting for lock on $2."
755 make_valgrind_symlink () {
756 # handle only executables
757 test -x "$1" || return
759 base=$(basename "$1")
760 symlink_target=$TEST_DIRECTORY/../$base
761 # do not override scripts
762 if test -x "$symlink_target" &&
763 test ! -d "$symlink_target" &&
764 test "#!" != "$(head -c 2 < "$symlink_target")"
766 symlink_target=../valgrind.sh
770 symlink_target=../unprocessed-script
772 # create the link, or replace it if it is out of date
773 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
776 # override notmuch executable in TEST_DIRECTORY/..
777 GIT_VALGRIND=$TEST_DIRECTORY/valgrind
778 mkdir -p "$GIT_VALGRIND"/bin
779 make_valgrind_symlink $TEST_DIRECTORY/../notmuch
784 ls "$path"/notmuch 2> /dev/null |
787 make_valgrind_symlink "$file"
791 PATH=$GIT_VALGRIND/bin:$PATH
792 GIT_EXEC_PATH=$GIT_VALGRIND/bin
795 notmuch_path=`find_notmuch_path "$TEST_DIRECTORY"`
796 test -n "$notmuch_path" && PATH="$notmuch_path:$PATH"
801 test="tmp.$(basename "$0" .sh)"
802 test -n "$root" && test="$root/$test"
804 /*) TMP_DIRECTORY="$test" ;;
805 *) TMP_DIRECTORY="$TEST_DIRECTORY/$test" ;;
807 test ! -z "$debug" || remove_tmp=$TMP_DIRECTORY
810 echo >&5 "FATAL: Cannot prepare test area"
814 MAIL_DIR="${TMP_DIRECTORY}/mail"
815 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
820 cat <<EOF >"${NOTMUCH_CONFIG}"
825 name=Notmuch Test Suite
826 primary_email=test_suite@notmuchmail.org
827 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
831 # Use -P to resolve symlinks in our working directory so that the cwd
832 # in subprocesses like git equals our $PWD (for pathname comparisons).
833 cd -P "$test" || error "Cannot setup test environment"
836 this_test=${this_test%%-*}
837 for skp in $NOTMUCH_SKIP_TESTS
840 for skp in $NOTMUCH_SKIP_TESTS
849 say_color skip >&3 "skipping test $this_test altogether"
850 say_color skip "skip all tests in $this_test"
855 # Provide an implementation of the 'yes' utility
870 # Fix some commands on Windows
873 # Windows has its own (incompatible) sort and find
883 # git sees Windows-style pwd
887 # no POSIX permissions
888 # backslashes in pathspec are converted to '/'
889 # exec does not inherit the PID
892 test_set_prereq POSIXPERM
893 test_set_prereq BSLASHPSPEC
894 test_set_prereq EXECKEEPSPID
898 test -z "$NO_PERL" && test_set_prereq PERL
899 test -z "$NO_PYTHON" && test_set_prereq PYTHON
901 # test whether the filesystem supports symbolic links
902 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS