forked from progfolio/elpaca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elpaca-test.el
201 lines (184 loc) · 9.7 KB
/
elpaca-test.el
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
;;; elpaca-test.el --- Testing macro for Elpaca -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Nicholas Vollmer
;; Author: Nicholas Vollmer
;; Keywords: convenience
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; `elpaca-test' is a declarative macro aimed at making it easy to create and share bug reproduction enviornments.
;; Example usages:
;;
;; Installs Elpaca in a temporary environment. Launches an Emacs instance with doc/init.el
;; (elpaca-test)
;;
;; As above, but installs Elpaca and checks out the "feat/example" branch first.
;; (elpaca-test :ref "feat/example")
;;
;; As above, but copies over user's init.el
;; (elpaca-test :ref "feat/example" :init (:file "~/.emacs.d/init.el"))
;;
;; As above, with forms replacing everything after the installer in doc/init.el
;; (elpaca-test :ref "feat/example" :init (elpaca one) (elpaca two))
;; @TODO: Once elpaca-wait installed, re-implement to use yodel?
;;; Code:
(require 'elpaca)
(defvar elpaca-test--keywords '(:before :dir :early-init :init :keep :name :ref))
(eval-and-compile
(defun elpaca-test--args (body)
"Return arg plist from BODY."
(cl-loop with (acc results)
for form in (reverse body)
for flushp = (keywordp form)
when (and flushp (not (memq form elpaca-test--keywords)))
do (user-error "Unrecognized keyword %s" form)
do (if (not flushp)
(push form acc)
(push acc results)
(setq acc nil)
(push form results))
finally do (when acc (user-error "Missing first keyword"))
finally return results))
(defun elpaca-test--delete-files (files)
"Delete FILES."
(dolist (path files)
(when-let ((expanded (expand-file-name path)))
(if (file-directory-p expanded) (delete-directory expanded 'recursive)
(delete-file expanded))))))
;;;###autoload
(defmacro elpaca-test (&rest body)
"Test Elpaca in a clean environment.
BODY is psuedo plist which allows multiple values for certain keys.
The following keys are recognized:
:name description of the test
:ref git ref to check out or `local' to use local copy in current repo state
:dir `user-emacs-directory' name expanded in `temporary-file-irectory'.
Only relative paths are accepted.
:init `user', (:file \"path/to/init.el\") or forms...
Content of the init.el file.
`user' is shorthand for `user-emacs-diretory'/init.el.
:early-init `user', (:file \"path/to/early-init.el\") or forms...
Content of the early-init.el file.
`user' is shorthand for `user-emacs-diretory'/early-init.el.
:keep t or a list containing any of the following symbols:
`builds'
`cache'
`repos'
If t, the directory is left in the state it was in as of last run.
If a symbol list, those specific `elpaca-directory' folders are not removed."
(declare (indent 0))
(let* ((args (elpaca-test--args body))
(keep (car (plist-get args :keep)))
(keep-all-p (eq keep t))
(keep-builds-p (or keep-all-p (member 'builds keep)))
(keep-cache-p (or keep-all-p (member 'cache keep)))
(keep-repos-p (or keep-all-p (member 'repos keep)))
(init (plist-get args :init))
(init-filep (or (eq (car-safe (car-safe init)) :file)
(eq (car-safe init) 'user)))
(early (plist-get args :early-init))
(early-filep (or (eq (car-safe (car-safe early)) :file)
(eq (car-safe early) 'user)))
(ref (car (plist-get args :ref)))
(localp (eq ref 'local))
(test-builds "./elpaca/builds/")
(test-repos "./elpaca/repos/")
(test-cache "./elpaca/cache/")
(dir (car (plist-get args :dir))))
(when-let ((dir)
(expanded (file-name-as-directory (expand-file-name dir))))
(when (equal expanded (file-name-as-directory dir))
(user-error ":dir must be relative path"))
(when (equal expanded (expand-file-name user-emacs-directory))
(user-error ":dir cannot be user-emacs-directory")))
(when-let (((listp keep))
(err (cl-remove-if (lambda (el) (memq el '(builds cache repos))) keep)))
(user-error "Unknown :keep value %S" err))
`(let ((default-directory ,(if dir `(expand-file-name ,dir temporary-file-directory)
'(make-temp-file "elpaca." 'directory))))
(unless (file-exists-p default-directory) (make-directory default-directory 'parents))
(elpaca-test--delete-files ',`(,@(unless keep-builds-p (list test-builds))
,@(unless keep-repos-p (list test-repos))
,@(unless keep-cache-p (list test-cache))
"./early-init.el"
"./init.el"
,@(and localp '("./elpaca/repos/elpaca"
"./elpaca/builds/elpaca"))))
,@(when localp
'((dolist (path '("./repos/elpaca" "./builds/elpaca" "./cache/"))
(when-let ((local (expand-file-name path elpaca-directory))
((file-exists-p local)))
(copy-directory local (expand-file-name path (expand-file-name "./elpaca/"))
nil 'parents 'copy-contents)))))
,@(when early
`((with-temp-buffer
,@(if early-filep
`((insert-file-contents
(expand-file-name ,(if (eq (car-safe early) 'user)
(locate-user-emacs-file "./early-init.el")
(cadar early)))))
`((insert ,(pp-to-string `(progn ,@early)))))
(write-file (expand-file-name "./early-init.el")))))
(with-temp-buffer
,@`(,(cond
(init-filep `(insert-file-contents
(expand-file-name ,(if (eq (car-safe init) 'user)
(locate-user-emacs-file "./init.el")
(cadar init)))))
(localp '(insert-file-contents
(expand-file-name "./repos/elpaca/doc/init.el" elpaca-directory)))
;;@TODO :repo arg which alllows to specify different URL.
;; Would also need to search/replace :repo in demo init.
(t `(let ((url ,(format "https://raw.githubusercontent.com/progfolio/elpaca/%s/doc/init.el" (or ref "master"))))
(defvar url-http-end-of-headers)
(defvar url-http-response-status)
(insert
(with-current-buffer (url-retrieve-synchronously url nil 'inhibit-cookies)
(unless (equal url-http-response-status 200)
(error "Unable to download %S %S" url
url-http-response-status))
(delete-region (point-min) url-http-end-of-headers)
(string-trim (buffer-string))))))))
,@(unless (or (null ref) init-filep localp)
`((goto-char (point-min))
(re-search-forward ":ref nil")
(replace-match (format ":ref %S" ,ref))))
,@(when (and init (not init-filep))
`((goto-char (point-max))
(forward-line -1)
(re-search-backward "^;; Install" nil 'noerror)
(delete-region (point) (point-max))
(insert ,(pp-to-string `(progn ,@init)))))
(elisp-enable-lexical-binding)
(write-file (expand-file-name "./init.el")))
,@(plist-get args :before)
(make-process
:name "elpaca-test"
:command (list ,(elpaca--emacs-path)
"--debug-init"
,@(if (< emacs-major-version 29)
`("-q" ; Approximate startup.el sequence
"--eval" "(setq debug-on-error t)"
"--eval" (format "(setq user-emacs-directory %S)" default-directory)
,@(when early '("-l" "./early-init.el"))
"--eval" "(run-hooks 'before-init-hook)"
"-l" "./init.el"
"--eval" "(setq after-init-time (current-time))"
"--eval" "(run-hooks 'after-init-hook)"
"--eval" "(run-hooks 'emacs-startup-hook)")
'((format "--init-directory=%s" default-directory)))))
(message "testing Elpaca @ %s in %s"
,@(or (and ref (not localp) (list ref))
`((let ((default-directory (expand-file-name "repos/elpaca/" elpaca-directory)))
(concat (and ,localp (or (ignore-errors (elpaca-process-output "git" "diff" "--quiet")) "DIRTY "))
(string-trim (elpaca-process-output "git" "log" "--pretty=%h %D" "-1"))))))
default-directory))))
(provide 'elpaca-test)
;;; elpaca-test.el ends here