2 test_description="API tests for notmuch_message_*"
4 . $(dirname "$0")/test-lib.sh || exit 1
6 if [ -n "${NOTMUCH_TEST_INSTALLED}" ]; then
12 test_begin_subtest "building database"
13 test_expect_success "NOTMUCH_NEW"
17 const char *stat_str = notmuch_database_status_string (db);
19 fputs (stat_str, stderr);
26 #include <notmuch-test.h>
28 int main (int argc, char** argv)
30 notmuch_database_t *db;
31 notmuch_status_t stat;
33 notmuch_message_t *message = NULL;
34 const char *id = "87pr7gqidx.fsf@yoom.home.cworth.org";
36 stat = notmuch_database_open_with_config (argv[1],
37 NOTMUCH_DATABASE_MODE_READ_WRITE,
38 NULL, NULL, &db, &msg);
39 if (stat != NOTMUCH_STATUS_SUCCESS) {
40 fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
43 EXPECT0(notmuch_database_find_message (db, id, &message));
47 echo " EXPECT0(notmuch_database_close (db));" >> c_head
49 test_begin_subtest "Handle getting message-id from closed database"
50 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
53 id2=notmuch_message_get_message_id (message);
54 printf("%d\n%d\n", message != NULL, id2==NULL);
63 test_expect_equal_file EXPECTED OUTPUT
65 test_begin_subtest "Handle getting thread-id from closed database"
66 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
69 id2=notmuch_message_get_thread_id (message);
70 printf("%d\n%d\n", message != NULL, id2==NULL);
79 test_expect_equal_file EXPECTED OUTPUT
81 test_begin_subtest "Handle getting header from closed database"
82 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
85 from=notmuch_message_get_header (message, "from");
86 printf("%s\n%d\n", id, from == NULL);
91 87pr7gqidx.fsf@yoom.home.cworth.org
95 test_expect_equal_file EXPECTED OUTPUT
97 # XXX this test only tests the trivial code path
98 test_begin_subtest "Handle getting replies from closed database"
99 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
101 notmuch_messages_t *replies;
102 replies = notmuch_message_get_replies (message);
103 printf("%d\n%d\n", message != NULL, replies==NULL);
112 test_expect_equal_file EXPECTED OUTPUT
114 test_begin_subtest "Handle getting message filename from closed database"
115 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
117 const char *filename;
118 filename = notmuch_message_get_filename (message);
119 printf("%d\n%d\n", message != NULL, filename == NULL);
128 test_expect_equal_file EXPECTED OUTPUT
130 test_begin_subtest "Handle getting all message filenames from closed database"
131 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
133 notmuch_filenames_t *filenames;
134 filenames = notmuch_message_get_filenames (message);
135 printf("%d\n%d\n", message != NULL, filenames == NULL);
144 test_expect_equal_file EXPECTED OUTPUT
146 test_begin_subtest "iterate over all message filenames from closed database"
147 cat c_head0 - c_tail <<'EOF' | test_C ${MAIL_DIR}
149 notmuch_filenames_t *filenames;
150 filenames = notmuch_message_get_filenames (message);
151 EXPECT0(notmuch_database_close (db));
152 for (; notmuch_filenames_valid (filenames);
153 notmuch_filenames_move_to_next (filenames)) {
154 const char *filename = notmuch_filenames_get (filenames);
155 printf("%s\n", filename);
157 notmuch_filenames_destroy (filenames);
167 test_expect_equal_file EXPECTED OUTPUT
169 test_begin_subtest "Handle getting ghost flag from closed database"
170 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
172 notmuch_bool_t result;
173 result = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_GHOST);
174 printf("%d\n%d\n", message != NULL, result == FALSE);
183 test_expect_equal_file EXPECTED OUTPUT
185 test_begin_subtest "Handle getting date from closed database"
186 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
189 result = notmuch_message_get_date (message);
190 printf("%d\n%d\n", message != NULL, result == 0);
199 test_expect_equal_file EXPECTED OUTPUT
201 test_begin_subtest "Handle getting tags from closed database"
202 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
204 notmuch_tags_t *result;
205 result = notmuch_message_get_tags (message);
206 printf("%d\n%d\n", message != NULL, result == NULL);
215 test_expect_equal_file EXPECTED OUTPUT
217 test_begin_subtest "Handle counting files from closed database"
218 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
221 result = notmuch_message_count_files (message);
222 printf("%d\n%d\n", message != NULL, result < 0);
231 test_expect_equal_file EXPECTED OUTPUT
233 test_begin_subtest "Handle adding tag with closed database"
234 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
236 notmuch_status_t status;
237 status = notmuch_message_add_tag (message, "boom");
238 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE);
247 test_expect_equal_file EXPECTED OUTPUT
249 test_begin_subtest "Handle removing tag with closed database"
250 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
252 notmuch_status_t status;
253 status = notmuch_message_remove_tag (message, "boom");
254 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE);
263 test_expect_equal_file EXPECTED OUTPUT
265 test_begin_subtest "Handle read maildir flag with closed database"
266 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
268 notmuch_bool_t is_set = -1;
269 is_set = notmuch_message_has_maildir_flag (message, 'S');
270 printf("%d\n%d\n", message != NULL, is_set == FALSE || is_set == TRUE);
279 test_expect_equal_file EXPECTED OUTPUT
281 test_begin_subtest "Handle checking maildir flag with closed db (new API)"
282 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
284 notmuch_status_t status;
286 status = notmuch_message_has_maildir_flag_st (message, 'S', &out);
287 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
296 test_expect_equal_file EXPECTED OUTPUT
298 test_begin_subtest "Handle converting maildir flags to tags with closed db"
299 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
301 notmuch_status_t status;
302 status = notmuch_message_maildir_flags_to_tags (message);
303 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
312 test_expect_equal_file EXPECTED OUTPUT
314 test_begin_subtest "_notmuch_message_add_term catches exceptions"
315 cat c_head0 - c_tail <<'EOF' | test_private_C ${MAIL_DIR}
317 notmuch_private_status_t status;
318 /* This relies on Xapian throwing an exception for adding empty terms */
319 status = _notmuch_message_add_term (message, "body", "");
320 printf("%d\n%d\n", message != NULL, status != NOTMUCH_STATUS_SUCCESS );
329 test_expect_equal_file EXPECTED OUTPUT
331 test_begin_subtest "_notmuch_message_remove_term catches exceptions"
332 cat c_head0 - c_tail <<'EOF' | test_private_C ${MAIL_DIR}
334 notmuch_private_status_t status;
335 /* Xapian throws the same exception for empty and non-existent terms;
336 * error string varies between Xapian versions. */
337 status = _notmuch_message_remove_term (message, "tag", "nonexistent");
338 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_SUCCESS );
347 test_expect_equal_file EXPECTED OUTPUT
349 test_begin_subtest "_notmuch_message_add_filename on closed db"
350 cat c_head - c_tail <<'EOF' | test_private_C ${MAIL_DIR}
352 notmuch_private_status_t status;
353 status = _notmuch_message_add_filename (message, "some-filename");
354 printf("%d\n%d\n", message != NULL, status != NOTMUCH_STATUS_SUCCESS);
363 test_expect_equal_file EXPECTED OUTPUT
365 test_begin_subtest "_notmuch_message_remove_filename on closed db"
366 cat c_head - c_tail <<'EOF' | test_private_C ${MAIL_DIR}
368 notmuch_private_status_t status;
369 status = _notmuch_message_remove_filename (message, "some-filename");
370 printf("%d\n%d\n", message != NULL, status != NOTMUCH_STATUS_SUCCESS);
379 test_expect_equal_file EXPECTED OUTPUT
381 test_begin_subtest "Handle converting tags to maildir flags with closed db"
382 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
384 notmuch_status_t status;
385 status = notmuch_message_tags_to_maildir_flags (message);
386 printf("%d\n%d\n", message != NULL, status != NOTMUCH_STATUS_SUCCESS);
395 test_expect_equal_file EXPECTED OUTPUT
397 POSTLIST_PATH=(${MAIL_DIR}/.notmuch/xapian/postlist.*)
398 test_begin_subtest "Handle converting tags to maildir flags with corrupted db"
400 cat c_head0 - c_tail <<'EOF' | test_C ${MAIL_DIR} ${POSTLIST_PATH}
402 notmuch_status_t status;
404 status = notmuch_message_add_tag (message, "draft");
407 int fd = open(argv[2],O_WRONLY|O_TRUNC);
409 fprintf (stderr, "error opening %s\n", argv[1]);
413 status = notmuch_message_tags_to_maildir_flags (message);
414 printf("%d\n%d\n", message != NULL, status != NOTMUCH_STATUS_SUCCESS);
425 notmuch tag -draft id:87pr7gqidx.fsf@yoom.home.cworth.org
426 test_expect_equal_file EXPECTED OUTPUT
428 test_begin_subtest "Handle removing all tags with closed db"
429 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
431 notmuch_status_t status;
432 status = notmuch_message_remove_all_tags (message);
433 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE);
442 test_expect_equal_file EXPECTED OUTPUT
444 test_begin_subtest "Handle freezing message with closed db"
445 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
447 notmuch_status_t status;
448 status = notmuch_message_freeze (message);
449 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE);
458 test_expect_equal_file EXPECTED OUTPUT
460 test_begin_subtest "Handle thawing message with closed db"
461 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
463 notmuch_status_t status;
464 status = notmuch_message_thaw (message);
465 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE);
474 test_expect_equal_file EXPECTED OUTPUT
476 test_begin_subtest "Handle destroying message with closed db"
477 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
479 notmuch_message_destroy (message);
480 printf("%d\n%d\n", message != NULL, 1);
489 test_expect_equal_file EXPECTED OUTPUT
491 test_begin_subtest "Handle retrieving closed db from message"
492 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
494 notmuch_database_t *db2;
495 db2 = notmuch_message_get_database (message);
496 printf("%d\n%d\n", message != NULL, db == db2);
505 test_expect_equal_file EXPECTED OUTPUT
507 test_begin_subtest "Handle reindexing message with closed db"
508 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
510 notmuch_status_t status;
511 status = notmuch_message_reindex (message, NULL);
512 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
521 test_expect_equal_file EXPECTED OUTPUT
523 TERMLIST_PATH=(${MAIL_DIR}/.notmuch/xapian/termlist.*)
524 test_begin_subtest "remove message with corrupted db"
526 cat c_head0 - c_tail <<'EOF' | test_private_C ${MAIL_DIR} ${TERMLIST_PATH}
528 notmuch_status_t status;
530 int fd = open(argv[2],O_WRONLY|O_TRUNC);
532 fprintf (stderr, "error opening %s\n", argv[1]);
536 stat = _notmuch_message_delete (message);
537 printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
544 A Xapian exception occurred at message.cc:XXX: EOF reading block YYY
546 sed 's/EOF reading block [0-9]*/EOF reading block YYY/' < OUTPUT > OUTPUT.clean
547 test_expect_equal_file EXPECTED OUTPUT.clean