OLD | NEW |
1 from .. import util | 1 from .. import util |
2 from . import util as import_util | 2 from . import util as import_util |
3 from types import MethodType | 3 from types import MethodType |
4 import unittest | 4 import unittest |
5 | 5 |
6 | 6 |
7 class CallingOrder(unittest.TestCase): | 7 class CallingOrder(unittest.TestCase): |
8 | 8 |
9 """Calls to the importers on sys.meta_path happen in order that they are | 9 """Calls to the importers on sys.meta_path happen in order that they are |
10 specified in the sequence, starting with the first importer | 10 specified in the sequence, starting with the first importer |
11 [first called], and then continuing on down until one is found that doesn't | 11 [first called], and then continuing on down until one is found that doesn't |
12 return None [continuing].""" | 12 return None [continuing].""" |
13 | 13 |
14 | 14 |
15 def test_first_called(self): | 15 def test_first_called(self): |
16 # [first called] | 16 # [first called] |
17 mod = 'top_level' | 17 mod = 'top_level' |
18 first = util.mock_modules(mod) | 18 first = util.mock_modules(mod) |
19 second = util.mock_modules(mod) | 19 second = util.mock_modules(mod) |
20 with util.mock_modules(mod) as first, util.mock_modules(mod) as second: | 20 with util.mock_modules(mod) as first, util.mock_modules(mod) as second: |
21 first.modules[mod] = 42 | 21 first.modules[mod] = 42 |
22 second.modules[mod] = -13 | 22 second.modules[mod] = -13 |
23 with util.import_state(meta_path=[first, second]): | 23 with util.import_state(meta_path=[first, second]): |
24 self.assertEquals(import_util.import_(mod), 42) | 24 self.assertEqual(import_util.import_(mod), 42) |
25 | 25 |
26 def test_continuing(self): | 26 def test_continuing(self): |
27 # [continuing] | 27 # [continuing] |
28 mod_name = 'for_real' | 28 mod_name = 'for_real' |
29 with util.mock_modules('nonexistent') as first, \ | 29 with util.mock_modules('nonexistent') as first, \ |
30 util.mock_modules(mod_name) as second: | 30 util.mock_modules(mod_name) as second: |
31 first.find_module = lambda self, fullname, path=None: None | 31 first.find_module = lambda self, fullname, path=None: None |
32 second.modules[mod_name] = 42 | 32 second.modules[mod_name] = 42 |
33 with util.import_state(meta_path=[first, second]): | 33 with util.import_state(meta_path=[first, second]): |
34 self.assertEquals(import_util.import_(mod_name), 42) | 34 self.assertEqual(import_util.import_(mod_name), 42) |
35 | 35 |
36 | 36 |
37 class CallSignature(unittest.TestCase): | 37 class CallSignature(unittest.TestCase): |
38 | 38 |
39 """If there is no __path__ entry on the parent module, then 'path' is None | 39 """If there is no __path__ entry on the parent module, then 'path' is None |
40 [no path]. Otherwise, the value for __path__ is passed in for the 'path' | 40 [no path]. Otherwise, the value for __path__ is passed in for the 'path' |
41 argument [path set].""" | 41 argument [path set].""" |
42 | 42 |
43 def log(self, fxn): | 43 def log(self, fxn): |
44 log = [] | 44 log = [] |
45 def wrapper(self, *args, **kwargs): | 45 def wrapper(self, *args, **kwargs): |
46 log.append([args, kwargs]) | 46 log.append([args, kwargs]) |
47 return fxn(*args, **kwargs) | 47 return fxn(*args, **kwargs) |
48 return log, wrapper | 48 return log, wrapper |
49 | 49 |
50 | 50 |
51 def test_no_path(self): | 51 def test_no_path(self): |
52 # [no path] | 52 # [no path] |
53 mod_name = 'top_level' | 53 mod_name = 'top_level' |
54 assert '.' not in mod_name | 54 assert '.' not in mod_name |
55 with util.mock_modules(mod_name) as importer: | 55 with util.mock_modules(mod_name) as importer: |
56 log, wrapped_call = self.log(importer.find_module) | 56 log, wrapped_call = self.log(importer.find_module) |
57 importer.find_module = MethodType(wrapped_call, importer) | 57 importer.find_module = MethodType(wrapped_call, importer) |
58 with util.import_state(meta_path=[importer]): | 58 with util.import_state(meta_path=[importer]): |
59 import_util.import_(mod_name) | 59 import_util.import_(mod_name) |
60 assert len(log) == 1 | 60 assert len(log) == 1 |
61 args = log[0][0] | 61 args = log[0][0] |
62 kwargs = log[0][1] | 62 kwargs = log[0][1] |
63 # Assuming all arguments are positional. | 63 # Assuming all arguments are positional. |
64 self.assertEquals(len(args), 2) | 64 self.assertEqual(len(args), 2) |
65 self.assertEquals(len(kwargs), 0) | 65 self.assertEqual(len(kwargs), 0) |
66 self.assertEquals(args[0], mod_name) | 66 self.assertEqual(args[0], mod_name) |
67 self.assertTrue(args[1] is None) | 67 self.assertTrue(args[1] is None) |
68 | 68 |
69 def test_with_path(self): | 69 def test_with_path(self): |
70 # [path set] | 70 # [path set] |
71 pkg_name = 'pkg' | 71 pkg_name = 'pkg' |
72 mod_name = pkg_name + '.module' | 72 mod_name = pkg_name + '.module' |
73 path = [42] | 73 path = [42] |
74 assert '.' in mod_name | 74 assert '.' in mod_name |
75 with util.mock_modules(pkg_name+'.__init__', mod_name) as importer: | 75 with util.mock_modules(pkg_name+'.__init__', mod_name) as importer: |
76 importer.modules[pkg_name].__path__ = path | 76 importer.modules[pkg_name].__path__ = path |
77 log, wrapped_call = self.log(importer.find_module) | 77 log, wrapped_call = self.log(importer.find_module) |
78 importer.find_module = MethodType(wrapped_call, importer) | 78 importer.find_module = MethodType(wrapped_call, importer) |
79 with util.import_state(meta_path=[importer]): | 79 with util.import_state(meta_path=[importer]): |
80 import_util.import_(mod_name) | 80 import_util.import_(mod_name) |
81 assert len(log) == 2 | 81 assert len(log) == 2 |
82 args = log[1][0] | 82 args = log[1][0] |
83 kwargs = log[1][1] | 83 kwargs = log[1][1] |
84 # Assuming all arguments are positional. | 84 # Assuming all arguments are positional. |
85 self.assertTrue(not kwargs) | 85 self.assertTrue(not kwargs) |
86 self.assertEquals(args[0], mod_name) | 86 self.assertEqual(args[0], mod_name) |
87 self.assertTrue(args[1] is path) | 87 self.assertTrue(args[1] is path) |
88 | 88 |
89 | 89 |
90 | 90 |
91 def test_main(): | 91 def test_main(): |
92 from test.support import run_unittest | 92 from test.support import run_unittest |
93 run_unittest(CallingOrder, CallSignature) | 93 run_unittest(CallingOrder, CallSignature) |
94 | 94 |
95 | 95 |
96 if __name__ == '__main__': | 96 if __name__ == '__main__': |
97 test_main() | 97 test_main() |
OLD | NEW |