]> git.notmuchmail.org Git - notmuch/blob - test/T560-lib-error.sh
test: move nonexistent directory to something under notmuch's control.
[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 "Add non-existent file"
117 test_C ${MAIL_DIR} <<'EOF'
118 #include <stdio.h>
119 #include <notmuch.h>
120 int main (int argc, char** argv)
121 {
122    notmuch_database_t *db;
123    notmuch_status_t stat;
124    stat = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db);
125    if (stat != NOTMUCH_STATUS_SUCCESS) {
126      fprintf (stderr, "error opening database: %d\n", stat);
127    }
128    stat = notmuch_database_add_message (db, "./nonexistent", NULL);
129    if (stat) {
130        char *status_string = notmuch_database_status_string (db);
131        if (status_string) fputs (status_string, stderr);
132    }
133 }
134 EOF
135 cat <<'EOF' >EXPECTED
136 == stdout ==
137 == stderr ==
138 Error opening ./nonexistent: No such file or directory
139 EOF
140 test_expect_equal_file EXPECTED OUTPUT
141
142 test_begin_subtest "compact, overwriting existing backup"
143 test_C ${MAIL_DIR} <<'EOF'
144 #include <stdio.h>
145 #include <notmuch.h>
146 static void
147 status_cb (const char *msg, void *closure)
148 {
149     printf ("%s\n", msg);
150 }
151 int main (int argc, char** argv)
152 {
153    notmuch_database_t *db;
154    notmuch_status_t stat;
155    stat = notmuch_database_compact (argv[1], argv[1], status_cb, NULL);
156 }
157 EOF
158 cat <<'EOF' >EXPECTED
159 == stdout ==
160 Path already exists: CWD/mail
161
162 == stderr ==
163 EOF
164 test_expect_equal_file EXPECTED OUTPUT
165
166 cat <<'EOF' > c_head
167 #include <stdio.h>
168 #include <sys/types.h>
169 #include <sys/stat.h>
170 #include <fcntl.h>
171 #include <talloc.h>
172 #include <notmuch.h>
173
174 int main (int argc, char** argv)
175 {
176    notmuch_database_t *db;
177    notmuch_status_t stat;
178    char *path;
179    int fd;
180
181    stat = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db);
182    if (stat != NOTMUCH_STATUS_SUCCESS) {
183      fprintf (stderr, "error opening database: %d\n", stat);
184    }
185    path = talloc_asprintf (db, "%s/.notmuch/xapian/postlist.DB", argv[1]);
186    fd = open(path,O_WRONLY|O_TRUNC);
187    if (fd < 0)
188        fprintf (stderr, "error opening %s\n");
189 EOF
190 cat <<'EOF' > c_tail
191    if (stat) {
192        const char *stat_str = notmuch_database_status_string (db);
193        if (stat_str)
194            fputs (stat_str, stderr);
195     }
196
197 }
198 EOF
199
200 backup_database
201 test_begin_subtest "Xapian exception finding message"
202 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
203    {
204        notmuch_message_t *message = NULL;
205        stat = notmuch_database_find_message (db, "id:nonexistent", &message);
206    }
207 EOF
208 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
209 cat <<'EOF' >EXPECTED
210 == stdout ==
211 == stderr ==
212 A Xapian exception occurred finding message
213 EOF
214 test_expect_equal_file EXPECTED OUTPUT.clean
215 restore_database
216
217 backup_database
218 test_begin_subtest "Xapian exception getting tags"
219 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
220    {
221        notmuch_tags_t *tags = NULL;
222        tags = notmuch_database_get_all_tags (db);
223        stat = (tags == NULL);
224    }
225 EOF
226 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
227 cat <<'EOF' >EXPECTED
228 == stdout ==
229 == stderr ==
230 A Xapian exception occurred getting tags
231 EOF
232 test_expect_equal_file EXPECTED OUTPUT.clean
233 restore_database
234
235 backup_database
236 test_begin_subtest "Xapian exception creating directory"
237 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
238    {
239        notmuch_directory_t *directory = NULL;
240        stat = notmuch_database_get_directory (db, "none/existing", &directory);
241    }
242 EOF
243 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
244 cat <<'EOF' >EXPECTED
245 == stdout ==
246 == stderr ==
247 A Xapian exception occurred creating a directory
248 EOF
249 test_expect_equal_file EXPECTED OUTPUT.clean
250 restore_database
251
252 backup_database
253 test_begin_subtest "Xapian exception searching messages"
254 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
255    {
256        notmuch_messages_t *messages = NULL;
257        notmuch_query_t *query=notmuch_query_create (db, "*");
258        stat = notmuch_query_search_messages_st (query, &messages);
259    }
260 EOF
261 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
262 cat <<'EOF' >EXPECTED
263 == stdout ==
264 == stderr ==
265 A Xapian exception occurred performing query
266 Query string was: *
267 EOF
268 test_expect_equal_file EXPECTED OUTPUT.clean
269 restore_database
270
271 backup_database
272 test_begin_subtest "Xapian exception counting messages"
273 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
274    {
275        notmuch_query_t *query=notmuch_query_create (db, "id:87ocn0qh6d.fsf@yoom.home.cworth.org");
276        int count = notmuch_query_count_messages (query);
277        stat = (count == 0);
278    }
279 EOF
280 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
281 cat <<'EOF' >EXPECTED
282 == stdout ==
283 == stderr ==
284 A Xapian exception occurred performing query
285 Query string was: id:87ocn0qh6d.fsf@yoom.home.cworth.org
286 EOF
287 test_expect_equal_file EXPECTED OUTPUT.clean
288 restore_database
289
290 test_done