aboutsummaryrefslogtreecommitdiff
path: root/tests/test-dev-fkeys--projectile-reset-cmds.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-03 18:25:58 -0500
committerCraig Jennings <c@cjennings.net>2026-05-03 18:25:58 -0500
commit475d6305e150c0a8ac61738eabe434c432acd991 (patch)
tree77093ef1afd7cb0ae259e1655b8641cbf7c9e40e /tests/test-dev-fkeys--projectile-reset-cmds.el
parentdbce94b38f3267e59b015880d34ff31104126e47 (diff)
downloaddotemacs-475d6305e150c0a8ac61738eabe434c432acd991.tar.gz
dotemacs-475d6305e150c0a8ac61738eabe434c432acd991.zip
feat(dev-fkeys): revert projectile cache on failed-and-modified compile
Without this, a one-off typo at projectile's compile/test/run prompt poisons the per-project cache: every subsequent invocation pre-fills the broken value. I hit it during the Phase 2a live-test, where projectile's "All tests" prompt was replaying `go test ../.` and there was no clean way to get the prior known-good back. Three pieces of machinery, all in `dev-fkeys.el`: `cj/--projectile-capture-cmd' captures the current cached cmd at the project root before each invocation, stashing a plist with :map / :root / :prior in `cj/--projectile-revert-state'. `cj/--projectile-revert-on-fail' is a `compilation-finish-functions' hook that reads that state. If the compile failed AND the cmd was modified from the captured prior value AND the prior was non-nil, it puts the prior back in projectile's cmd-map. Test-fails-because-of-real-bug (cmd unchanged through the run) leaves the cache alone. The hook self-removes on first invocation regardless of outcome and clears the state. `cj/--projectile-around-revert' is the around-advice that wires the two together. I added the advice to all three projectile cmd-runners — `projectile-compile-project', `projectile-test-project', `projectile-run-project' — so the auto-revert applies whether the user invoked via F4 / F6 or directly via `M-x'. Plus the manual escape-hatch: `cj/projectile-reset-cmds' clears compile/test/run cache for the current project. Bound to `C-; P' under the personal keymap. Use when projectile's auto-derived default was wrong from the start and you want to start fresh — the next F4 / F6 invocation re-derives projectile's project-type default. TDD: 18 new tests across 4 files, one per helper. The around-advice tests build the capture/install/orig-fn flow against stub cmd-maps and verify state captured, hook installed, orig-fn invoked. The revert hook tests cover failure-and-modified (revert), success (leave alone), failure-but-unchanged (leave alone), nil prior (leave alone), nil state (no-op), and self-removal. The reset-cmds tests cover the all-three-maps clear, no-cached-entry no-op, and no-project user-error.
Diffstat (limited to 'tests/test-dev-fkeys--projectile-reset-cmds.el')
-rw-r--r--tests/test-dev-fkeys--projectile-reset-cmds.el62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/test-dev-fkeys--projectile-reset-cmds.el b/tests/test-dev-fkeys--projectile-reset-cmds.el
new file mode 100644
index 00000000..6fb1cc17
--- /dev/null
+++ b/tests/test-dev-fkeys--projectile-reset-cmds.el
@@ -0,0 +1,62 @@
+;;; test-dev-fkeys--projectile-reset-cmds.el --- Tests for cj/projectile-reset-cmds -*- lexical-binding: t -*-
+
+;;; Commentary:
+;; Tests for the manual escape-hatch command that clears projectile's
+;; per-project compile / test / run cache for the current project. Use
+;; case: projectile's auto-derived default was wrong to begin with and
+;; you want to reset to projectile's default-derived cmd at the next
+;; prompt.
+
+;;; Code:
+
+(require 'ert)
+(require 'cl-lib)
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(require 'dev-fkeys)
+
+(defvar projectile-compile-cmd-map nil)
+(defvar projectile-test-cmd-map nil)
+(defvar projectile-run-cmd-map nil)
+
+;;; Normal Cases
+
+(ert-deftest test-dev-fkeys-projectile-reset-cmds-clears-all-three-maps ()
+ "Normal: clears compile, test, and run cache entries for the current root.
+Other projects' entries are left alone."
+ (let ((projectile-compile-cmd-map (make-hash-table :test 'equal))
+ (projectile-test-cmd-map (make-hash-table :test 'equal))
+ (projectile-run-cmd-map (make-hash-table :test 'equal)))
+ (puthash "/p/" "make" projectile-compile-cmd-map)
+ (puthash "/p/" "make test" projectile-test-cmd-map)
+ (puthash "/p/" "./run.sh" projectile-run-cmd-map)
+ (puthash "/other/" "untouched" projectile-compile-cmd-map)
+ (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () "/p/")))
+ (cj/projectile-reset-cmds))
+ (should-not (gethash "/p/" projectile-compile-cmd-map))
+ (should-not (gethash "/p/" projectile-test-cmd-map))
+ (should-not (gethash "/p/" projectile-run-cmd-map))
+ (should (string= (gethash "/other/" projectile-compile-cmd-map) "untouched"))))
+
+;;; Boundary Cases
+
+(ert-deftest test-dev-fkeys-projectile-reset-cmds-no-cached-entry-is-noop ()
+ "Boundary: project root has no cached entries → command runs cleanly,
+no error, maps stay empty."
+ (let ((projectile-compile-cmd-map (make-hash-table :test 'equal))
+ (projectile-test-cmd-map (make-hash-table :test 'equal))
+ (projectile-run-cmd-map (make-hash-table :test 'equal)))
+ (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () "/p/")))
+ (cj/projectile-reset-cmds))
+ (should (zerop (hash-table-count projectile-compile-cmd-map)))
+ (should (zerop (hash-table-count projectile-test-cmd-map)))
+ (should (zerop (hash-table-count projectile-run-cmd-map)))))
+
+;;; Error Cases
+
+(ert-deftest test-dev-fkeys-projectile-reset-cmds-no-project-signals-user-error ()
+ "Error: no project detected → user-error rather than silent no-op."
+ (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () nil)))
+ (should-error (cj/projectile-reset-cmds) :type 'user-error)))
+
+(provide 'test-dev-fkeys--projectile-reset-cmds)
+;;; test-dev-fkeys--projectile-reset-cmds.el ends here