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