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_write_hook () {
19 local TOKEN="${RANDOM}"
21 cat <<EOF >"${HOOK_DIR}/${1}"
23 if xapian-delve ${MAIL_DIR}/.notmuch/xapian | grep -q "writing = false"; then
24 echo "${TOKEN}" > ${3}
27 chmod +x "${HOOK_DIR}/${1}"
28 echo "${TOKEN}" > ${2}
31 create_change_hook () {
33 cat <<EOF >"${HOOK_DIR}/${1}"
35 notmuch insert --no-hooks < ${2} > /dev/null
38 chmod +x "${HOOK_DIR}/${1}"
41 create_failing_hook () {
44 cat <<EOF >"${HOOK_DIR}/${1}"
48 chmod +x "${HOOK_DIR}/${1}"
51 # add a message to generate mail dir and database
53 # create maildir structure for notmuch-insert
54 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
56 for config in traditional profile explicit relative XDG split; do
58 notmuch config set database.hook_dir
59 notmuch config set database.path ${MAIL_DIR}
62 HOOK_DIR=${MAIL_DIR}/.notmuch/hooks
65 dir=${HOME}/.config/notmuch/other
68 cp ${NOTMUCH_CONFIG} ${dir}/config
69 export NOTMUCH_PROFILE=other
72 HOOK_DIR=${HOME}/.notmuch-hooks
74 notmuch config set database.hook_dir $HOOK_DIR
77 HOOK_DIR=${HOME}/.notmuch-hooks
79 notmuch config set database.hook_dir .notmuch-hooks
82 HOOK_DIR=${HOME}/.config/notmuch/default/hooks
85 dir="$TMP_DIRECTORY/database.$test_count"
86 notmuch config set database.path $dir
87 notmuch config set database.mail_root $MAIL_DIR
92 test_begin_subtest "pre-new is run [${config}]"
95 create_echo_hook "pre-new" expected output $HOOK_DIR
96 notmuch new > /dev/null
97 test_expect_equal_file expected output
99 test_begin_subtest "post-new is run [${config}]"
102 create_echo_hook "post-new" expected output $HOOK_DIR
103 notmuch new > /dev/null
104 test_expect_equal_file expected output
106 test_begin_subtest "post-insert hook is run [${config}]"
109 create_echo_hook "post-insert" expected output $HOOK_DIR
110 notmuch insert < "$gen_msg_filename"
111 test_expect_equal_file expected output
113 test_begin_subtest "pre-new is run before post-new [${config}]"
116 create_echo_hook "pre-new" pre-new.expected pre-new.output $HOOK_DIR
117 create_echo_hook "post-new" post-new.expected post-new.output $HOOK_DIR
118 notmuch new > /dev/null
119 test_expect_equal_file post-new.expected post-new.output
121 test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
124 create_failing_hook "pre-new" $HOOK_DIR
125 output=`notmuch new 2>&1`
126 test_expect_equal "$output" "Error: pre-new hook failed with status 13"
128 # depends on the previous subtest leaving broken hook behind
129 test_begin_subtest "pre-new non-zero exit status (notmuch status) [${config}]"
130 test_expect_code 1 "notmuch new"
132 # depends on the previous subtests leaving 1 new message behind
133 test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
135 output=$(NOTMUCH_NEW)
136 test_expect_equal "$output" "Added 1 new message to the database."
138 test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
141 create_failing_hook "post-new" $HOOK_DIR
142 NOTMUCH_NEW 2>output.stderr >output
143 cat output.stderr >> output
144 echo "Added 1 new message to the database." > expected
145 echo "Error: post-new hook failed with status 13" >> expected
146 test_expect_equal_file expected output
148 # depends on the previous subtest leaving broken hook behind
149 test_begin_subtest "post-new non-zero exit status (notmuch status) [${config}]"
150 test_expect_code 1 "notmuch new"
152 test_begin_subtest "post-insert hook does not affect insert status [${config}]"
155 create_failing_hook "post-insert" $HOOK_DIR
156 test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
158 test_begin_subtest "hook without executable permissions [${config}]"
161 cat <<EOF >"${HOOK_DIR}/pre-new"
165 output=`notmuch new 2>&1`
166 test_expect_code 1 "notmuch new"
168 test_begin_subtest "hook execution failure [${config}]"
171 cat <<EOF >"${HOOK_DIR}/pre-new"
172 no hashbang, execl fails
174 chmod +x "${HOOK_DIR}/pre-new"
175 test_expect_code 1 "notmuch new"
177 test_begin_subtest "post-new with write access [${config}]"
179 create_write_hook "post-new" write.expected write.output $HOOK_DIR
181 test_expect_equal_file write.expected write.output
183 test_begin_subtest "pre-new with write access [${config}]"
185 create_write_hook "pre-new" write.expected write.output $HOOK_DIR
187 test_expect_equal_file write.expected write.output
189 test_begin_subtest "add message in pre-new [${config}]"
191 generate_message '[subject]="add msg in pre-new"'
193 create_change_hook "pre-new" $gen_msg_filename $HOOK_DIR
194 generate_message '[subject]="add msg in new"'
196 notmuch search id:$id1 or id:$gen_msg_id | notmuch_search_sanitize > OUTPUT
197 cat <<EOF | sed s'/^[ \t]*//' > EXPECTED
198 thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; add msg in pre-new (inbox unread)
199 thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; add msg in new (inbox unread)
201 test_expect_equal_file EXPECTED OUTPUT