]> git.notmuchmail.org Git - notmuch/blob - test/T566-lib-message.sh
test: move notmuch_message_* tests to their own file
[notmuch] / test / T566-lib-message.sh
1 #!/usr/bin/env bash
2 test_description="API tests for notmuch_message_*"
3
4 . $(dirname "$0")/test-lib.sh || exit 1
5
6 add_email_corpus
7
8 test_begin_subtest "building database"
9 test_expect_success "NOTMUCH_NEW"
10
11 cat <<'EOF' > c_tail
12    if (stat) {
13        const char *stat_str = notmuch_database_status_string (db);
14        if (stat_str)
15            fputs (stat_str, stderr);
16     }
17
18 }
19 EOF
20
21 cat <<EOF > c_head
22 #include <stdio.h>
23 #include <notmuch.h>
24 #include <notmuch-test.h>
25 int main (int argc, char** argv)
26 {
27    notmuch_database_t *db;
28    notmuch_status_t stat;
29    char *msg = NULL;
30    notmuch_message_t *message = NULL;
31    const char *id = "1258471718-6781-1-git-send-email-dottedmag@dottedmag.net";
32
33    stat = notmuch_database_open_verbose (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db, &msg);
34    if (stat != NOTMUCH_STATUS_SUCCESS) {
35      fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
36      exit (1);
37    }
38    EXPECT0(notmuch_database_find_message (db, id, &message));
39    EXPECT0(notmuch_database_close (db));
40 EOF
41
42 test_begin_subtest "Handle getting message-id from closed database"
43 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
44     {
45         const char *id2;
46         id2=notmuch_message_get_message_id (message);
47         printf("%d\n%d\n", message != NULL, id2==NULL);
48     }
49 EOF
50 cat <<EOF > EXPECTED
51 == stdout ==
52 1
53 1
54 == stderr ==
55 EOF
56 test_expect_equal_file EXPECTED OUTPUT
57
58 test_begin_subtest "Handle getting thread-id from closed database"
59 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
60     {
61         const char *id2;
62         id2=notmuch_message_get_thread_id (message);
63         printf("%d\n%d\n", message != NULL, id2==NULL);
64     }
65 EOF
66 cat <<EOF > EXPECTED
67 == stdout ==
68 1
69 1
70 == stderr ==
71 EOF
72 test_expect_equal_file EXPECTED OUTPUT
73
74 test_begin_subtest "Handle getting header from closed database"
75 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
76     {
77         const char *from;
78         from=notmuch_message_get_header (message, "from");
79         printf("%s\n%d\n", id, from == NULL);
80     }
81 EOF
82 cat <<EOF > EXPECTED
83 == stdout ==
84 1258471718-6781-1-git-send-email-dottedmag@dottedmag.net
85 1
86 == stderr ==
87 EOF
88 test_expect_equal_file EXPECTED OUTPUT
89
90 # XXX TODO: test on a message from notmuch_thread_get_toplevel_messages
91 # XXX this test only tests the trivial code path
92 test_begin_subtest "Handle getting replies from closed database"
93 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
94     {
95         notmuch_messages_t *replies;
96         replies = notmuch_message_get_replies (message);
97         printf("%d\n%d\n", message != NULL, replies==NULL);
98     }
99 EOF
100 cat <<EOF > EXPECTED
101 == stdout ==
102 1
103 1
104 == stderr ==
105 EOF
106 test_expect_equal_file EXPECTED OUTPUT
107
108 test_begin_subtest "Handle getting message filename from closed database"
109 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
110     {
111         const char *filename;
112         filename = notmuch_message_get_filename (message);
113         printf("%d\n%d\n", message != NULL, filename == NULL);
114     }
115 EOF
116 cat <<EOF > EXPECTED
117 == stdout ==
118 1
119 1
120 == stderr ==
121 EOF
122 test_expect_equal_file EXPECTED OUTPUT
123
124 test_begin_subtest "Handle getting all message filenames from closed database"
125 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
126     {
127         notmuch_filenames_t *filenames;
128         filenames = notmuch_message_get_filenames (message);
129         printf("%d\n%d\n", message != NULL, filenames == NULL);
130     }
131 EOF
132 cat <<EOF > EXPECTED
133 == stdout ==
134 1
135 1
136 == stderr ==
137 EOF
138 test_expect_equal_file EXPECTED OUTPUT
139
140 test_begin_subtest "Handle getting ghost flag from closed database"
141 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
142     {
143         notmuch_bool_t result;
144         result = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_GHOST);
145         printf("%d\n%d\n", message != NULL, result == FALSE);
146     }
147 EOF
148 cat <<EOF > EXPECTED
149 == stdout ==
150 1
151 1
152 == stderr ==
153 EOF
154 test_expect_equal_file EXPECTED OUTPUT
155
156 test_begin_subtest "Handle getting date from closed database"
157 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
158     {
159         time_t result;
160         result = notmuch_message_get_date (message);
161         printf("%d\n%d\n", message != NULL, result == 0);
162     }
163 EOF
164 cat <<EOF > EXPECTED
165 == stdout ==
166 1
167 1
168 == stderr ==
169 EOF
170 test_expect_equal_file EXPECTED OUTPUT
171
172 test_begin_subtest "Handle getting tags from closed database"
173 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
174     {
175         notmuch_tags_t *result;
176         result = notmuch_message_get_tags (message);
177         printf("%d\n%d\n", message != NULL, result == NULL);
178     }
179 EOF
180 cat <<EOF > EXPECTED
181 == stdout ==
182 1
183 1
184 == stderr ==
185 EOF
186 test_expect_equal_file EXPECTED OUTPUT
187
188 test_begin_subtest "Handle counting files from closed database"
189 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
190     {
191         int result;
192         result = notmuch_message_count_files (message);
193         printf("%d\n%d\n", message != NULL, result < 0);
194     }
195 EOF
196 cat <<EOF > EXPECTED
197 == stdout ==
198 1
199 1
200 == stderr ==
201 EOF
202 test_expect_equal_file EXPECTED OUTPUT
203
204 test_begin_subtest "Handle adding tag with closed database"
205 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
206     {
207         notmuch_status_t status;
208         status = notmuch_message_add_tag (message, "boom");
209         printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
210     }
211 EOF
212 cat <<EOF > EXPECTED
213 == stdout ==
214 1
215 1
216 == stderr ==
217 EOF
218 test_expect_equal_file EXPECTED OUTPUT
219
220 test_begin_subtest "Handle removing tag with closed database"
221 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
222     {
223         notmuch_status_t status;
224         status = notmuch_message_remove_tag (message, "boom");
225         printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
226     }
227 EOF
228 cat <<EOF > EXPECTED
229 == stdout ==
230 1
231 1
232 == stderr ==
233 EOF
234 test_expect_equal_file EXPECTED OUTPUT
235
236 test_begin_subtest "Handle read maildir flag with closed database"
237 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
238     {
239         notmuch_bool_t is_set = -1;
240         is_set = notmuch_message_has_maildir_flag (message, 'S');
241         printf("%d\n%d\n", message != NULL, is_set == FALSE || is_set == TRUE);
242     }
243 EOF
244 cat <<EOF > EXPECTED
245 == stdout ==
246 1
247 1
248 == stderr ==
249 EOF
250 test_expect_equal_file EXPECTED OUTPUT
251
252 test_begin_subtest "Handle checking maildir flag with closed db (new API)"
253 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
254     {
255         notmuch_status_t status;
256         notmuch_bool_t out;
257         status = notmuch_message_has_maildir_flag_st (message, 'S', &out);
258         printf("%d\n%d\n", message != NULL,  status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
259     }
260 EOF
261 cat <<EOF > EXPECTED
262 == stdout ==
263 1
264 1
265 == stderr ==
266 EOF
267 test_expect_equal_file EXPECTED OUTPUT
268
269 test_begin_subtest "Handle converting maildir flags to tags with closed db"
270 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
271     {
272         notmuch_status_t status;
273         status = notmuch_message_maildir_flags_to_tags (message);
274         printf("%d\n%d\n", message != NULL,  status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
275     }
276 EOF
277 cat <<EOF > EXPECTED
278 == stdout ==
279 1
280 1
281 == stderr ==
282 EOF
283 test_expect_equal_file EXPECTED OUTPUT
284
285 test_begin_subtest "Handle removing all tags with closed db"
286 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
287     {
288         notmuch_status_t status;
289         status = notmuch_message_remove_all_tags (message);
290         printf("%d\n%d\n", message != NULL,  status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
291     }
292 EOF
293 cat <<EOF > EXPECTED
294 == stdout ==
295 1
296 1
297 == stderr ==
298 EOF
299 test_expect_equal_file EXPECTED OUTPUT
300
301 test_begin_subtest "Handle freezing message with closed db"
302 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
303     {
304         notmuch_status_t status;
305         status = notmuch_message_freeze (message);
306         printf("%d\n%d\n", message != NULL,  status == NOTMUCH_STATUS_SUCCESS);
307     }
308 EOF
309 cat <<EOF > EXPECTED
310 == stdout ==
311 1
312 1
313 == stderr ==
314 EOF
315 test_expect_equal_file EXPECTED OUTPUT
316
317 test_begin_subtest "Handle thawing message with closed db"
318 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
319     {
320         notmuch_status_t status;
321         status = notmuch_message_thaw (message);
322         printf("%d\n%d\n", message != NULL,  status == NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW);
323     }
324 EOF
325 cat <<EOF > EXPECTED
326 == stdout ==
327 1
328 1
329 == stderr ==
330 EOF
331 test_expect_equal_file EXPECTED OUTPUT
332
333 test_begin_subtest "Handle destroying message with closed db"
334 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
335     {
336         notmuch_message_destroy (message);
337         printf("%d\n%d\n", message != NULL,  1);
338     }
339 EOF
340 cat <<EOF > EXPECTED
341 == stdout ==
342 1
343 1
344 == stderr ==
345 EOF
346 test_expect_equal_file EXPECTED OUTPUT
347
348 test_begin_subtest "Handle retrieving closed db from message"
349 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
350     {
351         notmuch_database_t *db2;
352         db2 = notmuch_message_get_database (message);
353         printf("%d\n%d\n", message != NULL,  db == db2);
354     }
355 EOF
356 cat <<EOF > EXPECTED
357 == stdout ==
358 1
359 1
360 == stderr ==
361 EOF
362 test_expect_equal_file EXPECTED OUTPUT
363
364 test_begin_subtest "Handle reindexing message with closed db"
365 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
366     {
367         notmuch_status_t status;
368         status = notmuch_message_reindex (message, NULL);
369         printf("%d\n%d\n", message != NULL,  status == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
370     }
371 EOF
372 cat <<EOF > EXPECTED
373 == stdout ==
374 1
375 1
376 == stderr ==
377 EOF
378 test_expect_equal_file EXPECTED OUTPUT
379
380 test_done