Added test-pick.
[mmh] / test / pick / test-pick
1 #!/bin/sh
2 ######################################################
3 #
4 # Test pick
5 #
6 ######################################################
7
8 set -e
9
10 if test -z "${MH_OBJ_DIR}"; then
11     srcdir=`dirname $0`/../..
12     MH_OBJ_DIR=`cd $srcdir && pwd`; export MH_OBJ_DIR
13 fi
14
15 . "$MH_OBJ_DIR/test/common.sh"
16
17 setup_test
18
19 expected=$MH_TEST_DIR/$$.expected
20 actual=$MH_TEST_DIR/$$.actual
21
22 # Add an 11th message.
23 sed \
24   -e 's/10/11/g' \
25   -e 's/^To:\(.*\)/To:\1\nCc: <another@example.com>/' \
26   -e 's/^\(Date:.*\)2006\( 00:00:\)00/\12008\200\nDelivery-Date: \12009\241/' \
27   $MH_TEST_DIR/Mail/inbox/10 >$MH_TEST_DIR/Mail/inbox/11
28
29 # Test message specification.
30 run_test 'pick first last' '1
31 11'
32
33 # Test -and.
34 run_test 'pick -subject message.3 -and -from Test3' '3'
35 run_test 'pick -subject message.3 -and -from Test4' \
36          'pick: no messages match specification
37 0'
38
39 # Test -or.
40 run_test 'pick -subject message.3 -or -from Test3' '3'
41 run_test 'pick -subject message.3 -or -from Test4' '3
42 4'
43
44 # Test -not.
45 run_test 'pick -not -subject message.8 last:5' '7
46 9
47 10
48 11'
49
50 # Test -lbrace, -rbrace.
51 run_test 'pick -subject message.12 -and -subject message.3 -or -from Test4' '4'
52 run_test 'pick -subject message.12 -and -lbrace -subject message.3 -rbrace' \
53          'pick: no messages match specification
54 0'
55
56 # -and takes precedence over -or.
57 run_test 'pick -subject message.12 -or -subject message.3 -and -from Test3' '3'
58 run_test 'pick -subject message.12 -or -subject message.3 -and -from Test4' \
59          'pick: no messages match specification
60 0'
61
62 # -not takes precedence over -and.
63 run_test 'pick -not -subject message.12 -and -subject message.3' '3'
64 run_test 'pick -not -lbrace -subject NoMatch -and -subject message.3 -rbrace' \
65          '1
66 2
67 3
68 4
69 5
70 6
71 7
72 8
73 9
74 10
75 11'
76
77 # Test MHPDEBUG.
78 MHPDEBUG=1 pick -not -lbrace -subject NoMatch -and -subject message.3 -rbrace \
79  >/dev/null 2>$actual
80 cat >$expected <<EOF
81 NOT
82 | AND
83 | | PATTERN(header) ^subject[   ]*:.*NoMatch
84 | | PATTERN(header) ^subject[   ]*:.*message.3
85 EOF
86 check $expected $actual
87
88
89 # Produce 0 if no messages match and standard output is not a tty.
90 # Note that there is an error message on stderr, but it's redirected
91 # to /dev/null here.
92 echo `pick -subject message.3 -and -from Test4 >$actual 2>/dev/null` >/dev/null
93 cat >$expected <<EOF
94 0
95 EOF
96 check $expected $actual
97
98 # Produce no standard output if no messages match and standard output
99 # is a tty.  To test that, move stderr to stdout.
100 echo `pick -subject message.3 -and -from Test4 >&2-` >$actual
101 cat >$expected <<EOF
102
103 EOF
104 check $expected $actual
105
106 # Also, check that the exit status is 1.
107 set +e
108 pick -subject message.3 -and -from Test4 2>/dev/null
109 run_test "echo $?" '1'
110 set -e
111
112 # Test -cc.
113 run_test 'pick -cc another' '11'
114
115 # Test -date.
116 run_test 'pick -date 29.Sep.2008' '11'
117
118 # Test -from.
119 run_test 'pick -from test7' '7'
120
121 # Test -search.
122 run_test 'pick -search This.is.message.number.[456]' '4
123 5
124 6'
125
126 # Test -subject.
127 run_test 'pick -subject message.2' '2'
128
129 # Test -to.
130 run_test 'pick -to some -nolist' '11 hits'
131 run_test 'pick -to user@example.com -nolist' '11 hits'
132
133 # Test -after.
134 pick -after '28 Sep 2008' >$actual 2>&1
135 cat >$expected <<EOF
136 11
137 EOF
138 check $expected $actual
139 # Invert exit status so execution doesn't terminate due to -e.
140 ! pick -after '29 Sep 2008' >$actual 2>/dev/null
141 cat >$expected <<EOF
142 0
143 EOF
144 check $expected $actual
145
146 # Test -before.  While -after doesn't include the specified date, -before does.
147 pick -before '29 Sep 2008' last:3 >$actual 2>&1
148 cat >$expected <<EOF
149 9
150 10
151 11
152 EOF
153 check $expected $actual
154 pick -before '28 Sep 2008' last:3 >$actual 2>&1
155 cat >$expected <<EOF
156 9
157 10
158 EOF
159 check $expected $actual
160 # Invert exit status so execution doesn't terminate due to -e.
161 ! pick -before '28 Sep 2006' last:3 >$actual 2>/dev/null
162 cat >$expected <<EOF
163 0
164 EOF
165 check $expected $actual
166
167 # Test -datefield.
168 # Invert exit status so execution doesn't terminate due to -e.
169 ! pick -datefield date -after '29 Sep 2008 00:00:00' >$actual 2>/dev/null
170 cat >$expected <<EOF
171 0
172 EOF
173 check $expected $actual
174 pick -datefield delivery-date -after '29 Sep 2008 00:00:00' >$actual 2>&1
175 cat >$expected <<EOF
176 11
177 EOF
178 check $expected $actual
179
180 # Test sequence creation.
181 run_test 'pick 2 4 6 8 10 -sequence even' '5 hits'
182 run_test 'mark -s even -list' 'even: 2 4 6 8 10'
183
184 # Test private sequence creation.
185 # Set current message for following tests.
186 folder +inbox 1 > /dev/null
187 run_test 'pick -date 29.Sep.2008 -sequence privateseq -nopublic' '1 hit'
188 run_test 'mark -list' 'cur: 1
189 even: 2 4 6 8 10
190 privateseq (private): 11'
191
192 # Test add to private sequence.
193 run_test 'pick first -sequence privateseq -nozero -nopublic' '1 hit'
194 run_test 'mark -list' 'cur: 1
195 even: 2 4 6 8 10
196 privateseq (private): 1 11'
197
198 # Test sequence creation, with -list.
199 run_test 'pick 5 7 -sequence odd -list' '5
200 7'
201 run_test 'mark -s odd -list' 'odd: 5 7'
202
203 # Test sequence creation, with default of -zero.
204 run_test 'pick 1 3 -sequence odd' '2 hits'
205 run_test 'mark -s odd -list' 'odd: 1 3'
206
207 # Test sequence creation, with -nozero.
208 run_test 'pick  5 7 9 -sequence odd -nozero' '3 hits'
209 run_test 'mark -s odd -list' 'odd: 1 3 5 7 9'
210
211 # Test sequence creation, with explicit folder, -public, and -zero.
212 run_test 'pick +inbox 5 7 9 11 -public -sequence odd' '4 hits'
213 run_test 'mark -s odd -list' 'odd: 5 7 9 11'
214
215 # Test -nolist.
216 run_test 'pick all -nolist' '11 hits'
217
218
219 exit $failed