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 TEST_EMACS=${TEST_EMACS:-${EMACS:-emacs}}
54 # Protect ourselves from common misconfiguration to export
55 # CDPATH into the environment
62 # A regexp to match 5 and 40 hexdigits
63 _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
64 _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
66 _x04='[0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
67 _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
69 # Each test should start with something like this, after copyright notices:
71 # test_description='Description of this test...
72 # This test checks if command xyzzy does the right thing...
75 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
76 TERM=$ORIGINAL_TERM &&
79 tput bold >/dev/null 2>&1 &&
80 tput setaf 1 >/dev/null 2>&1 &&
81 tput sgr0 >/dev/null 2>&1
88 -d|--d|--de|--deb|--debu|--debug)
90 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
92 -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
93 GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
94 -h|--h|--he|--hel|--help)
96 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
98 -q|--q|--qu|--qui|--quie|--quiet)
101 with_dashes=t; shift ;;
107 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
108 valgrind=t; verbose=t; shift ;;
110 shift ;; # was handled already
112 root=$(expr "z$1" : 'z[^=]*=\(.*\)')
115 echo "error: unknown test option '$1'" >&2; exit 1 ;;
119 if test -n "$debug"; then
121 printf " %-4s" "[$((test_count - 1))]"
129 if test -n "$color"; then
135 error) tput bold; tput setaf 1;; # bold red
136 skip) tput bold; tput setaf 2;; # bold green
137 pass) tput setaf 2;; # green
138 info) tput setaf 3;; # brown
139 *) test -n "$quiet" && return;;
150 test -z "$1" && test -n "$quiet" && return
159 say_color error "error: $*\n"
168 test "${test_description}" != "" ||
169 error "Test script did not set test_description."
171 if test "$help" = "t"
173 echo "Tests ${test_description}"
177 echo $(basename "$0"): "Testing ${test_description}"
189 rm -rf "$TEST_TMPDIR"
190 if test -n "$GIT_EXIT_OK"
194 echo >&5 "FATAL: Unexpected exit with code $code"
200 # Note: TEST_TMPDIR *NOT* exported!
201 TEST_TMPDIR=$(mktemp -d "${TMPDIR:-/tmp}/notmuch-test-$$.XXXXXX")
204 test_decode_color () {
205 sed -e 's/.\[1m/<WHITE>/g' \
206 -e 's/.\[31m/<RED>/g' \
207 -e 's/.\[32m/<GREEN>/g' \
208 -e 's/.\[33m/<YELLOW>/g' \
209 -e 's/.\[34m/<BLUE>/g' \
210 -e 's/.\[35m/<MAGENTA>/g' \
211 -e 's/.\[36m/<CYAN>/g' \
212 -e 's/.\[m/<RESET>/g'
224 sed -e 's/$/Q/' | tr Q '\015'
228 tr '\015' Q | sed -e 's/Q$//'
231 # Generate a new message in the mail directory, with a unique message
232 # ID and subject. The message is not added to the index.
234 # After this function returns, the filename of the generated message
235 # is available as $gen_msg_filename and the message ID is available as
238 # This function supports named parameters with the bash syntax for
239 # assigning a value to an associative array ([name]=value). The
240 # supported parameters are:
242 # [dir]=directory/of/choice
244 # Generate the message in directory 'directory/of/choice' within
245 # the mail store. The directory will be created if necessary.
249 # Store the message in file 'name'. The default is to store it
250 # in 'msg-<count>', where <count> is three-digit number of the
255 # Text to use as the body of the email message
257 # '[from]="Some User <user@example.com>"'
258 # '[to]="Some User <user@example.com>"'
259 # '[subject]="Subject of email message"'
260 # '[date]="RFC 822 Date"'
262 # Values for email headers. If not provided, default values will
263 # be generated instead.
265 # '[cc]="Some User <user@example.com>"'
266 # [reply-to]=some-address
267 # [in-reply-to]=<message-id>
268 # [references]=<message-id>
269 # [content-type]=content-type-specification
270 # '[header]=full header line, including keyword'
272 # Additional values for email headers. If these are not provided
273 # then the relevant headers will simply not appear in the
278 # Controls the message-id of the created message.
284 # This is our (bash-specific) magic for doing named parameters
285 local -A template="($@)"
286 local additional_headers
288 gen_msg_cnt=$((gen_msg_cnt + 1))
289 if [ -z "${template[filename]}" ]; then
290 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
292 gen_msg_name=${template[filename]}
295 if [ -z "${template[id]}" ]; then
296 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
298 gen_msg_id="${template[id]}"
301 if [ -z "${template[dir]}" ]; then
302 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
304 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
305 mkdir -p "$(dirname "$gen_msg_filename")"
308 if [ -z "${template[body]}" ]; then
309 template[body]="This is just a test message (#${gen_msg_cnt})"
312 if [ -z "${template[from]}" ]; then
313 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
316 if [ -z "${template[to]}" ]; then
317 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
320 if [ -z "${template[subject]}" ]; then
321 template[subject]="Test message #${gen_msg_cnt}"
324 if [ -z "${template[date]}" ]; then
325 template[date]="Fri, 05 Jan 2001 15:43:57 +0000"
328 additional_headers=""
329 if [ ! -z "${template[header]}" ]; then
330 additional_headers="${template[header]}
331 ${additional_headers}"
334 if [ ! -z "${template[reply-to]}" ]; then
335 additional_headers="Reply-To: ${template[reply-to]}
336 ${additional_headers}"
339 if [ ! -z "${template[in-reply-to]}" ]; then
340 additional_headers="In-Reply-To: ${template[in-reply-to]}
341 ${additional_headers}"
344 if [ ! -z "${template[cc]}" ]; then
345 additional_headers="Cc: ${template[cc]}
346 ${additional_headers}"
349 if [ ! -z "${template[references]}" ]; then
350 additional_headers="References: ${template[references]}
351 ${additional_headers}"
354 if [ ! -z "${template[content-type]}" ]; then
355 additional_headers="Content-Type: ${template[content-type]}
356 ${additional_headers}"
359 # Note that in the way we're setting it above and using it below,
360 # `additional_headers' will also serve as the header / body separator
361 # (empty line in between).
363 cat <<EOF >"$gen_msg_filename"
364 From: ${template[from]}
366 Message-Id: <${gen_msg_id}>
367 Subject: ${template[subject]}
368 Date: ${template[date]}
369 ${additional_headers}
374 # Generate a new message and add it to the database.
376 # All of the arguments and return values supported by generate_message
377 # are also supported here, so see that function for details.
380 generate_message "$@" &&
381 notmuch new > /dev/null
384 # Deliver a message with emacs and add it to the database
386 # Uses emacs to generate and deliver a message to the mail store.
387 # Accepts arbitrary extra emacs/elisp functions to modify the message
388 # before sending, which is useful to doing things like attaching files
389 # to the message and encrypting/signing.
390 emacs_deliver_message ()
395 # before we can send a message, we have to prepare the FCC maildir
396 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
397 $TEST_DIRECTORY/smtp-dummy sent_message &
400 "(let ((message-send-mail-function 'message-smtpmail-send-it)
401 (smtpmail-smtp-server \"localhost\")
402 (smtpmail-smtp-service \"25025\"))
406 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
407 (message-goto-subject)
408 (insert \"${subject}\")
412 (message-send-and-exit))"
413 # opportunistically quit smtp-dummy in case above fails.
414 { echo QUIT > /dev/tcp/localhost/25025; } 2>/dev/null
415 wait ${smtp_dummy_pid}
416 notmuch new >/dev/null
419 # Generate a corpus of email and add it to the database.
421 # This corpus is fixed, (it happens to be 50 messages from early in
422 # the history of the notmuch mailing list), which allows for reliably
423 # testing commands that need to operate on a not-totally-trivial
424 # number of messages.
428 if [ -d $TEST_DIRECTORY/corpus.mail ]; then
429 cp -a $TEST_DIRECTORY/corpus.mail ${MAIL_DIR}
431 cp -a $TEST_DIRECTORY/corpus ${MAIL_DIR}
432 notmuch new >/dev/null
433 cp -a ${MAIL_DIR} $TEST_DIRECTORY/corpus.mail
437 test_begin_subtest ()
439 if [ -n "$inside_subtest" ]; then
440 exec 1>&6 2>&7 # Restore stdout and stderr
441 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
443 test_subtest_name="$1"
445 # Remember stdout and stderr file descriptors and redirect test
446 # output to the previously prepared file descriptors 3 and 4 (see
448 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
449 exec 6>&1 7>&2 >&3 2>&4
453 # Pass test if two arguments match
455 # Note: Unlike all other test_expect_* functions, this function does
456 # not accept a test name. Instead, the caller should call
457 # test_begin_subtest before calling this function in order to set the
461 exec 1>&6 2>&7 # Restore stdout and stderr
463 test "$#" = 3 && { prereq=$1; shift; } || prereq=
465 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
469 if ! test_skip "$test_subtest_name"
471 if [ "$output" = "$expected" ]; then
472 test_ok_ "$test_subtest_name"
474 testname=$this_test.$test_count
475 echo "$expected" > $testname.expected
476 echo "$output" > $testname.output
477 test_failure_ "$test_subtest_name" "$(diff -u $testname.expected $testname.output)"
482 # Like test_expect_equal, but takes two filenames.
483 test_expect_equal_file ()
485 exec 1>&6 2>&7 # Restore stdout and stderr
487 test "$#" = 3 && { prereq=$1; shift; } || prereq=
489 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
493 if ! test_skip "$test_subtest_name"
495 if diff -q "$expected" "$output" >/dev/null ; then
496 test_ok_ "$test_subtest_name"
498 testname=$this_test.$test_count
499 cp "$output" $testname.output
500 cp "$expected" $testname.expected
501 test_failure_ "$test_subtest_name" "$(diff -u $testname.expected $testname.output)"
506 test_emacs_expect_t () {
507 test "$#" = 2 && { prereq=$1; shift; } || prereq=
509 error "bug in the test script: not 1 or 2 parameters to test_emacs_expect_t"
512 if ! test_skip "$test_subtest_name"
514 test_emacs "(notmuch-test-run $1)" >/dev/null
516 # Restore state after the test.
517 exec 1>&6 2>&7 # Restore stdout and stderr
520 # Report success/failure.
524 test_ok_ "$test_subtest_name"
526 test_failure_ "$test_subtest_name" "${result}"
529 # Restore state after the (non) test.
530 exec 1>&6 2>&7 # Restore stdout and stderr
537 notmuch new | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
540 notmuch_search_sanitize ()
542 sed -r -e 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
545 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
546 notmuch_show_sanitize ()
548 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
550 notmuch_show_sanitize_all ()
553 -e 's| filename:.*| filename:XXXXX|' \
554 -e 's| id:[^ ]* | id:XXXXX |'
557 notmuch_json_show_sanitize ()
559 sed -e 's|, |,\n |g' | \
561 -e 's|"id": "[^"]*",|"id": "XXXXX",|' \
562 -e 's|"filename": "[^"]*",|"filename": "YYYYY",|'
565 # End of notmuch helper functions
567 # Use test_set_prereq to tell that a particular prerequisite is available.
568 # The prerequisite can later be checked for in two ways:
570 # - Explicitly using test_have_prereq.
572 # - Implicitly by specifying the prerequisite tag in the calls to
573 # test_expect_{success,failure,code}.
575 # The single parameter is the prerequisite tag (a simple word, in all
576 # capital letters by convention).
579 satisfied="$satisfied$1 "
583 test_have_prereq () {
592 # declare prerequisite for the given external binary
593 test_declare_external_prereq () {
595 test "$#" = 2 && name=$2 || name="$binary(1)"
597 hash $binary 2>/dev/null || eval "
598 test_missing_external_prereq_${binary}_=t
600 echo -n \"\$test_subtest_missing_external_prereqs_ \" | grep -qe \" $name \" ||
601 test_subtest_missing_external_prereqs_=\"\$test_subtest_missing_external_prereqs_ $name\"
606 # Explicitly require external prerequisite. Useful when binary is
607 # called indirectly (e.g. from emacs).
608 # Returns success if dependency is available, failure otherwise.
609 test_require_external_prereq () {
611 if [ "$(eval echo -n \$test_missing_external_prereq_${binary}_)" = t ]; then
612 # dependency is missing, call the replacement function to note it
619 # You are not expected to call test_ok_ and test_failure_ directly, use
620 # the text_expect_* functions instead.
623 if test "$test_subtest_known_broken_" = "t"; then
624 test_known_broken_ok_ "$@"
627 test_success=$(($test_success + 1))
628 say_color pass "%-6s" "PASS"
633 if test "$test_subtest_known_broken_" = "t"; then
634 test_known_broken_failure_ "$@"
637 test_failure=$(($test_failure + 1))
638 test_failure_message_ "FAIL" "$@"
639 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
643 test_failure_message_ () {
644 say_color error "%-6s" "$1"
647 echo "$@" | sed -e 's/^/ /'
648 if test "$verbose" != "t"; then cat test.output; fi
651 test_known_broken_ok_ () {
653 test_fixed=$(($test_fixed+1))
654 say_color pass "%-6s" "FIXED"
658 test_known_broken_failure_ () {
660 test_broken=$(($test_broken+1))
661 test_failure_message_ "BROKEN" "$@"
666 test "$debug" = "" || eval "$1"
671 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
674 eval >&3 2>&4 "$test_cleanup"
679 test_count=$(($test_count+1))
681 for skp in $NOTMUCH_SKIP_TESTS
683 case $this_test.$test_count in
688 if test -z "$to_skip" && test -n "$prereq" &&
689 ! test_have_prereq "$prereq"
695 test_report_skip_ "$@"
698 test_check_missing_external_prereqs_ "$@"
703 test_check_missing_external_prereqs_ () {
704 if test -n "$test_subtest_missing_external_prereqs_"; then
705 say_color skip >&3 "missing prerequisites:"
706 echo "$test_subtest_missing_external_prereqs_" >&3
707 test_report_skip_ "$@"
713 test_report_skip_ () {
715 say_color skip >&3 "skipping test:"
717 say_color skip "%-6s" "SKIP"
721 test_subtest_known_broken () {
722 test_subtest_known_broken_=t
725 test_expect_success () {
726 test "$#" = 3 && { prereq=$1; shift; } || prereq=
728 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
734 # test_run_ may update missing external prerequisites
735 test_check_missing_external_prereqs_ "$@" ||
736 if [ "$run_ret" = 0 -a "$eval_ret" = 0 ]
745 test_expect_code () {
746 test "$#" = 4 && { prereq=$1; shift; } || prereq=
748 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
754 # test_run_ may update missing external prerequisites,
755 test_check_missing_external_prereqs_ "$@" ||
756 if [ "$run_ret" = 0 -a "$eval_ret" = "$1" ]
765 # test_external runs external test scripts that provide continuous
766 # test output about their progress, and succeeds/fails on
767 # zero/non-zero exit code. It outputs the test output on stdout even
768 # in non-verbose mode, and announces the external script with "* run
769 # <n>: ..." before running it. When providing relative paths, keep in
770 # mind that all scripts run in "trash directory".
771 # Usage: test_external description command arguments...
772 # Example: test_external 'Perl API' perl ../path/to/test.pl
774 test "$#" = 4 && { prereq=$1; shift; } || prereq=
776 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
780 if ! test_skip "$descr" "$@"
782 # Announce the script to reduce confusion about the
783 # test output that follows.
784 say_color "" " run $test_count: $descr ($*)"
785 # Run command; redirect its stderr to &4 as in
786 # test_run_, but keep its stdout on our stdout even in
793 test_failure_ "$descr" "$@"
798 # Like test_external, but in addition tests that the command generated
799 # no output on stderr.
800 test_external_without_stderr () {
801 # The temporary file has no (and must have no) security
803 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
804 stderr="$tmp/git-external-stderr.$$.tmp"
805 test_external "$@" 4> "$stderr"
806 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
807 descr="no stderr: $1"
809 if [ ! -s "$stderr" ]; then
813 if [ "$verbose" = t ]; then
814 output=`echo; echo Stderr is:; cat "$stderr"`
818 # rm first in case test_failure exits.
820 test_failure_ "$descr" "$@" "$output"
824 # This is not among top-level (test_expect_success)
825 # but is a prefix that can be used in the test script, like:
827 # test_expect_success 'complain and die' '
829 # do something else &&
830 # test_must_fail git checkout ../outerspace
833 # Writing this as "! git checkout ../outerspace" is wrong, because
834 # the failure could be due to a segv. We want a controlled failure.
838 test $? -gt 0 -a $? -le 129 -o $? -gt 192
841 # test_cmp is a helper function to compare actual and expected output.
842 # You can use it like:
844 # test_expect_success 'foo works' '
845 # echo expected >expected &&
847 # test_cmp expected actual
850 # This could be written as either "cmp" or "diff -u", but:
851 # - cmp's output is not nearly as easy to read as diff -u
852 # - not all diff versions understand "-u"
858 # This function can be used to schedule some commands to be run
859 # unconditionally at the end of the test to restore sanity:
861 # test_expect_success 'test core.capslock' '
862 # git config core.capslock true &&
863 # test_when_finished "git config --unset core.capslock" &&
867 # That would be roughly equivalent to
869 # test_expect_success 'test core.capslock' '
870 # git config core.capslock true &&
872 # git config --unset core.capslock
875 # except that the greeting and config --unset must both succeed for
878 test_when_finished () {
880 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
885 test_results_dir="$TEST_DIRECTORY/test-results"
886 mkdir -p "$test_results_dir"
887 test_results_path="$test_results_dir/${0%.sh}-$$"
889 echo "total $test_count" >> $test_results_path
890 echo "success $test_success" >> $test_results_path
891 echo "fixed $test_fixed" >> $test_results_path
892 echo "broken $test_broken" >> $test_results_path
893 echo "failed $test_failure" >> $test_results_path
894 echo "" >> $test_results_path
898 [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'
900 if [ "$test_failure" = "0" ]; then
901 if [ "$test_broken" = "0" ]; then
910 emacs_generate_script () {
911 # Construct a little test script here for the benefit of the user,
912 # (who can easily run "run_emacs" to get the same emacs environment
913 # for investigating any failures).
914 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
917 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
919 # Here's what we are using here:
921 # --no-init-file Don't load users ~/.emacs
923 # --no-site-file Don't load the site-wide startup stuff
925 # --directory Ensure that the local elisp sources are found
927 # --load Force loading of notmuch.el and test-lib.el
929 exec ${TEST_EMACS} --no-init-file --no-site-file \
930 --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
931 --directory "$TEST_DIRECTORY" --load test-lib.el \
934 chmod a+x "$TMP_DIRECTORY/run_emacs"
938 # test dependencies beforehand to avoid the waiting loop below
939 missing_dependencies=
940 test_require_external_prereq dtach || missing_dependencies=1
941 test_require_external_prereq emacs || missing_dependencies=1
942 test_require_external_prereq emacsclient || missing_dependencies=1
943 test -z "$missing_dependencies" || return
945 if [ -z "$EMACS_SERVER" ]; then
946 server_name="notmuch-test-suite-$$"
947 # start a detached session with an emacs server
948 # user's TERM is given to dtach which assumes a minimally
949 # VT100-compatible terminal -- and emacs inherits that
950 TERM=$ORIGINAL_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
951 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
953 --eval '(setq server-name \"$server_name\")' \
954 --eval '(server-start)' \
955 --eval '(orphan-watchdog $$)'" || return
956 EMACS_SERVER="$server_name"
957 # wait until the emacs server is up
958 until test_emacs '()' >/dev/null 2>/dev/null; do
963 emacsclient --socket-name="$EMACS_SERVER" --eval "(progn $@)"
967 export LD_LIBRARY_PATH=$TEST_DIRECTORY/../lib
968 export PYTHONPATH=$TEST_DIRECTORY/../bindings/python
970 # Some distros (e.g. Arch Linux) ship Python 2.* as /usr/bin/python2,
971 # most others as /usr/bin/python. So first try python2, and fallback to
972 # python if python2 doesn't exist.
974 [[ "$test_missing_external_prereq_python2_" = t ]] && cmd=python
976 (echo "import sys; _orig_stdout=sys.stdout; sys.stdout=open('OUTPUT', 'w')"; cat) \
980 # Creates a script that counts how much time it is executed and calls
981 # notmuch. $notmuch_counter_command is set to the path to the
982 # generated script. Use notmuch_counter_value() function to get the
983 # current counter value.
984 notmuch_counter_reset () {
985 notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
986 if [ ! -x "$notmuch_counter_command" ]; then
987 notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
988 cat >"$notmuch_counter_command" <<EOF || return
991 read count < "$notmuch_counter_state_path"
992 echo \$((count + 1)) > "$notmuch_counter_state_path"
996 chmod +x "$notmuch_counter_command" || return
999 echo 0 > "$notmuch_counter_state_path"
1002 # Returns the current notmuch counter value.
1003 notmuch_counter_value () {
1004 if [ -r "$notmuch_counter_state_path" ]; then
1005 read count < "$notmuch_counter_state_path"
1012 test_reset_state_ () {
1013 test -z "$test_init_done_" && test_init_
1015 test_subtest_known_broken_=
1016 test_subtest_missing_external_prereqs_=
1019 # called once before the first subtest
1023 # skip all tests if there were external prerequisites missing during init
1024 test_check_missing_external_prereqs_ "all tests in $this_test" && test_done
1028 find_notmuch_path ()
1032 while [ -n "$dir" ]; do
1034 if [ -x "$bin" ]; then
1038 dir="$(dirname "$dir")"
1039 if [ "$dir" = "/" ]; then
1045 # Test the binaries we have just built. The tests are kept in
1046 # test/ subdirectory and are run in 'trash directory' subdirectory.
1047 TEST_DIRECTORY=$(pwd)
1048 if test -n "$valgrind"
1052 test "$1" = "$(readlink "$2")" || {
1060 while test -d "$2".lock
1062 say "Waiting for lock on $2."
1069 make_valgrind_symlink () {
1070 # handle only executables
1071 test -x "$1" || return
1073 base=$(basename "$1")
1074 symlink_target=$TEST_DIRECTORY/../$base
1075 # do not override scripts
1076 if test -x "$symlink_target" &&
1077 test ! -d "$symlink_target" &&
1078 test "#!" != "$(head -c 2 < "$symlink_target")"
1080 symlink_target=$TEST_DIRECTORY/valgrind.sh
1084 symlink_target=$TEST_DIRECTORY/unprocessed-script
1086 # create the link, or replace it if it is out of date
1087 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
1090 # override notmuch executable in TEST_DIRECTORY/..
1091 GIT_VALGRIND=$TEST_DIRECTORY/valgrind
1092 mkdir -p "$GIT_VALGRIND"/bin
1093 make_valgrind_symlink $TEST_DIRECTORY/../notmuch
1098 ls "$path"/notmuch 2> /dev/null |
1101 make_valgrind_symlink "$file"
1105 PATH=$GIT_VALGRIND/bin:$PATH
1106 GIT_EXEC_PATH=$GIT_VALGRIND/bin
1109 notmuch_path=`find_notmuch_path "$TEST_DIRECTORY"`
1110 test -n "$notmuch_path" && PATH="$notmuch_path:$PATH"
1115 test="tmp.$(basename "$0" .sh)"
1116 test -n "$root" && test="$root/$test"
1118 /*) TMP_DIRECTORY="$test" ;;
1119 *) TMP_DIRECTORY="$TEST_DIRECTORY/$test" ;;
1121 test ! -z "$debug" || remove_tmp=$TMP_DIRECTORY
1124 echo >&5 "FATAL: Cannot prepare test area"
1128 # A temporary home directory is needed by at least:
1129 # - emacs/"Sending a message via (fake) SMTP"
1130 # - emacs/"Reply within emacs"
1131 # - crypto/emacs_deliver_message
1132 export HOME="${TMP_DIRECTORY}/home"
1135 MAIL_DIR="${TMP_DIRECTORY}/mail"
1136 export GNUPGHOME="${TMP_DIRECTORY}/gnupg"
1137 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
1140 mkdir -p "${MAIL_DIR}"
1142 cat <<EOF >"${NOTMUCH_CONFIG}"
1147 name=Notmuch Test Suite
1148 primary_email=test_suite@notmuchmail.org
1149 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
1152 emacs_generate_script
1155 # Use -P to resolve symlinks in our working directory so that the cwd
1156 # in subprocesses like git equals our $PWD (for pathname comparisons).
1157 cd -P "$test" || error "Cannot setup test environment"
1159 if test "$verbose" = "t"
1163 exec 4>test.output 3>&4
1167 for skp in $NOTMUCH_SKIP_TESTS
1170 for skp in $NOTMUCH_SKIP_TESTS
1172 case "$this_test" in
1179 say_color skip >&3 "skipping test $this_test altogether"
1180 say_color skip "skip all tests in $this_test"
1185 # Provide an implementation of the 'yes' utility
1200 # Fix some commands on Windows
1203 # Windows has its own (incompatible) sort and find
1213 # git sees Windows-style pwd
1217 # no POSIX permissions
1218 # backslashes in pathspec are converted to '/'
1219 # exec does not inherit the PID
1222 test_set_prereq POSIXPERM
1223 test_set_prereq BSLASHPSPEC
1224 test_set_prereq EXECKEEPSPID
1228 test -z "$NO_PERL" && test_set_prereq PERL
1229 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1231 # test whether the filesystem supports symbolic links
1232 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
1235 # declare prerequisites for external binaries used in tests
1236 test_declare_external_prereq dtach
1237 test_declare_external_prereq emacs
1238 test_declare_external_prereq emacsclient
1239 test_declare_external_prereq gdb
1240 test_declare_external_prereq gpg
1241 test_declare_external_prereq python
1242 test_declare_external_prereq python2