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
|
import sre_constants
import pytest
from snakeoil import demandload
# few notes:
# all tests need to be wrapped w/ the following decorator; it
# ensures that snakeoils env-aware disabling is reversed, ensuring the
# setup is what the test expects.
# it also explicitly resets the state on the way out.
def reset_globals(functor):
def f(*args, **kwds):
orig_demandload = demandload.demandload
orig_demand_compile = demandload.demand_compile_regexp
orig_protection = demandload._protection_enabled
orig_noisy = demandload._noisy_protection
try:
return functor(*args, **kwds)
finally:
demandload.demandload = orig_demandload
demandload.demand_compile_regexp = orig_demand_compile
demandload._protection_enabled = orig_protection
demandload._noisy_protection = orig_noisy
return f
class TestParser:
@reset_globals
def test_parse(self):
for input, output in [
('foo', [('foo', 'foo')]),
('foo:bar', [('foo.bar', 'bar')]),
('foo:bar,baz@spork', [('foo.bar', 'bar'), ('foo.baz', 'spork')]),
('foo@bar', [('foo', 'bar')]),
('foo_bar', [('foo_bar', 'foo_bar')]),
]:
assert output == list(demandload.parse_imports([input]))
pytest.raises(ValueError, list, demandload.parse_imports(['a.b']))
pytest.raises(ValueError, list, demandload.parse_imports(['a:,']))
pytest.raises(ValueError, list, demandload.parse_imports(['a:b,x@']))
pytest.raises(ValueError, list, demandload.parse_imports(['b-x']))
pytest.raises(ValueError, list, demandload.parse_imports([' b_x']))
class TestPlaceholder:
@reset_globals
def test_getattr(self):
scope = {}
placeholder = demandload.Placeholder(scope, 'foo', list)
assert scope == object.__getattribute__(placeholder, '_scope')
assert placeholder.__doc__ == [].__doc__
assert scope['foo'] == []
demandload._protection_enabled = lambda: True
with pytest.raises(ValueError):
getattr(placeholder, '__doc__')
@reset_globals
def test__str__(self):
scope = {}
placeholder = demandload.Placeholder(scope, 'foo', list)
assert scope == object.__getattribute__(placeholder, '_scope')
assert str(placeholder) == str([])
assert scope['foo'] == []
@reset_globals
def test_call(self):
def passthrough(*args, **kwargs):
return args, kwargs
def get_func():
return passthrough
scope = {}
placeholder = demandload.Placeholder(scope, 'foo', get_func)
assert scope == object.__getattribute__(placeholder, '_scope')
assert (('arg',), {'kwarg': 42}) == placeholder('arg', kwarg=42)
assert passthrough is scope['foo']
@reset_globals
def test_setattr(self):
class Struct:
pass
scope = {}
placeholder = demandload.Placeholder(scope, 'foo', Struct)
placeholder.val = 7
demandload._protection_enabled = lambda: True
with pytest.raises(ValueError):
getattr(placeholder, 'val')
assert 7 == scope['foo'].val
class TestImport:
@reset_globals
def test_demandload(self):
scope = {}
demandload.demandload('snakeoil:demandload', scope=scope)
assert demandload is not scope['demandload']
assert demandload.demandload is scope['demandload'].demandload
assert demandload is scope['demandload']
@reset_globals
def test_disabled_demandload(self):
scope = {}
demandload.disabled_demandload('snakeoil:demandload', scope=scope)
assert demandload is scope['demandload']
class TestDemandCompileRegexp:
@reset_globals
def test_demand_compile_regexp(self):
scope = {}
demandload.demand_compile_regexp('foo', 'frob', scope=scope)
assert list(scope.keys()) == ['foo']
assert 'frob' == scope['foo'].pattern
assert 'frob' == scope['foo'].pattern
# verify it's delayed via a bad regex.
demandload.demand_compile_regexp('foo', 'f(', scope=scope)
assert list(scope.keys()) == ['foo']
# should blow up on accessing an attribute.
obj = scope['foo']
with pytest.raises(sre_constants.error):
getattr(obj, 'pattern')
|