diff options
| author | Craig Jennings <c@cjennings.net> | 2026-04-22 11:45:19 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-04-22 11:45:19 -0500 |
| commit | 848a2d52c9eb796dac94a6f263b0518abe580c69 (patch) | |
| tree | dfa2345fb1e1107555efaa721d51479d900c20f8 | |
| parent | 9896e7f5861beeb591ece48f38d1135ffdc6059b (diff) | |
| download | dotemacs-848a2d52c9eb796dac94a6f263b0518abe580c69.tar.gz dotemacs-848a2d52c9eb796dac94a6f263b0518abe580c69.zip | |
fix(host-environment): detect battery correctly on Linux desktops
`env-laptop-p` treated any `battery-format "%B"` value that wasn't literally "N/A" as "has a battery." On a Linux desktop using `battery-upower`, the result is "unknown". The AC adapter and USB-C power entries exist in /sys but there's no BAT*. That made desktops look like laptops. The per-machine font height switch in `font-config.el` broke as a result.
The fix uses /sys/class/power_supply/BAT* as the canonical Linux signal. That's what the kernel exposes, and what upower itself reads. Other platforms keep the `battery-format` path, but the fallback now checks for a live battery status char ("!", "+", "-") instead of only excluding "N/A".
Two pure helpers (`env--battery-status-char-indicates-battery-p`, `env--power-supply-has-battery-p`) keep the logic testable. The new test file covers Normal, Boundary, and Error cases for each helper.
| -rw-r--r-- | modules/host-environment.el | 35 | ||||
| -rw-r--r-- | tests/test-host-environment.el | 68 |
2 files changed, 98 insertions, 5 deletions
diff --git a/modules/host-environment.el b/modules/host-environment.el index af9248c2..832cc195 100644 --- a/modules/host-environment.el +++ b/modules/host-environment.el @@ -12,12 +12,37 @@ +(defun env--battery-status-char-indicates-battery-p (status) + "Return non-nil if STATUS indicates a real battery is present. +STATUS is the value returned by `battery-format' with the \"%B\" spec. +A real battery reports \"!\" (critical), \"+\" (charging), or \"-\" +(discharging). Desktops report \"N/A\", \"unknown\", or empty, which +all mean no battery." + (and (stringp status) + (member status '("!" "+" "-")) + t)) + +(defun env--power-supply-has-battery-p (power-supply-dir) + "Return non-nil if POWER-SUPPLY-DIR contains a BAT* entry. +Canonical Linux check. A laptop exposes directories like +/sys/class/power_supply/BAT0; a desktop exposes only AC adapters +and USB-C power entries." + (and (file-directory-p power-supply-dir) + (file-expand-wildcards (expand-file-name "BAT*" power-supply-dir)) + t)) + (defun env-laptop-p () - "Non-nil if a battery is present." - (when (and (require 'battery nil 'noerror) - battery-status-function) - (not (string= "N/A" - (battery-format "%B" (funcall battery-status-function)))))) + "Non-nil if the host has a battery. +On Linux, checks /sys/class/power_supply for BAT* entries, which is the +canonical signal. On other platforms, falls back to `battery-format' +\"%B\" and checks for a live battery status char." + (cond + ((eq system-type 'gnu/linux) + (env--power-supply-has-battery-p "/sys/class/power_supply")) + ((and (require 'battery nil 'noerror) + battery-status-function) + (env--battery-status-char-indicates-battery-p + (battery-format "%B" (funcall battery-status-function)))))) (defun env-desktop-p () "Return t if host is a laptop (has a battery), nil if not." diff --git a/tests/test-host-environment.el b/tests/test-host-environment.el new file mode 100644 index 00000000..9290a32f --- /dev/null +++ b/tests/test-host-environment.el @@ -0,0 +1,68 @@ +;;; test-host-environment.el --- Tests for host-environment.el -*- lexical-binding: t; -*- + +;;; Commentary: +;; Unit tests for `env-laptop-p' and its pure helpers. +;; +;; Regression driver: upower on a desktop with only an AC adapter reports +;; `(battery-format "%B" ...)' as "unknown", not "N/A". The previous +;; implementation treated any non-"N/A" value as "has a battery", so +;; desktops were misclassified as laptops. + +;;; Code: + +(require 'ert) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'host-environment) + +;;; env--battery-status-char-indicates-battery-p + +(ert-deftest test-host-environment-battery-char-live-codes () + "Normal: \"+\", \"-\", and \"!\" indicate a live battery." + (should (env--battery-status-char-indicates-battery-p "+")) + (should (env--battery-status-char-indicates-battery-p "-")) + (should (env--battery-status-char-indicates-battery-p "!"))) + +(ert-deftest test-host-environment-battery-char-na-returns-nil () + "Boundary: legacy \"N/A\" sentinel means no battery." + (should-not (env--battery-status-char-indicates-battery-p "N/A"))) + +(ert-deftest test-host-environment-battery-char-unknown-returns-nil () + "Boundary: upower \"unknown\" (desktop with AC adapter) means no battery." + (should-not (env--battery-status-char-indicates-battery-p "unknown"))) + +(ert-deftest test-host-environment-battery-char-empty-returns-nil () + "Boundary: empty string means no battery." + (should-not (env--battery-status-char-indicates-battery-p ""))) + +(ert-deftest test-host-environment-battery-char-nil-returns-nil () + "Error: nil input means no battery." + (should-not (env--battery-status-char-indicates-battery-p nil))) + +;;; env--power-supply-has-battery-p + +(ert-deftest test-host-environment-power-supply-with-bat-dir () + "Normal: a directory with BAT0 subdirectory reports a battery." + (let ((dir (make-temp-file "power-supply-test-" t))) + (unwind-protect + (progn + (make-directory (expand-file-name "BAT0" dir)) + (should (env--power-supply-has-battery-p dir))) + (delete-directory dir t)))) + +(ert-deftest test-host-environment-power-supply-no-bat-dir () + "Boundary: a directory with only AC adapter entries reports no battery." + (let ((dir (make-temp-file "power-supply-test-" t))) + (unwind-protect + (progn + (make-directory (expand-file-name "ACAD" dir)) + (make-directory (expand-file-name "ucsi-source-psy-USBC000:001" dir)) + (should-not (env--power-supply-has-battery-p dir))) + (delete-directory dir t)))) + +(ert-deftest test-host-environment-power-supply-missing-dir-returns-nil () + "Error: nonexistent power-supply directory is treated as no battery." + (should-not (env--power-supply-has-battery-p "/nonexistent/path/for-test-12345"))) + +(provide 'test-host-environment) +;;; test-host-environment.el ends here |
