diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-10 13:31:53 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-10 13:31:53 -0500 |
| commit | 1f907e40bb2124084e336d429cd60830df6aa369 (patch) | |
| tree | 67be9d17a9b54ac09ece7f3261316e15f1028a1e | |
| parent | 32e57be15a48aa24470e9f3bc6a206c7624bd6b0 (diff) | |
| download | dotemacs-1f907e40bb2124084e336d429cd60830df6aa369.tar.gz dotemacs-1f907e40bb2124084e336d429cd60830df6aa369.zip | |
test(dirvish): cover cj/get-project-root with stubbed APIs
`cj/get-project-root' was already in the right shape (pure dispatch over `projectile-project-root' and `project-current'), but had no tests. Add four covering the four meaningful states: projectile finds a root, projectile returns nil and project.el finds one, projectile errors and the wrapper falls through to project.el, and neither finds anything. The tests stub all three external symbols (`projectile-project-root', `project-current', `project-root') so they exercise the function without a project on disk.
| -rw-r--r-- | tests/test-dirvish-config-get-project-root.el | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/test-dirvish-config-get-project-root.el b/tests/test-dirvish-config-get-project-root.el new file mode 100644 index 00000000..91d322f9 --- /dev/null +++ b/tests/test-dirvish-config-get-project-root.el @@ -0,0 +1,71 @@ +;;; test-dirvish-config-get-project-root.el --- Tests for cj/get-project-root -*- lexical-binding: t; -*- + +;;; Commentary: +;; `cj/get-project-root' picks projectile when available, falls back to +;; project.el, and returns nil when neither finds a project. These tests +;; stub the two underlying APIs (`projectile-project-root', +;; `project-current', `project-root') so the function can be exercised +;; without an actual project on disk. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(require 'package) + +(setq package-user-dir (expand-file-name "elpa" user-emacs-directory)) +(package-initialize) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(add-to-list 'load-path (expand-file-name "elpa/dirvish-2.3.0/extensions" + user-emacs-directory)) +(require 'user-constants) +(require 'keybindings) +(require 'dirvish-config) + +;; Make sure both APIs are at least fbound so the `fboundp' guards in +;; `cj/get-project-root' fire correctly under cl-letf. +(unless (fboundp 'projectile-project-root) + (defalias 'projectile-project-root #'ignore)) +(unless (fboundp 'project-current) + (defalias 'project-current #'ignore)) +(unless (fboundp 'project-root) + (defalias 'project-root #'ignore)) + +(ert-deftest test-cj-get-project-root-projectile-wins () + "Normal: when projectile returns a root, that root is returned." + (cl-letf (((symbol-function 'projectile-project-root) + (lambda (&rest _) "/home/me/code/proj/")) + ((symbol-function 'project-current) + (lambda (&rest _) (error "project-current should not be reached")))) + (should (equal (cj/get-project-root) "/home/me/code/proj/")))) + +(ert-deftest test-cj-get-project-root-falls-back-to-project-el () + "Normal: when projectile returns nil, project.el's root wins." + (cl-letf (((symbol-function 'projectile-project-root) + (lambda (&rest _) nil)) + ((symbol-function 'project-current) + (lambda (&rest _) '(vc Git "/home/me/repo/"))) + ((symbol-function 'project-root) + (lambda (_) "/home/me/repo/"))) + (should (equal (cj/get-project-root) "/home/me/repo/")))) + +(ert-deftest test-cj-get-project-root-projectile-errors-then-fallback () + "Boundary: projectile signaling an error is suppressed; fallback runs." + (cl-letf (((symbol-function 'projectile-project-root) + (lambda (&rest _) (error "projectile not initialized"))) + ((symbol-function 'project-current) + (lambda (&rest _) '(vc Git "/home/me/repo/"))) + ((symbol-function 'project-root) + (lambda (_) "/home/me/repo/"))) + (should (equal (cj/get-project-root) "/home/me/repo/")))) + +(ert-deftest test-cj-get-project-root-no-project-returns-nil () + "Boundary: neither projectile nor project.el find anything." + (cl-letf (((symbol-function 'projectile-project-root) + (lambda (&rest _) nil)) + ((symbol-function 'project-current) + (lambda (&rest _) nil))) + (should-not (cj/get-project-root)))) + +(provide 'test-dirvish-config-get-project-root) +;;; test-dirvish-config-get-project-root.el ends here |
