3 # Copyright (c) 2005 Junio C Hamano
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see http://www.gnu.org/licenses/ .
18 if [ ${BASH_VERSINFO[0]} -lt 4 ]; then
19 echo "Error: The notmuch test suite requires a bash version >= 4.0"
20 echo "due to use of associative arrays within the test suite."
21 echo "Please try again with a newer bash (or help us fix the"
22 echo "test suite to be more portable). Thanks."
26 # if --tee was passed, write the output not only to the terminal, but
27 # additionally to the file test-results/$BASENAME.out, too.
28 case "$GIT_TEST_TEE_STARTED, $* " in
30 # do not redirect again
32 *' --tee '*|*' --va'*)
34 BASE=test-results/$(basename "$0" .sh)
35 (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
36 echo $? > $BASE.exit) | tee $BASE.out
37 test "$(cat $BASE.exit)" = 0
42 # Keep the original TERM for say_color
45 # For repeatability, reset the environment to known value.
51 export LANG LC_ALL PAGER TERM TZ
52 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
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 "$color"; then
125 error) tput bold; tput setaf 1;; # bold red
126 skip) tput bold; tput setaf 2;; # bold green
127 pass) tput setaf 2;; # green
128 info) tput setaf 3;; # brown
129 *) test -n "$quiet" && return;;
139 test -z "$1" && test -n "$quiet" && return
147 say_color error "error: $*\n"
156 test "${test_description}" != "" ||
157 error "Test script did not set test_description."
159 if test "$help" = "t"
161 echo "Tests ${test_description}"
165 echo $(basename "$0"): "Testing ${test_description}"
177 if test -n "$GIT_EXIT_OK"
181 echo >&5 "FATAL: Unexpected exit with code $code"
189 test_decode_color () {
190 sed -e 's/.\[1m/<WHITE>/g' \
191 -e 's/.\[31m/<RED>/g' \
192 -e 's/.\[32m/<GREEN>/g' \
193 -e 's/.\[33m/<YELLOW>/g' \
194 -e 's/.\[34m/<BLUE>/g' \
195 -e 's/.\[35m/<MAGENTA>/g' \
196 -e 's/.\[36m/<CYAN>/g' \
197 -e 's/.\[m/<RESET>/g'
209 sed -e 's/$/Q/' | tr Q '\015'
213 tr '\015' Q | sed -e 's/Q$//'
216 # Notmuch helper functions
217 increment_mtime_amount=0
222 increment_mtime_amount=$((increment_mtime_amount + 1))
223 touch -d "+${increment_mtime_amount} seconds" "$dir"
226 # Generate a new message in the mail directory, with a unique message
227 # ID and subject. The message is not added to the index.
229 # After this function returns, the filename of the generated message
230 # is available as $gen_msg_filename and the message ID is available as
233 # This function supports named parameters with the bash syntax for
234 # assigning a value to an associative array ([name]=value). The
235 # supported parameters are:
237 # [dir]=directory/of/choice
239 # Generate the message in directory 'directory/of/choice' within
240 # the mail store. The directory will be created if necessary.
244 # Store the message in file 'name'. The default is to store it
245 # in 'msg-<count>', where <count> is three-digit number of the
250 # Text to use as the body of the email message
252 # '[from]="Some User <user@example.com>"'
253 # '[to]="Some User <user@example.com>"'
254 # '[subject]="Subject of email message"'
255 # '[date]="RFC 822 Date"'
257 # Values for email headers. If not provided, default values will
258 # be generated instead.
260 # '[cc]="Some User <user@example.com>"'
261 # [reply-to]=some-address
262 # [in-reply-to]=<message-id>
263 # [references]=<message-id>
264 # [content-type]=content-type-specification
265 # '[header]=full header line, including keyword'
267 # Additional values for email headers. If these are not provided
268 # then the relevant headers will simply not appear in the
273 # Controls the message-id of the created message.
279 # This is our (bash-specific) magic for doing named parameters
280 local -A template="($@)"
281 local additional_headers
283 gen_msg_cnt=$((gen_msg_cnt + 1))
284 if [ -z "${template[filename]}" ]; then
285 gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
287 gen_msg_name=${template[filename]}
290 if [ -z "${template[id]}" ]; then
291 gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
293 gen_msg_id="${template[id]}"
296 if [ -z "${template[dir]}" ]; then
297 gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
299 gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
300 mkdir -p "$(dirname "$gen_msg_filename")"
303 if [ -z "${template[body]}" ]; then
304 template[body]="This is just a test message (#${gen_msg_cnt})"
307 if [ -z "${template[from]}" ]; then
308 template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
311 if [ -z "${template[to]}" ]; then
312 template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
315 if [ -z "${template[subject]}" ]; then
316 template[subject]="Test message #${gen_msg_cnt}"
319 if [ -z "${template[date]}" ]; then
320 template[date]="Tue, 05 Jan 2001 15:43:57 -0000"
323 additional_headers=""
324 if [ ! -z "${template[header]}" ]; then
325 additional_headers="${template[header]}
326 ${additional_headers}"
329 if [ ! -z "${template[reply-to]}" ]; then
330 additional_headers="Reply-To: ${template[reply-to]}
331 ${additional_headers}"
334 if [ ! -z "${template[in-reply-to]}" ]; then
335 additional_headers="In-Reply-To: ${template[in-reply-to]}
336 ${additional_headers}"
339 if [ ! -z "${template[cc]}" ]; then
340 additional_headers="Cc: ${template[cc]}
341 ${additional_headers}"
344 if [ ! -z "${template[references]}" ]; then
345 additional_headers="References: ${template[references]}
346 ${additional_headers}"
349 if [ ! -z "${template[content-type]}" ]; then
350 additional_headers="Content-Type: ${template[content-type]}
351 ${additional_headers}"
354 # Note that in the way we're setting it above and using it below,
355 # `additional_headers' will also serve as the header / body separator
356 # (empty line in between).
358 cat <<EOF >"$gen_msg_filename"
359 From: ${template[from]}
361 Message-Id: <${gen_msg_id}>
362 Subject: ${template[subject]}
363 Date: ${template[date]}
364 ${additional_headers}
368 # Ensure that the mtime of the containing directory is updated
369 increment_mtime "$(dirname "${gen_msg_filename}")"
372 # Generate a new message and add it to the database.
374 # All of the arguments and return values supported by generate_message
375 # are also supported here, so see that function for details.
378 generate_message "$@" &&
379 notmuch new > /dev/null
382 # Generate a corpus of email and add it to the database.
384 # This corpus is fixed, (it happens to be 50 messages from early in
385 # the history of the notmuch mailing list), which allows for reliably
386 # testing commands that need to operate on a not-totally-trivial
387 # number of messages.
391 if [ -d ../corpus.mail ]; then
392 cp -a ../corpus.mail ${MAIL_DIR}
394 cp -a ../corpus ${MAIL_DIR}
395 notmuch new >/dev/null
396 cp -a ${MAIL_DIR} ../corpus.mail
400 test_begin_subtest ()
402 if [ -n "$inside_subtest" ]; then
403 exec 1>&6 2>&7 # Restore stdout and stderr
404 error "bug in test script: Missing test_expect_equal in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
406 test_subtest_name="$1"
407 # Remember stdout and stderr file descriptios and redirect test
408 # output to the previously prepared file descriptors 3 and 4 (see
410 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
411 exec 6>&1 7>&2 >&3 2>&4
415 # Pass test if two arguments match
417 # Note: Unlike all other test_expect_* functions, this function does
418 # not accept a test name. Instead, the caller should call
419 # test_begin_subtest before calling this function in order to set the
423 exec 1>&6 2>&7 # Restore stdout and stderr
425 test "$#" = 3 && { prereq=$1; shift; } || prereq=
427 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
433 if [ "$output" = "$expected" ]; then
434 test_ok_ "$test_subtest_name"
436 testname=$this_test.$test_count
437 echo "$expected" > $testname.expected
438 echo "$output" > $testname.output
439 test_failure_ "$test_subtest_name" "$(diff -u $testname.expected $testname.output)"
444 test_expect_equal_file ()
446 exec 1>&6 2>&7 # Restore stdout and stderr
448 test "$#" = 3 && { prereq=$1; shift; } || prereq=
450 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
456 if diff -q "$expected" "$output" >/dev/null ; then
457 test_ok_ "$test_subtest_name"
459 testname=$this_test.$test_count
460 mv "$output" $testname.output
461 mv "$expected" $testname.expected
462 test_failure_ "$test_subtest_name" "$(diff -u $testname.expected $testname.output)"
467 test_expect_equal_failure ()
469 exec 1>&6 2>&7 # Restore stdout and stderr
471 test "$#" = 3 && { prereq=$1; shift; } || prereq=
473 error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
479 if [ "$output" = "$expected" ]; then
480 test_known_broken_ok_ "$test_subtest_name"
482 test_known_broken_failure_ "$test_subtest_name"
483 testname=$this_test.$test_count
484 echo "$expected" > $testname.expected
485 echo "$output" > $testname.output
492 notmuch new | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
495 notmuch_search_sanitize ()
497 sed -r -e 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'
500 NOTMUCH_SHOW_FILENAME_SQUELCH='s,filename:.*/mail,filename:/XXX/mail,'
501 notmuch_show_sanitize ()
503 sed -e "$NOTMUCH_SHOW_FILENAME_SQUELCH"
505 notmuch_show_sanitize_all ()
508 -e 's| filename:.*| filename:XXXXX|' \
509 -e 's| id:[^ ]* | id:XXXXX |'
512 # End of notmuch helper functions
514 # Use test_set_prereq to tell that a particular prerequisite is available.
515 # The prerequisite can later be checked for in two ways:
517 # - Explicitly using test_have_prereq.
519 # - Implicitly by specifying the prerequisite tag in the calls to
520 # test_expect_{success,failure,code}.
522 # The single parameter is the prerequisite tag (a simple word, in all
523 # capital letters by convention).
526 satisfied="$satisfied$1 "
530 test_have_prereq () {
539 # You are not expected to call test_ok_ and test_failure_ directly, use
540 # the text_expect_* functions instead.
543 test_success=$(($test_success + 1))
544 say_color pass "%-6s" "PASS"
549 test_failure=$(($test_failure + 1))
550 say_color error "%-6s" "FAIL"
553 echo "$@" | sed -e 's/^/ /'
554 if test "$verbose" != "t"; then cat test.output; fi
555 test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
558 test_known_broken_ok_ () {
559 test_fixed=$(($test_fixed+1))
560 say_color pass "%-6s" "FIXED"
564 test_known_broken_failure_ () {
565 test_broken=$(($test_broken+1))
566 say_color pass "%-6s" "BROKEN"
571 test "$debug" = "" || eval "$1"
576 if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
579 eval >&3 2>&4 "$test_cleanup"
584 test_count=$(($test_count+1))
586 for skp in $NOTMUCH_SKIP_TESTS
588 case $this_test.$test_count in
593 if test -z "$to_skip" && test -n "$prereq" &&
594 ! test_have_prereq "$prereq"
600 say_color skip >&3 "skipping test: $@"
601 say_color skip "%-6s" "SKIP"
611 test_expect_failure () {
612 test "$#" = 3 && { prereq=$1; shift; } || prereq=
614 error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
618 if [ "$?" = 0 -a "$eval_ret" = 0 ]
620 test_known_broken_ok_ "$1"
622 test_known_broken_failure_ "$1"
627 test_expect_success () {
628 test "$#" = 3 && { prereq=$1; shift; } || prereq=
630 error "bug in the test script: not 2 or 3 parameters to test-expect-success"
634 if [ "$?" = 0 -a "$eval_ret" = 0 ]
643 test_expect_code () {
644 test "$#" = 4 && { prereq=$1; shift; } || prereq=
646 error "bug in the test script: not 3 or 4 parameters to test-expect-code"
650 if [ "$?" = 0 -a "$eval_ret" = "$1" ]
659 # test_external runs external test scripts that provide continuous
660 # test output about their progress, and succeeds/fails on
661 # zero/non-zero exit code. It outputs the test output on stdout even
662 # in non-verbose mode, and announces the external script with "* run
663 # <n>: ..." before running it. When providing relative paths, keep in
664 # mind that all scripts run in "trash directory".
665 # Usage: test_external description command arguments...
666 # Example: test_external 'Perl API' perl ../path/to/test.pl
668 test "$#" = 4 && { prereq=$1; shift; } || prereq=
670 error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
673 if ! test_skip "$descr" "$@"
675 # Announce the script to reduce confusion about the
676 # test output that follows.
677 say_color "" " run $test_count: $descr ($*)"
678 # Run command; redirect its stderr to &4 as in
679 # test_run_, but keep its stdout on our stdout even in
686 test_failure_ "$descr" "$@"
691 # Like test_external, but in addition tests that the command generated
692 # no output on stderr.
693 test_external_without_stderr () {
694 # The temporary file has no (and must have no) security
696 tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
697 stderr="$tmp/git-external-stderr.$$.tmp"
698 test_external "$@" 4> "$stderr"
699 [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
700 descr="no stderr: $1"
702 if [ ! -s "$stderr" ]; then
706 if [ "$verbose" = t ]; then
707 output=`echo; echo Stderr is:; cat "$stderr"`
711 # rm first in case test_failure exits.
713 test_failure_ "$descr" "$@" "$output"
717 # This is not among top-level (test_expect_success | test_expect_failure)
718 # but is a prefix that can be used in the test script, like:
720 # test_expect_success 'complain and die' '
722 # do something else &&
723 # test_must_fail git checkout ../outerspace
726 # Writing this as "! git checkout ../outerspace" is wrong, because
727 # the failure could be due to a segv. We want a controlled failure.
731 test $? -gt 0 -a $? -le 129 -o $? -gt 192
734 # test_cmp is a helper function to compare actual and expected output.
735 # You can use it like:
737 # test_expect_success 'foo works' '
738 # echo expected >expected &&
740 # test_cmp expected actual
743 # This could be written as either "cmp" or "diff -u", but:
744 # - cmp's output is not nearly as easy to read as diff -u
745 # - not all diff versions understand "-u"
751 # This function can be used to schedule some commands to be run
752 # unconditionally at the end of the test to restore sanity:
754 # test_expect_success 'test core.capslock' '
755 # git config core.capslock true &&
756 # test_when_finished "git config --unset core.capslock" &&
760 # That would be roughly equivalent to
762 # test_expect_success 'test core.capslock' '
763 # git config core.capslock true &&
765 # git config --unset core.capslock
768 # except that the greeting and config --unset must both succeed for
771 test_when_finished () {
773 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
778 test_results_dir="$TEST_DIRECTORY/test-results"
779 mkdir -p "$test_results_dir"
780 test_results_path="$test_results_dir/${0%.sh}-$$"
782 echo "total $test_count" >> $test_results_path
783 echo "success $test_success" >> $test_results_path
784 echo "fixed $test_fixed" >> $test_results_path
785 echo "broken $test_broken" >> $test_results_path
786 echo "failed $test_failure" >> $test_results_path
787 echo "" >> $test_results_path
791 if [ "$test_failure" = "0" ]; then
792 if [ "$test_broken" = "0" ]; then
802 # Construct a little test script here for the benefit of the user,
803 # (who can easily run "run_emacs" to get the same emacs environment
804 # for investigating any failures).
805 cat <<EOF > run_emacs
808 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
810 # We assume that the user will give a command-line argument only if
811 # wanting to run in batch mode.
812 if [ \$# -gt 0 ]; then
816 # Here's what we are using here:
818 # --batch: Quit after given commands and print all (messages)
820 # --no-init-file Don't load users ~/.emacs
822 # --no-site-file Don't load the site-wide startup stuff
824 # --directory Ensure that the local elisp sources are found
826 # --load Force loading of notmuch.el and test-lib.el
828 # notmuch-test-wait Function for tests to use to wait for process completion
830 # message-signature Avoiding appending user's signature on messages
832 # set-frame-width 80 columns (avoids crazy 10-column default of --batch)
834 emacs \$BATCH --no-init-file --no-site-file \
835 --directory ../../emacs --load notmuch.el \
836 --directory .. --load test-lib.el \
837 --eval "(defun notmuch-test-wait ()
838 (while (get-buffer-process (current-buffer))
840 --eval "(setq message-signature nil)" \
841 --eval "(progn (set-frame-width (window-frame (get-buffer-window)) 80) \$@)"
843 chmod a+x ./run_emacs
852 while [ -n "$dir" ]; do
854 if [ -x "$bin" ]; then
858 dir="$(dirname "$dir")"
859 if [ "$dir" = "/" ]; then
865 # Test the binaries we have just built. The tests are kept in
866 # test/ subdirectory and are run in 'trash directory' subdirectory.
867 TEST_DIRECTORY=$(pwd)
868 if test -n "$valgrind"
872 test "$1" = "$(readlink "$2")" || {
880 while test -d "$2".lock
882 say "Waiting for lock on $2."
889 make_valgrind_symlink () {
890 # handle only executables
891 test -x "$1" || return
893 base=$(basename "$1")
894 symlink_target=$TEST_DIRECTORY/../$base
895 # do not override scripts
896 if test -x "$symlink_target" &&
897 test ! -d "$symlink_target" &&
898 test "#!" != "$(head -c 2 < "$symlink_target")"
900 symlink_target=../valgrind.sh
904 symlink_target=../unprocessed-script
906 # create the link, or replace it if it is out of date
907 make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
910 # override notmuch executable in TEST_DIRECTORY/..
911 GIT_VALGRIND=$TEST_DIRECTORY/valgrind
912 mkdir -p "$GIT_VALGRIND"/bin
913 make_valgrind_symlink $TEST_DIRECTORY/../notmuch
918 ls "$path"/notmuch 2> /dev/null |
921 make_valgrind_symlink "$file"
925 PATH=$GIT_VALGRIND/bin:$PATH
926 GIT_EXEC_PATH=$GIT_VALGRIND/bin
929 notmuch_path=`find_notmuch_path "$TEST_DIRECTORY"`
930 test -n "$notmuch_path" && PATH="$notmuch_path:$PATH"
935 test="tmp.$(basename "$0" .sh)"
936 test -n "$root" && test="$root/$test"
938 /*) TMP_DIRECTORY="$test" ;;
939 *) TMP_DIRECTORY="$TEST_DIRECTORY/$test" ;;
941 test ! -z "$debug" || remove_tmp=$TMP_DIRECTORY
944 echo >&5 "FATAL: Cannot prepare test area"
948 MAIL_DIR="${TMP_DIRECTORY}/mail"
949 export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
952 mkdir -p "${MAIL_DIR}"
954 cat <<EOF >"${NOTMUCH_CONFIG}"
959 name=Notmuch Test Suite
960 primary_email=test_suite@notmuchmail.org
961 other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
965 # Use -P to resolve symlinks in our working directory so that the cwd
966 # in subprocesses like git equals our $PWD (for pathname comparisons).
967 cd -P "$test" || error "Cannot setup test environment"
969 if test "$verbose" = "t"
973 exec 4>test.output 3>&4
977 for skp in $NOTMUCH_SKIP_TESTS
980 for skp in $NOTMUCH_SKIP_TESTS
989 say_color skip >&3 "skipping test $this_test altogether"
990 say_color skip "skip all tests in $this_test"
995 # Provide an implementation of the 'yes' utility
1010 # Fix some commands on Windows
1013 # Windows has its own (incompatible) sort and find
1023 # git sees Windows-style pwd
1027 # no POSIX permissions
1028 # backslashes in pathspec are converted to '/'
1029 # exec does not inherit the PID
1032 test_set_prereq POSIXPERM
1033 test_set_prereq BSLASHPSPEC
1034 test_set_prereq EXECKEEPSPID
1038 test -z "$NO_PERL" && test_set_prereq PERL
1039 test -z "$NO_PYTHON" && test_set_prereq PYTHON
1041 # test whether the filesystem supports symbolic links
1042 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS