~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

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:
91
91
                delattr(plugin, submodule_name)
92
92
 
93
93
    def assertPluginUnknown(self, name):
94
 
        self.failIf(getattr(bzrlib.plugins, name, None) is not None)
95
 
        self.failIf('bzrlib.plugins.%s' % name in sys.modules)
 
94
        self.assertFalse(getattr(bzrlib.plugins, name, None) is not None)
 
95
        self.assertFalse('bzrlib.plugins.%s' % name in sys.modules)
96
96
 
97
97
    def assertPluginKnown(self, name):
98
 
        self.failUnless(getattr(bzrlib.plugins, name, None) is not None)
99
 
        self.failUnless('bzrlib.plugins.%s' % name in sys.modules)
100
 
 
101
 
 
102
 
class TestLoadingPlugins(tests.TestCaseInTempDir, TestPluginMixin):
 
98
        self.assertTrue(getattr(bzrlib.plugins, name, None) is not None)
 
99
        self.assertTrue('bzrlib.plugins.%s' % name in sys.modules)
 
100
 
 
101
 
 
102
class TestLoadingPlugins(BaseTestPlugins):
103
103
 
104
104
    activeattributes = {}
105
105
 
109
109
        # file name we can use which is also a valid attribute for accessing in
110
110
        # activeattributes. - we cannot give import parameters.
111
111
        tempattribute = "0"
112
 
        self.failIf(tempattribute in self.activeattributes)
 
112
        self.assertFalse(tempattribute in self.activeattributes)
113
113
        # set a place for the plugins to record their loading, and at the same
114
114
        # time validate that the location the plugins should record to is
115
115
        # valid and correct.
116
116
        self.__class__.activeattributes [tempattribute] = []
117
 
        self.failUnless(tempattribute in self.activeattributes)
 
117
        self.assertTrue(tempattribute in self.activeattributes)
118
118
        # create two plugin directories
119
119
        os.mkdir('first')
120
120
        os.mkdir('second')
147
147
        self.assertPluginUnknown('plugin')
148
148
 
149
149
    def test_plugins_from_different_dirs_can_demand_load(self):
150
 
        self.failIf('bzrlib.plugins.pluginone' in sys.modules)
151
 
        self.failIf('bzrlib.plugins.plugintwo' in sys.modules)
 
150
        self.assertFalse('bzrlib.plugins.pluginone' in sys.modules)
 
151
        self.assertFalse('bzrlib.plugins.plugintwo' in sys.modules)
152
152
        # This test tests that having two plugins in different
153
153
        # directories with different names allows them both to be loaded, when
154
154
        # we do a direct import statement.
155
155
        # Determine a file name we can use which is also a valid attribute
156
156
        # for accessing in activeattributes. - we cannot give import parameters.
157
157
        tempattribute = "different-dirs"
158
 
        self.failIf(tempattribute in self.activeattributes)
 
158
        self.assertFalse(tempattribute in self.activeattributes)
159
159
        # set a place for the plugins to record their loading, and at the same
160
160
        # time validate that the location the plugins should record to is
161
161
        # valid and correct.
162
162
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
163
163
            [tempattribute] = []
164
 
        self.failUnless(tempattribute in self.activeattributes)
 
164
        self.assertTrue(tempattribute in self.activeattributes)
165
165
        # create two plugin directories
166
166
        os.mkdir('first')
167
167
        os.mkdir('second')
186
186
 
187
187
        oldpath = bzrlib.plugins.__path__
188
188
        try:
189
 
            self.failIf('bzrlib.plugins.pluginone' in sys.modules)
190
 
            self.failIf('bzrlib.plugins.plugintwo' in sys.modules)
 
189
            self.assertFalse('bzrlib.plugins.pluginone' in sys.modules)
 
190
            self.assertFalse('bzrlib.plugins.plugintwo' in sys.modules)
191
191
            bzrlib.plugins.__path__ = ['first', 'second']
192
192
            exec "import bzrlib.plugins.pluginone"
193
193
            self.assertEqual(['first'], self.activeattributes[tempattribute])
208
208
        # check the plugin is not loaded already
209
209
        self.assertPluginUnknown('ts_plugin')
210
210
        tempattribute = "trailing-slash"
211
 
        self.failIf(tempattribute in self.activeattributes)
 
211
        self.assertFalse(tempattribute in self.activeattributes)
212
212
        # set a place for the plugin to record its loading, and at the same
213
213
        # time validate that the location the plugin should record to is
214
214
        # valid and correct.
215
215
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
216
216
            [tempattribute] = []
217
 
        self.failUnless(tempattribute in self.activeattributes)
 
217
        self.assertTrue(tempattribute in self.activeattributes)
218
218
        # create a directory for the plugin
219
219
        os.mkdir('plugin_test')
220
220
        # write a plugin that will record when its loaded in the
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()))