/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_plugins.py

  • Committer: Jelmer Vernooij
  • Date: 2019-05-29 03:22:34 UTC
  • mfrom: (7303 work)
  • mto: This revision was merged to the branch mainline in revision 7306.
  • Revision ID: jelmer@jelmer.uk-20190529032234-mt3fuws8gq03tapi
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
import breezy
27
27
from .. import (
28
 
    errors,
29
28
    osutils,
30
29
    plugin,
31
30
    tests,
32
 
    trace,
33
31
    )
 
32
from ..tests.features import pkg_resources_feature
34
33
from ..sixish import (
35
 
    PY3,
36
34
    StringIO,
37
35
    viewkeys,
38
36
    )
118
116
                rel = osutils.relpath(self.test_dir, cache_dir)
119
117
                self.log("moving %s in %s to %s", name, rel, magicless_name)
120
118
                os.rename(os.path.join(cache_dir, name),
121
 
                    os.path.join(directory, magicless_name))
 
119
                          os.path.join(directory, magicless_name))
122
120
 
123
121
    def _unregister_finder(self):
124
122
        """Removes any test copies of _PluginsAtFinder from sys.meta_path."""
140
138
    def assertPluginModules(self, plugin_dict):
141
139
        self.assertEqual(
142
140
            dict((k[len(self.module_prefix):], sys.modules[k])
143
 
                for k in sys.modules if k.startswith(self.module_prefix)),
 
141
                 for k in sys.modules if k.startswith(self.module_prefix)),
144
142
            plugin_dict)
145
143
 
146
144
    def assertPluginUnknown(self, name):
166
164
        # set a place for the plugins to record their loading, and at the same
167
165
        # time validate that the location the plugins should record to is
168
166
        # valid and correct.
169
 
        self.__class__.activeattributes [tempattribute] = []
 
167
        self.__class__.activeattributes[tempattribute] = []
170
168
        self.assertTrue(tempattribute in self.activeattributes)
171
169
        # create two plugin directories
172
170
        os.mkdir('first')
203
201
        # set a place for the plugins to record their loading, and at the same
204
202
        # time validate that the location the plugins should record to is
205
203
        # valid and correct.
206
 
        breezy.tests.test_plugins.TestLoadingPlugins.activeattributes \
207
 
            [tempattribute] = []
 
204
        breezy.tests.test_plugins.TestLoadingPlugins.activeattributes[tempattribute] = [
 
205
            ]
208
206
        self.assertTrue(tempattribute in self.activeattributes)
209
207
        # create two plugin directories
210
208
        os.mkdir('first')
230
228
            self.assertEqual(['first'], self.activeattributes[tempattribute])
231
229
            exec("import %splugintwo" % self.module_prefix)
232
230
            self.assertEqual(['first', 'second'],
233
 
                self.activeattributes[tempattribute])
 
231
                             self.activeattributes[tempattribute])
234
232
        finally:
235
233
            del self.activeattributes[tempattribute]
236
234
 
244
242
        # set a place for the plugin to record its loading, and at the same
245
243
        # time validate that the location the plugin should record to is
246
244
        # valid and correct.
247
 
        breezy.tests.test_plugins.TestLoadingPlugins.activeattributes \
248
 
            [tempattribute] = []
 
245
        breezy.tests.test_plugins.TestLoadingPlugins.activeattributes[tempattribute] = [
 
246
            ]
249
247
        self.assertTrue(tempattribute in self.activeattributes)
250
248
        # create a directory for the plugin
251
249
        os.mkdir('plugin_test')
259
257
            outfile.write('\n')
260
258
 
261
259
        try:
262
 
            self.load_with_paths(['plugin_test'+os.sep])
 
260
            self.load_with_paths(['plugin_test' + os.sep])
263
261
            self.assertEqual(['plugin'], self.activeattributes[tempattribute])
264
262
            self.assertPluginKnown('ts_plugin')
265
263
        finally:
298
296
        name = 'wants100.py'
299
297
        with open(name, 'w') as f:
300
298
            f.write("import breezy\n"
301
 
                "from breezy.errors import IncompatibleVersion\n"
302
 
                "raise IncompatibleVersion(breezy, [(1, 0, 0)], (0, 0, 5))\n")
 
299
                    "from breezy.errors import IncompatibleVersion\n"
 
300
                    "raise IncompatibleVersion(breezy, [(1, 0, 0)], (0, 0, 5))\n")
303
301
        log = self.load_and_capture(name)
304
302
        self.assertNotContainsRe(log,
305
 
            r"It supports breezy version")
 
303
                                 r"It supports breezy version")
