1#!/usr/bin/env bash
2test_description='Configuration of mail-root and database path'
3. $(dirname "$0")/test-lib.sh || exit 1
4
5test_require_external_prereq xapian-metdata
6
7backup_config () {
8    local test_name=$(basename $0 .sh)
9    cp ${NOTMUCH_CONFIG} notmuch-config-backup.${test_name}
10}
11
12restore_config () {
13    local test_name=$(basename $0 .sh)
14    export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
15    unset CONFIG_PATH
16    unset DATABASE_PATH
17    unset NOTMUCH_PROFILE
18    unset XAPIAN_PATH
19    unset MAILDIR
20    rm -f "$HOME/mail"
21    cp notmuch-config-backup.${test_name} ${NOTMUCH_CONFIG}
22}
23
24split_config () {
25    local dir
26    backup_config
27    dir="$TMP_DIRECTORY/database.$test_count"
28    rm -rf $dir
29    mkdir $dir
30    notmuch config set database.path $dir
31    notmuch config set database.mail_root $MAIL_DIR
32    DATABASE_PATH=$dir
33    XAPIAN_PATH="$dir/xapian"
34}
35
36symlink_config () {
37    local dir
38    backup_config
39    dir="$TMP_DIRECTORY/link.$test_count"
40    ln -s $MAIL_DIR $dir
41    notmuch config set database.path $dir
42    notmuch config set database.mail_root $MAIL_DIR
43    XAPIAN_PATH="$MAIL_DIR/.notmuch/xapian"
44    unset DATABASE_PATH
45}
46
47
48home_mail_config () {
49    local dir
50    backup_config
51    dir="${HOME}/mail"
52    ln -s $MAIL_DIR $dir
53    notmuch config set database.path
54    notmuch config set database.mail_root
55    XAPIAN_PATH="$MAIL_DIR/.notmuch/xapian"
56    unset DATABASE_PATH
57}
58
59maildir_env_config () {
60    local dir
61    backup_config
62    dir="${HOME}/env_points_here"
63    ln -s $MAIL_DIR $dir
64    export MAILDIR=$dir
65    notmuch config set database.path
66    notmuch config set database.mail_root
67    XAPIAN_PATH="${MAIL_DIR}/.notmuch/xapian"
68    unset DATABASE_PATH
69}
70
71xdg_config () {
72    local dir
73    local profile=${1:-default}
74
75    if [[ $profile != default ]]; then
76	export NOTMUCH_PROFILE=$profile
77    fi
78
79    backup_config
80    DATABASE_PATH="${HOME}/.local/share/notmuch/${profile}"
81    rm -rf $DATABASE_PATH
82    mkdir -p $DATABASE_PATH
83
84    config_dir="${HOME}/.config/notmuch/${profile}"
85    mkdir -p ${config_dir}
86    CONFIG_PATH=$config_dir/config
87    mv ${NOTMUCH_CONFIG} $CONFIG_PATH
88    unset NOTMUCH_CONFIG
89
90    XAPIAN_PATH="${DATABASE_PATH}/xapian"
91    notmuch --config=${CONFIG_PATH} config set database.mail_root ${TMP_DIRECTORY}/mail
92    notmuch --config=${CONFIG_PATH} config set database.path
93}
94
95for config in traditional split XDG XDG+profile symlink home_mail maildir_env; do
96    #start each set of tests with an known set of messages
97    add_email_corpus
98
99    case $config in
100	traditional)
101	    backup_config
102	    XAPIAN_PATH="$MAIL_DIR/.notmuch/xapian"
103	    ;;
104	split)
105	    split_config
106	    mv mail/.notmuch/xapian $DATABASE_PATH
107	    ;;
108	XDG)
109	    xdg_config
110	    mv mail/.notmuch/xapian $DATABASE_PATH
111	    ;;
112	XDG+profile)
113	    xdg_config ${RANDOM}
114	    mv mail/.notmuch/xapian $DATABASE_PATH
115	    ;;
116	symlink)
117	    symlink_config
118	    ;;
119	home_mail)
120	    home_mail_config
121	    ;;
122	maildir_env)
123	    maildir_env_config
124	    ;;
125    esac
126
127    test_begin_subtest "count ($config)"
128    output=$(notmuch count '*')
129    test_expect_equal "$output" '52'
130
131    test_begin_subtest "count+tag ($config)"
132    tag="tag${RANDOM}"
133    notmuch tag +$tag '*'
134    output=$(notmuch count tag:$tag)
135    notmuch tag -$tag '*'
136    test_expect_equal "$output" '52'
137
138    test_begin_subtest "address ($config)"
139    notmuch address --deduplicate=no --sort=newest-first --output=sender --output=recipients path:foo >OUTPUT
140    cat <<EOF >EXPECTED
141Carl Worth <cworth@cworth.org>
142notmuch@notmuchmail.org
143EOF
144    test_expect_equal_file EXPECTED OUTPUT
145
146    test_begin_subtest "dump ($config)"
147    notmuch dump is:attachment and is:signed | sort > OUTPUT
148    cat <<EOF > EXPECTED
149#notmuch-dump batch-tag:3 config,properties,tags
150+attachment +inbox +signed +unread -- id:20091118005829.GB25380@dottiness.seas.harvard.edu
151+attachment +inbox +signed +unread -- id:20091118010116.GC25380@dottiness.seas.harvard.edu
152EOF
153    test_expect_equal_file EXPECTED OUTPUT
154
155    test_begin_subtest "dump + tag + restore ($config)"
156    notmuch dump '*' > EXPECTED
157    notmuch tag -inbox '*'
158    notmuch restore < EXPECTED
159    notmuch dump > OUTPUT
160    test_expect_equal_file_nonempty EXPECTED OUTPUT
161
162    test_begin_subtest "reindex ($config)"
163    notmuch search --output=messages '*' > EXPECTED
164    notmuch reindex '*'
165    notmuch search --output=messages '*' > OUTPUT
166    test_expect_equal_file_nonempty EXPECTED OUTPUT
167
168    test_begin_subtest "use existing database ($config)"
169    output=$(notmuch new)
170    test_expect_equal "$output" 'No new mail.'
171
172    test_begin_subtest "create database ($config)"
173    rm -rf $DATABASE_PATH/{.notmuch,}/xapian
174    notmuch new
175    output=$(notmuch count '*')
176    test_expect_equal "$output" '52'
177
178    test_begin_subtest "detect new files ($config)"
179    generate_message
180    generate_message
181    notmuch new
182    output=$(notmuch count '*')
183    test_expect_equal "$output" '54'
184
185    test_begin_subtest "Show a raw message ($config)"
186    add_message
187    notmuch show --format=raw id:$gen_msg_id > OUTPUT
188    test_expect_equal_file_nonempty $gen_msg_filename OUTPUT
189    rm -f $gen_msg_filename
190
191    test_begin_subtest "reply ($config)"
192    add_message '[from]="Sender <sender@example.com>"' \
193		[to]=test_suite@notmuchmail.org \
194		[subject]=notmuch-reply-test \
195		'[date]="Tue, 05 Jan 2010 15:43:56 -0000"' \
196		'[body]="basic reply test"'
197    notmuch reply id:${gen_msg_id} 2>&1 > OUTPUT
198    cat <<EOF > EXPECTED
199From: Notmuch Test Suite <test_suite@notmuchmail.org>
200Subject: Re: notmuch-reply-test
201To: Sender <sender@example.com>
202In-Reply-To: <${gen_msg_id}>
203References: <${gen_msg_id}>
204
205On Tue, 05 Jan 2010 15:43:56 -0000, Sender <sender@example.com> wrote:
206> basic reply test
207EOF
208    test_expect_equal_file EXPECTED OUTPUT
209
210    test_begin_subtest "insert+search ($config)"
211    generate_message \
212	"[subject]=\"insert-subject\"" \
213	"[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" \
214	"[body]=\"insert-message\""
215    mkdir -p "$MAIL_DIR"/{cur,new,tmp}
216    notmuch insert < "$gen_msg_filename"
217    cur_msg_filename=$(notmuch search --output=files "subject:insert-subject")
218    test_expect_equal_file_nonempty "$cur_msg_filename" "$gen_msg_filename"
219
220    test_begin_subtest "compact+search ($config)"
221    notmuch search --output=messages '*' | sort > EXPECTED
222    notmuch compact
223    notmuch search --output=messages '*' | sort > OUTPUT
224    test_expect_equal_file_nonempty EXPECTED OUTPUT
225
226    test_begin_subtest "upgrade backup ($config)"
227    features=$(xapian-metadata get $XAPIAN_PATH features | grep -v "^relative directory paths")
228    xapian-metadata set $XAPIAN_PATH features "$features"
229    output=$(notmuch new | grep Welcome)
230    test_expect_equal \
231	"$output" \
232	"Welcome to a new version of notmuch! Your database will now be upgraded."
233
234    test_begin_subtest "notmuch +config -database suggests notmuch new ($config)"
235    mv "$XAPIAN_PATH" "${XAPIAN_PATH}.bak"
236    notmuch > OUTPUT
237cat <<EOF > EXPECTED
238Notmuch is configured, but no database was found.
239You probably want to run "notmuch new" now to create a database.
240
241Note that the first run of "notmuch new" can take a very long time
242and that the resulting database will use roughly the same amount of
243storage space as the email being indexed.
244
245EOF
246    mv "${XAPIAN_PATH}.bak" "$XAPIAN_PATH"
247
248   test_expect_equal_file EXPECTED OUTPUT
249
250   test_begin_subtest "Set config value ($config)"
251   name=${RANDOM}
252   value=${RANDOM}
253   notmuch config set test${test_count}.${name} ${value}
254   output=$(notmuch config get test${test_count}.${name})
255   notmuch config set test${test_count}.${name}
256   output2=$(notmuch config get test${test_count}.${name})
257   test_expect_equal "${output}+${output2}" "${value}+"
258
259   test_begin_subtest "Set config value in database ($config)"
260   name=${RANDOM}
261   value=${RANDOM}
262   notmuch config set --database test${test_count}.${name} ${value}
263   output=$(notmuch config get test${test_count}.${name})
264   notmuch config set --database test${test_count}.${name}
265   output2=$(notmuch config get test${test_count}.${name})
266   test_expect_equal "${output}+${output2}" "${value}+"
267
268   test_begin_subtest "Config list ($config)"
269   notmuch config list | notmuch_config_sanitize | \
270       sed -e "s/^database.backup_dir=.*$/database.backup_dir/"  \
271	   -e "s/^database.hook_dir=.*$/database.hook_dir/" \
272	   -e "s/^database.path=.*$/database.path/"  \
273	   -e "s,^database.mail_root=CWD/home/mail,database.mail_root=MAIL_DIR," \
274	   -e "s,^database.mail_root=CWD/home/env_points_here,database.mail_root=MAIL_DIR," \
275	   > OUTPUT
276   cat <<EOF > EXPECTED
277built_with.compact=something
278built_with.field_processor=something
279built_with.retry_lock=something
280built_with.sexpr_query=something
281database.autocommit=8000
282database.backup_dir
283database.hook_dir
284database.mail_root=MAIL_DIR
285database.path
286maildir.synchronize_flags=true
287new.ignore=
288new.tags=unread;inbox
289search.exclude_tags=
290user.name=Notmuch Test Suite
291user.other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
292user.primary_email=test_suite@notmuchmail.org
293EOF
294   test_expect_equal_file EXPECTED OUTPUT
295
296   case $config in
297       XDG*)
298	   test_begin_subtest "Set shadowed config value in database ($config)"
299	   name=${RANDOM}
300	   value=${RANDOM}
301	   key=test${test_count}.${name}
302	   notmuch config set --database ${key}  ${value}
303	   notmuch config set ${key} shadow${value}
304	   output=$(notmuch --config='' config get ${key})
305	   notmuch config set --database ${key}
306	   output2=$(notmuch --config='' config get ${key})
307	   notmuch config set ${key}
308	   test_expect_equal "${output}+${output2}" "${value}+"
309	   ;&
310       split)
311	   test_begin_subtest "'to' header does not crash (python-cffi) ($config)"
312	   echo 'notmuch@notmuchmail.org' > EXPECTED
313	   test_python <<EOF
314from notmuch2 import Database
315db=Database(config=Database.CONFIG.SEARCH)
316m=db.find('20091117232137.GA7669@griffis1.net')
317to=m.header('To')
318print(to)
319EOF
320	   test_expect_equal_file EXPECTED OUTPUT
321	   ;& # fall through
322   esac
323
324   case $config in
325       split|XDG*)
326   esac
327   restore_config
328   rm -rf home/.local
329   rm -rf home/.config
330done
331
332test_done
333