]> git.notmuchmail.org Git - notmuch/blobdiff - test/test-lib.sh
notmuch-show: include Bcc header in json output
[notmuch] / test / test-lib.sh
index 063a2b273049c18a9ba95cbede3348063a174d3b..e7638bee9f417c05f7ca37cb2febfda6ee360e0a 100644 (file)
@@ -318,7 +318,11 @@ generate_message ()
     fi
 
     if [ -z "${template[subject]}" ]; then
-       template[subject]="Test message #${gen_msg_cnt}"
+       if [ -n "$test_subtest_name" ]; then
+           template[subject]="$test_subtest_name"
+       else
+           template[subject]="Test message #${gen_msg_cnt}"
+       fi
     fi
 
     if [ -z "${template[date]}" ]; then
@@ -346,6 +350,11 @@ ${additional_headers}"
 ${additional_headers}"
     fi
 
+    if [ ! -z "${template[bcc]}" ]; then
+       additional_headers="Bcc: ${template[bcc]}
+${additional_headers}"
+    fi
+
     if [ ! -z "${template[references]}" ]; then
        additional_headers="References: ${template[references]}
 ${additional_headers}"
@@ -356,6 +365,11 @@ ${additional_headers}"
 ${additional_headers}"
     fi
 
+    if [ ! -z "${template[content-transfer-encoding]}" ]; then
+       additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
+${additional_headers}"
+    fi
+
     # Note that in the way we're setting it above and using it below,
     # `additional_headers' will also serve as the header / body separator
     # (empty line in between).
@@ -394,8 +408,11 @@ emacs_deliver_message ()
     shift 2
     # before we can send a message, we have to prepare the FCC maildir
     mkdir -p "$MAIL_DIR"/sent/{cur,new,tmp}
-    $TEST_DIRECTORY/smtp-dummy sent_message &
-    smtp_dummy_pid=$!
+    # eval'ing smtp-dummy --background will set smtp_dummy_pid
+    smtp_dummy_pid=
+    eval `$TEST_DIRECTORY/smtp-dummy --background sent_message`
+    test -n "$smtp_dummy_pid" || return 1
+
     test_emacs \
        "(let ((message-send-mail-function 'message-smtpmail-send-it)
               (smtpmail-smtp-server \"localhost\")
@@ -410,9 +427,11 @@ emacs_deliver_message ()
           (insert \"${body}\")
           $@
           (message-send-and-exit))"
-    # opportunistically quit smtp-dummy in case above fails.
-    { echo QUIT > /dev/tcp/localhost/25025; } 2>/dev/null
-    wait ${smtp_dummy_pid}
+
+    # In case message was sent properly, client waits for confirmation
+    # before exiting and resuming control here; therefore making sure
+    # that server exits by sending (KILL) signal to it is safe.
+    kill -9 $smtp_dummy_pid
     notmuch new >/dev/null
 }
 
@@ -488,21 +507,33 @@ test_expect_equal_file ()
        test "$#" = 2 ||
        error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
 
-       output="$1"
-       expected="$2"
+       file1="$1"
+       basename1=`basename "$file1"`
+       file2="$2"
+       basename2=`basename "$file2"`
        if ! test_skip "$test_subtest_name"
        then
-               if diff -q "$expected" "$output" >/dev/null ; then
+               if diff -q "$file1" "$file2" >/dev/null ; then
                        test_ok_ "$test_subtest_name"
                else
                        testname=$this_test.$test_count
-                       cp "$output" $testname.output
-                       cp "$expected" $testname.expected
-                       test_failure_ "$test_subtest_name" "$(diff -u $testname.expected $testname.output)"
+                       cp "$file1" "$testname.$basename1"
+                       cp "$file2" "$testname.$basename2"
+                       test_failure_ "$test_subtest_name" "$(diff -u "$testname.$basename1" "$testname.$basename2")"
                fi
     fi
 }
 
+# Like test_expect_equal, but arguments are JSON expressions to be
+# canonicalized before diff'ing.  If an argument cannot be parsed, it
+# is used unchanged so that there's something to diff against.
+test_expect_equal_json () {
+    output=$(echo "$1" | python -mjson.tool || echo "$1")
+    expected=$(echo "$2" | python -mjson.tool || echo "$2")
+    shift 2
+    test_expect_equal "$output" "$expected" "$@"
+}
+
 test_emacs_expect_t () {
        test "$#" = 2 && { prereq=$1; shift; } || prereq=
        test "$#" = 1 ||
@@ -534,7 +565,7 @@ test_emacs_expect_t () {
 
 NOTMUCH_NEW ()
 {
-    notmuch new | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
+    notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'
 }
 
 notmuch_search_sanitize ()
@@ -556,10 +587,9 @@ notmuch_show_sanitize_all ()
 
 notmuch_json_show_sanitize ()
 {
-    sed -e 's|, |,\n |g' | \
-       sed \
-       -e 's|"id": "[^"]*",|"id": "XXXXX",|' \
-       -e 's|"filename": "[^"]*",|"filename": "YYYYY",|'
+    sed \
+       -e 's|"id": "[^"]*",|"id": "XXXXX",|g' \
+       -e 's|"filename": "[^"]*",|"filename": "YYYYY",|g'
 }
 
 # End of notmuch helper functions
@@ -967,6 +997,14 @@ test_emacs () {
                done
        fi
 
+       # Clear test-output output file.  Most Emacs tests end with a
+       # call to (test-output).  If the test code fails with an
+       # exception before this call, the output file won't get
+       # updated.  Since we don't want to compare against an output
+       # file from another test, so start out with an empty file.
+       rm -f OUTPUT
+       touch OUTPUT
+
        emacsclient --socket-name="$EMACS_SERVER" --eval "(progn $@)"
 }
 
@@ -1052,6 +1090,7 @@ find_notmuch_path ()
 # Test the binaries we have just built.  The tests are kept in
 # test/ subdirectory and are run in 'trash directory' subdirectory.
 TEST_DIRECTORY=$(pwd)
+notmuch_path=`find_notmuch_path "$TEST_DIRECTORY"`
 if test -n "$valgrind"
 then
        make_symlink () {
@@ -1112,11 +1151,15 @@ then
        PATH=$GIT_VALGRIND/bin:$PATH
        GIT_EXEC_PATH=$GIT_VALGRIND/bin
        export GIT_VALGRIND
+       test -n "$notmuch_path" && MANPATH="$notmuch_path/man:$MANPATH"
 else # normal case
-       notmuch_path=`find_notmuch_path "$TEST_DIRECTORY"`
-       test -n "$notmuch_path" && PATH="$notmuch_path:$PATH"
+       if test -n "$notmuch_path"
+               then
+                       PATH="$notmuch_path:$PATH"
+                       MANPATH="$notmuch_path/man:$MANPATH"
+               fi
 fi
-export PATH
+export PATH MANPATH
 
 # Test repository
 test="tmp.$(basename "$0" .sh)"