]> git.notmuchmail.org Git - notmuch/blob - test/T400-hooks.sh
emacs: Add new option notmuch-search-hide-excluded
[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 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         XDG)
67             HOOK_DIR=${HOME}/.config/notmuch/default/hooks
68             ;;
69         split)
70             dir="$TMP_DIRECTORY/database.$test_count"
71             notmuch config set database.path $dir
72             notmuch config set database.mail_root $MAIL_DIR
73             HOOK_DIR=${dir}/hooks
74             ;;
75     esac
76
77     test_begin_subtest "pre-new is run [${config}]"
78     rm -rf ${HOOK_DIR}
79     generate_message
80     create_echo_hook "pre-new" expected output $HOOK_DIR
81     notmuch new > /dev/null
82     test_expect_equal_file expected output
83
84     test_begin_subtest "post-new is run [${config}]"
85     rm -rf ${HOOK_DIR}
86     generate_message
87     create_echo_hook "post-new" expected output $HOOK_DIR
88     notmuch new > /dev/null
89     test_expect_equal_file expected output
90
91     test_begin_subtest "post-insert hook is run [${config}]"
92     rm -rf ${HOOK_DIR}
93     generate_message
94     create_echo_hook "post-insert" expected output $HOOK_DIR
95     notmuch insert < "$gen_msg_filename"
96     test_expect_equal_file expected output
97
98     test_begin_subtest "pre-new is run before post-new [${config}]"
99     rm -rf ${HOOK_DIR}
100     generate_message
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
105
106     test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
107     rm -rf ${HOOK_DIR}
108     generate_message
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"
112
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"
116
117     # depends on the previous subtests leaving 1 new message behind
118     test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
119     rm -rf ${HOOK_DIR}
120     output=$(NOTMUCH_NEW)
121     test_expect_equal "$output" "Added 1 new message to the database."
122
123     test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
124     rm -rf ${HOOK_DIR}
125     generate_message
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
132
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"
136
137     test_begin_subtest "post-insert hook does not affect insert status [${config}]"
138     rm -rf ${HOOK_DIR}
139     generate_message
140     create_failing_hook "post-insert" $HOOK_DIR
141     test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
142
143     test_begin_subtest "hook without executable permissions [${config}]"
144     rm -rf ${HOOK_DIR}
145     mkdir -p ${HOOK_DIR}
146     cat <<EOF >"${HOOK_DIR}/pre-new"
147     #!/bin/sh
148     echo foo
149 EOF
150     output=`notmuch new 2>&1`
151     test_expect_code 1 "notmuch new"
152
153     test_begin_subtest "hook execution failure [${config}]"
154     rm -rf ${HOOK_DIR}
155     mkdir -p ${HOOK_DIR}
156     cat <<EOF >"${HOOK_DIR}/pre-new"
157     no hashbang, execl fails
158 EOF
159     chmod +x "${HOOK_DIR}/pre-new"
160     test_expect_code 1 "notmuch new"
161
162     test_begin_subtest "post-new with write access [${config}]"
163     rm -rf ${HOOK_DIR}
164     create_write_hook "post-new" write.expected write.output $HOOK_DIR
165     NOTMUCH_NEW
166     test_expect_equal_file write.expected write.output
167
168     test_begin_subtest "pre-new with write access [${config}]"
169     rm -rf ${HOOK_DIR}
170     create_write_hook "pre-new" write.expected write.output $HOOK_DIR
171     NOTMUCH_NEW
172     test_expect_equal_file write.expected write.output
173
174     rm -rf ${HOOK_DIR}
175 done
176 test_done