2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
import email.utils
|
|
|
|
import sys
|
|
|
|
import types
|
|
|
|
import zlib
|
|
|
|
|
|
|
|
import mock
|
|
|
|
import pytest
|
2017-11-11 22:59:38 +00:00
|
|
|
import unittest2 as unittest
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
import mitogen.core
|
|
|
|
import testlib
|
|
|
|
|
|
|
|
|
2017-09-24 23:50:50 +00:00
|
|
|
class ImporterMixin(testlib.RouterMixin):
|
2017-09-15 06:24:41 +00:00
|
|
|
modname = None
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(ImporterMixin, self).setUp()
|
|
|
|
self.context = mock.Mock()
|
2017-10-01 10:00:43 +00:00
|
|
|
self.importer = mitogen.core.Importer(self.context, '')
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.modules.pop(self.modname, None)
|
|
|
|
super(ImporterMixin, self).tearDown()
|
|
|
|
|
|
|
|
|
2017-10-01 10:00:43 +00:00
|
|
|
class LoadModuleTest(ImporterMixin, testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
data = zlib.compress("data = 1\n\n")
|
|
|
|
path = 'fake_module.py'
|
|
|
|
modname = 'fake_module'
|
|
|
|
response = (None, path, data)
|
|
|
|
|
|
|
|
def test_no_such_module(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = None
|
2017-09-15 06:24:41 +00:00
|
|
|
self.assertRaises(ImportError,
|
|
|
|
lambda: self.importer.load_module(self.modname))
|
|
|
|
|
|
|
|
def test_module_added_to_sys_modules(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIs(sys.modules[self.modname], mod)
|
|
|
|
self.assertIsInstance(mod, types.ModuleType)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_module_file_set(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
self.assertEquals(mod.__file__, 'master:' + self.path)
|
|
|
|
|
|
|
|
def test_module_loader_set(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIs(mod.__loader__, self.importer)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_module_package_unset(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIsNone(mod.__package__)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
|
2017-10-01 10:00:43 +00:00
|
|
|
class LoadSubmoduleTest(ImporterMixin, testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
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):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
self.assertEquals(mod.__package__, 'mypkg')
|
|
|
|
|
|
|
|
|
2017-10-01 10:00:43 +00:00
|
|
|
class LoadModulePackageTest(ImporterMixin, testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
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):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
self.assertEquals(mod.__file__, 'master:' + self.path)
|
|
|
|
|
|
|
|
def test_get_filename(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
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):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
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):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIs(mod.__loader__, self.importer)
|
2017-09-15 06:24:41 +00:00
|
|
|
|
|
|
|
def test_module_path_present(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
self.assertEquals(mod.__path__, [])
|
|
|
|
|
|
|
|
def test_module_package_set(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
|
|
|
self.assertEquals(mod.__package__, self.modname)
|
|
|
|
|
|
|
|
def test_module_data(self):
|
2017-09-24 23:50:50 +00:00
|
|
|
self.context.send_await.return_value = self.response
|
2017-09-15 06:24:41 +00:00
|
|
|
mod = self.importer.load_module(self.modname)
|
2017-11-11 23:08:08 +00:00
|
|
|
self.assertIsInstance(mod.func, types.FunctionType)
|
2017-09-15 06:24:41 +00:00
|
|
|
self.assertEquals(mod.func.__module__, self.modname)
|
|
|
|
|
|
|
|
|
2017-10-01 10:00:43 +00:00
|
|
|
class EmailParseAddrSysTest(testlib.RouterMixin, testlib.TestCase):
|
2017-09-15 06:24:41 +00:00
|
|
|
@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
|
2017-10-01 10:00:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|