forked from pantsbuild/pants
/
test_checkstyle.py
254 lines (223 loc) · 11.2 KB
/
test_checkstyle.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
# coding=utf-8
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import absolute_import, division, print_function, unicode_literals
import os
import sys
from builtins import str
from contextlib import contextmanager
from textwrap import dedent
from pants.backend.python.subsystems.python_repos import PythonRepos
from pants.backend.python.subsystems.python_setup import PythonSetup
from pants.backend.python.targets.python_library import PythonLibrary
from pants.base.build_environment import get_buildroot
from pants.base.exceptions import TaskError
from pants.util.contextutil import environment_as
from pants.util.dirutil import safe_mkdtemp, safe_rmtree
from pants.util.process_handler import subprocess
from pants_test.backend.python.tasks.python_task_test_base import PythonTaskTestBase
from parameterized import parameterized
from pex.interpreter import PythonInterpreter
from wheel.install import WheelFile
from pants.contrib.python.checks.tasks.checkstyle.checkstyle import Checkstyle
CHECKER_RESOLVE_METHOD = [('sys.path', True), ('resolve', False)]
class CheckstyleTest(PythonTaskTestBase):
@staticmethod
def build_checker_wheel(root_dir):
target = Checkstyle._CHECKER_ADDRESS_SPEC
subprocess.check_call([os.path.join(get_buildroot(), 'pants'),
'--pants-distdir={}'.format(root_dir),
'setup-py',
'--run=bdist_wheel --universal',
target])
for root, _, files in os.walk(root_dir):
for f in files:
if f.endswith('.whl'):
return os.path.join(root, f)
raise AssertionError('Failed to generate a wheel for {}'.format(target))
@staticmethod
def install_wheel(wheel, root_dir):
importable_path = os.path.join(root_dir, 'install', os.path.basename(wheel))
overrides = {path: importable_path
for path in ('purelib', 'platlib', 'headers', 'scripts', 'data')}
WheelFile(wheel).install(force=True, overrides=overrides)
return importable_path
_distdir = None
_checker_dist = None
_checker_dist_importable_path = None
@classmethod
def setUpClass(cls):
cls._distdir = safe_mkdtemp()
cls._checker_dist = cls.build_checker_wheel(cls._distdir)
cls._checker_dist_importable_path = cls.install_wheel(cls._checker_dist, cls._distdir)
@classmethod
def tearDownClass(cls):
if cls._distdir:
safe_rmtree(cls._distdir)
@classmethod
def task_type(cls):
return Checkstyle
@contextmanager
def resolve_configuration(self, resolve_local=False):
if resolve_local:
# Ensure our checkstyle task runs under the same interpreter we are running under so that
# local resolves find dists compatible with the current interpreter.
current_interpreter = PythonInterpreter.get()
constraint = '{}=={}'.format(current_interpreter.identity.interpreter,
current_interpreter.identity.version_str)
self.set_options_for_scope(PythonSetup.options_scope, interpreter_constraints=[constraint])
prior = sys.path[:]
sys.path.append(self._checker_dist_importable_path)
try:
yield
finally:
sys.path = prior
else:
self.set_options_for_scope(PythonRepos.options_scope,
repos=[os.path.dirname(self._checker_dist)])
self.set_options_for_scope(PythonSetup.options_scope,
resolver_allow_prereleases=True)
yield
def execute_task(self, target_roots=None, resolve_local=False):
with self.resolve_configuration(resolve_local=resolve_local):
with environment_as(PANTS_DEV=None, PEX_VERBOSE='9'):
context = self.context(target_roots=target_roots)
return self.create_task(context).execute()
@parameterized.expand(CHECKER_RESOLVE_METHOD)
def test_no_sources(self, unused_test_name, resolve_local):
self.assertEqual(0, self.execute_task(resolve_local=resolve_local))
@parameterized.expand(CHECKER_RESOLVE_METHOD)
def test_pass(self, unused_test_name, resolve_local):
self.create_file('a/python/pass.py', contents=dedent("""
class UpperCase(object):
pass
"""))
target = self.make_target('a/python:pass', PythonLibrary, sources=['pass.py'])
self.assertEqual(0, self.execute_task(target_roots=[target], resolve_local=resolve_local))
@parameterized.expand(CHECKER_RESOLVE_METHOD)
def test_failure(self, unused_test_name, resolve_local):
self.create_file('a/python/fail.py', contents=dedent("""
class lower_case(object):
pass
"""))
target = self.make_target('a/python:fail', PythonLibrary, sources=['fail.py'])
with self.assertRaises(TaskError) as task_error:
self.execute_task(target_roots=[target], resolve_local=resolve_local)
self.assertIn('1 Python Style issues found', str(task_error.exception))
@parameterized.expand(CHECKER_RESOLVE_METHOD)
def test_suppressed_file_passes(self, unused_test_name, resolve_local):
self.create_file('a/python/fail.py', contents=dedent("""
class lower_case(object):
pass
"""))
suppression_file = self.create_file('suppress.txt', contents=dedent("""
a/python/fail\.py::variable-names"""))
target = self.make_target('a/python:fail', PythonLibrary, sources=['fail.py'])
self.set_options(suppress=suppression_file)
self.assertEqual(0, self.execute_task(target_roots=[target], resolve_local=resolve_local))
@parameterized.expand(CHECKER_RESOLVE_METHOD)
def test_failure_fail_false(self, unused_test_name, resolve_local):
self.create_file('a/python/fail.py', contents=dedent("""
class lower_case(object):
pass
"""))
target = self.make_target('a/python:fail', PythonLibrary, sources=['fail.py'])
self.set_options(fail=False)
self.assertEqual(1, self.execute_task(target_roots=[target], resolve_local=resolve_local))
@parameterized.expand(CHECKER_RESOLVE_METHOD)
def test_syntax_error(self, unused_test_name, resolve_local):
self.create_file('a/python/error.py', contents=dedent("""
invalid python
"""))
target = self.make_target('a/python:error', PythonLibrary, sources=['error.py'])
self.set_options(fail=False)
self.assertEqual(1, self.execute_task(target_roots=[target], resolve_local=resolve_local))
# NB: the whitelist tests below have the following lint errors in their respective test files:
# fail_py2.py: 4 lint errrors
# fail_py3.py: 3 lint errors
def test_lint_runs_for_blanket_whitelist(self):
self.create_file('a/python/fail_py2.py', contents=dedent("""
x=2+3
print x+7
"""))
target_py2 = self.make_target('a/python:fail2', PythonLibrary, sources=['fail_py2.py'],
compatibility=['CPython>=2.7,<3'])
self.create_file('a/python/fail_py3.py', contents=dedent("""
x=2+3
print(x+7)
"""))
target_py3 = self.make_target('a/python:fail3', PythonLibrary, sources=['fail_py3.py'],
compatibility=['CPython>=3.6'])
self.set_options(interpreter_constraints_whitelist=[])
with self.assertRaises(TaskError) as task_error:
self.execute_task(target_roots=[target_py2, target_py3])
self.assertIn('7 Python Style issues found', str(task_error.exception))
def test_lint_runs_for_default_constraints_only(self):
self.create_file('a/python/fail_py2.py', contents=dedent("""
x=2+3
print x+7
"""))
target_py2 = self.make_target('a/python:fail2', PythonLibrary, sources=['fail_py2.py'],
compatibility=['CPython>=2.7,<3'])
self.create_file('a/python/fail_py3.py', contents=dedent("""
x=2+3
print(x+7)
"""))
target_py3 = self.make_target('a/python:fail3', PythonLibrary, sources=['fail_py3.py'],
compatibility=['CPython>=3.6'])
with self.assertRaises(TaskError) as task_error:
self.execute_task(target_roots=[target_py2, target_py3])
self.assertIn('4 Python Style issues found', str(task_error.exception))
def test_lint_ignores_unwhitelisted_constraints(self):
self.create_file('a/python/fail_py3.py', contents=dedent("""
x=2+3
print(x+7)
"""))
target_py3 = self.make_target('a/python:fail3', PythonLibrary, sources=['fail_py3.py'],
compatibility=['CPython>=3.6'])
self.assertEqual(0, self.execute_task(target_roots=[target_py3]))
def test_lint_runs_for_single_whitelisted_constraints(self):
self.create_file('a/python/fail_py3.py', contents=dedent("""
x=2+3
print(x+7)
"""))
target_py3 = self.make_target('a/python:fail3', PythonLibrary, sources=['fail_py3.py'],
compatibility=['CPython>=3.6'])
self.set_options(interpreter_constraints_whitelist=["CPython>=3.6"])
with self.assertRaises(TaskError) as task_error:
self.execute_task(target_roots=[target_py3])
self.assertIn('3 Python Style issues found', str(task_error.exception))
def test_lint_runs_for_multiple_whitelisted_constraints(self):
self.create_file('a/python/fail_py2.py', contents=dedent("""
x=2+3
print x+7
"""))
target_py2 = self.make_target('a/python:fail2', PythonLibrary, sources=['fail_py2.py'],
compatibility=['CPython>=2.6'])
self.create_file('a/python/fail_py3.py', contents=dedent("""
x=2+3
print(x+7)
"""))
target_py3 = self.make_target('a/python:fail3', PythonLibrary, sources=['fail_py3.py'],
compatibility=['CPython>=3.6'])
self.set_options(interpreter_constraints_whitelist=["CPython>=2.6", "CPython>=3.6"])
with self.assertRaises(TaskError) as task_error:
self.execute_task(target_roots=[target_py2, target_py3])
self.assertIn('7 Python Style issues found', str(task_error.exception))
def test_lint_runs_for_default_constraints_and_matching_whitelist(self):
self.create_file('a/python/fail_py2.py', contents=dedent("""
x=2+3
print x+7
"""))
target_py2 = self.make_target('a/python:fail2', PythonLibrary, sources=['fail_py2.py'],
compatibility=['CPython>=2.7,<3'])
self.create_file('a/python/fail_py3.py', contents=dedent("""
x=2+3
print(x+7)
"""))
target_py3 = self.make_target('a/python:fail3', PythonLibrary, sources=['fail_py3.py'],
compatibility=['CPython>=3.6'])
self.set_options(interpreter_constraints_whitelist=["CPython>=3.6"])
with self.assertRaises(TaskError) as task_error:
self.execute_task(target_roots=[target_py2, target_py3])
self.assertIn('7 Python Style issues found', str(task_error.exception))