forked from gforcada/flake8-isort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_tests.py
317 lines (285 loc) · 11.4 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
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
# -*- coding: utf-8 -*-
from flake8.main import application
from flake8_isort import Flake8Isort
from tempfile import mkdtemp
from testfixtures import OutputCapture
import collections
import os
import unittest
class TestFlake8Isort(unittest.TestCase):
def _given_a_file_in_test_dir(self, contents, isort_config):
test_dir = os.path.realpath(mkdtemp())
file_path = os.path.join(test_dir, 'test.py')
isort_path = os.path.join(test_dir, '.isort.cfg')
with open(file_path, 'w') as a_file:
a_file.write(contents)
with open(isort_path, 'w') as a_file:
a_file.write('[settings]\n')
a_file.write(isort_config)
return (file_path, contents)
def test_sorted_correctly_alpha(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'from sys import path\n'
'\n'
'import os\n',
isort_config='force_single_line=True\nforce_alphabetical_sort=True'
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(ret, [])
def test_sorted_correctly_default(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'import os\n'
'from sys import path\n',
isort_config=''
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(ret, [])
def test_with_eof_blank_lines(self):
"""Pass with eof blank line as flake8 will flag them"""
(file_path, lines) = self._given_a_file_in_test_dir(
'import os\n'
'from sys import path\n'
'\n'
'\n'
' \n',
isort_config=''
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(ret, [])
def test_imports_requires_blank_line(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'from __future__ import division\n'
'import threading\n'
'from sys import pid\n',
isort_config=''
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 2)
self.assertEqual(ret[0][1], 0)
self.assertTrue(ret[0][2].startswith('I003 '))
def test_isortcfg_skip_file(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'skipped_file',
isort_config='skip=test.py'
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(ret, [])
def test_file_skipped_with_comment(self):
# Note: files skipped in this way are not marked as
# "skipped" by isort <= 4.2.15, so we handle them in a
# different code path and test to ensure they also work.
(file_path, lines) = self._given_a_file_in_test_dir(
'# isort:skip_file',
isort_config=''
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(ret, [])
def test_imports_unexpected_blank_line(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'from __future__ import division\n'
'\n'
'import threading\n'
'\n'
'from sys import pid\n',
isort_config=''
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 4)
self.assertEqual(ret[0][1], 0)
self.assertTrue(ret[0][2].startswith('I004 '))
def test_sorted_incorrectly_multiple(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'from __future__ import division\n'
'import os\n'
'from sys import pid\n'
'import threading\n'
'\n'
'import isort\n'
'\n\n\n'
'def func()\n',
isort_config=''
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(len(ret), 3)
self.assertEqual(ret[0][0], 2)
self.assertEqual(ret[0][1], 0)
self.assertTrue(ret[0][2].startswith('I003 '))
self.assertEqual(ret[1][0], 4)
self.assertEqual(ret[1][1], 0)
self.assertTrue(ret[1][2].startswith('I001 '))
self.assertEqual(ret[2][0], 9)
self.assertEqual(ret[2][1], 0)
self.assertTrue(ret[2][2].startswith('I004 '))
def test_sorted_incorrectly(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'from sys import pid\n'
'import threading',
isort_config='force_single_line=True'
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 2)
self.assertEqual(ret[0][1], 0)
self.assertTrue(ret[0][2].startswith('I001 '))
def test_empty_file(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'\n\n',
isort_config=''
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(ret, [])
def test_wrapped_imports(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'from deluge.common import (fdate, fpcnt, fpeer, fsize, fspeed,\n'
' ftime, get_path_size, is_infohash,\n'
' is_ip, is_magnet, is_url)\n',
isort_config='wrap_length=65'
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(ret, [])
def test_force_single_line_imports(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'from plone.app.testing import applyProfile\n'
'from plone.app.testing import FunctionalTesting\n',
isort_config='force_alphabetical_sort=True\nforce_single_line=True'
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(ret, [])
def test_missing_add_imports(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'import os',
isort_config='add_imports=from __future__ import unicode_literals'
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(len(ret), 2)
self.assertEqual(ret[0][0], 1)
self.assertEqual(ret[0][1], 0)
self.assertTrue(ret[0][2].startswith('I005 '))
self.assertEqual(ret[1][0], 1)
self.assertEqual(ret[1][1], 0)
self.assertTrue(ret[1][2].startswith('I003 '))
def test_isortcfg_found(self):
# _given_a_file_in_test_dir already creates an .isort.cfg file
(file_path, lines) = self._given_a_file_in_test_dir(
'from sys import pid\n'
'import threading',
isort_config='force_single_line=True'
)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
checker.config_file = True
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 2)
self.assertEqual(ret[0][1], 0)
self.assertTrue(ret[0][2].startswith('I001 '))
def test_isortcfg_not_found(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'from sys import pid\n'
'import threading',
isort_config='force_single_line=True'
)
# remove the .isort.cfg file
isortcfg_path = file_path.split('/')[: -1]
isortcfg_path = '{0}/.isort.cfg'.format('/'.join(isortcfg_path))
os.remove(isortcfg_path)
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
checker.search_current = False
checker.config_file = True
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 0)
self.assertEqual(ret[0][1], 0)
self.assertTrue(ret[0][2].startswith('I002 '))
def test_default_option(self):
"""By default a config file (.isort.cfg) is expected"""
(file_path, lines) = self._given_a_file_in_test_dir(
'from sys import pid\n'
'import threading\n',
isort_config='force_single_line=True'
)
with OutputCapture():
app = application.Application()
app.run([file_path, ])
self.assertTrue(Flake8Isort.config_file)
def test_config_file(self):
"""Check that one can force to not look for a config file"""
(file_path, lines) = self._given_a_file_in_test_dir(
'from sys import pid\n'
'import threading\n',
isort_config='force_single_line=True'
)
with OutputCapture():
app = application.Application()
app.run(['--no-isort-config', file_path, ])
self.assertFalse(Flake8Isort.config_file)
def test_isort_formatted_output(self):
options = collections.namedtuple(
'Options', [
'no_isort_config',
'isort_show_traceback',
'stdin_display_name'
]
)
(file_path, lines) = self._given_a_file_in_test_dir(
'from __future__ import division\n'
'import os\n'
'from sys import pid\n',
isort_config=''
)
diff = ' from __future__ import division\n+\n import os'
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
checker.parse_options(options(None, True, 'stdin'))
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 2)
self.assertEqual(ret[0][1], 0)
self.assertIn(diff, ret[0][2])
def test_isort_uses_pyproject_toml_if_available(self):
(file_path, lines) = self._given_a_file_in_test_dir(
'import os\n'
'from sys import path\n',
isort_config=''
)
pyproject_toml_path = os.path.join(
os.path.dirname(file_path), 'pyproject.toml',
)
with open(pyproject_toml_path, 'w') as f:
f.write('[tool.isort]\nlines_between_types=1')
with OutputCapture():
checker = Flake8Isort(None, file_path, lines)
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 2)
self.assertEqual(ret[0][1], 0)
self.assertTrue(ret[0][2].startswith('I003 '))