]> git.notmuchmail.org Git - notmuch/blob - test/T240-dump-restore.sh
dump: support gzipped and atomic output
[notmuch] / test / T240-dump-restore.sh
1 #!/usr/bin/env bash
2 test_description="\"notmuch dump\" and \"notmuch restore\""
3 . ./test-lib.sh
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 # Note, we assume all messages from cworth have a message-id
84 # containing cworth.org
85
86 grep 'cworth[.]org' dump.expected > dump-cworth.expected
87
88 test_begin_subtest "dump -- from:cworth"
89 notmuch dump -- from:cworth > dump-dash-cworth.actual
90 test_expect_equal_file dump-cworth.expected dump-dash-cworth.actual
91
92 test_begin_subtest "dump --output=outfile from:cworth"
93 notmuch dump --output=dump-outfile-cworth.actual from:cworth
94 test_expect_equal_file dump-cworth.expected dump-outfile-cworth.actual
95
96 test_begin_subtest "dump --output=outfile -- from:cworth"
97 notmuch dump --output=dump-outfile-dash-inbox.actual -- from:cworth
98 test_expect_equal_file dump-cworth.expected dump-outfile-dash-inbox.actual
99
100 test_begin_subtest "Check for a safe set of message-ids"
101 notmuch search --output=messages from:cworth | sed s/^id:// > EXPECTED
102 notmuch search --output=messages from:cworth | sed s/^id:// |\
103         $TEST_DIRECTORY/hex-xcode --direction=encode > OUTPUT
104 test_expect_equal_file OUTPUT EXPECTED
105
106 test_begin_subtest "format=batch-tag, dump sanity check."
107 notmuch dump --format=sup from:cworth | cut -f1 -d' ' | \
108     sort > EXPECTED.$test_count
109 notmuch dump --format=batch-tag from:cworth | sed 's/^.*-- id://' | \
110     sort > OUTPUT.$test_count
111 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
112
113 test_begin_subtest "format=batch-tag, # round-trip"
114 notmuch dump --format=sup | sort > EXPECTED.$test_count
115 notmuch dump --format=batch-tag | notmuch restore --format=batch-tag
116 notmuch dump --format=sup | sort > OUTPUT.$test_count
117 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
118
119 test_begin_subtest "format=batch-tag, # blank lines and comments"
120 notmuch dump --format=batch-tag| sort > EXPECTED.$test_count
121 notmuch restore <<EOF
122 # this line is a comment; the next has only white space
123          
124
125 # the previous line is empty
126 EOF
127 notmuch dump --format=batch-tag | sort > OUTPUT.$test_count
128 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
129
130 test_begin_subtest "format=batch-tag, # reverse-round-trip empty tag"
131 cat <<EOF >EXPECTED.$test_count
132 + -- id:20091117232137.GA7669@griffis1.net
133 EOF
134 notmuch restore --format=batch-tag < EXPECTED.$test_count
135 notmuch dump --format=batch-tag id:20091117232137.GA7669@griffis1.net > OUTPUT.$test_count
136 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
137
138 tag1='comic_swear=$&^%$^%\\//-+$^%$'
139 enc1=$($TEST_DIRECTORY/hex-xcode --direction=encode "$tag1")
140
141 tag2=$(printf 'this\n tag\t has\n spaces')
142 enc2=$($TEST_DIRECTORY/hex-xcode --direction=encode "$tag2")
143
144 enc3='%c3%91%c3%a5%c3%b0%c3%a3%c3%a5%c3%a9-%c3%8f%c3%8a'
145 tag3=$($TEST_DIRECTORY/hex-xcode --direction=decode $enc3)
146
147 notmuch dump --format=batch-tag > BACKUP
148
149 notmuch tag +"$tag1" +"$tag2" +"$tag3" -inbox -unread "*"
150
151 # initial segment of file used for several tests below.
152 cat <<EOF > comments-and-blanks
153 # this is a comment
154
155 # next line has leading whitespace
156         
157
158 EOF
159
160 test_begin_subtest 'restoring empty file is not an error'
161 notmuch restore < /dev/null 2>OUTPUT.$test_count
162 cp /dev/null EXPECTED
163 test_expect_equal_file EXPECTED OUTPUT.$test_count
164
165 test_begin_subtest 'file of comments and blank lines is not an error'
166 notmuch restore --input=comments-and-blanks
167 ret_val=$?
168 test_expect_equal "$ret_val" "0"
169
170 cp comments-and-blanks leading-comments-blanks-batch-tag
171 echo "+some_tag -- id:yun1vjwegii.fsf@aiko.keithp.com" \
172     >> leading-comments-blanks-batch-tag
173
174 test_begin_subtest 'detect format=batch-tag with leading comments and blanks'
175 notmuch restore --input=leading-comments-blanks-batch-tag
176 notmuch search --output=tags id:yun1vjwegii.fsf@aiko.keithp.com > OUTPUT.$test_count
177 echo "some_tag" > EXPECTED
178 test_expect_equal_file EXPECTED OUTPUT.$test_count
179
180 cp comments-and-blanks leading-comments-blanks-sup
181 echo "yun1vjwegii.fsf@aiko.keithp.com (another_tag)" \
182     >> leading-comments-blanks-sup
183
184 test_begin_subtest 'detect format=sup with leading comments and blanks'
185 notmuch restore --input=leading-comments-blanks-sup
186 notmuch search --output=tags id:yun1vjwegii.fsf@aiko.keithp.com > OUTPUT.$test_count
187 echo "another_tag" > EXPECTED
188 test_expect_equal_file EXPECTED OUTPUT.$test_count
189
190 test_begin_subtest 'format=batch-tag, round trip with strange tags'
191 notmuch dump --format=batch-tag > EXPECTED.$test_count
192 notmuch dump --format=batch-tag | notmuch restore --format=batch-tag
193 notmuch dump --format=batch-tag > OUTPUT.$test_count
194 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
195
196 test_begin_subtest 'format=batch-tag, checking encoded output'
197 notmuch dump --format=batch-tag -- from:cworth |\
198          awk "{ print \"+$enc1 +$enc2 +$enc3 -- \" \$5 }" > EXPECTED.$test_count
199 notmuch dump --format=batch-tag -- from:cworth  > OUTPUT.$test_count
200 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
201
202 test_begin_subtest 'restoring sane tags'
203 notmuch restore --format=batch-tag < BACKUP
204 notmuch dump --format=batch-tag > OUTPUT.$test_count
205 test_expect_equal_file BACKUP OUTPUT.$test_count
206
207 test_begin_subtest 'format=batch-tag, restore=auto'
208 notmuch dump --format=batch-tag > EXPECTED.$test_count
209 notmuch tag -inbox -unread "*"
210 notmuch restore --format=auto < EXPECTED.$test_count
211 notmuch dump --format=batch-tag > OUTPUT.$test_count
212 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
213
214 test_begin_subtest 'format=sup, restore=auto'
215 notmuch dump --format=sup > EXPECTED.$test_count
216 notmuch tag -inbox -unread "*"
217 notmuch restore --format=auto < EXPECTED.$test_count
218 notmuch dump --format=sup > OUTPUT.$test_count
219 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
220
221 test_begin_subtest 'format=batch-tag, restore=default'
222 notmuch dump --format=batch-tag > EXPECTED.$test_count
223 notmuch tag -inbox -unread "*"
224 notmuch restore < EXPECTED.$test_count
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=sup, restore=default'
229 notmuch dump --format=sup > EXPECTED.$test_count
230 notmuch tag -inbox -unread "*"
231 notmuch restore < EXPECTED.$test_count
232 notmuch dump --format=sup > OUTPUT.$test_count
233 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
234
235 test_begin_subtest 'restore: checking error messages'
236 notmuch restore <<EOF 2>OUTPUT
237 # the next line has a space
238  
239 a
240 +0
241 +a +b
242 # trailing whitespace
243 +a +b 
244 +c +d --
245 # this is a harmless comment, do not yell about it.
246
247 # the previous line was blank; also no yelling please
248 +%zz -- id:whatever
249 +e +f id:"
250 +e +f tag:abc
251 # the next non-comment line should report an an empty tag error for
252 # batch tagging, but not for restore
253 + +e -- id:20091117232137.GA7669@griffis1.net
254 # valid id, but warning about missing message
255 +e id:missing_message_id
256 # exercise parser
257 +e -- id:some)stuff
258 +e -- id:some stuff
259 +e -- id:some"stuff
260 +e -- id:"a_message_id_with""_a_quote"
261 +e -- id:"a message id with spaces"
262 +e --  id:an_id_with_leading_and_trailing_ws \
263
264 EOF
265
266 cat <<EOF > EXPECTED
267 Warning: cannot parse query: a (skipping)
268 Warning: no query string [+0]
269 Warning: no query string [+a +b]
270 Warning: missing query string [+a +b ]
271 Warning: no query string after -- [+c +d --]
272 Warning: hex decoding of tag %zz failed [+%zz -- id:whatever]
273 Warning: cannot parse query: id:" (skipping)
274 Warning: not an id query: tag:abc (skipping)
275 Warning: cannot apply tags to missing message: missing_message_id
276 Warning: cannot parse query: id:some)stuff (skipping)
277 Warning: cannot parse query: id:some stuff (skipping)
278 Warning: cannot apply tags to missing message: some"stuff
279 Warning: cannot apply tags to missing message: a_message_id_with"_a_quote
280 Warning: cannot apply tags to missing message: a message id with spaces
281 Warning: cannot apply tags to missing message: an_id_with_leading_and_trailing_ws
282 EOF
283
284 test_expect_equal_file EXPECTED OUTPUT
285
286 test_begin_subtest 'roundtripping random message-ids and tags'
287
288     ${TEST_DIRECTORY}/random-corpus --config-path=${NOTMUCH_CONFIG} \
289                         --num-messages=100
290
291      notmuch dump --format=batch-tag| \
292          sort > EXPECTED.$test_count
293
294      notmuch tag +this_tag_is_very_unlikely_to_be_random '*'
295
296      notmuch restore --format=batch-tag < EXPECTED.$test_count
297
298      notmuch dump --format=batch-tag| \
299          sort > OUTPUT.$test_count
300
301 test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count
302
303 test_done
304
305 # Note the database is "poisoned" for sup format at this point.