From 1d9d252e8b9e1385337cd0af087a7007f8e62da8 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 3 May 2026 17:20:54 -0500 Subject: feat(dev-fkeys): add F6 test runner menu (Phase 2a) I extended `dev-fkeys.el` with the F6 dispatcher half of the spec. F6 prompts via `completing-read` between two candidates: "All tests" delegates to `projectile-test-project`, and "Current file's tests" detects the buffer's language by extension, derives the runner command, and pipes through `compile' from the projectile root. C-F6 is the fast path straight to "Current file's tests". Per-language coverage: - Elisp source files map to `make test-name TEST=^test--`. Elisp test files run with `make test-file FILE=` so a per-helper file like `test-foo--bar.el' runs only its own tests. - Python source files map to `pytest tests/test_.py'. Python test files run with `pytest '. - Go runs the package containing the file: `go test ./'. Source and test files use the same command since Go test scope is per-package. Limit: this runs every `_test.go' in the package, not just the buffer's file. Phase 2b can refine via test-name discovery. - TypeScript and JavaScript are detected but punted for v1. The runner-command builder returns nil and the orchestrator signals a user-error rather than guessing. The F6 binding moved from the Phase 1 stopgap (`projectile-test-project') to `cj/f6-test-runner'. C-F6 is newly bound to `cj/f6-current-file-tests'. M-F6 stays unbound, reserved for Phase 2b's "Run a test..." menu entry. TDD: 68 new tests across 7 files. Production code split into small testable internals (`cj/--f6-language-detect', `cj/--f6-buffer-is-test-file-p', `cj/--f6-source-stem', `cj/--f6-test-runner-cmd-for', `cj/--f6-current-file-tests-impl') plus two thin interactive wrappers. Smoke tests confirm bindings register on load. I also updated the module commentary with the Phase 2b plan, the capture-then-filter approach for tree-sitter discovery, and a pointer to Emacs bug #79687. The bug is the predicate-syntax mismatch that breaks `:match' / `:equal' / `:pred' queries on Emacs 30.2 with libtree-sitter 0.26. The fix lives on Emacs master (commit b0143530), targets Emacs 31, and has not been backported to the emacs-30 branch as of today. Phase 2b will use queries without predicates and filter results in Elisp, sidestepping the issue. Mike Olson's `treesit-predicate-rewrite.el' applies the same idea to font-lock if you want it before Phase 2b lands. --- .../test-dev-fkeys--f6-current-file-tests-impl.el | 125 +++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tests/test-dev-fkeys--f6-current-file-tests-impl.el (limited to 'tests/test-dev-fkeys--f6-current-file-tests-impl.el') diff --git a/tests/test-dev-fkeys--f6-current-file-tests-impl.el b/tests/test-dev-fkeys--f6-current-file-tests-impl.el new file mode 100644 index 00000000..ecc7a4f4 --- /dev/null +++ b/tests/test-dev-fkeys--f6-current-file-tests-impl.el @@ -0,0 +1,125 @@ +;;; test-dev-fkeys--f6-current-file-tests-impl.el --- Tests for cj/--f6-current-file-tests-impl -*- lexical-binding: t -*- + +;;; Commentary: +;; Tests for the "Current file's tests" orchestrator. Takes (FILE +;; PROJECT-ROOT), composes the per-language test-runner command, and runs +;; it through `compile' with `default-directory' bound to PROJECT-ROOT. +;; Errors when no file, no project root, or no test runner is available +;; for the file's language. + +;;; 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-f6-current-file-tests-impl-elisp-source () + "Normal: an elisp source file runs `make test-name TEST=^test--'. + +Components integrated: +- `cj/--f6-current-file-tests-impl' (unit under test) +- `cj/--f6-language-detect' (real) +- `cj/--f6-source-stem' (real) +- `cj/--f6-buffer-is-test-file-p' (real) +- `cj/--f6-test-runner-cmd-for' (real) +- `compile' (MOCKED — captures cmd and default-directory)" + (let (seen-cmd seen-dir) + (cl-letf (((symbol-function 'compile) + (lambda (cmd) (setq seen-cmd cmd + seen-dir default-directory)))) + (cj/--f6-current-file-tests-impl + "/home/u/proj/modules/foo.el" + "/home/u/proj/") + (should (string= seen-cmd "make test-name TEST=^test-foo-")) + (should (string= (file-name-as-directory seen-dir) + (file-name-as-directory "/home/u/proj/")))))) + +(ert-deftest test-dev-fkeys-f6-current-file-tests-impl-elisp-test-file () + "Normal: an elisp test file runs `make test-file FILE='." + (let (seen-cmd) + (cl-letf (((symbol-function 'compile) + (lambda (cmd) (setq seen-cmd cmd)))) + (cj/--f6-current-file-tests-impl + "/home/u/proj/tests/test-foo.el" + "/home/u/proj/") + (should (string= seen-cmd "make test-file FILE=tests/test-foo.el"))))) + +(ert-deftest test-dev-fkeys-f6-current-file-tests-impl-python-source () + "Normal: a Python source file maps to `pytest tests/test_.py'." + (let (seen-cmd) + (cl-letf (((symbol-function 'compile) + (lambda (cmd) (setq seen-cmd cmd)))) + (cj/--f6-current-file-tests-impl + "/home/u/proj/pkg/foo.py" + "/home/u/proj/") + (should (string= seen-cmd "pytest tests/test_foo.py"))))) + +(ert-deftest test-dev-fkeys-f6-current-file-tests-impl-go-source () + "Normal: a Go source file runs the package via `go test ./'." + (let (seen-cmd) + (cl-letf (((symbol-function 'compile) + (lambda (cmd) (setq seen-cmd cmd)))) + (cj/--f6-current-file-tests-impl + "/home/u/proj/pkg/foo.go" + "/home/u/proj/") + (should (string= seen-cmd "go test ./pkg"))))) + +;;; Boundary Cases + +(ert-deftest test-dev-fkeys-f6-current-file-tests-impl-go-source-at-root () + "Boundary: a Go source file at project root runs `go test ./'." + (let (seen-cmd) + (cl-letf (((symbol-function 'compile) + (lambda (cmd) (setq seen-cmd cmd)))) + (cj/--f6-current-file-tests-impl + "/home/u/proj/main.go" + "/home/u/proj/") + (should (string= seen-cmd "go test ./"))))) + +(ert-deftest test-dev-fkeys-f6-current-file-tests-impl-elisp-double-dash-test () + "Boundary: a per-helper elisp test file runs `make test-file FILE=...' so +just that file's tests run, not the whole module's prefix." + (let (seen-cmd) + (cl-letf (((symbol-function 'compile) + (lambda (cmd) (setq seen-cmd cmd)))) + (cj/--f6-current-file-tests-impl + "/home/u/proj/tests/test-foo--bar.el" + "/home/u/proj/") + (should (string= seen-cmd "make test-file FILE=tests/test-foo--bar.el"))))) + +;;; Error Cases + +(ert-deftest test-dev-fkeys-f6-current-file-tests-impl-nil-file-errors () + "Error: nil FILE signals a user-error." + (cl-letf (((symbol-function 'compile) (lambda (_cmd) nil))) + (should-error (cj/--f6-current-file-tests-impl nil "/home/u/proj/") + :type 'user-error))) + +(ert-deftest test-dev-fkeys-f6-current-file-tests-impl-nil-root-errors () + "Error: nil PROJECT-ROOT signals a user-error." + (cl-letf (((symbol-function 'compile) (lambda (_cmd) nil))) + (should-error (cj/--f6-current-file-tests-impl + "/home/u/proj/modules/foo.el" nil) + :type 'user-error))) + +(ert-deftest test-dev-fkeys-f6-current-file-tests-impl-unsupported-language-errors () + "Error: a file with no language-specific runner signals a user-error +naming the language." + (cl-letf (((symbol-function 'compile) (lambda (_cmd) nil))) + (should-error (cj/--f6-current-file-tests-impl + "/home/u/proj/src/foo.test.ts" "/home/u/proj/") + :type 'user-error))) + +(ert-deftest test-dev-fkeys-f6-current-file-tests-impl-unknown-language-errors () + "Error: an unknown extension signals a user-error rather than running +something the user didn't ask for." + (cl-letf (((symbol-function 'compile) (lambda (_cmd) nil))) + (should-error (cj/--f6-current-file-tests-impl + "/home/u/proj/Makefile" "/home/u/proj/") + :type 'user-error))) + +(provide 'test-dev-fkeys--f6-current-file-tests-impl) +;;; test-dev-fkeys--f6-current-file-tests-impl.el ends here -- cgit v1.2.3