-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.el
353 lines (312 loc) · 12.3 KB
/
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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
;; See `Running the tests` section in the readme.
(require 'abl-mode)
(require 'ert)
(require 'cl-lib)
(require 'f)
(toggle-debug-on-error)
(add-hook 'find-file-hooks 'abl-mode-hook)
(defun write-to-file (file-path string)
(with-temp-buffer (insert string)
(write-region (point-min) (point-max) file-path)))
(defun read-from-file (file-path)
(with-temp-buffer
(insert-file-contents file-path)
(buffer-string)))
(defconst test-file-content
(concat "import unittest\n"
"\n"
"def test_free_standing_one():\n"
"#markerone\n"
" pass\n"
"\n"
"class AblTest(unittest.TestCase):\n"
"#markertwo\n"
" def test_abl_mode(self):\n"
" self.fail('A FAILING ' + 'TEST')\n"
"\n"
" def test_other_thing(self):\n"
" pass"
"\n"
" def test_one_more_thing(self):\n"
" self.fail()"
"\n"
"def test_free_standing_two():\n"
"#markerthree\n"
" pass\n"
))
(defconst setup-content
(concat "from setuptools import setup\n"
"setup(name='test')\n"))
(defun character-count (path char)
(let ((count 0))
(save-excursion
(find-file path)
(goto-char (point-min))
(setq count (count-matches "t"))
(kill-buffer))
count))
(character-count "/Users/ulas/temp/yada.txt" "t")
(cl-defstruct (testenv
(:constructor new-testenv
(base-dir
&optional
(project-dir (f-join base-dir "aproject"))
(package-dir (f-join project-dir "apackage"))
(proof-dir (f-join base-dir "_proof"))
(setup-py-path (f-join project-dir "setup.py"))
(test-file-path (f-join project-dir "project_tests.py"))
(init-file-path (f-join package-dir "__init__.py")))))
base-dir project-dir package-dir proof-dir setup-py-path test-file-path init-file-path)
(defun random-testenv ()
(new-testenv (make-temp-file "abltest" 't)))
(defun testenv-init (env)
;; create git repo with setup.py and a test file. the folder structure will
;; look something like this (the temp directory name starting with abltest
;; will be different):
;;
;; /tmp
;; |
;; - abltest18945
;; |
;; - .git
;; - _proof (dir)
;; - aproject
;; |
;; - setup.py
;; - project_tests.py (contents: test-file-content)
;; - apackage
;; |
;; - __init__.py (contents: #nothing)
(make-directory (testenv-base-dir env) 't)
(cl-assert (s-contains? "Initialized empty Git repository"
(shell-command-to-string
(concat "git init -b trunk " (testenv-base-dir env)))))
(make-directory (testenv-project-dir env))
(make-directory (testenv-package-dir env))
(make-directory (testenv-proof-dir env))
(write-to-file (testenv-setup-py-path env) setup-content)
(write-to-file (testenv-test-file-path env) test-file-content)
(write-to-file (testenv-init-file-path env) "#nothing")
(shell-command-to-string
(format
"cd %s && git add setup.py && git add %s && git commit -am 'haha'"
(testenv-base-dir env)
(testenv-project-dir env)))
env)
(defun testenv-project-name (env)
(abl-last-path-comp (testenv-project-dir env)))
(defun testenv-base-dirname (env)
(abl-last-path-comp (directory-file-name (testenv-base-dir env))))
(defun testenv-proof-file (env)
(f-join (testenv-proof-dir env) "out"))
(defun testenv-branch-git (env branch-name)
(shell-command-to-string (format
"cd %s && git checkout -b %s"
(testenv-base-dir env) branch-name)))
(defun cleanup (path)
;; rm -rf's a folder which begins with /tmp. you shouldn't put
;; important stuff into /tmp.
(unless (or (s-starts-with? "/tmp" path) (s-starts-with? "/var/folders/" path))
(error
(format "Tried to cleanup a path (%s) not in /tmp; refusing to do so."
path)))
(if (file-directory-p path) (delete-directory path 't)))
(defmacro abl-git-test (&rest tests-etc)
"Macro for tests. The first argument determines whether a dummy
vem is created."
`(let* ((env (testenv-init (random-testenv)))
;; (vem-proof-file-path (format "%s/_proof/proveit.txt" base-dir))
;; (test-proof-file-path (format "%s/_proof/prove_test.txt" base-dir))
;; (run-proof-file-path (format "%s/_proof/prove_run.txt" base-dir))
)
(unwind-protect
(progn
,@tests-etc)
(cleanup (testenv-base-dir env))
(dolist (buffer (buffer-list))
(if (s-starts-with? abl-mode-branch-shell-prefix (buffer-name buffer))
(let ((kill-buffer-query-functions nil))
(kill-buffer buffer))))
)))
(defun abl-values-for-path (path)
(let ((buffer (find-file path)))
(list
(buffer-local-value 'abl-mode buffer)
(buffer-local-value 'abl-mode-branch buffer)
(buffer-local-value 'abl-mode-project-name buffer)
(buffer-local-value 'abl-mode-vem-name buffer)
(buffer-local-value 'abl-mode-shell-name buffer))))
;; -----------------------------------------------------------------------------------------
;; Tests start here
(ert-deftest test-abl-capitalized ()
(should (abl-capitalized? "Hello"))
(should-not (abl-capitalized? "hello"))
)
(ert-deftest test-git-utils ()
(abl-git-test
(should (string-equal (abl-git-branch (testenv-project-dir env)) "trunk"))
(testenv-branch-git env "yello")
(should (string-equal (abl-git-branch (testenv-project-dir env)) "yello"))
(cleanup (concat (testenv-base-dir env) "/.git"))
(should-not (abl-git-branch (testenv-project-dir env)))
))
(ert-deftest test-project-vars ()
(abl-git-test
(should (string-equal (abl-git-branch (testenv-project-dir env)) "trunk"))
(should (string-equal (abl-get-project-name (testenv-project-dir env)) "aproject"))
(should (string-equal (abl-make-ve-name "trunk" "project")
"project_trunk"))
))
(ert-deftest test-no-vc-abl ()
(abl-git-test
(cleanup (f-join (testenv-base-dir env) ".git"))
(let ((test-buffer (find-file (testenv-test-file-path env))))
(should (buffer-local-value 'abl-mode test-buffer))
(should (string-equal (buffer-local-value 'abl-mode-project-name test-buffer) "aproject"))
(should-not (buffer-local-value 'abl-mode-branch test-buffer))
(should (string-equal (buffer-local-value 'abl-ve-name test-buffer) "aproject"))
)))
(ert-deftest test-git-abl ()
(abl-git-test
(let ((test-buffer (find-file (testenv-test-file-path env))))
(should (buffer-local-value 'abl-mode test-buffer))
(should (string-equal (buffer-local-value 'abl-mode-branch test-buffer)
"trunk"))
(should (string-equal (buffer-local-value 'abl-mode-project-name test-buffer) "aproject"))
(should (string-equal (buffer-local-value 'abl-ve-name test-buffer) "aproject_trunk"))
(should (string-equal (buffer-local-value 'abl-mode-shell-name test-buffer) "ABL-SHELL:aproject_trunk"))
)))
(ert-deftest test-branched-git-abl ()
(abl-git-test
(testenv-branch-git env "gitbranch")
(let ((test-buffer (find-file (testenv-test-file-path env))))
(should (buffer-local-value 'abl-mode test-buffer))
(should (string-equal (buffer-local-value 'abl-mode-branch test-buffer)
"gitbranch"))
(should (string-equal (buffer-local-value 'abl-mode-project-name test-buffer) "aproject"))
(should (string-equal (buffer-local-value 'abl-ve-name test-buffer) "aproject_gitbranch"))
(should (string-equal (buffer-local-value 'abl-mode-shell-name test-buffer) "ABL-SHELL:aproject_gitbranch"))
)))
(ert-deftest test-test-at-point ()
(abl-git-test
(find-file (testenv-test-file-path env))
(goto-char (point-min))
(should (string-equal (abl-mode-get-test-entity) "project_tests.py"))
(search-forward "markerone")
(should (string-equal (abl-mode-get-test-entity) "project_tests.py::test_free_standing_one"))
(search-forward "markertwo")
(should (string-equal (abl-mode-get-test-entity)
"project_tests.py::AblTest"))
(search-forward "self.fail")
(should (string-equal (abl-mode-get-test-entity)
"project_tests.py::AblTest::test_abl_mode"))
(search-forward "pass")
(should (string-equal (abl-mode-get-test-entity)
"project_tests.py::AblTest::test_other_thing"))
(search-forward "markerthree")
(should (string-equal (abl-mode-get-test-entity) "project_tests.py::test_free_standing_two"))
))
(ert-deftest test-dot-file ()
(abl-git-test
(write-to-file (f-join (testenv-project-dir env) ".abl")
"abl-ve-name \"VENAME\"\nabl-mode-shell-name \"SHELLNAME\"")
(find-file (testenv-test-file-path env))
(should (string-equal abl-ve-name "VENAME"))
(should (string-equal abl-mode-shell-name "SHELLNAME"))))
(ert-deftest test-ve-check-and-activation ()
(let ((collected-msgs '())
(output-dir (make-temp-file "testout" 't)))
;; mock the read-from-minibuffer used for getting user input
(cl-letf (((symbol-function 'read-from-minibuffer)
(lambda (msg)
(setq collected-msgs (append collected-msgs (list msg)))
(if (= (length collected-msgs) 1)
"test-ve"
"y"))))
(abl-git-test
(find-file (testenv-test-file-path env))
(setq abl-mode-ve-create-command (concat "cd " output-dir " && touch %s"))
(abl-mode-exec-command "ls")
(sleep-for 1)
(file-exists-p (f-join output-dir "test-ve"))
))))
(ert-deftest test-running-tests ()
(abl-git-test
(find-file (testenv-test-file-path env))
(let ((shell-name abl-mode-shell-name))
(setq abl-mode-check-and-activate-ve nil)
(abl-mode-run-test-at-point)
(let ((shell-buffer (get-buffer shell-name)))
(should shell-buffer)
(switch-to-buffer shell-buffer)
;; f**k it, we'll do it with sleep. I'll fix it later.
(sleep-for 2)
(goto-char (point-min))
(should (search-forward "A FAILING TEST" nil t))
(goto-char (point-min))
(should (= (count-matches "A FAILING TEST") 1))
))))
(ert-deftest test-rerun-last ()
(abl-git-test
(find-file (testenv-test-file-path env))
(let ((shell-name abl-mode-shell-name)
(new-test-file-path
(replace-regexp-in-string
"project_tests" "other_tests" (testenv-test-file-path env))))
(setq abl-mode-check-and-activate-ve nil)
(abl-mode-run-test-at-point)
(sleep-for 1)
(while (abl-shell-busy shell-name) (sleep-for 1))
(should (gethash shell-name abl-mode-last-tests-run))
(switch-to-buffer shell-name)
(goto-char (point-min))
(should (= (count-matches "A FAILING TEST") 1))
;; we want to make sure the test from project_tests.py is executed
(copy-file (testenv-test-file-path env) new-test-file-path)
(find-file new-test-file-path)
(setq abl-mode-check-and-activate-ve nil)
(abl-mode-rerun-last-test)
(sleep-for 1)
(while (abl-shell-busy shell-name) (sleep-for 1))
(switch-to-buffer shell-name)
(goto-char (point-min))
(should (= (count-matches "A FAILING TEST") 2))
)))
(ert-deftest test-replacement-ve ()
(let* ((output-dir (make-temp-file "testout" 't))
(ve-dir (make-temp-file "ves" 't))
(collected-msgs '())
(replacement-ve-name "test-ve")
(ve-out-file (f-join output-dir replacement-ve-name)))
(write-to-file (f-join ve-dir replacement-ve-name) "blah")
(cl-letf (((symbol-function 'read-from-minibuffer)
(lambda (msg)
(setq collected-msgs (append collected-msgs (list msg))) "test-ve")))
(abl-git-test
(find-file (testenv-test-file-path env))
(setq abl-mode-ve-activate-command
(concat "cd " output-dir " && echo 't' >> %s"))
(setq abl-mode-ve-base-dir ve-dir)
(save-excursion (abl-mode-exec-command "ls"))
(sleep-for 1)
(should (file-exists-p ve-out-file))
(should (= (character-count ve-out-file "t") 1))
(should (= (length collected-msgs) 1))
(should (string-equal (gethash abl-mode-shell-name
abl-mode-replacement-vems nil)
replacement-ve-name))
(should (string-equal abl-ve-name replacement-ve-name))
(let ((new-test-file-path
(replace-regexp-in-string
"project_tests" "other_tests" (testenv-test-file-path env))))
(copy-file (testenv-test-file-path env) new-test-file-path)
(find-file new-test-file-path)
(setq abl-mode-ve-activate-command
(concat "cd " output-dir " && echo 't' >> %s"))
(setq abl-mode-ve-base-dir ve-dir)
(abl-mode-exec-command "ls")
(sleep-for 1)
(should (= (character-count ve-out-file "t") 2))
(should (= (length collected-msgs) 1))
))
)))