/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: 2020-07-05 12:50:01 UTC
  • mfrom: (7490.40.46 work)
  • mto: (7490.40.48 work)
  • mto: This revision was merged to the branch mainline in revision 7519.
  • Revision ID: jelmer@jelmer.uk-20200705125001-7s3vo0p55szbbws7
Merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Tests for plugins"""
19
19
 
20
20
import importlib
21
 
from io import StringIO
22
21
import logging
23
22
import os
24
23
import sys
31
30
    tests,
32
31
    )
33
32
from ..tests.features import pkg_resources_feature
 
33
from ..sixish import (
 
34
    StringIO,
 
35
    viewkeys,
 
36
    )
34
37
 
35
38
 
36
39
# TODO: Write a test for plugin decoration of commands.
76
79
        self.log("using %r", paths)
77
80
        return paths
78
81
 
79
 
    def load_with_paths(self, paths, warn_load_problems=True):
 
82
    def load_with_paths(self, paths):
80
83
        self.log("loading plugins!")
81
 
        plugin.load_plugins(
82
 
            self.update_module_paths(paths), state=self,
83
 
            warn_load_problems=warn_load_problems)
 
84
        plugin.load_plugins(self.update_module_paths(paths), state=self)
84
85
 
85
86
    def create_plugin(self, name, source=None, dir='.', file_name=None):
86
87
        if source is None:
262
263
        finally:
263
264
            del self.activeattributes[tempattribute]
264
265
 
265
 
    def load_and_capture(self, name, warn_load_problems=True):
 
266
    def load_and_capture(self, name):
266
267
        """Load plugins from '.' capturing the output.
267
268
 
268
269
        :param name: The name of the plugin.
275
276
            log = logging.getLogger('brz')
276
277
            log.addHandler(handler)
277
278
            try:
278
 
                self.load_with_paths(
279
 
                    ['.'], warn_load_problems=warn_load_problems)
 
279
                self.load_with_paths(['.'])
280
280
            finally:
281
281
                # Stop capturing output
282
282
                handler.flush()
289
289
    def test_plugin_with_bad_api_version_reports(self):
290
290
        """Try loading a plugin that requests an unsupported api.
291
291
 
292
 
        Observe that it records the problem but doesn't complain on stderr
293
 
        when warn_load_problems=False
 
292
        Observe that it records the problem but doesn't complain on stderr.
 
293
 
 
294
        See https://bugs.launchpad.net/bzr/+bug/704195
294
295
        """
295
296
        name = 'wants100.py'
296
297
        with open(name, 'w') as f:
297
298
            f.write("import breezy\n"
298
299
                    "from breezy.errors import IncompatibleVersion\n"
299
300
                    "raise IncompatibleVersion(breezy, [(1, 0, 0)], (0, 0, 5))\n")
300
 
        log = self.load_and_capture(name, warn_load_problems=False)
301
 
        self.assertNotContainsRe(log, r"It supports breezy version")
302
 
        self.assertEqual({'wants100'}, self.plugin_warnings.keys())
 
301
        log = self.load_and_capture(name)
 
302
        self.assertNotContainsRe(log,
 
303
                                 r"It supports breezy version")
 
304
        self.assertEqual({'wants100'}, viewkeys(self.plugin_warnings))
303
305
        self.assertContainsRe(
304
306
            self.plugin_warnings['wants100'][0],
305
307
            r"It supports breezy version")
314
316
                              "because the file path isn't a valid module name; try renaming "
315
317
                              "it to 'bad_plugin_name_'\\.")
316
318
 
317
 
    def test_plugin_with_error_suppress(self):
318
 
        # The file name here invalid for a python module.
319
 
        name = 'some_error.py'
320
 
        with open(name, 'w') as f:
321
 
            f.write('raise Exception("bad")\n')
322
 
        log = self.load_and_capture(name, warn_load_problems=False)
323
 
        self.assertEqual('', log)
324
 
 
325
 
    def test_plugin_with_error(self):
326
 
        # The file name here invalid for a python module.
327
 
        name = 'some_error.py'
328
 
        with open(name, 'w') as f:
329
 
            f.write('raise Exception("bad")\n')
330
 
        log = self.load_and_capture(name, warn_load_problems=True)
331
 
        self.assertEqual(
332
 
            'Unable to load plugin \'some_error\' from \'.\': bad\n', log)
333
 
 
334
319
 
335
320
class TestPlugins(BaseTestPlugins):
336
321
 
715
700
        self.create_plugin_package('ugly')
716
701
        self.overrideEnv('BRZ_DISABLE_PLUGINS', 'bad:ugly')
717
702
        self.load_with_paths(['.'])
718
 
        self.assertEqual({'good'}, self.plugins.keys())
 
703
        self.assertEqual({'good'}, viewkeys(self.plugins))
719
704
        self.assertPluginModules({
720
705
            'good': self.plugins['good'].module,
721
706
            'bad': None,