forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_test.py
206 lines (180 loc) · 7.18 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
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from textwrap import dedent
from pants.backend.python.dependency_inference import module_mapper
from pants.backend.python.dependency_inference.rules import (
InferConftestDependencies,
InferInitDependencies,
InferPythonDependencies,
PythonInference,
infer_python_conftest_dependencies,
infer_python_dependencies,
infer_python_init_dependencies,
)
from pants.backend.python.target_types import (
PythonLibrary,
PythonRequirementLibrary,
PythonSources,
PythonTests,
)
from pants.backend.python.util_rules import ancestor_files
from pants.core.util_rules import stripped_source_files
from pants.engine.addresses import Address
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() -> None:
rule_runner = RuleRunner(
rules=[
*stripped_source_files.rules(),
*module_mapper.rules(),
infer_python_dependencies,
SubsystemRule(PythonInference),
QueryRule(InferredDependencies, (InferPythonDependencies,)),
],
target_types=[PythonLibrary, PythonRequirementLibrary],
)
rule_runner.add_to_build_file(
"3rdparty/python",
dedent(
"""\
python_requirement_library(
name='Django',
requirements=['Django==1.21'],
)
"""
),
)
rule_runner.create_file("src/python/str_import/subdir/f.py")
rule_runner.add_to_build_file("src/python/str_import/subdir", "python_library()")
rule_runner.create_file("src/python/util/dep.py")
rule_runner.add_to_build_file("src/python/util", "python_library()")
rule_runner.create_file(
"src/python/app.py",
dedent(
"""\
import django
from util.dep import Demo
from util import dep
"""
),
)
rule_runner.create_file(
"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')
"""
),
)
rule_runner.add_to_build_file("src/python", "python_library()")
def run_dep_inference(
address: Address, *, enable_string_imports: bool = False
) -> InferredDependencies:
args = ["--backend-packages=pants.backend.python", "--source-root-patterns=src/python"]
if enable_string_imports:
args.append("--python-infer-string-imports")
rule_runner.set_options(args)
target = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies,
[InferPythonDependencies(target[PythonSources])],
)
normal_address = Address("src/python")
assert run_dep_inference(normal_address) == InferredDependencies(
[
Address("3rdparty/python", target_name="Django"),
Address("src/python", relative_file_path="app.py"),
Address("src/python/util", relative_file_path="dep.py", target_name="util"),
],
sibling_dependencies_inferrable=True,
)
generated_subtarget_address = Address(
"src/python", relative_file_path="f2.py", target_name="python"
)
assert run_dep_inference(generated_subtarget_address) == InferredDependencies(
[Address("src/python", relative_file_path="app.py", target_name="python")],
sibling_dependencies_inferrable=True,
)
assert run_dep_inference(
generated_subtarget_address, enable_string_imports=True
) == InferredDependencies(
[
Address("src/python", relative_file_path="app.py", target_name="python"),
Address("src/python/str_import/subdir", relative_file_path="f.py"),
],
sibling_dependencies_inferrable=True,
)
def test_infer_python_inits() -> None:
rule_runner = RuleRunner(
rules=[
*ancestor_files.rules(),
infer_python_init_dependencies,
SubsystemRule(PythonInference),
QueryRule(InferredDependencies, (InferInitDependencies,)),
],
target_types=[PythonLibrary],
)
rule_runner.set_options(
[
"--backend-packages=pants.backend.python",
"--python-infer-inits",
"--source-root-patterns=src/python",
]
)
rule_runner.create_file("src/python/root/__init__.py")
rule_runner.add_to_build_file("src/python/root", "python_library()")
rule_runner.create_file("src/python/root/mid/__init__.py")
rule_runner.add_to_build_file("src/python/root/mid", "python_library()")
rule_runner.create_file("src/python/root/mid/leaf/__init__.py")
rule_runner.add_to_build_file("src/python/root/mid/leaf", "python_library()")
def run_dep_inference(address: Address) -> InferredDependencies:
target = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies,
[InferInitDependencies(target[PythonSources])],
)
assert run_dep_inference(Address.parse("src/python/root/mid/leaf")) == InferredDependencies(
[
Address("src/python/root", relative_file_path="__init__.py", target_name="root"),
Address("src/python/root/mid", relative_file_path="__init__.py", target_name="mid"),
],
sibling_dependencies_inferrable=False,
)
def test_infer_python_conftests() -> None:
rule_runner = RuleRunner(
rules=[
*ancestor_files.rules(),
infer_python_conftest_dependencies,
SubsystemRule(PythonInference),
QueryRule(InferredDependencies, (InferConftestDependencies,)),
],
target_types=[PythonTests],
)
rule_runner.set_options(
["--backend-packages=pants.backend.python", "--source-root-patterns=src/python"]
)
rule_runner.create_file("src/python/root/conftest.py")
rule_runner.add_to_build_file("src/python/root", "python_tests()")
rule_runner.create_file("src/python/root/mid/conftest.py")
rule_runner.add_to_build_file("src/python/root/mid", "python_tests()")
rule_runner.create_file("src/python/root/mid/leaf/conftest.py")
rule_runner.create_file("src/python/root/mid/leaf/this_is_a_test.py")
rule_runner.add_to_build_file("src/python/root/mid/leaf", "python_tests()")
def run_dep_inference(address: Address) -> InferredDependencies:
target = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies,
[InferConftestDependencies(target[PythonSources])],
)
assert run_dep_inference(Address.parse("src/python/root/mid/leaf")) == InferredDependencies(
[
Address("src/python/root", relative_file_path="conftest.py", target_name="root"),
Address("src/python/root/mid", relative_file_path="conftest.py", target_name="mid"),
],
sibling_dependencies_inferrable=False,
)