You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mitogen/tests/importer_test.py

132 lines
4.4 KiB
Python

7 years ago
import email.utils
import sys
import types
import zlib
import mock
import pytest
import unittest2
7 years ago
import mitogen.core
import testlib
class ImporterMixin(testlib.RouterMixin):
7 years ago
modname = None
def setUp(self):
super(ImporterMixin, self).setUp()
self.context = mock.Mock()
self.importer = mitogen.core.Importer(self.context, '')
7 years ago
def tearDown(self):
sys.modules.pop(self.modname, None)
super(ImporterMixin, self).tearDown()
class LoadModuleTest(ImporterMixin, testlib.TestCase):
7 years ago
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.send_await.return_value = None
7 years ago
self.assertRaises(ImportError,
lambda: self.importer.load_module(self.modname))
def test_module_added_to_sys_modules(self):
self.context.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
self.assertIs(sys.modules[self.modname], mod)
self.assertIsInstance(mod, types.ModuleType)
7 years ago
def test_module_file_set(self):
self.context.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
self.assertEquals(mod.__file__, 'master:' + self.path)
def test_module_loader_set(self):
self.context.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
self.assertIs(mod.__loader__, self.importer)
7 years ago
def test_module_package_unset(self):
self.context.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
self.assertIsNone(mod.__package__)
7 years ago
class LoadSubmoduleTest(ImporterMixin, testlib.TestCase):
7 years ago
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.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
self.assertEquals(mod.__package__, 'mypkg')
class LoadModulePackageTest(ImporterMixin, testlib.TestCase):
7 years ago
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.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
self.assertEquals(mod.__file__, 'master:' + self.path)
def test_get_filename(self):
self.context.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
filename = mod.__loader__.get_filename(self.modname)
self.assertEquals('master:fake_pkg/__init__.py', filename)
def test_get_source(self):
self.context.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
source = mod.__loader__.get_source(self.modname)
self.assertEquals(source, zlib.decompress(self.data))
def test_module_loader_set(self):
self.context.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
self.assertIs(mod.__loader__, self.importer)
7 years ago
def test_module_path_present(self):
self.context.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
self.assertEquals(mod.__path__, [])
def test_module_package_set(self):
self.context.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
self.assertEquals(mod.__package__, self.modname)
def test_module_data(self):
self.context.send_await.return_value = self.response
7 years ago
mod = self.importer.load_module(self.modname)
self.assertIsInstance(mod.func, types.FunctionType)
7 years ago
self.assertEquals(mod.func.__module__, self.modname)
class EmailParseAddrSysTest(testlib.RouterMixin, testlib.TestCase):
7 years ago
@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
if __name__ == '__main__':
unittest2.main()