2 test_description='"notmuch count" for messages and threads'
3 . $(dirname "$0")/test-lib.sh || exit 1
7 # Note: The 'wc -l' results below are wrapped in arithmetic evaluation
8 # $((...)) to strip whitespace. This is for portability, as 'wc -l'
9 # emits whitespace on some BSD variants.
11 test_begin_subtest "message count is the default for notmuch count"
13 "$((`notmuch search --output=messages '*' | wc -l`))" \
16 test_begin_subtest "message count with --output=messages"
18 "$((`notmuch search --output=messages '*' | wc -l`))" \
19 "`notmuch count --output=messages '*'`"
21 test_begin_subtest "thread count with --output=threads"
23 "$((`notmuch search --output=threads '*' | wc -l`))" \
24 "`notmuch count --output=threads '*'`"
26 test_begin_subtest "thread count is the default for notmuch search"
28 "$((`notmuch search '*' | wc -l`))" \
29 "`notmuch count --output=threads '*'`"
31 test_begin_subtest "files count"
33 "$((`notmuch search --output=files '*' | wc -l`))" \
34 "`notmuch count --output=files '*'`"
36 test_begin_subtest "files count for a duplicate message-id"
39 "`notmuch count --output=files id:20091117232137.GA7669@griffis1.net`"
41 test_begin_subtest "count with no matching messages"
44 "`notmuch count --output=messages from:cworth and not from:cworth`"
46 test_begin_subtest "count with no matching threads"
49 "`notmuch count --output=threads from:cworth and not from:cworth`"
51 test_begin_subtest "message count is the default for batch count"
52 notmuch count --batch >OUTPUT <<EOF
56 notmuch count --output=messages >EXPECTED
57 notmuch count --output=messages from:cworth >>EXPECTED
58 test_expect_equal_file EXPECTED OUTPUT
60 test_begin_subtest "batch message count"
61 notmuch count --batch --output=messages >OUTPUT <<EOF
66 notmuch count --output=messages from:cworth >EXPECTED
67 notmuch count --output=messages >>EXPECTED
68 notmuch count --output=messages tag:inbox >>EXPECTED
69 test_expect_equal_file EXPECTED OUTPUT
71 test_begin_subtest "batch thread count"
72 notmuch count --batch --output=threads >OUTPUT <<EOF
75 from:cworth and not from:cworth
78 notmuch count --output=threads >EXPECTED
79 notmuch count --output=threads from:cworth >>EXPECTED
80 notmuch count --output=threads from:cworth and not from:cworth >>EXPECTED
81 notmuch count --output=threads foo >>EXPECTED
82 test_expect_equal_file EXPECTED OUTPUT
84 test_begin_subtest "batch message count with input file"
90 notmuch count --input=INPUT --output=messages >OUTPUT
91 notmuch count --output=messages from:cworth >EXPECTED
92 notmuch count --output=messages >>EXPECTED
93 notmuch count --output=messages tag:inbox >>EXPECTED
94 test_expect_equal_file EXPECTED OUTPUT
97 test_begin_subtest "error message for database open"
98 target=(${MAIL_DIR}/.notmuch/xapian/postlist.*)
99 dd if=/dev/zero of="$target" count=3
100 notmuch count '*' 2>OUTPUT 1>/dev/null
101 output=$(sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' OUTPUT)
102 test_expect_equal "${output}" "A Xapian exception occurred opening database"
105 make_shim qsm-shim<<EOF
106 #include <notmuch-test.h>
108 WRAP_DLFUNC (notmuch_status_t, notmuch_query_search_messages, (notmuch_query_t *query, notmuch_messages_t **messages))
110 /* XXX WARNING THIS CORRUPTS THE DATABASE */
111 int fd = open ("target_postlist", O_WRONLY|O_TRUNC);
116 return notmuch_query_search_messages_orig(query, messages);
121 test_begin_subtest "error message from query_search_messages"
122 ln -s ${MAIL_DIR}/.notmuch/xapian/postlist.* target_postlist
123 notmuch_with_shim qsm-shim count --output=files '*' 2>OUTPUT 1>/dev/null
125 notmuch count: A Xapian exception occurred
126 A Xapian exception occurred performing query
129 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
130 test_expect_equal_file EXPECTED OUTPUT.clean
133 test_begin_subtest "count library function is non-destructive"
145 query_string = 'tag:inbox or tag:spam'
148 database = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
149 query = notmuch.Query(database, query_string)
151 print("1: {} messages".format(query.count_messages()))
152 print("2: {} messages".format(query.count_messages()))
153 print("Exclude '{}'".format(tag_string))
154 query.exclude_tag(tag_string)
155 print("3: {} messages".format(query.count_messages()))
156 print("4: {} messages".format(query.count_messages()))
158 test_expect_equal_file EXPECTED OUTPUT
160 if [ $NOTMUCH_HAVE_SFSEXP -eq 1 ]; then
162 test_begin_subtest "and of exact terms (query=sexp)"
163 output=$(notmuch count --query=sexp '(and "wonderful" "wizard")')
164 test_expect_equal "$output" 1
166 test_begin_subtest "or of exact terms (query=sexp)"
167 output=$(notmuch count --query=sexp '(or "php" "wizard")')
168 test_expect_equal "$output" 2
170 test_begin_subtest "starts-with, case-insensitive (query=sexp)"
171 output=$(notmuch count --query=sexp '(starts-with FreeB)')
172 test_expect_equal "$output" 5
174 test_begin_subtest "query that matches no messages (query=sexp)"
175 count=$(notmuch count --query=sexp '(and (from keithp) (to keithp))')
176 test_expect_equal 0 "$count"
178 test_begin_subtest "Compound subquery (query=sexp)"
179 output=$(notmuch count --query=sexp '(thread (of (from keithp) (subject Maildir)))')
180 test_expect_equal "$output" 7