~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: 2009-12-10 02:12:40 UTC
  • mfrom: (4880.1.1 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20091210021240-dn85gd35waethxku
(mbp) mention GNU in --version

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