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 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}/.config/notmuch/default/hooks
70 dir="$TMP_DIRECTORY/database.$test_count"
71 notmuch config set database.path $dir
72 notmuch config set database.mail_root $MAIL_DIR
77 test_begin_subtest "pre-new is run [${config}]"
80 create_echo_hook "pre-new" expected output $HOOK_DIR
81 notmuch new > /dev/null
82 test_expect_equal_file expected output
84 test_begin_subtest "post-new is run [${config}]"
87 create_echo_hook "post-new" expected output $HOOK_DIR
88 notmuch new > /dev/null
89 test_expect_equal_file expected output
91 test_begin_subtest "post-insert hook is run [${config}]"
94 create_echo_hook "post-insert" expected output $HOOK_DIR
95 notmuch insert < "$gen_msg_filename"
96 test_expect_equal_file expected output
98 test_begin_subtest "pre-new is run before post-new [${config}]"
101 create_echo_hook "pre-new" pre-new.expected pre-new.output $HOOK_DIR
102 create_echo_hook "post-new" post-new.expected post-new.output $HOOK_DIR
103 notmuch new > /dev/null
104 test_expect_equal_file post-new.expected post-new.output
106 test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
109 create_failing_hook "pre-new" $HOOK_DIR
110 output=`notmuch new 2>&1`
111 test_expect_equal "$output" "Error: pre-new hook failed with status 13"
113 # depends on the previous subtest leaving broken hook behind
114 test_begin_subtest "pre-new non-zero exit status (notmuch status) [${config}]"
115 test_expect_code 1 "notmuch new"
117 # depends on the previous subtests leaving 1 new message behind
118 test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
120 output=$(NOTMUCH_NEW)
121 test_expect_equal "$output" "Added 1 new message to the database."
123 test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
126 create_failing_hook "post-new" $HOOK_DIR
127 NOTMUCH_NEW 2>output.stderr >output
128 cat output.stderr >> output
129 echo "Added 1 new message to the database." > expected
130 echo "Error: post-new hook failed with status 13" >> expected
131 test_expect_equal_file expected output
133 # depends on the previous subtest leaving broken hook behind
134 test_begin_subtest "post-new non-zero exit status (notmuch status) [${config}]"
135 test_expect_code 1 "notmuch new"
137 test_begin_subtest "post-insert hook does not affect insert status [${config}]"
140 create_failing_hook "post-insert" $HOOK_DIR
141 test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
143 test_begin_subtest "hook without executable permissions [${config}]"
146 cat <<EOF >"${HOOK_DIR}/pre-new"
150 output=`notmuch new 2>&1`
151 test_expect_code 1 "notmuch new"
153 test_begin_subtest "hook execution failure [${config}]"
156 cat <<EOF >"${HOOK_DIR}/pre-new"
157 no hashbang, execl fails
159 chmod +x "${HOOK_DIR}/pre-new"
160 test_expect_code 1 "notmuch new"
162 test_begin_subtest "post-new with write access [${config}]"
164 create_write_hook "post-new" write.expected write.output $HOOK_DIR
166 test_expect_equal_file write.expected write.output
168 test_begin_subtest "pre-new with write access [${config}]"
170 create_write_hook "pre-new" write.expected write.output $HOOK_DIR
172 test_expect_equal_file write.expected write.output