forked from boostpro/lazy_reload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
96 lines (74 loc) · 1.81 KB
/
tests.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
import sys
import __builtin__
#
# Record the history of reloads
#
reload_history = []
real_reload = __builtin__.reload
def reload_recorder(module):
reload_history.append(module.__name__)
real_reload(module)
__builtin__.reload = reload_recorder
from lazy_reload import *
def assert_reload_history(*l):
global reload_history
if tuple(reload_history) != l:
raise AssertionError, reload_history
reload_history = []
from lazy_reload import *
# An ordinary import of something already loaded shouldn't cause a
# reload
import lazy_reload as lr
assert_reload_history()
#
# Test that lazy_reload itself can be reloaded
#
lazy_reload('lazy_reload')
# Test that it's actually lazy
assert_reload_history()
import lazy_reload as lr
assert_reload_history('lazy_reload')
lazy_reload('lazy_reload')
from lazy_reload import *
assert_reload_history('lazy_reload')
lazy_reload('lazy_reload')
from lazy_reload import lazy_reload
assert_reload_history('lazy_reload')
#
# Some more complex cases
#
import test.a
assert_reload_history()
import test
assert_reload_history()
lazy_reload('test.a')
import test.b
assert_reload_history()
del test.a
from test import a
assert_reload_history('test.a')
lazy_reload(test)
from test import a
assert_reload_history('test', 'test.a')
#
# Check a case with circular references
#
import test.c
assert test.c.d.test.c is test.c
lazy_reload(test)
import test.c.d
assert_reload_history('test', 'test.c', 'test.c.d')
assert test.c.d.test.c is test.c
#
# Test sibling references
#
from test import e, a
reload_history = []
lazy_reload(test.a)
lazy_reload(test.b)
reload(e)
# e's attempt to directly import a should cause test.a to reload
#
# Our special import hook causes e's "from test import b" to reload b
assert_reload_history('test.e', 'test.a', 'test.b')
print '****** PASSED ******'