~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 08:15:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101008081514-dviqzrdfwyzsqbz2
Split NEWS into per-release doc/en/release-notes/bzr-*.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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 BaseTestPlugins(tests.TestCaseInTempDir):
 
41
class TestPluginMixin(object):
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.assertFalse(getattr(bzrlib.plugins, name, None) is not None)
95
 
        self.assertFalse('bzrlib.plugins.%s' % name in sys.modules)
 
94
        self.failIf(getattr(bzrlib.plugins, name, None) is not None)
 
95
        self.failIf('bzrlib.plugins.%s' % name in sys.modules)
96
96
 
97
97
    def assertPluginKnown(self, name):
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):
 
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):
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.assertFalse(tempattribute in self.activeattributes)
 
112
        self.failIf(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.assertTrue(tempattribute in self.activeattributes)
 
117
        self.failUnless(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.assertFalse('bzrlib.plugins.pluginone' in sys.modules)
151
 
        self.assertFalse('bzrlib.plugins.plugintwo' in sys.modules)
 
150
        self.failIf('bzrlib.plugins.pluginone' in sys.modules)
 
151
        self.failIf('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.assertFalse(tempattribute in self.activeattributes)
 
158
        self.failIf(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.assertTrue(tempattribute in self.activeattributes)
 
164
        self.failUnless(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.assertFalse('bzrlib.plugins.pluginone' in sys.modules)
190
 
            self.assertFalse('bzrlib.plugins.plugintwo' in sys.modules)
 
189
            self.failIf('bzrlib.plugins.pluginone' in sys.modules)
 
190
            self.failIf('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.assertFalse(tempattribute in self.activeattributes)
 
211
        self.failIf(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.assertTrue(tempattribute in self.activeattributes)
 
217
        self.failUnless(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
 
        """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', {})
 
270
        # This plugin asks for bzrlib api version 1.0.0, which is not supported
 
271
        # anymore.
277
272
        name = 'wants100.py'
278
273
        f = file(name, 'w')
279
274
        try:
281
276
                "bzrlib.api.require_any_api(bzrlib, [(1, 0, 0)])\n")
282
277
        finally:
283
278
            f.close()
 
279
 
284
280
        log = self.load_and_capture(name)
285
 
        self.assertNotContainsRe(log,
286
 
            r"It requested API version")
287
 
        self.assertEqual(
288
 
            ['wants100'],
289
 
            plugin.plugin_warnings.keys())
290
 
        self.assertContainsRe(
291
 
            plugin.plugin_warnings['wants100'][0],
 
281
        self.assertContainsRe(log,
292
282
            r"It requested API version")
293
283
 
294
284
    def test_plugin_with_bad_name_does_not_load(self):
302
292
            "it to 'bad_plugin_name_'\.")
303
293
 
304
294
 
305
 
class TestPlugins(BaseTestPlugins):
 
295
class TestPlugins(tests.TestCaseInTempDir, TestPluginMixin):
306
296
 
307
297
    def setup_plugin(self, source=""):
308
298
        # This test tests a new plugin appears in bzrlib.plugin.plugins().
309
299
        # check the plugin is not loaded already
310
300
        self.assertPluginUnknown('plugin')
311
301
        # write a plugin that _cannot_ fail to load.
312
 
        with file('plugin.py', 'w') as f: f.write(source + '\n')
 
302
        file('plugin.py', 'w').write(source + '\n')
313
303
        self.addCleanup(self.teardown_plugin)
314
304
        plugin.load_from_path(['.'])
315
305
 
452
442
    def test_final_fallback__version__with_version_info(self):
453
443
        self.setup_plugin("version_info = (1, 2, 3, 'final', 2)")
454
444
        plugin = bzrlib.plugin.plugins()['plugin']
455
 
        self.assertEqual("1.2.3.2", plugin.__version__)
 
445
        self.assertEqual("1.2.3.final.2", plugin.__version__)
456
446
 
457
447
 
458
448
class TestPluginHelp(tests.TestCaseInTempDir):
615
605
    def test_get_help_text_with_additional_see_also(self):
616
606
        mod = FakeModule('two lines of help\nand more', 'demo')
617
607
        topic = plugin.ModuleHelpTopic(mod)
618
 
        self.assertEqual("two lines of help\nand more\n\n:See also: bar, foo\n",
619
 
                         topic.get_help_text(['foo', 'bar']))
 
608
        self.assertEqual("two lines of help\nand more\nSee also: bar, foo\n",
 
609
            topic.get_help_text(['foo', 'bar']))
620
610
 
621
611
    def test_get_help_topic(self):
622
612
        """The help topic for a plugin is its module name."""
623
613
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.demo')
624
614
        topic = plugin.ModuleHelpTopic(mod)
625
615
        self.assertEqual('demo', topic.get_help_topic())
626
 
        mod = FakeModule('two lines of help\nand more',
627
 
                         'bzrlib.plugins.foo_bar')
 
616
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
628
617
        topic = plugin.ModuleHelpTopic(mod)
629
618
        self.assertEqual('foo_bar', topic.get_help_topic())
630
619
 
667
656
                    self.fail('No path to global plugins')
668
657
 
669
658
    def test_get_standard_plugins_path_env(self):
670
 
        self.overrideEnv('BZR_PLUGIN_PATH', 'foo/')
 
659
        os.environ['BZR_PLUGIN_PATH'] = 'foo/'
671
660
        path = plugin.get_standard_plugins_path()
672
661
        for directory in path:
673
662
            self.assertNotContainsRe(directory, r'\\/$')
703
692
 
704
693
    def _set_path(self, *args):
705
694
        path = os.pathsep.join(self._list2paths(*args))
706
 
        self.overrideEnv('BZR_PLUGIN_PATH', path)
 
695
        osutils.set_or_unset_env('BZR_PLUGIN_PATH', path)
707
696
 
708
697
    def check_path(self, expected_dirs, setting_dirs):
709
698
        if setting_dirs:
710
699
            self._set_path(*setting_dirs)
711
700
        actual = plugin.get_standard_plugins_path()
712
 
        self.assertEqual(self._list2paths(*expected_dirs), actual)
 
701
        self.assertEquals(self._list2paths(*expected_dirs), actual)
713
702
 
714
703
    def test_default(self):
715
704
        self.check_path([self.user, self.core, self.site],
779
768
                        ['+foo', '-bar'])
780
769
 
781
770
 
782
 
class TestDisablePlugin(BaseTestPlugins):
 
771
class TestDisablePlugin(tests.TestCaseInTempDir, TestPluginMixin):
783
772
 
784
773
    def setUp(self):
785
774
        super(TestDisablePlugin, self).setUp()
790
779
        self.addCleanup(self._unregister_plugin, 'test_foo')
791
780
 
792
781
    def test_cannot_import(self):
793
 
        self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
 
782
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
794
783
        plugin.set_plugins_path(['.'])
795
784
        try:
796
785
            import bzrlib.plugins.test_foo
812
801
        self.overrideAttr(trace, 'warning', captured_warning)
813
802
        # Reset the flag that protect against double loading
814
803
        self.overrideAttr(plugin, '_loaded', False)
815
 
        self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
 
804
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
816
805
        plugin.load_plugins(['.'])
817
806
        self.assertPluginUnknown('test_foo')
818
807
        # Make sure we don't warn about the plugin ImportError since this has
820
809
        self.assertLength(0, self.warnings)
821
810
 
822
811
 
823
 
 
824
812
class TestLoadPluginAtSyntax(tests.TestCase):
825
813
 
826
814
    def _get_paths(self, paths):
827
815
        return plugin._get_specific_plugin_paths(paths)
828
816
 
829
817
    def test_empty(self):
830
 
        self.assertEqual([], self._get_paths(None))
831
 
        self.assertEqual([], self._get_paths(''))
 
818
        self.assertEquals([], self._get_paths(None))
 
819
        self.assertEquals([], self._get_paths(''))
832
820
 
833
821
    def test_one_path(self):
834
 
        self.assertEqual([('b', 'man')], self._get_paths('b@man'))
 
822
        self.assertEquals([('b', 'man')], self._get_paths('b@man'))
835
823
 
836
824
    def test_bogus_path(self):
837
825
        # We need a '@'
844
832
                          os.pathsep.join(['batman@cave', '', 'robin@mobile']))
845
833
 
846
834
 
847
 
class TestLoadPluginAt(BaseTestPlugins):
 
835
class TestLoadPluginAt(tests.TestCaseInTempDir, TestPluginMixin):
848
836
 
849
837
    def setUp(self):
850
838
        super(TestLoadPluginAt, self).setUp()
859
847
        self.create_plugin_package('test_foo', dir='standard/test_foo')
860
848
        # All the tests will load the 'test_foo' plugin from various locations
861
849
        self.addCleanup(self._unregister_plugin, 'test_foo')
862
 
        # Unfortunately there's global cached state for the specific
863
 
        # registered paths.
864
 
        self.addCleanup(plugin.PluginImporter.reset)
865
850
 
866
851
    def assertTestFooLoadedFrom(self, path):
867
852
        self.assertPluginKnown('test_foo')
874
859
        self.assertTestFooLoadedFrom('standard/test_foo')
875
860
 
876
861
    def test_import(self):
877
 
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
862
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
878
863
        plugin.set_plugins_path(['standard'])
879
864
        try:
880
865
            import bzrlib.plugins.test_foo
883
868
        self.assertTestFooLoadedFrom('non-standard-dir')
884
869
 
885
870
    def test_loading(self):
886
 
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
871
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
887
872
        plugin.load_plugins(['standard'])
888
873
        self.assertTestFooLoadedFrom('non-standard-dir')
889
874
 
890
875
    def test_compiled_loaded(self):
891
 
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
876
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
892
877
        plugin.load_plugins(['standard'])
893
878
        self.assertTestFooLoadedFrom('non-standard-dir')
894
879
        self.assertIsSameRealPath('non-standard-dir/__init__.py',
911
896
        self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
912
897
        self.addCleanup(self._unregister_plugin_submodule,
913
898
                        'test_foo', 'test_bar')
914
 
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
899
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
915
900
        plugin.set_plugins_path(['standard'])
916
901
        import bzrlib.plugins.test_foo
917
902
        self.assertEqual('bzrlib.plugins.test_foo',
928
913
        self.create_plugin_package('test_bar', dir='another-dir/test_bar')
929
914
        self.addCleanup(self._unregister_plugin_submodule,
930
915
                        'test_foo', 'test_bar')
931
 
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@another-dir')
 
916
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@another-dir')
932
917
        plugin.set_plugins_path(['standard'])
933
918
        import bzrlib.plugins.test_foo
934
919
        self.assertEqual('bzrlib.plugins.test_foo',
943
928
        random = 'non-standard-dir/setup.py'
944
929
        os.rename(init, random)
945
930
        self.addCleanup(os.rename, random, init)
946
 
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
931
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
947
932
        plugin.load_plugins(['standard'])
948
933
        self.assertPluginUnknown('test_foo')
949
934
 
957
942
''' % ('test_foo', plugin_path)
958
943
        self.create_plugin('test_foo', source=source,
959
944
                           dir=plugin_dir, file_name=plugin_file_name)
960
 
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
 
945
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
961
946
        plugin.load_plugins(['standard'])
962
947
        self.assertTestFooLoadedFrom(plugin_path)
963
 
 
964
 
 
965
 
class TestDescribePlugins(BaseTestPlugins):
966
 
 
967
 
    def test_describe_plugins(self):
968
 
        class DummyModule(object):
969
 
            __doc__ = 'Hi there'
970
 
        class DummyPlugin(object):
971
 
            __version__ = '0.1.0'
972
 
            module = DummyModule()
973
 
        def dummy_plugins():
974
 
            return { 'good': DummyPlugin() }
975
 
        self.overrideAttr(plugin, 'plugin_warnings',
976
 
            {'bad': ['Failed to load (just testing)']})
977
 
        self.overrideAttr(plugin, 'plugins', dummy_plugins)
978
 
        self.assertEqual("""\
979
 
bad (failed to load)
980
 
  ** Failed to load (just testing)
981
 
 
982
 
good 0.1.0
983
 
  Hi there
984
 
 
985
 
""", ''.join(plugin.describe_plugins()))