From 9e68408597467183d174876f76e5aa3e27e3632a Mon Sep 17 00:00:00 2001 From: David Wilson Date: Wed, 17 Aug 2016 14:24:18 +0100 Subject: [PATCH] Draft importer tests. --- econtext/core.py | 3 +- tests/importer_test.py | 115 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 tests/importer_test.py diff --git a/econtext/core.py b/econtext/core.py index b4689887..872bf57e 100644 --- a/econtext/core.py +++ b/econtext/core.py @@ -200,13 +200,14 @@ class Importer(object): pkg_present, path, data = ret mod = sys.modules.setdefault(fullname, imp.new_module(fullname)) + mod.__file__ = path mod.__loader__ = self if pkg_present is not None: # it's a package. mod.__path__ = [] mod.__package__ = fullname self._present[fullname] = pkg_present else: - mod.__package__ = fullname.rpartition('.')[0] + mod.__package__ = fullname.rpartition('.')[0] or None code = compile(zlib.decompress(data), 'master:' + path, 'exec') exec code in vars(mod) return mod diff --git a/tests/importer_test.py b/tests/importer_test.py new file mode 100644 index 00000000..647d95ff --- /dev/null +++ b/tests/importer_test.py @@ -0,0 +1,115 @@ + +import email.utils +import sys +import types +import unittest +import zlib + +import mock +import pytest + +import econtext.core +import testlib + + +class ImporterMixin(object): + modname = None + + def setUp(self): + super(ImporterMixin, self).setUp() + self.context = mock.Mock() + self.importer = econtext.core.Importer(self.context) + + def tearDown(self): + sys.modules.pop(self.modname, None) + super(ImporterMixin, self).tearDown() + + +class LoadModuleTest(ImporterMixin, unittest.TestCase): + data = zlib.compress("data = 1\n\n") + path = 'fake_module.py' + modname = 'fake_module' + response = (None, path, data) + + def test_no_such_module(self): + self.context.enqueue_await_reply.return_value = None + self.assertRaises(ImportError, + lambda: self.importer.load_module(self.modname)) + + def test_module_added_to_sys_modules(self): + self.context.enqueue_await_reply.return_value = self.response + mod = self.importer.load_module(self.modname) + self.assertTrue(sys.modules[self.modname] is mod) + self.assertTrue(isinstance(mod, types.ModuleType)) + + def test_module_file_set(self): + self.context.enqueue_await_reply.return_value = self.response + mod = self.importer.load_module(self.modname) + self.assertEquals(mod.__file__, self.path) + + def test_module_loader_set(self): + self.context.enqueue_await_reply.return_value = self.response + mod = self.importer.load_module(self.modname) + self.assertTrue(mod.__loader__ is self.importer) + + def test_module_package_unset(self): + self.context.enqueue_await_reply.return_value = self.response + mod = self.importer.load_module(self.modname) + self.assertTrue(mod.__package__ is None) + + +class LoadSubmoduleTest(ImporterMixin, unittest.TestCase): + data = zlib.compress("data = 1\n\n") + path = 'fake_module.py' + modname = 'mypkg.fake_module' + response = (None, path, data) + + def test_module_package_unset(self): + self.context.enqueue_await_reply.return_value = self.response + mod = self.importer.load_module(self.modname) + self.assertEquals(mod.__package__, 'mypkg') + + +class LoadModulePackageTest(ImporterMixin, unittest.TestCase): + data = zlib.compress("func = lambda: 1\n\n") + path = 'fake_pkg/__init__.py' + modname = 'fake_pkg' + response = ([], path, data) + + def test_module_file_set(self): + self.context.enqueue_await_reply.return_value = self.response + mod = self.importer.load_module(self.modname) + self.assertEquals(mod.__file__, self.path) + + def test_module_loader_set(self): + self.context.enqueue_await_reply.return_value = self.response + mod = self.importer.load_module(self.modname) + self.assertTrue(mod.__loader__ is self.importer) + + def test_module_path_present(self): + self.context.enqueue_await_reply.return_value = self.response + mod = self.importer.load_module(self.modname) + self.assertEquals(mod.__path__, []) + + def test_module_package_set(self): + self.context.enqueue_await_reply.return_value = self.response + mod = self.importer.load_module(self.modname) + self.assertEquals(mod.__package__, self.modname) + + def test_module_data(self): + self.context.enqueue_await_reply.return_value = self.response + mod = self.importer.load_module(self.modname) + self.assertTrue(isinstance(mod.func, types.FunctionType)) + self.assertEquals(mod.func.__module__, self.modname) + + +class EmailParseAddrSysTest(testlib.BrokerMixin, unittest.TestCase): + @pytest.fixture(autouse=True) + def initdir(self, caplog): + self.caplog = caplog + + def test_sys_module_not_fetched(self): + # An old version of core.Importer would request the email.sys module + # while executing email.utils.parseaddr(). Ensure this needless + # roundtrip has not reappeared. + pass