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 # if --tee was passed, write the output not only to the terminal, but
26 # additionally to the file test-results/$BASENAME.out, too.
27 case "$GIT_TEST_TEE_STARTED, $* " in
29 # do not redirect again
31 *' --tee '*|*' --va'*)
33 BASE=test-results/$(basename "$0" .sh)
34 (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
35 echo $? > $BASE.exit) | tee $BASE.out
36 test "$(cat $BASE.exit)" = 0
41 # Keep the original TERM for say_color and test_emacs
44 # For repeatability, reset the environment to known value.
50 export LANG LC_ALL PAGER TERM TZ
51 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
52 if [[ ( -n "$TEST_EMACS" && -z "$TEST_EMACSCLIENT" ) || \
53 ( -z "$TEST_EMACS" && -n "$TEST_EMACSCLIENT" ) ]]; then
54 echo "error: must specify both or neither of TEST_EMACS and TEST_EMACSCLIENT" >&2
57 TEST_EMACS=${TEST_EMACS:-${EMACS:-emacs}}
58 TEST_EMACSCLIENT=${TEST_EMACSCLIENT:-emacsclient}
60 # Protect ourselves from common misconfiguration to export
61 # CDPATH into the environment
68 # A regexp to match 5 and 40 hexdigits
69 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
70 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
72 _x04='[0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
73 _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
75 # Each test should start with something like this, after copyright notices:
77 # test_description='Description of this test...
78 # This test checks if command xyzzy does the right thing...
81 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
82 TERM=$ORIGINAL_TERM &&
85 tput bold >/dev/null 2>&1 &&
86 tput setaf 1 >/dev/null 2>&1 &&
87 tput sgr0 >/dev/null 2>&1
94 -d|--d|--de|--deb|--debu|--debug)
96 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
98 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
99 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
100 -h|--h|--he|--hel|--help)
102 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
104 -q|--q|--qu|--qui|--quie|--quiet)
107 with_dashes=t; shift ;;
113 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
114 valgrind=t; verbose=t; shift ;;
116 shift ;; # was handled already
118 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
121 echo "error: unknown test option '$1'" >&2; exit 1 ;;
125 if test -n "$debug"; then
127 printf " %-4s" "[$((test_count - 1))]"
135 if test -n "$color"; then
141 error) tput bold; tput setaf 1;; # bold red
142 skip) tput bold; tput setaf 2;; # bold green
143 pass) tput setaf 2;; # green
144 info) tput setaf 3;; # brown
145 *) test -n "$quiet" && return;;
156 test -z "$1" && test -n "$quiet" && return
165 say_color error "error: $*\n"
174 test "${test_description}" != "" ||
175 error "Test script did not set test_description."
177 if test "$help" = "t"
179 echo "Tests ${test_description}"
183 echo $(basename "$0"): "Testing ${test_description}"
195 rm -rf "$TEST_TMPDIR"
196 if test -n "$GIT_EXIT_OK"
200 echo >&5 "FATAL: Unexpected exit with code $code"
206 # Note: TEST_TMPDIR *NOT* exported!
207 TEST_TMPDIR=$(mktemp -d "${TMPDIR:-/tmp}/notmuch-test-$$.XXXXXX")
210 test_decode_color () {
211 sed -e 's/.\[1m/<WHITE>/g' \
212 -e 's/.\[31m/<RED>/g' \
213 -e 's/.\[32m/<GREEN>/g' \
214 -e 's/.\[33m/<YELLOW>/g' \
215 -e 's/.\[34m/<BLUE>/g' \
216 -e 's/.\[35m/<MAGENTA>/g' \
217 -e 's/.\[36m/<CYAN>/g' \
218 -e 's/.\[m/<RESET>/g'
230 sed -e 's/$/Q/' | tr Q '\015'
234 tr '\015' Q | sed -e 's/Q$//'
237 # Generate a new message in the mail directory, with a unique message
238 # ID and subject. The message is not added to the index.
240 # After this function returns, the filename of the generated message
241 # is available as $gen_msg_filename and the message ID is available as
244 # This function supports named parameters with the bash syntax for
245 # assigning a value to an associative array ([name]=value). The
246 # supported parameters are:
248 # [dir]=directory/of/choice
250 # Generate the message in directory 'directory/of/choice' within
251 # the mail store. The directory will be created if necessary.
255 # Store the message in file 'name'. The default is to store it
256 # in 'msg-<count>', where <count> is three-digit number of the
261 # Text to use as the body of the email message
263 # '[from]="Some User <user@example.com>"'
264 # '[to]="Some User <user@example.com>"'
265 # '[subject]="Subject of email message"'
266 # '[date]="RFC 822 Date"'
268 # Values for email headers. If not provided, default values will
269 # be generated instead.
271 # '[cc]="Some User <user@example.com>"'
272 # [reply-to]=some-address
273 # [in-reply-to]=<message-id>
274 # [references]=<message-id>
275 # [content-type]=content-type-specification
276 # '[header]=full header line, including keyword'
278 # Additional values for email headers. If these are not provided
279 # then the relevant headers will simply not appear in the
284 # Controls the message-id of the created message.
290 # This is our (bash-specific) magic for doing named parameters
291 local -A template="($@)"
292 local additional_headers
294 gen_msg_cnt=$((gen_msg_cnt + 1))
295 if [ -z "${template[filename]}" ]; then
296 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
298 gen_msg_name=${template[filename]}
301 if [ -z "${template[id]}" ]; then
302 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
304 gen_msg_id="${template[id]}"
307 if [ -z "${template[dir]}" ]; then
308 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
310 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
311 mkdir -p "$(dirname "$gen_msg_filename")"
314 if [ -z "${template[body]}" ]; then
315 template[body]="This is just a test message (#${gen_msg_cnt})"
318 if [ -z "${template[from]}" ]; then
319 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
322 if [ -z "${template[to]}" ]; then
323 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
326 if [ -z "${template[subject]}" ]; then
327 if [ -n "$test_subtest_name" ]; then
328 template[subject]="$test_subtest_name"
330 template[subject]="Test message #${gen_msg_cnt}"
334 if [ -z "${template[date]}" ]; then
335 template[date]="Fri, 05 Jan 2001 15:43:57 +0000"
338 additional_headers=""
339 if [ ! -z "${template[header]}" ]; then
340 additional_headers="${template[header]}
341 ${additional_headers}"
344 if [ ! -z "${template[reply-to]}" ]; then
345 additional_headers="Reply-To: ${template[reply-to]}
346 ${additional_headers}"
349 if [ ! -z "${template[in-reply-to]}" ]; then
350 additional_headers="In-Reply-To: ${template[in-reply-to]}
351 ${additional_headers}"
354 if [ ! -z "${template[cc]}" ]; then
355 additional_headers="Cc: ${template[cc]}
356 ${additional_headers}"
359 if [ ! -z "${template[bcc]}" ]; then
360 additional_headers="Bcc: ${template[bcc]}
361 ${additional_headers}"
364 if [ ! -z "${template[references]}" ]; then
365 additional_headers="References: ${template[references]}
366 ${additional_headers}"
369 if [ ! -z "${template[content-type]}" ]; then
370 additional_headers="Content-Type: ${template[content-type]}
371 ${additional_headers}"
374 if [ ! -z "${template[content-transfer-encoding]}" ]; then
375 additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
376 ${additional_headers}"
379 # Note that in the way we're setting it above and using it below,
380 # `additional_headers' will also serve as the header / body separator
381 # (empty line in between).
383 cat <<EOF >"$gen_msg_filename"
384 From: ${template[from]}
386 Message-Id: <${gen_msg_id}>
387 Subject: ${template[subject]}
388 Date: ${template[date]}
389 ${additional_headers}
394 # Generate a new message and add it to the database.
396 # All of the arguments and return values supported by generate_message
397 # are also supported here, so see that function for details.
400 generate_message "$@" &&
401 notmuch new > /dev/null
404 # Deliver a message with emacs and add it to the database
406 # Uses emacs to generate and deliver a message to the mail store.
407 # Accepts arbitrary extra emacs/elisp functions to modify the message
408 # before sending, which is useful to doing things like attaching files
409 # to the message and encrypting/signing.
410 emacs_deliver_message ()
415 # before we can send a message, we have to prepare the FCC maildir
416 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
417 # eval'ing smtp-dummy --background will set smtp_dummy_pid
419 eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
420 test -n "$smtp_dummy_pid" || return 1
423 "(let ((message-send-mail-function 'message-smtpmail-send-it)
424 (smtpmail-smtp-server \"localhost\")
425 (smtpmail-smtp-service \"25025\"))
429 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
430 (message-goto-subject)
431 (insert \"${subject}\")
435 (message-send-and-exit))"
437 # In case message was sent properly, client waits for confirmation
438 # before exiting and resuming control here; therefore making sure
439 # that server exits by sending (KILL) signal to it is safe.
440 kill -9 $smtp_dummy_pid
441 notmuch new >/dev/null
444 # Generate a corpus of email and add it to the database.
446 # This corpus is fixed, (it happens to be 50 messages from early in
447 # the history of the notmuch mailing list), which allows for reliably
448 # testing commands that need to operate on a not-totally-trivial
449 # number of messages.
453 if [ -d $TEST_DIRECTORY/corpus.mail ]; then
454 cp -a $TEST_DIRECTORY/corpus.mail ${MAIL_DIR}
456 cp -a $TEST_DIRECTORY/corpus ${MAIL_DIR}
457 notmuch new >/dev/null
458 cp -a ${MAIL_DIR} $TEST_DIRECTORY/corpus.mail
462 test_begin_subtest ()
464 if [ -n "$inside_subtest" ]; then
465 exec 1>&6 2>&7 # Restore stdout and stderr
466 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
468 test_subtest_name="$1"
470 # Remember stdout and stderr file descriptors and redirect test
471 # output to the previously prepared file descriptors 3 and 4 (see
473 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
474 exec 6>&1 7>&2 >&3 2>&4
478 # Pass test if two arguments match
480 # Note: Unlike all other test_expect_* functions, this function does
481 # not accept a test name. Instead, the caller should call
482 # test_begin_subtest before calling this function in order to set the
486 exec 1>&6 2>&7 # Restore stdout and stderr
488 test "$#" = 3 && { prereq=$1; shift; } || prereq=
490 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
494 if ! test_skip "$test_subtest_name"
496 if [ "$output" = "$expected" ]; then
497 test_ok_ "$test_subtest_name"
499 testname=$this_test.$test_count
500 echo "$expected" > $testname.expected
501 echo "$output" > $testname.output
502 test_failure_ "$test_subtest_name" "$(diff -u $testname.expected $testname.output)"
507 # Like test_expect_equal, but takes two filenames.
508 test_expect_equal_file ()
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"
517 basename1=`basename "$file1"`
519 basename2=`basename "$file2"`
520 if ! test_skip "$test_subtest_name"
522 if diff -q "$file1" "$file2" >/dev/null ; then
523 test_ok_ "$test_subtest_name"
525 testname=$this_test.$test_count
526 cp "$file1" "$testname.$basename1"
527 cp "$file2" "$testname.$basename2"
528 test_failure_ "$test_subtest_name" "$(diff -u "$testname.$basename1" "$testname.$basename2")"
533 # Like test_expect_equal, but arguments are JSON expressions to be
534 # canonicalized before diff'ing. If an argument cannot be parsed, it
535 # is used unchanged so that there's something to diff against.
536 test_expect_equal_json () {
537 output=$(echo "$1" | python -mjson.tool || echo "$1")
538 expected=$(echo "$2" | python -mjson.tool || echo "$2")
540 test_expect_equal "$output" "$expected" "$@"
543 test_emacs_expect_t () {
544 test "$#" = 2 && { prereq=$1; shift; } || prereq=
546 error "bug in the test script: not 1 or 2 parameters to test_emacs_expect_t"
549 if ! test_skip "$test_subtest_name"
551 test_emacs "(notmuch-test-run $1)" >/dev/null
553 # Restore state after the test.
554 exec 1>&6 2>&7 # Restore stdout and stderr
557 # Report success/failure.
561 test_ok_ "$test_subtest_name"
563 test_failure_ "$test_subtest_name" "${result}"
566 # Restore state after the (non) test.
567 exec 1>&6 2>&7 # Restore stdout and stderr
574 notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
577 notmuch_search_sanitize ()
579 sed -r -e 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
582 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
583 notmuch_show_sanitize ()
585 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
587 notmuch_show_sanitize_all ()
590 -e 's| filename:.*| filename:XXXXX|' \
591 -e 's| id:[^ ]* | id:XXXXX |'
594 notmuch_json_show_sanitize ()
597 -e 's|"id": "[^"]*",|"id": "XXXXX",|g' \
598 -e 's|"filename": "[^"]*",|"filename": "YYYYY",|g'
601 # End of notmuch helper functions
603 # Use test_set_prereq to tell that a particular prerequisite is available.
604 # The prerequisite can later be checked for in two ways:
606 # - Explicitly using test_have_prereq.
608 # - Implicitly by specifying the prerequisite tag in the calls to
609 # test_expect_{success,failure,code}.
611 # The single parameter is the prerequisite tag (a simple word, in all
612 # capital letters by convention).
615 satisfied="$satisfied$1 "
619 test_have_prereq () {
628 declare -A test_missing_external_prereq_
629 declare -A test_subtest_missing_external_prereq_
631 # declare prerequisite for the given external binary
632 test_declare_external_prereq () {
634 test "$#" = 2 && name=$2 || name="$binary(1)"
636 if ! hash $binary 2>/dev/null; then
637 test_missing_external_prereq_["${binary}"]=t
640 test_subtest_missing_external_prereq_[\"${name}\"]=t
646 # Explicitly require external prerequisite. Useful when binary is
647 # called indirectly (e.g. from emacs).
648 # Returns success if dependency is available, failure otherwise.
649 test_require_external_prereq () {
651 if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
652 # dependency is missing, call the replacement function to note it
659 # You are not expected to call test_ok_ and test_failure_ directly, use
660 # the text_expect_* functions instead.
663 if test "$test_subtest_known_broken_" = "t"; then
664 test_known_broken_ok_ "$@"
667 test_success=$(($test_success + 1))
668 say_color pass "%-6s" "PASS"
673 if test "$test_subtest_known_broken_" = "t"; then
674 test_known_broken_failure_ "$@"
677 test_failure=$(($test_failure + 1))
678 test_failure_message_ "FAIL" "$@"
679 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
683 test_failure_message_ () {
684 say_color error "%-6s" "$1"
687 echo "$@" | sed -e 's/^/ /'
688 if test "$verbose" != "t"; then cat test.output; fi
691 test_known_broken_ok_ () {
693 test_fixed=$(($test_fixed+1))
694 say_color pass "%-6s" "FIXED"
698 test_known_broken_failure_ () {
700 test_broken=$(($test_broken+1))
701 test_failure_message_ "BROKEN" "$@"
706 test "$debug" = "" || eval "$1"
711 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
714 eval >&3 2>&4 "$test_cleanup"
719 test_count=$(($test_count+1))
721 for skp in $NOTMUCH_SKIP_TESTS
723 case $this_test.$test_count in
728 if test -z "$to_skip" && test -n "$prereq" &&
729 ! test_have_prereq "$prereq"
735 test_report_skip_ "$@"
738 test_check_missing_external_prereqs_ "$@"
743 test_check_missing_external_prereqs_ () {
744 if [[ ${#test_subtest_missing_external_prereq_[@]} != 0 ]]; then
745 say_color skip >&1 "missing prerequisites: "
746 echo ${!test_subtest_missing_external_prereq_[@]} >&1
747 test_report_skip_ "$@"
753 test_report_skip_ () {
755 say_color skip >&3 "skipping test:"
757 say_color skip "%-6s" "SKIP"
761 test_subtest_known_broken () {
762 test_subtest_known_broken_=t
765 test_expect_success () {
766 test "$#" = 3 && { prereq=$1; shift; } || prereq=
768 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
774 # test_run_ may update missing external prerequisites
775 test_check_missing_external_prereqs_ "$@" ||
776 if [ "$run_ret" = 0 -a "$eval_ret" = 0 ]
785 test_expect_code () {
786 test "$#" = 4 && { prereq=$1; shift; } || prereq=
788 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
794 # test_run_ may update missing external prerequisites,
795 test_check_missing_external_prereqs_ "$@" ||
796 if [ "$run_ret" = 0 -a "$eval_ret" = "$1" ]
805 # test_external runs external test scripts that provide continuous
806 # test output about their progress, and succeeds/fails on
807 # zero/non-zero exit code. It outputs the test output on stdout even
808 # in non-verbose mode, and announces the external script with "* run
809 # <n>: ..." before running it. When providing relative paths, keep in
810 # mind that all scripts run in "trash directory".
811 # Usage: test_external description command arguments...
812 # Example: test_external 'Perl API' perl ../path/to/test.pl
814 test "$#" = 4 && { prereq=$1; shift; } || prereq=
816 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
820 if ! test_skip "$descr" "$@"
822 # Announce the script to reduce confusion about the
823 # test output that follows.
824 say_color "" " run $test_count: $descr ($*)"
825 # Run command; redirect its stderr to &4 as in
826 # test_run_, but keep its stdout on our stdout even in
833 test_failure_ "$descr" "$@"
838 # Like test_external, but in addition tests that the command generated
839 # no output on stderr.
840 test_external_without_stderr () {
841 # The temporary file has no (and must have no) security
843 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
844 stderr="$tmp/git-external-stderr.$$.tmp"
845 test_external "$@" 4> "$stderr"
846 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
847 descr="no stderr: $1"
849 if [ ! -s "$stderr" ]; then
853 if [ "$verbose" = t ]; then
854 output=`echo; echo Stderr is:; cat "$stderr"`
858 # rm first in case test_failure exits.
860 test_failure_ "$descr" "$@" "$output"
864 # This is not among top-level (test_expect_success)
865 # but is a prefix that can be used in the test script, like:
867 # test_expect_success 'complain and die' '
869 # do something else &&
870 # test_must_fail git checkout ../outerspace
873 # Writing this as "! git checkout ../outerspace" is wrong, because
874 # the failure could be due to a segv. We want a controlled failure.
878 test $? -gt 0 -a $? -le 129 -o $? -gt 192
881 # test_cmp is a helper function to compare actual and expected output.
882 # You can use it like:
884 # test_expect_success 'foo works' '
885 # echo expected >expected &&
887 # test_cmp expected actual
890 # This could be written as either "cmp" or "diff -u", but:
891 # - cmp's output is not nearly as easy to read as diff -u
892 # - not all diff versions understand "-u"
898 # This function can be used to schedule some commands to be run
899 # unconditionally at the end of the test to restore sanity:
901 # test_expect_success 'test core.capslock' '
902 # git config core.capslock true &&
903 # test_when_finished "git config --unset core.capslock" &&
907 # That would be roughly equivalent to
909 # test_expect_success 'test core.capslock' '
910 # git config core.capslock true &&
912 # git config --unset core.capslock
915 # except that the greeting and config --unset must both succeed for
918 test_when_finished () {
920 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
925 test_results_dir="$TEST_DIRECTORY/test-results"
926 mkdir -p "$test_results_dir"
927 test_results_path="$test_results_dir/${0%.sh}"
929 echo "total $test_count" >> $test_results_path
930 echo "success $test_success" >> $test_results_path
931 echo "fixed $test_fixed" >> $test_results_path
932 echo "broken $test_broken" >> $test_results_path
933 echo "failed $test_failure" >> $test_results_path
934 echo "" >> $test_results_path
938 [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'
940 if [ "$test_failure" = "0" ]; then
941 if [ "$test_broken" = "0" ]; then
950 emacs_generate_script () {
951 # Construct a little test script here for the benefit of the user,
952 # (who can easily run "run_emacs" to get the same emacs environment
953 # for investigating any failures).
954 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
957 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
959 # Here's what we are using here:
961 # --no-init-file Don't load users ~/.emacs
963 # --no-site-file Don't load the site-wide startup stuff
965 # --directory Ensure that the local elisp sources are found
967 # --load Force loading of notmuch.el and test-lib.el
969 exec ${TEST_EMACS} --no-init-file --no-site-file \
970 --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
971 --directory "$TEST_DIRECTORY" --load test-lib.el \
974 chmod a+x "$TMP_DIRECTORY/run_emacs"
978 # test dependencies beforehand to avoid the waiting loop below
979 missing_dependencies=
980 test_require_external_prereq dtach || missing_dependencies=1
981 test_require_external_prereq emacs || missing_dependencies=1
982 test_require_external_prereq ${TEST_EMACSCLIENT} || missing_dependencies=1
983 test -z "$missing_dependencies" || return
985 if [ -z "$EMACS_SERVER" ]; then
986 emacs_tests="$(basename $0).el"
987 if [ -f "$TEST_DIRECTORY/$emacs_tests" ]; then
988 load_emacs_tests="--eval '(load \"$emacs_tests\")'"
992 server_name="notmuch-test-suite-$$"
993 # start a detached session with an emacs server
994 # user's TERM is given to dtach which assumes a minimally
995 # VT100-compatible terminal -- and emacs inherits that
996 TERM=$ORIGINAL_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
997 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
1000 --eval '(setq server-name \"$server_name\")' \
1001 --eval '(server-start)' \
1002 --eval '(orphan-watchdog $$)'" || return
1003 EMACS_SERVER="$server_name"
1004 # wait until the emacs server is up
1005 until test_emacs '()' >/dev/null 2>/dev/null; do
1010 # Clear test-output output file. Most Emacs tests end with a
1011 # call to (test-output). If the test code fails with an
1012 # exception before this call, the output file won't get
1013 # updated. Since we don't want to compare against an output
1014 # file from another test, so start out with an empty file.
1018 ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(progn $@)"
1022 export LD_LIBRARY_PATH=$TEST_DIRECTORY/../lib
1023 export PYTHONPATH=$TEST_DIRECTORY/../bindings/python
1025 # Some distros (e.g. Arch Linux) ship Python 2.* as /usr/bin/python2,
1026 # most others as /usr/bin/python. So first try python2, and fallback to
1027 # python if python2 doesn't exist.
1029 [[ ${test_missing_external_prereq_[python2]} == t ]] && cmd=python
1031 (echo "import sys; _orig_stdout=sys.stdout; sys.stdout=open('OUTPUT', 'w')"; cat) \
1035 # Creates a script that counts how much time it is executed and calls
1036 # notmuch. $notmuch_counter_command is set to the path to the
1037 # generated script. Use notmuch_counter_value() function to get the
1038 # current counter value.
1039 notmuch_counter_reset () {
1040 notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
1041 if [ ! -x "$notmuch_counter_command" ]; then
1042 notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
1043 cat >"$notmuch_counter_command" <<EOF || return
1046 read count < "$notmuch_counter_state_path"
1047 echo \$((count + 1)) > "$notmuch_counter_state_path"
1051 chmod +x "$notmuch_counter_command" || return
1054 echo 0 > "$notmuch_counter_state_path"
1057 # Returns the current notmuch counter value.
1058 notmuch_counter_value () {
1059 if [ -r "$notmuch_counter_state_path" ]; then
1060 read count < "$notmuch_counter_state_path"
1067 test_reset_state_ () {
1068 test -z "$test_init_done_" && test_init_
1070 test_subtest_known_broken_=
1071 test_subtest_missing_external_prereq_=()
1074 # called once before the first subtest
1078 # skip all tests if there were external prerequisites missing during init
1079 test_check_missing_external_prereqs_ "all tests in $this_test" && test_done
1083 . ./test-lib-common.sh
1085 emacs_generate_script
1088 # Use -P to resolve symlinks in our working directory so that the cwd
1089 # in subprocesses like git equals our $PWD (for pathname comparisons).
1090 cd -P "$test" || error "Cannot setup test environment"
1092 if test "$verbose" = "t"
1096 exec 4>test.output 3>&4
1100 for skp in $NOTMUCH_SKIP_TESTS
1103 for skp in $NOTMUCH_SKIP_TESTS
1105 case "$this_test" in
1112 say_color skip >&3 "skipping test $this_test altogether"
1113 say_color skip "skip all tests in $this_test"
1118 # Provide an implementation of the 'yes' utility
1133 # Fix some commands on Windows
1136 # Windows has its own (incompatible) sort and find
1146 # git sees Windows-style pwd
1150 # no POSIX permissions
1151 # backslashes in pathspec are converted to '/'
1152 # exec does not inherit the PID
1155 test_set_prereq POSIXPERM
1156 test_set_prereq BSLASHPSPEC
1157 test_set_prereq EXECKEEPSPID
1161 test -z "$NO_PERL" && test_set_prereq PERL
1162 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1164 # test whether the filesystem supports symbolic links
1165 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1168 # declare prerequisites for external binaries used in tests
1169 test_declare_external_prereq dtach
1170 test_declare_external_prereq emacs
1171 test_declare_external_prereq ${TEST_EMACSCLIENT}
1172 test_declare_external_prereq gdb
1173 test_declare_external_prereq gpg
1174 test_declare_external_prereq python
1175 test_declare_external_prereq python2