-
Notifications
You must be signed in to change notification settings - Fork 146
/
run_tests.py
267 lines (221 loc) · 7.21 KB
/
run_tests.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
"""unit tests for flake8-isort"""
import collections
import os
import textwrap
import pytest
from flake8_isort import Flake8Isort
def write_python_file(tmpdir, content):
source = textwrap.dedent(content)
file_path = os.path.join(str(tmpdir), 'test.py')
with open(file_path, 'w') as python_file:
python_file.write(source)
return (file_path, source)
def write_isort_cfg(tmpdir, content):
write_config_file(tmpdir, '.isort.cfg', 'settings', content)
def write_setup_cfg(tmpdir, content):
write_config_file(tmpdir, 'setup.cfg', 'isort', content)
def write_tox_ini(tmpdir, content):
write_config_file(tmpdir, 'tox.ini', 'isort', content)
def write_pyproject_toml(tmpdir, content):
write_config_file(tmpdir, 'pyproject.toml', 'tool.isort', content)
def write_config_file(tmpdir, filename, header, content):
source = f'[{header}]\n{textwrap.dedent(content)}'
file_path = os.path.join(str(tmpdir), filename)
with open(file_path, 'w') as config_file:
config_file.write(source)
def check_isort(return_values, references):
"""Sort the return by (line, errortype) and compare it to the reference"""
assert len(return_values) == len(references)
for return_value, reference in zip(
sorted(return_values, key=lambda x: (x[0], x[2])), references
):
assert return_value[:2] == reference[:2]
assert return_value[2].startswith(reference[2])
testcases = [
{
'name': 'sorted_correctly_default',
'code': """
import os
from sys import path
""",
'reference': [],
},
{
'name': 'sorted_correctly_alpha',
'config': """
force_single_line=True
force_alphabetical_sort=True
""",
'code': """
from sys import path
import os
""",
'reference': [],
},
{
'name': 'eof_blank_lines',
'code': """
import os
from sys import path
""",
'reference': [],
},
{
'name': 'imports_requires_blank_line',
'code': """
from __future__ import division
import threading
from sys import pid
""",
'reference': [(3, 0, 'I003 ')],
},
{
'name': 'isortcfg_skip_file',
'config': 'skip=test.py',
'code': 'skipped_file',
'reference': [],
},
{'name': 'file_skipped_with_comment', 'code': '# isort:skip_file', 'reference': []},
{
'name': 'imports_unexpected_blank_line',
'code': """
from __future__ import division
import threading
from sys import pid
""",
'reference': [(5, 0, 'I004 ')],
},
{
'name': 'sorted_incorrectly_multiple',
'code': """
from __future__ import division
import os
from sys import pid
import threading
import isort
def func(): ...
""",
'reference': [(3, 0, 'I003 '), (5, 0, 'I001 '), (10, 0, 'I004 ')],
},
{
'name': 'sorted_incorrectly',
'config': 'force_single_line=True',
'code': """
from sys import pid
import threading
""",
'reference': [(3, 0, 'I001 ')],
},
{'name': 'empty_file', 'code': '\n\n', 'reference': []},
{
'name': 'wrapped_imports',
'config': 'wrap_length=65',
'code': """
from deluge.common import (fdate, fpcnt, fpeer, fsize, fspeed,
ftime, get_path_size, is_infohash,
is_ip, is_magnet, is_url)
""",
'reference': [],
},
{
'name': 'force_single_line_imports',
'config': """
force_alphabetical_sort=True
force_single_line=True
""",
'code': """
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
""",
'reference': [],
},
{
'name': 'missing_add_imports',
'config': 'add_imports=from __future__ import unicode_literals',
'code': 'import os\n',
'reference': [(1, 0, 'I003'), (1, 0, 'I005')],
},
]
@pytest.mark.parametrize('mode', ['file', 'code_string'])
@pytest.mark.parametrize('testcase', testcases, ids=[t['name'] for t in testcases])
def test_flake8_isort(tmpdir, testcase, mode):
"""Test the code examples in files and directly from string"""
with tmpdir.as_cwd():
if 'config' in testcase:
write_isort_cfg(tmpdir, testcase['config'])
if mode == 'file':
(file_path, lines) = write_python_file(tmpdir, testcase['code'])
checker = Flake8Isort(None, file_path, lines)
elif mode == 'code_string':
source = textwrap.dedent(testcase['code'])
checker = Flake8Isort(None, None, source)
return_values = list(checker.run())
check_isort(return_values, testcase['reference'])
def test_isortcfg_found(tmpdir):
source = """
from sys import pid
import threading
"""
(file_path, lines) = write_python_file(tmpdir, source)
write_isort_cfg(tmpdir, 'force_single_line=True')
checker = Flake8Isort(None, file_path, lines)
checker.config_file = True
ret = list(checker.run())
check_isort(ret, [(3, 0, 'I001 ')])
def test_isortcfg_not_found(tmpdir):
(file_path, lines) = write_python_file(tmpdir, 'from sys import pid, path')
checker = Flake8Isort(None, file_path, lines)
checker.search_current = False
checker.config_file = True
ret = list(checker.run())
check_isort(ret, [(1, 0, 'I001 ')])
def test_isort_formatted_output(tmpdir):
source = """
from __future__ import division
import os
from sys import pid
"""
options = collections.namedtuple(
'Options',
[
'no_isort_config',
'isort_show_traceback',
'stdin_display_name',
'isort_no_skip_gitignore',
],
)
(file_path, lines) = write_python_file(tmpdir, source)
diff = ' from __future__ import division\n+\n import os'
checker = Flake8Isort(None, file_path, lines)
checker.parse_options(None, options(None, True, 'stdin', None), None)
ret = list(checker.run())
assert len(ret) == 1
assert ret[0][0] == 3
assert ret[0][1] == 0
assert diff in ret[0][2]
@pytest.mark.parametrize(
'method_to_write_config',
[write_isort_cfg, write_setup_cfg, write_tox_ini, write_pyproject_toml],
)
def test_if_config_file_is_used(tmpdir, method_to_write_config):
source = """
import os
from sys import path
"""
(file_path, lines) = write_python_file(
tmpdir,
source,
)
method_to_write_config(tmpdir, 'lines_between_types=1')
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
check_isort(ret, [(3, 0, 'I003 ')])
def test_flake8(tmpdir):
from flake8.main import cli
import sys
(file_path, lines) = write_python_file(tmpdir, 'from sys import pid, path')
sys.argv = sys.argv[:2]
try:
assert isinstance(cli.main(), int)
except SystemExit:
pass