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