summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/coverage-elisp.el74
-rw-r--r--tests/test-coverage-elisp--detect.el99
2 files changed, 173 insertions, 0 deletions
diff --git a/modules/coverage-elisp.el b/modules/coverage-elisp.el
new file mode 100644
index 00000000..63d89906
--- /dev/null
+++ b/modules/coverage-elisp.el
@@ -0,0 +1,74 @@
+;;; coverage-elisp.el --- Elisp coverage backend for coverage-core -*- lexical-binding: t; coding: utf-8; -*-
+;; author: Craig Jennings <c@cjennings.net>
+
+;;; Commentary:
+;; Registers the `elisp' coverage backend with `coverage-core'.
+;;
+;; Detection: a project root with a Makefile / Eask / Cask plus any
+;; .el files (either at root or under modules/). Loose on purpose —
+;; `.dir-locals.el' can pin the backend explicitly when the heuristic
+;; guesses wrong.
+;;
+;; :run invokes `make coverage' in a compilation buffer. On success,
+;; the callback is invoked with the LCOV path; on failure, the buffer
+;; stays visible for the user to inspect.
+;;
+;; :lcov-path resolves to `<project-root>/.coverage/lcov.info', which
+;; matches the path the Makefile's coverage target writes to.
+
+;;; Code:
+
+(require 'coverage-core)
+
+(use-package undercover
+ :defer t)
+
+(defconst cj/--coverage-elisp-lcov-relative-path
+ ".coverage/lcov.info"
+ "Project-relative path to the LCOV file produced by `make coverage'.")
+
+(defun cj/--coverage-elisp-project-root (&optional root)
+ "Return ROOT or fall back to projectile's root or `default-directory'."
+ (or root
+ (and (fboundp 'projectile-project-root)
+ (projectile-project-root))
+ default-directory))
+
+(defun cj/--coverage-elisp-detect (root)
+ "Return non-nil if ROOT looks like an Elisp project.
+The heuristic needs both (a) a Makefile, Eask, or Cask at ROOT and
+\(b) any .el files at ROOT or under modules/."
+ (and (or (file-exists-p (expand-file-name "Makefile" root))
+ (file-exists-p (expand-file-name "Eask" root))
+ (file-exists-p (expand-file-name "Cask" root)))
+ (or (file-expand-wildcards (expand-file-name "modules/*.el" root))
+ (file-expand-wildcards (expand-file-name "*.el" root)))))
+
+(defun cj/--coverage-elisp-lcov-path (&optional root)
+ "Return the absolute path to the LCOV file for ROOT."
+ (expand-file-name cj/--coverage-elisp-lcov-relative-path
+ (cj/--coverage-elisp-project-root root)))
+
+(defun cj/--coverage-elisp-run (callback)
+ "Run `make coverage' asynchronously.
+CALLBACK is invoked with the LCOV path when the build finishes
+successfully. On failure, no callback is invoked and the compilation
+buffer stays visible so the user can read the error."
+ (let* ((default-directory (cj/--coverage-elisp-project-root))
+ (buffer (compilation-start "make coverage" nil
+ (lambda (_mode) "*coverage-run*"))))
+ (with-current-buffer buffer
+ (add-hook 'compilation-finish-functions
+ (lambda (_buf status)
+ (when (string-match-p "^finished" status)
+ (funcall callback (cj/--coverage-elisp-lcov-path))))
+ nil t))))
+
+(cj/coverage-register-backend
+ (list :name 'elisp
+ :detect #'cj/--coverage-elisp-detect
+ :run #'cj/--coverage-elisp-run
+ :lcov-path #'cj/--coverage-elisp-lcov-path))
+
+(provide 'coverage-elisp)
+;;; coverage-elisp.el ends here
diff --git a/tests/test-coverage-elisp--detect.el b/tests/test-coverage-elisp--detect.el
new file mode 100644
index 00000000..1301495a
--- /dev/null
+++ b/tests/test-coverage-elisp--detect.el
@@ -0,0 +1,99 @@
+;;; test-coverage-elisp--detect.el --- Tests for cj/--coverage-elisp-detect -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; Unit tests for `cj/--coverage-elisp-detect', the heuristic that
+;; decides whether a given project root is an Elisp project.
+;;
+;; Heuristic: requires BOTH a Makefile/Eask/Cask AND some .el files
+;; (at root or under modules/).
+
+;;; Code:
+
+(require 'ert)
+
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(require 'coverage-elisp)
+
+(defun test-coverage-elisp-detect--make-project (spec)
+ "Create a temp directory matching SPEC.
+SPEC is a list of relative paths to create. Paths ending in `/' are
+directories; others are files. Returns the temp directory path."
+ (let ((root (make-temp-file "test-elisp-project-" t)))
+ (dolist (path spec)
+ (let ((full (expand-file-name path root)))
+ (if (string-suffix-p "/" path)
+ (make-directory full t)
+ (make-directory (file-name-directory full) t)
+ (write-region "" nil full))))
+ root))
+
+;;; Normal cases
+
+(ert-deftest test-coverage-elisp-detect-with-makefile-and-modules ()
+ "Normal: Makefile plus modules/foo.el is detected as elisp."
+ (let ((root (test-coverage-elisp-detect--make-project
+ '("Makefile" "modules/foo.el"))))
+ (unwind-protect
+ (should (cj/--coverage-elisp-detect root))
+ (delete-directory root t))))
+
+(ert-deftest test-coverage-elisp-detect-with-eask-and-root-el ()
+ "Normal: Eask plus root-level .el file is detected."
+ (let ((root (test-coverage-elisp-detect--make-project
+ '("Eask" "main.el"))))
+ (unwind-protect
+ (should (cj/--coverage-elisp-detect root))
+ (delete-directory root t))))
+
+(ert-deftest test-coverage-elisp-detect-with-cask-and-modules ()
+ "Normal: Cask plus modules/ directory is detected."
+ (let ((root (test-coverage-elisp-detect--make-project
+ '("Cask" "modules/bar.el"))))
+ (unwind-protect
+ (should (cj/--coverage-elisp-detect root))
+ (delete-directory root t))))
+
+;;; Boundary cases
+
+(ert-deftest test-coverage-elisp-detect-no-build-file ()
+ "Boundary: .el files without a Makefile/Eask/Cask is NOT detected."
+ (let ((root (test-coverage-elisp-detect--make-project
+ '("main.el" "other.el"))))
+ (unwind-protect
+ (should-not (cj/--coverage-elisp-detect root))
+ (delete-directory root t))))
+
+(ert-deftest test-coverage-elisp-detect-makefile-without-el ()
+ "Boundary: Makefile with no .el files is NOT detected."
+ (let ((root (test-coverage-elisp-detect--make-project
+ '("Makefile" "README.md"))))
+ (unwind-protect
+ (should-not (cj/--coverage-elisp-detect root))
+ (delete-directory root t))))
+
+(ert-deftest test-coverage-elisp-detect-empty-directory ()
+ "Boundary: an empty directory is not an elisp project."
+ (let ((root (make-temp-file "test-empty-" t)))
+ (unwind-protect
+ (should-not (cj/--coverage-elisp-detect root))
+ (delete-directory root t))))
+
+;;; Error cases
+
+(ert-deftest test-coverage-elisp-detect-nonexistent-root ()
+ "Error: a nonexistent ROOT returns nil, not an error."
+ (should-not (cj/--coverage-elisp-detect "/nonexistent/path/for-test-12345")))
+
+;;; Registry integration
+
+(ert-deftest test-coverage-elisp-registered-on-load ()
+ "Normal: loading coverage-elisp registers the `elisp' backend."
+ (let ((backend (cj/--coverage-backend-by-name 'elisp)))
+ (should backend)
+ (should (eq 'elisp (plist-get backend :name)))
+ (should (functionp (plist-get backend :detect)))
+ (should (functionp (plist-get backend :run)))
+ (should (functionp (plist-get backend :lcov-path)))))
+
+(provide 'test-coverage-elisp--detect)
+;;; test-coverage-elisp--detect.el ends here