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