/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 bzrlib/tests/test_plugins.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-03-19 12:44:48 UTC
  • mfrom: (5100.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100319124448-8xooaqwyiwqhrq49
(vila) Allow plugins to be disabled via BZR_DISABLE_PLUGINS

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    plugin,
32
32
    plugins,
33
33
    tests,
 
34
    trace,
34
35
    )
35
36
 
36
37
 
38
39
 
39
40
class TestPluginMixin(object):
40
41
 
 
42
    def create_plugin(self, name, source='', dir='.', file_name=None):
 
43
        if file_name is None:
 
44
            file_name = name + '.py'
 
45
        # 'source' must not fail to load
 
46
        path = osutils.pathjoin(dir, file_name)
 
47
        f = open(path, 'w')
 
48
        self.addCleanup(os.unlink, path)
 
49
        try:
 
50
            f.write(source + '\n')
 
51
        finally:
 
52
            f.close()
 
53
 
 
54
    def create_plugin_package(self, name, source='', dir='.'):
 
55
        plugin_dir = osutils.pathjoin(dir, name)
 
56
        os.mkdir(plugin_dir)
 
57
        self.addCleanup(osutils.rmtree, plugin_dir)
 
58
        self.create_plugin(name, source, dir=plugin_dir,
 
59
                           file_name='__init__.py')
 
60
 
41
61
    def _unregister_plugin(self, name):
42
62
        """Remove the plugin from sys.modules and the bzrlib namespace."""
43
63
        py_name = 'bzrlib.plugins.%s' % name
47
67
            delattr(bzrlib.plugins, name)
48
68
 
49
69
    def assertPluginUnknown(self, name):
50
 
        self.failIf(getattr(bzrlib.plugins, 'plugin', None) is not None)
 
70
        self.failIf(getattr(bzrlib.plugins, name, None) is not None)
51
71
        self.failIf('bzrlib.plugins.%s' % name in sys.modules)
52
72
 
53
73
    def assertPluginKnown(self, name):
54
 
        self.failUnless(getattr(bzrlib.plugins, 'plugin', None) is not None)
 
74
        self.failUnless(getattr(bzrlib.plugins, name, None) is not None)
55
75
        self.failUnless('bzrlib.plugins.%s' % name in sys.modules)
56
76
 
57
77
 
723
743
        self.check_path(['+foo', '-bar', self.core, self.site],
724
744
                        ['+foo', '-bar'])
725
745
 
 
746
 
 
747
class TestDisablePlugin(tests.TestCaseInTempDir, TestPluginMixin):
 
748
 
 
749
    def setUp(self):
 
750
        super(TestDisablePlugin, self).setUp()
 
751
        self.create_plugin_package('test_foo')
 
752
        # Make sure we don't pollute the plugins namespace
 
753
        self.overrideAttr(plugins, '__path__')
 
754
        # Be paranoid in case a test fail
 
755
        self.addCleanup(self._unregister_plugin, 'test_foo')
 
756
 
 
757
    def test_cannot_import(self):
 
758
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
 
759
        plugin.set_plugins_path(['.'])
 
760
        try:
 
761
            import bzrlib.plugins.test_foo
 
762
        except ImportError:
 
763
            pass
 
764
        self.assertPluginUnknown('test_foo')
 
765
 
 
766
    def test_regular_load(self):
 
767
        self.overrideAttr(plugin, '_loaded', False)
 
768
        plugin.load_plugins(['.'])
 
769
        self.assertPluginKnown('test_foo')
 
770
 
 
771
    def test_not_loaded(self):
 
772
        self.warnings = []
 
773
        def captured_warning(*args, **kwargs):
 
774
            self.warnings.append((args, kwargs))
 
775
        self.overrideAttr(trace, 'warning', captured_warning)
 
776
        self.overrideAttr(plugin, '_loaded', False)
 
777
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
 
778
        plugin.load_plugins(plugin.set_plugins_path(['.']))
 
779
        self.assertPluginUnknown('test_foo')
 
780
        # Make sure we don't warn about the plugin ImportError since this has
 
781
        # been *requested* by the user.
 
782
        self.assertLength(0, self.warnings)