~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Robert Collins
  • Date: 2009-04-27 03:27:46 UTC
  • mto: This revision was merged to the branch mainline in revision 4304.
  • Revision ID: robertc@robertcollins.net-20090427032746-vqmcsfbsbvbm04sk
Fixup tests broken by cleaning up the layering.

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 (
30
 
    osutils,
31
 
    plugin,
32
 
    tests,
33
 
    )
 
29
from bzrlib import plugin, tests
34
30
import bzrlib.plugin
35
31
import bzrlib.plugins
36
32
import bzrlib.commands
394
390
    def test_dev_fallback__version__with_version_info(self):
395
391
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
396
392
        plugin = bzrlib.plugin.plugins()['plugin']
397
 
        self.assertEqual("1.2.3dev4", plugin.__version__)
 
393
        self.assertEqual("1.2.3.dev.4", plugin.__version__)
398
394
 
399
395
    def test_final__version__with_version_info(self):
400
396
        self.setup_plugin("version_info = (1, 2, 3, 'final', 0)")
401
397
        plugin = bzrlib.plugin.plugins()['plugin']
402
398
        self.assertEqual("1.2.3", plugin.__version__)
403
399
 
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
 
 
409
400
 
410
401
class TestPluginHelp(TestCaseInTempDir):
411
402
 
463
454
                delattr(bzrlib.plugins, 'myplug')
464
455
 
465
456
 
 
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
 
466
492
class TestHelpIndex(tests.TestCase):
467
493
    """Tests for the PluginsHelpIndex class."""
468
494
 
571
597
        self.assertEqual('foo_bar', topic.get_help_topic())
572
598
 
573
599
 
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)
 
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):
584
620
 
585
621
    def test_set_plugins_path_with_args(self):
 
622
        clear_plugins(self)
586
623
        plugin.set_plugins_path(['a', 'b'])
587
624
        self.assertEqual(['a', 'b'], bzrlib.plugins.__path__)
588
625
 
589
626
    def test_set_plugins_path_defaults(self):
 
627
        clear_plugins(self)
590
628
        plugin.set_plugins_path()
591
629
        self.assertEqual(plugin.get_standard_plugins_path(),
592
630
                         bzrlib.plugins.__path__)
593
631
 
594
632
    def test_get_standard_plugins_path(self):
595
633
        path = plugin.get_standard_plugins_path()
 
634
        self.assertEqual(plugin.get_default_plugin_path(), path[0])
596
635
        for directory in path:
597
 
            self.assertNotContainsRe(directory, r'\\/$')
 
636
            self.assertNotContainsRe(r'\\/$', directory)
598
637
        try:
599
638
            from distutils.sysconfig import get_python_lib
600
639
        except ImportError:
610
649
 
611
650
    def test_get_standard_plugins_path_env(self):
612
651
        os.environ['BZR_PLUGIN_PATH'] = 'foo/'
613
 
        path = plugin.get_standard_plugins_path()
614
 
        for directory in path:
615
 
            self.assertNotContainsRe(directory, r'\\/$')
 
652
        self.assertEqual('foo', plugin.get_standard_plugins_path()[0])
 
653
 
 
654
 
 
655
class TestLoadPlugins(tests.TestCaseInTempDir):
616
656
 
617
657
    def test_load_plugins(self):
 
658
        clear_plugins(self)
618
659
        plugin.load_plugins(['.'])
619
660
        self.assertEqual(bzrlib.plugins.__path__, ['.'])
620
661
        # subsequent loads are no-ops
622
663
        self.assertEqual(bzrlib.plugins.__path__, ['.'])
623
664
 
624
665
    def test_load_plugins_default(self):
 
666
        clear_plugins(self)
625
667
        plugin.load_plugins()
626
668
        path = plugin.get_standard_plugins_path()
627
669
        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'])