~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-06-11 11:01:07 UTC
  • mfrom: (5286.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100611110107-t1zug4icv4ipz3dt
(vila) BZR_PLUGINS_AT should import packages properly to handle
        relative imports

Show diffs side-by-side

added added

removed removed

Lines of Context:
80
80
        if getattr(bzrlib.plugins, name, None) is not None:
81
81
            delattr(bzrlib.plugins, name)
82
82
 
 
83
    def _unregister_plugin_submodule(self, plugin_name, submodule_name):
 
84
        """Remove the submodule from sys.modules and the bzrlib namespace."""
 
85
        py_name = 'bzrlib.plugins.%s.%s' % (plugin_name, submodule_name)
 
86
        if py_name in sys.modules:
 
87
            del sys.modules[py_name]
 
88
        plugin = getattr(bzrlib.plugins, plugin_name, None)
 
89
        if plugin is not None:
 
90
            if getattr(plugin, submodule_name, None) is not None:
 
91
                delattr(plugin, submodule_name)
 
92
 
83
93
    def assertPluginUnknown(self, name):
84
94
        self.failIf(getattr(bzrlib.plugins, name, None) is not None)
85
95
        self.failIf('bzrlib.plugins.%s' % name in sys.modules)
828
838
        super(TestLoadPluginAt, self).setUp()
829
839
        # Make sure we don't pollute the plugins namespace
830
840
        self.overrideAttr(plugins, '__path__')
831
 
        # Be paranoid in case a test fail
832
 
        self.addCleanup(self._unregister_plugin, 'test_foo')
833
841
        # Reset the flag that protect against double loading
834
842
        self.overrideAttr(plugin, '_loaded', False)
835
843
        # Create the same plugin in two directories
837
845
        # The "normal" directory, we use 'standard' instead of 'plugins' to
838
846
        # avoid depending on the precise naming.
839
847
        self.create_plugin_package('test_foo', dir='standard/test_foo')
 
848
        # All the tests will load the 'test_foo' plugin from various locations
 
849
        self.addCleanup(self._unregister_plugin, 'test_foo')
840
850
 
841
851
    def assertTestFooLoadedFrom(self, path):
842
852
        self.assertPluginKnown('test_foo')
884
894
    def test_submodule_loading(self):
885
895
        # We create an additional directory under the one for test_foo
886
896
        self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
 
897
        self.addCleanup(self._unregister_plugin_submodule,
 
898
                        'test_foo', 'test_bar')
887
899
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
888
900
        plugin.set_plugins_path(['standard'])
889
901
        import bzrlib.plugins.test_foo
893
905
        self.assertIsSameRealPath('non-standard-dir/test_bar/__init__.py',
894
906
                                  bzrlib.plugins.test_foo.test_bar.__file__)
895
907
 
 
908
    def test_relative_submodule_loading(self):
 
909
        self.create_plugin_package('test_foo', dir='another-dir', source='''
 
910
import test_bar
 
911
''')
 
912
        # We create an additional directory under the one for test_foo
 
913
        self.create_plugin_package('test_bar', dir='another-dir/test_bar')
 
914
        self.addCleanup(self._unregister_plugin_submodule,
 
915
                        'test_foo', 'test_bar')
 
916
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@another-dir')
 
917
        plugin.set_plugins_path(['standard'])
 
918
        import bzrlib.plugins.test_foo
 
919
        self.assertEqual('bzrlib.plugins.test_foo',
 
920
                         bzrlib.plugins.test_foo.__package__)
 
921
        self.assertIsSameRealPath('another-dir/test_bar/__init__.py',
 
922
                                  bzrlib.plugins.test_foo.test_bar.__file__)
 
923
 
896
924
    def test_loading_from___init__only(self):
897
925
        # We rename the existing __init__.py file to ensure that we don't load
898
926
        # a random file