diff options
| author | Craig Jennings <c@cjennings.net> | 2026-04-23 01:47:52 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-04-23 01:47:52 -0500 |
| commit | fb2593ad55d0523dc211019f7ec856d5898d7c99 (patch) | |
| tree | 94c599d23f9e9bb3bca9d3cf0dc3c0043bc5dd39 /tests/test-system-utils--open-with-is-launcher-p.el | |
| parent | 73a95c7d1c5ba591a3444f92012be4a281e8d08b (diff) | |
| download | dotemacs-fb2593ad55d0523dc211019f7ec856d5898d7c99.tar.gz dotemacs-fb2593ad55d0523dc211019f7ec856d5898d7c99.zip | |
refactor(system-utils): extract testable open-file helpers
Extracts two pure helpers from cj/open-file-with-command and cj/xdg-open so the file-resolution and launcher-detection logic becomes testable without mocking process launchers.
New helpers:
- cj/--file-from-context returns a file path from the current context, resolving in priority order (explicit arg, buffer-file-name, dired file at point). Returns nil when none apply.
- cj/--open-with-is-launcher-p is a predicate for whether a command is a desktop launcher (xdg-open, open, start) that needs call-process detachment.
Both commands now delegate. cj/open-file-with-command uses cj/--file-from-context with read-file-name as the final fallback, plus cj/--open-with-is-launcher-p for the launcher dispatch. cj/xdg-open uses cj/--file-from-context with user-error as the "no file" fallback.
Behavior preserved. The existing system-utils test suites still pass, and the shape of each command's final effect is identical.
New tests, 14 cases across two per-function files:
- tests/test-system-utils--file-from-context.el covers: explicit wins over buffer-file, explicit wins over dired, buffer-file fallback, dired fallback, all-nil returns nil, explicit-nil uses chain, dired-mode-but-no-file-at-point.
- tests/test-system-utils--open-with-is-launcher-p.el covers: each of the three launcher names returns t, non-launcher returns nil, empty string returns nil, case-sensitive check, nil input returns nil.
Coverage: system-utils.el went from 10/52 (19.2%) to 15/52 (28.8%). The remaining uncovered lines are mostly in the process-launching paths of cj/open-file-with-command and cj/xdg-open. Those are testability-blocked. Mocking call-process, start-process-shell-command, and generate-new-buffer would give a lot of mock surface for low value. cj/server-shutdown is not meaningfully testable because it kills Emacs.
Diffstat (limited to 'tests/test-system-utils--open-with-is-launcher-p.el')
| -rw-r--r-- | tests/test-system-utils--open-with-is-launcher-p.el | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/test-system-utils--open-with-is-launcher-p.el b/tests/test-system-utils--open-with-is-launcher-p.el new file mode 100644 index 000000000..64e9a4b6f --- /dev/null +++ b/tests/test-system-utils--open-with-is-launcher-p.el @@ -0,0 +1,52 @@ +;;; test-system-utils--open-with-is-launcher-p.el --- Tests for cj/--open-with-is-launcher-p -*- lexical-binding: t; -*- + +;;; Commentary: +;; Unit tests for `cj/--open-with-is-launcher-p' in system-utils.el. +;; The predicate returns t for desktop launcher commands (xdg-open, +;; open, start) that need `call-process' with a zero buffer argument +;; to fully detach from Emacs. Anything else returns nil. + +;;; Code: + +(require 'ert) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'system-utils) + +;;; Normal cases + +(ert-deftest test-owilp-xdg-open-is-launcher () + "Normal: \"xdg-open\" (Linux launcher) returns t." + (should (eq t (cj/--open-with-is-launcher-p "xdg-open")))) + +(ert-deftest test-owilp-open-is-launcher () + "Normal: \"open\" (macOS launcher) returns t." + (should (eq t (cj/--open-with-is-launcher-p "open")))) + +(ert-deftest test-owilp-start-is-launcher () + "Normal: \"start\" (Windows launcher) returns t." + (should (eq t (cj/--open-with-is-launcher-p "start")))) + +;;; Boundary cases + +(ert-deftest test-owilp-non-launcher-command-returns-nil () + "Boundary: a non-launcher command (e.g. gimp) returns nil." + (should-not (cj/--open-with-is-launcher-p "gimp"))) + +(ert-deftest test-owilp-empty-string-returns-nil () + "Boundary: empty string is not a launcher." + (should-not (cj/--open-with-is-launcher-p ""))) + +(ert-deftest test-owilp-case-sensitive () + "Boundary: launcher check is case-sensitive (\"Open\" is not \"open\")." + (should-not (cj/--open-with-is-launcher-p "Open")) + (should-not (cj/--open-with-is-launcher-p "XDG-OPEN"))) + +;;; Error cases + +(ert-deftest test-owilp-nil-argument-returns-nil () + "Error: nil input is handled gracefully (not in the launcher list)." + (should-not (cj/--open-with-is-launcher-p nil))) + +(provide 'test-system-utils--open-with-is-launcher-p) +;;; test-system-utils--open-with-is-launcher-p.el ends here |
