forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_test.py
111 lines (97 loc) · 4 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
# 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.module_mapper import rules as module_mapper_rules
from pants.backend.python.dependency_inference.rules import (
InferPythonDependencies,
infer_python_dependencies,
)
from pants.backend.python.target_types import PythonLibrary, PythonRequirementLibrary, PythonSources
from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.core.util_rules import strip_source_roots
from pants.engine.addresses import Address
from pants.engine.rules import RootRule
from pants.engine.selectors import Params
from pants.engine.target import InferredDependencies, WrappedTarget
from pants.python.python_requirement import PythonRequirement
from pants.source.source_root import all_roots
from pants.testutil.option.util import create_options_bootstrapper
from pants.testutil.test_base import TestBase
class PythonDependencyInferenceTest(TestBase):
@classmethod
def alias_groups(cls) -> BuildFileAliases:
return BuildFileAliases(objects={"python_requirement": PythonRequirement})
@classmethod
def rules(cls):
return (
*super().rules(),
*strip_source_roots.rules(),
infer_python_dependencies,
*module_mapper_rules(),
all_roots,
RootRule(InferPythonDependencies),
)
@classmethod
def target_types(cls):
return [PythonLibrary, PythonRequirementLibrary]
def test_infer_python_dependencies(self) -> None:
options_bootstrapper = create_options_bootstrapper(
args=["--source-root-patterns=src/python"]
)
self.add_to_build_file(
"3rdparty/python",
dedent(
"""\
python_requirement_library(
name='Django',
requirements=[python_requirement('Django==1.21')],
)
"""
),
)
self.create_file("src/python/no_owner/f.py")
self.add_to_build_file("src/python/no_owner", "python_library()")
self.create_file("src/python/util/dep.py")
self.add_to_build_file("src/python/util", "python_library()")
self.create_file(
"src/python/app.py",
dedent(
"""\
import django
from util.dep import Demo
from util import dep
"""
),
)
self.create_file(
"src/python/f2.py",
dedent(
"""\
import typing
# Import from another file in the same target.
from app import main
"""
),
)
self.add_to_build_file("src/python", "python_library()")
def run_dep_inference(address: Address) -> InferredDependencies:
target = self.request_single_product(WrappedTarget, address).target
return self.request_single_product(
InferredDependencies,
Params(InferPythonDependencies(target[PythonSources]), options_bootstrapper),
)
# NB: We do not infer `src/python/app.py`, even though it's used by `src/python/f2.py`,
# because it is part of the requested address.
normal_address = Address("src/python", "python")
assert run_dep_inference(normal_address) == InferredDependencies(
[
Address("3rdparty/python", "Django"),
Address("src/python/util", target_name="dep.py", generated_base_target_name="util"),
]
)
generated_subtarget_address = Address(
"src/python", target_name="f2.py", generated_base_target_name="python"
)
assert run_dep_inference(generated_subtarget_address) == InferredDependencies(
[Address("src/python", target_name="app.py", generated_base_target_name="python")]
)