2 test_description="API tests for notmuch_message_*"
4 . $(dirname "$0")/test-lib.sh || exit 1
8 test_begin_subtest "building database"
9 test_expect_success "NOTMUCH_NEW"
13 const char *stat_str = notmuch_database_status_string (db);
15 fputs (stat_str, stderr);
22 #include <notmuch-test.h>
24 int main (int argc, char** argv)
26 notmuch_database_t *db;
27 notmuch_status_t stat;
29 notmuch_message_t *message = NULL;
30 const char *id = "87pr7gqidx.fsf@yoom.home.cworth.org";
32 stat = notmuch_database_open_with_config (argv[1],
33 NOTMUCH_DATABASE_MODE_READ_WRITE,
34 NULL, NULL, &db, &msg);
35 if (stat != NOTMUCH_STATUS_SUCCESS) {
36 fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
39 EXPECT0(notmuch_database_find_message (db, id, &message));
43 echo " EXPECT0(notmuch_database_close (db));" >> c_head
45 test_begin_subtest "Handle getting message-id from closed database"
46 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
49 id2=notmuch_message_get_message_id (message);
50 printf("%d\n%d\n", message != NULL, id2==NULL);
59 test_expect_equal_file EXPECTED OUTPUT
61 test_begin_subtest "Handle getting thread-id from closed database"
62 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
65 id2=notmuch_message_get_thread_id (message);
66 printf("%d\n%d\n", message != NULL, id2==NULL);
75 test_expect_equal_file EXPECTED OUTPUT
77 test_begin_subtest "Handle getting header from closed database"
78 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
81 from=notmuch_message_get_header (message, "from");
82 printf("%s\n%d\n", id, from == NULL);
87 87pr7gqidx.fsf@yoom.home.cworth.org
91 test_expect_equal_file EXPECTED OUTPUT
93 # XXX this test only tests the trivial code path
94 test_begin_subtest "Handle getting replies from closed database"
95 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
97 notmuch_messages_t *replies;
98 replies = notmuch_message_get_replies (message);
99 printf("%d\n%d\n", message != NULL, replies==NULL);
108 test_expect_equal_file EXPECTED OUTPUT
110 test_begin_subtest "Handle getting message filename from closed database"
111 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
113 const char *filename;
114 filename = notmuch_message_get_filename (message);
115 printf("%d\n%d\n", message != NULL, filename == NULL);
124 test_expect_equal_file EXPECTED OUTPUT
126 test_begin_subtest "Handle getting all message filenames from closed database"
127 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
129 notmuch_filenames_t *filenames;
130 filenames = notmuch_message_get_filenames (message);
131 printf("%d\n%d\n", message != NULL, filenames == NULL);
140 test_expect_equal_file EXPECTED OUTPUT
142 test_begin_subtest "iterate over all message filenames from closed database"
143 cat c_head0 - c_tail <<'EOF' | test_C ${MAIL_DIR}
145 notmuch_filenames_t *filenames;
146 filenames = notmuch_message_get_filenames (message);
147 EXPECT0(notmuch_database_close (db));
148 for (; notmuch_filenames_valid (filenames);
149 notmuch_filenames_move_to_next (filenames)) {
150 const char *filename = notmuch_filenames_get (filenames);
151 printf("%s\n", filename);
153 notmuch_filenames_destroy (filenames);
163 test_expect_equal_file EXPECTED OUTPUT
165 test_begin_subtest "Handle getting ghost flag from closed database"
166 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
168 notmuch_bool_t result;
169 result = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_GHOST);
170 printf("%d\n%d\n", message != NULL, result == FALSE);
179 test_expect_equal_file EXPECTED OUTPUT
181 test_begin_subtest "Handle getting date from closed database"
182 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
185 result = notmuch_message_get_date (message);
186 printf("%d\n%d\n", message != NULL, result == 0);
195 test_expect_equal_file EXPECTED OUTPUT
197 test_begin_subtest "Handle getting tags from closed database"
198 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
200 notmuch_tags_t *result;
201 result = notmuch_message_get_tags (message);
202 printf("%d\n%d\n", message != NULL, result == NULL);
211 test_expect_equal_file EXPECTED OUTPUT
213 test_begin_subtest "Handle counting files from closed database"
214 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
217 result = notmuch_message_count_files (message);
218 printf("%d\n%d\n", message != NULL, result < 0);
227 test_expect_equal_file EXPECTED OUTPUT
229 test_begin_subtest "Handle adding tag with closed database"
230 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
232 notmuch_status_t status;
233 status = notmuch_message_add_tag (message, "boom");
234 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE);
243 test_expect_equal_file EXPECTED OUTPUT
245 test_begin_subtest "Handle removing tag with closed database"
246 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
248 notmuch_status_t status;
249 status = notmuch_message_remove_tag (message, "boom");
250 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE);
259 test_expect_equal_file EXPECTED OUTPUT
261 test_begin_subtest "Handle read maildir flag with closed database"
262 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
264 notmuch_bool_t is_set = -1;
265 is_set = notmuch_message_has_maildir_flag (message, 'S');
266 printf("%d\n%d\n", message != NULL, is_set == FALSE || is_set == TRUE);
275 test_expect_equal_file EXPECTED OUTPUT
277 test_begin_subtest "Handle checking maildir flag with closed db (new API)"
278 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
280 notmuch_status_t status;
282 status = notmuch_message_has_maildir_flag_st (message, 'S', &out);
283 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
292 test_expect_equal_file EXPECTED OUTPUT
294 test_begin_subtest "Handle converting maildir flags to tags with closed db"
295 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
297 notmuch_status_t status;
298 status = notmuch_message_maildir_flags_to_tags (message);
299 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
308 test_expect_equal_file EXPECTED OUTPUT
310 test_begin_subtest "_notmuch_message_add_term catches exceptions"
311 cat c_head0 - c_tail <<'EOF' | test_private_C ${MAIL_DIR}
313 notmuch_private_status_t status;
314 /* This relies on Xapian throwing an exception for adding empty terms */
315 status = _notmuch_message_add_term (message, "body", "");
316 printf("%d\n%d\n", message != NULL, status != NOTMUCH_STATUS_SUCCESS );
325 test_expect_equal_file EXPECTED OUTPUT
327 test_begin_subtest "_notmuch_message_remove_term catches exceptions"
328 cat c_head0 - c_tail <<'EOF' | test_private_C ${MAIL_DIR}
330 notmuch_private_status_t status;
331 /* Xapian throws the same exception for empty and non-existent terms;
332 * error string varies between Xapian versions. */
333 status = _notmuch_message_remove_term (message, "tag", "nonexistent");
334 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_SUCCESS );
343 test_expect_equal_file EXPECTED OUTPUT
345 test_begin_subtest "_notmuch_message_add_filename on closed db"
346 cat c_head - c_tail <<'EOF' | test_private_C ${MAIL_DIR}
348 notmuch_private_status_t status;
349 status = _notmuch_message_add_filename (message, "some-filename");
350 printf("%d\n%d\n", message != NULL, status != NOTMUCH_STATUS_SUCCESS);
359 test_expect_equal_file EXPECTED OUTPUT
361 test_begin_subtest "_notmuch_message_remove_filename on closed db"
362 cat c_head - c_tail <<'EOF' | test_private_C ${MAIL_DIR}
364 notmuch_private_status_t status;
365 status = _notmuch_message_remove_filename (message, "some-filename");
366 printf("%d\n%d\n", message != NULL, status != NOTMUCH_STATUS_SUCCESS);
375 test_expect_equal_file EXPECTED OUTPUT
377 test_begin_subtest "Handle converting tags to maildir flags with closed db"
378 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
380 notmuch_status_t status;
381 status = notmuch_message_tags_to_maildir_flags (message);
382 printf("%d\n%d\n", message != NULL, status != NOTMUCH_STATUS_SUCCESS);
391 test_expect_equal_file EXPECTED OUTPUT
393 POSTLIST_PATH=(${MAIL_DIR}/.notmuch/xapian/postlist.*)
394 test_begin_subtest "Handle converting tags to maildir flags with corrupted db"
396 cat c_head0 - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
398 notmuch_status_t status;
400 status = notmuch_message_add_tag (message, "draft");
403 int fd = open(argv[2],O_WRONLY|O_TRUNC);
405 fprintf (stderr, "error opening %s\n", argv[1]);
409 status = notmuch_message_tags_to_maildir_flags (message);
410 printf("%d\n%d\n", message != NULL, status != NOTMUCH_STATUS_SUCCESS);
421 notmuch tag -draft id:87pr7gqidx.fsf@yoom.home.cworth.org
422 test_expect_equal_file EXPECTED OUTPUT
424 test_begin_subtest "Handle removing all tags with closed db"
425 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
427 notmuch_status_t status;
428 status = notmuch_message_remove_all_tags (message);
429 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE);
438 test_expect_equal_file EXPECTED OUTPUT
440 test_begin_subtest "Handle freezing message with closed db"
441 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
443 notmuch_status_t status;
444 status = notmuch_message_freeze (message);
445 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE);
454 test_expect_equal_file EXPECTED OUTPUT
456 test_begin_subtest "Handle thawing message with closed db"
457 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
459 notmuch_status_t status;
460 status = notmuch_message_thaw (message);
461 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE);
470 test_expect_equal_file EXPECTED OUTPUT
472 test_begin_subtest "Handle destroying message with closed db"
473 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
475 notmuch_message_destroy (message);
476 printf("%d\n%d\n", message != NULL, 1);
485 test_expect_equal_file EXPECTED OUTPUT
487 test_begin_subtest "Handle retrieving closed db from message"
488 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
490 notmuch_database_t *db2;
491 db2 = notmuch_message_get_database (message);
492 printf("%d\n%d\n", message != NULL, db == db2);
501 test_expect_equal_file EXPECTED OUTPUT
503 test_begin_subtest "Handle reindexing message with closed db"
504 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
506 notmuch_status_t status;
507 status = notmuch_message_reindex (message, NULL);
508 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
517 test_expect_equal_file EXPECTED OUTPUT
519 TERMLIST_PATH=(${MAIL_DIR}/.notmuch/xapian/termlist.*)
520 test_begin_subtest "remove message with corrupted db"
522 cat c_head0 - c_tail <<'EOF' | test_private_C ${MAIL_DIR} ${TERMLIST_PATH}
524 notmuch_status_t status;
526 int fd = open(argv[2],O_WRONLY|O_TRUNC);
528 fprintf (stderr, "error opening %s\n", argv[1]);
532 stat = _notmuch_message_delete (message);
533 printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
540 A Xapian exception occurred at message.cc:XXX: EOF reading block YYY
542 sed 's/EOF reading block [0-9]*/EOF reading block YYY/' < OUTPUT > OUTPUT.clean
543 test_expect_equal_file EXPECTED OUTPUT.clean