Skip to content

Commit b6074c4

Browse files
committed
add tests for pants-plugins/pack_metadata
1 parent 16213a1 commit b6074c4

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

pants-plugins/pack_metadata/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
python_sources()
2+
3+
python_tests(
4+
name="tests",
5+
)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
import pytest
17+
18+
from pants.core.goals.tailor import (
19+
AllOwnedSources,
20+
PutativeTarget,
21+
PutativeTargets,
22+
)
23+
from pants.testutil.rule_runner import QueryRule, RuleRunner
24+
25+
from .tailor import (
26+
PutativePackMetadataTargetsRequest,
27+
rules as pack_metadata_rules,
28+
)
29+
from .target_types import PackMetadata, PackMetadataInGitSubmodule
30+
31+
32+
@pytest.fixture
33+
def rule_runner() -> RuleRunner:
34+
return RuleRunner(
35+
rules=[
36+
*pack_metadata_rules(),
37+
QueryRule(
38+
PutativeTargets, (PutativePackMetadataTargetsRequest, AllOwnedSources)
39+
),
40+
],
41+
target_types=[PackMetadata, PackMetadataInGitSubmodule],
42+
)
43+
44+
45+
def test_find_putative_targets(rule_runner: RuleRunner) -> None:
46+
rule_runner.write_files(
47+
{
48+
"packs/already_owned/pack.yaml": "---\nname: already_owned\n",
49+
"packs/already_owned/actions/action.yaml": "---\nname: action\n",
50+
"packs/foo/pack.yaml": "---\nname: foo\n",
51+
"packs/foo/actions/action.yaml": "---\nname: action\n",
52+
"packs/bar/pack.yaml": "---\nname: bar\n",
53+
"packs/bar/sensors/sensor.yaml": "---\nname: sensor\n",
54+
"other/deep/baz/pack.yaml": "---\nname: baz\n",
55+
}
56+
)
57+
pts = rule_runner.request(
58+
PutativeTargets,
59+
[
60+
PutativePackMetadataTargetsRequest(
61+
(
62+
"packs",
63+
"packs/already_owned",
64+
"packs/already_owned/actions",
65+
"packs/foo",
66+
"packs/foo/actions",
67+
"packs/bar",
68+
"packs/bar/sensors",
69+
"other/deep/baz",
70+
)
71+
),
72+
AllOwnedSources(
73+
[
74+
"packs/already_owned/pack.yaml",
75+
"packs/already_owned/actions/action.yaml",
76+
]
77+
),
78+
],
79+
)
80+
assert (
81+
PutativeTargets(
82+
[
83+
PutativeTarget.for_target_type(
84+
PackMetadata,
85+
path="packs/foo",
86+
name="metadata",
87+
triggering_sources=["packs/foo/pack.yaml"],
88+
kwargs={"name": "metadata"},
89+
),
90+
PutativeTarget.for_target_type(
91+
PackMetadata,
92+
path="packs/bar",
93+
name="metadata",
94+
triggering_sources=["packs/bar/pack.yaml"],
95+
kwargs={"name": "metadata"},
96+
),
97+
PutativeTarget.for_target_type(
98+
PackMetadata,
99+
path="other/deep/baz",
100+
name="metadata",
101+
triggering_sources=["other/deep/baz/pack.yaml"],
102+
kwargs={"name": "metadata"},
103+
),
104+
]
105+
)
106+
== pts
107+
)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
import pytest
17+
18+
from pants.engine.addresses import Address
19+
from pants.engine.target import SourcesPaths, SourcesPathsRequest
20+
from pants.testutil.rule_runner import QueryRule, RuleRunner
21+
22+
from .target_types import (
23+
PackMetadata,
24+
# PackMetadataSourcesField,
25+
PackMetadataInGitSubmodule,
26+
PackMetadataInGitSubmoduleSources,
27+
UnmatchedGlobsError,
28+
)
29+
30+
31+
@pytest.fixture
32+
def rule_runner() -> RuleRunner:
33+
return RuleRunner(
34+
rules=[
35+
QueryRule(SourcesPaths, [SourcesPathsRequest]),
36+
],
37+
target_types=[PackMetadata, PackMetadataInGitSubmodule],
38+
)
39+
40+
41+
GIT_SUBMODULE_BUILD_FILE = """
42+
pack_metadata_in_git_submodule(
43+
name="metadata",
44+
sources=["./submodule_dir/pack.yaml"],
45+
)
46+
"""
47+
48+
49+
def test_git_submodule_sources_missing(rule_runner: RuleRunner) -> None:
50+
rule_runner.write_files(
51+
{
52+
"packs/BUILD": GIT_SUBMODULE_BUILD_FILE,
53+
}
54+
)
55+
tgt = rule_runner.get_target(Address("packs", target_name="metadata"))
56+
57+
with pytest.raises(UnmatchedGlobsError):
58+
_ = rule_runner.request(
59+
SourcesPaths, [SourcesPathsRequest(tgt[PackMetadataInGitSubmoduleSources])]
60+
)
61+
62+
63+
def test_git_submodule_sources_present(rule_runner: RuleRunner) -> None:
64+
rule_runner.write_files(
65+
{
66+
"packs/BUILD": GIT_SUBMODULE_BUILD_FILE,
67+
"packs/submodule_dir/pack.yaml": "---\nname: foobar\n",
68+
}
69+
)
70+
tgt = rule_runner.get_target(Address("packs", target_name="metadata"))
71+
72+
# basically: this asserts that it does not raise UnmatchedGlobsError
73+
_ = rule_runner.request(
74+
SourcesPaths, [SourcesPathsRequest(tgt[PackMetadataInGitSubmoduleSources])]
75+
)

0 commit comments

Comments
 (0)