]> git.notmuchmail.org Git - notmuch/blob - test/T400-hooks.sh
Merge tag '0.31.4'
[notmuch] / test / T400-hooks.sh
1 #!/usr/bin/env bash
2 test_description='hooks'
3 . $(dirname "$0")/test-lib.sh || exit 1
4
5 create_echo_hook () {
6     local TOKEN="${RANDOM}"
7     mkdir -p ${HOOK_DIR}
8     cat <<EOF >"${HOOK_DIR}/${1}"
9 #!/bin/sh
10 echo "${TOKEN}" > ${3}
11 EOF
12     chmod +x "${HOOK_DIR}/${1}"
13     echo "${TOKEN}" > ${2}
14 }
15
16 create_failing_hook () {
17     local HOOK_DIR=${2}
18     mkdir -p ${HOOK_DIR}
19     cat <<EOF >"${HOOK_DIR}/${1}"
20 #!/bin/sh
21 exit 13
22 EOF
23     chmod +x "${HOOK_DIR}/${1}"
24 }
25
26 # add a message to generate mail dir and database
27 add_message
28 # create maildir structure for notmuch-insert
29 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
30
31 for config in traditional profile explicit XDG; do
32     unset NOTMUCH_PROFILE
33     notmuch config set database.hook_dir
34     case $config in
35         traditional)
36             HOOK_DIR=${MAIL_DIR}/.notmuch/hooks
37             ;;
38         profile)
39             dir=${HOME}/.config/notmuch/other
40             mkdir -p ${dir}
41             HOOK_DIR=${dir}/hooks
42             cp ${NOTMUCH_CONFIG} ${dir}/config
43             export NOTMUCH_PROFILE=other
44             ;;
45         explicit)
46             HOOK_DIR=${HOME}/.notmuch-hooks
47             mkdir -p $HOOK_DIR
48             notmuch config set database.hook_dir $HOOK_DIR
49             ;;
50         XDG)
51             HOOK_DIR=${HOME}/.config/notmuch/default/hooks
52             ;;
53     esac
54
55     test_begin_subtest "pre-new is run [${config}]"
56     rm -rf ${HOOK_DIR}
57     generate_message
58     create_echo_hook "pre-new" expected output $HOOK_DIR
59     notmuch new > /dev/null
60     test_expect_equal_file expected output
61
62     test_begin_subtest "post-new is run [${config}]"
63     rm -rf ${HOOK_DIR}
64     generate_message
65     create_echo_hook "post-new" expected output $HOOK_DIR
66     notmuch new > /dev/null
67     test_expect_equal_file expected output
68
69     test_begin_subtest "post-insert hook is run [${config}]"
70     rm -rf ${HOOK_DIR}
71     generate_message
72     create_echo_hook "post-insert" expected output $HOOK_DIR
73     notmuch insert < "$gen_msg_filename"
74     test_expect_equal_file expected output
75
76     test_begin_subtest "pre-new is run before post-new [${config}]"
77     rm -rf ${HOOK_DIR}
78     generate_message
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
83
84     test_begin_subtest "pre-new non-zero exit status (hook status) [${config}]"
85     rm -rf ${HOOK_DIR}
86     generate_message
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"
90
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"
94
95     # depends on the previous subtests leaving 1 new message behind
96     test_begin_subtest "pre-new non-zero exit status aborts new [${config}]"
97     rm -rf ${HOOK_DIR}
98     output=$(NOTMUCH_NEW)
99     test_expect_equal "$output" "Added 1 new message to the database."
100
101     test_begin_subtest "post-new non-zero exit status (hook status) [${config}]"
102     rm -rf ${HOOK_DIR}
103     generate_message
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
110
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"
114
115     test_begin_subtest "post-insert hook does not affect insert status [${config}]"
116     rm -rf ${HOOK_DIR}
117     generate_message
118     create_failing_hook "post-insert" $HOOK_DIR
119     test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
120
121     test_begin_subtest "hook without executable permissions [${config}]"
122     rm -rf ${HOOK_DIR}
123     mkdir -p ${HOOK_DIR}
124     cat <<EOF >"${HOOK_DIR}/pre-new"
125     #!/bin/sh
126     echo foo
127 EOF
128     output=`notmuch new 2>&1`
129     test_expect_code 1 "notmuch new"
130
131     test_begin_subtest "hook execution failure [${config}]"
132     rm -rf ${HOOK_DIR}
133     mkdir -p ${HOOK_DIR}
134     cat <<EOF >"${HOOK_DIR}/pre-new"
135     no hashbang, execl fails
136 EOF
137     chmod +x "${HOOK_DIR}/pre-new"
138     test_expect_code 1 "notmuch new"
139
140     rm -rf ${HOOK_DIR}
141 done
142 test_done