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