1
1
import re
2
- from typing import TYPE_CHECKING , Any , Callable , TypeVar
3
2
from unittest .mock import patch
4
3
5
4
import pytest
15
14
except ImportError as e :
16
15
raise RuntimeError ("Please install Black with the 'd' extra" ) from e
17
16
18
- if TYPE_CHECKING :
19
- F = TypeVar ("F" , bound = Callable [..., Any ])
20
-
21
- unittest_run_loop : Callable [[F ], F ] = lambda x : x
22
- else :
23
- try :
24
- from aiohttp .test_utils import unittest_run_loop
25
- except ImportError :
26
- # unittest_run_loop is unnecessary and a no-op since aiohttp 3.8, and
27
- # aiohttp 4 removed it. To maintain compatibility we can make our own
28
- # no-op decorator.
29
- def unittest_run_loop (func , * args , ** kwargs ):
30
- return func
31
-
32
17
33
18
@pytest .mark .blackd
34
19
class BlackDTestCase (AioHTTPTestCase ):
@@ -42,20 +27,17 @@ def test_blackd_main(self) -> None:
42
27
async def get_application (self ) -> web .Application :
43
28
return blackd .make_app ()
44
29
45
- @unittest_run_loop
46
30
async def test_blackd_request_needs_formatting (self ) -> None :
47
31
response = await self .client .post ("/" , data = b"print('hello world')" )
48
32
self .assertEqual (response .status , 200 )
49
33
self .assertEqual (response .charset , "utf8" )
50
34
self .assertEqual (await response .read (), b'print("hello world")\n ' )
51
35
52
- @unittest_run_loop
53
36
async def test_blackd_request_no_change (self ) -> None :
54
37
response = await self .client .post ("/" , data = b'print("hello world")\n ' )
55
38
self .assertEqual (response .status , 204 )
56
39
self .assertEqual (await response .read (), b"" )
57
40
58
- @unittest_run_loop
59
41
async def test_blackd_request_syntax_error (self ) -> None :
60
42
response = await self .client .post ("/" , data = b"what even ( is" )
61
43
self .assertEqual (response .status , 400 )
@@ -65,21 +47,18 @@ async def test_blackd_request_syntax_error(self) -> None:
65
47
msg = f"Expected error to start with 'Cannot parse', got { repr (content )} " ,
66
48
)
67
49
68
- @unittest_run_loop
69
50
async def test_blackd_unsupported_version (self ) -> None :
70
51
response = await self .client .post (
71
52
"/" , data = b"what" , headers = {blackd .PROTOCOL_VERSION_HEADER : "2" }
72
53
)
73
54
self .assertEqual (response .status , 501 )
74
55
75
- @unittest_run_loop
76
56
async def test_blackd_supported_version (self ) -> None :
77
57
response = await self .client .post (
78
58
"/" , data = b"what" , headers = {blackd .PROTOCOL_VERSION_HEADER : "1" }
79
59
)
80
60
self .assertEqual (response .status , 200 )
81
61
82
- @unittest_run_loop
83
62
async def test_blackd_invalid_python_variant (self ) -> None :
84
63
async def check (header_value : str , expected_status : int = 400 ) -> None :
85
64
response = await self .client .post (
@@ -102,7 +81,6 @@ async def check(header_value: str, expected_status: int = 400) -> None:
102
81
await check ("pypy3.0" )
103
82
await check ("jython3.4" )
104
83
105
- @unittest_run_loop
106
84
async def test_blackd_pyi (self ) -> None :
107
85
source , expected = read_data ("cases" , "stub.py" )
108
86
response = await self .client .post (
@@ -111,7 +89,6 @@ async def test_blackd_pyi(self) -> None:
111
89
self .assertEqual (response .status , 200 )
112
90
self .assertEqual (await response .text (), expected )
113
91
114
- @unittest_run_loop
115
92
async def test_blackd_diff (self ) -> None :
116
93
diff_header = re .compile (
117
94
r"(In|Out)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d\+\d\d:\d\d"
@@ -129,7 +106,6 @@ async def test_blackd_diff(self) -> None:
129
106
actual = diff_header .sub (DETERMINISTIC_HEADER , actual )
130
107
self .assertEqual (actual , expected )
131
108
132
- @unittest_run_loop
133
109
async def test_blackd_python_variant (self ) -> None :
134
110
code = (
135
111
"def f(\n "
@@ -161,14 +137,12 @@ async def check(header_value: str, expected_status: int) -> None:
161
137
await check ("py34,py36" , 204 )
162
138
await check ("34" , 204 )
163
139
164
- @unittest_run_loop
165
140
async def test_blackd_line_length (self ) -> None :
166
141
response = await self .client .post (
167
142
"/" , data = b'print("hello")\n ' , headers = {blackd .LINE_LENGTH_HEADER : "7" }
168
143
)
169
144
self .assertEqual (response .status , 200 )
170
145
171
- @unittest_run_loop
172
146
async def test_blackd_invalid_line_length (self ) -> None :
173
147
response = await self .client .post (
174
148
"/" ,
@@ -177,7 +151,6 @@ async def test_blackd_invalid_line_length(self) -> None:
177
151
)
178
152
self .assertEqual (response .status , 400 )
179
153
180
- @unittest_run_loop
181
154
async def test_blackd_skip_first_source_line (self ) -> None :
182
155
invalid_first_line = b"Header will be skipped\r \n i = [1,2,3]\n j = [1,2,3]\n "
183
156
expected_result = b"Header will be skipped\r \n i = [1, 2, 3]\n j = [1, 2, 3]\n "
@@ -191,19 +164,16 @@ async def test_blackd_skip_first_source_line(self) -> None:
191
164
self .assertEqual (response .status , 200 )
192
165
self .assertEqual (await response .read (), expected_result )
193
166
194
- @unittest_run_loop
195
167
async def test_blackd_preview (self ) -> None :
196
168
response = await self .client .post (
197
169
"/" , data = b'print("hello")\n ' , headers = {blackd .PREVIEW : "true" }
198
170
)
199
171
self .assertEqual (response .status , 204 )
200
172
201
- @unittest_run_loop
202
173
async def test_blackd_response_black_version_header (self ) -> None :
203
174
response = await self .client .post ("/" )
204
175
self .assertIsNotNone (response .headers .get (blackd .BLACK_VERSION_HEADER ))
205
176
206
- @unittest_run_loop
207
177
async def test_cors_preflight (self ) -> None :
208
178
response = await self .client .options (
209
179
"/" ,
@@ -218,13 +188,11 @@ async def test_cors_preflight(self) -> None:
218
188
self .assertIsNotNone (response .headers .get ("Access-Control-Allow-Headers" ))
219
189
self .assertIsNotNone (response .headers .get ("Access-Control-Allow-Methods" ))
220
190
221
- @unittest_run_loop
222
191
async def test_cors_headers_present (self ) -> None :
223
192
response = await self .client .post ("/" , headers = {"Origin" : "*" })
224
193
self .assertIsNotNone (response .headers .get ("Access-Control-Allow-Origin" ))
225
194
self .assertIsNotNone (response .headers .get ("Access-Control-Expose-Headers" ))
226
195
227
- @unittest_run_loop
228
196
async def test_preserves_line_endings (self ) -> None :
229
197
for data in (b"c\r \n c\r \n " , b"l\n l\n " ):
230
198
# test preserved newlines when reformatted
@@ -234,14 +202,12 @@ async def test_preserves_line_endings(self) -> None:
234
202
response = await self .client .post ("/" , data = data )
235
203
self .assertEqual (response .status , 204 )
236
204
237
- @unittest_run_loop
238
205
async def test_normalizes_line_endings (self ) -> None :
239
206
for data , expected in ((b"c\r \n c\n " , "c\r \n c\r \n " ), (b"l\n l\r \n " , "l\n l\n " )):
240
207
response = await self .client .post ("/" , data = data )
241
208
self .assertEqual (await response .text (), expected )
242
209
self .assertEqual (response .status , 200 )
243
210
244
- @unittest_run_loop
245
211
async def test_single_character (self ) -> None :
246
212
response = await self .client .post ("/" , data = "1" )
247
213
self .assertEqual (await response .text (), "1\n " )
0 commit comments