~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

MergeĀ lp:bzr.

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
35
35
    mail_client,
36
36
    ui,
37
37
    urlutils,
 
38
    registry as _mod_registry,
38
39
    remote,
39
40
    tests,
40
41
    trace,
1910
1911
            location='http://example.com/specific')
1911
1912
        self.assertEqual(my_config.get_user_option('option'), 'exact')
1912
1913
 
1913
 
    def test_get_mail_client(self):
1914
 
        config = self.get_branch_config()
1915
 
        client = config.get_mail_client()
1916
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1917
 
 
1918
 
        # Specific clients
1919
 
        config.set_user_option('mail_client', 'evolution')
1920
 
        client = config.get_mail_client()
1921
 
        self.assertIsInstance(client, mail_client.Evolution)
1922
 
 
1923
 
        config.set_user_option('mail_client', 'kmail')
1924
 
        client = config.get_mail_client()
1925
 
        self.assertIsInstance(client, mail_client.KMail)
1926
 
 
1927
 
        config.set_user_option('mail_client', 'mutt')
1928
 
        client = config.get_mail_client()
1929
 
        self.assertIsInstance(client, mail_client.Mutt)
1930
 
 
1931
 
        config.set_user_option('mail_client', 'thunderbird')
1932
 
        client = config.get_mail_client()
1933
 
        self.assertIsInstance(client, mail_client.Thunderbird)
1934
 
 
1935
 
        # Generic options
1936
 
        config.set_user_option('mail_client', 'default')
1937
 
        client = config.get_mail_client()
1938
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1939
 
 
1940
 
        config.set_user_option('mail_client', 'editor')
1941
 
        client = config.get_mail_client()
1942
 
        self.assertIsInstance(client, mail_client.Editor)
1943
 
 
1944
 
        config.set_user_option('mail_client', 'mapi')
1945
 
        client = config.get_mail_client()
1946
 
        self.assertIsInstance(client, mail_client.MAPIClient)
1947
 
 
1948
 
        config.set_user_option('mail_client', 'xdg-email')
1949
 
        client = config.get_mail_client()
1950
 
        self.assertIsInstance(client, mail_client.XDGEmail)
1951
 
 
1952
 
        config.set_user_option('mail_client', 'firebird')
1953
 
        self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1954
 
 
1955
1914
 
1956
1915
class TestMailAddressExtraction(tests.TestCase):
1957
1916
 
2487
2446
        self.assertConverted([u'bar'], opt, u'bar')
2488
2447
 
2489
2448
 
 
2449
class TestRegistryOption(tests.TestCase, TestOptionConverterMixin):
 
2450
 
 
2451
    def get_option(self, registry):
 
2452
        return config.RegistryOption('foo', registry,
 
2453
                help='A registry option.')
 
2454
 
 
2455
    def test_convert_invalid(self):
 
2456
        registry = _mod_registry.Registry()
 
2457
        opt = self.get_option(registry)
 
2458
        self.assertConvertInvalid(opt, [1])
 
2459
        self.assertConvertInvalid(opt, u"notregistered")
 
2460
 
 
2461
    def test_convert_valid(self):
 
2462
        registry = _mod_registry.Registry()
 
2463
        registry.register("someval", 1234)
 
2464
        opt = self.get_option(registry)
 
2465
        # Using a bare str() just in case
 
2466
        self.assertConverted(1234, opt, "someval")
 
2467
        self.assertConverted(1234, opt, u'someval')
 
2468
        self.assertConverted(None, opt, None)
 
2469
 
 
2470
    def test_help(self):
 
2471
        registry = _mod_registry.Registry()
 
2472
        registry.register("someval", 1234, help="some option")
 
2473
        registry.register("dunno", 1234, help="some other option")
 
2474
        opt = self.get_option(registry)
 
2475
        self.assertEquals(
 
2476
            'A registry option.\n'
 
2477
            '\n'
 
2478
            'The following values are supported:\n'
 
2479
            ' dunno - some other option\n'
 
2480
            ' someval - some option\n',
 
2481
            opt.help)
 
2482
 
 
2483
    def test_get_help_text(self):
 
2484
        registry = _mod_registry.Registry()
 
2485
        registry.register("someval", 1234, help="some option")
 
2486
        registry.register("dunno", 1234, help="some other option")
 
2487
        opt = self.get_option(registry)
 
2488
        self.assertEquals(
 
2489
            'A registry option.\n'
 
2490
            '\n'
 
2491
            'The following values are supported:\n'
 
2492
            ' dunno - some other option\n'
 
2493
            ' someval - some option\n',
 
2494
            opt.get_help_text())
 
2495
 
 
2496
 
2490
2497
class TestOptionRegistry(tests.TestCase):
2491
2498
 
2492
2499
    def setUp(self):
