-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_plugins.py
210 lines (174 loc) · 6.52 KB
/
test_plugins.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
207
208
209
210
from dataclasses import dataclass
from typing import Any, Optional
import pytest
from variantlib.base import PluginBase
from variantlib.config import KeyConfig, ProviderConfig
from variantlib.meta import VariantDescription, VariantMeta
from variantlib.plugins import PluginLoader
class MockedPluginA(PluginBase):
namespace = "test_plugin"
def get_supported_configs(self) -> Optional[ProviderConfig]:
return ProviderConfig(
namespace=self.namespace,
configs=[
KeyConfig("key1", ["val1a", "val1b"]),
KeyConfig("key2", ["val2a", "val2b", "val2c"]),
],
)
def get_variant_labels(self, variant_desc: VariantDescription) -> list[str]:
for meta in variant_desc:
if meta.namespace == self.namespace and meta.key == "key1":
return [meta.value.removeprefix("val")]
return []
# NB: this plugin deliberately does not inherit from PluginBase
# to test that we don't rely on that inheritance
class MockedPluginB:
namespace = "second_plugin"
def get_supported_configs(self) -> Optional[ProviderConfig]:
return ProviderConfig(
namespace=self.namespace,
configs=[
KeyConfig("key3", ["val3a"]),
],
)
def get_variant_labels(self, variant_desc: VariantDescription) -> list[str]:
if VariantMeta(self.namespace, "key3", "val3a") in variant_desc:
return ["sec"]
return []
class MockedPluginC(PluginBase):
namespace = "other_plugin"
def get_supported_configs(self) -> Optional[ProviderConfig]:
return None
def get_variant_labels(self, variant_desc: VariantDescription) -> list[str]:
ret = []
for meta in variant_desc:
if meta.namespace == self.namespace and meta.value == "on":
ret.append(meta.key)
return ret
class MockedPluginD:
namespace = "plugin_without_labels"
def get_supported_configs(self) -> Optional[ProviderConfig]:
return None
class ClashingPlugin(PluginBase):
namespace = "test_plugin"
def get_supported_configs(self) -> Optional[ProviderConfig]:
return None
def get_variant_labels(self, variant_desc: VariantDescription) -> list[str]:
ret = []
for meta in variant_desc:
if meta.namespace == self.namespace and meta.value == "on":
ret.append(meta.key)
return ret
@dataclass
class MockedDistribution:
name: str
version: str
@dataclass
class MockedEntryPoint:
name: Optional[str]
value: str
plugin: Any
group: Optional[str] = None
dist: Optional[MockedDistribution] = None
def load(self) -> Any:
return self.plugin
@pytest.fixture(scope="session")
def mocked_plugin_loader(session_mocker):
session_mocker.patch("variantlib.plugins.entry_points")().select.return_value = [
MockedEntryPoint(
name="test_plugin",
value="tests.test_plugins:MockedPluginA",
dist=MockedDistribution(name="test-plugin", version="1.2.3"),
plugin=MockedPluginA,
),
MockedEntryPoint(
name="second_plugin",
value="tests.test_plugins:MockedPluginB",
dist=MockedDistribution(name="second-plugin", version="4.5.6"),
plugin=MockedPluginB,
),
MockedEntryPoint(
name="incompatible_plugin",
value="tests.test_plugins:MockedPluginC",
plugin=MockedPluginC,
),
MockedEntryPoint(
name="no_labels",
value="tests.test_plugins:MockedPluginD",
plugin=MockedPluginD,
),
]
yield PluginLoader()
def test_get_supported_configs(mocked_plugin_loader):
assert mocked_plugin_loader.get_supported_configs() == {
"second_plugin": ProviderConfig(
namespace="second_plugin",
configs=[
KeyConfig("key3", ["val3a"]),
],
),
"test_plugin": ProviderConfig(
namespace="test_plugin",
configs=[
KeyConfig("key1", ["val1a", "val1b"]),
KeyConfig("key2", ["val2a", "val2b", "val2c"]),
],
),
}
def test_get_dist_name_mapping(mocked_plugin_loader):
assert mocked_plugin_loader.get_dist_name_mapping() == {
"second_plugin": "second-plugin",
"test_plugin": "test-plugin",
}
def test_namespace_clash(mocker):
mocker.patch("variantlib.plugins.entry_points")().select.return_value = [
MockedEntryPoint(
name="test_plugin",
value="tests.test_plugins:MockedPluginA",
dist=MockedDistribution(name="test-plugin", version="1.2.3"),
plugin=MockedPluginA,
),
MockedEntryPoint(
name="clashing_plugin",
value="tests.test_plugins:ClashingPlugin",
dist=MockedDistribution(name="clashing-plugin", version="4.5.6"),
plugin=ClashingPlugin,
),
]
mocker.patch("variantlib.metaclasses.SingletonMetaClass._instances", [])
with pytest.raises(RuntimeError) as exc:
PluginLoader()
assert "same namespace test_plugin" in str(exc)
assert "test-plugin" in str(exc)
assert "clashing-plugin" in str(exc)
@pytest.mark.parametrize("variant_desc,expected",
[
(VariantDescription([
VariantMeta("test_plugin", "key1", "val1a"),
VariantMeta("test_plugin", "key2", "val2b"),
VariantMeta("second_plugin", "key3", "val3a"),
VariantMeta("other_plugin", "flag2", "on"),
]), ["1a", "sec", "flag2"]),
(VariantDescription([
# note that VariantMetas don't actually have to be supported
# by the system in question -- we could be cross-building
# for another system
VariantMeta("test_plugin", "key1", "val1f"),
VariantMeta("test_plugin", "key2", "val2b"),
VariantMeta("second_plugin", "key3", "val3a"),
]), ["1f", "sec"]),
(VariantDescription([
VariantMeta("test_plugin", "key2", "val2b"),
VariantMeta("second_plugin", "key3", "val3a"),
]), ["sec"]),
(VariantDescription([
VariantMeta("test_plugin", "key2", "val2b"),
]), []),
(VariantDescription([
VariantMeta("test_plugin", "key2", "val2b"),
VariantMeta("other_plugin", "flag1", "on"),
VariantMeta("other_plugin", "flag2", "on"),
]), ["flag1", "flag2"]),
])
def test_get_variant_labels(mocked_plugin_loader, variant_desc, expected):
assert mocked_plugin_loader.get_variant_labels(variant_desc) == expected