]> git.notmuchmail.org Git - notmuch/blob - test/T150-tagging.sh
lib/database: delete stemmer on destroy
[notmuch] / test / T150-tagging.sh
1 #!/usr/bin/env bash
2 test_description='"notmuch tag"'
3 . $(dirname "$0")/test-lib.sh || exit 1
4
5 test_query_syntax () {
6     # use a tag with a space to stress the query string munging code.
7     local new_tag="${RANDOM} ${RANDOM}"
8     test_begin_subtest "sexpr query: $1"
9     backup_database
10     notmuch tag --query=sexp "+${new_tag}" -- "$1"
11     notmuch dump > OUTPUT
12     restore_database
13     backup_database
14     notmuch tag "+${new_tag}" -- "$2"
15     notmuch dump > EXPECTED
16     restore_database
17     test_expect_equal_file_nonempty EXPECTED OUTPUT
18 }
19
20 add_message '[subject]=One'
21 add_message '[subject]=Two'
22
23 test_begin_subtest "Adding tags"
24 notmuch tag +tag1 +tag2 +tag3 \*
25 output=$(notmuch search \* | notmuch_search_sanitize)
26 test_expect_equal "$output" "\
27 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (inbox tag1 tag2 tag3 unread)
28 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 tag2 tag3 unread)"
29
30 test_begin_subtest "Removing tags"
31 notmuch tag -tag1 -tag2 \*
32 output=$(notmuch search \* | notmuch_search_sanitize)
33 test_expect_equal "$output" "\
34 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (inbox tag3 unread)
35 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag3 unread)"
36
37 test_begin_subtest "No tag operations"
38 test_expect_code 1 'notmuch tag One'
39
40 test_begin_subtest "No query"
41 test_expect_code 1 'notmuch tag +tag2'
42
43 test_begin_subtest "Redundant tagging"
44 notmuch tag +tag1 -tag3 One
45 notmuch tag +tag1 -tag3 \*
46 output=$(notmuch search \* | notmuch_search_sanitize)
47 test_expect_equal "$output" "\
48 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (inbox tag1 unread)
49 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 unread)"
50
51 test_begin_subtest "Remove all"
52 notmuch tag --remove-all One
53 notmuch tag --remove-all +tag5 +tag6 +unread Two
54 output=$(notmuch search \* | notmuch_search_sanitize)
55 test_expect_equal "$output" "\
56 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One ()
57 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (tag5 tag6 unread)"
58
59 test_begin_subtest "Remove all with batch"
60 notmuch tag +tag1 One
61 notmuch tag --remove-all --batch <<EOF
62 -- One
63 +tag3 +tag4 +inbox -- Two
64 EOF
65 output=$(notmuch search \* | notmuch_search_sanitize)
66 test_expect_equal "$output" "\
67 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One ()
68 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag3 tag4)"
69
70 test_begin_subtest "Remove all with a no-op"
71 notmuch tag +inbox +tag1 +unread One
72 notmuch tag --remove-all +foo +inbox +tag1 -foo +unread Two
73 output=$(notmuch search \* | notmuch_search_sanitize)
74 test_expect_equal "$output" "\
75 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (inbox tag1 unread)
76 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 unread)"
77
78 test_begin_subtest "Special characters in tags"
79 notmuch tag +':" ' \*
80 notmuch tag -':" ' Two
81 output=$(notmuch search \* | notmuch_search_sanitize)
82 test_expect_equal "$output" "\
83 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (:\"  inbox tag1 unread)
84 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 unread)"
85
86 test_begin_subtest "Tagging order"
87 notmuch tag +tag4 -tag4 One
88 notmuch tag -tag4 +tag4 Two
89 output=$(notmuch search \* | notmuch_search_sanitize)
90 test_expect_equal "$output" "\
91 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (:\"  inbox tag1 unread)
92 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 tag4 unread)"
93
94 test_begin_subtest "--batch"
95 notmuch tag --batch <<EOF
96 # %20 is a space in tag
97 -:"%20 -tag1 +tag5 +tag6 -- One
98 +tag1 -tag1 -tag4 +tag4 -- Two
99 -tag6 One
100 +tag5 Two
101 EOF
102 output=$(notmuch search \* | notmuch_search_sanitize)
103 test_expect_equal "$output" "\
104 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (inbox tag5 unread)
105 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag4 tag5 unread)"
106
107 # generate a common input file for the next several tests.
108 cat > batch.in <<EOF
109 # %40 is an @ in tag
110 +%40 -tag5 +tag6 -- One
111 +tag1 -tag1 -tag4 +tag4 -- Two
112 -tag5 +tag6 Two
113 EOF
114
115 cat > batch.expected <<EOF
116 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (@ inbox tag6 unread)
117 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag4 tag6 unread)
118 EOF
119
120 test_begin_subtest "--input"
121 notmuch dump --format=batch-tag > backup.tags
122 notmuch tag --input=batch.in
123 notmuch search \* | notmuch_search_sanitize > OUTPUT
124 notmuch restore --format=batch-tag < backup.tags
125 test_expect_equal_file batch.expected OUTPUT
126
127 test_begin_subtest "--batch --input"
128 notmuch dump --format=batch-tag > backup.tags
129 notmuch tag --batch --input=batch.in
130 notmuch search \* | notmuch_search_sanitize > OUTPUT
131 notmuch restore --format=batch-tag < backup.tags
132 test_expect_equal_file batch.expected OUTPUT
133
134 test_begin_subtest "--batch --input --remove-all"
135 notmuch dump --format=batch-tag > backup.tags
136 notmuch tag +foo +bar -- One
137 notmuch tag +tag7 -- Two
138 notmuch tag --batch --input=batch.in --remove-all
139 notmuch search \* | notmuch_search_sanitize > OUTPUT
140 notmuch restore --format=batch-tag < backup.tags
141 cat > batch_removeall.expected <<EOF
142 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (@ tag6)
143 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (tag6)
144 EOF
145 test_expect_equal_file batch_removeall.expected OUTPUT
146 rm batch_removeall.expected
147
148 test_begin_subtest "--batch, dependence on previous line"
149 notmuch dump --format=batch-tag > backup.tags
150 notmuch tag --batch<<EOF
151 +trigger -- One
152 +second_tag -- tag:trigger
153 EOF
154 NOTMUCH_DUMP_TAGS tag:second_tag > OUTPUT
155 notmuch restore --format=batch-tag < backup.tags
156 cat <<EOF >EXPECTED
157 +inbox +second_tag +tag5 +trigger +unread -- id:msg-001@notmuch-test-suite
158 EOF
159 test_expect_equal_file EXPECTED OUTPUT
160
161 test_begin_subtest "--batch, blank lines and comments"
162 notmuch dump | sort > EXPECTED
163 notmuch tag --batch <<EOF
164 # this line is a comment; the next has only white space
165          
166
167 # the previous line is empty
168 EOF
169 notmuch dump | sort > OUTPUT
170 test_expect_equal_file EXPECTED OUTPUT
171
172 test_begin_subtest '--batch: checking error messages'
173 notmuch dump --format=batch-tag > BACKUP
174 notmuch tag --batch <<EOF 2>OUTPUT
175 # the next line has a space
176  
177 # this line has no tag operations, but this is permitted in batch format.
178 a
179 +0
180 +a +b
181 # trailing whitespace
182 +a +b 
183 +c +d --
184 # this is a harmless comment, do not yell about it.
185
186 # the previous line was blank; also no yelling please
187 +%zz -- id:whatever
188 # the next non-comment line should report an an empty tag error for
189 # batch tagging, but not for restore
190 + +e -- id:foo
191 +- -- id:foo
192 EOF
193
194 cat <<EOF > EXPECTED
195 Warning: no query string [+0]
196 Warning: no query string [+a +b]
197 Warning: missing query string [+a +b ]
198 Warning: no query string after -- [+c +d --]
199 Warning: hex decoding of tag %zz failed [+%zz -- id:whatever]
200 Warning: empty tag forbidden [+ +e -- id:foo]
201 Warning: tag starting with '-' forbidden [+- -- id:foo]
202 EOF
203
204 notmuch restore --format=batch-tag < BACKUP
205 test_expect_equal_file EXPECTED OUTPUT
206
207 test_begin_subtest '--batch: tags with quotes'
208 notmuch dump --format=batch-tag > BACKUP
209
210 notmuch tag --batch <<EOF
211 +%22%27%22%27%22%22%27%27 -- One
212 -%22%27%22%27%22%22%27%27 -- One
213 +%22%27%22%22%22%27 -- One
214 +%22%27%22%27%22%22%27%27 -- Two
215 EOF
216
217 cat <<EOF > EXPECTED
218 +%22%27%22%22%22%27 +inbox +tag5 +unread -- id:msg-001@notmuch-test-suite
219 +%22%27%22%27%22%22%27%27 +inbox +tag4 +tag5 +unread -- id:msg-002@notmuch-test-suite
220 EOF
221
222 NOTMUCH_DUMP_TAGS > OUTPUT
223 notmuch restore --format=batch-tag < BACKUP
224 test_expect_equal_file EXPECTED OUTPUT
225
226 test_begin_subtest '--batch: tags with punctuation and space'
227 notmuch dump --format=batch-tag > BACKUP
228
229 notmuch tag --batch <<EOF
230 +%21@%23%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e -- One
231 -%21@%23%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e -- One
232 +%21@%23%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%20%60%7e -- Two
233 -%21@%23%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%20%60%7e -- Two
234 +%21@%23%20%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e -- One
235 +%21@%23%20%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e -- Two
236 EOF
237
238 cat <<EOF > EXPECTED
239 +%21@%23%20%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e +inbox +tag4 +tag5 +unread -- id:msg-002@notmuch-test-suite
240 +%21@%23%20%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e +inbox +tag5 +unread -- id:msg-001@notmuch-test-suite
241 EOF
242
243 NOTMUCH_DUMP_TAGS > OUTPUT
244 notmuch restore --format=batch-tag < BACKUP
245 test_expect_equal_file EXPECTED OUTPUT
246
247 test_begin_subtest '--batch: unicode tags'
248 notmuch dump --format=batch-tag > BACKUP
249
250 notmuch tag --batch <<EOF
251 +%2a@%7d%cf%b5%f4%85%80%adO3%da%a7 -- One
252 +=%e0%ac%95%c8%b3+%ef%aa%95%c8%a64w%c7%9d%c9%a2%cf%b3%d6%82%24B%c4%a9%c5%a1UX%ee%99%b0%27E7%ca%a4%d0%8b%5d -- One
253 +A%e1%a0%bc%de%8b%d5%b2V%d9%9b%f3%b5%a2%a3M%d8%a1u@%f0%a0%ac%948%7e%f0%ab%86%af%27 -- One
254 +R -- One
255 +%da%88=f%cc%b9I%ce%af%7b%c9%97%e3%b9%8bH%cb%92X%d2%8c6 -- One
256 +%dc%9crh%d2%86B%e5%97%a2%22t%ed%99%82d -- One
257 +L%df%85%ef%a1%a5m@%d3%96%c2%ab%d4%9f%ca%b8%f3%b3%a2%bf%c7%b1_u%d7%b4%c7%b1 -- One
258 +P%c4%98%2f -- One
259 +%7e%d1%8b%25%ec%a0%ae%d1%a0M%3b%e3%b6%b7%e9%a4%87%3c%db%9a%cc%a8%e1%96%9d -- One
260 +%c4%bf7%c7%ab9H%c4%99k%ea%91%bd%c3%8ck%e2%b3%8dk%c5%952V%e4%99%b2%d9%b3%e4%8b%bda%5b%24%c7%9b -- One
261 +%2a@%7d%cf%b5%f4%85%80%adO3%da%a7  +=%e0%ac%95%c8%b3+%ef%aa%95%c8%a64w%c7%9d%c9%a2%cf%b3%d6%82%24B%c4%a9%c5%a1UX%ee%99%b0%27E7%ca%a4%d0%8b%5d  +A%e1%a0%bc%de%8b%d5%b2V%d9%9b%f3%b5%a2%a3M%d8%a1u@%f0%a0%ac%948%7e%f0%ab%86%af%27  +R  +%da%88=f%cc%b9I%ce%af%7b%c9%97%e3%b9%8bH%cb%92X%d2%8c6  +%dc%9crh%d2%86B%e5%97%a2%22t%ed%99%82d  +L%df%85%ef%a1%a5m@%d3%96%c2%ab%d4%9f%ca%b8%f3%b3%a2%bf%c7%b1_u%d7%b4%c7%b1  +P%c4%98%2f  +%7e%d1%8b%25%ec%a0%ae%d1%a0M%3b%e3%b6%b7%e9%a4%87%3c%db%9a%cc%a8%e1%96%9d  +%c4%bf7%c7%ab9H%c4%99k%ea%91%bd%c3%8ck%e2%b3%8dk%c5%952V%e4%99%b2%d9%b3%e4%8b%bda%5b%24%c7%9b -- Two
262 EOF
263
264 cat <<EOF > EXPECTED
265 +%2a@%7d%cf%b5%f4%85%80%adO3%da%a7 +=%e0%ac%95%c8%b3+%ef%aa%95%c8%a64w%c7%9d%c9%a2%cf%b3%d6%82%24B%c4%a9%c5%a1UX%ee%99%b0%27E7%ca%a4%d0%8b%5d +A%e1%a0%bc%de%8b%d5%b2V%d9%9b%f3%b5%a2%a3M%d8%a1u@%f0%a0%ac%948%7e%f0%ab%86%af%27 +L%df%85%ef%a1%a5m@%d3%96%c2%ab%d4%9f%ca%b8%f3%b3%a2%bf%c7%b1_u%d7%b4%c7%b1 +P%c4%98%2f +R +inbox +tag4 +tag5 +unread +%7e%d1%8b%25%ec%a0%ae%d1%a0M%3b%e3%b6%b7%e9%a4%87%3c%db%9a%cc%a8%e1%96%9d +%c4%bf7%c7%ab9H%c4%99k%ea%91%bd%c3%8ck%e2%b3%8dk%c5%952V%e4%99%b2%d9%b3%e4%8b%bda%5b%24%c7%9b +%da%88=f%cc%b9I%ce%af%7b%c9%97%e3%b9%8bH%cb%92X%d2%8c6 +%dc%9crh%d2%86B%e5%97%a2%22t%ed%99%82d -- id:msg-002@notmuch-test-suite
266 +%2a@%7d%cf%b5%f4%85%80%adO3%da%a7 +=%e0%ac%95%c8%b3+%ef%aa%95%c8%a64w%c7%9d%c9%a2%cf%b3%d6%82%24B%c4%a9%c5%a1UX%ee%99%b0%27E7%ca%a4%d0%8b%5d +A%e1%a0%bc%de%8b%d5%b2V%d9%9b%f3%b5%a2%a3M%d8%a1u@%f0%a0%ac%948%7e%f0%ab%86%af%27 +L%df%85%ef%a1%a5m@%d3%96%c2%ab%d4%9f%ca%b8%f3%b3%a2%bf%c7%b1_u%d7%b4%c7%b1 +P%c4%98%2f +R +inbox +tag5 +unread +%7e%d1%8b%25%ec%a0%ae%d1%a0M%3b%e3%b6%b7%e9%a4%87%3c%db%9a%cc%a8%e1%96%9d +%c4%bf7%c7%ab9H%c4%99k%ea%91%bd%c3%8ck%e2%b3%8dk%c5%952V%e4%99%b2%d9%b3%e4%8b%bda%5b%24%c7%9b +%da%88=f%cc%b9I%ce%af%7b%c9%97%e3%b9%8bH%cb%92X%d2%8c6 +%dc%9crh%d2%86B%e5%97%a2%22t%ed%99%82d -- id:msg-001@notmuch-test-suite
267 EOF
268
269 NOTMUCH_DUMP_TAGS > OUTPUT
270 notmuch restore --format=batch-tag < BACKUP
271 test_expect_equal_file EXPECTED OUTPUT
272
273 test_begin_subtest "--batch: only space and % needs to be encoded."
274 notmuch dump --format=batch-tag > BACKUP
275
276 notmuch tag --batch <<EOF
277 +winner *
278 +foo::bar%25 -- (One and Two) or (One and tag:winner)
279 +found::it -- tag:foo::bar%
280 # ignore this line and the next
281
282 +space%20in%20tags -- Two
283 # add tag '(tags)', among other stunts.
284 +crazy{ +(tags) +&are +#possible\ -- tag:"space in tags"
285 +match*crazy -- tag:crazy{
286 +some_tag -- id:"this is ""nauty)"""
287 EOF
288
289 cat <<EOF > EXPECTED
290 +%23possible%5c +%26are +%28tags%29 +crazy%7b +inbox +match%2acrazy +space%20in%20tags +tag4 +tag5 +unread +winner -- id:msg-002@notmuch-test-suite
291 +foo%3a%3abar%25 +found%3a%3ait +inbox +tag5 +unread +winner -- id:msg-001@notmuch-test-suite
292 EOF
293
294 NOTMUCH_DUMP_TAGS > OUTPUT
295 notmuch restore --format=batch-tag < BACKUP
296 test_expect_equal_file EXPECTED OUTPUT
297
298 test_begin_subtest '--batch: unicode message-ids'
299
300 ${TEST_DIRECTORY}/random-corpus --config-path=${NOTMUCH_CONFIG} \
301      --num-messages=100
302
303 notmuch dump --format=batch-tag | sed 's/^.* -- /+common_tag -- /' | \
304     sort > EXPECTED
305
306 notmuch dump --format=batch-tag | sed 's/^.* -- /  -- /' > INTERMEDIATE_STEP
307 notmuch restore --format=batch-tag < INTERMEDIATE_STEP
308
309 notmuch tag --batch < EXPECTED
310
311 notmuch dump --format=batch-tag| \
312     sort > OUTPUT
313
314 test_expect_equal_file EXPECTED OUTPUT
315
316 test_begin_subtest "Empty tag names"
317 test_expect_code 1 'notmuch tag + One'
318
319 test_begin_subtest "Tag name beginning with -"
320 test_expect_code 1 'notmuch tag +- One'
321
322 test_begin_subtest "Xapian exception: read only files"
323 chmod u-w ${MAIL_DIR}/.notmuch/xapian/*.*
324 output=$(notmuch tag +something '*' 2>&1 | sed 's/: .*$//' )
325 chmod u+w ${MAIL_DIR}/.notmuch/xapian/*.*
326 test_expect_equal "$output" "A Xapian exception occurred opening database"
327
328 add_email_corpus
329
330 if [ $NOTMUCH_HAVE_SFSEXP -eq 1 ]; then
331
332     test_query_syntax '(and "wonderful" "wizard")' 'wonderful and wizard'
333     test_query_syntax '(or "php" "wizard")' 'php or wizard'
334     test_query_syntax 'wizard' 'wizard'
335     test_query_syntax 'Wizard' 'Wizard'
336     test_query_syntax '(attachment notmuch-help.patch)' 'attachment:notmuch-help.patch'
337     test_query_syntax '(mimetype text/html)' 'mimetype:text/html'
338
339     test_begin_subtest "--batch --query=sexp"
340     notmuch dump --format=batch-tag > backup.tags
341     notmuch tag --batch --query=sexp  <<EOF
342     +all -- (or One Two)
343     +none -- (and One Two)
344     EOF
345     notmuch dump > OUTPUT
346     cat <<EOF > EXPECTED
347     #notmuch-dump batch-tag:3 config,properties,tags
348     +all +inbox +tag5 +unread -- id:msg-001@notmuch-test-suite
349     +all +inbox +tag4 +tag5 +unread -- id:msg-002@notmuch-test-suite
350 EOF
351     notmuch restore --format=batch-tag < backup.tags
352     test_expect_equal_file EXPECTED OUTPUT
353
354 fi
355
356 test_done