]> git.notmuchmail.org Git - notmuch/blob - test/T400-hooks.sh
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / test / T400-hooks.sh
1 #!/usr/bin/env bash
2 test_description='hooks'
3 . $(dirname "$0")/test-lib.sh || exit 1
4
5 test_require_external_prereq xapian-delve
6
7 create_echo_hook () {
8     local TOKEN="${RANDOM}"
9     mkdir -p ${HOOK_DIR}
10     cat <<EOF >"${HOOK_DIR}/${1}"
11 #!/bin/sh
12 echo "${TOKEN}" > ${3}
13 EOF
14     chmod +x "${HOOK_DIR}/${1}"
15     echo "${TOKEN}" > ${2}
16 }
17
18 create_printenv_hook () {
19     mkdir -p ${HOOK_DIR}
20     cat <<EOF >"${HOOK_DIR}/${1}"
21 #!/bin/sh
22 printenv "${2}" > "${3}"
23 EOF
24     chmod +x "${HOOK_DIR}/${1}"
25 }
26
27 create_write_hook () {
28     local TOKEN="${RANDOM}"
29     mkdir -p ${HOOK_DIR}
30     cat <<EOF >"${HOOK_DIR}/${1}"
31 #!/bin/sh
32 if xapian-delve ${MAIL_DIR}/.notmuch/xapian | grep -q "writing = false"; then
33    echo "${TOKEN}" > ${3}
34 fi
35 EOF
36     chmod +x "${HOOK_DIR}/${1}"
37     echo "${TOKEN}" > ${2}
38 }
39
40 create_change_hook () {
41     mkdir -p ${HOOK_DIR}
42     cat <<EOF >"${HOOK_DIR}/${1}"
43 #!/bin/sh
44 notmuch insert --no-hooks < ${2} > /dev/null
45 rm -f ${2}
46 EOF
47     chmod +x "${HOOK_DIR}/${1}"
48 }
49
50 create_failing_hook () {
51     local HOOK_DIR=${2}
52     mkdir -p ${HOOK_DIR}
53     cat <<EOF >"${HOOK_DIR}/${1}"
54 #!/bin/sh
55 exit 13
56 EOF
57     chmod +x "${HOOK_DIR}/${1}"
58 }
59
60 # add a message to generate mail dir and database
61 add_message
62 # create maildir structure for notmuch-insert
63 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
64
65 ORIG_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
66 for config in traditional profile explicit relative XDG split; do
67     unset NOTMUCH_PROFILE
68     export NOTMUCH_CONFIG=${ORIG_NOTMUCH_CONFIG}
69     EXPECTED_CONFIG=${NOTMUCH_CONFIG}
70     notmuch config set database.hook_dir
71     notmuch config set database.path ${MAIL_DIR}
72     case $config in
73         traditional)
74             HOOK_DIR=${MAIL_DIR}/.notmuch/hooks
75             ;;
76         profile)
77             dir=${HOME}/.config/notmuch/other
78             mkdir -p ${dir}
79             HOOK_DIR=${dir}/hooks
80             EXPECTED_CONFIG=${dir}/config
81             cp ${NOTMUCH_CONFIG} ${EXPECTED_CONFIG}
82             export NOTMUCH_PROFILE=other
83             unset NOTMUCH_CONFIG
84             ;;
85         explicit)
86             HOOK_DIR=${HOME}/.notmuch-hooks
87             mkdir -p $HOOK_DIR
88             notmuch config set database.hook_dir $HOOK_DIR
89             ;;
90         relative)
91             HOOK_DIR=${HOME}/.notmuch-hooks
92             mkdir -p $HOOK_DIR
93             notmuch config set database.hook_dir .notmuch-hooks
94             ;;
95         XDG)
96             HOOK_DIR=${HOME}/.config/notmuch/default/hooks
97             ;;
98         split)
99             dir="$TMP_DIRECTORY/database.$test_count"
100             notmuch config set database.path $dir
101             notmuch config set database.mail_root $MAIL_DIR
102             HOOK_DIR=${dir}/hooks
103             ;;
104     esac
105
106     test_begin_subtest "pre-new is run [${config}]"
107     rm -rf ${HOOK_DIR}
108     generate_message
109     create_echo_hook "pre-new" expected output $HOOK_DIR
110     notmuch new > /dev/null
111     test_expect_equal_file expected output
112
113     test_begin_subtest "post-new is run [${config}]"
114     rm -rf ${HOOK_DIR}
115     generate_message
116     create_echo_hook "post-new" expected output $HOOK_DIR
117     notmuch new > /dev/null
118     test_expect_equal_file expected output
119
120     test_begin_subtest "post-insert hook is run [${config}]"
121     rm -rf ${HOOK_DIR}
122     generate_message
123     create_echo_hook "post-insert" expected output $HOOK_DIR
124     notmuch insert < "$gen_msg_filename"
125     test_expect_equal_file expected output
126
127     test_begin_subtest "pre-new is run before post-new [${config}]"
128     rm -rf ${HOOK_DIR}
129     generate_message
130     create_echo_hook "pre-new" pre-new.expected pre-new.output $HOOK_DIR
131     create_echo_hook "post-new" post-new.expected post-new.output $HOOK_DIR
132     notmuch new > /dev/null
133     test_expect_equal_file post-new.expected post-new.output
134
135     test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
136     rm -rf ${HOOK_DIR}
137     generate_message
138     create_failing_hook "pre-new" $HOOK_DIR
139     output=`notmuch new 2>&1`
140     test_expect_equal "$output" "Error: pre-new hook failed with status 13"
141
142     # depends on the previous subtest leaving broken hook behind
143     test_begin_subtest "pre-new non-zero exit status (notmuch status) [${config}]"
144     test_expect_code 1 "notmuch new"
145
146     # depends on the previous subtests leaving 1 new message behind
147     test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
148     rm -rf ${HOOK_DIR}
149     output=$(NOTMUCH_NEW)
150     test_expect_equal "$output" "Added 1 new message to the database."
151
152     test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
153     rm -rf ${HOOK_DIR}
154     generate_message
155     create_failing_hook "post-new" $HOOK_DIR
156     NOTMUCH_NEW 2>output.stderr >output
157     cat output.stderr >> output
158     echo "Added 1 new message to the database." > expected
159     echo "Error: post-new hook failed with status 13" >> expected
160     test_expect_equal_file expected output
161
162     # depends on the previous subtest leaving broken hook behind
163     test_begin_subtest "post-new non-zero exit status (notmuch status) [${config}]"
164     test_expect_code 1 "notmuch new"
165
166     test_begin_subtest "post-insert hook does not affect insert status [${config}]"
167     rm -rf ${HOOK_DIR}
168     generate_message
169     create_failing_hook "post-insert" $HOOK_DIR
170     test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
171
172     test_begin_subtest "hook without executable permissions [${config}]"
173     rm -rf ${HOOK_DIR}
174     mkdir -p ${HOOK_DIR}
175     cat <<EOF >"${HOOK_DIR}/pre-new"
176     #!/bin/sh
177     echo foo
178 EOF
179     output=`notmuch new 2>&1`
180     test_expect_code 1 "notmuch new"
181
182     test_begin_subtest "hook execution failure [${config}]"
183     rm -rf ${HOOK_DIR}
184     mkdir -p ${HOOK_DIR}
185     cat <<EOF >"${HOOK_DIR}/pre-new"
186     no hashbang, execl fails
187 EOF
188     chmod +x "${HOOK_DIR}/pre-new"
189     test_expect_code 1 "notmuch new"
190
191     test_begin_subtest "post-new with write access [${config}]"
192     rm -rf ${HOOK_DIR}
193     create_write_hook "post-new" write.expected write.output $HOOK_DIR
194     NOTMUCH_NEW
195     test_expect_equal_file write.expected write.output
196
197     test_begin_subtest "pre-new with write access [${config}]"
198     rm -rf ${HOOK_DIR}
199     create_write_hook "pre-new" write.expected write.output $HOOK_DIR
200     NOTMUCH_NEW
201     test_expect_equal_file write.expected write.output
202
203     test_begin_subtest "add message in pre-new [${config}]"
204     rm -rf ${HOOK_DIR}
205     generate_message '[subject]="add msg in pre-new"'
206     id1=$gen_msg_id
207     create_change_hook "pre-new" $gen_msg_filename $HOOK_DIR
208     generate_message '[subject]="add msg in new"'
209     NOTMUCH_NEW
210     notmuch search id:$id1 or id:$gen_msg_id | notmuch_search_sanitize > OUTPUT
211     cat <<EOF | sed s'/^[ \t]*//' > EXPECTED
212     thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; add msg in pre-new (inbox unread)
213     thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; add msg in new (inbox unread)
214 EOF
215     test_expect_equal_file EXPECTED OUTPUT
216
217     test_begin_subtest "NOTMUCH_CONFIG is set"
218     create_printenv_hook "pre-new" NOTMUCH_CONFIG OUTPUT
219     NOTMUCH_NEW
220     cat <<EOF > EXPECTED
221 ${EXPECTED_CONFIG}
222 EOF
223     test_expect_equal_file_nonempty EXPECTED OUTPUT
224
225     test_begin_subtest "NOTMUCH_CONFIG is set by --config"
226     create_printenv_hook "pre-new" NOTMUCH_CONFIG OUTPUT
227     cp "${EXPECTED_CONFIG}" "${EXPECTED_CONFIG}.alternate"
228     notmuch --config "${EXPECTED_CONFIG}.alternate" new
229     cat <<EOF > EXPECTED
230 ${EXPECTED_CONFIG}.alternate
231 EOF
232     test_expect_equal_file_nonempty EXPECTED OUTPUT
233
234     rm -rf ${HOOK_DIR}
235 done
236 test_done