]> git.cworth.org Git - notmuch/blob - test/T160-json.sh
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / test / T160-json.sh
1 #!/usr/bin/env bash
2 test_description="--format=json output"
3 . $(dirname "$0")/test-lib.sh || exit 1
4 .  $NOTMUCH_SRCDIR/test/test-lib-emacs.sh || exit 1
5
6 test_begin_subtest "Show message: json"
7 add_message "[subject]=\"json-show-subject\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[bcc]=\"test_suite+bcc@notmuchmail.org\"" "[reply-to]=\"test_suite+replyto@notmuchmail.org\"" "[body]=\"json-show-message\""
8 output=$(notmuch show --format=json "json-show-message" | notmuch_json_show_sanitize)
9 test_expect_equal_json "$output" "[[[{\"id\": \"XXXXX\", \"crypto\": {}, \"match\": true, \"excluded\": false, \"filename\": [\"YYYYY\"], \"timestamp\": 946728000, \"date_relative\": \"2000-01-01\", \"tags\": [\"inbox\",\"unread\"], \"headers\": {\"Subject\": \"json-show-subject\", \"From\": \"Notmuch Test Suite <test_suite@notmuchmail.org>\", \"To\": \"Notmuch Test Suite <test_suite@notmuchmail.org>\", \"Bcc\": \"test_suite+bcc@notmuchmail.org\", \"Reply-To\": \"test_suite+replyto@notmuchmail.org\", \"Date\": \"Sat, 01 Jan 2000 12:00:00 +0000\"}, \"body\": [{\"id\": 1, \"content-type\": \"text/plain\", \"content\": \"json-show-message\n\"}]}, []]]]"
10
11 # This should be the same output as above.
12 test_begin_subtest "Show message: json --body=true"
13 output=$(notmuch show --format=json --body=true "json-show-message" | notmuch_json_show_sanitize)
14 test_expect_equal_json "$output" "[[[{\"id\": \"XXXXX\",  \"crypto\": {}, \"match\": true, \"excluded\": false, \"filename\": [\"YYYYY\"], \"timestamp\": 946728000, \"date_relative\": \"2000-01-01\", \"tags\": [\"inbox\",\"unread\"], \"headers\": {\"Subject\": \"json-show-subject\", \"From\": \"Notmuch Test Suite <test_suite@notmuchmail.org>\", \"To\": \"Notmuch Test Suite <test_suite@notmuchmail.org>\", \"Bcc\": \"test_suite+bcc@notmuchmail.org\", \"Reply-To\": \"test_suite+replyto@notmuchmail.org\", \"Date\": \"Sat, 01 Jan 2000 12:00:00 +0000\"}, \"body\": [{\"id\": 1, \"content-type\": \"text/plain\", \"content\": \"json-show-message\n\"}]}, []]]]"
15
16 test_begin_subtest "Show message: json --body=false"
17 output=$(notmuch show --format=json --body=false "json-show-message" | notmuch_json_show_sanitize)
18 test_expect_equal_json "$output" "[[[{\"id\": \"XXXXX\",  \"crypto\": {}, \"match\": true, \"excluded\": false, \"filename\": [\"YYYYY\"], \"timestamp\": 946728000, \"date_relative\": \"2000-01-01\", \"tags\": [\"inbox\",\"unread\"], \"headers\": {\"Subject\": \"json-show-subject\", \"From\": \"Notmuch Test Suite <test_suite@notmuchmail.org>\", \"To\": \"Notmuch Test Suite <test_suite@notmuchmail.org>\", \"Bcc\": \"test_suite+bcc@notmuchmail.org\", \"Reply-To\": \"test_suite+replyto@notmuchmail.org\", \"Date\": \"Sat, 01 Jan 2000 12:00:00 +0000\"}}, []]]]"
19
20 test_begin_subtest "Search message: json"
21 add_message "[subject]=\"json-search-subject\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"json-search-message\""
22 output=$(notmuch search --format=json "json-search-message" | notmuch_search_sanitize)
23 test_expect_equal_json "$output" "[{\"thread\": \"XXX\",
24  \"timestamp\": 946728000,
25  \"date_relative\": \"2000-01-01\",
26  \"matched\": 1,
27  \"total\": 1,
28  \"authors\": \"Notmuch Test Suite\",
29  \"subject\": \"json-search-subject\",
30  \"query\": [\"id:$gen_msg_id\", null],
31  \"tags\": [\"inbox\",
32  \"unread\"]}]"
33
34 test_begin_subtest "Show message: json, utf-8"
35 add_message "[subject]=\"json-show-utf8-body-sübjéct\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"jsön-show-méssage\""
36 output=$(notmuch show --format=json "jsön-show-méssage" | notmuch_json_show_sanitize)
37 test_expect_equal_json "$output" "[[[{\"id\": \"XXXXX\",  \"crypto\": {}, \"match\": true, \"excluded\": false, \"filename\": [\"YYYYY\"], \"timestamp\": 946728000, \"date_relative\": \"2000-01-01\", \"tags\": [\"inbox\",\"unread\"], \"headers\": {\"Subject\": \"json-show-utf8-body-sübjéct\", \"From\": \"Notmuch Test Suite <test_suite@notmuchmail.org>\", \"To\": \"Notmuch Test Suite <test_suite@notmuchmail.org>\", \"Date\": \"Sat, 01 Jan 2000 12:00:00 +0000\"}, \"body\": [{\"id\": 1, \"content-type\": \"text/plain\", \"content\": \"jsön-show-méssage\n\"}]}, []]]]"
38
39 test_begin_subtest "Show message: json, inline attachment filename"
40 subject='json-show-inline-attachment-filename'
41 id="json-show-inline-attachment-filename@notmuchmail.org"
42 emacs_fcc_message \
43     "$subject" \
44     'This is a test message with inline attachment with a filename' \
45     "(mml-attach-file \"$NOTMUCH_SRCDIR/test/README\" nil nil \"inline\")
46      (message-goto-eoh)
47      (insert \"Message-ID: <$id>\n\")"
48 output=$(notmuch show --format=json "id:$id")
49 filename=$(notmuch search --output=files "id:$id")
50 # Get length of README after base64-encoding, minus additional newline.
51 attachment_length=$(( $(base64 $NOTMUCH_SRCDIR/test/README | wc -c) - 1 ))
52 test_expect_equal_json "$output" "[[[{\"id\": \"$id\", \"duplicate\": 1, \"crypto\": {}, \"match\": true, \"excluded\": false, \"filename\": [\"$filename\"], \"timestamp\": 946728000, \"date_relative\": \"2000-01-01\", \"tags\": [\"inbox\"], \"headers\": {\"Subject\": \"$subject\", \"From\": \"Notmuch Test Suite <test_suite@notmuchmail.org>\", \"To\": \"test_suite@notmuchmail.org\", \"Date\": \"Sat, 01 Jan 2000 12:00:00 +0000\"}, \"body\": [{\"id\": 1, \"content-type\": \"multipart/mixed\", \"content\": [{\"id\": 2, \"content-type\": \"text/plain\", \"content\": \"This is a test message with inline attachment with a filename\"}, {\"id\": 3, \"content-type\": \"application/octet-stream\", \"content-length\": $attachment_length, \"content-transfer-encoding\": \"base64\", \"content-disposition\": \"inline\", \"filename\": \"README\"}]}]}, []]]]"
53
54 test_begin_subtest "Search message: json, utf-8"
55 add_message "[subject]=\"json-search-utf8-body-sübjéct\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"jsön-search-méssage\""
56 output=$(notmuch search --format=json "jsön-search-méssage" | notmuch_search_sanitize)
57 test_expect_equal_json "$output" "[{\"thread\": \"XXX\",
58  \"timestamp\": 946728000,
59  \"date_relative\": \"2000-01-01\",
60  \"matched\": 1,
61  \"total\": 1,
62  \"authors\": \"Notmuch Test Suite\",
63  \"subject\": \"json-search-utf8-body-sübjéct\",
64  \"query\": [\"id:$gen_msg_id\", null],
65  \"tags\": [\"inbox\",
66  \"unread\"]}]"
67
68 if [ -z "${NOTMUCH_TEST_INSTALLED-}" ]; then
69 test_begin_subtest "Search message: json, 64-bit timestamp"
70 if [ "${NOTMUCH_HAVE_64BIT_TIME_T-0}" != "1" ]; then
71     test_subtest_known_broken
72 fi
73 add_message "[subject]=\"json-search-64bit-timestamp-subject\"" "[date]=\"Tue, 01 Jan 2999 12:00:00 -0000\"" "[body]=\"json-search-64bit-timestamp-message\""
74 output=$(notmuch search --format=json "json-search-64bit-timestamp-message" | notmuch_search_sanitize)
75 test_expect_equal_json "$output" "[{\"thread\": \"XXX\",
76  \"timestamp\": 32472187200,
77  \"date_relative\": \"the future\",
78  \"matched\": 1,
79  \"total\": 1,
80  \"authors\": \"Notmuch Test Suite\",
81  \"subject\": \"json-search-64bit-timestamp-subject\",
82  \"query\": [\"id:$gen_msg_id\", null],
83  \"tags\": [\"inbox\",
84  \"unread\"]}]"
85 fi # NOTMUCH_TEST_INSTALLED undefined / empty
86
87 test_begin_subtest "Format version: too low"
88 test_expect_code 20 "notmuch search --format-version=0 \\*"
89
90 test_begin_subtest "Format version: too high"
91 test_expect_code 21 "notmuch search --format-version=999 \\*"
92
93 test_begin_subtest "Show message: multiple filenames"
94 add_message '[id]=message-id@example.com [filename]=copy1 [date]="Fri, 05 Jan 2001 15:43:52 +0000"'
95 add_message '[id]=message-id@example.com [filename]=copy2 [date]="Fri, 05 Jan 2001 15:43:52 +0000"'
96 cat <<EOF > EXPECTED
97 [
98     [
99         [
100             {
101                 "date_relative": "2001-01-05",
102                 "duplicate": 1,
103                 "excluded": false,
104                 "filename": [
105                     "${MAIL_DIR}/copy1",
106                     "${MAIL_DIR}/copy2"
107                 ],
108                 "headers": {
109                     "Date": "Fri, 05 Jan 2001 15:43:52 +0000",
110                     "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
111                     "Subject": "Show message: multiple filenames",
112                     "To": "Notmuch Test Suite <test_suite@notmuchmail.org>"
113                 },
114                 "id": "message-id@example.com",
115                 "crypto": {},
116                 "match": true,
117                 "tags": [
118                     "inbox",
119                     "unread"
120                 ],
121                 "timestamp": 978709432
122             },
123             []
124         ]
125     ]
126 ]
127 EOF
128 output=$(notmuch show --format=json --body=false id:message-id@example.com)
129 test_expect_equal_json "$output" "$(cat EXPECTED)"
130
131 test_begin_subtest "Show message: multiple filenames, format version 2"
132 cat <<EOF > EXPECTED
133 [
134     [
135         [
136             {
137                 "date_relative": "2001-01-05",
138                 "duplicate": 1,
139                 "excluded": false,
140                 "filename": "${MAIL_DIR}/copy1",
141                 "headers": {
142                     "Date": "Fri, 05 Jan 2001 15:43:52 +0000",
143                     "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
144                     "Subject": "Show message: multiple filenames",
145                     "To": "Notmuch Test Suite <test_suite@notmuchmail.org>"
146                 },
147                 "id": "message-id@example.com",
148                 "match": true,
149                 "tags": [
150                     "inbox",
151                     "unread"
152                 ],
153                 "timestamp": 978709432
154             },
155             []
156         ]
157     ]
158 ]
159 EOF
160 output=$(notmuch show --format=json --body=false --format-version=2 id:message-id@example.com)
161 test_expect_equal_json "$output" "$(cat EXPECTED)"
162
163 test_begin_subtest "show extra headers"
164 add_message "[subject]=\"extra-headers\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[in-reply-to]=\"<parent@notmuch-test-suite>\"" "[body]=\"extra-headers test\""\
165            "[header]=\"Received: from mail.example.com (mail.example.com [1.1.1.1])
166         by mail.notmuchmail.org (some MTA) with ESMTP id 12345678
167         for <test_suite_other@notmuchmail.org>; Sat, 10 Apr 2010 07:54:51 -0400 (EDT)\"" \
168
169 notmuch config set show.extra_headers "in-reply-to;received"
170 output=$(notmuch show --format=json --body=false id:${gen_msg_id} | notmuch_json_show_sanitize)
171 cat <<EOF > EXPECTED
172 [
173     [
174         [
175             {
176                 "crypto": {},
177                 "date_relative": "2000-01-01",
178                 "excluded": false,
179                 "filename": [
180                     "YYYYY"
181                 ],
182                 "headers": {
183                     "Date": "Sat, 01 Jan 2000 12:00:00 +0000",
184                     "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
185                     "In-Reply-To": "<parent@notmuch-test-suite>",
186                     "Received": "from mail.example.com (mail.example.com [1.1.1.1])\tby mail.notmuchmail.org (some MTA) with ESMTP id 12345678\tfor <test_suite_other@notmuchmail.org>; Sat, 10 Apr 2010 07:54:51 -0400 (EDT)",
187                     "Subject": "extra-headers",
188                     "To": "Notmuch Test Suite <test_suite@notmuchmail.org>"
189                 },
190                 "id": "XXXXX",
191                 "match": true,
192                 "tags": [
193                     "inbox",
194                     "unread"
195                 ],
196                 "timestamp": 946728000
197             },
198             []
199         ]
200     ]
201 ]
202 EOF
203 test_expect_equal_json "${output}" "$(cat EXPECTED)"
204
205 test_done