diff options
author | Eric V. Smith <eric@trueblade.com> | 2012-05-16 00:44:06 (GMT) |
---|---|---|
committer | Eric V. Smith <eric@trueblade.com> | 2012-05-16 00:44:06 (GMT) |
commit | a790c9b6d6b5b706f1b29be316f2e41d24eeb295 (patch) | |
tree | e1a115296b5dfc2fff15c81690bd147ef0b3006f /Lib | |
parent | 0ea91cb5c6eac161722b3109ac636d5c782bf454 (diff) | |
download | cpython-a790c9b6d6b5b706f1b29be316f2e41d24eeb295.zip cpython-a790c9b6d6b5b706f1b29be316f2e41d24eeb295.tar.gz cpython-a790c9b6d6b5b706f1b29be316f2e41d24eeb295.tar.bz2 |
Issue #14817: Add rudimentary tests for pkgutil.extend_path.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_pkgutil.py | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index f4e0323..d5d7448 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -137,8 +137,54 @@ class PkgutilPEP302Tests(unittest.TestCase): self.assertEqual(foo.loads, 1) del sys.modules['foo'] + +class ExtendPathTests(unittest.TestCase): + def create_init(self, pkgname): + dirname = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, dirname) + sys.path.insert(0, dirname) + + pkgdir = os.path.join(dirname, pkgname) + os.mkdir(pkgdir) + with open(os.path.join(pkgdir, '__init__.py'), 'w') as fl: + fl.write('from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n') + + return dirname + + def create_submodule(self, dirname, pkgname, submodule_name, value): + module_name = os.path.join(dirname, pkgname, submodule_name + '.py') + with open(module_name, 'w') as fl: + print('value={}'.format(value), file=fl) + + def setUp(self): + # Create 2 directories on sys.path + self.pkgname = 'foo' + self.dirname_0 = self.create_init(self.pkgname) + self.dirname_1 = self.create_init(self.pkgname) + + def tearDown(self): + del sys.path[0] + del sys.path[0] + + def test_simple(self): + self.create_submodule(self.dirname_0, self.pkgname, 'bar', 0) + self.create_submodule(self.dirname_1, self.pkgname, 'baz', 1) + import foo.bar + import foo.baz + # Ensure we read the expected values + self.assertEqual(foo.bar.value, 0) + self.assertEqual(foo.baz.value, 1) + + # Ensure the path is set up correctly + self.assertEqual(sorted(foo.__path__), + sorted([os.path.join(self.dirname_0, self.pkgname), + os.path.join(self.dirname_1, self.pkgname)])) + + # XXX: test .pkg files + + def test_main(): - run_unittest(PkgutilTests, PkgutilPEP302Tests) + run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests) # this is necessary if test is run repeated (like when finding leaks) import zipimport zipimport._zip_directory_cache.clear() |