~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Vincent Ladeuil
  • Date: 2009-09-04 15:36:48 UTC
  • mfrom: (4628.2.5 412930-plugin-path)
  • mto: This revision was merged to the branch mainline in revision 4673.
  • Revision ID: v.ladeuil+lp@free.fr-20090904153648-f4ajhttkhs92mgjz
BZR_PLUGIN_PATH can be used to fully control the plugin directories

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
454
458
                delattr(bzrlib.plugins, 'myplug')
455
459
 
456
460
 
457
 
class TestSetPluginsPath(TestCase):
458
 
 
459
 
    def test_set_plugins_path(self):
460
 
        """set_plugins_path should set the module __path__ correctly."""
461
 
        old_path = bzrlib.plugins.__path__
462
 
        try:
463
 
            bzrlib.plugins.__path__ = []
464
 
            expected_path = bzrlib.plugin.set_plugins_path()
465
 
            self.assertEqual(expected_path, bzrlib.plugins.__path__)
466
 
        finally:
467
 
            bzrlib.plugins.__path__ = old_path
468
 
 
469
 
    def test_set_plugins_path_with_trailing_slashes(self):
470
 
        """set_plugins_path should set the module __path__ based on
471
 
        BZR_PLUGIN_PATH after removing all trailing slashes."""
472
 
        old_path = bzrlib.plugins.__path__
473
 
        old_env = os.environ.get('BZR_PLUGIN_PATH')
474
 
        try:
475
 
            bzrlib.plugins.__path__ = []
476
 
            os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
477
 
                "second/\\/\\/"
478
 
            bzrlib.plugin.set_plugins_path()
479
 
            # We expect our nominated paths to have all path-seps removed,
480
 
            # and this is testing only that.
481
 
            expected_path = ['first', 'second']
482
 
            self.assertEqual(expected_path,
483
 
                bzrlib.plugins.__path__[:len(expected_path)])
484
 
        finally:
485
 
            bzrlib.plugins.__path__ = old_path
486
 
            if old_env is not None:
487
 
                os.environ['BZR_PLUGIN_PATH'] = old_env
488
 
            else:
489
 
                del os.environ['BZR_PLUGIN_PATH']
490
 
 
491
 
 
492
461
class TestHelpIndex(tests.TestCase):
493
462
    """Tests for the PluginsHelpIndex class."""
494
463
 
597
566
        self.assertEqual('foo_bar', topic.get_help_topic())
598
567
 
599
568
 
600
 
def clear_plugins(test_case):
601
 
    # Save the attributes that we're about to monkey-patch.
602
 
    old_plugins_path = bzrlib.plugins.__path__
603
 
    old_loaded = plugin._loaded
604
 
    old_load_from_path = plugin.load_from_path
605
 
    # Change bzrlib.plugin to think no plugins have been loaded yet.
606
 
    bzrlib.plugins.__path__ = []
607
 
    plugin._loaded = False
608
 
    # Monkey-patch load_from_path to stop it from actually loading anything.
609
 
    def load_from_path(dirs):
610
 
        pass
611
 
    plugin.load_from_path = load_from_path
612
 
    def restore_plugins():
613
 
        bzrlib.plugins.__path__ = old_plugins_path
614
 
        plugin._loaded = old_loaded
615
 
        plugin.load_from_path = old_load_from_path
616
 
    test_case.addCleanup(restore_plugins)
617
 
 
618
 
 
619
 
class TestPluginPaths(tests.TestCase):
 
569
class TestLoadFromPath(tests.TestCaseInTempDir):
 
570
 
 
571
    def setUp(self):
 
572
        super(TestLoadFromPath, self).setUp()
 
573
        # Save the attributes that we're about to monkey-patch.
 
574
        old_plugins_path = bzrlib.plugins.__path__
 
575
        old_loaded = plugin._loaded
 
576
        old_load_from_path = plugin.load_from_path
 
577
 
 
578
        def restore():
 
579
            bzrlib.plugins.__path__ = old_plugins_path
 
580
            plugin._loaded = old_loaded
 
581
            plugin.load_from_path = old_load_from_path
 
582
 
 
583
        self.addCleanup(restore)
 
584
 
 
585
        # Change bzrlib.plugin to think no plugins have been loaded yet.
 
586
        bzrlib.plugins.__path__ = []
 
587
        plugin._loaded = False
 
588
 
 
589
        # Monkey-patch load_from_path to stop it from actually loading anything.
 
590
        def load_from_path(dirs):
 
591
            pass
 
592
        plugin.load_from_path = load_from_path
620
593
 
621
594
    def test_set_plugins_path_with_args(self):
622
 
        clear_plugins(self)
623
595
        plugin.set_plugins_path(['a', 'b'])
624
596
        self.assertEqual(['a', 'b'], bzrlib.plugins.__path__)
625
597
 
626
598
    def test_set_plugins_path_defaults(self):
627
 
        clear_plugins(self)
628
599
        plugin.set_plugins_path()
629
600
        self.assertEqual(plugin.get_standard_plugins_path(),
630
601
                         bzrlib.plugins.__path__)
631
602
 
632
603
    def test_get_standard_plugins_path(self):
633
604
        path = plugin.get_standard_plugins_path()
634
 
        self.assertEqual(plugin.get_default_plugin_path(), path[0])
635
605
        for directory in path:
636
606
            self.assertNotContainsRe(directory, r'\\/$')
637
607
        try:
649
619
 
650
620
    def test_get_standard_plugins_path_env(self):
