/
test_handlers.py
421 lines (381 loc) · 13.6 KB
/
test_handlers.py
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import json
import typing as t
import pkg_resources
import pytest
import requests
from jsonschema import validate
from jupyterlab_code_formatter.formatters import SERVER_FORMATTERS
from jupyterlab_code_formatter.handlers import setup_handlers
from notebook.tests.launchnotebook import NotebookTestBase
def _generate_list_formaters_entry_json_schema(
formatter_name: str,
) -> t.Dict[str, t.Any]:
return {
"type": "object",
"properties": {
formatter_name: {
"type": "object",
"properties": {
"enabled": {"type": "boolean"},
"label": {"type": "string"},
},
}
},
}
EXPECTED_VERSION_SCHEMA = {
"type": "object",
"properties": {
"version": {
"type": "string",
}
},
}
EXPECTED_LIST_FORMATTERS_SCHEMA = {
"type": "object",
"properties": {
"formatters": {
formatter_name: _generate_list_formaters_entry_json_schema(formatter_name)
for formatter_name in SERVER_FORMATTERS
}
},
}
EXPECTED_FROMAT_SCHEMA = {
"type": "object",
"properties": {
"code": {
"type": "array",
"items": {
"type": "object",
"properties": {"code": {"type": "string"}, "error": {"type": "string"}},
},
}
},
}
SIMPLE_VALID_PYTHON_CODE = "x= 22; e =1"
class TestHandlers(NotebookTestBase):
def setUp(self) -> None:
setup_handlers(self.notebook.web_app)
def _create_headers(
self, plugin_version: t.Optional[str] = None
) -> t.Dict[str, str]:
return {
"Plugin-Version": plugin_version
if plugin_version is not None
else pkg_resources.get_distribution("jupyterlab_code_formatter").version
}
def _format_code_request(
self,
formatter: str,
code: t.List[str],
options: t.Dict[str, t.Any],
plugin_version: t.Optional[str] = None,
) -> requests.Response:
return self.request(
verb="POST",
path="/jupyterlab_code_formatter/format",
data=json.dumps(
{
"code": code,
"options": options,
"notebook": True,
"formatter": formatter,
}
),
headers=self._create_headers(plugin_version),
)
@staticmethod
def _check_http_200_and_schema(response):
assert response.status_code == 200
json_result = response.json()
validate(instance=json_result, schema=EXPECTED_FROMAT_SCHEMA)
return json_result
def test_list_formatters(self):
"""Check if the formatters list route works."""
response = self.request(
verb="GET",
path="/jupyterlab_code_formatter/formatters",
headers=self._create_headers(),
)
validate(instance=response.json(), schema=EXPECTED_LIST_FORMATTERS_SCHEMA)
def test_404_on_unknown(self):
"""Check that it 404 correctly if formatter name is bad."""
response = self._format_code_request(
formatter="UNKNOWN", code=[SIMPLE_VALID_PYTHON_CODE], options={}
)
assert response.status_code == 404
def test_can_apply_python_formatter(self):
"""Check that it can apply black with simple config."""
response = self._format_code_request(
formatter="black",
code=[SIMPLE_VALID_PYTHON_CODE],
options={"line_length": 88},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == "x = 22\ne = 1"
def test_can_use_black_config(self):
"""Check that it can apply black with advanced config."""
given = "some_string='abc'"
expected = "some_string = 'abc'"
response = self._format_code_request(
formatter="black",
options={"line_length": 123, "string_normalization": False},
code=[given],
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_return_error_if_any(self):
"""Check that it returns the error if any."""
bad_python = "this_is_bad = 'hihi"
response = self._format_code_request(
formatter="black",
options={"line_length": 123, "string_normalization": False},
code=[bad_python],
)
json_result = self._check_http_200_and_schema(response)
assert (
json_result["code"][0]["error"] == "Cannot parse: 1:13: this_is_bad = 'hihi"
)
def test_can_handle_magic(self):
"""Check that it's fine to run formatters for code with magic."""
given = '%%timeit\nsome_string = "abc"'
expected = '%%timeit\nsome_string = "abc"'
for formatter in ["black", "yapf", "isort"]:
response = self._format_code_request(
formatter=formatter,
code=[given],
options={},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_handle_shell_cmd(self):
"""Check that it's fine to run formatters for code with shell cmd."""
given = '%%timeit\nsome_string = "abc"\n!pwd'
expected = '%%timeit\nsome_string = "abc"\n!pwd'
for formatter in ["black", "yapf", "isort"]:
response = self._format_code_request(
formatter=formatter,
code=[given],
options={},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_handle_incompatible_magic_language(self):
"""Check that it will ignore incompatible magic language cellblock."""
given = "%%html\n<h1>Hi</h1>"
expected = "%%html\n<h1>Hi</h1>"
for formatter in ["black", "yapf", "isort"]:
response = self._format_code_request(
formatter=formatter,
code=[given],
options={},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_handle_incompatible_magic_language_single(self):
"""Check that it will ignore incompatible magic language cellblock with single %."""
given = "%html <h1>Hi</h1>"
expected = "%html <h1>Hi</h1>"
for formatter in ["black", "yapf", "isort"]:
response = self._format_code_request(
formatter=formatter,
code=[given],
options={},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_ipython_help_signle(self) -> None:
"""Check that it will ignore single question mark interactive help lines on the fly."""
given = " bruh?\nprint('test')\n#test?"
expected = ' bruh?\nprint("test")\n# test?'
response = self._format_code_request(
formatter="black",
code=[given],
options={},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_ipython_help_double(self) -> None:
"""Check that it will ignore double question mark interactive help lines on the fly."""
given = " bruh??\nprint('test')\n#test?"
expected = ' bruh??\nprint("test")\n# test?'
response = self._format_code_request(
formatter="black",
code=[given],
options={},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_will_ignore_question_mark(self) -> None:
"""Check that it will ignore single question mark in comments."""
given = """def f():
# bruh what?
# again bruh? really
# a ? b
print('hi')
x = '?'"""
expected = """def f():
# bruh what?
# again bruh? really
# a ? b
print("hi")
x = "?\""""
response = self._format_code_request(
formatter="black",
code=[given],
options={},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_will_ignore_question_mark2(self) -> None:
"""Check that it will ignore single question mark in comments."""
given = """def f():
# bruh what??
# again bruh?? really
# a ? b ? c
print('hi')"""
expected = """def f():
# bruh what??
# again bruh?? really
# a ? b ? c
print("hi")"""
response = self._format_code_request(
formatter="black",
code=[given],
options={},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_will_ignore_question_weird(self) -> None:
given = """wat
wat??"""
expected = """wat
wat??"""
response = self._format_code_request(
formatter="black",
code=[given],
options={},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_use_styler(self):
given = "a = 3; 2"
expected = "a <- 3\n2"
response = self._format_code_request(
formatter="styler",
code=[given],
options={"scope": "tokens"},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_use_styler_2(self):
given = """data_frame(
small = 2 ,
medium = 4,#comment without space
large =6
)"""
expected = """data_frame(
small = 2,
medium = 4, # comment without space
large = 6
)"""
response = self._format_code_request(
code=[given],
options={"strict": False},
formatter="styler",
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_use_styler_3(self):
given = "1++1/2*2^2"
expected = "1 + +1/2*2^2"
response = self._format_code_request(
formatter="styler",
options={
"math_token_spacing": {
"one": ["'+'", "'-'"],
"zero": ["'/'", "'*'", "'^'"],
}
},
code=[given],
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_use_styler_4(self):
given = """a <- function() {
### not to be indented
# indent normally
33
}"""
expected = """a <- function() {
### not to be indented
# indent normally
33
}"""
response = self._format_code_request(
code=[given],
formatter="styler",
options=dict(
reindention=dict(regex_pattern="^###", indention=0, comments_only=True)
),
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_use_styler_5(self):
given = """call(
# SHOULD BE ONE SPACE BEFORE
1,2)
"""
expected = """call(
# SHOULD BE ONE SPACE BEFORE
1, 2
)"""
response = self._format_code_request(
code=[given],
formatter="styler",
options=dict(indent_by=4, start_comments_with_one_space=True),
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_can_use_styler_6(self):
given = "1+1-3"
expected = "1 + 1 - 3"
response = self._format_code_request(
code=[given],
formatter="styler",
options={
"math_token_spacing": "tidyverse_math_token_spacing",
"reindention": "tidyverse_reindention",
},
)
json_result = self._check_http_200_and_schema(response)
assert json_result["code"][0]["code"] == expected
def test_422_on_mismatch_version_1(self):
response = self.request(
verb="GET",
path="/jupyterlab_code_formatter/formatters",
headers=self._create_headers("0.0.0"),
)
assert response.status_code == 422
def test_200_on_version_without_header(self):
response = self.request(
verb="GET",
path="/jupyterlab_code_formatter/version",
)
assert response.status_code == 200
validate(instance=response.json(), schema=EXPECTED_VERSION_SCHEMA)
def test_200_on_version_with_wrong_header(self):
response = self.request(
verb="GET",
path="/jupyterlab_code_formatter/version",
headers=self._create_headers("0.0.0"),
)
assert response.status_code == 200
validate(instance=response.json(), schema=EXPECTED_VERSION_SCHEMA)
def test_200_on_version_with_correct_header(self):
response = self.request(
verb="GET",
path="/jupyterlab_code_formatter/version",
headers=self._create_headers(),
)
assert response.status_code == 200
validate(instance=response.json(), schema=EXPECTED_VERSION_SCHEMA)