]> 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     subject="$1"
34     body="$2"
35     shift 2
36     # before we can send a message, we have to prepare the FCC maildir
37     mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
38     # eval'ing smtp-dummy --background will set smtp_dummy_pid and -_port
39     smtp_dummy_pid= smtp_dummy_port=
40     eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
41     test -n "$smtp_dummy_pid" || return 1
42     test -n "$smtp_dummy_port" || return 1
43
44     test_emacs \
45         "(let ((message-send-mail-function 'message-smtpmail-send-it)
46                (mail-host-address \"example.com\")
47                (smtpmail-smtp-server \"localhost\")
48                (smtpmail-smtp-service \"${smtp_dummy_port}\"))
49            (notmuch-mua-mail)
50            (message-goto-to)
51            (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
52            (message-goto-subject)
53            (insert \"${subject}\")
54            (message-goto-body)
55            (insert \"${body}\")
56            $*
57            (let ((mml-secure-smime-sign-with-sender t)
58                  (mml-secure-openpgp-sign-with-sender t))
59              (notmuch-mua-send-and-exit)))"
60     # In case message was sent properly, client waits for confirmation
61     # before exiting and resuming control here; therefore making sure
62     # that server exits by sending (KILL) signal to it is safe.
63     kill -9 $smtp_dummy_pid
64     notmuch new >/dev/null
65 }
66
67 # Pretend to deliver a message with emacs. Really save it to a file
68 # and add it to the database
69 #
70 # Uses emacs to generate and deliver a message to the mail store.
71 # Accepts arbitrary extra emacs/elisp functions to modify the message
72 # before sending, which is useful to doing things like attaching files
73 # to the message and encrypting/signing.
74 #
75 # If any GNU-style long-arguments (like --quiet or --decrypt=true) are
76 # at the head of the argument list, they are sent directly to "notmuch
77 # new" after message delivery
78 emacs_fcc_message () {
79     local nmn_args subject body
80     nmn_args=''
81     while [[ "$1" =~ ^-- ]]; do
82         nmn_args="$nmn_args $1"
83         shift
84     done
85     subject="$1"
86     body="$2"
87     shift 2
88     # before we can send a message, we have to prepare the FCC maildir
89     mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
90
91     test_emacs \
92         "(let ((message-send-mail-function (lambda () t))
93                (mail-host-address \"example.com\"))
94            (notmuch-mua-mail)
95            (message-goto-to)
96            (insert \"test_suite@notmuchmail.org\nDate: 01 Jan 2000 12:00:00 -0000\")
97            (message-goto-subject)
98            (insert \"${subject}\")
99            (message-goto-body)
100            (insert \"${body}\")
101            $*
102            (let ((mml-secure-smime-sign-with-sender t)
103                  (mml-secure-openpgp-sign-with-sender t))
104              (notmuch-mua-send-and-exit)))" || return 1
105     notmuch new $nmn_args >/dev/null
106 }
107
108 test_emacs_expect_t () {
109         local result
110         test "$#" = 1 ||
111         error "bug in the test script: not 1 parameter to test_emacs_expect_t"
112         if [ -z "$inside_subtest" ]; then
113                 error "bug in the test script: test_emacs_expect_t without test_begin_subtest"
114         fi
115
116         # Run the test.
117         if ! test_skip "$test_subtest_name"
118         then
119                 test_emacs "(notmuch-test-run $1)" >/dev/null
120
121                 # Restore state after the test.
122                 exec 1>&6 2>&7          # Restore stdout and stderr
123                 inside_subtest=
124
125                 # test_emacs may update missing external prerequisites
126                 test_check_missing_external_prereqs_ "$test_subtest_name" && return
127
128                 # Report success/failure.
129                 result=$(cat OUTPUT)
130                 if [ "$result" = t ]
131                 then
132                         test_ok_
133                 else
134                         test_failure_ "${result}"
135                 fi
136         else
137                 # Restore state after the (non) test.
138                 exec 1>&6 2>&7          # Restore stdout and stderr
139                 inside_subtest=
140         fi
141 }
142
143 emacs_generate_script () {
144         # Construct a little test script here for the benefit of the user,
145         # (who can easily run "run_emacs" to get the same emacs environment
146         # for investigating any failures).
147         cat <<EOF >"$TMP_DIRECTORY/run_emacs"
148 #!/bin/sh
149 export PATH=$PATH
150 export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
151
152 # Here's what we are using here:
153 #
154 # --quick               Use minimal customization. This implies --no-init-file,
155 #                       --no-site-file and (emacs 24) --no-site-lisp
156 #
157 # --directory           Ensure that the local elisp sources are found
158 #
159 # --load                Force loading of notmuch.el and test-lib.el
160
161 exec ${TEST_EMACS} --quick \
162         --directory "$NOTMUCH_BUILDDIR/emacs" --load notmuch.el \
163         --directory "$NOTMUCH_SRCDIR/test" --load test-lib.el \
164         "\$@"
165 EOF
166         chmod a+x "$TMP_DIRECTORY/run_emacs"
167 }
168
169 test_emacs () {
170         # test dependencies beforehand to avoid the waiting loop below
171         test_require_emacs || return
172
173         if [ -z "$EMACS_SERVER" ]; then
174                 emacs_tests="$NOTMUCH_SRCDIR/test/${this_test_bare}.el"
175                 if [ -f "$emacs_tests" ]; then
176                         load_emacs_tests="--eval '(load \"$emacs_tests\")'"
177                 else
178                         load_emacs_tests=
179                 fi
180                 server_name="notmuch-test-suite-$$"
181                 # start a detached session with an emacs server
182                 # user's TERM (or 'vt100' in case user's TERM is known dumb
183                 # or unknown) is given to dtach which assumes a minimally
184                 # VT100-compatible terminal -- and emacs inherits that
185                 TERM=$SMART_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
186                         sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
187                                 --no-window-system \
188                                 $load_emacs_tests \
189                                 --eval '(setq server-name \"$server_name\")' \
190                                 --eval '(server-start)' \
191                                 --eval '(orphan-watchdog $$)'" || return
192                 EMACS_SERVER="$server_name"
193                 # wait until the emacs server is up
194                 until test_emacs '()' >/dev/null 2>/dev/null; do
195                         sleep 1
196                 done
197         fi
198
199         # Clear test-output output file.  Most Emacs tests end with a
200         # call to (test-output).  If the test code fails with an
201         # exception before this call, the output file won't get
202         # updated.  Since we don't want to compare against an output
203         # file from another test, so start out with an empty file.
204         rm -f OUTPUT
205         touch OUTPUT
206
207         ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(notmuch-test-progn $*)"
208 }
209
210 time_emacs () {
211     rm -f MESSAGES
212     printf "%s" "$1"
213     shift
214     test_emacs "(test-time $*)" > emacs.out
215     tail -n 1 MESSAGES
216 }
217
218 emacs_generate_script