1312
1652
self.assertIs(None, bzrdir_config.get_default_stack_on())
1655
class TestOldConfigHooks(tests.TestCaseWithTransport):
1658
super(TestOldConfigHooks, self).setUp()
1659
create_configs_with_file_option(self)
1661
def assertGetHook(self, conf, name, value):
1666
config.OldConfigHooks.install_named_hook('get', hook, None)
1668
config.OldConfigHooks.uninstall_named_hook, 'get', None)
1669
self.assertLength(0, calls)
1670
actual_value = conf.get_user_option(name)
1671
self.assertEqual(value, actual_value)
1672
self.assertLength(1, calls)
1673
self.assertEqual((conf, name, value), calls[0])
1675
def test_get_hook_breezy(self):
1676
self.assertGetHook(self.breezy_config, 'file', 'breezy')
1678
def test_get_hook_locations(self):
1679
self.assertGetHook(self.locations_config, 'file', 'locations')
1681
def test_get_hook_branch(self):
1682
# Since locations masks branch, we define a different option
1683
self.branch_config.set_user_option('file2', 'branch')
1684
self.assertGetHook(self.branch_config, 'file2', 'branch')
1686
def assertSetHook(self, conf, name, value):
1691
config.OldConfigHooks.install_named_hook('set', hook, None)
1693
config.OldConfigHooks.uninstall_named_hook, 'set', None)
1694
self.assertLength(0, calls)
1695
conf.set_user_option(name, value)
1696
self.assertLength(1, calls)
1697
# We can't assert the conf object below as different configs use
1698
# different means to implement set_user_option and we care only about
1700
self.assertEqual((name, value), calls[0][1:])
1702
def test_set_hook_breezy(self):
1703
self.assertSetHook(self.breezy_config, 'foo', 'breezy')
1705
def test_set_hook_locations(self):
1706
self.assertSetHook(self.locations_config, 'foo', 'locations')
1708
def test_set_hook_branch(self):
1709
self.assertSetHook(self.branch_config, 'foo', 'branch')
1711
def assertRemoveHook(self, conf, name, section_name=None):
1716
config.OldConfigHooks.install_named_hook('remove', hook, None)
1718
config.OldConfigHooks.uninstall_named_hook, 'remove', None)
1719
self.assertLength(0, calls)
1720
conf.remove_user_option(name, section_name)
1721
self.assertLength(1, calls)
1722
# We can't assert the conf object below as different configs use
1723
# different means to implement remove_user_option and we care only about
1725
self.assertEqual((name,), calls[0][1:])
1727
def test_remove_hook_breezy(self):
1728
self.assertRemoveHook(self.breezy_config, 'file')
1730
def test_remove_hook_locations(self):
1731
self.assertRemoveHook(self.locations_config, 'file',
1732
self.locations_config.location)
1734
def test_remove_hook_branch(self):
1735
self.assertRemoveHook(self.branch_config, 'file')
1737
def assertLoadHook(self, name, conf_class, *conf_args):
1742
config.OldConfigHooks.install_named_hook('load', hook, None)
1744
config.OldConfigHooks.uninstall_named_hook, 'load', None)
1745
self.assertLength(0, calls)
1747
conf = conf_class(*conf_args)
1748
# Access an option to trigger a load
1749
conf.get_user_option(name)
1750
self.assertLength(1, calls)
1751
# Since we can't assert about conf, we just use the number of calls ;-/
1753
def test_load_hook_breezy(self):
1754
self.assertLoadHook('file', config.GlobalConfig)
1756
def test_load_hook_locations(self):
1757
self.assertLoadHook('file', config.LocationConfig, self.tree.basedir)
1759
def test_load_hook_branch(self):
1760
self.assertLoadHook('file', config.BranchConfig, self.tree.branch)
1762
def assertSaveHook(self, conf):
1767
config.OldConfigHooks.install_named_hook('save', hook, None)
1769
config.OldConfigHooks.uninstall_named_hook, 'save', None)
1770
self.assertLength(0, calls)
1771
# Setting an option triggers a save
1772
conf.set_user_option('foo', 'bar')
1773
self.assertLength(1, calls)
1774
# Since we can't assert about conf, we just use the number of calls ;-/
1776
def test_save_hook_breezy(self):
1777
self.assertSaveHook(self.breezy_config)
1779
def test_save_hook_locations(self):
1780
self.assertSaveHook(self.locations_config)
1782
def test_save_hook_branch(self):
1783
self.assertSaveHook(self.branch_config)
1786
class TestOldConfigHooksForRemote(tests.TestCaseWithTransport):
1787
"""Tests config hooks for remote configs.
1789
No tests for the remove hook as this is not implemented there.
1793
super(TestOldConfigHooksForRemote, self).setUp()
1794
self.transport_server = test_server.SmartTCPServer_for_testing
1795
create_configs_with_file_option(self)
1797
def assertGetHook(self, conf, name, value):
1802
config.OldConfigHooks.install_named_hook('get', hook, None)
1804
config.OldConfigHooks.uninstall_named_hook, 'get', None)
1805
self.assertLength(0, calls)
1806
actual_value = conf.get_option(name)
1807
self.assertEqual(value, actual_value)
1808
self.assertLength(1, calls)
1809
self.assertEqual((conf, name, value), calls[0])
1811
def test_get_hook_remote_branch(self):
1812
remote_branch = branch.Branch.open(self.get_url('tree'))
1813
self.assertGetHook(remote_branch._get_config(), 'file', 'branch')
1815
def test_get_hook_remote_bzrdir(self):
1816
remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
1817
conf = remote_bzrdir._get_config()
1818
conf.set_option('remotedir', 'file')
1819
self.assertGetHook(conf, 'file', 'remotedir')
1821
def assertSetHook(self, conf, name, value):
1826
config.OldConfigHooks.install_named_hook('set', hook, None)
1828
config.OldConfigHooks.uninstall_named_hook, 'set', None)
1829
self.assertLength(0, calls)
1830
conf.set_option(value, name)
1831
self.assertLength(1, calls)
1832
# We can't assert the conf object below as different configs use
1833
# different means to implement set_user_option and we care only about
1835
self.assertEqual((name, value), calls[0][1:])
1837
def test_set_hook_remote_branch(self):
1838
remote_branch = branch.Branch.open(self.get_url('tree'))
1839
self.addCleanup(remote_branch.lock_write().unlock)
1840
self.assertSetHook(remote_branch._get_config(), 'file', 'remote')
1842
def test_set_hook_remote_bzrdir(self):
1843
remote_branch = branch.Branch.open(self.get_url('tree'))
1844
self.addCleanup(remote_branch.lock_write().unlock)
1845
remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
1846
self.assertSetHook(remote_bzrdir._get_config(), 'file', 'remotedir')
1848
def assertLoadHook(self, expected_nb_calls, name, conf_class, *conf_args):
1853
config.OldConfigHooks.install_named_hook('load', hook, None)
1855
config.OldConfigHooks.uninstall_named_hook, 'load', None)
1856
self.assertLength(0, calls)
1858
conf = conf_class(*conf_args)
1859
# Access an option to trigger a load
1860
conf.get_option(name)
1861
self.assertLength(expected_nb_calls, calls)
1862
# Since we can't assert about conf, we just use the number of calls ;-/
1864
def test_load_hook_remote_branch(self):
1865
remote_branch = branch.Branch.open(self.get_url('tree'))
1866
self.assertLoadHook(
1867
1, 'file', remote.RemoteBranchConfig, remote_branch)
1869
def test_load_hook_remote_bzrdir(self):
1870
remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
1871
# The config file doesn't exist, set an option to force its creation
1872
conf = remote_bzrdir._get_config()
1873
conf.set_option('remotedir', 'file')
1874
# We get one call for the server and one call for the client, this is
1875
# caused by the differences in implementations betwen
1876
# SmartServerBzrDirRequestConfigFile (in smart/bzrdir.py) and
1877
# SmartServerBranchGetConfigFile (in smart/branch.py)
1878
self.assertLoadHook(
1879
2, 'file', remote.RemoteBzrDirConfig, remote_bzrdir)
1881
def assertSaveHook(self, conf):
1886
config.OldConfigHooks.install_named_hook('save', hook, None)
1888
config.OldConfigHooks.uninstall_named_hook, 'save', None)
1889
self.assertLength(0, calls)
1890
# Setting an option triggers a save
1891
conf.set_option('foo', 'bar')
1892
self.assertLength(1, calls)
1893
# Since we can't assert about conf, we just use the number of calls ;-/
1895
def test_save_hook_remote_branch(self):
1896
remote_branch = branch.Branch.open(self.get_url('tree'))
1897
self.addCleanup(remote_branch.lock_write().unlock)
1898
self.assertSaveHook(remote_branch._get_config())
1900
def test_save_hook_remote_bzrdir(self):
1901
remote_branch = branch.Branch.open(self.get_url('tree'))
1902
self.addCleanup(remote_branch.lock_write().unlock)
1903
remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
1904
self.assertSaveHook(remote_bzrdir._get_config())
1907
class TestOptionNames(tests.TestCase):
1909
def is_valid(self, name):
1910
return config._option_ref_re.match('{%s}' % name) is not None
1912
def test_valid_names(self):
1913
self.assertTrue(self.is_valid('foo'))
1914
self.assertTrue(self.is_valid('foo.bar'))
1915
self.assertTrue(self.is_valid('f1'))
1916
self.assertTrue(self.is_valid('_'))
1917
self.assertTrue(self.is_valid('__bar__'))
1918
self.assertTrue(self.is_valid('a_'))
1919
self.assertTrue(self.is_valid('a1'))
1920
# Don't break bzr-svn for no good reason
1921
self.assertTrue(self.is_valid('guessed-layout'))
1923
def test_invalid_names(self):
1924
self.assertFalse(self.is_valid(' foo'))
1925
self.assertFalse(self.is_valid('foo '))
1926
self.assertFalse(self.is_valid('1'))
1927
self.assertFalse(self.is_valid('1,2'))
1928
self.assertFalse(self.is_valid('foo$'))
1929
self.assertFalse(self.is_valid('!foo'))
1930
self.assertFalse(self.is_valid('foo.'))
1931
self.assertFalse(self.is_valid('foo..bar'))
1932
self.assertFalse(self.is_valid('{}'))
1933
self.assertFalse(self.is_valid('{a}'))
1934
self.assertFalse(self.is_valid('a\n'))
1935
self.assertFalse(self.is_valid('-'))
1936
self.assertFalse(self.is_valid('-a'))
1937
self.assertFalse(self.is_valid('a-'))
1938
self.assertFalse(self.is_valid('a--a'))
1940
def assertSingleGroup(self, reference):
1941
# the regexp is used with split and as such should match the reference
1942
# *only*, if more groups needs to be defined, (?:...) should be used.
1943
m = config._option_ref_re.match('{a}')
1944
self.assertLength(1, m.groups())
1946
def test_valid_references(self):
1947
self.assertSingleGroup('{a}')
1948
self.assertSingleGroup('{{a}}')
1951
class TestOption(tests.TestCase):
1953
def test_default_value(self):
1954
opt = config.Option('foo', default='bar')
1955
self.assertEqual('bar', opt.get_default())
1957
def test_callable_default_value(self):
1958
def bar_as_unicode():
1960
opt = config.Option('foo', default=bar_as_unicode)
1961
self.assertEqual('bar', opt.get_default())
1963
def test_default_value_from_env(self):
1964
opt = config.Option('foo', default='bar', default_from_env=['FOO'])
1965
self.overrideEnv('FOO', 'quux')
1966
# Env variable provides a default taking over the option one
1967
self.assertEqual('quux', opt.get_default())
1969
def test_first_default_value_from_env_wins(self):
1970
opt = config.Option('foo', default='bar',
1971
default_from_env=['NO_VALUE', 'FOO', 'BAZ'])
1972
self.overrideEnv('FOO', 'foo')
1973
self.overrideEnv('BAZ', 'baz')
1974
# The first env var set wins
1975
self.assertEqual('foo', opt.get_default())
1977
def test_not_supported_list_default_value(self):
1978
self.assertRaises(AssertionError, config.Option, 'foo', default=[1])
1980
def test_not_supported_object_default_value(self):
1981
self.assertRaises(AssertionError, config.Option, 'foo',
1984
def test_not_supported_callable_default_value_not_unicode(self):
1985
def bar_not_unicode():
1987
opt = config.Option('foo', default=bar_not_unicode)
1988
self.assertRaises(AssertionError, opt.get_default)
1990
def test_get_help_topic(self):
1991
opt = config.Option('foo')
1992
self.assertEqual('foo', opt.get_help_topic())
1995
class TestOptionConverter(tests.TestCase):
1997
def assertConverted(self, expected, opt, value):
1998
self.assertEqual(expected, opt.convert_from_unicode(None, value))
2000
def assertCallsWarning(self, opt, value):
2004
warnings.append(args[0] % args[1:])
2005
self.overrideAttr(trace, 'warning', warning)
2006
self.assertEqual(None, opt.convert_from_unicode(None, value))
2007
self.assertLength(1, warnings)
2009
'Value "%s" is not valid for "%s"' % (value, opt.name),
2012
def assertCallsError(self, opt, value):
2013
self.assertRaises(config.ConfigOptionValueError,
2014
opt.convert_from_unicode, None, value)
2016
def assertConvertInvalid(self, opt, invalid_value):
2018
self.assertEqual(None, opt.convert_from_unicode(None, invalid_value))
2019
opt.invalid = 'warning'
2020
self.assertCallsWarning(opt, invalid_value)
2021
opt.invalid = 'error'
2022
self.assertCallsError(opt, invalid_value)
2025
class TestOptionWithBooleanConverter(TestOptionConverter):
2027
def get_option(self):
2028
return config.Option('foo', help='A boolean.',
2029
from_unicode=config.bool_from_store)
2031
def test_convert_invalid(self):
2032
opt = self.get_option()
2033
# A string that is not recognized as a boolean
2034
self.assertConvertInvalid(opt, u'invalid-boolean')
2035
# A list of strings is never recognized as a boolean
2036
self.assertConvertInvalid(opt, [u'not', u'a', u'boolean'])
2038
def test_convert_valid(self):
2039
opt = self.get_option()
2040
self.assertConverted(True, opt, u'True')
2041
self.assertConverted(True, opt, u'1')
2042
self.assertConverted(False, opt, u'False')
2045
class TestOptionWithIntegerConverter(TestOptionConverter):
2047
def get_option(self):
2048
return config.Option('foo', help='An integer.',
2049
from_unicode=config.int_from_store)
2051
def test_convert_invalid(self):
2052
opt = self.get_option()
2053
# A string that is not recognized as an integer
2054
self.assertConvertInvalid(opt, u'forty-two')
2055
# A list of strings is never recognized as an integer
2056
self.assertConvertInvalid(opt, [u'a', u'list'])
2058
def test_convert_valid(self):
2059
opt = self.get_option()
2060
self.assertConverted(16, opt, u'16')
2063
class TestOptionWithSIUnitConverter(TestOptionConverter):
2065
def get_option(self):
2066
return config.Option('foo', help='An integer in SI units.',
2067
from_unicode=config.int_SI_from_store)
2069
def test_convert_invalid(self):
2070
opt = self.get_option()
2071
self.assertConvertInvalid(opt, u'not-a-unit')
2072
self.assertConvertInvalid(opt, u'Gb') # Forgot the value
2073
self.assertConvertInvalid(opt, u'1b') # Forgot the unit
2074
self.assertConvertInvalid(opt, u'1GG')
2075
self.assertConvertInvalid(opt, u'1Mbb')
2076
self.assertConvertInvalid(opt, u'1MM')
2078
def test_convert_valid(self):
2079
opt = self.get_option()
2080
self.assertConverted(int(5e3), opt, u'5kb')
2081
self.assertConverted(int(5e6), opt, u'5M')
2082
self.assertConverted(int(5e6), opt, u'5MB')
2083
self.assertConverted(int(5e9), opt, u'5g')
2084
self.assertConverted(int(5e9), opt, u'5gB')
2085
self.assertConverted(100, opt, u'100')
2088
class TestListOption(TestOptionConverter):
2090
def get_option(self):
2091
return config.ListOption('foo', help='A list.')
2093
def test_convert_invalid(self):
2094
opt = self.get_option()
2095
# We don't even try to convert a list into a list, we only expect
2097
self.assertConvertInvalid(opt, [1])
2098
# No string is invalid as all forms can be converted to a list
2100
def test_convert_valid(self):
2101
opt = self.get_option()
2102
# An empty string is an empty list
2103
self.assertConverted([], opt, '') # Using a bare str() just in case
2104
self.assertConverted([], opt, u'')
2106
self.assertConverted([u'True'], opt, u'True')
2108
self.assertConverted([u'42'], opt, u'42')
2110
self.assertConverted([u'bar'], opt, u'bar')
2113
class TestRegistryOption(TestOptionConverter):
2115
def get_option(self, registry):
2116
return config.RegistryOption('foo', registry,
2117
help='A registry option.')
2119
def test_convert_invalid(self):
2120
registry = _mod_registry.Registry()
2121
opt = self.get_option(registry)
2122
self.assertConvertInvalid(opt, [1])
2123
self.assertConvertInvalid(opt, u"notregistered")
2125
def test_convert_valid(self):
2126
registry = _mod_registry.Registry()
2127
registry.register("someval", 1234)
2128
opt = self.get_option(registry)
2129
# Using a bare str() just in case
2130
self.assertConverted(1234, opt, "someval")
2131
self.assertConverted(1234, opt, u'someval')
2132
self.assertConverted(None, opt, None)
2134
def test_help(self):
2135
registry = _mod_registry.Registry()
2136
registry.register("someval", 1234, help="some option")
2137
registry.register("dunno", 1234, help="some other option")
2138
opt = self.get_option(registry)
2140
'A registry option.\n'
2142
'The following values are supported:\n'
2143
' dunno - some other option\n'
2144
' someval - some option\n',
2147
def test_get_help_text(self):
2148
registry = _mod_registry.Registry()
2149
registry.register("someval", 1234, help="some option")
2150
registry.register("dunno", 1234, help="some other option")
2151
opt = self.get_option(registry)
2153
'A registry option.\n'
2155
'The following values are supported:\n'
2156
' dunno - some other option\n'
2157
' someval - some option\n',
2158
opt.get_help_text())
2161
class TestOptionRegistry(tests.TestCase):
2164
super(TestOptionRegistry, self).setUp()
2165
# Always start with an empty registry
2166
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
2167
self.registry = config.option_registry
2169
def test_register(self):
2170
opt = config.Option('foo')
2171
self.registry.register(opt)
2172
self.assertIs(opt, self.registry.get('foo'))
2174
def test_registered_help(self):
2175
opt = config.Option('foo', help='A simple option')
2176
self.registry.register(opt)
2177
self.assertEqual('A simple option', self.registry.get_help('foo'))
2179
def test_dont_register_illegal_name(self):
2180
self.assertRaises(config.IllegalOptionName,
2181
self.registry.register, config.Option(' foo'))
2182
self.assertRaises(config.IllegalOptionName,
2183
self.registry.register, config.Option('bar,'))
2185
lazy_option = config.Option('lazy_foo', help='Lazy help')
2187
def test_register_lazy(self):
2188
self.registry.register_lazy('lazy_foo', self.__module__,
2189
'TestOptionRegistry.lazy_option')
2190
self.assertIs(self.lazy_option, self.registry.get('lazy_foo'))
2192
def test_registered_lazy_help(self):
2193
self.registry.register_lazy('lazy_foo', self.__module__,
2194
'TestOptionRegistry.lazy_option')
2195
self.assertEqual('Lazy help', self.registry.get_help('lazy_foo'))
2197
def test_dont_lazy_register_illegal_name(self):
2198
# This is where the root cause of http://pad.lv/1235099 is better
2199
# understood: 'register_lazy' doc string mentions that key should match
2200
# the option name which indirectly requires that the option name is a
2201
# valid python identifier. We violate that rule here (using a key that
2202
# doesn't match the option name) to test the option name checking.
2203
self.assertRaises(config.IllegalOptionName,
2204
self.registry.register_lazy, ' foo', self.__module__,
2205
'TestOptionRegistry.lazy_option')
2206
self.assertRaises(config.IllegalOptionName,
2207
self.registry.register_lazy, '1,2', self.__module__,
2208
'TestOptionRegistry.lazy_option')
2211
class TestRegisteredOptions(tests.TestCase):
2212
"""All registered options should verify some constraints."""
2214
scenarios = [(key, {'option_name': key, 'option': option}) for key, option
2215
in config.option_registry.iteritems()]
2218
super(TestRegisteredOptions, self).setUp()
2219
self.registry = config.option_registry
2221
def test_proper_name(self):
2222
# An option should be registered under its own name, this can't be
2223
# checked at registration time for the lazy ones.
2224
self.assertEqual(self.option_name, self.option.name)
2226
def test_help_is_set(self):
2227
option_help = self.registry.get_help(self.option_name)
2228
# Come on, think about the user, he really wants to know what the
2230
self.assertIsNot(None, option_help)
2231
self.assertNotEqual('', option_help)
2234
class TestSection(tests.TestCase):
2236
# FIXME: Parametrize so that all sections produced by Stores run these
2237
# tests -- vila 2011-04-01
2239
def test_get_a_value(self):
2240
a_dict = dict(foo='bar')
2241
section = config.Section('myID', a_dict)
2242
self.assertEqual('bar', section.get('foo'))
2244
def test_get_unknown_option(self):
2246
section = config.Section(None, a_dict)
2247
self.assertEqual('out of thin air',
2248
section.get('foo', 'out of thin air'))
2250
def test_options_is_shared(self):
2252
section = config.Section(None, a_dict)
2253
self.assertIs(a_dict, section.options)
2256
class TestMutableSection(tests.TestCase):
2258
scenarios = [('mutable',
2260
lambda opts: config.MutableSection('myID', opts)},),
2264
a_dict = dict(foo='bar')
2265
section = self.get_section(a_dict)
2266
section.set('foo', 'new_value')
2267
self.assertEqual('new_value', section.get('foo'))
2268
# The change appears in the shared section
2269
self.assertEqual('new_value', a_dict.get('foo'))
2270
# We keep track of the change
2271
self.assertTrue('foo' in section.orig)
2272
self.assertEqual('bar', section.orig.get('foo'))
2274
def test_set_preserve_original_once(self):
2275
a_dict = dict(foo='bar')
2276
section = self.get_section(a_dict)
2277
section.set('foo', 'first_value')
2278
section.set('foo', 'second_value')
2279
# We keep track of the original value
2280
self.assertTrue('foo' in section.orig)
2281
self.assertEqual('bar', section.orig.get('foo'))
2283
def test_remove(self):
2284
a_dict = dict(foo='bar')
2285
section = self.get_section(a_dict)
2286
section.remove('foo')
2287
# We get None for unknown options via the default value
2288
self.assertEqual(None, section.get('foo'))
2289
# Or we just get the default value
2290
self.assertEqual('unknown', section.get('foo', 'unknown'))
2291
self.assertFalse('foo' in section.options)
2292
# We keep track of the deletion
2293
self.assertTrue('foo' in section.orig)
2294
self.assertEqual('bar', section.orig.get('foo'))
2296
def test_remove_new_option(self):
2298
section = self.get_section(a_dict)
2299
section.set('foo', 'bar')
2300
section.remove('foo')
2301
self.assertFalse('foo' in section.options)
2302
# The option didn't exist initially so it we need to keep track of it
2303
# with a special value
2304
self.assertTrue('foo' in section.orig)
2305
self.assertEqual(config._NewlyCreatedOption, section.orig['foo'])
2308
class TestCommandLineStore(tests.TestCase):
2311
super(TestCommandLineStore, self).setUp()
2312
self.store = config.CommandLineStore()
2313
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
2315
def get_section(self):
2316
"""Get the unique section for the command line overrides."""
2317
sections = list(self.store.get_sections())
2318
self.assertLength(1, sections)
2319
store, section = sections[0]
2320
self.assertEqual(self.store, store)
2323
def test_no_override(self):
2324
self.store._from_cmdline([])
2325
section = self.get_section()
2326
self.assertLength(0, list(section.iter_option_names()))
2328
def test_simple_override(self):
2329
self.store._from_cmdline(['a=b'])
2330
section = self.get_section()
2331
self.assertEqual('b', section.get('a'))
2333
def test_list_override(self):
2334
opt = config.ListOption('l')
2335
config.option_registry.register(opt)
2336
self.store._from_cmdline(['l=1,2,3'])
2337
val = self.get_section().get('l')
2338
self.assertEqual('1,2,3', val)
2339
# Reminder: lists should be registered as such explicitely, otherwise
2340
# the conversion needs to be done afterwards.
2341
self.assertEqual(['1', '2', '3'],
2342
opt.convert_from_unicode(self.store, val))
2344
def test_multiple_overrides(self):
2345
self.store._from_cmdline(['a=b', 'x=y'])
2346
section = self.get_section()
2347
self.assertEqual('b', section.get('a'))
2348
self.assertEqual('y', section.get('x'))
2350
def test_wrong_syntax(self):
2351
self.assertRaises(errors.CommandError,
2352
self.store._from_cmdline, ['a=b', 'c'])
2355
class TestStoreMinimalAPI(tests.TestCaseWithTransport):
2357
scenarios = [(key, {'get_store': builder}) for key, builder
2358
in config.test_store_builder_registry.iteritems()] + [
2359
('cmdline', {'get_store': lambda test: config.CommandLineStore()})]
2362
store = self.get_store(self)
2363
if isinstance(store, config.TransportIniFileStore):
2364
raise tests.TestNotApplicable(
2365
"%s is not a concrete Store implementation"
2366
" so it doesn't need an id" % (store.__class__.__name__,))
2367
self.assertIsNot(None, store.id)
2370
class TestStore(tests.TestCaseWithTransport):
2372
def assertSectionContent(self, expected, store_and_section):
2373
"""Assert that some options have the proper values in a section."""
2374
_, section = store_and_section
2375
expected_name, expected_options = expected
2376
self.assertEqual(expected_name, section.id)
2379
dict([(k, section.get(k)) for k in expected_options.keys()]))
2382
class TestReadonlyStore(TestStore):
2384
scenarios = [(key, {'get_store': builder}) for key, builder
2385
in config.test_store_builder_registry.iteritems()]
2387
def test_building_delays_load(self):
2388
store = self.get_store(self)
2389
self.assertEqual(False, store.is_loaded())
2390
store._load_from_string(b'')
2391
self.assertEqual(True, store.is_loaded())
2393
def test_get_no_sections_for_empty(self):
2394
store = self.get_store(self)
2395
store._load_from_string(b'')
2396
self.assertEqual([], list(store.get_sections()))
2398
def test_get_default_section(self):
2399
store = self.get_store(self)
2400
store._load_from_string(b'foo=bar')
2401
sections = list(store.get_sections())
2402
self.assertLength(1, sections)
2403
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2405
def test_get_named_section(self):
2406
store = self.get_store(self)
2407
store._load_from_string(b'[baz]\nfoo=bar')
2408
sections = list(store.get_sections())
2409
self.assertLength(1, sections)
2410
self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
2412
def test_load_from_string_fails_for_non_empty_store(self):
2413
store = self.get_store(self)
2414
store._load_from_string(b'foo=bar')
2415
self.assertRaises(AssertionError, store._load_from_string, b'bar=baz')
2418
class TestStoreQuoting(TestStore):
2420
scenarios = [(key, {'get_store': builder}) for key, builder
2421
in config.test_store_builder_registry.iteritems()]
2424
super(TestStoreQuoting, self).setUp()
2425
self.store = self.get_store(self)
2426
# We need a loaded store but any content will do
2427
self.store._load_from_string(b'')
2429
def assertIdempotent(self, s):
2430
"""Assert that quoting an unquoted string is a no-op and vice-versa.
2432
What matters here is that option values, as they appear in a store, can
2433
be safely round-tripped out of the store and back.
2435
:param s: A string, quoted if required.
2437
self.assertEqual(s, self.store.quote(self.store.unquote(s)))
2438
self.assertEqual(s, self.store.unquote(self.store.quote(s)))
2440
def test_empty_string(self):
2441
if isinstance(self.store, config.IniFileStore):
2442
# configobj._quote doesn't handle empty values
2443
self.assertRaises(AssertionError,
2444
self.assertIdempotent, '')
2446
self.assertIdempotent('')
2447
# But quoted empty strings are ok
2448
self.assertIdempotent('""')
2450
def test_embedded_spaces(self):
2451
self.assertIdempotent('" a b c "')
2453
def test_embedded_commas(self):
2454
self.assertIdempotent('" a , b c "')
2456
def test_simple_comma(self):
2457
if isinstance(self.store, config.IniFileStore):
2458
# configobj requires that lists are special-cased
2459
self.assertRaises(AssertionError,
2460
self.assertIdempotent, ',')
2462
self.assertIdempotent(',')
2463
# When a single comma is required, quoting is also required
2464
self.assertIdempotent('","')
2466
def test_list(self):
2467
if isinstance(self.store, config.IniFileStore):
2468
# configobj requires that lists are special-cased
2469
self.assertRaises(AssertionError,
2470
self.assertIdempotent, 'a,b')
2472
self.assertIdempotent('a,b')
2475
class TestDictFromStore(tests.TestCase):
2477
def test_unquote_not_string(self):
2478
conf = config.MemoryStack(b'x=2\n[a_section]\na=1\n')
2479
value = conf.get('a_section')
2480
# Urgh, despite 'conf' asking for the no-name section, we get the
2481
# content of another section as a dict o_O
2482
self.assertEqual({'a': '1'}, value)
2483
unquoted = conf.store.unquote(value)
2484
# Which cannot be unquoted but shouldn't crash either (the use cases
2485
# are getting the value or displaying it. In the later case, '%s' will
2487
self.assertEqual({'a': '1'}, unquoted)
2488
self.assertIn('%s' % (unquoted,), ("{u'a': u'1'}", "{'a': '1'}"))
2491
class TestIniFileStoreContent(tests.TestCaseWithTransport):
2492
"""Simulate loading a config store with content of various encodings.
2494
All files produced by bzr are in utf8 content.
2496
Users may modify them manually and end up with a file that can't be
2497
loaded. We need to issue proper error messages in this case.
2500
invalid_utf8_char = b'\xff'
2502
def test_load_utf8(self):
2503
"""Ensure we can load an utf8-encoded file."""
2504
t = self.get_transport()
2505
# From http://pad.lv/799212
2506
unicode_user = u'b\N{Euro Sign}ar'
2507
unicode_content = u'user=%s' % (unicode_user,)
2508
utf8_content = unicode_content.encode('utf8')
2509
# Store the raw content in the config file
2510
t.put_bytes('foo.conf', utf8_content)
2511
store = config.TransportIniFileStore(t, 'foo.conf')
2513
stack = config.Stack([store.get_sections], store)
2514
self.assertEqual(unicode_user, stack.get('user'))
2516
def test_load_non_ascii(self):
2517
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
2518
t = self.get_transport()
2519
t.put_bytes('foo.conf', b'user=foo\n#%s\n' % (self.invalid_utf8_char,))
2520
store = config.TransportIniFileStore(t, 'foo.conf')
2521
self.assertRaises(config.ConfigContentError, store.load)
2523
def test_load_erroneous_content(self):
2524
"""Ensure we display a proper error on content that can't be parsed."""
2525
t = self.get_transport()
2526
t.put_bytes('foo.conf', b'[open_section\n')
2527
store = config.TransportIniFileStore(t, 'foo.conf')
2528
self.assertRaises(config.ParseConfigError, store.load)
2530
def test_load_permission_denied(self):
2531
"""Ensure we get warned when trying to load an inaccessible file."""
2535
warnings.append(args[0] % args[1:])
2536
self.overrideAttr(trace, 'warning', warning)
2538
t = self.get_transport()
2540
def get_bytes(relpath):
2541
raise errors.PermissionDenied(relpath, "")
2542
t.get_bytes = get_bytes
2543
store = config.TransportIniFileStore(t, 'foo.conf')
2544
self.assertRaises(errors.PermissionDenied, store.load)
2547
[u'Permission denied while trying to load configuration store %s.'
2548
% store.external_url()])
2551
class TestIniConfigContent(tests.TestCaseWithTransport):
2552
"""Simulate loading a IniBasedConfig with content of various encodings.
2554
All files produced by bzr are in utf8 content.
2556
Users may modify them manually and end up with a file that can't be
2557
loaded. We need to issue proper error messages in this case.
2560
invalid_utf8_char = b'\xff'
2562
def test_load_utf8(self):
2563
"""Ensure we can load an utf8-encoded file."""
2564
# From http://pad.lv/799212
2565
unicode_user = u'b\N{Euro Sign}ar'
2566
unicode_content = u'user=%s' % (unicode_user,)
2567
utf8_content = unicode_content.encode('utf8')
2568
# Store the raw content in the config file
2569
with open('foo.conf', 'wb') as f:
2570
f.write(utf8_content)
2571
conf = config.IniBasedConfig(file_name='foo.conf')
2572
self.assertEqual(unicode_user, conf.get_user_option('user'))
2574
def test_load_badly_encoded_content(self):
2575
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
2576
with open('foo.conf', 'wb') as f:
2577
f.write(b'user=foo\n#%s\n' % (self.invalid_utf8_char,))
2578
conf = config.IniBasedConfig(file_name='foo.conf')
2579
self.assertRaises(config.ConfigContentError, conf._get_parser)
2581
def test_load_erroneous_content(self):
2582
"""Ensure we display a proper error on content that can't be parsed."""
2583
with open('foo.conf', 'wb') as f:
2584
f.write(b'[open_section\n')
2585
conf = config.IniBasedConfig(file_name='foo.conf')
2586
self.assertRaises(config.ParseConfigError, conf._get_parser)
2589
class TestMutableStore(TestStore):
2591
scenarios = [(key, {'store_id': key, 'get_store': builder}) for key, builder
2592
in config.test_store_builder_registry.iteritems()]
2595
super(TestMutableStore, self).setUp()
2596
self.transport = self.get_transport()
2598
def has_store(self, store):
2599
store_basename = urlutils.relative_url(self.transport.external_url(),
2600
store.external_url())
2601
return self.transport.has(store_basename)
2603
def test_save_empty_creates_no_file(self):
2604
# FIXME: There should be a better way than relying on the test
2605
# parametrization to identify branch.conf -- vila 2011-0526
2606
if self.store_id in ('branch', 'remote_branch'):
2607
raise tests.TestNotApplicable(
2608
'branch.conf is *always* created when a branch is initialized')
2609
store = self.get_store(self)
2611
self.assertEqual(False, self.has_store(store))
2613
def test_mutable_section_shared(self):
2614
store = self.get_store(self)
2615
store._load_from_string(b'foo=bar\n')
2616
# FIXME: There should be a better way than relying on the test
2617
# parametrization to identify branch.conf -- vila 2011-0526
2618
if self.store_id in ('branch', 'remote_branch'):
2619
# branch stores requires write locked branches
2620
self.addCleanup(store.branch.lock_write().unlock)
2621
section1 = store.get_mutable_section(None)
2622
section2 = store.get_mutable_section(None)
2623
# If we get different sections, different callers won't share the
2625
self.assertIs(section1, section2)
2627
def test_save_emptied_succeeds(self):
2628
store = self.get_store(self)
2629
store._load_from_string(b'foo=bar\n')
2630
# FIXME: There should be a better way than relying on the test
2631
# parametrization to identify branch.conf -- vila 2011-0526
2632
if self.store_id in ('branch', 'remote_branch'):
2633
# branch stores requires write locked branches
2634
self.addCleanup(store.branch.lock_write().unlock)
2635
section = store.get_mutable_section(None)
2636
section.remove('foo')
2638
self.assertEqual(True, self.has_store(store))
2639
modified_store = self.get_store(self)
2640
sections = list(modified_store.get_sections())
2641
self.assertLength(0, sections)
2643
def test_save_with_content_succeeds(self):
2644
# FIXME: There should be a better way than relying on the test
2645
# parametrization to identify branch.conf -- vila 2011-0526
2646
if self.store_id in ('branch', 'remote_branch'):
2647
raise tests.TestNotApplicable(
2648
'branch.conf is *always* created when a branch is initialized')
2649
store = self.get_store(self)
2650
store._load_from_string(b'foo=bar\n')
2651
self.assertEqual(False, self.has_store(store))
2653
self.assertEqual(True, self.has_store(store))
2654
modified_store = self.get_store(self)
2655
sections = list(modified_store.get_sections())
2656
self.assertLength(1, sections)
2657
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2659
def test_set_option_in_empty_store(self):
2660
store = self.get_store(self)
2661
# FIXME: There should be a better way than relying on the test
2662
# parametrization to identify branch.conf -- vila 2011-0526
2663
if self.store_id in ('branch', 'remote_branch'):
2664
# branch stores requires write locked branches
2665
self.addCleanup(store.branch.lock_write().unlock)
2666
section = store.get_mutable_section(None)
2667
section.set('foo', 'bar')
2669
modified_store = self.get_store(self)
2670
sections = list(modified_store.get_sections())
2671
self.assertLength(1, sections)
2672
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2674
def test_set_option_in_default_section(self):
2675
store = self.get_store(self)
2676
store._load_from_string(b'')
2677
# FIXME: There should be a better way than relying on the test
2678
# parametrization to identify branch.conf -- vila 2011-0526
2679
if self.store_id in ('branch', 'remote_branch'):
2680
# branch stores requires write locked branches
2681
self.addCleanup(store.branch.lock_write().unlock)
2682
section = store.get_mutable_section(None)
2683
section.set('foo', 'bar')
2685
modified_store = self.get_store(self)
2686
sections = list(modified_store.get_sections())
2687
self.assertLength(1, sections)
2688
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2690
def test_set_option_in_named_section(self):
2691
store = self.get_store(self)
2692
store._load_from_string(b'')
2693
# FIXME: There should be a better way than relying on the test
2694
# parametrization to identify branch.conf -- vila 2011-0526
2695
if self.store_id in ('branch', 'remote_branch'):
2696
# branch stores requires write locked branches
2697
self.addCleanup(store.branch.lock_write().unlock)
2698
section = store.get_mutable_section('baz')
2699
section.set('foo', 'bar')
2701
modified_store = self.get_store(self)
2702
sections = list(modified_store.get_sections())
2703
self.assertLength(1, sections)
2704
self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
2706
def test_load_hook(self):
2707
# First, we need to ensure that the store exists
2708
store = self.get_store(self)
2709
# FIXME: There should be a better way than relying on the test
2710
# parametrization to identify branch.conf -- vila 2011-0526
2711
if self.store_id in ('branch', 'remote_branch'):
2712
# branch stores requires write locked branches
2713
self.addCleanup(store.branch.lock_write().unlock)
2714
section = store.get_mutable_section('baz')
2715
section.set('foo', 'bar')
2717
# Now we can try to load it
2718
store = self.get_store(self)
2723
config.ConfigHooks.install_named_hook('load', hook, None)
2724
self.assertLength(0, calls)
2726
self.assertLength(1, calls)
2727
self.assertEqual((store,), calls[0])
2729
def test_save_hook(self):
2734
config.ConfigHooks.install_named_hook('save', hook, None)
2735
self.assertLength(0, calls)
2736
store = self.get_store(self)
2737
# FIXME: There should be a better way than relying on the test
2738
# parametrization to identify branch.conf -- vila 2011-0526
2739
if self.store_id in ('branch', 'remote_branch'):
2740
# branch stores requires write locked branches
2741
self.addCleanup(store.branch.lock_write().unlock)
2742
section = store.get_mutable_section('baz')
2743
section.set('foo', 'bar')
2745
self.assertLength(1, calls)
2746
self.assertEqual((store,), calls[0])
2748
def test_set_mark_dirty(self):
2749
stack = config.MemoryStack(b'')
2750
self.assertLength(0, stack.store.dirty_sections)
2751
stack.set('foo', 'baz')
2752
self.assertLength(1, stack.store.dirty_sections)
2753
self.assertTrue(stack.store._need_saving())
2755
def test_remove_mark_dirty(self):
2756
stack = config.MemoryStack(b'foo=bar')
2757
self.assertLength(0, stack.store.dirty_sections)
2759
self.assertLength(1, stack.store.dirty_sections)
2760
self.assertTrue(stack.store._need_saving())
2763
class TestStoreSaveChanges(tests.TestCaseWithTransport):
2764
"""Tests that config changes are kept in memory and saved on-demand."""
2767
super(TestStoreSaveChanges, self).setUp()
2768
self.transport = self.get_transport()
2769
# Most of the tests involve two stores pointing to the same persistent
2770
# storage to observe the effects of concurrent changes
2771
self.st1 = config.TransportIniFileStore(self.transport, 'foo.conf')
2772
self.st2 = config.TransportIniFileStore(self.transport, 'foo.conf')
2776
self.warnings.append(args[0] % args[1:])
2777
self.overrideAttr(trace, 'warning', warning)
2779
def has_store(self, store):
2780
store_basename = urlutils.relative_url(self.transport.external_url(),
2781
store.external_url())
2782
return self.transport.has(store_basename)
2784
def get_stack(self, store):
2785
# Any stack will do as long as it uses the right store, just a single
2786
# no-name section is enough
2787
return config.Stack([store.get_sections], store)
2789
def test_no_changes_no_save(self):
2790
s = self.get_stack(self.st1)
2791
s.store.save_changes()
2792
self.assertEqual(False, self.has_store(self.st1))
2794
def test_unrelated_concurrent_update(self):
2795
s1 = self.get_stack(self.st1)
2796
s2 = self.get_stack(self.st2)
2797
s1.set('foo', 'bar')
2798
s2.set('baz', 'quux')
2800
# Changes don't propagate magically
2801
self.assertEqual(None, s1.get('baz'))
2802
s2.store.save_changes()
2803
self.assertEqual('quux', s2.get('baz'))
2804
# Changes are acquired when saving
2805
self.assertEqual('bar', s2.get('foo'))
2806
# Since there is no overlap, no warnings are emitted
2807
self.assertLength(0, self.warnings)
2809
def test_concurrent_update_modified(self):
2810
s1 = self.get_stack(self.st1)
2811
s2 = self.get_stack(self.st2)
2812
s1.set('foo', 'bar')
2813
s2.set('foo', 'baz')
2816
s2.store.save_changes()
2817
self.assertEqual('baz', s2.get('foo'))
2818
# But the user get a warning
2819
self.assertLength(1, self.warnings)
2820
warning = self.warnings[0]
2821
self.assertStartsWith(warning, 'Option foo in section None')
2822
self.assertEndsWith(warning, 'was changed from <CREATED> to bar.'
2823
' The baz value will be saved.')
2825
def test_concurrent_deletion(self):
2826
self.st1._load_from_string(b'foo=bar')
2828
s1 = self.get_stack(self.st1)
2829
s2 = self.get_stack(self.st2)
2832
s1.store.save_changes()
2834
self.assertLength(0, self.warnings)
2835
s2.store.save_changes()
2837
self.assertLength(1, self.warnings)
2838
warning = self.warnings[0]
2839
self.assertStartsWith(warning, 'Option foo in section None')
2840
self.assertEndsWith(warning, 'was changed from bar to <CREATED>.'
2841
' The <DELETED> value will be saved.')
2844
class TestQuotingIniFileStore(tests.TestCaseWithTransport):
2846
def get_store(self):
2847
return config.TransportIniFileStore(self.get_transport(), 'foo.conf')
2849
def test_get_quoted_string(self):
2850
store = self.get_store()
2851
store._load_from_string(b'foo= " abc "')
2852
stack = config.Stack([store.get_sections])
2853
self.assertEqual(' abc ', stack.get('foo'))
2855
def test_set_quoted_string(self):
2856
store = self.get_store()
2857
stack = config.Stack([store.get_sections], store)
2858
stack.set('foo', ' a b c ')
2860
self.assertFileEqual(b'foo = " a b c "' +
2861
os.linesep.encode('ascii'), 'foo.conf')
2864
class TestTransportIniFileStore(TestStore):
2866
def test_loading_unknown_file_fails(self):
2867
store = config.TransportIniFileStore(self.get_transport(),
2869
self.assertRaises(errors.NoSuchFile, store.load)
2871
def test_invalid_content(self):
2872
store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
2873
self.assertEqual(False, store.is_loaded())
2874
exc = self.assertRaises(
2875
config.ParseConfigError, store._load_from_string,
2876
b'this is invalid !')
2877
self.assertEndsWith(exc.filename, 'foo.conf')
2878
# And the load failed
2879
self.assertEqual(False, store.is_loaded())
2881
def test_get_embedded_sections(self):
2882
# A more complicated example (which also shows that section names and
2883
# option names share the same name space...)
2884
# FIXME: This should be fixed by forbidding dicts as values ?
2885
# -- vila 2011-04-05
2886
store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
2887
store._load_from_string(b'''
2891
foo_in_DEFAULT=foo_DEFAULT
2899
sections = list(store.get_sections())
2900
self.assertLength(4, sections)
2901
# The default section has no name.
2902
# List values are provided as strings and need to be explicitly
2903
# converted by specifying from_unicode=list_from_store at option
2905
self.assertSectionContent((None, {'foo': 'bar', 'l': u'1,2'}),
2907
self.assertSectionContent(
2908
('DEFAULT', {'foo_in_DEFAULT': 'foo_DEFAULT'}), sections[1])
2909
self.assertSectionContent(
2910
('bar', {'foo_in_bar': 'barbar'}), sections[2])
2911
# sub sections are provided as embedded dicts.
2912
self.assertSectionContent(
2913
('baz', {'foo_in_baz': 'barbaz', 'qux': {'foo_in_qux': 'quux'}}),
2917
class TestLockableIniFileStore(TestStore):
2919
def test_create_store_in_created_dir(self):
2920
self.assertPathDoesNotExist('dir')
2921
t = self.get_transport('dir/subdir')
2922
store = config.LockableIniFileStore(t, 'foo.conf')
2923
store.get_mutable_section(None).set('foo', 'bar')
2925
self.assertPathExists('dir/subdir')
2928
class TestConcurrentStoreUpdates(TestStore):
2929
"""Test that Stores properly handle conccurent updates.
2931
New Store implementation may fail some of these tests but until such
2932
implementations exist it's hard to properly filter them from the scenarios
2933
applied here. If you encounter such a case, contact the bzr devs.
2936
scenarios = [(key, {'get_stack': builder}) for key, builder
2937
in config.test_stack_builder_registry.iteritems()]
2940
super(TestConcurrentStoreUpdates, self).setUp()
2941
self.stack = self.get_stack(self)
2942
if not isinstance(self.stack, config._CompatibleStack):
2943
raise tests.TestNotApplicable(
2944
'%s is not meant to be compatible with the old config design'
2946
self.stack.set('one', '1')
2947
self.stack.set('two', '2')
2949
self.stack.store.save()
2951
def test_simple_read_access(self):
2952
self.assertEqual('1', self.stack.get('one'))
2954
def test_simple_write_access(self):
2955
self.stack.set('one', 'one')
2956
self.assertEqual('one', self.stack.get('one'))
2958
def test_listen_to_the_last_speaker(self):
2960
c2 = self.get_stack(self)
2961
c1.set('one', 'ONE')
2962
c2.set('two', 'TWO')
2963
self.assertEqual('ONE', c1.get('one'))
2964
self.assertEqual('TWO', c2.get('two'))
2965
# The second update respect the first one
2966
self.assertEqual('ONE', c2.get('one'))
2968
def test_last_speaker_wins(self):
2969
# If the same config is not shared, the same variable modified twice
2970
# can only see a single result.
2972
c2 = self.get_stack(self)
2975
self.assertEqual('c2', c2.get('one'))
2976
# The first modification is still available until another refresh
2978
self.assertEqual('c1', c1.get('one'))
2979
c1.set('two', 'done')
2980
self.assertEqual('c2', c1.get('one'))
2982
def test_writes_are_serialized(self):
2984
c2 = self.get_stack(self)
2986
# We spawn a thread that will pause *during* the config saving.
2987
before_writing = threading.Event()
2988
after_writing = threading.Event()
2989
writing_done = threading.Event()
2990
c1_save_without_locking_orig = c1.store.save_without_locking
2992
def c1_save_without_locking():
2993
before_writing.set()
2994
c1_save_without_locking_orig()
2995
# The lock is held. We wait for the main thread to decide when to
2997
after_writing.wait()
2998
c1.store.save_without_locking = c1_save_without_locking
3003
t1 = threading.Thread(target=c1_set)
3004
# Collect the thread after the test
3005
self.addCleanup(t1.join)
3006
# Be ready to unblock the thread if the test goes wrong
3007
self.addCleanup(after_writing.set)
3009
before_writing.wait()
3010
self.assertRaises(errors.LockContention,
3011
c2.set, 'one', 'c2')
3012
self.assertEqual('c1', c1.get('one'))
3013
# Let the lock be released
3017
self.assertEqual('c2', c2.get('one'))
3019
def test_read_while_writing(self):
3021
# We spawn a thread that will pause *during* the write
3022
ready_to_write = threading.Event()
3023
do_writing = threading.Event()
3024
writing_done = threading.Event()
3025
# We override the _save implementation so we know the store is locked
3026
c1_save_without_locking_orig = c1.store.save_without_locking
3028
def c1_save_without_locking():
3029
ready_to_write.set()
3030
# The lock is held. We wait for the main thread to decide when to
3033
c1_save_without_locking_orig()
3035
c1.store.save_without_locking = c1_save_without_locking
3039
t1 = threading.Thread(target=c1_set)
3040
# Collect the thread after the test
3041
self.addCleanup(t1.join)
3042
# Be ready to unblock the thread if the test goes wrong
3043
self.addCleanup(do_writing.set)
3045
# Ensure the thread is ready to write
3046
ready_to_write.wait()
3047
self.assertEqual('c1', c1.get('one'))
3048
# If we read during the write, we get the old value
3049
c2 = self.get_stack(self)
3050
self.assertEqual('1', c2.get('one'))
3051
# Let the writing occur and ensure it occurred
3054
# Now we get the updated value
3055
c3 = self.get_stack(self)
3056
self.assertEqual('c1', c3.get('one'))
3058
# FIXME: It may be worth looking into removing the lock dir when it's not
3059
# needed anymore and look at possible fallouts for concurrent lockers. This
3060
# will matter if/when we use config files outside of breezy directories
3061
# (.config/breezy or .bzr) -- vila 20110-04-111
3064
class TestSectionMatcher(TestStore):
3066
scenarios = [('location', {'matcher': config.LocationMatcher}),
3067
('id', {'matcher': config.NameMatcher}), ]
3070
super(TestSectionMatcher, self).setUp()
3071
# Any simple store is good enough
3072
self.get_store = config.test_store_builder_registry.get('configobj')
3074
def test_no_matches_for_empty_stores(self):
3075
store = self.get_store(self)
3076
store._load_from_string(b'')
3077
matcher = self.matcher(store, '/bar')
3078
self.assertEqual([], list(matcher.get_sections()))
3080
def test_build_doesnt_load_store(self):
3081
store = self.get_store(self)
3082
self.matcher(store, '/bar')
3083
self.assertFalse(store.is_loaded())
3086
class TestLocationSection(tests.TestCase):
3088
def get_section(self, options, extra_path):
3089
section = config.Section('foo', options)
3090
return config.LocationSection(section, extra_path)
3092
def test_simple_option(self):
3093
section = self.get_section({'foo': 'bar'}, '')
3094
self.assertEqual('bar', section.get('foo'))
3096
def test_option_with_extra_path(self):
3097
section = self.get_section({'foo': 'bar', 'foo:policy': 'appendpath'},
3099
self.assertEqual('bar/baz', section.get('foo'))
3101
def test_invalid_policy(self):
3102
section = self.get_section({'foo': 'bar', 'foo:policy': 'die'},
3104
# invalid policies are ignored
3105
self.assertEqual('bar', section.get('foo'))
3108
class TestLocationMatcher(TestStore):
3111
super(TestLocationMatcher, self).setUp()
3112
# Any simple store is good enough
3113
self.get_store = config.test_store_builder_registry.get('configobj')
3115
def test_unrelated_section_excluded(self):
3116
store = self.get_store(self)
3117
store._load_from_string(b'''
3125
section=/foo/bar/baz
3129
self.assertEqual(['/foo', '/foo/baz', '/foo/bar', '/foo/bar/baz',
3131
[section.id for _, section in store.get_sections()])
3132
matcher = config.LocationMatcher(store, '/foo/bar/quux')
3133
sections = [section for _, section in matcher.get_sections()]
3134
self.assertEqual(['/foo/bar', '/foo'],
3135
[section.id for section in sections])
3136
self.assertEqual(['quux', 'bar/quux'],
3137
[section.extra_path for section in sections])
3139
def test_more_specific_sections_first(self):
3140
store = self.get_store(self)
3141
store._load_from_string(b'''
3147
self.assertEqual(['/foo', '/foo/bar'],
3148
[section.id for _, section in store.get_sections()])
3149
matcher = config.LocationMatcher(store, '/foo/bar/baz')
3150
sections = [section for _, section in matcher.get_sections()]
3151
self.assertEqual(['/foo/bar', '/foo'],
3152
[section.id for section in sections])
3153
self.assertEqual(['baz', 'bar/baz'],
3154
[section.extra_path for section in sections])
3156
def test_appendpath_in_no_name_section(self):
3157
# It's a bit weird to allow appendpath in a no-name section, but
3158
# someone may found a use for it
3159
store = self.get_store(self)
3160
store._load_from_string(b'''
3162
foo:policy = appendpath
3164
matcher = config.LocationMatcher(store, 'dir/subdir')
3165
sections = list(matcher.get_sections())
3166
self.assertLength(1, sections)
3167
self.assertEqual('bar/dir/subdir', sections[0][1].get('foo'))
3169
def test_file_urls_are_normalized(self):
3170
store = self.get_store(self)
3171
if sys.platform == 'win32':
3172
expected_url = 'file:///C:/dir/subdir'
3173
expected_location = 'C:/dir/subdir'
3175
expected_url = 'file:///dir/subdir'
3176
expected_location = '/dir/subdir'
3177
matcher = config.LocationMatcher(store, expected_url)
3178
self.assertEqual(expected_location, matcher.location)
3180
def test_branch_name_colo(self):
3181
store = self.get_store(self)
3182
store._load_from_string(dedent("""\
3184
push_location=my{branchname}
3185
""").encode('ascii'))
3186
matcher = config.LocationMatcher(store, 'file:///,branch=example%3c')
3187
self.assertEqual('example<', matcher.branch_name)
3188
((_, section),) = matcher.get_sections()
3189
self.assertEqual('example<', section.locals['branchname'])
3191
def test_branch_name_basename(self):
3192
store = self.get_store(self)
3193
store._load_from_string(dedent("""\
3195
push_location=my{branchname}
3196
""").encode('ascii'))
3197
matcher = config.LocationMatcher(store, 'file:///parent/example%3c')
3198
self.assertEqual('example<', matcher.branch_name)
3199
((_, section),) = matcher.get_sections()
3200
self.assertEqual('example<', section.locals['branchname'])
3203
class TestStartingPathMatcher(TestStore):
3206
super(TestStartingPathMatcher, self).setUp()
3207
# Any simple store is good enough
3208
self.store = config.IniFileStore()
3210
def assertSectionIDs(self, expected, location, content):
3211
self.store._load_from_string(content)
3212
matcher = config.StartingPathMatcher(self.store, location)
3213
sections = list(matcher.get_sections())
3214
self.assertLength(len(expected), sections)
3215
self.assertEqual(expected, [section.id for _, section in sections])
3218
def test_empty(self):
3219
self.assertSectionIDs([], self.get_url(), b'')
3221
def test_url_vs_local_paths(self):
3222
# The matcher location is an url and the section names are local paths
3223
self.assertSectionIDs(['/foo/bar', '/foo'],
3224
'file:///foo/bar/baz', b'''\
3229
def test_local_path_vs_url(self):
3230
# The matcher location is a local path and the section names are urls
3231
self.assertSectionIDs(['file:///foo/bar', 'file:///foo'],
3232
'/foo/bar/baz', b'''\
3237
def test_no_name_section_included_when_present(self):
3238
# Note that other tests will cover the case where the no-name section
3239
# is empty and as such, not included.
3240
sections = self.assertSectionIDs(['/foo/bar', '/foo', None],
3241
'/foo/bar/baz', b'''\
3242
option = defined so the no-name section exists
3246
self.assertEqual(['baz', 'bar/baz', '/foo/bar/baz'],
3247
[s.locals['relpath'] for _, s in sections])
3249
def test_order_reversed(self):
3250
self.assertSectionIDs(['/foo/bar', '/foo'], '/foo/bar/baz', b'''\
3255
def test_unrelated_section_excluded(self):
3256
self.assertSectionIDs(['/foo/bar', '/foo'], '/foo/bar/baz', b'''\
3262
def test_glob_included(self):
3263
sections = self.assertSectionIDs(['/foo/*/baz', '/foo/b*', '/foo'],
3264
'/foo/bar/baz', b'''\
3270
# Note that 'baz' as a relpath for /foo/b* is not fully correct, but
3271
# nothing really is... as far using {relpath} to append it to something
3272
# else, this seems good enough though.
3273
self.assertEqual(['', 'baz', 'bar/baz'],
3274
[s.locals['relpath'] for _, s in sections])
3276
def test_respect_order(self):
3277
self.assertSectionIDs(['/foo', '/foo/b*', '/foo/*/baz'],
3278
'/foo/bar/baz', b'''\
3286
class TestNameMatcher(TestStore):
3289
super(TestNameMatcher, self).setUp()
3290
self.matcher = config.NameMatcher
3291
# Any simple store is good enough
3292
self.get_store = config.test_store_builder_registry.get('configobj')
3294
def get_matching_sections(self, name):
3295
store = self.get_store(self)
3296
store._load_from_string(b'''
3304
matcher = self.matcher(store, name)
3305
return list(matcher.get_sections())
3307
def test_matching(self):
3308
sections = self.get_matching_sections('foo')
3309
self.assertLength(1, sections)
3310
self.assertSectionContent(('foo', {'option': 'foo'}), sections[0])
3312
def test_not_matching(self):
3313
sections = self.get_matching_sections('baz')
3314
self.assertLength(0, sections)
3317
class TestBaseStackGet(tests.TestCase):
3320
super(TestBaseStackGet, self).setUp()
3321
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3323
def test_get_first_definition(self):
3324
store1 = config.IniFileStore()
3325
store1._load_from_string(b'foo=bar')
3326
store2 = config.IniFileStore()
3327
store2._load_from_string(b'foo=baz')
3328
conf = config.Stack([store1.get_sections, store2.get_sections])
3329
self.assertEqual('bar', conf.get('foo'))
3331
def test_get_with_registered_default_value(self):
3332
config.option_registry.register(config.Option('foo', default='bar'))
3333
conf_stack = config.Stack([])
3334
self.assertEqual('bar', conf_stack.get('foo'))
3336
def test_get_without_registered_default_value(self):
3337
config.option_registry.register(config.Option('foo'))
3338
conf_stack = config.Stack([])
3339
self.assertEqual(None, conf_stack.get('foo'))
3341
def test_get_without_default_value_for_not_registered(self):
3342
conf_stack = config.Stack([])
3343
self.assertEqual(None, conf_stack.get('foo'))
3345
def test_get_for_empty_section_callable(self):
3346
conf_stack = config.Stack([lambda: []])
3347
self.assertEqual(None, conf_stack.get('foo'))
3349
def test_get_for_broken_callable(self):
3350
# Trying to use and invalid callable raises an exception on first use
3351
conf_stack = config.Stack([object])
3352
self.assertRaises(TypeError, conf_stack.get, 'foo')
3355
class TestStackWithSimpleStore(tests.TestCase):
3358
super(TestStackWithSimpleStore, self).setUp()
3359
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3360
self.registry = config.option_registry
3362
def get_conf(self, content=None):
3363
return config.MemoryStack(content)
3365
def test_override_value_from_env(self):
3366
self.overrideEnv('FOO', None)
3367
self.registry.register(
3368
config.Option('foo', default='bar', override_from_env=['FOO']))
3369
self.overrideEnv('FOO', 'quux')
3370
# Env variable provides a default taking over the option one
3371
conf = self.get_conf(b'foo=store')
3372
self.assertEqual('quux', conf.get('foo'))
3374
def test_first_override_value_from_env_wins(self):
3375
self.overrideEnv('NO_VALUE', None)
3376
self.overrideEnv('FOO', None)
3377
self.overrideEnv('BAZ', None)
3378
self.registry.register(
3379
config.Option('foo', default='bar',
3380
override_from_env=['NO_VALUE', 'FOO', 'BAZ']))
3381
self.overrideEnv('FOO', 'foo')
3382
self.overrideEnv('BAZ', 'baz')
3383
# The first env var set wins
3384
conf = self.get_conf(b'foo=store')
3385
self.assertEqual('foo', conf.get('foo'))
3388
class TestMemoryStack(tests.TestCase):
3391
conf = config.MemoryStack(b'foo=bar')
3392
self.assertEqual('bar', conf.get('foo'))
3395
conf = config.MemoryStack(b'foo=bar')
3396
conf.set('foo', 'baz')
3397
self.assertEqual('baz', conf.get('foo'))
3399
def test_no_content(self):
3400
conf = config.MemoryStack()
3401
# No content means no loading
3402
self.assertFalse(conf.store.is_loaded())
3403
self.assertRaises(NotImplementedError, conf.get, 'foo')
3404
# But a content can still be provided
3405
conf.store._load_from_string(b'foo=bar')
3406
self.assertEqual('bar', conf.get('foo'))
3409
class TestStackIterSections(tests.TestCase):
3411
def test_empty_stack(self):
3412
conf = config.Stack([])
3413
sections = list(conf.iter_sections())
3414
self.assertLength(0, sections)
3416
def test_empty_store(self):
3417
store = config.IniFileStore()
3418
store._load_from_string(b'')
3419
conf = config.Stack([store.get_sections])
3420
sections = list(conf.iter_sections())
3421
self.assertLength(0, sections)
3423
def test_simple_store(self):
3424
store = config.IniFileStore()
3425
store._load_from_string(b'foo=bar')
3426
conf = config.Stack([store.get_sections])
3427
tuples = list(conf.iter_sections())
3428
self.assertLength(1, tuples)
3429
(found_store, found_section) = tuples[0]
3430
self.assertIs(store, found_store)
3432
def test_two_stores(self):
3433
store1 = config.IniFileStore()
3434
store1._load_from_string(b'foo=bar')
3435
store2 = config.IniFileStore()
3436
store2._load_from_string(b'bar=qux')
3437
conf = config.Stack([store1.get_sections, store2.get_sections])
3438
tuples = list(conf.iter_sections())
3439
self.assertLength(2, tuples)
3440
self.assertIs(store1, tuples[0][0])
3441
self.assertIs(store2, tuples[1][0])
3444
class TestStackWithTransport(tests.TestCaseWithTransport):
3446
scenarios = [(key, {'get_stack': builder}) for key, builder
3447
in config.test_stack_builder_registry.iteritems()]
3450
class TestConcreteStacks(TestStackWithTransport):
3452
def test_build_stack(self):
3453
# Just a smoke test to help debug builders
3454
self.get_stack(self)
3457
class TestStackGet(TestStackWithTransport):
3460
super(TestStackGet, self).setUp()
3461
self.conf = self.get_stack(self)
3463
def test_get_for_empty_stack(self):
3464
self.assertEqual(None, self.conf.get('foo'))
3466
def test_get_hook(self):
3467
self.conf.set('foo', 'bar')
3472
config.ConfigHooks.install_named_hook('get', hook, None)
3473
self.assertLength(0, calls)
3474
value = self.conf.get('foo')
3475
self.assertEqual('bar', value)
3476
self.assertLength(1, calls)
3477
self.assertEqual((self.conf, 'foo', 'bar'), calls[0])
3480
class TestStackGetWithConverter(tests.TestCase):
3483
super(TestStackGetWithConverter, self).setUp()
3484
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3485
self.registry = config.option_registry
3487
def get_conf(self, content=None):
3488
return config.MemoryStack(content)
3490
def register_bool_option(self, name, default=None, default_from_env=None):
3491
b = config.Option(name, help='A boolean.',
3492
default=default, default_from_env=default_from_env,
3493
from_unicode=config.bool_from_store)
3494
self.registry.register(b)
3496
def test_get_default_bool_None(self):
3497
self.register_bool_option('foo')
3498
conf = self.get_conf(b'')
3499
self.assertEqual(None, conf.get('foo'))
3501
def test_get_default_bool_True(self):
3502
self.register_bool_option('foo', u'True')
3503
conf = self.get_conf(b'')
3504
self.assertEqual(True, conf.get('foo'))
3506
def test_get_default_bool_False(self):
3507
self.register_bool_option('foo', False)
3508
conf = self.get_conf(b'')
3509
self.assertEqual(False, conf.get('foo'))
3511
def test_get_default_bool_False_as_string(self):
3512
self.register_bool_option('foo', u'False')
3513
conf = self.get_conf(b'')
3514
self.assertEqual(False, conf.get('foo'))
3516
def test_get_default_bool_from_env_converted(self):
3517
self.register_bool_option('foo', u'True', default_from_env=['FOO'])
3518
self.overrideEnv('FOO', 'False')
3519
conf = self.get_conf(b'')
3520
self.assertEqual(False, conf.get('foo'))
3522
def test_get_default_bool_when_conversion_fails(self):
3523
self.register_bool_option('foo', default='True')
3524
conf = self.get_conf(b'foo=invalid boolean')
3525
self.assertEqual(True, conf.get('foo'))
3527
def register_integer_option(self, name,
3528
default=None, default_from_env=None):
3529
i = config.Option(name, help='An integer.',
3530
default=default, default_from_env=default_from_env,
3531
from_unicode=config.int_from_store)
3532
self.registry.register(i)
3534
def test_get_default_integer_None(self):
3535
self.register_integer_option('foo')
3536
conf = self.get_conf(b'')
3537
self.assertEqual(None, conf.get('foo'))
3539
def test_get_default_integer(self):
3540
self.register_integer_option('foo', 42)
3541
conf = self.get_conf(b'')
3542
self.assertEqual(42, conf.get('foo'))
3544
def test_get_default_integer_as_string(self):
3545
self.register_integer_option('foo', u'42')
3546
conf = self.get_conf(b'')
3547
self.assertEqual(42, conf.get('foo'))
3549
def test_get_default_integer_from_env(self):
3550
self.register_integer_option('foo', default_from_env=['FOO'])
3551
self.overrideEnv('FOO', '18')
3552
conf = self.get_conf(b'')
3553
self.assertEqual(18, conf.get('foo'))
3555
def test_get_default_integer_when_conversion_fails(self):
3556
self.register_integer_option('foo', default='12')
3557
conf = self.get_conf(b'foo=invalid integer')
3558
self.assertEqual(12, conf.get('foo'))
3560
def register_list_option(self, name, default=None, default_from_env=None):
3561
l = config.ListOption(name, help='A list.', default=default,
3562
default_from_env=default_from_env)
3563
self.registry.register(l)
3565
def test_get_default_list_None(self):
3566
self.register_list_option('foo')
3567
conf = self.get_conf(b'')
3568
self.assertEqual(None, conf.get('foo'))
3570
def test_get_default_list_empty(self):
3571
self.register_list_option('foo', '')
3572
conf = self.get_conf(b'')
3573
self.assertEqual([], conf.get('foo'))
3575
def test_get_default_list_from_env(self):
3576
self.register_list_option('foo', default_from_env=['FOO'])
3577
self.overrideEnv('FOO', '')
3578
conf = self.get_conf(b'')
3579
self.assertEqual([], conf.get('foo'))
3581
def test_get_with_list_converter_no_item(self):
3582
self.register_list_option('foo', None)
3583
conf = self.get_conf(b'foo=,')
3584
self.assertEqual([], conf.get('foo'))
3586
def test_get_with_list_converter_many_items(self):
3587
self.register_list_option('foo', None)
3588
conf = self.get_conf(b'foo=m,o,r,e')
3589
self.assertEqual(['m', 'o', 'r', 'e'], conf.get('foo'))
3591
def test_get_with_list_converter_embedded_spaces_many_items(self):
3592
self.register_list_option('foo', None)
3593
conf = self.get_conf(b'foo=" bar", "baz "')
3594
self.assertEqual([' bar', 'baz '], conf.get('foo'))
3596
def test_get_with_list_converter_stripped_spaces_many_items(self):
3597
self.register_list_option('foo', None)
3598
conf = self.get_conf(b'foo= bar , baz ')
3599
self.assertEqual(['bar', 'baz'], conf.get('foo'))
3602
class TestIterOptionRefs(tests.TestCase):
3603
"""iter_option_refs is a bit unusual, document some cases."""
3605
def assertRefs(self, expected, string):
3606
self.assertEqual(expected, list(config.iter_option_refs(string)))
3608
def test_empty(self):
3609
self.assertRefs([(False, '')], '')
3611
def test_no_refs(self):
3612
self.assertRefs([(False, 'foo bar')], 'foo bar')
3614
def test_single_ref(self):
3615
self.assertRefs([(False, ''), (True, '{foo}'), (False, '')], '{foo}')
3617
def test_broken_ref(self):
3618
self.assertRefs([(False, '{foo')], '{foo')
3620
def test_embedded_ref(self):
3621
self.assertRefs([(False, '{'), (True, '{foo}'), (False, '}')],
3624
def test_two_refs(self):
3625
self.assertRefs([(False, ''), (True, '{foo}'),
3626
(False, ''), (True, '{bar}'),
3630
def test_newline_in_refs_are_not_matched(self):
3631
self.assertRefs([(False, '{\nxx}{xx\n}{{\n}}')], '{\nxx}{xx\n}{{\n}}')
3634
class TestStackExpandOptions(tests.TestCaseWithTransport):
3637
super(TestStackExpandOptions, self).setUp()
3638
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3639
self.registry = config.option_registry
3640
store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
3641
self.conf = config.Stack([store.get_sections], store)
3643
def assertExpansion(self, expected, string, env=None):
3644
self.assertEqual(expected, self.conf.expand_options(string, env))
3646
def test_no_expansion(self):
3647
self.assertExpansion('foo', 'foo')
3649
def test_expand_default_value(self):
3650
self.conf.store._load_from_string(b'bar=baz')
3651
self.registry.register(config.Option('foo', default=u'{bar}'))
3652
self.assertEqual('baz', self.conf.get('foo', expand=True))
3654
def test_expand_default_from_env(self):
3655
self.conf.store._load_from_string(b'bar=baz')
3656
self.registry.register(config.Option('foo', default_from_env=['FOO']))
3657
self.overrideEnv('FOO', '{bar}')
3658
self.assertEqual('baz', self.conf.get('foo', expand=True))
3660
def test_expand_default_on_failed_conversion(self):
3661
self.conf.store._load_from_string(b'baz=bogus\nbar=42\nfoo={baz}')
3662
self.registry.register(
3663
config.Option('foo', default=u'{bar}',
3664
from_unicode=config.int_from_store))
3665
self.assertEqual(42, self.conf.get('foo', expand=True))
3667
def test_env_adding_options(self):
3668
self.assertExpansion('bar', '{foo}', {'foo': 'bar'})
3670
def test_env_overriding_options(self):
3671
self.conf.store._load_from_string(b'foo=baz')
3672
self.assertExpansion('bar', '{foo}', {'foo': 'bar'})
3674
def test_simple_ref(self):
3675
self.conf.store._load_from_string(b'foo=xxx')
3676
self.assertExpansion('xxx', '{foo}')
3678
def test_unknown_ref(self):
3679
self.assertRaises(config.ExpandingUnknownOption,
3680
self.conf.expand_options, '{foo}')
3682
def test_illegal_def_is_ignored(self):
3683
self.assertExpansion('{1,2}', '{1,2}')
3684
self.assertExpansion('{ }', '{ }')
3685
self.assertExpansion('${Foo,f}', '${Foo,f}')
3687
def test_indirect_ref(self):
3688
self.conf.store._load_from_string(b'''
3692
self.assertExpansion('xxx', '{bar}')
3694
def test_embedded_ref(self):
3695
self.conf.store._load_from_string(b'''
3699
self.assertExpansion('xxx', '{{bar}}')
3701
def test_simple_loop(self):
3702
self.conf.store._load_from_string(b'foo={foo}')
3703
self.assertRaises(config.OptionExpansionLoop,
3704
self.conf.expand_options, '{foo}')
3706
def test_indirect_loop(self):
3707
self.conf.store._load_from_string(b'''
3711
e = self.assertRaises(config.OptionExpansionLoop,
3712
self.conf.expand_options, '{foo}')
3713
self.assertEqual('foo->bar->baz', e.refs)
3714
self.assertEqual('{foo}', e.string)
3716
def test_list(self):
3717
self.conf.store._load_from_string(b'''
3721
list={foo},{bar},{baz}
3723
self.registry.register(
3724
config.ListOption('list'))
3725
self.assertEqual(['start', 'middle', 'end'],
3726
self.conf.get('list', expand=True))
3728
def test_cascading_list(self):
3729
self.conf.store._load_from_string(b'''
3735
self.registry.register(config.ListOption('list'))
3736
# Register an intermediate option as a list to ensure no conversion
3737
# happen while expanding. Conversion should only occur for the original
3738
# option ('list' here).
3739
self.registry.register(config.ListOption('baz'))
3740
self.assertEqual(['start', 'middle', 'end'],
3741
self.conf.get('list', expand=True))
3743
def test_pathologically_hidden_list(self):
3744
self.conf.store._load_from_string(b'''
3750
hidden={start}{middle}{end}
3752
# What matters is what the registration says, the conversion happens
3753
# only after all expansions have been performed
3754
self.registry.register(config.ListOption('hidden'))
3755
self.assertEqual(['bin', 'go'],
3756
self.conf.get('hidden', expand=True))
3759
class TestStackCrossSectionsExpand(tests.TestCaseWithTransport):
3762
super(TestStackCrossSectionsExpand, self).setUp()
3764
def get_config(self, location, string):
3767
# Since we don't save the config we won't strictly require to inherit
3768
# from TestCaseInTempDir, but an error occurs so quickly...
3769
c = config.LocationStack(location)
3770
c.store._load_from_string(string)
3773
def test_dont_cross_unrelated_section(self):
3774
c = self.get_config('/another/branch/path', b'''
3779
[/another/branch/path]
3782
self.assertRaises(config.ExpandingUnknownOption,
3783
c.get, 'bar', expand=True)
3785
def test_cross_related_sections(self):
3786
c = self.get_config('/project/branch/path', b'''
3790
[/project/branch/path]
3793
self.assertEqual('quux', c.get('bar', expand=True))
3796
class TestStackCrossStoresExpand(tests.TestCaseWithTransport):
3798
def test_cross_global_locations(self):
3799
l_store = config.LocationStore()
3800
l_store._load_from_string(b'''
3806
g_store = config.GlobalStore()
3807
g_store._load_from_string(b'''
3813
stack = config.LocationStack('/branch')
3814
self.assertEqual('glob-bar', stack.get('lbar', expand=True))
3815
self.assertEqual('loc-foo', stack.get('gfoo', expand=True))
3818
class TestStackExpandSectionLocals(tests.TestCaseWithTransport):
3820
def test_expand_locals_empty(self):
3821
l_store = config.LocationStore()
3822
l_store._load_from_string(b'''
3823
[/home/user/project]
3828
stack = config.LocationStack('/home/user/project/')
3829
self.assertEqual('', stack.get('base', expand=True))
3830
self.assertEqual('', stack.get('rel', expand=True))
3832
def test_expand_basename_locally(self):
3833
l_store = config.LocationStore()
3834
l_store._load_from_string(b'''
3835
[/home/user/project]
3839
stack = config.LocationStack('/home/user/project/branch')
3840
self.assertEqual('branch', stack.get('bfoo', expand=True))
3842
def test_expand_basename_locally_longer_path(self):
3843
l_store = config.LocationStore()
3844
l_store._load_from_string(b'''
3849
stack = config.LocationStack('/home/user/project/dir/branch')
3850
self.assertEqual('branch', stack.get('bfoo', expand=True))
3852
def test_expand_relpath_locally(self):
3853
l_store = config.LocationStore()
3854
l_store._load_from_string(b'''
3855
[/home/user/project]
3856
lfoo = loc-foo/{relpath}
3859
stack = config.LocationStack('/home/user/project/branch')
3860
self.assertEqual('loc-foo/branch', stack.get('lfoo', expand=True))
3862
def test_expand_relpath_unknonw_in_global(self):
3863
g_store = config.GlobalStore()
3864
g_store._load_from_string(b'''
3869
stack = config.LocationStack('/home/user/project/branch')
3870
self.assertRaises(config.ExpandingUnknownOption,
3871
stack.get, 'gfoo', expand=True)
3873
def test_expand_local_option_locally(self):
3874
l_store = config.LocationStore()
3875
l_store._load_from_string(b'''
3876
[/home/user/project]
3877
lfoo = loc-foo/{relpath}
3881
g_store = config.GlobalStore()
3882
g_store._load_from_string(b'''
3888
stack = config.LocationStack('/home/user/project/branch')
3889
self.assertEqual('glob-bar', stack.get('lbar', expand=True))
3890
self.assertEqual('loc-foo/branch', stack.get('gfoo', expand=True))
3892
def test_locals_dont_leak(self):
3893
"""Make sure we chose the right local in presence of several sections.
3895
l_store = config.LocationStore()
3896
l_store._load_from_string(b'''
3898
lfoo = loc-foo/{relpath}
3899
[/home/user/project]
3900
lfoo = loc-foo/{relpath}
3903
stack = config.LocationStack('/home/user/project/branch')
3904
self.assertEqual('loc-foo/branch', stack.get('lfoo', expand=True))
3905
stack = config.LocationStack('/home/user/bar/baz')
3906
self.assertEqual('loc-foo/bar/baz', stack.get('lfoo', expand=True))
3909
class TestStackSet(TestStackWithTransport):
3911
def test_simple_set(self):
3912
conf = self.get_stack(self)
3913
self.assertEqual(None, conf.get('foo'))
3914
conf.set('foo', 'baz')
3915
# Did we get it back ?
3916
self.assertEqual('baz', conf.get('foo'))
3918
def test_set_creates_a_new_section(self):
3919
conf = self.get_stack(self)
3920
conf.set('foo', 'baz')
3921
self.assertEqual, 'baz', conf.get('foo')
3923
def test_set_hook(self):
3928
config.ConfigHooks.install_named_hook('set', hook, None)
3929
self.assertLength(0, calls)
3930
conf = self.get_stack(self)
3931
conf.set('foo', 'bar')
3932
self.assertLength(1, calls)
3933
self.assertEqual((conf, 'foo', 'bar'), calls[0])
3936
class TestStackRemove(TestStackWithTransport):
3938
def test_remove_existing(self):
3939
conf = self.get_stack(self)
3940
conf.set('foo', 'bar')
3941
self.assertEqual('bar', conf.get('foo'))
3943
# Did we get it back ?
3944
self.assertEqual(None, conf.get('foo'))
3946
def test_remove_unknown(self):
3947
conf = self.get_stack(self)
3948
self.assertRaises(KeyError, conf.remove, 'I_do_not_exist')
3950
def test_remove_hook(self):
3955
config.ConfigHooks.install_named_hook('remove', hook, None)
3956
self.assertLength(0, calls)
3957
conf = self.get_stack(self)
3958
conf.set('foo', 'bar')
3960
self.assertLength(1, calls)
3961
self.assertEqual((conf, 'foo'), calls[0])
3964
class TestConfigGetOptions(tests.TestCaseWithTransport, TestOptionsMixin):
3967
super(TestConfigGetOptions, self).setUp()
3968
create_configs(self)
3970
def test_no_variable(self):
3971
# Using branch should query branch, locations and breezy
3972
self.assertOptions([], self.branch_config)
3974
def test_option_in_breezy(self):
3975
self.breezy_config.set_user_option('file', 'breezy')
3976
self.assertOptions([('file', 'breezy', 'DEFAULT', 'breezy')],
3979
def test_option_in_locations(self):
3980
self.locations_config.set_user_option('file', 'locations')
3982
[('file', 'locations', self.tree.basedir, 'locations')],
3983
self.locations_config)
3985
def test_option_in_branch(self):
3986
self.branch_config.set_user_option('file', 'branch')
3987
self.assertOptions([('file', 'branch', 'DEFAULT', 'branch')],
3990
def test_option_in_breezy_and_branch(self):
3991
self.breezy_config.set_user_option('file', 'breezy')
3992
self.branch_config.set_user_option('file', 'branch')
3993
self.assertOptions([('file', 'branch', 'DEFAULT', 'branch'),
3994
('file', 'breezy', 'DEFAULT', 'breezy'), ],
3997
def test_option_in_branch_and_locations(self):
3998
# Hmm, locations override branch :-/
3999
self.locations_config.set_user_option('file', 'locations')
4000
self.branch_config.set_user_option('file', 'branch')
4002
[('file', 'locations', self.tree.basedir, 'locations'),
4003
('file', 'branch', 'DEFAULT', 'branch'), ],
4006
def test_option_in_breezy_locations_and_branch(self):
4007
self.breezy_config.set_user_option('file', 'breezy')
4008
self.locations_config.set_user_option('file', 'locations')
4009
self.branch_config.set_user_option('file', 'branch')
4011
[('file', 'locations', self.tree.basedir, 'locations'),
4012
('file', 'branch', 'DEFAULT', 'branch'),
4013
('file', 'breezy', 'DEFAULT', 'breezy'), ],
4017
class TestConfigRemoveOption(tests.TestCaseWithTransport, TestOptionsMixin):
4020
super(TestConfigRemoveOption, self).setUp()
4021
create_configs_with_file_option(self)
4023
def test_remove_in_locations(self):
4024
self.locations_config.remove_user_option('file', self.tree.basedir)
4026
[('file', 'branch', 'DEFAULT', 'branch'),
4027
('file', 'breezy', 'DEFAULT', 'breezy'), ],
4030
def test_remove_in_branch(self):
4031
self.branch_config.remove_user_option('file')
4033
[('file', 'locations', self.tree.basedir, 'locations'),
4034
('file', 'breezy', 'DEFAULT', 'breezy'), ],
4037
def test_remove_in_breezy(self):
4038
self.breezy_config.remove_user_option('file')
4040
[('file', 'locations', self.tree.basedir, 'locations'),
4041
('file', 'branch', 'DEFAULT', 'branch'), ],
4045
class TestConfigGetSections(tests.TestCaseWithTransport):
4048
super(TestConfigGetSections, self).setUp()
4049
create_configs(self)
4051
def assertSectionNames(self, expected, conf, name=None):
4052
"""Check which sections are returned for a given config.
4054
If fallback configurations exist their sections can be included.
4056
:param expected: A list of section names.
4058
:param conf: The configuration that will be queried.
4060
:param name: An optional section name that will be passed to
4063
sections = list(conf._get_sections(name))
4064
self.assertLength(len(expected), sections)
4065
self.assertEqual(expected, [n for n, _, _ in sections])
4067
def test_breezy_default_section(self):
4068
self.assertSectionNames(['DEFAULT'], self.breezy_config)
4070
def test_locations_default_section(self):
4071
# No sections are defined in an empty file
4072
self.assertSectionNames([], self.locations_config)
4074
def test_locations_named_section(self):
4075
self.locations_config.set_user_option('file', 'locations')
4076
self.assertSectionNames([self.tree.basedir], self.locations_config)
4078
def test_locations_matching_sections(self):
4079
loc_config = self.locations_config
4080
loc_config.set_user_option('file', 'locations')
4081
# We need to cheat a bit here to create an option in sections above and
4082
# below the 'location' one.
4083
parser = loc_config._get_parser()
4084
# locations.cong deals with '/' ignoring native os.sep
4085
location_names = self.tree.basedir.split('/')
4086
parent = '/'.join(location_names[:-1])
4087
child = '/'.join(location_names + ['child'])
4089
parser[parent]['file'] = 'parent'
4091
parser[child]['file'] = 'child'
4092
self.assertSectionNames([self.tree.basedir, parent], loc_config)
4094
def test_branch_data_default_section(self):
4095
self.assertSectionNames([None],
4096
self.branch_config._get_branch_data_config())
4098
def test_branch_default_sections(self):
4099
# No sections are defined in an empty locations file
4100
self.assertSectionNames([None, 'DEFAULT'],
4102
# Unless we define an option
4103
self.branch_config._get_location_config().set_user_option(
4104
'file', 'locations')
4105
self.assertSectionNames([self.tree.basedir, None, 'DEFAULT'],
4108
def test_breezy_named_section(self):
4109
# We need to cheat as the API doesn't give direct access to sections
4110
# other than DEFAULT.
4111
self.breezy_config.set_alias('breezy', 'bzr')
4112
self.assertSectionNames(['ALIASES'], self.breezy_config, 'ALIASES')
4115
class TestSharedStores(tests.TestCaseInTempDir):
4117
def test_breezy_conf_shared(self):
4118
g1 = config.GlobalStack()
4119
g2 = config.GlobalStack()
4120
# The two stacks share the same store
4121
self.assertIs(g1.store, g2.store)
4124
class TestAuthenticationConfigFilePermissions(tests.TestCaseInTempDir):
4125
"""Test warning for permissions of authentication.conf."""
4128
super(TestAuthenticationConfigFilePermissions, self).setUp()
4129
self.path = osutils.pathjoin(self.test_dir, 'authentication.conf')
4130
with open(self.path, 'wb') as f:
4131
f.write(b"""[broken]
4134
port=port # Error: Not an int
4136
self.overrideAttr(bedding, 'authentication_config_path',
4138
osutils.chmod_if_possible(self.path, 0o755)
4140
def test_check_warning(self):
4141
conf = config.AuthenticationConfig()
4142
self.assertEqual(conf._filename, self.path)
4143
self.assertContainsRe(self.get_log(),
4144
'Saved passwords may be accessible by other users.')
4146
def test_check_suppressed_warning(self):
4147
global_config = config.GlobalConfig()
4148
global_config.set_user_option('suppress_warnings',
4149
'insecure_permissions')
4150
conf = config.AuthenticationConfig()
4151
self.assertEqual(conf._filename, self.path)
4152
self.assertNotContainsRe(self.get_log(),
4153
'Saved passwords may be accessible by other users.')
1315
4156
class TestAuthenticationConfigFile(tests.TestCase):
1316
4157
"""Test the authentication.conf file matching"""