2 test_description="error reporting for library"
4 . $(dirname "$0")/test-lib.sh || exit 1
8 test_begin_subtest "building database"
9 test_expect_success "NOTMUCH_NEW"
11 test_begin_subtest "Open null pointer"
15 int main (int argc, char** argv)
17 notmuch_database_t *db;
18 notmuch_status_t stat;
19 stat = notmuch_database_open (NULL, 0, 0);
25 Error: Cannot open a database for a NULL path.
27 test_expect_equal_file EXPECTED OUTPUT
29 test_begin_subtest "Open relative path"
33 int main (int argc, char** argv)
35 notmuch_database_t *db;
36 notmuch_status_t stat;
37 stat = notmuch_database_open ("./nonexistent/foo", 0, 0);
43 Error: Database path must be absolute.
45 test_expect_equal_file EXPECTED OUTPUT
47 test_begin_subtest "Create database in relative path"
51 int main (int argc, char** argv)
53 notmuch_database_t *db;
54 notmuch_status_t stat;
55 stat = notmuch_database_create ("./nonexistent/foo", &db);
61 Error: Database path must be absolute.
63 test_expect_equal_file EXPECTED OUTPUT
65 test_begin_subtest "Open nonexistent database"
66 test_C ${PWD}/nonexistent/foo <<'EOF'
69 int main (int argc, char** argv)
71 notmuch_database_t *db;
72 notmuch_status_t stat;
73 stat = notmuch_database_open (argv[1], 0, 0);
79 Error opening database at CWD/nonexistent/foo/.notmuch: No such file or directory
81 test_expect_equal_file EXPECTED OUTPUT
83 test_begin_subtest "create NULL path"
87 int main (int argc, char** argv)
89 notmuch_status_t stat;
90 stat = notmuch_database_create (NULL, NULL);
96 Error: Cannot create a database for a NULL path.
98 test_expect_equal_file EXPECTED OUTPUT
100 test_begin_subtest "Create database in nonexistent directory"
101 test_C ${PWD}/nonexistent/foo<<'EOF'
104 int main (int argc, char** argv)
106 notmuch_database_t *db;
107 notmuch_status_t stat;
108 stat = notmuch_database_create (argv[1], &db);
111 cat <<'EOF' >EXPECTED
114 Error: Cannot create database at CWD/nonexistent/foo: No such file or directory.
116 test_expect_equal_file EXPECTED OUTPUT
118 test_begin_subtest "Write to read-only database"
119 test_C ${MAIL_DIR} <<'EOF'
122 int main (int argc, char** argv)
124 notmuch_database_t *db;
125 notmuch_status_t stat;
126 stat = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_ONLY, &db);
127 if (stat != NOTMUCH_STATUS_SUCCESS) {
128 fprintf (stderr, "error opening database: %d\n", stat);
130 stat = notmuch_database_index_file (db, "/dev/null", NULL, NULL);
132 fputs (notmuch_database_status_string (db), stderr);
136 cat <<'EOF' >EXPECTED
139 Cannot write to a read-only database.
141 test_expect_equal_file EXPECTED OUTPUT
143 test_begin_subtest "Add non-existent file"
144 test_C ${MAIL_DIR} <<'EOF'
147 int main (int argc, char** argv)
149 notmuch_database_t *db;
150 notmuch_status_t stat;
151 stat = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db);
152 if (stat != NOTMUCH_STATUS_SUCCESS) {
153 fprintf (stderr, "error opening database: %d\n", stat);
155 stat = notmuch_database_index_file (db, "./nonexistent", NULL, NULL);
157 char *status_string = notmuch_database_status_string (db);
158 if (status_string) fputs (status_string, stderr);
162 cat <<'EOF' >EXPECTED
165 Error opening ./nonexistent: No such file or directory
167 test_expect_equal_file EXPECTED OUTPUT
169 test_begin_subtest "compact, overwriting existing backup"
170 test_C ${MAIL_DIR} <<'EOF'
174 status_cb (const char *msg, void *closure)
176 printf ("%s\n", msg);
178 int main (int argc, char** argv)
180 notmuch_database_t *db;
181 notmuch_status_t stat;
182 stat = notmuch_database_compact (argv[1], argv[1], status_cb, NULL);
185 cat <<'EOF' >EXPECTED
187 Path already exists: MAIL_DIR
191 test_expect_equal_file EXPECTED OUTPUT
195 #include <sys/types.h>
196 #include <sys/stat.h>
201 int main (int argc, char** argv)
203 notmuch_database_t *db;
204 notmuch_status_t stat;
209 stat = notmuch_database_open_verbose (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db, &msg);
210 if (stat != NOTMUCH_STATUS_SUCCESS) {
211 fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
214 path = talloc_asprintf (db, "%s/.notmuch/xapian/postlist.${db_ending}", argv[1]);
215 fd = open(path,O_WRONLY|O_TRUNC);
217 fprintf (stderr, "error opening %s\n", argv[1]);
223 const char *stat_str = notmuch_database_status_string (db);
225 fputs (stat_str, stderr);
232 test_begin_subtest "Xapian exception finding message"
233 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
235 notmuch_message_t *message = NULL;
236 stat = notmuch_database_find_message (db, "id:nonexistent", &message);
239 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
240 cat <<'EOF' >EXPECTED
243 A Xapian exception occurred finding message
245 test_expect_equal_file EXPECTED OUTPUT.clean
249 test_begin_subtest "Xapian exception getting tags"
250 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
252 notmuch_tags_t *tags = NULL;
253 tags = notmuch_database_get_all_tags (db);
254 stat = (tags == NULL);
257 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
258 cat <<'EOF' >EXPECTED
261 A Xapian exception occurred getting tags
263 test_expect_equal_file EXPECTED OUTPUT.clean
267 test_begin_subtest "Xapian exception creating directory"
268 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
270 notmuch_directory_t *directory = NULL;
271 stat = notmuch_database_get_directory (db, "none/existing", &directory);
274 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
275 cat <<'EOF' >EXPECTED
278 A Xapian exception occurred creating a directory
280 test_expect_equal_file EXPECTED OUTPUT.clean
284 test_begin_subtest "Xapian exception searching messages"
285 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
287 notmuch_messages_t *messages = NULL;
288 notmuch_query_t *query=notmuch_query_create (db, "*");
289 stat = notmuch_query_search_messages (query, &messages);
292 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
293 cat <<'EOF' >EXPECTED
296 A Xapian exception occurred performing query
299 test_expect_equal_file EXPECTED OUTPUT.clean
303 test_begin_subtest "Xapian exception counting messages"
304 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
307 notmuch_query_t *query=notmuch_query_create (db, "id:87ocn0qh6d.fsf@yoom.home.cworth.org");
308 stat = notmuch_query_count_messages (query, &count);
311 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
312 cat <<'EOF' >EXPECTED
315 A Xapian exception occurred performing query
316 Query string was: id:87ocn0qh6d.fsf@yoom.home.cworth.org
318 test_expect_equal_file EXPECTED OUTPUT.clean
324 #include <notmuch-test.h>
325 int main (int argc, char** argv)
327 notmuch_database_t *db;
328 notmuch_status_t stat;
330 notmuch_message_t *message = NULL;
331 const char *id = "1258471718-6781-1-git-send-email-dottedmag@dottedmag.net";
333 stat = notmuch_database_open_verbose (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db, &msg);
334 if (stat != NOTMUCH_STATUS_SUCCESS) {
335 fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
338 EXPECT0(notmuch_database_find_message (db, id, &message));
339 EXPECT0(notmuch_database_close (db));
342 test_begin_subtest "Handle getting message-id from closed database"
343 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
346 id2=notmuch_message_get_message_id (message);
347 printf("%d\n%d\n", message != NULL, id2==NULL);
356 test_expect_equal_file EXPECTED OUTPUT
358 test_begin_subtest "Handle getting thread-id from closed database"
359 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
362 id2=notmuch_message_get_thread_id (message);
363 printf("%d\n%d\n", message != NULL, id2==NULL);
372 test_expect_equal_file EXPECTED OUTPUT
374 test_begin_subtest "Handle getting header from closed database"
375 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
378 from=notmuch_message_get_header (message, "from");
379 printf("%s\n%d\n", id, from == NULL);
384 1258471718-6781-1-git-send-email-dottedmag@dottedmag.net
388 test_expect_equal_file EXPECTED OUTPUT
390 # XXX TODO: test on a message from notmuch_thread_get_toplevel_messages
391 # XXX this test only tests the trivial code path
392 test_begin_subtest "Handle getting replies from closed database"
393 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
395 notmuch_messages_t *replies;
396 replies = notmuch_message_get_replies (message);
397 printf("%d\n%d\n", message != NULL, replies==NULL);
406 test_expect_equal_file EXPECTED OUTPUT
408 test_begin_subtest "Handle getting message filename from closed database"
409 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
411 const char *filename;
412 filename = notmuch_message_get_filename (message);
413 printf("%d\n%d\n", message != NULL, filename == NULL);
422 test_expect_equal_file EXPECTED OUTPUT
424 test_begin_subtest "Handle getting all message filenames from closed database"
425 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
427 notmuch_filenames_t *filenames;
428 filenames = notmuch_message_get_filenames (message);
429 printf("%d\n%d\n", message != NULL, filenames == NULL);
438 test_expect_equal_file EXPECTED OUTPUT
440 test_begin_subtest "Handle getting ghost flag from closed database"
441 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
443 notmuch_bool_t result;
444 result = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_GHOST);
445 printf("%d\n%d\n", message != NULL, result == FALSE);
454 test_expect_equal_file EXPECTED OUTPUT
456 test_begin_subtest "Handle getting date from closed database"
457 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
460 result = notmuch_message_get_date (message);
461 printf("%d\n%d\n", message != NULL, result == 0);
470 test_expect_equal_file EXPECTED OUTPUT
472 test_begin_subtest "Handle getting tags from closed database"
473 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
475 notmuch_tags_t *result;
476 result = notmuch_message_get_tags (message);
477 printf("%d\n%d\n", message != NULL, result == NULL);
486 test_expect_equal_file EXPECTED OUTPUT
488 test_begin_subtest "Handle counting files from closed database"
489 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
492 result = notmuch_message_count_files (message);
493 printf("%d\n%d\n", message != NULL, result < 0);
502 test_expect_equal_file EXPECTED OUTPUT
504 test_begin_subtest "Handle adding tag with closed database"
505 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
507 notmuch_status_t status;
508 status = notmuch_message_add_tag (message, "boom");
509 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
518 test_expect_equal_file EXPECTED OUTPUT
520 test_begin_subtest "Handle removing tag with closed database"
521 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
523 notmuch_status_t status;
524 status = notmuch_message_remove_tag (message, "boom");
525 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
534 test_expect_equal_file EXPECTED OUTPUT
536 test_begin_subtest "Handle read maildir flag with closed database"
537 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
539 notmuch_bool_t is_set = -1;
540 is_set = notmuch_message_has_maildir_flag (message, 'S');
541 printf("%d\n%d\n", message != NULL, is_set == FALSE || is_set == TRUE);
550 test_expect_equal_file EXPECTED OUTPUT
552 test_begin_subtest "Handle checking maildir flag with closed db (new API)"
553 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
555 notmuch_status_t status;
557 status = notmuch_message_has_maildir_flag_st (message, 'S', &out);
558 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
567 test_expect_equal_file EXPECTED OUTPUT
569 test_begin_subtest "Handle converting maildir flags to tags with closed db"
570 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
572 notmuch_status_t status;
573 status = notmuch_message_maildir_flags_to_tags (message);
574 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
583 test_expect_equal_file EXPECTED OUTPUT
585 test_begin_subtest "Handle removing all tags with closed db"
586 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
588 notmuch_status_t status;
589 status = notmuch_message_remove_all_tags (message);
590 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
599 test_expect_equal_file EXPECTED OUTPUT
601 test_begin_subtest "Handle freezing message with closed db"
602 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
604 notmuch_status_t status;
605 status = notmuch_message_freeze (message);
606 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_SUCCESS);
615 test_expect_equal_file EXPECTED OUTPUT
617 test_begin_subtest "Handle thawing message with closed db"
618 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
620 notmuch_status_t status;
621 status = notmuch_message_thaw (message);
622 printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW);
631 test_expect_equal_file EXPECTED OUTPUT
633 test_begin_subtest "Handle destroying message with closed db"
634 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
636 notmuch_message_destroy (message);
637 printf("%d\n%d\n", message != NULL, 1);
646 test_expect_equal_file EXPECTED OUTPUT
648 test_begin_subtest "Handle retrieving closed db from message"
649 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR}
651 notmuch_database_t *db2;
652 db2 = notmuch_message_get_database (message);
653 printf("%d\n%d\n", message != NULL, db == db2);
662 test_expect_equal_file EXPECTED OUTPUT