2 test_description='"notmuch insert"'
3 . ./test-lib.sh || exit 1
5 test_require_external_prereq gdb
7 # Create directories and database before inserting.
8 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
9 mkdir -p "$MAIL_DIR"/Drafts/{cur,new,tmp}
10 notmuch new > /dev/null
12 # We use generate_message to create the temporary message files.
13 # They happen to be in the mail directory already but that is okay
14 # since we do not call notmuch new hereafter.
18 "[subject]=\"insert-subject\"" \
19 "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" \
20 "[body]=\"insert-message\""
23 test_expect_code 1 "Insert zero-length file" \
24 "notmuch insert < /dev/null"
26 # This test is a proxy for other errors that may occur while trying to
27 # add a message to the notmuch database, e.g. database locked.
28 test_expect_code 1 "Insert non-message" \
29 "echo bad_message | notmuch insert"
31 test_begin_subtest "Database empty so far"
32 test_expect_equal "0" "`notmuch count --output=messages '*'`"
34 test_begin_subtest "Insert message"
36 notmuch insert < "$gen_msg_filename"
37 cur_msg_filename=$(notmuch search --output=files "subject:insert-subject")
38 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
40 test_begin_subtest "Insert message adds default tags"
41 output=$(notmuch show --format=json "subject:insert-subject")
43 "id": "'"${gen_msg_id}"'",
46 "filename": "'"${cur_msg_filename}"'",
47 "timestamp": 946728000,
48 "date_relative": "2000-01-01",
49 "tags": ["inbox","unread"],
51 "Subject": "insert-subject",
52 "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
53 "To": "Notmuch Test Suite <test_suite@notmuchmail.org>",
54 "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
56 "content-type": "text/plain",
57 "content": "insert-message\n"}]},
59 test_expect_equal_json "$output" "$expected"
61 test_begin_subtest "Insert duplicate message"
62 notmuch insert +duptag -unread < "$gen_msg_filename"
63 output=$((`notmuch search --output=files "subject:insert-subject" | wc -l`))
64 test_expect_equal "$output" 2
66 test_begin_subtest "Duplicate message does not change tags"
67 output=$(notmuch search --format=json --output=tags "subject:insert-subject")
68 test_expect_equal_json "$output" '["inbox", "unread"]'
70 test_begin_subtest "Insert message, add tag"
72 notmuch insert +custom < "$gen_msg_filename"
73 output=$(notmuch search --output=messages tag:custom)
74 test_expect_equal "$output" "id:$gen_msg_id"
76 test_begin_subtest "Insert message, add/remove tags"
78 notmuch insert +custom -unread < "$gen_msg_filename"
79 output=$(notmuch search --output=messages tag:custom NOT tag:unread)
80 test_expect_equal "$output" "id:$gen_msg_id"
82 test_begin_subtest "Insert message with default tags stays in new/"
84 notmuch insert < "$gen_msg_filename"
85 output=$(notmuch search --output=files id:$gen_msg_id)
86 dirname=$(dirname "$output")
87 test_expect_equal "$dirname" "$MAIL_DIR/new"
89 test_begin_subtest "Insert message with non-maildir synced tags stays in new/"
91 notmuch insert +custom -inbox < "$gen_msg_filename"
92 output=$(notmuch search --output=files id:$gen_msg_id)
93 dirname=$(dirname "$output")
94 test_expect_equal "$dirname" "$MAIL_DIR/new"
96 test_begin_subtest "Insert message with custom new.tags goes to cur/"
97 OLDCONFIG=$(notmuch config get new.tags)
98 notmuch config set new.tags test
100 notmuch insert < "$gen_msg_filename"
101 output=$(notmuch search --output=files id:$gen_msg_id)
102 dirname=$(dirname "$output")
103 notmuch config set new.tags $OLDCONFIG
104 test_expect_equal "$dirname" "$MAIL_DIR/cur"
106 # additional check on the previous message
107 test_begin_subtest "Insert message with custom new.tags actually gets the tags"
108 output=$(notmuch search --output=tags id:$gen_msg_id)
109 test_expect_equal "$output" "test"
111 test_begin_subtest "Insert message with maildir synced tags goes to cur/"
113 notmuch insert +flagged < "$gen_msg_filename"
114 output=$(notmuch search --output=files id:$gen_msg_id)
115 dirname=$(dirname "$output")
116 test_expect_equal "$dirname" "$MAIL_DIR/cur"
118 test_begin_subtest "Insert message with maildir sync off goes to new/"
119 OLDCONFIG=$(notmuch config get maildir.synchronize_flags)
120 notmuch config set maildir.synchronize_flags false
122 notmuch insert +flagged < "$gen_msg_filename"
123 output=$(notmuch search --output=files id:$gen_msg_id)
124 dirname=$(dirname "$output")
125 notmuch config set maildir.synchronize_flags $OLDCONFIG
126 test_expect_equal "$dirname" "$MAIL_DIR/new"
128 test_begin_subtest "Insert message into folder"
130 notmuch insert --folder=Drafts < "$gen_msg_filename"
131 output=$(notmuch search --output=files path:Drafts/new)
132 dirname=$(dirname "$output")
133 test_expect_equal "$dirname" "$MAIL_DIR/Drafts/new"
135 test_begin_subtest "Insert message into folder, add/remove tags"
137 notmuch insert --folder=Drafts +draft -unread < "$gen_msg_filename"
138 output=$(notmuch search --output=messages path:Drafts/cur tag:draft NOT tag:unread)
139 test_expect_equal "$output" "id:$gen_msg_id"
142 test_expect_code 1 "Insert message into non-existent folder" \
143 "notmuch insert --folder=nonesuch < $gen_msg_filename"
145 test_begin_subtest "Insert message, create folder"
147 notmuch insert --folder=F --create-folder +folder < "$gen_msg_filename"
148 output=$(notmuch search --output=files path:F/new tag:folder)
149 basename=$(basename "$output")
150 test_expect_equal_file "$gen_msg_filename" "$MAIL_DIR/F/new/${basename}"
152 test_begin_subtest "Insert message, create subfolder"
154 notmuch insert --folder=F/G/H/I/J --create-folder +folder < "$gen_msg_filename"
155 output=$(notmuch search --output=files path:F/G/H/I/J/new tag:folder)
156 basename=$(basename "$output")
157 test_expect_equal_file "$gen_msg_filename" "${MAIL_DIR}/F/G/H/I/J/new/${basename}"
159 test_begin_subtest "Insert message, create existing subfolder"
161 notmuch insert --folder=F/G/H/I/J --create-folder +folder < "$gen_msg_filename"
162 output=$(notmuch count path:F/G/H/I/J/new tag:folder)
163 test_expect_equal "$output" "2"
166 test_expect_code 1 "Insert message, create invalid subfolder" \
167 "notmuch insert --folder=../G --create-folder $gen_msg_filename"
169 OLDCONFIG=$(notmuch config get new.tags)
171 test_begin_subtest "Empty tags in new.tags are forbidden"
172 notmuch config set new.tags "foo;;bar"
174 output=$(notmuch insert $gen_msg_filename 2>&1)
175 test_expect_equal "$output" "Error: tag '' in new.tags: empty tag forbidden"
177 test_begin_subtest "Tags starting with '-' in new.tags are forbidden"
178 notmuch config set new.tags "-foo;bar"
180 output=$(notmuch insert $gen_msg_filename 2>&1)
181 test_expect_equal "$output" "Error: tag '-foo' in new.tags: tag starting with '-' forbidden"
183 test_expect_code 1 "Invalid tags set exit code" \
184 "notmuch insert $gen_msg_filename 2>&1"
186 notmuch config set new.tags $OLDCONFIG
188 # DUPLICATE_MESSAGE_ID is not tested here, because it should actually pass.
190 for code in OUT_OF_MEMORY XAPIAN_EXCEPTION FILE_NOT_EMAIL \
191 READ_ONLY_DATABASE UPGRADE_REQUIRED PATH_ERROR; do
192 cat <<EOF > index-file-$code.gdb
193 set breakpoint pending on
194 set logging file index-file-$code.log
196 break notmuch_database_add_message
198 return NOTMUCH_STATUS_$code
207 for code in FILE_NOT_EMAIL READ_ONLY_DATABASE UPGRADE_REQUIRED PATH_ERROR; do
208 test_expect_code 1 "EXIT_FAILURE when add_message returns $code" \
209 "${TEST_GDB} --batch-silent --return-child-result \
210 -ex 'set args insert < $gen_msg_filename' \
211 -x index-file-$code.gdb notmuch"
212 test_expect_code 0 "success exit with --keep when add_message returns $code" \
213 "${TEST_GDB} --batch-silent --return-child-result \
214 -ex 'set args insert --keep < $gen_msg_filename' \
215 -x index-file-$code.gdb notmuch"
218 for code in OUT_OF_MEMORY XAPIAN_EXCEPTION ; do
219 test_expect_code 75 "EX_TEMPFAIL when add_message returns $code" \
220 "${TEST_GDB} --batch-silent --return-child-result \
221 -ex 'set args insert < $gen_msg_filename' \
222 -x index-file-$code.gdb notmuch"
223 test_expect_code 0 "success exit with --keep when add_message returns $code" \
224 "${TEST_GDB} --batch-silent --return-child-result \
225 -ex 'set args insert --keep < $gen_msg_filename' \
226 -x index-file-$code.gdb notmuch"