2 # Copyright (c) 2005 Junio C Hamano
3 # Copyright (c) 2010 Notmuch Developers
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 [ ${BASH_VERSINFO[0]} -lt 4 ]; then
19 echo "Error: The notmuch test suite requires a bash version >= 4.0"
20 echo "due to use of associative arrays within the test suite."
21 echo "Please try again with a newer bash (or help us fix the"
22 echo "test suite to be more portable). Thanks."
26 # Make sure echo builtin does not expand backslash-escape sequences by default.
30 this_test=${this_test%.sh}
31 this_test_bare=${this_test#T[0-9][0-9][0-9]-}
33 # if --tee was passed, write the output not only to the terminal, but
34 # additionally to the file test-results/$BASENAME.out, too.
35 case "$GIT_TEST_TEE_STARTED, $* " in
37 # do not redirect again
39 *' --tee '*|*' --va'*)
41 BASE=test-results/$this_test
42 (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
43 echo $? > $BASE.exit) | tee $BASE.out
44 test "$(cat $BASE.exit)" = 0
49 # Save STDOUT to fd 6 and STDERR to fd 7.
51 # Make xtrace debugging (when used) use redirected STDERR, with verbose lead:
53 export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
55 # Keep the original TERM for say_color and test_emacs
58 # dtach(1) provides more capable terminal environment to anything
59 # that requires more than dumb terminal...
60 [ x"${TERM:-dumb}" = xdumb ] && DTACH_TERM=vt100 || DTACH_TERM=$TERM
62 # For repeatability, reset the environment to known value.
68 export LANG LC_ALL PAGER TERM TZ
69 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
70 if [[ ( -n "$TEST_EMACS" && -z "$TEST_EMACSCLIENT" ) || \
71 ( -z "$TEST_EMACS" && -n "$TEST_EMACSCLIENT" ) ]]; then
72 echo "error: must specify both or neither of TEST_EMACS and TEST_EMACSCLIENT" >&2
75 TEST_EMACS=${TEST_EMACS:-${EMACS:-emacs}}
76 TEST_EMACSCLIENT=${TEST_EMACSCLIENT:-emacsclient}
77 TEST_CC=${TEST_CC:-cc}
78 TEST_CFLAGS=${TEST_CFLAGS:-"-g -O0"}
80 # Protect ourselves from common misconfiguration to export
81 # CDPATH into the environment
87 unset ALTERNATE_EDITOR
91 # A regexp to match 5 and 40 hexdigits
92 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
93 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
95 _x04='[0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
96 _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
98 # Each test should start with something like this, after copyright notices:
100 # test_description='Description of this test...
101 # This test checks if command xyzzy does the right thing...
103 # . ./test-lib.sh || exit 1
105 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
106 TERM=$ORIGINAL_TERM &&
109 tput bold >/dev/null 2>&1 &&
110 tput setaf 1 >/dev/null 2>&1 &&
111 tput sgr0 >/dev/null 2>&1
115 while test "$#" -ne 0
118 -d|--d|--de|--deb|--debu|--debug)
120 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
121 immediate=t; shift ;;
122 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
123 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
124 -h|--h|--he|--hel|--help)
126 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
128 -q|--q|--qu|--qui|--quie|--quiet)
131 with_dashes=t; shift ;;
137 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
138 valgrind=t; verbose=t; shift ;;
140 shift ;; # was handled already
142 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
145 echo "error: unknown test option '$1'" >&2; exit 1 ;;
149 if test -n "$debug"; then
151 printf " %-4s" "[$((test_count - 1))]"
159 if test -n "$color"; then
165 error) tput bold; tput setaf 1;; # bold red
166 skip) tput bold; tput setaf 2;; # bold green
167 pass) tput setaf 2;; # green
168 info) tput setaf 3;; # brown
169 *) test -n "$quiet" && return;;
180 test -z "$1" && test -n "$quiet" && return
189 say_color error "error: $*\n"
198 test "${test_description}" != "" ||
199 error "Test script did not set test_description."
201 if test "$help" = "t"
203 echo "Tests ${test_description}"
207 test_description_printed=
208 print_test_description ()
210 test -z "$test_description_printed" || return 0
212 echo $this_test: "Testing ${test_description}"
213 test_description_printed=1
215 if [ -z "$NOTMUCH_TEST_QUIET" ]
217 print_test_description
230 rm -rf "$TEST_TMPDIR"
235 if test -n "$GIT_EXIT_OK"
240 say_color error '%-6s' FATAL
241 echo " $test_subtest_name"
243 echo "Unexpected exit while executing $0. Exit code $code."
250 echo >&6 "FATAL: $0: interrupted by signal" $((code - 128))
255 # Note: TEST_TMPDIR *NOT* exported!
256 TEST_TMPDIR=$(mktemp -d "${TMPDIR:-/tmp}/notmuch-test-$$.XXXXXX")
258 trap 'die_signal' HUP INT TERM
260 test_decode_color () {
261 sed -e 's/.\[1m/<WHITE>/g' \
262 -e 's/.\[31m/<RED>/g' \
263 -e 's/.\[32m/<GREEN>/g' \
264 -e 's/.\[33m/<YELLOW>/g' \
265 -e 's/.\[34m/<BLUE>/g' \
266 -e 's/.\[35m/<MAGENTA>/g' \
267 -e 's/.\[36m/<CYAN>/g' \
268 -e 's/.\[m/<RESET>/g'
280 sed -e 's/$/Q/' | tr Q '\015'
284 tr '\015' Q | sed -e 's/Q$//'
287 # Generate a new message in the mail directory, with a unique message
288 # ID and subject. The message is not added to the index.
290 # After this function returns, the filename of the generated message
291 # is available as $gen_msg_filename and the message ID is available as
294 # This function supports named parameters with the bash syntax for
295 # assigning a value to an associative array ([name]=value). The
296 # supported parameters are:
298 # [dir]=directory/of/choice
300 # Generate the message in directory 'directory/of/choice' within
301 # the mail store. The directory will be created if necessary.
305 # Store the message in file 'name'. The default is to store it
306 # in 'msg-<count>', where <count> is three-digit number of the
311 # Text to use as the body of the email message
313 # '[from]="Some User <user@example.com>"'
314 # '[to]="Some User <user@example.com>"'
315 # '[subject]="Subject of email message"'
316 # '[date]="RFC 822 Date"'
318 # Values for email headers. If not provided, default values will
319 # be generated instead.
321 # '[cc]="Some User <user@example.com>"'
322 # [reply-to]=some-address
323 # [in-reply-to]=<message-id>
324 # [references]=<message-id>
325 # [content-type]=content-type-specification
326 # '[header]=full header line, including keyword'
328 # Additional values for email headers. If these are not provided
329 # then the relevant headers will simply not appear in the
334 # Controls the message-id of the created message.
340 # This is our (bash-specific) magic for doing named parameters
341 local -A template="($@)"
342 local additional_headers
344 gen_msg_cnt=$((gen_msg_cnt + 1))
345 if [ -z "${template[filename]}" ]; then
346 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
348 gen_msg_name=${template[filename]}
351 if [ -z "${template[id]}" ]; then
352 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
354 gen_msg_id="${template[id]}"
357 if [ -z "${template[dir]}" ]; then
358 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
360 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
361 mkdir -p "$(dirname "$gen_msg_filename")"
364 if [ -z "${template[body]}" ]; then
365 template[body]="This is just a test message (#${gen_msg_cnt})"
368 if [ -z "${template[from]}" ]; then
369 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
372 if [ -z "${template[to]}" ]; then
373 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
376 if [ -z "${template[subject]}" ]; then
377 if [ -n "$test_subtest_name" ]; then
378 template[subject]="$test_subtest_name"
380 template[subject]="Test message #${gen_msg_cnt}"
382 elif [ "${template[subject]}" = "@FORCE_EMPTY" ]; then
386 if [ -z "${template[date]}" ]; then
387 # we use decreasing timestamps here for historical reasons;
388 # the existing test suite when we converted to unique timestamps just
389 # happened to have signicantly fewer failures with that choice.
390 local date_secs=$((978709437 - gen_msg_cnt))
391 # printf %(..)T is bash 4.2+ feature. use perl fallback if needed...
392 TZ=UTC printf -v template[date] "%(%a, %d %b %Y %T %z)T" $date_secs 2>/dev/null ||
393 template[date]=`perl -le 'use POSIX "strftime";
394 @time = gmtime '"$date_secs"';
395 print strftime "%a, %d %b %Y %T +0000", @time'`
398 additional_headers=""
399 if [ ! -z "${template[header]}" ]; then
400 additional_headers="${template[header]}
401 ${additional_headers}"
404 if [ ! -z "${template[reply-to]}" ]; then
405 additional_headers="Reply-To: ${template[reply-to]}
406 ${additional_headers}"
409 if [ ! -z "${template[in-reply-to]}" ]; then
410 additional_headers="In-Reply-To: ${template[in-reply-to]}
411 ${additional_headers}"
414 if [ ! -z "${template[cc]}" ]; then
415 additional_headers="Cc: ${template[cc]}
416 ${additional_headers}"
419 if [ ! -z "${template[bcc]}" ]; then
420 additional_headers="Bcc: ${template[bcc]}
421 ${additional_headers}"
424 if [ ! -z "${template[references]}" ]; then
425 additional_headers="References: ${template[references]}
426 ${additional_headers}"
429 if [ ! -z "${template[content-type]}" ]; then
430 additional_headers="Content-Type: ${template[content-type]}
431 ${additional_headers}"
434 if [ ! -z "${template[content-transfer-encoding]}" ]; then
435 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
436 ${additional_headers}"
439 # Note that in the way we're setting it above and using it below,
440 # `additional_headers' will also serve as the header / body separator
441 # (empty line in between).
443 cat <<EOF >"$gen_msg_filename"
444 From: ${template[from]}
446 Message-Id: <${gen_msg_id}>
447 Subject: ${template[subject]}
448 Date: ${template[date]}
449 ${additional_headers}
454 # Generate a new message and add it to the database.
456 # All of the arguments and return values supported by generate_message
457 # are also supported here, so see that function for details.
460 generate_message "$@" &&
461 notmuch new > /dev/null
464 # Deliver a message with emacs and add it to the database
466 # Uses emacs to generate and deliver a message to the mail store.
467 # Accepts arbitrary extra emacs/elisp functions to modify the message
468 # before sending, which is useful to doing things like attaching files
469 # to the message and encrypting/signing.
470 emacs_deliver_message ()
475 # before we can send a message, we have to prepare the FCC maildir
476 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
477 # eval'ing smtp-dummy --background will set smtp_dummy_pid
479 eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
480 test -n "$smtp_dummy_pid" || return 1
483 "(let ((message-send-mail-function 'message-smtpmail-send-it)
484 (mail-host-address \"example.com\")
485 (smtpmail-smtp-server \"localhost\")
486 (smtpmail-smtp-service \"25025\"))
489 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
490 (message-goto-subject)
491 (insert \"${subject}\")
495 (notmuch-mua-send-and-exit))"
497 # In case message was sent properly, client waits for confirmation
498 # before exiting and resuming control here; therefore making sure
499 # that server exits by sending (KILL) signal to it is safe.
500 kill -9 $smtp_dummy_pid
501 notmuch new >/dev/null
504 # Pretend to deliver a message with emacs. Really save it to a file
505 # and add it to the database
507 # Uses emacs to generate and deliver a message to the mail store.
508 # Accepts arbitrary extra emacs/elisp functions to modify the message
509 # before sending, which is useful to doing things like attaching files
510 # to the message and encrypting/signing.
516 # before we can send a message, we have to prepare the FCC maildir
517 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
520 "(let ((message-send-mail-function (lambda () t))
521 (mail-host-address \"example.com\"))
524 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
525 (message-goto-subject)
526 (insert \"${subject}\")
530 (notmuch-mua-send-and-exit))" || return 1
531 notmuch new >/dev/null
534 # Generate a corpus of email and add it to the database.
536 # This corpus is fixed, (it happens to be 50 messages from early in
537 # the history of the notmuch mailing list), which allows for reliably
538 # testing commands that need to operate on a not-totally-trivial
539 # number of messages.
543 if [ -d $TEST_DIRECTORY/corpus.mail ]; then
544 cp -a $TEST_DIRECTORY/corpus.mail ${MAIL_DIR}
546 cp -a $TEST_DIRECTORY/corpus ${MAIL_DIR}
547 notmuch new >/dev/null
548 cp -a ${MAIL_DIR} $TEST_DIRECTORY/corpus.mail
552 test_begin_subtest ()
554 if [ -n "$inside_subtest" ]; then
555 exec 1>&6 2>&7 # Restore stdout and stderr
556 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
558 test_subtest_name="$1"
560 # Redirect test output to the previously prepared file descriptors
561 # 3 and 4 (see below)
562 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
567 # Pass test if two arguments match
569 # Note: Unlike all other test_expect_* functions, this function does
570 # not accept a test name. Instead, the caller should call
571 # test_begin_subtest before calling this function in order to set the
575 exec 1>&6 2>&7 # Restore stdout and stderr
577 test "$#" = 3 && { prereq=$1; shift; } || prereq=
579 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
583 if ! test_skip "$test_subtest_name"
585 if [ "$output" = "$expected" ]; then
588 testname=$this_test.$test_count
589 echo "$expected" > $testname.expected
590 echo "$output" > $testname.output
591 test_failure_ "$(diff -u $testname.expected $testname.output)"
596 # Like test_expect_equal, but takes two filenames.
597 test_expect_equal_file ()
599 exec 1>&6 2>&7 # Restore stdout and stderr
601 test "$#" = 3 && { prereq=$1; shift; } || prereq=
603 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
606 basename1=`basename "$file1"`
608 basename2=`basename "$file2"`
609 if ! test_skip "$test_subtest_name"
611 if diff -q "$file1" "$file2" >/dev/null ; then
614 testname=$this_test.$test_count
615 cp "$file1" "$testname.$basename1"
616 cp "$file2" "$testname.$basename2"
617 test_failure_ "$(diff -u "$testname.$basename1" "$testname.$basename2")"
622 # Like test_expect_equal, but arguments are JSON expressions to be
623 # canonicalized before diff'ing. If an argument cannot be parsed, it
624 # is used unchanged so that there's something to diff against.
625 test_expect_equal_json () {
626 # The test suite forces LC_ALL=C, but this causes Python 3 to
627 # decode stdin as ASCII. We need to read JSON in UTF-8, so
628 # override Python's stdio encoding defaults.
629 output=$(echo "$1" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
631 expected=$(echo "$2" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
634 test_expect_equal "$output" "$expected" "$@"
637 # Sort the top-level list of JSON data from stdin.
639 PYTHONIOENCODING=utf-8 python -c \
640 "import sys, json; json.dump(sorted(json.load(sys.stdin)),sys.stdout)"
643 test_emacs_expect_t () {
644 test "$#" = 2 && { prereq=$1; shift; } || prereq=
646 error "bug in the test script: not 1 or 2 parameters to test_emacs_expect_t"
649 if ! test_skip "$test_subtest_name"
651 test_emacs "(notmuch-test-run $1)" >/dev/null
653 # Restore state after the test.
654 exec 1>&6 2>&7 # Restore stdout and stderr
657 # Report success/failure.
663 test_failure_ "${result}"
666 # Restore state after the (non) test.
667 exec 1>&6 2>&7 # Restore stdout and stderr
674 notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
677 notmuch_search_sanitize ()
679 perl -pe 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
682 notmuch_search_files_sanitize ()
687 notmuch_dir_sanitize ()
689 sed -e "s,$MAIL_DIR,MAIL_DIR," -e "s,${PWD},CWD,g" "$@"
692 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
693 notmuch_show_sanitize ()
695 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
697 notmuch_show_sanitize_all ()
700 -e 's| filename:.*| filename:XXXXX|' \
701 -e 's| id:[^ ]* | id:XXXXX |' | \
702 notmuch_date_sanitize
705 notmuch_json_show_sanitize ()
708 -e 's|"id": "[^"]*",|"id": "XXXXX",|g' \
709 -e 's|"Date": "Fri, 05 Jan 2001 [^"]*0000"|"Date": "GENERATED_DATE"|g' \
710 -e 's|"filename": "signature.asc",||g' \
711 -e 's|"filename": "/[^"]*",|"filename": "YYYYY",|g' \
712 -e 's|"timestamp": 97.......|"timestamp": 42|g'
715 notmuch_emacs_error_sanitize ()
723 -e 's/^\[.*\]$/[XXX]/' \
724 -e "s|^\(command: \)\{0,1\}/.*/$command|\1YYY/$command|"
727 notmuch_date_sanitize ()
730 -e 's/^Date: Fri, 05 Jan 2001 .*0000/Date: GENERATED_DATE/'
733 notmuch_uuid_sanitize ()
735 sed 's/[0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}/UUID/g'
738 notmuch_built_with_sanitize ()
740 sed 's/^built_with[.]\(.*\)=.*$/built_with.\1=something/'
743 # End of notmuch helper functions
745 # Use test_set_prereq to tell that a particular prerequisite is available.
746 # The prerequisite can later be checked for in two ways:
748 # - Explicitly using test_have_prereq.
750 # - Implicitly by specifying the prerequisite tag in the calls to
751 # test_expect_{success,failure,code}.
753 # The single parameter is the prerequisite tag (a simple word, in all
754 # capital letters by convention).
757 satisfied="$satisfied$1 "
761 test_have_prereq () {
770 declare -A test_missing_external_prereq_
771 declare -A test_subtest_missing_external_prereq_
773 # declare prerequisite for the given external binary
774 test_declare_external_prereq () {
776 test "$#" = 2 && name=$2 || name="$binary(1)"
778 if ! hash $binary 2>/dev/null; then
779 test_missing_external_prereq_["${binary}"]=t
782 test_subtest_missing_external_prereq_[\"${name}\"]=t
788 # Explicitly require external prerequisite. Useful when binary is
789 # called indirectly (e.g. from emacs).
790 # Returns success if dependency is available, failure otherwise.
791 test_require_external_prereq () {
793 if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
794 # dependency is missing, call the replacement function to note it
801 # You are not expected to call test_ok_ and test_failure_ directly, use
802 # the text_expect_* functions instead.
805 if test "$test_subtest_known_broken_" = "t"; then
806 test_known_broken_ok_
809 test_success=$(($test_success + 1))
810 if test -n "$NOTMUCH_TEST_QUIET"; then
813 say_color pass "%-6s" "PASS"
814 echo " $test_subtest_name"
818 if test "$test_subtest_known_broken_" = "t"; then
819 test_known_broken_failure_ "$@"
822 test_failure=$(($test_failure + 1))
823 print_test_description
824 test_failure_message_ "FAIL" "$test_subtest_name" "$@"
825 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
829 test_failure_message_ () {
830 say_color error "%-6s" "$1"
833 echo "$@" | sed -e 's/^/ /'
834 if test "$verbose" != "t"; then cat test.output; fi
837 test_known_broken_ok_ () {
839 test_fixed=$(($test_fixed+1))
840 say_color pass "%-6s" "FIXED"
841 echo " $test_subtest_name"
844 test_known_broken_failure_ () {
846 test_broken=$(($test_broken+1))
847 test_failure_message_ "BROKEN" "$test_subtest_name" "$@"
852 test "$debug" = "" || eval "$1"
857 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
860 eval >&3 2>&4 "$test_cleanup"
865 test_count=$(($test_count+1))
867 for skp in $NOTMUCH_SKIP_TESTS
869 case $this_test.$test_count in
874 case $this_test_bare.$test_count in
880 if test -z "$to_skip" && test -n "$prereq" &&
881 ! test_have_prereq "$prereq"
887 test_report_skip_ "$@"
890 test_check_missing_external_prereqs_ "$@"
895 test_check_missing_external_prereqs_ () {
896 if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
897 say_color skip >&1 "missing prerequisites: "
898 echo ${!test_subtest_missing_external_prereq_[@]} >&1
899 test_report_skip_ "$@"
905 test_report_skip_ () {
907 say_color skip >&3 "skipping test:"
909 say_color skip "%-6s" "SKIP"
913 test_subtest_known_broken () {
914 test_subtest_known_broken_=t
917 test_expect_success () {
918 test "$#" = 3 && { prereq=$1; shift; } || prereq=
920 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
921 test_subtest_name="$1"
927 # test_run_ may update missing external prerequisites
928 test_check_missing_external_prereqs_ "$@" ||
929 if [ "$run_ret" = 0 -a "$eval_ret" = 0 ]
938 test_expect_code () {
939 test "$#" = 4 && { prereq=$1; shift; } || prereq=
941 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
942 test_subtest_name="$2"
948 # test_run_ may update missing external prerequisites,
949 test_check_missing_external_prereqs_ "$@" ||
950 if [ "$run_ret" = 0 -a "$eval_ret" = "$1" ]
954 test_failure_ "exit code $eval_ret, expected $1" "$3"
959 # test_external runs external test scripts that provide continuous
960 # test output about their progress, and succeeds/fails on
961 # zero/non-zero exit code. It outputs the test output on stdout even
962 # in non-verbose mode, and announces the external script with "* run
963 # <n>: ..." before running it. When providing relative paths, keep in
964 # mind that all scripts run in "trash directory".
965 # Usage: test_external description command arguments...
966 # Example: test_external 'Perl API' perl ../path/to/test.pl
968 test "$#" = 4 && { prereq=$1; shift; } || prereq=
970 error >&6 "bug in the test script: not 3 or 4 parameters to test_external"
971 test_subtest_name="$1"
974 if ! test_skip "$test_subtest_name" "$@"
976 # Announce the script to reduce confusion about the
977 # test output that follows.
978 say_color "" " run $test_count: $descr ($*)"
979 # Run command; redirect its stderr to &4 as in
980 # test_run_, but keep its stdout on our stdout even in
992 # Like test_external, but in addition tests that the command generated
993 # no output on stderr.
994 test_external_without_stderr () {
995 # The temporary file has no (and must have no) security
997 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
998 stderr="$tmp/git-external-stderr.$$.tmp"
999 test_external "$@" 4> "$stderr"
1000 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
1001 test_subtest_name="no stderr: $1"
1003 if [ ! -s "$stderr" ]; then
1007 if [ "$verbose" = t ]; then
1008 output=`echo; echo Stderr is:; cat "$stderr"`
1012 # rm first in case test_failure exits.
1014 test_failure_ "$@" "$output"
1018 # This is not among top-level (test_expect_success)
1019 # but is a prefix that can be used in the test script, like:
1021 # test_expect_success 'complain and die' '
1023 # do something else &&
1024 # test_must_fail git checkout ../outerspace
1027 # Writing this as "! git checkout ../outerspace" is wrong, because
1028 # the failure could be due to a segv. We want a controlled failure.
1032 test $? -gt 0 -a $? -le 129 -o $? -gt 192
1035 # test_cmp is a helper function to compare actual and expected output.
1036 # You can use it like:
1038 # test_expect_success 'foo works' '
1039 # echo expected >expected &&
1041 # test_cmp expected actual
1044 # This could be written as either "cmp" or "diff -u", but:
1045 # - cmp's output is not nearly as easy to read as diff -u
1046 # - not all diff versions understand "-u"
1052 # This function can be used to schedule some commands to be run
1053 # unconditionally at the end of the test to restore sanity:
1055 # test_expect_success 'test core.capslock' '
1056 # git config core.capslock true &&
1057 # test_when_finished "git config --unset core.capslock" &&
1061 # That would be roughly equivalent to
1063 # test_expect_success 'test core.capslock' '
1064 # git config core.capslock true &&
1066 # git config --unset core.capslock
1069 # except that the greeting and config --unset must both succeed for
1072 test_when_finished () {
1074 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
1079 test_results_dir="$TEST_DIRECTORY/test-results"
1080 mkdir -p "$test_results_dir"
1081 test_results_path="$test_results_dir/$this_test"
1083 echo "total $test_count" >> $test_results_path
1084 echo "success $test_success" >> $test_results_path
1085 echo "fixed $test_fixed" >> $test_results_path
1086 echo "broken $test_broken" >> $test_results_path
1087 echo "failed $test_failure" >> $test_results_path
1088 echo "" >> $test_results_path
1090 [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'
1092 if [ "$test_failure" = "0" ]; then
1093 if [ "$test_broken" = "0" ]; then
1094 rm -rf "$remove_tmp"
1102 emacs_generate_script () {
1103 # Construct a little test script here for the benefit of the user,
1104 # (who can easily run "run_emacs" to get the same emacs environment
1105 # for investigating any failures).
1106 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
1109 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
1111 # Here's what we are using here:
1113 # --quick Use minimal customization. This implies --no-init-file,
1114 # --no-site-file and (emacs 24) --no-site-lisp
1116 # --directory Ensure that the local elisp sources are found
1118 # --load Force loading of notmuch.el and test-lib.el
1120 exec ${TEST_EMACS} --quick \
1121 --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
1122 --directory "$TEST_DIRECTORY" --load test-lib.el \
1125 chmod a+x "$TMP_DIRECTORY/run_emacs"
1129 # test dependencies beforehand to avoid the waiting loop below
1130 missing_dependencies=
1131 test_require_external_prereq dtach || missing_dependencies=1
1132 test_require_external_prereq emacs || missing_dependencies=1
1133 test_require_external_prereq ${TEST_EMACSCLIENT} || missing_dependencies=1
1134 test -z "$missing_dependencies" || return
1136 if [ -z "$EMACS_SERVER" ]; then
1137 emacs_tests="${this_test_bare}.el"
1138 if [ -f "$TEST_DIRECTORY/$emacs_tests" ]; then
1139 load_emacs_tests="--eval '(load \"$emacs_tests\")'"
1143 server_name="notmuch-test-suite-$$"
1144 # start a detached session with an emacs server
1145 # user's TERM (or 'vt100' in case user's TERM is unset, empty
1146 # or 'dumb') is given to dtach which assumes a minimally
1147 # VT100-compatible terminal -- and emacs inherits that
1148 TERM=$DTACH_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
1149 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
1150 --no-window-system \
1152 --eval '(setq server-name \"$server_name\")' \
1153 --eval '(server-start)' \
1154 --eval '(orphan-watchdog $$)'" || return
1155 EMACS_SERVER="$server_name"
1156 # wait until the emacs server is up
1157 until test_emacs '()' >/dev/null 2>/dev/null; do
1162 # Clear test-output output file. Most Emacs tests end with a
1163 # call to (test-output). If the test code fails with an
1164 # exception before this call, the output file won't get
1165 # updated. Since we don't want to compare against an output
1166 # file from another test, so start out with an empty file.
1170 ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(notmuch-test-progn $@)"
1174 export LD_LIBRARY_PATH=$TEST_DIRECTORY/../lib
1175 export PYTHONPATH=$TEST_DIRECTORY/../bindings/python
1177 (echo "import sys; _orig_stdout=sys.stdout; sys.stdout=open('OUTPUT', 'w')"; cat) \
1182 export LD_LIBRARY_PATH=$TEST_DIRECTORY/../lib
1183 MAIL_DIR=$MAIL_DIR ruby -I $TEST_DIRECTORY/../bindings/ruby> OUTPUT
1187 exec_file="test${test_count}"
1188 test_file="${exec_file}.c"
1190 export LD_LIBRARY_PATH=${TEST_DIRECTORY}/../lib
1191 ${TEST_CC} ${TEST_CFLAGS} -I${TEST_DIRECTORY}/../lib -o ${exec_file} ${test_file} -L${TEST_DIRECTORY}/../lib/ -lnotmuch -ltalloc
1192 echo "== stdout ==" > OUTPUT.stdout
1193 echo "== stderr ==" > OUTPUT.stderr
1194 ./${exec_file} "$@" 1>>OUTPUT.stdout 2>>OUTPUT.stderr
1195 notmuch_dir_sanitize OUTPUT.stdout OUTPUT.stderr > OUTPUT
1199 # Creates a script that counts how much time it is executed and calls
1200 # notmuch. $notmuch_counter_command is set to the path to the
1201 # generated script. Use notmuch_counter_value() function to get the
1202 # current counter value.
1203 notmuch_counter_reset () {
1204 notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
1205 if [ ! -x "$notmuch_counter_command" ]; then
1206 notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
1207 cat >"$notmuch_counter_command" <<EOF || return
1210 read count < "$notmuch_counter_state_path"
1211 echo \$((count + 1)) > "$notmuch_counter_state_path"
1215 chmod +x "$notmuch_counter_command" || return
1218 echo 0 > "$notmuch_counter_state_path"
1221 # Returns the current notmuch counter value.
1222 notmuch_counter_value () {
1223 if [ -r "$notmuch_counter_state_path" ]; then
1224 read count < "$notmuch_counter_state_path"
1231 test_reset_state_ () {
1232 test -z "$test_init_done_" && test_init_
1234 test_subtest_known_broken_=
1235 test_subtest_missing_external_prereq_=()
1238 # called once before the first subtest
1242 # skip all tests if there were external prerequisites missing during init
1243 test_check_missing_external_prereqs_ "all tests in $this_test" && test_done
1247 . ./test-lib-common.sh || exit 1
1249 emacs_generate_script
1252 # Use -P to resolve symlinks in our working directory so that the cwd
1253 # in subprocesses like git equals our $PWD (for pathname comparisons).
1254 cd -P "$test" || error "Cannot set up test environment"
1256 if test "$verbose" = "t"
1260 exec 4>test.output 3>&4
1263 for skp in $NOTMUCH_SKIP_TESTS
1266 for skp in $NOTMUCH_SKIP_TESTS
1268 case "$this_test" in
1273 case "$this_test_bare" in
1281 say_color skip >&3 "skipping test $this_test altogether"
1282 say_color skip "skip all tests in $this_test"
1287 # Provide an implementation of the 'yes' utility
1302 # Fix some commands on Windows
1305 # Windows has its own (incompatible) sort and find
1315 # git sees Windows-style pwd
1319 # no POSIX permissions
1320 # backslashes in pathspec are converted to '/'
1321 # exec does not inherit the PID
1324 test_set_prereq POSIXPERM
1325 test_set_prereq BSLASHPSPEC
1326 test_set_prereq EXECKEEPSPID
1330 test -z "$NO_PERL" && test_set_prereq PERL
1331 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1333 # test whether the filesystem supports symbolic links
1334 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1337 # convert variable from configure to more convenient form
1338 case "$NOTMUCH_DEFAULT_XAPIAN_BACKEND" in
1346 error "Unknown Xapian backend $NOTMUCH_DEFAULT_XAPIAN_BACKEND"
1348 # declare prerequisites for external binaries used in tests
1349 test_declare_external_prereq dtach
1350 test_declare_external_prereq emacs
1351 test_declare_external_prereq ${TEST_EMACSCLIENT}
1352 test_declare_external_prereq gdb
1353 test_declare_external_prereq gpg
1354 test_declare_external_prereq openssl
1355 test_declare_external_prereq gpgsm
1356 test_declare_external_prereq ${NOTMUCH_PYTHON}