2921
2928
    def test_save_emptied_succeeds(self):
2922
2929
        store = self.get_store(self)
2923
2930
        store._load_from_string('foo=bar\n')
 
2931
        # FIXME: There should be a better way than relying on the test
 
2932
        # parametrization to identify branch.conf -- vila 2011-0526
 
2933
        if self.store_id in ('branch', 'remote_branch'):
 
2934
            # branch stores requires write locked branches
 
2935
            self.addCleanup(store.branch.lock_write().unlock)
2924
2936
        section = store.get_mutable_section(None)
2925
2937
        section.remove('foo')
2926
2938
        store.save()
2947
2959
 
2948
2960
    def test_set_option_in_empty_store(self):
2949
2961
        store = self.get_store(self)
 
2962
        # FIXME: There should be a better way than relying on the test
 
2963
        # parametrization to identify branch.conf -- vila 2011-0526
 
2964
        if self.store_id in ('branch', 'remote_branch'):
 
2965
            # branch stores requires write locked branches
 
2966
            self.addCleanup(store.branch.lock_write().unlock)
2950
2967
        section = store.get_mutable_section(None)
2951
2968
        section.set('foo', 'bar')
2952
2969
        store.save()
2958
2975
    def test_set_option_in_default_section(self):
2959
2976
        store = self.get_store(self)
2960
2977
        store._load_from_string('')
 
2978
        # FIXME: There should be a better way than relying on the test
 
2979
        # parametrization to identify branch.conf -- vila 2011-0526
 
2980
        if self.store_id in ('branch', 'remote_branch'):
 
2981
            # branch stores requires write locked branches
 
2982
            self.addCleanup(store.branch.lock_write().unlock)
2961
2983
        section = store.get_mutable_section(None)
2962
2984
        section.set('foo', 'bar')
2963
2985
        store.save()
2969
2991
    def test_set_option_in_named_section(self):
2970
2992
        store = self.get_store(self)
2971
2993
        store._load_from_string('')
 
2994
        # FIXME: There should be a better way than relying on the test
 
2995
        # parametrization to identify branch.conf -- vila 2011-0526
 
2996
        if self.store_id in ('branch', 'remote_branch'):
 
2997
            # branch stores requires write locked branches
 
2998
            self.addCleanup(store.branch.lock_write().unlock)
2972
2999
        section = store.get_mutable_section('baz')
2973
3000
        section.set('foo', 'bar')
2974
3001
        store.save()
2978
3005
        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
2979
3006
 
2980
3007
    def test_load_hook(self):
2981
 
        # We first needs to ensure that the store exists
 
3008
        # First, we need to ensure that the store exists
2982
3009
        store = self.get_store(self)
 
3010
        # FIXME: There should be a better way than relying on the test
 
3011
        # parametrization to identify branch.conf -- vila 2011-0526
 
3012
        if self.store_id in ('branch', 'remote_branch'):
 
3013
            # branch stores requires write locked branches
 
3014
            self.addCleanup(store.branch.lock_write().unlock)
2983
3015
        section = store.get_mutable_section('baz')
2984
3016
        section.set('foo', 'bar')
2985
3017
        store.save()
3001
3033
        config.ConfigHooks.install_named_hook('save', hook, None)
3002
3034
        self.assertLength(0, calls)
3003
3035
        store = self.get_store(self)
 
3036
        # FIXME: There should be a better way than relying on the test
 
3037
        # parametrization to identify branch.conf -- vila 2011-0526
 
3038
        if self.store_id in ('branch', 'remote_branch'):
 
3039
            # branch stores requires write locked branches
 
3040
            self.addCleanup(store.branch.lock_write().unlock)
3004
3041
        section = store.get_mutable_section('baz')
3005
3042
        section.set('foo', 'bar')
3006
3043
        store.save()
3637
3674
        self.assertEquals('bar', conf.get('foo'))
3638
3675
 
3639
3676
 
 
3677
class TestStackIterSections(tests.TestCase):
 
3678
 
 
3679
    def test_empty_stack(self):
 
3680
        conf = config.Stack([])
 
3681
        sections = list(conf.iter_sections())
 
3682
        self.assertLength(0, sections)
 
3683
 
 
3684
    def test_empty_store(self):
 
3685
        store = config.IniFileStore()
 
3686
        store._load_from_string('')
 
3687
        conf = config.Stack([store.get_sections])
 
3688
        sections = list(conf.iter_sections())
 
3689
        self.assertLength(0, sections)
 
3690
 
 
3691
    def test_simple_store(self):
 
3692
        store = config.IniFileStore()
 
3693
        store._load_from_string('foo=bar')
 
3694
        conf = config.Stack([store.get_sections])
 
3695
        tuples = list(conf.iter_sections())
 
