1#!/usr/bin/env bash
2test_description='"notmuch search" in several variations'
3. $(dirname "$0")/test-lib.sh || exit 1
4
5if [ $NOTMUCH_HAVE_SFSEXP -ne 1 ]; then
6    printf "Skipping due to missing sfsexp library\n"
7    test_done
8fi
9
10add_email_corpus
11
12for query in '()' '(not)' '(and)' '(or ())' '(or (not))' '(or (and))' \
13            '(or (and) (or) (not (and)))'; do
14    test_begin_subtest "all messages: $query"
15    notmuch search '*' > EXPECTED
16    notmuch search --query=sexp "$query" > OUTPUT
17    test_expect_equal_file EXPECTED OUTPUT
18done
19
20for query in '(or)' '(not ())' '(not (not))' '(not (and))' \
21                   '(not (or (and) (or) (not (and))))'; do
22    test_begin_subtest "no messages: $query"
23    notmuch search --query=sexp "$query" > OUTPUT
24    test_expect_equal_file /dev/null OUTPUT
25done
26
27test_begin_subtest "and of exact terms"
28notmuch search --query=sexp '(and "wonderful" "wizard")' | notmuch_search_sanitize > OUTPUT
29cat <<EOF > EXPECTED
30thread:XXX   2009-11-18 [1/3] Carl Worth| Jan Janak; [notmuch] What a great idea! (inbox unread)
31EOF
32test_expect_equal_file EXPECTED OUTPUT
33
34test_begin_subtest "or of exact terms"
35notmuch search --query=sexp '(or "php" "wizard")' | notmuch_search_sanitize > OUTPUT
36cat <<EOF > EXPECTED
37thread:XXX   2010-12-29 [1/1] François Boulogne; [aur-general] Guidelines: cp, mkdir vs install (inbox unread)
38thread:XXX   2009-11-18 [1/3] Carl Worth| Jan Janak; [notmuch] What a great idea! (inbox unread)
39EOF
40test_expect_equal_file EXPECTED OUTPUT
41
42test_begin_subtest "single term in body"
43notmuch search --query=sexp 'wizard' | notmuch_search_sanitize>OUTPUT
44cat <<EOF > EXPECTED
45thread:XXX   2009-11-18 [1/3] Carl Worth| Jan Janak; [notmuch] What a great idea! (inbox unread)
46EOF
47test_expect_equal_file EXPECTED OUTPUT
48
49test_begin_subtest "single term in body (case insensitive)"
50notmuch search --query=sexp 'Wizard' | notmuch_search_sanitize>OUTPUT
51cat <<EOF > EXPECTED
52thread:XXX   2009-11-18 [1/3] Carl Worth| Jan Janak; [notmuch] What a great idea! (inbox unread)
53EOF
54test_expect_equal_file EXPECTED OUTPUT
55
56test_begin_subtest "single term in body, stemmed version"
57notmuch search arriv > EXPECTED
58notmuch search --query=sexp arriv > OUTPUT
59test_expect_equal_file EXPECTED OUTPUT
60
61test_begin_subtest "single term in body, unstemmed version"
62notmuch search --query=sexp '"arriv"' > OUTPUT
63test_expect_equal_file /dev/null OUTPUT
64
65test_begin_subtest "Search by 'subject'"
66add_message [subject]=subjectsearchtest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
67output=$(notmuch search --query=sexp '(subject subjectsearchtest)' | notmuch_search_sanitize)
68test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; subjectsearchtest (inbox unread)"
69
70test_begin_subtest "Search by 'subject' (case insensitive)"
71notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED
72notmuch search --query=sexp '(subject "Maildir")' | notmuch_search_sanitize > OUTPUT
73test_expect_equal_file EXPECTED OUTPUT
74
75test_begin_subtest "Search by 'subject' (utf-8):"
76add_message [subject]=utf8-sübjéct '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
77output=$(notmuch search --query=sexp '(subject utf8 sübjéct)' | notmuch_search_sanitize)
78test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-sübjéct (inbox unread)"
79
80test_begin_subtest "Search by 'subject' (utf-8, and):"
81output=$(notmuch search --query=sexp '(subject (and utf8 sübjéct))' | notmuch_search_sanitize)
82test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-sübjéct (inbox unread)"
83
84test_begin_subtest "Search by 'subject' (utf-8, and outside):"
85output=$(notmuch search --query=sexp '(and (subject utf8) (subject sübjéct))' | notmuch_search_sanitize)
86test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-sübjéct (inbox unread)"
87
88test_begin_subtest "Search by 'subject' (utf-8, or):"
89notmuch search --query=sexp '(subject (or utf8 subjectsearchtest))' | notmuch_search_sanitize > OUTPUT
90cat <<EOF > EXPECTED
91thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; subjectsearchtest (inbox unread)
92thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-sübjéct (inbox unread)
93EOF
94test_expect_equal_file EXPECTED OUTPUT
95
96test_begin_subtest "Search by 'subject' (utf-8, or outside):"
97notmuch search --query=sexp '(or (subject utf8) (subject subjectsearchtest))' | notmuch_search_sanitize > OUTPUT
98cat <<EOF > EXPECTED
99thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; subjectsearchtest (inbox unread)
100thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-sübjéct (inbox unread)
101EOF
102test_expect_equal_file EXPECTED OUTPUT
103
104test_begin_subtest "Search by 'attachment'"
105notmuch search attachment:notmuch-help.patch > EXPECTED
106notmuch search --query=sexp '(attachment notmuch-help.patch)' > OUTPUT
107test_expect_equal_file EXPECTED OUTPUT
108
109test_begin_subtest "Search by 'body'"
110add_message '[subject]="body search"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [body]=bodysearchtest
111output=$(notmuch search --query=sexp '(body bodysearchtest)' | notmuch_search_sanitize)
112test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; body search (inbox unread)"
113
114test_begin_subtest "Search by 'body' (phrase)"
115add_message '[subject]="body search (phrase)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[body]="body search (phrase)"'
116add_message '[subject]="negative result"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[body]="This phrase should not match the body search"'
117output=$(notmuch search --query=sexp '(body "body search phrase")' | notmuch_search_sanitize)
118test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; body search (phrase) (inbox unread)"
119
120test_begin_subtest "Search by 'body' (utf-8):"
121add_message '[subject]="utf8-message-body-subject"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[body]="message body utf8: bödý"'
122output=$(notmuch search --query=sexp '(body bödý)' | notmuch_search_sanitize)
123test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-message-body-subject (inbox unread)"
124
125add_message "[body]=thebody-1" "[subject]=kryptonite-1"
126add_message "[body]=nothing-to-see-here-1" "[subject]=thebody-1"
127
128test_begin_subtest 'search without body: prefix'
129notmuch search thebody > EXPECTED
130notmuch search --query=sexp '(and thebody)' > OUTPUT
131test_expect_equal_file EXPECTED OUTPUT
132
133test_begin_subtest 'negated body: prefix'
134notmuch search thebody and not body:thebody > EXPECTED
135notmuch search --query=sexp '(and (not (body thebody)) thebody)' > OUTPUT
136test_expect_equal_file EXPECTED OUTPUT
137
138test_begin_subtest 'search unprefixed for prefixed term'
139notmuch search kryptonite > EXPECTED
140notmuch search --query=sexp '(and kryptonite)' > OUTPUT
141test_expect_equal_file EXPECTED OUTPUT
142
143test_begin_subtest 'search with body: prefix for term only in subject'
144notmuch search body:kryptonite > EXPECTED
145notmuch search --query=sexp '(body kryptonite)' > OUTPUT
146test_expect_equal_file EXPECTED OUTPUT
147
148test_begin_subtest "Search by 'from'"
149add_message '[subject]="search by from"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [from]=searchbyfrom
150output=$(notmuch search --query=sexp '(from searchbyfrom)' | notmuch_search_sanitize)
151test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] searchbyfrom; search by from (inbox unread)"
152
153test_begin_subtest "Search by 'from' (address)"
154add_message '[subject]="search by from (address)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [from]=searchbyfrom@example.com
155output=$(notmuch search --query=sexp '(from searchbyfrom@example.com)' | notmuch_search_sanitize)
156test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] searchbyfrom@example.com; search by from (address) (inbox unread)"
157
158test_begin_subtest "Search by 'from' (name)"
159add_message '[subject]="search by from (name)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[from]="Search By From Name <test@example.com>"'
160output=$(notmuch search --query=sexp '(from "Search By From Name")' | notmuch_search_sanitize)
161test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Search By From Name; search by from (name) (inbox unread)"
162
163test_begin_subtest "Search by 'from' (name and address)"
164output=$(notmuch search --query=sexp '(from "Search By From Name <test@example.com>")' | notmuch_search_sanitize)
165test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Search By From Name; search by from (name) (inbox unread)"
166
167add_message '[dir]=bad' '[subject]="To the bone"'
168add_message '[dir]=.' '[subject]="Top level"'
169add_message '[dir]=bad/news' '[subject]="Bears"'
170mkdir -p "${MAIL_DIR}/duplicate/bad/news"
171cp "$gen_msg_filename" "${MAIL_DIR}/duplicate/bad/news"
172
173add_message '[dir]=things' '[subject]="These are a few"'
174add_message '[dir]=things/favorite' '[subject]="Raindrops, whiskers, kettles"'
175add_message '[dir]=things/bad' '[subject]="Bites, stings, sad feelings"'
176
177test_begin_subtest "Search by 'folder' (multiple)"
178output=$(notmuch search --query=sexp '(folder bad bad/news things/bad)' | notmuch_search_sanitize)
179test_expect_equal "$output" "thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; To the bone (inbox unread)
180thread:XXX   2001-01-05 [1/1(2)] Notmuch Test Suite; Bears (inbox unread)
181thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Bites, stings, sad feelings (inbox unread)"
182
183test_begin_subtest "Search by 'folder': top level."
184notmuch search folder:'""' > EXPECTED
185notmuch search --query=sexp '(folder "")'  > OUTPUT
186test_expect_equal_file EXPECTED OUTPUT
187
188test_begin_subtest "Search by 'id'"
189add_message '[subject]="search by id"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
190output=$(notmuch search --query=sexp "(id ${gen_msg_id})" | notmuch_search_sanitize)
191test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by id (inbox unread)"
192
193test_begin_subtest "Search by 'id' (or)"
194add_message '[subject]="search by id"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
195output=$(notmuch search --query=sexp "(id non-existent-mid ${gen_msg_id})" | notmuch_search_sanitize)
196test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by id (inbox unread)"
197
198test_begin_subtest "Search by 'is' (multiple)"
199notmuch tag -inbox tag:searchbytag
200notmuch search is:inbox AND is:unread | notmuch_search_sanitize > EXPECTED
201notmuch search --query=sexp '(is inbox unread)' | notmuch_search_sanitize > OUTPUT
202notmuch tag +inbox tag:searchbytag
203test_expect_equal_file EXPECTED OUTPUT
204
205test_begin_subtest "Search by 'mid'"
206add_message '[subject]="search by mid"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
207output=$(notmuch search --query=sexp "(mid ${gen_msg_id})" | notmuch_search_sanitize)
208test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by mid (inbox unread)"
209
210test_begin_subtest "Search by 'mid' (or)"
211add_message '[subject]="search by mid"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
212output=$(notmuch search --query=sexp "(mid non-existent-mid ${gen_msg_id})" | notmuch_search_sanitize)
213test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by mid (inbox unread)"
214
215test_begin_subtest "Search by 'mimetype'"
216notmuch search mimetype:text/html > EXPECTED
217notmuch search --query=sexp '(mimetype text html)'  > OUTPUT
218test_expect_equal_file EXPECTED OUTPUT
219
220QUERYSTR="date:2009-11-18..2009-11-18 and tag:unread"
221QUERYSTR2="query:test and subject:Maildir"
222notmuch config set --database query.test "$QUERYSTR"
223notmuch config set query.test2 "$QUERYSTR2"
224
225test_begin_subtest "ill-formed named query search"
226notmuch search --query=sexp '(query)' > OUTPUT 2>&1
227cat <<EOF > EXPECTED
228notmuch search: Syntax error in query
229'query' expects single atom as argument
230EOF
231test_expect_equal_file EXPECTED OUTPUT
232
233test_begin_subtest "ill-formed named query search 2"
234notmuch search --query=sexp '(to (query))' > OUTPUT 2>&1
235cat <<EOF > EXPECTED
236notmuch search: Syntax error in query
237'query' not supported inside 'to'
238EOF
239test_expect_equal_file EXPECTED OUTPUT
240
241test_begin_subtest "search named query"
242notmuch search --query=sexp '(query test)' > OUTPUT
243notmuch search $QUERYSTR > EXPECTED
244test_expect_equal_file EXPECTED OUTPUT
245
246test_begin_subtest "Search by 'subject' (utf-8, phrase-token):"
247output=$(notmuch search --query=sexp '(subject utf8-sübjéct)' | notmuch_search_sanitize)
248test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-sübjéct (inbox unread)"
249
250test_begin_subtest "search named query with other terms"
251notmuch search --query=sexp '(and (query test) (subject Maildir))' > OUTPUT
252notmuch search $QUERYSTR and subject:Maildir > EXPECTED
253test_expect_equal_file EXPECTED OUTPUT
254
255test_begin_subtest "search nested named query"
256notmuch search --query=sexp '(query test2)' > OUTPUT
257notmuch search $QUERYSTR2 > EXPECTED
258test_expect_equal_file EXPECTED OUTPUT
259
260test_begin_subtest "Search by 'subject' (utf-8, quoted string):"
261output=$(notmuch search --query=sexp '(subject "utf8 sübjéct")' | notmuch_search_sanitize)
262test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-sübjéct (inbox unread)"
263
264test_begin_subtest "Search by 'subject' (combine phrase, term):"
265output=$(notmuch search --query=sexp '(subject Mac "compatibility issues")' | notmuch_search_sanitize)
266test_expect_equal "$output" "thread:XXX   2009-11-18 [4/4] Jjgod Jiang, Alexander Botero-Lowry; [notmuch] Mac OS X/Darwin compatibility issues (inbox unread)"
267
268test_begin_subtest "Search by 'subject' (combine phrase, term 2):"
269notmuch search --query=sexp '(subject (or utf8 "compatibility issues"))' | notmuch_search_sanitize > OUTPUT
270cat <<EOF > EXPECTED
271thread:XXX   2009-11-18 [4/4] Jjgod Jiang, Alexander Botero-Lowry; [notmuch] Mac OS X/Darwin compatibility issues (inbox unread)
272thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-sübjéct (inbox unread)
273thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; utf8-message-body-subject (inbox unread)
274EOF
275test_expect_equal_file EXPECTED OUTPUT
276
277test_begin_subtest "Search by 'subject' (combine phrase, term 3):"
278notmuch search --query=sexp '(subject issues X/Darwin)' | notmuch_search_sanitize > OUTPUT
279cat <<EOF > EXPECTED
280thread:XXX   2009-11-18 [4/4] Jjgod Jiang, Alexander Botero-Lowry; [notmuch] Mac OS X/Darwin compatibility issues (inbox unread)
281EOF
282test_expect_equal_file EXPECTED OUTPUT
283
284test_begin_subtest "Search by 'tag'"
285add_message '[subject]="search by tag"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
286notmuch tag +searchbytag id:${gen_msg_id}
287output=$(notmuch search --query=sexp '(tag searchbytag)' | notmuch_search_sanitize)
288test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by tag (inbox searchbytag unread)"
289
290test_begin_subtest "Search by 'tag' (multiple)"
291notmuch tag -inbox tag:searchbytag
292notmuch search tag:inbox AND tag:unread | notmuch_search_sanitize > EXPECTED
293notmuch search --query=sexp '(tag inbox unread)' | notmuch_search_sanitize > OUTPUT
294notmuch tag +inbox tag:searchbytag
295test_expect_equal_file EXPECTED OUTPUT
296
297test_begin_subtest "Search by 'tag' and 'subject'"
298notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED
299notmuch search --query=sexp '(and (tag inbox) (subject maildir))' | notmuch_search_sanitize > OUTPUT
300test_expect_equal_file EXPECTED OUTPUT
301
302test_begin_subtest "Search by 'thread'"
303add_message '[subject]="search by thread"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"'
304thread_id=$(notmuch search id:${gen_msg_id} | sed -e "s/thread:\([a-f0-9]*\).*/\1/")
305output=$(notmuch search --query=sexp "(thread ${thread_id})" | notmuch_search_sanitize)
306test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by thread (inbox unread)"
307
308test_begin_subtest "Search by 'to'"
309add_message '[subject]="search by to"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [to]=searchbyto
310output=$(notmuch search --query=sexp '(to searchbyto)' | notmuch_search_sanitize)
311test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (inbox unread)"
312
313test_begin_subtest "Search by 'to' (address)"
314add_message '[subject]="search by to (address)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' [to]=searchbyto@example.com
315output=$(notmuch search --query=sexp '(to searchbyto@example.com)' | notmuch_search_sanitize)
316test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (address) (inbox unread)"
317
318test_begin_subtest "Search by 'to' (name)"
319add_message '[subject]="search by to (name)"' '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' '[to]="Search By To Name <test@example.com>"'
320output=$(notmuch search --query=sexp '(to "Search By To Name")' | notmuch_search_sanitize)
321test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (name) (inbox unread)"
322
323test_begin_subtest "Search by 'to' (name and address)"
324output=$(notmuch search --query=sexp '(to "Search By To Name <test@example.com>")' | notmuch_search_sanitize)
325test_expect_equal "$output" "thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (name) (inbox unread)"
326
327test_begin_subtest "starts-with, no prefix"
328output=$(notmuch search --query=sexp '(starts-with prelim)' | notmuch_search_sanitize)
329test_expect_equal "$output" "thread:XXX   2009-11-17 [2/2] Alex Botero-Lowry, Carl Worth; [notmuch] preliminary FreeBSD support (attachment inbox unread)"
330
331test_begin_subtest "starts-with, case-insensitive"
332notmuch search --query=sexp '(starts-with FreeB)' | notmuch_search_sanitize > OUTPUT
333cat <<EOF > EXPECTED
334thread:XXX   2009-11-18 [3/4] Alexander Botero-Lowry, Jjgod Jiang; [notmuch] Mac OS X/Darwin compatibility issues (inbox unread)
335thread:XXX   2009-11-17 [2/2] Alex Botero-Lowry, Carl Worth; [notmuch] preliminary FreeBSD support (attachment inbox unread)
336EOF
337test_expect_equal_file EXPECTED OUTPUT
338
339test_begin_subtest "starts-with, no prefix, all messages"
340notmuch search --query=sexp '(starts-with "")' | notmuch_search_sanitize > OUTPUT
341notmuch search '*' | notmuch_search_sanitize > EXPECTED
342test_expect_equal_file EXPECTED OUTPUT
343
344test_begin_subtest "starts-with, attachment"
345output=$(notmuch search --query=sexp '(attachment (starts-with not))' | notmuch_search_sanitize)
346test_expect_equal "$output" 'thread:XXX   2009-11-18 [2/2] Lars Kellogg-Stedman; [notmuch] "notmuch help" outputs to stderr? (attachment inbox signed unread)'
347
348test_begin_subtest "starts-with, folder"
349notmuch search --output=files --query=sexp '(folder (starts-with bad))' | notmuch_dir_sanitize | sed 's/[0-9]*$/XXX/' > OUTPUT
350cat <<EOF > EXPECTED
351MAIL_DIR/bad/msg-XXX
352MAIL_DIR/bad/news/msg-XXX
353MAIL_DIR/duplicate/bad/news/msg-XXX
354EOF
355test_expect_equal_file EXPECTED OUTPUT
356
357test_begin_subtest "starts-with, from"
358notmuch search --query=sexp '(from (starts-with Mik))' | notmuch_search_sanitize > OUTPUT
359cat <<EOF > EXPECTED
360thread:XXX   2009-11-17 [1/1] Mikhail Gusarov; [notmuch] [PATCH] Handle rename of message file (inbox unread)
361thread:XXX   2009-11-17 [2/7] Mikhail Gusarov| Lars Kellogg-Stedman, Keith Packard, Carl Worth; [notmuch] Working with Maildir storage? (inbox signed unread)
362thread:XXX   2009-11-17 [2/5] Mikhail Gusarov| Carl Worth, Keith Packard; [notmuch] [PATCH 2/2] Include <stdint.h> to get uint32_t in C++ file with gcc 4.4 (inbox unread)
363EOF
364test_expect_equal_file EXPECTED OUTPUT
365
366test_begin_subtest "starts-with, id"
367notmuch search --query=sexp --output=messages '(id (starts-with 877))' > OUTPUT
368cat <<EOF > EXPECTED
369id:877h1wv7mg.fsf@inf-8657.int-evry.fr
370id:877htoqdbo.fsf@yoom.home.cworth.org
371EOF
372test_expect_equal_file EXPECTED OUTPUT
373
374test_begin_subtest "starts-with, is"
375output=$(notmuch search --query=sexp '(is (starts-with searchby))' | notmuch_search_sanitize)
376test_expect_equal "$output" 'thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by tag (inbox searchbytag unread)'
377
378test_begin_subtest "starts-with, mid"
379notmuch search --query=sexp --output=messages '(mid (starts-with 877))' > OUTPUT
380cat <<EOF > EXPECTED
381id:877h1wv7mg.fsf@inf-8657.int-evry.fr
382id:877htoqdbo.fsf@yoom.home.cworth.org
383EOF
384test_expect_equal_file EXPECTED OUTPUT
385
386test_begin_subtest "starts-with, mimetype"
387notmuch search --query=sexp '(mimetype (starts-with sig))' | notmuch_search_sanitize > OUTPUT
388cat <<EOF > EXPECTED
389thread:XXX   2009-11-18 [2/2] Lars Kellogg-Stedman; [notmuch] "notmuch help" outputs to stderr? (attachment inbox signed unread)
390thread:XXX   2009-11-18 [4/7] Lars Kellogg-Stedman, Mikhail Gusarov| Keith Packard, Carl Worth; [notmuch] Working with Maildir storage? (inbox signed unread)
391thread:XXX   2009-11-17 [1/3] Adrian Perez de Castro| Keith Packard, Carl Worth; [notmuch] Introducing myself (inbox signed unread)
392EOF
393test_expect_equal_file EXPECTED OUTPUT
394
395add_message '[subject]="message with properties"'
396notmuch restore <<EOF
397#= ${gen_msg_id} foo=bar
398EOF
399
400test_begin_subtest "starts-with, property"
401notmuch search --query=sexp '(property (starts-with foo=))' | notmuch_search_sanitize > OUTPUT
402cat <<EOF > EXPECTED
403thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; message with properties (inbox unread)
404EOF
405test_expect_equal_file EXPECTED OUTPUT
406
407test_begin_subtest "starts-with, subject"
408notmuch search --query=sexp '(subject (starts-with FreeB))' | notmuch_search_sanitize > OUTPUT
409cat <<EOF > EXPECTED
410thread:XXX   2009-11-17 [2/2] Alex Botero-Lowry, Carl Worth; [notmuch] preliminary FreeBSD support (attachment inbox unread)
411EOF
412test_expect_equal_file EXPECTED OUTPUT
413
414test_begin_subtest "starts-with, tag"
415output=$(notmuch search --query=sexp '(tag (starts-with searchby))' | notmuch_search_sanitize)
416test_expect_equal "$output" 'thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by tag (inbox searchbytag unread)'
417
418add_message '[subject]="no tags"'
419notag_mid=${gen_msg_id}
420notmuch tag -unread -inbox id:${notag_mid}
421
422test_begin_subtest "negated starts-with, tag"
423output=$(notmuch search --query=sexp '(tag (not (starts-with in)))' | notmuch_search_sanitize)
424test_expect_equal "$output" 'thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; no tags ()'
425
426test_begin_subtest "negated starts-with, tag 2"
427output=$(notmuch search --query=sexp '(not (tag (starts-with in)))' | notmuch_search_sanitize)
428test_expect_equal "$output" 'thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; no tags ()'
429
430test_begin_subtest "negated starts-with, tag 3"
431output=$(notmuch search --query=sexp '(not (tag (starts-with "")))' | notmuch_search_sanitize)
432test_expect_equal "$output" 'thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; no tags ()'
433
434test_begin_subtest "starts-with, thread"
435notmuch search --query=sexp '(thread (starts-with "00"))' > OUTPUT
436notmuch search '*' > EXPECTED
437test_expect_equal_file EXPECTED OUTPUT
438
439test_begin_subtest "starts-with, to"
440notmuch search --query=sexp '(to (starts-with "search"))' | notmuch_search_sanitize > OUTPUT
441cat <<EOF > EXPECTED
442thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (inbox unread)
443thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (address) (inbox unread)
444thread:XXX   2000-01-01 [1/1] Notmuch Test Suite; search by to (name) (inbox unread)
445EOF
446test_expect_equal_file EXPECTED OUTPUT
447
448test_begin_subtest "wildcard search for 'is'"
449notmuch search not id:${notag_mid} > EXPECTED
450notmuch search --query=sexp '(is *)' > OUTPUT
451test_expect_equal_file EXPECTED OUTPUT
452
453test_begin_subtest "negated wildcard search for 'is'"
454notmuch search id:${notag_mid} > EXPECTED
455notmuch search --query=sexp '(not (is *))' > OUTPUT
456test_expect_equal_file EXPECTED OUTPUT
457
458test_begin_subtest "wildcard search for 'property'"
459notmuch search property:foo=bar > EXPECTED
460notmuch search --query=sexp '(property *)' > OUTPUT
461test_expect_equal_file EXPECTED OUTPUT
462
463test_begin_subtest "wildcard search for 'tag'"
464notmuch search not id:${notag_mid} > EXPECTED
465notmuch search --query=sexp '(tag *)' > OUTPUT
466test_expect_equal_file EXPECTED OUTPUT
467
468test_begin_subtest "negated wildcard search for 'tag'"
469notmuch search id:${notag_mid} > EXPECTED
470notmuch search --query=sexp '(not (tag *))' > OUTPUT
471test_expect_equal_file EXPECTED OUTPUT
472
473add_message '[subject]="message with tag \"*\""'
474notmuch tag '+*' id:${gen_msg_id}
475
476test_begin_subtest "search for 'tag' \"*\""
477output=$(notmuch search --query=sexp --output=messages '(tag "*")')
478test_expect_equal "$output" "id:$gen_msg_id"
479
480test_begin_subtest "search for missing / empty to"
481add_message [to]="undisclosed-recipients:"
482notmuch search --query=sexp '(not (to *))' | notmuch_search_sanitize > OUTPUT
483cat <<EOF > EXPECTED
484thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; search for missing / empty to (inbox unread)
485EOF
486test_expect_equal_file EXPECTED OUTPUT
487
488test_begin_subtest "Unbalanced parens"
489# A code 1 indicates the error was handled (a crash will return e.g. 139).
490test_expect_code 1 "notmuch search --query=sexp '('"
491
492test_begin_subtest "Unbalanced parens, error message"
493notmuch search --query=sexp '(' >OUTPUT 2>&1
494cat <<EOF > EXPECTED
495notmuch search: Syntax error in query
496invalid s-expression: '('
497EOF
498test_expect_equal_file EXPECTED OUTPUT
499
500test_begin_subtest "unknown prefix"
501notmuch search --query=sexp '(foo)' >OUTPUT 2>&1
502cat <<EOF > EXPECTED
503notmuch search: Syntax error in query
504unknown prefix 'foo'
505EOF
506test_expect_equal_file EXPECTED OUTPUT
507
508test_begin_subtest "list as prefix"
509notmuch search --query=sexp '((foo))' >OUTPUT 2>&1
510cat <<EOF > EXPECTED
511notmuch search: Syntax error in query
512unexpected list in field/operation position
513EOF
514test_expect_equal_file EXPECTED OUTPUT
515
516test_begin_subtest "illegal nesting"
517notmuch search --query=sexp '(subject (subject foo))' >OUTPUT 2>&1
518cat <<EOF > EXPECTED
519notmuch search: Syntax error in query
520nested field: 'subject' inside 'subject'
521EOF
522test_expect_equal_file EXPECTED OUTPUT
523
524test_begin_subtest "starts-with, no argument"
525notmuch search --query=sexp '(starts-with)' >OUTPUT 2>&1
526cat <<EOF > EXPECTED
527notmuch search: Syntax error in query
528'starts-with' expects single atom as argument
529EOF
530test_expect_equal_file EXPECTED OUTPUT
531
532test_begin_subtest "starts-with, list argument"
533notmuch search --query=sexp '(starts-with (stuff))' >OUTPUT 2>&1
534cat <<EOF > EXPECTED
535notmuch search: Syntax error in query
536'starts-with' expects single atom as argument
537EOF
538test_expect_equal_file EXPECTED OUTPUT
539
540test_begin_subtest "starts-with, too many arguments"
541notmuch search --query=sexp '(starts-with a b c)' >OUTPUT 2>&1
542cat <<EOF > EXPECTED
543notmuch search: Syntax error in query
544'starts-with' expects single atom as argument
545EOF
546test_expect_equal_file EXPECTED OUTPUT
547
548test_begin_subtest "starts-with, illegal field"
549notmuch search --query=sexp '(body (starts-with foo))' >OUTPUT 2>&1
550cat <<EOF > EXPECTED
551notmuch search: Syntax error in query
552'body' does not support wildcard queries
553EOF
554test_expect_equal_file EXPECTED OUTPUT
555
556test_begin_subtest "wildcard, illegal field"
557notmuch search --query=sexp '(body *)' >OUTPUT 2>&1
558cat <<EOF > EXPECTED
559notmuch search: Syntax error in query
560'body' does not support wildcard queries
561EOF
562test_expect_equal_file EXPECTED OUTPUT
563
564test_begin_subtest "Search, exclude \"deleted\" messages from search"
565notmuch config set search.exclude_tags deleted
566generate_message '[subject]="Not deleted"'
567not_deleted_id=$gen_msg_id
568generate_message '[subject]="Deleted"'
569notmuch new > /dev/null
570notmuch tag +deleted id:$gen_msg_id
571deleted_id=$gen_msg_id
572output=$(notmuch search --query=sexp '(subject deleted)' | notmuch_search_sanitize)
573test_expect_equal "$output" "thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Not deleted (inbox unread)"
574
575test_begin_subtest "Search, exclude \"deleted\" messages from message search --exclude=false"
576output=$(notmuch search --query=sexp --exclude=false --output=messages '(subject deleted)' | notmuch_search_sanitize)
577test_expect_equal "$output" "id:$not_deleted_id
578id:$deleted_id"
579
580test_begin_subtest "Search, exclude \"deleted\" messages from search, overridden"
581notmuch search --query=sexp '(and (subject deleted) (tag deleted))' | notmuch_search_sanitize > OUTPUT
582cat <<EOF > EXPECTED
583thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Deleted (deleted inbox unread)
584EOF
585test_expect_equal_file EXPECTED OUTPUT
586
587test_begin_subtest "Search, exclude \"deleted\" messages from threads"
588add_message '[subject]="Not deleted reply"' '[in-reply-to]="<$gen_msg_id>"'
589output=$(notmuch search --query=sexp '(subject deleted)' | notmuch_search_sanitize)
590test_expect_equal "$output" "thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Not deleted (inbox unread)
591thread:XXX   2001-01-05 [1/2] Notmuch Test Suite; Not deleted reply (deleted inbox unread)"
592
593test_begin_subtest "Search, don't exclude \"deleted\" messages when --exclude=flag specified"
594output=$(notmuch search --query=sexp --exclude=flag '(subject deleted)' | notmuch_search_sanitize)
595test_expect_equal "$output" "thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Not deleted (inbox unread)
596thread:XXX   2001-01-05 [1/2] Notmuch Test Suite; Deleted (deleted inbox unread)"
597
598test_begin_subtest "Search, don't exclude \"deleted\" messages from search if not configured"
599notmuch config set search.exclude_tags
600output=$(notmuch search --query=sexp '(subject deleted)' | notmuch_search_sanitize)
601test_expect_equal "$output" "thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Not deleted (inbox unread)
602thread:XXX   2001-01-05 [2/2] Notmuch Test Suite; Deleted (deleted inbox unread)"
603
604test_begin_subtest "regex at top level"
605notmuch search --query=sexp '(rx foo)' >& OUTPUT
606cat <<EOF > EXPECTED
607notmuch search: Syntax error in query
608illegal 'rx' outside field
609EOF
610test_expect_equal_file EXPECTED OUTPUT
611
612test_begin_subtest "regex in illegal field"
613notmuch search --query=sexp '(body (regex foo))' >& OUTPUT
614cat <<EOF > EXPECTED
615notmuch search: Syntax error in query
616'regex' not supported in field 'body'
617EOF
618test_expect_equal_file EXPECTED OUTPUT
619
620notmuch search --output=messages from:cworth > cworth.msg-ids
621
622test_begin_subtest "regexp 'from' search"
623notmuch search --output=messages --query=sexp '(from (rx cworth))' > OUTPUT
624test_expect_equal_file cworth.msg-ids OUTPUT
625
626test_begin_subtest "regexp search for 'from' 2"
627notmuch search from:/cworth@cworth.org/ and subject:patch | notmuch_search_sanitize > EXPECTED
628notmuch search --query=sexp '(and (from (rx cworth@cworth.org)) (subject patch))' \
629    | notmuch_search_sanitize > OUTPUT
630test_expect_equal_file EXPECTED OUTPUT
631
632test_begin_subtest "regexp 'folder' search"
633notmuch search 'folder:/^bar$/' | notmuch_search_sanitize > EXPECTED
634notmuch search --query=sexp '(folder (rx ^bar$))' | notmuch_search_sanitize > OUTPUT
635test_expect_equal_file EXPECTED OUTPUT
636
637test_begin_subtest "regexp 'id' search"
638notmuch search --output=messages --query=sexp '(id (rx yoom))' > OUTPUT
639test_expect_equal_file cworth.msg-ids OUTPUT
640
641test_begin_subtest "unanchored 'is' search"
642notmuch search tag:signed or tag:inbox > EXPECTED
643notmuch search --query=sexp '(is (rx i))' > OUTPUT
644test_expect_equal_file EXPECTED OUTPUT
645
646test_begin_subtest "anchored 'is' search"
647notmuch search tag:signed > EXPECTED
648notmuch search --query=sexp '(is (rx ^si))' > OUTPUT
649test_expect_equal_file EXPECTED OUTPUT
650
651test_begin_subtest "combine regexp mid and subject"
652notmuch search subject:/-C/ and mid:/y..m/ | notmuch_search_sanitize > EXPECTED
653notmuch search --query=sexp '(and (subject (rx -C)) (mid (rx y..m)))' | notmuch_search_sanitize > OUTPUT
654test_expect_equal_file EXPECTED OUTPUT
655
656test_begin_subtest "regexp 'path' search"
657notmuch search 'path:/^bar$/' | notmuch_search_sanitize > EXPECTED
658notmuch search --query=sexp '(path (rx ^bar$))' | notmuch_search_sanitize > OUTPUT
659test_expect_equal_file EXPECTED OUTPUT
660
661test_begin_subtest "regexp 'property' search"
662notmuch search property:foo=bar > EXPECTED
663notmuch search --query=sexp '(property (rx foo=.*))' > OUTPUT
664test_expect_equal_file EXPECTED OUTPUT
665
666test_begin_subtest "anchored 'tag' search"
667notmuch search tag:signed > EXPECTED
668notmuch search --query=sexp '(tag (rx ^si))' > OUTPUT
669test_expect_equal_file EXPECTED OUTPUT
670
671test_begin_subtest "regexp 'thread' search"
672notmuch search --output=threads '*' | grep '7$' > EXPECTED
673notmuch search --output=threads --query=sexp '(thread (rx 7$))' > OUTPUT
674test_expect_equal_file EXPECTED OUTPUT
675
676test_begin_subtest "Basic query that matches no messages"
677count=$(notmuch count from:keithp and to:keithp)
678test_expect_equal 0 "$count"
679
680test_begin_subtest "Same query against threads"
681notmuch search --query=sexp '(and (thread (of (from keithp))) (thread (matching (to keithp))))' \
682    | notmuch_search_sanitize > OUTPUT
683cat<<EOF > EXPECTED
684thread:XXX   2009-11-18 [7/7] Lars Kellogg-Stedman, Mikhail Gusarov, Keith Packard, Carl Worth; [notmuch] Working with Maildir storage? (inbox signed unread)
685EOF
686test_expect_equal_file EXPECTED OUTPUT
687
688test_begin_subtest "Mix thread and non-threads query"
689notmuch search --query=sexp '(and (thread (matching keithp)) (to keithp))' | notmuch_search_sanitize > OUTPUT
690cat<<EOF > EXPECTED
691thread:XXX   2009-11-18 [1/7] Lars Kellogg-Stedman| Mikhail Gusarov, Keith Packard, Carl Worth; [notmuch] Working with Maildir storage? (inbox signed unread)
692EOF
693test_expect_equal_file EXPECTED OUTPUT
694
695test_begin_subtest "Compound subquery"
696notmuch search --query=sexp '(thread (of (from keithp) (subject Maildir)))' | notmuch_search_sanitize > OUTPUT
697cat<<EOF > EXPECTED
698thread:XXX   2009-11-18 [7/7] Lars Kellogg-Stedman, Mikhail Gusarov, Keith Packard, Carl Worth; [notmuch] Working with Maildir storage? (inbox signed unread)
699EOF
700test_expect_equal_file EXPECTED OUTPUT
701
702test_begin_subtest "empty subquery"
703notmuch search --query=sexp '(thread (of))' 1>OUTPUT 2>&1
704notmuch search '*' > EXPECTED
705test_expect_equal_file EXPECTED OUTPUT
706
707test_begin_subtest "illegal expansion"
708notmuch search --query=sexp '(id (of ego))' 1>OUTPUT 2>&1
709cat<<EOF > EXPECTED
710notmuch search: Syntax error in query
711'of' unsupported inside 'id'
712EOF
713test_expect_equal_file EXPECTED OUTPUT
714
715test_begin_subtest "(folder (of subquery))"
716notmuch search --query=sexp --output=messages '(folder (of (id yun3a4cegoa.fsf@aiko.keithp.com)))' > OUTPUT
717cat <<EOF > EXPECTED
718id:yun1vjwegii.fsf@aiko.keithp.com
719id:yun3a4cegoa.fsf@aiko.keithp.com
720id:1258509400-32511-1-git-send-email-stewart@flamingspork.com
721id:1258506353-20352-1-git-send-email-stewart@flamingspork.com
722id:20091118010116.GC25380@dottiness.seas.harvard.edu
723id:20091118005829.GB25380@dottiness.seas.harvard.edu
724id:cf0c4d610911171136h1713aa59w9cf9aa31f052ad0a@mail.gmail.com
725EOF
726test_expect_equal_file EXPECTED OUTPUT
727
728test_begin_subtest "infix query"
729notmuch search to:searchbyto | notmuch_search_sanitize > EXPECTED
730notmuch search --query=sexp '(infix "to:searchbyto")' |  notmuch_search_sanitize > OUTPUT
731test_expect_equal_file EXPECTED OUTPUT
732
733test_begin_subtest "bad infix query 1"
734notmuch search --query=sexp '(infix "from:/unbalanced")' 2>&1|  notmuch_search_sanitize > OUTPUT
735cat <<EOF > EXPECTED
736notmuch search: Syntax error in query
737Syntax error in infix query: from:/unbalanced
738EOF
739test_expect_equal_file EXPECTED OUTPUT
740
741test_begin_subtest "bad infix query 2"
742notmuch search --query=sexp '(infix "thread:{unbalanced")' 2>&1|  notmuch_search_sanitize > OUTPUT
743cat <<EOF > EXPECTED
744notmuch search: Syntax error in query
745Syntax error in infix query: thread:{unbalanced
746EOF
747test_expect_equal_file EXPECTED OUTPUT
748
749test_begin_subtest "bad infix query 3: bad nesting"
750notmuch search --query=sexp '(subject (infix "tag:inbox"))' 2>&1|  notmuch_search_sanitize > OUTPUT
751cat <<EOF > EXPECTED
752notmuch search: Syntax error in query
753'infix' not supported inside 'subject'
754EOF
755test_expect_equal_file EXPECTED OUTPUT
756
757test_begin_subtest "infix query that matches no messages"
758notmuch search --query=sexp '(and (infix "from:keithp") (infix "to:keithp"))' > OUTPUT
759test_expect_equal_file /dev/null OUTPUT
760
761test_begin_subtest "compound infix query"
762notmuch search date:2009-11-18..2009-11-18 and tag:unread > EXPECTED
763notmuch search --query=sexp  '(infix "date:2009-11-18..2009-11-18 and tag:unread")' > OUTPUT
764test_expect_equal_file EXPECTED OUTPUT
765
766test_begin_subtest "compound infix query 2"
767notmuch search date:2009-11-18..2009-11-18 and tag:unread > EXPECTED
768notmuch search --query=sexp  '(and (infix "date:2009-11-18..2009-11-18") (infix "tag:unread"))' > OUTPUT
769test_expect_equal_file EXPECTED OUTPUT
770
771test_begin_subtest "user header (unknown header)"
772notmuch search --query=sexp '(FooBar)' >& OUTPUT
773cat <<EOF > EXPECTED
774notmuch search: Syntax error in query
775unknown prefix 'FooBar'
776EOF
777test_expect_equal_file EXPECTED OUTPUT
778
779test_begin_subtest "adding user header"
780test_expect_code 0 "notmuch config set index.header.List \"List-Id\""
781
782test_begin_subtest "reindexing"
783test_expect_code 0 'notmuch reindex "*"'
784
785test_begin_subtest "wildcard search for user header"
786grep -Ril List-Id ${MAIL_DIR} | sort | notmuch_dir_sanitize > EXPECTED
787notmuch search --output=files --query=sexp '(List *)' | sort | notmuch_dir_sanitize > OUTPUT
788test_expect_equal_file EXPECTED OUTPUT
789
790test_begin_subtest "wildcard search for user header 2"
791grep -Ril List-Id ${MAIL_DIR} | sort | notmuch_dir_sanitize > EXPECTED
792notmuch search --output=files --query=sexp '(List (starts-with not))' | sort | notmuch_dir_sanitize > OUTPUT
793test_expect_equal_file EXPECTED OUTPUT
794
795test_begin_subtest "search for user header"
796notmuch search List:notmuch | notmuch_search_sanitize > EXPECTED
797notmuch search --query=sexp '(List notmuch)' | notmuch_search_sanitize > OUTPUT
798test_expect_equal_file EXPECTED OUTPUT
799
800test_begin_subtest "search for user header (list token)"
801notmuch search List:notmuch | notmuch_search_sanitize > EXPECTED
802notmuch search --query=sexp '(List notmuch.notmuchmail.org)' | notmuch_search_sanitize > OUTPUT
803test_expect_equal_file EXPECTED OUTPUT
804
805test_begin_subtest "search for user header (quoted string)"
806notmuch search 'List:"notmuch notmuchmail org"' | notmuch_search_sanitize > EXPECTED
807notmuch search --query=sexp '(List "notmuch notmuchmail org")' | notmuch_search_sanitize > OUTPUT
808test_expect_equal_file EXPECTED OUTPUT
809
810test_begin_subtest "search for user header (atoms)"
811notmuch search 'List:"notmuch notmuchmail org"' | notmuch_search_sanitize > EXPECTED
812notmuch search --query=sexp '(List notmuch notmuchmail org)' | notmuch_search_sanitize > OUTPUT
813test_expect_equal_file EXPECTED OUTPUT
814
815test_begin_subtest "check saved query name"
816test_expect_code 1 "notmuch config set squery.test '(subject utf8-sübjéct)'"
817
818test_begin_subtest "roundtrip saved query (database)"
819notmuch config set --database squery.Test '(subject utf8-sübjéct)'
820output=$(notmuch config get squery.Test)
821test_expect_equal "$output" '(subject utf8-sübjéct)'
822
823test_begin_subtest "roundtrip saved query"
824notmuch config set squery.Test '(subject override subject)'
825output=$(notmuch config get squery.Test)
826test_expect_equal "$output" '(subject override subject)'
827
828test_begin_subtest "unknown saved query"
829notmuch search --query=sexp '(Unknown foo bar)' >OUTPUT 2>&1
830cat <<EOF > EXPECTED
831notmuch search: Syntax error in query
832unknown prefix 'Unknown'
833EOF
834test_expect_equal_file EXPECTED OUTPUT
835
836test_begin_subtest "syntax error in saved query"
837notmuch config set squery.Bad '(Bad'
838notmuch search --query=sexp '(Bad foo bar)' >OUTPUT 2>&1
839cat <<EOF > EXPECTED
840notmuch search: Syntax error in query
841invalid saved s-expression query: '(Bad'
842EOF
843test_expect_equal_file EXPECTED OUTPUT
844
845test_begin_subtest "Saved Search by 'tag' and 'subject'"
846notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED
847notmuch config set squery.TagSubject  '(and (tag inbox) (subject maildir))'
848notmuch search --query=sexp '(TagSubject)' | notmuch_search_sanitize > OUTPUT
849test_expect_equal_file EXPECTED OUTPUT
850
851test_begin_subtest "Saved Search: illegal nesting"
852notmuch config set squery.TagSubject  '(and (tag inbox) (subject maildir))'
853notmuch search --query=sexp '(subject (TagSubject))' >OUTPUT 2>&1
854cat <<EOF > EXPECTED
855notmuch search: Syntax error in query
856nested field: 'tag' inside 'subject'
857EOF
858test_expect_equal_file EXPECTED OUTPUT
859
860test_begin_subtest "Saved Search: list as prefix"
861notmuch config set squery.Bad2  '((and) (tag inbox) (subject maildir))'
862notmuch search --query=sexp '(Bad2)' >OUTPUT 2>&1
863cat <<EOF > EXPECTED
864notmuch search: Syntax error in query
865unexpected list in field/operation position
866EOF
867test_expect_equal_file EXPECTED OUTPUT
868
869test_begin_subtest "Saved Search: bad parameter syntax"
870notmuch config set squery.Bad3  '(macro a b)'
871notmuch search --query=sexp '(Bad3)' >OUTPUT 2>&1
872cat <<EOF > EXPECTED
873notmuch search: Syntax error in query
874missing (possibly empty) list of arguments to macro
875EOF
876test_expect_equal_file EXPECTED OUTPUT
877
878test_begin_subtest "Saved Search: bad parameter syntax 2"
879notmuch config set squery.Bad4  '(macro ((a b)) a)'
880notmuch search --query=sexp '(Bad4 1)' >OUTPUT 2>&1
881cat <<EOF > EXPECTED
882notmuch search: Syntax error in query
883macro parameters must be unquoted atoms
884EOF
885test_expect_equal_file EXPECTED OUTPUT
886
887test_begin_subtest "Saved Search: bad parameter syntax 3"
888notmuch config set squery.Bad5  '(macro (a b) a)'
889notmuch search --query=sexp '(Bad5 1)' >OUTPUT 2>&1
890cat <<EOF > EXPECTED
891notmuch search: Syntax error in query
892too few arguments to macro
893EOF
894test_expect_equal_file EXPECTED OUTPUT
895
896test_begin_subtest "Saved Search: bad parameter syntax 4"
897notmuch search --query=sexp '(Bad5 1 2 3)' >OUTPUT 2>&1
898cat <<EOF > EXPECTED
899notmuch search: Syntax error in query
900too many arguments to macro
901EOF
902test_expect_equal_file EXPECTED OUTPUT
903
904test_begin_subtest "Saved Search: macro without body"
905notmuch config set squery.Bad3  '(macro (a b))'
906notmuch search --query=sexp '(Bad3)' >OUTPUT 2>&1
907cat <<EOF > EXPECTED
908notmuch search: Syntax error in query
909missing body of macro
910EOF
911test_expect_equal_file EXPECTED OUTPUT
912
913test_begin_subtest "macro in query"
914notmuch search --query=sexp '(macro (a) (and ,b (subject maildir)))' >OUTPUT 2>&1
915cat <<EOF > EXPECTED
916notmuch search: Syntax error in query
917macro definition not permitted here
918EOF
919test_expect_equal_file EXPECTED OUTPUT
920
921test_begin_subtest "zero argument macro"
922notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED
923notmuch config set squery.TagSubject0  '(macro () (and (tag inbox) (subject maildir)))'
924notmuch search --query=sexp '(TagSubject0)' | notmuch_search_sanitize > OUTPUT
925test_expect_equal_file EXPECTED OUTPUT
926
927test_begin_subtest "undefined argument"
928notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED
929notmuch config set squery.Bad6  '(macro (a) (and ,b (subject maildir)))'
930notmuch search --query=sexp '(Bad6 foo)' >OUTPUT 2>&1
931cat <<EOF > EXPECTED
932notmuch search: Syntax error in query
933undefined parameter b
934EOF
935test_expect_equal_file EXPECTED OUTPUT
936
937test_begin_subtest "Single argument macro"
938notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED
939notmuch config set squery.TagSubject1  '(macro (tagname) (and (tag ,tagname) (subject maildir)))'
940notmuch search --query=sexp '(TagSubject1 inbox)' | notmuch_search_sanitize > OUTPUT
941test_expect_equal_file EXPECTED OUTPUT
942
943test_begin_subtest "Single argument macro, list argument"
944notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED
945notmuch config set squery.ThingSubject  '(macro (thing) (and ,thing (subject maildir)))'
946notmuch search --query=sexp '(ThingSubject (tag inbox))' | notmuch_search_sanitize > OUTPUT
947test_expect_equal_file EXPECTED OUTPUT
948
949test_begin_subtest "two argument macro"
950notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED
951notmuch config set squery.TagSubject2  '(macro (tagname subj) (and (tag ,tagname) (subject ,subj)))'
952notmuch search --query=sexp '(TagSubject2 inbox maildir)' | notmuch_search_sanitize > OUTPUT
953test_expect_equal_file EXPECTED OUTPUT
954
955test_begin_subtest "nested macros (shadowing)"
956notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED
957notmuch config set squery.Inner '(macro (x) (subject ,x))'
958notmuch config set squery.Outer  '(macro (x y) (and (tag ,x) (Inner ,y)))'
959notmuch search --query=sexp '(Outer inbox maildir)' | notmuch_search_sanitize > OUTPUT
960test_expect_equal_file EXPECTED OUTPUT
961
962test_begin_subtest "nested macros (no dynamic scope)"
963notmuch config set squery.Inner2 '(macro (x) (subject ,y))'
964notmuch config set squery.Outer2  '(macro (x y) (and (tag ,x) (Inner2 ,y)))'
965notmuch search --query=sexp '(Outer2 inbox maildir)' > OUTPUT 2>&1
966cat <<EOF > EXPECTED
967notmuch search: Syntax error in query
968undefined parameter y
969EOF
970test_expect_equal_file EXPECTED OUTPUT
971
972test_begin_subtest "combine macro and user defined header"
973notmuch config set squery.About '(macro (name) (or (subject ,name) (List ,name)))'
974notmuch search subject:notmuch or List:notmuch | notmuch_search_sanitize > EXPECTED
975notmuch search --query=sexp '(About notmuch)' | notmuch_search_sanitize > OUTPUT
976test_expect_equal_file EXPECTED OUTPUT
977
978
979test_begin_subtest "combine macro and user defined header"
980notmuch config set squery.About '(macro (name) (or (subject ,name) (List ,name)))'
981notmuch search subject:notmuch or List:notmuch | notmuch_search_sanitize > EXPECTED
982notmuch search --query=sexp '(About notmuch)' | notmuch_search_sanitize > OUTPUT
983test_expect_equal_file EXPECTED OUTPUT
984
985
986test_done
987