]> git.notmuchmail.org Git - notmuch/blob - test/T050-new.sh
test: add tests for notmuch new --full-scan
[notmuch] / test / T050-new.sh
1 #!/usr/bin/env bash
2 test_description='"notmuch new" in several variations'
3 . $(dirname "$0")/test-lib.sh || exit 1
4
5 test_begin_subtest "No new messages"
6 output=$(NOTMUCH_NEW --debug)
7 test_expect_equal "$output" "No new mail."
8
9
10 test_begin_subtest "Single new message"
11 generate_message
12 output=$(NOTMUCH_NEW --debug)
13 test_expect_equal "$output" "Added 1 new message to the database."
14
15 test_begin_subtest "Single message (full-scan)"
16 generate_message
17 output=$(NOTMUCH_NEW --debug --full-scan 2>&1)
18 test_expect_equal "$output" "Added 1 new message to the database."
19
20 test_begin_subtest "Multiple new messages"
21 generate_message
22 generate_message
23 output=$(NOTMUCH_NEW --debug)
24 test_expect_equal "$output" "Added 2 new messages to the database."
25
26 test_begin_subtest "Multiple new messages (full-scan)"
27 generate_message
28 generate_message
29 output=$(NOTMUCH_NEW --debug --full-scan 2>&1)
30 test_expect_equal "$output" "Added 2 new messages to the database."
31
32 test_begin_subtest "No new messages (non-empty DB)"
33 output=$(NOTMUCH_NEW --debug)
34 test_expect_equal "$output" "No new mail."
35
36 test_begin_subtest "No new messages (full-scan)"
37 output=$(NOTMUCH_NEW --debug --full-scan 2>&1)
38 test_expect_equal "$output" "No new mail."
39
40 test_begin_subtest "New directories"
41 rm -rf "${MAIL_DIR}"/* "${MAIL_DIR}"/.notmuch
42 mkdir "${MAIL_DIR}"/def
43 mkdir "${MAIL_DIR}"/ghi
44 generate_message [dir]=def
45
46 output=$(NOTMUCH_NEW --debug)
47 test_expect_equal "$output" "Added 1 new message to the database."
48
49
50 test_begin_subtest "Alternate inode order"
51
52 rm -rf "${MAIL_DIR}"/.notmuch
53 mv "${MAIL_DIR}"/ghi "${MAIL_DIR}"/abc
54 rm "${MAIL_DIR}"/def/*
55 generate_message [dir]=abc
56
57 output=$(NOTMUCH_NEW --debug)
58 test_expect_equal "$output" "Added 1 new message to the database."
59
60
61 test_begin_subtest "Message moved in"
62 rm -rf "${MAIL_DIR}"/* "${MAIL_DIR}"/.notmuch
63 generate_message
64 tmp_msg_filename=tmp/"$gen_msg_filename"
65 mkdir -p "$(dirname "$tmp_msg_filename")"
66 mv "$gen_msg_filename" "$tmp_msg_filename"
67 notmuch new > /dev/null
68 mv "$tmp_msg_filename" "$gen_msg_filename"
69 output=$(NOTMUCH_NEW --debug)
70 test_expect_equal "$output" "Added 1 new message to the database."
71
72
73 test_begin_subtest "Renamed message"
74
75 generate_message
76 notmuch new > /dev/null
77 mv "$gen_msg_filename" "${gen_msg_filename}"-renamed
78 output=$(NOTMUCH_NEW --debug)
79 test_expect_equal "$output" "(D) add_files, pass 2: queuing passed file ${gen_msg_filename} for deletion from database
80 No new mail. Detected 1 file rename."
81
82
83 test_begin_subtest "Deleted message"
84
85 rm "${gen_msg_filename}"-renamed
86 output=$(NOTMUCH_NEW --debug)
87 test_expect_equal "$output" "(D) add_files, pass 3: queuing leftover file ${gen_msg_filename}-renamed for deletion from database
88 No new mail. Removed 1 message."
89
90
91
92 test_begin_subtest "Renamed directory"
93
94 generate_message [dir]=dir
95 generate_message [dir]=dir
96 generate_message [dir]=dir
97
98 notmuch new > /dev/null
99
100 mv "${MAIL_DIR}"/dir "${MAIL_DIR}"/dir-renamed
101
102 output=$(NOTMUCH_NEW --debug)
103 test_expect_equal "$output" "(D) add_files, pass 2: queuing passed directory ${MAIL_DIR}/dir for deletion from database
104 No new mail. Detected 3 file renames."
105
106
107 test_begin_subtest "Deleted directory"
108 rm -rf "${MAIL_DIR}"/dir-renamed
109
110 output=$(NOTMUCH_NEW --debug)
111 test_expect_equal "$output" "(D) add_files, pass 2: queuing passed directory ${MAIL_DIR}/dir-renamed for deletion from database
112 No new mail. Removed 3 messages."
113
114
115 test_begin_subtest "New directory (at end of list)"
116
117 generate_message [dir]=zzz
118 generate_message [dir]=zzz
119 generate_message [dir]=zzz
120
121 output=$(NOTMUCH_NEW --debug)
122 test_expect_equal "$output" "Added 3 new messages to the database."
123
124
125 test_begin_subtest "Deleted directory (end of list)"
126
127 rm -rf "${MAIL_DIR}"/zzz
128
129 output=$(NOTMUCH_NEW --debug)
130 test_expect_equal "$output" "(D) add_files, pass 3: queuing leftover directory ${MAIL_DIR}/zzz for deletion from database
131 No new mail. Removed 3 messages."
132
133
134 test_begin_subtest "New symlink to directory"
135
136 rm -rf "${MAIL_DIR}"/.notmuch
137 mv "${MAIL_DIR}" "${TMP_DIRECTORY}"/actual_maildir
138
139 mkdir "${MAIL_DIR}"
140 ln -s "${TMP_DIRECTORY}"/actual_maildir "${MAIL_DIR}"/symlink
141
142 output=$(NOTMUCH_NEW --debug)
143 test_expect_equal "$output" "Added 1 new message to the database."
144
145
146 test_begin_subtest "New symlink to a file"
147 generate_message
148 external_msg_filename="${TMP_DIRECTORY}"/external/"$(basename "$gen_msg_filename")"
149 mkdir -p "$(dirname "$external_msg_filename")"
150 mv "$gen_msg_filename" "$external_msg_filename"
151 ln -s "$external_msg_filename" "$gen_msg_filename"
152 output=$(NOTMUCH_NEW --debug)
153 test_expect_equal "$output" "Added 1 new message to the database."
154
155
156 test_begin_subtest "Broken symlink aborts"
157 ln -s does-not-exist "${MAIL_DIR}/broken"
158 output=$(NOTMUCH_NEW --debug 2>&1)
159 test_expect_equal "$output" \
160 "Error reading file ${MAIL_DIR}/broken: No such file or directory
161 Note: A fatal error was encountered: Something went wrong trying to read or write a file
162 No new mail."
163 rm "${MAIL_DIR}/broken"
164
165
166 test_begin_subtest "New two-level directory"
167
168 generate_message [dir]=two/levels
169 generate_message [dir]=two/levels
170 generate_message [dir]=two/levels
171
172 output=$(NOTMUCH_NEW --debug)
173 test_expect_equal "$output" "Added 3 new messages to the database."
174
175
176 test_begin_subtest "Deleted two-level directory"
177
178 rm -rf "${MAIL_DIR}"/two
179
180 output=$(NOTMUCH_NEW --debug)
181 test_expect_equal "$output" "(D) add_files, pass 3: queuing leftover directory ${MAIL_DIR}/two for deletion from database
182 No new mail. Removed 3 messages."
183
184 test_begin_subtest "One character directory at top level"
185
186 generate_message [dir]=A
187 generate_message [dir]=A/B
188 generate_message [dir]=A/B/C
189
190 output=$(NOTMUCH_NEW --debug)
191 test_expect_equal "$output" "Added 3 new messages to the database."
192
193 test_begin_subtest "Support single-message mbox"
194 cat > "${MAIL_DIR}"/mbox_file1 <<EOF
195 From test_suite@notmuchmail.org Fri Jan  5 15:43:57 2001
196 From: Notmuch Test Suite <test_suite@notmuchmail.org>
197 To: Notmuch Test Suite <test_suite@notmuchmail.org>
198 Subject: Test mbox message 1
199
200 Body.
201 EOF
202 output=$(NOTMUCH_NEW --debug 2>&1)
203 test_expect_equal "$output" "Added 1 new message to the database."
204
205 # This test requires that notmuch new has been run at least once.
206 test_begin_subtest "Skip and report non-mail files"
207 generate_message
208 mkdir -p "${MAIL_DIR}"/.git && touch "${MAIL_DIR}"/.git/config
209 touch "${MAIL_DIR}"/ignored_file
210 touch "${MAIL_DIR}"/.ignored_hidden_file
211 cat > "${MAIL_DIR}"/mbox_file <<EOF
212 From test_suite@notmuchmail.org Fri Jan  5 15:43:57 2001
213 From: Notmuch Test Suite <test_suite@notmuchmail.org>
214 To: Notmuch Test Suite <test_suite@notmuchmail.org>
215 Subject: Test mbox message 1
216
217 Body.
218
219 From test_suite@notmuchmail.org Fri Jan  5 15:43:57 2001
220 From: Notmuch Test Suite <test_suite@notmuchmail.org>
221 To: Notmuch Test Suite <test_suite@notmuchmail.org>
222 Subject: Test mbox message 2
223
224 Body 2.
225 EOF
226 output=$(NOTMUCH_NEW --debug 2>&1)
227 test_expect_equal "$output" \
228 "Note: Ignoring non-mail file: ${MAIL_DIR}/.git/config
229 Note: Ignoring non-mail file: ${MAIL_DIR}/.ignored_hidden_file
230 Note: Ignoring non-mail file: ${MAIL_DIR}/ignored_file
231 Note: Ignoring non-mail file: ${MAIL_DIR}/mbox_file
232 Added 1 new message to the database."
233 rm "${MAIL_DIR}"/mbox_file
234
235 test_begin_subtest "Ignore files and directories specified in new.ignore"
236 generate_message
237 notmuch config set new.ignore .git ignored_file .ignored_hidden_file
238 touch "${MAIL_DIR}"/.git # change .git's mtime for notmuch new to rescan.
239 NOTMUCH_NEW --debug 2>&1 | sort > OUTPUT
240 cat <<EOF > EXPECTED
241 (D) add_files, pass 1: explicitly ignoring /home/bremner/software/upstream/notmuch/test/tmp.T050-new/mail/.git
242 (D) add_files, pass 1: explicitly ignoring /home/bremner/software/upstream/notmuch/test/tmp.T050-new/mail/.ignored_hidden_file
243 (D) add_files, pass 1: explicitly ignoring /home/bremner/software/upstream/notmuch/test/tmp.T050-new/mail/ignored_file
244 (D) add_files, pass 2: explicitly ignoring /home/bremner/software/upstream/notmuch/test/tmp.T050-new/mail/.git
245 (D) add_files, pass 2: explicitly ignoring /home/bremner/software/upstream/notmuch/test/tmp.T050-new/mail/.ignored_hidden_file
246 (D) add_files, pass 2: explicitly ignoring /home/bremner/software/upstream/notmuch/test/tmp.T050-new/mail/ignored_file
247 Added 1 new message to the database.
248 EOF
249 test_expect_equal_file EXPECTED OUTPUT
250
251 test_begin_subtest "Ignore files and directories specified in new.ignore (full-scan)"
252 generate_message
253 notmuch config set new.ignore .git ignored_file .ignored_hidden_file
254 NOTMUCH_NEW --debug --full-scan 2>&1 | sort > OUTPUT
255 # reuse EXPECTED from previous test
256 test_expect_equal_file EXPECTED OUTPUT
257
258 test_begin_subtest "Ignore files and directories specified in new.ignore (multiple occurrences)"
259 notmuch config set new.ignore .git ignored_file .ignored_hidden_file
260 notmuch new > /dev/null # ensure that files/folders will be printed in ASCII order.
261 touch "${MAIL_DIR}"/.git # change .git's mtime for notmuch new to rescan.
262 touch "${MAIL_DIR}"      # likewise for MAIL_DIR
263 mkdir -p "${MAIL_DIR}"/one/two/three/.git
264 touch "${MAIL_DIR}"/{one,one/two,one/two/three}/ignored_file
265 output=$(NOTMUCH_NEW --debug 2>&1 | sort)
266 test_expect_equal "$output" \
267 "(D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/.git
268 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/.ignored_hidden_file
269 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/ignored_file
270 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/one/ignored_file
271 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/one/two/ignored_file
272 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/one/two/three/.git
273 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/one/two/three/ignored_file
274 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/.git
275 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/.ignored_hidden_file
276 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/ignored_file
277 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/one/ignored_file
278 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/one/two/ignored_file
279 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/one/two/three/.git
280 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/one/two/three/ignored_file
281 No new mail."
282
283
284 test_begin_subtest "Don't stop for ignored broken symlinks"
285 notmuch config set new.ignore .git ignored_file .ignored_hidden_file broken_link
286 ln -s i_do_not_exist "${MAIL_DIR}"/broken_link
287 output=$(NOTMUCH_NEW 2>&1)
288 test_expect_equal "$output" "No new mail."
289
290 test_begin_subtest "Ignore files and directories specified in new.ignore (regexp)"
291 notmuch config set new.ignore ".git" "/^bro.*ink\$/" "/ignored.*file/"
292 output=$(NOTMUCH_NEW --debug 2>&1 | sort)
293 test_expect_equal "$output" \
294 "(D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/.git
295 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/.ignored_hidden_file
296 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/broken_link
297 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/ignored_file
298 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/one/ignored_file
299 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/one/two/ignored_file
300 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/one/two/three/.git
301 (D) add_files, pass 1: explicitly ignoring ${MAIL_DIR}/one/two/three/ignored_file
302 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/.git
303 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/.ignored_hidden_file
304 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/broken_link
305 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/ignored_file
306 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/one/ignored_file
307 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/one/two/ignored_file
308 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/one/two/three/.git
309 (D) add_files, pass 2: explicitly ignoring ${MAIL_DIR}/one/two/three/ignored_file
310 No new mail."
311
312 test_begin_subtest "Quiet: No new mail."
313 output=$(NOTMUCH_NEW --quiet)
314 test_expect_equal "$output" ""
315
316 test_begin_subtest "Quiet: new, removed and renamed messages."
317 # new
318 generate_message
319 # deleted
320 notmuch search --format=text0 --output=files --limit=1 '*' | xargs -0 rm
321 # moved
322 mkdir "${MAIL_DIR}"/moved_messages
323 notmuch search --format=text0 --output=files --offset=1 --limit=1 '*' | xargs -0 -I {} mv {} "${MAIL_DIR}"/moved_messages
324 output=$(NOTMUCH_NEW --quiet)
325 test_expect_equal "$output" ""
326
327 OLDCONFIG=$(notmuch config get new.tags)
328
329 test_begin_subtest "Empty tags in new.tags are forbidden"
330 notmuch config set new.tags "foo;;bar"
331 output=$(NOTMUCH_NEW --debug 2>&1)
332 test_expect_equal "$output" "Error: tag '' in new.tags: empty tag forbidden"
333
334 test_begin_subtest "Tags starting with '-' in new.tags are forbidden"
335 notmuch config set new.tags "-foo;bar"
336 output=$(NOTMUCH_NEW --debug 2>&1)
337 test_expect_equal "$output" "Error: tag '-foo' in new.tags: tag starting with '-' forbidden"
338
339 test_begin_subtest "Invalid tags set exit code"
340 test_expect_code 1 "NOTMUCH_NEW --debug 2>&1"
341
342 notmuch config set new.tags $OLDCONFIG
343
344
345 test_begin_subtest "Xapian exception: read only files"
346 chmod u-w  ${MAIL_DIR}/.notmuch/xapian/*.${db_ending}
347 output=$(NOTMUCH_NEW --debug 2>&1 | sed 's/: .*$//' )
348 chmod u+w  ${MAIL_DIR}/.notmuch/xapian/*.${db_ending}
349 test_expect_equal "$output" "A Xapian exception occurred opening database"
350
351
352 test_begin_subtest "Handle files vanishing between scandir and add_file"
353
354 # A file for scandir to find. It won't get indexed, so can be empty.
355 touch ${MAIL_DIR}/vanish
356
357 # Breakpoint to remove the file before indexing
358 cat <<EOF > notmuch-new-vanish.gdb
359 set breakpoint pending on
360 set logging file notmuch-new-vanish-gdb.log
361 set logging on
362 break notmuch_database_index_file
363 commands
364 shell rm -f ${MAIL_DIR}/vanish
365 continue
366 end
367 run
368 EOF
369
370 ${TEST_GDB} --batch-silent --return-child-result -x notmuch-new-vanish.gdb \
371     --args notmuch new 2>OUTPUT 1>/dev/null
372 echo "exit status: $?" >> OUTPUT
373
374 # Clean up the file in case gdb isn't available.
375 rm -f ${MAIL_DIR}/vanish
376
377 cat <<EOF > EXPECTED
378 Unexpected error with file ${MAIL_DIR}/vanish
379 add_file: Something went wrong trying to read or write a file
380 Error opening ${MAIL_DIR}/vanish: No such file or directory
381 exit status: 75
382 EOF
383 test_expect_equal_file EXPECTED OUTPUT
384
385 add_email_corpus broken
386 test_begin_subtest "reference loop does not crash"
387 test_expect_code 0 "notmuch show --format=json id:mid-loop-12@example.org id:mid-loop-21@example.org > OUTPUT"
388
389 test_begin_subtest "reference loop ordered by date"
390 threadid=$(notmuch search --output=threads  id:mid-loop-12@example.org)
391 notmuch show --format=mbox $threadid | grep '^Date'  > OUTPUT
392 cat <<EOF > EXPECTED
393 Date: Thu, 16 Jun 2016 22:14:41 -0400
394 Date: Fri, 17 Jun 2016 22:14:41 -0400
395 EOF
396 test_expect_equal_file EXPECTED OUTPUT
397
398 test_done