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_failing_hook () {
34 cat <<EOF >"${HOOK_DIR}/${1}"
38 chmod +x "${HOOK_DIR}/${1}"
41 # add a message to generate mail dir and database
43 # create maildir structure for notmuch-insert
44 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
46 for config in traditional profile explicit relative XDG split; do
48 notmuch config set database.hook_dir
49 notmuch config set database.path ${MAIL_DIR}
52 HOOK_DIR=${MAIL_DIR}/.notmuch/hooks
55 dir=${HOME}/.config/notmuch/other
58 cp ${NOTMUCH_CONFIG} ${dir}/config
59 export NOTMUCH_PROFILE=other
62 HOOK_DIR=${HOME}/.notmuch-hooks
64 notmuch config set database.hook_dir $HOOK_DIR
67 HOOK_DIR=${HOME}/.notmuch-hooks
69 notmuch config set database.hook_dir .notmuch-hooks
72 HOOK_DIR=${HOME}/.config/notmuch/default/hooks
75 dir="$TMP_DIRECTORY/database.$test_count"
76 notmuch config set database.path $dir
77 notmuch config set database.mail_root $MAIL_DIR
82 test_begin_subtest "pre-new is run [${config}]"
85 create_echo_hook "pre-new" expected output $HOOK_DIR
86 notmuch new > /dev/null
87 test_expect_equal_file expected output
89 test_begin_subtest "post-new is run [${config}]"
92 create_echo_hook "post-new" expected output $HOOK_DIR
93 notmuch new > /dev/null
94 test_expect_equal_file expected output
96 test_begin_subtest "post-insert hook is run [${config}]"
99 create_echo_hook "post-insert" expected output $HOOK_DIR
100 notmuch insert < "$gen_msg_filename"
101 test_expect_equal_file expected output
103 test_begin_subtest "pre-new is run before post-new [${config}]"
106 create_echo_hook "pre-new" pre-new.expected pre-new.output $HOOK_DIR
107 create_echo_hook "post-new" post-new.expected post-new.output $HOOK_DIR
108 notmuch new > /dev/null
109 test_expect_equal_file post-new.expected post-new.output
111 test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
114 create_failing_hook "pre-new" $HOOK_DIR
115 output=`notmuch new 2>&1`
116 test_expect_equal "$output" "Error: pre-new hook failed with status 13"
118 # depends on the previous subtest leaving broken hook behind
119 test_begin_subtest "pre-new non-zero exit status (notmuch status) [${config}]"
120 test_expect_code 1 "notmuch new"
122 # depends on the previous subtests leaving 1 new message behind
123 test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
125 output=$(NOTMUCH_NEW)
126 test_expect_equal "$output" "Added 1 new message to the database."
128 test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
131 create_failing_hook "post-new" $HOOK_DIR
132 NOTMUCH_NEW 2>output.stderr >output
133 cat output.stderr >> output
134 echo "Added 1 new message to the database." > expected
135 echo "Error: post-new hook failed with status 13" >> expected
136 test_expect_equal_file expected output
138 # depends on the previous subtest leaving broken hook behind
139 test_begin_subtest "post-new non-zero exit status (notmuch status) [${config}]"
140 test_expect_code 1 "notmuch new"
142 test_begin_subtest "post-insert hook does not affect insert status [${config}]"
145 create_failing_hook "post-insert" $HOOK_DIR
146 test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
148 test_begin_subtest "hook without executable permissions [${config}]"
151 cat <<EOF >"${HOOK_DIR}/pre-new"
155 output=`notmuch new 2>&1`
156 test_expect_code 1 "notmuch new"
158 test_begin_subtest "hook execution failure [${config}]"
161 cat <<EOF >"${HOOK_DIR}/pre-new"
162 no hashbang, execl fails
164 chmod +x "${HOOK_DIR}/pre-new"
165 test_expect_code 1 "notmuch new"
167 test_begin_subtest "post-new with write access [${config}]"
169 create_write_hook "post-new" write.expected write.output $HOOK_DIR
171 test_expect_equal_file write.expected write.output
173 test_begin_subtest "pre-new with write access [${config}]"
175 create_write_hook "pre-new" write.expected write.output $HOOK_DIR
177 test_expect_equal_file write.expected write.output