-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_prybar.py
428 lines (314 loc) · 13.7 KB
/
test_prybar.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import contextlib
import pkg_resources
import pytest
import prybar
from prybar import dynamic_entrypoint
@pytest.fixture(autouse=True)
def validate_entrypoint_cleanup():
for key, dist in pkg_resources.working_set.by_key.items():
if dist.location == prybar.__file__:
pytest.fail(f'a dist created by prybar exists before running a '
f'test: key: {key}, dist: {dist}')
if prybar.__file__ in pkg_resources.working_set.entries:
pytest.fail('prybar.py in working_set.entries before running a test')
yield
for key, dist in pkg_resources.working_set.by_key.items():
if dist.location == prybar.__file__:
pytest.fail(f'a dist created by prybar exists after running a '
f'test: key: {key}, dist: {dist}')
if prybar.__file__ in pkg_resources.working_set.entries:
pytest.fail('prybar.py in working_set.entries after running a test')
class SomeClass:
class Nested:
def method(self):
pass
@staticmethod
def func():
pass
@staticmethod
def func_without_module(self):
pass
SomeClass.func_without_module.__module__ = None
def ep_1():
return 1
def ep_2():
return 2
def ep_3():
return 3
def test_dynamic_entrypoint_registers_entrypoint_via_with():
assert list(pkg_resources.iter_entry_points('test-group')) == []
with dynamic_entrypoint('test-group', ep_1):
eps = list(pkg_resources.iter_entry_points('test-group'))
assert len(eps) == 1
assert eps[0].name == 'ep_1'
assert eps[0].load() is ep_1
assert list(pkg_resources.iter_entry_points('test-group')) == []
def test_dynamic_entrypoint_registers_entrypoint_via_decorator():
assert list(pkg_resources.iter_entry_points('test-group')) == []
@dynamic_entrypoint('test-group', ep_1)
def func():
eps = list(pkg_resources.iter_entry_points('test-group'))
assert len(eps) == 1
assert eps[0].name == 'ep_1'
assert eps[0].load() is ep_1
func()
assert list(pkg_resources.iter_entry_points('test-group')) == []
@pytest.mark.parametrize('expected_args', [(), (1, 2, 3)])
@pytest.mark.parametrize('expected_kwargs', [{}, {'a': 1, 'b': 2}])
def test_decorator_maintains_arguments(expected_args, expected_kwargs):
@dynamic_entrypoint('test-group', ep_1)
def func(*args, **kwargs):
assert args == expected_args
assert kwargs == expected_kwargs
func(*expected_args, **expected_kwargs)
def test_decorator_maintains_return_value():
@dynamic_entrypoint('test-group', ep_1)
def func():
return 42
assert func() == 42
def test_decorator_maintains_raised_exceptions():
@dynamic_entrypoint('test-group', ep_1)
def func():
raise ValueError('foo')
with pytest.raises(ValueError) as excinfo:
func()
assert str(excinfo.value) == 'foo'
def test_dynamic_entrypoint_registers_entrypoint_via_start():
assert list(pkg_resources.iter_entry_points('test-group')) == []
dep = dynamic_entrypoint('test-group', ep_1)
dep.start()
eps = list(pkg_resources.iter_entry_points('test-group'))
assert len(eps) == 1
assert eps[0].name == 'ep_1'
assert eps[0].load() is ep_1
dep.stop()
assert list(pkg_resources.iter_entry_points('test-group')) == []
def test_entrypoint_can_be_entered_multiple_times():
dyn_ep = dynamic_entrypoint('test-group', ep_1)
def assert_ep_1_active():
eps = list(pkg_resources.iter_entry_points('test-group'))
assert len(eps) == 1
assert eps[0].load() is ep_1
@dyn_ep
def func_with_ep_1_active():
assert_ep_1_active()
# we can enter the context manager again from a decorated function
with dyn_ep:
assert_ep_1_active()
# It's still active now, even though this context manager has stopped
assert_ep_1_active()
# Nothing's used the entry point yet, so it doesn't exist
assert list(pkg_resources.iter_entry_points('test-group')) == []
with dyn_ep:
assert_ep_1_active()
# we can use a decorated function with entry point active
func_with_ep_1_active()
# we can enter the manager again
with dyn_ep:
assert_ep_1_active()
# We can't use the manual API while a context manager is active
with pytest.raises(RuntimeError):
dyn_ep.stop()
with pytest.raises(RuntimeError):
dyn_ep.start()
# and again, it's still active now
assert_ep_1_active()
# Now there are no more users, so it's gone
assert list(pkg_resources.iter_entry_points('test-group')) == []
# And we can re-activate it again:
with dyn_ep:
assert_ep_1_active()
assert list(pkg_resources.iter_entry_points('test-group')) == []
def test_multiple_entrypoint_registration():
names = ['ep_1', 'ep_2', 'ep_3']
with contextlib.ExitStack() as stack:
for name in ['ep_1', 'ep_2', 'ep_3']:
stack.enter_context(dynamic_entrypoint('test-group',
entrypoint=globals()[name]))
eps = list(pkg_resources.iter_entry_points('test-group'))
assert [ep.name for ep in eps] == names
def test_names_must_be_unique_per_scope():
with dynamic_entrypoint('test-group', name='foo', entrypoint=ep_1):
with pytest.raises(ValueError) as excinfo:
with dynamic_entrypoint('test-group', name='foo', entrypoint=ep_2):
pytest.fail('must not enter')
assert (str(excinfo.value) ==
"'foo' is already registered under 'test-group' in scope "
"'prybar.scope.default'")
def test_scope_cant_shadow_existing_distribution():
with pytest.raises(ValueError) as excinfo:
# Try to shadow pytest
with dynamic_entrypoint('test-group', ep_1, scope='pytest'):
pytest.fail('must not enter')
assert str(excinfo.value).startswith(
"scope 'pytest' already exists in working set at location /")
def test_names_need_not_be_unique_in_different_scopes():
with dynamic_entrypoint('test-group', name='foo', entrypoint=ep_1,
scope='a'):
with dynamic_entrypoint('test-group', name='foo', entrypoint=ep_2,
scope='b'):
foos = list(pkg_resources.iter_entry_points('test-group', 'foo'))
assert [ep.name for ep in foos] == ['foo', 'foo']
assert [ep.load() for ep in foos] == [ep_1, ep_2]
@pytest.mark.parametrize('func_name', ['ep_1', 'ep_2', 'ep_3'])
def test_entrypoints_are_loadable(func_name):
with dynamic_entrypoint('test-group', name=func_name, module=__name__):
ep = next(pkg_resources.iter_entry_points('test-group'))
assert ep.name == func_name
assert ep.load() is globals()[func_name]
@pytest.mark.parametrize('kwargs, expected', [
(dict(name='foo', module=__name__, attribute=('SomeClass', 'func')),
SomeClass.func),
(dict(name='foo', module=__name__, attribute=('SomeClass', 'Nested')),
SomeClass.Nested),
(dict(name='foo', module=__name__,
attribute=('SomeClass', 'Nested', 'method')),
SomeClass.Nested.method),
(dict(name='foo', entrypoint=SomeClass.func), SomeClass.func),
(dict(name='foo', entrypoint=SomeClass.Nested), SomeClass.Nested),
(dict(name='foo', entrypoint=SomeClass.Nested.method),
SomeClass.Nested.method),
])
def test_entrypoints_with_multiple_attributes_load_nested_objects(
kwargs, expected):
with dynamic_entrypoint('test-group', **kwargs):
ep = next(pkg_resources.iter_entry_points('test-group'))
assert ep.load() is expected
def test_callable_entrypoint_must_have_module_attr():
assert SomeClass.func_without_module.__module__ is None
with pytest.raises(ValueError) as excinfo:
dynamic_entrypoint('test-group',
entrypoint=SomeClass.func_without_module)
msg = str(excinfo.value)
assert msg.startswith('callable entrypoint has no __module__: ')
assert f'SomeClass.func_without_module' in msg
@pytest.mark.parametrize('args, kwargs', [
# entrypoint as a function object
(['test-group', ep_1], {}),
# Use different name for entrypoint than function name
(['test-group'], dict(name='ep_1', entrypoint=ep_2)),
([], dict(group='test-group', entrypoint=ep_1)),
# Pre-created entrypoint
(['test-group', pkg_resources.EntryPoint('ep_1', __name__,
attrs=('ep_1',))], {}),
# Entrypoint as a string to parse
(['test-group', f'ep_1 = {__name__}:ep_1'], {}),
# Names / paths to create entrypoint from
(['test-group'], dict(name='ep_1', module=__name__)),
(['test-group'], dict(name='ep_1', module=__name__, attribute='ep_1')),
(['test-group'], dict(name='ep_1', module=__name__, attribute=('ep_1',))),
(['test-group'], dict(name='ep_1', module=__name__, attribute=None)),
(['test-group'], dict(name='ep_1', module=__name__, attribute=None))
])
def test_valid_arguments(args, kwargs):
with dynamic_entrypoint(*args, **kwargs):
assert ['ep_1'] == [ep.name for ep in
pkg_resources.iter_entry_points('test-group')]
@pytest.mark.parametrize('args, kwargs, exc, msg', [
([42], {}, TypeError,
'group must be a string, got: 42'),
(['test-group'], {}, TypeError,
'name and module_name must be specified when entrypoint is not '
'specified'),
(['test-group', None], {}, TypeError,
'name and module_name must be specified when entrypoint is not '
'specified'),
# EntryPoint instance with existing dist
(['test-group', pkg_resources.EntryPoint('ep_1', __name__,
attrs=('ep_1',), dist=object())],
{}, ValueError, 'can\'t specify a pkg_resources.Entrypoint instance with '
'a dist already attached'),
# Specifying things twice
(['test-group', f'ep_1 = {__name__}:ep_1'], dict(name='abc'), TypeError,
'can\'t specify name, module_name or attribute when entrypoint is a '
'pkg_resources.Entrypoint'),
(['test-group', f'ep_1 = {__name__}:ep_1'], dict(module='abc'), TypeError,
'can\'t specify name, module_name or attribute when entrypoint is a '
'pkg_resources.Entrypoint'),
(['test-group', f'ep_1 = {__name__}:ep_1'], dict(attribute='abc'),
TypeError, 'can\'t specify name, module_name or attribute when '
'entrypoint is a pkg_resources.Entrypoint'),
])
def test_invalid_arguments(args, kwargs, exc, msg):
with pytest.raises(exc) as excinfo:
with dynamic_entrypoint(*args, **kwargs):
pass
assert msg in str(excinfo.value)
def test_cant_exit_without_enter():
with pytest.raises(RuntimeError) as excinfo:
dynamic_entrypoint('test-group', ep_1).__exit__(None, None, None)
assert str(excinfo.value) == '__exit__() called more than __enter__()'
def test_cant_use_stop_with_context_manager():
dep = dynamic_entrypoint('test-group', ep_1)
with pytest.raises(RuntimeError) as excinfo:
with dep:
dep.stop()
assert (str(excinfo.value) ==
'can\'t stop() while active via __enter__()')
def test_cant_use_start_with_context_manager():
dep = dynamic_entrypoint('test-group', ep_1)
with pytest.raises(RuntimeError) as excinfo:
with dep:
dep.start()
assert (str(excinfo.value) ==
'can\'t start() while active via __enter__()')
def test_cant_use_stop_with_decorator():
dep = dynamic_entrypoint('test-group', ep_1)
@dep
def block():
with pytest.raises(RuntimeError) as excinfo:
dep.stop()
assert (str(excinfo.value) ==
'can\'t stop() while active via __enter__()')
block()
def test_cant_use_start_with_decorator():
dep = dynamic_entrypoint('test-group', ep_1)
@dep
def block():
with pytest.raises(RuntimeError) as excinfo:
dep.start()
assert (str(excinfo.value) ==
'can\'t start() while active via __enter__()')
block()
def test_cant_use_context_manager_with_start():
dep = dynamic_entrypoint('test-group', ep_1)
dep.start()
with pytest.raises(RuntimeError) as excinfo:
with dep:
pytest.fail('should not enter block')
dep.stop()
assert str(excinfo.value) == 'can\'t __enter__() while active via start()'
def test_cant_use_decorator_with_start():
dep = dynamic_entrypoint('test-group', ep_1)
dep.start()
@dep
def foo():
pytest.fail('should not enter function')
with pytest.raises(RuntimeError) as excinfo:
foo()
dep.stop()
assert str(excinfo.value) == 'can\'t __enter__() while active via start()'
def test_start_stop_can_be_called_when_already_started_or_stopped():
dep = dynamic_entrypoint('test-group', ep_1)
assert list(pkg_resources.iter_entry_points('test-group')) == []
dep.stop()
dep.stop()
dep.start()
assert len(list(pkg_resources.iter_entry_points('test-group'))) == 1
dep.start()
dep.start()
assert len(list(pkg_resources.iter_entry_points('test-group'))) == 1
dep.stop()
dep.stop()
assert list(pkg_resources.iter_entry_points('test-group')) == []
def test_scopes_are_normalised_by_pkg_resources():
# scopes are pkg_resources distributions, and underscores are normalised
# to hyphens in distribution names.
with dynamic_entrypoint('test-group', entrypoint=ep_1, scope='foo-bar'):
with pytest.raises(ValueError) as excinfo:
with dynamic_entrypoint('test-group', entrypoint=ep_1,
scope='foo_bar'):
pass
assert str(excinfo.value) == (
"'ep_1' is already registered under 'test-group' in scope "
"'foo_bar' ('foo-bar')")