2 test_description='"notmuch insert"'
5 # Create directories and database before inserting.
6 mkdir -p "$MAIL_DIR"/{cur,new,tmp}
7 mkdir -p "$MAIL_DIR"/Drafts/{cur,new,tmp}
8 notmuch new > /dev/null
10 # We use generate_message to create the temporary message files.
11 # They happen to be in the mail directory already but that is okay
12 # since we do not call notmuch new hereafter.
16 "[subject]=\"insert-subject\"" \
17 "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" \
18 "[body]=\"insert-message\""
21 test_expect_code 1 "Insert zero-length file" \
22 "notmuch insert < /dev/null"
24 # This test is a proxy for other errors that may occur while trying to
25 # add a message to the notmuch database, e.g. database locked.
26 test_expect_code 0 "Insert non-message" \
27 "echo bad_message | notmuch insert"
29 test_begin_subtest "Database empty so far"
30 test_expect_equal "0" "`notmuch count --output=messages '*'`"
32 test_begin_subtest "Insert message"
34 notmuch insert < "$gen_msg_filename"
35 cur_msg_filename=$(notmuch search --output=files "subject:insert-subject")
36 test_expect_equal_file "$cur_msg_filename" "$gen_msg_filename"
38 test_begin_subtest "Insert message adds default tags"
39 output=$(notmuch show --format=json "subject:insert-subject")
41 "id": "'"${gen_msg_id}"'",
44 "filename": "'"${cur_msg_filename}"'",
45 "timestamp": 946728000,
46 "date_relative": "2000-01-01",
47 "tags": ["inbox","unread"],
49 "Subject": "insert-subject",
50 "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
51 "To": "Notmuch Test Suite <test_suite@notmuchmail.org>",
52 "Date": "Sat, 01 Jan 2000 12:00:00 +0000"},
54 "content-type": "text/plain",
55 "content": "insert-message\n"}]},
57 test_expect_equal_json "$output" "$expected"
59 test_begin_subtest "Insert duplicate message"
60 notmuch insert +duptag -unread < "$gen_msg_filename"
61 output=$(notmuch search --output=files "subject:insert-subject" | wc -l)
62 test_expect_equal "$output" 2
64 test_begin_subtest "Duplicate message does not change tags"
65 output=$(notmuch search --format=json --output=tags "subject:insert-subject")
66 test_expect_equal_json "$output" '["inbox", "unread"]'
68 test_begin_subtest "Insert message, add tag"
70 notmuch insert +custom < "$gen_msg_filename"
71 output=$(notmuch search --output=messages tag:custom)
72 test_expect_equal "$output" "id:$gen_msg_id"
74 test_begin_subtest "Insert message, add/remove tags"
76 notmuch insert +custom -unread < "$gen_msg_filename"
77 output=$(notmuch search --output=messages tag:custom NOT tag:unread)
78 test_expect_equal "$output" "id:$gen_msg_id"
80 test_begin_subtest "Insert message with default tags stays in new/"
82 notmuch insert < "$gen_msg_filename"
83 output=$(notmuch search --output=files id:$gen_msg_id)
84 dirname=$(dirname "$output")
85 test_expect_equal "$dirname" "$MAIL_DIR/new"
87 test_begin_subtest "Insert message with non-maildir synced tags stays in new/"
89 notmuch insert +custom -inbox < "$gen_msg_filename"
90 output=$(notmuch search --output=files id:$gen_msg_id)
91 dirname=$(dirname "$output")
92 test_expect_equal "$dirname" "$MAIL_DIR/new"
94 test_begin_subtest "Insert message with custom new.tags goes to cur/"
95 OLDCONFIG=$(notmuch config get new.tags)
96 notmuch config set new.tags test
98 notmuch insert < "$gen_msg_filename"
99 output=$(notmuch search --output=files id:$gen_msg_id)
100 dirname=$(dirname "$output")
101 notmuch config set new.tags $OLDCONFIG
102 test_expect_equal "$dirname" "$MAIL_DIR/cur"
104 # additional check on the previous message
105 test_begin_subtest "Insert message with custom new.tags actually gets the tags"
106 output=$(notmuch search --output=tags id:$gen_msg_id)
107 test_expect_equal "$output" "test"
109 test_begin_subtest "Insert message with maildir synced tags goes to cur/"
111 notmuch insert +flagged < "$gen_msg_filename"
112 output=$(notmuch search --output=files id:$gen_msg_id)
113 dirname=$(dirname "$output")
114 test_expect_equal "$dirname" "$MAIL_DIR/cur"
116 test_begin_subtest "Insert message with maildir sync off goes to new/"
117 OLDCONFIG=$(notmuch config get maildir.synchronize_flags)
118 notmuch config set maildir.synchronize_flags false
120 notmuch insert +flagged < "$gen_msg_filename"
121 output=$(notmuch search --output=files id:$gen_msg_id)
122 dirname=$(dirname "$output")
123 notmuch config set maildir.synchronize_flags $OLDCONFIG
124 test_expect_equal "$dirname" "$MAIL_DIR/new"
126 test_begin_subtest "Insert message into folder"
128 notmuch insert --folder=Drafts < "$gen_msg_filename"
129 output=$(notmuch search --output=files path:Drafts/new)
130 dirname=$(dirname "$output")
131 test_expect_equal "$dirname" "$MAIL_DIR/Drafts/new"
133 test_begin_subtest "Insert message into folder, add/remove tags"
135 notmuch insert --folder=Drafts +draft -unread < "$gen_msg_filename"
136 output=$(notmuch search --output=messages path:Drafts/cur tag:draft NOT tag:unread)
137 test_expect_equal "$output" "id:$gen_msg_id"
140 test_expect_code 1 "Insert message into non-existent folder" \
141 "notmuch insert --folder=nonesuch < $gen_msg_filename"
143 test_begin_subtest "Insert message, create folder"
145 notmuch insert --folder=F --create-folder +folder < "$gen_msg_filename"
146 output=$(notmuch search --output=files path:F/new tag:folder)
147 basename=$(basename "$output")
148 test_expect_equal_file "$gen_msg_filename" "$MAIL_DIR/F/new/${basename}"
150 test_begin_subtest "Insert message, create subfolder"
152 notmuch insert --folder=F/G/H/I/J --create-folder +folder < "$gen_msg_filename"
153 output=$(notmuch search --output=files path:F/G/H/I/J/new tag:folder)
154 basename=$(basename "$output")
155 test_expect_equal_file "$gen_msg_filename" "${MAIL_DIR}/F/G/H/I/J/new/${basename}"
157 test_begin_subtest "Insert message, create existing subfolder"
159 notmuch insert --folder=F/G/H/I/J --create-folder +folder < "$gen_msg_filename"
160 output=$(notmuch count path:F/G/H/I/J/new tag:folder)
161 test_expect_equal "$output" "2"
164 test_expect_code 1 "Insert message, create invalid subfolder" \
165 "notmuch insert --folder=../G --create-folder $gen_msg_filename"
167 OLDCONFIG=$(notmuch config get new.tags)
169 test_begin_subtest "Empty tags in new.tags are forbidden"
170 notmuch config set new.tags "foo;;bar"
172 output=$(notmuch insert $gen_msg_filename 2>&1)
173 test_expect_equal "$output" "Error: tag '' in new.tags: empty tag forbidden"
175 test_begin_subtest "Tags starting with '-' in new.tags are forbidden"
176 notmuch config set new.tags "-foo;bar"
178 output=$(notmuch insert $gen_msg_filename 2>&1)
179 test_expect_equal "$output" "Error: tag '-foo' in new.tags: tag starting with '-' forbidden"
181 test_expect_code 1 "Invalid tags set exit code" \
182 "notmuch insert $gen_msg_filename 2>&1"
184 notmuch config set new.tags $OLDCONFIG