-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hide_modules.py
91 lines (68 loc) · 2.49 KB
/
test_hide_modules.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
#!/usr/bin/env python
import unittest
import hide_modules
def import_csv():
import csv
def import_wsgiref_handlers():
import wsgiref.handlers
@hide_modules.hide_modules(['csv', 'wsgiref.handlers'])
def blocked_import_csv():
import_csv()
@hide_modules.hide_modules(['csv', 'wsgiref.handlers'])
def blocked_import_wsgiref_handlers():
import_wsgiref_handlers()
class TestHideModules(unittest.TestCase):
def testHideUnhide(self):
"Test ModuleHider hide and unhide methods"
mh = hide_modules.ModuleHider(['csv', 'wsgiref.handlers'])
import_csv()
import_wsgiref_handlers()
mh.hide()
self.assertRaises(ImportError, import_csv)
self.assertRaises(ImportError, import_wsgiref_handlers)
mh.unhide()
import_csv()
import_wsgiref_handlers()
def testContextManager(self):
"Test ModuleHider as context manager"
import_csv()
import_wsgiref_handlers()
with hide_modules.ModuleHider(['csv', 'wsgiref.handlers']):
self.assertRaises(ImportError, import_csv)
self.assertRaises(ImportError, import_wsgiref_handlers)
import_csv()
import_wsgiref_handlers()
def testDecorator(self):
"Test hide_modules decorator"
import_csv()
import_wsgiref_handlers()
self.assertRaises(ImportError, blocked_import_csv)
self.assertRaises(ImportError, blocked_import_wsgiref_handlers)
import_csv()
import_wsgiref_handlers()
@hide_modules.hide_modules(['csv', 'wsgiref.handlers'])
def testDecoratorOnTest(self):
"Test hide_modules applied to test method"
self.assertRaises(ImportError, import_csv)
self.assertRaises(ImportError, import_wsgiref_handlers)
def testRestoringHiddenModules(self):
"""Test that unhidden modules are restored.
Some packages---e.g., encodings from stdlib, and numpy---must
be restored to sys.modules, or they cannot be re-imported.
This is a regression test; modules were previously *not*
restored, which caused problems in Python 2.
"""
def fallback_on_import():
try:
import encodings
self.assertTrue(False)
except ImportError:
pass
@hide_modules.hide_modules(['encodings'])
def fn():
fallback_on_import()
import encodings
fn()
import encodings
if __name__ == '__main__':
unittest.main()