~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Andrew Bennetts
  • Date: 2010-03-26 04:47:45 UTC
  • mfrom: (5116 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5117.
  • Revision ID: andrew.bennetts@canonical.com-20100326044745-ubvt5tmse1a17s1f
MergeĀ lp:bzr.

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=None, dir='.', file_name=None):
 
43
        if source is None:
 
44
            source = '''\
 
45
"""This is the doc for %s"""
 
46
''' % (name)
 
47
        if file_name is None:
 
48
            file_name = name + '.py'
 
49
        # 'source' must not fail to load
 
50
        path = osutils.pathjoin(dir, file_name)
 
51
        f = open(path, 'w')
 
52
        self.addCleanup(os.unlink, path)
 
53
        try:
 
54
            f.write(source + '\n')
 
55
        finally:
 
56
            f.close()
 
57
 
 
58
    def create_plugin_package(self, name, dir=None, source=None):
 
59
        if dir is None:
 
60
            dir = name
 
61
        if source is None:
 
62
            source = '''\
 
63
"""This is the doc for %s"""
 
64
dir_source = '%s'
 
65
''' % (name, dir)
 
66
        os.makedirs(dir)
 
67
        def cleanup():
 
68
            # Workaround lazy import random? madness
 
69
            osutils.rmtree(dir)
 
70
        self.addCleanup(cleanup)
 
71
        self.create_plugin(name, source, dir,
 
72
                           file_name='__init__.py')
 
73
 
41
74
    def _unregister_plugin(self, name):
42
75
        """Remove the plugin from sys.modules and the bzrlib namespace."""
43
76
        py_name = 'bzrlib.plugins.%s' % name
47
80
            delattr(bzrlib.plugins, name)
48
81
 
49
82
    def assertPluginUnknown(self, name):
50
 
        self.failIf(getattr(bzrlib.plugins, 'plugin', None) is not None)
 
83
        self.failIf(getattr(bzrlib.plugins, name, None) is not None)
51
84
        self.failIf('bzrlib.plugins.%s' % name in sys.modules)
52
85
 
53
86
    def assertPluginKnown(self, name):
54
 
        self.failUnless(getattr(bzrlib.plugins, 'plugin', None) is not None)
 
87
        self.failUnless(getattr(bzrlib.plugins, name, None) is not None)
55
88
        self.failUnless('bzrlib.plugins.%s' % name in sys.modules)
56
89
 
57
90
 
723
756
        self.check_path(['+foo', '-bar', self.core, self.site],
724
757
                        ['+foo', '-bar'])
725
758
 
 
759
 
 
760
class TestDisablePlugin(tests.TestCaseInTempDir, TestPluginMixin):
 
761
 
 
762
    def setUp(self):
 
763
        super(TestDisablePlugin, self).setUp()
 
764
        self.create_plugin_package('test_foo')
 
765
        # Make sure we don't pollute the plugins namespace
 
766
        self.overrideAttr(plugins, '__path__')
 
767
        # Be paranoid in case a test fail
 
768
        self.addCleanup(self._unregister_plugin, 'test_foo')
 
769
 
 
770
    def test_cannot_import(self):
 
771
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
 
772
        plugin.set_plugins_path(['.'])
 
773
        try:
 
774
            import bzrlib.plugins.test_foo
 
775
        except ImportError:
 
776
            pass
 
777
        self.assertPluginUnknown('test_foo')
 
778
 
 
779
    def test_regular_load(self):
 
780
        self.overrideAttr(plugin, '_loaded', False)
 
781
        plugin.load_plugins(['.'])
 
782
        self.assertPluginKnown('test_foo')
 
783
        self.assertEqual("This is the doc for test_foo",
 
784
                         bzrlib.plugins.test_foo.__doc__)
 
785
 
 
786
    def test_not_loaded(self):
 
787
        self.warnings = []
 
788
        def captured_warning(*args, **kwargs):
 
