/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: Vincent Ladeuil
  • Date: 2010-03-24 13:58:37 UTC
  • mfrom: (5086.5.12 82693-plugin-at-path)
  • mto: This revision was merged to the branch mainline in revision 5108.
  • Revision ID: v.ladeuil+lp@free.fr-20100324135837-06hiz4gj3b29zlr0
Add BZR_PLUGINS_AT support to specify a directory for a given plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
class TestPluginMixin(object):
41
41
 
42
 
    def create_plugin(self, name, source='', dir='.', file_name=None):
 
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)
43
47
        if file_name is None:
44
48
            file_name = name + '.py'
45
49
        # 'source' must not fail to load
51
55
        finally:
52
56
            f.close()
53
57
 
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,
 
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,
59
72
                           file_name='__init__.py')
60
73
 
61
74
    def _unregister_plugin(self, name):
767
780
        self.overrideAttr(plugin, '_loaded', False)
768
781
        plugin.load_plugins(['.'])
769
782
        self.assertPluginKnown('test_foo')
 
783
        self.assertEqual("This is the doc for test_foo",
 
784
                         bzrlib.plugins.test_foo.__doc__)
770
785
 
771
786
    def test_not_loaded(self):
772
787
        self.warnings = []
773
788
        def captured_warning(*args, **kwargs):
774
789
            self.warnings.append((args, kwargs))
775
790
        self.overrideAttr(trace, 'warning', captured_warning)
 
791
        # Reset the flag that protect against double loading
776
792
        self.overrideAttr(plugin, '_loaded', False)
777
793
        osutils.set_or_unset_env('BZR_DISABLE_PLUGINS', 'test_foo')
778
 
        plugin.load_plugins(plugin.set_plugins_path(['.']))
 
794
        plugin.load_plugins(['.'])
779
795
        self.assertPluginUnknown('test_foo')
780
796
        # Make sure we don't warn about the plugin ImportError since this has
781
797
        # been *requested* by the user.
782
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__)