2 test_description='hooks'
3 . $(dirname "$0")/test-lib.sh || exit 1
5 test_require_external_prereq xapian-delve
8 local TOKEN="${RANDOM}"
10 cat <<EOF >"${HOOK_DIR}/${1}"
12 echo "${TOKEN}" > ${3}
14 chmod +x "${HOOK_DIR}/${1}"
15 echo "${TOKEN}" > ${2}
18 create_printenv_hook () {
20 cat <<EOF >"${HOOK_DIR}/${1}"
22 printenv "${2}" > "${3}"
24 chmod +x "${HOOK_DIR}/${1}"
27 create_write_hook () {
28 local TOKEN="${RANDOM}"
30 cat <<EOF >"${HOOK_DIR}/${1}"
32 if xapian-delve ${MAIL_DIR}/.notmuch/xapian | grep -q "writing = false"; then
33 echo "${TOKEN}" > ${3}
36 chmod +x "${HOOK_DIR}/${1}"
37 echo "${TOKEN}" > ${2}
40 create_change_hook () {
42 cat <<EOF >"${HOOK_DIR}/${1}"
44 notmuch insert --no-hooks < ${2} > /dev/null
47 chmod +x "${HOOK_DIR}/${1}"
50 create_failing_hook () {
53 cat <<EOF >"${HOOK_DIR}/${1}"
57 chmod +x "${HOOK_DIR}/${1}"
60 # add a message to generate mail dir and database
62 # create maildir structure for notmuch-insert
63 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
65 ORIG_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
66 for config in traditional profile explicit relative XDG split; do
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}
74 HOOK_DIR=${MAIL_DIR}/.notmuch/hooks
77 dir=${HOME}/.config/notmuch/other
80 EXPECTED_CONFIG=${dir}/config
81 cp ${NOTMUCH_CONFIG} ${EXPECTED_CONFIG}
82 export NOTMUCH_PROFILE=other
86 HOOK_DIR=${HOME}/.notmuch-hooks
88 notmuch config set database.hook_dir $HOOK_DIR
91 HOOK_DIR=${HOME}/.notmuch-hooks
93 notmuch config set database.hook_dir .notmuch-hooks
96 HOOK_DIR=${HOME}/.config/notmuch/default/hooks
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
106 test_begin_subtest "pre-new is run [${config}]"
109 create_echo_hook "pre-new" expected output $HOOK_DIR
110 notmuch new > /dev/null
111 test_expect_equal_file expected output
113 test_begin_subtest "post-new is run [${config}]"
116 create_echo_hook "post-new" expected output $HOOK_DIR
117 notmuch new > /dev/null
118 test_expect_equal_file expected output
120 test_begin_subtest "post-insert hook is run [${config}]"
123 create_echo_hook "post-insert" expected output $HOOK_DIR
124 notmuch insert < "$gen_msg_filename"
125 test_expect_equal_file expected output
127 test_begin_subtest "pre-new is run before post-new [${config}]"
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
135 test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
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"
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"
146 # depends on the previous subtests leaving 1 new message behind
147 test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
149 output=$(NOTMUCH_NEW)
150 test_expect_equal "$output" "Added 1 new message to the database."
152 test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
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
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"
166 test_begin_subtest "post-insert hook does not affect insert status [${config}]"
169 create_failing_hook "post-insert" $HOOK_DIR
170 test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
172 test_begin_subtest "hook without executable permissions [${config}]"
175 cat <<EOF >"${HOOK_DIR}/pre-new"
179 output=`notmuch new 2>&1`
180 test_expect_code 1 "notmuch new"
182 test_begin_subtest "hook execution failure [${config}]"
185 cat <<EOF >"${HOOK_DIR}/pre-new"
186 no hashbang, execl fails
188 chmod +x "${HOOK_DIR}/pre-new"
189 test_expect_code 1 "notmuch new"
191 test_begin_subtest "post-new with write access [${config}]"
193 create_write_hook "post-new" write.expected write.output $HOOK_DIR
195 test_expect_equal_file write.expected write.output
197 test_begin_subtest "pre-new with write access [${config}]"
199 create_write_hook "pre-new" write.expected write.output $HOOK_DIR
201 test_expect_equal_file write.expected write.output
203 test_begin_subtest "add message in pre-new [${config}]"
205 generate_message '[subject]="add msg in pre-new"'
207 create_change_hook "pre-new" $gen_msg_filename $HOOK_DIR
208 generate_message '[subject]="add msg in 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)
215 test_expect_equal_file EXPECTED OUTPUT
217 test_begin_subtest "NOTMUCH_CONFIG is set"
218 create_printenv_hook "pre-new" NOTMUCH_CONFIG OUTPUT
223 test_expect_equal_file_nonempty EXPECTED OUTPUT
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
230 ${EXPECTED_CONFIG}.alternate
232 test_expect_equal_file_nonempty EXPECTED OUTPUT