-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
executable file
·298 lines (259 loc) · 13.5 KB
/
test.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
#!/usr/bin/env python3
"""Unit tests."""
import os
import subprocess
import tempfile
import unittest
from typing import List
def flake8(test: str, options: List[str] = None) -> List[str]:
"""Run flake8 on test input and return output."""
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(test.encode('utf-8'))
# print(test)
# print(' '.join(['flake8', '--isolated', '--select=LIT', temp_file.name] + [f'--literal-{option}' for option in (options or [])]))
process = subprocess.Popen(['flake8', '--isolated', '--select=LIT', temp_file.name] + [f'--literal-{option}' for option in (options or [])],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
os.remove(temp_file.name)
if (stderr):
return [f'0:0:{line}' for line in stderr.decode('utf-8').splitlines()]
# print(repr([line.split(':', 1)[1] for line in stdout.decode('utf-8').splitlines()]))
return [line.split(':', 1)[1] for line in stdout.decode('utf-8').splitlines()]
class TestQuotes(unittest.TestCase):
"""Test quote handling."""
def test_valid(self) -> None:
self.assertEqual(flake8('"""module docstring"""'), [])
self.assertEqual(flake8('"""module docstring"""\n"""additional docstring"""'), [])
self.assertEqual(flake8('def strings(x={1:2}, y=[({3:4},)]):\n """function docstring"""\n """additional function docstring'), [])
self.assertEqual(flake8('x = 42\n"""variable docstring"""\n"""additional varaible docstring"""'), [])
self.assertEqual(flake8('class Foo: """inline docstring""" ;'), [])
self.assertEqual(flake8('def inline(x={1:2}, y=[({3:4},)]): """inline docstring""" ; pass'), [])
self.assertEqual(flake8("x = '''multiline\n string'''"), [])
self.assertEqual(flake8("x = 'inline string'"), [])
def test_valid_switched(self) -> None:
options = ['inline-quotes=double', 'multiline-quotes=double', 'docstring-quotes=single']
self.assertEqual(flake8("'''module docstring'''", options), [])
self.assertEqual(flake8("'''module docstring'''\n'''additional docstring'''", options), [])
self.assertEqual(flake8("def strings(x={1:2}, y=[({3:4},)]):\n '''function docstring'''\n '''additional function docstring'''",
options), [])
self.assertEqual(flake8("x = 42\n'''variable docstring'''\n'''additional varaible docstring'''", options), [])
self.assertEqual(flake8("class Foo: '''inline docstring''' ; pass", options), [])
self.assertEqual(flake8("def inline(x={1:2}, y=[({3:4},)]): '''inline docstring''' ; pass", options), [])
self.assertEqual(flake8('x = """multiline\n string"""', options), [])
self.assertEqual(flake8('x = "inline string"', options), [])
def test_wrong_quote(self) -> None:
self.assertEqual(flake8("'''module docstring'''"), [
'1:1: LIT006 Use double quotes for docstring',
])
self.assertEqual(flake8("'''module docstring'''\n'''additional docstring'''"), [
'1:1: LIT006 Use double quotes for docstring',
'2:1: LIT006 Use double quotes for docstring',
])
self.assertEqual(flake8('x = """multiline\n string"""'), [
'1:5: LIT003 Use single quotes for multiline string',
])
self.assertEqual(flake8('x = "inline string"'), [
'1:5: LIT001 Use single quotes for string',
])
self.assertEqual(flake8('x = "inline string with \'both\' \\\"quotes\\\""'), [
'1:5: LIT001 Use single quotes for string',
])
self.assertEqual(flake8(r'x = r"\raw\string"'), [
'1:5: LIT001 Use single quotes for string',
])
def test_wrong_quote_switched(self) -> None:
options = ['inline-quotes=double', 'multiline-quotes=double', 'docstring-quotes=single']
self.assertEqual(flake8('"""module docstring"""', options), [
'1:1: LIT005 Use single quotes for docstring',
])
self.assertEqual(flake8('"""module docstring"""\n"""additional docstring"""', options), [
'1:1: LIT005 Use single quotes for docstring',
'2:1: LIT005 Use single quotes for docstring',
])
self.assertEqual(flake8("x = '''multiline\n string'''", options), [
'1:5: LIT004 Use double quotes for multiline string',
])
self.assertEqual(flake8("x = 'inline string'", options), [
'1:5: LIT002 Use double quotes for string',
])
self.assertEqual(flake8('x = \'inline string with \\\'both\\\' "quotes"\'', options), [
'1:5: LIT002 Use double quotes for string',
])
self.assertEqual(flake8(r"x = r'\raw\string'", options), [
'1:5: LIT002 Use double quotes for string',
])
def test_non_triple(self) -> None:
self.assertEqual(flake8('"module docstring"'), [
'1:1: LIT008 Use triple double quotes for docstring',
])
self.assertEqual(flake8("'module docstring'", ['docstring-quotes=single']), [
'1:1: LIT007 Use triple single quotes for docstring',
])
self.assertEqual(flake8("'module docstring'"), [
'1:1: LIT006 Use double quotes for docstring',
])
self.assertEqual(flake8('"module docstring"', ['docstring-quotes=single']), [
'1:1: LIT005 Use single quotes for docstring',
])
def test_avoid_escape(self) -> None:
self.assertEqual(flake8("x = 'avoid \\\' escape'"), [ # noqa: LIT013
'1:5: LIT011 Use double quotes for string to avoid escaped single quote',
])
self.assertEqual(flake8('x = "avoid \\\" escape"', ['inline-quotes=double']), [ # noqa: LIT014
'1:5: LIT012 Use single quotes for string to avoid escaped double quote',
])
class TestEscapes(unittest.TestCase):
"""Test escape handling."""
def test_valid(self) -> None:
self.assertEqual(flake8('x = \'inline string with "quotes"\''), [])
self.assertEqual(flake8('x = \'inline string with \\\'both\\\' "quotes"\''), [])
self.assertEqual(flake8('x = "avoid \' escape"'), [])
def test_valid_switched(self) -> None:
self.assertEqual(flake8('x = "inline string with \'quotes\'"', ['inline-quotes=double']), [])
self.assertEqual(flake8('x = "inline string with \\\"both\\\" \'quotes\'"', ['inline-quotes=double']), [])
self.assertEqual(flake8('x = \'avoid " escape\'', ['inline-quotes=double']), [])
def test_bad_escape(self) -> None:
self.assertEqual(flake8('x = "avoid \\\' escape"'), [
'1:5: LIT013 Escaped single quote is not necessary',
])
self.assertEqual(flake8('x = "avoid \\\' escape"', ['inline-quotes=double']), [
'1:5: LIT013 Escaped single quote is not necessary',
])
self.assertEqual(flake8('x = \'avoid \\\" escape\''), [
'1:5: LIT014 Escaped double quote is not necessary',
])
self.assertEqual(flake8('x = \'avoid \\\" escape\'', ['inline-quotes=double']), [
'1:5: LIT014 Escaped double quote is not necessary',
])
class TestContinuation(unittest.TestCase):
"""Test continuation string handling."""
def test_valid(self) -> None:
self.assertEqual(flake8("x = 'first' 'inline string'"), [])
self.assertEqual(flake8('x = "first" "avoid \' escape"'), [])
self.assertEqual(flake8('x = \'first " escape\' "avoid \' escape"'), [])
def test_valid_switched(self) -> None:
self.assertEqual(flake8('x = "first" "inline string"', ['inline-quotes=double']), [])
self.assertEqual(flake8('x = \'first\' \'avoid " escape\'', ['inline-quotes=double']), [])
self.assertEqual(flake8('x = "first \' escape" \'avoid " escape\'', ['inline-quotes=double']), [])
def test_continuation(self) -> None:
self.assertEqual(flake8('x = \'first\' "second"'), [
'1:13: LIT001 Use single quotes for string',
])
self.assertEqual(flake8('x = \'first\' "second"', ['inline-quotes=double']), [
'1:5: LIT002 Use double quotes for string',
])
self.assertEqual(flake8('x = \'first\' "avoid \' escape"'), [
'1:5: LIT015 Use double quotes for continuation strings to match',
])
self.assertEqual(flake8('x = "first" \'avoid " escape\'', ['inline-quotes=double']), [
'1:5: LIT016 Use single quotes for continuation strings to match',
])
self.assertEqual(flake8("x = 'first' 'avoid \\\' escape'"), [ # noqa: LIT013
'1:5: LIT015 Use double quotes for continuation strings to match',
'1:13: LIT011 Use double quotes for string to avoid escaped single quote',
])
self.assertEqual(flake8('x = "first" "avoid \\\" escape"', ['inline-quotes=double']), [ # noqa: LIT014
'1:5: LIT016 Use single quotes for continuation strings to match',
'1:13: LIT012 Use single quotes for string to avoid escaped double quote',
])
self.assertEqual(flake8('x = "first" "sec\'ond" \'thi"rd\''), [
'1:5: LIT001 Use single quotes for string',
])
self.assertEqual(flake8('x = \'first\' "sec\'ond" \'thi"rd\'', ['inline-quotes=double']), [
'1:5: LIT002 Use double quotes for string',
])
class TestRaw(unittest.TestCase):
"""Test raw string handling."""
def test_valid(self) -> None:
self.assertEqual(flake8(r"x = r'\raw\string'"), [])
self.assertEqual(flake8(r"x = 'non-raw\nstring'"), [])
self.assertEqual(flake8(r"x = '\\non-raw\nstring'"), [])
self.assertEqual(flake8(r"x = '\\'"), [])
def test_valid_switched(self) -> None:
options = ['inline-quotes=double']
self.assertEqual(flake8(r'x = r"\raw\string"', options), [])
self.assertEqual(flake8(r'x = "non-raw\nstring"', options), [])
self.assertEqual(flake8(r'x = "\\non-raw\nstring"', options), [])
self.assertEqual(flake8(r'x = "\\"', options), [])
def test_raw(self) -> None:
self.assertEqual(flake8("x = r'unnecessary raw'"), [
'1:5: LIT101 Remove raw prefix when not using escapes',
])
self.assertEqual(flake8(r"x = 'need \\ raw'"), [
'1:5: LIT102 Use raw prefix to avoid escaped slash',
])
def test_re_raw_avoid(self) -> None:
options = ['re-pattern-raw=avoid']
self.assertEqual(flake8("import re\nx = re.compile(r'necessary\\nraw')", options), [])
self.assertEqual(flake8("import re\nx = re.compile(r'unnecessary raw')", options), [
'2:16: LIT101 Remove raw prefix when not using escapes',
])
self.assertEqual(flake8("import re\nx = re.compile(rb'unnecessary raw')", options), [
'2:16: LIT101 Remove raw prefix when not using escapes',
])
self.assertEqual(flake8("import re\nx = re.compile(r'unnecessary raw'.join([]))", options), [
'2:16: LIT101 Remove raw prefix when not using escapes',
])
self.assertEqual(flake8("import re as regex\nx = regex.compile(r'unnecessary raw')", options), [
'2:19: LIT101 Remove raw prefix when not using escapes',
])
self.assertEqual(flake8("from re import compile\nx = compile(r'unnecessary raw')", options), [
'2:13: LIT101 Remove raw prefix when not using escapes',
])
self.assertEqual(flake8("from re import compile as re_comp\nx = re_comp(r'unnecessary raw')", options), [
'2:13: LIT101 Remove raw prefix when not using escapes',
])
self.assertEqual(flake8("x = re.compile(r'unnecessary raw')", options), [
'1:16: LIT101 Remove raw prefix when not using escapes',
])
self.assertEqual(flake8("import re\nx = re.compile(pattern(r'unnecessary raw'))", options), [
'2:24: LIT101 Remove raw prefix when not using escapes',
])
def test_re_raw_allow(self) -> None:
options = ['re-pattern-raw=allow']
self.assertEqual(flake8("import re\nx = re.compile(r'necessary\\nraw')", options), [])
self.assertEqual(flake8("import re\nx = re.compile(r'unnecessary raw')", options), [])
self.assertEqual(flake8("import re\nx = re.compile(rb'unnecessary raw')", options), [])
self.assertEqual(flake8("import re\nx = re.compile(r'unnecessary raw'.join([]))", options), [])
self.assertEqual(flake8("import re as regex\nx = regex.compile(r'unnecessary raw')", options), [])
self.assertEqual(flake8("from re import compile\nx = compile(r'unnecessary raw')", options), [])
self.assertEqual(flake8("from re import compile as re_comp\nx = re_comp(r'unnecessary raw')", options), [])
self.assertEqual(flake8("x = re.compile(r'unnecessary raw')", options), [
'1:16: LIT101 Remove raw prefix when not using escapes',
])
self.assertEqual(flake8("import re\nx = re.compile(pattern(r'unnecessary raw'))", options), [
'2:24: LIT101 Remove raw prefix when not using escapes',
])
def test_re_raw_always(self) -> None:
options = ['re-pattern-raw=always']
self.assertEqual(flake8("import re\nx = re.compile(r'necessary\\nraw')", options), [])
self.assertEqual(flake8("import re\nx = re.compile(r'unnecessary raw')", options), [])
self.assertEqual(flake8("import re\nx = re.compile(rb'unnecessary raw')", options), [])
self.assertEqual(flake8("import re\nx = re.compile(r'unnecessary raw'.join([]))", options), [])
self.assertEqual(flake8("import re as regex\nx = regex.compile(r'unnecessary raw')", options), [])
self.assertEqual(flake8("from re import compile\nx = compile(r'unnecessary raw')", options), [])
self.assertEqual(flake8("from re import compile as re_comp\nx = re_comp(r'unnecessary raw')", options), [])
self.assertEqual(flake8("x = re.compile(r'unnecessary raw')", options), [
'1:16: LIT101 Remove raw prefix when not using escapes',
])
self.assertEqual(flake8("import re\nx = re.compile(pattern(r'unnecessary raw'))", options), [
'2:24: LIT101 Remove raw prefix when not using escapes',
])
class TestOptions(unittest.TestCase):
"""Test options."""
def test_no_avoid_escape(self) -> None:
self.assertEqual(flake8('x = "avoid \' escape"', ['no-avoid-escape']), [
'1:5: LIT001 Use single quotes for string',
])
self.assertEqual(flake8('x = \'first\' "avoid \' escape"', ['no-avoid-escape']), [
'1:13: LIT001 Use single quotes for string',
])
self.assertEqual(flake8("x = 'avoid \\\' escape'", ['no-avoid-escape']), []) # noqa: LIT013
self.assertEqual(flake8("x = 'not\\raw'", ['no-avoid-escape']), []) # noqa: LIT102
pass
def test_include_name(self) -> None:
self.assertEqual(flake8('x = "inline string"', ['include-name']), [
'1:5: LIT001 (flake8-literal) Use single quotes for string',
])
if __name__ == '__main__':
unittest.main()