]> git.notmuchmail.org Git - notmuch/blob - test/T560-lib-error.sh
lib: replace almost all fprintfs in library with _n_d_log
[notmuch] / test / T560-lib-error.sh
1 #!/usr/bin/env bash
2 test_description="error reporting for library"
3
4 . ./test-lib.sh
5
6 backup_database () {
7     rm -rf notmuch-dir-backup
8     cp -pR ${MAIL_DIR}/.notmuch notmuch-dir-backup
9 }
10 restore_database () {
11     rm -rf ${MAIL_DIR}/.notmuch
12     cp -pR notmuch-dir-backup ${MAIL_DIR}/.notmuch
13 }
14
15
16 add_email_corpus
17
18 test_expect_success "building database" "NOTMUCH_NEW"
19
20 test_begin_subtest "Open null pointer"
21 test_C <<'EOF'
22 #include <stdio.h>
23 #include <notmuch.h>
24 int main (int argc, char** argv)
25 {
26     notmuch_database_t *db;
27     notmuch_status_t stat;
28     stat = notmuch_database_open (NULL, 0, 0);
29 }
30 EOF
31 cat <<'EOF' >EXPECTED
32 == stdout ==
33 == stderr ==
34 Error: Cannot open a database for a NULL path.
35 EOF
36 test_expect_equal_file EXPECTED OUTPUT
37
38 test_begin_subtest "Open nonexistent database"
39 test_C <<'EOF'
40 #include <stdio.h>
41 #include <notmuch.h>
42 int main (int argc, char** argv)
43 {
44     notmuch_database_t *db;
45     notmuch_status_t stat;
46     stat = notmuch_database_open ("/nonexistent/foo", 0, 0);
47 }
48 EOF
49 cat <<'EOF' >EXPECTED
50 == stdout ==
51 == stderr ==
52 Error opening database at /nonexistent/foo/.notmuch: No such file or directory
53 EOF
54 test_expect_equal_file EXPECTED OUTPUT
55
56 test_begin_subtest "create NULL path"
57 test_C <<'EOF'
58 #include <stdio.h>
59 #include <notmuch.h>
60 int main (int argc, char** argv)
61 {
62     notmuch_status_t stat;
63     stat = notmuch_database_create (NULL, NULL);
64 }
65 EOF
66 cat <<'EOF' >EXPECTED
67 == stdout ==
68 == stderr ==
69 Error: Cannot create a database for a NULL path.
70 EOF
71 test_expect_equal_file EXPECTED OUTPUT
72
73 test_begin_subtest "Create database in non-existant directory"
74 test_C <<'EOF'
75 #include <stdio.h>
76 #include <notmuch.h>
77 int main (int argc, char** argv)
78 {
79     notmuch_database_t *db;
80     notmuch_status_t stat;
81     stat = notmuch_database_create ("/nonexistent/foo", &db);
82 }
83 EOF
84 cat <<'EOF' >EXPECTED
85 == stdout ==
86 == stderr ==
87 Error: Cannot create database at /nonexistent/foo: No such file or directory.
88 EOF
89 test_expect_equal_file EXPECTED OUTPUT
90
91 test_begin_subtest "Write to read-only database"
92 test_C ${MAIL_DIR} <<'EOF'
93 #include <stdio.h>
94 #include <notmuch.h>
95 int main (int argc, char** argv)
96 {
97    notmuch_database_t *db;
98    notmuch_status_t stat;
99    stat = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_ONLY, &db);
100    if (stat != NOTMUCH_STATUS_SUCCESS) {
101      fprintf (stderr, "error opening database: %d\n", stat);
102    }
103    stat = notmuch_database_add_message (db, "/dev/null", NULL);
104    if (stat)
105        fputs (notmuch_database_status_string (db), stderr);
106
107 }
108 EOF
109 cat <<'EOF' >EXPECTED
110 == stdout ==
111 == stderr ==
112 Cannot write to a read-only database.
113 EOF
114 test_expect_equal_file EXPECTED OUTPUT
115
116 test_begin_subtest "compact, overwriting existing backup"
117 test_C ${MAIL_DIR} <<'EOF'
118 #include <stdio.h>
119 #include <notmuch.h>
120 static void
121 status_cb (const char *msg, void *closure)
122 {
123     printf ("%s\n", msg);
124 }
125 int main (int argc, char** argv)
126 {
127    notmuch_database_t *db;
128    notmuch_status_t stat;
129    stat = notmuch_database_compact (argv[1], argv[1], status_cb, NULL);
130 }
131 EOF
132 cat <<'EOF' >EXPECTED
133 == stdout ==
134 Path already exists: CWD/mail
135
136 == stderr ==
137 EOF
138 test_expect_equal_file EXPECTED OUTPUT
139
140 test_done