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