~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Patch Queue Manager
  • Date: 2016-02-01 19:56:05 UTC
  • mfrom: (6615.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20160201195605-o7rl92wf6uyum3fk
(vila) Open trunk again as 2.8b1 (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2012, 2016 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.assertEqual(
 
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().
299
309
        # check the plugin is not loaded already
300
310
        self.assertPluginUnknown('plugin')
301
311
        # write a plugin that _cannot_ fail to load.
302
 
        file('plugin.py', 'w').write(source + '\n')
 
312
        with file('plugin.py', 'w') as f: f.write(source + '\n')
303
313
        self.addCleanup(self.teardown_plugin)
304
314
        plugin.load_from_path(['.'])
305
315
 
442
452
    def test_final_fallback__version__with_version_info(self):
443
453
        self.setup_plugin("version_info = (1, 2, 3, 'final', 2)")
444
454
        plugin = bzrlib.plugin.plugins()['plugin']
445
 
        self.assertEqual("1.2.3.final.2", plugin.__version__)
 
455
        self.assertEqual("1.2.3.2", plugin.__version__)
446
456
 
447
457
 
448
458
class TestPluginHelp(tests.TestCaseInTempDir):
605
615
    def test_get_help_text_with_additional_see_also(self):
606
616
        mod = FakeModule('two lines of help\nand more', 'demo')
607
617
        topic = plugin.ModuleHelpTopic(mod)
608
 
        self.assertEqual("two lines of help\nand more\nSee also: bar, foo\n",
609
 
            topic.get_help_text(['foo', 'bar']))
 
618
        self.assertEqual("two lines of help\nand more\n\n:See also: bar, foo\n",
 
619
                         topic.get_help_text(['foo', 'bar']))
610
620
 
611
621
    def test_get_help_topic(self):
612
622
        """The help topic for a plugin is its module name."""
613
623
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.demo')
614
624
        topic = plugin.ModuleHelpTopic(mod)
615
625
        self.assertEqual('demo', topic.get_help_topic())
616
 
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
 
626
        mod = FakeModule('two lines of help\nand more',
 
627
                         'bzrlib.plugins.foo_bar')
617
628
        topic = plugin.ModuleHelpTopic(mod)
618
629
        self.assertEqual('foo_bar', topic.get_help_topic())
619
630
 
656
667
                    self.fail('No path to global plugins')
657
668
 
658
669
    def test_get_standard_plugins_path_env(self):
659
 
        os.environ['BZR_PLUGIN_PATH'] = 'foo/'
 
670
        self.overrideEnv('BZR_PLUGIN_PATH', 'foo/')
660
671
        path = plugin.get_standard_plugins_path()
661
672
        for directory in path:
662
673
            self.assertNotContainsRe(directory, r'\\/$')
692
703
 
693
704
    def _set_path(self, *args):
694
705
        path = os.pathsep.join(self._list2paths(*args))
695
 
        osutils.set_or_unset_env('BZR_PLUGIN_PATH', path)
 
706
        self.overrideEnv('BZR_PLUGIN_PATH', path)
696
707
 
697
708
    def check_path(self, expected_dirs, setting_dirs):
698
709
        if setting_dirs:
699
710
            self._set_path(*setting_dirs)
700
711
        actual = plugin.get_standard_plugins_path()
701
 
        self.assertEquals(self._list2paths(*expected_dirs), actual)
 
712
        self.assertEqual(self._list2paths(*expected_dirs), actual)
702
713
 
703
714
    def test_default(self):
704
715
        self.check_path([self.user, self.core, self.site],
768
779
                        ['+foo', '-bar'])
769
780
 
770
781
 
771
 
class TestDisablePlugin(tests.TestCaseInTempDir, TestPluginMixin):
 
782
class TestDisablePlugin(BaseTestPlugins):
772
783
 
773
784
    def setUp(self):
774
785
        super(TestDisablePlugin, self).setUp()
779
790
        self.addCleanup(self._unregister_plugin, 'test_foo')
780
791
 
781
792
    def test_cannot_import(self):
782
 
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
 
793
        self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
783
794
        plugin.set_plugins_path(['.'])
784
795
        try:
785
796
            import bzrlib.plugins.test_foo
801
812
        self.overrideAttr(trace, 'warning', captured_warning)
802
813
        # Reset the flag that protect against double loading
803
814
        self.overrideAttr(plugin, '_loaded', False)
804
 
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
 
815
        self.overrideEnv('BZR_DISABLE_PLUGINS', 'test_foo')
805
816
        plugin.load_plugins(['.'])
806
817
        self.assertPluginUnknown('test_foo')
807
818
        # Make sure we don't warn about the plugin ImportError since this has
809
820
        self.assertLength(0, self.warnings)
810
821
 
811
822
 
 
823
 
812
824
class TestLoadPluginAtSyntax(tests.TestCase):
813
825
 
814
826
    def _get_paths(self, paths):
815
827
        return plugin._get_specific_plugin_paths(paths)
816
828
 
817
829
    def test_empty(self):
818
 
        self.assertEquals([], self._get_paths(None))
819
 
        self.assertEquals([], self._get_paths(''))
 
830
        self.assertEqual([], self._get_paths(None))
 
831
        self.assertEqual([], self._get_paths(''))
820
832
 
821
833
    def test_one_path(self):
822
 
        self.assertEquals([('b', 'man')], self._get_paths('b@man'))
 
834
        self.assertEqual([('b', 'man')], self._get_paths('b@man'))
823
835
 
824
836
    def test_bogus_path(self):
825
837
        # We need a '@'
832
844
                          os.pathsep.join(['batman@cave', '', 'robin@mobile']))
833
845
 
834
846
 
835
 
class TestLoadPluginAt(tests.TestCaseInTempDir, TestPluginMixin):
 
847
class TestLoadPluginAt(BaseTestPlugins):
836
848
 
837
849
    def setUp(self):
838
850
        super(TestLoadPluginAt, self).setUp()
847
859
        self.create_plugin_package('test_foo', dir='standard/test_foo')
848
860
        # All the tests will load the 'test_foo' plugin from various locations
849
861
        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)
850
865
 
851
866
    def assertTestFooLoadedFrom(self, path):
852
867
        self.assertPluginKnown('test_foo')
859
874
        self.assertTestFooLoadedFrom('standard/test_foo')
860
875
 
861
876
    def test_import(self):
862
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
877
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
863
878
        plugin.set_plugins_path(['standard'])
864
879
        try:
865
880
            import bzrlib.plugins.test_foo
868
883
        self.assertTestFooLoadedFrom('non-standard-dir')
869
884
 
870
885
    def test_loading(self):
871
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
886
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
872
887
        plugin.load_plugins(['standard'])
873
888
        self.assertTestFooLoadedFrom('non-standard-dir')
874
889
 
875
890
    def test_compiled_loaded(self):
876
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
891
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
877
892
        plugin.load_plugins(['standard'])
878
893
        self.assertTestFooLoadedFrom('non-standard-dir')
879
894
        self.assertIsSameRealPath('non-standard-dir/__init__.py',
896
911
        self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
897
912
        self.addCleanup(self._unregister_plugin_submodule,
898
913
                        'test_foo', 'test_bar')
899
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
914
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
900
915
        plugin.set_plugins_path(['standard'])
901
916
        import bzrlib.plugins.test_foo
902
917
        self.assertEqual('bzrlib.plugins.test_foo',
913
928
        self.create_plugin_package('test_bar', dir='another-dir/test_bar')
914
929
        self.addCleanup(self._unregister_plugin_submodule,
915
930
                        'test_foo', 'test_bar')
916
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@another-dir')
 
931
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@another-dir')
917
932
        plugin.set_plugins_path(['standard'])
918
933
        import bzrlib.plugins.test_foo
919
934
        self.assertEqual('bzrlib.plugins.test_foo',
928
943
        random = 'non-standard-dir/setup.py'
929
944
        os.rename(init, random)
930
945
        self.addCleanup(os.rename, random, init)
931
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
946
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
932
947
        plugin.load_plugins(['standard'])
933
948
        self.assertPluginUnknown('test_foo')
934
949
 
942
957
''' % ('test_foo', plugin_path)
943
958
        self.create_plugin('test_foo', source=source,
944
959
                           dir=plugin_dir, file_name=plugin_file_name)
945
 
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
 
960
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
946
961
        plugin.load_plugins(['standard'])
947
962
        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()))