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; do
48 notmuch config set database.hook_dir
51 HOOK_DIR=${MAIL_DIR}/.notmuch/hooks
54 dir=${HOME}/.config/notmuch/other
57 cp ${NOTMUCH_CONFIG} ${dir}/config
58 export NOTMUCH_PROFILE=other
61 HOOK_DIR=${HOME}/.notmuch-hooks
63 notmuch config set database.hook_dir $HOOK_DIR
66 HOOK_DIR=${HOME}/.config/notmuch/default/hooks
70 test_begin_subtest "pre-new is run [${config}]"
73 create_echo_hook "pre-new" expected output $HOOK_DIR
74 notmuch new > /dev/null
75 test_expect_equal_file expected output
77 test_begin_subtest "post-new is run [${config}]"
80 create_echo_hook "post-new" expected output $HOOK_DIR
81 notmuch new > /dev/null
82 test_expect_equal_file expected output
84 test_begin_subtest "post-insert hook is run [${config}]"
87 create_echo_hook "post-insert" expected output $HOOK_DIR
88 notmuch insert < "$gen_msg_filename"
89 test_expect_equal_file expected output
91 test_begin_subtest "pre-new is run before post-new [${config}]"
94 create_echo_hook "pre-new" pre-new.expected pre-new.output $HOOK_DIR
95 create_echo_hook "post-new" post-new.expected post-new.output $HOOK_DIR
96 notmuch new > /dev/null
97 test_expect_equal_file post-new.expected post-new.output
99 test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
102 create_failing_hook "pre-new" $HOOK_DIR
103 output=`notmuch new 2>&1`
104 test_expect_equal "$output" "Error: pre-new hook failed with status 13"
106 # depends on the previous subtest leaving broken hook behind
107 test_begin_subtest "pre-new non-zero exit status (notmuch status) [${config}]"
108 test_expect_code 1 "notmuch new"
110 # depends on the previous subtests leaving 1 new message behind
111 test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
113 output=$(NOTMUCH_NEW)
114 test_expect_equal "$output" "Added 1 new message to the database."
116 test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
119 create_failing_hook "post-new" $HOOK_DIR
120 NOTMUCH_NEW 2>output.stderr >output
121 cat output.stderr >> output
122 echo "Added 1 new message to the database." > expected
123 echo "Error: post-new hook failed with status 13" >> expected
124 test_expect_equal_file expected output
126 # depends on the previous subtest leaving broken hook behind
127 test_begin_subtest "post-new non-zero exit status (notmuch status) [${config}]"
128 test_expect_code 1 "notmuch new"
130 test_begin_subtest "post-insert hook does not affect insert status [${config}]"
133 create_failing_hook "post-insert" $HOOK_DIR
134 test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
136 test_begin_subtest "hook without executable permissions [${config}]"
139 cat <<EOF >"${HOOK_DIR}/pre-new"
143 output=`notmuch new 2>&1`
144 test_expect_code 1 "notmuch new"
146 test_begin_subtest "hook execution failure [${config}]"
149 cat <<EOF >"${HOOK_DIR}/pre-new"
150 no hashbang, execl fails
152 chmod +x "${HOOK_DIR}/pre-new"
153 test_expect_code 1 "notmuch new"
155 test_begin_subtest "post-new with write access [${config}]"
157 create_write_hook "post-new" write.expected write.output $HOOK_DIR
159 test_expect_equal_file write.expected write.output
161 test_begin_subtest "pre-new with write access [${config}]"
163 create_write_hook "pre-new" write.expected write.output $HOOK_DIR
165 test_expect_equal_file write.expected write.output