-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathprettier-tests.el
307 lines (263 loc) · 9.5 KB
/
prettier-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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
;;; prettier-tests.el --- Tests for prettier.el -*- lexical-binding: t; -*-
;; Copyright (c) 2018-present Julian Scheid
;; Package-Requires: ((markdown-mode "20230723")(web-mode "20200501") (elm-mode "20200406") (pug-mode "20180513") (svelte-mode "20210222") (toml-mode "20161107") (solidity-mode "20200418") (vue-mode "20190415") (lsp-mode "20201111") (noflet "20141102") (f "20191110") (json-mode "20190123") (typescript-mode "20201002") (ert-async "0.1") (f "20220531"))
;;; Commentary:
;; Test suite for prettier.el.
;;; Code:
(require 'ert)
(require 'noflet)
(require 'thingatpt)
(require 'prettier)
(require 'svelte-mode)
(require 'typescript-mode)
(require 'f)
(require 'hexl)
(eval-when-compile
(require 'ert-async))
(defun prettier--hexlify-str (str)
"Return STR, hexlified."
(with-temp-buffer
(insert str)
(hexlify-buffer)
(buffer-string)))
(defun prettier--eval-file-if-exists (filename)
"Read and eval file FILENAME if it exists."
(let ((setup-elisp filename))
(when (file-exists-p setup-elisp)
(eval
(thing-at-point--read-from-whole-string
(f-read-text setup-elisp))))))
(defun prettier--run-test-case (directory)
"Run prettier formatting test in DIRECTORY."
(let ((default-directory directory))
(mapc
(lambda (original-file)
(let ((actual
(let ((prettier-diff-timeout-seconds 0)
(out-file (replace-regexp-in-string
"\\.original\\."
".test."
original-file)))
(with-temp-buffer
(unwind-protect
(progn
(insert-file-contents original-file)
(setq buffer-file-name out-file)
(rename-buffer out-file)
(prettier--eval-file-if-exists "prepare.elisp")
(set-auto-mode)
(prettier--eval-file-if-exists "setup.elisp")
(prettier-mode 1)
(save-buffer 0)
(prettier--eval-file-if-exists "test.elisp")
(should (equal (with-current-buffer "*prettier (local)*"
(buffer-string))
""))
(let ((error-buffer
(get-buffer prettier-error-buffer-name)))
(when error-buffer
(should (equal (with-current-buffer error-buffer
(buffer-string))
""))))
(f-read-bytes out-file))
(delete-file out-file)))))
(expected
(f-read-bytes
(replace-regexp-in-string
"\\.original\\."
".prettier."
original-file))))
(should (equal (prettier--hexlify-str actual)
(prettier--hexlify-str expected)))
(prettier--eval-file-if-exists "teardown.elisp")))
(directory-files directory t "\\.original\\."))))
(mapc
(lambda (test-directory)
(let ((dirname (file-name-base test-directory))
(pdirname (file-name-base
(directory-file-name
(file-name-directory test-directory)))))
(eval
`(ert-deftest
,(intern (format "prettier-testcase-%s/%s" dirname pdirname)) ()
,(format "Run testcases %s in %s." dirname pdirname)
(condition-case err
(prettier--run-test-case ,test-directory)
((quit error)
(sit-for 0.25)
(signal (car err) (cdr err))))))))
(mapcar
#'car
(seq-filter
(lambda (file-and-attributes)
(and
(not (equal "node_modules" (file-name-base (car file-and-attributes))))
(eq t (cadr file-and-attributes))))
(apply
'append
(mapcar
(lambda (x)
(directory-files-and-attributes
(file-name-as-directory (concat default-directory x))
t
"[a-z].*"))
'("test-stable" "test-v2"))))))
(defmacro prettier--ert-deftest-for-dirs (testname testdoc dirnames &rest body)
"Execute the specified test code in multiple directories.
TESTNAME and TESTDOC are used as macro name and doc string for `ert-deftest',
respectively. DIRNAMES is a list of directories in which to run the test code.
BODY is the test code."
`(mapc
(lambda (dirname)
(eval
(list
'ert-deftest
(intern (format "%s/%s" ,testname dirname))
'()
(format "%s Runs in %s." ,testdoc dirname)
(list 'cd (list 'concat 'default-directory dirname))
'(progn ,@body))))
,dirnames))
(defmacro prettier--ert-deftest-async-for-dirs (testname testdoc dirnames body)
"Async version of `prettier--ert-deftest-for-dirs'.
TESTNAME, TESTDOC and DIRNAMES are the same but BODY must be a closure that
accepts a `done' argument."
`(mapc
(lambda (dirname)
(eval
(list
'ert-deftest-async
(intern (format "%s/%s" ,testname dirname))
'(done)
(format "%s Runs in %s." ,testdoc dirname)
(list 'cd (list 'concat 'default-directory dirname))
'(funcall ,body done))))
,dirnames))
(prettier--ert-deftest-for-dirs
"prettier-test-prettier-version"
"Ensure the prettier version of each directory."
'("test-stable" "test-v2")
(with-temp-buffer
(setq buffer-file-name (concat default-directory "test.js"))
(js-mode)
(prettier-info)
(let ((dirname (file-name-base (directory-file-name default-directory))))
(with-current-buffer (get-buffer "prettier-info.el")
(let* ((info (read (buffer-substring (point-min) (point-max))))
(vers (plist-get (plist-get info :prettier-options) :versions))
(pver (plist-get vers :prettier)))
(cond
((equal dirname "test-stable") (should (equal pver "3.0.0")))
((equal dirname "test-v2") (should (equal pver "2.6.2")))
(t (error (format "Unexpected directory name: %s" dirname)))))))))
(prettier--ert-deftest-for-dirs
"web-mode-typescript"
""
'("test-stable" "test-v2")
(with-temp-buffer
(setq buffer-file-name (concat default-directory "test.ts"))
(web-mode)
(should (equal (prettier--parsers) '(typescript babel-ts)))))
(prettier--ert-deftest-for-dirs
"prettier--parsers-temp-buffer"
""
'("test-stable" "test-v2")
(with-temp-buffer
(setq buffer-file-name (concat default-directory "test.js"))
(js-mode)
(require 'lsp-mode)
(should (equal (prettier--parsers)
'(babel meriyah espree flow babel-flow)))))
(prettier--ert-deftest-for-dirs
"customize-prettier"
""
'("test-stable" "test-v2")
(customize-option 'prettier-pre-warm)
(customize-option 'prettier-inline-errors-flag)
(customize-option 'prettier-mode-sync-config-flag)
(customize-option 'prettier-editorconfig-flag)
(customize-option 'prettier-infer-parser-flag)
(customize-option 'prettier-enabled-parsers)
(customize-option 'prettier-mode-ignore-buffer-function)
(customize-option 'prettier-lighter))
(defun prettier-process-p (process)
"Return non-nil if PROCESS is like a Prettier process."
(and (string-match "^prettier" (process-name process))
(process-get process :server-id)
(process-live-p process)))
(defun any-prettier-process-p ()
"Return non-nil if any process is like a Prettier process."
(seq-some #'prettier-process-p (process-list)))
(defun delay (callback)
"Call CALLBACK delayed by a little while."
(run-at-time 0.5 nil callback))
(prettier--ert-deftest-async-for-dirs
"restart-prettier"
""
'("test-stable" "test-v2")
'(lambda (done)
(prettier--quit-all-processes)
(delay
(lambda ()
(with-current-buffer (get-buffer-create "test.js")
(js-mode)
(should (not (any-prettier-process-p)))
(prettier-mode)
(delay
(lambda ()
(should (any-prettier-process-p))
(prettier-restart)
(delay
(lambda ()
(should (any-prettier-process-p))
(funcall done))))))))))
(prettier--ert-deftest-for-dirs
"load-local-config-once"
"Ensure config is loaded only once for buffer without file."
'("test-stable" "test-v2")
(with-temp-buffer (js-mode) (prettier-mode))
(cl-flet ((prettier--load-config (&rest) (error "Called again")))
(with-temp-buffer (js-mode) (prettier-mode))))
(prettier--ert-deftest-for-dirs
"org-mode-src-block"
"Formatting a source block in org mode."
'("test-stable" "test-v2")
(with-temp-buffer
(org-mode)
(setq buffer-file-name (concat default-directory "test.org"))
(insert "\
Header
#+begin_src js
if ( true ) {
console.log('Hello, World!');
}
#+end_src
Footer")
(goto-char 24)
(prettier-prettify-org-src-code-at-point)
(should (equal (buffer-string)
"\
Header
#+begin_src js
if (true) {
console.log(\"Hello, World!\");
}
#+end_src
Footer"))))
(prettier--ert-deftest-for-dirs
"sync-test"
"Test syncing settings from Prettier."
'("test-stable" "test-v2")
(with-temp-buffer
(js-mode)
(setq buffer-file-name (concat default-directory "test.js"))
(setq js-indent-level 10)
(prettier-mode)
(should (equal js-indent-level 2))
(should (local-variable-p 'js-indent-first-init))
(should (null js-indent-first-init))
(prettier-mode -1)
(should (equal js-indent-level 10))
(should (not (local-variable-p 'js-indent-first-init)))))
(provide 'prettier-tests)
;;; prettier-tests.el ends here