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 https://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))
257 say_color error '%-6s' FATAL
260 echo "Unexpected exit while executing $0."
265 # Note: TEST_TMPDIR *NOT* exported!
266 TEST_TMPDIR=$(mktemp -d "${TMPDIR:-/tmp}/notmuch-test-$$.XXXXXX")
267 trap 'trap_exit' EXIT
268 trap 'trap_signal' HUP INT TERM
270 test_decode_color () {
271 sed -e 's/.\[1m/<WHITE>/g' \
272 -e 's/.\[31m/<RED>/g' \
273 -e 's/.\[32m/<GREEN>/g' \
274 -e 's/.\[33m/<YELLOW>/g' \
275 -e 's/.\[34m/<BLUE>/g' \
276 -e 's/.\[35m/<MAGENTA>/g' \
277 -e 's/.\[36m/<CYAN>/g' \
278 -e 's/.\[m/<RESET>/g'
290 sed -e 's/$/Q/' | tr Q '\015'
294 tr '\015' Q | sed -e 's/Q$//'
297 # Generate a new message in the mail directory, with a unique message
298 # ID and subject. The message is not added to the index.
300 # After this function returns, the filename of the generated message
301 # is available as $gen_msg_filename and the message ID is available as
304 # This function supports named parameters with the bash syntax for
305 # assigning a value to an associative array ([name]=value). The
306 # supported parameters are:
308 # [dir]=directory/of/choice
310 # Generate the message in directory 'directory/of/choice' within
311 # the mail store. The directory will be created if necessary.
315 # Store the message in file 'name'. The default is to store it
316 # in 'msg-<count>', where <count> is three-digit number of the
321 # Text to use as the body of the email message
323 # '[from]="Some User <user@example.com>"'
324 # '[to]="Some User <user@example.com>"'
325 # '[subject]="Subject of email message"'
326 # '[date]="RFC 822 Date"'
328 # Values for email headers. If not provided, default values will
329 # be generated instead.
331 # '[cc]="Some User <user@example.com>"'
332 # [reply-to]=some-address
333 # [in-reply-to]=<message-id>
334 # [references]=<message-id>
335 # [content-type]=content-type-specification
336 # '[header]=full header line, including keyword'
338 # Additional values for email headers. If these are not provided
339 # then the relevant headers will simply not appear in the
344 # Controls the message-id of the created message.
350 # This is our (bash-specific) magic for doing named parameters
351 local -A template="($@)"
352 local additional_headers
354 gen_msg_cnt=$((gen_msg_cnt + 1))
355 if [ -z "${template[filename]}" ]; then
356 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
358 gen_msg_name=${template[filename]}
361 if [ -z "${template[id]}" ]; then
362 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
364 gen_msg_id="${template[id]}"
367 if [ -z "${template[dir]}" ]; then
368 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
370 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
371 mkdir -p "$(dirname "$gen_msg_filename")"
374 if [ -z "${template[body]}" ]; then
375 template[body]="This is just a test message (#${gen_msg_cnt})"
378 if [ -z "${template[from]}" ]; then
379 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
382 if [ -z "${template[to]}" ]; then
383 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
386 if [ -z "${template[subject]}" ]; then
387 if [ -n "$test_subtest_name" ]; then
388 template[subject]="$test_subtest_name"
390 template[subject]="Test message #${gen_msg_cnt}"
392 elif [ "${template[subject]}" = "@FORCE_EMPTY" ]; then
396 if [ -z "${template[date]}" ]; then
397 # we use decreasing timestamps here for historical reasons;
398 # the existing test suite when we converted to unique timestamps just
399 # happened to have signicantly fewer failures with that choice.
400 local date_secs=$((978709437 - gen_msg_cnt))
401 # printf %(..)T is bash 4.2+ feature. use perl fallback if needed...
402 TZ=UTC printf -v template[date] "%(%a, %d %b %Y %T %z)T" $date_secs 2>/dev/null ||
403 template[date]=`perl -le 'use POSIX "strftime";
404 @time = gmtime '"$date_secs"';
405 print strftime "%a, %d %b %Y %T +0000", @time'`
408 additional_headers=""
409 if [ ! -z "${template[header]}" ]; then
410 additional_headers="${template[header]}
411 ${additional_headers}"
414 if [ ! -z "${template[reply-to]}" ]; then
415 additional_headers="Reply-To: ${template[reply-to]}
416 ${additional_headers}"
419 if [ ! -z "${template[in-reply-to]}" ]; then
420 additional_headers="In-Reply-To: ${template[in-reply-to]}
421 ${additional_headers}"
424 if [ ! -z "${template[cc]}" ]; then
425 additional_headers="Cc: ${template[cc]}
426 ${additional_headers}"
429 if [ ! -z "${template[bcc]}" ]; then
430 additional_headers="Bcc: ${template[bcc]}
431 ${additional_headers}"
434 if [ ! -z "${template[references]}" ]; then
435 additional_headers="References: ${template[references]}
436 ${additional_headers}"
439 if [ ! -z "${template[content-type]}" ]; then
440 additional_headers="Content-Type: ${template[content-type]}
441 ${additional_headers}"
444 if [ ! -z "${template[content-transfer-encoding]}" ]; then
445 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
446 ${additional_headers}"
449 # Note that in the way we're setting it above and using it below,
450 # `additional_headers' will also serve as the header / body separator
451 # (empty line in between).
453 cat <<EOF >"$gen_msg_filename"
454 From: ${template[from]}
456 Message-Id: <${gen_msg_id}>
457 Subject: ${template[subject]}
458 Date: ${template[date]}
459 ${additional_headers}
464 # Generate a new message and add it to the database.
466 # All of the arguments and return values supported by generate_message
467 # are also supported here, so see that function for details.
470 generate_message "$@" &&
471 notmuch new > /dev/null
474 # Deliver a message with emacs and add it to the database
476 # Uses emacs to generate and deliver a message to the mail store.
477 # Accepts arbitrary extra emacs/elisp functions to modify the message
478 # before sending, which is useful to doing things like attaching files
479 # to the message and encrypting/signing.
480 emacs_deliver_message ()
485 # before we can send a message, we have to prepare the FCC maildir
486 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
487 # eval'ing smtp-dummy --background will set smtp_dummy_pid
489 eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
490 test -n "$smtp_dummy_pid" || return 1
493 "(let ((message-send-mail-function 'message-smtpmail-send-it)
494 (mail-host-address \"example.com\")
495 (smtpmail-smtp-server \"localhost\")
496 (smtpmail-smtp-service \"25025\"))
499 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
500 (message-goto-subject)
501 (insert \"${subject}\")
505 (notmuch-mua-send-and-exit))"
507 # In case message was sent properly, client waits for confirmation
508 # before exiting and resuming control here; therefore making sure
509 # that server exits by sending (KILL) signal to it is safe.
510 kill -9 $smtp_dummy_pid
511 notmuch new >/dev/null
514 # Pretend to deliver a message with emacs. Really save it to a file
515 # and add it to the database
517 # Uses emacs to generate and deliver a message to the mail store.
518 # Accepts arbitrary extra emacs/elisp functions to modify the message
519 # before sending, which is useful to doing things like attaching files
520 # to the message and encrypting/signing.
526 # before we can send a message, we have to prepare the FCC maildir
527 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
530 "(let ((message-send-mail-function (lambda () t))
531 (mail-host-address \"example.com\"))
534 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
535 (message-goto-subject)
536 (insert \"${subject}\")
540 (notmuch-mua-send-and-exit))" || return 1
541 notmuch new >/dev/null
544 # Add an existing, fixed corpus of email to the database.
546 # $1 is the corpus dir under corpora to add, using "default" if unset.
548 # The default corpus is based on about 50 messages from early in the
549 # history of the notmuch mailing list, which allows for reliably
550 # testing commands that need to operate on a not-totally-trivial
551 # number of messages.
557 if [ -d $TEST_DIRECTORY/corpora.mail/$corpus ]; then
558 cp -a $TEST_DIRECTORY/corpora.mail/$corpus ${MAIL_DIR}
560 cp -a $TEST_DIRECTORY/corpora/$corpus ${MAIL_DIR}
561 notmuch new >/dev/null || die "'notmuch new' failed while adding email corpus"
562 mkdir -p $TEST_DIRECTORY/corpora.mail
563 cp -a ${MAIL_DIR} $TEST_DIRECTORY/corpora.mail/$corpus
567 test_begin_subtest ()
569 if [ -n "$inside_subtest" ]; then
570 exec 1>&6 2>&7 # Restore stdout and stderr
571 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
573 test_subtest_name="$1"
575 # Redirect test output to the previously prepared file descriptors
576 # 3 and 4 (see below)
577 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
582 # Pass test if two arguments match
584 # Note: Unlike all other test_expect_* functions, this function does
585 # not accept a test name. Instead, the caller should call
586 # test_begin_subtest before calling this function in order to set the
590 exec 1>&6 2>&7 # Restore stdout and stderr
592 test "$#" = 3 && { prereq=$1; shift; } || prereq=
594 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
598 if ! test_skip "$test_subtest_name"
600 if [ "$output" = "$expected" ]; then
603 testname=$this_test.$test_count
604 echo "$expected" > $testname.expected
605 echo "$output" > $testname.output
606 test_failure_ "$(diff -u $testname.expected $testname.output)"
611 # Like test_expect_equal, but takes two filenames.
612 test_expect_equal_file ()
614 exec 1>&6 2>&7 # Restore stdout and stderr
616 test "$#" = 3 && { prereq=$1; shift; } || prereq=
618 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
621 basename1=`basename "$file1"`
623 basename2=`basename "$file2"`
624 if ! test_skip "$test_subtest_name"
626 if diff -q "$file1" "$file2" >/dev/null ; then
629 testname=$this_test.$test_count
630 cp "$file1" "$testname.$basename1"
631 cp "$file2" "$testname.$basename2"
632 test_failure_ "$(diff -u "$testname.$basename1" "$testname.$basename2")"
637 # Like test_expect_equal, but arguments are JSON expressions to be
638 # canonicalized before diff'ing. If an argument cannot be parsed, it
639 # is used unchanged so that there's something to diff against.
640 test_expect_equal_json () {
641 # The test suite forces LC_ALL=C, but this causes Python 3 to
642 # decode stdin as ASCII. We need to read JSON in UTF-8, so
643 # override Python's stdio encoding defaults.
644 output=$(echo "$1" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
646 expected=$(echo "$2" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
649 test_expect_equal "$output" "$expected" "$@"
652 # Sort the top-level list of JSON data from stdin.
654 PYTHONIOENCODING=utf-8 python -c \
655 "import sys, json; json.dump(sorted(json.load(sys.stdin)),sys.stdout)"
658 test_emacs_expect_t () {
659 test "$#" = 2 && { prereq=$1; shift; } || prereq=
661 error "bug in the test script: not 1 or 2 parameters to test_emacs_expect_t"
664 if ! test_skip "$test_subtest_name"
666 test_emacs "(notmuch-test-run $1)" >/dev/null
668 # Restore state after the test.
669 exec 1>&6 2>&7 # Restore stdout and stderr
672 # Report success/failure.
678 test_failure_ "${result}"
681 # Restore state after the (non) test.
682 exec 1>&6 2>&7 # Restore stdout and stderr
689 notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
694 # this relies on the default format being batch-tag, otherwise some tests will break
695 notmuch dump --include=tags "${@}" | sed '/^#/d' | sort
698 notmuch_search_sanitize ()
700 perl -pe 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
703 notmuch_search_files_sanitize ()
708 notmuch_dir_sanitize ()
710 sed -e "s,$MAIL_DIR,MAIL_DIR," -e "s,${PWD},CWD,g" "$@"
713 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
714 notmuch_show_sanitize ()
716 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
718 notmuch_show_sanitize_all ()
721 -e 's| filename:.*| filename:XXXXX|' \
722 -e 's| id:[^ ]* | id:XXXXX |' | \
723 notmuch_date_sanitize
726 notmuch_json_show_sanitize ()
729 -e 's|"id": "[^"]*",|"id": "XXXXX",|g' \
730 -e 's|"Date": "Fri, 05 Jan 2001 [^"]*0000"|"Date": "GENERATED_DATE"|g' \
731 -e 's|"filename": "signature.asc",||g' \
732 -e 's|"filename": "/[^"]*",|"filename": "YYYYY",|g' \
733 -e 's|"timestamp": 97.......|"timestamp": 42|g'
736 notmuch_emacs_error_sanitize ()
744 -e 's/^\[.*\]$/[XXX]/' \
745 -e "s|^\(command: \)\{0,1\}/.*/$command|\1YYY/$command|"
748 notmuch_date_sanitize ()
751 -e 's/^Date: Fri, 05 Jan 2001 .*0000/Date: GENERATED_DATE/'
754 notmuch_uuid_sanitize ()
756 sed 's/[0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}/UUID/g'
759 notmuch_built_with_sanitize ()
761 sed 's/^built_with[.]\(.*\)=.*$/built_with.\1=something/'
764 notmuch_config_sanitize ()
766 notmuch_dir_sanitize | notmuch_built_with_sanitize
769 # End of notmuch helper functions
771 # Use test_set_prereq to tell that a particular prerequisite is available.
772 # The prerequisite can later be checked for in two ways:
774 # - Explicitly using test_have_prereq.
776 # - Implicitly by specifying the prerequisite tag in the calls to
777 # test_expect_{success,failure,code}.
779 # The single parameter is the prerequisite tag (a simple word, in all
780 # capital letters by convention).
783 satisfied="$satisfied$1 "
787 test_have_prereq () {
796 declare -A test_missing_external_prereq_
797 declare -A test_subtest_missing_external_prereq_
799 # declare prerequisite for the given external binary
800 test_declare_external_prereq () {
802 test "$#" = 2 && name=$2 || name="$binary(1)"
804 if ! hash $binary 2>/dev/null; then
805 test_missing_external_prereq_["${binary}"]=t
808 test_subtest_missing_external_prereq_[\"${name}\"]=t
814 # Explicitly require external prerequisite. Useful when binary is
815 # called indirectly (e.g. from emacs).
816 # Returns success if dependency is available, failure otherwise.
817 test_require_external_prereq () {
819 if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
820 # dependency is missing, call the replacement function to note it
827 # You are not expected to call test_ok_ and test_failure_ directly, use
828 # the text_expect_* functions instead.
831 if test "$test_subtest_known_broken_" = "t"; then
832 test_known_broken_ok_
835 test_success=$(($test_success + 1))
836 if test -n "$NOTMUCH_TEST_QUIET"; then
839 say_color pass "%-6s" "PASS"
840 echo " $test_subtest_name"
844 if test "$test_subtest_known_broken_" = "t"; then
845 test_known_broken_failure_ "$@"
848 test_failure=$(($test_failure + 1))
849 print_test_description
850 test_failure_message_ "FAIL" "$test_subtest_name" "$@"
851 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
855 test_failure_message_ () {
856 say_color error "%-6s" "$1"
859 echo "$@" | sed -e 's/^/ /'
860 if test "$verbose" != "t"; then cat test.output; fi
863 test_known_broken_ok_ () {
865 test_fixed=$(($test_fixed+1))
866 say_color pass "%-6s" "FIXED"
867 echo " $test_subtest_name"
870 test_known_broken_failure_ () {
872 test_broken=$(($test_broken+1))
873 test_failure_message_ "BROKEN" "$test_subtest_name" "$@"
878 test "$debug" = "" || eval "$1"
883 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
886 eval >&3 2>&4 "$test_cleanup"
891 test_count=$(($test_count+1))
893 for skp in $NOTMUCH_SKIP_TESTS
895 case $this_test.$test_count in
900 case $this_test_bare.$test_count in
906 if test -z "$to_skip" && test -n "$prereq" &&
907 ! test_have_prereq "$prereq"
913 test_report_skip_ "$@"
916 test_check_missing_external_prereqs_ "$@"
921 test_check_missing_external_prereqs_ () {
922 if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
923 say_color skip >&1 "missing prerequisites: "
924 echo ${!test_subtest_missing_external_prereq_[@]} >&1
925 test_report_skip_ "$@"
931 test_report_skip_ () {
933 say_color skip >&3 "skipping test:"
935 say_color skip "%-6s" "SKIP"
939 test_subtest_known_broken () {
940 test_subtest_known_broken_=t
943 test_expect_success () {
944 test "$#" = 3 && { prereq=$1; shift; } || prereq=
946 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
947 test_subtest_name="$1"
953 # test_run_ may update missing external prerequisites
954 test_check_missing_external_prereqs_ "$@" ||
955 if [ "$run_ret" = 0 -a "$eval_ret" = 0 ]
964 test_expect_code () {
965 test "$#" = 4 && { prereq=$1; shift; } || prereq=
967 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
968 test_subtest_name="$2"
974 # test_run_ may update missing external prerequisites,
975 test_check_missing_external_prereqs_ "$@" ||
976 if [ "$run_ret" = 0 -a "$eval_ret" = "$1" ]
980 test_failure_ "exit code $eval_ret, expected $1" "$3"
985 # test_external runs external test scripts that provide continuous
986 # test output about their progress, and succeeds/fails on
987 # zero/non-zero exit code. It outputs the test output on stdout even
988 # in non-verbose mode, and announces the external script with "* run
989 # <n>: ..." before running it. When providing relative paths, keep in
990 # mind that all scripts run in "trash directory".
991 # Usage: test_external description command arguments...
992 # Example: test_external 'Perl API' perl ../path/to/test.pl
994 test "$#" = 4 && { prereq=$1; shift; } || prereq=
996 error >&6 "bug in the test script: not 3 or 4 parameters to test_external"
997 test_subtest_name="$1"
1000 if ! test_skip "$test_subtest_name" "$@"
1002 # Announce the script to reduce confusion about the
1003 # test output that follows.
1004 say_color "" " run $test_count: $descr ($*)"
1005 # Run command; redirect its stderr to &4 as in
1006 # test_run_, but keep its stdout on our stdout even in
1018 # Like test_external, but in addition tests that the command generated
1019 # no output on stderr.
1020 test_external_without_stderr () {
1021 # The temporary file has no (and must have no) security
1023 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
1024 stderr="$tmp/git-external-stderr.$$.tmp"
1025 test_external "$@" 4> "$stderr"
1026 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
1027 test_subtest_name="no stderr: $1"
1029 if [ ! -s "$stderr" ]; then
1033 if [ "$verbose" = t ]; then
1034 output=`echo; echo Stderr is:; cat "$stderr"`
1038 # rm first in case test_failure exits.
1040 test_failure_ "$@" "$output"
1044 # This is not among top-level (test_expect_success)
1045 # but is a prefix that can be used in the test script, like:
1047 # test_expect_success 'complain and die' '
1049 # do something else &&
1050 # test_must_fail git checkout ../outerspace
1053 # Writing this as "! git checkout ../outerspace" is wrong, because
1054 # the failure could be due to a segv. We want a controlled failure.
1058 test $? -gt 0 -a $? -le 129 -o $? -gt 192
1061 # test_cmp is a helper function to compare actual and expected output.
1062 # You can use it like:
1064 # test_expect_success 'foo works' '
1065 # echo expected >expected &&
1067 # test_cmp expected actual
1070 # This could be written as either "cmp" or "diff -u", but:
1071 # - cmp's output is not nearly as easy to read as diff -u
1072 # - not all diff versions understand "-u"
1078 # This function can be used to schedule some commands to be run
1079 # unconditionally at the end of the test to restore sanity:
1081 # test_expect_success 'test core.capslock' '
1082 # git config core.capslock true &&
1083 # test_when_finished "git config --unset core.capslock" &&
1087 # That would be roughly equivalent to
1089 # test_expect_success 'test core.capslock' '
1090 # git config core.capslock true &&
1092 # git config --unset core.capslock
1095 # except that the greeting and config --unset must both succeed for
1098 test_when_finished () {
1100 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
1105 test_results_dir="$TEST_DIRECTORY/test-results"
1106 mkdir -p "$test_results_dir"
1107 test_results_path="$test_results_dir/$this_test"
1109 echo "total $test_count" >> $test_results_path
1110 echo "success $test_success" >> $test_results_path
1111 echo "fixed $test_fixed" >> $test_results_path
1112 echo "broken $test_broken" >> $test_results_path
1113 echo "failed $test_failure" >> $test_results_path
1114 echo "" >> $test_results_path
1116 [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'
1118 if [ "$test_failure" = "0" ]; then
1119 if [ "$test_broken" = "0" ]; then
1120 rm -rf "$remove_tmp"
1128 emacs_generate_script () {
1129 # Construct a little test script here for the benefit of the user,
1130 # (who can easily run "run_emacs" to get the same emacs environment
1131 # for investigating any failures).
1132 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
1135 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
1137 # Here's what we are using here:
1139 # --quick Use minimal customization. This implies --no-init-file,
1140 # --no-site-file and (emacs 24) --no-site-lisp
1142 # --directory Ensure that the local elisp sources are found
1144 # --load Force loading of notmuch.el and test-lib.el
1146 exec ${TEST_EMACS} --quick \
1147 --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
1148 --directory "$TEST_DIRECTORY" --load test-lib.el \
1151 chmod a+x "$TMP_DIRECTORY/run_emacs"
1155 # test dependencies beforehand to avoid the waiting loop below
1156 missing_dependencies=
1157 test_require_external_prereq dtach || missing_dependencies=1
1158 test_require_external_prereq emacs || missing_dependencies=1
1159 test_require_external_prereq ${TEST_EMACSCLIENT} || missing_dependencies=1
1160 test -z "$missing_dependencies" || return
1162 if [ -z "$EMACS_SERVER" ]; then
1163 emacs_tests="${this_test_bare}.el"
1164 if [ -f "$TEST_DIRECTORY/$emacs_tests" ]; then
1165 load_emacs_tests="--eval '(load \"$emacs_tests\")'"
1169 server_name="notmuch-test-suite-$$"
1170 # start a detached session with an emacs server
1171 # user's TERM (or 'vt100' in case user's TERM is unset, empty
1172 # or 'dumb') is given to dtach which assumes a minimally
1173 # VT100-compatible terminal -- and emacs inherits that
1174 TERM=$DTACH_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
1175 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
1176 --no-window-system \
1178 --eval '(setq server-name \"$server_name\")' \
1179 --eval '(server-start)' \
1180 --eval '(orphan-watchdog $$)'" || return
1181 EMACS_SERVER="$server_name"
1182 # wait until the emacs server is up
1183 until test_emacs '()' >/dev/null 2>/dev/null; do
1188 # Clear test-output output file. Most Emacs tests end with a
1189 # call to (test-output). If the test code fails with an
1190 # exception before this call, the output file won't get
1191 # updated. Since we don't want to compare against an output
1192 # file from another test, so start out with an empty file.
1196 ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(notmuch-test-progn $@)"
1200 # Note: if there is need to print debug information from python program,
1201 # use stdout = os.fdopen(6, 'w') or stderr = os.fdopen(7, 'w')
1202 PYTHONPATH="$TEST_DIRECTORY/../bindings/python${PYTHONPATH:+:$PYTHONPATH}" \
1203 $NOTMUCH_PYTHON -B - > OUTPUT
1207 MAIL_DIR=$MAIL_DIR ruby -I $TEST_DIRECTORY/../bindings/ruby> OUTPUT
1211 exec_file="test${test_count}"
1212 test_file="${exec_file}.c"
1214 ${TEST_CC} ${TEST_CFLAGS} -I${TEST_DIRECTORY} -I${TEST_DIRECTORY}/../lib -o ${exec_file} ${test_file} -L${TEST_DIRECTORY}/../lib/ -lnotmuch -ltalloc
1215 echo "== stdout ==" > OUTPUT.stdout
1216 echo "== stderr ==" > OUTPUT.stderr
1217 ./${exec_file} "$@" 1>>OUTPUT.stdout 2>>OUTPUT.stderr
1218 notmuch_dir_sanitize OUTPUT.stdout OUTPUT.stderr > OUTPUT
1222 # Creates a script that counts how much time it is executed and calls
1223 # notmuch. $notmuch_counter_command is set to the path to the
1224 # generated script. Use notmuch_counter_value() function to get the
1225 # current counter value.
1226 notmuch_counter_reset () {
1227 notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
1228 if [ ! -x "$notmuch_counter_command" ]; then
1229 notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
1230 cat >"$notmuch_counter_command" <<EOF || return
1233 read count < "$notmuch_counter_state_path"
1234 echo \$((count + 1)) > "$notmuch_counter_state_path"
1238 chmod +x "$notmuch_counter_command" || return
1241 echo 0 > "$notmuch_counter_state_path"
1244 # Returns the current notmuch counter value.
1245 notmuch_counter_value () {
1246 if [ -r "$notmuch_counter_state_path" ]; then
1247 read count < "$notmuch_counter_state_path"
1254 test_reset_state_ () {
1255 test -z "$test_init_done_" && test_init_
1257 test_subtest_known_broken_=
1258 test_subtest_missing_external_prereq_=()
1261 # called once before the first subtest
1265 # skip all tests if there were external prerequisites missing during init
1266 test_check_missing_external_prereqs_ "all tests in $this_test" && test_done
1270 . ./test-lib-common.sh || exit 1
1272 emacs_generate_script
1275 # Use -P to resolve symlinks in our working directory so that the cwd
1276 # in subprocesses like git equals our $PWD (for pathname comparisons).
1277 cd -P "$test" || error "Cannot set up test environment"
1279 if test "$verbose" = "t"
1283 exec 4>test.output 3>&4
1286 for skp in $NOTMUCH_SKIP_TESTS
1289 for skp in $NOTMUCH_SKIP_TESTS
1291 case "$this_test" in
1296 case "$this_test_bare" in
1304 say_color skip >&3 "skipping test $this_test altogether"
1305 say_color skip "skip all tests in $this_test"
1310 # Provide an implementation of the 'yes' utility
1325 # Fix some commands on Windows
1328 # Windows has its own (incompatible) sort and find
1338 # git sees Windows-style pwd
1342 # no POSIX permissions
1343 # backslashes in pathspec are converted to '/'
1344 # exec does not inherit the PID
1347 test_set_prereq POSIXPERM
1348 test_set_prereq BSLASHPSPEC
1349 test_set_prereq EXECKEEPSPID
1353 test -z "$NO_PERL" && test_set_prereq PERL
1354 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1356 # test whether the filesystem supports symbolic links
1357 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1360 # convert variable from configure to more convenient form
1361 case "$NOTMUCH_DEFAULT_XAPIAN_BACKEND" in
1369 error "Unknown Xapian backend $NOTMUCH_DEFAULT_XAPIAN_BACKEND"
1371 # declare prerequisites for external binaries used in tests
1372 test_declare_external_prereq dtach
1373 test_declare_external_prereq emacs
1374 test_declare_external_prereq ${TEST_EMACSCLIENT}
1375 test_declare_external_prereq gdb
1376 test_declare_external_prereq gpg
1377 test_declare_external_prereq openssl
1378 test_declare_external_prereq gpgsm
1379 test_declare_external_prereq ${NOTMUCH_PYTHON}