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 "$BASH" "$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 # Set SMART_TERM to vt100 for known dumb/unknown terminal.
59 # Otherwise use whatever TERM is currently used so that
60 # users' actual TERM environments are being used in tests.
68 # For repeatability, reset the environment to known value.
74 export LANG LC_ALL PAGER TERM TZ
75 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
76 if [[ ( -n "$TEST_EMACS" && -z "$TEST_EMACSCLIENT" ) || \
77 ( -z "$TEST_EMACS" && -n "$TEST_EMACSCLIENT" ) ]]; then
78 echo "error: must specify both or neither of TEST_EMACS and TEST_EMACSCLIENT" >&2
81 TEST_EMACS=${TEST_EMACS:-${EMACS:-emacs}}
82 TEST_EMACSCLIENT=${TEST_EMACSCLIENT:-emacsclient}
83 TEST_GDB=${TEST_GDB:-gdb}
84 TEST_CC=${TEST_CC:-cc}
85 TEST_CFLAGS=${TEST_CFLAGS:-"-g -O0"}
87 # Protect ourselves from common misconfiguration to export
88 # CDPATH into the environment
94 unset ALTERNATE_EDITOR
98 # A regexp to match 5 and 40 hexdigits
99 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
100 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
102 _x04='[0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
103 _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
105 # Each test should start with something like this, after copyright notices:
107 # test_description='Description of this test...
108 # This test checks if command xyzzy does the right thing...
110 # . ./test-lib.sh || exit 1
112 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
113 TERM=$ORIGINAL_TERM &&
116 tput bold >/dev/null 2>&1 &&
117 tput setaf 1 >/dev/null 2>&1 &&
118 tput sgr0 >/dev/null 2>&1
122 while test "$#" -ne 0
125 -d|--d|--de|--deb|--debu|--debug)
127 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
128 immediate=t; shift ;;
129 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
130 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
131 -h|--h|--he|--hel|--help)
133 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
135 -q|--q|--qu|--qui|--quie|--quiet)
138 with_dashes=t; shift ;;
144 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
145 valgrind=t; verbose=t; shift ;;
147 shift ;; # was handled already
149 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
152 echo "error: unknown test option '$1'" >&2; exit 1 ;;
156 if test -n "$debug"; then
158 printf " %-4s" "[$((test_count - 1))]"
166 if test -n "$color"; then
172 error) tput bold; tput setaf 1;; # bold red
173 skip) tput bold; tput setaf 2;; # bold green
174 pass) tput setaf 2;; # green
175 info) tput setaf 3;; # brown
176 *) test -n "$quiet" && return;;
187 test -z "$1" && test -n "$quiet" && return
196 say_color error "error: $*\n"
205 test "${test_description}" != "" ||
206 error "Test script did not set test_description."
208 if test "$help" = "t"
210 echo "Tests ${test_description}"
214 test_description_printed=
215 print_test_description ()
217 test -z "$test_description_printed" || return 0
219 echo $this_test: "Testing ${test_description}"
220 test_description_printed=1
222 if [ -z "$NOTMUCH_TEST_QUIET" ]
224 print_test_description
237 rm -rf "$TEST_TMPDIR"
242 if test -n "$GIT_EXIT_OK"
247 say_color error '%-6s' FATAL
248 echo " $test_subtest_name"
250 echo "Unexpected exit while executing $0. Exit code $code."
257 echo >&6 "FATAL: $0: interrupted by signal" $((code - 128))
264 say_color error '%-6s' FATAL
267 echo "Unexpected exit while executing $0."
272 # Note: TEST_TMPDIR *NOT* exported!
273 TEST_TMPDIR=$(mktemp -d "${TMPDIR:-/tmp}/notmuch-test-$$.XXXXXX")
274 # Put GNUPGHOME in TMPDIR to avoid problems with long paths.
275 export GNUPGHOME="${TEST_TMPDIR}/gnupg"
276 trap 'trap_exit' EXIT
277 trap 'trap_signal' HUP INT TERM
279 test_decode_color () {
280 sed -e 's/.\[1m/<WHITE>/g' \
281 -e 's/.\[31m/<RED>/g' \
282 -e 's/.\[32m/<GREEN>/g' \
283 -e 's/.\[33m/<YELLOW>/g' \
284 -e 's/.\[34m/<BLUE>/g' \
285 -e 's/.\[35m/<MAGENTA>/g' \
286 -e 's/.\[36m/<CYAN>/g' \
287 -e 's/.\[m/<RESET>/g'
299 sed -e 's/$/Q/' | tr Q '\015'
303 tr '\015' Q | sed -e 's/Q$//'
306 # Generate a new message in the mail directory, with a unique message
307 # ID and subject. The message is not added to the index.
309 # After this function returns, the filename of the generated message
310 # is available as $gen_msg_filename and the message ID is available as
313 # This function supports named parameters with the bash syntax for
314 # assigning a value to an associative array ([name]=value). The
315 # supported parameters are:
317 # [dir]=directory/of/choice
319 # Generate the message in directory 'directory/of/choice' within
320 # the mail store. The directory will be created if necessary.
324 # Store the message in file 'name'. The default is to store it
325 # in 'msg-<count>', where <count> is three-digit number of the
330 # Text to use as the body of the email message
332 # '[from]="Some User <user@example.com>"'
333 # '[to]="Some User <user@example.com>"'
334 # '[subject]="Subject of email message"'
335 # '[date]="RFC 822 Date"'
337 # Values for email headers. If not provided, default values will
338 # be generated instead.
340 # '[cc]="Some User <user@example.com>"'
341 # [reply-to]=some-address
342 # [in-reply-to]=<message-id>
343 # [references]=<message-id>
344 # [content-type]=content-type-specification
345 # '[header]=full header line, including keyword'
347 # Additional values for email headers. If these are not provided
348 # then the relevant headers will simply not appear in the
353 # Controls the message-id of the created message.
359 # This is our (bash-specific) magic for doing named parameters
360 local -A template="($@)"
361 local additional_headers
363 gen_msg_cnt=$((gen_msg_cnt + 1))
364 if [ -z "${template[filename]}" ]; then
365 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
367 gen_msg_name=${template[filename]}
370 if [ -z "${template[id]}" ]; then
371 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
373 gen_msg_id="${template[id]}"
376 if [ -z "${template[dir]}" ]; then
377 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
379 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
380 mkdir -p "$(dirname "$gen_msg_filename")"
383 if [ -z "${template[body]}" ]; then
384 template[body]="This is just a test message (#${gen_msg_cnt})"
387 if [ -z "${template[from]}" ]; then
388 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
391 if [ -z "${template[to]}" ]; then
392 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
395 if [ -z "${template[subject]}" ]; then
396 if [ -n "$test_subtest_name" ]; then
397 template[subject]="$test_subtest_name"
399 template[subject]="Test message #${gen_msg_cnt}"
401 elif [ "${template[subject]}" = "@FORCE_EMPTY" ]; then
405 if [ -z "${template[date]}" ]; then
406 # we use decreasing timestamps here for historical reasons;
407 # the existing test suite when we converted to unique timestamps just
408 # happened to have signicantly fewer failures with that choice.
409 local date_secs=$((978709437 - gen_msg_cnt))
410 # printf %(..)T is bash 4.2+ feature. use perl fallback if needed...
411 TZ=UTC printf -v template[date] "%(%a, %d %b %Y %T %z)T" $date_secs 2>/dev/null ||
412 template[date]=`perl -le 'use POSIX "strftime";
413 @time = gmtime '"$date_secs"';
414 print strftime "%a, %d %b %Y %T +0000", @time'`
417 additional_headers=""
418 if [ ! -z "${template[header]}" ]; then
419 additional_headers="${template[header]}
420 ${additional_headers}"
423 if [ ! -z "${template[reply-to]}" ]; then
424 additional_headers="Reply-To: ${template[reply-to]}
425 ${additional_headers}"
428 if [ ! -z "${template[in-reply-to]}" ]; then
429 additional_headers="In-Reply-To: ${template[in-reply-to]}
430 ${additional_headers}"
433 if [ ! -z "${template[cc]}" ]; then
434 additional_headers="Cc: ${template[cc]}
435 ${additional_headers}"
438 if [ ! -z "${template[bcc]}" ]; then
439 additional_headers="Bcc: ${template[bcc]}
440 ${additional_headers}"
443 if [ ! -z "${template[references]}" ]; then
444 additional_headers="References: ${template[references]}
445 ${additional_headers}"
448 if [ ! -z "${template[content-type]}" ]; then
449 additional_headers="Content-Type: ${template[content-type]}
450 ${additional_headers}"
453 if [ ! -z "${template[content-transfer-encoding]}" ]; then
454 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
455 ${additional_headers}"
458 # Note that in the way we're setting it above and using it below,
459 # `additional_headers' will also serve as the header / body separator
460 # (empty line in between).
462 cat <<EOF >"$gen_msg_filename"
463 From: ${template[from]}
465 Message-Id: <${gen_msg_id}>
466 Subject: ${template[subject]}
467 Date: ${template[date]}
468 ${additional_headers}
473 # Generate a new message and add it to the database.
475 # All of the arguments and return values supported by generate_message
476 # are also supported here, so see that function for details.
479 generate_message "$@" &&
480 notmuch new > /dev/null
483 # Deliver a message with emacs and add it to the database
485 # Uses emacs to generate and deliver a message to the mail store.
486 # Accepts arbitrary extra emacs/elisp functions to modify the message
487 # before sending, which is useful to doing things like attaching files
488 # to the message and encrypting/signing.
489 emacs_deliver_message ()
494 # before we can send a message, we have to prepare the FCC maildir
495 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
496 # eval'ing smtp-dummy --background will set smtp_dummy_pid
498 eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
499 test -n "$smtp_dummy_pid" || return 1
502 "(let ((message-send-mail-function 'message-smtpmail-send-it)
503 (mail-host-address \"example.com\")
504 (smtpmail-smtp-server \"localhost\")
505 (smtpmail-smtp-service \"25025\"))
508 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
509 (message-goto-subject)
510 (insert \"${subject}\")
514 (notmuch-mua-send-and-exit))"
516 # In case message was sent properly, client waits for confirmation
517 # before exiting and resuming control here; therefore making sure
518 # that server exits by sending (KILL) signal to it is safe.
519 kill -9 $smtp_dummy_pid
520 notmuch new >/dev/null
523 # Pretend to deliver a message with emacs. Really save it to a file
524 # and add it to the database
526 # Uses emacs to generate and deliver a message to the mail store.
527 # Accepts arbitrary extra emacs/elisp functions to modify the message
528 # before sending, which is useful to doing things like attaching files
529 # to the message and encrypting/signing.
535 # before we can send a message, we have to prepare the FCC maildir
536 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
539 "(let ((message-send-mail-function (lambda () t))
540 (mail-host-address \"example.com\"))
543 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
544 (message-goto-subject)
545 (insert \"${subject}\")
549 (notmuch-mua-send-and-exit))" || return 1
550 notmuch new >/dev/null
553 # Add an existing, fixed corpus of email to the database.
555 # $1 is the corpus dir under corpora to add, using "default" if unset.
557 # The default corpus is based on about 50 messages from early in the
558 # history of the notmuch mailing list, which allows for reliably
559 # testing commands that need to operate on a not-totally-trivial
560 # number of messages.
566 if [ -d $TEST_DIRECTORY/corpora.mail/$corpus ]; then
567 cp -a $TEST_DIRECTORY/corpora.mail/$corpus ${MAIL_DIR}
569 cp -a $TEST_DIRECTORY/corpora/$corpus ${MAIL_DIR}
570 notmuch new >/dev/null || die "'notmuch new' failed while adding email corpus"
571 mkdir -p $TEST_DIRECTORY/corpora.mail
572 cp -a ${MAIL_DIR} $TEST_DIRECTORY/corpora.mail/$corpus
576 test_begin_subtest ()
578 if [ -n "$inside_subtest" ]; then
579 exec 1>&6 2>&7 # Restore stdout and stderr
580 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
582 test_subtest_name="$1"
584 # Redirect test output to the previously prepared file descriptors
585 # 3 and 4 (see below)
586 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
591 # Pass test if two arguments match
593 # Note: Unlike all other test_expect_* functions, this function does
594 # not accept a test name. Instead, the caller should call
595 # test_begin_subtest before calling this function in order to set the
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"
607 if ! test_skip "$test_subtest_name"
609 if [ "$output" = "$expected" ]; then
612 testname=$this_test.$test_count
613 echo "$expected" > $testname.expected
614 echo "$output" > $testname.output
615 test_failure_ "$(diff -u $testname.expected $testname.output)"
620 # Like test_expect_equal, but takes two filenames.
621 test_expect_equal_file ()
623 exec 1>&6 2>&7 # Restore stdout and stderr
625 test "$#" = 3 && { prereq=$1; shift; } || prereq=
627 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
631 if ! test_skip "$test_subtest_name"
633 if diff -q "$file1" "$file2" >/dev/null ; then
636 testname=$this_test.$test_count
637 basename1=`basename "$file1"`
638 basename2=`basename "$file2"`
639 cp "$file1" "$testname.$basename1"
640 cp "$file2" "$testname.$basename2"
641 test_failure_ "$(diff -u "$testname.$basename1" "$testname.$basename2")"
646 # Like test_expect_equal, but arguments are JSON expressions to be
647 # canonicalized before diff'ing. If an argument cannot be parsed, it
648 # is used unchanged so that there's something to diff against.
649 test_expect_equal_json () {
650 # The test suite forces LC_ALL=C, but this causes Python 3 to
651 # decode stdin as ASCII. We need to read JSON in UTF-8, so
652 # override Python's stdio encoding defaults.
653 output=$(echo "$1" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
655 expected=$(echo "$2" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
658 test_expect_equal "$output" "$expected" "$@"
661 # Sort the top-level list of JSON data from stdin.
663 PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -c \
664 "import sys, json; json.dump(sorted(json.load(sys.stdin)),sys.stdout)"
667 test_emacs_expect_t () {
668 test "$#" = 2 && { prereq=$1; shift; } || prereq=
670 error "bug in the test script: not 1 or 2 parameters to test_emacs_expect_t"
673 if ! test_skip "$test_subtest_name"
675 test_emacs "(notmuch-test-run $1)" >/dev/null
677 # Restore state after the test.
678 exec 1>&6 2>&7 # Restore stdout and stderr
681 # Report success/failure.
687 test_failure_ "${result}"
690 # Restore state after the (non) test.
691 exec 1>&6 2>&7 # Restore stdout and stderr
698 notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
703 # this relies on the default format being batch-tag, otherwise some tests will break
704 notmuch dump --include=tags "${@}" | sed '/^#/d' | sort
707 notmuch_search_sanitize ()
709 perl -pe 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
712 notmuch_search_files_sanitize ()
717 notmuch_dir_sanitize ()
719 sed -e "s,$MAIL_DIR,MAIL_DIR," -e "s,${PWD},CWD,g" "$@"
722 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
723 notmuch_show_sanitize ()
725 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
727 notmuch_show_sanitize_all ()
730 -e 's| filename:.*| filename:XXXXX|' \
731 -e 's| id:[^ ]* | id:XXXXX |' | \
732 notmuch_date_sanitize
735 notmuch_json_show_sanitize ()
738 -e 's|"id": "[^"]*",|"id": "XXXXX",|g' \
739 -e 's|"Date": "Fri, 05 Jan 2001 [^"]*0000"|"Date": "GENERATED_DATE"|g' \
740 -e 's|"filename": "signature.asc",||g' \
741 -e 's|"filename": \["/[^"]*"\],|"filename": \["YYYYY"\],|g' \
742 -e 's|"timestamp": 97.......|"timestamp": 42|g' \
743 -e 's|"content-length": [1-9][0-9]*|"content-length": "NONZERO"|g'
746 notmuch_emacs_error_sanitize ()
754 -e 's/^\[.*\]$/[XXX]/' \
755 -e "s|^\(command: \)\{0,1\}/.*/$command|\1YYY/$command|"
758 notmuch_date_sanitize ()
761 -e 's/^Date: Fri, 05 Jan 2001 .*0000/Date: GENERATED_DATE/'
764 notmuch_uuid_sanitize ()
766 sed 's/[0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}/UUID/g'
769 notmuch_built_with_sanitize ()
771 sed 's/^built_with[.]\(.*\)=.*$/built_with.\1=something/'
774 notmuch_config_sanitize ()
776 notmuch_dir_sanitize | notmuch_built_with_sanitize
779 # End of notmuch helper functions
781 # Use test_set_prereq to tell that a particular prerequisite is available.
782 # The prerequisite can later be checked for in two ways:
784 # - Explicitly using test_have_prereq.
786 # - Implicitly by specifying the prerequisite tag in the calls to
787 # test_expect_{success,failure,code}.
789 # The single parameter is the prerequisite tag (a simple word, in all
790 # capital letters by convention).
793 satisfied="$satisfied$1 "
797 test_have_prereq () {
806 declare -A test_missing_external_prereq_
807 declare -A test_subtest_missing_external_prereq_
809 # declare prerequisite for the given external binary
810 test_declare_external_prereq () {
812 test "$#" = 2 && name=$2 || name="$binary(1)"
814 if ! hash $binary 2>/dev/null; then
815 test_missing_external_prereq_["${binary}"]=t
818 test_subtest_missing_external_prereq_[\"${name}\"]=t
824 # Explicitly require external prerequisite. Useful when binary is
825 # called indirectly (e.g. from emacs).
826 # Returns success if dependency is available, failure otherwise.
827 test_require_external_prereq () {
829 if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
830 # dependency is missing, call the replacement function to note it
837 # You are not expected to call test_ok_ and test_failure_ directly, use
838 # the text_expect_* functions instead.
841 if test "$test_subtest_known_broken_" = "t"; then
842 test_known_broken_ok_
845 test_success=$(($test_success + 1))
846 if test -n "$NOTMUCH_TEST_QUIET"; then
849 say_color pass "%-6s" "PASS"
850 echo " $test_subtest_name"
854 print_test_description
855 if test "$test_subtest_known_broken_" = "t"; then
856 test_known_broken_failure_ "$@"
859 test_failure=$(($test_failure + 1))
860 test_failure_message_ "FAIL" "$test_subtest_name" "$@"
861 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
865 test_failure_message_ () {
866 say_color error "%-6s" "$1"
869 if [ "$#" != "0" ]; then
870 echo "$@" | sed -e 's/^/ /'
872 if test "$verbose" != "t"; then cat test.output; fi
875 test_known_broken_ok_ () {
877 test_fixed=$(($test_fixed+1))
878 say_color pass "%-6s" "FIXED"
879 echo " $test_subtest_name"
882 test_known_broken_failure_ () {
884 test_broken=$(($test_broken+1))
885 if [ -z "$NOTMUCH_TEST_QUIET" ]; then
886 test_failure_message_ "BROKEN" "$test_subtest_name" "$@"
888 test_failure_message_ "BROKEN" "$test_subtest_name"
894 test "$debug" = "" || eval "$1"
899 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
902 eval >&3 2>&4 "$test_cleanup"
907 test_count=$(($test_count+1))
909 for skp in $NOTMUCH_SKIP_TESTS
911 case $this_test.$test_count in
916 case $this_test_bare.$test_count in
922 if test -z "$to_skip" && test -n "$prereq" &&
923 ! test_have_prereq "$prereq"
929 test_report_skip_ "$@"
932 test_check_missing_external_prereqs_ "$@"
937 test_check_missing_external_prereqs_ () {
938 if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
939 say_color skip >&1 "missing prerequisites: "
940 echo ${!test_subtest_missing_external_prereq_[@]} >&1
941 test_report_skip_ "$@"
947 test_report_skip_ () {
949 say_color skip >&3 "skipping test:"
951 say_color skip "%-6s" "SKIP"
955 test_subtest_known_broken () {
956 test_subtest_known_broken_=t
959 test_expect_success () {
960 test "$#" = 3 && { prereq=$1; shift; } || prereq=
962 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
963 test_subtest_name="$1"
969 # test_run_ may update missing external prerequisites
970 test_check_missing_external_prereqs_ "$@" ||
971 if [ "$run_ret" = 0 -a "$eval_ret" = 0 ]
980 test_expect_code () {
981 test "$#" = 4 && { prereq=$1; shift; } || prereq=
983 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
984 test_subtest_name="$2"
990 # test_run_ may update missing external prerequisites,
991 test_check_missing_external_prereqs_ "$@" ||
992 if [ "$run_ret" = 0 -a "$eval_ret" = "$1" ]
996 test_failure_ "exit code $eval_ret, expected $1" "$3"
1001 # test_external runs external test scripts that provide continuous
1002 # test output about their progress, and succeeds/fails on
1003 # zero/non-zero exit code. It outputs the test output on stdout even
1004 # in non-verbose mode, and announces the external script with "* run
1005 # <n>: ..." before running it. When providing relative paths, keep in
1006 # mind that all scripts run in "trash directory".
1007 # Usage: test_external description command arguments...
1008 # Example: test_external 'Perl API' perl ../path/to/test.pl
1010 test "$#" = 4 && { prereq=$1; shift; } || prereq=
1012 error >&6 "bug in the test script: not 3 or 4 parameters to test_external"
1013 test_subtest_name="$1"
1016 if ! test_skip "$test_subtest_name" "$@"
1018 # Announce the script to reduce confusion about the
1019 # test output that follows.
1020 say_color "" " run $test_count: $descr ($*)"
1021 # Run command; redirect its stderr to &4 as in
1022 # test_run_, but keep its stdout on our stdout even in
1034 # Like test_external, but in addition tests that the command generated
1035 # no output on stderr.
1036 test_external_without_stderr () {
1037 # The temporary file has no (and must have no) security
1039 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
1040 stderr="$tmp/git-external-stderr.$$.tmp"
1041 test_external "$@" 4> "$stderr"
1042 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
1043 test_subtest_name="no stderr: $1"
1045 if [ ! -s "$stderr" ]; then
1049 if [ "$verbose" = t ]; then
1050 output=`echo; echo Stderr is:; cat "$stderr"`
1054 # rm first in case test_failure exits.
1056 test_failure_ "$@" "$output"
1060 # This is not among top-level (test_expect_success)
1061 # but is a prefix that can be used in the test script, like:
1063 # test_expect_success 'complain and die' '
1065 # do something else &&
1066 # test_must_fail git checkout ../outerspace
1069 # Writing this as "! git checkout ../outerspace" is wrong, because
1070 # the failure could be due to a segv. We want a controlled failure.
1074 test $? -gt 0 -a $? -le 129 -o $? -gt 192
1077 # test_cmp is a helper function to compare actual and expected output.
1078 # You can use it like:
1080 # test_expect_success 'foo works' '
1081 # echo expected >expected &&
1083 # test_cmp expected actual
1086 # This could be written as either "cmp" or "diff -u", but:
1087 # - cmp's output is not nearly as easy to read as diff -u
1088 # - not all diff versions understand "-u"
1094 # This function can be used to schedule some commands to be run
1095 # unconditionally at the end of the test to restore sanity:
1097 # test_expect_success 'test core.capslock' '
1098 # git config core.capslock true &&
1099 # test_when_finished "git config --unset core.capslock" &&
1103 # That would be roughly equivalent to
1105 # test_expect_success 'test core.capslock' '
1106 # git config core.capslock true &&
1108 # git config --unset core.capslock
1111 # except that the greeting and config --unset must both succeed for
1114 test_when_finished () {
1116 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
1121 test_results_dir="$TEST_DIRECTORY/test-results"
1122 mkdir -p "$test_results_dir"
1123 test_results_path="$test_results_dir/$this_test"
1125 echo "total $test_count" >> $test_results_path
1126 echo "success $test_success" >> $test_results_path
1127 echo "fixed $test_fixed" >> $test_results_path
1128 echo "broken $test_broken" >> $test_results_path
1129 echo "failed $test_failure" >> $test_results_path
1130 echo "" >> $test_results_path
1132 [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'
1134 if [ "$test_failure" = "0" ]; then
1135 if [ "$test_broken" = "0" ]; then
1136 rm -rf "$remove_tmp"
1144 emacs_generate_script () {
1145 # Construct a little test script here for the benefit of the user,
1146 # (who can easily run "run_emacs" to get the same emacs environment
1147 # for investigating any failures).
1148 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
1151 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
1153 # Here's what we are using here:
1155 # --quick Use minimal customization. This implies --no-init-file,
1156 # --no-site-file and (emacs 24) --no-site-lisp
1158 # --directory Ensure that the local elisp sources are found
1160 # --load Force loading of notmuch.el and test-lib.el
1162 exec ${TEST_EMACS} --quick \
1163 --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
1164 --directory "$TEST_DIRECTORY" --load test-lib.el \
1167 chmod a+x "$TMP_DIRECTORY/run_emacs"
1171 # test dependencies beforehand to avoid the waiting loop below
1172 missing_dependencies=
1173 test_require_external_prereq dtach || missing_dependencies=1
1174 test_require_external_prereq emacs || missing_dependencies=1
1175 test_require_external_prereq ${TEST_EMACSCLIENT} || missing_dependencies=1
1176 test -z "$missing_dependencies" || return
1178 if [ -z "$EMACS_SERVER" ]; then
1179 emacs_tests="${this_test_bare}.el"
1180 if [ -f "$TEST_DIRECTORY/$emacs_tests" ]; then
1181 load_emacs_tests="--eval '(load \"$emacs_tests\")'"
1185 server_name="notmuch-test-suite-$$"
1186 # start a detached session with an emacs server
1187 # user's TERM (or 'vt100' in case user's TERM is known dumb
1188 # or unknown) is given to dtach which assumes a minimally
1189 # VT100-compatible terminal -- and emacs inherits that
1190 TERM=$SMART_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
1191 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
1192 --no-window-system \
1194 --eval '(setq server-name \"$server_name\")' \
1195 --eval '(server-start)' \
1196 --eval '(orphan-watchdog $$)'" || return
1197 EMACS_SERVER="$server_name"
1198 # wait until the emacs server is up
1199 until test_emacs '()' >/dev/null 2>/dev/null; do
1204 # Clear test-output output file. Most Emacs tests end with a
1205 # call to (test-output). If the test code fails with an
1206 # exception before this call, the output file won't get
1207 # updated. Since we don't want to compare against an output
1208 # file from another test, so start out with an empty file.
1212 ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(notmuch-test-progn $@)"
1216 # Note: if there is need to print debug information from python program,
1217 # use stdout = os.fdopen(6, 'w') or stderr = os.fdopen(7, 'w')
1218 PYTHONPATH="$TEST_DIRECTORY/../bindings/python${PYTHONPATH:+:$PYTHONPATH}" \
1219 $NOTMUCH_PYTHON -B - > OUTPUT
1223 MAIL_DIR=$MAIL_DIR ruby -I $TEST_DIRECTORY/../bindings/ruby> OUTPUT
1227 exec_file="test${test_count}"
1228 test_file="${exec_file}.c"
1230 ${TEST_CC} ${TEST_CFLAGS} -I${TEST_DIRECTORY} -I${TEST_DIRECTORY}/../lib -o ${exec_file} ${test_file} -L${TEST_DIRECTORY}/../lib/ -lnotmuch -ltalloc
1231 echo "== stdout ==" > OUTPUT.stdout
1232 echo "== stderr ==" > OUTPUT.stderr
1233 ./${exec_file} "$@" 1>>OUTPUT.stdout 2>>OUTPUT.stderr
1234 notmuch_dir_sanitize OUTPUT.stdout OUTPUT.stderr > OUTPUT
1238 # Creates a script that counts how much time it is executed and calls
1239 # notmuch. $notmuch_counter_command is set to the path to the
1240 # generated script. Use notmuch_counter_value() function to get the
1241 # current counter value.
1242 notmuch_counter_reset () {
1243 notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
1244 if [ ! -x "$notmuch_counter_command" ]; then
1245 notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
1246 cat >"$notmuch_counter_command" <<EOF || return
1249 read count < "$notmuch_counter_state_path"
1250 echo \$((count + 1)) > "$notmuch_counter_state_path"
1254 chmod +x "$notmuch_counter_command" || return
1257 echo 0 > "$notmuch_counter_state_path"
1260 # Returns the current notmuch counter value.
1261 notmuch_counter_value () {
1262 if [ -r "$notmuch_counter_state_path" ]; then
1263 read count < "$notmuch_counter_state_path"
1270 test_reset_state_ () {
1271 test -z "$test_init_done_" && test_init_
1273 test_subtest_known_broken_=
1274 test_subtest_missing_external_prereq_=()
1277 # called once before the first subtest
1281 # skip all tests if there were external prerequisites missing during init
1282 test_check_missing_external_prereqs_ "all tests in $this_test" && test_done
1286 . ./test-lib-common.sh || exit 1
1288 emacs_generate_script
1291 # Use -P to resolve symlinks in our working directory so that the cwd
1292 # in subprocesses like git equals our $PWD (for pathname comparisons).
1293 cd -P "$test" || error "Cannot set up test environment"
1295 if test "$verbose" = "t"
1299 exec 4>test.output 3>&4
1302 for skp in $NOTMUCH_SKIP_TESTS
1305 for skp in $NOTMUCH_SKIP_TESTS
1307 case "$this_test" in
1312 case "$this_test_bare" in
1320 say_color skip >&3 "skipping test $this_test altogether"
1321 say_color skip "skip all tests in $this_test"
1326 # Provide an implementation of the 'yes' utility
1341 # Fix some commands on Windows
1344 # Windows has its own (incompatible) sort and find
1354 # git sees Windows-style pwd
1358 # no POSIX permissions
1359 # backslashes in pathspec are converted to '/'
1360 # exec does not inherit the PID
1363 test_set_prereq POSIXPERM
1364 test_set_prereq BSLASHPSPEC
1365 test_set_prereq EXECKEEPSPID
1369 test -z "$NO_PERL" && test_set_prereq PERL
1370 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1372 # test whether the filesystem supports symbolic links
1373 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1376 # convert variable from configure to more convenient form
1377 case "$NOTMUCH_DEFAULT_XAPIAN_BACKEND" in
1385 error "Unknown Xapian backend $NOTMUCH_DEFAULT_XAPIAN_BACKEND"
1387 # declare prerequisites for external binaries used in tests
1388 test_declare_external_prereq dtach
1389 test_declare_external_prereq emacs
1390 test_declare_external_prereq ${TEST_EMACSCLIENT}
1391 test_declare_external_prereq ${TEST_GDB}
1392 test_declare_external_prereq gpg
1393 test_declare_external_prereq openssl
1394 test_declare_external_prereq gpgsm
1395 test_declare_external_prereq ${NOTMUCH_PYTHON}