]> git.notmuchmail.org Git - notmuch/blob - test/T610-message-property.sh
test: drop the implicit prereq check mechanism from test_expect_*
[notmuch] / test / T610-message-property.sh
1 #!/usr/bin/env bash
2 test_description="message property API"
3
4 . ./test-lib.sh || exit 1
5
6 add_email_corpus
7
8 cat <<EOF > c_head
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <talloc.h>
13 #include <notmuch-test.h>
14
15 void print_properties (notmuch_message_t *message, const char *prefix, notmuch_bool_t exact) {
16     notmuch_message_properties_t *list;
17     for (list = notmuch_message_get_properties (message, prefix, exact);
18          notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
19        printf("%s\n", notmuch_message_properties_value(list));
20     }
21     notmuch_message_properties_destroy (list);
22 }
23
24 int main (int argc, char** argv)
25 {
26    notmuch_database_t *db;
27    notmuch_message_t *message = NULL;
28    const char *val;
29    notmuch_status_t stat;
30
31    EXPECT0(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));
32    EXPECT0(notmuch_database_find_message(db, "4EFC743A.3060609@april.org", &message));
33    if (message == NULL) {
34         fprintf (stderr, "unable to find message");
35         exit (1);
36    }
37 EOF
38
39 cat <<EOF > c_tail
40    EXPECT0(notmuch_database_destroy(db));
41 }
42 EOF
43
44 test_begin_subtest "notmuch_message_{add,get,remove}_property"
45 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
46 {
47    EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue1"));
48    EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
49    printf("testkey1[1] = %s\n", val);
50    EXPECT0(notmuch_message_add_property (message, "testkey2", "this value has spaces and = sign"));
51    EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
52    printf("testkey1[2] = %s\n", val);
53    EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
54
55    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
56    printf("testkey2 = %s\n", val);
57
58    /* Add second value for key */
59    EXPECT0(notmuch_message_add_property (message, "testkey2", "zztestvalue3"));
60    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
61    printf("testkey2 = %s\n", val);
62
63    /* remove first value for key */
64    EXPECT0(notmuch_message_remove_property (message, "testkey2", "this value has spaces and = sign"));
65    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
66    printf("testkey2 = %s\n", val);
67
68    /* remove non-existant value for key */
69    EXPECT0(notmuch_message_remove_property (message, "testkey2", "this value has spaces and = sign"));
70    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
71    printf("testkey2 = %s\n", val);
72
73    /* remove only value for key */
74    EXPECT0(notmuch_message_remove_property (message, "testkey2", "zztestvalue3"));
75    EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
76    printf("testkey2 = %s\n", val == NULL ? "NULL" : val);
77 }
78 EOF
79 cat <<'EOF' >EXPECTED
80 == stdout ==
81 testkey1[1] = testvalue1
82 testkey1[2] = testvalue1
83 testkey2 = this value has spaces and = sign
84 testkey2 = this value has spaces and = sign
85 testkey2 = zztestvalue3
86 testkey2 = zztestvalue3
87 testkey2 = NULL
88 == stderr ==
89 EOF
90 test_expect_equal_file EXPECTED OUTPUT
91
92 test_begin_subtest "notmuch_message_remove_all_properties"
93 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
94 EXPECT0(notmuch_message_remove_all_properties (message, NULL));
95 print_properties (message, "", FALSE);
96 EOF
97 cat <<'EOF' >EXPECTED
98 == stdout ==
99 == stderr ==
100 EOF
101 test_expect_equal_file EXPECTED OUTPUT
102
103 test_begin_subtest "notmuch_message_get_properties: empty list"
104 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
105 {
106    notmuch_message_properties_t *list;
107    list = notmuch_message_get_properties (message, "nonexistent", TRUE);
108    printf("valid = %d\n", notmuch_message_properties_valid (list));
109    notmuch_message_properties_destroy (list);
110 }
111 EOF
112 cat <<'EOF' >EXPECTED
113 == stdout ==
114 valid = 0
115 == stderr ==
116 EOF
117 test_expect_equal_file EXPECTED OUTPUT
118
119 test_begin_subtest "notmuch_message_properties: one value"
120 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
121 print_properties (message, "testkey1", TRUE);
122 EOF
123 cat <<'EOF' >EXPECTED
124 == stdout ==
125 testvalue1
126 == stderr ==
127 EOF
128 test_expect_equal_file EXPECTED OUTPUT
129
130 test_begin_subtest "notmuch_message_properties: multiple values"
131 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
132 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
133 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
134 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
135 print_properties (message, "testkey1", TRUE);
136 EOF
137 cat <<'EOF' >EXPECTED
138 == stdout ==
139 alice
140 bob
141 testvalue1
142 testvalue2
143 == stderr ==
144 EOF
145 test_expect_equal_file EXPECTED OUTPUT
146
147 test_begin_subtest "notmuch_message_properties: prefix"
148 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
149 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
150 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
151 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
152 print_properties (message, "testkey", FALSE);
153 EOF
154 cat <<'EOF' >EXPECTED
155 == stdout ==
156 alice
157 bob
158 testvalue1
159 testvalue2
160 alice3
161 bob3
162 testvalue3
163 == stderr ==
164 EOF
165 test_expect_equal_file EXPECTED OUTPUT
166
167 test_begin_subtest "notmuch_message_properties: modify during iteration"
168 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
169 {
170     const char *keys[1000] = {NULL};
171     const char *vals[1000] = {NULL};
172     notmuch_message_properties_t *properties;
173     int i;
174
175     for (properties = notmuch_message_get_properties (message, "", FALSE), i=0;
176          notmuch_message_properties_valid (properties);
177          notmuch_message_properties_move_to_next (properties), i++)
178     {
179         const char *key, *value;
180
181         keys[i]=talloc_strdup(message,
182                     notmuch_message_properties_key (properties));
183         vals[i]=talloc_strdup(message,
184                     notmuch_message_properties_value (properties));
185
186         EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
187     }
188
189     print_properties (message, "", FALSE);
190
191     for (i = 0; keys[i] && vals[i]; i++) {
192         EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
193     }
194 }
195 EOF
196 cat <<'EOF' >EXPECTED
197 == stdout ==
198 == stderr ==
199 EOF
200 test_expect_equal_file EXPECTED OUTPUT
201
202 test_begin_subtest "dump message properties"
203 cat <<EOF > PROPERTIES
204 #= 4EFC743A.3060609@april.org fancy%20key%20with%20%c3%a1cc%c3%a8nts=import%20value%20with%20= testkey1=alice testkey1=bob testkey1=testvalue1 testkey1=testvalue2 testkey3=alice3 testkey3=bob3 testkey3=testvalue3
205 EOF
206 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
207 EXPECT0(notmuch_message_add_property (message, "fancy key with áccènts", "import value with ="));
208 EOF
209 notmuch dump | grep '^#=' > OUTPUT
210 test_expect_equal_file PROPERTIES OUTPUT
211
212
213 test_begin_subtest "restore missing message property (single line)"
214 notmuch dump | grep '^#=' > BEFORE1
215 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
216 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
217 EOF
218 notmuch restore < BEFORE1
219 notmuch dump | grep '^#=' > OUTPUT
220 test_expect_equal_file PROPERTIES OUTPUT
221
222
223 test_begin_subtest "restore missing message property (full dump)"
224 notmuch dump > BEFORE2
225 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
226 EXPECT0(notmuch_message_remove_property (message, "testkey1", "bob"));
227 EOF
228 notmuch restore < BEFORE2
229 notmuch dump | grep '^#=' > OUTPUT
230 test_expect_equal_file PROPERTIES OUTPUT
231
232 test_begin_subtest "restore clear extra message property"
233 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
234 EXPECT0(notmuch_message_add_property (message, "testkey1", "charles"));
235 EOF
236 notmuch restore < BEFORE2
237 notmuch dump | grep '^#=' > OUTPUT
238 test_expect_equal_file PROPERTIES OUTPUT
239
240 test_begin_subtest "test 'property:' queries: empty"
241 notmuch search property:testkey1=charles > OUTPUT
242 test_expect_equal_file /dev/null OUTPUT
243
244 test_begin_subtest "test 'property:' queries: single message"
245 notmuch search --output=messages property:testkey1=alice > OUTPUT
246 cat <<EOF >EXPECTED
247 id:4EFC743A.3060609@april.org
248 EOF
249 test_expect_equal_file EXPECTED OUTPUT
250
251 test_done