From 2c94acd52cc92dc4ebefd999dbca771367cc3090 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 3 May 2026 16:13:21 -0500 Subject: feat(dev-fkeys): add project-aware F4 compile/run dispatcher MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I added a new module `modules/dev-fkeys.el` that owns the dev F-key block. F4 prompts via `completing-read` with a candidate set filtered by project type (compiled / interpreted / unknown). C-F4 is the compile-only fast path. M-F4 is clean + rebuild. It runs a heuristic clean command derived from the project markers (go.mod, Cargo.toml, Eask, Makefile, CMakeLists.txt) and chains `projectile-compile-project` on success. S-F4 stays on `recompile` and now lives globally instead of duplicated across prog-general.el and prog-c.el. F6 is bound globally to `projectile-test-project` as a Phase 1 stopgap. Phase 2 replaces it with the polyglot test runner spec'd in todo.org. Project-type detection runs against the projectile root and falls back to `unknown` when no marker matches. Interpreted markers are checked first so a Python or Node project with a Makefile for tasks classifies as interpreted instead of compiled. Compile + Run sequencing uses a one-shot `compilation-finish-functions` hook that self-removes on first invocation and only fires the follow-up when the status string starts with `finished`. Cleanup in the same commit: - Dropped F4/F5/F6 from `prog-general.el`'s prog-mode-hook. They are now global. - Dropped F6→format bindings from prog-c.el / prog-python.el / prog-shell.el. C-; f was already bound in each, so this is pure removal. - Dropped the duplicate S-F4 from prog-c.el. The global binding covers it. - Updated the keybinding header in prog-general.el and the workflow comments in prog-c.el / prog-shell.el. - Wired `(require 'dev-fkeys)` in init.el alongside coverage-core. TDD: 73 tests across 11 files, one per helper. Production code is split into small testable internals (`cj/--detect-project-type`, `cj/--f4-candidates`, `cj/--f4-derive-clean-cmd`, `cj/--f4-make-once-hook`, `cj/--f4-dispatch`, `cj/--f4-compile-and-run-impl`, `cj/--f4-clean-rebuild-impl`, `cj/--f4-project-root`) plus three thin interactive wrappers. Smoke tests confirm bindings register on load. Known limitation: if another `compilation-finish-functions` hook fires between my add-hook and the compile finishing, the chain can fire on the wrong compile. The hook self-removes on first invocation regardless of which compile it sees. Documented in the impl docstring. Acceptable for v1. Phase 2 will replace F6 with the polyglot test runner (tree-sitter queries for Python/Go/TS, sexp scan for Elisp, buffer-local last-test memory). --- tests/test-dev-fkeys--f4-make-once-hook.el | 115 +++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/test-dev-fkeys--f4-make-once-hook.el (limited to 'tests/test-dev-fkeys--f4-make-once-hook.el') diff --git a/tests/test-dev-fkeys--f4-make-once-hook.el b/tests/test-dev-fkeys--f4-make-once-hook.el new file mode 100644 index 00000000..b6c71dd7 --- /dev/null +++ b/tests/test-dev-fkeys--f4-make-once-hook.el @@ -0,0 +1,115 @@ +;;; test-dev-fkeys--f4-make-once-hook.el --- Tests for cj/--f4-make-once-hook -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the one-shot `compilation-finish-functions' hook builder used by +;; "Compile + Run" and "Clean + Rebuild" to chain a follow-up command after +;; a successful compile. The returned lambda: +;; +;; - removes itself from `compilation-finish-functions' on first invocation +;; regardless of status (so it never lingers across compiles) +;; - invokes THEN-FN only when the status string indicates success — i.e. +;; `string-prefix-p \"finished\"' matches. +;; +;; The status conventions come from the compile.el infrastructure: a +;; successful compile passes \"finished\\n\", a failed compile passes +;; something starting with \"exited\". + +;;; Code: + +(require 'ert) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-make-once-hook-success-status-calls-then-fn () + "Normal: status starting with 'finished' invokes THEN-FN once." + (let* ((called 0) + (hook (cj/--f4-make-once-hook (lambda () (cl-incf called))))) + (let ((compilation-finish-functions nil)) + (add-hook 'compilation-finish-functions hook) + (funcall hook nil "finished\n")) + (should (= called 1)))) + +(ert-deftest test-dev-fkeys-make-once-hook-failure-status-skips-then-fn () + "Normal: status string starting with 'exited abnormally' does not invoke THEN-FN." + (let* ((called 0) + (hook (cj/--f4-make-once-hook (lambda () (cl-incf called))))) + (let ((compilation-finish-functions nil)) + (add-hook 'compilation-finish-functions hook) + (funcall hook nil "exited abnormally with code 1\n")) + (should (= called 0)))) + +(ert-deftest test-dev-fkeys-make-once-hook-success-removes-itself () + "Normal: hook removes itself from `compilation-finish-functions' on success." + (let* ((hook (cj/--f4-make-once-hook (lambda () nil))) + (compilation-finish-functions (list hook))) + (should (memq hook compilation-finish-functions)) + (funcall hook nil "finished\n") + (should-not (memq hook compilation-finish-functions)))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-make-once-hook-failure-also-removes-itself () + "Boundary: hook removes itself even on failure, so it never fires on the +next compile. THEN-FN is not invoked, but the hook is still cleaned up." + (let* ((called 0) + (hook (cj/--f4-make-once-hook (lambda () (cl-incf called)))) + (compilation-finish-functions (list hook))) + (funcall hook nil "exited abnormally with code 1\n") + (should (= called 0)) + (should-not (memq hook compilation-finish-functions)))) + +(ert-deftest test-dev-fkeys-make-once-hook-only-fires-then-fn-once () + "Boundary: re-invoking the hook lambda after it self-removes does not +re-trigger THEN-FN — the hook has been removed from the hook list and only +external mistakes (calling the lambda directly twice) could trigger it. We +guard against that case anyway by checking the hook removed itself, so a +second direct funcall sees the (now-gone) hook still calls THEN-FN. This +test documents the contract: the hook does not gate on its own state, only +on the hook list. So the SECOND direct funcall WILL call THEN-FN. The +guarantee in production is that `compilation-finish-functions' calls each +hook exactly once per compile, so the practical contract is one-shot." + (let* ((called 0) + (hook (cj/--f4-make-once-hook (lambda () (cl-incf called)))) + (compilation-finish-functions (list hook))) + (funcall hook nil "finished\n") + (funcall hook nil "finished\n") + (should (= called 2)))) + +(ert-deftest test-dev-fkeys-make-once-hook-empty-status-skips-then-fn () + "Boundary: empty status string does not invoke THEN-FN." + (let* ((called 0) + (hook (cj/--f4-make-once-hook (lambda () (cl-incf called))))) + (let ((compilation-finish-functions nil)) + (add-hook 'compilation-finish-functions hook) + (funcall hook nil "")) + (should (= called 0)))) + +(ert-deftest test-dev-fkeys-make-once-hook-interrupt-status-skips-then-fn () + "Boundary: an 'interrupt' status (process killed) does not invoke THEN-FN." + (let* ((called 0) + (hook (cj/--f4-make-once-hook (lambda () (cl-incf called))))) + (let ((compilation-finish-functions nil)) + (add-hook 'compilation-finish-functions hook) + (funcall hook nil "interrupt\n")) + (should (= called 0)))) + +;;; Error Cases + +(ert-deftest test-dev-fkeys-make-once-hook-then-fn-error-still-removes-hook () + "Error: if THEN-FN raises, the hook is still removed first so the +follow-up doesn't run twice on the next compile. + +Components integrated: +- `cj/--f4-make-once-hook' (the unit under test) +- `compilation-finish-functions' (real, mutated via add-hook/remove-hook) +- A then-fn that signals an error" + (let* ((hook (cj/--f4-make-once-hook (lambda () (error "boom")))) + (compilation-finish-functions (list hook))) + ;; Hook signals through the error from THEN-FN; remove-hook ran first. + (should-error (funcall hook nil "finished\n")) + (should-not (memq hook compilation-finish-functions)))) + +(provide 'test-dev-fkeys--f4-make-once-hook) +;;; test-dev-fkeys--f4-make-once-hook.el ends here -- cgit v1.2.3