"""Tests for stevedore.extension
"""

import mock
import pkg_resources

from stevedore import driver
from stevedore.tests import test_extension
from stevedore.tests import utils


class TestCallback(utils.TestCase):
    def test_detect_plugins(self):
        em = driver.DriverManager('stevedore.test.extension', 't1')
        names = sorted(em.names())
        self.assertEqual(names, ['t1'])

    def test_call(self):
        def invoke(ext, *args, **kwds):
            return (ext.name, args, kwds)
        em = driver.DriverManager('stevedore.test.extension', 't1')
        result = em(invoke, 'a', b='C')
        self.assertEqual(result, ('t1', ('a',), {'b': 'C'}))

    def test_driver_property_not_invoked_on_load(self):
        em = driver.DriverManager('stevedore.test.extension', 't1',
                                  invoke_on_load=False)
        d = em.driver
        self.assertIs(d, test_extension.FauxExtension)

    def test_driver_property_invoked_on_load(self):
        em = driver.DriverManager('stevedore.test.extension', 't1',
                                  invoke_on_load=True)
        d = em.driver
        self.assertIsInstance(d, test_extension.FauxExtension)

    def test_no_drivers(self):
        try:
            driver.DriverManager('stevedore.test.extension.none', 't1')
        except RuntimeError as err:
            self.assertIn("No 'stevedore.test.extension.none' driver found",
                          str(err))

    def test_bad_driver(self):
        try:
            driver.DriverManager('stevedore.test.extension', 'e2')
        except ImportError:
            pass
        else:
            self.assertEquals(False, "No error raised")

    def test_multiple_drivers(self):
        # The idea for this test was contributed by clayg:
        # https://gist.github.com/clayg/6311348
        fep_name = 'stevedore.extension.ExtensionManager._find_entry_points'
        with mock.patch(fep_name) as fep:
            fep.return_value = [
                pkg_resources.EntryPoint.parse('backend = pkg1:driver'),
                pkg_resources.EntryPoint.parse('backend = pkg2:driver'),
            ]
            for ep in fep.return_value:
                ep.load = lambda *args, **kwds: 'pkg backend'
            try:
                driver.DriverManager('stevedore.test.multiple_drivers',
                                     'backend')
            except RuntimeError as err:
                self.assertIn("Multiple", str(err))
            fep.assert_called_with('stevedore.test.multiple_drivers')

@@ -35,20 +35,8 @@
 %22%22%0A%0A
-import mock%0A
 impo
@@ -82,16 +82,48 @@
  driver%0A
+from stevedore import extension%0A
 from ste
@@ -1853,223 +1853,282 @@
     
-fep_name = 'stevedore.extension.ExtensionManager._find_entry_points'%0A        with mock.patch(fep_name) as fep:%0A            fep.return_value = %5B%0A                pkg_resources.EntryPoint.parse('backend = pkg1:driver')
+extensions = %5B%0A            extension.Extension(%0A                'backend',%0A                pkg_resources.EntryPoint.parse('backend = pkg1:driver'),%0A                'pkg backend',%0A                None,%0A            ),%0A            extension.Extension(%0A                'backend'
 ,%0A  
@@ -2214,116 +2214,74 @@
     
-%5D%0A
     
-        for ep in fep.return_value:%0A                ep.load = lambda *args, **kwds: 'pkg backend'%0A
+'pkg backend',%0A                None,%0A            ),%0A    
     
+%5D%0A
     
@@ -2293,35 +2293,36 @@
 ry:%0A            
-   
+dm =
  driver.DriverMa
@@ -2330,96 +2330,155 @@
 ager
-('stevedore.test.multiple_drivers',%0A                                     'backend')%0A    
+.make_test_instance(extensions%5B0%5D)%0A            # Call the initialization code that verifies the extension%0A            dm._init_plugins(extensions)%0A
     
@@ -2501,36 +2501,32 @@
 meError as err:%0A
-    
             self
@@ -2569,66 +2569,60 @@
     
-    fep.assert_called_with('stevedore.test.multiple_drivers
+else:%0A            self.fail('Should have had an error
 ')%0A

Fix test for finding multiple drivers