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