-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathenvrc-tests.el
285 lines (225 loc) · 8.67 KB
/
envrc-tests.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
;;; envrc-tests.el --- Test suite for envrc -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Steve Purcell
;; Author: Steve Purcell <[email protected]>
;; Keywords:
;; 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:
;; Just a few basic regression tests
;;; Code:
(require 'envrc)
(require 'ert)
(require 'cl-lib)
(defgroup envrc-tests nil "Envrc.el tests." :group 'test)
(setq envrc-debug t)
(defun envrc-tests--exec (&rest args)
(should (apply 'call-process envrc-direnv-executable nil nil nil args)))
(defmacro envrc-tests--with-extra-global-env-var (key val &rest body)
"Temporarily set var KEY to VAL in the global `process-environment', while BODY is evaluated."
(declare (indent 2))
(let ((old-env (cl-gensym)))
`(let ((,old-env (default-value 'process-environment)))
(push (format "%s=%s" ,key ,val) (default-value 'process-environment))
(unwind-protect
(progn
,@body)
(setq-default process-environment ,old-env)))))
(defmacro envrc-tests--with-temp-directory (var &rest body)
"Create a temporary directory, bind it to VAR, make it current, and execute BODY."
(declare (indent 1))
(let ((passed (cl-gensym)))
`(let* ((default-directory (make-temp-file "envrc" t))
(envrc-debug t)
,passed
(,var default-directory))
(unwind-protect
(progn
(when (get-buffer "*envrc-debug*")
(kill-buffer "*envrc-debug*"))
,@body
(setq ,passed t))
(unless ,passed
(message "Debug output: %s"
(when (get-buffer "*envrc-debug*")
(with-current-buffer "*envrc-debug*" (buffer-string)))))))))
(ert-deftest envrc-no-op ()
"When there's no .envrc, do nothing."
(envrc-tests--with-temp-directory _
(with-temp-buffer
(envrc-mode 1)
(should (eq envrc--status 'none))
(should (not (local-variable-p 'process-environment))))))
(ert-deftest envrc-direnv-is-available ()
"Check the executable is executable!"
(should (executable-find envrc-direnv-executable)))
(ert-deftest envrc-no-op-unless-allowed ()
"When the .envrc isn't allowed, do nothing."
(envrc-tests--with-temp-directory _
(with-temp-file ".envrc"
(insert "export FOO=BAR"))
(with-temp-buffer
(envrc-mode 1)
(should (not (local-variable-p 'process-environment)))
(should (eq envrc--status 'error)))))
(ert-deftest envrc-setting-propagates-when-mode-enabled ()
"Pick up existing .envrc at mode startup."
(envrc-tests--with-temp-directory _
(with-temp-file ".envrc"
(insert "export FOO=BAR"))
(envrc-tests--exec "allow")
(with-temp-buffer
(envrc-mode 1)
(should (local-variable-p 'process-environment))
(should (equal "BAR" (getenv "FOO")))
(should (eq envrc--status 'on)))))
(ert-deftest envrc-setting-propagates-when-allowed ()
(envrc-tests--with-temp-directory _
(with-temp-file ".envrc"
(insert "export FOO=BAR"))
(with-temp-buffer
(envrc-mode 1)
(should (not (local-variable-p 'process-environment)))
(envrc-allow)
(should (local-variable-p 'process-environment))
(should (equal "BAR" (getenv "FOO")))
(should (eq envrc--status 'on)))))
(ert-deftest envrc-setting-removed-when-denied ()
(envrc-tests--with-temp-directory _
(with-temp-file ".envrc"
(insert "export FOO=BAR"))
(envrc-tests--exec "allow")
(with-temp-buffer
(envrc-mode 1)
(should (local-variable-p 'process-environment))
(should (equal "BAR" (getenv "FOO")))
(should (eq envrc--status 'on))
(envrc-deny)
(should (not (local-variable-p 'process-environment)))
(should (eq envrc--status 'error)))))
(ert-deftest envrc-reload-existing-buffer ()
(envrc-tests--with-temp-directory _
(with-temp-file ".envrc"
(insert "export FOO=BAR"))
(envrc-tests--exec "allow")
(with-temp-buffer
(envrc-mode 1)
(should (equal "BAR" (getenv "FOO")))
(with-temp-file ".envrc"
(insert "export FOO=BAZ"))
(envrc-tests--exec "allow")
(envrc-reload)
(should (equal "BAZ" (getenv "FOO"))))))
(ert-deftest envrc-masks-global-var-when-overridden ()
(envrc-tests--with-extra-global-env-var "FOO" "BANANA"
(envrc-tests--with-temp-directory _
(with-temp-file ".envrc"
(insert "export FOO=BAR"))
(envrc-tests--exec "allow")
(with-temp-buffer
(should (equal "BANANA" (getenv "FOO")))
(envrc-mode 1)
(should (equal "BAR" (getenv "FOO")))))))
(ert-deftest envrc-state-shared-between-buffers-in-dir ()
(envrc-tests--with-temp-directory _
(with-temp-file ".envrc"
(insert "export FOO=BAR"))
(envrc-tests--exec "allow")
(with-temp-buffer
(envrc-mode 1)
(should (local-variable-p 'process-environment))
(should (equal "BAR" (getenv "FOO")))
(envrc-tests--exec "deny")
(with-temp-buffer
(envrc-mode 1)
(should (local-variable-p 'process-environment))
(should (equal "BAR" (getenv "FOO")))
(envrc-reload)
(should (eq envrc--status 'error)))
(should (eq envrc--status 'error))
(should (not (local-variable-p 'process-environment))))))
(ert-deftest envrc-remove-variable ()
(envrc-tests--with-temp-directory _
(with-temp-file ".envrc"
(insert "export FOO=BAR"))
(envrc-tests--exec "allow")
(with-temp-buffer
(envrc-mode 1)
(should (equal "BAR" (getenv "FOO")))
(with-temp-file ".envrc"
(insert ""))
(envrc-allow)
(should (equal nil (getenv "FOO"))))))
(ert-deftest envrc-cache-is-refreshed-if-global-env-changes ()
(envrc-tests--with-temp-directory _
(with-temp-file ".envrc"
(insert "export FOO=BAR"))
(envrc-tests--exec "allow")
(with-temp-buffer
(envrc-mode 1)
(should (equal "BAR" (getenv "FOO")))
(envrc-tests--with-extra-global-env-var (symbol-name (cl-gensym)) "blah"
(with-temp-file ".envrc"
(insert "export FOO=BAZ"))
(envrc-tests--exec "allow")
(with-temp-buffer
;; We expect a cache miss, and therefore a refresh
(envrc--debug "buffer is %S" (current-buffer))
(envrc-mode 1)
(should (local-variable-p 'process-environment))
(should (equal "BAZ" (getenv "FOO"))))
;; TODO?
;; (should (local-variable-p 'process-environment))
;; (should (equal "BAZ" (getenv "FOO")))
))))
;; ;; Now requires a per-user config setting for direnv,
;; ;; so tests will fail by default.
;; (ert-deftest envrc-fall-back-to-env-files ()
;; (envrc-tests--with-temp-directory _
;; (with-temp-file ".env"
;; (insert "FOO=BAR"))
;; (envrc-tests--exec "allow")
;; (with-temp-buffer
;; (envrc-mode 1)
;; (should (equal "BAR" (getenv "FOO"))))))
(require 'eshell)
(ert-deftest envrc-eshell-updates-environment-when-changing-directory ()
(let ((current-dir default-directory))
(eshell)
(envrc-tests--with-temp-directory envrc-dir
(with-temp-file ".envrc"
(insert "export FOO=BAR"))
(envrc-tests--exec "allow")
;; envrc mode is not activated
(eshell/cd envrc-dir)
(should (equal nil (getenv "FOO")))
;; envrc mode is activated with option set to not update env on directory change
(eshell/cd current-dir)
(let ((envrc-update-on-eshell-directory-change nil))
(envrc-mode 1))
(eshell/cd envrc-dir)
(should (equal nil (getenv "FOO")))
;; envrc mode is activated and updates environment with default options
(eshell/cd current-dir)
(envrc-mode -1)
(envrc-mode 1)
(eshell/cd envrc-dir)
(should (equal "BAR" (getenv "FOO")))
;; environment is cleared when exiting directory
(eshell/cd current-dir)
(should (equal nil (getenv "FOO")))
;; environment is cleared when envrc-mode is disabled
(eshell/cd envrc-dir)
(envrc-mode -1)
(should (equal nil (getenv "FOO"))))))
;; TODO:
;; - Setting exec-path and eshell-path-env
(provide 'envrc-tests)
;;; envrc-tests.el ends here