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.
28 # if --tee was passed, write the output not only to the terminal, but
29 # additionally to the file test-results/$BASENAME.out, too.
30 case "$GIT_TEST_TEE_STARTED, $* " in
32 # do not redirect again
34 *' --tee '*|*' --va'*)
36 BASE=test-results/$(basename "$0" .sh)
37 (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
38 echo $? > $BASE.exit) | tee $BASE.out
39 test "$(cat $BASE.exit)" = 0
44 # Keep the original TERM for say_color and test_emacs
47 # dtach(1) provides more capable terminal environment to anything
48 # that requires more than dumb terminal...
49 [ x"${TERM:-dumb}" = xdumb ] && DTACH_TERM=vt100 || DTACH_TERM=$TERM
51 # For repeatability, reset the environment to known value.
57 export LANG LC_ALL PAGER TERM TZ
58 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
59 if [[ ( -n "$TEST_EMACS" && -z "$TEST_EMACSCLIENT" ) || \
60 ( -z "$TEST_EMACS" && -n "$TEST_EMACSCLIENT" ) ]]; then
61 echo "error: must specify both or neither of TEST_EMACS and TEST_EMACSCLIENT" >&2
64 TEST_EMACS=${TEST_EMACS:-${EMACS:-emacs}}
65 TEST_EMACSCLIENT=${TEST_EMACSCLIENT:-emacsclient}
67 # Protect ourselves from common misconfiguration to export
68 # CDPATH into the environment
75 # A regexp to match 5 and 40 hexdigits
76 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
77 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
79 _x04='[0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
80 _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
82 # Each test should start with something like this, after copyright notices:
84 # test_description='Description of this test...
85 # This test checks if command xyzzy does the right thing...
88 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
89 TERM=$ORIGINAL_TERM &&
92 tput bold >/dev/null 2>&1 &&
93 tput setaf 1 >/dev/null 2>&1 &&
94 tput sgr0 >/dev/null 2>&1
101 -d|--d|--de|--deb|--debu|--debug)
103 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
104 immediate=t; shift ;;
105 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
106 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
107 -h|--h|--he|--hel|--help)
109 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
111 -q|--q|--qu|--qui|--quie|--quiet)
114 with_dashes=t; shift ;;
120 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
121 valgrind=t; verbose=t; shift ;;
123 shift ;; # was handled already
125 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
128 echo "error: unknown test option '$1'" >&2; exit 1 ;;
132 if test -n "$debug"; then
134 printf " %-4s" "[$((test_count - 1))]"
142 if test -n "$color"; then
148 error) tput bold; tput setaf 1;; # bold red
149 skip) tput bold; tput setaf 2;; # bold green
150 pass) tput setaf 2;; # green
151 info) tput setaf 3;; # brown
152 *) test -n "$quiet" && return;;
163 test -z "$1" && test -n "$quiet" && return
172 say_color error "error: $*\n"
181 test "${test_description}" != "" ||
182 error "Test script did not set test_description."
184 if test "$help" = "t"
186 echo "Tests ${test_description}"
190 echo $(basename "$0"): "Testing ${test_description}"
204 rm -rf "$TEST_TMPDIR"
209 if test -n "$GIT_EXIT_OK"
214 say_color error '%-6s' FATAL
215 echo " $test_subtest_name"
217 echo "Unexpected exit while executing $0. Exit code $code."
224 echo >&5 "FATAL: $0: interrupted by signal" $((code - 128))
229 # Note: TEST_TMPDIR *NOT* exported!
230 TEST_TMPDIR=$(mktemp -d "${TMPDIR:-/tmp}/notmuch-test-$$.XXXXXX")
232 trap 'die_signal' HUP INT TERM
234 test_decode_color () {
235 sed -e 's/.\[1m/<WHITE>/g' \
236 -e 's/.\[31m/<RED>/g' \
237 -e 's/.\[32m/<GREEN>/g' \
238 -e 's/.\[33m/<YELLOW>/g' \
239 -e 's/.\[34m/<BLUE>/g' \
240 -e 's/.\[35m/<MAGENTA>/g' \
241 -e 's/.\[36m/<CYAN>/g' \
242 -e 's/.\[m/<RESET>/g'
254 sed -e 's/$/Q/' | tr Q '\015'
258 tr '\015' Q | sed -e 's/Q$//'
261 # Generate a new message in the mail directory, with a unique message
262 # ID and subject. The message is not added to the index.
264 # After this function returns, the filename of the generated message
265 # is available as $gen_msg_filename and the message ID is available as
268 # This function supports named parameters with the bash syntax for
269 # assigning a value to an associative array ([name]=value). The
270 # supported parameters are:
272 # [dir]=directory/of/choice
274 # Generate the message in directory 'directory/of/choice' within
275 # the mail store. The directory will be created if necessary.
279 # Store the message in file 'name'. The default is to store it
280 # in 'msg-<count>', where <count> is three-digit number of the
285 # Text to use as the body of the email message
287 # '[from]="Some User <user@example.com>"'
288 # '[to]="Some User <user@example.com>"'
289 # '[subject]="Subject of email message"'
290 # '[date]="RFC 822 Date"'
292 # Values for email headers. If not provided, default values will
293 # be generated instead.
295 # '[cc]="Some User <user@example.com>"'
296 # [reply-to]=some-address
297 # [in-reply-to]=<message-id>
298 # [references]=<message-id>
299 # [content-type]=content-type-specification
300 # '[header]=full header line, including keyword'
302 # Additional values for email headers. If these are not provided
303 # then the relevant headers will simply not appear in the
308 # Controls the message-id of the created message.
314 # This is our (bash-specific) magic for doing named parameters
315 local -A template="($@)"
316 local additional_headers
318 gen_msg_cnt=$((gen_msg_cnt + 1))
319 if [ -z "${template[filename]}" ]; then
320 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
322 gen_msg_name=${template[filename]}
325 if [ -z "${template[id]}" ]; then
326 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
328 gen_msg_id="${template[id]}"
331 if [ -z "${template[dir]}" ]; then
332 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
334 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
335 mkdir -p "$(dirname "$gen_msg_filename")"
338 if [ -z "${template[body]}" ]; then
339 template[body]="This is just a test message (#${gen_msg_cnt})"
342 if [ -z "${template[from]}" ]; then
343 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
346 if [ -z "${template[to]}" ]; then
347 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
350 if [ -z "${template[subject]}" ]; then
351 if [ -n "$test_subtest_name" ]; then
352 template[subject]="$test_subtest_name"
354 template[subject]="Test message #${gen_msg_cnt}"
358 if [ -z "${template[date]}" ]; then
359 template[date]="Fri, 05 Jan 2001 15:43:57 +0000"
362 additional_headers=""
363 if [ ! -z "${template[header]}" ]; then
364 additional_headers="${template[header]}
365 ${additional_headers}"
368 if [ ! -z "${template[reply-to]}" ]; then
369 additional_headers="Reply-To: ${template[reply-to]}
370 ${additional_headers}"
373 if [ ! -z "${template[in-reply-to]}" ]; then
374 additional_headers="In-Reply-To: ${template[in-reply-to]}
375 ${additional_headers}"
378 if [ ! -z "${template[cc]}" ]; then
379 additional_headers="Cc: ${template[cc]}
380 ${additional_headers}"
383 if [ ! -z "${template[bcc]}" ]; then
384 additional_headers="Bcc: ${template[bcc]}
385 ${additional_headers}"
388 if [ ! -z "${template[references]}" ]; then
389 additional_headers="References: ${template[references]}
390 ${additional_headers}"
393 if [ ! -z "${template[content-type]}" ]; then
394 additional_headers="Content-Type: ${template[content-type]}
395 ${additional_headers}"
398 if [ ! -z "${template[content-transfer-encoding]}" ]; then
399 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
400 ${additional_headers}"
403 # Note that in the way we're setting it above and using it below,
404 # `additional_headers' will also serve as the header / body separator
405 # (empty line in between).
407 cat <<EOF >"$gen_msg_filename"
408 From: ${template[from]}
410 Message-Id: <${gen_msg_id}>
411 Subject: ${template[subject]}
412 Date: ${template[date]}
413 ${additional_headers}
418 # Generate a new message and add it to the database.
420 # All of the arguments and return values supported by generate_message
421 # are also supported here, so see that function for details.
424 generate_message "$@" &&
425 notmuch new > /dev/null
428 # Deliver a message with emacs and add it to the database
430 # Uses emacs to generate and deliver a message to the mail store.
431 # Accepts arbitrary extra emacs/elisp functions to modify the message
432 # before sending, which is useful to doing things like attaching files
433 # to the message and encrypting/signing.
434 emacs_deliver_message ()
439 # before we can send a message, we have to prepare the FCC maildir
440 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
441 # eval'ing smtp-dummy --background will set smtp_dummy_pid
443 eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
444 test -n "$smtp_dummy_pid" || return 1
447 "(let ((message-send-mail-function 'message-smtpmail-send-it)
448 (smtpmail-smtp-server \"localhost\")
449 (smtpmail-smtp-service \"25025\"))
453 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
454 (message-goto-subject)
455 (insert \"${subject}\")
459 (message-send-and-exit))"
461 # In case message was sent properly, client waits for confirmation
462 # before exiting and resuming control here; therefore making sure
463 # that server exits by sending (KILL) signal to it is safe.
464 kill -9 $smtp_dummy_pid
465 notmuch new >/dev/null
468 # Generate a corpus of email and add it to the database.
470 # This corpus is fixed, (it happens to be 50 messages from early in
471 # the history of the notmuch mailing list), which allows for reliably
472 # testing commands that need to operate on a not-totally-trivial
473 # number of messages.
477 if [ -d $TEST_DIRECTORY/corpus.mail ]; then
478 cp -a $TEST_DIRECTORY/corpus.mail ${MAIL_DIR}
480 cp -a $TEST_DIRECTORY/corpus ${MAIL_DIR}
481 notmuch new >/dev/null
482 cp -a ${MAIL_DIR} $TEST_DIRECTORY/corpus.mail
486 test_begin_subtest ()
488 if [ -n "$inside_subtest" ]; then
489 exec 1>&6 2>&7 # Restore stdout and stderr
490 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
492 test_subtest_name="$1"
494 # Remember stdout and stderr file descriptors and redirect test
495 # output to the previously prepared file descriptors 3 and 4 (see
497 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
498 exec 6>&1 7>&2 >&3 2>&4
502 # Pass test if two arguments match
504 # Note: Unlike all other test_expect_* functions, this function does
505 # not accept a test name. Instead, the caller should call
506 # test_begin_subtest before calling this function in order to set the
510 exec 1>&6 2>&7 # Restore stdout and stderr
512 test "$#" = 3 && { prereq=$1; shift; } || prereq=
514 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
518 if ! test_skip "$test_subtest_name"
520 if [ "$output" = "$expected" ]; then
523 testname=$this_test.$test_count
524 echo "$expected" > $testname.expected
525 echo "$output" > $testname.output
526 test_failure_ "$(diff -u $testname.expected $testname.output)"
531 # Like test_expect_equal, but takes two filenames.
532 test_expect_equal_file ()
534 exec 1>&6 2>&7 # Restore stdout and stderr
536 test "$#" = 3 && { prereq=$1; shift; } || prereq=
538 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
541 basename1=`basename "$file1"`
543 basename2=`basename "$file2"`
544 if ! test_skip "$test_subtest_name"
546 if diff -q "$file1" "$file2" >/dev/null ; then
549 testname=$this_test.$test_count
550 cp "$file1" "$testname.$basename1"
551 cp "$file2" "$testname.$basename2"
552 test_failure_ "$(diff -u "$testname.$basename1" "$testname.$basename2")"
557 # Like test_expect_equal, but arguments are JSON expressions to be
558 # canonicalized before diff'ing. If an argument cannot be parsed, it
559 # is used unchanged so that there's something to diff against.
560 test_expect_equal_json () {
561 # The test suite forces LC_ALL=C, but this causes Python 3 to
562 # decode stdin as ASCII. We need to read JSON in UTF-8, so
563 # override Python's stdio encoding defaults.
564 output=$(echo "$1" | PYTHONIOENCODING=utf-8 python -mjson.tool \
566 expected=$(echo "$2" | PYTHONIOENCODING=utf-8 python -mjson.tool \
569 test_expect_equal "$output" "$expected" "$@"
572 test_emacs_expect_t () {
573 test "$#" = 2 && { prereq=$1; shift; } || prereq=
575 error "bug in the test script: not 1 or 2 parameters to test_emacs_expect_t"
578 if ! test_skip "$test_subtest_name"
580 test_emacs "(notmuch-test-run $1)" >/dev/null
582 # Restore state after the test.
583 exec 1>&6 2>&7 # Restore stdout and stderr
586 # Report success/failure.
592 test_failure_ "${result}"
595 # Restore state after the (non) test.
596 exec 1>&6 2>&7 # Restore stdout and stderr
603 notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
606 notmuch_search_sanitize ()
608 perl -pe 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
611 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
612 notmuch_show_sanitize ()
614 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
616 notmuch_show_sanitize_all ()
619 -e 's| filename:.*| filename:XXXXX|' \
620 -e 's| id:[^ ]* | id:XXXXX |'
623 notmuch_json_show_sanitize ()
626 -e 's|"id": "[^"]*",|"id": "XXXXX",|g' \
627 -e 's|"filename": "/[^"]*",|"filename": "YYYYY",|g'
630 # End of notmuch helper functions
632 # Use test_set_prereq to tell that a particular prerequisite is available.
633 # The prerequisite can later be checked for in two ways:
635 # - Explicitly using test_have_prereq.
637 # - Implicitly by specifying the prerequisite tag in the calls to
638 # test_expect_{success,failure,code}.
640 # The single parameter is the prerequisite tag (a simple word, in all
641 # capital letters by convention).
644 satisfied="$satisfied$1 "
648 test_have_prereq () {
657 declare -A test_missing_external_prereq_
658 declare -A test_subtest_missing_external_prereq_
660 # declare prerequisite for the given external binary
661 test_declare_external_prereq () {
663 test "$#" = 2 && name=$2 || name="$binary(1)"
665 if ! hash $binary 2>/dev/null; then
666 test_missing_external_prereq_["${binary}"]=t
669 test_subtest_missing_external_prereq_[\"${name}\"]=t
675 # Explicitly require external prerequisite. Useful when binary is
676 # called indirectly (e.g. from emacs).
677 # Returns success if dependency is available, failure otherwise.
678 test_require_external_prereq () {
680 if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
681 # dependency is missing, call the replacement function to note it
688 # You are not expected to call test_ok_ and test_failure_ directly, use
689 # the text_expect_* functions instead.
692 if test "$test_subtest_known_broken_" = "t"; then
693 test_known_broken_ok_
696 test_success=$(($test_success + 1))
697 say_color pass "%-6s" "PASS"
698 echo " $test_subtest_name"
702 if test "$test_subtest_known_broken_" = "t"; then
703 test_known_broken_failure_ "$@"
706 test_failure=$(($test_failure + 1))
707 test_failure_message_ "FAIL" "$test_subtest_name" "$@"
708 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
712 test_failure_message_ () {
713 say_color error "%-6s" "$1"
716 echo "$@" | sed -e 's/^/ /'
717 if test "$verbose" != "t"; then cat test.output; fi
720 test_known_broken_ok_ () {
722 test_fixed=$(($test_fixed+1))
723 say_color pass "%-6s" "FIXED"
724 echo " $test_subtest_name"
727 test_known_broken_failure_ () {
729 test_broken=$(($test_broken+1))
730 test_failure_message_ "BROKEN" "$test_subtest_name" "$@"
735 test "$debug" = "" || eval "$1"
740 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
743 eval >&3 2>&4 "$test_cleanup"
748 test_count=$(($test_count+1))
750 for skp in $NOTMUCH_SKIP_TESTS
752 case $this_test.$test_count in
757 if test -z "$to_skip" && test -n "$prereq" &&
758 ! test_have_prereq "$prereq"
764 test_report_skip_ "$@"
767 test_check_missing_external_prereqs_ "$@"
772 test_check_missing_external_prereqs_ () {
773 if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
774 say_color skip >&1 "missing prerequisites: "
775 echo ${!test_subtest_missing_external_prereq_[@]} >&1
776 test_report_skip_ "$@"
782 test_report_skip_ () {
784 say_color skip >&3 "skipping test:"
786 say_color skip "%-6s" "SKIP"
790 test_subtest_known_broken () {
791 test_subtest_known_broken_=t
794 test_expect_success () {
795 test "$#" = 3 && { prereq=$1; shift; } || prereq=
797 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
798 test_subtest_name="$1"
804 # test_run_ may update missing external prerequisites
805 test_check_missing_external_prereqs_ "$@" ||
806 if [ "$run_ret" = 0 -a "$eval_ret" = 0 ]
815 test_expect_code () {
816 test "$#" = 4 && { prereq=$1; shift; } || prereq=
818 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
819 test_subtest_name="$2"
825 # test_run_ may update missing external prerequisites,
826 test_check_missing_external_prereqs_ "$@" ||
827 if [ "$run_ret" = 0 -a "$eval_ret" = "$1" ]
831 test_failure_ "exit code $eval_ret, expected $1" "$3"
836 # test_external runs external test scripts that provide continuous
837 # test output about their progress, and succeeds/fails on
838 # zero/non-zero exit code. It outputs the test output on stdout even
839 # in non-verbose mode, and announces the external script with "* run
840 # <n>: ..." before running it. When providing relative paths, keep in
841 # mind that all scripts run in "trash directory".
842 # Usage: test_external description command arguments...
843 # Example: test_external 'Perl API' perl ../path/to/test.pl
845 test "$#" = 4 && { prereq=$1; shift; } || prereq=
847 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
848 test_subtest_name="$1"
851 if ! test_skip "$test_subtest_name" "$@"
853 # Announce the script to reduce confusion about the
854 # test output that follows.
855 say_color "" " run $test_count: $descr ($*)"
856 # Run command; redirect its stderr to &4 as in
857 # test_run_, but keep its stdout on our stdout even in
869 # Like test_external, but in addition tests that the command generated
870 # no output on stderr.
871 test_external_without_stderr () {
872 # The temporary file has no (and must have no) security
874 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
875 stderr="$tmp/git-external-stderr.$$.tmp"
876 test_external "$@" 4> "$stderr"
877 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
878 test_subtest_name="no stderr: $1"
880 if [ ! -s "$stderr" ]; then
884 if [ "$verbose" = t ]; then
885 output=`echo; echo Stderr is:; cat "$stderr"`
889 # rm first in case test_failure exits.
891 test_failure_ "$@" "$output"
895 # This is not among top-level (test_expect_success)
896 # but is a prefix that can be used in the test script, like:
898 # test_expect_success 'complain and die' '
900 # do something else &&
901 # test_must_fail git checkout ../outerspace
904 # Writing this as "! git checkout ../outerspace" is wrong, because
905 # the failure could be due to a segv. We want a controlled failure.
909 test $? -gt 0 -a $? -le 129 -o $? -gt 192
912 # test_cmp is a helper function to compare actual and expected output.
913 # You can use it like:
915 # test_expect_success 'foo works' '
916 # echo expected >expected &&
918 # test_cmp expected actual
921 # This could be written as either "cmp" or "diff -u", but:
922 # - cmp's output is not nearly as easy to read as diff -u
923 # - not all diff versions understand "-u"
929 # This function can be used to schedule some commands to be run
930 # unconditionally at the end of the test to restore sanity:
932 # test_expect_success 'test core.capslock' '
933 # git config core.capslock true &&
934 # test_when_finished "git config --unset core.capslock" &&
938 # That would be roughly equivalent to
940 # test_expect_success 'test core.capslock' '
941 # git config core.capslock true &&
943 # git config --unset core.capslock
946 # except that the greeting and config --unset must both succeed for
949 test_when_finished () {
951 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
956 test_results_dir="$TEST_DIRECTORY/test-results"
957 mkdir -p "$test_results_dir"
958 test_results_path="$test_results_dir/${0%.sh}"
960 echo "total $test_count" >> $test_results_path
961 echo "success $test_success" >> $test_results_path
962 echo "fixed $test_fixed" >> $test_results_path
963 echo "broken $test_broken" >> $test_results_path
964 echo "failed $test_failure" >> $test_results_path
965 echo "" >> $test_results_path
969 [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'
971 if [ "$test_failure" = "0" ]; then
972 if [ "$test_broken" = "0" ]; then
981 emacs_generate_script () {
982 # Construct a little test script here for the benefit of the user,
983 # (who can easily run "run_emacs" to get the same emacs environment
984 # for investigating any failures).
985 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
988 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
990 # Here's what we are using here:
992 # --no-init-file Don't load users ~/.emacs
994 # --no-site-file Don't load the site-wide startup stuff
996 # --directory Ensure that the local elisp sources are found
998 # --load Force loading of notmuch.el and test-lib.el
1000 exec ${TEST_EMACS} --no-init-file --no-site-file \
1001 --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
1002 --directory "$TEST_DIRECTORY" --load test-lib.el \
1005 chmod a+x "$TMP_DIRECTORY/run_emacs"
1009 # test dependencies beforehand to avoid the waiting loop below
1010 missing_dependencies=
1011 test_require_external_prereq dtach || missing_dependencies=1
1012 test_require_external_prereq emacs || missing_dependencies=1
1013 test_require_external_prereq ${TEST_EMACSCLIENT} || missing_dependencies=1
1014 test -z "$missing_dependencies" || return
1016 if [ -z "$EMACS_SERVER" ]; then
1017 emacs_tests="$(basename $0).el"
1018 if [ -f "$TEST_DIRECTORY/$emacs_tests" ]; then
1019 load_emacs_tests="--eval '(load \"$emacs_tests\")'"
1023 server_name="notmuch-test-suite-$$"
1024 # start a detached session with an emacs server
1025 # user's TERM (or 'vt100' in case user's TERM is unset, empty
1026 # or 'dumb') is given to dtach which assumes a minimally
1027 # VT100-compatible terminal -- and emacs inherits that
1028 TERM=$DTACH_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
1029 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
1030 --no-window-system \
1032 --eval '(setq server-name \"$server_name\")' \
1033 --eval '(server-start)' \
1034 --eval '(orphan-watchdog $$)'" || return
1035 EMACS_SERVER="$server_name"
1036 # wait until the emacs server is up
1037 until test_emacs '()' >/dev/null 2>/dev/null; do
1042 # Clear test-output output file. Most Emacs tests end with a
1043 # call to (test-output). If the test code fails with an
1044 # exception before this call, the output file won't get
1045 # updated. Since we don't want to compare against an output
1046 # file from another test, so start out with an empty file.
1050 ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(progn $@)"
1054 export LD_LIBRARY_PATH=$TEST_DIRECTORY/../lib
1055 export PYTHONPATH=$TEST_DIRECTORY/../bindings/python
1057 # Some distros (e.g. Arch Linux) ship Python 2.* as /usr/bin/python2,
1058 # most others as /usr/bin/python. So first try python2, and fallback to
1059 # python if python2 doesn't exist.
1061 [[ ${test_missing_external_prereq_[python2]} == t ]] && cmd=python
1063 (echo "import sys; _orig_stdout=sys.stdout; sys.stdout=open('OUTPUT', 'w')"; cat) \
1067 # Creates a script that counts how much time it is executed and calls
1068 # notmuch. $notmuch_counter_command is set to the path to the
1069 # generated script. Use notmuch_counter_value() function to get the
1070 # current counter value.
1071 notmuch_counter_reset () {
1072 notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
1073 if [ ! -x "$notmuch_counter_command" ]; then
1074 notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
1075 cat >"$notmuch_counter_command" <<EOF || return
1078 read count < "$notmuch_counter_state_path"
1079 echo \$((count + 1)) > "$notmuch_counter_state_path"
1083 chmod +x "$notmuch_counter_command" || return
1086 echo 0 > "$notmuch_counter_state_path"
1089 # Returns the current notmuch counter value.
1090 notmuch_counter_value () {
1091 if [ -r "$notmuch_counter_state_path" ]; then
1092 read count < "$notmuch_counter_state_path"
1099 test_reset_state_ () {
1100 test -z "$test_init_done_" && test_init_
1102 test_subtest_known_broken_=
1103 test_subtest_missing_external_prereq_=()
1106 # called once before the first subtest
1110 # skip all tests if there were external prerequisites missing during init
1111 test_check_missing_external_prereqs_ "all tests in $this_test" && test_done
1115 . ./test-lib-common.sh
1117 emacs_generate_script
1120 # Use -P to resolve symlinks in our working directory so that the cwd
1121 # in subprocesses like git equals our $PWD (for pathname comparisons).
1122 cd -P "$test" || error "Cannot setup test environment"
1124 if test "$verbose" = "t"
1128 exec 4>test.output 3>&4
1132 for skp in $NOTMUCH_SKIP_TESTS
1135 for skp in $NOTMUCH_SKIP_TESTS
1137 case "$this_test" in
1144 say_color skip >&3 "skipping test $this_test altogether"
1145 say_color skip "skip all tests in $this_test"
1150 # Provide an implementation of the 'yes' utility
1165 # Fix some commands on Windows
1168 # Windows has its own (incompatible) sort and find
1178 # git sees Windows-style pwd
1182 # no POSIX permissions
1183 # backslashes in pathspec are converted to '/'
1184 # exec does not inherit the PID
1187 test_set_prereq POSIXPERM
1188 test_set_prereq BSLASHPSPEC
1189 test_set_prereq EXECKEEPSPID
1193 test -z "$NO_PERL" && test_set_prereq PERL
1194 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1196 # test whether the filesystem supports symbolic links
1197 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1200 # declare prerequisites for external binaries used in tests
1201 test_declare_external_prereq dtach
1202 test_declare_external_prereq emacs
1203 test_declare_external_prereq ${TEST_EMACSCLIENT}
1204 test_declare_external_prereq gdb
1205 test_declare_external_prereq gpg
1206 test_declare_external_prereq python
1207 test_declare_external_prereq python2