]> git.notmuchmail.org Git - notmuch/blob - test/T400-hooks.sh
test: remove unused regexp convenience variables
[notmuch] / test / T400-hooks.sh
1 #!/usr/bin/env bash
2 test_description='hooks'
3 . ./test-lib.sh || exit 1
4
5 HOOK_DIR=${MAIL_DIR}/.notmuch/hooks
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_failing_hook () {
19     mkdir -p ${HOOK_DIR}
20     cat <<EOF >"${HOOK_DIR}/${1}"
21 #!/bin/sh
22 exit 13
23 EOF
24     chmod +x "${HOOK_DIR}/${1}"
25 }
26
27 rm_hooks () {
28     rm -rf ${HOOK_DIR}
29 }
30
31 # add a message to generate mail dir and database
32 add_message
33 # create maildir structure for notmuch-insert
34 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
35
36 test_begin_subtest "pre-new is run"
37 rm_hooks
38 generate_message
39 create_echo_hook "pre-new" expected output
40 notmuch new > /dev/null
41 test_expect_equal_file expected output
42
43 test_begin_subtest "post-new is run"
44 rm_hooks
45 generate_message
46 create_echo_hook "post-new" expected output
47 notmuch new > /dev/null
48 test_expect_equal_file expected output
49
50 test_begin_subtest "post-insert hook is run"
51 rm_hooks
52 generate_message
53 create_echo_hook "post-insert" expected output
54 notmuch insert < "$gen_msg_filename"
55 test_expect_equal_file expected output
56
57 test_begin_subtest "pre-new is run before post-new"
58 rm_hooks
59 generate_message
60 create_echo_hook "pre-new" pre-new.expected pre-new.output
61 create_echo_hook "post-new" post-new.expected post-new.output
62 notmuch new > /dev/null
63 test_expect_equal_file post-new.expected post-new.output
64
65 test_begin_subtest "pre-new non-zero exit status (hook status)"
66 rm_hooks
67 generate_message
68 create_failing_hook "pre-new"
69 output=`notmuch new 2>&1`
70 test_expect_equal "$output" "Error: pre-new hook failed with status 13"
71
72 # depends on the previous subtest leaving broken hook behind
73 test_expect_code 1 "pre-new non-zero exit status (notmuch status)" "notmuch new"
74
75 # depends on the previous subtests leaving 1 new message behind
76 test_begin_subtest "pre-new non-zero exit status aborts new"
77 rm_hooks
78 output=$(NOTMUCH_NEW)
79 test_expect_equal "$output" "Added 1 new message to the database."
80
81 test_begin_subtest "post-new non-zero exit status (hook status)"
82 rm_hooks
83 generate_message
84 create_failing_hook "post-new"
85 NOTMUCH_NEW 2>output.stderr >output
86 cat output.stderr >> output
87 echo "Added 1 new message to the database." > expected
88 echo "Error: post-new hook failed with status 13" >> expected
89 test_expect_equal_file expected output
90
91 # depends on the previous subtest leaving broken hook behind
92 test_expect_code 1 "post-new non-zero exit status (notmuch status)" "notmuch new"
93
94 rm_hooks
95 generate_message
96 create_failing_hook "post-insert"
97 test_expect_success "post-insert hook does not affect insert status" \
98     "notmuch insert < \"$gen_msg_filename\" > /dev/null"
99
100 # test_begin_subtest "hook without executable permissions"
101 rm_hooks
102 mkdir -p ${HOOK_DIR}
103 cat <<EOF >"${HOOK_DIR}/pre-new"
104 #!/bin/sh
105 echo foo
106 EOF
107 output=`notmuch new 2>&1`
108 test_expect_code 1 "hook without executable permissions" "notmuch new"
109
110 # test_begin_subtest "hook execution failure"
111 rm_hooks
112 mkdir -p ${HOOK_DIR}
113 cat <<EOF >"${HOOK_DIR}/pre-new"
114 no hashbang, execl fails
115 EOF
116 chmod +x "${HOOK_DIR}/pre-new"
117 test_expect_code 1 "hook execution failure" "notmuch new"
118
119 test_done