Subject: [bug#63802] [PATCH 1/3] client: Separate serialize-email-address into a procedure.

From: Arun Isaac <[email-masked]>
Date: Mon, 17 Jul 2023 23:19:19 +0100

* mumi/client.scm (serialize-email-address): New procedure.
(reply-email-headers): Use serialize-email-address.
* tests/client.scm (serialize-email-address): New variable.
("serialize email address", "serialize email address with comma in
name"): New tests.
---
 mumi/client.scm  | 14 ++++++++++----
 tests/client.scm | 11 +++++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/mumi/client.scm b/mumi/client.scm
index 64ccbeb..c30429d 100644
--- a/mumi/client.scm
+++ b/mumi/client.scm
@@ -221,6 +221,14 @@ arguments."
            (unless (zero? (close-pipe port))
              (error "Command invocation failed" command))))))))
 
+(define (serialize-email-address name address)
+  "Combine NAME and ADDRESS into a complete email address of the form
+\"NAME <ADDRESS>\". Double quote NAME if necessary."
+  (string-append (if (string-contains name ",")
+                     (string-append "\"" name "\"")
+                     name)
+                 " <" address ">"))
+
 
 (define* (git-send-email to patches #:optional (options '()))
   "Send PATCHES using git send-email to the TO address with
@@ -269,10 +277,8 @@ ISSUE-NUMBER."
       ,@(match (delete-duplicates
                 (map (lambda (message)
                        (let ((from (assoc-ref message "from")))
-                         (string-append (if (string-contains (assoc-ref from "name") ",")
-                                            (string-append "\"" (assoc-ref from "name") "\"")
-                                            (assoc-ref from "name"))
-                                        " <" (assoc-ref from "address") ">")))
+                         (serialize-email-address (assoc-ref from "name")
+                                                  (assoc-ref from "address"))))
                      (vector->list (assoc-ref issue "messages"))))
           (() (list))
           (cc (list (cons 'cc (string-join cc ", "))))))))
diff --git a/tests/client.scm b/tests/client.scm
index 5352b08..2b2c1be 100644
--- a/tests/client.scm
+++ b/tests/client.scm
@@ -65,8 +65,19 @@ called with."
         (lambda _
           (error "Do not poll server for issue number"))))
 
+(define serialize-email-address
+  (@@ (mumi client) serialize-email-address))
+
 (test-begin "client")
 
+(test-equal "serialize email address"
+  "Foo <[email-masked]>"
+  (serialize-email-address "Foo" "[email-masked]"))
+
+(test-equal "serialize email address with comma in name"
+  "\"Bar, Foo\" <[email-masked]>"
+  (serialize-email-address "Bar, Foo" "[email-masked]"))
+
 (test-equal "send patches to new issue"
   '(("git" "send-email" "--to=[email-masked]" "foo.patch")
     ("git" "send-email" "--to=[email-masked]" "bar.patch" "foobar.patch"))
-- 
2.39.2