]> git.cworth.org Git - notmuch/blob - test/T590-libconfig.sh
Merge tag 'debian/0.34.2-1'
[notmuch] / test / T590-libconfig.sh
1 #!/usr/bin/env bash
2 test_description="library config API"
3
4 . $(dirname "$0")/test-lib.sh || exit 1
5
6 add_email_corpus
7
8 _libconfig_sanitize() {
9     ${NOTMUCH_PYTHON} /dev/fd/3 3<<'EOF'
10 import os, sys, pwd, socket
11
12 pw = pwd.getpwuid(os.getuid())
13 user = pw.pw_name
14 name = pw.pw_gecos.partition(",")[0]
15
16 for l in sys.stdin:
17     if l[:4] == "08: ":
18         l = l.replace(user, "USERNAME", 1)
19     elif l[:4] == "10: ":
20         l = l.replace("'" + name, "'USER_FULL_NAME", 1)
21     sys.stdout.write(l)
22 EOF
23 }
24
25 cat <<EOF > c_head
26 #include <notmuch-test.h>
27
28 int main (int argc, char** argv)
29 {
30    notmuch_database_t *db;
31    char *val;
32    notmuch_status_t stat;
33    char *msg = NULL;
34
35    for (int i = 1; i < argc; i++)
36       if (strcmp (argv[i], "%NULL%") == 0) argv[i] = NULL;
37
38    stat = notmuch_database_open_with_config (argv[1],
39                                               NOTMUCH_DATABASE_MODE_READ_WRITE,
40                                               argv[2],
41                                               argv[3],
42                                               &db,
43                                               &msg);
44    if (stat != NOTMUCH_STATUS_SUCCESS) {
45      fprintf (stderr, "error opening database\n%s\n%s\n", notmuch_status_to_string (stat), msg ? msg : "");
46      exit (1);
47    }
48 EOF
49
50 cat <<EOF > c_tail
51    EXPECT0(notmuch_database_destroy(db));
52 }
53 EOF
54
55 test_begin_subtest "notmuch_database_{set,get}_config"
56 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG}
57 {
58    EXPECT0(notmuch_database_set_config (db, "test.key1", "testvalue1"));
59    EXPECT0(notmuch_database_set_config (db, "test.key2", "testvalue2"));
60    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
61    printf("test.key1 = %s\n", val);
62    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
63    printf("test.key2 = %s\n", val);
64 }
65 EOF
66 cat <<'EOF' >EXPECTED
67 == stdout ==
68 test.key1 = testvalue1
69 test.key2 = testvalue2
70 == stderr ==
71 EOF
72 test_expect_equal_file EXPECTED OUTPUT
73
74
75 test_begin_subtest "notmuch_database_get_config_list: empty list"
76 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
77 {
78    notmuch_config_list_t *list;
79    EXPECT0(notmuch_database_get_config_list (db, "nonexistent", &list));
80    printf("valid = %d\n", notmuch_config_list_valid (list));
81    notmuch_config_list_destroy (list);
82 }
83 EOF
84 cat <<'EOF' >EXPECTED
85 == stdout ==
86 valid = 0
87 == stderr ==
88 EOF
89 test_expect_equal_file EXPECTED OUTPUT
90
91 test_begin_subtest "notmuch_database_get_config_list: closed db"
92 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
93 {
94    notmuch_config_list_t *list;
95    EXPECT0(notmuch_database_close (db));
96    stat = notmuch_database_get_config_list (db, "nonexistent", &list);
97    printf("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION);
98 }
99 EOF
100 cat <<'EOF' >EXPECTED
101 == stdout ==
102 1
103 == stderr ==
104 EOF
105 test_expect_equal_file EXPECTED OUTPUT
106
107 test_begin_subtest "notmuch_database_get_config_list: all pairs"
108 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
109 {
110    notmuch_config_list_t *list;
111    EXPECT0(notmuch_database_set_config (db, "zzzafter", "afterval"));
112    EXPECT0(notmuch_database_set_config (db, "aaabefore", "beforeval"));
113    EXPECT0(notmuch_database_get_config_list (db, "", &list));
114    for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
115       printf("%s %s\n", notmuch_config_list_key (list), notmuch_config_list_value(list));
116    }
117    notmuch_config_list_destroy (list);
118 }
119 EOF
120 cat <<'EOF' >EXPECTED
121 == stdout ==
122 aaabefore beforeval
123 test.key1 testvalue1
124 test.key2 testvalue2
125 zzzafter afterval
126 == stderr ==
127 EOF
128 test_expect_equal_file EXPECTED OUTPUT
129
130 test_begin_subtest "notmuch_database_get_config_list: all pairs (closed db)"
131 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
132 {
133    notmuch_config_list_t *list;
134    EXPECT0(notmuch_database_get_config_list (db, "", &list));
135    EXPECT0(notmuch_database_close (db));
136    for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
137       printf("%s %d\n", notmuch_config_list_key (list), NULL == notmuch_config_list_value(list));
138    }
139    notmuch_config_list_destroy (list);
140 }
141 EOF
142 cat <<'EOF' >EXPECTED
143 == stdout ==
144 aaabefore 1
145 test.key1 1
146 test.key2 1
147 zzzafter 1
148 == stderr ==
149 EOF
150 test_expect_equal_file EXPECTED OUTPUT
151
152 test_begin_subtest "notmuch_database_get_config_list: one prefix"
153 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
154 {
155    notmuch_config_list_t *list;
156    EXPECT0(notmuch_database_get_config_list (db, "test.key", &list));
157    for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
158       printf("%s %s\n", notmuch_config_list_key (list), notmuch_config_list_value(list));
159    }
160    notmuch_config_list_destroy (list);
161 }
162 EOF
163 cat <<'EOF' >EXPECTED
164 == stdout ==
165 test.key1 testvalue1
166 test.key2 testvalue2
167 == stderr ==
168 EOF
169 test_expect_equal_file EXPECTED OUTPUT
170
171 test_begin_subtest "dump config"
172 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
173 {
174     EXPECT0(notmuch_database_set_config (db, "key with spaces", "value, with, spaces!"));
175 }
176 EOF
177 notmuch dump --include=config >OUTPUT
178 cat <<'EOF' >EXPECTED
179 #notmuch-dump batch-tag:3 config
180 #@ aaabefore beforeval
181 #@ key%20with%20spaces value,%20with,%20spaces%21
182 #@ test.key1 testvalue1
183 #@ test.key2 testvalue2
184 #@ zzzafter afterval
185 EOF
186 test_expect_equal_file EXPECTED OUTPUT
187
188 test_begin_subtest "restore config"
189 notmuch dump --include=config >EXPECTED
190 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
191 {
192     EXPECT0(notmuch_database_set_config (db, "test.key1", "mutatedvalue"));
193 }
194 EOF
195 notmuch restore --include=config <EXPECTED
196 notmuch dump --include=config >OUTPUT
197 test_expect_equal_file EXPECTED OUTPUT
198
199 backup_database
200 test_begin_subtest "override config from file"
201 notmuch config set test.key1 overridden
202 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG}
203 {
204    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
205    printf("test.key1 = %s\n", val);
206    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
207    printf("test.key2 = %s\n", val);
208 }
209 EOF
210 cat <<'EOF' >EXPECTED
211 == stdout ==
212 test.key1 = overridden
213 test.key2 = testvalue2
214 == stderr ==
215 EOF
216 test_expect_equal_file EXPECTED OUTPUT
217 restore_database
218
219 test_begin_subtest "NOTMUCH_CONFIG_HOOK_DIR: traditional"
220 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
221 {
222    const char *val = notmuch_config_get (db, NOTMUCH_CONFIG_HOOK_DIR);
223    printf("database.hook_dir = %s\n", val);
224 }
225 EOF
226 cat <<'EOF' >EXPECTED
227 == stdout ==
228 database.hook_dir = MAIL_DIR/.notmuch/hooks
229 == stderr ==
230 EOF
231 test_expect_equal_file EXPECTED OUTPUT
232
233 test_begin_subtest "NOTMUCH_CONFIG_HOOK_DIR: xdg"
234 dir="${HOME}/.config/notmuch/default/hooks"
235 mkdir -p $dir
236 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
237 {
238    const char *val = notmuch_config_get (db, NOTMUCH_CONFIG_HOOK_DIR);
239    printf("database.hook_dir = %s\n", val);
240 }
241 EOF
242 cat <<'EOF' >EXPECTED
243 == stdout ==
244 database.hook_dir = CWD/home/.config/notmuch/default/hooks
245 == stderr ==
246 EOF
247 rmdir $dir
248 test_expect_equal_file EXPECTED OUTPUT
249
250 test_begin_subtest "notmuch_config_get_values"
251 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
252 {
253     notmuch_config_values_t *values;
254     EXPECT0(notmuch_config_set (db, NOTMUCH_CONFIG_NEW_TAGS, "a;b;c"));
255     for (values = notmuch_config_get_values (db, NOTMUCH_CONFIG_NEW_TAGS);
256          notmuch_config_values_valid (values);
257          notmuch_config_values_move_to_next (values))
258     {
259           puts (notmuch_config_values_get (values));
260     }
261 }
262 EOF
263 cat <<'EOF' >EXPECTED
264 == stdout ==
265 a
266 b
267 c
268 == stderr ==
269 EOF
270 test_expect_equal_file EXPECTED OUTPUT
271 restore_database
272
273 test_begin_subtest "notmuch_config_get_values (ignore leading/trailing whitespace)"
274 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
275 {
276     notmuch_config_values_t *values;
277     EXPECT0(notmuch_config_set (db, NOTMUCH_CONFIG_NEW_TAGS, " a ; b c ; d "));
278     for (values = notmuch_config_get_values (db, NOTMUCH_CONFIG_NEW_TAGS);
279          notmuch_config_values_valid (values);
280          notmuch_config_values_move_to_next (values))
281     {
282           puts (notmuch_config_values_get (values));
283     }
284 }
285 EOF
286 cat <<'EOF' >EXPECTED
287 == stdout ==
288 a
289 b c
290 d
291 == stderr ==
292 EOF
293 test_expect_equal_file EXPECTED OUTPUT
294 restore_database
295
296 test_begin_subtest "notmuch_config_get_values_string"
297 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
298 {
299     notmuch_config_values_t *values;
300     EXPECT0(notmuch_database_set_config (db, "test.list", "x;y;z"));
301     for (values = notmuch_config_get_values_string (db, "test.list");
302          notmuch_config_values_valid (values);
303          notmuch_config_values_move_to_next (values))
304     {
305           puts (notmuch_config_values_get (values));
306     }
307 }
308 EOF
309 cat <<'EOF' >EXPECTED
310 == stdout ==
311 x
312 y
313 z
314 == stderr ==
315 EOF
316 test_expect_equal_file EXPECTED OUTPUT
317 restore_database
318
319 test_begin_subtest "notmuch_config_get_values (restart)"
320 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
321 {
322     notmuch_config_values_t *values;
323     EXPECT0(notmuch_config_set (db, NOTMUCH_CONFIG_NEW_TAGS, "a;b;c"));
324     for (values = notmuch_config_get_values (db, NOTMUCH_CONFIG_NEW_TAGS);
325          notmuch_config_values_valid (values);
326          notmuch_config_values_move_to_next (values))
327     {
328           puts (notmuch_config_values_get (values));
329     }
330     for (notmuch_config_values_start (values);
331          notmuch_config_values_valid (values);
332          notmuch_config_values_move_to_next (values))
333     {
334           puts (notmuch_config_values_get (values));
335     }
336 }
337 EOF
338 cat <<'EOF' >EXPECTED
339 == stdout ==
340 a
341 b
342 c
343 a
344 b
345 c
346 == stderr ==
347 EOF
348 test_expect_equal_file EXPECTED OUTPUT
349 restore_database
350
351 backup_database
352 test_begin_subtest "notmuch_config_get_values, trailing ;"
353 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
354 {
355     notmuch_config_values_t *values;
356     EXPECT0(notmuch_config_set (db, NOTMUCH_CONFIG_NEW_TAGS, "a;b;c"));
357     for (values = notmuch_config_get_values (db, NOTMUCH_CONFIG_NEW_TAGS);
358          notmuch_config_values_valid (values);
359          notmuch_config_values_move_to_next (values))
360     {
361           puts (notmuch_config_values_get (values));
362     }
363 }
364 EOF
365 cat <<'EOF' >EXPECTED
366 == stdout ==
367 a
368 b
369 c
370 == stderr ==
371 EOF
372 test_expect_equal_file EXPECTED OUTPUT
373 restore_database
374
375 backup_database
376 test_begin_subtest "get config by key"
377 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG}
378 {
379    printf("before = %s\n", notmuch_config_get (db, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS));
380    EXPECT0(notmuch_database_set_config (db, "maildir.synchronize_flags", "false"));
381    printf("after = %s\n", notmuch_config_get (db, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS));
382 }
383 EOF
384 cat <<'EOF' >EXPECTED
385 == stdout ==
386 before = true
387 after = false
388 == stderr ==
389 EOF
390 test_expect_equal_file EXPECTED OUTPUT
391 restore_database
392
393 backup_database
394 test_begin_subtest "set config by key"
395 notmuch config set test.key1 overridden
396 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG}
397 {
398    printf("before = %s\n", notmuch_config_get (db, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS));
399    EXPECT0(notmuch_config_set (db, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS, "false"));
400    printf("after = %s\n", notmuch_config_get (db, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS));
401 }
402 EOF
403 cat <<'EOF' >EXPECTED
404 == stdout ==
405 before = true
406 after = false
407 == stderr ==
408 EOF
409 test_expect_equal_file EXPECTED OUTPUT
410 restore_database
411
412 test_begin_subtest "load default values"
413 export MAILDIR=${MAIL_DIR}
414 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} '' %NULL%
415 {
416     notmuch_config_key_t key;
417     for (key = NOTMUCH_CONFIG_FIRST;
418          key < NOTMUCH_CONFIG_LAST;
419          key = (notmuch_config_key_t)(key + 1)) {
420         const char *val = notmuch_config_get (db, key);
421         printf("%02d: '%s'\n", key, val ? val : "NULL" );
422     }
423 }
424 EOF
425
426 _libconfig_sanitize < OUTPUT > OUTPUT.clean
427
428 cat <<'EOF' >EXPECTED
429 == stdout ==
430 00: 'MAIL_DIR'
431 01: 'MAIL_DIR'
432 02: 'MAIL_DIR/.notmuch/hooks'
433 03: 'MAIL_DIR/.notmuch/backups'
434 04: ''
435 05: 'unread;inbox'
436 06: ''
437 07: 'true'
438 08: 'USERNAME@localhost'
439 09: 'NULL'
440 10: 'USER_FULL_NAME'
441 11: '8000'
442 == stderr ==
443 EOF
444 unset MAILDIR
445 test_expect_equal_file EXPECTED OUTPUT.clean
446
447 backup_database
448 test_begin_subtest "override config from \${NOTMUCH_CONFIG}"
449 notmuch config set test.key1 overridden
450 # second argument omitted to make argv[2] == NULL
451 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
452 {
453    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
454    printf("test.key1 = %s\n", val);
455    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
456    printf("test.key2 = %s\n", val);
457 }
458 EOF
459 notmuch config set test.key1
460 cat <<'EOF' >EXPECTED
461 == stdout ==
462 test.key1 = overridden
463 test.key2 = testvalue2
464 == stderr ==
465 EOF
466 test_expect_equal_file EXPECTED OUTPUT
467 restore_database
468
469 backup_database
470 test_begin_subtest "override config from \${HOME}/.notmuch-config"
471 ovconfig=${HOME}/.notmuch-config
472 cp ${NOTMUCH_CONFIG} ${ovconfig}
473 old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
474 unset NOTMUCH_CONFIG
475 notmuch --config=${ovconfig} config set test.key1 overridden-home
476 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} %NULL% %NULL%
477 {
478    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
479    printf("test.key1 = %s\n", val);
480    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
481    printf("test.key2 = %s\n", val);
482 }
483 EOF
484 rm -f ${ovconfig}
485 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
486 cat <<'EOF' >EXPECTED
487 == stdout ==
488 test.key1 = overridden-home
489 test.key2 = testvalue2
490 == stderr ==
491 EOF
492 test_expect_equal_file EXPECTED OUTPUT
493 restore_database
494
495 backup_database
496 test_begin_subtest "override config from \${XDG_CONFIG_HOME}/notmuch"
497 ovconfig=${HOME}/.config/notmuch/default/config
498 mkdir -p $(dirname ${ovconfig})
499 cp ${NOTMUCH_CONFIG} ${ovconfig}
500 old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
501 unset NOTMUCH_CONFIG
502 notmuch --config=${ovconfig} config set test.key1 overridden-xdg
503 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} %NULL% %NULL%
504 {
505    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
506    printf("test.key1 = %s\n", val);
507    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
508    printf("test.key2 = %s\n", val);
509 }
510 EOF
511 rm -f ${ovconfig}
512 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
513 cat <<'EOF' >EXPECTED
514 == stdout ==
515 test.key1 = overridden-xdg
516 test.key2 = testvalue2
517 == stderr ==
518 EOF
519 test_expect_equal_file EXPECTED OUTPUT
520 restore_database
521
522 backup_database
523 test_begin_subtest "override config from \${XDG_CONFIG_HOME}/notmuch with profile"
524 ovconfig=${HOME}/.config/notmuch/work/config
525 mkdir -p $(dirname ${ovconfig})
526 cp ${NOTMUCH_CONFIG} ${ovconfig}
527 old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
528 unset NOTMUCH_CONFIG
529 notmuch --config=${ovconfig} config set test.key1 overridden-xdg-profile
530 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} %NULL% work
531 {
532    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
533    printf("test.key1 = %s\n", val);
534    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
535    printf("test.key2 = %s\n", val);
536 }
537 EOF
538 rm -f ${ovconfig}
539 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
540 cat <<'EOF' >EXPECTED
541 == stdout ==
542 test.key1 = overridden-xdg-profile
543 test.key2 = testvalue2
544 == stderr ==
545 EOF
546 test_expect_equal_file EXPECTED OUTPUT
547 restore_database
548
549 backup_database
550 test_begin_subtest "override config from \${HOME}/.notmuch-config.work (via args)"
551 ovconfig=${HOME}/.notmuch-config.work
552 cp ${NOTMUCH_CONFIG} ${ovconfig}
553 old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
554 unset NOTMUCH_CONFIG
555 notmuch --config=${ovconfig} config set test.key1 overridden-profile
556 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} %NULL% work
557 {
558    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
559    printf("test.key1 = %s\n", val);
560    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
561    printf("test.key2 = %s\n", val);
562 }
563 EOF
564 #rm -f ${ovconfig}
565 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
566 cat <<'EOF' >EXPECTED
567 == stdout ==
568 test.key1 = overridden-profile
569 test.key2 = testvalue2
570 == stderr ==
571 EOF
572 test_expect_equal_file EXPECTED OUTPUT
573 restore_database
574
575 test_begin_subtest "no config, fail to open database"
576 old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
577 unset NOTMUCH_CONFIG
578 cat c_head - c_tail <<'EOF' | test_C %NULL% '' %NULL%
579 {
580    printf("NOT RUN");
581 }
582 EOF
583 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
584 cat <<'EOF' >EXPECTED
585 == stdout ==
586 == stderr ==
587 error opening database
588 No database found
589 Error: could not locate database.
590
591 EOF
592 test_expect_equal_file EXPECTED OUTPUT
593
594 test_begin_subtest "open database from NOTMUCH_DATABASE"
595 old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
596 unset NOTMUCH_CONFIG
597 export NOTMUCH_DATABASE=${MAIL_DIR}
598 cat c_head - c_tail <<'EOF' | test_C %NULL% '' %NULL%
599 {
600    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
601    printf("test.key1 = %s\n", val);
602    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
603    printf("test.key2 = %s\n", val);
604 }
605 EOF
606 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
607 unset NOTMUCH_DATABASE
608 cat <<'EOF' >EXPECTED
609 == stdout ==
610 test.key1 = testvalue1
611 test.key2 = testvalue2
612 == stderr ==
613 EOF
614 test_expect_equal_file EXPECTED OUTPUT
615
616 test_begin_subtest "NOTMUCH_DATABASE overrides config"
617 cp notmuch-config notmuch-config.bak
618 notmuch config set database.path /nonexistent
619 export NOTMUCH_DATABASE=${MAIL_DIR}
620 cat c_head - c_tail <<'EOF' | test_C %NULL% '' %NULL%
621 {
622    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
623    printf("test.key1 = %s\n", val);
624    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
625    printf("test.key2 = %s\n", val);
626 }
627 EOF
628 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
629 unset NOTMUCH_DATABASE
630 cat <<'EOF' >EXPECTED
631 == stdout ==
632 test.key1 = testvalue1
633 test.key2 = testvalue2
634 == stderr ==
635 EOF
636 cp notmuch-config.bak notmuch-config
637 test_expect_equal_file EXPECTED OUTPUT
638
639 cat <<EOF > c_head2
640 #include <notmuch-test.h>
641
642 int main (int argc, char** argv)
643 {
644    notmuch_database_t *db;
645    char *val;
646    notmuch_status_t stat;
647    char *msg = NULL;
648
649    for (int i = 1; i < argc; i++)
650       if (strcmp (argv[i], "%NULL%") == 0) argv[i] = NULL;
651
652    stat = notmuch_database_load_config (argv[1],
653                                         argv[2],
654                                         argv[3],
655                                         &db,
656                                         &msg);
657    if (stat != NOTMUCH_STATUS_SUCCESS  && stat != NOTMUCH_STATUS_NO_CONFIG) {
658      fprintf (stderr, "error opening database\n%d: %s\n%s\n", stat,
659               notmuch_status_to_string (stat), msg ? msg : "");
660      exit (1);
661    }
662 EOF
663
664
665 test_begin_subtest "notmuch_database_get_config (ndlc)"
666 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR} %NULL% %NULL%
667 {
668    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
669    printf("test.key1 = %s\n", val);
670    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
671    printf("test.key2 = %s\n", val);
672 }
673 EOF
674 cat <<'EOF' >EXPECTED
675 == stdout ==
676 test.key1 = testvalue1
677 test.key2 = testvalue2
678 == stderr ==
679 EOF
680 test_expect_equal_file EXPECTED OUTPUT
681
682
683 test_begin_subtest "notmuch_database_get_config_list: all pairs (ndlc)"
684 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
685 {
686    notmuch_config_list_t *list;
687    EXPECT0(notmuch_database_get_config_list (db, "", &list));
688    for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
689       printf("%s %s\n", notmuch_config_list_key (list), notmuch_config_list_value(list));
690    }
691    notmuch_config_list_destroy (list);
692 }
693 EOF
694 cat <<'EOF' >EXPECTED
695 == stdout ==
696 aaabefore beforeval
697 key with spaces value, with, spaces!
698 test.key1 testvalue1
699 test.key2 testvalue2
700 zzzafter afterval
701 == stderr ==
702 EOF
703 test_expect_equal_file EXPECTED OUTPUT
704
705 test_begin_subtest "notmuch_database_get_config_list: one prefix (ndlc)"
706 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
707 {
708    notmuch_config_list_t *list;
709    EXPECT0(notmuch_database_get_config_list (db, "test.key", &list));
710    for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
711       printf("%s %s\n", notmuch_config_list_key (list), notmuch_config_list_value(list));
712    }
713    notmuch_config_list_destroy (list);
714 }
715 EOF
716 cat <<'EOF' >EXPECTED
717 == stdout ==
718 test.key1 testvalue1
719 test.key2 testvalue2
720 == stderr ==
721 EOF
722 test_expect_equal_file EXPECTED OUTPUT
723
724 test_begin_subtest "list by keys (ndlc)"
725 notmuch config set search.exclude_tags "foo;bar;fub"
726 notmuch config set new.ignore "sekrit_junk"
727 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR} %NULL% %NULL%
728 {
729     notmuch_config_key_t key;
730     for (key = NOTMUCH_CONFIG_FIRST;
731          key < NOTMUCH_CONFIG_LAST;
732          key = (notmuch_config_key_t)(key + 1)) {
733         const char *val = notmuch_config_get (db, key);
734         printf("%02d: '%s'\n", key, val ? val : "NULL" );
735     }
736 }
737 EOF
738 cat <<'EOF' >EXPECTED
739 == stdout ==
740 00: 'MAIL_DIR'
741 01: 'MAIL_DIR'
742 02: 'MAIL_DIR/.notmuch/hooks'
743 03: 'MAIL_DIR/.notmuch/backups'
744 04: 'foo;bar;fub'
745 05: 'unread;inbox'
746 06: 'sekrit_junk'
747 07: 'true'
748 08: 'test_suite@notmuchmail.org'
749 09: 'test_suite_other@notmuchmail.org;test_suite@otherdomain.org'
750 10: 'Notmuch Test Suite'
751 11: '8000'
752 == stderr ==
753 EOF
754 test_expect_equal_file EXPECTED OUTPUT
755
756 test_begin_subtest "load default values (ndlc, nonexistent config)"
757 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR} /nonexistent %NULL%
758 {
759     notmuch_config_key_t key;
760     for (key = NOTMUCH_CONFIG_FIRST;
761          key < NOTMUCH_CONFIG_LAST;
762          key = (notmuch_config_key_t)(key + 1)) {
763         const char *val = notmuch_config_get (db, key);
764         printf("%02d: '%s'\n", key, val ? val : "NULL" );
765     }
766 }
767 EOF
768
769 _libconfig_sanitize < OUTPUT > OUTPUT.clean
770
771 cat <<'EOF' >EXPECTED
772 == stdout ==
773 00: 'MAIL_DIR'
774 01: 'MAIL_DIR'
775 02: 'MAIL_DIR/.notmuch/hooks'
776 03: 'MAIL_DIR/.notmuch/backups'
777 04: ''
778 05: 'unread;inbox'
779 06: ''
780 07: 'true'
781 08: 'USERNAME@localhost'
782 09: 'NULL'
783 10: 'USER_FULL_NAME'
784 11: '8000'
785 == stderr ==
786 EOF
787 test_expect_equal_file EXPECTED OUTPUT.clean
788
789 backup_database
790 test_begin_subtest "override config from \${HOME}/.notmuch-config (ndlc)"
791 ovconfig=${HOME}/.notmuch-config
792 cp ${NOTMUCH_CONFIG} ${ovconfig}
793 old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
794 unset NOTMUCH_CONFIG
795 notmuch --config=${ovconfig} config set test.key1 overridden-home
796 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR} %NULL% %NULL%
797 {
798    EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
799    printf("test.key1 = %s\n", val);
800    EXPECT0(notmuch_database_get_config (db, "test.key2", &val));
801    printf("test.key2 = %s\n", val);
802 }
803 EOF
804 rm -f ${ovconfig}
805 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
806 cat <<'EOF' >EXPECTED
807 == stdout ==
808 test.key1 = overridden-home
809 test.key2 = testvalue2
810 == stderr ==
811 EOF
812 test_expect_equal_file EXPECTED OUTPUT
813 restore_database
814
815 test_begin_subtest "notmuch_config_get_pairs: prefix (ndlc)"
816 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
817 {
818    notmuch_config_pairs_t *list;
819    for (list =  notmuch_config_get_pairs (db, "user.");
820         notmuch_config_pairs_valid (list);
821         notmuch_config_pairs_move_to_next (list)) {
822      printf("%s %s\n", notmuch_config_pairs_key (list), notmuch_config_pairs_value(list));
823    }
824    notmuch_config_pairs_destroy (list);
825 }
826 EOF
827 cat <<'EOF' >EXPECTED
828 == stdout ==
829 user.name Notmuch Test Suite
830 user.other_email test_suite_other@notmuchmail.org;test_suite@otherdomain.org
831 user.primary_email test_suite@notmuchmail.org
832 == stderr ==
833 EOF
834 test_expect_equal_file EXPECTED OUTPUT
835
836 test_begin_subtest "notmuch_config_get_pairs: all pairs (ndlc)"
837 cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL%
838 {
839    notmuch_config_pairs_t *list;
840    for (list =  notmuch_config_get_pairs (db, "");
841         notmuch_config_pairs_valid (list);
842         notmuch_config_pairs_move_to_next (list)) {
843      printf("%s %s\n", notmuch_config_pairs_key (list), notmuch_config_pairs_value(list));
844    }
845    notmuch_config_pairs_destroy (list);
846 }
847 EOF
848 cat <<'EOF' >EXPECTED
849 == stdout ==
850 aaabefore beforeval
851 database.autocommit 8000
852 database.backup_dir MAIL_DIR/.notmuch/backups
853 database.hook_dir MAIL_DIR/.notmuch/hooks
854 database.mail_root MAIL_DIR
855 database.path MAIL_DIR
856 key with spaces value, with, spaces!
857 maildir.synchronize_flags true
858 new.ignore sekrit_junk
859 new.tags unread;inbox
860 search.exclude_tags foo;bar;fub
861 test.key1 testvalue1
862 test.key2 testvalue2
863 user.name Notmuch Test Suite
864 user.other_email test_suite_other@notmuchmail.org;test_suite@otherdomain.org
865 user.primary_email test_suite@notmuchmail.org
866 zzzafter afterval
867 == stderr ==
868 EOF
869 test_expect_equal_file EXPECTED OUTPUT
870
871 cat <<EOF > c_head3
872 #include <notmuch-test.h>
873 int main (int argc, char **argv) {
874   notmuch_status_t stat;
875   notmuch_database_t *db = NULL;
876 EOF
877
878 cat <<EOF > c_tail3
879   printf("db == NULL: %d\n", db == NULL);
880 }
881 EOF
882
883 test_begin_subtest "open: database set to null on missing config"
884 cat c_head3 - c_tail3 <<'EOF' | test_C ${MAIL_DIR}
885   notmuch_status_t st = notmuch_database_open_with_config(argv[1],
886                                                           NOTMUCH_DATABASE_MODE_READ_ONLY,
887                                                           "/nonexistent", NULL, &db, NULL);
888 EOF
889 cat <<EOF> EXPECTED
890 == stdout ==
891 db == NULL: 1
892 == stderr ==
893 EOF
894 test_expect_equal_file EXPECTED OUTPUT
895
896 test_begin_subtest "open: database set to null on missing config (env)"
897 old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
898 export NOTMUCH_CONFIG="/nonexistent"
899 cat c_head3 - c_tail3 <<'EOF' | test_C ${MAIL_DIR}
900   notmuch_status_t st = notmuch_database_open_with_config(argv[1],
901                                                           NOTMUCH_DATABASE_MODE_READ_ONLY,
902                                                           NULL, NULL, &db, NULL);
903 EOF
904 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
905 cat <<EOF> EXPECTED
906 == stdout ==
907 db == NULL: 1
908 == stderr ==
909 EOF
910 test_expect_equal_file EXPECTED OUTPUT
911
912 test_begin_subtest "create: database set to null on missing config"
913 cat c_head3 - c_tail3 <<'EOF' | test_C ${MAIL_DIR} "/nonexistent"
914   notmuch_status_t st = notmuch_database_create_with_config(argv[1],argv[2], NULL, &db, NULL);
915 EOF
916 cat <<EOF> EXPECTED
917 == stdout ==
918 db == NULL: 1
919 == stderr ==
920 EOF
921 test_expect_equal_file EXPECTED OUTPUT
922
923 test_begin_subtest "create: database set to null on missing config (env)"
924 old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
925 export NOTMUCH_CONFIG="/nonexistent"
926 cat c_head3 - c_tail3 <<'EOF' | test_C ${MAIL_DIR}
927   notmuch_status_t st = notmuch_database_create_with_config(argv[1],
928                                                           NULL, NULL, &db, NULL);
929 EOF
930 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
931 cat <<EOF> EXPECTED
932 == stdout ==
933 db == NULL: 1
934 == stderr ==
935 EOF
936 test_expect_equal_file EXPECTED OUTPUT
937
938 test_begin_subtest "load_config: database set non-null on missing config"
939 cat c_head3 - c_tail3 <<'EOF' | test_C ${MAIL_DIR} "/nonexistent"
940   notmuch_status_t st = notmuch_database_load_config(argv[1],argv[2], NULL, &db, NULL);
941 EOF
942 cat <<EOF> EXPECTED
943 == stdout ==
944 db == NULL: 0
945 == stderr ==
946 EOF
947 test_expect_equal_file EXPECTED OUTPUT
948
949 test_begin_subtest "load_config: database non-null on missing config (env)"
950 old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG}
951 export NOTMUCH_CONFIG="/nonexistent"
952 cat c_head3 - c_tail3 <<'EOF' | test_C ${MAIL_DIR}
953   notmuch_status_t st = notmuch_database_load_config(argv[1], NULL, NULL, &db, NULL);
954 EOF
955 export NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG}
956 cat <<EOF> EXPECTED
957 == stdout ==
958 db == NULL: 0
959 == stderr ==
960 EOF
961 test_expect_equal_file EXPECTED OUTPUT
962
963 test_begin_subtest "load_config: database set to NULL on fatal error"
964 cat c_head3 - c_tail3 <<'EOF' | test_C
965   notmuch_status_t st = notmuch_database_load_config("relative", NULL, NULL, &db, NULL);
966 EOF
967 cat <<EOF> EXPECTED
968 == stdout ==
969 db == NULL: 1
970 == stderr ==
971 EOF
972 test_expect_equal_file EXPECTED OUTPUT
973
974 test_begin_subtest "open: database parameter overrides implicit config"
975 notmuch config set database.path ${MAIL_DIR}/nonexistent
976 cat c_head3 - c_tail3 <<'EOF' | test_C ${MAIL_DIR}
977   const char *path = NULL;
978   notmuch_status_t st = notmuch_database_open_with_config(argv[1],
979                                                           NOTMUCH_DATABASE_MODE_READ_ONLY,
980                                                           NULL, NULL, &db, NULL);
981   printf ("status: %d\n", st);
982   path = notmuch_database_get_path (db);
983   printf ("path: %s\n", path ? path : "(null)");
984 EOF
985 cat <<EOF> EXPECTED
986 == stdout ==
987 status: 0
988 path: MAIL_DIR
989 db == NULL: 0
990 == stderr ==
991 EOF
992 notmuch_dir_sanitize < OUTPUT > OUTPUT.clean
993 test_expect_equal_file EXPECTED OUTPUT.clean
994
995 test_done