~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-04-06 08:40:57 UTC
  • mfrom: (5133.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100406084057-8bjovmj2hjaee1bb
Stricter rules for loading plugins from BZR_PLUGINS_AT

Show diffs side-by-side

added added

removed removed

Lines of Context:
810
810
        self.overrideAttr(plugin, '_loaded', False)
811
811
        # Create the same plugin in two directories
812
812
        self.create_plugin_package('test_foo', dir='non-standard-dir')
813
 
        self.create_plugin_package('test_foo', dir='b/test_foo')
 
813
        # The "normal" directory, we use 'standard' instead of 'plugins' to
 
814
        # avoid depending on the precise naming.
 
815
        self.create_plugin_package('test_foo', dir='standard/test_foo')
814
816
 
815
 
    def assertTestFooLoadedFrom(self, dir):
 
817
    def assertTestFooLoadedFrom(self, path):
816
818
        self.assertPluginKnown('test_foo')
817
819
        self.assertEqual('This is the doc for test_foo',
818
820
                         bzrlib.plugins.test_foo.__doc__)
819
 
        self.assertEqual(dir, bzrlib.plugins.test_foo.dir_source)
 
821
        self.assertEqual(path, bzrlib.plugins.test_foo.dir_source)
820
822
 
821
823
    def test_regular_load(self):
822
 
        plugin.load_plugins(['b'])
823
 
        self.assertTestFooLoadedFrom('b/test_foo')
 
824
        plugin.load_plugins(['standard'])
 
825
        self.assertTestFooLoadedFrom('standard/test_foo')
824
826
 
825
827
    def test_import(self):
826
828
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
827
 
        plugin.set_plugins_path(['b'])
 
829
        plugin.set_plugins_path(['standard'])
828
830
        try:
829
831
            import bzrlib.plugins.test_foo
830
832
        except ImportError:
833
835
 
834
836
    def test_loading(self):
835
837
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
836
 
        plugin.load_plugins(['b'])
 
838
        plugin.load_plugins(['standard'])
837
839
        self.assertTestFooLoadedFrom('non-standard-dir')
838
840
 
839
841
    def test_compiled_loaded(self):
840
842
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
841
 
        plugin.load_plugins(['b'])
 
843
        plugin.load_plugins(['standard'])
842
844
        self.assertTestFooLoadedFrom('non-standard-dir')
843
845
        self.assertEqual('non-standard-dir/__init__.py',
844
846
                         bzrlib.plugins.test_foo.__file__)
846
848
        # Try importing again now that the source has been compiled
847
849
        self._unregister_plugin('test_foo')
848
850
        plugin._loaded = False
849
 
        plugin.load_plugins(['b'])
 
851
        plugin.load_plugins(['standard'])
850
852
        self.assertTestFooLoadedFrom('non-standard-dir')
851
853
        if __debug__:
852
854
            suffix = 'pyc'
859
861
        # We create an additional directory under the one for test_foo
860
862
        self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
861
863
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
862
 
        plugin.set_plugins_path(['b'])
 
864
        plugin.set_plugins_path(['standard'])
863
865
        import bzrlib.plugins.test_foo
864
866
        self.assertEqual('bzrlib.plugins.test_foo',
865
867
                         bzrlib.plugins.test_foo.__package__)
866
868
        import bzrlib.plugins.test_foo.test_bar
867
869
        self.assertEqual('non-standard-dir/test_bar/__init__.py',
868
870
                         bzrlib.plugins.test_foo.test_bar.__file__)
 
871
 
 
872
    def test_loading_from___init__only(self):
 
873
        # We rename the existing __init__.py file to ensure that we don't load
 
874
        # a random file
 
875
        init = 'non-standard-dir/__init__.py'
 
876
        random = 'non-standard-dir/setup.py'
 
877
        os.rename(init, random)
 
878
        self.addCleanup(os.rename, random, init)
 
879
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@non-standard-dir')
 
880
        plugin.load_plugins(['standard'])
 
881
        self.assertPluginUnknown('test_foo')
 
882
 
 
883
    def test_loading_from_specific_file(self):
 
884
        plugin_dir = 'non-standard-dir'
 
885
        plugin_file_name = 'iamtestfoo.py'
 
886
        plugin_path = osutils.pathjoin(plugin_dir, plugin_file_name)
 
887
        source = '''\
 
888
"""This is the doc for %s"""
 
889
dir_source = '%s'
 
890
''' % ('test_foo', plugin_path)
 
891
        self.create_plugin('test_foo', source=source,
 
892
                           dir=plugin_dir, file_name=plugin_file_name)
 
893
        osutils.set_or_unset_env('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
 
894
        plugin.load_plugins(['standard'])
 
895
        self.assertTestFooLoadedFrom(plugin_path)