~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

(gz) Create working tree at specified revision when doing a local push
 (Martin Packman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for finding and reading the bzr config file[s]."""
18
 
 
 
18
# import system imports here
19
19
from cStringIO import StringIO
20
20
from textwrap import dedent
21
21
import os
22
22
import sys
23
23
import threading
24
24
 
 
25
 
25
26
from testtools import matchers
26
27
 
 
28
#import bzrlib specific imports here
27
29
from bzrlib import (
28
30
    branch,
29
31
    config,
558
560
    def setUp(self):
559
561
        super(TestConfigPath, self).setUp()
560
562
        self.overrideEnv('HOME', '/home/bogus')
561
 
        self.overrideEnv('XDG_CACHE_HOME', '')
 
563
        self.overrideEnv('XDG_CACHE_DIR', '')
562
564
        if sys.platform == 'win32':
563
565
            self.overrideEnv(
564
566
                'BZR_HOME', r'C:\Documents and Settings\bogus\Application Data')
595
597
    # subdirectory of $XDG_CONFIG_HOME
596
598
 
597
599
    def setUp(self):
598
 
        if sys.platform == 'win32':
 
600
        if sys.platform in ('darwin', 'win32'):
599
601
            raise tests.TestNotApplicable(
600
602
                'XDG config dir not used on this platform')
601
603
        super(TestXDGConfigDir, self).setUp()
628
630
class TestIniConfigBuilding(TestIniConfig):
629
631
 
630
632
    def test_contructs(self):
631
 
        config.IniBasedConfig()
 
633
        my_config = config.IniBasedConfig()
632
634
 
633
635
    def test_from_fp(self):
634
636
        my_config = config.IniBasedConfig.from_string(sample_config_text)
677
679
 
678
680
    def test_saved_with_content(self):
679
681
        content = 'foo = bar\n'
680
 
        config.IniBasedConfig.from_string(content, file_name='./test.conf',
681
 
                                          save=True)
 
682
        conf = config.IniBasedConfig.from_string(
 
683
            content, file_name='./test.conf', save=True)
682
684
        self.assertFileEqual(content, 'test.conf')
683
685
 
684
686
 
1046
1048
class TestGetConfig(tests.TestCase):
1047
1049
 
1048
1050
    def test_constructs(self):
1049
 
        config.GlobalConfig()
 
1051
        my_config = config.GlobalConfig()
1050
1052
 
1051
1053
    def test_calls_read_filenames(self):
1052
1054
        # replace the class that is constructed, to check its parameters
1064
1066
 
1065
1067
class TestBranchConfig(tests.TestCaseWithTransport):
1066
1068
 
1067
 
    def test_constructs_valid(self):
 
1069
    def test_constructs(self):
1068
1070
        branch = FakeBranch()
1069
1071
        my_config = config.BranchConfig(branch)
1070
 
        self.assertIsNot(None, my_config)
1071
 
 
1072
 
    def test_constructs_error(self):
1073
1072
        self.assertRaises(TypeError, config.BranchConfig)
1074
1073
 
1075
1074
    def test_get_location_config(self):
1107
1106
        conf = config.LocationConfig.from_string(
1108
1107
            '[%s]\nnickname = foobar' % (local_url,),
1109
1108
            local_url, save=True)
1110
 
        self.assertIsNot(None, conf)
1111
1109
        self.assertEqual('foobar', branch.nick)
1112
1110
 
1113
1111
    def test_config_local_path(self):
1116
1114
        self.assertEqual('branch', branch.nick)
1117
1115
 
1118
1116
        local_path = osutils.getcwd().encode('utf8')
1119
 
        config.LocationConfig.from_string(
 
1117
        conf = config.LocationConfig.from_string(
1120
1118
            '[%s/branch]\nnickname = barry' % (local_path,),
1121
1119
            'branch',  save=True)
1122
 
        # Now the branch will find its nick via the location config
1123
1120
        self.assertEqual('barry', branch.nick)
1124
1121
 
1125
1122
    def test_config_creates_local(self):
1373
1370
 
1374
1371
class TestLocationConfig(tests.TestCaseInTempDir, TestOptionsMixin):
1375
1372
 
1376
 
    def test_constructs_valid(self):
1377
 
        config.LocationConfig('http://example.com')
1378
 
 
1379
 
    def test_constructs_error(self):
 
1373
    def test_constructs(self):
 
1374
        my_config = config.LocationConfig('http://example.com')
1380
1375
        self.assertRaises(TypeError, config.LocationConfig)
1381
1376
 
1382
1377
    def test_branch_calls_read_filenames(self):
1668
1663
        if location_config is None:
1669
1664
            location_config = sample_branches_text
1670
1665
 
1671
 
        config.GlobalConfig.from_string(global_config, save=True)
1672
 
        config.LocationConfig.from_string(location_config, my_branch.base,
1673
 
                                          save=True)
 
1666
        my_global_config = config.GlobalConfig.from_string(global_config,
 
1667
                                                           save=True)
 
1668
        my_location_config = config.LocationConfig.from_string(
 
1669
            location_config, my_branch.base, save=True)
1674
1670
        my_config = config.BranchConfig(my_branch)
1675
1671
        self.my_config = my_config
1676
1672
        self.my_location_config = my_config._get_location_config()
1741
1737
                          location_config=None, branch_data_config=None):
1742
1738
        my_branch = FakeBranch(location)
1743
1739
        if global_config is not None:
1744
 
            config.GlobalConfig.from_string(global_config, save=True)
 
1740
            my_global_config = config.GlobalConfig.from_string(global_config,
 
1741
                                                               save=True)
1745
1742
        if location_config is not None:
1746
 
            config.LocationConfig.from_string(location_config, my_branch.base,
1747
 
                                              save=True)
 
1743
            my_location_config = config.LocationConfig.from_string(
 
1744
                location_config, my_branch.base, save=True)
1748
1745
        my_config = config.BranchConfig(my_branch)
1749
1746
        if branch_data_config is not None:
1750
1747
            my_config.branch.control_files.files['branch.conf'] = \
2224
2221
        self.assertSaveHook(remote_bzrdir._get_config())
2225
2222
 
2226
2223
 
2227
 
class TestOptionNames(tests.TestCase):
2228
 
 
2229
 
    def is_valid(self, name):
2230
 
        return config._option_ref_re.match('{%s}' % name) is not None
2231
 
 
2232
 
    def test_valid_names(self):
2233
 
        self.assertTrue(self.is_valid('foo'))
2234
 
        self.assertTrue(self.is_valid('foo.bar'))
2235
 
        self.assertTrue(self.is_valid('f1'))
2236
 
        self.assertTrue(self.is_valid('_'))
2237
 
        self.assertTrue(self.is_valid('__bar__'))
2238
 
        self.assertTrue(self.is_valid('a_'))
2239
 
        self.assertTrue(self.is_valid('a1'))
2240
 
 
2241
 
    def test_invalid_names(self):
2242
 
        self.assertFalse(self.is_valid(' foo'))
2243
 
        self.assertFalse(self.is_valid('foo '))
2244
 
        self.assertFalse(self.is_valid('1'))
2245
 
        self.assertFalse(self.is_valid('1,2'))
2246
 
        self.assertFalse(self.is_valid('foo$'))
2247
 
        self.assertFalse(self.is_valid('!foo'))
2248
 
        self.assertFalse(self.is_valid('foo.'))
2249
 
        self.assertFalse(self.is_valid('foo..bar'))
2250
 
        self.assertFalse(self.is_valid('{}'))
2251
 
        self.assertFalse(self.is_valid('{a}'))
2252
 
        self.assertFalse(self.is_valid('a\n'))
2253
 
 
2254
 
    def assertSingleGroup(self, reference):
2255
 
        # the regexp is used with split and as such should match the reference
2256
 
        # *only*, if more groups needs to be defined, (?:...) should be used.
2257
 
        m = config._option_ref_re.match('{a}')
2258
 
        self.assertLength(1, m.groups())
2259
 
 
2260
 
    def test_valid_references(self):
2261
 
        self.assertSingleGroup('{a}')
2262
 
        self.assertSingleGroup('{{a}}')
2263
 
 
2264
 
 
2265
2224
class TestOption(tests.TestCase):
2266
2225
 
2267
2226
    def test_default_value(self):
2489
2448
        self.registry.register(opt)
2490
2449
        self.assertEquals('A simple option', self.registry.get_help('foo'))
2491
2450
 
2492
 
    def test_dont_register_illegal_name(self):
2493
 
        self.assertRaises(errors.IllegalOptionName,
2494
 
                          self.registry.register, config.Option(' foo'))
2495
 
        self.assertRaises(errors.IllegalOptionName,
2496
 
                          self.registry.register, config.Option('bar,'))
2497
 
 
2498
2451
    lazy_option = config.Option('lazy_foo', help='Lazy help')
2499
2452
 
2500
2453
    def test_register_lazy(self):
2507
2460
                                    'TestOptionRegistry.lazy_option')
2508
2461
        self.assertEquals('Lazy help', self.registry.get_help('lazy_foo'))
2509
2462
 
2510
 
    def test_dont_lazy_register_illegal_name(self):
2511
 
        # This is where the root cause of http://pad.lv/1235099 is better
2512
 
        # understood: 'register_lazy' doc string mentions that key should match
2513
 
        # the option name which indirectly requires that the option name is a
2514
 
        # valid python identifier. We violate that rule here (using a key that
2515
 
        # doesn't match the option name) to test the option name checking.
2516
 
        self.assertRaises(errors.IllegalOptionName,
2517
 
                          self.registry.register_lazy, ' foo', self.__module__,
2518
 
                          'TestOptionRegistry.lazy_option')
2519
 
        self.assertRaises(errors.IllegalOptionName,
2520
 
                          self.registry.register_lazy, '1,2', self.__module__,
2521
 
                          'TestOptionRegistry.lazy_option')
2522
 
 
2523
2463
 
2524
2464
class TestRegisteredOptions(tests.TestCase):
2525
2465
    """All registered options should verify some constraints."""
3381
3321
 
3382
3322
    def test_build_doesnt_load_store(self):
3383
3323
        store = self.get_store(self)
3384
 
        self.matcher(store, '/bar')
 
3324
        matcher = self.matcher(store, '/bar')
3385
3325
        self.assertFalse(store.is_loaded())
3386
3326
 
3387
3327
 
3522
3462
 
3523
3463
    def test_url_vs_local_paths(self):
3524
3464
        # The matcher location is an url and the section names are local paths
3525
 
        self.assertSectionIDs(['/foo/bar', '/foo'],
3526
 
                              'file:///foo/bar/baz', '''\
 
3465
        sections = self.assertSectionIDs(['/foo/bar', '/foo'],
 
3466
                                         'file:///foo/bar/baz', '''\
3527
3467
[/foo]
3528
3468
[/foo/bar]
3529
3469
''')
3530
3470
 
3531
3471
    def test_local_path_vs_url(self):
3532
3472
        # The matcher location is a local path and the section names are urls
3533
 
        self.assertSectionIDs(['file:///foo/bar', 'file:///foo'],
3534
 
                              '/foo/bar/baz', '''\
 
3473
        sections = self.assertSectionIDs(['file:///foo/bar', 'file:///foo'],
 
3474
                                         '/foo/bar/baz', '''\
3535
3475
[file:///foo]
3536
3476
[file:///foo/bar]
3537
3477
''')
3754
3694
 
3755
3695
    def test_build_stack(self):
3756
3696
        # Just a smoke test to help debug builders
3757
 
        self.get_stack(self)
 
3697
        stack = self.get_stack(self)
3758
3698
 
3759
3699
 
3760
3700
class TestStackGet(TestStackWithTransport):
3981
3921
        self.assertRaises(errors.ExpandingUnknownOption,
3982
3922
                          self.conf.expand_options, '{foo}')
3983
3923
 
3984
 
    def test_illegal_def_is_ignored(self):
3985
 
        self.assertExpansion('{1,2}', '{1,2}')
3986
 
        self.assertExpansion('{ }', '{ }')
3987
 
        self.assertExpansion('${Foo,f}', '${Foo,f}')
3988
 
 
3989
3924
    def test_indirect_ref(self):
3990
3925
        self.conf.store._load_from_string('''
3991
3926
foo=xxx
4363
4298
        """
4364
4299
        sections = list(conf._get_sections(name))
4365
4300
        self.assertLength(len(expected), sections)
4366
 
        self.assertEqual(expected, [n for n, _, _ in sections])
 
4301
        self.assertEqual(expected, [name for name, _, _ in sections])
4367
4302
 
4368
4303
    def test_bazaar_default_section(self):
4369
4304
        self.assertSectionNames(['DEFAULT'], self.bazaar_config)
4413
4348
        self.assertSectionNames(['ALIASES'], self.bazaar_config, 'ALIASES')
4414
4349
 
4415
4350
 
4416
 
class TestSharedStores(tests.TestCaseInTempDir):
4417
 
 
4418
 
    def test_bazaar_conf_shared(self):
4419
 
        g1 = config.GlobalStack()
4420
 
        g2 = config.GlobalStack()
4421
 
        # The two stacks share the same store
4422
 
        self.assertIs(g1.store, g2.store)
4423
 
 
4424
 
 
4425
4351
class TestAuthenticationConfigFile(tests.TestCase):
4426
4352
    """Test the authentication.conf file matching"""
4427
4353
 
4916
4842
        self.assertEquals('secret', decoded)
4917
4843
 
4918
4844
 
4919
 
class TestBase64CredentialStore(tests.TestCase):
4920
 
 
4921
 
    def test_decode_password(self):
4922
 
        r = config.credential_store_registry
4923
 
        plain_text = r.get_credential_store('base64')
4924
 
        decoded = plain_text.decode_password(dict(password='c2VjcmV0'))
4925
 
        self.assertEquals('secret', decoded)
4926
 
 
4927
 
 
4928
4845
# FIXME: Once we have a way to declare authentication to all test servers, we
4929
4846
# can implement generic tests.
4930
4847
# test_user_password_in_url