|
7 | 7 | import os |
8 | 8 | import os.path |
9 | 9 | import tempfile |
10 | | -import types |
11 | 10 | import shutil |
12 | 11 | import zipfile |
13 | 12 |
|
@@ -101,6 +100,83 @@ def test_unreadable_dir_on_syspath(self): |
101 | 100 | for t in pkgutil.walk_packages(path=[self.dirname]): |
102 | 101 | self.fail("unexpected package found") |
103 | 102 |
|
| 103 | + def test_walkpackages_filesys(self): |
| 104 | + pkg1 = 'test_walkpackages_filesys' |
| 105 | + pkg1_dir = os.path.join(self.dirname, pkg1) |
| 106 | + os.mkdir(pkg1_dir) |
| 107 | + f = open(os.path.join(pkg1_dir, '__init__.py'), "wb") |
| 108 | + f.close() |
| 109 | + os.mkdir(os.path.join(pkg1_dir, 'sub')) |
| 110 | + f = open(os.path.join(pkg1_dir, 'sub', '__init__.py'), "wb") |
| 111 | + f.close() |
| 112 | + f = open(os.path.join(pkg1_dir, 'sub', 'mod.py'), "wb") |
| 113 | + f.close() |
| 114 | + |
| 115 | + # Now, to juice it up, let's add the opposite packages, too. |
| 116 | + pkg2 = 'sub' |
| 117 | + pkg2_dir = os.path.join(self.dirname, pkg2) |
| 118 | + os.mkdir(pkg2_dir) |
| 119 | + f = open(os.path.join(pkg2_dir, '__init__.py'), "wb") |
| 120 | + f.close() |
| 121 | + os.mkdir(os.path.join(pkg2_dir, 'test_walkpackages_filesys')) |
| 122 | + f = open(os.path.join(pkg2_dir, 'test_walkpackages_filesys', '__init__.py'), "wb") |
| 123 | + f.close() |
| 124 | + f = open(os.path.join(pkg2_dir, 'test_walkpackages_filesys', 'mod.py'), "wb") |
| 125 | + f.close() |
| 126 | + |
| 127 | + expected = [ |
| 128 | + 'sub', |
| 129 | + 'sub.test_walkpackages_filesys', |
| 130 | + 'sub.test_walkpackages_filesys.mod', |
| 131 | + 'test_walkpackages_filesys', |
| 132 | + 'test_walkpackages_filesys.sub', |
| 133 | + 'test_walkpackages_filesys.sub.mod', |
| 134 | + ] |
| 135 | + actual= [e[1] for e in pkgutil.walk_packages([self.dirname])] |
| 136 | + self.assertEqual(actual, expected) |
| 137 | + |
| 138 | + for pkg in expected: |
| 139 | + if pkg.endswith('mod'): |
| 140 | + continue |
| 141 | + del sys.modules[pkg] |
| 142 | + |
| 143 | + def test_walkpackages_zipfile(self): |
| 144 | + """Tests the same as test_walkpackages_filesys, only with a zip file.""" |
| 145 | + |
| 146 | + zip = 'test_walkpackages_zipfile.zip' |
| 147 | + pkg1 = 'test_walkpackages_zipfile' |
| 148 | + pkg2 = 'sub' |
| 149 | + |
| 150 | + zip_file = os.path.join(self.dirname, zip) |
| 151 | + z = zipfile.ZipFile(zip_file, 'w') |
| 152 | + z.writestr(pkg2 + '/__init__.py', "") |
| 153 | + z.writestr(pkg2 + '/' + pkg1 + '/__init__.py', "") |
| 154 | + z.writestr(pkg2 + '/' + pkg1 + '/mod.py', "") |
| 155 | + z.writestr(pkg1 + '/__init__.py', "") |
| 156 | + z.writestr(pkg1 + '/' + pkg2 + '/__init__.py', "") |
| 157 | + z.writestr(pkg1 + '/' + pkg2 + '/mod.py', "") |
| 158 | + z.close() |
| 159 | + |
| 160 | + sys.path.insert(0, zip_file) |
| 161 | + expected = [ |
| 162 | + 'sub', |
| 163 | + 'sub.test_walkpackages_zipfile', |
| 164 | + 'sub.test_walkpackages_zipfile.mod', |
| 165 | + 'test_walkpackages_zipfile', |
| 166 | + 'test_walkpackages_zipfile.sub', |
| 167 | + 'test_walkpackages_zipfile.sub.mod', |
| 168 | + ] |
| 169 | + actual= [e[1] for e in pkgutil.walk_packages([zip_file])] |
| 170 | + self.assertEqual(actual, expected) |
| 171 | + del sys.path[0] |
| 172 | + |
| 173 | + for pkg in expected: |
| 174 | + if pkg.endswith('mod'): |
| 175 | + continue |
| 176 | + del sys.modules[pkg] |
| 177 | + |
| 178 | + |
| 179 | + |
104 | 180 | class PkgutilPEP302Tests(unittest.TestCase): |
105 | 181 |
|
106 | 182 | class MyTestLoader(object): |
@@ -324,11 +400,11 @@ def check_deprecated(self): |
324 | 400 |
|
325 | 401 | def test_importer_deprecated(self): |
326 | 402 | with self.check_deprecated(): |
327 | | - x = pkgutil.ImpImporter("") |
| 403 | + pkgutil.ImpImporter("") |
328 | 404 |
|
329 | 405 | def test_loader_deprecated(self): |
330 | 406 | with self.check_deprecated(): |
331 | | - x = pkgutil.ImpLoader("", "", "", "") |
| 407 | + pkgutil.ImpLoader("", "", "", "") |
332 | 408 |
|
333 | 409 | def test_get_loader_avoids_emulation(self): |
334 | 410 | with check_warnings() as w: |
|
0 commit comments