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_CC=${TEST_CC:-cc}
84 TEST_CFLAGS=${TEST_CFLAGS:-"-g -O0"}
86 # Protect ourselves from common misconfiguration to export
87 # CDPATH into the environment
93 unset ALTERNATE_EDITOR
97 # A regexp to match 5 and 40 hexdigits
98 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
99 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
101 _x04='[0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
102 _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
104 # Each test should start with something like this, after copyright notices:
106 # test_description='Description of this test...
107 # This test checks if command xyzzy does the right thing...
109 # . ./test-lib.sh || exit 1
111 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
112 TERM=$ORIGINAL_TERM &&
115 tput bold >/dev/null 2>&1 &&
116 tput setaf 1 >/dev/null 2>&1 &&
117 tput sgr0 >/dev/null 2>&1
121 while test "$#" -ne 0
124 -d|--d|--de|--deb|--debu|--debug)
126 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
127 immediate=t; shift ;;
128 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
129 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
130 -h|--h|--he|--hel|--help)
132 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
134 -q|--q|--qu|--qui|--quie|--quiet)
137 with_dashes=t; shift ;;
143 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
144 valgrind=t; verbose=t; shift ;;
146 shift ;; # was handled already
148 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
151 echo "error: unknown test option '$1'" >&2; exit 1 ;;
155 if test -n "$debug"; then
157 printf " %-4s" "[$((test_count - 1))]"
165 if test -n "$color"; then
171 error) tput bold; tput setaf 1;; # bold red
172 skip) tput bold; tput setaf 2;; # bold green
173 pass) tput setaf 2;; # green
174 info) tput setaf 3;; # brown
175 *) test -n "$quiet" && return;;
186 test -z "$1" && test -n "$quiet" && return
195 say_color error "error: $*\n"
204 test "${test_description}" != "" ||
205 error "Test script did not set test_description."
207 if test "$help" = "t"
209 echo "Tests ${test_description}"
213 test_description_printed=
214 print_test_description ()
216 test -z "$test_description_printed" || return 0
218 echo $this_test: "Testing ${test_description}"
219 test_description_printed=1
221 if [ -z "$NOTMUCH_TEST_QUIET" ]
223 print_test_description
236 rm -rf "$TEST_TMPDIR"
241 if test -n "$GIT_EXIT_OK"
246 say_color error '%-6s' FATAL
247 echo " $test_subtest_name"
249 echo "Unexpected exit while executing $0. Exit code $code."
256 echo >&6 "FATAL: $0: interrupted by signal" $((code - 128))
263 say_color error '%-6s' FATAL
266 echo "Unexpected exit while executing $0."
271 # Note: TEST_TMPDIR *NOT* exported!
272 TEST_TMPDIR=$(mktemp -d "${TMPDIR:-/tmp}/notmuch-test-$$.XXXXXX")
273 trap 'trap_exit' EXIT
274 trap 'trap_signal' HUP INT TERM
276 test_decode_color () {
277 sed -e 's/.\[1m/<WHITE>/g' \
278 -e 's/.\[31m/<RED>/g' \
279 -e 's/.\[32m/<GREEN>/g' \
280 -e 's/.\[33m/<YELLOW>/g' \
281 -e 's/.\[34m/<BLUE>/g' \
282 -e 's/.\[35m/<MAGENTA>/g' \
283 -e 's/.\[36m/<CYAN>/g' \
284 -e 's/.\[m/<RESET>/g'
296 sed -e 's/$/Q/' | tr Q '\015'
300 tr '\015' Q | sed -e 's/Q$//'
303 # Generate a new message in the mail directory, with a unique message
304 # ID and subject. The message is not added to the index.
306 # After this function returns, the filename of the generated message
307 # is available as $gen_msg_filename and the message ID is available as
310 # This function supports named parameters with the bash syntax for
311 # assigning a value to an associative array ([name]=value). The
312 # supported parameters are:
314 # [dir]=directory/of/choice
316 # Generate the message in directory 'directory/of/choice' within
317 # the mail store. The directory will be created if necessary.
321 # Store the message in file 'name'. The default is to store it
322 # in 'msg-<count>', where <count> is three-digit number of the
327 # Text to use as the body of the email message
329 # '[from]="Some User <user@example.com>"'
330 # '[to]="Some User <user@example.com>"'
331 # '[subject]="Subject of email message"'
332 # '[date]="RFC 822 Date"'
334 # Values for email headers. If not provided, default values will
335 # be generated instead.
337 # '[cc]="Some User <user@example.com>"'
338 # [reply-to]=some-address
339 # [in-reply-to]=<message-id>
340 # [references]=<message-id>
341 # [content-type]=content-type-specification
342 # '[header]=full header line, including keyword'
344 # Additional values for email headers. If these are not provided
345 # then the relevant headers will simply not appear in the
350 # Controls the message-id of the created message.
356 # This is our (bash-specific) magic for doing named parameters
357 local -A template="($@)"
358 local additional_headers
360 gen_msg_cnt=$((gen_msg_cnt + 1))
361 if [ -z "${template[filename]}" ]; then
362 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
364 gen_msg_name=${template[filename]}
367 if [ -z "${template[id]}" ]; then
368 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
370 gen_msg_id="${template[id]}"
373 if [ -z "${template[dir]}" ]; then
374 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
376 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
377 mkdir -p "$(dirname "$gen_msg_filename")"
380 if [ -z "${template[body]}" ]; then
381 template[body]="This is just a test message (#${gen_msg_cnt})"
384 if [ -z "${template[from]}" ]; then
385 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
388 if [ -z "${template[to]}" ]; then
389 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
392 if [ -z "${template[subject]}" ]; then
393 if [ -n "$test_subtest_name" ]; then
394 template[subject]="$test_subtest_name"
396 template[subject]="Test message #${gen_msg_cnt}"
398 elif [ "${template[subject]}" = "@FORCE_EMPTY" ]; then
402 if [ -z "${template[date]}" ]; then
403 # we use decreasing timestamps here for historical reasons;
404 # the existing test suite when we converted to unique timestamps just
405 # happened to have signicantly fewer failures with that choice.
406 local date_secs=$((978709437 - gen_msg_cnt))
407 # printf %(..)T is bash 4.2+ feature. use perl fallback if needed...
408 TZ=UTC printf -v template[date] "%(%a, %d %b %Y %T %z)T" $date_secs 2>/dev/null ||
409 template[date]=`perl -le 'use POSIX "strftime";
410 @time = gmtime '"$date_secs"';
411 print strftime "%a, %d %b %Y %T +0000", @time'`
414 additional_headers=""
415 if [ ! -z "${template[header]}" ]; then
416 additional_headers="${template[header]}
417 ${additional_headers}"
420 if [ ! -z "${template[reply-to]}" ]; then
421 additional_headers="Reply-To: ${template[reply-to]}
422 ${additional_headers}"
425 if [ ! -z "${template[in-reply-to]}" ]; then
426 additional_headers="In-Reply-To: ${template[in-reply-to]}
427 ${additional_headers}"
430 if [ ! -z "${template[cc]}" ]; then
431 additional_headers="Cc: ${template[cc]}
432 ${additional_headers}"
435 if [ ! -z "${template[bcc]}" ]; then
436 additional_headers="Bcc: ${template[bcc]}
437 ${additional_headers}"
440 if [ ! -z "${template[references]}" ]; then
441 additional_headers="References: ${template[references]}
442 ${additional_headers}"
445 if [ ! -z "${template[content-type]}" ]; then
446 additional_headers="Content-Type: ${template[content-type]}
447 ${additional_headers}"
450 if [ ! -z "${template[content-transfer-encoding]}" ]; then
451 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
452 ${additional_headers}"
455 # Note that in the way we're setting it above and using it below,
456 # `additional_headers' will also serve as the header / body separator
457 # (empty line in between).
459 cat <<EOF >"$gen_msg_filename"
460 From: ${template[from]}
462 Message-Id: <${gen_msg_id}>
463 Subject: ${template[subject]}
464 Date: ${template[date]}
465 ${additional_headers}
470 # Generate a new message and add it to the database.
472 # All of the arguments and return values supported by generate_message
473 # are also supported here, so see that function for details.
476 generate_message "$@" &&
477 notmuch new > /dev/null
480 # Deliver a message with emacs and add it to the database
482 # Uses emacs to generate and deliver a message to the mail store.
483 # Accepts arbitrary extra emacs/elisp functions to modify the message
484 # before sending, which is useful to doing things like attaching files
485 # to the message and encrypting/signing.
486 emacs_deliver_message ()
491 # before we can send a message, we have to prepare the FCC maildir
492 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
493 # eval'ing smtp-dummy --background will set smtp_dummy_pid
495 eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
496 test -n "$smtp_dummy_pid" || return 1
499 "(let ((message-send-mail-function 'message-smtpmail-send-it)
500 (mail-host-address \"example.com\")
501 (smtpmail-smtp-server \"localhost\")
502 (smtpmail-smtp-service \"25025\"))
505 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
506 (message-goto-subject)
507 (insert \"${subject}\")
511 (notmuch-mua-send-and-exit))"
513 # In case message was sent properly, client waits for confirmation
514 # before exiting and resuming control here; therefore making sure
515 # that server exits by sending (KILL) signal to it is safe.
516 kill -9 $smtp_dummy_pid
517 notmuch new >/dev/null
520 # Pretend to deliver a message with emacs. Really save it to a file
521 # and add it to the database
523 # Uses emacs to generate and deliver a message to the mail store.
524 # Accepts arbitrary extra emacs/elisp functions to modify the message
525 # before sending, which is useful to doing things like attaching files
526 # to the message and encrypting/signing.
532 # before we can send a message, we have to prepare the FCC maildir
533 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
536 "(let ((message-send-mail-function (lambda () t))
537 (mail-host-address \"example.com\"))
540 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
541 (message-goto-subject)
542 (insert \"${subject}\")
546 (notmuch-mua-send-and-exit))" || return 1
547 notmuch new >/dev/null
550 # Add an existing, fixed corpus of email to the database.
552 # $1 is the corpus dir under corpora to add, using "default" if unset.
554 # The default corpus is based on about 50 messages from early in the
555 # history of the notmuch mailing list, which allows for reliably
556 # testing commands that need to operate on a not-totally-trivial
557 # number of messages.
563 if [ -d $TEST_DIRECTORY/corpora.mail/$corpus ]; then
564 cp -a $TEST_DIRECTORY/corpora.mail/$corpus ${MAIL_DIR}
566 cp -a $TEST_DIRECTORY/corpora/$corpus ${MAIL_DIR}
567 notmuch new >/dev/null || die "'notmuch new' failed while adding email corpus"
568 mkdir -p $TEST_DIRECTORY/corpora.mail
569 cp -a ${MAIL_DIR} $TEST_DIRECTORY/corpora.mail/$corpus
573 test_begin_subtest ()
575 if [ -n "$inside_subtest" ]; then
576 exec 1>&6 2>&7 # Restore stdout and stderr
577 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
579 test_subtest_name="$1"
581 # Redirect test output to the previously prepared file descriptors
582 # 3 and 4 (see below)
583 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
588 # Pass test if two arguments match
590 # Note: Unlike all other test_expect_* functions, this function does
591 # not accept a test name. Instead, the caller should call
592 # test_begin_subtest before calling this function in order to set the
596 exec 1>&6 2>&7 # Restore stdout and stderr
598 test "$#" = 3 && { prereq=$1; shift; } || prereq=
600 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
604 if ! test_skip "$test_subtest_name"
606 if [ "$output" = "$expected" ]; then
609 testname=$this_test.$test_count
610 echo "$expected" > $testname.expected
611 echo "$output" > $testname.output
612 test_failure_ "$(diff -u $testname.expected $testname.output)"
617 # Like test_expect_equal, but takes two filenames.
618 test_expect_equal_file ()
620 exec 1>&6 2>&7 # Restore stdout and stderr
622 test "$#" = 3 && { prereq=$1; shift; } || prereq=
624 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
627 basename1=`basename "$file1"`
629 basename2=`basename "$file2"`
630 if ! test_skip "$test_subtest_name"
632 if diff -q "$file1" "$file2" >/dev/null ; then
635 testname=$this_test.$test_count
636 cp "$file1" "$testname.$basename1"
637 cp "$file2" "$testname.$basename2"
638 test_failure_ "$(diff -u "$testname.$basename1" "$testname.$basename2")"
643 # Like test_expect_equal, but arguments are JSON expressions to be
644 # canonicalized before diff'ing. If an argument cannot be parsed, it
645 # is used unchanged so that there's something to diff against.
646 test_expect_equal_json () {
647 # The test suite forces LC_ALL=C, but this causes Python 3 to
648 # decode stdin as ASCII. We need to read JSON in UTF-8, so
649 # override Python's stdio encoding defaults.
650 output=$(echo "$1" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
652 expected=$(echo "$2" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
655 test_expect_equal "$output" "$expected" "$@"
658 # Sort the top-level list of JSON data from stdin.
660 PYTHONIOENCODING=utf-8 python -c \
661 "import sys, json; json.dump(sorted(json.load(sys.stdin)),sys.stdout)"
664 test_emacs_expect_t () {
665 test "$#" = 2 && { prereq=$1; shift; } || prereq=
667 error "bug in the test script: not 1 or 2 parameters to test_emacs_expect_t"
670 if ! test_skip "$test_subtest_name"
672 test_emacs "(notmuch-test-run $1)" >/dev/null
674 # Restore state after the test.
675 exec 1>&6 2>&7 # Restore stdout and stderr
678 # Report success/failure.
684 test_failure_ "${result}"
687 # Restore state after the (non) test.
688 exec 1>&6 2>&7 # Restore stdout and stderr
695 notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
700 # this relies on the default format being batch-tag, otherwise some tests will break
701 notmuch dump --include=tags "${@}" | sed '/^#/d' | sort
704 notmuch_search_sanitize ()
706 perl -pe 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
709 notmuch_search_files_sanitize ()
714 notmuch_dir_sanitize ()
716 sed -e "s,$MAIL_DIR,MAIL_DIR," -e "s,${PWD},CWD,g" "$@"
719 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
720 notmuch_show_sanitize ()
722 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
724 notmuch_show_sanitize_all ()
727 -e 's| filename:.*| filename:XXXXX|' \
728 -e 's| id:[^ ]* | id:XXXXX |' | \
729 notmuch_date_sanitize
732 notmuch_json_show_sanitize ()
735 -e 's|"id": "[^"]*",|"id": "XXXXX",|g' \
736 -e 's|"Date": "Fri, 05 Jan 2001 [^"]*0000"|"Date": "GENERATED_DATE"|g' \
737 -e 's|"filename": "signature.asc",||g' \
738 -e 's|"filename": "/[^"]*",|"filename": "YYYYY",|g' \
739 -e 's|"timestamp": 97.......|"timestamp": 42|g' \
740 -e 's|"content-length": [1-9][0-9]*|"content-length": "NONZERO"|g'
743 notmuch_emacs_error_sanitize ()
751 -e 's/^\[.*\]$/[XXX]/' \
752 -e "s|^\(command: \)\{0,1\}/.*/$command|\1YYY/$command|"
755 notmuch_date_sanitize ()
758 -e 's/^Date: Fri, 05 Jan 2001 .*0000/Date: GENERATED_DATE/'
761 notmuch_uuid_sanitize ()
763 sed 's/[0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}/UUID/g'
766 notmuch_built_with_sanitize ()
768 sed 's/^built_with[.]\(.*\)=.*$/built_with.\1=something/'
771 notmuch_config_sanitize ()
773 notmuch_dir_sanitize | notmuch_built_with_sanitize
776 # End of notmuch helper functions
778 # Use test_set_prereq to tell that a particular prerequisite is available.
779 # The prerequisite can later be checked for in two ways:
781 # - Explicitly using test_have_prereq.
783 # - Implicitly by specifying the prerequisite tag in the calls to
784 # test_expect_{success,failure,code}.
786 # The single parameter is the prerequisite tag (a simple word, in all
787 # capital letters by convention).
790 satisfied="$satisfied$1 "
794 test_have_prereq () {
803 declare -A test_missing_external_prereq_
804 declare -A test_subtest_missing_external_prereq_
806 # declare prerequisite for the given external binary
807 test_declare_external_prereq () {
809 test "$#" = 2 && name=$2 || name="$binary(1)"
811 if ! hash $binary 2>/dev/null; then
812 test_missing_external_prereq_["${binary}"]=t
815 test_subtest_missing_external_prereq_[\"${name}\"]=t
821 # Explicitly require external prerequisite. Useful when binary is
822 # called indirectly (e.g. from emacs).
823 # Returns success if dependency is available, failure otherwise.
824 test_require_external_prereq () {
826 if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
827 # dependency is missing, call the replacement function to note it
834 # You are not expected to call test_ok_ and test_failure_ directly, use
835 # the text_expect_* functions instead.
838 if test "$test_subtest_known_broken_" = "t"; then
839 test_known_broken_ok_
842 test_success=$(($test_success + 1))
843 if test -n "$NOTMUCH_TEST_QUIET"; then
846 say_color pass "%-6s" "PASS"
847 echo " $test_subtest_name"
851 if test "$test_subtest_known_broken_" = "t"; then
852 test_known_broken_failure_ "$@"
855 test_failure=$(($test_failure + 1))
856 print_test_description
857 test_failure_message_ "FAIL" "$test_subtest_name" "$@"
858 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
862 test_failure_message_ () {
863 say_color error "%-6s" "$1"
866 echo "$@" | sed -e 's/^/ /'
867 if test "$verbose" != "t"; then cat test.output; fi
870 test_known_broken_ok_ () {
872 test_fixed=$(($test_fixed+1))
873 say_color pass "%-6s" "FIXED"
874 echo " $test_subtest_name"
877 test_known_broken_failure_ () {
879 test_broken=$(($test_broken+1))
880 test_failure_message_ "BROKEN" "$test_subtest_name" "$@"
885 test "$debug" = "" || eval "$1"
890 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
893 eval >&3 2>&4 "$test_cleanup"
898 test_count=$(($test_count+1))
900 for skp in $NOTMUCH_SKIP_TESTS
902 case $this_test.$test_count in
907 case $this_test_bare.$test_count in
913 if test -z "$to_skip" && test -n "$prereq" &&
914 ! test_have_prereq "$prereq"
920 test_report_skip_ "$@"
923 test_check_missing_external_prereqs_ "$@"
928 test_check_missing_external_prereqs_ () {
929 if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
930 say_color skip >&1 "missing prerequisites: "
931 echo ${!test_subtest_missing_external_prereq_[@]} >&1
932 test_report_skip_ "$@"
938 test_report_skip_ () {
940 say_color skip >&3 "skipping test:"
942 say_color skip "%-6s" "SKIP"
946 test_subtest_known_broken () {
947 test_subtest_known_broken_=t
950 test_expect_success () {
951 test "$#" = 3 && { prereq=$1; shift; } || prereq=
953 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
954 test_subtest_name="$1"
960 # test_run_ may update missing external prerequisites
961 test_check_missing_external_prereqs_ "$@" ||
962 if [ "$run_ret" = 0 -a "$eval_ret" = 0 ]
971 test_expect_code () {
972 test "$#" = 4 && { prereq=$1; shift; } || prereq=
974 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
975 test_subtest_name="$2"
981 # test_run_ may update missing external prerequisites,
982 test_check_missing_external_prereqs_ "$@" ||
983 if [ "$run_ret" = 0 -a "$eval_ret" = "$1" ]
987 test_failure_ "exit code $eval_ret, expected $1" "$3"
992 # test_external runs external test scripts that provide continuous
993 # test output about their progress, and succeeds/fails on
994 # zero/non-zero exit code. It outputs the test output on stdout even
995 # in non-verbose mode, and announces the external script with "* run
996 # <n>: ..." before running it. When providing relative paths, keep in
997 # mind that all scripts run in "trash directory".
998 # Usage: test_external description command arguments...
999 # Example: test_external 'Perl API' perl ../path/to/test.pl
1001 test "$#" = 4 && { prereq=$1; shift; } || prereq=
1003 error >&6 "bug in the test script: not 3 or 4 parameters to test_external"
1004 test_subtest_name="$1"
1007 if ! test_skip "$test_subtest_name" "$@"
1009 # Announce the script to reduce confusion about the
1010 # test output that follows.
1011 say_color "" " run $test_count: $descr ($*)"
1012 # Run command; redirect its stderr to &4 as in
1013 # test_run_, but keep its stdout on our stdout even in
1025 # Like test_external, but in addition tests that the command generated
1026 # no output on stderr.
1027 test_external_without_stderr () {
1028 # The temporary file has no (and must have no) security
1030 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
1031 stderr="$tmp/git-external-stderr.$$.tmp"
1032 test_external "$@" 4> "$stderr"
1033 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
1034 test_subtest_name="no stderr: $1"
1036 if [ ! -s "$stderr" ]; then
1040 if [ "$verbose" = t ]; then
1041 output=`echo; echo Stderr is:; cat "$stderr"`
1045 # rm first in case test_failure exits.
1047 test_failure_ "$@" "$output"
1051 # This is not among top-level (test_expect_success)
1052 # but is a prefix that can be used in the test script, like:
1054 # test_expect_success 'complain and die' '
1056 # do something else &&
1057 # test_must_fail git checkout ../outerspace
1060 # Writing this as "! git checkout ../outerspace" is wrong, because
1061 # the failure could be due to a segv. We want a controlled failure.
1065 test $? -gt 0 -a $? -le 129 -o $? -gt 192
1068 # test_cmp is a helper function to compare actual and expected output.
1069 # You can use it like:
1071 # test_expect_success 'foo works' '
1072 # echo expected >expected &&
1074 # test_cmp expected actual
1077 # This could be written as either "cmp" or "diff -u", but:
1078 # - cmp's output is not nearly as easy to read as diff -u
1079 # - not all diff versions understand "-u"
1085 # This function can be used to schedule some commands to be run
1086 # unconditionally at the end of the test to restore sanity:
1088 # test_expect_success 'test core.capslock' '
1089 # git config core.capslock true &&
1090 # test_when_finished "git config --unset core.capslock" &&
1094 # That would be roughly equivalent to
1096 # test_expect_success 'test core.capslock' '
1097 # git config core.capslock true &&
1099 # git config --unset core.capslock
1102 # except that the greeting and config --unset must both succeed for
1105 test_when_finished () {
1107 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
1112 test_results_dir="$TEST_DIRECTORY/test-results"
1113 mkdir -p "$test_results_dir"
1114 test_results_path="$test_results_dir/$this_test"
1116 echo "total $test_count" >> $test_results_path
1117 echo "success $test_success" >> $test_results_path
1118 echo "fixed $test_fixed" >> $test_results_path
1119 echo "broken $test_broken" >> $test_results_path
1120 echo "failed $test_failure" >> $test_results_path
1121 echo "" >> $test_results_path
1123 [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'
1125 if [ "$test_failure" = "0" ]; then
1126 if [ "$test_broken" = "0" ]; then
1127 rm -rf "$remove_tmp"
1135 emacs_generate_script () {
1136 # Construct a little test script here for the benefit of the user,
1137 # (who can easily run "run_emacs" to get the same emacs environment
1138 # for investigating any failures).
1139 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
1142 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
1144 # Here's what we are using here:
1146 # --quick Use minimal customization. This implies --no-init-file,
1147 # --no-site-file and (emacs 24) --no-site-lisp
1149 # --directory Ensure that the local elisp sources are found
1151 # --load Force loading of notmuch.el and test-lib.el
1153 exec ${TEST_EMACS} --quick \
1154 --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
1155 --directory "$TEST_DIRECTORY" --load test-lib.el \
1158 chmod a+x "$TMP_DIRECTORY/run_emacs"
1162 # test dependencies beforehand to avoid the waiting loop below
1163 missing_dependencies=
1164 test_require_external_prereq dtach || missing_dependencies=1
1165 test_require_external_prereq emacs || missing_dependencies=1
1166 test_require_external_prereq ${TEST_EMACSCLIENT} || missing_dependencies=1
1167 test -z "$missing_dependencies" || return
1169 if [ -z "$EMACS_SERVER" ]; then
1170 emacs_tests="${this_test_bare}.el"
1171 if [ -f "$TEST_DIRECTORY/$emacs_tests" ]; then
1172 load_emacs_tests="--eval '(load \"$emacs_tests\")'"
1176 server_name="notmuch-test-suite-$$"
1177 # start a detached session with an emacs server
1178 # user's TERM (or 'vt100' in case user's TERM is known dumb
1179 # or unknown) is given to dtach which assumes a minimally
1180 # VT100-compatible terminal -- and emacs inherits that
1181 TERM=$SMART_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
1182 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
1183 --no-window-system \
1185 --eval '(setq server-name \"$server_name\")' \
1186 --eval '(server-start)' \
1187 --eval '(orphan-watchdog $$)'" || return
1188 EMACS_SERVER="$server_name"
1189 # wait until the emacs server is up
1190 until test_emacs '()' >/dev/null 2>/dev/null; do
1195 # Clear test-output output file. Most Emacs tests end with a
1196 # call to (test-output). If the test code fails with an
1197 # exception before this call, the output file won't get
1198 # updated. Since we don't want to compare against an output
1199 # file from another test, so start out with an empty file.
1203 ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(notmuch-test-progn $@)"
1207 # Note: if there is need to print debug information from python program,
1208 # use stdout = os.fdopen(6, 'w') or stderr = os.fdopen(7, 'w')
1209 PYTHONPATH="$TEST_DIRECTORY/../bindings/python${PYTHONPATH:+:$PYTHONPATH}" \
1210 $NOTMUCH_PYTHON -B - > OUTPUT
1214 MAIL_DIR=$MAIL_DIR ruby -I $TEST_DIRECTORY/../bindings/ruby> OUTPUT
1218 exec_file="test${test_count}"
1219 test_file="${exec_file}.c"
1221 ${TEST_CC} ${TEST_CFLAGS} -I${TEST_DIRECTORY} -I${TEST_DIRECTORY}/../lib -o ${exec_file} ${test_file} -L${TEST_DIRECTORY}/../lib/ -lnotmuch -ltalloc
1222 echo "== stdout ==" > OUTPUT.stdout
1223 echo "== stderr ==" > OUTPUT.stderr
1224 ./${exec_file} "$@" 1>>OUTPUT.stdout 2>>OUTPUT.stderr
1225 notmuch_dir_sanitize OUTPUT.stdout OUTPUT.stderr > OUTPUT
1229 # Creates a script that counts how much time it is executed and calls
1230 # notmuch. $notmuch_counter_command is set to the path to the
1231 # generated script. Use notmuch_counter_value() function to get the
1232 # current counter value.
1233 notmuch_counter_reset () {
1234 notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
1235 if [ ! -x "$notmuch_counter_command" ]; then
1236 notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
1237 cat >"$notmuch_counter_command" <<EOF || return
1240 read count < "$notmuch_counter_state_path"
1241 echo \$((count + 1)) > "$notmuch_counter_state_path"
1245 chmod +x "$notmuch_counter_command" || return
1248 echo 0 > "$notmuch_counter_state_path"
1251 # Returns the current notmuch counter value.
1252 notmuch_counter_value () {
1253 if [ -r "$notmuch_counter_state_path" ]; then
1254 read count < "$notmuch_counter_state_path"
1261 test_reset_state_ () {
1262 test -z "$test_init_done_" && test_init_
1264 test_subtest_known_broken_=
1265 test_subtest_missing_external_prereq_=()
1268 # called once before the first subtest
1272 # skip all tests if there were external prerequisites missing during init
1273 test_check_missing_external_prereqs_ "all tests in $this_test" && test_done
1277 . ./test-lib-common.sh || exit 1
1279 emacs_generate_script
1282 # Use -P to resolve symlinks in our working directory so that the cwd
1283 # in subprocesses like git equals our $PWD (for pathname comparisons).
1284 cd -P "$test" || error "Cannot set up test environment"
1286 if test "$verbose" = "t"
1290 exec 4>test.output 3>&4
1293 for skp in $NOTMUCH_SKIP_TESTS
1296 for skp in $NOTMUCH_SKIP_TESTS
1298 case "$this_test" in
1303 case "$this_test_bare" in
1311 say_color skip >&3 "skipping test $this_test altogether"
1312 say_color skip "skip all tests in $this_test"
1317 # Provide an implementation of the 'yes' utility
1332 # Fix some commands on Windows
1335 # Windows has its own (incompatible) sort and find
1345 # git sees Windows-style pwd
1349 # no POSIX permissions
1350 # backslashes in pathspec are converted to '/'
1351 # exec does not inherit the PID
1354 test_set_prereq POSIXPERM
1355 test_set_prereq BSLASHPSPEC
1356 test_set_prereq EXECKEEPSPID
1360 test -z "$NO_PERL" && test_set_prereq PERL
1361 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1363 # test whether the filesystem supports symbolic links
1364 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1367 # convert variable from configure to more convenient form
1368 case "$NOTMUCH_DEFAULT_XAPIAN_BACKEND" in
1376 error "Unknown Xapian backend $NOTMUCH_DEFAULT_XAPIAN_BACKEND"
1378 # declare prerequisites for external binaries used in tests
1379 test_declare_external_prereq dtach
1380 test_declare_external_prereq emacs
1381 test_declare_external_prereq ${TEST_EMACSCLIENT}
1382 test_declare_external_prereq gdb
1383 test_declare_external_prereq gpg
1384 test_declare_external_prereq openssl
1385 test_declare_external_prereq gpgsm
1386 test_declare_external_prereq ${NOTMUCH_PYTHON}