From 45cadbd4f304a3dcb7b3fcd47032a39fac715f6f Mon Sep 17 00:00:00 2001
From: Robert Helgesson <robert@rycee.net>
Date: Tue, 29 Jan 2019 18:17:24 +0100
Subject: [PATCH] git: quote sendemail section header

This will allow, e.g., the character `@` in the email identity.

Also adds a test case.

Fixes #557
---
 modules/programs/git.nix                      |  2 +-
 tests/default.nix                             |  1 +
 .../modules/accounts/email-test-accounts.nix  | 27 +++++++++++++++
 .../programs/git-with-email-expected.conf     | 15 +++++++++
 tests/modules/programs/git-with-email.nix     | 33 +++++++++++++++++++
 5 files changed, 77 insertions(+), 1 deletion(-)
 create mode 100644 tests/modules/accounts/email-test-accounts.nix
 create mode 100644 tests/modules/programs/git-with-email-expected.conf
 create mode 100644 tests/modules/programs/git-with-email.nix

diff --git a/modules/programs/git.nix b/modules/programs/git.nix
index 9b7858bfa..4c962e99c 100644
--- a/modules/programs/git.nix
+++ b/modules/programs/git.nix
@@ -151,7 +151,7 @@ in
             hasSmtp = name: account: account.smtp != null;
 
             genIdentity = name: account: with account;
-              nameValuePair "sendemail.${name}" ({
+              nameValuePair "sendemail \"${name}\"" ({
                 smtpEncryption = if smtp.tls.enable then "tls" else "";
                 smtpServer = smtp.host;
                 smtpUser = userName;
diff --git a/tests/default.nix b/tests/default.nix
index 1da2abf17..279fc6318 100644
--- a/tests/default.nix
+++ b/tests/default.nix
@@ -20,6 +20,7 @@ import nmt {
     files-hidden-source = ./modules/files/hidden-source.nix;
     files-source-with-spaces = ./modules/files/source-with-spaces.nix;
     files-text = ./modules/files/text.nix;
+    git-with-email = ./modules/programs/git-with-email.nix;
     git-with-most-options = ./modules/programs/git.nix;
     git-with-str-extra-config = ./modules/programs/git-with-str-extra-config.nix;
     texlive-minimal = ./modules/programs/texlive-minimal.nix;
diff --git a/tests/modules/accounts/email-test-accounts.nix b/tests/modules/accounts/email-test-accounts.nix
new file mode 100644
index 000000000..9c9c90cf8
--- /dev/null
+++ b/tests/modules/accounts/email-test-accounts.nix
@@ -0,0 +1,27 @@
+{ ... }:
+
+{
+  accounts.email = {
+    maildirBasePath = "Mail";
+
+    accounts = {
+      "hm@example.com" = {
+        address = "hm@example.com";
+        userName = "home.manager";
+        realName = "H. M. Test";
+        passwordCommand = "password-command";
+        imap.host = "imap.example.com";
+        smtp.host = "smtp.example.com";
+      };
+
+      hm-account = {
+        address = "hm@example.org";
+        userName = "home.manager.jr";
+        realName = "H. M. Test Jr.";
+        passwordCommand = "password-command 2";
+        imap.host = "imap.example.org";
+        smtp.host = "smtp.example.org";
+      };
+    };
+  };
+}
diff --git a/tests/modules/programs/git-with-email-expected.conf b/tests/modules/programs/git-with-email-expected.conf
new file mode 100644
index 000000000..01c1eec58
--- /dev/null
+++ b/tests/modules/programs/git-with-email-expected.conf
@@ -0,0 +1,15 @@
+[sendemail "hm-account"]
+from=hm@example.org
+smtpEncryption=tls
+smtpServer=smtp.example.org
+smtpUser=home.manager.jr
+
+[sendemail "hm@example.com"]
+from=hm@example.com
+smtpEncryption=tls
+smtpServer=smtp.example.com
+smtpUser=home.manager
+
+[user]
+email=hm@example.com
+name=H. M. Test
diff --git a/tests/modules/programs/git-with-email.nix b/tests/modules/programs/git-with-email.nix
new file mode 100644
index 000000000..f8a762dcc
--- /dev/null
+++ b/tests/modules/programs/git-with-email.nix
@@ -0,0 +1,33 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+{
+  imports = [ ../accounts/email-test-accounts.nix ];
+
+  config = {
+    programs.git = {
+      enable = true;
+      userEmail = "hm@example.com";
+      userName = "H. M. Test";
+    };
+
+    nmt.script = ''
+      function assertGitConfig() {
+        local value
+        value=$(${pkgs.git}/bin/git config \
+          --file $TESTED/home-files/.config/git/config \
+          --get $1)
+        if [[ $value != $2 ]]; then
+          fail "Expected option '$1' to have value '$2' but it was '$value'"
+        fi
+      }
+
+      assertFileExists home-files/.config/git/config
+      assertFileContent home-files/.config/git/config ${./git-with-email-expected.conf}
+
+      assertGitConfig "sendemail.hm@example.com.from" "hm@example.com"
+      assertGitConfig "sendemail.hm-account.from" "hm@example.org"
+    '';
+  };
+}