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