]> git.notmuchmail.org Git - notmuch/blob - test/T400-hooks.sh
Merge branch 'release'
[notmuch] / test / T400-hooks.sh
1 #!/usr/bin/env bash
2 test_description='hooks'
3 . $(dirname "$0")/test-lib.sh || exit 1
4
5 test_require_external_prereq xapian-delve
6
7 create_echo_hook () {
8     local TOKEN="${RANDOM}"
9     mkdir -p ${HOOK_DIR}
10     cat <<EOF >"${HOOK_DIR}/${1}"
11 #!/bin/sh
12 echo "${TOKEN}" > ${3}
13 EOF
14     chmod +x "${HOOK_DIR}/${1}"
15     echo "${TOKEN}" > ${2}
16 }
17
18 create_write_hook () {
19     local TOKEN="${RANDOM}"
20     mkdir -p ${HOOK_DIR}
21     cat <<EOF >"${HOOK_DIR}/${1}"
22 #!/bin/sh
23 if xapian-delve ${MAIL_DIR}/.notmuch/xapian | grep -q "writing = false"; then
24    echo "${TOKEN}" > ${3}
25 fi
26 EOF
27     chmod +x "${HOOK_DIR}/${1}"
28     echo "${TOKEN}" > ${2}
29 }
30
31 create_failing_hook () {
32     local HOOK_DIR=${2}
33     mkdir -p ${HOOK_DIR}
34     cat <<EOF >"${HOOK_DIR}/${1}"
35 #!/bin/sh
36 exit 13
37 EOF
38     chmod +x "${HOOK_DIR}/${1}"
39 }
40
41 # add a message to generate mail dir and database
42 add_message
43 # create maildir structure for notmuch-insert
44 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
45
46 for config in traditional profile explicit relative XDG split; do
47     unset NOTMUCH_PROFILE
48     notmuch config set database.hook_dir
49     notmuch config set database.path ${MAIL_DIR}
50     case $config in
51         traditional)
52             HOOK_DIR=${MAIL_DIR}/.notmuch/hooks
53             ;;
54         profile)
55             dir=${HOME}/.config/notmuch/other
56             mkdir -p ${dir}
57             HOOK_DIR=${dir}/hooks
58             cp ${NOTMUCH_CONFIG} ${dir}/config
59             export NOTMUCH_PROFILE=other
60             ;;
61         explicit)
62             HOOK_DIR=${HOME}/.notmuch-hooks
63             mkdir -p $HOOK_DIR
64             notmuch config set database.hook_dir $HOOK_DIR
65             ;;
66         relative)
67             HOOK_DIR=${HOME}/.notmuch-hooks
68             mkdir -p $HOOK_DIR
69             notmuch config set database.hook_dir .notmuch-hooks
70             ;;
71         XDG)
72             HOOK_DIR=${HOME}/.config/notmuch/default/hooks
73             ;;
74         split)
75             dir="$TMP_DIRECTORY/database.$test_count"
76             notmuch config set database.path $dir
77             notmuch config set database.mail_root $MAIL_DIR
78             HOOK_DIR=${dir}/hooks
79             ;;
80     esac
81
82     test_begin_subtest "pre-new is run [${config}]"
83     rm -rf ${HOOK_DIR}
84     generate_message
85     create_echo_hook "pre-new" expected output $HOOK_DIR
86     notmuch new > /dev/null
87     test_expect_equal_file expected output
88
89     test_begin_subtest "post-new is run [${config}]"
90     rm -rf ${HOOK_DIR}
91     generate_message
92     create_echo_hook "post-new" expected output $HOOK_DIR
93     notmuch new > /dev/null
94     test_expect_equal_file expected output
95
96     test_begin_subtest "post-insert hook is run [${config}]"
97     rm -rf ${HOOK_DIR}
98     generate_message
99     create_echo_hook "post-insert" expected output $HOOK_DIR
100     notmuch insert < "$gen_msg_filename"
101     test_expect_equal_file expected output
102
103     test_begin_subtest "pre-new is run before post-new [${config}]"
104     rm -rf ${HOOK_DIR}
105     generate_message
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
110
111     test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
112     rm -rf ${HOOK_DIR}
113     generate_message
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"
117
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"
121
122     # depends on the previous subtests leaving 1 new message behind
123     test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
124     rm -rf ${HOOK_DIR}
125     output=$(NOTMUCH_NEW)
126     test_expect_equal "$output" "Added 1 new message to the database."
127
128     test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
129     rm -rf ${HOOK_DIR}
130     generate_message
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
137
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"
141
142     test_begin_subtest "post-insert hook does not affect insert status [${config}]"
143     rm -rf ${HOOK_DIR}
144     generate_message
145     create_failing_hook "post-insert" $HOOK_DIR
146     test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
147
148     test_begin_subtest "hook without executable permissions [${config}]"
149     rm -rf ${HOOK_DIR}
150     mkdir -p ${HOOK_DIR}
151     cat <<EOF >"${HOOK_DIR}/pre-new"
152     #!/bin/sh
153     echo foo
154 EOF
155     output=`notmuch new 2>&1`
156     test_expect_code 1 "notmuch new"
157
158     test_begin_subtest "hook execution failure [${config}]"
159     rm -rf ${HOOK_DIR}
160     mkdir -p ${HOOK_DIR}
161     cat <<EOF >"${HOOK_DIR}/pre-new"
162     no hashbang, execl fails
163 EOF
164     chmod +x "${HOOK_DIR}/pre-new"
165     test_expect_code 1 "notmuch new"
166
167     test_begin_subtest "post-new with write access [${config}]"
168     rm -rf ${HOOK_DIR}
169     create_write_hook "post-new" write.expected write.output $HOOK_DIR
170     NOTMUCH_NEW
171     test_expect_equal_file write.expected write.output
172
173     test_begin_subtest "pre-new with write access [${config}]"
174     rm -rf ${HOOK_DIR}
175     create_write_hook "pre-new" write.expected write.output $HOOK_DIR
176     NOTMUCH_NEW
177     test_expect_equal_file write.expected write.output
178
179     rm -rf ${HOOK_DIR}
180 done
181 test_done