651
621
        os.environ['BZR_PLUGIN_PATH'] = 'foo/'
652
 
        self.assertEqual('foo', plugin.get_standard_plugins_path()[0])
653
 
 
654
 
 
655
 
class TestLoadPlugins(tests.TestCaseInTempDir):
 
622
        path = plugin.get_standard_plugins_path()
 
623
        for directory in path:
 
624
            self.assertNotContainsRe(directory, r'\\/$')
656
625
 
657
626
    def test_load_plugins(self):
658
 
        clear_plugins(self)
659
627
        plugin.load_plugins(['.'])
660
628
        self.assertEqual(bzrlib.plugins.__path__, ['.'])
661
629
        # subsequent loads are no-ops
663
631
        self.assertEqual(bzrlib.plugins.__path__, ['.'])
664
632
 
665
633
    def test_load_plugins_default(self):
666
 
        clear_plugins(self)
667
634
        plugin.load_plugins()
668
635
        path = plugin.get_standard_plugins_path()
669
636
        self.assertEqual(path, bzrlib.plugins.__path__)
 
637
 
 
638
 
 
639
class TestEnvPluginPath(tests.TestCaseInTempDir):
 
640
 
 
641
    def setUp(self):
 
642
        super(TestEnvPluginPath, self).setUp()
 
643
        old_default = plugin.DEFAULT_PLUGIN_PATH
 
644
 
 
645
        def restore():
 
646
            plugin.DEFAULT_PLUGIN_PATH = old_default
 
647
 
 
648
        self.addCleanup(restore)
 
649
 
 
650
        plugin.DEFAULT_PLUGIN_PATH = None
 
651
 
 
652
        self.user = plugin.get_user_plugin_path()
 
653
        self.site = plugin.get_site_plugin_path()
 
654
        self.core = plugin.get_core_plugin_path()
 
655
 
 
656
    def _list2paths(self, *args):
 
657
        paths = []
 
658
        for p in args:
 
659
            plugin._append_new_path(paths, p)
 
660
        return paths
 
661
 
 
662
    def _set_path(self, *args):
 
663
        path = os.pathsep.join(self._list2paths(*args))
 
664
        osutils.set_or_unset_env('BZR_PLUGIN_PATH', path)
 
665
 
 
666
    def check_path(self, expected_dirs, setting_dirs):
 
667
        if setting_dirs:
 
668
            self._set_path(*setting_dirs)
 
669
        actual = plugin.get_standard_plugins_path()
 
670
        self.assertEquals(self._list2paths(*expected_dirs), actual)
 
671
 
 
672
    def test_default(self):
 
673
        self.check_path([self.user, self.core, self.site],
 
674
                        None)
 
675
 
 
676
    def test_adhoc_policy(self):
 
677
        self.check_path([self.user, self.core, self.site],
 
678
                        ['+user', '+core', '+site'])
 
679
 
 
680
    def test_fallback_policy(self):
 
681
        self.check_path([self.core, self.site, self.user],
 
682
                        ['+core', '+site', '+user'])
 
683
 
 
684
    def test_override_policy(self):
 
685
        self.check_path([self.user, self.site, self.core],
 
686
                        ['+user', '+site', '+core'])
 
687
 
 
688
    def test_disable_user(self):
 
689
        self.check_path([self.core, self.site], ['-user'])
 
690
 
 
691
    def test_disable_user_twice(self):
 
692
        # Ensures multiple removals don't left cruft
 
693
        self.check_path([self.core, self.site], ['-user', '-user'])
 
694
 
 
695
    def test_duplicates_are_removed(self):
 
696
        self.check_path([self.user, self.core, self.site],
 
697
                        ['+user', '+user'])
 
698
        # And only the first reference is kept (since the later references will
 
699
        # onnly produce <plugin> already loaded mutters)
 
700
        self.check_path([self.user, self.core, self.site],
 
701
                        ['+user', '+user', '+core',
 
702
                         '+user', '+site', '+site',
 
703
                         '+core'])
 
704
 
 
705
    def test_disable_overrides_disable(self):
 
706
        self.check_path([self.core, self.site], ['-user', '+user'])
 
707
 
 
708
    def test_disable_core(self):
 
709
        self.check_path([self.site], ['-core'])
 
710
        self.check_path([self.user, self.site], ['+user', '-core'])
 
711
 
 
712
    def test_disable_site(self):
 
713
        self.check_path([self.core], ['-site'])
 
714
        self.check_path([self.user, self.core], ['-site', '+user'])
 
715
 
 
716
    def test_override_site(self):
 
717
        self.check_path(['mysite', self.user, self.core],
 
718
                        ['mysite', '-site', '+user'])
 
719
        self.check_path(['mysite', self.core],
 
720
                        ['mysite', '-site'])
 
721
 
 
722
    def test_override_core(self):
 
723
        self.check_path(['mycore', self.user, self.site],
 
724
                        ['mycore', '-core', '+user', '+site'])
 
725
        self.check_path(['mycore', self.site],
 
726
                        ['mycore', '-core'])
 
727
 
 
728
    def test_my_plugin_only(self):
 
729
        self.check_path(['myplugin'], ['myplugin', '-user', '-core', '-site'])
 
730
 
 
731
    def test_my_plugin_first(self):
 
732
        self.check_path(['myplugin', self.core, self.site, self.user],
 
733
                        ['myplugin', '+core', '+site', '+user'])
 
734
 
 
735
    def test_bogus_references(self):
 
736
        self.check_path(['+foo', '-bar', self.core, self.site],
 
737
                        ['+foo', '-bar'])