]> git.cworth.org Git - notmuch/blob - test/T400-hooks.sh
News: add entry for unthreaded mode
[notmuch] / test / T400-hooks.sh
1 #!/usr/bin/env bash
2 test_description='hooks'
3 . $(dirname "$0")/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_begin_subtest "pre-new non-zero exit status (notmuch status)"
74 test_expect_code 1 "notmuch new"
75
76 # depends on the previous subtests leaving 1 new message behind
77 test_begin_subtest "pre-new non-zero exit status aborts new"
78 rm_hooks
79 output=$(NOTMUCH_NEW)
80 test_expect_equal "$output" "Added 1 new message to the database."
81
82 test_begin_subtest "post-new non-zero exit status (hook status)"
83 rm_hooks
84 generate_message
85 create_failing_hook "post-new"
86 NOTMUCH_NEW 2>output.stderr >output
87 cat output.stderr >> output
88 echo "Added 1 new message to the database." > expected
89 echo "Error: post-new hook failed with status 13" >> expected
90 test_expect_equal_file expected output
91
92 # depends on the previous subtest leaving broken hook behind
93 test_begin_subtest "post-new non-zero exit status (notmuch status)"
94 test_expect_code 1 "notmuch new"
95
96 test_begin_subtest "post-insert hook does not affect insert status"
97 rm_hooks
98 generate_message
99 create_failing_hook "post-insert"
100 test_expect_success "notmuch insert < \"$gen_msg_filename\" > /dev/null"
101
102 test_begin_subtest "hook without executable permissions"
103 rm_hooks
104 mkdir -p ${HOOK_DIR}
105 cat <<EOF >"${HOOK_DIR}/pre-new"
106 #!/bin/sh
107 echo foo
108 EOF
109 output=`notmuch new 2>&1`
110 test_expect_code 1 "notmuch new"
111
112 test_begin_subtest "hook execution failure"
113 rm_hooks
114 mkdir -p ${HOOK_DIR}
115 cat <<EOF >"${HOOK_DIR}/pre-new"
116 no hashbang, execl fails
117 EOF
118 chmod +x "${HOOK_DIR}/pre-new"
119 test_expect_code 1 "notmuch new"
120
121 test_done