Skip to content

Commit 96979cb

Browse files
committed
-
1 parent fc6a12f commit 96979cb

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

garlicsim/garlicsim/general_misc/module_tasting/sys_modules_unchanged_assertor.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,21 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
4040
new_modules_in_sys_modules = [module_name for module_name in
4141
sys.modules if module_name not in
4242
self.old_sys_modules]
43-
assert set(new_modules_in_sys_modules).issubset(
43+
if not set(new_modules_in_sys_modules).issubset(
4444
known_false_positive_new_modules
45-
)
45+
):
46+
unexpected_modules = sorted(
47+
list(
48+
set(
49+
set(new_modules_in_sys_modules).difference(
50+
known_false_positive_new_modules
51+
)
52+
)
53+
)
54+
)
55+
raise RuntimeError(
56+
'The modules `%s` were added to `sys.modules`, while we '
57+
'expected `sys.modules` to be unchanged.' % unexpected_modules
58+
)
4659

4760

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright 2009-2011 Ram Rachum.
2+
# This program is distributed under the LGPL2.1 license.
3+
4+
'''Testing pacakge for `SysModulesUnchangedAssertor`.'''
5+
6+
from __future__ import with_statement
7+
8+
import sys
9+
import uuid
10+
11+
import nose
12+
13+
from garlicsim.general_misc import cute_testing
14+
from garlicsim.general_misc import module_tasting
15+
from garlicsim.general_misc.module_tasting.sys_modules_unchanged_assertor \
16+
import SysModulesUnchangedAssertor
17+
18+
class MyException(Exception):
19+
pass
20+
21+
22+
def test_without_change():
23+
'''
24+
Test that `SysModulesUnchangedAssertor` doesn't raise exception needlessly.
25+
'''
26+
with SysModulesUnchangedAssertor():
27+
pass
28+
29+
30+
def test_with_change():
31+
'''Test `SysModulesUnchangedAssertor` raises exception when needed.'''
32+
with SysModulesUnchangedAssertor():
33+
random_key = str(uuid.uuid4())
34+
with cute_testing.RaiseAssertor(exception_type=RuntimeError,
35+
text=' we expected `sys.modules` to '
36+
'be unchanged',
37+
assert_exact_type=True):
38+
with SysModulesUnchangedAssertor():
39+
sys.modules[uuid] = None
40+
41+
assert uuid in sys.modules
42+
del sys.modules[uuid]
43+
44+
45+
def test_error_propagation_without_change():
46+
'''
47+
Test that `SysModulesUnchangedAssertor` propagates any exception.
48+
49+
This tests when `sys.modules` was not changed.
50+
'''
51+
with cute_testing.RaiseAssertor(exception_type=MyException,
52+
text='meow',
53+
assert_exact_type=True):
54+
with SysModulesUnchangedAssertor():
55+
raise MyException('meow')
56+
57+
58+
def test_error_propagation_with_change():
59+
'''
60+
Test that `SysModulesUnchangedAssertor` propagates any exception.
61+
62+
This tests when `sys.modules` was changed.
63+
'''
64+
with SysModulesUnchangedAssertor():
65+
random_key = str(uuid.uuid4())
66+
with cute_testing.RaiseAssertor(exception_type=MyException,
67+
text='frrr',
68+
assert_exact_type=True):
69+
with SysModulesUnchangedAssertor():
70+
sys.modules[uuid] = None
71+
raise MyException('frrr')
72+
73+
74+
assert uuid in sys.modules
75+
del sys.modules[uuid]
76+

0 commit comments

Comments
 (0)