Skip to content

Commit 0efe93d

Browse files
committed
Add test for packs_glob target dep inference
1 parent 6ffb7db commit 0efe93d

File tree

2 files changed

+122
-10
lines changed

2 files changed

+122
-10
lines changed

pants-plugins/pack_metadata/target_types_rules.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
# repurposed from pants.backend.python.target_types_rules
1514
import os
1615
from dataclasses import dataclass
1716

@@ -65,17 +64,14 @@ async def infer_packs_globs_dependencies(
6564
),
6665
)
6766

68-
# explicitly_provided_deps.includes: FrozenOrderedSet[Address]
69-
# explicitly_provided_deps.ignores: FrozenOrderedSet[Address]
67+
implicit_packs_deps = {Address(pack) for pack in paths.dirs}
7068

71-
implicit_packs = {Address(f"{pack}/") for pack in paths.dirs}
72-
73-
inferred_deps = (
74-
implicit_packs
75-
- explicitly_provided_deps.ignores
76-
- explicitly_provided_deps.includes
69+
inferred_packs_deps = (
70+
implicit_packs_deps
71+
- explicitly_provided_deps.ignores # FrozenOrderedSet[Address]
72+
- explicitly_provided_deps.includes # FrozenOrderedSet[Address]
7773
)
78-
return InferredDependencies(inferred_deps)
74+
return InferredDependencies(inferred_packs_deps)
7975

8076

8177
def rules():
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright 2023 The StackStorm Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
from textwrap import dedent
17+
18+
from pants.backend.python.target_types import (
19+
PythonSourceTarget,
20+
PythonSourcesGeneratorTarget,
21+
)
22+
from pants.backend.python.target_types_rules import rules as python_target_types_rules
23+
from pants.engine.addresses import Address
24+
from pants.engine.target import InferredDependencies
25+
from pants.testutil.rule_runner import QueryRule, RuleRunner
26+
27+
from .target_types_rules import (
28+
InferPacksGlobDependencies,
29+
PacksGlobInferenceFieldSet,
30+
rules as pack_metadata_target_types_rules,
31+
)
32+
from .target_types import PacksGlob
33+
34+
35+
def test_infer_packs_globs_dependencies() -> None:
36+
rule_runner = RuleRunner(
37+
rules=[
38+
*python_target_types_rules(),
39+
*pack_metadata_target_types_rules(),
40+
QueryRule(InferredDependencies, (InferPacksGlobDependencies,)),
41+
],
42+
target_types=[
43+
PythonSourceTarget,
44+
PythonSourcesGeneratorTarget,
45+
PacksGlob,
46+
],
47+
)
48+
rule_runner.write_files(
49+
{
50+
"packs/BUILD": dedent(
51+
"""\
52+
packs_glob(
53+
name="all_packs_glob",
54+
dependencies=[
55+
"!./configs", # explicit ignore
56+
"./a", # explicit include
57+
],
58+
)
59+
"""
60+
),
61+
"packs/a/BUILD": "python_sources()",
62+
"packs/a/__init__.py": "",
63+
"packs/a/fixture.py": "",
64+
"packs/b/BUILD": dedent(
65+
"""\
66+
python_sources(
67+
dependencies=["packs/configs/b.yaml"],
68+
)
69+
"""
70+
),
71+
"packs/b/__init__.py": "",
72+
"packs/b/fixture.py": "",
73+
"packs/c/BUILD": "python_sources()",
74+
"packs/c/__init__.py": "",
75+
"packs/c/fixture.py": "",
76+
"packs/d/BUILD": "python_sources()",
77+
"packs/d/__init__.py": "",
78+
"packs/d/fixture.py": "",
79+
"packs/configs/BUILD": dedent(
80+
"""\
81+
resources(
82+
sources=["*.yaml"],
83+
)
84+
"""
85+
),
86+
"packs/configs/b.yaml": dedent(
87+
"""\
88+
---
89+
# pack config for pack b
90+
"""
91+
),
92+
}
93+
)
94+
95+
def run_dep_inference(address: Address) -> InferredDependencies:
96+
args = [
97+
"--source-root-patterns=/packs",
98+
]
99+
rule_runner.set_options(args, env_inherit={"PATH", "PYENV_ROOT", "HOME"})
100+
target = rule_runner.get_target(address)
101+
return rule_runner.request(
102+
InferredDependencies,
103+
[InferPacksGlobDependencies(PacksGlobInferenceFieldSet.create(target))],
104+
)
105+
106+
assert run_dep_inference(
107+
Address("packs", target_name="all_packs_glob")
108+
) == InferredDependencies(
109+
[
110+
# should not have packs/a (explicit dep does not need to be inferred)
111+
# should not have packs/configs (explicitly ignored)
112+
Address("packs/b"),
113+
Address("packs/c"),
114+
Address("packs/d"),
115+
],
116+
)

0 commit comments

Comments
 (0)