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