~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Andrew Bennetts
  • Date: 2011-02-07 04:14:29 UTC
  • mfrom: (5535.4.26 fetch-all-tags-309682)
  • mto: This revision was merged to the branch mainline in revision 5648.
  • Revision ID: andrew.bennetts@canonical.com-20110207041429-3kc1blj34rvvxod9
Merge fetch-all-tags-309682.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
38
38
 
39
39
# TODO: Write a test for plugin decoration of commands.
40
40
 
41
 
class TestPluginMixin(object):
 
41
class BaseTestPlugins(tests.TestCaseInTempDir):
42
42
 
43
43
    def create_plugin(self, name, source=None, dir='.', file_name=None):
44
44
        if source is None:
99
99
        self.failUnless('bzrlib.plugins.%s' % name in sys.modules)
100
100
 
101
101
 
102
 
class TestLoadingPlugins(tests.TestCaseInTempDir, TestPluginMixin):
 
102
class TestLoadingPlugins(BaseTestPlugins):
103
103
 
104
104
    activeattributes = {}
105
105
 
267
267
            stream.close()
268
268
 
269
269
    def test_plugin_with_bad_api_version_reports(self):
270
 
        # This plugin asks for bzrlib api version 1.0.0, which is not supported
271
 
        # anymore.
 
270
        """Try loading a plugin that requests an unsupported api.
 
271
        
 
272
        Observe that it records the problem but doesn't complain on stderr.
 
273
 
 
274
        See https://bugs.launchpad.net/bzr/+bug/704195
 
275
        """
 
276
        self.overrideAttr(plugin, 'plugin_warnings', {})
272
277
        name = 'wants100.py'
273
278
        f = file(name, 'w')
274
279
        try:
276
281
                "bzrlib.api.require_any_api(bzrlib, [(1, 0, 0)])\n")
277
282
        finally:
278
283
            f.close()
279
 
 
280
284
        log = self.load_and_capture(name)
281
 
        self.assertContainsRe(log,
 
285
        self.assertNotContainsRe(log,
 
286
            r"It requested API version")
 
287
        self.assertEquals(
 
288
            ['wants100'],
 
289
            plugin.plugin_warnings.keys())
 
290
        self.assertContainsRe(
 
291
            plugin.plugin_warnings['wants100'][0],
282
292
            r"It requested API version")
283
293
 
284
294
    def test_plugin_with_bad_name_does_not_load(self):
292
302
            "it to 'bad_plugin_name_'\.")
293
303
 
294
304
 
295
 
class TestPlugins(tests.TestCaseInTempDir, TestPluginMixin):
 
305
class TestPlugins(BaseTestPlugins):
296
306
 
297
307
    def setup_plugin(self, source=""):
298
308
        # This test tests a new plugin appears in bzrlib.plugin.plugins().
656
666
                    self.fail('No path to global plugins')
657
667
 
658
668
    def test_get_standard_plugins_path_env(self):
659
 
        os.environ['BZR_PLUGIN_PATH'] = 'foo/'
 
669
        self.overrideEnv('BZR_PLUGIN_PATH', 'foo/')
660
670
        path = plugin.get_standard_plugins_path()
661
671
        for directory in path:
662
672
            self.assertNotContainsRe(directory, r'\\/$')
692
702
 
693
703
    def _set_path(self, *args):
694
704
        path = os.pathsep.join(self._list2paths(*args))
695
 
        osutils.set_or_unset_env('BZR_PLUGIN_PATH', path)
 
705
        self.overrideEnv('BZR_PLUGIN_PATH', path)
696
706
 
697
707
    def check_path(self, expected_dirs, setting_dirs):
698
708
        if setting_dirs:
768
778
                        ['+foo', '-bar'])
769
779
 
770
780
 
771
 
class TestDisablePlugin(tests.TestCaseInTempDir, TestPluginMixin):
 
781
class TestDisablePlugin(BaseTestPlugins):
772
782
 
773
783
    def setUp(self):
774
784
        super(TestDisablePlugin, self).setUp()
779
789
        self.addCleanup(self._unregister_plugin, 'test_foo')
780
790
 
781
791
    def test_cannot_import(self):
782
 
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
 
792
        self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
783
793
        plugin.set_plugins_path(['.'])
784
794
        try:
785
795
            import bzrlib.plugins.test_foo
801
811
        self.overrideAttr(trace, 'warning', captured_warning)
802
812
        # Reset the flag that protect against double loading
803
813
        self.overrideAttr(plugin, '_loaded', False)
804
 
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
 
