]> git.notmuchmail.org Git - notmuch/blob - test/test-lib-emacs.sh
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / test / test-lib-emacs.sh
1 #
2 # Copyright (c) 2010-2020 Notmuch Developers
3 #
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.
8 #
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.
13 #
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/ .
16
17 test_require_emacs () {
18     local ret=0
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
22     return $ret
23 }
24
25 # Deliver a message with emacs and add it to the database
26 #
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
34     subject="$1"
35     body="$2"
36     shift 2
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
44
45     test_emacs \
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}\"))
50            (notmuch-mua-mail)
51            (message-goto-to)
52            (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
53            (message-goto-subject)
54            (insert \"${subject}\")
55            (message-goto-body)
56            (insert \"${body}\")
57            $*
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
66 }
67
68 # Pretend to deliver a message with emacs. Really save it to a file
69 # and add it to the database
70 #
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.
75 #
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
81     nmn_args=''
82     while [[ "$1" =~ ^-- ]]; do
83         nmn_args="$nmn_args $1"
84         shift
85     done
86     subject="$1"
87     body="$2"
88     shift 2
89     # before we can send a message, we have to prepare the FCC maildir
90     mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
91
92     test_emacs \
93         "(let ((message-send-mail-function (lambda () t))
94                (mail-host-address \"example.com\"))
95            (notmuch-mua-mail)
96            (message-goto-to)
97            (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
98            (message-goto-subject)
99            (insert \"${subject}\")
100            (message-goto-body)
101            (insert \"${body}\")
102            $*
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
107 }
108
109 test_emacs_expect_t () {
110         local result
111         test "$#" = 1 ||
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"
115         fi
116
117         # Run the test.
118         if ! test_skip "$test_subtest_name"
119         then
120                 test_emacs "(notmuch-test-run $1)" >/dev/null
121
122                 # Restore state after the test.
123                 exec 1>&6 2>&7          # Restore stdout and stderr
124                 inside_subtest=
125
126                 # test_emacs may update missing external prerequisites
127                 test_check_missing_external_prereqs_ "$test_subtest_name" && return
128
129                 # Report success/failure.
130                 result=$(cat OUTPUT)
131                 if [ "$result" = t ]
132                 then
133                         test_ok_
134                 else
135                         test_failure_ "${result}"
136                 fi
137         else
138                 # Restore state after the (non) test.
139                 exec 1>&6 2>&7          # Restore stdout and stderr
140                 inside_subtest=
141         fi
142 }
143
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"'
150     else
151         ### XXX FIXME: this should really use the installed emacs lisp files
152         find_notmuch_el='--directory "$NOTMUCH_SRCDIR/emacs"'
153     fi
154
155         cat <<EOF >"$TMP_DIRECTORY/run_emacs"
156 #!/bin/sh
157 export PATH=$PATH
158 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
159
160 # Here's what we are using here:
161 #
162 # --quick               Use minimal customization. This implies --no-init-file,
163 #                       --no-site-file and (emacs 24) --no-site-lisp
164 #
165 # --directory           Ensure that the local elisp sources are found
166 #
167 # --load                Force loading of notmuch.el and test-lib.el
168
169 exec ${TEST_EMACS} ${find_notmuch_el} --quick \
170         ${EXTRA_DIR} --load notmuch.el \
171         --directory "$NOTMUCH_SRCDIR/test" --load test-lib.el \
172         "\$@"
173 EOF
174         chmod a+x "$TMP_DIRECTORY/run_emacs"
175 }
176
177 test_emacs () {
178         # test dependencies beforehand to avoid the waiting loop below
179         test_require_emacs || return
180
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\")'"
185                 else
186                         load_emacs_tests=
187                 fi
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' \
195                                 --no-window-system \
196                                 $load_emacs_tests \
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
203                         sleep 1
204                 done
205         fi
206
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.
212         rm -f OUTPUT
213         touch OUTPUT
214
215         ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(notmuch-test-progn $*)"
216 }
217
218 time_emacs () {
219     rm -f MESSAGES
220     printf "%s" "$1"
221     shift
222     test_emacs "(test-time $*)" > emacs.out
223     tail -n 1 MESSAGES
224 }
225
226 emacs_generate_script