]> git.notmuchmail.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
5 test_begin_subtest "Show message: json"
6 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\""
7 output=$(notmuch show --format=json "json-show-message")
8 test_expect_equal_json "$output" "[[[{\"id\": \"${gen_msg_id}\", \"crypto\": {}, \"match\": true, \"excluded\": false, \"filename\": [\"${gen_msg_filename}\"], \"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\"}]}, []]]]"
9
10 # This should be the same output as above.
11 test_begin_subtest "Show message: json --body=true"
12 output=$(notmuch show --format=json --body=true "json-show-message")
13 test_expect_equal_json "$output" "[[[{\"id\": \"${gen_msg_id}\",  \"crypto\": {}, \"match\": true, \"excluded\": false, \"filename\": [\"${gen_msg_filename}\"], \"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\"}]}, []]]]"
14
15 test_begin_subtest "Show message: json --body=false"
16 output=$(notmuch show --format=json --body=false "json-show-message")
17 test_expect_equal_json "$output" "[[[{\"id\": \"${gen_msg_id}\",  \"crypto\": {}, \"match\": true, \"excluded\": false, \"filename\": [\"${gen_msg_filename}\"], \"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\"}}, []]]]"
18
19 test_begin_subtest "Search message: json"
20 add_message "[subject]=\"json-search-subject\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"json-search-message\""
21 output=$(notmuch search --format=json "json-search-message" | notmuch_search_sanitize)
22 test_expect_equal_json "$output" "[{\"thread\": \"XXX\",
23  \"timestamp\": 946728000,
24  \"date_relative\": \"2000-01-01\",
25  \"matched\": 1,
26  \"total\": 1,
27  \"authors\": \"Notmuch Test Suite\",
28  \"subject\": \"json-search-subject\",
29  \"query\": [\"id:$gen_msg_id\", null],
30  \"tags\": [\"inbox\",
31  \"unread\"]}]"
32
33 test_begin_subtest "Show message: json, utf-8"
34 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\""
35 output=$(notmuch show --format=json "jsön-show-méssage")
36 test_expect_equal_json "$output" "[[[{\"id\": \"${gen_msg_id}\",  \"crypto\": {}, \"match\": true, \"excluded\": false, \"filename\": [\"${gen_msg_filename}\"], \"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\"}]}, []]]]"
37
38 test_begin_subtest "Show message: json, inline attachment filename"
39 subject='json-show-inline-attachment-filename'
40 id="json-show-inline-attachment-filename@notmuchmail.org"
41 emacs_fcc_message \
42     "$subject" \
43     'This is a test message with inline attachment with a filename' \
44     "(mml-attach-file \"$NOTMUCH_SRCDIR/test/README\" nil nil \"inline\")
45      (message-goto-eoh)
46      (insert \"Message-ID: <$id>\n\")"
47 output=$(notmuch show --format=json "id:$id")
48 filename=$(notmuch search --output=files "id:$id")
49 # Get length of README after base64-encoding, minus additional newline.
50 attachment_length=$(( $(base64 $NOTMUCH_SRCDIR/test/README | wc -c) - 1 ))
51 test_expect_equal_json "$output" "[[[{\"id\": \"$id\",  \"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\"}]}]}, []]]]"
52
53 test_begin_subtest "Search message: json, utf-8"
54 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\""
55 output=$(notmuch search --format=json "jsön-search-méssage" | notmuch_search_sanitize)
56 test_expect_equal_json "$output" "[{\"thread\": \"XXX\",
57  \"timestamp\": 946728000,
58  \"date_relative\": \"2000-01-01\",
59  \"matched\": 1,
60  \"total\": 1,
61  \"authors\": \"Notmuch Test Suite\",
62  \"subject\": \"json-search-utf8-body-sübjéct\",
63  \"query\": [\"id:$gen_msg_id\", null],
64  \"tags\": [\"inbox\",
65  \"unread\"]}]"
66
67 test_begin_subtest "Search message: json, 64-bit timestamp"
68 if [ $NOTMUCH_HAVE_64BIT_TIME_T -ne 1 ]; then
69     test_subtest_known_broken
70 fi
71 add_message "[subject]=\"json-search-64bit-timestamp-subject\"" "[date]=\"Tue, 01 Jan 2999 12:00:00 -0000\"" "[body]=\"json-search-64bit-timestamp-message\""
72 output=$(notmuch search --format=json "json-search-64bit-timestamp-message" | notmuch_search_sanitize)
73 test_expect_equal_json "$output" "[{\"thread\": \"XXX\",
74  \"timestamp\": 32472187200,
75  \"date_relative\": \"the future\",
76  \"matched\": 1,
77  \"total\": 1,
78  \"authors\": \"Notmuch Test Suite\",
79  \"subject\": \"json-search-64bit-timestamp-subject\",
80  \"query\": [\"id:$gen_msg_id\", null],
81  \"tags\": [\"inbox\",
82  \"unread\"]}]"
83
84 test_begin_subtest "Format version: too low"
85 test_expect_code 20 "notmuch search --format-version=0 \\*"
86
87 test_begin_subtest "Format version: too high"
88 test_expect_code 21 "notmuch search --format-version=999 \\*"
89
90 test_begin_subtest "Show message: multiple filenames"
91 add_message '[id]=message-id@example.com [filename]=copy1 [date]="Fri, 05 Jan 2001 15:43:52 +0000"'
92 add_message '[id]=message-id@example.com [filename]=copy2 [date]="Fri, 05 Jan 2001 15:43:52 +0000"'
93 cat <<EOF > EXPECTED
94 [
95     [
96         [
97             {
98                 "date_relative": "2001-01-05",
99                 "excluded": false,
100                 "filename": [
101                     "${MAIL_DIR}/copy1",
102                     "${MAIL_DIR}/copy2"
103                 ],
104                 "headers": {
105                     "Date": "Fri, 05 Jan 2001 15:43:52 +0000",
106                     "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
107                     "Subject": "Show message: multiple filenames",
108                     "To": "Notmuch Test Suite <test_suite@notmuchmail.org>"
109                 },
110                 "id": "message-id@example.com",
111                 "crypto": {},
112                 "match": true,
113                 "tags": [
114                     "inbox",
115                     "unread"
116                 ],
117                 "timestamp": 978709432
118             },
119             []
120         ]
121     ]
122 ]
123 EOF
124 output=$(notmuch show --format=json --body=false id:message-id@example.com)
125 test_expect_equal_json "$output" "$(cat EXPECTED)"
126
127 test_begin_subtest "Show message: multiple filenames, format version 2"
128 cat <<EOF > EXPECTED
129 [
130     [
131         [
132             {
133                 "date_relative": "2001-01-05",
134                 "excluded": false,
135                 "filename": "${MAIL_DIR}/copy1",
136                 "headers": {
137                     "Date": "Fri, 05 Jan 2001 15:43:52 +0000",
138                     "From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
139                     "Subject": "Show message: multiple filenames",
140                     "To": "Notmuch Test Suite <test_suite@notmuchmail.org>"
141                 },
142                 "id": "message-id@example.com",
143                 "match": true,
144                 "tags": [
145                     "inbox",
146                     "unread"
147                 ],
148                 "timestamp": 978709432
149             },
150             []
151         ]
152     ]
153 ]
154 EOF
155 output=$(notmuch show --format=json --body=false --format-version=2 id:message-id@example.com)
156 test_expect_equal_json "$output" "$(cat EXPECTED)"
157
158 test_done