2 test_description='"notmuch insert"'
3 . $(dirname "$0")/test-lib.sh || exit 1
5 # subtests about file permissions assume that we're working with umask
9 # Create directories and database before inserting.
10 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
11 mkdir -p "$MAIL_DIR"/Drafts/{cur,new,tmp}
12 notmuch new > /dev/null
14 # We use generate_message to create the temporary message files.
15 # They happen to be in the mail directory already but that is okay
16 # since we do not call notmuch new hereafter.
20 "[subject]=\"insert-subject\"" \
21 "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" \
22 "[body]=\"insert-message\""
25 test_begin_subtest "Insert zero-length file"
26 test_expect_code 1 "notmuch insert < /dev/null"
28 # This test is a proxy for other errors that may occur while trying to
29 # add a message to the notmuch database, e.g. database locked.
30 test_begin_subtest "Insert non-message"
31 test_expect_code 1 "echo bad_message | notmuch insert"
33 test_begin_subtest "Database empty so far"
34 test_expect_equal "0" "`notmuch count --output=messages '*'`"
36 test_begin_subtest "Insert message"
38 notmuch insert < "$gen_msg_filename"
39 cur_msg_filename=$(notmuch search --output=files "subject:insert-subject")
40 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
42 test_begin_subtest "Permissions on inserted message should be 0600"
43 test_expect_equal "600" "$(stat -c %a "$cur_msg_filename")"
45 test_begin_subtest "Insert message adds default tags"
46 output=$(notmuch show --format=json "subject:insert-subject")
48 "id": "'"${gen_msg_id}"'",
52 "filename": ["'"${cur_msg_filename}"'"],
53 "timestamp": 946728000,
54 "date_relative": "2000-01-01",
55 "tags": ["inbox","unread"],
57 "Subject": "insert-subject",
58 "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
59 "To": "Notmuch Test Suite <test_suite@notmuchmail.org>",
60 "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
62 "content-type": "text/plain",
63 "content": "insert-message\n"}]},
65 test_expect_equal_json "$output" "$expected"
67 test_begin_subtest "Insert duplicate message"
68 notmuch insert +duptag -unread < "$gen_msg_filename"
69 output=$((`notmuch search --output=files "subject:insert-subject" | wc -l`))
70 test_expect_equal "$output" 2
72 test_begin_subtest "Duplicate message does not change tags"
73 output=$(notmuch search --format=json --output=tags "subject:insert-subject")
74 test_expect_equal_json "$output" '["inbox", "unread"]'
76 test_begin_subtest "Insert message, add tag"
78 notmuch insert +custom < "$gen_msg_filename"
79 output=$(notmuch search --output=messages tag:custom)
80 test_expect_equal "$output" "id:$gen_msg_id"
82 test_begin_subtest "Insert tagged world-readable message"
84 notmuch insert --world-readable +world-readable-test < "$gen_msg_filename"
85 cur_msg_filename=$(notmuch search --output=files "tag:world-readable-test")
86 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
88 test_begin_subtest "Permissions on inserted world-readable message should be 0644"
89 test_expect_equal "644" "$(stat -c %a "$cur_msg_filename")"
91 test_begin_subtest "Insert tagged world-readable message with group-only umask"
95 notmuch insert --world-readable +world-readable-umask-test < "$gen_msg_filename"
96 cur_msg_filename=$(notmuch search --output=files "tag:world-readable-umask-test")
98 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
100 test_begin_subtest "Permissions on inserted world-readable message with funny umask should be 0640"
101 test_expect_equal "640" "$(stat -c %a "$cur_msg_filename")"
103 test_begin_subtest "Insert message, add/remove tags"
105 notmuch insert +custom -unread < "$gen_msg_filename"
106 output=$(notmuch search --output=messages tag:custom NOT tag:unread)
107 test_expect_equal "$output" "id:$gen_msg_id"
109 test_begin_subtest "Insert message with default tags stays in new/"
111 notmuch insert < "$gen_msg_filename"
112 output=$(notmuch search --output=files id:$gen_msg_id)
113 dirname=$(dirname "$output")
114 test_expect_equal "$dirname" "$MAIL_DIR/new"
116 test_begin_subtest "Insert message with non-maildir synced tags stays in new/"
118 notmuch insert +custom -inbox < "$gen_msg_filename"
119 output=$(notmuch search --output=files id:$gen_msg_id)
120 dirname=$(dirname "$output")
121 test_expect_equal "$dirname" "$MAIL_DIR/new"
123 test_begin_subtest "Insert message with custom new.tags goes to cur/"
124 OLDCONFIG=$(notmuch config get new.tags)
125 notmuch config set new.tags test
127 notmuch insert < "$gen_msg_filename"
128 output=$(notmuch search --output=files id:$gen_msg_id)
129 dirname=$(dirname "$output")
130 notmuch config set new.tags $OLDCONFIG
131 test_expect_equal "$dirname" "$MAIL_DIR/cur"
133 # additional check on the previous message
134 test_begin_subtest "Insert message with custom new.tags actually gets the tags"
135 output=$(notmuch search --output=tags id:$gen_msg_id)
136 test_expect_equal "$output" "test"
138 test_begin_subtest "Insert message with maildir synced tags goes to cur/"
140 notmuch insert +flagged < "$gen_msg_filename"
141 output=$(notmuch search --output=files id:$gen_msg_id)
142 dirname=$(dirname "$output")
143 test_expect_equal "$dirname" "$MAIL_DIR/cur"
145 test_begin_subtest "Insert message with maildir sync off goes to new/"
146 OLDCONFIG=$(notmuch config get maildir.synchronize_flags)
147 notmuch config set maildir.synchronize_flags false
149 notmuch insert +flagged < "$gen_msg_filename"
150 output=$(notmuch search --output=files id:$gen_msg_id)
151 dirname=$(dirname "$output")
152 notmuch config set maildir.synchronize_flags $OLDCONFIG
153 test_expect_equal "$dirname" "$MAIL_DIR/new"
155 test_begin_subtest "Insert message into folder"
157 notmuch insert --folder=Drafts < "$gen_msg_filename"
158 output=$(notmuch search --output=files path:Drafts/new)
159 dirname=$(dirname "$output")
160 test_expect_equal "$dirname" "$MAIL_DIR/Drafts/new"
162 test_begin_subtest "Insert message into top level folder"
164 notmuch insert --folder="" < "$gen_msg_filename"
165 output=$(notmuch search --output=files id:${gen_msg_id})
166 dirname=$(dirname "$output")
167 test_expect_equal "$dirname" "$MAIL_DIR/new"
169 test_begin_subtest "Insert message into folder with trailing /"
171 notmuch insert --folder=Drafts/ < "$gen_msg_filename"
172 output=$(notmuch search --output=files id:${gen_msg_id})
173 dirname=$(dirname "$output")
174 test_expect_equal "$dirname" "$MAIL_DIR/Drafts/new"
176 test_begin_subtest "Insert message into folder, add/remove tags"
178 notmuch insert --folder=Drafts +draft -unread < "$gen_msg_filename"
179 output=$(notmuch search --output=messages path:Drafts/cur tag:draft NOT tag:unread)
180 test_expect_equal "$output" "id:$gen_msg_id"
182 test_begin_subtest "Insert message into non-existent folder"
184 test_expect_code 1 "notmuch insert --folder=nonesuch < $gen_msg_filename"
186 test_begin_subtest "Insert message, create folder"
188 notmuch insert --folder=F --create-folder +folder < "$gen_msg_filename"
189 output=$(notmuch search --output=files path:F/new tag:folder)
190 basename=$(basename "$output")
191 test_expect_equal_file "$gen_msg_filename" "$MAIL_DIR/F/new/${basename}"
193 test_begin_subtest "Insert message, create subfolder"
195 notmuch insert --folder=F/G/H/I/J --create-folder +folder < "$gen_msg_filename"
196 output=$(notmuch search --output=files path:F/G/H/I/J/new tag:folder)
197 basename=$(basename "$output")
198 test_expect_equal_file "$gen_msg_filename" "${MAIL_DIR}/F/G/H/I/J/new/${basename}"
200 test_begin_subtest "Created subfolder should have permissions 0700"
201 test_expect_equal "700" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J")"
202 test_begin_subtest "Created subfolder new/ should also have permissions 0700"
203 test_expect_equal "700" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J/new")"
205 test_begin_subtest "Insert message, create world-readable subfolder"
207 notmuch insert --folder=F/G/H/I/J/K --create-folder --world-readable +folder-world-readable < "$gen_msg_filename"
208 output=$(notmuch search --output=files path:F/G/H/I/J/K/new tag:folder-world-readable)
209 basename=$(basename "$output")
210 test_expect_equal_file "$gen_msg_filename" "${MAIL_DIR}/F/G/H/I/J/K/new/${basename}"
212 test_begin_subtest "Created world-readable subfolder should have permissions 0755"
213 test_expect_equal "755" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J/K")"
214 test_begin_subtest "Created world-readable subfolder new/ should also have permissions 0755"
215 test_expect_equal "755" "$(stat -c %a "${MAIL_DIR}/F/G/H/I/J/K/new")"
217 test_begin_subtest "Insert message, create existing subfolder"
219 notmuch insert --folder=F/G/H/I/J --create-folder +folder < "$gen_msg_filename"
220 output=$(notmuch count path:F/G/H/I/J/new tag:folder)
221 test_expect_equal "$output" "2"
223 test_begin_subtest "Insert message, create invalid subfolder"
225 test_expect_code 1 "notmuch insert --folder=../G --create-folder < $gen_msg_filename"
227 OLDCONFIG=$(notmuch config get new.tags)
229 test_begin_subtest "Empty tags in new.tags are forbidden"
230 notmuch config set new.tags "foo;;bar"
232 output=$(notmuch insert < $gen_msg_filename 2>&1)
233 test_expect_equal "$output" "Error: tag '' in new.tags: empty tag forbidden"
235 test_begin_subtest "Tags starting with '-' in new.tags are forbidden"
236 notmuch config set new.tags "-foo;bar"
238 output=$(notmuch insert < $gen_msg_filename 2>&1)
239 test_expect_equal "$output" "Error: tag '-foo' in new.tags: tag starting with '-' forbidden"
241 test_begin_subtest "Invalid tags set exit code"
242 test_expect_code 1 "notmuch insert < $gen_msg_filename 2>&1"
244 notmuch config set new.tags $OLDCONFIG
246 # DUPLICATE_MESSAGE_ID is not tested here, because it should actually pass.
247 # pregenerate all of the test shims
248 for code in FILE_NOT_EMAIL READ_ONLY_DATABASE UPGRADE_REQUIRED PATH_ERROR OUT_OF_MEMORY XAPIAN_EXCEPTION; do
249 make_shim shim-$code <<EOF
253 notmuch_database_index_file (notmuch_database_t *notmuch,
254 const char *filename,
255 notmuch_indexopts_t *indexopts,
256 notmuch_message_t **message_ret)
258 return NOTMUCH_STATUS_$code;
265 for code in FILE_NOT_EMAIL READ_ONLY_DATABASE UPGRADE_REQUIRED PATH_ERROR; do
266 test_begin_subtest "EXIT_FAILURE when index_file returns $code"
267 test_expect_code 1 "notmuch_with_shim shim-$code insert < \"$gen_msg_filename\""
269 test_begin_subtest "success exit with --keep when index_file returns $code"
270 test_expect_code 0 "notmuch_with_shim shim-$code insert --keep < \"$gen_msg_filename\""
273 for code in OUT_OF_MEMORY XAPIAN_EXCEPTION ; do
274 test_begin_subtest "EX_TEMPFAIL when index_file returns $code"
275 test_expect_code 75 "notmuch_with_shim shim-$code insert < \"$gen_msg_filename\""
277 test_begin_subtest "success exit with --keep when index_file returns $code"
278 test_expect_code 0 "notmuch_with_shim shim-$code insert --keep < \"$gen_msg_filename\""