aboutsummaryrefslogtreecommitdiff
path: root/tests/test-early-init-paths.el
blob: 2051da730a0d4caff6ff25b62c3433450f9f669c (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
;;; test-early-init-paths.el --- Tests for early-init path construction -*- lexical-binding: t; -*-

;;; Commentary:

;; Load early-init.el with package side effects stubbed so path construction can
;; be tested without touching real package archives or the network.

;;; Code:

(require 'cl-lib)
(require 'ert)
(require 'package)

(defvar cj/use-online-repos nil
  "Test binding for early-init online repository setup.")

(defconst test-early-init-paths--repo-root
  (file-name-directory
   (directory-file-name
    (file-name-directory (or load-file-name buffer-file-name))))
  "Repository root for early-init path tests.")

(defun test-early-init-paths--load-with-temp-home (home emacs-home)
  "Load early-init.el with HOME and EMACS-HOME as isolated roots."
  (let ((process-environment (copy-sequence process-environment))
        (user-emacs-directory (file-name-as-directory emacs-home))
        (package-user-dir (expand-file-name "elpa/" emacs-home))
        (package-archives nil)
        (package-archive-priorities nil)
        (debug-on-error nil)
        (debug-on-quit nil))
    (setq cj/use-online-repos nil)
    (setenv "HOME" home)
    (cl-letf (((symbol-function 'package-initialize) #'ignore)
              ((symbol-function 'package-installed-p) (lambda (_package) t))
              ((symbol-function 'package-refresh-contents) #'ignore)
              ((symbol-function 'package-install) #'ignore))
      (load (expand-file-name "early-init.el" test-early-init-paths--repo-root) nil t))
    (list :user-home-dir user-home-dir
          :elpa-mirror-location elpa-mirror-location
          :localrepo-location localrepo-location
          :gnu elpa-mirror-gnu-location
          :nongnu elpa-mirror-nongnu-location
          :melpa elpa-mirror-melpa-location
          :stable-melpa elpa-mirror-stable-melpa-location
          :package-archives package-archives
          :package-archive-priorities package-archive-priorities)))

(ert-deftest test-early-init-paths-normal-expanded-under-home ()
  "Normal: local mirror paths expand under HOME, not beside it."
  (let* ((home (make-temp-file "early-init-home-" t))
         (emacs-home (make-temp-file "early-init-emacs-" t))
         (_ (dolist (dir '(".localrepo"
                           ".elpa-mirrors/gnu"
                           ".elpa-mirrors/nongnu"
                           ".elpa-mirrors/melpa"
                           ".elpa-mirrors/stable-melpa"))
              (make-directory (expand-file-name dir emacs-home) t)))
         ;; Mirror directories intentionally live under HOME, while .localrepo
         ;; lives under user-emacs-directory.
         (_ (dolist (dir '(".elpa-mirrors/gnu"
                           ".elpa-mirrors/nongnu"
                           ".elpa-mirrors/melpa"
                           ".elpa-mirrors/stable-melpa"))
              (make-directory (expand-file-name dir home) t)))
         (result (test-early-init-paths--load-with-temp-home home emacs-home)))
    (should (string= (plist-get result :user-home-dir) home))
    (should (string= (plist-get result :elpa-mirror-location)
                     (expand-file-name ".elpa-mirrors/" home)))
    (should (string= (plist-get result :localrepo-location)
                     (expand-file-name ".localrepo/" emacs-home)))
    (should (string= (plist-get result :gnu)
                     (expand-file-name ".elpa-mirrors/gnu/" home)))
    (should-not (string= (plist-get result :elpa-mirror-location)
                         (concat home ".elpa-mirrors/")))))

(ert-deftest test-early-init-paths-normal-local-archives-use-expanded-paths ()
  "Normal: local package archives use expanded path constants."
  (let* ((home (make-temp-file "early-init-home-" t))
         (emacs-home (make-temp-file "early-init-emacs-" t)))
    (make-directory (expand-file-name ".localrepo/" emacs-home) t)
    (dolist (dir '(".elpa-mirrors/gnu"
                   ".elpa-mirrors/nongnu"
                   ".elpa-mirrors/melpa"
                   ".elpa-mirrors/stable-melpa"))
      (make-directory (expand-file-name dir home) t))
    (let* ((result (test-early-init-paths--load-with-temp-home home emacs-home))
           (archives (plist-get result :package-archives)))
      (should (equal (cdr (assoc "localrepo" archives))
                     (expand-file-name ".localrepo/" emacs-home)))
      (should (equal (cdr (assoc "gnu-local" archives))
                     (expand-file-name ".elpa-mirrors/gnu/" home)))
      (should (equal (cdr (assoc "nongnu-local" archives))
                     (expand-file-name ".elpa-mirrors/nongnu/" home)))
      (should (equal (cdr (assoc "melpa-local" archives))
                     (expand-file-name ".elpa-mirrors/melpa/" home)))
      (should (equal (cdr (assoc "melpa-stable-local" archives))
                     (expand-file-name ".elpa-mirrors/stable-melpa/" home))))))

(provide 'test-early-init-paths)
;;; test-early-init-paths.el ends here