/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-08-10 15:00:17 UTC
  • mfrom: (7490.40.99 work)
  • mto: This revision was merged to the branch mainline in revision 7521.
  • Revision ID: jelmer@jelmer.uk-20200810150017-vs7xnrd1vat4iktg
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
21
22
import logging
22
23
import os
23
24
import sys
30
31
    tests,
31
32
    )
32
33
from ..tests.features import pkg_resources_feature
33
 
from ..sixish import (
34
 
    StringIO,
35
 
    viewkeys,
36
 
    )
37
34
 
38
35
 
39
36
# TODO: Write a test for plugin decoration of commands.
79
76
        self.log("using %r", paths)
80
77
        return paths
81
78
 
82
 
    def load_with_paths(self, paths):
 
79
    def load_with_paths(self, paths, warn_load_problems=True):
83
80
        self.log("loading plugins!")
84
 
        plugin.load_plugins(self.update_module_paths(paths), state=self)
 
81
        plugin.load_plugins(
 
82
            self.update_module_paths(paths), state=self,
 
83
            warn_load_problems=warn_load_problems)
85
84
 
86
85
    def create_plugin(self, name, source=None, dir='.', file_name=None):
87
86
        if source is None:
263
262
        finally:
264
263
            del self.activeattributes[tempattribute]
265
264
 
266
 
    def load_and_capture(self, name):
 
265
    def load_and_capture(self, name, warn_load_problems=True):
267
266
        """Load plugins from '.' capturing the output.
268
267
 
269
268
        :param name: The name of the plugin.
276
275
            log = logging.getLogger('brz')
277
276
            log.addHandler(handler)
278
277
            try:
279
 
                self.load_with_paths(['.'])
 
278
                self.load_with_paths(
 
279
                    ['.'], warn_load_problems=warn_load_problems)
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
 
 
294
 
        See https://bugs.launchpad.net/bzr/+bug/704195
 
292
        Observe that it records the problem but doesn't complain on stderr
 
293
        when warn_load_problems=False
295
294
        """
296
295
        name = 'wants100.py'
297
296
        with open(name, 'w') as f:
298
297
            f.write("import breezy\n"
299
298
                    "from breezy.errors import IncompatibleVersion\n"
300
299
                    "raise IncompatibleVersion(breezy, [(1, 0, 0)], (0, 0, 5))\n")
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))
 
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())
305
303
        self.assertContainsRe(
306
304
            self.plugin_warnings['wants100'][0],
307
305
            r"It supports breezy version")
316
314
                              "because the file path isn't a valid module name; try renaming "
317
315
                              "it to 'bad_plugin_name_'\\.")
318
316
 
 
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
 
319
334
 
320
335
class TestPlugins(BaseTestPlugins):
321
336
 
700
715
        self.create_plugin_package('ugly')
701
716
        self.overrideEnv('BRZ_DISABLE_PLUGINS', 'bad:ugly')
702
717
        self.load_with_paths(['.'])
703
 
        self.assertEqual({'good'}, viewkeys(self.plugins))
 
718
        self.assertEqual({'good'}, self.plugins.keys())
704
719
        self.assertPluginModules({
705
720
            'good': self.plugins['good'].module,
706
721
            'bad': None,