From 621f2533f73264877f5c7ceec5a22ef9bdc6031d Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Mon, 26 Feb 2018 23:34:02 +0000 Subject: [PATCH] tests: Add mitogen.core.is_blacklisted_import() tests These test the current behviour, which may not be exactly the intended behaviour. Refs #98 --- tests/importer_test.py | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/importer_test.py b/tests/importer_test.py index 5fe65f45..9d533752 100644 --- a/tests/importer_test.py +++ b/tests/importer_test.py @@ -9,6 +9,8 @@ import pytest import unittest2 import mitogen.core +import mitogen.utils + import testlib @@ -25,6 +27,55 @@ class ImporterMixin(testlib.RouterMixin): super(ImporterMixin, self).tearDown() +class ImporterBlacklist(testlib.TestCase): + def test_is_blacklisted_import_default(self): + importer = mitogen.core.Importer( + router=mock.Mock(), context=None, core_src='', + ) + self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg')) + self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod')) + self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg')) + self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins')) + + def test_is_blacklisted_import_just_whitelist(self): + importer = mitogen.core.Importer( + router=mock.Mock(), context=None, core_src='', + whitelist=('mypkg',), + ) + self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg')) + self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins')) + + def test_is_blacklisted_import_just_blacklist(self): + importer = mitogen.core.Importer( + router=mock.Mock(), context=None, core_src='', + blacklist=('mypkg',), + ) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod')) + self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg')) + self.assertFalse(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins')) + + def test_is_blacklisted_import_whitelist_and_blacklist(self): + importer = mitogen.core.Importer( + router=mock.Mock(), context=None, core_src='', + whitelist=('mypkg',), blacklist=('mypkg',), + ) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'mypkg.mod')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'otherpkg.mod')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, '__builtin__')) + self.assertTrue(mitogen.core.is_blacklisted_import(importer, 'builtins')) + + class LoadModuleTest(ImporterMixin, testlib.TestCase): data = zlib.compress("data = 1\n\n") path = 'fake_module.py'