789
            self.warnings.append((args, kwargs))
 
790
        self.overrideAttr(trace, 'warning', captured_warning)
 
791
        # Reset the flag that protect against double loading
 
792
        self.overrideAttr(plugin, '_loaded', False)
 
793
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
 
794
        plugin.load_plugins(['.'])
 
795
        self.assertPluginUnknown('test_foo')
 
796
        # Make sure we don't warn about the plugin ImportError since this has
 
797
        # been *requested* by the user.
 
798
        self.assertLength(0, self.warnings)
 
799
 
 
800
 
 
801
class TestLoadPluginAt(tests.TestCaseInTempDir, TestPluginMixin):
 
802
 
 
803
    def setUp(self):
 
804
        super(TestLoadPluginAt, self).setUp()
 
805
        # Make sure we don't pollute the plugins namespace
 
806
        self.overrideAttr(plugins, '__path__')
 
807
        # Be paranoid in case a test fail
 
808
        self.addCleanup(self._unregister_plugin, 'test_foo')
 
809
        # Reset the flag that protect against double loading
 
810
        self.overrideAttr(plugin, '_loaded', False)
 
811
        # Create the same plugin in two directories
 
812
        self.create_plugin_package('test_foo', dir='non-standard-dir')
 
813
        self.create_plugin_package('test_foo', dir='b/test_foo')
 
814
 
 
815
    def assertTestFooLoadedFrom(self, dir):
 
816
        self.assertPluginKnown('test_foo')
 
817
        self.assertEqual('This is the doc for test_foo',
 
818
                         bzrlib.plugins.test_foo.__doc__)
 
819
        self.assertEqual(dir, bzrlib.plugins.test_foo.dir_source)
 
820
 
 
821
    def test_regular_load(self):
 
822
        plugin.load_plugins(['b'])
 
823
        self.assertTestFooLoadedFrom('b/test_foo')
 
824
 
 
825
    def test_import(self):
 
826
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
827
        plugin.set_plugins_path(['b'])
 
828
        try:
 
829
            import bzrlib.plugins.test_foo
 
830
        except ImportError:
 
831
            pass
 
832
        self.assertTestFooLoadedFrom('non-standard-dir')
 
833
 
 
834
    def test_loading(self):
 
835
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
836
        plugin.load_plugins(['b'])
 
837
        self.assertTestFooLoadedFrom('non-standard-dir')
 
838
 
 
839
    def test_compiled_loaded(self):
 
840
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
841
        plugin.load_plugins(['b'])
 
842
        self.assertTestFooLoadedFrom('non-standard-dir')
 
843
        self.assertEqual('non-standard-dir/__init__.py',
 
844
                         bzrlib.plugins.test_foo.__file__)
 
845
 
 
846
        # Try importing again now that the source has been compiled
 
847
        self._unregister_plugin('test_foo')
 
848
        plugin._loaded = False
 
849
        plugin.load_plugins(['b'])
 
850
        self.assertTestFooLoadedFrom('non-standard-dir')
 
851
        if __debug__:
 
852
            suffix = 'pyc'
 
853
        else:
 
854
            suffix = 'pyo'
 
855
        self.assertEqual('non-standard-dir/__init__.%s' % suffix,
 
856
                         bzrlib.plugins.test_foo.__file__)
 
857
 
 
858
    def test_submodule_loading(self):
 
859
        # We create an additional directory under the one for test_foo
 
860
        self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
 
861
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
862
        plugin.set_plugins_path(['b'])
 
863
        import bzrlib.plugins.test_foo
 
864
        self.assertEqual('bzrlib.plugins.test_foo',
 
865
                         bzrlib.plugins.test_foo.__package__)
 
866
        import bzrlib.plugins.test_foo.test_bar
 
867
        self.assertEqual('non-standard-dir/test_bar/__init__.py',
 
868
                         bzrlib.plugins.test_foo.test_bar.__file__)