diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-03 18:25:58 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-03 18:25:58 -0500 |
| commit | 475d6305e150c0a8ac61738eabe434c432acd991 (patch) | |
| tree | 77093ef1afd7cb0ae259e1655b8641cbf7c9e40e | |
| parent | dbce94b38f3267e59b015880d34ff31104126e47 (diff) | |
| download | dotemacs-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.
| -rw-r--r-- | modules/dev-fkeys.el | 93 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--projectile-around-revert.el | 76 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--projectile-capture-cmd.el | 70 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--projectile-reset-cmds.el | 62 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--projectile-revert-on-fail.el | 104 |
5 files changed, 405 insertions, 0 deletions
diff --git a/modules/dev-fkeys.el b/modules/dev-fkeys.el index a6f41adf..8c5b388c 100644 --- a/modules/dev-fkeys.el +++ b/modules/dev-fkeys.el @@ -167,6 +167,80 @@ a single Compile entry that calls plain `compile'." ('interpreted '(("Run" . run-only))) (_ '(("Compile" . compile-plain))))) +;; ---------- Projectile cache revert on failure ---------- +;; +;; Without this, a one-off typo at projectile's prompt poisons the per- +;; project cmd cache: every subsequent invocation pre-fills the broken +;; value. The capture/finish-hook pair installed by `:around' advice on +;; the three projectile cmd-runners reverts the cache to its prior value +;; if the compile fails AND the cmd was modified. A test that fails +;; because of a real code bug (cmd unchanged) leaves the cache alone. + +(defvar cj/--projectile-revert-state nil + "Plist describing the projectile cache state to potentially revert. +Set by `cj/--projectile-capture-cmd' before each invocation; read and +cleared by `cj/--projectile-revert-on-fail' after the compile finishes. +Keys: :map (cmd-map symbol), :root (project root), :prior (cached cmd +before invocation, may be nil).") + +(defun cj/--projectile-capture-cmd (map-symbol) + "Capture the cached cmd at the project root in MAP-SYMBOL. +MAP-SYMBOL is the symbol of a projectile cmd-map (e.g. +`projectile-compile-cmd-map'). Stashes a plist in +`cj/--projectile-revert-state' for the finish hook to read. No-op when +the project root cannot be resolved or MAP-SYMBOL is unbound (projectile +not loaded)." + (let ((root (cj/--f4-project-root))) + (when (and root (boundp map-symbol)) + (let ((prior (gethash root (symbol-value map-symbol)))) + (setq cj/--projectile-revert-state + (list :map map-symbol :root root :prior prior)))))) + +(defun cj/--projectile-revert-on-fail (_buf status) + "Compilation-finish hook: revert projectile cache on failed-and-modified. +Always self-removes from `compilation-finish-functions' and clears +`cj/--projectile-revert-state'. Reverts the cmd-map entry only when the +compile failed AND the cmd was modified from the captured prior value +AND that prior was non-nil. The unchanged-and-failed case (test fails +because of a real bug) leaves the cache alone." + (remove-hook 'compilation-finish-functions #'cj/--projectile-revert-on-fail) + (let ((state cj/--projectile-revert-state)) + (setq cj/--projectile-revert-state nil) + (when (and state (stringp status) + (not (string-prefix-p "finished" status))) + (let* ((map (plist-get state :map)) + (root (plist-get state :root)) + (prior (plist-get state :prior)) + (current (and (boundp map) (gethash root (symbol-value map))))) + (when (and root prior (boundp map) + (not (equal prior current))) + (puthash root prior (symbol-value map))))))) + +(defun cj/--projectile-around-revert (map-symbol orig-fn &rest args) + "Around-advice for projectile cmd-runners. +MAP-SYMBOL identifies which cmd-map to capture (compile / test / run). +Captures the prior cached cmd, installs the one-shot revert-on-failure +hook, then invokes ORIG-FN with ARGS." + (cj/--projectile-capture-cmd map-symbol) + (add-hook 'compilation-finish-functions #'cj/--projectile-revert-on-fail) + (apply orig-fn args)) + +(defun cj/projectile-reset-cmds () + "Clear projectile's cached compile/test/run cmds for the current project. +Use when projectile's auto-detected default was wrong to begin with and +you want to start fresh — the next F4 / F6 invocation will re-derive +projectile's project-type default." + (interactive) + (let ((root (cj/--f4-project-root))) + (unless root + (user-error "F-keys: no project detected")) + (dolist (map '(projectile-compile-cmd-map + projectile-test-cmd-map + projectile-run-cmd-map)) + (when (boundp map) + (remhash root (symbol-value map)))) + (message "Cleared projectile compile/test/run cache for %s" root))) + ;; ---------- F6 language detection ---------- (defconst cj/--f6-extension-language-map @@ -368,8 +442,27 @@ message." ('interpreted (message "M-F4: not a compiled language")) (_ (message "M-F4: no project detected"))))) +;; ---------- Projectile advice ---------- + +(advice-add 'projectile-compile-project :around + (apply-partially #'cj/--projectile-around-revert + 'projectile-compile-cmd-map)) +(advice-add 'projectile-test-project :around + (apply-partially #'cj/--projectile-around-revert + 'projectile-test-cmd-map)) +(advice-add 'projectile-run-project :around + (apply-partially #'cj/--projectile-around-revert + 'projectile-run-cmd-map)) + ;; ---------- Bindings ---------- +(eval-when-compile (defvar cj/custom-keymap)) ;; defined in keybindings.el + +;; Skip the binding if cj/custom-keymap isn't loaded yet (e.g. when this +;; module is required directly in batch tests). +(when (boundp 'cj/custom-keymap) + (keymap-set cj/custom-keymap "P" #'cj/projectile-reset-cmds)) + (keymap-global-set "<f4>" #'cj/f4-compile-and-run) (keymap-global-set "C-<f4>" #'cj/f4-compile-only) (keymap-global-set "M-<f4>" #'cj/f4-clean-rebuild) diff --git a/tests/test-dev-fkeys--projectile-around-revert.el b/tests/test-dev-fkeys--projectile-around-revert.el new file mode 100644 index 00000000..bdcb11a1 --- /dev/null +++ b/tests/test-dev-fkeys--projectile-around-revert.el @@ -0,0 +1,76 @@ +;;; test-dev-fkeys--projectile-around-revert.el --- Tests for cj/--projectile-around-revert -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the around-advice that wires the capture / finish-hook pair +;; to projectile cmd runners. The advice: +;; +;; 1. Captures the prior cached cmd via `cj/--projectile-capture-cmd'. +;; 2. Adds `cj/--projectile-revert-on-fail' to `compilation-finish-functions'. +;; 3. Calls ORIG-FN with ARGS so projectile's normal flow proceeds. + +;;; 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) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-projectile-around-revert-invokes-orig-fn () + "Normal: advice calls the wrapped function with its args." + (let ((calls nil) + (cj/--projectile-revert-state nil) + (compilation-finish-functions nil) + (projectile-compile-cmd-map (make-hash-table :test 'equal))) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () "/p/"))) + (cj/--projectile-around-revert + 'projectile-compile-cmd-map + (lambda (&rest args) (push args calls)) + 'arg1 'arg2)) + (should (equal calls '((arg1 arg2)))))) + +(ert-deftest test-dev-fkeys-projectile-around-revert-captures-prior () + "Normal: advice captures the prior cmd into the revert state." + (let ((cj/--projectile-revert-state nil) + (compilation-finish-functions nil) + (projectile-compile-cmd-map (make-hash-table :test 'equal))) + (puthash "/p/" "make build" projectile-compile-cmd-map) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () "/p/"))) + (cj/--projectile-around-revert + 'projectile-compile-cmd-map + (lambda (&rest _) nil))) + (should (equal (plist-get cj/--projectile-revert-state :prior) + "make build")))) + +(ert-deftest test-dev-fkeys-projectile-around-revert-installs-finish-hook () + "Normal: advice adds the revert-on-fail hook to compilation-finish-functions." + (let ((cj/--projectile-revert-state nil) + (compilation-finish-functions nil) + (projectile-compile-cmd-map (make-hash-table :test 'equal))) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () "/p/"))) + (cj/--projectile-around-revert + 'projectile-compile-cmd-map + (lambda (&rest _) nil))) + (should (member #'cj/--projectile-revert-on-fail + compilation-finish-functions)))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-projectile-around-revert-no-project-still-runs-orig-fn () + "Boundary: no project root → capture is a no-op, orig-fn still runs. +The state stays nil so the finish hook will be a no-op too." + (let ((calls 0) + (cj/--projectile-revert-state nil) + (compilation-finish-functions nil)) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () nil))) + (cj/--projectile-around-revert + 'projectile-compile-cmd-map + (lambda (&rest _) (cl-incf calls)))) + (should (= calls 1)) + (should (null cj/--projectile-revert-state)))) + +(provide 'test-dev-fkeys--projectile-around-revert) +;;; test-dev-fkeys--projectile-around-revert.el ends here diff --git a/tests/test-dev-fkeys--projectile-capture-cmd.el b/tests/test-dev-fkeys--projectile-capture-cmd.el new file mode 100644 index 00000000..92309198 --- /dev/null +++ b/tests/test-dev-fkeys--projectile-capture-cmd.el @@ -0,0 +1,70 @@ +;;; test-dev-fkeys--projectile-capture-cmd.el --- Tests for cj/--projectile-capture-cmd -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the prior-cmd capture helper used by the auto-revert advice. +;; Captures the current cached cmd at the project root into +;; `cj/--projectile-revert-state' so a later finish-hook can restore it +;; if the compile fails after the cmd was modified. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +;; Test stub: projectile cmd-maps as defvars so `boundp' is non-nil and +;; `let'-binding has a target. In real use, projectile defines these. +(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-capture-cmd-stores-prior-value () + "Normal: captures the cached cmd at the project root into the state plist." + (let* ((cj/--projectile-revert-state nil) + (projectile-compile-cmd-map (make-hash-table :test 'equal))) + (puthash "/p/" "make build" projectile-compile-cmd-map) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () "/p/"))) + (cj/--projectile-capture-cmd 'projectile-compile-cmd-map)) + (should (equal (plist-get cj/--projectile-revert-state :map) + 'projectile-compile-cmd-map)) + (should (equal (plist-get cj/--projectile-revert-state :root) "/p/")) + (should (equal (plist-get cj/--projectile-revert-state :prior) "make build")))) + +(ert-deftest test-dev-fkeys-projectile-capture-cmd-no-prior-stores-nil () + "Normal: when no cmd is cached, captures :prior nil — distinct from +\"didn't capture at all\" because :map and :root are still set." + (let* ((cj/--projectile-revert-state nil) + (projectile-test-cmd-map (make-hash-table :test 'equal))) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () "/p/"))) + (cj/--projectile-capture-cmd 'projectile-test-cmd-map)) + (should (eq (plist-get cj/--projectile-revert-state :map) + 'projectile-test-cmd-map)) + (should (null (plist-get cj/--projectile-revert-state :prior))))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-projectile-capture-cmd-nil-root-leaves-state-nil () + "Boundary: when no project root resolves, state stays nil so the +finish hook treats it as a no-op." + (let ((cj/--projectile-revert-state nil) + (projectile-compile-cmd-map (make-hash-table :test 'equal))) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () nil))) + (cj/--projectile-capture-cmd 'projectile-compile-cmd-map)) + (should (null cj/--projectile-revert-state)))) + +;;; Error Cases + +(ert-deftest test-dev-fkeys-projectile-capture-cmd-unbound-map-leaves-state-nil () + "Error: when the cmd-map symbol is unbound (projectile not loaded), +state stays nil and no error is raised." + (let ((cj/--projectile-revert-state nil)) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () "/p/"))) + ;; Use a clearly-unbound symbol to simulate projectile-not-loaded. + (cj/--projectile-capture-cmd 'cj-test--definitely-not-bound-xyzzy)) + (should (null cj/--projectile-revert-state)))) + +(provide 'test-dev-fkeys--projectile-capture-cmd) +;;; test-dev-fkeys--projectile-capture-cmd.el ends here 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 diff --git a/tests/test-dev-fkeys--projectile-revert-on-fail.el b/tests/test-dev-fkeys--projectile-revert-on-fail.el new file mode 100644 index 00000000..6fadd539 --- /dev/null +++ b/tests/test-dev-fkeys--projectile-revert-on-fail.el @@ -0,0 +1,104 @@ +;;; test-dev-fkeys--projectile-revert-on-fail.el --- Tests for cj/--projectile-revert-on-fail -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the compilation-finish hook that reverts projectile's +;; per-project cache when a build/test failed AND the cmd was modified. +;; Test-fails-because-of-real-bug (cmd unchanged) leaves the cache alone. +;; The hook also self-removes from `compilation-finish-functions' on +;; first invocation and clears `cj/--projectile-revert-state'. + +;;; 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) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-projectile-revert-on-fail-failure-and-modified-reverts () + "Normal: failure status + cmd modified from prior → revert to prior." + (let* ((projectile-compile-cmd-map (make-hash-table :test 'equal)) + (cj/--projectile-revert-state + (list :map 'projectile-compile-cmd-map + :root "/p/" + :prior "make build"))) + (puthash "/p/" "make buidl" projectile-compile-cmd-map) + (cj/--projectile-revert-on-fail nil "exited abnormally with code 2\n") + (should (string= (gethash "/p/" projectile-compile-cmd-map) "make build")))) + +(ert-deftest test-dev-fkeys-projectile-revert-on-fail-success-leaves-cache () + "Normal: success status → no revert, cache keeps the modified cmd." + (let* ((projectile-compile-cmd-map (make-hash-table :test 'equal)) + (cj/--projectile-revert-state + (list :map 'projectile-compile-cmd-map + :root "/p/" + :prior "make build"))) + (puthash "/p/" "make build-fast" projectile-compile-cmd-map) + (cj/--projectile-revert-on-fail nil "finished\n") + (should (string= (gethash "/p/" projectile-compile-cmd-map) "make build-fast")))) + +(ert-deftest test-dev-fkeys-projectile-revert-on-fail-clears-state () + "Normal: hook clears `cj/--projectile-revert-state' regardless of outcome." + (let* ((projectile-compile-cmd-map (make-hash-table :test 'equal)) + (cj/--projectile-revert-state + (list :map 'projectile-compile-cmd-map :root "/p/" :prior "x"))) + (cj/--projectile-revert-on-fail nil "finished\n") + (should (null cj/--projectile-revert-state)))) + +(ert-deftest test-dev-fkeys-projectile-revert-on-fail-removes-itself () + "Normal: hook removes itself from `compilation-finish-functions'." + (let* ((projectile-compile-cmd-map (make-hash-table :test 'equal)) + (cj/--projectile-revert-state + (list :map 'projectile-compile-cmd-map :root "/p/" :prior "x")) + (compilation-finish-functions + (list #'cj/--projectile-revert-on-fail))) + (cj/--projectile-revert-on-fail nil "finished\n") + (should-not (member #'cj/--projectile-revert-on-fail + compilation-finish-functions)))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-projectile-revert-on-fail-failure-but-unchanged-no-revert () + "Boundary: failure status + cmd unchanged from prior → no revert. +This is the test-fails-because-of-real-bug case. Don't flap the cache. + +Components integrated: +- `cj/--projectile-revert-on-fail' (unit under test) +- `cj/--projectile-revert-state' (real, scoped via let) +- `projectile-compile-cmd-map' (test stub)" + (let* ((projectile-compile-cmd-map (make-hash-table :test 'equal)) + (cj/--projectile-revert-state + (list :map 'projectile-compile-cmd-map + :root "/p/" + :prior "pytest"))) + (puthash "/p/" "pytest" projectile-compile-cmd-map) + (cj/--projectile-revert-on-fail nil "exited abnormally with code 1\n") + ;; Cache value still equals the prior value (unchanged through the run). + (should (string= (gethash "/p/" projectile-compile-cmd-map) "pytest")))) + +(ert-deftest test-dev-fkeys-projectile-revert-on-fail-failure-with-nil-prior-no-revert () + "Boundary: failure with no prior cmd cached → don't store nil. +A nil prior means projectile cached for the first time on this run; even +if it failed, there's nothing to revert to." + (let* ((projectile-compile-cmd-map (make-hash-table :test 'equal)) + (cj/--projectile-revert-state + (list :map 'projectile-compile-cmd-map + :root "/p/" + :prior nil))) + (puthash "/p/" "broken-cmd" projectile-compile-cmd-map) + (cj/--projectile-revert-on-fail nil "exited abnormally\n") + (should (string= (gethash "/p/" projectile-compile-cmd-map) "broken-cmd")))) + +;;; Error Cases + +(ert-deftest test-dev-fkeys-projectile-revert-on-fail-nil-state-is-noop () + "Error: nil state (capture didn't run) → hook is a no-op, no error." + (let ((cj/--projectile-revert-state nil)) + (cj/--projectile-revert-on-fail nil "exited abnormally\n") + (should (null cj/--projectile-revert-state)))) + +(provide 'test-dev-fkeys--projectile-revert-on-fail) +;;; test-dev-fkeys--projectile-revert-on-fail.el ends here |
