]> git.notmuchmail.org Git - notmuch/blob - test/T150-tagging.sh
lib/message.cc: fix Coverity finding (use after free)
[notmuch] / test / T150-tagging.sh
1 #!/usr/bin/env bash
2 test_description='"notmuch tag"'
3 . ./test-lib.sh || exit 1
4
5 add_message '[subject]=One'
6 add_message '[subject]=Two'
7
8 test_begin_subtest "Adding tags"
9 notmuch tag +tag1 +tag2 +tag3 \*
10 output=$(notmuch search \* | notmuch_search_sanitize)
11 test_expect_equal "$output" "\
12 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (inbox tag1 tag2 tag3 unread)
13 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 tag2 tag3 unread)"
14
15 test_begin_subtest "Removing tags"
16 notmuch tag -tag1 -tag2 \*
17 output=$(notmuch search \* | notmuch_search_sanitize)
18 test_expect_equal "$output" "\
19 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (inbox tag3 unread)
20 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag3 unread)"
21
22 test_expect_code 1 "No tag operations" 'notmuch tag One'
23 test_expect_code 1 "No query" 'notmuch tag +tag2'
24
25 test_begin_subtest "Redundant tagging"
26 notmuch tag +tag1 -tag3 One
27 notmuch tag +tag1 -tag3 \*
28 output=$(notmuch search \* | notmuch_search_sanitize)
29 test_expect_equal "$output" "\
30 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (inbox tag1 unread)
31 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 unread)"
32
33 test_begin_subtest "Remove all"
34 notmuch tag --remove-all One
35 notmuch tag --remove-all +tag5 +tag6 +unread Two
36 output=$(notmuch search \* | notmuch_search_sanitize)
37 test_expect_equal "$output" "\
38 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One ()
39 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (tag5 tag6 unread)"
40
41 test_begin_subtest "Remove all with batch"
42 notmuch tag +tag1 One
43 notmuch tag --remove-all --batch <<EOF
44 -- One
45 +tag3 +tag4 +inbox -- Two
46 EOF
47 output=$(notmuch search \* | notmuch_search_sanitize)
48 test_expect_equal "$output" "\
49 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One ()
50 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag3 tag4)"
51
52 test_begin_subtest "Remove all with a no-op"
53 notmuch tag +inbox +tag1 +unread One
54 notmuch tag --remove-all +foo +inbox +tag1 -foo +unread Two
55 output=$(notmuch search \* | notmuch_search_sanitize)
56 test_expect_equal "$output" "\
57 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (inbox tag1 unread)
58 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 unread)"
59
60 test_begin_subtest "Special characters in tags"
61 notmuch tag +':" ' \*
62 notmuch tag -':" ' Two
63 output=$(notmuch search \* | notmuch_search_sanitize)
64 test_expect_equal "$output" "\
65 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (:\"  inbox tag1 unread)
66 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 unread)"
67
68 test_begin_subtest "Tagging order"
69 notmuch tag +tag4 -tag4 One
70 notmuch tag -tag4 +tag4 Two
71 output=$(notmuch search \* | notmuch_search_sanitize)
72 test_expect_equal "$output" "\
73 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (:\"  inbox tag1 unread)
74 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag1 tag4 unread)"
75
76 test_begin_subtest "--batch"
77 notmuch tag --batch <<EOF
78 # %20 is a space in tag
79 -:"%20 -tag1 +tag5 +tag6 -- One
80 +tag1 -tag1 -tag4 +tag4 -- Two
81 -tag6 One
82 +tag5 Two
83 EOF
84 output=$(notmuch search \* | notmuch_search_sanitize)
85 test_expect_equal "$output" "\
86 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (inbox tag5 unread)
87 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag4 tag5 unread)"
88
89 # generate a common input file for the next several tests.
90 cat > batch.in  <<EOF
91 # %40 is an @ in tag
92 +%40 -tag5 +tag6 -- One
93 +tag1 -tag1 -tag4 +tag4 -- Two
94 -tag5 +tag6 Two
95 EOF
96
97 cat > batch.expected <<EOF
98 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (@ inbox tag6 unread)
99 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag4 tag6 unread)
100 EOF
101
102 test_begin_subtest "--input"
103 notmuch dump --format=batch-tag > backup.tags
104 notmuch tag --input=batch.in
105 notmuch search \* | notmuch_search_sanitize > OUTPUT
106 notmuch restore --format=batch-tag < backup.tags
107 test_expect_equal_file batch.expected OUTPUT
108
109 test_begin_subtest "--batch --input"
110 notmuch dump --format=batch-tag > backup.tags
111 notmuch tag --batch --input=batch.in
112 notmuch search \* | notmuch_search_sanitize > OUTPUT
113 notmuch restore --format=batch-tag < backup.tags
114 test_expect_equal_file batch.expected OUTPUT
115
116 test_begin_subtest "--batch --input --remove-all"
117 notmuch dump --format=batch-tag > backup.tags
118 notmuch tag +foo +bar -- One
119 notmuch tag +tag7 -- Two
120 notmuch tag --batch --input=batch.in --remove-all
121 notmuch search \* | notmuch_search_sanitize > OUTPUT
122 notmuch restore --format=batch-tag < backup.tags
123 cat > batch_removeall.expected <<EOF
124 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (@ tag6)
125 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (tag6)
126 EOF
127 test_expect_equal_file batch_removeall.expected OUTPUT
128 rm batch_removeall.expected
129
130 test_begin_subtest "--batch, blank lines and comments"
131 notmuch dump | sort > EXPECTED
132 notmuch tag --batch <<EOF
133 # this line is a comment; the next has only white space
134          
135
136 # the previous line is empty
137 EOF
138 notmuch dump | sort > OUTPUT
139 test_expect_equal_file EXPECTED OUTPUT
140
141 test_begin_subtest '--batch: checking error messages'
142 notmuch dump --format=batch-tag > BACKUP
143 notmuch tag --batch <<EOF 2>OUTPUT
144 # the next line has a space
145  
146 # this line has no tag operations, but this is permitted in batch format.
147 a
148 +0
149 +a +b
150 # trailing whitespace
151 +a +b 
152 +c +d --
153 # this is a harmless comment, do not yell about it.
154
155 # the previous line was blank; also no yelling please
156 +%zz -- id:whatever
157 # the next non-comment line should report an an empty tag error for
158 # batch tagging, but not for restore
159 + +e -- id:foo
160 +- -- id:foo
161 EOF
162
163 cat <<EOF > EXPECTED
164 Warning: no query string [+0]
165 Warning: no query string [+a +b]
166 Warning: missing query string [+a +b ]
167 Warning: no query string after -- [+c +d --]
168 Warning: hex decoding of tag %zz failed [+%zz -- id:whatever]
169 Warning: empty tag forbidden [+ +e -- id:foo]
170 Warning: tag starting with '-' forbidden [+- -- id:foo]
171 EOF
172
173 notmuch restore --format=batch-tag < BACKUP
174 test_expect_equal_file EXPECTED OUTPUT
175
176 test_begin_subtest '--batch: tags with quotes'
177 notmuch dump --format=batch-tag > BACKUP
178
179 notmuch tag --batch <<EOF
180 +%22%27%22%27%22%22%27%27 -- One
181 -%22%27%22%27%22%22%27%27 -- One
182 +%22%27%22%22%22%27 -- One
183 +%22%27%22%27%22%22%27%27 -- Two
184 EOF
185
186 cat <<EOF > EXPECTED
187 +%22%27%22%22%22%27 +inbox +tag5 +unread -- id:msg-001@notmuch-test-suite
188 +%22%27%22%27%22%22%27%27 +inbox +tag4 +tag5 +unread -- id:msg-002@notmuch-test-suite
189 EOF
190
191 NOTMUCH_DUMP_TAGS > OUTPUT
192 notmuch restore --format=batch-tag < BACKUP
193 test_expect_equal_file EXPECTED OUTPUT
194
195 test_begin_subtest '--batch: tags with punctuation and space'
196 notmuch dump --format=batch-tag > BACKUP
197
198 notmuch tag --batch <<EOF
199 +%21@%23%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e -- One
200 -%21@%23%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e -- One
201 +%21@%23%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%20%60%7e -- Two
202 -%21@%23%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%20%60%7e -- Two
203 +%21@%23%20%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e -- One
204 +%21@%23%20%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e -- Two
205 EOF
206
207 cat <<EOF > EXPECTED
208 +%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
209 +%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
210 EOF
211
212 NOTMUCH_DUMP_TAGS > OUTPUT
213 notmuch restore --format=batch-tag < BACKUP
214 test_expect_equal_file EXPECTED OUTPUT
215
216 test_begin_subtest '--batch: unicode tags'
217 notmuch dump --format=batch-tag > BACKUP
218
219 notmuch tag --batch <<EOF
220 +%2a@%7d%cf%b5%f4%85%80%adO3%da%a7 -- One
221 +=%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
222 +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
223 +R -- One
224 +%da%88=f%cc%b9I%ce%af%7b%c9%97%e3%b9%8bH%cb%92X%d2%8c6 -- One
225 +%dc%9crh%d2%86B%e5%97%a2%22t%ed%99%82d -- One
226 +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
227 +P%c4%98%2f -- One
228 +%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
229 +%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
230 +%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
231 EOF
232
233 cat <<EOF > EXPECTED
234 +%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
235 +%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
236 EOF
237
238 NOTMUCH_DUMP_TAGS > OUTPUT
239 notmuch restore --format=batch-tag < BACKUP
240 test_expect_equal_file EXPECTED OUTPUT
241
242 test_begin_subtest "--batch: only space and % needs to be encoded."
243 notmuch dump --format=batch-tag > BACKUP
244
245 notmuch tag --batch <<EOF
246 +winner *
247 +foo::bar%25 -- (One and Two) or (One and tag:winner)
248 +found::it -- tag:foo::bar%
249 # ignore this line and the next
250
251 +space%20in%20tags -- Two
252 # add tag '(tags)', among other stunts.
253 +crazy{ +(tags) +&are +#possible\ -- tag:"space in tags"
254 +match*crazy -- tag:crazy{
255 +some_tag -- id:"this is ""nauty)"""
256 EOF
257
258 cat <<EOF > EXPECTED
259 +%23possible%5c +%26are +%28tags%29 +crazy%7b +inbox +match%2acrazy +space%20in%20tags +tag4 +tag5 +unread +winner -- id:msg-002@notmuch-test-suite
260 +foo%3a%3abar%25 +found%3a%3ait +inbox +tag5 +unread +winner -- id:msg-001@notmuch-test-suite
261 EOF
262
263 NOTMUCH_DUMP_TAGS > OUTPUT
264 notmuch restore --format=batch-tag < BACKUP
265 test_expect_equal_file EXPECTED OUTPUT
266
267 test_begin_subtest '--batch: unicode message-ids'
268
269 ${TEST_DIRECTORY}/random-corpus --config-path=${NOTMUCH_CONFIG} \
270      --num-messages=100
271
272 notmuch dump --format=batch-tag | sed 's/^.* -- /+common_tag -- /' | \
273     sort > EXPECTED
274
275 notmuch dump --format=batch-tag | sed 's/^.* -- /  -- /' > INTERMEDIATE_STEP
276 notmuch restore --format=batch-tag < INTERMEDIATE_STEP
277
278 notmuch tag --batch < EXPECTED
279
280 notmuch dump --format=batch-tag| \
281     sort > OUTPUT
282
283 test_expect_equal_file EXPECTED OUTPUT
284
285 test_expect_code 1 "Empty tag names" 'notmuch tag + One'
286
287 test_expect_code 1 "Tag name beginning with -" 'notmuch tag +- One'
288
289 test_begin_subtest "Xapian exception: read only files"
290 chmod u-w  ${MAIL_DIR}/.notmuch/xapian/*.${db_ending}
291 output=$(notmuch tag +something '*' 2>&1 | sed 's/: .*$//' )
292 chmod u+w  ${MAIL_DIR}/.notmuch/xapian/*.${db_ending}
293 test_expect_equal "$output" "A Xapian exception occurred opening database"
294
295 test_done