forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_test.py
369 lines (337 loc) · 13.6 KB
/
rules_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
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from textwrap import dedent
import pytest
from pants.backend.python import target_types_rules
from pants.backend.python.dependency_inference.rules import (
InferConftestDependencies,
InferInitDependencies,
InferPythonImportDependencies,
PythonInferSubsystem,
UnownedDependencyError,
UnownedDependencyUsage,
import_rules,
infer_python_conftest_dependencies,
infer_python_init_dependencies,
)
from pants.backend.python.macros import python_requirements
from pants.backend.python.macros.python_requirements import PythonRequirementsTargetGenerator
from pants.backend.python.target_types import (
PythonRequirementsFileTarget,
PythonRequirementTarget,
PythonSourceField,
PythonSourcesGeneratorTarget,
PythonTestsGeneratorTarget,
PythonTestUtilsGeneratorTarget,
)
from pants.backend.python.util_rules import ancestor_files
from pants.engine.addresses import Address
from pants.engine.internals.scheduler import ExecutionError
from pants.engine.rules import SubsystemRule
from pants.engine.target import InferredDependencies
from pants.testutil.rule_runner import QueryRule, RuleRunner
def test_infer_python_imports(caplog) -> None:
rule_runner = RuleRunner(
rules=[
*import_rules(),
*target_types_rules.rules(),
QueryRule(InferredDependencies, [InferPythonImportDependencies]),
],
target_types=[PythonSourcesGeneratorTarget, PythonRequirementTarget],
)
rule_runner.write_files(
{
"3rdparty/python/BUILD": dedent(
"""\
python_requirement(
name='Django',
requirements=['Django==1.21'],
)
"""
),
# If there's a `.py` and `.pyi` file for the same module, we should infer a dependency on both.
"src/python/str_import/subdir/f.py": "",
"src/python/str_import/subdir/f.pyi": "",
"src/python/str_import/subdir/BUILD": "python_sources()",
"src/python/util/dep.py": "",
"src/python/util/BUILD": "python_sources()",
"src/python/app.py": dedent(
"""\
import django
import unrecognized.module
from util.dep import Demo
from util import dep
"""
),
"src/python/f2.py": dedent(
"""\
import typing
# Import from another file in the same target.
from app import main
# Dynamic string import.
importlib.import_module('str_import.subdir.f')
"""
),
"src/python/BUILD": "python_sources()",
}
)
def run_dep_inference(
address: Address, *, enable_string_imports: bool = False
) -> InferredDependencies:
args = ["--source-root-patterns=src/python"]
if enable_string_imports:
args.append("--python-infer-string-imports")
rule_runner.set_options(args, env_inherit={"PATH", "PYENV_ROOT", "HOME"})
target = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies, [InferPythonImportDependencies(target[PythonSourceField])]
)
assert run_dep_inference(
Address("src/python", relative_file_path="app.py")
) == InferredDependencies(
[
Address("3rdparty/python", target_name="Django"),
Address("src/python/util", relative_file_path="dep.py"),
],
)
addr = Address("src/python", relative_file_path="f2.py")
assert run_dep_inference(addr) == InferredDependencies(
[Address("src/python", relative_file_path="app.py")]
)
assert run_dep_inference(addr, enable_string_imports=True) == InferredDependencies(
[
Address("src/python", relative_file_path="app.py"),
Address("src/python/str_import/subdir", relative_file_path="f.py"),
Address("src/python/str_import/subdir", relative_file_path="f.pyi"),
],
)
# Test handling of ambiguous imports. We should warn on the ambiguous dependency, but not warn
# on the disambiguated one and should infer a dep.
caplog.clear()
rule_runner.write_files(
{
"src/python/ambiguous/dep.py": "",
"src/python/ambiguous/disambiguated_via_ignores.py": "",
"src/python/ambiguous/main.py": (
"import ambiguous.dep\nimport ambiguous.disambiguated_via_ignores\n"
),
"src/python/ambiguous/BUILD": dedent(
"""\
python_sources(name='dep1', sources=['dep.py', 'disambiguated_via_ignores.py'])
python_sources(name='dep2', sources=['dep.py', 'disambiguated_via_ignores.py'])
python_sources(
name='main',
sources=['main.py'],
dependencies=['!./disambiguated_via_ignores.py:dep2'],
)
"""
),
}
)
assert run_dep_inference(
Address("src/python/ambiguous", target_name="main", relative_file_path="main.py")
) == InferredDependencies(
[
Address(
"src/python/ambiguous",
target_name="dep1",
relative_file_path="disambiguated_via_ignores.py",
)
],
)
assert len(caplog.records) == 1
assert "The target src/python/ambiguous/main.py:main imports `ambiguous.dep`" in caplog.text
assert "['src/python/ambiguous/dep.py:dep1', 'src/python/ambiguous/dep.py:dep2']" in caplog.text
assert "disambiguated_via_ignores.py" not in caplog.text
def test_infer_python_inits() -> None:
rule_runner = RuleRunner(
rules=[
*ancestor_files.rules(),
*target_types_rules.rules(),
infer_python_init_dependencies,
SubsystemRule(PythonInferSubsystem),
QueryRule(InferredDependencies, (InferInitDependencies,)),
],
target_types=[PythonSourcesGeneratorTarget],
)
rule_runner.set_options(
["--python-infer-inits", "--source-root-patterns=src/python"],
env_inherit={"PATH", "PYENV_ROOT", "HOME"},
)
rule_runner.write_files(
{
"src/python/root/__init__.py": "",
"src/python/root/BUILD": "python_sources()",
"src/python/root/mid/__init__.py": "",
"src/python/root/mid/BUILD": "python_sources()",
"src/python/root/mid/leaf/__init__.py": "",
"src/python/root/mid/leaf/f.py": "",
"src/python/root/mid/leaf/BUILD": "python_sources()",
"src/python/type_stub/__init__.pyi": "",
"src/python/type_stub/foo.pyi": "",
"src/python/type_stub/BUILD": "python_sources()",
}
)
def run_dep_inference(address: Address) -> InferredDependencies:
target = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies,
[InferInitDependencies(target[PythonSourceField])],
)
assert run_dep_inference(
Address("src/python/root/mid/leaf", relative_file_path="f.py")
) == InferredDependencies(
[
Address("src/python/root", relative_file_path="__init__.py"),
Address("src/python/root/mid", relative_file_path="__init__.py"),
Address("src/python/root/mid/leaf", relative_file_path="__init__.py"),
],
)
assert run_dep_inference(
Address("src/python/type_stub", relative_file_path="foo.pyi")
) == InferredDependencies([Address("src/python/type_stub", relative_file_path="__init__.pyi")])
def test_infer_python_conftests() -> None:
rule_runner = RuleRunner(
rules=[
*ancestor_files.rules(),
*target_types_rules.rules(),
infer_python_conftest_dependencies,
SubsystemRule(PythonInferSubsystem),
QueryRule(InferredDependencies, (InferConftestDependencies,)),
],
target_types=[PythonTestsGeneratorTarget, PythonTestUtilsGeneratorTarget],
)
rule_runner.set_options(
["--source-root-patterns=src/python"],
env_inherit={"PATH", "PYENV_ROOT", "HOME"},
)
rule_runner.write_files(
{
"src/python/root/conftest.py": "",
"src/python/root/BUILD": "python_test_utils()",
"src/python/root/mid/conftest.py": "",
"src/python/root/mid/BUILD": "python_test_utils()",
"src/python/root/mid/leaf/conftest.py": "",
"src/python/root/mid/leaf/this_is_a_test.py": "",
"src/python/root/mid/leaf/BUILD": "python_test_utils()\npython_tests(name='tests')",
}
)
def run_dep_inference(address: Address) -> InferredDependencies:
target = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies,
[InferConftestDependencies(target[PythonSourceField])],
)
assert run_dep_inference(
Address(
"src/python/root/mid/leaf", target_name="tests", relative_file_path="this_is_a_test.py"
)
) == InferredDependencies(
[
Address("src/python/root", relative_file_path="conftest.py"),
Address("src/python/root/mid", relative_file_path="conftest.py"),
Address("src/python/root/mid/leaf", relative_file_path="conftest.py"),
],
)
def test_infer_python_strict(caplog) -> None:
rule_runner = RuleRunner(
rules=[
*import_rules(),
*target_types_rules.rules(),
*python_requirements.rules(),
QueryRule(InferredDependencies, [InferPythonImportDependencies]),
],
target_types=[
PythonSourcesGeneratorTarget,
PythonRequirementTarget,
PythonRequirementsFileTarget,
PythonRequirementsTargetGenerator,
],
)
rule_runner.write_files(
{
"src/python/cheesey.py": dedent(
"""\
import venezuelan_beaver_cheese
"japanese.sage.derby"
"""
),
"src/python/BUILD": "python_sources()",
}
)
def run_dep_inference(
address: Address,
unowned_dependency_behavior: str,
) -> InferredDependencies:
rule_runner.set_options(
[
f"--python-infer-unowned-dependency-behavior={unowned_dependency_behavior}",
"--python-infer-string-imports",
"--source-root-patterns=src/python",
],
env_inherit={"PATH", "PYENV_ROOT", "HOME"},
)
target = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies,
[InferPythonImportDependencies(target[PythonSourceField])],
)
# First test with "warning"
run_dep_inference(Address("src/python", relative_file_path="cheesey.py"), "warning")
assert len(caplog.records) == 1
assert "The following imports in src/python/cheesey.py have no owners:" in caplog.text
assert " * venezuelan_beaver_cheese (src/python/cheesey.py:1)" in caplog.text
assert "japanese.sage.derby" not in caplog.text
# Now test with "error"
caplog.clear()
with pytest.raises(ExecutionError) as exc_info:
run_dep_inference(Address("src/python", relative_file_path="cheesey.py"), "error")
assert isinstance(exc_info.value.wrapped_exceptions[0], UnownedDependencyError)
assert len(caplog.records) == 2 # one for the error being raised and one for our message
assert "The following imports in src/python/cheesey.py have no owners:" in caplog.text
assert " * venezuelan_beaver_cheese (src/python/cheesey.py:1)" in caplog.text
assert "japanese.sage.derby" not in caplog.text
caplog.clear()
# All modes should be fine if the module is explicitly declared as a requirement
rule_runner.write_files(
{
"src/python/BUILD": dedent(
"""\
python_requirement(
name="venezuelan_beaver_cheese",
modules=["venezuelan_beaver_cheese"],
requirements=["venezuelan_beaver_cheese==1.0.0"],
)
python_sources(dependencies=[":venezuelan_beaver_cheese"])
"""
),
}
)
for mode in UnownedDependencyUsage:
run_dep_inference(Address("src/python", relative_file_path="cheesey.py"), mode.value)
assert not caplog.records
# All modes should be fine if the module is implictly found via requirements.txt
rule_runner.write_files(
{
"src/python/requirements.txt": "venezuelan_beaver_cheese==1.0.0",
"src/python/BUILD": dedent(
"""\
python_requirements(name='reqs')
python_sources()
"""
),
}
)
for mode in UnownedDependencyUsage:
run_dep_inference(Address("src/python", relative_file_path="cheesey.py"), mode.value)
assert not caplog.records
# All modes should be fine if the module is owned by a first party
rule_runner.write_files(
{
"src/python/venezuelan_beaver_cheese.py": "",
"src/python/BUILD": "python_sources()",
}
)
for mode in UnownedDependencyUsage:
run_dep_inference(Address("src/python", relative_file_path="cheesey.py"), mode.value)
assert not caplog.records