3696
        self.assertLength(1, tuples)
 
3697
        (found_store, found_section) = tuples[0]
 
3698
        self.assertIs(store, found_store)
 
3699
 
 
3700
    def test_two_stores(self):
 
3701
        store1 = config.IniFileStore()
 
3702
        store1._load_from_string('foo=bar')
 
3703
        store2 = config.IniFileStore()
 
3704
        store2._load_from_string('bar=qux')
 
3705
        conf = config.Stack([store1.get_sections, store2.get_sections])
 
3706
        tuples = list(conf.iter_sections())
 
3707
        self.assertLength(2, tuples)
 
3708
        self.assertIs(store1, tuples[0][0])
 
3709
        self.assertIs(store2, tuples[1][0])
 
3710
 
 
3711
 
3640
3712
class TestStackWithTransport(tests.TestCaseWithTransport):
3641
3713
 
3642
3714
    scenarios = [(key, {'get_stack': builder}) for key, builder
3832
3904
        super(TestStackExpandOptions, self).setUp()
3833
3905
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3834
3906
        self.registry = config.option_registry
3835
 
        self.conf = build_branch_stack(self)
 
3907
        store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
 
3908
        self.conf = config.Stack([store.get_sections], store)
3836
3909
 
3837
3910
    def assertExpansion(self, expected, string, env=None):
3838
3911
        self.assertEquals(expected, self.conf.expand_options(string, env))
3921
3994
baz=end
3922
3995
list={foo}
3923
3996
''')
3924
 
        self.registry.register(
3925
 
            config.ListOption('list'))
 
3997
        self.registry.register(config.ListOption('list'))
 
3998
        # Register an intermediate option as a list to ensure no conversion
 
3999
        # happen while expanding. Conversion should only occur for the original
 
4000
        # option ('list' here).
 
4001
        self.registry.register(config.ListOption('baz'))
3926
4002
        self.assertEquals(['start', 'middle', 'end'],
3927
4003
                           self.conf.get('list', expand=True))
3928
4004
 
4847
4923
        self.overrideEnv('BZR_EMAIL', None)
4848
4924
        self.overrideEnv('EMAIL', 'jelmer@samba.org')
4849
4925
        self.assertEquals('jelmer@debian.org', conf.get('email'))
 
4926
 
 
4927
 
 
4928
class MailClientOptionTests(tests.TestCase):
 
4929
 
 
4930
    def test_default(self):
 
4931
        conf = config.MemoryStack('')
 
4932
        client = conf.get('mail_client')
 
4933
        self.assertIs(client, mail_client.DefaultMail)
 
4934
 
 
4935
    def test_evolution(self):
 
4936
        conf = config.MemoryStack('mail_client=evolution')
 
4937
        client = conf.get('mail_client')
 
4938
        self.assertIs(client, mail_client.Evolution)
 
4939
 
 
4940
    def test_kmail(self):
 
4941
        conf = config.MemoryStack('mail_client=kmail')
 
4942
        client = conf.get('mail_client')
 
4943
        self.assertIs(client, mail_client.KMail)
 
4944
 
 
4945
    def test_mutt(self):
 
4946
        conf = config.MemoryStack('mail_client=mutt')
 
4947
        client = conf.get('mail_client')
 
4948
        self.assertIs(client, mail_client.Mutt)
 
4949
 
 
4950
    def test_thunderbird(self):
 
4951
        conf = config.MemoryStack('mail_client=thunderbird')
 
4952
        client = conf.get('mail_client')
 
4953
        self.assertIs(client, mail_client.Thunderbird)
 
4954
 
 
4955
    def test_explicit_default(self):
 
4956
        conf = config.MemoryStack('mail_client=default')
 
4957
        client = conf.get('mail_client')
 
4958
        self.assertIs(client, mail_client.DefaultMail)
 
4959
 
 
4960
    def test_editor(self):
 
4961
        conf = config.MemoryStack('mail_client=editor')
 
4962
        client = conf.get('mail_client')
 
4963
        self.assertIs(client, mail_client.Editor)
 
4964
 
 
4965
    def test_mapi(self):
 
4966
        conf = config.MemoryStack('mail_client=mapi')
 
4967
        client = conf.get('mail_client')
 
4968
        self.assertIs(client, mail_client.MAPIClient)
 
4969
 
 
4970
    def test_xdg_email(self):
 
4971
        conf = config.MemoryStack('mail_client=xdg-email')
 
4972
        client = conf.get('mail_client')
 
4973
        self.assertIs(client, mail_client.XDGEmail)
 
4974
 
 
4975
    def test_unknown(self):
 
4976
        conf = config.MemoryStack('mail_client=firebird')
 
4977
        self.assertRaises(errors.ConfigOptionValueError, conf.get,
 
4978
                'mail_client')