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