~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Vincent Ladeuil
  • Date: 2010-02-10 15:46:03 UTC
  • mfrom: (4985.3.21 update)
  • mto: This revision was merged to the branch mainline in revision 5021.
  • Revision ID: v.ladeuil+lp@free.fr-20100210154603-k4no1gvfuqpzrw7p
Update performs two merges in a more logical order but stop on conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for plugins"""
18
18
 
26
26
import sys
27
27
import zipfile
28
28
 
29
 
from bzrlib import plugin, tests
 
29
from bzrlib import (
 
30
    osutils,
 
31
    plugin,
 
32
    tests,
 
33
    )
30
34
import bzrlib.plugin
31
35
import bzrlib.plugins
32
36
import bzrlib.commands
33
37
import bzrlib.help
34
 
from bzrlib.symbol_versioning import one_three
35
38
from bzrlib.tests import (
36
39
    TestCase,
37
40
    TestCaseInTempDir,
71
74
        # create two plugin directories
72
75
        os.mkdir('first')
73
76
        os.mkdir('second')
74
 
        # write a plugin that will record when its loaded in the 
 
77
        # write a plugin that will record when its loaded in the
75
78
        # tempattribute list.
76
79
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
77
80
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
119
122
        # create two plugin directories
120
123
        os.mkdir('first')
121
124
        os.mkdir('second')
122
 
        # write plugins that will record when they are loaded in the 
 
125
        # write plugins that will record when they are loaded in the
123
126
        # tempattribute list.
124
127
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
125
128
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
171
174
        self.failUnless(tempattribute in self.activeattributes)
172
175
        # create a directory for the plugin
173
176
        os.mkdir('plugin_test')
174
 
        # write a plugin that will record when its loaded in the 
 
177
        # write a plugin that will record when its loaded in the
175
178
        # tempattribute list.
176
179
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
177
180
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
195
198
 
196
199
    def load_and_capture(self, name):
197
200
        """Load plugins from '.' capturing the output.
198
 
        
 
201
 
199
202
        :param name: The name of the plugin.
200
203
        :return: A string with the log from the plugin loading call.
