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
234 _shutdown_gpg_agent () {
235 if [ ${NOTMUCH_HAVE_GPGCONF_SOCKETDIR} = 1 ]; then
236 gpgconf --kill gpg-agent
237 gpgconf --remove-socketdir
246 rm -rf "$TEST_TMPDIR"
251 if test -n "$GIT_EXIT_OK"
256 say_color error '%-6s' FATAL
257 echo " $test_subtest_name"
259 echo "Unexpected exit while executing $0. Exit code $code."
266 echo >&6 "FATAL: $0: interrupted by signal" $((code - 128))
273 say_color error '%-6s' FATAL
276 echo "Unexpected exit while executing $0."
281 # Note: TEST_TMPDIR *NOT* exported!
282 TEST_TMPDIR=$(mktemp -d "${TMPDIR:-/tmp}/notmuch-test-$$.XXXXXX")
283 trap 'trap_exit' EXIT
284 trap 'trap_signal' HUP INT TERM
286 test_decode_color () {
287 sed -e 's/.\[1m/<WHITE>/g' \
288 -e 's/.\[31m/<RED>/g' \
289 -e 's/.\[32m/<GREEN>/g' \
290 -e 's/.\[33m/<YELLOW>/g' \
291 -e 's/.\[34m/<BLUE>/g' \
292 -e 's/.\[35m/<MAGENTA>/g' \
293 -e 's/.\[36m/<CYAN>/g' \
294 -e 's/.\[m/<RESET>/g'
306 sed -e 's/$/Q/' | tr Q '\015'
310 tr '\015' Q | sed -e 's/Q$//'
313 # Generate a new message in the mail directory, with a unique message
314 # ID and subject. The message is not added to the index.
316 # After this function returns, the filename of the generated message
317 # is available as $gen_msg_filename and the message ID is available as
320 # This function supports named parameters with the bash syntax for
321 # assigning a value to an associative array ([name]=value). The
322 # supported parameters are:
324 # [dir]=directory/of/choice
326 # Generate the message in directory 'directory/of/choice' within
327 # the mail store. The directory will be created if necessary.
331 # Store the message in file 'name'. The default is to store it
332 # in 'msg-<count>', where <count> is three-digit number of the
337 # Text to use as the body of the email message
339 # '[from]="Some User <user@example.com>"'
340 # '[to]="Some User <user@example.com>"'
341 # '[subject]="Subject of email message"'
342 # '[date]="RFC 822 Date"'
344 # Values for email headers. If not provided, default values will
345 # be generated instead.
347 # '[cc]="Some User <user@example.com>"'
348 # [reply-to]=some-address
349 # [in-reply-to]=<message-id>
350 # [references]=<message-id>
351 # [content-type]=content-type-specification
352 # '[header]=full header line, including keyword'
354 # Additional values for email headers. If these are not provided
355 # then the relevant headers will simply not appear in the
360 # Controls the message-id of the created message.
366 # This is our (bash-specific) magic for doing named parameters
367 local -A template="($@)"
368 local additional_headers
370 gen_msg_cnt=$((gen_msg_cnt + 1))
371 if [ -z "${template[filename]}" ]; then
372 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
374 gen_msg_name=${template[filename]}
377 if [ -z "${template[id]}" ]; then
378 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
380 gen_msg_id="${template[id]}"
383 if [ -z "${template[dir]}" ]; then
384 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
386 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
387 mkdir -p "$(dirname "$gen_msg_filename")"
390 if [ -z "${template[body]}" ]; then
391 template[body]="This is just a test message (#${gen_msg_cnt})"
394 if [ -z "${template[from]}" ]; then
395 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
398 if [ -z "${template[to]}" ]; then
399 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
402 if [ -z "${template[subject]}" ]; then
403 if [ -n "$test_subtest_name" ]; then
404 template[subject]="$test_subtest_name"
406 template[subject]="Test message #${gen_msg_cnt}"
408 elif [ "${template[subject]}" = "@FORCE_EMPTY" ]; then
412 if [ -z "${template[date]}" ]; then
413 # we use decreasing timestamps here for historical reasons;
414 # the existing test suite when we converted to unique timestamps just
415 # happened to have signicantly fewer failures with that choice.
416 local date_secs=$((978709437 - gen_msg_cnt))
417 # printf %(..)T is bash 4.2+ feature. use perl fallback if needed...
418 TZ=UTC printf -v template[date] "%(%a, %d %b %Y %T %z)T" $date_secs 2>/dev/null ||
419 template[date]=`perl -le 'use POSIX "strftime";
420 @time = gmtime '"$date_secs"';
421 print strftime "%a, %d %b %Y %T +0000", @time'`
424 additional_headers=""
425 if [ ! -z "${template[header]}" ]; then
426 additional_headers="${template[header]}
427 ${additional_headers}"
430 if [ ! -z "${template[reply-to]}" ]; then
431 additional_headers="Reply-To: ${template[reply-to]}
432 ${additional_headers}"
435 if [ ! -z "${template[in-reply-to]}" ]; then
436 additional_headers="In-Reply-To: ${template[in-reply-to]}
437 ${additional_headers}"
440 if [ ! -z "${template[cc]}" ]; then
441 additional_headers="Cc: ${template[cc]}
442 ${additional_headers}"
445 if [ ! -z "${template[bcc]}" ]; then
446 additional_headers="Bcc: ${template[bcc]}
447 ${additional_headers}"
450 if [ ! -z "${template[references]}" ]; then
451 additional_headers="References: ${template[references]}
452 ${additional_headers}"
455 if [ ! -z "${template[content-type]}" ]; then
456 additional_headers="Content-Type: ${template[content-type]}
457 ${additional_headers}"
460 if [ ! -z "${template[content-transfer-encoding]}" ]; then
461 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
462 ${additional_headers}"
465 # Note that in the way we're setting it above and using it below,
466 # `additional_headers' will also serve as the header / body separator
467 # (empty line in between).
469 cat <<EOF >"$gen_msg_filename"
470 From: ${template[from]}
472 Message-Id: <${gen_msg_id}>
473 Subject: ${template[subject]}
474 Date: ${template[date]}
475 ${additional_headers}
480 # Generate a new message and add it to the database.
482 # All of the arguments and return values supported by generate_message
483 # are also supported here, so see that function for details.
486 generate_message "$@" &&
487 notmuch new > /dev/null
490 # Deliver a message with emacs and add it to the database
492 # Uses emacs to generate and deliver a message to the mail store.
493 # Accepts arbitrary extra emacs/elisp functions to modify the message
494 # before sending, which is useful to doing things like attaching files
495 # to the message and encrypting/signing.
496 emacs_deliver_message ()
501 # before we can send a message, we have to prepare the FCC maildir
502 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
503 # eval'ing smtp-dummy --background will set smtp_dummy_pid
505 eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
506 test -n "$smtp_dummy_pid" || return 1
509 "(let ((message-send-mail-function 'message-smtpmail-send-it)
510 (mail-host-address \"example.com\")
511 (smtpmail-smtp-server \"localhost\")
512 (smtpmail-smtp-service \"25025\"))
515 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
516 (message-goto-subject)
517 (insert \"${subject}\")
521 (notmuch-mua-send-and-exit))"
523 # In case message was sent properly, client waits for confirmation
524 # before exiting and resuming control here; therefore making sure
525 # that server exits by sending (KILL) signal to it is safe.
526 kill -9 $smtp_dummy_pid
527 notmuch new >/dev/null
530 # Pretend to deliver a message with emacs. Really save it to a file
531 # and add it to the database
533 # Uses emacs to generate and deliver a message to the mail store.
534 # Accepts arbitrary extra emacs/elisp functions to modify the message
535 # before sending, which is useful to doing things like attaching files
536 # to the message and encrypting/signing.
542 # before we can send a message, we have to prepare the FCC maildir
543 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
546 "(let ((message-send-mail-function (lambda () t))
547 (mail-host-address \"example.com\"))
550 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
551 (message-goto-subject)
552 (insert \"${subject}\")
556 (notmuch-mua-send-and-exit))" || return 1
557 notmuch new >/dev/null
560 # Add an existing, fixed corpus of email to the database.
562 # $1 is the corpus dir under corpora to add, using "default" if unset.
564 # The default corpus is based on about 50 messages from early in the
565 # history of the notmuch mailing list, which allows for reliably
566 # testing commands that need to operate on a not-totally-trivial
567 # number of messages.
573 if [ -d $TEST_DIRECTORY/corpora.mail/$corpus ]; then
574 cp -a $TEST_DIRECTORY/corpora.mail/$corpus ${MAIL_DIR}
576 cp -a $TEST_DIRECTORY/corpora/$corpus ${MAIL_DIR}
577 notmuch new >/dev/null || die "'notmuch new' failed while adding email corpus"
578 mkdir -p $TEST_DIRECTORY/corpora.mail
579 cp -a ${MAIL_DIR} $TEST_DIRECTORY/corpora.mail/$corpus
583 test_begin_subtest ()
585 if [ -n "$inside_subtest" ]; then
586 exec 1>&6 2>&7 # Restore stdout and stderr
587 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
589 test_subtest_name="$1"
591 # Redirect test output to the previously prepared file descriptors
592 # 3 and 4 (see below)
593 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
598 # Pass test if two arguments match
600 # Note: Unlike all other test_expect_* functions, this function does
601 # not accept a test name. Instead, the caller should call
602 # test_begin_subtest before calling this function in order to set the
606 exec 1>&6 2>&7 # Restore stdout and stderr
608 test "$#" = 3 && { prereq=$1; shift; } || prereq=
610 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
614 if ! test_skip "$test_subtest_name"
616 if [ "$output" = "$expected" ]; then
619 testname=$this_test.$test_count
620 echo "$expected" > $testname.expected
621 echo "$output" > $testname.output
622 test_failure_ "$(diff -u $testname.expected $testname.output)"
627 # Like test_expect_equal, but takes two filenames.
628 test_expect_equal_file ()
630 exec 1>&6 2>&7 # Restore stdout and stderr
632 test "$#" = 3 && { prereq=$1; shift; } || prereq=
634 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
638 if ! test_skip "$test_subtest_name"
640 if diff -q "$file1" "$file2" >/dev/null ; then
643 testname=$this_test.$test_count
644 basename1=`basename "$file1"`
645 basename2=`basename "$file2"`
646 cp "$file1" "$testname.$basename1"
647 cp "$file2" "$testname.$basename2"
648 test_failure_ "$(diff -u "$testname.$basename1" "$testname.$basename2")"
653 # Like test_expect_equal, but arguments are JSON expressions to be
654 # canonicalized before diff'ing. If an argument cannot be parsed, it
655 # is used unchanged so that there's something to diff against.
656 test_expect_equal_json () {
657 # The test suite forces LC_ALL=C, but this causes Python 3 to
658 # decode stdin as ASCII. We need to read JSON in UTF-8, so
659 # override Python's stdio encoding defaults.
660 output=$(echo "$1" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
662 expected=$(echo "$2" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \
665 test_expect_equal "$output" "$expected" "$@"
668 # Sort the top-level list of JSON data from stdin.
670 PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -c \
671 "import sys, json; json.dump(sorted(json.load(sys.stdin)),sys.stdout)"
674 test_emacs_expect_t () {
675 test "$#" = 2 && { prereq=$1; shift; } || prereq=
677 error "bug in the test script: not 1 or 2 parameters to test_emacs_expect_t"
680 if ! test_skip "$test_subtest_name"
682 test_emacs "(notmuch-test-run $1)" >/dev/null
684 # Restore state after the test.
685 exec 1>&6 2>&7 # Restore stdout and stderr
688 # Report success/failure.
694 test_failure_ "${result}"
697 # Restore state after the (non) test.
698 exec 1>&6 2>&7 # Restore stdout and stderr
705 notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
710 # this relies on the default format being batch-tag, otherwise some tests will break
711 notmuch dump --include=tags "${@}" | sed '/^#/d' | sort
714 notmuch_search_sanitize ()
716 perl -pe 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
719 notmuch_search_files_sanitize ()
724 notmuch_dir_sanitize ()
726 sed -e "s,$MAIL_DIR,MAIL_DIR," -e "s,${PWD},CWD,g" "$@"
729 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
730 notmuch_show_sanitize ()
732 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
734 notmuch_show_sanitize_all ()
737 -e 's| filename:.*| filename:XXXXX|' \
738 -e 's| id:[^ ]* | id:XXXXX |' | \
739 notmuch_date_sanitize
742 notmuch_json_show_sanitize ()
745 -e 's|"id": "[^"]*",|"id": "XXXXX",|g' \
746 -e 's|"Date": "Fri, 05 Jan 2001 [^"]*0000"|"Date": "GENERATED_DATE"|g' \
747 -e 's|"filename": "signature.asc",||g' \
748 -e 's|"filename": "/[^"]*",|"filename": "YYYYY",|g' \
749 -e 's|"timestamp": 97.......|"timestamp": 42|g' \
750 -e 's|"content-length": [1-9][0-9]*|"content-length": "NONZERO"|g'
753 notmuch_emacs_error_sanitize ()
761 -e 's/^\[.*\]$/[XXX]/' \
762 -e "s|^\(command: \)\{0,1\}/.*/$command|\1YYY/$command|"
765 notmuch_date_sanitize ()
768 -e 's/^Date: Fri, 05 Jan 2001 .*0000/Date: GENERATED_DATE/'
771 notmuch_uuid_sanitize ()
773 sed 's/[0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}/UUID/g'
776 notmuch_built_with_sanitize ()
778 sed 's/^built_with[.]\(.*\)=.*$/built_with.\1=something/'
781 notmuch_config_sanitize ()
783 notmuch_dir_sanitize | notmuch_built_with_sanitize
786 # End of notmuch helper functions
788 # Use test_set_prereq to tell that a particular prerequisite is available.
789 # The prerequisite can later be checked for in two ways:
791 # - Explicitly using test_have_prereq.
793 # - Implicitly by specifying the prerequisite tag in the calls to
794 # test_expect_{success,failure,code}.
796 # The single parameter is the prerequisite tag (a simple word, in all
797 # capital letters by convention).
800 satisfied="$satisfied$1 "
804 test_have_prereq () {
813 declare -A test_missing_external_prereq_
814 declare -A test_subtest_missing_external_prereq_
816 # declare prerequisite for the given external binary
817 test_declare_external_prereq () {
819 test "$#" = 2 && name=$2 || name="$binary(1)"
821 if ! hash $binary 2>/dev/null; then
822 test_missing_external_prereq_["${binary}"]=t
825 test_subtest_missing_external_prereq_[\"${name}\"]=t
831 # Explicitly require external prerequisite. Useful when binary is
832 # called indirectly (e.g. from emacs).
833 # Returns success if dependency is available, failure otherwise.
834 test_require_external_prereq () {
836 if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
837 # dependency is missing, call the replacement function to note it
844 # You are not expected to call test_ok_ and test_failure_ directly, use
845 # the text_expect_* functions instead.
848 if test "$test_subtest_known_broken_" = "t"; then
849 test_known_broken_ok_
852 test_success=$(($test_success + 1))
853 if test -n "$NOTMUCH_TEST_QUIET"; then
856 say_color pass "%-6s" "PASS"
857 echo " $test_subtest_name"
861 if test "$test_subtest_known_broken_" = "t"; then
862 test_known_broken_failure_ "$@"
865 test_failure=$(($test_failure + 1))
866 print_test_description
867 test_failure_message_ "FAIL" "$test_subtest_name" "$@"
868 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
872 test_failure_message_ () {
873 say_color error "%-6s" "$1"
876 echo "$@" | sed -e 's/^/ /'
877 if test "$verbose" != "t"; then cat test.output; fi
880 test_known_broken_ok_ () {
882 test_fixed=$(($test_fixed+1))
883 say_color pass "%-6s" "FIXED"
884 echo " $test_subtest_name"
887 test_known_broken_failure_ () {
889 test_broken=$(($test_broken+1))
890 test_failure_message_ "BROKEN" "$test_subtest_name" "$@"
895 test "$debug" = "" || eval "$1"
900 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
903 eval >&3 2>&4 "$test_cleanup"
908 test_count=$(($test_count+1))
910 for skp in $NOTMUCH_SKIP_TESTS
912 case $this_test.$test_count in
917 case $this_test_bare.$test_count in
923 if test -z "$to_skip" && test -n "$prereq" &&
924 ! test_have_prereq "$prereq"
930 test_report_skip_ "$@"
933 test_check_missing_external_prereqs_ "$@"
938 test_check_missing_external_prereqs_ () {
939 if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
940 say_color skip >&1 "missing prerequisites: "
941 echo ${!test_subtest_missing_external_prereq_[@]} >&1
942 test_report_skip_ "$@"
948 test_report_skip_ () {
950 say_color skip >&3 "skipping test:"
952 say_color skip "%-6s" "SKIP"
956 test_subtest_known_broken () {
957 test_subtest_known_broken_=t
960 test_expect_success () {
961 test "$#" = 3 && { prereq=$1; shift; } || prereq=
963 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
964 test_subtest_name="$1"
970 # test_run_ may update missing external prerequisites
971 test_check_missing_external_prereqs_ "$@" ||
972 if [ "$run_ret" = 0 -a "$eval_ret" = 0 ]
981 test_expect_code () {
982 test "$#" = 4 && { prereq=$1; shift; } || prereq=
984 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
985 test_subtest_name="$2"
991 # test_run_ may update missing external prerequisites,
992 test_check_missing_external_prereqs_ "$@" ||
993 if [ "$run_ret" = 0 -a "$eval_ret" = "$1" ]
997 test_failure_ "exit code $eval_ret, expected $1" "$3"
1002 # test_external runs external test scripts that provide continuous
1003 # test output about their progress, and succeeds/fails on
1004 # zero/non-zero exit code. It outputs the test output on stdout even
1005 # in non-verbose mode, and announces the external script with "* run
1006 # <n>: ..." before running it. When providing relative paths, keep in
1007 # mind that all scripts run in "trash directory".
1008 # Usage: test_external description command arguments...
1009 # Example: test_external 'Perl API' perl ../path/to/test.pl
1011 test "$#" = 4 && { prereq=$1; shift; } || prereq=
1013 error >&6 "bug in the test script: not 3 or 4 parameters to test_external"
1014 test_subtest_name="$1"
1017 if ! test_skip "$test_subtest_name" "$@"
1019 # Announce the script to reduce confusion about the
1020 # test output that follows.
1021 say_color "" " run $test_count: $descr ($*)"
1022 # Run command; redirect its stderr to &4 as in
1023 # test_run_, but keep its stdout on our stdout even in
1035 # Like test_external, but in addition tests that the command generated
1036 # no output on stderr.
1037 test_external_without_stderr () {
1038 # The temporary file has no (and must have no) security
1040 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
1041 stderr="$tmp/git-external-stderr.$$.tmp"
1042 test_external "$@" 4> "$stderr"
1043 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
1044 test_subtest_name="no stderr: $1"
1046 if [ ! -s "$stderr" ]; then
1050 if [ "$verbose" = t ]; then
1051 output=`echo; echo Stderr is:; cat "$stderr"`
1055 # rm first in case test_failure exits.
1057 test_failure_ "$@" "$output"
1061 # This is not among top-level (test_expect_success)
1062 # but is a prefix that can be used in the test script, like:
1064 # test_expect_success 'complain and die' '
1066 # do something else &&
1067 # test_must_fail git checkout ../outerspace
1070 # Writing this as "! git checkout ../outerspace" is wrong, because
1071 # the failure could be due to a segv. We want a controlled failure.
1075 test $? -gt 0 -a $? -le 129 -o $? -gt 192
1078 # test_cmp is a helper function to compare actual and expected output.
1079 # You can use it like:
1081 # test_expect_success 'foo works' '
1082 # echo expected >expected &&
1084 # test_cmp expected actual
1087 # This could be written as either "cmp" or "diff -u", but:
1088 # - cmp's output is not nearly as easy to read as diff -u
1089 # - not all diff versions understand "-u"
1095 # This function can be used to schedule some commands to be run
1096 # unconditionally at the end of the test to restore sanity:
1098 # test_expect_success 'test core.capslock' '
1099 # git config core.capslock true &&
1100 # test_when_finished "git config --unset core.capslock" &&
1104 # That would be roughly equivalent to
1106 # test_expect_success 'test core.capslock' '
1107 # git config core.capslock true &&
1109 # git config --unset core.capslock
1112 # except that the greeting and config --unset must both succeed for
1115 test_when_finished () {
1117 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
1122 test_results_dir="$TEST_DIRECTORY/test-results"
1123 mkdir -p "$test_results_dir"
1124 test_results_path="$test_results_dir/$this_test"
1126 echo "total $test_count" >> $test_results_path
1127 echo "success $test_success" >> $test_results_path
1128 echo "fixed $test_fixed" >> $test_results_path
1129 echo "broken $test_broken" >> $test_results_path
1130 echo "failed $test_failure" >> $test_results_path
1131 echo "" >> $test_results_path
1133 [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'
1135 if [ "$test_failure" = "0" ]; then
1136 if [ "$test_broken" = "0" ]; then
1137 rm -rf "$remove_tmp"
1145 emacs_generate_script () {
1146 # Construct a little test script here for the benefit of the user,
1147 # (who can easily run "run_emacs" to get the same emacs environment
1148 # for investigating any failures).
1149 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
1152 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
1154 # Here's what we are using here:
1156 # --quick Use minimal customization. This implies --no-init-file,
1157 # --no-site-file and (emacs 24) --no-site-lisp
1159 # --directory Ensure that the local elisp sources are found
1161 # --load Force loading of notmuch.el and test-lib.el
1163 exec ${TEST_EMACS} --quick \
1164 --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
1165 --directory "$TEST_DIRECTORY" --load test-lib.el \
1168 chmod a+x "$TMP_DIRECTORY/run_emacs"
1172 # test dependencies beforehand to avoid the waiting loop below
1173 missing_dependencies=
1174 test_require_external_prereq dtach || missing_dependencies=1
1175 test_require_external_prereq emacs || missing_dependencies=1
1176 test_require_external_prereq ${TEST_EMACSCLIENT} || missing_dependencies=1
1177 test -z "$missing_dependencies" || return
1179 if [ -z "$EMACS_SERVER" ]; then
1180 emacs_tests="${this_test_bare}.el"
1181 if [ -f "$TEST_DIRECTORY/$emacs_tests" ]; then
1182 load_emacs_tests="--eval '(load \"$emacs_tests\")'"
1186 server_name="notmuch-test-suite-$$"
1187 # start a detached session with an emacs server
1188 # user's TERM (or 'vt100' in case user's TERM is known dumb
1189 # or unknown) is given to dtach which assumes a minimally
1190 # VT100-compatible terminal -- and emacs inherits that
1191 TERM=$SMART_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
1192 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
1193 --no-window-system \
1195 --eval '(setq server-name \"$server_name\")' \
1196 --eval '(server-start)' \
1197 --eval '(orphan-watchdog $$)'" || return
1198 EMACS_SERVER="$server_name"
1199 # wait until the emacs server is up
1200 until test_emacs '()' >/dev/null 2>/dev/null; do
1205 # Clear test-output output file. Most Emacs tests end with a
1206 # call to (test-output). If the test code fails with an
1207 # exception before this call, the output file won't get
1208 # updated. Since we don't want to compare against an output
1209 # file from another test, so start out with an empty file.
1213 ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(notmuch-test-progn $@)"
1217 # Note: if there is need to print debug information from python program,
1218 # use stdout = os.fdopen(6, 'w') or stderr = os.fdopen(7, 'w')
1219 PYTHONPATH="$TEST_DIRECTORY/../bindings/python${PYTHONPATH:+:$PYTHONPATH}" \
1220 $NOTMUCH_PYTHON -B - > OUTPUT
1224 MAIL_DIR=$MAIL_DIR ruby -I $TEST_DIRECTORY/../bindings/ruby> OUTPUT
1228 exec_file="test${test_count}"
1229 test_file="${exec_file}.c"
1231 ${TEST_CC} ${TEST_CFLAGS} -I${TEST_DIRECTORY} -I${TEST_DIRECTORY}/../lib -o ${exec_file} ${test_file} -L${TEST_DIRECTORY}/../lib/ -lnotmuch -ltalloc
1232 echo "== stdout ==" > OUTPUT.stdout
1233 echo "== stderr ==" > OUTPUT.stderr
1234 ./${exec_file} "$@" 1>>OUTPUT.stdout 2>>OUTPUT.stderr
1235 notmuch_dir_sanitize OUTPUT.stdout OUTPUT.stderr > OUTPUT
1239 # Creates a script that counts how much time it is executed and calls
1240 # notmuch. $notmuch_counter_command is set to the path to the
1241 # generated script. Use notmuch_counter_value() function to get the
1242 # current counter value.
1243 notmuch_counter_reset () {
1244 notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
1245 if [ ! -x "$notmuch_counter_command" ]; then
1246 notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
1247 cat >"$notmuch_counter_command" <<EOF || return
1250 read count < "$notmuch_counter_state_path"
1251 echo \$((count + 1)) > "$notmuch_counter_state_path"
1255 chmod +x "$notmuch_counter_command" || return
1258 echo 0 > "$notmuch_counter_state_path"
1261 # Returns the current notmuch counter value.
1262 notmuch_counter_value () {
1263 if [ -r "$notmuch_counter_state_path" ]; then
1264 read count < "$notmuch_counter_state_path"
1271 test_reset_state_ () {
1272 test -z "$test_init_done_" && test_init_
1274 test_subtest_known_broken_=
1275 test_subtest_missing_external_prereq_=()
1278 # called once before the first subtest
1282 # skip all tests if there were external prerequisites missing during init
1283 test_check_missing_external_prereqs_ "all tests in $this_test" && test_done
1287 . ./test-lib-common.sh || exit 1
1289 # we need the setting of GNUPGHOME in test-lib-common.sh
1290 if [ ${NOTMUCH_HAVE_GPGCONF_SOCKETDIR} = 1 ]; then
1291 gpgconf --create-socketdir
1294 emacs_generate_script
1297 # Use -P to resolve symlinks in our working directory so that the cwd
1298 # in subprocesses like git equals our $PWD (for pathname comparisons).
1299 cd -P "$test" || error "Cannot set up test environment"
1301 if test "$verbose" = "t"
1305 exec 4>test.output 3>&4
1308 for skp in $NOTMUCH_SKIP_TESTS
1311 for skp in $NOTMUCH_SKIP_TESTS
1313 case "$this_test" in
1318 case "$this_test_bare" in
1326 say_color skip >&3 "skipping test $this_test altogether"
1327 say_color skip "skip all tests in $this_test"
1332 # Provide an implementation of the 'yes' utility
1347 # Fix some commands on Windows
1350 # Windows has its own (incompatible) sort and find
1360 # git sees Windows-style pwd
1364 # no POSIX permissions
1365 # backslashes in pathspec are converted to '/'
1366 # exec does not inherit the PID
1369 test_set_prereq POSIXPERM
1370 test_set_prereq BSLASHPSPEC
1371 test_set_prereq EXECKEEPSPID
1375 test -z "$NO_PERL" && test_set_prereq PERL
1376 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1378 # test whether the filesystem supports symbolic links
1379 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1382 # convert variable from configure to more convenient form
1383 case "$NOTMUCH_DEFAULT_XAPIAN_BACKEND" in
1391 error "Unknown Xapian backend $NOTMUCH_DEFAULT_XAPIAN_BACKEND"
1393 # declare prerequisites for external binaries used in tests
1394 test_declare_external_prereq dtach
1395 test_declare_external_prereq emacs
1396 test_declare_external_prereq ${TEST_EMACSCLIENT}
1397 test_declare_external_prereq ${TEST_GDB}
1398 test_declare_external_prereq gpg
1399 test_declare_external_prereq openssl
1400 test_declare_external_prereq gpgsm
1401 test_declare_external_prereq ${NOTMUCH_PYTHON}