306
304
        self.assertEqual({'wants100'}, viewkeys(self.plugin_warnings))
307
305
        self.assertContainsRe(
308
306
            self.plugin_warnings['wants100'][0],
314
312
        open(name, 'w').close()
315
313
        log = self.load_and_capture(name)
316
314
        self.assertContainsRe(log,
317
 
            r"Unable to load 'brz-bad plugin-name\.' in '\.' as a plugin "
318
 
            "because the file path isn't a valid module name; try renaming "
319
 
            "it to 'bad_plugin_name_'\\.")
 
315
                              r"Unable to load 'brz-bad plugin-name\.' in '\.' as a plugin "
 
316
                              "because the file path isn't a valid module name; try renaming "
 
317
                              "it to 'bad_plugin_name_'\\.")
320
318
 
321
319
 
322
320
class TestPlugins(BaseTestPlugins):
326
324
        # check the plugin is not loaded already
327
325
        self.assertPluginUnknown('plugin')
328
326
        # write a plugin that _cannot_ fail to load.
329
 
        with open('plugin.py', 'w') as f: f.write(source + '\n')
 
327
        with open('plugin.py', 'w') as f:
 
328
            f.write(source + '\n')
330
329
        self.load_with_paths(['.'])
331
330
 
332
331
    def test_plugin_loaded(self):
361
360
        self.setup_plugin()
362
361
        self.promote_cache(self.test_dir)
363
362
        self.reset()
364
 
        self.load_with_paths(['.']) # import plugin.pyc
 
363
        self.load_with_paths(['.'])  # import plugin.pyc
365
364
        p = plugin.plugins()['plugin']
366
365
        plugin_path = self.test_dir + '/plugin.py'
367
366
        self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
372
371
        os.unlink(self.test_dir + '/plugin.py')
373
372
        self.promote_cache(self.test_dir)
374
373
        self.reset()
375
 
        self.load_with_paths(['.']) # import plugin.pyc (or .pyo)
 
374
        self.load_with_paths(['.'])  # import plugin.pyc (or .pyo)
376
375
        p = plugin.plugins()['plugin']
377
376
        plugin_path = self.test_dir + '/plugin' + plugin.COMPILED_EXT
378
377
        self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
556
555
        mod = FakeModule(None, 'demo')
557
556
        topic = plugin.ModuleHelpTopic(mod)
558
557
        self.assertEqual("Plugin 'demo' has no docstring.\n",
559
 
            topic.get_help_text())
 
558
                         topic.get_help_text())
560
559
 
561
560
    def test_get_help_text_no_carriage_return(self):
562
561
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
563
562
        mod = FakeModule('one line of help', 'demo')
564
563
        topic = plugin.ModuleHelpTopic(mod)
565
564
        self.assertEqual("one line of help\n",
566
 
            topic.get_help_text())
 
565
                         topic.get_help_text())
567
566
 
568
567
    def test_get_help_text_carriage_return(self):
569
568
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
570
569
        mod = FakeModule('two lines of help\nand more\n', 'demo')
571
570
        topic = plugin.ModuleHelpTopic(mod)
572
571
        self.assertEqual("two lines of help\nand more\n",
573
 
            topic.get_help_text())
 
572
                         topic.get_help_text())
574
573
 
575
574
    def test_get_help_text_with_additional_see_also(self):
576
575
        mod = FakeModule('two lines of help\nand more', 'demo')
577
576
        topic = plugin.ModuleHelpTopic(mod)
578
 
        self.assertEqual("two lines of help\nand more\n\n:See also: bar, foo\n",
579
 
                         topic.get_help_text(['foo', 'bar']))
 
577
        self.assertEqual(
 
578
            "two lines of help\nand more\n\n:See also: bar, foo\n",
 
579
            topic.get_help_text(['foo', 'bar']))
580
580
 
581
581
    def test_get_help_topic(self):
582
582
        """The help topic for a plugin is its module name."""
594
594
    user = "USER"
595
595
    core = "CORE"
596
596
    site = "SITE"
 
597
    entrypoints = "ENTRYPOINTS"
597
598
 
598
599
    def check_path(self, expected_dirs, setting_dirs):
599
600
        if setting_dirs is None:
601
602
        else:
602
603
            os.environ['BRZ_PLUGIN_PATH'] = os.pathsep.join(setting_dirs)
603
604
        actual = [(p if t == 'path' else t.upper())
604
 
            for p, t in plugin._env_plugin_path()]
 
605
                  for p, t in plugin._env_plugin_path()]
605
606
        self.assertEqual(expected_dirs, actual)
606
607
 
607
608
    def test_default(self):
