blob: a41b5f5fa3db96e9ace5a70006d3f1a31b78104d [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test.support import run_unittest
Christian Heimesdae2a892008-04-19 00:55:37 +00002import unittest
3import sys
4import imp
5import pkgutil
6import os
7import os.path
8import tempfile
9import shutil
10import zipfile
11
12
13
14class PkgutilTests(unittest.TestCase):
15
16 def setUp(self):
17 self.dirname = tempfile.mkdtemp()
Ned Deily7010a072011-10-07 12:01:40 -070018 self.addCleanup(shutil.rmtree, self.dirname)
Christian Heimesdae2a892008-04-19 00:55:37 +000019 sys.path.insert(0, self.dirname)
20
21 def tearDown(self):
22 del sys.path[0]
Christian Heimesdae2a892008-04-19 00:55:37 +000023
24 def test_getdata_filesys(self):
25 pkg = 'test_getdata_filesys'
26
27 # Include a LF and a CRLF, to test that binary data is read back
28 RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line'
29
30 # Make a package with some resources
31 package_dir = os.path.join(self.dirname, pkg)
32 os.mkdir(package_dir)
33 # Empty init.py
34 f = open(os.path.join(package_dir, '__init__.py'), "wb")
35 f.close()
36 # Resource files, res.txt, sub/res.txt
37 f = open(os.path.join(package_dir, 'res.txt'), "wb")
38 f.write(RESOURCE_DATA)
39 f.close()
40 os.mkdir(os.path.join(package_dir, 'sub'))
41 f = open(os.path.join(package_dir, 'sub', 'res.txt'), "wb")
42 f.write(RESOURCE_DATA)
43 f.close()
44
45 # Check we can read the resources
46 res1 = pkgutil.get_data(pkg, 'res.txt')
47 self.assertEqual(res1, RESOURCE_DATA)
48 res2 = pkgutil.get_data(pkg, 'sub/res.txt')
49 self.assertEqual(res2, RESOURCE_DATA)
50
51 del sys.modules[pkg]
52
53 def test_getdata_zipfile(self):
54 zip = 'test_getdata_zipfile.zip'
55 pkg = 'test_getdata_zipfile'
56
57 # Include a LF and a CRLF, to test that binary data is read back
58 RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line'
59
60 # Make a package with some resources
61 zip_file = os.path.join(self.dirname, zip)
62 z = zipfile.ZipFile(zip_file, 'w')
63
64 # Empty init.py
65 z.writestr(pkg + '/__init__.py', "")
66 # Resource files, res.txt, sub/res.txt
67 z.writestr(pkg + '/res.txt', RESOURCE_DATA)
68 z.writestr(pkg + '/sub/res.txt', RESOURCE_DATA)
69 z.close()
70
71 # Check we can read the resources
72 sys.path.insert(0, zip_file)
73 res1 = pkgutil.get_data(pkg, 'res.txt')
74 self.assertEqual(res1, RESOURCE_DATA)
75 res2 = pkgutil.get_data(pkg, 'sub/res.txt')
76 self.assertEqual(res2, RESOURCE_DATA)
Alexandre Vassalotti515a74f2009-07-05 06:42:44 +000077
78 names = []
79 for loader, name, ispkg in pkgutil.iter_modules([zip_file]):
80 names.append(name)
81 self.assertEqual(names, ['test_getdata_zipfile'])
82
Christian Heimesdae2a892008-04-19 00:55:37 +000083 del sys.path[0]
84
85 del sys.modules[pkg]
86
Ned Deilycaf5a222011-10-06 14:19:06 -070087 def test_unreadable_dir_on_syspath(self):
88 # issue7367 - walk_packages failed if unreadable dir on sys.path
89 package_name = "unreadable_package"
90 d = os.path.join(self.dirname, package_name)
91 # this does not appear to create an unreadable dir on Windows
92 # but the test should not fail anyway
93 os.mkdir(d, 0)
Ned Deily7010a072011-10-07 12:01:40 -070094 self.addCleanup(os.rmdir, d)
Ned Deilycaf5a222011-10-06 14:19:06 -070095 for t in pkgutil.walk_packages(path=[self.dirname]):
96 self.fail("unexpected package found")
Ned Deilycaf5a222011-10-06 14:19:06 -070097
Christian Heimesdae2a892008-04-19 00:55:37 +000098class PkgutilPEP302Tests(unittest.TestCase):
99
100 class MyTestLoader(object):
101 def load_module(self, fullname):
102 # Create an empty module
103 mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
104 mod.__file__ = "<%s>" % self.__class__.__name__
105 mod.__loader__ = self
106 # Make it a package
107 mod.__path__ = []
108 # Count how many times the module is reloaded
109 mod.__dict__['loads'] = mod.__dict__.get('loads',0) + 1
110 return mod
111
112 def get_data(self, path):
113 return "Hello, world!"
114
115 class MyTestImporter(object):
116 def find_module(self, fullname, path=None):
117 return PkgutilPEP302Tests.MyTestLoader()
118
119 def setUp(self):
120 sys.meta_path.insert(0, self.MyTestImporter())
121
122 def tearDown(self):
123 del sys.meta_path[0]
124
125 def test_getdata_pep302(self):
126 # Use a dummy importer/loader
127 self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
128 del sys.modules['foo']
129
130 def test_alreadyloaded(self):
131 # Ensure that get_data works without reloading - the "loads" module
132 # variable in the example loader should count how many times a reload
133 # occurs.
134 import foo
135 self.assertEqual(foo.loads, 1)
136 self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
137 self.assertEqual(foo.loads, 1)
138 del sys.modules['foo']
139
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400140
Eric V. Smith984b11f2012-05-24 20:21:04 -0400141# These tests, especially the setup and cleanup, are hideous. They
142# need to be cleaned up once issue 14715 is addressed.
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400143class ExtendPathTests(unittest.TestCase):
144 def create_init(self, pkgname):
145 dirname = tempfile.mkdtemp()
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400146 sys.path.insert(0, dirname)
147
148 pkgdir = os.path.join(dirname, pkgname)
149 os.mkdir(pkgdir)
150 with open(os.path.join(pkgdir, '__init__.py'), 'w') as fl:
151 fl.write('from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n')
152
153 return dirname
154
155 def create_submodule(self, dirname, pkgname, submodule_name, value):
156 module_name = os.path.join(dirname, pkgname, submodule_name + '.py')
157 with open(module_name, 'w') as fl:
158 print('value={}'.format(value), file=fl)
159
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400160 def test_simple(self):
Eric V. Smith984b11f2012-05-24 20:21:04 -0400161 pkgname = 'foo'
162 dirname_0 = self.create_init(pkgname)
163 dirname_1 = self.create_init(pkgname)
164 self.create_submodule(dirname_0, pkgname, 'bar', 0)
165 self.create_submodule(dirname_1, pkgname, 'baz', 1)
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400166 import foo.bar
167 import foo.baz
168 # Ensure we read the expected values
169 self.assertEqual(foo.bar.value, 0)
170 self.assertEqual(foo.baz.value, 1)
171
172 # Ensure the path is set up correctly
173 self.assertEqual(sorted(foo.__path__),
Eric V. Smith984b11f2012-05-24 20:21:04 -0400174 sorted([os.path.join(dirname_0, pkgname),
175 os.path.join(dirname_1, pkgname)]))
176
177 # Cleanup
178 shutil.rmtree(dirname_0)
179 shutil.rmtree(dirname_1)
180 del sys.path[0]
181 del sys.path[0]
182 del sys.modules['foo']
183 del sys.modules['foo.bar']
184 del sys.modules['foo.baz']
185
186 def test_mixed_namespace(self):
187 pkgname = 'foo'
188 dirname_0 = self.create_init(pkgname)
189 dirname_1 = self.create_init(pkgname)
190 self.create_submodule(dirname_0, pkgname, 'bar', 0)
191 # Turn this into a PEP 420 namespace package
192 os.unlink(os.path.join(dirname_0, pkgname, '__init__.py'))
193 self.create_submodule(dirname_1, pkgname, 'baz', 1)
194 import foo.bar
195 import foo.baz
196 # Ensure we read the expected values
197 self.assertEqual(foo.bar.value, 0)
198 self.assertEqual(foo.baz.value, 1)
199
200 # Ensure the path is set up correctly
201 self.assertEqual(sorted(foo.__path__),
202 sorted([os.path.join(dirname_0, pkgname),
203 os.path.join(dirname_1, pkgname)]))
204
205 # Cleanup
206 shutil.rmtree(dirname_0)
207 shutil.rmtree(dirname_1)
208 del sys.path[0]
209 del sys.path[0]
210 del sys.modules['foo']
211 del sys.modules['foo.bar']
212 del sys.modules['foo.baz']
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400213
214 # XXX: test .pkg files
215
216
Christian Heimesdae2a892008-04-19 00:55:37 +0000217def test_main():
Eric V. Smitha790c9b2012-05-15 20:44:06 -0400218 run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests)
Christian Heimese57950f2008-04-21 13:08:03 +0000219 # this is necessary if test is run repeated (like when finding leaks)
220 import zipimport
221 zipimport._zip_directory_cache.clear()
Christian Heimesdae2a892008-04-19 00:55:37 +0000222
223if __name__ == '__main__':
224 test_main()