aboutsummaryrefslogtreecommitdiff
path: root/tests/test-dirvish-config-get-project-root.el
blob: 91d322f9ff3d04e0a0bf58ff7095a71c9abc3146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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