2 # Copyright (c) 2005 Junio C Hamano
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see http://www.gnu.org/licenses/ .
17 if [ ${BASH_VERSINFO[0]} -lt 4 ]; then
18 echo "Error: The notmuch test suite requires a bash version >= 4.0"
19 echo "due to use of associative arrays within the test suite."
20 echo "Please try again with a newer bash (or help us fix the"
21 echo "test suite to be more portable). Thanks."
25 # Make sure echo builtin does not expand backslash-escape sequences by default.
29 this_test=${this_test%.sh}
31 # if --tee was passed, write the output not only to the terminal, but
32 # additionally to the file test-results/$BASENAME.out, too.
33 case "$GIT_TEST_TEE_STARTED, $* " in
35 # do not redirect again
37 *' --tee '*|*' --va'*)
39 BASE=test-results/$this_test
40 (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
41 echo $? > $BASE.exit) | tee $BASE.out
42 test "$(cat $BASE.exit)" = 0
47 # Keep the original TERM for say_color and test_emacs
50 # dtach(1) provides more capable terminal environment to anything
51 # that requires more than dumb terminal...
52 [ x"${TERM:-dumb}" = xdumb ] && DTACH_TERM=vt100 || DTACH_TERM=$TERM
54 # For repeatability, reset the environment to known value.
60 export LANG LC_ALL PAGER TERM TZ
61 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
62 if [[ ( -n "$TEST_EMACS" && -z "$TEST_EMACSCLIENT" ) || \
63 ( -z "$TEST_EMACS" && -n "$TEST_EMACSCLIENT" ) ]]; then
64 echo "error: must specify both or neither of TEST_EMACS and TEST_EMACSCLIENT" >&2
67 TEST_EMACS=${TEST_EMACS:-${EMACS:-emacs}}
68 TEST_EMACSCLIENT=${TEST_EMACSCLIENT:-emacsclient}
70 # Protect ourselves from common misconfiguration to export
71 # CDPATH into the environment
78 # A regexp to match 5 and 40 hexdigits
79 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
80 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
82 _x04='[0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
83 _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
85 # Each test should start with something like this, after copyright notices:
87 # test_description='Description of this test...
88 # This test checks if command xyzzy does the right thing...
91 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
92 TERM=$ORIGINAL_TERM &&
95 tput bold >/dev/null 2>&1 &&
96 tput setaf 1 >/dev/null 2>&1 &&
97 tput sgr0 >/dev/null 2>&1
101 while test "$#" -ne 0
104 -d|--d|--de|--deb|--debu|--debug)
106 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
107 immediate=t; shift ;;
108 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
109 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
110 -h|--h|--he|--hel|--help)
112 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
114 -q|--q|--qu|--qui|--quie|--quiet)
117 with_dashes=t; shift ;;
123 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
124 valgrind=t; verbose=t; shift ;;
126 shift ;; # was handled already
128 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
131 echo "error: unknown test option '$1'" >&2; exit 1 ;;
135 if test -n "$debug"; then
137 printf " %-4s" "[$((test_count - 1))]"
145 if test -n "$color"; then
151 error) tput bold; tput setaf 1;; # bold red
152 skip) tput bold; tput setaf 2;; # bold green
153 pass) tput setaf 2;; # green
154 info) tput setaf 3;; # brown
155 *) test -n "$quiet" && return;;
166 test -z "$1" && test -n "$quiet" && return
175 say_color error "error: $*\n"
184 test "${test_description}" != "" ||
185 error "Test script did not set test_description."
187 if test "$help" = "t"
189 echo "Tests ${test_description}"
193 test_description_printed=
194 print_test_description ()
196 test -z "$test_description_printed" || return 0
198 echo $this_test: "Testing ${test_description}"
199 test_description_printed=1
201 if [ -z "$NOTMUCH_TEST_QUIET" ]
203 print_test_description
218 rm -rf "$TEST_TMPDIR"
223 if test -n "$GIT_EXIT_OK"
228 say_color error '%-6s' FATAL
229 echo " $test_subtest_name"
231 echo "Unexpected exit while executing $0. Exit code $code."
238 echo >&5 "FATAL: $0: interrupted by signal" $((code - 128))
243 # Note: TEST_TMPDIR *NOT* exported!
244 TEST_TMPDIR=$(mktemp -d "${TMPDIR:-/tmp}/notmuch-test-$$.XXXXXX")
246 trap 'die_signal' HUP INT TERM
248 test_decode_color () {
249 sed -e 's/.\[1m/<WHITE>/g' \
250 -e 's/.\[31m/<RED>/g' \
251 -e 's/.\[32m/<GREEN>/g' \
252 -e 's/.\[33m/<YELLOW>/g' \
253 -e 's/.\[34m/<BLUE>/g' \
254 -e 's/.\[35m/<MAGENTA>/g' \
255 -e 's/.\[36m/<CYAN>/g' \
256 -e 's/.\[m/<RESET>/g'
268 sed -e 's/$/Q/' | tr Q '\015'
272 tr '\015' Q | sed -e 's/Q$//'
275 # Generate a new message in the mail directory, with a unique message
276 # ID and subject. The message is not added to the index.
278 # After this function returns, the filename of the generated message
279 # is available as $gen_msg_filename and the message ID is available as
282 # This function supports named parameters with the bash syntax for
283 # assigning a value to an associative array ([name]=value). The
284 # supported parameters are:
286 # [dir]=directory/of/choice
288 # Generate the message in directory 'directory/of/choice' within
289 # the mail store. The directory will be created if necessary.
293 # Store the message in file 'name'. The default is to store it
294 # in 'msg-<count>', where <count> is three-digit number of the
299 # Text to use as the body of the email message
301 # '[from]="Some User <user@example.com>"'
302 # '[to]="Some User <user@example.com>"'
303 # '[subject]="Subject of email message"'
304 # '[date]="RFC 822 Date"'
306 # Values for email headers. If not provided, default values will
307 # be generated instead.
309 # '[cc]="Some User <user@example.com>"'
310 # [reply-to]=some-address
311 # [in-reply-to]=<message-id>
312 # [references]=<message-id>
313 # [content-type]=content-type-specification
314 # '[header]=full header line, including keyword'
316 # Additional values for email headers. If these are not provided
317 # then the relevant headers will simply not appear in the
322 # Controls the message-id of the created message.
328 # This is our (bash-specific) magic for doing named parameters
329 local -A template="($@)"
330 local additional_headers
332 gen_msg_cnt=$((gen_msg_cnt + 1))
333 if [ -z "${template[filename]}" ]; then
334 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
336 gen_msg_name=${template[filename]}
339 if [ -z "${template[id]}" ]; then
340 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
342 gen_msg_id="${template[id]}"
345 if [ -z "${template[dir]}" ]; then
346 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
348 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
349 mkdir -p "$(dirname "$gen_msg_filename")"
352 if [ -z "${template[body]}" ]; then
353 template[body]="This is just a test message (#${gen_msg_cnt})"
356 if [ -z "${template[from]}" ]; then
357 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
360 if [ -z "${template[to]}" ]; then
361 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
364 if [ -z "${template[subject]}" ]; then
365 if [ -n "$test_subtest_name" ]; then
366 template[subject]="$test_subtest_name"
368 template[subject]="Test message #${gen_msg_cnt}"
372 if [ -z "${template[date]}" ]; then
373 # we use decreasing timestamps here for historical reasons;
374 # the existing test suite when we converted to unique timestamps just
375 # happened to have signicantly fewer failures with that choice.
376 template[date]=$(TZ=UTC printf "%(%a, %d %b %Y %T %z)T\n" \
377 $((978709437 - gen_msg_cnt)))
380 additional_headers=""
381 if [ ! -z "${template[header]}" ]; then
382 additional_headers="${template[header]}
383 ${additional_headers}"
386 if [ ! -z "${template[reply-to]}" ]; then
387 additional_headers="Reply-To: ${template[reply-to]}
388 ${additional_headers}"
391 if [ ! -z "${template[in-reply-to]}" ]; then
392 additional_headers="In-Reply-To: ${template[in-reply-to]}
393 ${additional_headers}"
396 if [ ! -z "${template[cc]}" ]; then
397 additional_headers="Cc: ${template[cc]}
398 ${additional_headers}"
401 if [ ! -z "${template[bcc]}" ]; then
402 additional_headers="Bcc: ${template[bcc]}
403 ${additional_headers}"
406 if [ ! -z "${template[references]}" ]; then
407 additional_headers="References: ${template[references]}
408 ${additional_headers}"
411 if [ ! -z "${template[content-type]}" ]; then
412 additional_headers="Content-Type: ${template[content-type]}
413 ${additional_headers}"
416 if [ ! -z "${template[content-transfer-encoding]}" ]; then
417 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
418 ${additional_headers}"
421 # Note that in the way we're setting it above and using it below,
422 # `additional_headers' will also serve as the header / body separator
423 # (empty line in between).
425 cat <<EOF >"$gen_msg_filename"
426 From: ${template[from]}
428 Message-Id: <${gen_msg_id}>
429 Subject: ${template[subject]}
430 Date: ${template[date]}
431 ${additional_headers}
436 # Generate a new message and add it to the database.
438 # All of the arguments and return values supported by generate_message
439 # are also supported here, so see that function for details.
442 generate_message "$@" &&
443 notmuch new > /dev/null
446 # Deliver a message with emacs and add it to the database
448 # Uses emacs to generate and deliver a message to the mail store.
449 # Accepts arbitrary extra emacs/elisp functions to modify the message
450 # before sending, which is useful to doing things like attaching files
451 # to the message and encrypting/signing.
452 emacs_deliver_message ()
457 # before we can send a message, we have to prepare the FCC maildir
458 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
459 # eval'ing smtp-dummy --background will set smtp_dummy_pid
461 eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
462 test -n "$smtp_dummy_pid" || return 1
465 "(let ((message-send-mail-function 'message-smtpmail-send-it)
466 (mail-host-address \"example.com\")
467 (smtpmail-smtp-server \"localhost\")
468 (smtpmail-smtp-service \"25025\"))
471 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
472 (message-goto-subject)
473 (insert \"${subject}\")
477 (message-send-and-exit))"
479 # In case message was sent properly, client waits for confirmation
480 # before exiting and resuming control here; therefore making sure
481 # that server exits by sending (KILL) signal to it is safe.
482 kill -9 $smtp_dummy_pid
483 notmuch new >/dev/null
486 # Pretend to deliver a message with emacs. Really save it to a file
487 # and add it to the database
489 # Uses emacs to generate and deliver a message to the mail store.
490 # Accepts arbitrary extra emacs/elisp functions to modify the message
491 # before sending, which is useful to doing things like attaching files
492 # to the message and encrypting/signing.
498 # before we can send a message, we have to prepare the FCC maildir
499 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
502 "(let ((message-send-mail-function (lambda () t))
503 (mail-host-address \"example.com\"))
506 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
507 (message-goto-subject)
508 (insert \"${subject}\")
512 (message-send-and-exit))" || return 1
513 notmuch new >/dev/null
516 # Generate a corpus of email and add it to the database.
518 # This corpus is fixed, (it happens to be 50 messages from early in
519 # the history of the notmuch mailing list), which allows for reliably
520 # testing commands that need to operate on a not-totally-trivial
521 # number of messages.
525 if [ -d $TEST_DIRECTORY/corpus.mail ]; then
526 cp -a $TEST_DIRECTORY/corpus.mail ${MAIL_DIR}
528 cp -a $TEST_DIRECTORY/corpus ${MAIL_DIR}
529 notmuch new >/dev/null
530 cp -a ${MAIL_DIR} $TEST_DIRECTORY/corpus.mail
534 test_begin_subtest ()
536 if [ -n "$inside_subtest" ]; then
537 exec 1>&6 2>&7 # Restore stdout and stderr
538 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
540 test_subtest_name="$1"
542 # Remember stdout and stderr file descriptors and redirect test
543 # output to the previously prepared file descriptors 3 and 4 (see
545 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
546 exec 6>&1 7>&2 >&3 2>&4
550 # Pass test if two arguments match
552 # Note: Unlike all other test_expect_* functions, this function does
553 # not accept a test name. Instead, the caller should call
554 # test_begin_subtest before calling this function in order to set the
558 exec 1>&6 2>&7 # Restore stdout and stderr
560 test "$#" = 3 && { prereq=$1; shift; } || prereq=
562 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
566 if ! test_skip "$test_subtest_name"
568 if [ "$output" = "$expected" ]; then
571 testname=$this_test.$test_count
572 echo "$expected" > $testname.expected
573 echo "$output" > $testname.output
574 test_failure_ "$(diff -u $testname.expected $testname.output)"
579 # Like test_expect_equal, but takes two filenames.
580 test_expect_equal_file ()
582 exec 1>&6 2>&7 # Restore stdout and stderr
584 test "$#" = 3 && { prereq=$1; shift; } || prereq=
586 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
589 basename1=`basename "$file1"`
591 basename2=`basename "$file2"`
592 if ! test_skip "$test_subtest_name"
594 if diff -q "$file1" "$file2" >/dev/null ; then
597 testname=$this_test.$test_count
598 cp "$file1" "$testname.$basename1"
599 cp "$file2" "$testname.$basename2"
600 test_failure_ "$(diff -u "$testname.$basename1" "$testname.$basename2")"
605 # Like test_expect_equal, but arguments are JSON expressions to be
606 # canonicalized before diff'ing. If an argument cannot be parsed, it
607 # is used unchanged so that there's something to diff against.
608 test_expect_equal_json () {
609 # The test suite forces LC_ALL=C, but this causes Python 3 to
610 # decode stdin as ASCII. We need to read JSON in UTF-8, so
611 # override Python's stdio encoding defaults.
612 output=$(echo "$1" | PYTHONIOENCODING=utf-8 python -mjson.tool \
614 expected=$(echo "$2" | PYTHONIOENCODING=utf-8 python -mjson.tool \
617 test_expect_equal "$output" "$expected" "$@"
620 test_emacs_expect_t () {
621 test "$#" = 2 && { prereq=$1; shift; } || prereq=
623 error "bug in the test script: not 1 or 2 parameters to test_emacs_expect_t"
626 if ! test_skip "$test_subtest_name"
628 test_emacs "(notmuch-test-run $1)" >/dev/null
630 # Restore state after the test.
631 exec 1>&6 2>&7 # Restore stdout and stderr
634 # Report success/failure.
640 test_failure_ "${result}"
643 # Restore state after the (non) test.
644 exec 1>&6 2>&7 # Restore stdout and stderr
651 notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
654 notmuch_search_sanitize ()
656 perl -pe 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
659 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
660 notmuch_show_sanitize ()
662 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
664 notmuch_show_sanitize_all ()
667 -e 's| filename:.*| filename:XXXXX|' \
668 -e 's| id:[^ ]* | id:XXXXX |' | \
669 notmuch_date_sanitize
672 notmuch_json_show_sanitize ()
675 -e 's|"id": "[^"]*",|"id": "XXXXX",|g' \
676 -e 's|"Date": "Fri, 05 Jan 2001 [^"]*0000"|"Date": "GENERATED_DATE"|g' \
677 -e 's|"filename": "/[^"]*",|"filename": "YYYYY",|g' \
678 -e 's|"timestamp": 97.......|"timestamp": 42|g'
681 notmuch_emacs_error_sanitize ()
689 -e 's/^\[.*\]$/[XXX]/' \
690 -e "s|^\(command: \)\{0,1\}/.*/$command|\1YYY/$command|"
693 notmuch_date_sanitize ()
696 -e 's/^Date: Fri, 05 Jan 2001 .*0000/Date: GENERATED_DATE/'
698 # End of notmuch helper functions
700 # Use test_set_prereq to tell that a particular prerequisite is available.
701 # The prerequisite can later be checked for in two ways:
703 # - Explicitly using test_have_prereq.
705 # - Implicitly by specifying the prerequisite tag in the calls to
706 # test_expect_{success,failure,code}.
708 # The single parameter is the prerequisite tag (a simple word, in all
709 # capital letters by convention).
712 satisfied="$satisfied$1 "
716 test_have_prereq () {
725 declare -A test_missing_external_prereq_
726 declare -A test_subtest_missing_external_prereq_
728 # declare prerequisite for the given external binary
729 test_declare_external_prereq () {
731 test "$#" = 2 && name=$2 || name="$binary(1)"
733 if ! hash $binary 2>/dev/null; then
734 test_missing_external_prereq_["${binary}"]=t
737 test_subtest_missing_external_prereq_[\"${name}\"]=t
743 # Explicitly require external prerequisite. Useful when binary is
744 # called indirectly (e.g. from emacs).
745 # Returns success if dependency is available, failure otherwise.
746 test_require_external_prereq () {
748 if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
749 # dependency is missing, call the replacement function to note it
756 # You are not expected to call test_ok_ and test_failure_ directly, use
757 # the text_expect_* functions instead.
760 if test "$test_subtest_known_broken_" = "t"; then
761 test_known_broken_ok_
764 test_success=$(($test_success + 1))
765 if test -n "$NOTMUCH_TEST_QUIET"; then
768 say_color pass "%-6s" "PASS"
769 echo " $test_subtest_name"
773 if test "$test_subtest_known_broken_" = "t"; then
774 test_known_broken_failure_ "$@"
777 test_failure=$(($test_failure + 1))
778 print_test_description
779 test_failure_message_ "FAIL" "$test_subtest_name" "$@"
780 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
784 test_failure_message_ () {
785 say_color error "%-6s" "$1"
788 echo "$@" | sed -e 's/^/ /'
789 if test "$verbose" != "t"; then cat test.output; fi
792 test_known_broken_ok_ () {
794 test_fixed=$(($test_fixed+1))
795 say_color pass "%-6s" "FIXED"
796 echo " $test_subtest_name"
799 test_known_broken_failure_ () {
801 test_broken=$(($test_broken+1))
802 test_failure_message_ "BROKEN" "$test_subtest_name" "$@"
807 test "$debug" = "" || eval "$1"
812 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
815 eval >&3 2>&4 "$test_cleanup"
820 test_count=$(($test_count+1))
822 for skp in $NOTMUCH_SKIP_TESTS
824 case $this_test.$test_count in
829 if test -z "$to_skip" && test -n "$prereq" &&
830 ! test_have_prereq "$prereq"
836 test_report_skip_ "$@"
839 test_check_missing_external_prereqs_ "$@"
844 test_check_missing_external_prereqs_ () {
845 if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
846 say_color skip >&1 "missing prerequisites: "
847 echo ${!test_subtest_missing_external_prereq_[@]} >&1
848 test_report_skip_ "$@"
854 test_report_skip_ () {
856 say_color skip >&3 "skipping test:"
858 say_color skip "%-6s" "SKIP"
862 test_subtest_known_broken () {
863 test_subtest_known_broken_=t
866 test_expect_success () {
867 test "$#" = 3 && { prereq=$1; shift; } || prereq=
869 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
870 test_subtest_name="$1"
876 # test_run_ may update missing external prerequisites
877 test_check_missing_external_prereqs_ "$@" ||
878 if [ "$run_ret" = 0 -a "$eval_ret" = 0 ]
887 test_expect_code () {
888 test "$#" = 4 && { prereq=$1; shift; } || prereq=
890 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
891 test_subtest_name="$2"
897 # test_run_ may update missing external prerequisites,
898 test_check_missing_external_prereqs_ "$@" ||
899 if [ "$run_ret" = 0 -a "$eval_ret" = "$1" ]
903 test_failure_ "exit code $eval_ret, expected $1" "$3"
908 # test_external runs external test scripts that provide continuous
909 # test output about their progress, and succeeds/fails on
910 # zero/non-zero exit code. It outputs the test output on stdout even
911 # in non-verbose mode, and announces the external script with "* run
912 # <n>: ..." before running it. When providing relative paths, keep in
913 # mind that all scripts run in "trash directory".
914 # Usage: test_external description command arguments...
915 # Example: test_external 'Perl API' perl ../path/to/test.pl
917 test "$#" = 4 && { prereq=$1; shift; } || prereq=
919 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
920 test_subtest_name="$1"
923 if ! test_skip "$test_subtest_name" "$@"
925 # Announce the script to reduce confusion about the
926 # test output that follows.
927 say_color "" " run $test_count: $descr ($*)"
928 # Run command; redirect its stderr to &4 as in
929 # test_run_, but keep its stdout on our stdout even in
941 # Like test_external, but in addition tests that the command generated
942 # no output on stderr.
943 test_external_without_stderr () {
944 # The temporary file has no (and must have no) security
946 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
947 stderr="$tmp/git-external-stderr.$$.tmp"
948 test_external "$@" 4> "$stderr"
949 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
950 test_subtest_name="no stderr: $1"
952 if [ ! -s "$stderr" ]; then
956 if [ "$verbose" = t ]; then
957 output=`echo; echo Stderr is:; cat "$stderr"`
961 # rm first in case test_failure exits.
963 test_failure_ "$@" "$output"
967 # This is not among top-level (test_expect_success)
968 # but is a prefix that can be used in the test script, like:
970 # test_expect_success 'complain and die' '
972 # do something else &&
973 # test_must_fail git checkout ../outerspace
976 # Writing this as "! git checkout ../outerspace" is wrong, because
977 # the failure could be due to a segv. We want a controlled failure.
981 test $? -gt 0 -a $? -le 129 -o $? -gt 192
984 # test_cmp is a helper function to compare actual and expected output.
985 # You can use it like:
987 # test_expect_success 'foo works' '
988 # echo expected >expected &&
990 # test_cmp expected actual
993 # This could be written as either "cmp" or "diff -u", but:
994 # - cmp's output is not nearly as easy to read as diff -u
995 # - not all diff versions understand "-u"
1001 # This function can be used to schedule some commands to be run
1002 # unconditionally at the end of the test to restore sanity:
1004 # test_expect_success 'test core.capslock' '
1005 # git config core.capslock true &&
1006 # test_when_finished "git config --unset core.capslock" &&
1010 # That would be roughly equivalent to
1012 # test_expect_success 'test core.capslock' '
1013 # git config core.capslock true &&
1015 # git config --unset core.capslock
1018 # except that the greeting and config --unset must both succeed for
1021 test_when_finished () {
1023 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
1028 test_results_dir="$TEST_DIRECTORY/test-results"
1029 mkdir -p "$test_results_dir"
1030 test_results_path="$test_results_dir/$this_test"
1032 echo "total $test_count" >> $test_results_path
1033 echo "success $test_success" >> $test_results_path
1034 echo "fixed $test_fixed" >> $test_results_path
1035 echo "broken $test_broken" >> $test_results_path
1036 echo "failed $test_failure" >> $test_results_path
1037 echo "" >> $test_results_path
1039 [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'
1041 if [ "$test_failure" = "0" ]; then
1042 if [ "$test_broken" = "0" ]; then
1043 rm -rf "$remove_tmp"
1051 emacs_generate_script () {
1052 # Construct a little test script here for the benefit of the user,
1053 # (who can easily run "run_emacs" to get the same emacs environment
1054 # for investigating any failures).
1055 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
1058 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
1060 # Here's what we are using here:
1062 # --no-init-file Don't load users ~/.emacs
1064 # --no-site-file Don't load the site-wide startup stuff
1066 # --directory Ensure that the local elisp sources are found
1068 # --load Force loading of notmuch.el and test-lib.el
1070 exec ${TEST_EMACS} --no-init-file --no-site-file \
1071 --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
1072 --directory "$TEST_DIRECTORY" --load test-lib.el \
1075 chmod a+x "$TMP_DIRECTORY/run_emacs"
1079 # test dependencies beforehand to avoid the waiting loop below
1080 missing_dependencies=
1081 test_require_external_prereq dtach || missing_dependencies=1
1082 test_require_external_prereq emacs || missing_dependencies=1
1083 test_require_external_prereq ${TEST_EMACSCLIENT} || missing_dependencies=1
1084 test -z "$missing_dependencies" || return
1086 if [ -z "$EMACS_SERVER" ]; then
1087 emacs_tests="${this_test}.el"
1088 if [ -f "$TEST_DIRECTORY/$emacs_tests" ]; then
1089 load_emacs_tests="--eval '(load \"$emacs_tests\")'"
1093 server_name="notmuch-test-suite-$$"
1094 # start a detached session with an emacs server
1095 # user's TERM (or 'vt100' in case user's TERM is unset, empty
1096 # or 'dumb') is given to dtach which assumes a minimally
1097 # VT100-compatible terminal -- and emacs inherits that
1098 TERM=$DTACH_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
1099 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
1100 --no-window-system \
1102 --eval '(setq server-name \"$server_name\")' \
1103 --eval '(server-start)' \
1104 --eval '(orphan-watchdog $$)'" || return
1105 EMACS_SERVER="$server_name"
1106 # wait until the emacs server is up
1107 until test_emacs '()' >/dev/null 2>/dev/null; do
1112 # Clear test-output output file. Most Emacs tests end with a
1113 # call to (test-output). If the test code fails with an
1114 # exception before this call, the output file won't get
1115 # updated. Since we don't want to compare against an output
1116 # file from another test, so start out with an empty file.
1120 ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(progn $@)"
1124 export LD_LIBRARY_PATH=$TEST_DIRECTORY/../lib
1125 export PYTHONPATH=$TEST_DIRECTORY/../bindings/python
1127 # Some distros (e.g. Arch Linux) ship Python 2.* as /usr/bin/python2,
1128 # most others as /usr/bin/python. So first try python2, and fallback to
1129 # python if python2 doesn't exist.
1131 [[ ${test_missing_external_prereq_[python2]} == t ]] && cmd=python
1133 (echo "import sys; _orig_stdout=sys.stdout; sys.stdout=open('OUTPUT', 'w')"; cat) \
1137 # Creates a script that counts how much time it is executed and calls
1138 # notmuch. $notmuch_counter_command is set to the path to the
1139 # generated script. Use notmuch_counter_value() function to get the
1140 # current counter value.
1141 notmuch_counter_reset () {
1142 notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
1143 if [ ! -x "$notmuch_counter_command" ]; then
1144 notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
1145 cat >"$notmuch_counter_command" <<EOF || return
1148 read count < "$notmuch_counter_state_path"
1149 echo \$((count + 1)) > "$notmuch_counter_state_path"
1153 chmod +x "$notmuch_counter_command" || return
1156 echo 0 > "$notmuch_counter_state_path"
1159 # Returns the current notmuch counter value.
1160 notmuch_counter_value () {
1161 if [ -r "$notmuch_counter_state_path" ]; then
1162 read count < "$notmuch_counter_state_path"
1169 test_reset_state_ () {
1170 test -z "$test_init_done_" && test_init_
1172 test_subtest_known_broken_=
1173 test_subtest_missing_external_prereq_=()
1176 # called once before the first subtest
1180 # skip all tests if there were external prerequisites missing during init
1181 test_check_missing_external_prereqs_ "all tests in $this_test" && test_done
1185 . ./test-lib-common.sh
1187 emacs_generate_script
1190 # Use -P to resolve symlinks in our working directory so that the cwd
1191 # in subprocesses like git equals our $PWD (for pathname comparisons).
1192 cd -P "$test" || error "Cannot setup test environment"
1194 if test "$verbose" = "t"
1198 exec 4>test.output 3>&4
1201 for skp in $NOTMUCH_SKIP_TESTS
1204 for skp in $NOTMUCH_SKIP_TESTS
1206 case "$this_test" in
1213 say_color skip >&3 "skipping test $this_test altogether"
1214 say_color skip "skip all tests in $this_test"
1219 # Provide an implementation of the 'yes' utility
1234 # Fix some commands on Windows
1237 # Windows has its own (incompatible) sort and find
1247 # git sees Windows-style pwd
1251 # no POSIX permissions
1252 # backslashes in pathspec are converted to '/'
1253 # exec does not inherit the PID
1256 test_set_prereq POSIXPERM
1257 test_set_prereq BSLASHPSPEC
1258 test_set_prereq EXECKEEPSPID
1262 test -z "$NO_PERL" && test_set_prereq PERL
1263 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1265 # test whether the filesystem supports symbolic links
1266 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1269 # declare prerequisites for external binaries used in tests
1270 test_declare_external_prereq dtach
1271 test_declare_external_prereq emacs
1272 test_declare_external_prereq ${TEST_EMACSCLIENT}
1273 test_declare_external_prereq gdb
1274 test_declare_external_prereq gpg
1275 test_declare_external_prereq python
1276 test_declare_external_prereq python2