]> git.notmuchmail.org Git - notmuch/blob - test/T400-hooks.sh
test: change database from within pre-new hook
[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_change_hook () {
32     mkdir -p ${HOOK_DIR}
33     cat <<EOF >"${HOOK_DIR}/${1}"
34 #!/bin/sh
35 notmuch insert --no-hooks < ${2} > /dev/null
36 rm -f ${2}
37 EOF
38     chmod +x "${HOOK_DIR}/${1}"
39 }
40
41 create_failing_hook () {
42     local HOOK_DIR=${2}
43     mkdir -p ${HOOK_DIR}
44     cat <<EOF >"${HOOK_DIR}/${1}"
45 #!/bin/sh
46 exit 13
47 EOF
48     chmod +x "${HOOK_DIR}/${1}"
49 }
50
51 # add a message to generate mail dir and database
52 add_message
53 # create maildir structure for notmuch-insert
54 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
55
56 for config in traditional profile explicit relative XDG split; do
57     unset NOTMUCH_PROFILE
58     notmuch config set database.hook_dir
59     notmuch config set database.path ${MAIL_DIR}
60     case $config in
61         traditional)
62             HOOK_DIR=${MAIL_DIR}/.notmuch/hooks
63             ;;
64         profile)
65             dir=${HOME}/.config/notmuch/other
66             mkdir -p ${dir}
67             HOOK_DIR=${dir}/hooks
68             cp ${NOTMUCH_CONFIG} ${dir}/config
69             export NOTMUCH_PROFILE=other
70             ;;
71         explicit)
72             HOOK_DIR=${HOME}/.notmuch-hooks
73             mkdir -p $HOOK_DIR
74             notmuch config set database.hook_dir $HOOK_DIR
75             ;;
76         relative)
77             HOOK_DIR=${HOME}/.notmuch-hooks
78             mkdir -p $HOOK_DIR
79             notmuch config set database.hook_dir .notmuch-hooks
80             ;;
81         XDG)
82             HOOK_DIR=${HOME}/.config/notmuch/default/hooks
83             ;;
84         split)
85             dir="$TMP_DIRECTORY/database.$test_count"
86             notmuch config set database.path $dir
87             notmuch config set database.mail_root $MAIL_DIR
88             HOOK_DIR=${dir}/hooks
89             ;;
90     esac
91
92     test_begin_subtest "pre-new is run [${config}]"
93     rm -rf ${HOOK_DIR}
94     generate_message
95     create_echo_hook "pre-new" expected output $HOOK_DIR
96     notmuch new > /dev/null
97     test_expect_equal_file expected output
98
99     test_begin_subtest "post-new is run [${config}]"
100     rm -rf ${HOOK_DIR}
101     generate_message
102     create_echo_hook "post-new" expected output $HOOK_DIR
103     notmuch new > /dev/null
104     test_expect_equal_file expected output
105
106     test_begin_subtest "post-insert hook is run [${config}]"
107     rm -rf ${HOOK_DIR}
108     generate_message
109     create_echo_hook "post-insert" expected output $HOOK_DIR
110     notmuch insert < "$gen_msg_filename"
111     test_expect_equal_file expected output
112
113     test_begin_subtest "pre-new is run before post-new [${config}]"
114     rm -rf ${HOOK_DIR}
115     generate_message
116     create_echo_hook "pre-new" pre-new.expected pre-new.output $HOOK_DIR
117     create_echo_hook "post-new" post-new.expected post-new.output $HOOK_DIR
118     notmuch new > /dev/null
119     test_expect_equal_file post-new.expected post-new.output
120
121     test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
122     rm -rf ${HOOK_DIR}
123     generate_message
124     create_failing_hook "pre-new" $HOOK_DIR
125     output=`notmuch new 2>&1`
126     test_expect_equal "$output" "Error: pre-new hook failed with status 13"
127
128     # depends on the previous subtest leaving broken hook behind
129     test_begin_subtest "pre-new non-zero exit status (notmuch status) [${config}]"
130     test_expect_code 1 "notmuch new"
131
132     # depends on the previous subtests leaving 1 new message behind
133     test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
134     rm -rf ${HOOK_DIR}
135     output=$(NOTMUCH_NEW)
136     test_expect_equal "$output" "Added 1 new message to the database."
137
138     test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
139     rm -rf ${HOOK_DIR}
140     generate_message
141     create_failing_hook "post-new" $HOOK_DIR
142     NOTMUCH_NEW 2>output.stderr >output
143     cat output.stderr >> output
144     echo "Added 1 new message to the database." > expected
145     echo "Error: post-new hook failed with status 13" >> expected
146     test_expect_equal_file expected output
147
148     # depends on the previous subtest leaving broken hook behind
149     test_begin_subtest "post-new non-zero exit status (notmuch status) [${config}]"
150     test_expect_code 1 "notmuch new"
151
152     test_begin_subtest "post-insert hook does not affect insert status [${config}]"
153     rm -rf ${HOOK_DIR}
154     generate_message
155     create_failing_hook "post-insert" $HOOK_DIR
156     test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
157
158     test_begin_subtest "hook without executable permissions [${config}]"
159     rm -rf ${HOOK_DIR}
160     mkdir -p ${HOOK_DIR}
161     cat <<EOF >"${HOOK_DIR}/pre-new"
162     #!/bin/sh
163     echo foo
164 EOF
165     output=`notmuch new 2>&1`
166     test_expect_code 1 "notmuch new"
167
168     test_begin_subtest "hook execution failure [${config}]"
169     rm -rf ${HOOK_DIR}
170     mkdir -p ${HOOK_DIR}
171     cat <<EOF >"${HOOK_DIR}/pre-new"
172     no hashbang, execl fails
173 EOF
174     chmod +x "${HOOK_DIR}/pre-new"
175     test_expect_code 1 "notmuch new"
176
177     test_begin_subtest "post-new with write access [${config}]"
178     rm -rf ${HOOK_DIR}
179     create_write_hook "post-new" write.expected write.output $HOOK_DIR
180     NOTMUCH_NEW
181     test_expect_equal_file write.expected write.output
182
183     test_begin_subtest "pre-new with write access [${config}]"
184     rm -rf ${HOOK_DIR}
185     create_write_hook "pre-new" write.expected write.output $HOOK_DIR
186     NOTMUCH_NEW
187     test_expect_equal_file write.expected write.output
188
189     test_begin_subtest "add message in pre-new [${config}]"
190     test_subtest_known_broken
191     rm -rf ${HOOK_DIR}
192     generate_message '[subject]="add msg in pre-new"'
193     id1=$gen_msg_id
194     create_change_hook "pre-new" $gen_msg_filename $HOOK_DIR
195     generate_message '[subject]="add msg in new"'
196     NOTMUCH_NEW
197     notmuch search id:$id1 or id:$gen_msg_id | notmuch_search_sanitize > OUTPUT
198     cat <<EOF | sed s'/^[ \t]*//' > EXPECTED
199     thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; add msg in pre-new (inbox unread)
200     thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; add msg in new (inbox unread)
201 EOF
202     test_expect_equal_file EXPECTED OUTPUT
203
204     rm -rf ${HOOK_DIR}
205 done
206 test_done