-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconner-tests.el
More file actions
264 lines (229 loc) · 13.2 KB
/
Copy pathconner-tests.el
File metadata and controls
264 lines (229 loc) · 13.2 KB
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
(defmacro with-temp-env (&rest forms)
`(let* ((conner-file-name (concat conner-file-name (format "-test.%s" (% (random) 100000000))))
(conner-root-dir "/tmp")
(conner-env-file (concat conner-env-file (format "-test.%s" (% (random) 100000000))))
(conner-file-path (file-name-concat conner-root-dir conner-file-name))
(conner-env-path (file-name-concat conner-root-dir conner-env-file))
(user-emacs-directory conner-root-dir)
(conner-local-file-path (conner--construct-local-file-path "/tmp")))
(unwind-protect
(progn
,@forms)
(delete-file conner-file-path)
(delete-file conner-env-path)
(delete-directory "/tmp/conner" t))))
(defun pollute-env-file ()
(with-temp-buffer
(insert "VAR1=someval")
(write-file conner-env-path)))
(defun get-conner-contents ()
(conner--read-commands conner-file-path))
(defun get-local-conner-contents ()
(conner--read-commands conner-local-file-path))
(defun fake-command-runner (plist root-dir)
(should (equal (plist-get plist :name) "Run me"))
(should (equal (plist-get plist :command) "now"))
(should (equal (plist-get plist :type) "test type"))
(should (equal root-dir conner-root-dir)))
(defun fake-runner-check-env (plist &rest _)
(if (equal (plist-get plist :command) "should exist")
(should (equal (getenv "VAR1") "someval"))
(should (equal (getenv "VAR1") nil))))
(defun fake-runner-check-default-dir (plist root-dir)
(should (equal default-directory
(file-name-concat root-dir (plist-get plist :workdir)))))
(defun fake-func-check-display-buffer-alist (&optional is-silent)
(let ((are-equal (equal (car display-buffer-alist) '("\\*conner-.*"
(display-buffer-no-window)
(allow-no-window . t)))))
(if is-silent
(should are-equal)
(should-not are-equal))))
(ert-deftest conner-test-add-command ()
(with-temp-env
(conner-add-command conner-root-dir '(:name "New command" :command "echo \"test\"" :type "compile"))
(should (equal (get-conner-contents)
'((:name "New command" :command "echo \"test\"" :type "compile"))))))
(ert-deftest conner-test-add-existing-command ()
(with-temp-env
(conner-add-command conner-root-dir '(:name "Test command" :command "echo \"test\"" :type "compile"))
(should-error (conner-add-command conner-root-dir '(:name "Test command" :command "echo \"test\"" :type "compile")))))
(ert-deftest conner-test-add-invalid-command ()
(with-temp-env
(should-error
(conner-add-command conner-root-dir '(:should "fail" :because "it" :lacks "name" :key)))))
(ert-deftest conner-test-delete-command ()
(with-temp-env
(conner-add-command conner-root-dir '(:name "Delete me" :command "please" :type "compile"))
(conner-delete-command conner-root-dir "Delete me")
(should (equal (get-conner-contents) nil))
(conner-add-command conner-root-dir '(:name "Don't delete" :command "me" :type "compile"))
(conner-add-command conner-root-dir '(:name "Do delete" :command "me" :type "compile"))
(conner-delete-command conner-root-dir "Do delete")
(should (equal (get-conner-contents) '((:name "Don't delete" :command "me" :type "compile"))))))
(ert-deftest conner-test-update-command ()
(with-temp-env
(conner-add-command conner-root-dir '(:name "Tpyo in nmae" :command "tpyo" :type "compile"))
(conner-update-command conner-root-dir "Tpyo in nmae" '(:name "Typo in name" :command "typst" :type "compile"))
(should (equal (get-conner-contents) '((:name "Typo in name" :command "typst" :type "compile"))))))
(ert-deftest conner-test-run-command ()
(with-temp-env
(let ((conner-command-types-alist `(("test type" ,#'fake-command-runner))))
(conner-add-command conner-root-dir '(:name "Run me" :command "now" :type "test type"))
(conner-run-command conner-root-dir "Run me"))))
(ert-deftest conner-test-construct-file-path ()
(with-temp-env
(should (equal (conner--construct-file-path conner-root-dir)
(file-name-concat conner-root-dir conner-file-name)))))
(ert-deftest conner-test-construct-local-file-path ()
(with-temp-env
(should (equal (conner--construct-local-file-path conner-root-dir)
(expand-file-name (file-name-concat user-emacs-directory "conner/!tmp.#conner#"))))))
(ert-deftest conner-test-add-local-command ()
(with-temp-env
(let ((current-prefix-arg 4))
(conner-add-command conner-root-dir '(:name "New command" :command "echo \"test\"" :type "compile"))
(should (file-exists-p conner-local-file-path))
(should-not (file-exists-p conner-file-path)))))
(ert-deftest conner-test-delete-local-command ()
(with-temp-env
(let ((current-prefix-arg 4))
(conner-add-command conner-root-dir '(:name "New command" :command "echo \"test\"" :type "compile"))
(conner-delete-command conner-root-dir "New command")
(should (file-exists-p conner-local-file-path))
(should-not (file-exists-p conner-file-path))
(should (equal (get-local-conner-contents) nil)))))
(ert-deftest conner-test-update-local-command ()
(with-temp-env
(let ((current-prefix-arg 4))
(conner-update-command conner-root-dir "Old command" '(:name "New command" :command "echo \"test\"" :type "compile"))
(should (file-exists-p conner-local-file-path))
(should-not (file-exists-p conner-file-path))
(should (equal (get-local-conner-contents)
'((:name "New command" :command "echo \"test\"" :type "compile")))))))
(ert-deftest conner-test-run-local-command ()
(with-temp-env
(let ((current-prefix-arg 4)
(conner-command-types-alist `(("test type" ,#'fake-command-runner))))
(conner-add-command conner-root-dir '(:command "now" :name "Run me" :type "test type"))
(conner-run-command conner-root-dir "Run me"))))
(ert-deftest conner-test-read-env-file ()
(with-temp-env
(pollute-env-file)
(let ((conner-command-types-alist `(("test type" ,#'fake-runner-check-env))))
(conner-add-command conner-root-dir '(:name "Run me" :command "should exist" :type "test type"))
(conner-run-command conner-root-dir "Run me"))))
(ert-deftest conner-test-dont-read-env-file ()
(with-temp-env
(pollute-env-file)
(let ((conner-command-types-alist `(("test type" ,#'fake-runner-check-env)))
(conner-read-env-file nil))
(conner-add-command conner-root-dir '(:name "Run me" :command "echo $VAR1" :type "test type"))
(conner-run-command conner-root-dir "Run me"))))
(ert-deftest conner-test-add-local-command-with-default-behavior ()
(with-temp-env
(let ((current-prefix-arg 4)
(conner-default-file-behavior 'local))
(conner-add-command conner-root-dir '(:name "New command" :command "echo \"test\"" :type "compile"))
(should-not (file-exists-p conner-local-file-path))
(should (file-exists-p conner-file-path)))))
(ert-deftest conner-test-delete-local-command-with-default-behavior ()
(with-temp-env
(let ((current-prefix-arg 4)
(conner-default-file-behavior 'local))
(conner-add-command conner-root-dir '(:name "New command" :command "echo \"test\"" :type "compile"))
(conner-delete-command conner-root-dir "New command")
(should-not (file-exists-p conner-local-file-path))
(should (file-exists-p conner-file-path))
(should (equal (get-conner-contents) nil)))))
(ert-deftest conner-test-update-local-command-with-default-behavior ()
(with-temp-env
(let ((current-prefix-arg 4)
(conner-default-file-behavior 'local))
(conner-update-command conner-root-dir "Old command" '(:name "New command" :command "echo \"test\"" :type "compile"))
(should-not (file-exists-p conner-local-file-path))
(should (file-exists-p conner-file-path))
(should (equal (get-conner-contents) '((:name "New command" :command "echo \"test\"" :type "compile")))))))
(ert-deftest conner-test-run-local-command-with-default-behavior ()
(with-temp-env
(let ((current-prefix-arg 4)
(conner-default-file-behavior 'local)
(conner-command-types-alist `(("test type" ,#'fake-command-runner))))
(conner-add-command conner-root-dir '(:name "Run me" :command "now" :type "test type"))
(conner-run-command conner-root-dir "Run me"))))
(ert-deftest conner-test-read-command-env-var ()
(with-temp-env
(let ((conner-command-types-alist `(("test type" ,#'fake-runner-check-env))))
(conner-add-command conner-root-dir '(:name "Run me" :command "should exist" :type "test type" :environment ("VAR1: someval")))
(conner-run-command conner-root-dir "Run me"))))
(ert-deftest conner-test-change-workdir ()
(with-temp-env
(let ((conner-command-types-alist `(("workdirchange" ,#'fake-runner-check-default-dir))))
(conner-add-command conner-root-dir '(:name "Run me" :workdir "/relative/to/rootdir" :command "now" :type "workdirchange"))
(conner-run-command conner-root-dir "Run me"))))
(ert-deftest conner-test-expand-command ()
(with-temp-env
(let ((default-directory "/test/path"))
(should (equal (conner-expand-command "Expand: %d. This does not: %%d. Escape percent: %%")
"Expand: /test/path. This does not: %d. Escape percent: %"))
(should (equal (conner-expand-command '("Supports" "Lists!!" "With %d dirs!"))
"Supports && Lists!! && With /test/path dirs!"))
(should-error (conner-expand-command "Non-existent specifier %z")))))
(ert-deftest conner-test-validate-command ()
(with-temp-env
(should-error (conner--validate-command-plist '("Not" "a" "plist")))
(should-error (conner--validate-command-plist '(:name notstring :command "test" :type "compile")))
(should-error (conner--validate-command-plist '(:name "name" :type "compile")))
(should-error (conner--validate-command-plist '(:name "name" :command "test" :type notstring)))
(should-error (conner--validate-command-plist '(:name "name" :command "test" :type "invalidtype")))
(should-error (conner--validate-command-plist '(:name "name" :command "test" :type "compile" :workdir notstring)))
(should-error (conner--validate-command-plist '(:name "name" :command "test" :type "compile" :environment notlist)))
(should-error (conner--validate-command-plist '(:name "name" :command "test" :type "compile" :hook "not a function!")))
(should-error (conner--validate-command-plist '(:name "name" :command "test" :type "compile" :silent "not a boolean")))
(should (eq (conner--validate-command-plist '(:name "name" :command symbol :type "compile")) nil))
(should (eq (conner--validate-command-plist '(:name "name" :command "test" :type "compile")) nil))
(should (eq (conner--validate-command-plist '(:name "name" :command "hook symbol" :type "compile" :hook car)) nil))
(should (eq (conner--validate-command-plist '(:name "name" :command "hook lambda" :type "compile" :hook (lambda () t))) nil))))
(ert-deftest conner-test-hooks ()
(with-temp-env
(setq-local hook-test-value 42)
(defun func-hook-test ()
(setq-local hook-test-value 69))
(conner-add-command conner-root-dir '(:name "Run me" :command "now" :type "compile" :hook func-hook-test))
(conner-run-command conner-root-dir "Run me")
(should (eq 69 hook-test-value))))
(ert-deftest conner-test-clean-command ()
(with-temp-env
(should (equal (conner--clean-command-plist '(:hook nil :name "some command" :workdir nil)) '(:name "some command")))))
(ert-deftest conner-test-silent-command ()
(with-temp-env
(conner-add-command conner-root-dir '(:name "Silent command" :command (lambda () (fake-func-check-display-buffer-alist t)) :type "elispf" :silent t))
(conner-add-command conner-root-dir '(:name "Normal command" :command fake-func-check-display-buffer-alist :type "elispf"))
(conner-run-command conner-root-dir "Silent command")
(conner-run-command conner-root-dir "Normal command")))
(ert-deftest conner-test-project-backend ()
(with-temp-env
(let ((conner-project-backend 'projectile))
(defun projectile-project-root (root-dir)
root-dir)
(defun projectile-relevant-known-projects ())
(conner--act-on-project (lambda (root-dir) (should (equal root-dir "/test"))) "/test"))
(let ((conner-project-backend 'project.el))
(conner--act-on-project (lambda (root-dir) (should (equal root-dir "/test"))) '(vc Git "/test")))
(let ((conner-project-backend 'unknown-backend))
(should-error (conner--act-on-project (lambda ()))))))
(ert-deftest conner-test-pp-functions-error ()
(with-temp-env
(should-error (conner--pp-list "not a list"))
(should-error (conner--pp-plist "not a plist"))
(should-error (conner--pp-plist '("also" "not" "a plist")))
(should-error (conner--pp-plist-list "not a list"))))
(ert-deftest conner-test-pp-functions-output ()
(with-temp-env
(should (equal (conner--pp-list '(a)) "(a)"))
(should (equal (conner--pp-list '(a b)) "(a\n b)"))
(should (equal (conner--pp-plist '(a b)) "(a b)"))
(should (equal (conner--pp-plist '(a b c d)) "(a b\n c d)"))
(should (equal (conner--pp-plist-list '((a b c d))) "((a b\n c d))"))
(should (equal (conner--pp-plist-list '((a b c d) (e f g h))) "((a b\n c d)\n (e f\n g h))"))
(should (equal (conner--pp-plist-list '((a b c d) (e f g (h i j k)))) "((a b\n c d)\n (e f\n g (h\n i\n j\n k)))"))))