2 test_description='"notmuch insert"'
3 . $(dirname "$0")/test-lib.sh || exit 1
5 test_require_external_prereq gdb
7 # subtests about file permissions assume that we're working with umask
11 # Create directories and database before inserting.
12 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
13 mkdir -p "$MAIL_DIR"/Drafts/{cur,new,tmp}
14 notmuch new > /dev/null
16 # We use generate_message to create the temporary message files.
17 # They happen to be in the mail directory already but that is okay
18 # since we do not call notmuch new hereafter.
22 "[subject]=\"insert-subject\"" \
23 "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" \
24 "[body]=\"insert-message\""
27 test_begin_subtest "Insert zero-length file"
28 test_expect_code 1 "notmuch insert < /dev/null"
30 # This test is a proxy for other errors that may occur while trying to
31 # add a message to the notmuch database, e.g. database locked.
32 test_begin_subtest "Insert non-message"
33 test_expect_code 1 "echo bad_message | notmuch insert"
35 test_begin_subtest "Database empty so far"
36 test_expect_equal "0" "`notmuch count --output=messages '*'`"
38 test_begin_subtest "Insert message"
40 notmuch insert < "$gen_msg_filename"
41 cur_msg_filename=$(notmuch search --output=files "subject:insert-subject")
42 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
44 test_begin_subtest "Permissions on inserted message should be 0600"
45 test_expect_equal "600" "$(stat -c %a "$cur_msg_filename")"
47 test_begin_subtest "Insert message adds default tags"
48 output=$(notmuch show --format=json "subject:insert-subject")
50 "id": "'"${gen_msg_id}"'",
54 "filename": ["'"${cur_msg_filename}"'"],
55 "timestamp": 946728000,
56 "date_relative": "2000-01-01",
57 "tags": ["inbox","unread"],
59 "Subject": "insert-subject",
60 "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
61 "To": "Notmuch Test Suite <test_suite@notmuchmail.org>",
62 "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
64 "content-type": "text/plain",
65 "content": "insert-message\n"}]},
67 test_expect_equal_json "$output" "$expected"
69 test_begin_subtest "Insert duplicate message"
70 notmuch insert +duptag -unread < "$gen_msg_filename"
71 output=$((`notmuch search --output=files "subject:insert-subject" | wc -l`))
72 test_expect_equal "$output" 2
74 test_begin_subtest "Duplicate message does not change tags"
75 output=$(notmuch search --format=json --output=tags "subject:insert-subject")
76 test_expect_equal_json "$output" '["inbox", "unread"]'
78 test_begin_subtest "Insert message, add tag"
80 notmuch insert +custom < "$gen_msg_filename"
81 output=$(notmuch search --output=messages tag:custom)
82 test_expect_equal "$output" "id:$gen_msg_id"
84 test_begin_subtest "Insert tagged world-readable message"
86 notmuch insert --world-readable +world-readable-test < "$gen_msg_filename"
87 cur_msg_filename=$(notmuch search --output=files "tag:world-readable-test")
88 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
90 test_begin_subtest "Permissions on inserted world-readable message should be 0644"
91 test_expect_equal "644" "$(stat -c %a "$cur_msg_filename")"
93 test_begin_subtest "Insert tagged world-readable message with group-only umask"
97 notmuch insert --world-readable +world-readable-umask-test < "$gen_msg_filename"
98 cur_msg_filename=$(notmuch search --output=files "tag:world-readable-umask-test")
100 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
102 test_begin_subtest "Permissions on inserted world-readable message with funny umask should be 0640"
103 test_expect_equal "640" "$(stat -c %a "$cur_msg_filename")"
105 test_begin_subtest "Insert message, add/remove tags"
107 notmuch insert +custom -unread < "$gen_msg_filename"
108 output=$(notmuch search --output=messages tag:custom NOT tag:unread)
109 test_expect_equal "$output" "id:$gen_msg_id"
111 test_begin_subtest "Insert message with default tags stays in new/"
113 notmuch insert < "$gen_msg_filename"
114 output=$(notmuch search --output=files id:$gen_msg_id)
115 dirname=$(dirname "$output")
116 test_expect_equal "$dirname" "$MAIL_DIR/new"
118 test_begin_subtest "Insert message with non-maildir synced tags stays in new/"
120 notmuch insert +custom -inbox < "$gen_msg_filename"
121 output=$(notmuch search --output=files id:$gen_msg_id)
122 dirname=$(dirname "$output")
123 test_expect_equal "$dirname" "$MAIL_DIR/new"
125 test_begin_subtest "Insert message with custom new.tags goes to cur/"
126 OLDCONFIG=$(notmuch config get new.tags)
127 notmuch config set new.tags test
129 notmuch insert < "$gen_msg_filename"
130 output=$(notmuch search --output=files id:$gen_msg_id)
131 dirname=$(dirname "$output")
132 notmuch config set new.tags $OLDCONFIG
133 test_expect_equal "$dirname" "$MAIL_DIR/cur"
135 # additional check on the previous message
136 test_begin_subtest "Insert message with custom new.tags actually gets the tags"
137 output=$(notmuch search --output=tags id:$gen_msg_id)
138 test_expect_equal "$output" "test"
140 test_begin_subtest "Insert message with maildir synced tags goes to cur/"
142 notmuch insert +flagged < "$gen_msg_filename"
143 output=$(notmuch search --output=files id:$gen_msg_id)
144 dirname=$(dirname "$output")
145 test_expect_equal "$dirname" "$MAIL_DIR/cur"
147 test_begin_subtest "Insert message with maildir sync off goes to new/"
148 OLDCONFIG=$(notmuch config get maildir.synchronize_flags)
149 notmuch config set maildir.synchronize_flags false
151 notmuch insert +flagged < "$gen_msg_filename"
152 output=$(notmuch search --output=files id:$gen_msg_id)
153 dirname=$(dirname "$output")
154 notmuch config set maildir.synchronize_flags $OLDCONFIG
155 test_expect_equal "$dirname" "$MAIL_DIR/new"
157 test_begin_subtest "Insert message into folder"
159 notmuch insert --folder=Drafts < "$gen_msg_filename"
160 output=$(notmuch search --output=files path:Drafts/new)
161 dirname=$(dirname "$output")
162 test_expect_equal "$dirname" "$MAIL_DIR/Drafts/new"
164 test_begin_subtest "Insert message into top level folder"
166 notmuch insert --folder="" < "$gen_msg_filename"
167 output=$(notmuch search --output=files id:${gen_msg_id})
168 dirname=$(dirname "$output")
169 test_expect_equal "$dirname" "$MAIL_DIR/new"
171 test_begin_subtest "Insert message into folder with trailing /"
173 notmuch insert --folder=Drafts/ < "$gen_msg_filename"
174 output=$(notmuch search --output=files id:${gen_msg_id})
175 dirname=$(dirname "$output")
176 test_expect_equal "$dirname" "$MAIL_DIR/Drafts/new"
178 test_begin_subtest "Insert message into folder, add/remove tags"
180 notmuch insert --folder=Drafts +draft -unread < "$gen_msg_filename"
181 output=$(notmuch search --output=messages path:Drafts/cur tag:draft NOT tag:unread)
182 test_expect_equal "$output" "id:$gen_msg_id"
184 test_begin_subtest "Insert message into non-existent folder"
186 test_expect_code 1 "notmuch insert --folder=nonesuch < $gen_msg_filename"
188 test_begin_subtest "Insert message, create folder"
190 notmuch insert --folder=F --create-folder +folder < "$gen_msg_filename"
191 output=$(notmuch search --output=files path:F/new tag:folder)
192 basename=$(basename "$output")
193 test_expect_equal_file "$gen_msg_filename" "$MAIL_DIR/F/new/${basename}"
195 test_begin_subtest "Insert message, create subfolder"
197 notmuch insert --folder=F/G/H/I/J --create-folder +folder < "$gen_msg_filename"
198 output=$(notmuch search --output=files path:F/G/H/I/J/new tag:folder)
199 basename=$(basename "$output")
200 test_expect_equal_file "$gen_msg_filename" "${MAIL_DIR}/F/G/H/I/J/new/${basename}"
202 test_begin_subtest "Created subfolder should have permissions 0700"
203 test_expect_equal "700" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J")"
204 test_begin_subtest "Created subfolder new/ should also have permissions 0700"
205 test_expect_equal "700" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J/new")"
207 test_begin_subtest "Insert message, create world-readable subfolder"
209 notmuch insert --folder=F/G/H/I/J/K --create-folder --world-readable +folder-world-readable < "$gen_msg_filename"
210 output=$(notmuch search --output=files path:F/G/H/I/J/K/new tag:folder-world-readable)
211 basename=$(basename "$output")
212 test_expect_equal_file "$gen_msg_filename" "${MAIL_DIR}/F/G/H/I/J/K/new/${basename}"
214 test_begin_subtest "Created world-readable subfolder should have permissions 0755"
215 test_expect_equal "755" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J/K")"
216 test_begin_subtest "Created world-readable subfolder new/ should also have permissions 0755"
217 test_expect_equal "755" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J/K/new")"
219 test_begin_subtest "Insert message, create existing subfolder"
221 notmuch insert --folder=F/G/H/I/J --create-folder +folder < "$gen_msg_filename"
222 output=$(notmuch count path:F/G/H/I/J/new tag:folder)
223 test_expect_equal "$output" "2"
225 test_begin_subtest "Insert message, create invalid subfolder"
227 test_expect_code 1 "notmuch insert --folder=../G --create-folder $gen_msg_filename"
229 OLDCONFIG=$(notmuch config get new.tags)
231 test_begin_subtest "Empty tags in new.tags are forbidden"
232 notmuch config set new.tags "foo;;bar"
234 output=$(notmuch insert $gen_msg_filename 2>&1)
235 test_expect_equal "$output" "Error: tag '' in new.tags: empty tag forbidden"
237 test_begin_subtest "Tags starting with '-' in new.tags are forbidden"
238 notmuch config set new.tags "-foo;bar"
240 output=$(notmuch insert $gen_msg_filename 2>&1)
241 test_expect_equal "$output" "Error: tag '-foo' in new.tags: tag starting with '-' forbidden"
243 test_begin_subtest "Invalid tags set exit code"
244 test_expect_code 1 "notmuch insert $gen_msg_filename 2>&1"
246 notmuch config set new.tags $OLDCONFIG
248 # DUPLICATE_MESSAGE_ID is not tested here, because it should actually pass.
250 for code in OUT_OF_MEMORY XAPIAN_EXCEPTION FILE_NOT_EMAIL \
251 READ_ONLY_DATABASE UPGRADE_REQUIRED PATH_ERROR; do
252 cat <<EOF > index-file-$code.gdb
253 set breakpoint pending on
254 set logging file index-file-$code.log
256 break notmuch_database_index_file
258 return NOTMUCH_STATUS_$code
267 for code in FILE_NOT_EMAIL READ_ONLY_DATABASE UPGRADE_REQUIRED PATH_ERROR; do
268 test_begin_subtest "EXIT_FAILURE when index_file returns $code"
270 "${TEST_GDB} --batch-silent --return-child-result \
271 -ex 'set args insert < $gen_msg_filename' \
272 -x index-file-$code.gdb notmuch"
274 test_begin_subtest "success exit with --keep when index_file returns $code"
276 "${TEST_GDB} --batch-silent --return-child-result \
277 -ex 'set args insert --keep < $gen_msg_filename' \
278 -x index-file-$code.gdb notmuch"
281 for code in OUT_OF_MEMORY XAPIAN_EXCEPTION ; do
282 test_begin_subtest "EX_TEMPFAIL when index_file returns $code"
283 test_expect_code 75 \
284 "${TEST_GDB} --batch-silent --return-child-result \
285 -ex 'set args insert < $gen_msg_filename' \
286 -x index-file-$code.gdb notmuch"
288 test_begin_subtest "success exit with --keep when index_file returns $code"
290 "${TEST_GDB} --batch-silent --return-child-result \
291 -ex 'set args insert --keep < $gen_msg_filename' \
292 -x index-file-$code.gdb notmuch"