~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Martin Pool
  • Date: 2010-04-01 04:41:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5128.
  • Revision ID: mbp@sourcefrog.net-20100401044118-shyctqc02ob08ngz
ignore .testrepository

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
import bzrlib
29
29
from bzrlib import (
30
 
    errors,
31
30
    osutils,
32
31
    plugin,
33
32
    plugins,
80
79
        if getattr(bzrlib.plugins, name, None) is not None:
81
80
            delattr(bzrlib.plugins, name)
82
81
 
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
 
 
93
82
    def assertPluginUnknown(self, name):
94
83
        self.failIf(getattr(bzrlib.plugins, name, None) is not None)
95
84
        self.failIf('bzrlib.plugins.%s' % name in sys.modules)
484
473
        f.write("""\
485
474
from bzrlib import commands
486
475
class cmd_myplug(commands.Command):
487
 
    __doc__ = '''Just a simple test plugin.'''
 
476
    '''Just a simple test plugin.'''
488
477
    aliases = ['mplg']
489
478
    def run(self):
490
479
        print 'Hello from my plugin'
791
780
        self.overrideAttr(plugin, '_loaded', False)
792
781
        plugin.load_plugins(['.'])
793
782
        self.assertPluginKnown('test_foo')
794
 
        self.assertDocstring("This is the doc for test_foo",
795
 
                             bzrlib.plugins.test_foo)
 
783
        self.assertEqual("This is the doc for test_foo",
 
784
                         bzrlib.plugins.test_foo.__doc__)
796
785
 
797
786
    def test_not_loaded(self):
798
787
        self.warnings = []
809
798
        self.assertLength(0, self.warnings)
810
799
 
811
800
 
812
 
class TestLoadPluginAtSyntax(tests.TestCase):
813
 
 
814
 
    def _get_paths(self, paths):
815
 
        return plugin._get_specific_plugin_paths(paths)
816
 
 
817
 
    def test_empty(self):
818
 
        self.assertEquals([], self._get_paths(None))
819
 
        self.assertEquals([], self._get_paths(''))
820
 
 
821
 
    def test_one_path(self):
822
 
        self.assertEquals([('b', 'man')], self._get_paths('b@man'))
823
 
 
824
 
    def test_bogus_path(self):
825
 
        # We need a '@'
826
 
        self.assertRaises(errors.BzrCommandError, self._get_paths, 'batman')
827
 
        # Too much '@' isn't good either
828
 
        self.assertRaises(errors.BzrCommandError, self._get_paths,
829
 
                          'batman@mobile@cave')
830
 
        # An empty description probably indicates a problem
831
 
        self.assertRaises(errors.BzrCommandError, self._get_paths,
832
 
                          os.pathsep.join(['batman@cave', '', 'robin@mobile']))
833
 
 
834
 
 
835
801
class TestLoadPluginAt(tests.TestCaseInTempDir, TestPluginMixin):
836
802
 
837
803
    def setUp(self):
838
804
        super(TestLoadPluginAt, self).setUp()
839
805
        # Make sure we don't pollute the plugins namespace
840
806
        self.overrideAttr(plugins, '__path__')
 
807
        # Be paranoid in case a test fail
 
808
        self.addCleanup(self._unregister_plugin, 'test_foo')
841
809
        # Reset the flag that protect against double loading
842
810
        self.overrideAttr(plugin, '_loaded', False)
843
811
        # Create the same plugin in two directories
844
812
        self.create_plugin_package('test_foo', dir='non-standard-dir')
845
 
        # The "normal" directory, we use 'standard' instead of 'plugins' to
846
 
        # avoid depending on the precise naming.
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')
 
813
        self.create_plugin_package('test_foo', dir='b/test_foo')
850
814
 
851
 
    def assertTestFooLoadedFrom(self, path):
 
815
    def assertTestFooLoadedFrom(self, dir):
852
816
        self.assertPluginKnown('test_foo')
853
 
        self.assertDocstring('This is the doc for test_foo',
854
 
                             bzrlib.plugins.test_foo)
855
 
        self.assertEqual(path, bzrlib.plugins.test_foo.dir_source)
 
817
        self.assertEqual('This is the doc for test_foo',
 
818
                         bzrlib.plugins.test_foo.__doc__)
 
819
        self.assertEqual(dir, bzrlib.plugins.test_foo.dir_source)
856
820
 
857
821
    def test_regular_load(self):
858
 
        plugin.load_plugins(['standard'])
859
 
        self.assertTestFooLoadedFrom('standard/test_foo')
 
822
        plugin.load_plugins(['b'])
 
823
        self.assertTestFooLoadedFrom('b/test_foo')
860
824
 
861
825
    def test_import(self):
862
826
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
863
 
        plugin.set_plugins_path(['standard'])
 
827
        plugin.set_plugins_path(['b'])
864
828
        try:
865
829
            import bzrlib.plugins.test_foo
866
830
        except ImportError:
869
833
 
870
834
    def test_loading(self):
871
835
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
872
 
        plugin.load_plugins(['standard'])
 
836
        plugin.load_plugins(['b'])
873
837
        self.assertTestFooLoadedFrom('non-standard-dir')
874
838
 
875
839
    def test_compiled_loaded(self):
876
840
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
877
 
        plugin.load_plugins(['standard'])
 
841
        plugin.load_plugins(['b'])
878
842
        self.assertTestFooLoadedFrom('non-standard-dir')
879
 
        self.assertIsSameRealPath('non-standard-dir/__init__.py',
880
 
                                  bzrlib.plugins.test_foo.__file__)
 
843
        self.assertEqual('non-standard-dir/__init__.py',
 
844
                         bzrlib.plugins.test_foo.__file__)
881
845
 
882
846
        # Try importing again now that the source has been compiled
883
847
        self._unregister_plugin('test_foo')
884
848
        plugin._loaded = False
885
 
        plugin.load_plugins(['standard'])
 
849
        plugin.load_plugins(['b'])
886
850
        self.assertTestFooLoadedFrom('non-standard-dir')
887
851
        if __debug__:
888
852
            suffix = 'pyc'
889
853
        else:
890
854
            suffix = 'pyo'
891
 
        self.assertIsSameRealPath('non-standard-dir/__init__.%s' % suffix,
892
 
                                  bzrlib.plugins.test_foo.__file__)
 
855
        self.assertEqual('non-standard-dir/__init__.%s' % suffix,
 
856
                         bzrlib.plugins.test_foo.__file__)
893
857
 
894
858
    def test_submodule_loading(self):
895
859
        # We create an additional directory under the one for test_foo
896
860
        self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
897
 
        self.addCleanup(self._unregister_plugin_submodule,
898
 
                        'test_foo', 'test_bar')
899
861
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
900
 
        plugin.set_plugins_path(['standard'])
 
862
        plugin.set_plugins_path(['b'])
901
863
        import bzrlib.plugins.test_foo
902
864
        self.assertEqual('bzrlib.plugins.test_foo',
903
865
                         bzrlib.plugins.test_foo.__package__)
904
866
        import bzrlib.plugins.test_foo.test_bar
905
 
        self.assertIsSameRealPath('non-standard-dir/test_bar/__init__.py',
906
 
                                  bzrlib.plugins.test_foo.test_bar.__file__)
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
 
 
924
 
    def test_loading_from___init__only(self):
925
 
        # We rename the existing __init__.py file to ensure that we don't load
926
 
        # a random file
927
 
        init = 'non-standard-dir/__init__.py'
928
 
        random = 'non-standard-dir/setup.py'
929
 
        os.rename(init, random)
930
 
        self.addCleanup(os.rename, random, init)
931
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
932
 
        plugin.load_plugins(['standard'])
933
 
        self.assertPluginUnknown('test_foo')
934
 
 
935
 
    def test_loading_from_specific_file(self):
936
 
        plugin_dir = 'non-standard-dir'
937
 
        plugin_file_name = 'iamtestfoo.py'
938
 
        plugin_path = osutils.pathjoin(plugin_dir, plugin_file_name)
939
 
        source = '''\
940
 
"""This is the doc for %s"""
941
 
dir_source = '%s'
942
 
''' % ('test_foo', plugin_path)
943
 
        self.create_plugin('test_foo', source=source,
944
 
                           dir=plugin_dir, file_name=plugin_file_name)
945
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
946
 
        plugin.load_plugins(['standard'])
947
 
        self.assertTestFooLoadedFrom(plugin_path)
 
867
        self.assertEqual('non-standard-dir/test_bar/__init__.py',
 
868
                         bzrlib.plugins.test_foo.test_bar.__file__)