608
 
        self.check_path([self.user, self.core, self.site],
 
609
        self.check_path([self.user, self.core, self.entrypoints, self.site],
609
610
                        None)
610
611
 
611
612
    def test_adhoc_policy(self):
612
 
        self.check_path([self.user, self.core, self.site],
 
613
        self.check_path([self.user, self.core, self.site, self.entrypoints],
613
614
                        ['+user', '+core', '+site'])
614
615
 
615
616
    def test_fallback_policy(self):
616
 
        self.check_path([self.core, self.site, self.user],
 
617
        self.check_path([self.core, self.site, self.user, self.entrypoints],
617
618
                        ['+core', '+site', '+user'])
618
619
 
619
620
    def test_override_policy(self):
620
 
        self.check_path([self.user, self.site, self.core],
 
621
        self.check_path([self.user, self.site, self.core, self.entrypoints],
621
622
                        ['+user', '+site', '+core'])
622
623
 
623
624
    def test_disable_user(self):
624
 
        self.check_path([self.core, self.site], ['-user'])
 
625
        self.check_path([self.core, self.entrypoints, self.site], ['-user'])
625
626
 
626
627
    def test_disable_user_twice(self):
627
628
        # Ensures multiple removals don't left cruft
628
 
        self.check_path([self.core, self.site], ['-user', '-user'])
 
629
        self.check_path([self.core, self.entrypoints, self.site], ['-user', '-user'])
629
630
 
630
631
    def test_duplicates_are_removed(self):
631
 
        self.check_path([self.user, self.core, self.site],
 
632
        self.check_path([self.user, self.core, self.entrypoints, self.site],
632
633
                        ['+user', '+user'])
633
634
        # And only the first reference is kept (since the later references will
634
635
        # only produce '<plugin> already loaded' mutters)
635
 
        self.check_path([self.user, self.core, self.site],
 
636
        self.check_path([self.user, self.core, self.site, self.entrypoints],
636
637
                        ['+user', '+user', '+core',
637
638
                         '+user', '+site', '+site',
638
639
                         '+core'])
639
640
 
640
641
    def test_disable_overrides_enable(self):
641
 
        self.check_path([self.core, self.site], ['-user', '+user'])
 
642
        self.check_path([self.core, self.entrypoints, self.site], ['-user', '+user'])
642
643
 
643
644
    def test_disable_core(self):
644
 
        self.check_path([self.site], ['-core'])
645
 
        self.check_path([self.user, self.site], ['+user', '-core'])
 
645
        self.check_path([self.entrypoints, self.site], ['-core'])
 
646
        self.check_path([self.user, self.entrypoints, self.site], ['+user', '-core'])
646
647
 
647
648
    def test_disable_site(self):
648
 
        self.check_path([self.core], ['-site'])
649
 
        self.check_path([self.user, self.core], ['-site', '+user'])
 
649
        self.check_path([self.core, self.entrypoints], ['-site'])
 
650
        self.check_path([self.user, self.core, self.entrypoints], ['-site', '+user'])
650
651
 
651
652
    def test_override_site(self):
652
 
        self.check_path(['mysite', self.user, self.core],
 
653
        self.check_path(['mysite', self.user, self.core, self.entrypoints],
653
654
                        ['mysite', '-site', '+user'])
654
 
        self.check_path(['mysite', self.core],
 
655
        self.check_path(['mysite', self.core, self.entrypoints],
655
656
                        ['mysite', '-site'])
656
657
 
657
658
    def test_override_core(self):
658
 
        self.check_path(['mycore', self.user, self.site],
 
659
        self.check_path(['mycore', self.user, self.site, self.entrypoints],
659
660
                        ['mycore', '-core', '+user', '+site'])
660
 
        self.check_path(['mycore', self.site],
 
661
        self.check_path(['mycore', self.entrypoints, self.site],
661
662
                        ['mycore', '-core'])
662
663
 
663
664
    def test_my_plugin_only(self):
664
 
        self.check_path(['myplugin'], ['myplugin', '-user', '-core', '-site'])
 
665
        self.check_path(
 
666
            ['myplugin'],
 
667
            ['myplugin', '-user', '-core', '-site', '-entrypoints'])
665
668
 
666
669
    def test_my_plugin_first(self):
667
 
        self.check_path(['myplugin', self.core, self.site, self.user],
 
670
        self.check_path(['myplugin', self.core, self.site, self.user, self.entrypoints],
668
671
                        ['myplugin', '+core', '+site', '+user'])
669
672
 
670
673
    def test_bogus_references(self):
671
 
        self.check_path(['+foo', '-bar', self.core, self.site],
 
674
        self.check_path(['+foo', '-bar', self.core, self.entrypoints, self.site],
672
675
                        ['+foo', '-bar'])
673
676
 
674
677
 
727
730
    def test_mixed(self):
728
731
        value = os.pathsep.join(['valid', 'in-valid'])
729
732
        self.assertEqual(['valid'], self._get_names(value))
730
 
        self.assertContainsRe(self.get_log(),
 
733
        self.assertContainsRe(
 
734
            self.get_log(),
731
735
            r"Invalid name 'in-valid' in BRZ_DISABLE_PLUGINS=" + repr(value))
732
736
 
733
737
 
762
766
 
763
767
    def test_bad_name(self):
764
768
        self.assertEqual([], self._get_paths('/usr/local/bzr-git'))
765
 
        self.assertContainsRe(self.get_log(),
 
769
        self.assertContainsRe(
 
770
            self.get_log(),
766
771
            r"Invalid name 'bzr-git' in BRZ_PLUGINS_AT='/usr/local/bzr-git'")
767
772
 
768
773
 
825
830
        self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
826
831
        self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@non-standard-dir')
827
832
        self.update_module_paths(['standard'])
828
 
        import breezy.testingplugins.test_foo
 
833
        import breezy.testingplugins.test_foo  # noqa: F401
829
834
        self.assertEqual(self.module_prefix + 'test_foo',
830
835
                         self.module.test_foo.__package__)
831
 
        import breezy.testingplugins.test_foo.test_bar
 
836
        import breezy.testingplugins.test_foo.test_bar  # noqa: F401
832
837
        self.assertIsSameRealPath('non-standard-dir/test_bar/__init__.py',
833
838
                                  self.module.test_foo.test_bar.__file__)
834
839
 
840
845
        self.create_plugin_package('test_bar', dir='another-dir/test_bar')
841
846
        self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@another-dir')
842
847
        self.update_module_paths(['standard'])
843
 
        import breezy.testingplugins.test_foo
 
848
        import breezy.testingplugins.test_foo  # noqa: F401
844
849
        self.assertEqual(self.module_prefix + 'test_foo',
845
850
                         self.module.test_foo.__package__)
846
851
        self.assertIsSameRealPath('another-dir/test_bar/__init__.py',
876
881
    def test_describe_plugins(self):
877
882
        class DummyModule(object):
878
883
            __doc__ = 'Hi there'
 
884
 
879
885
        class DummyPlugin(object):
880
886
            __version__ = '0.1.0'
881
887
            module = DummyModule()
889
895
  Hi there
890
896
 
891
897
""", ''.join(plugin.describe_plugins(state=self)))
 
898
 
 
899
 
 
900
class DummyPlugin(object):
 
901
    """Plugin."""
 
902
 
 
903
 
 
904
class TestLoadEnvPlugin(BaseTestPlugins):
 
905
 
 
906
    _test_needs_features = [pkg_resources_feature]
 
907
 
 
908
    def setup_plugin(self, source=""):
 
909
        # This test tests a new plugin appears in breezy.plugin.plugins().
 
910
        # check the plugin is not loaded already
 
911
        self.assertPluginUnknown('plugin')
 
912
        # write a plugin that _cannot_ fail to load.
 
913
        import pkg_resources
 
914
        d = pkg_resources.Distribution(__file__)
 
915
        ep = pkg_resources.EntryPoint.parse(
 
916
            'plugin = ' + __name__ + ':DummyPlugin', dist=d)
 
917
        d._ep_map = {'breezy.plugin': {'plugin': ep}}
 
918
        pkg_resources.working_set.add(d, 'plugin', replace=True)
 
919
        eps = list(pkg_resources.iter_entry_points('breezy.plugin'))
 
920
        self.assertEqual(['plugin'], [ep.name for ep in eps])
 
921
        self.load_with_paths(['.'])
 
922
        self.addCleanup(d._ep_map.clear)
 
923
 
 
924
    def test_plugin_loaded(self):
 
925
        self.assertPluginUnknown('plugin')
 
926
        self.setup_plugin()
 
927
        p = self.plugins['plugin']
 
928
        self.assertIsInstance(p, breezy.plugin.PlugIn)
 
929
        self.assertIs(p.module, sys.modules[self.module_prefix + 'plugin'])
 
930
 
 
931
    def test_plugin_loaded_disabled(self):
 
932
        self.assertPluginUnknown('plugin')
 
933
        self.overrideEnv('BRZ_DISABLE_PLUGINS', 'plugin')
 
934
        self.setup_plugin()
 
935
        self.assertNotIn('plugin', self.plugins)