814
        self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
805
815
        plugin.load_plugins(['.'])
806
816
        self.assertPluginUnknown('test_foo')
807
817
        # Make sure we don't warn about the plugin ImportError since this has
809
819
        self.assertLength(0, self.warnings)
810
820
 
811
821
 
 
822
 
812
823
class TestLoadPluginAtSyntax(tests.TestCase):
813
824
 
814
825
    def _get_paths(self, paths):
832
843
                          os.pathsep.join(['batman@cave', '', 'robin@mobile']))
833
844
 
834
845
 
835
 
class TestLoadPluginAt(tests.TestCaseInTempDir, TestPluginMixin):
 
846
class TestLoadPluginAt(BaseTestPlugins):
836
847
 
837
848
    def setUp(self):
838
849
        super(TestLoadPluginAt, self).setUp()
847
858
        self.create_plugin_package('test_foo', dir='standard/test_foo')
848
859
        # All the tests will load the 'test_foo' plugin from various locations
849
860
        self.addCleanup(self._unregister_plugin, 'test_foo')
 
861
        # Unfortunately there's global cached state for the specific
 
862
        # registered paths.
 
863
        self.addCleanup(plugin.PluginImporter.reset)
850
864
 
851
865
    def assertTestFooLoadedFrom(self, path):
852
866
        self.assertPluginKnown('test_foo')
859
873
        self.assertTestFooLoadedFrom('standard/test_foo')
860
874
 
861
875
    def test_import(self):
862
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
876
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
863
877
        plugin.set_plugins_path(['standard'])
864
878
        try:
865
879
            import bzrlib.plugins.test_foo
868
882
        self.assertTestFooLoadedFrom('non-standard-dir')
869
883
 
870
884
    def test_loading(self):
871
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
885
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
872
886
        plugin.load_plugins(['standard'])
873
887
        self.assertTestFooLoadedFrom('non-standard-dir')
874
888
 
875
889
    def test_compiled_loaded(self):
876
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
890
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
877
891
        plugin.load_plugins(['standard'])
878
892
        self.assertTestFooLoadedFrom('non-standard-dir')
879
893
        self.assertIsSameRealPath('non-standard-dir/__init__.py',
896
910
        self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
897
911
        self.addCleanup(self._unregister_plugin_submodule,
898
912
                        'test_foo', 'test_bar')
899
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
913
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
900
914
        plugin.set_plugins_path(['standard'])
901
915
        import bzrlib.plugins.test_foo
902
916
        self.assertEqual('bzrlib.plugins.test_foo',
913
927
        self.create_plugin_package('test_bar', dir='another-dir/test_bar')
914
928
        self.addCleanup(self._unregister_plugin_submodule,
915
929
                        'test_foo', 'test_bar')
916
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@another-dir')
 
930
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@another-dir')
917
931
        plugin.set_plugins_path(['standard'])
918
932
        import bzrlib.plugins.test_foo
919
933
        self.assertEqual('bzrlib.plugins.test_foo',
928
942
        random = 'non-standard-dir/setup.py'
929
943
        os.rename(init, random)
930
944
        self.addCleanup(os.rename, random, init)
931
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
945
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
932
946
        plugin.load_plugins(['standard'])
933
947
        self.assertPluginUnknown('test_foo')
934
948
 
942
956
''' % ('test_foo', plugin_path)
943
957
        self.create_plugin('test_foo', source=source,
944
958
                           dir=plugin_dir, file_name=plugin_file_name)
945
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
 
959
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
946
960
        plugin.load_plugins(['standard'])
947
961
        self.assertTestFooLoadedFrom(plugin_path)
 
962
 
 
963
 
 
964
class TestDescribePlugins(BaseTestPlugins):
 
965
 
 
966
    def test_describe_plugins(self):
 
967
        class DummyModule(object):
 
968
            __doc__ = 'Hi there'
 
969
        class DummyPlugin(object):
 
970
            __version__ = '0.1.0'
 
971
            module = DummyModule()
 
972
        def dummy_plugins():
 
973
            return { 'good': DummyPlugin() }
 
974
        self.overrideAttr(plugin, 'plugin_warnings',
 
975
            {'bad': ['Failed to load (just testing)']})
 
976
        self.overrideAttr(plugin, 'plugins', dummy_plugins)
 
977
        self.assertEquals("""\
 
978
bad (failed to load)
 
979
  ** Failed to load (just testing)
 
980
 
 
981
good 0.1.0
 
982
  Hi there
 
983
 
 
984
""", ''.join(plugin.describe_plugins()))