blob: f4e03234e94911fb3bb379582fc1c0f14d5b9d84 [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
140def test_main():
141 run_unittest(PkgutilTests, PkgutilPEP302Tests)
Christian Heimese57950f2008-04-21 13:08:03 +0000142 # this is necessary if test is run repeated (like when finding leaks)
143 import zipimport
144 zipimport._zip_directory_cache.clear()
Christian Heimesdae2a892008-04-19 00:55:37 +0000145
146if __name__ == '__main__':
147 test_main()