201
204
        """
221
224
            return stream.getvalue()
222
225
        finally:
223
226
            stream.close()
224
 
    
 
227
 
225
228
    def test_plugin_with_bad_api_version_reports(self):
226
229
        # This plugin asks for bzrlib api version 1.0.0, which is not supported
227
230
        # anymore.
258
261
        file('plugin.py', 'w').write(source + '\n')
259
262
        self.addCleanup(self.teardown_plugin)
260
263
        bzrlib.plugin.load_from_path(['.'])
261
 
    
 
264
 
262
265
    def teardown_plugin(self):
263
266
        # remove the plugin 'plugin'
264
267
        if 'bzrlib.plugins.plugin' in sys.modules:
391
394
    def test_dev_fallback__version__with_version_info(self):
392
395
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
393
396
        plugin = bzrlib.plugin.plugins()['plugin']
394
 
        self.assertEqual("1.2.3.dev.4", plugin.__version__)
 
397
        self.assertEqual("1.2.3dev4", plugin.__version__)
395
398
 
396
399
    def test_final__version__with_version_info(self):
397
400
        self.setup_plugin("version_info = (1, 2, 3, 'final', 0)")
398
401
        plugin = bzrlib.plugin.plugins()['plugin']
399
402
        self.assertEqual("1.2.3", plugin.__version__)
400
403
 
 
404
    def test_final_fallback__version__with_version_info(self):
 
405
        self.setup_plugin("version_info = (1, 2, 3, 'final', 2)")
 
406
        plugin = bzrlib.plugin.plugins()['plugin']
 
407
        self.assertEqual("1.2.3.final.2", plugin.__version__)
 
408
 
401
409
 
402
410
class TestPluginHelp(TestCaseInTempDir):
403
411
 
455
463
                delattr(bzrlib.plugins, 'myplug')
456
464
 
457
465
 
458
 
class TestPluginFromZip(TestCaseInTempDir):
459
 
 
460
 
    def make_zipped_plugin(self, zip_name, filename):
461
 
        z = zipfile.ZipFile(zip_name, 'w')
462
 
        z.writestr(filename, PLUGIN_TEXT)
463
 
        z.close()
464
 
 
465
 
    def check_plugin_load(self, zip_name, plugin_name):
466
 
        self.assertFalse(plugin_name in dir(bzrlib.plugins),
467
 
                         'Plugin already loaded')
468
 
        old_path = bzrlib.plugins.__path__
469
 
        try:
470
 
            # this is normally done by load_plugins -> set_plugins_path
471
 
            bzrlib.plugins.__path__ = [zip_name]
472
 
            self.applyDeprecated(one_three,
473
 
                bzrlib.plugin.load_from_zip, zip_name)
474
 
            self.assertTrue(plugin_name in dir(bzrlib.plugins),
475
 
                            'Plugin is not loaded')
476
 
        finally:
477
 
            # unregister plugin
478
 
            if getattr(bzrlib.plugins, plugin_name, None):
479
 
                delattr(bzrlib.plugins, plugin_name)
480
 
                del sys.modules['bzrlib.plugins.' + plugin_name]
481
 
            bzrlib.plugins.__path__ = old_path
482
 
 
483
 
    def test_load_module(self):
484
 
        self.make_zipped_plugin('./test.zip', 'ziplug.py')
485
 
        self.check_plugin_load('./test.zip', 'ziplug')
486
 
 
487
 
    def test_load_package(self):
488
 
        self.make_zipped_plugin('./test.zip', 'ziplug/__init__.py')
489
 
        self.check_plugin_load('./test.zip', 'ziplug')
490
 
 
491
 
 
492
 
class TestSetPluginsPath(TestCase):
493
 
    
494
 
    def test_set_plugins_path(self):
495
 
        """set_plugins_path should set the module __path__ correctly."""
496
 
        old_path = bzrlib.plugins.__path__
497
 
        try:
498
 
            bzrlib.plugins.__path__ = []
499
 
            expected_path = bzrlib.plugin.set_plugins_path()
500
 
            self.assertEqual(expected_path, bzrlib.plugins.__path__)
501
 
        finally:
502
 
            bzrlib.plugins.__path__ = old_path
503
 
 
504
 
    def test_set_plugins_path_with_trailing_slashes(self):
505
 
        """set_plugins_path should set the module __path__ based on
