]> git.notmuchmail.org Git - notmuch/blob - test/T240-dump-restore.sh
test: add known broken test for dump header
[notmuch] / test / T240-dump-restore.sh
1 #!/usr/bin/env bash
2 test_description="\"notmuch dump\" and \"notmuch restore\""
3 . ./test-lib.sh || exit 1
4
5 NOTMUCH_NEW > /dev/null
6 test_begin_subtest "dump header"
7 test_subtest_known_broken
8 cat <<EOF > EXPECTED
9 #notmuch-dump batch-tag:2 config,properties,tags
10 EOF
11 notmuch dump > OUTPUT
12 test_expect_equal_file EXPECTED OUTPUT
13 add_email_corpus
14
15 test_begin_subtest "Dumping all tags"
16 test_expect_success 'generate_message && notmuch new && notmuch dump > dump.expected'
17
18 # The use of from:cworth is rather arbitrary: it matches some of the
19 # email corpus' messages, but not all of them.
20
21 test_begin_subtest "Dumping all tags II"
22 test_expect_success \
23   'notmuch tag +ABC +DEF -- from:cworth &&
24   notmuch dump > dump-ABC_DEF.expected &&
25   ! cmp dump.expected dump-ABC_DEF.expected'
26
27 test_begin_subtest "Clearing all tags"
28 test_expect_success \
29   'sed -e "s/(\([^(]*\))$/()/" < dump.expected > clear.expected &&
30   notmuch restore --input=clear.expected &&
31   notmuch dump > clear.actual &&
32   test_cmp clear.expected clear.actual'
33
34 test_begin_subtest "Clearing all tags"
35 test_expect_success \
36   'notmuch tag +ABC +DEF -- from:cworth &&
37   notmuch restore --accumulate < dump.expected &&
38   notmuch dump > dump.actual &&
39   test_cmp dump-ABC_DEF.expected dump.actual'
40
41 test_begin_subtest "Restoring original tags"
42 test_expect_success \
43   'notmuch restore --input=dump.expected &&
44   notmuch dump > dump.actual &&
45   test_cmp dump.expected dump.actual'
46
47 test_begin_subtest "Restore with nothing to do"
48 test_expect_success \
49   'notmuch restore < dump.expected &&
50   notmuch dump > dump.actual &&
51   test_cmp dump.expected dump.actual'
52
53 test_begin_subtest "Accumulate with existing tags"
54 test_expect_success \
55   'notmuch restore --accumulate --input=dump.expected &&
56   notmuch dump > dump.actual &&
57   test_cmp dump.expected dump.actual'
58
59 test_begin_subtest "Accumulate with no tags"
60 test_expect_success \
61   'notmuch restore --accumulate < clear.expected &&
62   notmuch dump > dump.actual &&
63   test_cmp dump.expected dump.actual'
64
65 test_begin_subtest "Accumulate with new tags"
66 test_expect_success \
67   'notmuch restore --input=dump.expected &&
68   notmuch restore --accumulate --input=dump-ABC_DEF.expected &&
69   notmuch dump >  OUTPUT.$test_count &&
70   notmuch restore --input=dump.expected &&
71   test_cmp dump-ABC_DEF.expected OUTPUT.$test_count'
72
73 # notmuch restore currently only considers the first argument.
74 test_begin_subtest "Invalid restore invocation"
75 test_expect_success \
76   'test_must_fail notmuch restore --input=dump.expected another_one'
77
78 test_begin_subtest "dump --output=outfile"
79 notmuch dump --output=dump-outfile.actual
80 test_expect_equal_file dump.expected dump-outfile.actual
81
82 test_begin_subtest "dump --output=outfile --"
83 notmuch dump --output=dump-1-arg-dash.actual --
84 test_expect_equal_file dump.expected dump-1-arg-dash.actual
85
86 # gzipped output
87
88 test_begin_subtest "dump --gzip"
89 notmuch dump --gzip > dump-gzip.gz
90 gunzip dump-gzip.gz
91 test_expect_equal_file dump.expected dump-gzip
92
93 test_begin_subtest "dump --gzip --output=outfile"
94 notmuch dump --gzip --output=dump-gzip-outfile.gz
95 gunzip dump-gzip-outfile.gz
96 test_expect_equal_file dump.expected dump-gzip-outfile
97
98 test_begin_subtest "restoring gzipped stdin"
99 notmuch dump --gzip --output=backup.gz
100 notmuch tag +new_tag '*'
101 notmuch restore < backup.gz
102 notmuch dump --output=dump.actual
103 test_expect_equal_file dump.expected dump.actual
104
105 test_begin_subtest "restoring gzipped file"
106 notmuch dump --gzip --output=backup.gz
107 notmuch tag +new_tag '*'
108 notmuch restore --input=backup.gz
109 notmuch dump --output=dump.actual
110 test_expect_equal_file dump.expected dump.actual
111
112 # Note, we assume all messages from cworth have a message-id
113 # containing cworth.org
114
115 { head -1 dump.expected ; grep 'cworth[.]org' dump.expected; } > dump-cworth.expected
116
117 test_begin_subtest "dump -- from:cworth"
118 notmuch dump -- from:cworth > dump-dash-cworth.actual
119 test_expect_equal_file dump-cworth.expected dump-dash-cworth.actual
120
121 test_begin_subtest "dump --output=outfile from:cworth"
122 notmuch dump --output=dump-outfile-cworth.actual from:cworth
123 test_expect_equal_file dump-cworth.expected dump-outfile-cworth.actual
124
125 test_begin_subtest "dump --output=outfile -- from:cworth"
126 notmuch dump --output=dump-outfile-dash-inbox.actual -- from:cworth
127 test_expect_equal_file dump-cworth.expected dump-outfile-dash-inbox.actual
128
129 test_begin_subtest "Check for a safe set of message-ids"
130 notmuch search --output=messages from:cworth | sed s/^id:// > EXPECTED
131 notmuch search --output=messages from:cworth | sed s/^id:// |\
132         $TEST_DIRECTORY/hex-xcode --direction=encode > OUTPUT
133 test_expect_equal_file OUTPUT EXPECTED
134
135 test_begin_subtest "format=batch-tag, dump sanity check."
136 NOTMUCH_DUMP_TAGS --format=sup from:cworth | cut -f1 -d' ' | \
137     sort > EXPECTED.$test_count
138 NOTMUCH_DUMP_TAGS --format=batch-tag from:cworth | sed 's/^.*-- id://' | \
139     sort > OUTPUT.$test_count
140 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
141
142 test_begin_subtest "format=batch-tag, missing newline"
143 printf "+a_tag_without_newline -- id:20091117232137.GA7669@griffis1.net" > IN
144 notmuch restore --accumulate < IN
145 NOTMUCH_DUMP_TAGS id:20091117232137.GA7669@griffis1.net > OUT
146 cat <<EOF > EXPECTED
147 +a_tag_without_newline +inbox +unread -- id:20091117232137.GA7669@griffis1.net
148 EOF
149 test_expect_equal_file EXPECTED OUT
150
151 test_begin_subtest "format=batch-tag, # round-trip"
152 notmuch dump --format=sup | sort > EXPECTED.$test_count
153 notmuch dump --format=batch-tag > DUMPFILE
154 notmuch restore --format=batch-tag < DUMPFILE
155 notmuch dump --format=sup | sort > OUTPUT.$test_count
156 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
157
158 test_begin_subtest "format=batch-tag, # blank lines and comments"
159 notmuch dump --format=batch-tag| sort > EXPECTED.$test_count
160 notmuch restore <<EOF
161 # this line is a comment; the next has only white space
162          
163
164 # the previous line is empty
165 EOF
166 notmuch dump --format=batch-tag | sort > OUTPUT.$test_count
167 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
168
169 test_begin_subtest "format=batch-tag, # reverse-round-trip empty tag"
170 cat <<EOF >EXPECTED.$test_count
171 + -- id:20091117232137.GA7669@griffis1.net
172 EOF
173 notmuch restore --format=batch-tag < EXPECTED.$test_count
174 NOTMUCH_DUMP_TAGS --format=batch-tag id:20091117232137.GA7669@griffis1.net > OUTPUT.$test_count
175 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
176
177 tag1='comic_swear=$&^%$^%\\//-+$^%$'
178 enc1=$($TEST_DIRECTORY/hex-xcode --direction=encode "$tag1")
179
180 tag2=$(printf 'this\n tag\t has\n spaces')
181 enc2=$($TEST_DIRECTORY/hex-xcode --direction=encode "$tag2")
182
183 enc3='%c3%91%c3%a5%c3%b0%c3%a3%c3%a5%c3%a9-%c3%8f%c3%8a'
184 tag3=$($TEST_DIRECTORY/hex-xcode --direction=decode $enc3)
185
186 notmuch dump --format=batch-tag > BACKUP
187
188 notmuch tag +"$tag1" +"$tag2" +"$tag3" -inbox -unread "*"
189
190 # initial segment of file used for several tests below.
191 cat <<EOF > comments-and-blanks
192 # this is a comment
193
194 # next line has leading whitespace
195         
196
197 EOF
198
199 test_begin_subtest 'restoring empty file is not an error'
200 notmuch restore < /dev/null 2>OUTPUT.$test_count
201 cp /dev/null EXPECTED
202 test_expect_equal_file EXPECTED OUTPUT.$test_count
203
204 test_begin_subtest 'file of comments and blank lines is not an error'
205 notmuch restore --input=comments-and-blanks
206 ret_val=$?
207 test_expect_equal "$ret_val" "0"
208
209 cp comments-and-blanks leading-comments-blanks-batch-tag
210 echo "+some_tag -- id:yun1vjwegii.fsf@aiko.keithp.com" \
211     >> leading-comments-blanks-batch-tag
212
213 test_begin_subtest 'detect format=batch-tag with leading comments and blanks'
214 notmuch restore --input=leading-comments-blanks-batch-tag
215 notmuch search --output=tags id:yun1vjwegii.fsf@aiko.keithp.com > OUTPUT.$test_count
216 echo "some_tag" > EXPECTED
217 test_expect_equal_file EXPECTED OUTPUT.$test_count
218
219 cp comments-and-blanks leading-comments-blanks-sup
220 echo "yun1vjwegii.fsf@aiko.keithp.com (another_tag)" \
221     >> leading-comments-blanks-sup
222
223 test_begin_subtest 'detect format=sup with leading comments and blanks'
224 notmuch restore --input=leading-comments-blanks-sup
225 notmuch search --output=tags id:yun1vjwegii.fsf@aiko.keithp.com > OUTPUT.$test_count
226 echo "another_tag" > EXPECTED
227 test_expect_equal_file EXPECTED OUTPUT.$test_count
228
229 test_begin_subtest 'format=batch-tag, round trip with strange tags'
230 notmuch dump --format=batch-tag > EXPECTED.$test_count
231 notmuch dump --format=batch-tag > DUMPFILE
232 notmuch restore --format=batch-tag < DUMPFILE
233 notmuch dump --format=batch-tag > OUTPUT.$test_count
234 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
235
236 test_begin_subtest 'format=batch-tag, checking encoded output'
237 NOTMUCH_DUMP_TAGS --format=batch-tag -- from:cworth |\
238          awk "{ print \"+$enc1 +$enc2 +$enc3 -- \" \$5 }" > EXPECTED.$test_count
239 NOTMUCH_DUMP_TAGS --format=batch-tag -- from:cworth  > OUTPUT.$test_count
240 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
241
242 test_begin_subtest 'restoring sane tags'
243 notmuch restore --format=batch-tag < BACKUP
244 notmuch dump --format=batch-tag > OUTPUT.$test_count
245 test_expect_equal_file BACKUP OUTPUT.$test_count
246
247 test_begin_subtest 'format=batch-tag, restore=auto'
248 notmuch dump --format=batch-tag > EXPECTED.$test_count
249 notmuch tag -inbox -unread "*"
250 notmuch restore --format=auto < EXPECTED.$test_count
251 notmuch dump --format=batch-tag > OUTPUT.$test_count
252 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
253
254 test_begin_subtest 'format=sup, restore=auto'
255 notmuch dump --format=sup > EXPECTED.$test_count
256 notmuch tag -inbox -unread "*"
257 notmuch restore --format=auto < EXPECTED.$test_count
258 notmuch dump --format=sup > OUTPUT.$test_count
259 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
260
261 test_begin_subtest 'format=batch-tag, restore=default'
262 notmuch dump --format=batch-tag > EXPECTED.$test_count
263 notmuch tag -inbox -unread "*"
264 notmuch restore < EXPECTED.$test_count
265 notmuch dump --format=batch-tag > OUTPUT.$test_count
266 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
267
268 test_begin_subtest 'format=sup, restore=default'
269 notmuch dump --format=sup > EXPECTED.$test_count
270 notmuch tag -inbox -unread "*"
271 notmuch restore < EXPECTED.$test_count
272 notmuch dump --format=sup > OUTPUT.$test_count
273 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
274
275 test_begin_subtest 'restore: checking error messages'
276 notmuch restore <<EOF 2>OUTPUT
277 # the next line has a space
278  
279 a
280 +0
281 +a +b
282 # trailing whitespace
283 +a +b 
284 +c +d --
285 # this is a harmless comment, do not yell about it.
286
287 # the previous line was blank; also no yelling please
288 +%zz -- id:whatever
289 +e +f id:"
290 +e +f tag:abc
291 # the next non-comment line should report an an empty tag error for
292 # batch tagging, but not for restore
293 + +e -- id:20091117232137.GA7669@griffis1.net
294 # valid id, but warning about missing message
295 +e id:missing_message_id
296 # exercise parser
297 +e -- id:some)stuff
298 +e -- id:some stuff
299 +e -- id:some"stuff
300 +e -- id:"a_message_id_with""_a_quote"
301 +e -- id:"a message id with spaces"
302 +e --  id:an_id_with_leading_and_trailing_ws \
303
304 EOF
305
306 cat <<EOF > EXPECTED
307 Warning: cannot parse query: a (skipping)
308 Warning: no query string [+0]
309 Warning: no query string [+a +b]
310 Warning: missing query string [+a +b ]
311 Warning: no query string after -- [+c +d --]
312 Warning: hex decoding of tag %zz failed [+%zz -- id:whatever]
313 Warning: cannot parse query: id:" (skipping)
314 Warning: not an id query: tag:abc (skipping)
315 Warning: cannot apply tags to missing message: missing_message_id
316 Warning: cannot parse query: id:some)stuff (skipping)
317 Warning: cannot parse query: id:some stuff (skipping)
318 Warning: cannot apply tags to missing message: some"stuff
319 Warning: cannot apply tags to missing message: a_message_id_with"_a_quote
320 Warning: cannot apply tags to missing message: a message id with spaces
321 Warning: cannot apply tags to missing message: an_id_with_leading_and_trailing_ws
322 EOF
323
324 test_expect_equal_file EXPECTED OUTPUT
325
326 test_begin_subtest 'roundtripping random message-ids and tags'
327
328     ${TEST_DIRECTORY}/random-corpus --config-path=${NOTMUCH_CONFIG} \
329                         --num-messages=100
330
331      notmuch dump --format=batch-tag| \
332          sort > EXPECTED.$test_count
333
334      notmuch tag +this_tag_is_very_unlikely_to_be_random '*'
335
336      notmuch restore --format=batch-tag < EXPECTED.$test_count
337
338      notmuch dump --format=batch-tag| \
339          sort > OUTPUT.$test_count
340
341 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
342
343 test_done
344
345 # Note the database is "poisoned" for sup format at this point.