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