2 test_description='hooks'
3 . $(dirname "$0")/test-lib.sh || exit 1
6 local TOKEN="${RANDOM}"
8 cat <<EOF >"${HOOK_DIR}/${1}"
10 echo "${TOKEN}" > ${3}
12 chmod +x "${HOOK_DIR}/${1}"
13 echo "${TOKEN}" > ${2}
16 create_failing_hook () {
19 cat <<EOF >"${HOOK_DIR}/${1}"
23 chmod +x "${HOOK_DIR}/${1}"
26 # add a message to generate mail dir and database
28 # create maildir structure for notmuch-insert
29 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
31 for config in traditional profile explicit XDG; do
33 notmuch config set database.hook_dir
36 HOOK_DIR=${MAIL_DIR}/.notmuch/hooks
39 dir=${HOME}/.config/notmuch/other
42 cp ${NOTMUCH_CONFIG} ${dir}/config
43 export NOTMUCH_PROFILE=other
46 HOOK_DIR=${HOME}/.notmuch-hooks
48 notmuch config set database.hook_dir $HOOK_DIR
51 HOOK_DIR=${HOME}/.config/notmuch/default/hooks
55 test_begin_subtest "pre-new is run [${config}]"
58 create_echo_hook "pre-new" expected output $HOOK_DIR
59 notmuch new > /dev/null
60 test_expect_equal_file expected output
62 test_begin_subtest "post-new is run [${config}]"
65 create_echo_hook "post-new" expected output $HOOK_DIR
66 notmuch new > /dev/null
67 test_expect_equal_file expected output
69 test_begin_subtest "post-insert hook is run [${config}]"
72 create_echo_hook "post-insert" expected output $HOOK_DIR
73 notmuch insert < "$gen_msg_filename"
74 test_expect_equal_file expected output
76 test_begin_subtest "pre-new is run before post-new [${config}]"
79 create_echo_hook "pre-new" pre-new.expected pre-new.output $HOOK_DIR
80 create_echo_hook "post-new" post-new.expected post-new.output $HOOK_DIR
81 notmuch new > /dev/null
82 test_expect_equal_file post-new.expected post-new.output
84 test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
87 create_failing_hook "pre-new" $HOOK_DIR
88 output=`notmuch new 2>&1`
89 test_expect_equal "$output" "Error: pre-new hook failed with status 13"
91 # depends on the previous subtest leaving broken hook behind
92 test_begin_subtest "pre-new non-zero exit status (notmuch status) [${config}]"
93 test_expect_code 1 "notmuch new"
95 # depends on the previous subtests leaving 1 new message behind
96 test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
99 test_expect_equal "$output" "Added 1 new message to the database."
101 test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
104 create_failing_hook "post-new" $HOOK_DIR
105 NOTMUCH_NEW 2>output.stderr >output
106 cat output.stderr >> output
107 echo "Added 1 new message to the database." > expected
108 echo "Error: post-new hook failed with status 13" >> expected
109 test_expect_equal_file expected output
111 # depends on the previous subtest leaving broken hook behind
112 test_begin_subtest "post-new non-zero exit status (notmuch status) [${config}]"
113 test_expect_code 1 "notmuch new"
115 test_begin_subtest "post-insert hook does not affect insert status [${config}]"
118 create_failing_hook "post-insert" $HOOK_DIR
119 test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
121 test_begin_subtest "hook without executable permissions [${config}]"
124 cat <<EOF >"${HOOK_DIR}/pre-new"
128 output=`notmuch new 2>&1`
129 test_expect_code 1 "notmuch new"
131 test_begin_subtest "hook execution failure [${config}]"
134 cat <<EOF >"${HOOK_DIR}/pre-new"
135 no hashbang, execl fails
137 chmod +x "${HOOK_DIR}/pre-new"
138 test_expect_code 1 "notmuch new"