2 # Copyright (c) 2010-2020 Notmuch Developers
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 https://www.gnu.org/licenses/ .
17 test_require_emacs () {
19 test_require_external_prereq "$TEST_EMACS" || ret=1
20 test_require_external_prereq "$TEST_EMACSCLIENT" || ret=1
21 test_require_external_prereq dtach || ret=1
25 # Deliver a message with emacs and add it to the database
27 # Uses emacs to generate and deliver a message to the mail store.
28 # Accepts arbitrary extra emacs/elisp functions to modify the message
29 # before sending, which is useful to doing things like attaching files
30 # to the message and encrypting/signing.
31 emacs_deliver_message () {
32 local subject body smtp_dummy_pid smtp_dummy_port
33 test_subtest_broken_for_installed
37 # before we can send a message, we have to prepare the FCC maildir
38 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
39 # eval'ing smtp-dummy --background will set smtp_dummy_pid and -_port
40 smtp_dummy_pid= smtp_dummy_port=
41 eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
42 test -n "$smtp_dummy_pid" || return 1
43 test -n "$smtp_dummy_port" || return 1
46 "(let ((message-send-mail-function 'message-smtpmail-send-it)
47 (mail-host-address \"example.com\")
48 (smtpmail-smtp-server \"localhost\")
49 (smtpmail-smtp-service \"${smtp_dummy_port}\"))
52 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
53 (message-goto-subject)
54 (insert \"${subject}\")
58 (let ((mml-secure-smime-sign-with-sender t)
59 (mml-secure-openpgp-sign-with-sender t))
60 (notmuch-mua-send-and-exit)))"
61 # In case message was sent properly, client waits for confirmation
62 # before exiting and resuming control here; therefore making sure
63 # that server exits by sending (KILL) signal to it is safe.
64 kill -9 $smtp_dummy_pid
65 notmuch new >/dev/null
68 # Pretend to deliver a message with emacs. Really save it to a file
69 # and add it to the database
71 # Uses emacs to generate and deliver a message to the mail store.
72 # Accepts arbitrary extra emacs/elisp functions to modify the message
73 # before sending, which is useful to doing things like attaching files
74 # to the message and encrypting/signing.
76 # If any GNU-style long-arguments (like --quiet or --decrypt=true) are
77 # at the head of the argument list, they are sent directly to "notmuch
78 # new" after message delivery
79 emacs_fcc_message () {
80 local nmn_args subject body
82 while [[ "$1" =~ ^-- ]]; do
83 nmn_args="$nmn_args $1"
89 # before we can send a message, we have to prepare the FCC maildir
90 mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
93 "(let ((message-send-mail-function (lambda () t))
94 (mail-host-address \"example.com\"))
97 (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
98 (message-goto-subject)
99 (insert \"${subject}\")
103 (let ((mml-secure-smime-sign-with-sender t)
104 (mml-secure-openpgp-sign-with-sender t))
105 (notmuch-mua-send-and-exit)))" || return 1
106 notmuch new $nmn_args >/dev/null
109 test_emacs_expect_t () {
112 error "bug in the test script: not 1 parameter to test_emacs_expect_t"
113 if [ -z "$inside_subtest" ]; then
114 error "bug in the test script: test_emacs_expect_t without test_begin_subtest"
118 if ! test_skip "$test_subtest_name"
120 test_emacs "(notmuch-test-run $1)" >/dev/null
122 # Restore state after the test.
123 exec 1>&6 2>&7 # Restore stdout and stderr
126 # test_emacs may update missing external prerequisites
127 test_check_missing_external_prereqs_ "$test_subtest_name" && return
129 # Report success/failure.
135 test_failure_ "${result}"
138 # Restore state after the (non) test.
139 exec 1>&6 2>&7 # Restore stdout and stderr
144 emacs_generate_script () {
145 # Construct a little test script here for the benefit of the user,
146 # (who can easily run "run_emacs" to get the same emacs environment
147 # for investigating any failures).
148 if [ -z "${NOTMUCH_TEST_INSTALLED-}" ]; then
149 find_notmuch_el='--directory "$NOTMUCH_BUILDDIR/emacs"'
151 ### XXX FIXME: this should really use the installed emacs lisp files
152 find_notmuch_el='--directory "$NOTMUCH_SRCDIR/emacs"'
155 cat <<EOF >"$TMP_DIRECTORY/run_emacs"
158 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
160 # Here's what we are using here:
162 # --quick Use minimal customization. This implies --no-init-file,
163 # --no-site-file and (emacs 24) --no-site-lisp
165 # --directory Ensure that the local elisp sources are found
167 # --load Force loading of notmuch.el and test-lib.el
169 exec ${TEST_EMACS} ${find_notmuch_el} --quick \
170 ${EXTRA_DIR} --load notmuch.el \
171 --directory "$NOTMUCH_SRCDIR/test" --load test-lib.el \
174 chmod a+x "$TMP_DIRECTORY/run_emacs"
178 # test dependencies beforehand to avoid the waiting loop below
179 test_require_emacs || return
181 if [ -z "$EMACS_SERVER" ]; then
182 emacs_tests="$NOTMUCH_SRCDIR/test/${this_test_bare}.el"
183 if [ -f "$emacs_tests" ]; then
184 load_emacs_tests="--eval '(load \"$emacs_tests\")'"
188 server_name="notmuch-test-suite-$$"
189 # start a detached session with an emacs server
190 # user's TERM (or 'vt100' in case user's TERM is known dumb
191 # or unknown) is given to dtach which assumes a minimally
192 # VT100-compatible terminal -- and emacs inherits that
193 TERM=$SMART_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
194 sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
197 --eval '(setq server-name \"$server_name\")' \
198 --eval '(server-start)' \
199 --eval '(orphan-watchdog $$)'" || return
200 EMACS_SERVER="$server_name"
201 # wait until the emacs server is up
202 until test_emacs '()' >/dev/null 2>/dev/null; do
207 # Clear test-output output file. Most Emacs tests end with a
208 # call to (test-output). If the test code fails with an
209 # exception before this call, the output file won't get
210 # updated. Since we don't want to compare against an output
211 # file from another test, so start out with an empty file.
215 ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(notmuch-test-progn $*)"
222 test_emacs "(test-time $*)" > emacs.out
226 emacs_generate_script