aboutsummaryrefslogtreecommitdiff
path: root/tests/test-mail-config-transport.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-03 19:57:30 -0500
committerCraig Jennings <c@cjennings.net>2026-05-03 19:57:30 -0500
commit3e9499f62c4fb621ec234a0cf5cee51eb2cf32c0 (patch)
tree1a7384b5204351eefa8135242d24da2a8da8d38d /tests/test-mail-config-transport.el
parent59094e944b4a9e8c5e1a20724c2681ffe9b6155c (diff)
downloaddotemacs-3e9499f62c4fb621ec234a0cf5cee51eb2cf32c0.tar.gz
dotemacs-3e9499f62c4fb621ec234a0cf5cee51eb2cf32c0.zip
fix: validate mail transport executables and default debug off
`mail-config.el` had three related issues. SMTP transport debug was hard-coded to t, which is sensitive since mail bodies and headers land in debug buffers. The use-package `:config` was also setting `sendmail-program` and `mu4e-get-mail-command` directly from `executable-find` results. So a host without msmtp or mbsync silently got `nil` or `(concat nil " -a")` instead of a clear failure mode. I added `cj/smtpmail-debug-enabled` (default nil) plus `cj/set-smtpmail-debug` and `cj/toggle-smtpmail-debug` for temporary troubleshooting, mirroring the pattern from `auth-config.el`. I extracted `cj/mail--executable-or-warn` so a missing program emits a one-time `display-warning` and returns nil. `cj/mail-configure-smtpmail` and `cj/mail--mbsync-command` both use it. Missing msmtp now leaves `sendmail-program` nil with a warning. Missing mbsync produces a nil sync command instead of the broken `(concat nil " -a")` string. I also wrapped the mbsync executable path in `shell-quote-argument` so unusual install paths don't fall apart on the `" -a"` concat. I added `tests/test-mail-config-transport.el` with seven tests across Normal / Boundary / Error: debug-default-off, toggle wiring, msmtp present and missing, mbsync present, mbsync path with spaces, and mbsync missing. The `test-mail-config--with-executables` macro stubs `executable-find` from an alist so each test names its own environment.
Diffstat (limited to 'tests/test-mail-config-transport.el')
-rw-r--r--tests/test-mail-config-transport.el88
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/test-mail-config-transport.el b/tests/test-mail-config-transport.el
new file mode 100644
index 00000000..2244b6dd
--- /dev/null
+++ b/tests/test-mail-config-transport.el
@@ -0,0 +1,88 @@
+;;; test-mail-config-transport.el --- Tests for mail transport setup -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; Verifies mail transport debug logging is opt-in and external mail
+;; executables are validated before command variables are assigned.
+
+;;; Code:
+
+(require 'ert)
+(require 'cl-lib)
+
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(require 'mail-config)
+
+(defmacro test-mail-config--with-executables (executables &rest body)
+ "Run BODY with `executable-find' returning paths from EXECUTABLES.
+EXECUTABLES is an alist of program name strings to executable paths."
+ (declare (indent 1))
+ `(let (test-mail-config--warnings)
+ (cl-letf (((symbol-function 'executable-find)
+ (lambda (program)
+ (cdr (assoc program ,executables))))
+ ((symbol-function 'display-warning)
+ (lambda (type message &rest _args)
+ (push (cons type message) test-mail-config--warnings))))
+ ,@body)))
+
+(ert-deftest test-mail-config-transport-smtpmail-debug-defaults-off ()
+ "SMTP transport debug logging should default to disabled."
+ (test-mail-config--with-executables '(("msmtp" . "/usr/bin/msmtp"))
+ (let ((cj/smtpmail-debug-enabled nil)
+ (smtpmail-debug-info :before-load))
+ (cj/mail-configure-smtpmail)
+ (should-not smtpmail-debug-info))))
+
+(ert-deftest test-mail-config-transport-smtpmail-debug-toggle ()
+ "Troubleshooting command should update both SMTP debug variables."
+ (let ((cj/smtpmail-debug-enabled nil)
+ (smtpmail-debug-info nil))
+ (cl-letf (((symbol-function 'message) (lambda (&rest _args) nil)))
+ (cj/set-smtpmail-debug t)
+ (should cj/smtpmail-debug-enabled)
+ (should smtpmail-debug-info)
+ (cj/set-smtpmail-debug nil)
+ (should-not cj/smtpmail-debug-enabled)
+ (should-not smtpmail-debug-info))))
+
+(ert-deftest test-mail-config-transport-msmtp-present-configures-sendmail ()
+ "When msmtp exists, sendmail transport variables should be configured."
+ (test-mail-config--with-executables '(("msmtp" . "/usr/bin/msmtp"))
+ (let (sendmail-program send-mail-function message-send-mail-function
+ message-sendmail-envelope-from)
+ (cj/mail-configure-smtpmail)
+ (should (equal sendmail-program "/usr/bin/msmtp"))
+ (should (eq send-mail-function 'message-send-mail-with-sendmail))
+ (should (eq message-send-mail-function 'message-send-mail-with-sendmail))
+ (should (eq message-sendmail-envelope-from 'header))
+ (should-not test-mail-config--warnings))))
+
+(ert-deftest test-mail-config-transport-msmtp-missing-warns-and-disables-program ()
+ "When msmtp is missing, sendmail-program should be nil and a warning emitted."
+ (test-mail-config--with-executables nil
+ (let ((sendmail-program "/old/msmtp"))
+ (cj/mail-configure-smtpmail)
+ (should-not sendmail-program)
+ (should (equal test-mail-config--warnings
+ '((mail-config . "msmtp not found; SMTP mail sending unavailable")))))))
+
+(ert-deftest test-mail-config-transport-mbsync-present-builds-command ()
+ "When mbsync exists, build the mu4e sync command."
+ (test-mail-config--with-executables '(("mbsync" . "/usr/bin/mbsync"))
+ (should (equal (cj/mail--mbsync-command) "/usr/bin/mbsync -a"))
+ (should-not test-mail-config--warnings)))
+
+(ert-deftest test-mail-config-transport-mbsync-path-with-spaces-is-quoted ()
+ "The mu4e sync command should quote unusual executable paths."
+ (test-mail-config--with-executables '(("mbsync" . "/opt/mail tools/mbsync"))
+ (should (equal (cj/mail--mbsync-command) "/opt/mail\\ tools/mbsync -a"))))
+
+(ert-deftest test-mail-config-transport-mbsync-missing-returns-nil-and-warns ()
+ "When mbsync is missing, no unusable sync command should be assigned."
+ (test-mail-config--with-executables nil
+ (should-not (cj/mail--mbsync-command))
+ (should (equal test-mail-config--warnings
+ '((mail-config . "mbsync not found; mu4e mail synchronization unavailable"))))))
+
+(provide 'test-mail-config-transport)
+;;; test-mail-config-transport.el ends here