]> git.notmuchmail.org Git - notmuch/commitdiff
json: Fix search result with no matches to be a valid json object.
authorCarl Worth <cworth@cworth.org>
Sun, 30 Jan 2011 19:26:04 +0000 (05:26 +1000)
committerCarl Worth <cworth@cworth.org>
Wed, 9 Mar 2011 23:10:03 +0000 (15:10 -0800)
In the original json code, search matching nothing would return a
valid, empty json array (that is, "[]"). I broke this in commit
6dcb7592e32ed5140ea0c0357ce78d6a37af6066 when adding support for
--output=threads|messages|tags. This time, while fixing the bug also
add a test to the test suite to help avoid future regressions.

notmuch-search.c
test/json

index a91b23110d240b755064bec12f0b2d28cd8affe0..8b901210ddaab31a4cacd4c4f1a883b80ecb4c8a 100644 (file)
@@ -72,7 +72,7 @@ static const search_format_t format_text = {
                "%s", " ",
            ")", "\n",
        "",
-    "\n",
+    "",
 };
 
 static void
@@ -182,15 +182,15 @@ do_search_threads (const search_format_t *format,
     if (threads == NULL)
        return 1;
 
+    fputs (format->results_start, stdout);
+
     for (;
         notmuch_threads_valid (threads);
         notmuch_threads_move_to_next (threads))
     {
        int first_tag = 1;
 
-       if (first_thread)
-           fputs (format->results_start, stdout);
-       else
+       if (! first_thread)
            fputs (format->item_sep, stdout);
 
        thread = notmuch_threads_get (threads);
@@ -236,8 +236,7 @@ do_search_threads (const search_format_t *format,
        notmuch_thread_destroy (thread);
     }
 
-    if (! first_thread)
-       fputs (format->results_end, stdout);
+    fputs (format->results_end, stdout);
 
     return 0;
 }
@@ -255,15 +254,15 @@ do_search_messages (const search_format_t *format,
     if (messages == NULL)
        return 1;
 
+    fputs (format->results_start, stdout);
+
     for (;
         notmuch_messages_valid (messages);
         notmuch_messages_move_to_next (messages))
     {
        message = notmuch_messages_get (messages);
 
-       if (first_message)
-           fputs (format->results_start, stdout);
-       else
+       if (! first_message)
            fputs (format->item_sep, stdout);
 
        if (output == OUTPUT_FILES) {
@@ -281,8 +280,7 @@ do_search_messages (const search_format_t *format,
 
     notmuch_messages_destroy (messages);
 
-    if (! first_message)
-       fputs (format->results_end, stdout);
+    fputs (format->results_end, stdout);
 
     return 0;
 }
@@ -310,15 +308,15 @@ do_search_tags (notmuch_database_t *notmuch,
     if (tags == NULL)
        return 1;
 
+    fputs (format->results_start, stdout);
+
     for (;
         notmuch_tags_valid (tags);
         notmuch_tags_move_to_next (tags))
     {
        tag = notmuch_tags_get (tags);
 
-       if (first_tag)
-           fputs (format->results_start, stdout);
-       else
+       if (! first_tag)
            fputs (format->item_sep, stdout);
 
        format->item_id (tags, "", tag);
@@ -331,8 +329,7 @@ do_search_tags (notmuch_database_t *notmuch,
     if (messages)
        notmuch_messages_destroy (messages);
 
-    if (! first_tag)
-       fputs (format->results_end, stdout);
+    fputs (format->results_end, stdout);
 
     return 0;
 }
index 7fe2a27a8d6479bfd32c0d03b1c68b23ff7bf26f..307cd1e6c927e3c655b273076c55e5ec436d2d06 100755 (executable)
--- a/test/json
+++ b/test/json
@@ -39,4 +39,8 @@ test_expect_equal "$output" "[{\"thread\": \"XXX\",
 \"subject\": \"json-search-utf8-body-sübjéct\",
 \"tags\": [\"inbox\", \"unread\"]}]"
 
+test_begin_subtest "Search returning no messages"
+output=$(notmuch search --format=json "this string had better not match any messages" | notmuch_search_sanitize)
+test_expect_equal "$output" "[]"
+
 test_done