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