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 /tests/test-dev-fkeys--projectile-revert-on-fail.el | |
| 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.
Diffstat (limited to 'tests/test-dev-fkeys--projectile-revert-on-fail.el')
| -rw-r--r-- | tests/test-dev-fkeys--projectile-revert-on-fail.el | 104 |
1 files changed, 104 insertions, 0 deletions
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 |
