]> git.notmuchmail.org Git - notmuch/blob - test/T560-lib-error.sh
test: remove unused regexp convenience variables
[notmuch] / test / T560-lib-error.sh
1 #!/usr/bin/env bash
2 test_description="error reporting for library"
3
4 . ./test-lib.sh || exit 1
5
6 add_email_corpus
7
8 test_expect_success "building database" "NOTMUCH_NEW"
9
10 test_begin_subtest "Open null pointer"
11 test_C <<'EOF'
12 #include <stdio.h>
13 #include <notmuch.h>
14 int main (int argc, char** argv)
15 {
16     notmuch_database_t *db;
17     notmuch_status_t stat;
18     stat = notmuch_database_open (NULL, 0, 0);
19 }
20 EOF
21 cat <<'EOF' >EXPECTED
22 == stdout ==
23 == stderr ==
24 Error: Cannot open a database for a NULL path.
25 EOF
26 test_expect_equal_file EXPECTED OUTPUT
27
28 test_begin_subtest "Open relative path"
29 test_C <<'EOF'
30 #include <stdio.h>
31 #include <notmuch.h>
32 int main (int argc, char** argv)
33 {
34     notmuch_database_t *db;
35     notmuch_status_t stat;
36     stat = notmuch_database_open ("./nonexistent/foo", 0, 0);
37 }
38 EOF
39 cat <<'EOF' >EXPECTED
40 == stdout ==
41 == stderr ==
42 Error: Database path must be absolute.
43 EOF
44 test_expect_equal_file EXPECTED OUTPUT
45
46 test_begin_subtest "Create database in relative path"
47 test_C <<'EOF'
48 #include <stdio.h>
49 #include <notmuch.h>
50 int main (int argc, char** argv)
51 {
52     notmuch_database_t *db;
53     notmuch_status_t stat;
54     stat = notmuch_database_create ("./nonexistent/foo", &db);
55 }
56 EOF
57 cat <<'EOF' >EXPECTED
58 == stdout ==
59 == stderr ==
60 Error: Database path must be absolute.
61 EOF
62 test_expect_equal_file EXPECTED OUTPUT
63
64 test_begin_subtest "Open nonexistent database"
65 test_C ${PWD}/nonexistent/foo <<'EOF'
66 #include <stdio.h>
67 #include <notmuch.h>
68 int main (int argc, char** argv)
69 {
70     notmuch_database_t *db;
71     notmuch_status_t stat;
72     stat = notmuch_database_open (argv[1], 0, 0);
73 }
74 EOF
75 cat <<'EOF' >EXPECTED
76 == stdout ==
77 == stderr ==
78 Error opening database at CWD/nonexistent/foo/.notmuch: No such file or directory
79 EOF
80 test_expect_equal_file EXPECTED OUTPUT
81
82 test_begin_subtest "create NULL path"
83 test_C <<'EOF'
84 #include <stdio.h>
85 #include <notmuch.h>
86 int main (int argc, char** argv)
87 {
88     notmuch_status_t stat;
89     stat = notmuch_database_create (NULL, NULL);
90 }
91 EOF
92 cat <<'EOF' >EXPECTED
93 == stdout ==
94 == stderr ==
95 Error: Cannot create a database for a NULL path.
96 EOF
97 test_expect_equal_file EXPECTED OUTPUT
98
99 test_begin_subtest "Create database in nonexistent directory"
100 test_C ${PWD}/nonexistent/foo<<'EOF'
101 #include <stdio.h>
102 #include <notmuch.h>
103 int main (int argc, char** argv)
104 {
105     notmuch_database_t *db;
106     notmuch_status_t stat;
107     stat = notmuch_database_create (argv[1], &db);
108 }
109 EOF
110 cat <<'EOF' >EXPECTED
111 == stdout ==
112 == stderr ==
113 Error: Cannot create database at CWD/nonexistent/foo: No such file or directory.
114 EOF
115 test_expect_equal_file EXPECTED OUTPUT
116
117 test_begin_subtest "Write to read-only database"
118 test_C ${MAIL_DIR} <<'EOF'
119 #include <stdio.h>
120 #include <notmuch.h>
121 int main (int argc, char** argv)
122 {
123    notmuch_database_t *db;
124    notmuch_status_t stat;
125    stat = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_ONLY, &db);
126    if (stat != NOTMUCH_STATUS_SUCCESS) {
127      fprintf (stderr, "error opening database: %d\n", stat);
128    }
129    stat = notmuch_database_add_message (db, "/dev/null", NULL);
130    if (stat)
131        fputs (notmuch_database_status_string (db), stderr);
132
133 }
134 EOF
135 cat <<'EOF' >EXPECTED
136 == stdout ==
137 == stderr ==
138 Cannot write to a read-only database.
139 EOF
140 test_expect_equal_file EXPECTED OUTPUT
141
142 test_begin_subtest "Add non-existent file"
143 test_C ${MAIL_DIR} <<'EOF'
144 #include <stdio.h>
145 #include <notmuch.h>
146 int main (int argc, char** argv)
147 {
148    notmuch_database_t *db;
149    notmuch_status_t stat;
150    stat = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db);
151    if (stat != NOTMUCH_STATUS_SUCCESS) {
152      fprintf (stderr, "error opening database: %d\n", stat);
153    }
154    stat = notmuch_database_add_message (db, "./nonexistent", NULL);
155    if (stat) {
156        char *status_string = notmuch_database_status_string (db);
157        if (status_string) fputs (status_string, stderr);
158    }
159 }
160 EOF
161 cat <<'EOF' >EXPECTED
162 == stdout ==
163 == stderr ==
164 Error opening ./nonexistent: No such file or directory
165 EOF
166 test_expect_equal_file EXPECTED OUTPUT
167
168 test_begin_subtest "compact, overwriting existing backup"
169 test_C ${MAIL_DIR} <<'EOF'
170 #include <stdio.h>
171 #include <notmuch.h>
172 static void
173 status_cb (const char *msg, void *closure)
174 {
175     printf ("%s\n", msg);
176 }
177 int main (int argc, char** argv)
178 {
179    notmuch_database_t *db;
180    notmuch_status_t stat;
181    stat = notmuch_database_compact (argv[1], argv[1], status_cb, NULL);
182 }
183 EOF
184 cat <<'EOF' >EXPECTED
185 == stdout ==
186 Path already exists: MAIL_DIR
187
188 == stderr ==
189 EOF
190 test_expect_equal_file EXPECTED OUTPUT
191
192 cat <<EOF > c_head
193 #include <stdio.h>
194 #include <sys/types.h>
195 #include <sys/stat.h>
196 #include <fcntl.h>
197 #include <talloc.h>
198 #include <notmuch.h>
199
200 int main (int argc, char** argv)
201 {
202    notmuch_database_t *db;
203    notmuch_status_t stat;
204    char *path;
205    char *msg = NULL;
206    int fd;
207
208    stat = notmuch_database_open_verbose (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db, &msg);
209    if (stat != NOTMUCH_STATUS_SUCCESS) {
210      fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
211      exit (1);
212    }
213    path = talloc_asprintf (db, "%s/.notmuch/xapian/postlist.${db_ending}", argv[1]);
214    fd = open(path,O_WRONLY|O_TRUNC);
215    if (fd < 0) {
216        fprintf (stderr, "error opening %s\n", argv[1]);
217        exit (1);
218    }
219 EOF
220 cat <<'EOF' > c_tail
221    if (stat) {
222        const char *stat_str = notmuch_database_status_string (db);
223        if (stat_str)
224            fputs (stat_str, stderr);
225     }
226
227 }
228 EOF
229
230 backup_database
231 test_begin_subtest "Xapian exception finding message"
232 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
233    {
234        notmuch_message_t *message = NULL;
235        stat = notmuch_database_find_message (db, "id:nonexistent", &message);
236    }
237 EOF
238 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
239 cat <<'EOF' >EXPECTED
240 == stdout ==
241 == stderr ==
242 A Xapian exception occurred finding message
243 EOF
244 test_expect_equal_file EXPECTED OUTPUT.clean
245 restore_database
246
247 backup_database
248 test_begin_subtest "Xapian exception getting tags"
249 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
250    {
251        notmuch_tags_t *tags = NULL;
252        tags = notmuch_database_get_all_tags (db);
253        stat = (tags == NULL);
254    }
255 EOF
256 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
257 cat <<'EOF' >EXPECTED
258 == stdout ==
259 == stderr ==
260 A Xapian exception occurred getting tags
261 EOF
262 test_expect_equal_file EXPECTED OUTPUT.clean
263 restore_database
264
265 backup_database
266 test_begin_subtest "Xapian exception creating directory"
267 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
268    {
269        notmuch_directory_t *directory = NULL;
270        stat = notmuch_database_get_directory (db, "none/existing", &directory);
271    }
272 EOF
273 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
274 cat <<'EOF' >EXPECTED
275 == stdout ==
276 == stderr ==
277 A Xapian exception occurred creating a directory
278 EOF
279 test_expect_equal_file EXPECTED OUTPUT.clean
280 restore_database
281
282 backup_database
283 test_begin_subtest "Xapian exception searching messages"
284 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
285    {
286        notmuch_messages_t *messages = NULL;
287        notmuch_query_t *query=notmuch_query_create (db, "*");
288        stat = notmuch_query_search_messages_st (query, &messages);
289    }
290 EOF
291 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
292 cat <<'EOF' >EXPECTED
293 == stdout ==
294 == stderr ==
295 A Xapian exception occurred performing query
296 Query string was: *
297 EOF
298 test_expect_equal_file EXPECTED OUTPUT.clean
299 restore_database
300
301 backup_database
302 test_begin_subtest "Xapian exception counting messages"
303 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
304    {
305        notmuch_query_t *query=notmuch_query_create (db, "id:87ocn0qh6d.fsf@yoom.home.cworth.org");
306        int count = notmuch_query_count_messages (query);
307        stat = (count == 0);
308    }
309 EOF
310 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
311 cat <<'EOF' >EXPECTED
312 == stdout ==
313 == stderr ==
314 A Xapian exception occurred performing query
315 Query string was: id:87ocn0qh6d.fsf@yoom.home.cworth.org
316 EOF
317 test_expect_equal_file EXPECTED OUTPUT.clean
318 restore_database
319
320 test_done