506
 
        BZR_PLUGIN_PATH after removing all trailing slashes."""
507
 
        old_path = bzrlib.plugins.__path__
508
 
        old_env = os.environ.get('BZR_PLUGIN_PATH')
509
 
        try:
510
 
            bzrlib.plugins.__path__ = []
511
 
            os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
512
 
                "second/\\/\\/"
513
 
            bzrlib.plugin.set_plugins_path()
514
 
            # We expect our nominated paths to have all path-seps removed,
515
 
            # and this is testing only that.
516
 
            expected_path = ['first', 'second']
517
 
            self.assertEqual(expected_path,
518
 
                bzrlib.plugins.__path__[:len(expected_path)])
519
 
        finally:
520
 
            bzrlib.plugins.__path__ = old_path
521
 
            if old_env is not None:
522
 
                os.environ['BZR_PLUGIN_PATH'] = old_env
523
 
            else:
524
 
                del os.environ['BZR_PLUGIN_PATH']
525
 
 
526
 
 
527
466
class TestHelpIndex(tests.TestCase):
528
467
    """Tests for the PluginsHelpIndex class."""
529
468
 
632
571
        self.assertEqual('foo_bar', topic.get_help_topic())
633
572
 
634
573
 
635
 
def clear_plugins(test_case):
636
 
    # Save the attributes that we're about to monkey-patch.
637
 
    old_plugins_path = bzrlib.plugins.__path__
638
 
    old_loaded = plugin._loaded
639
 
    old_load_from_path = plugin.load_from_path
640
 
    # Change bzrlib.plugin to think no plugins have been loaded yet.
641
 
    bzrlib.plugins.__path__ = []
642
 
    plugin._loaded = False
643
 
    # Monkey-patch load_from_path to stop it from actually loading anything.
644
 
    def load_from_path(dirs):
645
 
        pass
646
 
    plugin.load_from_path = load_from_path
647
 
    def restore_plugins():
648
 
        bzrlib.plugins.__path__ = old_plugins_path
649
 
        plugin._loaded = old_loaded
650
 
        plugin.load_from_path = old_load_from_path
651
 
    test_case.addCleanup(restore_plugins)
652
 
 
653
 
 
654
 
class TestPluginPaths(tests.TestCase):
 
574
class TestLoadFromPath(tests.TestCaseInTempDir):
 
575
 
 
576
    def setUp(self):
 
577
        super(TestLoadFromPath, self).setUp()
 
578
        # Change bzrlib.plugin to think no plugins have been loaded yet.
 
579
        self.overrideAttr(bzrlib.plugins, '__path__', [])
 
580
        self.overrideAttr(plugin, '_loaded', False)
 
581
 
 
582
        # Monkey-patch load_from_path to stop it from actually loading anything.
 
583
        self.overrideAttr(plugin, 'load_from_path', lambda dirs: None)
655
584
 
656
585
    def test_set_plugins_path_with_args(self):
657
 
        clear_plugins(self)
658
586
        plugin.set_plugins_path(['a', 'b'])
659
587
        self.assertEqual(['a', 'b'], bzrlib.plugins.__path__)
660
588
 
661
589
    def test_set_plugins_path_defaults(self):
662
 
        clear_plugins(self)
663
590
        plugin.set_plugins_path()
664
591
        self.assertEqual(plugin.get_standard_plugins_path(),
665
592
                         bzrlib.plugins.__path__)
666
593
 
667
594
    def test_get_standard_plugins_path(self):
668
595
        path = plugin.get_standard_plugins_path()
669
 
        self.assertEqual(plugin.get_default_plugin_path(), path[0])
670
596
        for directory in path:
671
 
            self.assertNotContainsRe(r'\\/$', directory)
 
597
            self.assertNotContainsRe(directory, r'\\/$')
672
598
        try:
673
599
            from distutils.sysconfig import get_python_lib
674
600
        except ImportError:
684
610
 
685
611
    def test_get_standard_plugins_path_env(self):
686
612
        os.environ['BZR_PLUGIN_PATH'] = 'foo/'
687
 
        self.assertEqual('foo', plugin.get_standard_plugins_path()[0])
688
 
 
689
 
 
690
 
class TestLoadPlugins(tests.TestCaseInTempDir):
 
613
        path = plugin.get_standard_plugins_path()
 
614
        for directory in path:
 
615
            self.assertNotContainsRe(directory, r'\\/$')
691
616
 
692
617
    def test_load_plugins(self):
693
 
        clear_plugins(self)
694
618
        plugin.load_plugins(['.'])
695
619
        self.assertEqual(bzrlib.plugins.__path__, ['.'])
696
620
        # subsequent loads are no-ops
698
622
        self.assertEqual(bzrlib.plugins.__path__, ['.'])
699
623
 
700
624
    def test_load_plugins_default(self):
701
 
        clear_plugins(self)
702
625
        plugin.load_plugins()
703
626
        path = plugin.get_standard_plugins_path()
704
627
        self.assertEqual(path, bzrlib.plugins.__path__)
 
628
 
 
629
 
 
630
class TestEnvPluginPath(tests.TestCaseInTempDir):
 
631
 
 
632
    def setUp(self):
 
633
        super(TestEnvPluginPath, self).setUp()
 
634
        self.overrideAttr(plugin, 'DEFAULT_PLUGIN_PATH', None)
 
635
 
 
636
        self.user = plugin.get_user_plugin_path()
 
637
        self.site = plugin.get_site_plugin_path()
 
638
        self.core = plugin.get_core_plugin_path()
 
639
 
 
640
    def _list2paths(self, *args):
 
641
        paths = []
 
642
        for p in args:
 
643
            plugin._append_new_path(paths, p)
 
644
        return paths
 
645
 
 
646
    def _set_path(self, *args):
 
647
        path = os.pathsep.join(self._list2paths(*args))
 
648
        osutils.set_or_unset_env('BZR_PLUGIN_PATH', path)
 
649
 
 
650
    def check_path(self, expected_dirs, setting_dirs):
 
651
        if setting_dirs:
 
652
            self._set_path(*setting_dirs)
 
653
        actual = plugin.get_standard_plugins_path()
 
654
        self.assertEquals(self._list2paths(*expected_dirs), actual)
 
655
 
 
656
    def test_default(self):
 
657
        self.check_path([self.user, self.core, self.site],
 
658
                        None)
 
659
 
 
660
    def test_adhoc_policy(self):
 
661
        self.check_path([self.user, self.core, self.site],
 
662
                        ['+user', '+core', '+site'])
 
663
 
 
664
    def test_fallback_policy(self):
 
665
        self.check_path([self.core, self.site, self.user],
 
666
                        ['+core', '+site', '+user'])
 
667
 
 
668
    def test_override_policy(self):
 
669
        self.check_path([self.user, self.site, self.core],
 
670
                        ['+user', '+site', '+core'])
 
671
 
 
672
    def test_disable_user(self):
 
673
        self.check_path([self.core, self.site], ['-user'])
 
674
 
 
675
    def test_disable_user_twice(self):
 
676
        # Ensures multiple removals don't left cruft
 
677
        self.check_path([self.core, self.site], ['-user', '-user'])
 
678
 
 
679
    def test_duplicates_are_removed(self):
 
680
        self.check_path([self.user, self.core, self.site],
 
681
                        ['+user', '+user'])
 
682
        # And only the first reference is kept (since the later references will
 
683
        # onnly produce <plugin> already loaded mutters)
 
684
        self.check_path([self.user, self.core, self.site],
 
685
                        ['+user', '+user', '+core',
 
686
                         '+user', '+site', '+site',
 
687
                         '+core'])
 
688
 
 
689
    def test_disable_overrides_disable(self):
 
690
        self.check_path([self.core, self.site], ['-user', '+user'])
 
691
 
 
692
    def test_disable_core(self):
 
693
        self.check_path([self.site], ['-core'])
 
694
        self.check_path([self.user, self.site], ['+user', '-core'])
 
695
 
 
696
    def test_disable_site(self):
 
697
        self.check_path([self.core], ['-site'])
 
698
        self.check_path([self.user, self.core], ['-site', '+user'])
 
699
 
 
700
    def test_override_site(self):
 
701
        self.check_path(['mysite', self.user, self.core],
 
702
                        ['mysite', '-site', '+user'])
 
703
        self.check_path(['mysite', self.core],
 
704
                        ['mysite', '-site'])
 
705
 
 
706
    def test_override_core(self):
 
707
        self.check_path(['mycore', self.user, self.site],
 
708
                        ['mycore', '-core', '+user', '+site'])
 
709
        self.check_path(['mycore', self.site],
 
710
                        ['mycore', '-core'])
 
711
 
 
712
    def test_my_plugin_only(self):
 
713
        self.check_path(['myplugin'], ['myplugin', '-user', '-core', '-site'])
 
714
 
 
715
    def test_my_plugin_first(self):
 
716
        self.check_path(['myplugin', self.core, self.site, self.user],
 
717
                        ['myplugin', '+core', '+site', '+user'])
 
718
 
 
719
    def test_bogus_references(self):
 
720
        self.check_path(['+foo', '-bar', self.core, self.site],
 
721
                        ['+foo', '-bar'])