class ThirdPartyPluginLoaderTest (unittest.TestCase):

    def setUp(self):
        super(ThirdPartyPluginLoaderTest, self).setUp()
        self.project = mock()
        self.loader = ThirdPartyPluginLoader(mock())

    def tearDown(self):
        super(ThirdPartyPluginLoaderTest, self).tearDown()
        unstub()

    def test_should_raise_exception_when_requiring_plugin_and_plugin_is_not_found(self):
        when(builtin_module).__import__(
            "spam").thenRaise(ImportError())

        self.assertRaises(
            MissingPluginException, self.loader.load_plugin, self.project, "spam")

        verify(builtin_module).__import__("spam")

    def test_should_import_plugin_when_requiring_plugin_and_plugin_is_found_as_third_party(self):
        old_module = sys.modules.get("spam")
        try:
            plugin_module = mock()
            sys.modules["spam"] = plugin_module
            when(builtin_module).__import__(
                "spam").thenReturn(plugin_module)

            self.loader.load_plugin(self.project, "spam")

            verify(builtin_module).__import__("spam")
        finally:
            del sys.modules["spam"]
            if old_module:
                sys.modules["spam"] = old_module
class ThirdPartyPluginLoaderTest(unittest.TestCase):
    def setUp(self):
        self.project = mock()
        self.loader = ThirdPartyPluginLoader(mock())

    def tearDown(self):
        unstub()

    def test_should_raise_exception_when_requiring_plugin_and_plugin_is_not_found(
            self):
        when(builtin_module).__import__("spam").thenRaise(ImportError())

        self.assertRaises(MissingPluginException, self.loader.load_plugin,
                          self.project, "spam")

        verify(builtin_module).__import__("spam")

    def test_should_import_plugin_when_requiring_plugin_and_plugin_is_found_as_third_party(
            self):
        old_module = sys.modules.get("spam")
        try:
            plugin_module = mock()
            plugin_module.pyb_version = None
            sys.modules["spam"] = plugin_module
            when(builtin_module).__import__("spam").thenReturn(plugin_module)

            self.loader.load_plugin(self.project, "spam")

            verify(builtin_module).__import__("spam")
        finally:
            del sys.modules["spam"]
            if old_module:
                sys.modules["spam"] = old_module

    def test_should_remove_pypi_protocol_when_importing(self):
        old_module = sys.modules.get("spam")
        try:
            plugin_module = mock()
            plugin_module.pyb_version = None
            sys.modules["spam"] = plugin_module
            when(builtin_module).__import__("pypi:spam").thenReturn(
                plugin_module)

            self.loader.load_plugin(self.project, "spam")

            verify(builtin_module).__import__("spam")
        finally:
            del sys.modules["spam"]
            if old_module:
                sys.modules["spam"] = old_module