summaryrefslogtreecommitdiff
path: root/tests/test-external-open-command.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-10 14:42:04 -0500
committerCraig Jennings <c@cjennings.net>2026-05-10 14:42:04 -0500
commit16396d25c2795bd7f8822a695de111d07f588b26 (patch)
treec452c193c1eb813cedcd3b4faca030d8775da4a3 /tests/test-external-open-command.el
parentc44a52a7905b605a6537e3ff9bb4fe3afede0485 (diff)
downloaddotemacs-16396d25c2795bd7f8822a695de111d07f588b26.tar.gz
dotemacs-16396d25c2795bd7f8822a695de111d07f588b26.zip
refactor(external-open): consolidate OS-open dispatch in external-open.el
Phase 4 of utility-consolidation. Three previously-overlapping helpers (system-utils' `cj/identify-external-open-command' and `cj/--open-with-is-launcher-p', plus the dirvish-only `cj/--file-manager-program-for' shipped earlier today) all answered "which OS-open program should I run?". Pull the answer into one place: external-open.el. Move and rename: - `cj/--open-with-is-launcher-p' (system-utils) -> `cj/external-open-launcher-p' (external-open). Public name now matches its module. - `cj/identify-external-open-command' (system-utils) -> `cj/external-open-command' (external-open). Returns nil for unsupported hosts instead of signaling -- callers that need a command must handle nil explicitly. The wrapper `cj/xdg-open' (also moved into external-open) converts nil to a `user-error' with a clear message, preserving the user-facing failure shape. - Delete dirvish's `cj/--file-manager-program-for' helper. `cj/dirvish-open-file-manager-here' now calls `cj/external-open-command' directly. The shell-command fallback for nil-program preserves the previous escape hatch. Break the system-utils <-> external-open recursive require by moving `cj/xdg-open' (the only system-utils function that external-open used) into external-open along with the dispatch. Tests reorganized to match the move. Two new test files (`test-external-open-command.el', `test-external-open-launcher-p.el') replace the two system-utils-named test files. The dirvish file-manager-program test goes away with the helper. 11 tests covering Normal/Boundary/Error for the dispatch (plus the new "unsupported host returns nil" contract). Add `(require \='external-open)' to system-utils.el and `(require \='system-lib)' to external-open.el (for `cj/file-from-context' which xdg-open uses).
Diffstat (limited to 'tests/test-external-open-command.el')
-rw-r--r--tests/test-external-open-command.el65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/test-external-open-command.el b/tests/test-external-open-command.el
new file mode 100644
index 00000000..b408b305
--- /dev/null
+++ b/tests/test-external-open-command.el
@@ -0,0 +1,65 @@
+;;; test-external-open-command.el --- Tests for cj/external-open-command -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; Unit tests for `cj/external-open-command' in external-open.el. The
+;; function dispatches on host-environment predicates to return the
+;; appropriate "open" command: xdg-open on Linux, open on macOS,
+;; start on Windows. Returns nil for unsupported hosts (callers that
+;; require a command should error on nil with a contextual message).
+
+;;; Code:
+
+(require 'ert)
+(require 'cl-lib)
+(require 'package)
+
+(setq package-user-dir (expand-file-name "elpa" user-emacs-directory))
+(package-initialize)
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(require 'external-open)
+
+(defmacro test-eoc--with-host (linux macos windows &rest body)
+ "Run BODY with env-*-p predicates stubbed to LINUX, MACOS, WINDOWS."
+ (declare (indent 3))
+ `(cl-letf (((symbol-function 'env-linux-p) (lambda () ,linux))
+ ((symbol-function 'env-macos-p) (lambda () ,macos))
+ ((symbol-function 'env-windows-p) (lambda () ,windows)))
+ ,@body))
+
+;;; Normal cases
+
+(ert-deftest test-eoc-linux-returns-xdg-open ()
+ "Normal: Linux host returns \"xdg-open\"."
+ (test-eoc--with-host t nil nil
+ (should (string= "xdg-open" (cj/external-open-command)))))
+
+(ert-deftest test-eoc-macos-returns-open ()
+ "Normal: macOS host returns \"open\"."
+ (test-eoc--with-host nil t nil
+ (should (string= "open" (cj/external-open-command)))))
+
+(ert-deftest test-eoc-windows-returns-start ()
+ "Normal: Windows host returns \"start\"."
+ (test-eoc--with-host nil nil t
+ (should (string= "start" (cj/external-open-command)))))
+
+;;; Boundary cases
+
+(ert-deftest test-eoc-dispatch-order-linux-wins ()
+ "Boundary: Linux check runs first; wins when predicates disagree.
+Documents the dispatch order. A real host only returns t from one
+of these anyway -- but if something goes wrong, Linux takes priority."
+ (test-eoc--with-host t t t
+ (should (string= "xdg-open" (cj/external-open-command)))))
+
+(ert-deftest test-eoc-unsupported-host-returns-nil ()
+ "Boundary: when no platform predicate returns non-nil, returns nil.
+Callers requiring a command must handle the nil case explicitly --
+this is a behavior change from the prior `cj/identify-external-open-command'
+which signaled an error. The wrapper `cj/xdg-open' converts nil to a
+user-error with a clear message."
+ (test-eoc--with-host nil nil nil
+ (should-not (cj/external-open-command))))
+
+(provide 'test-external-open-command)
+;;; test-external-open-command.el ends here