15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for finding and reading the bzr config file[s]."""
18
# import system imports here
19
19
from cStringIO import StringIO
20
20
from textwrap import dedent
25
26
from testtools import matchers
28
#import bzrlib specific imports here
27
29
from bzrlib import (
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
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()
678
680
def test_saved_with_content(self):
679
681
content = 'foo = bar\n'
680
config.IniBasedConfig.from_string(content, file_name='./test.conf',
682
conf = config.IniBasedConfig.from_string(
683
content, file_name='./test.conf', save=True)
682
684
self.assertFileEqual(content, 'test.conf')
1046
1048
class TestGetConfig(tests.TestCase):
1048
1050
def test_constructs(self):
1049
config.GlobalConfig()
1051
my_config = config.GlobalConfig()
1051
1053
def test_calls_read_filenames(self):
1052
1054
# replace the class that is constructed, to check its parameters
1065
1067
class TestBranchConfig(tests.TestCaseWithTransport):
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)
1072
def test_constructs_error(self):
1073
1072
self.assertRaises(TypeError, config.BranchConfig)
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)
1113
1111
def test_config_local_path(self):
1116
1114
self.assertEqual('branch', branch.nick)
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)
1125
1122
def test_config_creates_local(self):
1374
1371
class TestLocationConfig(tests.TestCaseInTempDir, TestOptionsMixin):
1376
def test_constructs_valid(self):
1377
config.LocationConfig('http://example.com')
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)
1382
1377
def test_branch_calls_read_filenames(self):
1668
1663
if location_config is None:
1669
1664
location_config = sample_branches_text
1671
config.GlobalConfig.from_string(global_config, save=True)
1672
config.LocationConfig.from_string(location_config, my_branch.base,
1666
my_global_config = config.GlobalConfig.from_string(global_config,
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,
1745
1742
if location_config is not None:
1746
config.LocationConfig.from_string(location_config, my_branch.base,
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())
2227
class TestOptionNames(tests.TestCase):
2229
def is_valid(self, name):
2230
return config._option_ref_re.match('{%s}' % name) is not None
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'))
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'))
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())
2260
def test_valid_references(self):
2261
self.assertSingleGroup('{a}')
2262
self.assertSingleGroup('{{a}}')
2265
2224
class TestOption(tests.TestCase):
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'))
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,'))
2498
2451
lazy_option = config.Option('lazy_foo', help='Lazy help')
2500
2453
def test_register_lazy(self):
2507
2460
'TestOptionRegistry.lazy_option')
2508
2461
self.assertEquals('Lazy help', self.registry.get_help('lazy_foo'))
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')
2524
2464
class TestRegisteredOptions(tests.TestCase):
2525
2465
"""All registered options should verify some constraints."""
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())
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', '''\
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', '''\
3536
3476
[file:///foo/bar]
3981
3921
self.assertRaises(errors.ExpandingUnknownOption,
3982
3922
self.conf.expand_options, '{foo}')
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}')
3989
3924
def test_indirect_ref(self):
3990
3925
self.conf.store._load_from_string('''
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])
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')
4416
class TestSharedStores(tests.TestCaseInTempDir):
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)
4425
4351
class TestAuthenticationConfigFile(tests.TestCase):
4426
4352
"""Test the authentication.conf file matching"""
4916
4842
self.assertEquals('secret', decoded)
4919
class TestBase64CredentialStore(tests.TestCase):
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)
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