~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Jelmer Vernooij
  • Date: 2012-04-16 11:08:11 UTC
  • mfrom: (6521 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6522.
  • Revision ID: jelmer@samba.org-20120416110811-0y996ihqy9o2bb1t
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2012 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
27
27
#import bzrlib specific imports here
28
28
from bzrlib import (
29
29
    branch,
30
 
    bzrdir,
31
30
    config,
 
31
    controldir,
32
32
    diff,
33
33
    errors,
34
34
    osutils,
35
35
    mail_client,
36
36
    ui,
37
37
    urlutils,
 
38
    registry as _mod_registry,
38
39
    remote,
39
40
    tests,
40
41
    trace,
114
115
 
115
116
def build_control_store(test):
116
117
    build_backing_branch(test, 'branch')
117
 
    b = bzrdir.BzrDir.open('branch')
 
118
    b = controldir.ControlDir.open('branch')
118
119
    return config.ControlStore(b)
119
120
config.test_store_builder_registry.register('control', build_control_store)
120
121
 
475
476
    def test_constructs(self):
476
477
        config.Config()
477
478
 
478
 
    def test_no_default_editor(self):
479
 
        self.assertRaises(
480
 
            NotImplementedError,
481
 
            self.applyDeprecated, deprecated_in((2, 4, 0)),
482
 
            config.Config().get_editor)
483
 
 
484
479
    def test_user_email(self):
485
480
        my_config = InstrumentedConfig()
486
481
        self.assertEqual('robert.collins@example.org', my_config.user_email())
576
571
    def test_config_dir(self):
577
572
        self.assertEqual(config.config_dir(), self.bzr_home)
578
573
 
 
574
    def test_config_dir_is_unicode(self):
 
575
        self.assertIsInstance(config.config_dir(), unicode)
 
576
 
579
577
    def test_config_filename(self):
580
578
        self.assertEqual(config.config_filename(),
581
579
                         self.bzr_home + '/bazaar.conf')
685
683
        self.assertFileEqual(content, 'test.conf')
686
684
 
687
685
 
688
 
class TestIniConfigOptionExpansionDefaultValue(tests.TestCaseInTempDir):
689
 
    """What is the default value of expand for config options.
690
 
 
691
 
    This is an opt-in beta feature used to evaluate whether or not option
692
 
    references can appear in dangerous place raising exceptions, disapearing
693
 
    (and as such corrupting data) or if it's safe to activate the option by
694
 
    default.
695
 
 
696
 
    Note that these tests relies on config._expand_default_value being already
697
 
    overwritten in the parent class setUp.
698
 
    """
699
 
 
700
 
    def setUp(self):
701
 
        super(TestIniConfigOptionExpansionDefaultValue, self).setUp()
702
 
        self.config = None
703
 
        self.warnings = []
704
 
        def warning(*args):
705
 
            self.warnings.append(args[0] % args[1:])
706
 
        self.overrideAttr(trace, 'warning', warning)
707
 
 
708
 
    def get_config(self, expand):
709
 
        c = config.GlobalConfig.from_string('bzr.config.expand=%s' % (expand,),
710
 
                                            save=True)
711
 
        return c
712
 
 
713
 
    def assertExpandIs(self, expected):
714
 
        actual = config._get_expand_default_value()
715
 
        #self.config.get_user_option_as_bool('bzr.config.expand')
716
 
        self.assertEquals(expected, actual)
717
 
 
718
 
    def test_default_is_None(self):
719
 
        self.assertEquals(None, config._expand_default_value)
720
 
 
721
 
    def test_default_is_False_even_if_None(self):
722
 
        self.config = self.get_config(None)
723
 
        self.assertExpandIs(False)
724
 
 
725
 
    def test_default_is_False_even_if_invalid(self):
726
 
        self.config = self.get_config('<your choice>')
727
 
        self.assertExpandIs(False)
728
 
        # ...
729
 
        # Huh ? My choice is False ? Thanks, always happy to hear that :D
730
 
        # Wait, you've been warned !
731
 
        self.assertLength(1, self.warnings)
732
 
        self.assertEquals(
733
 
            'Value "<your choice>" is not a boolean for "bzr.config.expand"',
734
 
            self.warnings[0])
735
 
 
736
 
    def test_default_is_True(self):
737
 
        self.config = self.get_config(True)
738
 
        self.assertExpandIs(True)
739
 
 
740
 
    def test_default_is_False(self):
741
 
        self.config = self.get_config(False)
742
 
        self.assertExpandIs(False)
743
 
 
744
 
 
745
686
class TestIniConfigOptionExpansion(tests.TestCase):
746
687
    """Test option expansion from the IniConfig level.
747
688
 
1138
1079
 
1139
1080
    def test_get_config(self):
1140
1081
        """The Branch.get_config method works properly"""
1141
 
        b = bzrdir.BzrDir.create_standalone_workingtree('.').branch
 
1082
        b = controldir.ControlDir.create_standalone_workingtree('.').branch
1142
1083
        my_config = b.get_config()
1143
1084
        self.assertIs(my_config.get_user_option('wacky'), None)
1144
1085
        my_config.set_user_option('wacky', 'unlikely')
1193
1134
        b = self.make_branch('!repo')
1194
1135
        self.assertEqual('!repo', b.get_config().get_nickname())
1195
1136
 
 
1137
    def test_autonick_uses_branch_name(self):
 
1138
        b = self.make_branch('foo', name='bar')
 
1139
        self.assertEqual('bar', b.get_config().get_nickname())
 
1140
 
1196
1141
    def test_warn_if_masked(self):
1197
1142
        warnings = []
1198
1143
        def warning(*args):
1238
1183
        my_config = config.GlobalConfig()
1239
1184
        self.assertEqual(None, my_config._get_user_id())
1240
1185
 
1241
 
    def test_configured_editor(self):
1242
 
        my_config = config.GlobalConfig.from_string(sample_config_text)
1243
 
        editor = self.applyDeprecated(
1244
 
            deprecated_in((2, 4, 0)), my_config.get_editor)
1245
 
        self.assertEqual('vim', editor)
1246
 
 
1247
1186
    def test_signatures_always(self):
1248
1187
        my_config = config.GlobalConfig.from_string(sample_always_signatures)
1249
1188
        self.assertEqual(config.CHECK_NEVER,
1903
1842
            location='http://example.com/specific')
1904
1843
        self.assertEqual(my_config.get_user_option('option'), 'exact')
1905
1844
 
1906
 
    def test_get_mail_client(self):
1907
 
        config = self.get_branch_config()
1908
 
        client = config.get_mail_client()
1909
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1910
 
 
1911
 
        # Specific clients
1912
 
        config.set_user_option('mail_client', 'evolution')
1913
 
        client = config.get_mail_client()
1914
 
        self.assertIsInstance(client, mail_client.Evolution)
1915
 
 
1916
 
        config.set_user_option('mail_client', 'kmail')
1917
 
        client = config.get_mail_client()
1918
 
        self.assertIsInstance(client, mail_client.KMail)
1919
 
 
1920
 
        config.set_user_option('mail_client', 'mutt')
1921
 
        client = config.get_mail_client()
1922
 
        self.assertIsInstance(client, mail_client.Mutt)
1923
 
 
1924
 
        config.set_user_option('mail_client', 'thunderbird')
1925
 
        client = config.get_mail_client()
1926
 
        self.assertIsInstance(client, mail_client.Thunderbird)
1927
 
 
1928
 
        # Generic options
1929
 
        config.set_user_option('mail_client', 'default')
1930
 
        client = config.get_mail_client()
1931
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1932
 
 
1933
 
        config.set_user_option('mail_client', 'editor')
1934
 
        client = config.get_mail_client()
1935
 
        self.assertIsInstance(client, mail_client.Editor)
1936
 
 
1937
 
        config.set_user_option('mail_client', 'mapi')
1938
 
        client = config.get_mail_client()
1939
 
        self.assertIsInstance(client, mail_client.MAPIClient)
1940
 
 
1941
 
        config.set_user_option('mail_client', 'xdg-email')
1942
 
        client = config.get_mail_client()
1943
 
        self.assertIsInstance(client, mail_client.XDGEmail)
1944
 
 
1945
 
        config.set_user_option('mail_client', 'firebird')
1946
 
        self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1947
 
 
1948
1845
 
1949
1846
class TestMailAddressExtraction(tests.TestCase):
1950
1847
 
2237
2134
        self.assertGetHook(remote_branch._get_config(), 'file', 'branch')
2238
2135
 
2239
2136
    def test_get_hook_remote_bzrdir(self):
2240
 
        remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
 
2137
        remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
2241
2138
        conf = remote_bzrdir._get_config()
2242
2139
        conf.set_option('remotedir', 'file')
2243
2140
        self.assertGetHook(conf, 'file', 'remotedir')
2265
2162
    def test_set_hook_remote_bzrdir(self):
2266
2163
        remote_branch = branch.Branch.open(self.get_url('tree'))
2267
2164
        self.addCleanup(remote_branch.lock_write().unlock)
2268
 
        remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
 
2165
        remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
2269
2166
        self.assertSetHook(remote_bzrdir._get_config(), 'file', 'remotedir')
2270
2167
 
2271
2168
    def assertLoadHook(self, expected_nb_calls, name, conf_class, *conf_args):
2288
2185
        self.assertLoadHook(1, 'file', remote.RemoteBranchConfig, remote_branch)
2289
2186
 
2290
2187
    def test_load_hook_remote_bzrdir(self):
2291
 
        remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
 
2188
        remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
2292
2189
        # The config file doesn't exist, set an option to force its creation
2293
2190
        conf = remote_bzrdir._get_config()
2294
2191
        conf.set_option('remotedir', 'file')
2319
2216
    def test_save_hook_remote_bzrdir(self):
2320
2217
        remote_branch = branch.Branch.open(self.get_url('tree'))
2321
2218
        self.addCleanup(remote_branch.lock_write().unlock)
2322
 
        remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
 
2219
        remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
2323
2220
        self.assertSaveHook(remote_bzrdir._get_config())
2324
2221
 
2325
2222
 
2362
2259
        opt = config.Option('foo', default=bar_not_unicode)
2363
2260
        self.assertRaises(AssertionError, opt.get_default)
2364
2261
 
 
2262
    def test_get_help_topic(self):
 
2263
        opt = config.Option('foo')
 
2264
        self.assertEquals('foo', opt.get_help_topic())
 
2265
 
2365
2266
 
2366
2267
class TestOptionConverterMixin(object):
2367
2268
 
2480
2381
        self.assertConverted([u'bar'], opt, u'bar')
2481
2382
 
2482
2383
 
 
2384
class TestRegistryOption(tests.TestCase, TestOptionConverterMixin):
 
2385
 
 
2386
    def get_option(self, registry):
 
2387
        return config.RegistryOption('foo', registry,
 
2388
                help='A registry option.')
 
2389
 
 
2390
    def test_convert_invalid(self):
 
2391
        registry = _mod_registry.Registry()
 
2392
        opt = self.get_option(registry)
 
2393
        self.assertConvertInvalid(opt, [1])
 
2394
        self.assertConvertInvalid(opt, u"notregistered")
 
2395
 
 
2396
    def test_convert_valid(self):
 
2397
        registry = _mod_registry.Registry()
 
2398
        registry.register("someval", 1234)
 
2399
        opt = self.get_option(registry)
 
2400
        # Using a bare str() just in case
 
2401
        self.assertConverted(1234, opt, "someval")
 
2402
        self.assertConverted(1234, opt, u'someval')
 
2403
        self.assertConverted(None, opt, None)
 
2404
 
 
2405
    def test_help(self):
 
2406
        registry = _mod_registry.Registry()
 
2407
        registry.register("someval", 1234, help="some option")
 
2408
        registry.register("dunno", 1234, help="some other option")
 
2409
        opt = self.get_option(registry)
 
2410
        self.assertEquals(
 
2411
            'A registry option.\n'
 
2412
            '\n'
 
2413
            'The following values are supported:\n'
 
2414
            ' dunno - some other option\n'
 
2415
            ' someval - some option\n',
 
2416
            opt.help)
 
2417
 
 
2418
    def test_get_help_text(self):
 
2419
        registry = _mod_registry.Registry()
 
2420
        registry.register("someval", 1234, help="some option")
 
2421
        registry.register("dunno", 1234, help="some other option")
 
2422
        opt = self.get_option(registry)
 
2423
        self.assertEquals(
 
2424
            'A registry option.\n'
 
2425
            '\n'
 
2426
            'The following values are supported:\n'
 
2427
            ' dunno - some other option\n'
 
2428
            ' someval - some option\n',
 
2429
            opt.get_help_text())
 
2430
 
 
2431
 
2483
2432
class TestOptionRegistry(tests.TestCase):
2484
2433
 
2485
2434
    def setUp(self):
2911
2860
        store.save()
2912
2861
        self.assertEquals(False, self.has_store(store))
2913
2862
 
 
2863
    def test_mutable_section_shared(self):
 
2864
        store = self.get_store(self)
 
2865
        store._load_from_string('foo=bar\n')
 
2866
        # FIXME: There should be a better way than relying on the test
 
2867
        # parametrization to identify branch.conf -- vila 2011-0526
 
2868
        if self.store_id in ('branch', 'remote_branch'):
 
2869
            # branch stores requires write locked branches
 
2870
            self.addCleanup(store.branch.lock_write().unlock)
 
2871
        section1 = store.get_mutable_section(None)
 
2872
        section2 = store.get_mutable_section(None)
 
2873
        # If we get different sections, different callers won't share the
 
2874
        # modification
 
2875
        self.assertIs(section1, section2)
 
2876
 
2914
2877
    def test_save_emptied_succeeds(self):
2915
2878
        store = self.get_store(self)
2916
2879
        store._load_from_string('foo=bar\n')
 
2880
        # FIXME: There should be a better way than relying on the test
 
2881
        # parametrization to identify branch.conf -- vila 2011-0526
 
2882
        if self.store_id in ('branch', 'remote_branch'):
 
2883
            # branch stores requires write locked branches
 
2884
            self.addCleanup(store.branch.lock_write().unlock)
2917
2885
        section = store.get_mutable_section(None)
2918
2886
        section.remove('foo')
2919
2887
        store.save()
2940
2908
 
2941
2909
    def test_set_option_in_empty_store(self):
2942
2910
        store = self.get_store(self)
 
2911
        # FIXME: There should be a better way than relying on the test
 
2912
        # parametrization to identify branch.conf -- vila 2011-0526
 
2913
        if self.store_id in ('branch', 'remote_branch'):
 
2914
            # branch stores requires write locked branches
 
2915
            self.addCleanup(store.branch.lock_write().unlock)
2943
2916
        section = store.get_mutable_section(None)
2944
2917
        section.set('foo', 'bar')
2945
2918
        store.save()
2951
2924
    def test_set_option_in_default_section(self):
2952
2925
        store = self.get_store(self)
2953
2926
        store._load_from_string('')
 
2927
        # FIXME: There should be a better way than relying on the test
 
2928
        # parametrization to identify branch.conf -- vila 2011-0526
 
2929
        if self.store_id in ('branch', 'remote_branch'):
 
2930
            # branch stores requires write locked branches
 
2931
            self.addCleanup(store.branch.lock_write().unlock)
2954
2932
        section = store.get_mutable_section(None)
2955
2933
        section.set('foo', 'bar')
2956
2934
        store.save()
2962
2940
    def test_set_option_in_named_section(self):
2963
2941
        store = self.get_store(self)
2964
2942
        store._load_from_string('')
 
2943
        # FIXME: There should be a better way than relying on the test
 
2944
        # parametrization to identify branch.conf -- vila 2011-0526
 
2945
        if self.store_id in ('branch', 'remote_branch'):
 
2946
            # branch stores requires write locked branches
 
2947
            self.addCleanup(store.branch.lock_write().unlock)
2965
2948
        section = store.get_mutable_section('baz')
2966
2949
        section.set('foo', 'bar')
2967
2950
        store.save()
2971
2954
        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
2972
2955
 
2973
2956
    def test_load_hook(self):
2974
 
        # We first needs to ensure that the store exists
 
2957
        # First, we need to ensure that the store exists
2975
2958
        store = self.get_store(self)
 
2959
        # FIXME: There should be a better way than relying on the test
 
2960
        # parametrization to identify branch.conf -- vila 2011-0526
 
2961
        if self.store_id in ('branch', 'remote_branch'):
 
2962
            # branch stores requires write locked branches
 
2963
            self.addCleanup(store.branch.lock_write().unlock)
2976
2964
        section = store.get_mutable_section('baz')
2977
2965
        section.set('foo', 'bar')
2978
2966
        store.save()
2994
2982
        config.ConfigHooks.install_named_hook('save', hook, None)
2995
2983
        self.assertLength(0, calls)
2996
2984
        store = self.get_store(self)
 
2985
        # FIXME: There should be a better way than relying on the test
 
2986
        # parametrization to identify branch.conf -- vila 2011-0526
 
2987
        if self.store_id in ('branch', 'remote_branch'):
 
2988
            # branch stores requires write locked branches
 
2989
            self.addCleanup(store.branch.lock_write().unlock)
2997
2990
        section = store.get_mutable_section('baz')
2998
2991
        section.set('foo', 'bar')
2999
2992
        store.save()
3630
3623
        self.assertEquals('bar', conf.get('foo'))
3631
3624
 
3632
3625
 
 
3626
class TestStackIterSections(tests.TestCase):
 
3627
 
 
3628
    def test_empty_stack(self):
 
3629
        conf = config.Stack([])
 
3630
        sections = list(conf.iter_sections())
 
3631
        self.assertLength(0, sections)
 
3632
 
 
3633
    def test_empty_store(self):
 
3634
        store = config.IniFileStore()
 
3635
        store._load_from_string('')
 
3636
        conf = config.Stack([store.get_sections])
 
3637
        sections = list(conf.iter_sections())
 
3638
        self.assertLength(0, sections)
 
3639
 
 
3640
    def test_simple_store(self):
 
3641
        store = config.IniFileStore()
 
3642
        store._load_from_string('foo=bar')
 
3643
        conf = config.Stack([store.get_sections])
 
3644
        tuples = list(conf.iter_sections())
 
3645
        self.assertLength(1, tuples)
 
3646
        (found_store, found_section) = tuples[0]
 
3647
        self.assertIs(store, found_store)
 
3648
 
 
3649
    def test_two_stores(self):
 
3650
        store1 = config.IniFileStore()
 
3651
        store1._load_from_string('foo=bar')
 
3652
        store2 = config.IniFileStore()
 
3653
        store2._load_from_string('bar=qux')
 
3654
        conf = config.Stack([store1.get_sections, store2.get_sections])
 
3655
        tuples = list(conf.iter_sections())
 
3656
        self.assertLength(2, tuples)
 
3657
        self.assertIs(store1, tuples[0][0])
 
3658
        self.assertIs(store2, tuples[1][0])
 
3659
 
 
3660
 
3633
3661
class TestStackWithTransport(tests.TestCaseWithTransport):
3634
3662
 
3635
3663
    scenarios = [(key, {'get_stack': builder}) for key, builder
3825
3853
        super(TestStackExpandOptions, self).setUp()
3826
3854
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3827
3855
        self.registry = config.option_registry
3828
 
        self.conf = build_branch_stack(self)
 
3856
        store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
 
3857
        self.conf = config.Stack([store.get_sections], store)
3829
3858
 
3830
3859
    def assertExpansion(self, expected, string, env=None):
3831
3860
        self.assertEquals(expected, self.conf.expand_options(string, env))
3914
3943
baz=end
3915
3944
list={foo}
3916
3945
''')
3917
 
        self.registry.register(
3918
 
            config.ListOption('list'))
 
3946
        self.registry.register(config.ListOption('list'))
 
3947
        # Register an intermediate option as a list to ensure no conversion
 
3948
        # happen while expanding. Conversion should only occur for the original
 
3949
        # option ('list' here).
 
3950
        self.registry.register(config.ListOption('baz'))
3919
3951
        self.assertEquals(['start', 'middle', 'end'],
3920
3952
                           self.conf.get('list', expand=True))
3921
3953
 
4840
4872
        self.overrideEnv('BZR_EMAIL', None)
4841
4873
        self.overrideEnv('EMAIL', 'jelmer@samba.org')
4842
4874
        self.assertEquals('jelmer@debian.org', conf.get('email'))
 
4875
 
 
4876
 
 
4877
class MailClientOptionTests(tests.TestCase):
 
4878
 
 
4879
    def test_default(self):
 
4880
        conf = config.MemoryStack('')
 
4881
        client = conf.get('mail_client')
 
4882
        self.assertIs(client, mail_client.DefaultMail)
 
4883
 
 
4884
    def test_evolution(self):
 
4885
        conf = config.MemoryStack('mail_client=evolution')
 
4886
        client = conf.get('mail_client')
 
4887
        self.assertIs(client, mail_client.Evolution)
 
4888
 
 
4889
    def test_kmail(self):
 
4890
        conf = config.MemoryStack('mail_client=kmail')
 
4891
        client = conf.get('mail_client')
 
4892
        self.assertIs(client, mail_client.KMail)
 
4893
 
 
4894
    def test_mutt(self):
 
4895
        conf = config.MemoryStack('mail_client=mutt')
 
4896
        client = conf.get('mail_client')
 
4897
        self.assertIs(client, mail_client.Mutt)
 
4898
 
 
4899
    def test_thunderbird(self):
 
4900
        conf = config.MemoryStack('mail_client=thunderbird')
 
4901
        client = conf.get('mail_client')
 
4902
        self.assertIs(client, mail_client.Thunderbird)
 
4903
 
 
4904
    def test_explicit_default(self):
 
4905
        conf = config.MemoryStack('mail_client=default')
 
4906
        client = conf.get('mail_client')
 
4907
        self.assertIs(client, mail_client.DefaultMail)
 
4908
 
 
4909
    def test_editor(self):
 
4910
        conf = config.MemoryStack('mail_client=editor')
 
4911
        client = conf.get('mail_client')
 
4912
        self.assertIs(client, mail_client.Editor)
 
4913
 
 
4914
    def test_mapi(self):
 
4915
        conf = config.MemoryStack('mail_client=mapi')
 
4916
        client = conf.get('mail_client')
 
4917
        self.assertIs(client, mail_client.MAPIClient)
 
4918
 
 
4919
    def test_xdg_email(self):
 
4920
        conf = config.MemoryStack('mail_client=xdg-email')
 
4921
        client = conf.get('mail_client')
 
4922
        self.assertIs(client, mail_client.XDGEmail)
 
4923
 
 
4924
    def test_unknown(self):
 
4925
        conf = config.MemoryStack('mail_client=firebird')
 
4926
        self.assertRaises(errors.ConfigOptionValueError, conf.get,
 
4927
                'mail_client')