]> git.notmuchmail.org Git - notmuch/blob - test/T060-count.sh
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / test / T060-count.sh
1 #!/usr/bin/env bash
2 test_description='"notmuch count" for messages and threads'
3 . $(dirname "$0")/test-lib.sh || exit 1
4
5 add_email_corpus
6
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.
10
11 test_begin_subtest "message count is the default for notmuch count"
12 test_expect_equal \
13     "$((`notmuch search --output=messages '*' | wc -l`))" \
14     "`notmuch count '*'`"
15
16 test_begin_subtest "message count with --output=messages"
17 test_expect_equal \
18     "$((`notmuch search --output=messages '*' | wc -l`))" \
19     "`notmuch count --output=messages '*'`"
20
21 test_begin_subtest "thread count with --output=threads"
22 test_expect_equal \
23     "$((`notmuch search --output=threads '*' | wc -l`))" \
24     "`notmuch count --output=threads '*'`"
25
26 test_begin_subtest "thread count is the default for notmuch search"
27 test_expect_equal \
28     "$((`notmuch search '*' | wc -l`))" \
29     "`notmuch count --output=threads '*'`"
30
31 test_begin_subtest "files count"
32 test_expect_equal \
33     "$((`notmuch search --output=files '*' | wc -l`))" \
34     "`notmuch count --output=files '*'`"
35
36 test_begin_subtest "files count for a duplicate message-id"
37 test_expect_equal \
38     "2" \
39     "`notmuch count --output=files id:20091117232137.GA7669@griffis1.net`"
40
41 test_begin_subtest "count with no matching messages"
42 test_expect_equal \
43     "0" \
44     "`notmuch count --output=messages from:cworth and not from:cworth`"
45
46 test_begin_subtest "count with no matching threads"
47 test_expect_equal \
48     "0" \
49     "`notmuch count --output=threads from:cworth and not from:cworth`"
50
51 test_begin_subtest "message count is the default for batch count"
52 notmuch count --batch >OUTPUT <<EOF
53
54 from:cworth
55 EOF
56 notmuch count --output=messages >EXPECTED
57 notmuch count --output=messages from:cworth >>EXPECTED
58 test_expect_equal_file EXPECTED OUTPUT
59
60 test_begin_subtest "batch message count"
61 notmuch count --batch --output=messages >OUTPUT <<EOF
62 from:cworth
63
64 tag:inbox
65 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
70
71 test_begin_subtest "batch thread count"
72 notmuch count --batch --output=threads >OUTPUT <<EOF
73
74 from:cworth
75 from:cworth and not from:cworth
76 foo
77 EOF
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
83
84 test_begin_subtest "batch message count with input file"
85 cat >INPUT <<EOF
86 from:cworth
87
88 tag:inbox
89 EOF
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
95
96 backup_database
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"
103 restore_database
104
105 make_shim qsm-shim<<EOF
106 #include <notmuch-test.h>
107
108 WRAP_DLFUNC (notmuch_status_t, notmuch_query_search_messages, (notmuch_query_t *query, notmuch_messages_t **messages))
109
110   /* XXX WARNING THIS CORRUPTS THE DATABASE */
111   int fd = open ("target_postlist", O_WRONLY|O_TRUNC);
112   if (fd < 0)
113     exit (8);
114   close (fd);
115
116   return notmuch_query_search_messages_orig(query, messages);
117 }
118 EOF
119
120 backup_database
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
124 cat <<EOF > EXPECTED
125 notmuch count: A Xapian exception occurred
126 A Xapian exception occurred performing query
127 Query string was: *
128 EOF
129 sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean
130 test_expect_equal_file EXPECTED OUTPUT.clean
131 restore_database
132
133 test_begin_subtest "count library function is non-destructive"
134 cat<<EOF > EXPECTED
135 1: 52 messages
136 2: 52 messages
137 Exclude 'spam'
138 3: 52 messages
139 4: 52 messages
140 EOF
141 test_python <<EOF
142 import sys
143 import notmuch
144
145 query_string = 'tag:inbox or tag:spam'
146 tag_string = 'spam'
147
148 database = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
149 query = notmuch.Query(database, query_string)
150
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()))
157 EOF
158 test_expect_equal_file EXPECTED OUTPUT
159
160 if [ "${NOTMUCH_HAVE_SFSEXP-0}" = "1" ]; then
161
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
165
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
169
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
173
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"
177
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
181
182 fi
183
184 test_done