test: basic: drop 'ensure all available tests are run'
[notmuch] / test / hooks
1 #!/usr/bin/env bash
2 test_description='hooks'
3 . ./test-lib.sh
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
34 test_begin_subtest "pre-new is run"
35 rm_hooks
36 generate_message
37 create_echo_hook "pre-new" expected output
38 notmuch new > /dev/null
39 test_expect_equal_file expected output
40
41 test_begin_subtest "post-new is run"
42 rm_hooks
43 generate_message
44 create_echo_hook "post-new" expected output
45 notmuch new > /dev/null
46 test_expect_equal_file expected output
47
48 test_begin_subtest "pre-new is run before post-new"
49 rm_hooks
50 generate_message
51 create_echo_hook "pre-new" pre-new.expected pre-new.output
52 create_echo_hook "post-new" post-new.expected post-new.output
53 notmuch new > /dev/null
54 test_expect_equal_file post-new.expected post-new.output
55
56 test_begin_subtest "pre-new non-zero exit status (hook status)"
57 rm_hooks
58 generate_message
59 create_failing_hook "pre-new"
60 output=`notmuch new 2>&1`
61 test_expect_equal "$output" "Error: pre-new hook failed with status 13"
62
63 # depends on the previous subtest leaving broken hook behind
64 test_expect_code 1 "pre-new non-zero exit status (notmuch status)" "notmuch new"
65
66 # depends on the previous subtests leaving 1 new message behind
67 test_begin_subtest "pre-new non-zero exit status aborts new"
68 rm_hooks
69 output=$(NOTMUCH_NEW)
70 test_expect_equal "$output" "Added 1 new message to the database."
71
72 test_begin_subtest "post-new non-zero exit status (hook status)"
73 rm_hooks
74 generate_message
75 create_failing_hook "post-new"
76 NOTMUCH_NEW 2>output.stderr >output
77 cat output.stderr >> output
78 echo "Added 1 new message to the database." > expected
79 echo "Error: post-new hook failed with status 13" >> expected
80 test_expect_equal_file expected output
81
82 # depends on the previous subtest leaving broken hook behind
83 test_expect_code 1 "post-new non-zero exit status (notmuch status)" "notmuch new"
84
85 # test_begin_subtest "hook without executable permissions"
86 rm_hooks
87 mkdir -p ${HOOK_DIR}
88 cat <<EOF >"${HOOK_DIR}/pre-new"
89 #!/bin/sh
90 echo foo
91 EOF
92 output=`notmuch new 2>&1`
93 test_expect_code 1 "hook without executable permissions" "notmuch new"
94
95 # test_begin_subtest "hook execution failure"
96 rm_hooks
97 mkdir -p ${HOOK_DIR}
98 cat <<EOF >"${HOOK_DIR}/pre-new"
99 no hashbang, execl fails
100 EOF
101 chmod +x "${HOOK_DIR}/pre-new"
102 test_expect_code 1 "hook execution failure" "notmuch new"
103
104 test_done