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}"'",
53 "filename": ["'"${cur_msg_filename}"'"],
54 "timestamp": 946728000,
55 "date_relative": "2000-01-01",
56 "tags": ["inbox","unread"],
58 "Subject": "insert-subject",
59 "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
60 "To": "Notmuch Test Suite <test_suite@notmuchmail.org>",
61 "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
63 "content-type": "text/plain",
64 "content": "insert-message\n"}]},
66 test_expect_equal_json "$output" "$expected"
68 test_begin_subtest "Insert duplicate message"
69 notmuch insert +duptag -unread < "$gen_msg_filename"
70 output=$((`notmuch search --output=files "subject:insert-subject" | wc -l`))
71 test_expect_equal "$output" 2
73 test_begin_subtest "Duplicate message does not change tags"
74 output=$(notmuch search --format=json --output=tags "subject:insert-subject")
75 test_expect_equal_json "$output" '["inbox", "unread"]'
77 test_begin_subtest "Insert message, add tag"
79 notmuch insert +custom < "$gen_msg_filename"
80 output=$(notmuch search --output=messages tag:custom)
81 test_expect_equal "$output" "id:$gen_msg_id"
83 test_begin_subtest "Insert tagged world-readable message"
85 notmuch insert --world-readable +world-readable-test < "$gen_msg_filename"
86 cur_msg_filename=$(notmuch search --output=files "tag:world-readable-test")
87 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
89 test_begin_subtest "Permissions on inserted world-readable message should be 0644"
90 test_expect_equal "644" "$(stat -c %a "$cur_msg_filename")"
92 test_begin_subtest "Insert tagged world-readable message with group-only umask"
96 notmuch insert --world-readable +world-readable-umask-test < "$gen_msg_filename"
97 cur_msg_filename=$(notmuch search --output=files "tag:world-readable-umask-test")
99 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
101 test_begin_subtest "Permissions on inserted world-readable message with funny umask should be 0640"
102 test_expect_equal "640" "$(stat -c %a "$cur_msg_filename")"
104 test_begin_subtest "Insert message, add/remove tags"
106 notmuch insert +custom -unread < "$gen_msg_filename"
107 output=$(notmuch search --output=messages tag:custom NOT tag:unread)
108 test_expect_equal "$output" "id:$gen_msg_id"
110 test_begin_subtest "Insert message with default tags stays in new/"
112 notmuch insert < "$gen_msg_filename"
113 output=$(notmuch search --output=files id:$gen_msg_id)
114 dirname=$(dirname "$output")
115 test_expect_equal "$dirname" "$MAIL_DIR/new"
117 test_begin_subtest "Insert message with non-maildir synced tags stays in new/"
119 notmuch insert +custom -inbox < "$gen_msg_filename"
120 output=$(notmuch search --output=files id:$gen_msg_id)
121 dirname=$(dirname "$output")
122 test_expect_equal "$dirname" "$MAIL_DIR/new"
124 test_begin_subtest "Insert message with custom new.tags goes to cur/"
125 OLDCONFIG=$(notmuch config get new.tags)
126 notmuch config set new.tags test
128 notmuch insert < "$gen_msg_filename"
129 output=$(notmuch search --output=files id:$gen_msg_id)
130 dirname=$(dirname "$output")
131 notmuch config set new.tags $OLDCONFIG
132 test_expect_equal "$dirname" "$MAIL_DIR/cur"
134 # additional check on the previous message
135 test_begin_subtest "Insert message with custom new.tags actually gets the tags"
136 output=$(notmuch search --output=tags id:$gen_msg_id)
137 test_expect_equal "$output" "test"
139 test_begin_subtest "Insert message with maildir synced tags goes to cur/"
141 notmuch insert +flagged < "$gen_msg_filename"
142 output=$(notmuch search --output=files id:$gen_msg_id)
143 dirname=$(dirname "$output")
144 test_expect_equal "$dirname" "$MAIL_DIR/cur"
146 test_begin_subtest "Insert message with maildir sync off goes to new/"
147 OLDCONFIG=$(notmuch config get maildir.synchronize_flags)
148 notmuch config set maildir.synchronize_flags false
150 notmuch insert +flagged < "$gen_msg_filename"
151 output=$(notmuch search --output=files id:$gen_msg_id)
152 dirname=$(dirname "$output")
153 notmuch config set maildir.synchronize_flags $OLDCONFIG
154 test_expect_equal "$dirname" "$MAIL_DIR/new"
156 test_begin_subtest "Insert message into folder"
158 notmuch insert --folder=Drafts < "$gen_msg_filename"
159 output=$(notmuch search --output=files path:Drafts/new)
160 dirname=$(dirname "$output")
161 test_expect_equal "$dirname" "$MAIL_DIR/Drafts/new"
163 test_begin_subtest "Insert message into top level folder"
165 notmuch insert --folder="" < "$gen_msg_filename"
166 output=$(notmuch search --output=files id:${gen_msg_id})
167 dirname=$(dirname "$output")
168 test_expect_equal "$dirname" "$MAIL_DIR/new"
170 test_begin_subtest "Insert message into folder with trailing /"
172 notmuch insert --folder=Drafts/ < "$gen_msg_filename"
173 output=$(notmuch search --output=files id:${gen_msg_id})
174 dirname=$(dirname "$output")
175 test_expect_equal "$dirname" "$MAIL_DIR/Drafts/new"
177 test_begin_subtest "Insert message into folder, add/remove tags"
179 notmuch insert --folder=Drafts +draft -unread < "$gen_msg_filename"
180 output=$(notmuch search --output=messages path:Drafts/cur tag:draft NOT tag:unread)
181 test_expect_equal "$output" "id:$gen_msg_id"
183 test_begin_subtest "Insert message into non-existent folder"
185 test_expect_code 1 "notmuch insert --folder=nonesuch < $gen_msg_filename"
187 test_begin_subtest "Insert message, create folder"
189 notmuch insert --folder=F --create-folder +folder < "$gen_msg_filename"
190 output=$(notmuch search --output=files path:F/new tag:folder)
191 basename=$(basename "$output")
192 test_expect_equal_file "$gen_msg_filename" "$MAIL_DIR/F/new/${basename}"
194 test_begin_subtest "Insert message, create subfolder"
196 notmuch insert --folder=F/G/H/I/J --create-folder +folder < "$gen_msg_filename"
197 output=$(notmuch search --output=files path:F/G/H/I/J/new tag:folder)
198 basename=$(basename "$output")
199 test_expect_equal_file "$gen_msg_filename" "${MAIL_DIR}/F/G/H/I/J/new/${basename}"
201 test_begin_subtest "Created subfolder should have permissions 0700"
202 test_expect_equal "700" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J")"
203 test_begin_subtest "Created subfolder new/ should also have permissions 0700"
204 test_expect_equal "700" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J/new")"
206 test_begin_subtest "Insert message, create world-readable subfolder"
208 notmuch insert --folder=F/G/H/I/J/K --create-folder --world-readable +folder-world-readable < "$gen_msg_filename"
209 output=$(notmuch search --output=files path:F/G/H/I/J/K/new tag:folder-world-readable)
210 basename=$(basename "$output")
211 test_expect_equal_file "$gen_msg_filename" "${MAIL_DIR}/F/G/H/I/J/K/new/${basename}"
213 test_begin_subtest "Created world-readable subfolder should have permissions 0755"
214 test_expect_equal "755" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J/K")"
215 test_begin_subtest "Created world-readable subfolder new/ should also have permissions 0755"
216 test_expect_equal "755" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J/K/new")"
218 test_begin_subtest "Insert message, create existing subfolder"
220 notmuch insert --folder=F/G/H/I/J --create-folder +folder < "$gen_msg_filename"
221 output=$(notmuch count path:F/G/H/I/J/new tag:folder)
222 test_expect_equal "$output" "2"
224 test_begin_subtest "Insert message, create invalid subfolder"
226 test_expect_code 1 "notmuch insert --folder=../G --create-folder $gen_msg_filename"
228 OLDCONFIG=$(notmuch config get new.tags)
230 test_begin_subtest "Empty tags in new.tags are forbidden"
231 notmuch config set new.tags "foo;;bar"
233 output=$(notmuch insert $gen_msg_filename 2>&1)
234 test_expect_equal "$output" "Error: tag '' in new.tags: empty tag forbidden"
236 test_begin_subtest "Tags starting with '-' in new.tags are forbidden"
237 notmuch config set new.tags "-foo;bar"
239 output=$(notmuch insert $gen_msg_filename 2>&1)
240 test_expect_equal "$output" "Error: tag '-foo' in new.tags: tag starting with '-' forbidden"
242 test_begin_subtest "Invalid tags set exit code"
243 test_expect_code 1 "notmuch insert $gen_msg_filename 2>&1"
245 notmuch config set new.tags $OLDCONFIG
247 # DUPLICATE_MESSAGE_ID is not tested here, because it should actually pass.
249 for code in OUT_OF_MEMORY XAPIAN_EXCEPTION FILE_NOT_EMAIL \
250 READ_ONLY_DATABASE UPGRADE_REQUIRED PATH_ERROR; do
251 cat <<EOF > index-file-$code.gdb
252 set breakpoint pending on
253 set logging file index-file-$code.log
255 break notmuch_database_index_file
257 return NOTMUCH_STATUS_$code
266 for code in FILE_NOT_EMAIL READ_ONLY_DATABASE UPGRADE_REQUIRED PATH_ERROR; do
267 test_begin_subtest "EXIT_FAILURE when index_file returns $code"
269 "${TEST_GDB} --batch-silent --return-child-result \
270 -ex 'set args insert < $gen_msg_filename' \
271 -x index-file-$code.gdb notmuch"
273 test_begin_subtest "success exit with --keep when index_file returns $code"
275 "${TEST_GDB} --batch-silent --return-child-result \
276 -ex 'set args insert --keep < $gen_msg_filename' \
277 -x index-file-$code.gdb notmuch"
280 for code in OUT_OF_MEMORY XAPIAN_EXCEPTION ; do
281 test_begin_subtest "EX_TEMPFAIL when index_file returns $code"
282 test_expect_code 75 \
283 "${TEST_GDB} --batch-silent --return-child-result \
284 -ex 'set args insert < $gen_msg_filename' \
285 -x index-file-$code.gdb notmuch"
287 test_begin_subtest "success exit with --keep when index_file returns $code"
289 "${TEST_GDB} --batch-silent --return-child-result \
290 -ex 'set args insert --keep < $gen_msg_filename' \
291 -x index-file-$code.gdb notmuch"