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 template[date]="Fri, 05 Jan 2001 15:43:57 +0000"
376 additional_headers=""
377 if [ ! -z "${template[header]}" ]; then
378 additional_headers="${template[header]}
379 ${additional_headers}"
382 if [ ! -z "${template[reply-to]}" ]; then
383 additional_headers="Reply-To: ${template[reply-to]}
384 ${additional_headers}"
387 if [ ! -z "${template[in-reply-to]}" ]; then
388 additional_headers="In-Reply-To: ${template[in-reply-to]}
389 ${additional_headers}"
392 if [ ! -z "${template[cc]}" ]; then
393 additional_headers="Cc: ${template[cc]}
394 ${additional_headers}"
397 if [ ! -z "${template[bcc]}" ]; then
398 additional_headers="Bcc: ${template[bcc]}
399 ${additional_headers}"
402 if [ ! -z "${template[references]}" ]; then
403 additional_headers="References: ${template[references]}
404 ${additional_headers}"
407 if [ ! -z "${template[content-type]}" ]; then
408 additional_headers="Content-Type: ${template[content-type]}
409 ${additional_headers}"
412 if [ ! -z "${template[content-transfer-encoding]}" ]; then
413 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
414 ${additional_headers}"
417 # Note that in the way we're setting it above and using it below,
418 # `additional_headers' will also serve as the header / body separator
419 # (empty line in between).
421 cat <<EOF >"$gen_msg_filename"
422 From: ${template[from]}
424 Message-Id: <${gen_msg_id}>
425 Subject: ${template[subject]}
426 Date: ${template[date]}
427 ${additional_headers}
432 # Generate a new message and add it to the database.
434 # All of the arguments and return values supported by generate_message
435 # are also supported here, so see that function for details.
438 generate_message "$@" &&
439 notmuch new > /dev/null
442 # Deliver a message with emacs and add it to the database
444 # Uses emacs to generate and deliver a message to the mail store.
445 # Accepts arbitrary extra emacs/elisp functions to modify the message
446 # before sending, which is useful to doing things like attaching files
447 # to the message and encrypting/signing.
448 emacs_deliver_message ()
453 # before we can send a message, we have to prepare the FCC maildir
454 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
455 # eval'ing smtp-dummy --background will set smtp_dummy_pid
457 eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
458 test -n "$smtp_dummy_pid" || return 1
461 "(let ((message-send-mail-function 'message-smtpmail-send-it)
462 (mail-host-address \"example.com\")
463 (smtpmail-smtp-server \"localhost\")
464 (smtpmail-smtp-service \"25025\"))
468 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
469 (message-goto-subject)
470 (insert \"${subject}\")
474 (message-send-and-exit))"
476 # In case message was sent properly, client waits for confirmation
477 # before exiting and resuming control here; therefore making sure
478 # that server exits by sending (KILL) signal to it is safe.
479 kill -9 $smtp_dummy_pid
480 notmuch new >/dev/null
483 # Generate a corpus of email and add it to the database.
485 # This corpus is fixed, (it happens to be 50 messages from early in
486 # the history of the notmuch mailing list), which allows for reliably
487 # testing commands that need to operate on a not-totally-trivial
488 # number of messages.
492 if [ -d $TEST_DIRECTORY/corpus.mail ]; then
493 cp -a $TEST_DIRECTORY/corpus.mail ${MAIL_DIR}
495 cp -a $TEST_DIRECTORY/corpus ${MAIL_DIR}
496 notmuch new >/dev/null
497 cp -a ${MAIL_DIR} $TEST_DIRECTORY/corpus.mail
501 test_begin_subtest ()
503 if [ -n "$inside_subtest" ]; then
504 exec 1>&6 2>&7 # Restore stdout and stderr
505 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
507 test_subtest_name="$1"
509 # Remember stdout and stderr file descriptors and redirect test
510 # output to the previously prepared file descriptors 3 and 4 (see
512 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
513 exec 6>&1 7>&2 >&3 2>&4
517 # Pass test if two arguments match
519 # Note: Unlike all other test_expect_* functions, this function does
520 # not accept a test name. Instead, the caller should call
521 # test_begin_subtest before calling this function in order to set the
525 exec 1>&6 2>&7 # Restore stdout and stderr
527 test "$#" = 3 && { prereq=$1; shift; } || prereq=
529 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
533 if ! test_skip "$test_subtest_name"
535 if [ "$output" = "$expected" ]; then
538 testname=$this_test.$test_count
539 echo "$expected" > $testname.expected
540 echo "$output" > $testname.output
541 test_failure_ "$(diff -u $testname.expected $testname.output)"
546 # Like test_expect_equal, but takes two filenames.
547 test_expect_equal_file ()
549 exec 1>&6 2>&7 # Restore stdout and stderr
551 test "$#" = 3 && { prereq=$1; shift; } || prereq=
553 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
556 basename1=`basename "$file1"`
558 basename2=`basename "$file2"`
559 if ! test_skip "$test_subtest_name"
561 if diff -q "$file1" "$file2" >/dev/null ; then
564 testname=$this_test.$test_count
565 cp "$file1" "$testname.$basename1"
566 cp "$file2" "$testname.$basename2"
567 test_failure_ "$(diff -u "$testname.$basename1" "$testname.$basename2")"
572 # Like test_expect_equal, but arguments are JSON expressions to be
573 # canonicalized before diff'ing. If an argument cannot be parsed, it
574 # is used unchanged so that there's something to diff against.
575 test_expect_equal_json () {
576 # The test suite forces LC_ALL=C, but this causes Python 3 to
577 # decode stdin as ASCII. We need to read JSON in UTF-8, so
578 # override Python's stdio encoding defaults.
579 output=$(echo "$1" | PYTHONIOENCODING=utf-8 python -mjson.tool \
581 expected=$(echo "$2" | PYTHONIOENCODING=utf-8 python -mjson.tool \
584 test_expect_equal "$output" "$expected" "$@"
587 test_emacs_expect_t () {
588 test "$#" = 2 && { prereq=$1; shift; } || prereq=
590 error "bug in the test script: not 1 or 2 parameters to test_emacs_expect_t"
593 if ! test_skip "$test_subtest_name"
595 test_emacs "(notmuch-test-run $1)" >/dev/null
597 # Restore state after the test.
598 exec 1>&6 2>&7 # Restore stdout and stderr
601 # Report success/failure.
607 test_failure_ "${result}"
610 # Restore state after the (non) test.
611 exec 1>&6 2>&7 # Restore stdout and stderr
618 notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
621 notmuch_search_sanitize ()
623 perl -pe 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
626 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
627 notmuch_show_sanitize ()
629 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
631 notmuch_show_sanitize_all ()
634 -e 's| filename:.*| filename:XXXXX|' \
635 -e 's| id:[^ ]* | id:XXXXX |'
638 notmuch_json_show_sanitize ()
641 -e 's|"id": "[^"]*",|"id": "XXXXX",|g' \
642 -e 's|"filename": "/[^"]*",|"filename": "YYYYY",|g'
645 notmuch_emacs_error_sanitize ()
653 -e 's/^\[.*\]$/[XXX]/' \
654 -e "s|^\(command: \)\{0,1\}/.*/$command|\1YYY/$command|"
656 # End of notmuch helper functions
658 # Use test_set_prereq to tell that a particular prerequisite is available.
659 # The prerequisite can later be checked for in two ways:
661 # - Explicitly using test_have_prereq.
663 # - Implicitly by specifying the prerequisite tag in the calls to
664 # test_expect_{success,failure,code}.
666 # The single parameter is the prerequisite tag (a simple word, in all
667 # capital letters by convention).
670 satisfied="$satisfied$1 "
674 test_have_prereq () {
683 declare -A test_missing_external_prereq_
684 declare -A test_subtest_missing_external_prereq_
686 # declare prerequisite for the given external binary
687 test_declare_external_prereq () {
689 test "$#" = 2 && name=$2 || name="$binary(1)"
691 if ! hash $binary 2>/dev/null; then
692 test_missing_external_prereq_["${binary}"]=t
695 test_subtest_missing_external_prereq_[\"${name}\"]=t
701 # Explicitly require external prerequisite. Useful when binary is
702 # called indirectly (e.g. from emacs).
703 # Returns success if dependency is available, failure otherwise.
704 test_require_external_prereq () {
706 if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
707 # dependency is missing, call the replacement function to note it
714 # You are not expected to call test_ok_ and test_failure_ directly, use
715 # the text_expect_* functions instead.
718 if test "$test_subtest_known_broken_" = "t"; then
719 test_known_broken_ok_
722 test_success=$(($test_success + 1))
723 if test -n "$NOTMUCH_TEST_QUIET"; then
726 say_color pass "%-6s" "PASS"
727 echo " $test_subtest_name"
731 if test "$test_subtest_known_broken_" = "t"; then
732 test_known_broken_failure_ "$@"
735 test_failure=$(($test_failure + 1))
736 print_test_description
737 test_failure_message_ "FAIL" "$test_subtest_name" "$@"
738 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
742 test_failure_message_ () {
743 say_color error "%-6s" "$1"
746 echo "$@" | sed -e 's/^/ /'
747 if test "$verbose" != "t"; then cat test.output; fi
750 test_known_broken_ok_ () {
752 test_fixed=$(($test_fixed+1))
753 say_color pass "%-6s" "FIXED"
754 echo " $test_subtest_name"
757 test_known_broken_failure_ () {
759 test_broken=$(($test_broken+1))
760 test_failure_message_ "BROKEN" "$test_subtest_name" "$@"
765 test "$debug" = "" || eval "$1"
770 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
773 eval >&3 2>&4 "$test_cleanup"
778 test_count=$(($test_count+1))
780 for skp in $NOTMUCH_SKIP_TESTS
782 case $this_test.$test_count in
787 if test -z "$to_skip" && test -n "$prereq" &&
788 ! test_have_prereq "$prereq"
794 test_report_skip_ "$@"
797 test_check_missing_external_prereqs_ "$@"
802 test_check_missing_external_prereqs_ () {
803 if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
804 say_color skip >&1 "missing prerequisites: "
805 echo ${!test_subtest_missing_external_prereq_[@]} >&1
806 test_report_skip_ "$@"
812 test_report_skip_ () {
814 say_color skip >&3 "skipping test:"
816 say_color skip "%-6s" "SKIP"
820 test_subtest_known_broken () {
821 test_subtest_known_broken_=t
824 test_expect_success () {
825 test "$#" = 3 && { prereq=$1; shift; } || prereq=
827 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
828 test_subtest_name="$1"
834 # test_run_ may update missing external prerequisites
835 test_check_missing_external_prereqs_ "$@" ||
836 if [ "$run_ret" = 0 -a "$eval_ret" = 0 ]
845 test_expect_code () {
846 test "$#" = 4 && { prereq=$1; shift; } || prereq=
848 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
849 test_subtest_name="$2"
855 # test_run_ may update missing external prerequisites,
856 test_check_missing_external_prereqs_ "$@" ||
857 if [ "$run_ret" = 0 -a "$eval_ret" = "$1" ]
861 test_failure_ "exit code $eval_ret, expected $1" "$3"
866 # test_external runs external test scripts that provide continuous
867 # test output about their progress, and succeeds/fails on
868 # zero/non-zero exit code. It outputs the test output on stdout even
869 # in non-verbose mode, and announces the external script with "* run
870 # <n>: ..." before running it. When providing relative paths, keep in
871 # mind that all scripts run in "trash directory".
872 # Usage: test_external description command arguments...
873 # Example: test_external 'Perl API' perl ../path/to/test.pl
875 test "$#" = 4 && { prereq=$1; shift; } || prereq=
877 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
878 test_subtest_name="$1"
881 if ! test_skip "$test_subtest_name" "$@"
883 # Announce the script to reduce confusion about the
884 # test output that follows.
885 say_color "" " run $test_count: $descr ($*)"
886 # Run command; redirect its stderr to &4 as in
887 # test_run_, but keep its stdout on our stdout even in
899 # Like test_external, but in addition tests that the command generated
900 # no output on stderr.
901 test_external_without_stderr () {
902 # The temporary file has no (and must have no) security
904 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
905 stderr="$tmp/git-external-stderr.$$.tmp"
906 test_external "$@" 4> "$stderr"
907 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
908 test_subtest_name="no stderr: $1"
910 if [ ! -s "$stderr" ]; then
914 if [ "$verbose" = t ]; then
915 output=`echo; echo Stderr is:; cat "$stderr"`
919 # rm first in case test_failure exits.
921 test_failure_ "$@" "$output"
925 # This is not among top-level (test_expect_success)
926 # but is a prefix that can be used in the test script, like:
928 # test_expect_success 'complain and die' '
930 # do something else &&
931 # test_must_fail git checkout ../outerspace
934 # Writing this as "! git checkout ../outerspace" is wrong, because
935 # the failure could be due to a segv. We want a controlled failure.
939 test $? -gt 0 -a $? -le 129 -o $? -gt 192
942 # test_cmp is a helper function to compare actual and expected output.
943 # You can use it like:
945 # test_expect_success 'foo works' '
946 # echo expected >expected &&
948 # test_cmp expected actual
951 # This could be written as either "cmp" or "diff -u", but:
952 # - cmp's output is not nearly as easy to read as diff -u
953 # - not all diff versions understand "-u"
959 # This function can be used to schedule some commands to be run
960 # unconditionally at the end of the test to restore sanity:
962 # test_expect_success 'test core.capslock' '
963 # git config core.capslock true &&
964 # test_when_finished "git config --unset core.capslock" &&
968 # That would be roughly equivalent to
970 # test_expect_success 'test core.capslock' '
971 # git config core.capslock true &&
973 # git config --unset core.capslock
976 # except that the greeting and config --unset must both succeed for
979 test_when_finished () {
981 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
986 test_results_dir="$TEST_DIRECTORY/test-results"
987 mkdir -p "$test_results_dir"
988 test_results_path="$test_results_dir/$this_test"
990 echo "total $test_count" >> $test_results_path
991 echo "success $test_success" >> $test_results_path
992 echo "fixed $test_fixed" >> $test_results_path
993 echo "broken $test_broken" >> $test_results_path
994 echo "failed $test_failure" >> $test_results_path
995 echo "" >> $test_results_path
997 [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'
999 if [ "$test_failure" = "0" ]; then
1000 if [ "$test_broken" = "0" ]; then
1001 rm -rf "$remove_tmp"
1009 emacs_generate_script () {
1010 # Construct a little test script here for the benefit of the user,
1011 # (who can easily run "run_emacs" to get the same emacs environment
1012 # for investigating any failures).
1013 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
1016 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
1018 # Here's what we are using here:
1020 # --no-init-file Don't load users ~/.emacs
1022 # --no-site-file Don't load the site-wide startup stuff
1024 # --directory Ensure that the local elisp sources are found
1026 # --load Force loading of notmuch.el and test-lib.el
1028 exec ${TEST_EMACS} --no-init-file --no-site-file \
1029 --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
1030 --directory "$TEST_DIRECTORY" --load test-lib.el \
1033 chmod a+x "$TMP_DIRECTORY/run_emacs"
1037 # test dependencies beforehand to avoid the waiting loop below
1038 missing_dependencies=
1039 test_require_external_prereq dtach || missing_dependencies=1
1040 test_require_external_prereq emacs || missing_dependencies=1
1041 test_require_external_prereq ${TEST_EMACSCLIENT} || missing_dependencies=1
1042 test -z "$missing_dependencies" || return
1044 if [ -z "$EMACS_SERVER" ]; then
1045 emacs_tests="${this_test}.el"
1046 if [ -f "$TEST_DIRECTORY/$emacs_tests" ]; then
1047 load_emacs_tests="--eval '(load \"$emacs_tests\")'"
1051 server_name="notmuch-test-suite-$$"
1052 # start a detached session with an emacs server
1053 # user's TERM (or 'vt100' in case user's TERM is unset, empty
1054 # or 'dumb') is given to dtach which assumes a minimally
1055 # VT100-compatible terminal -- and emacs inherits that
1056 TERM=$DTACH_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
1057 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
1058 --no-window-system \
1060 --eval '(setq server-name \"$server_name\")' \
1061 --eval '(server-start)' \
1062 --eval '(orphan-watchdog $$)'" || return
1063 EMACS_SERVER="$server_name"
1064 # wait until the emacs server is up
1065 until test_emacs '()' >/dev/null 2>/dev/null; do
1070 # Clear test-output output file. Most Emacs tests end with a
1071 # call to (test-output). If the test code fails with an
1072 # exception before this call, the output file won't get
1073 # updated. Since we don't want to compare against an output
1074 # file from another test, so start out with an empty file.
1078 ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(progn $@)"
1082 export LD_LIBRARY_PATH=$TEST_DIRECTORY/../lib
1083 export PYTHONPATH=$TEST_DIRECTORY/../bindings/python
1085 # Some distros (e.g. Arch Linux) ship Python 2.* as /usr/bin/python2,
1086 # most others as /usr/bin/python. So first try python2, and fallback to
1087 # python if python2 doesn't exist.
1089 [[ ${test_missing_external_prereq_[python2]} == t ]] && cmd=python
1091 (echo "import sys; _orig_stdout=sys.stdout; sys.stdout=open('OUTPUT', 'w')"; cat) \
1095 # Creates a script that counts how much time it is executed and calls
1096 # notmuch. $notmuch_counter_command is set to the path to the
1097 # generated script. Use notmuch_counter_value() function to get the
1098 # current counter value.
1099 notmuch_counter_reset () {
1100 notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
1101 if [ ! -x "$notmuch_counter_command" ]; then
1102 notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
1103 cat >"$notmuch_counter_command" <<EOF || return
1106 read count < "$notmuch_counter_state_path"
1107 echo \$((count + 1)) > "$notmuch_counter_state_path"
1111 chmod +x "$notmuch_counter_command" || return
1114 echo 0 > "$notmuch_counter_state_path"
1117 # Returns the current notmuch counter value.
1118 notmuch_counter_value () {
1119 if [ -r "$notmuch_counter_state_path" ]; then
1120 read count < "$notmuch_counter_state_path"
1127 test_reset_state_ () {
1128 test -z "$test_init_done_" && test_init_
1130 test_subtest_known_broken_=
1131 test_subtest_missing_external_prereq_=()
1134 # called once before the first subtest
1138 # skip all tests if there were external prerequisites missing during init
1139 test_check_missing_external_prereqs_ "all tests in $this_test" && test_done
1143 . ./test-lib-common.sh
1145 emacs_generate_script
1148 # Use -P to resolve symlinks in our working directory so that the cwd
1149 # in subprocesses like git equals our $PWD (for pathname comparisons).
1150 cd -P "$test" || error "Cannot setup test environment"
1152 if test "$verbose" = "t"
1156 exec 4>test.output 3>&4
1159 for skp in $NOTMUCH_SKIP_TESTS
1162 for skp in $NOTMUCH_SKIP_TESTS
1164 case "$this_test" in
1171 say_color skip >&3 "skipping test $this_test altogether"
1172 say_color skip "skip all tests in $this_test"
1177 # Provide an implementation of the 'yes' utility
1192 # Fix some commands on Windows
1195 # Windows has its own (incompatible) sort and find
1205 # git sees Windows-style pwd
1209 # no POSIX permissions
1210 # backslashes in pathspec are converted to '/'
1211 # exec does not inherit the PID
1214 test_set_prereq POSIXPERM
1215 test_set_prereq BSLASHPSPEC
1216 test_set_prereq EXECKEEPSPID
1220 test -z "$NO_PERL" && test_set_prereq PERL
1221 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1223 # test whether the filesystem supports symbolic links
1224 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1227 # declare prerequisites for external binaries used in tests
1228 test_declare_external_prereq dtach
1229 test_declare_external_prereq emacs
1230 test_declare_external_prereq ${TEST_EMACSCLIENT}
1231 test_declare_external_prereq gdb
1232 test_declare_external_prereq gpg
1233 test_declare_external_prereq python
1234 test_declare_external_prereq python2