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