forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_integration_test.py
408 lines (365 loc) · 15 KB
/
rules_integration_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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from pathlib import PurePath
from textwrap import dedent
from typing import List, Optional, Sequence
from pants.backend.python.lint.pylint.plugin_target_type import PylintSourcePlugin
from pants.backend.python.lint.pylint.rules import PylintFieldSet, PylintRequest
from pants.backend.python.lint.pylint.rules import rules as pylint_rules
from pants.backend.python.target_types import PythonLibrary, PythonRequirementLibrary
from pants.core.goals.lint import LintResult, LintResults
from pants.engine.addresses import Address
from pants.engine.fs import FileContent
from pants.engine.rules import RootRule
from pants.engine.target import Target, WrappedTarget
from pants.testutil.engine_util import Params
from pants.testutil.external_tool_test_base import ExternalToolTestBase
from pants.testutil.option_util import create_options_bootstrapper
from pants.testutil.python_interpreter_selection import skip_unless_python27_and_python3_present
# See http://pylint.pycqa.org/en/latest/user_guide/run.html#exit-codes for exit codes.
PYLINT_FAILURE_RETURN_CODE = 16
class PylintIntegrationTest(ExternalToolTestBase):
package = "src/python/project"
good_source = FileContent(f"{package}/good.py", b"'''docstring'''\nUPPERCASE_CONSTANT = ''\n")
bad_source = FileContent(f"{package}/bad.py", b"'''docstring'''\nlowercase_constant = ''\n")
py3_only_source = FileContent(f"{package}/py3.py", b"'''docstring'''\nCONSTANT: str = ''\n")
global_args = (
"--backend-packages=pants.backend.python.lint.pylint",
"--source-root-patterns=['src/python', 'tests/python']",
)
@classmethod
def target_types(cls):
return [PythonLibrary, PythonRequirementLibrary, PylintSourcePlugin]
@classmethod
def rules(cls):
return (*super().rules(), *pylint_rules(), RootRule(PylintRequest))
def make_target(
self,
source_files: List[FileContent],
*,
package: Optional[str] = None,
name: str = "target",
interpreter_constraints: Optional[str] = None,
dependencies: Optional[List[Address]] = None,
) -> Target:
if not package:
package = self.package
for source_file in source_files:
self.create_file(source_file.path, source_file.content.decode())
source_globs = [PurePath(source_file.path).name for source_file in source_files]
self.add_to_build_file(
package,
dedent(
f"""\
python_library(
name={repr(name)},
sources={source_globs},
dependencies={[str(dep) for dep in dependencies or ()]},
compatibility={repr(interpreter_constraints)},
)
"""
),
)
return self.request_product(
WrappedTarget,
Params(
Address(package, target_name=name),
create_options_bootstrapper(args=self.global_args),
),
).target
def run_pylint(
self,
targets: List[Target],
*,
config: Optional[str] = None,
passthrough_args: Optional[str] = None,
skip: bool = False,
additional_args: Optional[List[str]] = None,
) -> Sequence[LintResult]:
args = list(self.global_args)
if config:
self.create_file(relpath="pylintrc", contents=config)
args.append("--pylint-config=pylintrc")
if passthrough_args:
args.append(f"--pylint-args='{passthrough_args}'")
if skip:
args.append("--pylint-skip")
if additional_args:
args.extend(additional_args)
results = self.request_product(
LintResults,
Params(
PylintRequest(PylintFieldSet.create(tgt) for tgt in targets),
create_options_bootstrapper(args=args),
),
)
return results.results
def test_passing_source(self) -> None:
target = self.make_target([self.good_source])
result = self.run_pylint([target])
assert len(result) == 1
assert result[0].exit_code == 0
assert "Your code has been rated at 10.00/10" in result[0].stdout.strip()
def test_failing_source(self) -> None:
target = self.make_target([self.bad_source])
result = self.run_pylint([target])
assert len(result) == 1
assert result[0].exit_code == PYLINT_FAILURE_RETURN_CODE
assert f"{self.package}/bad.py:2:0: C0103" in result[0].stdout
def test_mixed_sources(self) -> None:
target = self.make_target([self.good_source, self.bad_source])
result = self.run_pylint([target])
assert len(result) == 1
assert result[0].exit_code == PYLINT_FAILURE_RETURN_CODE
assert f"{self.package}/good.py" not in result[0].stdout
assert f"{self.package}/bad.py:2:0: C0103" in result[0].stdout
def test_multiple_targets(self) -> None:
targets = [
self.make_target([self.good_source], name="t1"),
self.make_target([self.bad_source], name="t2"),
]
result = self.run_pylint(targets)
assert len(result) == 1
assert result[0].exit_code == PYLINT_FAILURE_RETURN_CODE
assert f"{self.package}/good.py" not in result[0].stdout
assert f"{self.package}/bad.py:2:0: C0103" in result[0].stdout
@skip_unless_python27_and_python3_present
def test_uses_correct_python_version(self) -> None:
py2_args = [
"--pylint-version=pylint<2",
"--pylint-extra-requirements=['setuptools<45', 'isort>=4.3.21,<4.4']",
]
py2_target = self.make_target(
[self.py3_only_source], name="py2", interpreter_constraints="CPython==2.7.*"
)
py2_result = self.run_pylint([py2_target], additional_args=py2_args)
assert len(py2_result) == 1
assert py2_result[0].exit_code == 2
assert "invalid syntax (<string>, line 2) (syntax-error)" in py2_result[0].stdout
py3_target = self.make_target(
[self.py3_only_source],
name="py3",
# NB: Avoid Python 3.8+ for this test due to issues with asteroid/ast.
# See https://github.com/pantsbuild/pants/issues/10547.
interpreter_constraints="CPython>=3.6,<3.8",
)
py3_result = self.run_pylint([py3_target])
assert len(py3_result) == 1
assert py3_result[0].exit_code == 0
assert "Your code has been rated at 10.00/10" in py3_result[0].stdout.strip()
combined_result = self.run_pylint([py2_target, py3_target], additional_args=py2_args)
assert len(combined_result) == 2
batched_py3_result, batched_py2_result = sorted(
combined_result, key=lambda result: result.exit_code
)
assert batched_py2_result.exit_code == 2
assert batched_py2_result.partition_description == "['CPython==2.7.*']"
assert "invalid syntax (<string>, line 2) (syntax-error)" in batched_py2_result.stdout
assert batched_py3_result.exit_code == 0
assert batched_py3_result.partition_description == "['CPython>=3.6,<3.8']"
assert "Your code has been rated at 10.00/10" in batched_py3_result.stdout.strip()
def test_respects_config_file(self) -> None:
target = self.make_target([self.bad_source])
result = self.run_pylint([target], config="[pylint]\ndisable = C0103\n")
assert len(result) == 1
assert result[0].exit_code == 0
assert "Your code has been rated at 10.00/10" in result[0].stdout.strip()
def test_respects_passthrough_args(self) -> None:
target = self.make_target([self.bad_source])
result = self.run_pylint([target], passthrough_args="--disable=C0103")
assert len(result) == 1
assert result[0].exit_code == 0
assert "Your code has been rated at 10.00/10" in result[0].stdout.strip()
def test_includes_direct_dependencies(self) -> None:
self.add_to_build_file(
"",
dedent(
"""\
python_requirement_library(
name='transitive_req',
requirements=['django'],
)
python_requirement_library(
name='direct_req',
requirements=['ansicolors'],
)
"""
),
)
self.add_to_build_file(self.package, "python_library(name='transitive_dep', sources=[])\n")
self.create_file(
f"{self.package}/direct_dep.py",
dedent(
"""\
# No docstring - Pylint doesn't lint dependencies.
from project.transitive_dep import doesnt_matter_if_variable_exists
THIS_VARIABLE_EXISTS = ''
"""
),
)
self.add_to_build_file(
self.package,
dedent(
"""\
python_library(
name='direct_dep',
sources=['direct_dep.py'],
dependencies=[':transitive_dep', '//:transitive_req'],
)
"""
),
)
source_content = dedent(
"""\
'''Pylint will check that variables exist and are used.'''
from colors import green
from project.direct_dep import THIS_VARIABLE_EXISTS
print(green(THIS_VARIABLE_EXISTS))
"""
)
target = self.make_target(
source_files=[FileContent(f"{self.package}/target.py", source_content.encode())],
dependencies=[
Address(self.package, target_name="direct_dep"),
Address("", target_name="direct_req"),
],
)
result = self.run_pylint([target])
assert len(result) == 1
assert result[0].exit_code == 0
assert "Your code has been rated at 10.00/10" in result[0].stdout.strip()
def test_skip(self) -> None:
target = self.make_target([self.bad_source])
result = self.run_pylint([target], skip=True)
assert not result
def test_pep420_namespace_packages(self) -> None:
test_fc = FileContent(
"tests/python/project/good_test.py",
dedent(
"""\
'''Docstring.'''
from project.good import UPPERCASE_CONSTANT
CONSTANT2 = UPPERCASE_CONSTANT
"""
).encode(),
)
targets = [
self.make_target([self.good_source]),
self.make_target(
[test_fc],
package="tests/python/project",
dependencies=[Address.parse(f"{self.package}:target")],
),
]
result = self.run_pylint(targets)
assert len(result) == 1
assert result[0].exit_code == 0
assert "Your code has been rated at 10.00/10" in result[0].stdout.strip()
def test_3rdparty_plugin(self) -> None:
source_content = dedent(
"""\
'''Docstring.'''
import unittest
class PluginTest(unittest.TestCase):
'''Docstring.'''
def test_plugin(self):
'''Docstring.'''
self.assertEqual(True, True)
"""
)
target = self.make_target(
[FileContent(f"{self.package}/thirdparty_plugin.py", source_content.encode())]
)
result = self.run_pylint(
[target],
additional_args=["--pylint-extra-requirements=pylint-unittest>=0.1.3,<0.2"],
passthrough_args="--load-plugins=pylint_unittest",
)
assert len(result) == 1
assert result[0].exit_code == 4
assert f"{self.package}/thirdparty_plugin.py:10:8: W5301" in result[0].stdout
def test_source_plugin(self) -> None:
# NB: We make this source plugin fairly complex by having it use transitive dependencies.
# This is to ensure that we can correctly support plugins with dependencies.
self.add_to_build_file(
"",
dedent(
"""\
python_requirement_library(
name='pylint',
requirements=['pylint>=2.4.4,<2.5'],
)
python_requirement_library(
name='colors',
requirements=['ansicolors'],
)
"""
),
)
self.create_file(
"build-support/plugins/subdir/dep.py",
dedent(
"""\
from colors import red
def is_print(node):
_ = red("Test that transitive deps are loaded.")
return node.func.name == "print"
"""
),
)
self.add_to_build_file(
"build-support/plugins/subdir", "python_library(dependencies=['//:colors'])"
)
self.create_file(
"build-support/plugins/print_plugin.py",
dedent(
"""\
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker
from subdir.dep import is_print
class PrintChecker(BaseChecker):
__implements__ = IAstroidChecker
name = "print_plugin"
msgs = {
"C9871": ("`print` statements are banned", "print-statement-used", ""),
}
def visit_call(self, node):
if is_print(node):
self.add_message("print-statement-used", node=node)
def register(linter):
linter.register_checker(PrintChecker(linter))
"""
),
)
self.add_to_build_file(
"build-support/plugins",
dedent(
"""\
pylint_source_plugin(
name='print_plugin',
sources=['print_plugin.py'],
dependencies=['//:pylint', 'build-support/plugins/subdir'],
)
"""
),
)
config_content = dedent(
"""\
[MASTER]
load-plugins=print_plugin
"""
)
target = self.make_target(
[FileContent(f"{self.package}/source_plugin.py", b"'''Docstring.'''\nprint()\n")]
)
result = self.run_pylint(
[target],
additional_args=[
"--pylint-source-plugins=['build-support/plugins:print_plugin']",
f"--source-root-patterns=['build-support/plugins', '{self.package}']",
],
config=config_content,
)
assert len(result) == 1
assert result[0].exit_code == PYLINT_FAILURE_RETURN_CODE
assert f"{self.package}/source_plugin.py:2:0: C9871" in result[0].stdout