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