diff options
| -rw-r--r-- | init.el | 1 | ||||
| -rw-r--r-- | modules/dev-fkeys.el | 208 | ||||
| -rw-r--r-- | modules/prog-c.el | 20 | ||||
| -rw-r--r-- | modules/prog-general.el | 42 | ||||
| -rw-r--r-- | modules/prog-python.el | 1 | ||||
| -rw-r--r-- | modules/prog-shell.el | 8 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--detect-project-type.el | 117 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--f4-candidates.el | 66 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--f4-clean-rebuild-impl.el | 118 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--f4-clean-rebuild.el | 61 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--f4-compile-and-run-impl.el | 75 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--f4-compile-and-run.el | 94 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--f4-compile-only.el | 61 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--f4-derive-clean-cmd.el | 93 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--f4-dispatch.el | 78 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--f4-make-once-hook.el | 115 | ||||
| -rw-r--r-- | tests/test-dev-fkeys--f4-project-root.el | 51 |
17 files changed, 1166 insertions, 43 deletions
@@ -50,6 +50,7 @@ (require 'browser-config) ;; browser configuration/integration (require 'coverage-core) ;; diff-aware coverage engine + F7 binding (require 'coverage-elisp) ;; elisp backend for coverage-core +(require 'dev-fkeys) ;; F4 compile+run dispatcher, F6 tests stopgap ;; ------------------------ User Interface Configuration ----------------------- diff --git a/modules/dev-fkeys.el b/modules/dev-fkeys.el new file mode 100644 index 00000000..d2c70131 --- /dev/null +++ b/modules/dev-fkeys.el @@ -0,0 +1,208 @@ +;;; dev-fkeys.el --- Developer F-key dispatchers -*- lexical-binding: t -*- + +;;; Commentary: +;; Project-aware F-key block for developer workflows: +;; +;; F4 completing-read of compile/run candidates filtered by project type +;; C-F4 fast path: compile only (no-op on interpreted projects) +;; M-F4 fast path: clean + rebuild (no-op on interpreted projects) +;; S-F4 recompile (built-in) +;; F6 project tests (Phase 1 stopgap; Phase 2 will replace 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 before +;; compiled markers, so a Python or Node project that also has a Makefile +;; for tasks classifies as interpreted. +;; +;; F7 (coverage) is wired in coverage-core.el. F5 is reserved for the debug +;; ticket and intentionally left unbound here. + +;;; Code: + +(require 'cl-lib) + +(declare-function projectile-compile-project "projectile" (arg)) +(declare-function projectile-run-project "projectile" (arg)) +(declare-function projectile-project-root "projectile" (&optional dir)) +(declare-function projectile-test-project "projectile" (arg)) +(declare-function recompile "compile" (&optional edit-command)) + +;; ---------- Project root ---------- + +(defun cj/--f4-project-root () + "Return projectile project root, or nil. Never errors. +Some projectile configurations signal an error when called outside a +known project; this wrapper degrades to nil so the F4 dispatcher routes +to the \\='unknown branch instead of crashing." + (when (fboundp 'projectile-project-root) + (condition-case nil + (projectile-project-root) + (error nil)))) + +;; ---------- Project-type detection ---------- + +(defconst cj/--f4-interpreted-markers + '("pyproject.toml" "requirements.txt" "Pipfile" "package.json") + "Markers that classify a project as interpreted (Run-only menu).") + +(defconst cj/--f4-compiled-markers + '("go.mod" "Cargo.toml" "CMakeLists.txt" "Makefile" "Eask") + "Markers that classify a project as compiled (full Compile/Run/Clean menu).") + +(defun cj/--f4-any-marker-p (root markers) + "Return non-nil if any of MARKERS exists at ROOT." + (cl-some (lambda (m) (file-exists-p (expand-file-name m root))) markers)) + +(defun cj/--detect-project-type (root) + "Classify project at ROOT as \\='compiled, \\='interpreted, or \\='unknown. +Interpreted markers are checked before compiled markers, so a Python or +Node project that also has a Makefile for tasks classifies as interpreted. +Returns \\='unknown when ROOT is nil or no marker matches." + (cond + ((not root) 'unknown) + ((cj/--f4-any-marker-p root cj/--f4-interpreted-markers) 'interpreted) + ((cj/--f4-any-marker-p root cj/--f4-compiled-markers) 'compiled) + (t 'unknown))) + +;; ---------- Clean command derivation ---------- + +(defun cj/--f4-derive-clean-cmd (root) + "Pick a clean shell command for the project at ROOT, or nil. +First marker matched wins. ROOT may be nil; nil and a path with no +recognized markers both return nil." + (when root + (cond + ((file-exists-p (expand-file-name "go.mod" root)) "go clean ./...") + ((file-exists-p (expand-file-name "Cargo.toml" root)) "cargo clean") + ((file-exists-p (expand-file-name "Eask" root)) "eask clean") + ((file-exists-p (expand-file-name "Makefile" root)) "make clean") + ((file-exists-p (expand-file-name "CMakeLists.txt" root)) "cmake --build build --target clean")))) + +;; ---------- Action handlers ---------- + +(defun cj/--f4-compile-and-run-impl () + "Run `projectile-compile-project', then `projectile-run-project' on success. +Installs a one-shot `compilation-finish-functions' hook to chain the run." + (add-hook 'compilation-finish-functions + (cj/--f4-make-once-hook + (lambda () (projectile-run-project nil)))) + (projectile-compile-project nil)) + +(defun cj/--f4-dispatch (action) + "Route ACTION (a symbol from `cj/--f4-candidates') to its handler. +Signals `user-error' on an unrecognized symbol or nil." + (pcase action + ('compile-only (projectile-compile-project nil)) + ('run-only (projectile-run-project nil)) + ('compile-and-run (cj/--f4-compile-and-run-impl)) + ('clean-rebuild (cj/--f4-clean-rebuild-impl (cj/--f4-project-root))) + ('compile-plain (call-interactively #'compile)) + (_ (user-error "Unknown F4 action: %s" action)))) + +(defun cj/--f4-clean-rebuild-impl (root) + "Run the heuristic clean for project at ROOT, then rebuild on success. +Signals `user-error' when no clean command can be derived. The rebuild +side reuses `projectile-compile-project' so the per-project compile +command (prompted-and-cached by projectile) drives the build." + (let ((clean-cmd (cj/--f4-derive-clean-cmd root))) + (unless clean-cmd + (user-error "Clean + Rebuild: no clean command for this project type")) + (add-hook 'compilation-finish-functions + (cj/--f4-make-once-hook + (lambda () (projectile-compile-project nil)))) + (let ((default-directory root)) + (compile clean-cmd)))) + +;; ---------- One-shot compilation-finish hook ---------- + +(defun cj/--f4-make-once-hook (then-fn) + "Build a one-shot `compilation-finish-functions' hook that chains THEN-FN. +The returned lambda removes itself from `compilation-finish-functions' on +first invocation regardless of status, then calls THEN-FN only if the +status string starts with \"finished\" (the convention used by compile.el +for a successful compile)." + (let (hook) + (setq hook + (lambda (_buf status) + (remove-hook 'compilation-finish-functions hook) + (when (and (stringp status) + (string-prefix-p "finished" status)) + (funcall then-fn)))) + hook)) + +;; ---------- Candidate menus ---------- + +(defun cj/--f4-candidates (project-type) + "Return alist of (LABEL . ACTION) for the F4 menu given PROJECT-TYPE. +The first entry is the default, selected on RET in completing-read. +Compiled projects get the full menu, interpreted projects get Run only, +anything else (including nil and unrecognized symbols) falls through to +a single Compile entry that calls plain `compile'." + (pcase project-type + ('compiled '(("Compile + Run" . compile-and-run) + ("Compile" . compile-only) + ("Run" . run-only) + ("Clean + Rebuild" . clean-rebuild))) + ('interpreted '(("Run" . run-only))) + (_ '(("Compile" . compile-plain))))) + +;; ---------- Interactive wrappers ---------- + +(defun cj/f4-compile-and-run () + "Project-aware F4 dispatcher. +Prompts via `completing-read' with a candidate set filtered by project +type (compiled / interpreted / unknown), then dispatches the chosen +label's action." + (interactive) + (let* ((root (cj/--f4-project-root)) + (project-type (cj/--detect-project-type root)) + (candidates (cj/--f4-candidates project-type)) + (default (caar candidates)) + (label (completing-read + (format "F4 (%s): " project-type) + (mapcar #'car candidates) nil t nil nil default)) + (action (cdr (assoc label candidates)))) + (cj/--f4-dispatch action))) + +(defun cj/f4-compile-only () + "C-F4 fast path: compile only. +Compiled projects run `projectile-compile-project'. Interpreted projects +get a no-op message. Outside any project, falls back to interactive +`compile'." + (interactive) + (let* ((root (cj/--f4-project-root)) + (project-type (cj/--detect-project-type root))) + (pcase project-type + ('compiled (projectile-compile-project nil)) + ('interpreted (message "C-F4: not a compiled language")) + (_ (call-interactively #'compile))))) + +(defun cj/f4-clean-rebuild () + "M-F4 fast path: clean + rebuild. +Compiled projects run the heuristic clean + projectile-compile-project +chain. Interpreted projects and unrecognized projects get a no-op +message." + (interactive) + (let* ((root (cj/--f4-project-root)) + (project-type (cj/--detect-project-type root))) + (pcase project-type + ('compiled (cj/--f4-clean-rebuild-impl root)) + ('interpreted (message "M-F4: not a compiled language")) + (_ (message "M-F4: no project detected"))))) + +;; ---------- Bindings ---------- + +(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) +(keymap-global-set "S-<f4>" #'recompile) +;; Phase 1 stopgap. Phase 2 replaces this with the polyglot test runner +;; spec'd in todo.org. Without a global F6 binding here, the per-language +;; F6→format bindings in prog-c/python/shell would have nothing to fall +;; back on after this commit drops them, leaving F6 on its useless Emacs +;; default (`2C-command'). +(keymap-global-set "<f6>" #'projectile-test-project) + +(provide 'dev-fkeys) +;;; dev-fkeys.el ends here. diff --git a/modules/prog-c.el b/modules/prog-c.el index d9191bf4..df32b76a 100644 --- a/modules/prog-c.el +++ b/modules/prog-c.el @@ -16,12 +16,12 @@ ;; ;; Workflow Example: ;; 1. Open a .c file → LSP auto-starts, provides completions -;; 2. S-<f2> → Compile (auto-detects Makefile/CMake/single file) -;; 3. <f5> → Quick recompile -;; 4. S-<f3> → Start GDB with multi-window layout -;; 5. <f6> or C-c f → Format code with clang-format -;; 6. M-. → Jump to function definition -;; 7. C-c l → Access LSP commands (rename, find references, etc.) +;; 2. F4 → compile + run dispatcher (dev-fkeys.el) +;; 3. S-F4 → recompile (repeat last) +;; 4. S-F6 → start GDB +;; 5. C-; f → format code with clang-format +;; 6. M-. → jump to function definition +;; 7. C-c l → LSP commands (rename, find references, etc.) ;;; Code: @@ -95,7 +95,6 @@ (use-package clang-format :if (executable-find clang-format-path) :bind (:map c-mode-base-map - ("<f6>" . clang-format-buffer) ("C-; f" . clang-format-buffer))) ;; -------------------------------- Compilation -------------------------------- @@ -135,11 +134,8 @@ ;; -------------------------------- Keybindings -------------------------------- (defun cj/c-mode-keybindings () - "Set up keybindings for C programming. -Overrides default prog-mode keybindings with C-specific commands." - ;; S-f4: Recompile (override default - C uses this more than projectile-compile) - (local-set-key (kbd "S-<f4>") #'recompile) - + "Set up C-specific S-modifier overrides on the dev F-keys. +S-F4 (recompile) is global, owned by dev-fkeys.el — not duplicated here." ;; S-f5: Static analysis placeholder (could add clang-tidy, cppcheck, etc.) (local-set-key (kbd "S-<f5>") #'cj/disabled) diff --git a/modules/prog-general.el b/modules/prog-general.el index 0ae6aa82..46599cc9 100644 --- a/modules/prog-general.el +++ b/modules/prog-general.el @@ -8,28 +8,22 @@ ;; ;; Keybinding Scheme: ;; ------------------ -;; Unified keybindings across all programming languages using Projectile -;; for project-aware operations with language-specific overrides. +;; The F4–F7 dev block is owned by dev-fkeys.el (global bindings). Per- +;; language modules only set S-F5 / S-F6 overrides for static analysis +;; and debugging. F5 is reserved for the debug ticket. ;; -;; Global Keybindings (all prog-mode buffers): -;; F4 - projectile-compile-project (smart compilation) -;; S-F4 - recompile (repeat last compile) -;; F5 - projectile-test-project (run tests) -;; S-F5 - Language-specific static analysis -;; F6 - projectile-run-project (run/execute) -;; S-F6 - Language-specific debugger -;; C-; f - Language-specific formatter +;; Global (dev-fkeys.el): +;; F4 / C-F4 / M-F4 compile + run dispatcher / compile only / clean + rebuild +;; S-F4 recompile (repeat last) +;; F6 project tests (Phase 1 stopgap; Phase 2 = polyglot dispatcher) +;; F7 coverage report (coverage-core.el) +;; C-; f language-specific formatter ;; -;; Quick Reference Table: -;; | Key | Global | C | Go | Python | Shell | -;; |-------|----------|---------------|-------------|-------------|-------------| -;; | F4 | compile | compile | compile | compile | compile | -;; | S-F4 | recompile| recompile | (projectile)| (projectile)| (projectile)| -;; | F5 | test | test | test | test | test | -;; | S-F5 | (none) | disabled | staticcheck | mypy | shellcheck | -;; | F6 | run | run | run | run | run | -;; | S-F6 | (none) | gdb | dlv | pdb | disabled | -;; | C-; f | format | clang-format | gofmt | blacken | shfmt | +;; Per-language S-modifier overrides: +;; | Key | C | Go | Python | Shell | +;; |------|----------|-------------|--------|------------| +;; | S-F5 | disabled | staticcheck | mypy | shellcheck | +;; | S-F6 | gdb | dlv | pdb | disabled | ;;; Code: @@ -79,11 +73,9 @@ (auto-fill-mode) ;; auto wrap at the fill column set (local-set-key (kbd "M-;") 'comment-dwim) ;; comment/uncomment region as appropriate - ;; Project-wide commands (can be overridden by language-specific modes) - (local-set-key (kbd "<f4>") 'projectile-compile-project) ;; compile project - (local-set-key (kbd "S-<f4>") 'recompile) ;; recompile (repeat last) - (local-set-key (kbd "<f5>") 'projectile-test-project) ;; run tests - (local-set-key (kbd "<f6>") 'projectile-run-project)) ;; run project + ;; F4–F6 are global, owned by dev-fkeys.el. F5 is reserved for the + ;; debug ticket (separate work). + ) (add-hook 'prog-mode-hook #'cj/general-prog-settings) (add-hook 'html-mode-hook #'cj/general-prog-settings) diff --git a/modules/prog-python.el b/modules/prog-python.el index 3b85bafd..a26b9760 100644 --- a/modules/prog-python.el +++ b/modules/prog-python.el @@ -128,7 +128,6 @@ Overrides default prog-mode keybindings with Python-specific commands." (blacken-skip-string-normalization t) :hook (python-ts-mode . blacken-mode) :bind (:map python-ts-mode-map - ("<f6>" . blacken-buffer) ("C-; f" . blacken-buffer))) ;; ---------------------------------- Numpydoc --------------------------------- diff --git a/modules/prog-shell.el b/modules/prog-shell.el index cb9598db..57347cc6 100644 --- a/modules/prog-shell.el +++ b/modules/prog-shell.el @@ -17,9 +17,9 @@ ;; ;; Workflow: ;; 1. Open .sh file → LSP auto-starts, ShellCheck runs -;; 2. <f6> → Format with shfmt -;; 3. C-c ! l → Show all ShellCheck diagnostics -;; 4. Save → Auto-set executable bit if script has shebang +;; 2. C-; f → format with shfmt +;; 3. C-c ! l → show all ShellCheck diagnostics +;; 4. Save → auto-set executable bit if script has shebang ;;; Code: @@ -143,10 +143,8 @@ Overrides default prog-mode keybindings with shell-specific commands." :if (executable-find shfmt-path) :hook ((sh-mode bash-ts-mode) . shfmt-on-save-mode) :bind ((:map sh-mode-map - ("<f6>" . shfmt-buffer) ("C-; f" . shfmt-buffer)) (:map bash-ts-mode-map - ("<f6>" . shfmt-buffer) ("C-; f" . shfmt-buffer))) :custom (shfmt-arguments '("-i" "2" ;; indent with 2 spaces diff --git a/tests/test-dev-fkeys--detect-project-type.el b/tests/test-dev-fkeys--detect-project-type.el new file mode 100644 index 00000000..3b306484 --- /dev/null +++ b/tests/test-dev-fkeys--detect-project-type.el @@ -0,0 +1,117 @@ +;;; test-dev-fkeys--detect-project-type.el --- Tests for cj/--detect-project-type -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the project-type classifier in dev-fkeys.el. +;; The classifier returns 'compiled, 'interpreted, or 'unknown based on +;; marker files at the project root. Interpreted markers are checked first +;; so a Python or Node project with a Makefile for tasks classifies as +;; interpreted. + +;;; Code: + +(require 'ert) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +(defmacro test-dev-fkeys--with-project-dir (markers &rest body) + "Create a temp project dir with each filename in MARKERS as an empty file. +Bind the dir path to ROOT in BODY. Cleans up on exit." + (declare (indent 1)) + `(let ((root (make-temp-file "test-dev-fkeys-" t))) + (unwind-protect + (progn + (dolist (marker ,markers) + (write-region "" nil (expand-file-name marker root))) + ,@body) + (delete-directory root t)))) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-detect-project-type-go-mod-is-compiled () + "Normal: go.mod marker classifies as compiled." + (test-dev-fkeys--with-project-dir '("go.mod") + (should (eq (cj/--detect-project-type root) 'compiled)))) + +(ert-deftest test-dev-fkeys-detect-project-type-cargo-toml-is-compiled () + "Normal: Cargo.toml marker classifies as compiled." + (test-dev-fkeys--with-project-dir '("Cargo.toml") + (should (eq (cj/--detect-project-type root) 'compiled)))) + +(ert-deftest test-dev-fkeys-detect-project-type-cmakelists-is-compiled () + "Normal: CMakeLists.txt marker classifies as compiled." + (test-dev-fkeys--with-project-dir '("CMakeLists.txt") + (should (eq (cj/--detect-project-type root) 'compiled)))) + +(ert-deftest test-dev-fkeys-detect-project-type-makefile-is-compiled () + "Normal: Makefile marker classifies as compiled." + (test-dev-fkeys--with-project-dir '("Makefile") + (should (eq (cj/--detect-project-type root) 'compiled)))) + +(ert-deftest test-dev-fkeys-detect-project-type-eask-is-compiled () + "Normal: Eask marker classifies as compiled." + (test-dev-fkeys--with-project-dir '("Eask") + (should (eq (cj/--detect-project-type root) 'compiled)))) + +(ert-deftest test-dev-fkeys-detect-project-type-pyproject-is-interpreted () + "Normal: pyproject.toml marker classifies as interpreted." + (test-dev-fkeys--with-project-dir '("pyproject.toml") + (should (eq (cj/--detect-project-type root) 'interpreted)))) + +(ert-deftest test-dev-fkeys-detect-project-type-requirements-is-interpreted () + "Normal: requirements.txt marker classifies as interpreted." + (test-dev-fkeys--with-project-dir '("requirements.txt") + (should (eq (cj/--detect-project-type root) 'interpreted)))) + +(ert-deftest test-dev-fkeys-detect-project-type-pipfile-is-interpreted () + "Normal: Pipfile marker classifies as interpreted." + (test-dev-fkeys--with-project-dir '("Pipfile") + (should (eq (cj/--detect-project-type root) 'interpreted)))) + +(ert-deftest test-dev-fkeys-detect-project-type-package-json-is-interpreted () + "Normal: package.json marker classifies as interpreted." + (test-dev-fkeys--with-project-dir '("package.json") + (should (eq (cj/--detect-project-type root) 'interpreted)))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-detect-project-type-interpreted-wins-over-compiled () + "Boundary: a Python project with a Makefile classifies as interpreted. +Interpreted markers are checked first so task-runner Makefiles in +Python/Node projects don't misclassify them as compiled." + (test-dev-fkeys--with-project-dir '("pyproject.toml" "Makefile") + (should (eq (cj/--detect-project-type root) 'interpreted)))) + +(ert-deftest test-dev-fkeys-detect-project-type-package-json-wins-over-makefile () + "Boundary: package.json + Makefile classifies as interpreted." + (test-dev-fkeys--with-project-dir '("package.json" "Makefile") + (should (eq (cj/--detect-project-type root) 'interpreted)))) + +(ert-deftest test-dev-fkeys-detect-project-type-pure-c-with-makefile-is-compiled () + "Boundary: a Makefile alone (no interpreted markers) classifies as compiled. +This is the typical pure-C project case." + (test-dev-fkeys--with-project-dir '("Makefile") + (should (eq (cj/--detect-project-type root) 'compiled)))) + +(ert-deftest test-dev-fkeys-detect-project-type-no-markers-is-unknown () + "Boundary: directory with no markers classifies as unknown." + (test-dev-fkeys--with-project-dir '() + (should (eq (cj/--detect-project-type root) 'unknown)))) + +(ert-deftest test-dev-fkeys-detect-project-type-irrelevant-file-is-unknown () + "Boundary: unrelated files in root don't trigger any classification." + (test-dev-fkeys--with-project-dir '("README.md" "LICENSE" ".gitignore") + (should (eq (cj/--detect-project-type root) 'unknown)))) + +;;; Error Cases + +(ert-deftest test-dev-fkeys-detect-project-type-nil-root-is-unknown () + "Error: nil ROOT returns 'unknown (buffer not in a project)." + (should (eq (cj/--detect-project-type nil) 'unknown))) + +(ert-deftest test-dev-fkeys-detect-project-type-nonexistent-root-is-unknown () + "Error: a directory path that doesn't exist returns 'unknown. +file-exists-p on a non-existent path returns nil, so no marker matches." + (should (eq (cj/--detect-project-type "/nonexistent/path/xyzzy") 'unknown))) + +(provide 'test-dev-fkeys--detect-project-type) +;;; test-dev-fkeys--detect-project-type.el ends here diff --git a/tests/test-dev-fkeys--f4-candidates.el b/tests/test-dev-fkeys--f4-candidates.el new file mode 100644 index 00000000..a2d1afa4 --- /dev/null +++ b/tests/test-dev-fkeys--f4-candidates.el @@ -0,0 +1,66 @@ +;;; test-dev-fkeys--f4-candidates.el --- Tests for cj/--f4-candidates -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the candidate-menu builder. Returns an alist of +;; (LABEL . ACTION) for the F4 completing-read prompt. The first entry +;; is the default (selected on RET). + +;;; 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-f4-candidates-compiled-has-four-entries () + "Normal: compiled project gets all four candidates." + (let ((cands (cj/--f4-candidates 'compiled))) + (should (= (length cands) 4)))) + +(ert-deftest test-dev-fkeys-f4-candidates-compiled-default-is-compile-and-run () + "Normal: first candidate for compiled is Compile + Run (the default)." + (let ((cands (cj/--f4-candidates 'compiled))) + (should (equal (caar cands) "Compile + Run")) + (should (eq (cdar cands) 'compile-and-run)))) + +(ert-deftest test-dev-fkeys-f4-candidates-compiled-includes-all-actions () + "Normal: compiled menu maps each label to its action symbol." + (let ((cands (cj/--f4-candidates 'compiled))) + (should (eq (cdr (assoc "Compile + Run" cands)) 'compile-and-run)) + (should (eq (cdr (assoc "Compile" cands)) 'compile-only)) + (should (eq (cdr (assoc "Run" cands)) 'run-only)) + (should (eq (cdr (assoc "Clean + Rebuild" cands)) 'clean-rebuild)))) + +(ert-deftest test-dev-fkeys-f4-candidates-interpreted-is-run-only () + "Normal: interpreted project gets a single Run candidate." + (let ((cands (cj/--f4-candidates 'interpreted))) + (should (= (length cands) 1)) + (should (equal (caar cands) "Run")) + (should (eq (cdar cands) 'run-only)))) + +(ert-deftest test-dev-fkeys-f4-candidates-unknown-is-compile-plain () + "Normal: unknown project gets a single Compile candidate that calls plain compile." + (let ((cands (cj/--f4-candidates 'unknown))) + (should (= (length cands) 1)) + (should (equal (caar cands) "Compile")) + (should (eq (cdar cands) 'compile-plain)))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-f4-candidates-bogus-symbol-falls-back-to-unknown () + "Boundary: an unrecognized project-type symbol falls through to the unknown branch." + (let ((cands (cj/--f4-candidates 'fictional-type))) + (should (= (length cands) 1)) + (should (eq (cdar cands) 'compile-plain)))) + +;;; Error Cases + +(ert-deftest test-dev-fkeys-f4-candidates-nil-falls-back-to-unknown () + "Error: nil project-type falls through to the unknown branch." + (let ((cands (cj/--f4-candidates nil))) + (should (= (length cands) 1)) + (should (eq (cdar cands) 'compile-plain)))) + +(provide 'test-dev-fkeys--f4-candidates) +;;; test-dev-fkeys--f4-candidates.el ends here diff --git a/tests/test-dev-fkeys--f4-clean-rebuild-impl.el b/tests/test-dev-fkeys--f4-clean-rebuild-impl.el new file mode 100644 index 00000000..27c7c56a --- /dev/null +++ b/tests/test-dev-fkeys--f4-clean-rebuild-impl.el @@ -0,0 +1,118 @@ +;;; test-dev-fkeys--f4-clean-rebuild-impl.el --- Tests for cj/--f4-clean-rebuild-impl -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the "Clean + Rebuild" action handler. Runs the heuristic clean +;; command via `compile' from the project root, then chains +;; `projectile-compile-project' on success via the one-shot finish hook. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +(defmacro test-dev-fkeys-cr--with-project (markers &rest body) + "Create a temp project dir with each filename in MARKERS as an empty file. +Bind the dir path to ROOT in BODY. Cleans up on exit." + (declare (indent 1)) + `(let ((root (make-temp-file "test-dev-fkeys-cr-" t))) + (unwind-protect + (progn + (dolist (marker ,markers) + (write-region "" nil (expand-file-name marker root))) + ,@body) + (delete-directory root t)))) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-clean-rebuild-impl-runs-derived-clean-cmd () + "Normal: handler invokes `compile' with the heuristic clean command for the +project marker present at ROOT. + +Components integrated: +- `cj/--f4-clean-rebuild-impl' (unit under test) +- `cj/--f4-derive-clean-cmd' (real) +- `compile' (MOCKED — captures the command string) +- `projectile-compile-project' (MOCKED — no-op) +- `compilation-finish-functions' (real, scoped via let)" + (test-dev-fkeys-cr--with-project '("Makefile") + (let ((compile-calls nil) + (compilation-finish-functions nil)) + (cl-letf (((symbol-function 'compile) + (lambda (cmd) (push cmd compile-calls))) + ((symbol-function 'projectile-compile-project) + (lambda (_arg) nil))) + (cj/--f4-clean-rebuild-impl root) + (should (equal compile-calls '("make clean"))))))) + +(ert-deftest test-dev-fkeys-clean-rebuild-impl-installs-finish-hook () + "Normal: handler installs exactly one hook in `compilation-finish-functions'." + (test-dev-fkeys-cr--with-project '("go.mod") + (let ((compilation-finish-functions nil)) + (cl-letf (((symbol-function 'compile) (lambda (_cmd) nil)) + ((symbol-function 'projectile-compile-project) + (lambda (_arg) nil))) + (cj/--f4-clean-rebuild-impl root) + (should (= (length compilation-finish-functions) 1)))))) + +(ert-deftest test-dev-fkeys-clean-rebuild-impl-hook-runs-projectile-compile-on-success () + "Normal: when the clean step finishes successfully, the installed hook +calls `projectile-compile-project' to do the rebuild." + (test-dev-fkeys-cr--with-project '("Cargo.toml") + (let ((compile-calls 0) + (compilation-finish-functions nil)) + (cl-letf (((symbol-function 'compile) (lambda (_cmd) nil)) + ((symbol-function 'projectile-compile-project) + (lambda (_arg) (cl-incf compile-calls)))) + (cj/--f4-clean-rebuild-impl root) + (run-hook-with-args 'compilation-finish-functions nil "finished\n") + (should (= compile-calls 1)))))) + +(ert-deftest test-dev-fkeys-clean-rebuild-impl-runs-clean-from-project-root () + "Normal: the clean compile runs with default-directory bound to ROOT." + (test-dev-fkeys-cr--with-project '("Eask") + (let ((seen-dir nil) + (compilation-finish-functions nil)) + (cl-letf (((symbol-function 'compile) + (lambda (_cmd) (setq seen-dir default-directory))) + ((symbol-function 'projectile-compile-project) + (lambda (_arg) nil))) + (cj/--f4-clean-rebuild-impl root) + (should (string= (file-name-as-directory seen-dir) + (file-name-as-directory root))))))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-clean-rebuild-impl-hook-skips-rebuild-on-failure () + "Boundary: when the clean step fails, projectile-compile-project does not run." + (test-dev-fkeys-cr--with-project '("Makefile") + (let ((compile-calls 0) + (compilation-finish-functions nil)) + (cl-letf (((symbol-function 'compile) (lambda (_cmd) nil)) + ((symbol-function 'projectile-compile-project) + (lambda (_arg) (cl-incf compile-calls)))) + (cj/--f4-clean-rebuild-impl root) + (run-hook-with-args 'compilation-finish-functions nil "exited abnormally\n") + (should (= compile-calls 0)))))) + +;;; Error Cases + +(ert-deftest test-dev-fkeys-clean-rebuild-impl-no-clean-cmd-signals-user-error () + "Error: a project root with no recognized markers signals a user-error +rather than silently running nothing." + (test-dev-fkeys-cr--with-project '("README.md") + (cl-letf (((symbol-function 'compile) (lambda (_cmd) nil)) + ((symbol-function 'projectile-compile-project) + (lambda (_arg) nil))) + (should-error (cj/--f4-clean-rebuild-impl root) :type 'user-error)))) + +(ert-deftest test-dev-fkeys-clean-rebuild-impl-nil-root-signals-user-error () + "Error: a nil root signals a user-error (no project detected)." + (cl-letf (((symbol-function 'compile) (lambda (_cmd) nil)) + ((symbol-function 'projectile-compile-project) + (lambda (_arg) nil))) + (should-error (cj/--f4-clean-rebuild-impl nil) :type 'user-error))) + +(provide 'test-dev-fkeys--f4-clean-rebuild-impl) +;;; test-dev-fkeys--f4-clean-rebuild-impl.el ends here diff --git a/tests/test-dev-fkeys--f4-clean-rebuild.el b/tests/test-dev-fkeys--f4-clean-rebuild.el new file mode 100644 index 00000000..040ce930 --- /dev/null +++ b/tests/test-dev-fkeys--f4-clean-rebuild.el @@ -0,0 +1,61 @@ +;;; test-dev-fkeys--f4-clean-rebuild.el --- Smoke tests for cj/f4-clean-rebuild -*- lexical-binding: t -*- + +;;; Commentary: +;; Smoke tests for the M-F4 fast path. On a compiled project, runs the +;; clean-rebuild handler. Interpreted and unknown projects get a no-op +;; message — no rebuild attempt, no error. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +(defmacro test-dev-fkeys-cr-w--with-project (markers &rest body) + "Set up a temp project with MARKERS, bind ROOT, run BODY, clean up." + (declare (indent 1)) + `(let ((root (make-temp-file "test-dev-fkeys-cr-w-" t))) + (unwind-protect + (progn + (dolist (marker ,markers) + (write-region "" nil (expand-file-name marker root))) + ,@body) + (delete-directory root t)))) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-f4-clean-rebuild-compiled-project-runs-impl-with-root () + "Normal: on a compiled project, calls cj/--f4-clean-rebuild-impl with the +project root." + (test-dev-fkeys-cr-w--with-project '("Makefile") + (let (received-root) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () root)) + ((symbol-function 'cj/--f4-clean-rebuild-impl) + (lambda (r) (setq received-root r)))) + (cj/f4-clean-rebuild) + (should (string= received-root root)))))) + +(ert-deftest test-dev-fkeys-f4-clean-rebuild-interpreted-project-skips-impl () + "Normal: on an interpreted project, the impl handler is not invoked." + (test-dev-fkeys-cr-w--with-project '("pyproject.toml") + (let ((calls 0)) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () root)) + ((symbol-function 'cj/--f4-clean-rebuild-impl) + (lambda (_r) (cl-incf calls)))) + (cj/f4-clean-rebuild) + (should (= calls 0)))))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-f4-clean-rebuild-unknown-project-skips-impl () + "Boundary: outside any project, the impl handler is not invoked." + (let ((calls 0)) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () nil)) + ((symbol-function 'cj/--f4-clean-rebuild-impl) + (lambda (_r) (cl-incf calls)))) + (cj/f4-clean-rebuild) + (should (= calls 0))))) + +(provide 'test-dev-fkeys--f4-clean-rebuild) +;;; test-dev-fkeys--f4-clean-rebuild.el ends here diff --git a/tests/test-dev-fkeys--f4-compile-and-run-impl.el b/tests/test-dev-fkeys--f4-compile-and-run-impl.el new file mode 100644 index 00000000..d59a6cd6 --- /dev/null +++ b/tests/test-dev-fkeys--f4-compile-and-run-impl.el @@ -0,0 +1,75 @@ +;;; test-dev-fkeys--f4-compile-and-run-impl.el --- Tests for cj/--f4-compile-and-run-impl -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the "Compile + Run" action handler. After kicking off the +;; compile, attaches a one-shot `compilation-finish-functions' hook that +;; runs the project on success. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-compile-and-run-impl-invokes-projectile-compile () + "Normal: handler calls `projectile-compile-project'. + +Components integrated: +- `cj/--f4-compile-and-run-impl' (unit under test) +- `projectile-compile-project' (MOCKED via cl-letf) +- `compilation-finish-functions' (real, scoped via let)" + (let ((compile-calls 0) + (compilation-finish-functions nil)) + (cl-letf (((symbol-function 'projectile-compile-project) + (lambda (_arg) (cl-incf compile-calls)))) + (cj/--f4-compile-and-run-impl) + (should (= compile-calls 1))))) + +(ert-deftest test-dev-fkeys-compile-and-run-impl-installs-finish-hook () + "Normal: handler installs exactly one hook in `compilation-finish-functions'." + (let ((compilation-finish-functions nil)) + (cl-letf (((symbol-function 'projectile-compile-project) + (lambda (_arg) nil))) + (cj/--f4-compile-and-run-impl) + (should (= (length compilation-finish-functions) 1))))) + +(ert-deftest test-dev-fkeys-compile-and-run-impl-hook-runs-projectile-run-on-success () + "Normal: when the compile finishes successfully, the installed hook calls +`projectile-run-project'. + +Components integrated: +- `cj/--f4-compile-and-run-impl' (unit under test) +- `projectile-compile-project' (MOCKED — no-op) +- `projectile-run-project' (MOCKED — counts calls) +- `compilation-finish-functions' (real) +- `run-hook-with-args' (real — simulates compile.el firing the hook)" + (let ((run-calls 0) + (compilation-finish-functions nil)) + (cl-letf (((symbol-function 'projectile-compile-project) + (lambda (_arg) nil)) + ((symbol-function 'projectile-run-project) + (lambda (_arg) (cl-incf run-calls)))) + (cj/--f4-compile-and-run-impl) + (run-hook-with-args 'compilation-finish-functions nil "finished\n") + (should (= run-calls 1))))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-compile-and-run-impl-hook-skips-projectile-run-on-failure () + "Boundary: when the compile fails, projectile-run-project must not run. +The hook still self-removes (covered in the make-once-hook tests)." + (let ((run-calls 0) + (compilation-finish-functions nil)) + (cl-letf (((symbol-function 'projectile-compile-project) + (lambda (_arg) nil)) + ((symbol-function 'projectile-run-project) + (lambda (_arg) (cl-incf run-calls)))) + (cj/--f4-compile-and-run-impl) + (run-hook-with-args 'compilation-finish-functions nil "exited abnormally\n") + (should (= run-calls 0))))) + +(provide 'test-dev-fkeys--f4-compile-and-run-impl) +;;; test-dev-fkeys--f4-compile-and-run-impl.el ends here diff --git a/tests/test-dev-fkeys--f4-compile-and-run.el b/tests/test-dev-fkeys--f4-compile-and-run.el new file mode 100644 index 00000000..3e0da877 --- /dev/null +++ b/tests/test-dev-fkeys--f4-compile-and-run.el @@ -0,0 +1,94 @@ +;;; test-dev-fkeys--f4-compile-and-run.el --- Smoke tests for cj/f4-compile-and-run -*- lexical-binding: t -*- + +;;; Commentary: +;; Smoke tests for the interactive F4 wrapper. The wrapper: +;; +;; 1. Resolves the project root. +;; 2. Detects project type from markers. +;; 3. Builds the candidate list. +;; 4. Prompts via completing-read. +;; 5. Looks up the chosen label's action. +;; 6. Dispatches. +;; +;; Per the elisp-testing rule on Interactive vs Internal split, the heavy +;; lifting is in `cj/--f4-dispatch' and the helpers — those are tested +;; directly in their own files. This file just confirms the wrapper wires +;; the pieces together: prompt fires, the chosen label routes to the right +;; action symbol. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +(defmacro test-dev-fkeys-f4--with-project (markers &rest body) + "Set up a temp project with MARKERS, bind ROOT, run BODY, clean up." + (declare (indent 1)) + `(let ((root (make-temp-file "test-dev-fkeys-f4-" t))) + (unwind-protect + (progn + (dolist (marker ,markers) + (write-region "" nil (expand-file-name marker root))) + ,@body) + (delete-directory root t)))) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-f4-compile-and-run-prompts-with-completing-read () + "Normal: the wrapper invokes completing-read with the project's candidate labels. + +Components integrated: +- `cj/f4-compile-and-run' (unit under test) +- `cj/--f4-project-root' (MOCKED — returns the temp project root) +- `cj/--detect-project-type' (real) +- `cj/--f4-candidates' (real) +- `completing-read' (MOCKED — captures candidates and returns the default) +- `cj/--f4-dispatch' (MOCKED — captures the action it received)" + (test-dev-fkeys-f4--with-project '("go.mod") + (let (seen-candidates seen-action) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () root)) + ((symbol-function 'completing-read) + (lambda (_prompt collection &rest _) + (setq seen-candidates collection) + (car collection))) + ((symbol-function 'cj/--f4-dispatch) + (lambda (action) (setq seen-action action)))) + (cj/f4-compile-and-run) + ;; Compiled-project candidates are the four labels. + (should (member "Compile + Run" seen-candidates)) + (should (member "Compile" seen-candidates)) + (should (member "Run" seen-candidates)) + (should (member "Clean + Rebuild" seen-candidates)) + ;; Default (first label) is "Compile + Run", which routes to + ;; the compile-and-run action. + (should (eq seen-action 'compile-and-run)))))) + +(ert-deftest test-dev-fkeys-f4-compile-and-run-routes-chosen-label-to-action () + "Normal: a non-default label selection routes to the right action." + (test-dev-fkeys-f4--with-project '("Cargo.toml") + (let (seen-action) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () root)) + ((symbol-function 'completing-read) + (lambda (&rest _) "Run")) + ((symbol-function 'cj/--f4-dispatch) + (lambda (action) (setq seen-action action)))) + (cj/f4-compile-and-run) + (should (eq seen-action 'run-only)))))) + +(ert-deftest test-dev-fkeys-f4-compile-and-run-interpreted-project-shows-run-only () + "Normal: an interpreted project's menu has only the Run candidate." + (test-dev-fkeys-f4--with-project '("pyproject.toml") + (let (seen-candidates) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () root)) + ((symbol-function 'completing-read) + (lambda (_prompt collection &rest _) + (setq seen-candidates collection) + (car collection))) + ((symbol-function 'cj/--f4-dispatch) (lambda (_action) nil))) + (cj/f4-compile-and-run) + (should (equal seen-candidates '("Run"))))))) + +(provide 'test-dev-fkeys--f4-compile-and-run) +;;; test-dev-fkeys--f4-compile-and-run.el ends here diff --git a/tests/test-dev-fkeys--f4-compile-only.el b/tests/test-dev-fkeys--f4-compile-only.el new file mode 100644 index 00000000..b0eec367 --- /dev/null +++ b/tests/test-dev-fkeys--f4-compile-only.el @@ -0,0 +1,61 @@ +;;; test-dev-fkeys--f4-compile-only.el --- Smoke tests for cj/f4-compile-only -*- lexical-binding: t -*- + +;;; Commentary: +;; Smoke tests for the C-F4 fast path. On a compiled project, calls +;; `projectile-compile-project'. On an interpreted project, messages +;; "not a compiled language". With no project detected, falls back to +;; plain `compile'. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +(defmacro test-dev-fkeys-co--with-project (markers &rest body) + "Set up a temp project with MARKERS, bind ROOT, run BODY, clean up." + (declare (indent 1)) + `(let ((root (make-temp-file "test-dev-fkeys-co-" t))) + (unwind-protect + (progn + (dolist (marker ,markers) + (write-region "" nil (expand-file-name marker root))) + ,@body) + (delete-directory root t)))) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-f4-compile-only-compiled-project-runs-projectile-compile () + "Normal: on a compiled project, calls projectile-compile-project." + (test-dev-fkeys-co--with-project '("go.mod") + (let ((calls 0)) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () root)) + ((symbol-function 'projectile-compile-project) + (lambda (_arg) (cl-incf calls)))) + (cj/f4-compile-only) + (should (= calls 1)))))) + +(ert-deftest test-dev-fkeys-f4-compile-only-interpreted-project-skips-compile () + "Normal: on an interpreted project, projectile-compile-project does not run." + (test-dev-fkeys-co--with-project '("pyproject.toml") + (let ((calls 0)) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () root)) + ((symbol-function 'projectile-compile-project) + (lambda (_arg) (cl-incf calls)))) + (cj/f4-compile-only) + (should (= calls 0)))))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-f4-compile-only-unknown-project-falls-back-to-compile () + "Boundary: outside any project, falls back to interactive `compile'." + (let (received-fn) + (cl-letf (((symbol-function 'cj/--f4-project-root) (lambda () nil)) + ((symbol-function 'call-interactively) + (lambda (fn &rest _) (setq received-fn fn)))) + (cj/f4-compile-only) + (should (eq received-fn #'compile))))) + +(provide 'test-dev-fkeys--f4-compile-only) +;;; test-dev-fkeys--f4-compile-only.el ends here diff --git a/tests/test-dev-fkeys--f4-derive-clean-cmd.el b/tests/test-dev-fkeys--f4-derive-clean-cmd.el new file mode 100644 index 00000000..9052bf55 --- /dev/null +++ b/tests/test-dev-fkeys--f4-derive-clean-cmd.el @@ -0,0 +1,93 @@ +;;; test-dev-fkeys--f4-derive-clean-cmd.el --- Tests for cj/--f4-derive-clean-cmd -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the heuristic clean-command picker used by M-F4 +;; (Clean + Rebuild). Returns a shell command string based on the project +;; root's marker files, or nil when no marker matches. + +;;; Code: + +(require 'ert) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +(defmacro test-dev-fkeys-clean--with-project-dir (markers &rest body) + "Create a temp project dir with each filename in MARKERS as an empty file. +Bind the dir path to ROOT in BODY. Cleans up on exit." + (declare (indent 1)) + `(let ((root (make-temp-file "test-dev-fkeys-clean-" t))) + (unwind-protect + (progn + (dolist (marker ,markers) + (write-region "" nil (expand-file-name marker root))) + ,@body) + (delete-directory root t)))) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-derive-clean-cmd-go-mod-uses-go-clean () + "Normal: go.mod yields go clean ./..." + (test-dev-fkeys-clean--with-project-dir '("go.mod") + (should (string= (cj/--f4-derive-clean-cmd root) "go clean ./...")))) + +(ert-deftest test-dev-fkeys-derive-clean-cmd-cargo-toml-uses-cargo-clean () + "Normal: Cargo.toml yields cargo clean." + (test-dev-fkeys-clean--with-project-dir '("Cargo.toml") + (should (string= (cj/--f4-derive-clean-cmd root) "cargo clean")))) + +(ert-deftest test-dev-fkeys-derive-clean-cmd-eask-uses-eask-clean () + "Normal: Eask yields eask clean." + (test-dev-fkeys-clean--with-project-dir '("Eask") + (should (string= (cj/--f4-derive-clean-cmd root) "eask clean")))) + +(ert-deftest test-dev-fkeys-derive-clean-cmd-makefile-uses-make-clean () + "Normal: Makefile yields make clean." + (test-dev-fkeys-clean--with-project-dir '("Makefile") + (should (string= (cj/--f4-derive-clean-cmd root) "make clean")))) + +(ert-deftest test-dev-fkeys-derive-clean-cmd-cmakelists-uses-cmake-clean () + "Normal: CMakeLists.txt yields cmake build-dir clean target." + (test-dev-fkeys-clean--with-project-dir '("CMakeLists.txt") + (should (string= (cj/--f4-derive-clean-cmd root) + "cmake --build build --target clean")))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-derive-clean-cmd-go-wins-over-makefile () + "Boundary: go.mod + Makefile picks go clean (go.mod is checked first)." + (test-dev-fkeys-clean--with-project-dir '("go.mod" "Makefile") + (should (string= (cj/--f4-derive-clean-cmd root) "go clean ./...")))) + +(ert-deftest test-dev-fkeys-derive-clean-cmd-cargo-wins-over-makefile () + "Boundary: Cargo.toml + Makefile picks cargo clean." + (test-dev-fkeys-clean--with-project-dir '("Cargo.toml" "Makefile") + (should (string= (cj/--f4-derive-clean-cmd root) "cargo clean")))) + +(ert-deftest test-dev-fkeys-derive-clean-cmd-eask-wins-over-makefile () + "Boundary: Eask + Makefile picks eask clean." + (test-dev-fkeys-clean--with-project-dir '("Eask" "Makefile") + (should (string= (cj/--f4-derive-clean-cmd root) "eask clean")))) + +(ert-deftest test-dev-fkeys-derive-clean-cmd-no-markers-returns-nil () + "Boundary: directory with no recognized markers returns nil." + (test-dev-fkeys-clean--with-project-dir '() + (should (null (cj/--f4-derive-clean-cmd root))))) + +(ert-deftest test-dev-fkeys-derive-clean-cmd-irrelevant-files-returns-nil () + "Boundary: only unrelated files returns nil." + (test-dev-fkeys-clean--with-project-dir '("README.md" "LICENSE") + (should (null (cj/--f4-derive-clean-cmd root))))) + +;;; Error Cases + +(ert-deftest test-dev-fkeys-derive-clean-cmd-nil-root-returns-nil () + "Error: nil ROOT returns nil with no error." + (should (null (cj/--f4-derive-clean-cmd nil)))) + +(ert-deftest test-dev-fkeys-derive-clean-cmd-nonexistent-root-returns-nil () + "Error: a non-existent path returns nil. file-exists-p returns nil so no +marker matches." + (should (null (cj/--f4-derive-clean-cmd "/nonexistent/path/xyzzy")))) + +(provide 'test-dev-fkeys--f4-derive-clean-cmd) +;;; test-dev-fkeys--f4-derive-clean-cmd.el ends here diff --git a/tests/test-dev-fkeys--f4-dispatch.el b/tests/test-dev-fkeys--f4-dispatch.el new file mode 100644 index 00000000..774daebf --- /dev/null +++ b/tests/test-dev-fkeys--f4-dispatch.el @@ -0,0 +1,78 @@ +;;; test-dev-fkeys--f4-dispatch.el --- Tests for cj/--f4-dispatch -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the dispatch router. Each known action symbol routes to the +;; corresponding command. Unknown actions raise user-error. The action +;; handlers themselves are tested in their own files; this file only +;; verifies the routing layer. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-dispatch-compile-only-calls-projectile-compile () + "Normal: 'compile-only routes to projectile-compile-project." + (let ((calls 0)) + (cl-letf (((symbol-function 'projectile-compile-project) + (lambda (_arg) (cl-incf calls)))) + (cj/--f4-dispatch 'compile-only) + (should (= calls 1))))) + +(ert-deftest test-dev-fkeys-dispatch-run-only-calls-projectile-run () + "Normal: 'run-only routes to projectile-run-project." + (let ((calls 0)) + (cl-letf (((symbol-function 'projectile-run-project) + (lambda (_arg) (cl-incf calls)))) + (cj/--f4-dispatch 'run-only) + (should (= calls 1))))) + +(ert-deftest test-dev-fkeys-dispatch-compile-and-run-routes-to-impl () + "Normal: 'compile-and-run routes to cj/--f4-compile-and-run-impl." + (let ((calls 0)) + (cl-letf (((symbol-function 'cj/--f4-compile-and-run-impl) + (lambda () (cl-incf calls)))) + (cj/--f4-dispatch 'compile-and-run) + (should (= calls 1))))) + +(ert-deftest test-dev-fkeys-dispatch-clean-rebuild-routes-to-impl-with-root () + "Normal: 'clean-rebuild routes to cj/--f4-clean-rebuild-impl with the project root. + +Components integrated: +- `cj/--f4-dispatch' (unit under test) +- `cj/--f4-project-root' (MOCKED — returns a fake path) +- `cj/--f4-clean-rebuild-impl' (MOCKED — captures the ROOT it received)" + (let (received-root) + (cl-letf (((symbol-function 'cj/--f4-project-root) + (lambda () "/fake/project/")) + ((symbol-function 'cj/--f4-clean-rebuild-impl) + (lambda (root) (setq received-root root)))) + (cj/--f4-dispatch 'clean-rebuild) + (should (string= received-root "/fake/project/"))))) + +(ert-deftest test-dev-fkeys-dispatch-compile-plain-uses-call-interactively-on-compile () + "Normal: 'compile-plain invokes `compile' interactively (so the user is +prompted for a command). We check that `call-interactively' fires with +the symbol `compile'." + (let (received-fn) + (cl-letf (((symbol-function 'call-interactively) + (lambda (fn &rest _) (setq received-fn fn)))) + (cj/--f4-dispatch 'compile-plain) + (should (eq received-fn #'compile))))) + +;;; Error Cases + +(ert-deftest test-dev-fkeys-dispatch-unknown-action-signals-user-error () + "Error: dispatch on an unknown symbol raises user-error." + (should-error (cj/--f4-dispatch 'fictional-action) :type 'user-error)) + +(ert-deftest test-dev-fkeys-dispatch-nil-action-signals-user-error () + "Error: nil action raises user-error (defensive against bad menu data)." + (should-error (cj/--f4-dispatch nil) :type 'user-error)) + +(provide 'test-dev-fkeys--f4-dispatch) +;;; test-dev-fkeys--f4-dispatch.el ends here 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 diff --git a/tests/test-dev-fkeys--f4-project-root.el b/tests/test-dev-fkeys--f4-project-root.el new file mode 100644 index 00000000..617bb9e1 --- /dev/null +++ b/tests/test-dev-fkeys--f4-project-root.el @@ -0,0 +1,51 @@ +;;; test-dev-fkeys--f4-project-root.el --- Tests for cj/--f4-project-root -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the projectile-root wrapper. Projectile's +;; `projectile-project-root' raises an error in some configurations when +;; called outside a known project; this wrapper degrades to nil so the F4 +;; dispatcher can route to the 'unknown branch instead of crashing. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'dev-fkeys) + +;;; Normal Cases + +(ert-deftest test-dev-fkeys-f4-project-root-returns-projectile-value () + "Normal: returns whatever `projectile-project-root' returns when fbound." + (cl-letf (((symbol-function 'projectile-project-root) + (lambda () "/some/project/"))) + (should (string= (cj/--f4-project-root) "/some/project/")))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-f4-project-root-returns-nil-when-projectile-unbound () + "Boundary: returns nil when projectile is not loaded. + +Components integrated: +- `cj/--f4-project-root' (unit under test) +- `projectile-project-root' (MOCKED — fmakunbound to simulate absence)" + (let ((had-projectile (fboundp 'projectile-project-root)) + (saved (and (fboundp 'projectile-project-root) + (symbol-function 'projectile-project-root)))) + (unwind-protect + (progn + (when had-projectile (fmakunbound 'projectile-project-root)) + (should (null (cj/--f4-project-root)))) + (when had-projectile + (fset 'projectile-project-root saved))))) + +;;; Error Cases + +(ert-deftest test-dev-fkeys-f4-project-root-returns-nil-on-projectile-error () + "Error: returns nil when `projectile-project-root' signals an error." + (cl-letf (((symbol-function 'projectile-project-root) + (lambda () (error "Outside a known project")))) + (should (null (cj/--f4-project-root))))) + +(provide 'test-dev-fkeys--f4-project-root) +;;; test-dev-fkeys--f4-project-root.el ends here |
