-#!/bin/sh
+#!/bin/bash
set -e
find_notmuch_binary ()
#
# The filename of the message generated is available as
# $gen_msg_filename
+#
+# This function supports named parameters with the bash syntax for
+# assigning a value to an associative array ([name]=value). The
+# supported parameters are:
+#
+# [dir]=directory/of/choice
+#
+# Generate the message in directory 'directory/of/choice' within
+# the mail store. The directory will be created if necessary.
+#
+# [body]=text
+#
+# Text to use as the body of the email message
+#
+# '[from]="Some User <user@example.com>"'
+# '[to]="Some User <user@example.com>"'
+# '[subject]="Subject of email message"'
+#
+# Values for email headers. If not provided, default values will
+# be generated instead.
+#
+# '[cc]="Some User <user@example.com>"'
+# [in-reply-to]=<message-id>
+#
+# Additional values for email headers. If these are not provided
+# then the relevant headers will simply not appear in the
+# message.
gen_msg_cnt=0
gen_msg_filename=""
generate_message ()
{
+ # This is our (bash-specific) magic for doing named parameters
+ local -A template="($@)"
+ local additional_headers
+
gen_msg_cnt=$((gen_msg_cnt + 1))
gen_msg_name=msg-$(printf "%03d" $gen_msg_cnt)
- if [ "$#" = "0" ]; then
+ if [ -z "${template[dir]}" ]; then
gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
else
- gen_msg_filename="${MAIL_DIR}/$1/$gen_msg_name"
+ gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
mkdir -p $(dirname $gen_msg_filename)
fi
+ if [ -z "${template[body]}" ]; then
+ template[body]="This is just a test message at ${gen_msg_filename}"
+ fi
+
+ if [ -z "${template[from]}" ]; then
+ template[from]="Notmuch Test Suite <test_suite@notmuchmail.org>"
+ fi
+
+ if [ -z "${template[to]}" ]; then
+ template[to]="Notmuch Test Suite <test_suite@notmuchmail.org>"
+ fi
+
+ if [ -z "${template[subject]}" ]; then
+ template[subject]="Test message ${gen_msg_filename}"
+ fi
+
+ additional_headers=""
+ if [ ! -z "${template[cc]}" ]; then
+ additional_headers="Cc: ${template[cc]}
+${additional_headers}"
+ fi
+
+ if [ ! -z "${template[in-reply-to]}" ]; then
+ additional_headers="In-Reply-To: ${template[in-reply-to]}
+${additional_headers}"
+ fi
+
cat <<EOF >$gen_msg_filename
-From: Notmuch Test Suite <test_suite@notmuchmail.org>
-To: Notmuch Test Suite <test_suite@notmuchmail.org>
+From: ${template[from]}
+To: ${template[to]}
Message-Id: <msg-${gen_msg_cnt}@notmuch-test-suite>
-Subject: Test message ${gen_msg_filename}
+Subject: ${template[subject]}
Date: Tue, 05 Jan 2010 15:43:57 -0800
-
-This is just a test message at ${gen_msg_filename}
+${additional_headers}
+${template[body]}
EOF
}
rm -rf ${MAIL_DIR}/* ${MAIL_DIR}/.notmuch
mkdir ${MAIL_DIR}/def
mkdir ${MAIL_DIR}/ghi
-generate_message def
+generate_message [dir]=def
execute_expecting new "Added 1 new message to the database."
rm -rf ${MAIL_DIR}/.notmuch
mv ${MAIL_DIR}/ghi ${MAIL_DIR}/abc
rm ${MAIL_DIR}/def/*
-generate_message abc
+generate_message [dir]=abc
execute_expecting new "Added 1 new message to the database."
printf " Renamed directory...\t\t"
do_sleep
-generate_message dir
-generate_message dir
-generate_message dir
+generate_message [dir]=dir
+generate_message [dir]=dir
+generate_message [dir]=dir
$NOTMUCH new > /dev/null
printf " New directory (at end of list)... "
do_sleep
-generate_message zzz
-generate_message zzz
-generate_message zzz
+generate_message [dir]=zzz
+generate_message [dir]=zzz
+generate_message [dir]=zzz
execute_expecting new "Added 3 new messages to the database."
printf " New two-level directory...\t"
do_sleep
-generate_message two/levels
-generate_message two/levels
-generate_message two/levels
+generate_message [dir]=two/levels
+generate_message [dir]=two/levels
+generate_message [dir]=two/levels
execute_expecting new "Added 3 new messages to the database."