1
|
|
|
import os |
2
|
|
|
import pytest |
3
|
|
|
|
4
|
|
|
import groundwork |
5
|
|
|
from groundwork.exceptions import PluginRegistrationException |
6
|
|
|
from groundwork.patterns import GwBasePattern |
7
|
|
|
from groundwork.patterns.gw_base_pattern import PluginAttributeMissing, PluginActivateMissing, PluginDeactivateMissing |
8
|
|
|
from groundwork.patterns.exceptions import PluginDependencyLoop |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def test_app_initialisation(): |
12
|
|
|
app = groundwork.App() |
13
|
|
|
assert app.path == os.getcwd() |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def test_app_initialisation_with_config(): |
17
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__)) |
18
|
|
|
config = os.path.join(current_dir, "static", "config.py") |
19
|
|
|
app = groundwork.App([config]) |
20
|
|
|
|
21
|
|
|
assert app.path == os.getcwd() |
22
|
|
|
assert app.config.PLUGINS == ["PluginA", "PluginB", "NoPlugin"] |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def test_app_initialisation_with_config2(): |
26
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__)) |
27
|
|
|
config = os.path.join(current_dir, "static", "config2.py") |
28
|
|
|
app = groundwork.App([config]) |
29
|
|
|
|
30
|
|
|
assert app.path == os.path.join(current_dir, "static") |
31
|
|
|
with pytest.raises(AttributeError): |
32
|
|
|
app.config.PLUGINS != ["PluginA", "PluginB", "NoPlugin"] |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def test_app_strict(): |
36
|
|
|
|
37
|
|
|
class MyBadPlugin(): |
38
|
|
|
pass |
39
|
|
|
|
40
|
|
|
my_app = groundwork.App(strict=True) |
41
|
|
|
with pytest.raises(AttributeError): |
42
|
|
|
my_app.plugins.classes.register([MyBadPlugin]) # will throw an exception |
43
|
|
|
|
44
|
|
|
my_app.strict = False |
45
|
|
|
my_app.plugins.classes.register([MyBadPlugin]) # will log a warning only |
46
|
|
|
|
47
|
|
|
assert len(my_app.plugins.get()) == 0 |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
# def test_app_initialisation_with_plugins(BasicPlugin): |
51
|
|
|
# # Tries to import, but doesn't stop on problems |
52
|
|
|
# # app = groundwork.App(plugins=["TestPluginA", "TestPluginB", "NoPlugin"]) |
53
|
|
|
# |
54
|
|
|
# # Tries to import and stops on problems |
55
|
|
|
# with pytest.raises(AttributeError): |
56
|
|
|
# app = groundwork.App(plugins=["TestPluginA", "TestPluginB", "NoPlugin"], strict=True) |
57
|
|
|
# |
58
|
|
|
# app = groundwork.App(plugins=[BasicPlugin]) |
59
|
|
|
# app.plugins.activate(["BasicPlugin"]) |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def test_app_plugin_registration(BasicPlugin): |
63
|
|
|
app = groundwork.App(plugins=[BasicPlugin], strict=True) |
64
|
|
|
plugin_class = app.plugins.classes.get("BasicPlugin") |
65
|
|
|
assert plugin_class is not None |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def test_app_plugin_activation(BasicPlugin): |
69
|
|
|
app = groundwork.App(plugins=[BasicPlugin], strict=True) |
70
|
|
|
app.plugins.activate(["BasicPlugin"]) |
71
|
|
|
plugin = app.plugins.get("BasicPlugin") |
72
|
|
|
assert plugin is not None |
73
|
|
|
assert plugin.active is True |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def test_app_plugin_deactivation(BasicPlugin): |
77
|
|
|
app = groundwork.App(plugins=[BasicPlugin], strict=True) |
78
|
|
|
app.plugins.activate(["BasicPlugin"]) |
79
|
|
|
plugin = app.plugins.get("BasicPlugin") |
80
|
|
|
assert plugin is not None |
81
|
|
|
assert plugin.active is True |
82
|
|
|
app.plugins.deactivate(["BasicPlugin"]) |
83
|
|
|
assert plugin.active is False |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def test_app_plugin_multi_status_change(BasicPlugin): |
87
|
|
|
app = groundwork.App(plugins=[BasicPlugin], strict=True) |
88
|
|
|
|
89
|
|
|
# De/Activation via app |
90
|
|
|
app.plugins.activate(["BasicPlugin"]) |
91
|
|
|
plugin = app.plugins.get("BasicPlugin") |
92
|
|
|
assert plugin.active is True |
93
|
|
|
app.plugins.deactivate(["BasicPlugin"]) |
94
|
|
|
assert plugin.active is False |
95
|
|
|
app.plugins.activate(["BasicPlugin"]) |
96
|
|
|
assert plugin.active is True |
97
|
|
|
# De/Activation via plugin itself |
98
|
|
|
plugin = app.plugins.get("BasicPlugin") |
99
|
|
|
plugin.deactivate() |
100
|
|
|
assert plugin.active is False |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
def test_app_multi_repeating_registration(BasicPlugin, basicApp): |
104
|
|
|
with pytest.raises(PluginRegistrationException): |
105
|
|
|
basicApp.plugins.classes.register([BasicPlugin]) |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
def test_multi_app(BasicPlugin): |
109
|
|
|
app = groundwork.App(plugins=[BasicPlugin], strict=True) |
110
|
|
|
plugin_class = app.plugins.classes.get("BasicPlugin") |
111
|
|
|
|
112
|
|
|
app2 = groundwork.App(plugins=[BasicPlugin], strict=True) |
113
|
|
|
plugin2_class = app2.plugins.classes.get("BasicPlugin") |
114
|
|
|
|
115
|
|
|
assert app is not app2 |
116
|
|
|
assert plugin_class is not plugin2_class # Checks, if reference is not the same |
117
|
|
|
|
118
|
|
|
app.plugins.activate(["BasicPlugin"]) |
119
|
|
|
plugin = app.plugins.get("BasicPlugin") |
120
|
|
|
|
121
|
|
|
app2.plugins.activate(["BasicPlugin"]) |
122
|
|
|
plugin2 = app2.plugins.get("BasicPlugin") |
123
|
|
|
|
124
|
|
|
assert plugin is not plugin2 |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
def test_plugin_missing_name(basicApp): |
128
|
|
|
|
129
|
|
|
class MyPlugin(GwBasePattern): |
130
|
|
|
def __init__(self, app, **kwargs): |
131
|
|
|
super().__init__(app, **kwargs) |
132
|
|
|
|
133
|
|
|
with pytest.raises(PluginAttributeMissing): |
134
|
|
|
MyPlugin(basicApp) |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
def test_plugin_missing_activate(basicApp): |
138
|
|
|
|
139
|
|
|
class MyPlugin(GwBasePattern): |
140
|
|
|
def __init__(self, app, **kwargs): |
141
|
|
|
self.name = "my_plugin" |
142
|
|
|
super().__init__(app, **kwargs) |
143
|
|
|
|
144
|
|
|
my_plugin = MyPlugin(basicApp) |
145
|
|
|
with pytest.raises(PluginActivateMissing): |
146
|
|
|
my_plugin.activate() |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
def test_plugin_missing_deactivate(basicApp): |
150
|
|
|
|
151
|
|
|
class MyPlugin(GwBasePattern): |
152
|
|
|
def __init__(self, app, **kwargs): |
153
|
|
|
self.name = "my_plugin" |
154
|
|
|
super().__init__(app, **kwargs) |
155
|
|
|
|
156
|
|
|
def activate(self): |
157
|
|
|
pass |
158
|
|
|
|
159
|
|
|
my_plugin = MyPlugin(basicApp) |
160
|
|
|
my_plugin.activate() |
161
|
|
|
with pytest.raises(PluginDeactivateMissing): |
162
|
|
|
my_plugin.deactivate() |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
def test_plugin_unknown_attribute(basicApp): |
166
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
167
|
|
|
with pytest.raises(AttributeError): |
168
|
|
|
plugin.nowhere() |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
def test_plugin_dependency(basicApp): |
172
|
|
|
|
173
|
|
|
class MyPluginA(GwBasePattern): |
174
|
|
|
def __init__(self, app, **kwargs): |
175
|
|
|
self.name = "my_plugin_a" |
176
|
|
|
self.needed_plugins = ("my_plugin_b",) |
177
|
|
|
super().__init__(app, **kwargs) |
178
|
|
|
|
179
|
|
|
def activate(self): |
180
|
|
|
pass |
181
|
|
|
|
182
|
|
|
def deactivate(self): |
183
|
|
|
pass |
184
|
|
|
|
185
|
|
|
class MyPluginB(GwBasePattern): |
186
|
|
|
def __init__(self, app, **kwargs): |
187
|
|
|
self.name = "my_plugin_b" |
188
|
|
|
super().__init__(app, **kwargs) |
189
|
|
|
|
190
|
|
|
def activate(self): |
191
|
|
|
pass |
192
|
|
|
|
193
|
|
|
def deactivate(self): |
194
|
|
|
pass |
195
|
|
|
|
196
|
|
|
my_plugin_a = MyPluginA(basicApp) |
197
|
|
|
my_plugin_b = MyPluginB(basicApp) |
198
|
|
|
|
199
|
|
|
assert my_plugin_b.active is False |
200
|
|
|
my_plugin_a.activate() |
201
|
|
|
assert my_plugin_b.active is True |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
def test_plugin_dependency_loop(basicApp): |
205
|
|
|
|
206
|
|
|
class MyPluginA(GwBasePattern): |
207
|
|
|
def __init__(self, app, **kwargs): |
208
|
|
|
self.name = "my_plugin_a" |
209
|
|
|
self.needed_plugins = ("my_plugin_b",) |
210
|
|
|
super().__init__(app, **kwargs) |
211
|
|
|
|
212
|
|
|
def activate(self): |
213
|
|
|
pass |
214
|
|
|
|
215
|
|
|
def deactivate(self): |
216
|
|
|
pass |
217
|
|
|
|
218
|
|
|
class MyPluginB(GwBasePattern): |
219
|
|
|
def __init__(self, app, **kwargs): |
220
|
|
|
self.name = "my_plugin_b" |
221
|
|
|
self.needed_plugins = ("my_plugin_a",) |
222
|
|
|
super().__init__(app, **kwargs) |
223
|
|
|
|
224
|
|
|
def activate(self): |
225
|
|
|
pass |
226
|
|
|
|
227
|
|
|
def deactivate(self): |
228
|
|
|
pass |
229
|
|
|
|
230
|
|
|
my_plugin_a = MyPluginA(basicApp) |
231
|
|
|
my_plugin_b = MyPluginB(basicApp) |
232
|
|
|
|
233
|
|
|
basicApp.strict = False |
234
|
|
|
assert my_plugin_b.active is False |
235
|
|
|
my_plugin_a.activate() |
236
|
|
|
assert my_plugin_b.active is True |
237
|
|
|
|
238
|
|
|
my_plugin_a.deactivate() |
239
|
|
|
my_plugin_b.deactivate() |
240
|
|
|
|
241
|
|
|
basicApp.strict = True |
242
|
|
|
assert my_plugin_b.active is False |
243
|
|
|
with pytest.raises(PluginDependencyLoop): |
244
|
|
|
my_plugin_a.activate() |
245
|
|
|
|