diff options
author | Brett Cannon <brett@python.org> | 2012-07-20 18:48:53 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-07-20 18:48:53 (GMT) |
commit | 45a5e3afe52ed89f298242143c5f7e2bb992ac63 (patch) | |
tree | 2255a23f991f1452335d865939f6c30d7b2d7d48 /Lib/test/test_importlib/frozen | |
parent | 4afc1c08d04a9c083b654178ea4f947563510836 (diff) | |
download | cpython-45a5e3afe52ed89f298242143c5f7e2bb992ac63.zip cpython-45a5e3afe52ed89f298242143c5f7e2bb992ac63.tar.gz cpython-45a5e3afe52ed89f298242143c5f7e2bb992ac63.tar.bz2 |
Issue #15168: Move importlb.test to test.test_importlib.
This should make the Linux distros happy as it is now easier to leave
importlib's tests out of their base Python distribution.
Diffstat (limited to 'Lib/test/test_importlib/frozen')
-rw-r--r-- | Lib/test/test_importlib/frozen/__init__.py | 13 | ||||
-rw-r--r-- | Lib/test/test_importlib/frozen/test_finder.py | 47 | ||||
-rw-r--r-- | Lib/test/test_importlib/frozen/test_loader.py | 121 |
3 files changed, 181 insertions, 0 deletions
diff --git a/Lib/test/test_importlib/frozen/__init__.py b/Lib/test/test_importlib/frozen/__init__.py new file mode 100644 index 0000000..9ef103b --- /dev/null +++ b/Lib/test/test_importlib/frozen/__init__.py @@ -0,0 +1,13 @@ +from .. import test_suite +import os.path +import unittest + + +def test_suite(): + directory = os.path.dirname(__file__) + return test_suite('importlib.test.frozen', directory) + + +if __name__ == '__main__': + from test.support import run_unittest + run_unittest(test_suite()) diff --git a/Lib/test/test_importlib/frozen/test_finder.py b/Lib/test/test_importlib/frozen/test_finder.py new file mode 100644 index 0000000..fa0c2a0 --- /dev/null +++ b/Lib/test/test_importlib/frozen/test_finder.py @@ -0,0 +1,47 @@ +from importlib import machinery +from .. import abc + +import unittest + + +class FinderTests(abc.FinderTests): + + """Test finding frozen modules.""" + + def find(self, name, path=None): + finder = machinery.FrozenImporter + return finder.find_module(name, path) + + def test_module(self): + name = '__hello__' + loader = self.find(name) + self.assertTrue(hasattr(loader, 'load_module')) + + def test_package(self): + loader = self.find('__phello__') + self.assertTrue(hasattr(loader, 'load_module')) + + def test_module_in_package(self): + loader = self.find('__phello__.spam', ['__phello__']) + self.assertTrue(hasattr(loader, 'load_module')) + + def test_package_in_package(self): + # No frozen package within another package to test with. + pass + + def test_package_over_module(self): + # No easy way to test. + pass + + def test_failure(self): + loader = self.find('<not real>') + self.assertIsNone(loader) + + +def test_main(): + from test.support import run_unittest + run_unittest(FinderTests) + + +if __name__ == '__main__': + test_main() diff --git a/Lib/test/test_importlib/frozen/test_loader.py b/Lib/test/test_importlib/frozen/test_loader.py new file mode 100644 index 0000000..4b8ec15 --- /dev/null +++ b/Lib/test/test_importlib/frozen/test_loader.py @@ -0,0 +1,121 @@ +from importlib import machinery +import imp +import unittest +from .. import abc +from .. import util +from test.support import captured_stdout + +class LoaderTests(abc.LoaderTests): + + def test_module(self): + with util.uncache('__hello__'), captured_stdout() as stdout: + module = machinery.FrozenImporter.load_module('__hello__') + check = {'__name__': '__hello__', + '__package__': '', + '__loader__': machinery.FrozenImporter, + } + for attr, value in check.items(): + self.assertEqual(getattr(module, attr), value) + self.assertEqual(stdout.getvalue(), 'Hello world!\n') + self.assertFalse(hasattr(module, '__file__')) + + def test_package(self): + with util.uncache('__phello__'), captured_stdout() as stdout: + module = machinery.FrozenImporter.load_module('__phello__') + check = {'__name__': '__phello__', + '__package__': '__phello__', + '__path__': ['__phello__'], + '__loader__': machinery.FrozenImporter, + } + for attr, value in check.items(): + attr_value = getattr(module, attr) + self.assertEqual(attr_value, value, + "for __phello__.%s, %r != %r" % + (attr, attr_value, value)) + self.assertEqual(stdout.getvalue(), 'Hello world!\n') + self.assertFalse(hasattr(module, '__file__')) + + def test_lacking_parent(self): + with util.uncache('__phello__', '__phello__.spam'), \ + captured_stdout() as stdout: + module = machinery.FrozenImporter.load_module('__phello__.spam') + check = {'__name__': '__phello__.spam', + '__package__': '__phello__', + '__loader__': machinery.FrozenImporter, + } + for attr, value in check.items(): + attr_value = getattr(module, attr) + self.assertEqual(attr_value, value, + "for __phello__.spam.%s, %r != %r" % + (attr, attr_value, value)) + self.assertEqual(stdout.getvalue(), 'Hello world!\n') + self.assertFalse(hasattr(module, '__file__')) + + def test_module_reuse(self): + with util.uncache('__hello__'), captured_stdout() as stdout: + module1 = machinery.FrozenImporter.load_module('__hello__') + module2 = machinery.FrozenImporter.load_module('__hello__') + self.assertIs(module1, module2) + self.assertEqual(stdout.getvalue(), + 'Hello world!\nHello world!\n') + + def test_module_repr(self): + with util.uncache('__hello__'), captured_stdout(): + module = machinery.FrozenImporter.load_module('__hello__') + self.assertEqual(repr(module), + "<module '__hello__' (frozen)>") + + def test_state_after_failure(self): + # No way to trigger an error in a frozen module. + pass + + def test_unloadable(self): + assert machinery.FrozenImporter.find_module('_not_real') is None + with self.assertRaises(ImportError) as cm: + machinery.FrozenImporter.load_module('_not_real') + self.assertEqual(cm.exception.name, '_not_real') + + +class InspectLoaderTests(unittest.TestCase): + + """Tests for the InspectLoader methods for FrozenImporter.""" + + def test_get_code(self): + # Make sure that the code object is good. + name = '__hello__' + with captured_stdout() as stdout: + code = machinery.FrozenImporter.get_code(name) + mod = imp.new_module(name) + exec(code, mod.__dict__) + self.assertTrue(hasattr(mod, 'initialized')) + self.assertEqual(stdout.getvalue(), 'Hello world!\n') + + def test_get_source(self): + # Should always return None. + result = machinery.FrozenImporter.get_source('__hello__') + self.assertIsNone(result) + + def test_is_package(self): + # Should be able to tell what is a package. + test_for = (('__hello__', False), ('__phello__', True), + ('__phello__.spam', False)) + for name, is_package in test_for: + result = machinery.FrozenImporter.is_package(name) + self.assertEqual(bool(result), is_package) + + def test_failure(self): + # Raise ImportError for modules that are not frozen. + for meth_name in ('get_code', 'get_source', 'is_package'): + method = getattr(machinery.FrozenImporter, meth_name) + with self.assertRaises(ImportError) as cm: + method('importlib') + self.assertEqual(cm.exception.name, 'importlib') + + +def test_main(): + from test.support import run_unittest + run_unittest(LoaderTests, InspectLoaderTests) + + +if __name__ == '__main__': + test_main() |