-
-
Notifications
You must be signed in to change notification settings - Fork 781
Expand file tree
/
Copy pathconftest.py
More file actions
282 lines (269 loc) · 10.4 KB
/
conftest.py
File metadata and controls
282 lines (269 loc) · 10.4 KB
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
# Copyright 2024 The StackStorm Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from textwrap import dedent
import pytest
from pants.backend.python.dependency_inference.module_mapper import (
FirstPartyPythonMappingImpl,
)
from pants.backend.python.goals.pytest_runner import PytestPluginSetup
from pants.backend.python.target_types import (
PythonSourceTarget,
PythonSourcesGeneratorTarget,
PythonTestTarget,
PythonTestsGeneratorTarget,
)
from pants.backend.python.target_types_rules import rules as python_target_types_rules
from pants.engine.rules import QueryRule
from pants.testutil.python_rule_runner import PythonRuleRunner
from pants.testutil.rule_runner import RuleRunner
from pack_metadata.python_rules import (
python_module_mapper,
python_pack_content,
python_path_rules,
)
from pack_metadata.python_rules.python_module_mapper import (
St2PythonPackContentMappingMarker,
)
from pack_metadata.python_rules.python_pack_content import (
PackContentPythonEntryPoints,
PackContentPythonEntryPointsRequest,
PackContentResourceTargetsOfType,
PackContentResourceTargetsOfTypeRequest,
PackPythonLibs,
PackPythonLibsRequest,
)
from pack_metadata.python_rules.python_path_rules import (
PackPythonPath,
PackPythonPathRequest,
PytestPackTestRequest,
)
from pack_metadata.target_types import (
InjectPackPythonPathField,
PackContentResourceTarget,
PackMetadata,
)
# some random pack names
packs = (
"foo", # imports between actions
"dr_seuss", # imports from <pack>/actions/lib
"shards", # imports from <pack>/lib
"metals", # imports the action from a subdirectory
)
@pytest.fixture
def pack_names() -> tuple[str, ...]:
return packs
def write_test_files(rule_runner: RuleRunner):
for pack in packs:
rule_runner.write_files(
{
f"packs/{pack}/BUILD": dedent(
"""
__defaults__(all=dict(inject_pack_python_path=True))
pack_metadata(name="metadata")
"""
),
f"packs/{pack}/pack.yaml": dedent(
f"""
---
name: {pack}
version: 1.0.0
author: StackStorm
email: info@stackstorm.com
"""
),
f"packs/{pack}/config.schema.yaml": "",
f"packs/{pack}/config.yaml.example": "",
f"packs/{pack}/icon.png": "",
f"packs/{pack}/README.md": f"# Pack {pack} README",
}
)
def action_metadata_file(action: str, entry_point: str = "") -> str:
entry_point = entry_point or f"{action}.py"
return dedent(
f"""
---
name: {action}
runner_type: python-script
entry_point: {entry_point}
"""
)
def test_file(module: str, _object: str) -> str:
return dedent(
f"""
from {module} import {_object}
def test_{module.replace(".", "_")}() -> None:
pass
"""
)
rule_runner.write_files(
{
"packs/foo/actions/BUILD": "python_sources()",
"packs/foo/actions/get_bar.yaml": action_metadata_file("get_bar"),
"packs/foo/actions/get_bar.py": dedent(
"""
RESPONSE_CONSTANT = "foobar_key"
class BarAction:
def run(self):
return {RESPONSE_CONSTANT: "bar"}
"""
),
"packs/foo/actions/get_baz.yaml": action_metadata_file("get_baz"),
"packs/foo/actions/get_baz.py": dedent(
"""
from get_bar import RESPONSE_CONSTANT
class BazAction:
def run(self):
return {RESPONSE_CONSTANT: "baz"}
"""
),
"packs/foo/tests/BUILD": "python_tests()",
"packs/foo/tests/test_get_bar_action.py": test_file("get_bar", "BarAction"),
"packs/foo/tests/test_get_baz_action.py": test_file("get_baz", "BazAction"),
"packs/dr_seuss/actions/lib/seuss/BUILD": "python_sources()",
"packs/dr_seuss/actions/lib/seuss/__init__.py": "",
"packs/dr_seuss/actions/lib/seuss/things.py": dedent(
"""
THING1 = "thing one"
THING2 = "thing two"
"""
),
"packs/dr_seuss/actions/BUILD": "python_sources()",
"packs/dr_seuss/actions/get_from_actions_lib.yaml": action_metadata_file(
"get_from_actions_lib"
),
"packs/dr_seuss/actions/get_from_actions_lib.py": dedent(
"""
from seuss.things import THING1, THING2
class GetFromActionsLibAction:
def run(self):
return {"things": (THING1, THING2)}
"""
),
"packs/dr_seuss/tests/BUILD": "python_tests()",
"packs/dr_seuss/tests/test_get_from_actions_lib_action.py": test_file(
"get_from_actions_lib", "GetFromActionsLibAction"
),
"packs/shards/lib/stormlight_archive/BUILD": "python_sources()",
"packs/shards/lib/stormlight_archive/__init__.py": "",
"packs/shards/lib/stormlight_archive/things.py": dedent(
"""
STORM_LIGHT = "Honor"
VOID_LIGHT = "Odium"
LIFE_LIGHT = "Cultivation"
"""
),
"packs/shards/actions/BUILD": "python_sources()",
"packs/shards/actions/get_from_pack_lib.yaml": action_metadata_file(
"get_from_pack_lib"
),
"packs/shards/actions/get_from_pack_lib.py": dedent(
"""
from stormlight_archive.things import STORM_LIGHT, VOID_LIGHT, LIFE_LIGHT
class GetFromPackLibAction:
def run(self):
return {"light_sources": (STORM_LIGHT, VOID_LIGHT, LIFE_LIGHT)}
"""
),
"packs/shards/sensors/BUILD": "python_sources()",
"packs/shards/sensors/horn_eater.yaml": dedent(
"""
---
name: horn_eater
entry_point: horn_eater.py
class_name: HornEaterSensor
trigger_types: [{name: horn_eater.saw.spren, payload_schema: {type: object}}]
"""
),
"packs/shards/sensors/horn_eater.py": dedent(
"""
from st2reactor.sensor.base import PollingSensor
from stormlight_archive.things import STORM_LIGHT
class HornEaterSensor(PollingSensor):
def setup(self): pass
def poll(self):
if STORM_LIGHT in self.config:
self.sensor_service.dispatch(
trigger="horn_eater.saw.spren", payload={"spren_type": STORM_LIGHT}
)
def cleanup(self): pass
def add_trigger(self): pass
def update_trigger(self): pass
def remove_trigger(self): pass
"""
),
"packs/shards/tests/BUILD": "python_tests()",
"packs/shards/tests/test_get_from_pack_lib_action.py": test_file(
"get_from_pack_lib", "GetFromPackLibAction"
),
"packs/shards/tests/test_horn_eater_sensor.py": test_file(
"horn_eater", "HornEaterSensor"
),
"packs/metals/actions/fly.yaml": action_metadata_file(
"fly", "mist_born/fly.py"
),
"packs/metals/actions/mist_born/BUILD": "python_sources()",
"packs/metals/actions/mist_born/__init__.py": "",
"packs/metals/actions/mist_born/fly.py": dedent(
"""
class FlyAction:
def run(self):
return {"metals": ("steel", "iron")}
"""
),
"packs/metals/tests/BUILD": "python_tests()",
"packs/metals/tests/test_fly_action.py": test_file(
"mist_born.fly", "FlyAction"
),
}
)
@pytest.fixture
def rule_runner() -> RuleRunner:
rule_runner = PythonRuleRunner(
rules=[
PythonTestsGeneratorTarget.register_plugin_field(
InjectPackPythonPathField, as_moved_field=True
),
PythonTestTarget.register_plugin_field(InjectPackPythonPathField),
*python_target_types_rules(),
# TODO: not sure if we need a QueryRule for every rule...
*python_pack_content.rules(),
QueryRule(
PackContentResourceTargetsOfType,
(PackContentResourceTargetsOfTypeRequest,),
),
QueryRule(
PackContentPythonEntryPoints, (PackContentPythonEntryPointsRequest,)
),
QueryRule(PackPythonLibs, (PackPythonLibsRequest,)),
*python_module_mapper.rules(),
QueryRule(
FirstPartyPythonMappingImpl, (St2PythonPackContentMappingMarker,)
),
*python_path_rules.rules(),
QueryRule(PackPythonPath, (PackPythonPathRequest,)),
QueryRule(PytestPluginSetup, (PytestPackTestRequest,)),
],
target_types=[
PackContentResourceTarget,
PackMetadata,
PythonSourceTarget,
PythonSourcesGeneratorTarget,
PythonTestTarget,
PythonTestsGeneratorTarget,
],
)
write_test_files(rule_runner)
args = ["--source-root-patterns=packs/*"]
rule_runner.set_options(args, env_inherit={"PATH", "PYENV_ROOT", "HOME"})
return rule_runner