1312
1700
self.assertIs(None, bzrdir_config.get_default_stack_on())
1703
class TestOldConfigHooks(tests.TestCaseWithTransport):
1706
super(TestOldConfigHooks, self).setUp()
1707
create_configs_with_file_option(self)
1709
def assertGetHook(self, conf, name, value):
1713
config.OldConfigHooks.install_named_hook('get', hook, None)
1715
config.OldConfigHooks.uninstall_named_hook, 'get', None)
1716
self.assertLength(0, calls)
1717
actual_value = conf.get_user_option(name)
1718
self.assertEqual(value, actual_value)
1719
self.assertLength(1, calls)
1720
self.assertEqual((conf, name, value), calls[0])
1722
def test_get_hook_breezy(self):
1723
self.assertGetHook(self.breezy_config, 'file', 'breezy')
1725
def test_get_hook_locations(self):
1726
self.assertGetHook(self.locations_config, 'file', 'locations')
1728
def test_get_hook_branch(self):
1729
# Since locations masks branch, we define a different option
1730
self.branch_config.set_user_option('file2', 'branch')
1731
self.assertGetHook(self.branch_config, 'file2', 'branch')
1733
def assertSetHook(self, conf, name, value):
1737
config.OldConfigHooks.install_named_hook('set', hook, None)
1739
config.OldConfigHooks.uninstall_named_hook, 'set', None)
1740
self.assertLength(0, calls)
1741
conf.set_user_option(name, value)
1742
self.assertLength(1, calls)
1743
# We can't assert the conf object below as different configs use
1744
# different means to implement set_user_option and we care only about
1746
self.assertEqual((name, value), calls[0][1:])
1748
def test_set_hook_breezy(self):
1749
self.assertSetHook(self.breezy_config, 'foo', 'breezy')
1751
def test_set_hook_locations(self):
1752
self.assertSetHook(self.locations_config, 'foo', 'locations')
1754
def test_set_hook_branch(self):
1755
self.assertSetHook(self.branch_config, 'foo', 'branch')
1757
def assertRemoveHook(self, conf, name, section_name=None):
1761
config.OldConfigHooks.install_named_hook('remove', hook, None)
1763
config.OldConfigHooks.uninstall_named_hook, 'remove', None)
1764
self.assertLength(0, calls)
1765
conf.remove_user_option(name, section_name)
1766
self.assertLength(1, calls)
1767
# We can't assert the conf object below as different configs use
1768
# different means to implement remove_user_option and we care only about
1770
self.assertEqual((name,), calls[0][1:])
1772
def test_remove_hook_breezy(self):
1773
self.assertRemoveHook(self.breezy_config, 'file')
1775
def test_remove_hook_locations(self):
1776
self.assertRemoveHook(self.locations_config, 'file',
1777
self.locations_config.location)
1779
def test_remove_hook_branch(self):
1780
self.assertRemoveHook(self.branch_config, 'file')
1782
def assertLoadHook(self, name, conf_class, *conf_args):
1786
config.OldConfigHooks.install_named_hook('load', hook, None)
1788
config.OldConfigHooks.uninstall_named_hook, 'load', None)
1789
self.assertLength(0, calls)
1791
conf = conf_class(*conf_args)
1792
# Access an option to trigger a load
1793
conf.get_user_option(name)
1794
self.assertLength(1, calls)
1795
# Since we can't assert about conf, we just use the number of calls ;-/
1797
def test_load_hook_breezy(self):
1798
self.assertLoadHook('file', config.GlobalConfig)
1800
def test_load_hook_locations(self):
1801
self.assertLoadHook('file', config.LocationConfig, self.tree.basedir)
1803
def test_load_hook_branch(self):
1804
self.assertLoadHook('file', config.BranchConfig, self.tree.branch)
1806
def assertSaveHook(self, conf):
1810
config.OldConfigHooks.install_named_hook('save', hook, None)
1812
config.OldConfigHooks.uninstall_named_hook, 'save', None)
1813
self.assertLength(0, calls)
1814
# Setting an option triggers a save
1815
conf.set_user_option('foo', 'bar')
1816
self.assertLength(1, calls)
1817
# Since we can't assert about conf, we just use the number of calls ;-/
1819
def test_save_hook_breezy(self):
1820
self.assertSaveHook(self.breezy_config)
1822
def test_save_hook_locations(self):
1823
self.assertSaveHook(self.locations_config)
1825
def test_save_hook_branch(self):
1826
self.assertSaveHook(self.branch_config)
1829
class TestOldConfigHooksForRemote(tests.TestCaseWithTransport):
1830
"""Tests config hooks for remote configs.
1832
No tests for the remove hook as this is not implemented there.
1836
super(TestOldConfigHooksForRemote, self).setUp()
1837
self.transport_server = test_server.SmartTCPServer_for_testing
1838
create_configs_with_file_option(self)
1840
def assertGetHook(self, conf, name, value):
1844
config.OldConfigHooks.install_named_hook('get', hook, None)
1846
config.OldConfigHooks.uninstall_named_hook, 'get', None)
1847
self.assertLength(0, calls)
1848
actual_value = conf.get_option(name)
1849
self.assertEqual(value, actual_value)
1850
self.assertLength(1, calls)
1851
self.assertEqual((conf, name, value), calls[0])
1853
def test_get_hook_remote_branch(self):
1854
remote_branch = branch.Branch.open(self.get_url('tree'))
1855
self.assertGetHook(remote_branch._get_config(), 'file', 'branch')
1857
def test_get_hook_remote_bzrdir(self):
1858
remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
1859
conf = remote_bzrdir._get_config()
1860
conf.set_option('remotedir', 'file')
1861
self.assertGetHook(conf, 'file', 'remotedir')
1863
def assertSetHook(self, conf, name, value):
1867
config.OldConfigHooks.install_named_hook('set', hook, None)
1869
config.OldConfigHooks.uninstall_named_hook, 'set', None)
1870
self.assertLength(0, calls)
1871
conf.set_option(value, name)
1872
self.assertLength(1, calls)
1873
# We can't assert the conf object below as different configs use
1874
# different means to implement set_user_option and we care only about
1876
self.assertEqual((name, value), calls[0][1:])
1878
def test_set_hook_remote_branch(self):
1879
remote_branch = branch.Branch.open(self.get_url('tree'))
1880
self.addCleanup(remote_branch.lock_write().unlock)
1881
self.assertSetHook(remote_branch._get_config(), 'file', 'remote')
1883
def test_set_hook_remote_bzrdir(self):
1884
remote_branch = branch.Branch.open(self.get_url('tree'))
1885
self.addCleanup(remote_branch.lock_write().unlock)
1886
remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
1887
self.assertSetHook(remote_bzrdir._get_config(), 'file', 'remotedir')
1889
def assertLoadHook(self, expected_nb_calls, name, conf_class, *conf_args):
1893
config.OldConfigHooks.install_named_hook('load', hook, None)
1895
config.OldConfigHooks.uninstall_named_hook, 'load', None)
1896
self.assertLength(0, calls)
1898
conf = conf_class(*conf_args)
1899
# Access an option to trigger a load
1900
conf.get_option(name)
1901
self.assertLength(expected_nb_calls, calls)
1902
# Since we can't assert about conf, we just use the number of calls ;-/
1904
def test_load_hook_remote_branch(self):
1905
remote_branch = branch.Branch.open(self.get_url('tree'))
1906
self.assertLoadHook(1, 'file', remote.RemoteBranchConfig, remote_branch)
1908
def test_load_hook_remote_bzrdir(self):
1909
remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
1910
# The config file doesn't exist, set an option to force its creation
1911
conf = remote_bzrdir._get_config()
1912
conf.set_option('remotedir', 'file')
1913
# We get one call for the server and one call for the client, this is
1914
# caused by the differences in implementations betwen
1915
# SmartServerBzrDirRequestConfigFile (in smart/bzrdir.py) and
1916
# SmartServerBranchGetConfigFile (in smart/branch.py)
1917
self.assertLoadHook(2, 'file', remote.RemoteBzrDirConfig, remote_bzrdir)
1919
def assertSaveHook(self, conf):
1923
config.OldConfigHooks.install_named_hook('save', hook, None)
1925
config.OldConfigHooks.uninstall_named_hook, 'save', None)
1926
self.assertLength(0, calls)
1927
# Setting an option triggers a save
1928
conf.set_option('foo', 'bar')
1929
self.assertLength(1, calls)
1930
# Since we can't assert about conf, we just use the number of calls ;-/
1932
def test_save_hook_remote_branch(self):
1933
remote_branch = branch.Branch.open(self.get_url('tree'))
1934
self.addCleanup(remote_branch.lock_write().unlock)
1935
self.assertSaveHook(remote_branch._get_config())
1937
def test_save_hook_remote_bzrdir(self):
1938
remote_branch = branch.Branch.open(self.get_url('tree'))
1939
self.addCleanup(remote_branch.lock_write().unlock)
1940
remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
1941
self.assertSaveHook(remote_bzrdir._get_config())
1944
class TestOptionNames(tests.TestCase):
1946
def is_valid(self, name):
1947
return config._option_ref_re.match('{%s}' % name) is not None
1949
def test_valid_names(self):
1950
self.assertTrue(self.is_valid('foo'))
1951
self.assertTrue(self.is_valid('foo.bar'))
1952
self.assertTrue(self.is_valid('f1'))
1953
self.assertTrue(self.is_valid('_'))
1954
self.assertTrue(self.is_valid('__bar__'))
1955
self.assertTrue(self.is_valid('a_'))
1956
self.assertTrue(self.is_valid('a1'))
1957
# Don't break bzr-svn for no good reason
1958
self.assertTrue(self.is_valid('guessed-layout'))
1960
def test_invalid_names(self):
1961
self.assertFalse(self.is_valid(' foo'))
1962
self.assertFalse(self.is_valid('foo '))
1963
self.assertFalse(self.is_valid('1'))
1964
self.assertFalse(self.is_valid('1,2'))
1965
self.assertFalse(self.is_valid('foo$'))
1966
self.assertFalse(self.is_valid('!foo'))
1967
self.assertFalse(self.is_valid('foo.'))
1968
self.assertFalse(self.is_valid('foo..bar'))
1969
self.assertFalse(self.is_valid('{}'))
1970
self.assertFalse(self.is_valid('{a}'))
1971
self.assertFalse(self.is_valid('a\n'))
1972
self.assertFalse(self.is_valid('-'))
1973
self.assertFalse(self.is_valid('-a'))
1974
self.assertFalse(self.is_valid('a-'))
1975
self.assertFalse(self.is_valid('a--a'))
1977
def assertSingleGroup(self, reference):
1978
# the regexp is used with split and as such should match the reference
1979
# *only*, if more groups needs to be defined, (?:...) should be used.
1980
m = config._option_ref_re.match('{a}')
1981
self.assertLength(1, m.groups())
1983
def test_valid_references(self):
1984
self.assertSingleGroup('{a}')
1985
self.assertSingleGroup('{{a}}')
1988
class TestOption(tests.TestCase):
1990
def test_default_value(self):
1991
opt = config.Option('foo', default='bar')
1992
self.assertEqual('bar', opt.get_default())
1994
def test_callable_default_value(self):
1995
def bar_as_unicode():
1997
opt = config.Option('foo', default=bar_as_unicode)
1998
self.assertEqual('bar', opt.get_default())
2000
def test_default_value_from_env(self):
2001
opt = config.Option('foo', default='bar', default_from_env=['FOO'])
2002
self.overrideEnv('FOO', 'quux')
2003
# Env variable provides a default taking over the option one
2004
self.assertEqual('quux', opt.get_default())
2006
def test_first_default_value_from_env_wins(self):
2007
opt = config.Option('foo', default='bar',
2008
default_from_env=['NO_VALUE', 'FOO', 'BAZ'])
2009
self.overrideEnv('FOO', 'foo')
2010
self.overrideEnv('BAZ', 'baz')
2011
# The first env var set wins
2012
self.assertEqual('foo', opt.get_default())
2014
def test_not_supported_list_default_value(self):
2015
self.assertRaises(AssertionError, config.Option, 'foo', default=[1])
2017
def test_not_supported_object_default_value(self):
2018
self.assertRaises(AssertionError, config.Option, 'foo',
2021
def test_not_supported_callable_default_value_not_unicode(self):
2022
def bar_not_unicode():
2024
opt = config.Option('foo', default=bar_not_unicode)
2025
self.assertRaises(AssertionError, opt.get_default)
2027
def test_get_help_topic(self):
2028
opt = config.Option('foo')
2029
self.assertEqual('foo', opt.get_help_topic())
2032
class TestOptionConverter(tests.TestCase):
2034
def assertConverted(self, expected, opt, value):
2035
self.assertEqual(expected, opt.convert_from_unicode(None, value))
2037
def assertCallsWarning(self, opt, value):
2041
warnings.append(args[0] % args[1:])
2042
self.overrideAttr(trace, 'warning', warning)
2043
self.assertEqual(None, opt.convert_from_unicode(None, value))
2044
self.assertLength(1, warnings)
2046
'Value "%s" is not valid for "%s"' % (value, opt.name),
2049
def assertCallsError(self, opt, value):
2050
self.assertRaises(config.ConfigOptionValueError,
2051
opt.convert_from_unicode, None, value)
2053
def assertConvertInvalid(self, opt, invalid_value):
2055
self.assertEqual(None, opt.convert_from_unicode(None, invalid_value))
2056
opt.invalid = 'warning'
2057
self.assertCallsWarning(opt, invalid_value)
2058
opt.invalid = 'error'
2059
self.assertCallsError(opt, invalid_value)
2062
class TestOptionWithBooleanConverter(TestOptionConverter):
2064
def get_option(self):
2065
return config.Option('foo', help='A boolean.',
2066
from_unicode=config.bool_from_store)
2068
def test_convert_invalid(self):
2069
opt = self.get_option()
2070
# A string that is not recognized as a boolean
2071
self.assertConvertInvalid(opt, u'invalid-boolean')
2072
# A list of strings is never recognized as a boolean
2073
self.assertConvertInvalid(opt, [u'not', u'a', u'boolean'])
2075
def test_convert_valid(self):
2076
opt = self.get_option()
2077
self.assertConverted(True, opt, u'True')
2078
self.assertConverted(True, opt, u'1')
2079
self.assertConverted(False, opt, u'False')
2082
class TestOptionWithIntegerConverter(TestOptionConverter):
2084
def get_option(self):
2085
return config.Option('foo', help='An integer.',
2086
from_unicode=config.int_from_store)
2088
def test_convert_invalid(self):
2089
opt = self.get_option()
2090
# A string that is not recognized as an integer
2091
self.assertConvertInvalid(opt, u'forty-two')
2092
# A list of strings is never recognized as an integer
2093
self.assertConvertInvalid(opt, [u'a', u'list'])
2095
def test_convert_valid(self):
2096
opt = self.get_option()
2097
self.assertConverted(16, opt, u'16')
2100
class TestOptionWithSIUnitConverter(TestOptionConverter):
2102
def get_option(self):
2103
return config.Option('foo', help='An integer in SI units.',
2104
from_unicode=config.int_SI_from_store)
2106
def test_convert_invalid(self):
2107
opt = self.get_option()
2108
self.assertConvertInvalid(opt, u'not-a-unit')
2109
self.assertConvertInvalid(opt, u'Gb') # Forgot the value
2110
self.assertConvertInvalid(opt, u'1b') # Forgot the unit
2111
self.assertConvertInvalid(opt, u'1GG')
2112
self.assertConvertInvalid(opt, u'1Mbb')
2113
self.assertConvertInvalid(opt, u'1MM')
2115
def test_convert_valid(self):
2116
opt = self.get_option()
2117
self.assertConverted(int(5e3), opt, u'5kb')
2118
self.assertConverted(int(5e6), opt, u'5M')
2119
self.assertConverted(int(5e6), opt, u'5MB')
2120
self.assertConverted(int(5e9), opt, u'5g')
2121
self.assertConverted(int(5e9), opt, u'5gB')
2122
self.assertConverted(100, opt, u'100')
2125
class TestListOption(TestOptionConverter):
2127
def get_option(self):
2128
return config.ListOption('foo', help='A list.')
2130
def test_convert_invalid(self):
2131
opt = self.get_option()
2132
# We don't even try to convert a list into a list, we only expect
2134
self.assertConvertInvalid(opt, [1])
2135
# No string is invalid as all forms can be converted to a list
2137
def test_convert_valid(self):
2138
opt = self.get_option()
2139
# An empty string is an empty list
2140
self.assertConverted([], opt, '') # Using a bare str() just in case
2141
self.assertConverted([], opt, u'')
2143
self.assertConverted([u'True'], opt, u'True')
2145
self.assertConverted([u'42'], opt, u'42')
2147
self.assertConverted([u'bar'], opt, u'bar')
2150
class TestRegistryOption(TestOptionConverter):
2152
def get_option(self, registry):
2153
return config.RegistryOption('foo', registry,
2154
help='A registry option.')
2156
def test_convert_invalid(self):
2157
registry = _mod_registry.Registry()
2158
opt = self.get_option(registry)
2159
self.assertConvertInvalid(opt, [1])
2160
self.assertConvertInvalid(opt, u"notregistered")
2162
def test_convert_valid(self):
2163
registry = _mod_registry.Registry()
2164
registry.register("someval", 1234)
2165
opt = self.get_option(registry)
2166
# Using a bare str() just in case
2167
self.assertConverted(1234, opt, "someval")
2168
self.assertConverted(1234, opt, u'someval')
2169
self.assertConverted(None, opt, None)
2171
def test_help(self):
2172
registry = _mod_registry.Registry()
2173
registry.register("someval", 1234, help="some option")
2174
registry.register("dunno", 1234, help="some other option")
2175
opt = self.get_option(registry)
2177
'A registry option.\n'
2179
'The following values are supported:\n'
2180
' dunno - some other option\n'
2181
' someval - some option\n',
2184
def test_get_help_text(self):
2185
registry = _mod_registry.Registry()
2186
registry.register("someval", 1234, help="some option")
2187
registry.register("dunno", 1234, help="some other option")
2188
opt = self.get_option(registry)
2190
'A registry option.\n'
2192
'The following values are supported:\n'
2193
' dunno - some other option\n'
2194
' someval - some option\n',
2195
opt.get_help_text())
2198
class TestOptionRegistry(tests.TestCase):
2201
super(TestOptionRegistry, self).setUp()
2202
# Always start with an empty registry
2203
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
2204
self.registry = config.option_registry
2206
def test_register(self):
2207
opt = config.Option('foo')
2208
self.registry.register(opt)
2209
self.assertIs(opt, self.registry.get('foo'))
2211
def test_registered_help(self):
2212
opt = config.Option('foo', help='A simple option')
2213
self.registry.register(opt)
2214
self.assertEqual('A simple option', self.registry.get_help('foo'))
2216
def test_dont_register_illegal_name(self):
2217
self.assertRaises(config.IllegalOptionName,
2218
self.registry.register, config.Option(' foo'))
2219
self.assertRaises(config.IllegalOptionName,
2220
self.registry.register, config.Option('bar,'))
2222
lazy_option = config.Option('lazy_foo', help='Lazy help')
2224
def test_register_lazy(self):
2225
self.registry.register_lazy('lazy_foo', self.__module__,
2226
'TestOptionRegistry.lazy_option')
2227
self.assertIs(self.lazy_option, self.registry.get('lazy_foo'))
2229
def test_registered_lazy_help(self):
2230
self.registry.register_lazy('lazy_foo', self.__module__,
2231
'TestOptionRegistry.lazy_option')
2232
self.assertEqual('Lazy help', self.registry.get_help('lazy_foo'))
2234
def test_dont_lazy_register_illegal_name(self):
2235
# This is where the root cause of http://pad.lv/1235099 is better
2236
# understood: 'register_lazy' doc string mentions that key should match
2237
# the option name which indirectly requires that the option name is a
2238
# valid python identifier. We violate that rule here (using a key that
2239
# doesn't match the option name) to test the option name checking.
2240
self.assertRaises(config.IllegalOptionName,
2241
self.registry.register_lazy, ' foo', self.__module__,
2242
'TestOptionRegistry.lazy_option')
2243
self.assertRaises(config.IllegalOptionName,
2244
self.registry.register_lazy, '1,2', self.__module__,
2245
'TestOptionRegistry.lazy_option')
2248
class TestRegisteredOptions(tests.TestCase):
2249
"""All registered options should verify some constraints."""
2251
scenarios = [(key, {'option_name': key, 'option': option}) for key, option
2252
in config.option_registry.iteritems()]
2255
super(TestRegisteredOptions, self).setUp()
2256
self.registry = config.option_registry
2258
def test_proper_name(self):
2259
# An option should be registered under its own name, this can't be
2260
# checked at registration time for the lazy ones.
2261
self.assertEqual(self.option_name, self.option.name)
2263
def test_help_is_set(self):
2264
option_help = self.registry.get_help(self.option_name)
2265
# Come on, think about the user, he really wants to know what the
2267
self.assertIsNot(None, option_help)
2268
self.assertNotEqual('', option_help)
2271
class TestSection(tests.TestCase):
2273
# FIXME: Parametrize so that all sections produced by Stores run these
2274
# tests -- vila 2011-04-01
2276
def test_get_a_value(self):
2277
a_dict = dict(foo='bar')
2278
section = config.Section('myID', a_dict)
2279
self.assertEqual('bar', section.get('foo'))
2281
def test_get_unknown_option(self):
2283
section = config.Section(None, a_dict)
2284
self.assertEqual('out of thin air',
2285
section.get('foo', 'out of thin air'))
2287
def test_options_is_shared(self):
2289
section = config.Section(None, a_dict)
2290
self.assertIs(a_dict, section.options)
2293
class TestMutableSection(tests.TestCase):
2295
scenarios = [('mutable',
2297
lambda opts: config.MutableSection('myID', opts)},),
2301
a_dict = dict(foo='bar')
2302
section = self.get_section(a_dict)
2303
section.set('foo', 'new_value')
2304
self.assertEqual('new_value', section.get('foo'))
2305
# The change appears in the shared section
2306
self.assertEqual('new_value', a_dict.get('foo'))
2307
# We keep track of the change
2308
self.assertTrue('foo' in section.orig)
2309
self.assertEqual('bar', section.orig.get('foo'))
2311
def test_set_preserve_original_once(self):
2312
a_dict = dict(foo='bar')
2313
section = self.get_section(a_dict)
2314
section.set('foo', 'first_value')
2315
section.set('foo', 'second_value')
2316
# We keep track of the original value
2317
self.assertTrue('foo' in section.orig)
2318
self.assertEqual('bar', section.orig.get('foo'))
2320
def test_remove(self):
2321
a_dict = dict(foo='bar')
2322
section = self.get_section(a_dict)
2323
section.remove('foo')
2324
# We get None for unknown options via the default value
2325
self.assertEqual(None, section.get('foo'))
2326
# Or we just get the default value
2327
self.assertEqual('unknown', section.get('foo', 'unknown'))
2328
self.assertFalse('foo' in section.options)
2329
# We keep track of the deletion
2330
self.assertTrue('foo' in section.orig)
2331
self.assertEqual('bar', section.orig.get('foo'))
2333
def test_remove_new_option(self):
2335
section = self.get_section(a_dict)
2336
section.set('foo', 'bar')
2337
section.remove('foo')
2338
self.assertFalse('foo' in section.options)
2339
# The option didn't exist initially so it we need to keep track of it
2340
# with a special value
2341
self.assertTrue('foo' in section.orig)
2342
self.assertEqual(config._NewlyCreatedOption, section.orig['foo'])
2345
class TestCommandLineStore(tests.TestCase):
2348
super(TestCommandLineStore, self).setUp()
2349
self.store = config.CommandLineStore()
2350
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
2352
def get_section(self):
2353
"""Get the unique section for the command line overrides."""
2354
sections = list(self.store.get_sections())
2355
self.assertLength(1, sections)
2356
store, section = sections[0]
2357
self.assertEqual(self.store, store)
2360
def test_no_override(self):
2361
self.store._from_cmdline([])
2362
section = self.get_section()
2363
self.assertLength(0, list(section.iter_option_names()))
2365
def test_simple_override(self):
2366
self.store._from_cmdline(['a=b'])
2367
section = self.get_section()
2368
self.assertEqual('b', section.get('a'))
2370
def test_list_override(self):
2371
opt = config.ListOption('l')
2372
config.option_registry.register(opt)
2373
self.store._from_cmdline(['l=1,2,3'])
2374
val = self.get_section().get('l')
2375
self.assertEqual('1,2,3', val)
2376
# Reminder: lists should be registered as such explicitely, otherwise
2377
# the conversion needs to be done afterwards.
2378
self.assertEqual(['1', '2', '3'],
2379
opt.convert_from_unicode(self.store, val))
2381
def test_multiple_overrides(self):
2382
self.store._from_cmdline(['a=b', 'x=y'])
2383
section = self.get_section()
2384
self.assertEqual('b', section.get('a'))
2385
self.assertEqual('y', section.get('x'))
2387
def test_wrong_syntax(self):
2388
self.assertRaises(errors.BzrCommandError,
2389
self.store._from_cmdline, ['a=b', 'c'])
2391
class TestStoreMinimalAPI(tests.TestCaseWithTransport):
2393
scenarios = [(key, {'get_store': builder}) for key, builder
2394
in config.test_store_builder_registry.iteritems()] + [
2395
('cmdline', {'get_store': lambda test: config.CommandLineStore()})]
2398
store = self.get_store(self)
2399
if isinstance(store, config.TransportIniFileStore):
2400
raise tests.TestNotApplicable(
2401
"%s is not a concrete Store implementation"
2402
" so it doesn't need an id" % (store.__class__.__name__,))
2403
self.assertIsNot(None, store.id)
2406
class TestStore(tests.TestCaseWithTransport):
2408
def assertSectionContent(self, expected, store_and_section):
2409
"""Assert that some options have the proper values in a section."""
2410
_, section = store_and_section
2411
expected_name, expected_options = expected
2412
self.assertEqual(expected_name, section.id)
2415
dict([(k, section.get(k)) for k in expected_options.keys()]))
2418
class TestReadonlyStore(TestStore):
2420
scenarios = [(key, {'get_store': builder}) for key, builder
2421
in config.test_store_builder_registry.iteritems()]
2423
def test_building_delays_load(self):
2424
store = self.get_store(self)
2425
self.assertEqual(False, store.is_loaded())
2426
store._load_from_string(b'')
2427
self.assertEqual(True, store.is_loaded())
2429
def test_get_no_sections_for_empty(self):
2430
store = self.get_store(self)
2431
store._load_from_string(b'')
2432
self.assertEqual([], list(store.get_sections()))
2434
def test_get_default_section(self):
2435
store = self.get_store(self)
2436
store._load_from_string(b'foo=bar')
2437
sections = list(store.get_sections())
2438
self.assertLength(1, sections)
2439
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2441
def test_get_named_section(self):
2442
store = self.get_store(self)
2443
store._load_from_string(b'[baz]\nfoo=bar')
2444
sections = list(store.get_sections())
2445
self.assertLength(1, sections)
2446
self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
2448
def test_load_from_string_fails_for_non_empty_store(self):
2449
store = self.get_store(self)
2450
store._load_from_string(b'foo=bar')
2451
self.assertRaises(AssertionError, store._load_from_string, b'bar=baz')
2454
class TestStoreQuoting(TestStore):
2456
scenarios = [(key, {'get_store': builder}) for key, builder
2457
in config.test_store_builder_registry.iteritems()]
2460
super(TestStoreQuoting, self).setUp()
2461
self.store = self.get_store(self)
2462
# We need a loaded store but any content will do
2463
self.store._load_from_string(b'')
2465
def assertIdempotent(self, s):
2466
"""Assert that quoting an unquoted string is a no-op and vice-versa.
2468
What matters here is that option values, as they appear in a store, can
2469
be safely round-tripped out of the store and back.
2471
:param s: A string, quoted if required.
2473
self.assertEqual(s, self.store.quote(self.store.unquote(s)))
2474
self.assertEqual(s, self.store.unquote(self.store.quote(s)))
2476
def test_empty_string(self):
2477
if isinstance(self.store, config.IniFileStore):
2478
# configobj._quote doesn't handle empty values
2479
self.assertRaises(AssertionError,
2480
self.assertIdempotent, '')
2482
self.assertIdempotent('')
2483
# But quoted empty strings are ok
2484
self.assertIdempotent('""')
2486
def test_embedded_spaces(self):
2487
self.assertIdempotent('" a b c "')
2489
def test_embedded_commas(self):
2490
self.assertIdempotent('" a , b c "')
2492
def test_simple_comma(self):
2493
if isinstance(self.store, config.IniFileStore):
2494
# configobj requires that lists are special-cased
2495
self.assertRaises(AssertionError,
2496
self.assertIdempotent, ',')
2498
self.assertIdempotent(',')
2499
# When a single comma is required, quoting is also required
2500
self.assertIdempotent('","')
2502
def test_list(self):
2503
if isinstance(self.store, config.IniFileStore):
2504
# configobj requires that lists are special-cased
2505
self.assertRaises(AssertionError,
2506
self.assertIdempotent, 'a,b')
2508
self.assertIdempotent('a,b')
2511
class TestDictFromStore(tests.TestCase):
2513
def test_unquote_not_string(self):
2514
conf = config.MemoryStack(b'x=2\n[a_section]\na=1\n')
2515
value = conf.get('a_section')
2516
# Urgh, despite 'conf' asking for the no-name section, we get the
2517
# content of another section as a dict o_O
2518
self.assertEqual({'a': '1'}, value)
2519
unquoted = conf.store.unquote(value)
2520
# Which cannot be unquoted but shouldn't crash either (the use cases
2521
# are getting the value or displaying it. In the later case, '%s' will
2523
self.assertEqual({'a': '1'}, unquoted)
2524
self.assertIn('%s' % (unquoted,), ("{u'a': u'1'}", "{'a': '1'}"))
2527
class TestIniFileStoreContent(tests.TestCaseWithTransport):
2528
"""Simulate loading a config store with content of various encodings.
2530
All files produced by bzr are in utf8 content.
2532
Users may modify them manually and end up with a file that can't be
2533
loaded. We need to issue proper error messages in this case.
2536
invalid_utf8_char = b'\xff'
2538
def test_load_utf8(self):
2539
"""Ensure we can load an utf8-encoded file."""
2540
t = self.get_transport()
2541
# From http://pad.lv/799212
2542
unicode_user = u'b\N{Euro Sign}ar'
2543
unicode_content = u'user=%s' % (unicode_user,)
2544
utf8_content = unicode_content.encode('utf8')
2545
# Store the raw content in the config file
2546
t.put_bytes('foo.conf', utf8_content)
2547
store = config.TransportIniFileStore(t, 'foo.conf')
2549
stack = config.Stack([store.get_sections], store)
2550
self.assertEqual(unicode_user, stack.get('user'))
2552
def test_load_non_ascii(self):
2553
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
2554
t = self.get_transport()
2555
t.put_bytes('foo.conf', b'user=foo\n#%s\n' % (self.invalid_utf8_char,))
2556
store = config.TransportIniFileStore(t, 'foo.conf')
2557
self.assertRaises(config.ConfigContentError, store.load)
2559
def test_load_erroneous_content(self):
2560
"""Ensure we display a proper error on content that can't be parsed."""
2561
t = self.get_transport()
2562
t.put_bytes('foo.conf', b'[open_section\n')
2563
store = config.TransportIniFileStore(t, 'foo.conf')
2564
self.assertRaises(config.ParseConfigError, store.load)
2566
def test_load_permission_denied(self):
2567
"""Ensure we get warned when trying to load an inaccessible file."""
2570
warnings.append(args[0] % args[1:])
2571
self.overrideAttr(trace, 'warning', warning)
2573
t = self.get_transport()
2575
def get_bytes(relpath):
2576
raise errors.PermissionDenied(relpath, "")
2577
t.get_bytes = get_bytes
2578
store = config.TransportIniFileStore(t, 'foo.conf')
2579
self.assertRaises(errors.PermissionDenied, store.load)
2582
[u'Permission denied while trying to load configuration store %s.'
2583
% store.external_url()])
2586
class TestIniConfigContent(tests.TestCaseWithTransport):
2587
"""Simulate loading a IniBasedConfig with content of various encodings.
2589
All files produced by bzr are in utf8 content.
2591
Users may modify them manually and end up with a file that can't be
2592
loaded. We need to issue proper error messages in this case.
2595
invalid_utf8_char = b'\xff'
2597
def test_load_utf8(self):
2598
"""Ensure we can load an utf8-encoded file."""
2599
# From http://pad.lv/799212
2600
unicode_user = u'b\N{Euro Sign}ar'
2601
unicode_content = u'user=%s' % (unicode_user,)
2602
utf8_content = unicode_content.encode('utf8')
2603
# Store the raw content in the config file
2604
with open('foo.conf', 'wb') as f:
2605
f.write(utf8_content)
2606
conf = config.IniBasedConfig(file_name='foo.conf')
2607
self.assertEqual(unicode_user, conf.get_user_option('user'))
2609
def test_load_badly_encoded_content(self):
2610
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
2611
with open('foo.conf', 'wb') as f:
2612
f.write(b'user=foo\n#%s\n' % (self.invalid_utf8_char,))
2613
conf = config.IniBasedConfig(file_name='foo.conf')
2614
self.assertRaises(config.ConfigContentError, conf._get_parser)
2616
def test_load_erroneous_content(self):
2617
"""Ensure we display a proper error on content that can't be parsed."""
2618
with open('foo.conf', 'wb') as f:
2619
f.write(b'[open_section\n')
2620
conf = config.IniBasedConfig(file_name='foo.conf')
2621
self.assertRaises(config.ParseConfigError, conf._get_parser)
2624
class TestMutableStore(TestStore):
2626
scenarios = [(key, {'store_id': key, 'get_store': builder}) for key, builder
2627
in config.test_store_builder_registry.iteritems()]
2630
super(TestMutableStore, self).setUp()
2631
self.transport = self.get_transport()
2633
def has_store(self, store):
2634
store_basename = urlutils.relative_url(self.transport.external_url(),
2635
store.external_url())
2636
return self.transport.has(store_basename)
2638
def test_save_empty_creates_no_file(self):
2639
# FIXME: There should be a better way than relying on the test
2640
# parametrization to identify branch.conf -- vila 2011-0526
2641
if self.store_id in ('branch', 'remote_branch'):
2642
raise tests.TestNotApplicable(
2643
'branch.conf is *always* created when a branch is initialized')
2644
store = self.get_store(self)
2646
self.assertEqual(False, self.has_store(store))
2648
def test_mutable_section_shared(self):
2649
store = self.get_store(self)
2650
store._load_from_string(b'foo=bar\n')
2651
# FIXME: There should be a better way than relying on the test
2652
# parametrization to identify branch.conf -- vila 2011-0526
2653
if self.store_id in ('branch', 'remote_branch'):
2654
# branch stores requires write locked branches
2655
self.addCleanup(store.branch.lock_write().unlock)
2656
section1 = store.get_mutable_section(None)
2657
section2 = store.get_mutable_section(None)
2658
# If we get different sections, different callers won't share the
2660
self.assertIs(section1, section2)
2662
def test_save_emptied_succeeds(self):
2663
store = self.get_store(self)
2664
store._load_from_string(b'foo=bar\n')
2665
# FIXME: There should be a better way than relying on the test
2666
# parametrization to identify branch.conf -- vila 2011-0526
2667
if self.store_id in ('branch', 'remote_branch'):
2668
# branch stores requires write locked branches
2669
self.addCleanup(store.branch.lock_write().unlock)
2670
section = store.get_mutable_section(None)
2671
section.remove('foo')
2673
self.assertEqual(True, self.has_store(store))
2674
modified_store = self.get_store(self)
2675
sections = list(modified_store.get_sections())
2676
self.assertLength(0, sections)
2678
def test_save_with_content_succeeds(self):
2679
# FIXME: There should be a better way than relying on the test
2680
# parametrization to identify branch.conf -- vila 2011-0526
2681
if self.store_id in ('branch', 'remote_branch'):
2682
raise tests.TestNotApplicable(
2683
'branch.conf is *always* created when a branch is initialized')
2684
store = self.get_store(self)
2685
store._load_from_string(b'foo=bar\n')
2686
self.assertEqual(False, self.has_store(store))
2688
self.assertEqual(True, self.has_store(store))
2689
modified_store = self.get_store(self)
2690
sections = list(modified_store.get_sections())
2691
self.assertLength(1, sections)
2692
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2694
def test_set_option_in_empty_store(self):
2695
store = self.get_store(self)
2696
# FIXME: There should be a better way than relying on the test
2697
# parametrization to identify branch.conf -- vila 2011-0526
2698
if self.store_id in ('branch', 'remote_branch'):
2699
# branch stores requires write locked branches
2700
self.addCleanup(store.branch.lock_write().unlock)
2701
section = store.get_mutable_section(None)
2702
section.set('foo', 'bar')
2704
modified_store = self.get_store(self)
2705
sections = list(modified_store.get_sections())
2706
self.assertLength(1, sections)
2707
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2709
def test_set_option_in_default_section(self):
2710
store = self.get_store(self)
2711
store._load_from_string(b'')
2712
# FIXME: There should be a better way than relying on the test
2713
# parametrization to identify branch.conf -- vila 2011-0526
2714
if self.store_id in ('branch', 'remote_branch'):
2715
# branch stores requires write locked branches
2716
self.addCleanup(store.branch.lock_write().unlock)
2717
section = store.get_mutable_section(None)
2718
section.set('foo', 'bar')
2720
modified_store = self.get_store(self)
2721
sections = list(modified_store.get_sections())
2722
self.assertLength(1, sections)
2723
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
2725
def test_set_option_in_named_section(self):
2726
store = self.get_store(self)
2727
store._load_from_string(b'')
2728
# FIXME: There should be a better way than relying on the test
2729
# parametrization to identify branch.conf -- vila 2011-0526
2730
if self.store_id in ('branch', 'remote_branch'):
2731
# branch stores requires write locked branches
2732
self.addCleanup(store.branch.lock_write().unlock)
2733
section = store.get_mutable_section('baz')
2734
section.set('foo', 'bar')
2736
modified_store = self.get_store(self)
2737
sections = list(modified_store.get_sections())
2738
self.assertLength(1, sections)
2739
self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
2741
def test_load_hook(self):
2742
# First, we need to ensure that the store exists
2743
store = self.get_store(self)
2744
# FIXME: There should be a better way than relying on the test
2745
# parametrization to identify branch.conf -- vila 2011-0526
2746
if self.store_id in ('branch', 'remote_branch'):
2747
# branch stores requires write locked branches
2748
self.addCleanup(store.branch.lock_write().unlock)
2749
section = store.get_mutable_section('baz')
2750
section.set('foo', 'bar')
2752
# Now we can try to load it
2753
store = self.get_store(self)
2757
config.ConfigHooks.install_named_hook('load', hook, None)
2758
self.assertLength(0, calls)
2760
self.assertLength(1, calls)
2761
self.assertEqual((store,), calls[0])
2763
def test_save_hook(self):
2767
config.ConfigHooks.install_named_hook('save', hook, None)
2768
self.assertLength(0, calls)
2769
store = self.get_store(self)
2770
# FIXME: There should be a better way than relying on the test
2771
# parametrization to identify branch.conf -- vila 2011-0526
2772
if self.store_id in ('branch', 'remote_branch'):
2773
# branch stores requires write locked branches
2774
self.addCleanup(store.branch.lock_write().unlock)
2775
section = store.get_mutable_section('baz')
2776
section.set('foo', 'bar')
2778
self.assertLength(1, calls)
2779
self.assertEqual((store,), calls[0])
2781
def test_set_mark_dirty(self):
2782
stack = config.MemoryStack(b'')
2783
self.assertLength(0, stack.store.dirty_sections)
2784
stack.set('foo', 'baz')
2785
self.assertLength(1, stack.store.dirty_sections)
2786
self.assertTrue(stack.store._need_saving())
2788
def test_remove_mark_dirty(self):
2789
stack = config.MemoryStack(b'foo=bar')
2790
self.assertLength(0, stack.store.dirty_sections)
2792
self.assertLength(1, stack.store.dirty_sections)
2793
self.assertTrue(stack.store._need_saving())
2796
class TestStoreSaveChanges(tests.TestCaseWithTransport):
2797
"""Tests that config changes are kept in memory and saved on-demand."""
2800
super(TestStoreSaveChanges, self).setUp()
2801
self.transport = self.get_transport()
2802
# Most of the tests involve two stores pointing to the same persistent
2803
# storage to observe the effects of concurrent changes
2804
self.st1 = config.TransportIniFileStore(self.transport, 'foo.conf')
2805
self.st2 = config.TransportIniFileStore(self.transport, 'foo.conf')
2808
self.warnings.append(args[0] % args[1:])
2809
self.overrideAttr(trace, 'warning', warning)
2811
def has_store(self, store):
2812
store_basename = urlutils.relative_url(self.transport.external_url(),
2813
store.external_url())
2814
return self.transport.has(store_basename)
2816
def get_stack(self, store):
2817
# Any stack will do as long as it uses the right store, just a single
2818
# no-name section is enough
2819
return config.Stack([store.get_sections], store)
2821
def test_no_changes_no_save(self):
2822
s = self.get_stack(self.st1)
2823
s.store.save_changes()
2824
self.assertEqual(False, self.has_store(self.st1))
2826
def test_unrelated_concurrent_update(self):
2827
s1 = self.get_stack(self.st1)
2828
s2 = self.get_stack(self.st2)
2829
s1.set('foo', 'bar')
2830
s2.set('baz', 'quux')
2832
# Changes don't propagate magically
2833
self.assertEqual(None, s1.get('baz'))
2834
s2.store.save_changes()
2835
self.assertEqual('quux', s2.get('baz'))
2836
# Changes are acquired when saving
2837
self.assertEqual('bar', s2.get('foo'))
2838
# Since there is no overlap, no warnings are emitted
2839
self.assertLength(0, self.warnings)
2841
def test_concurrent_update_modified(self):
2842
s1 = self.get_stack(self.st1)
2843
s2 = self.get_stack(self.st2)
2844
s1.set('foo', 'bar')
2845
s2.set('foo', 'baz')
2848
s2.store.save_changes()
2849
self.assertEqual('baz', s2.get('foo'))
2850
# But the user get a warning
2851
self.assertLength(1, self.warnings)
2852
warning = self.warnings[0]
2853
self.assertStartsWith(warning, 'Option foo in section None')
2854
self.assertEndsWith(warning, 'was changed from <CREATED> to bar.'
2855
' The baz value will be saved.')
2857
def test_concurrent_deletion(self):
2858
self.st1._load_from_string(b'foo=bar')
2860
s1 = self.get_stack(self.st1)
2861
s2 = self.get_stack(self.st2)
2864
s1.store.save_changes()
2866
self.assertLength(0, self.warnings)
2867
s2.store.save_changes()
2869
self.assertLength(1, self.warnings)
2870
warning = self.warnings[0]
2871
self.assertStartsWith(warning, 'Option foo in section None')
2872
self.assertEndsWith(warning, 'was changed from bar to <CREATED>.'
2873
' The <DELETED> value will be saved.')
2876
class TestQuotingIniFileStore(tests.TestCaseWithTransport):
2878
def get_store(self):
2879
return config.TransportIniFileStore(self.get_transport(), 'foo.conf')
2881
def test_get_quoted_string(self):
2882
store = self.get_store()
2883
store._load_from_string(b'foo= " abc "')
2884
stack = config.Stack([store.get_sections])
2885
self.assertEqual(' abc ', stack.get('foo'))
2887
def test_set_quoted_string(self):
2888
store = self.get_store()
2889
stack = config.Stack([store.get_sections], store)
2890
stack.set('foo', ' a b c ')
2892
self.assertFileEqual(b'foo = " a b c "' + os.linesep.encode('ascii'), 'foo.conf')
2895
class TestTransportIniFileStore(TestStore):
2897
def test_loading_unknown_file_fails(self):
2898
store = config.TransportIniFileStore(self.get_transport(),
2900
self.assertRaises(errors.NoSuchFile, store.load)
2902
def test_invalid_content(self):
2903
store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
2904
self.assertEqual(False, store.is_loaded())
2905
exc = self.assertRaises(
2906
config.ParseConfigError, store._load_from_string,
2907
b'this is invalid !')
2908
self.assertEndsWith(exc.filename, 'foo.conf')
2909
# And the load failed
2910
self.assertEqual(False, store.is_loaded())
2912
def test_get_embedded_sections(self):
2913
# A more complicated example (which also shows that section names and
2914
# option names share the same name space...)
2915
# FIXME: This should be fixed by forbidding dicts as values ?
2916
# -- vila 2011-04-05
2917
store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
2918
store._load_from_string(b'''
2922
foo_in_DEFAULT=foo_DEFAULT
2930
sections = list(store.get_sections())
2931
self.assertLength(4, sections)
2932
# The default section has no name.
2933
# List values are provided as strings and need to be explicitly
2934
# converted by specifying from_unicode=list_from_store at option
2936
self.assertSectionContent((None, {'foo': 'bar', 'l': u'1,2'}),
2938
self.assertSectionContent(
2939
('DEFAULT', {'foo_in_DEFAULT': 'foo_DEFAULT'}), sections[1])
2940
self.assertSectionContent(
2941
('bar', {'foo_in_bar': 'barbar'}), sections[2])
2942
# sub sections are provided as embedded dicts.
2943
self.assertSectionContent(
2944
('baz', {'foo_in_baz': 'barbaz', 'qux': {'foo_in_qux': 'quux'}}),
2948
class TestLockableIniFileStore(TestStore):
2950
def test_create_store_in_created_dir(self):
2951
self.assertPathDoesNotExist('dir')
2952
t = self.get_transport('dir/subdir')
2953
store = config.LockableIniFileStore(t, 'foo.conf')
2954
store.get_mutable_section(None).set('foo', 'bar')
2956
self.assertPathExists('dir/subdir')
2959
class TestConcurrentStoreUpdates(TestStore):
2960
"""Test that Stores properly handle conccurent updates.
2962
New Store implementation may fail some of these tests but until such
2963
implementations exist it's hard to properly filter them from the scenarios
2964
applied here. If you encounter such a case, contact the bzr devs.
2967
scenarios = [(key, {'get_stack': builder}) for key, builder
2968
in config.test_stack_builder_registry.iteritems()]
2971
super(TestConcurrentStoreUpdates, self).setUp()
2972
self.stack = self.get_stack(self)
2973
if not isinstance(self.stack, config._CompatibleStack):
2974
raise tests.TestNotApplicable(
2975
'%s is not meant to be compatible with the old config design'
2977
self.stack.set('one', '1')
2978
self.stack.set('two', '2')
2980
self.stack.store.save()
2982
def test_simple_read_access(self):
2983
self.assertEqual('1', self.stack.get('one'))
2985
def test_simple_write_access(self):
2986
self.stack.set('one', 'one')
2987
self.assertEqual('one', self.stack.get('one'))
2989
def test_listen_to_the_last_speaker(self):
2991
c2 = self.get_stack(self)
2992
c1.set('one', 'ONE')
2993
c2.set('two', 'TWO')
2994
self.assertEqual('ONE', c1.get('one'))
2995
self.assertEqual('TWO', c2.get('two'))
2996
# The second update respect the first one
2997
self.assertEqual('ONE', c2.get('one'))
2999
def test_last_speaker_wins(self):
3000
# If the same config is not shared, the same variable modified twice
3001
# can only see a single result.
3003
c2 = self.get_stack(self)
3006
self.assertEqual('c2', c2.get('one'))
3007
# The first modification is still available until another refresh
3009
self.assertEqual('c1', c1.get('one'))
3010
c1.set('two', 'done')
3011
self.assertEqual('c2', c1.get('one'))
3013
def test_writes_are_serialized(self):
3015
c2 = self.get_stack(self)
3017
# We spawn a thread that will pause *during* the config saving.
3018
before_writing = threading.Event()
3019
after_writing = threading.Event()
3020
writing_done = threading.Event()
3021
c1_save_without_locking_orig = c1.store.save_without_locking
3022
def c1_save_without_locking():
3023
before_writing.set()
3024
c1_save_without_locking_orig()
3025
# The lock is held. We wait for the main thread to decide when to
3027
after_writing.wait()
3028
c1.store.save_without_locking = c1_save_without_locking
3032
t1 = threading.Thread(target=c1_set)
3033
# Collect the thread after the test
3034
self.addCleanup(t1.join)
3035
# Be ready to unblock the thread if the test goes wrong
3036
self.addCleanup(after_writing.set)
3038
before_writing.wait()
3039
self.assertRaises(errors.LockContention,
3040
c2.set, 'one', 'c2')
3041
self.assertEqual('c1', c1.get('one'))
3042
# Let the lock be released
3046
self.assertEqual('c2', c2.get('one'))
3048
def test_read_while_writing(self):
3050
# We spawn a thread that will pause *during* the write
3051
ready_to_write = threading.Event()
3052
do_writing = threading.Event()
3053
writing_done = threading.Event()
3054
# We override the _save implementation so we know the store is locked
3055
c1_save_without_locking_orig = c1.store.save_without_locking
3056
def c1_save_without_locking():
3057
ready_to_write.set()
3058
# The lock is held. We wait for the main thread to decide when to
3061
c1_save_without_locking_orig()
3063
c1.store.save_without_locking = c1_save_without_locking
3066
t1 = threading.Thread(target=c1_set)
3067
# Collect the thread after the test
3068
self.addCleanup(t1.join)
3069
# Be ready to unblock the thread if the test goes wrong
3070
self.addCleanup(do_writing.set)
3072
# Ensure the thread is ready to write
3073
ready_to_write.wait()
3074
self.assertEqual('c1', c1.get('one'))
3075
# If we read during the write, we get the old value
3076
c2 = self.get_stack(self)
3077
self.assertEqual('1', c2.get('one'))
3078
# Let the writing occur and ensure it occurred
3081
# Now we get the updated value
3082
c3 = self.get_stack(self)
3083
self.assertEqual('c1', c3.get('one'))
3085
# FIXME: It may be worth looking into removing the lock dir when it's not
3086
# needed anymore and look at possible fallouts for concurrent lockers. This
3087
# will matter if/when we use config files outside of breezy directories
3088
# (.config/breezy or .bzr) -- vila 20110-04-111
3091
class TestSectionMatcher(TestStore):
3093
scenarios = [('location', {'matcher': config.LocationMatcher}),
3094
('id', {'matcher': config.NameMatcher}),]
3097
super(TestSectionMatcher, self).setUp()
3098
# Any simple store is good enough
3099
self.get_store = config.test_store_builder_registry.get('configobj')
3101
def test_no_matches_for_empty_stores(self):
3102
store = self.get_store(self)
3103
store._load_from_string(b'')
3104
matcher = self.matcher(store, '/bar')
3105
self.assertEqual([], list(matcher.get_sections()))
3107
def test_build_doesnt_load_store(self):
3108
store = self.get_store(self)
3109
self.matcher(store, '/bar')
3110
self.assertFalse(store.is_loaded())
3113
class TestLocationSection(tests.TestCase):
3115
def get_section(self, options, extra_path):
3116
section = config.Section('foo', options)
3117
return config.LocationSection(section, extra_path)
3119
def test_simple_option(self):
3120
section = self.get_section({'foo': 'bar'}, '')
3121
self.assertEqual('bar', section.get('foo'))
3123
def test_option_with_extra_path(self):
3124
section = self.get_section({'foo': 'bar', 'foo:policy': 'appendpath'},
3126
self.assertEqual('bar/baz', section.get('foo'))
3128
def test_invalid_policy(self):
3129
section = self.get_section({'foo': 'bar', 'foo:policy': 'die'},
3131
# invalid policies are ignored
3132
self.assertEqual('bar', section.get('foo'))
3135
class TestLocationMatcher(TestStore):
3138
super(TestLocationMatcher, self).setUp()
3139
# Any simple store is good enough
3140
self.get_store = config.test_store_builder_registry.get('configobj')
3142
def test_unrelated_section_excluded(self):
3143
store = self.get_store(self)
3144
store._load_from_string(b'''
3152
section=/foo/bar/baz
3156
self.assertEqual(['/foo', '/foo/baz', '/foo/bar', '/foo/bar/baz',
3158
[section.id for _, section in store.get_sections()])
3159
matcher = config.LocationMatcher(store, '/foo/bar/quux')
3160
sections = [section for _, section in matcher.get_sections()]
3161
self.assertEqual(['/foo/bar', '/foo'],
3162
[section.id for section in sections])
3163
self.assertEqual(['quux', 'bar/quux'],
3164
[section.extra_path for section in sections])
3166
def test_more_specific_sections_first(self):
3167
store = self.get_store(self)
3168
store._load_from_string(b'''
3174
self.assertEqual(['/foo', '/foo/bar'],
3175
[section.id for _, section in store.get_sections()])
3176
matcher = config.LocationMatcher(store, '/foo/bar/baz')
3177
sections = [section for _, section in matcher.get_sections()]
3178
self.assertEqual(['/foo/bar', '/foo'],
3179
[section.id for section in sections])
3180
self.assertEqual(['baz', 'bar/baz'],
3181
[section.extra_path for section in sections])
3183
def test_appendpath_in_no_name_section(self):
3184
# It's a bit weird to allow appendpath in a no-name section, but
3185
# someone may found a use for it
3186
store = self.get_store(self)
3187
store._load_from_string(b'''
3189
foo:policy = appendpath
3191
matcher = config.LocationMatcher(store, 'dir/subdir')
3192
sections = list(matcher.get_sections())
3193
self.assertLength(1, sections)
3194
self.assertEqual('bar/dir/subdir', sections[0][1].get('foo'))
3196
def test_file_urls_are_normalized(self):
3197
store = self.get_store(self)
3198
if sys.platform == 'win32':
3199
expected_url = 'file:///C:/dir/subdir'
3200
expected_location = 'C:/dir/subdir'
3202
expected_url = 'file:///dir/subdir'
3203
expected_location = '/dir/subdir'
3204
matcher = config.LocationMatcher(store, expected_url)
3205
self.assertEqual(expected_location, matcher.location)
3207
def test_branch_name_colo(self):
3208
store = self.get_store(self)
3209
store._load_from_string(dedent("""\
3211
push_location=my{branchname}
3212
""").encode('ascii'))
3213
matcher = config.LocationMatcher(store, 'file:///,branch=example%3c')
3214
self.assertEqual('example<', matcher.branch_name)
3215
((_, section),) = matcher.get_sections()
3216
self.assertEqual('example<', section.locals['branchname'])
3218
def test_branch_name_basename(self):
3219
store = self.get_store(self)
3220
store._load_from_string(dedent("""\
3222
push_location=my{branchname}
3223
""").encode('ascii'))
3224
matcher = config.LocationMatcher(store, 'file:///parent/example%3c')
3225
self.assertEqual('example<', matcher.branch_name)
3226
((_, section),) = matcher.get_sections()
3227
self.assertEqual('example<', section.locals['branchname'])
3230
class TestStartingPathMatcher(TestStore):
3233
super(TestStartingPathMatcher, self).setUp()
3234
# Any simple store is good enough
3235
self.store = config.IniFileStore()
3237
def assertSectionIDs(self, expected, location, content):
3238
self.store._load_from_string(content)
3239
matcher = config.StartingPathMatcher(self.store, location)
3240
sections = list(matcher.get_sections())
3241
self.assertLength(len(expected), sections)
3242
self.assertEqual(expected, [section.id for _, section in sections])
3245
def test_empty(self):
3246
self.assertSectionIDs([], self.get_url(), b'')
3248
def test_url_vs_local_paths(self):
3249
# The matcher location is an url and the section names are local paths
3250
self.assertSectionIDs(['/foo/bar', '/foo'],
3251
'file:///foo/bar/baz', b'''\
3256
def test_local_path_vs_url(self):
3257
# The matcher location is a local path and the section names are urls
3258
self.assertSectionIDs(['file:///foo/bar', 'file:///foo'],
3259
'/foo/bar/baz', b'''\
3265
def test_no_name_section_included_when_present(self):
3266
# Note that other tests will cover the case where the no-name section
3267
# is empty and as such, not included.
3268
sections = self.assertSectionIDs(['/foo/bar', '/foo', None],
3269
'/foo/bar/baz', b'''\
3270
option = defined so the no-name section exists
3274
self.assertEqual(['baz', 'bar/baz', '/foo/bar/baz'],
3275
[s.locals['relpath'] for _, s in sections])
3277
def test_order_reversed(self):
3278
self.assertSectionIDs(['/foo/bar', '/foo'], '/foo/bar/baz', b'''\
3283
def test_unrelated_section_excluded(self):
3284
self.assertSectionIDs(['/foo/bar', '/foo'], '/foo/bar/baz', b'''\
3290
def test_glob_included(self):
3291
sections = self.assertSectionIDs(['/foo/*/baz', '/foo/b*', '/foo'],
3292
'/foo/bar/baz', b'''\
3298
# Note that 'baz' as a relpath for /foo/b* is not fully correct, but
3299
# nothing really is... as far using {relpath} to append it to something
3300
# else, this seems good enough though.
3301
self.assertEqual(['', 'baz', 'bar/baz'],
3302
[s.locals['relpath'] for _, s in sections])
3304
def test_respect_order(self):
3305
self.assertSectionIDs(['/foo', '/foo/b*', '/foo/*/baz'],
3306
'/foo/bar/baz', b'''\
3314
class TestNameMatcher(TestStore):
3317
super(TestNameMatcher, self).setUp()
3318
self.matcher = config.NameMatcher
3319
# Any simple store is good enough
3320
self.get_store = config.test_store_builder_registry.get('configobj')
3322
def get_matching_sections(self, name):
3323
store = self.get_store(self)
3324
store._load_from_string(b'''
3332
matcher = self.matcher(store, name)
3333
return list(matcher.get_sections())
3335
def test_matching(self):
3336
sections = self.get_matching_sections('foo')
3337
self.assertLength(1, sections)
3338
self.assertSectionContent(('foo', {'option': 'foo'}), sections[0])
3340
def test_not_matching(self):
3341
sections = self.get_matching_sections('baz')
3342
self.assertLength(0, sections)
3345
class TestBaseStackGet(tests.TestCase):
3348
super(TestBaseStackGet, self).setUp()
3349
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3351
def test_get_first_definition(self):
3352
store1 = config.IniFileStore()
3353
store1._load_from_string(b'foo=bar')
3354
store2 = config.IniFileStore()
3355
store2._load_from_string(b'foo=baz')
3356
conf = config.Stack([store1.get_sections, store2.get_sections])
3357
self.assertEqual('bar', conf.get('foo'))
3359
def test_get_with_registered_default_value(self):
3360
config.option_registry.register(config.Option('foo', default='bar'))
3361
conf_stack = config.Stack([])
3362
self.assertEqual('bar', conf_stack.get('foo'))
3364
def test_get_without_registered_default_value(self):
3365
config.option_registry.register(config.Option('foo'))
3366
conf_stack = config.Stack([])
3367
self.assertEqual(None, conf_stack.get('foo'))
3369
def test_get_without_default_value_for_not_registered(self):
3370
conf_stack = config.Stack([])
3371
self.assertEqual(None, conf_stack.get('foo'))
3373
def test_get_for_empty_section_callable(self):
3374
conf_stack = config.Stack([lambda : []])
3375
self.assertEqual(None, conf_stack.get('foo'))
3377
def test_get_for_broken_callable(self):
3378
# Trying to use and invalid callable raises an exception on first use
3379
conf_stack = config.Stack([object])
3380
self.assertRaises(TypeError, conf_stack.get, 'foo')
3383
class TestStackWithSimpleStore(tests.TestCase):
3386
super(TestStackWithSimpleStore, self).setUp()
3387
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3388
self.registry = config.option_registry
3390
def get_conf(self, content=None):
3391
return config.MemoryStack(content)
3393
def test_override_value_from_env(self):
3394
self.overrideEnv('FOO', None)
3395
self.registry.register(
3396
config.Option('foo', default='bar', override_from_env=['FOO']))
3397
self.overrideEnv('FOO', 'quux')
3398
# Env variable provides a default taking over the option one
3399
conf = self.get_conf(b'foo=store')
3400
self.assertEqual('quux', conf.get('foo'))
3402
def test_first_override_value_from_env_wins(self):
3403
self.overrideEnv('NO_VALUE', None)
3404
self.overrideEnv('FOO', None)
3405
self.overrideEnv('BAZ', None)
3406
self.registry.register(
3407
config.Option('foo', default='bar',
3408
override_from_env=['NO_VALUE', 'FOO', 'BAZ']))
3409
self.overrideEnv('FOO', 'foo')
3410
self.overrideEnv('BAZ', 'baz')
3411
# The first env var set wins
3412
conf = self.get_conf(b'foo=store')
3413
self.assertEqual('foo', conf.get('foo'))
3416
class TestMemoryStack(tests.TestCase):
3419
conf = config.MemoryStack(b'foo=bar')
3420
self.assertEqual('bar', conf.get('foo'))
3423
conf = config.MemoryStack(b'foo=bar')
3424
conf.set('foo', 'baz')
3425
self.assertEqual('baz', conf.get('foo'))
3427
def test_no_content(self):
3428
conf = config.MemoryStack()
3429
# No content means no loading
3430
self.assertFalse(conf.store.is_loaded())
3431
self.assertRaises(NotImplementedError, conf.get, 'foo')
3432
# But a content can still be provided
3433
conf.store._load_from_string(b'foo=bar')
3434
self.assertEqual('bar', conf.get('foo'))
3437
class TestStackIterSections(tests.TestCase):
3439
def test_empty_stack(self):
3440
conf = config.Stack([])
3441
sections = list(conf.iter_sections())
3442
self.assertLength(0, sections)
3444
def test_empty_store(self):
3445
store = config.IniFileStore()
3446
store._load_from_string(b'')
3447
conf = config.Stack([store.get_sections])
3448
sections = list(conf.iter_sections())
3449
self.assertLength(0, sections)
3451
def test_simple_store(self):
3452
store = config.IniFileStore()
3453
store._load_from_string(b'foo=bar')
3454
conf = config.Stack([store.get_sections])
3455
tuples = list(conf.iter_sections())
3456
self.assertLength(1, tuples)
3457
(found_store, found_section) = tuples[0]
3458
self.assertIs(store, found_store)
3460
def test_two_stores(self):
3461
store1 = config.IniFileStore()
3462
store1._load_from_string(b'foo=bar')
3463
store2 = config.IniFileStore()
3464
store2._load_from_string(b'bar=qux')
3465
conf = config.Stack([store1.get_sections, store2.get_sections])
3466
tuples = list(conf.iter_sections())
3467
self.assertLength(2, tuples)
3468
self.assertIs(store1, tuples[0][0])
3469
self.assertIs(store2, tuples[1][0])
3472
class TestStackWithTransport(tests.TestCaseWithTransport):
3474
scenarios = [(key, {'get_stack': builder}) for key, builder
3475
in config.test_stack_builder_registry.iteritems()]
3478
class TestConcreteStacks(TestStackWithTransport):
3480
def test_build_stack(self):
3481
# Just a smoke test to help debug builders
3482
self.get_stack(self)
3485
class TestStackGet(TestStackWithTransport):
3488
super(TestStackGet, self).setUp()
3489
self.conf = self.get_stack(self)
3491
def test_get_for_empty_stack(self):
3492
self.assertEqual(None, self.conf.get('foo'))
3494
def test_get_hook(self):
3495
self.conf.set('foo', 'bar')
3499
config.ConfigHooks.install_named_hook('get', hook, None)
3500
self.assertLength(0, calls)
3501
value = self.conf.get('foo')
3502
self.assertEqual('bar', value)
3503
self.assertLength(1, calls)
3504
self.assertEqual((self.conf, 'foo', 'bar'), calls[0])
3507
class TestStackGetWithConverter(tests.TestCase):
3510
super(TestStackGetWithConverter, self).setUp()
3511
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3512
self.registry = config.option_registry
3514
def get_conf(self, content=None):
3515
return config.MemoryStack(content)
3517
def register_bool_option(self, name, default=None, default_from_env=None):
3518
b = config.Option(name, help='A boolean.',
3519
default=default, default_from_env=default_from_env,
3520
from_unicode=config.bool_from_store)
3521
self.registry.register(b)
3523
def test_get_default_bool_None(self):
3524
self.register_bool_option('foo')
3525
conf = self.get_conf(b'')
3526
self.assertEqual(None, conf.get('foo'))
3528
def test_get_default_bool_True(self):
3529
self.register_bool_option('foo', u'True')
3530
conf = self.get_conf(b'')
3531
self.assertEqual(True, conf.get('foo'))
3533
def test_get_default_bool_False(self):
3534
self.register_bool_option('foo', False)
3535
conf = self.get_conf(b'')
3536
self.assertEqual(False, conf.get('foo'))
3538
def test_get_default_bool_False_as_string(self):
3539
self.register_bool_option('foo', u'False')
3540
conf = self.get_conf(b'')
3541
self.assertEqual(False, conf.get('foo'))
3543
def test_get_default_bool_from_env_converted(self):
3544
self.register_bool_option('foo', u'True', default_from_env=['FOO'])
3545
self.overrideEnv('FOO', 'False')
3546
conf = self.get_conf(b'')
3547
self.assertEqual(False, conf.get('foo'))
3549
def test_get_default_bool_when_conversion_fails(self):
3550
self.register_bool_option('foo', default='True')
3551
conf = self.get_conf(b'foo=invalid boolean')
3552
self.assertEqual(True, conf.get('foo'))
3554
def register_integer_option(self, name,
3555
default=None, default_from_env=None):
3556
i = config.Option(name, help='An integer.',
3557
default=default, default_from_env=default_from_env,
3558
from_unicode=config.int_from_store)
3559
self.registry.register(i)
3561
def test_get_default_integer_None(self):
3562
self.register_integer_option('foo')
3563
conf = self.get_conf(b'')
3564
self.assertEqual(None, conf.get('foo'))
3566
def test_get_default_integer(self):
3567
self.register_integer_option('foo', 42)
3568
conf = self.get_conf(b'')
3569
self.assertEqual(42, conf.get('foo'))
3571
def test_get_default_integer_as_string(self):
3572
self.register_integer_option('foo', u'42')
3573
conf = self.get_conf(b'')
3574
self.assertEqual(42, conf.get('foo'))
3576
def test_get_default_integer_from_env(self):
3577
self.register_integer_option('foo', default_from_env=['FOO'])
3578
self.overrideEnv('FOO', '18')
3579
conf = self.get_conf(b'')
3580
self.assertEqual(18, conf.get('foo'))
3582
def test_get_default_integer_when_conversion_fails(self):
3583
self.register_integer_option('foo', default='12')
3584
conf = self.get_conf(b'foo=invalid integer')
3585
self.assertEqual(12, conf.get('foo'))
3587
def register_list_option(self, name, default=None, default_from_env=None):
3588
l = config.ListOption(name, help='A list.', default=default,
3589
default_from_env=default_from_env)
3590
self.registry.register(l)
3592
def test_get_default_list_None(self):
3593
self.register_list_option('foo')
3594
conf = self.get_conf(b'')
3595
self.assertEqual(None, conf.get('foo'))
3597
def test_get_default_list_empty(self):
3598
self.register_list_option('foo', '')
3599
conf = self.get_conf(b'')
3600
self.assertEqual([], conf.get('foo'))
3602
def test_get_default_list_from_env(self):
3603
self.register_list_option('foo', default_from_env=['FOO'])
3604
self.overrideEnv('FOO', '')
3605
conf = self.get_conf(b'')
3606
self.assertEqual([], conf.get('foo'))
3608
def test_get_with_list_converter_no_item(self):
3609
self.register_list_option('foo', None)
3610
conf = self.get_conf(b'foo=,')
3611
self.assertEqual([], conf.get('foo'))
3613
def test_get_with_list_converter_many_items(self):
3614
self.register_list_option('foo', None)
3615
conf = self.get_conf(b'foo=m,o,r,e')
3616
self.assertEqual(['m', 'o', 'r', 'e'], conf.get('foo'))
3618
def test_get_with_list_converter_embedded_spaces_many_items(self):
3619
self.register_list_option('foo', None)
3620
conf = self.get_conf(b'foo=" bar", "baz "')
3621
self.assertEqual([' bar', 'baz '], conf.get('foo'))
3623
def test_get_with_list_converter_stripped_spaces_many_items(self):
3624
self.register_list_option('foo', None)
3625
conf = self.get_conf(b'foo= bar , baz ')
3626
self.assertEqual(['bar', 'baz'], conf.get('foo'))
3629
class TestIterOptionRefs(tests.TestCase):
3630
"""iter_option_refs is a bit unusual, document some cases."""
3632
def assertRefs(self, expected, string):
3633
self.assertEqual(expected, list(config.iter_option_refs(string)))
3635
def test_empty(self):
3636
self.assertRefs([(False, '')], '')
3638
def test_no_refs(self):
3639
self.assertRefs([(False, 'foo bar')], 'foo bar')
3641
def test_single_ref(self):
3642
self.assertRefs([(False, ''), (True, '{foo}'), (False, '')], '{foo}')
3644
def test_broken_ref(self):
3645
self.assertRefs([(False, '{foo')], '{foo')
3647
def test_embedded_ref(self):
3648
self.assertRefs([(False, '{'), (True, '{foo}'), (False, '}')],
3651
def test_two_refs(self):
3652
self.assertRefs([(False, ''), (True, '{foo}'),
3653
(False, ''), (True, '{bar}'),
3657
def test_newline_in_refs_are_not_matched(self):
3658
self.assertRefs([(False, '{\nxx}{xx\n}{{\n}}')], '{\nxx}{xx\n}{{\n}}')
3661
class TestStackExpandOptions(tests.TestCaseWithTransport):
3664
super(TestStackExpandOptions, self).setUp()
3665
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3666
self.registry = config.option_registry
3667
store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
3668
self.conf = config.Stack([store.get_sections], store)
3670
def assertExpansion(self, expected, string, env=None):
3671
self.assertEqual(expected, self.conf.expand_options(string, env))
3673
def test_no_expansion(self):
3674
self.assertExpansion('foo', 'foo')
3676
def test_expand_default_value(self):
3677
self.conf.store._load_from_string(b'bar=baz')
3678
self.registry.register(config.Option('foo', default=u'{bar}'))
3679
self.assertEqual('baz', self.conf.get('foo', expand=True))
3681
def test_expand_default_from_env(self):
3682
self.conf.store._load_from_string(b'bar=baz')
3683
self.registry.register(config.Option('foo', default_from_env=['FOO']))
3684
self.overrideEnv('FOO', '{bar}')
3685
self.assertEqual('baz', self.conf.get('foo', expand=True))
3687
def test_expand_default_on_failed_conversion(self):
3688
self.conf.store._load_from_string(b'baz=bogus\nbar=42\nfoo={baz}')
3689
self.registry.register(
3690
config.Option('foo', default=u'{bar}',
3691
from_unicode=config.int_from_store))
3692
self.assertEqual(42, self.conf.get('foo', expand=True))
3694
def test_env_adding_options(self):
3695
self.assertExpansion('bar', '{foo}', {'foo': 'bar'})
3697
def test_env_overriding_options(self):
3698
self.conf.store._load_from_string(b'foo=baz')
3699
self.assertExpansion('bar', '{foo}', {'foo': 'bar'})
3701
def test_simple_ref(self):
3702
self.conf.store._load_from_string(b'foo=xxx')
3703
self.assertExpansion('xxx', '{foo}')
3705
def test_unknown_ref(self):
3706
self.assertRaises(config.ExpandingUnknownOption,
3707
self.conf.expand_options, '{foo}')
3709
def test_illegal_def_is_ignored(self):
3710
self.assertExpansion('{1,2}', '{1,2}')
3711
self.assertExpansion('{ }', '{ }')
3712
self.assertExpansion('${Foo,f}', '${Foo,f}')
3714
def test_indirect_ref(self):
3715
self.conf.store._load_from_string(b'''
3719
self.assertExpansion('xxx', '{bar}')
3721
def test_embedded_ref(self):
3722
self.conf.store._load_from_string(b'''
3726
self.assertExpansion('xxx', '{{bar}}')
3728
def test_simple_loop(self):
3729
self.conf.store._load_from_string(b'foo={foo}')
3730
self.assertRaises(config.OptionExpansionLoop,
3731
self.conf.expand_options, '{foo}')
3733
def test_indirect_loop(self):
3734
self.conf.store._load_from_string(b'''
3738
e = self.assertRaises(config.OptionExpansionLoop,
3739
self.conf.expand_options, '{foo}')
3740
self.assertEqual('foo->bar->baz', e.refs)
3741
self.assertEqual('{foo}', e.string)
3743
def test_list(self):
3744
self.conf.store._load_from_string(b'''
3748
list={foo},{bar},{baz}
3750
self.registry.register(
3751
config.ListOption('list'))
3752
self.assertEqual(['start', 'middle', 'end'],
3753
self.conf.get('list', expand=True))
3755
def test_cascading_list(self):
3756
self.conf.store._load_from_string(b'''
3762
self.registry.register(config.ListOption('list'))
3763
# Register an intermediate option as a list to ensure no conversion
3764
# happen while expanding. Conversion should only occur for the original
3765
# option ('list' here).
3766
self.registry.register(config.ListOption('baz'))
3767
self.assertEqual(['start', 'middle', 'end'],
3768
self.conf.get('list', expand=True))
3770
def test_pathologically_hidden_list(self):
3771
self.conf.store._load_from_string(b'''
3777
hidden={start}{middle}{end}
3779
# What matters is what the registration says, the conversion happens
3780
# only after all expansions have been performed
3781
self.registry.register(config.ListOption('hidden'))
3782
self.assertEqual(['bin', 'go'],
3783
self.conf.get('hidden', expand=True))
3786
class TestStackCrossSectionsExpand(tests.TestCaseWithTransport):
3789
super(TestStackCrossSectionsExpand, self).setUp()
3791
def get_config(self, location, string):
3794
# Since we don't save the config we won't strictly require to inherit
3795
# from TestCaseInTempDir, but an error occurs so quickly...
3796
c = config.LocationStack(location)
3797
c.store._load_from_string(string)
3800
def test_dont_cross_unrelated_section(self):
3801
c = self.get_config('/another/branch/path', b'''
3806
[/another/branch/path]
3809
self.assertRaises(config.ExpandingUnknownOption,
3810
c.get, 'bar', expand=True)
3812
def test_cross_related_sections(self):
3813
c = self.get_config('/project/branch/path', b'''
3817
[/project/branch/path]
3820
self.assertEqual('quux', c.get('bar', expand=True))
3823
class TestStackCrossStoresExpand(tests.TestCaseWithTransport):
3825
def test_cross_global_locations(self):
3826
l_store = config.LocationStore()
3827
l_store._load_from_string(b'''
3833
g_store = config.GlobalStore()
3834
g_store._load_from_string(b'''
3840
stack = config.LocationStack('/branch')
3841
self.assertEqual('glob-bar', stack.get('lbar', expand=True))
3842
self.assertEqual('loc-foo', stack.get('gfoo', expand=True))
3845
class TestStackExpandSectionLocals(tests.TestCaseWithTransport):
3847
def test_expand_locals_empty(self):
3848
l_store = config.LocationStore()
3849
l_store._load_from_string(b'''
3850
[/home/user/project]
3855
stack = config.LocationStack('/home/user/project/')
3856
self.assertEqual('', stack.get('base', expand=True))
3857
self.assertEqual('', stack.get('rel', expand=True))
3859
def test_expand_basename_locally(self):
3860
l_store = config.LocationStore()
3861
l_store._load_from_string(b'''
3862
[/home/user/project]
3866
stack = config.LocationStack('/home/user/project/branch')
3867
self.assertEqual('branch', stack.get('bfoo', expand=True))
3869
def test_expand_basename_locally_longer_path(self):
3870
l_store = config.LocationStore()
3871
l_store._load_from_string(b'''
3876
stack = config.LocationStack('/home/user/project/dir/branch')
3877
self.assertEqual('branch', stack.get('bfoo', expand=True))
3879
def test_expand_relpath_locally(self):
3880
l_store = config.LocationStore()
3881
l_store._load_from_string(b'''
3882
[/home/user/project]
3883
lfoo = loc-foo/{relpath}
3886
stack = config.LocationStack('/home/user/project/branch')
3887
self.assertEqual('loc-foo/branch', stack.get('lfoo', expand=True))
3889
def test_expand_relpath_unknonw_in_global(self):
3890
g_store = config.GlobalStore()
3891
g_store._load_from_string(b'''
3896
stack = config.LocationStack('/home/user/project/branch')
3897
self.assertRaises(config.ExpandingUnknownOption,
3898
stack.get, 'gfoo', expand=True)
3900
def test_expand_local_option_locally(self):
3901
l_store = config.LocationStore()
3902
l_store._load_from_string(b'''
3903
[/home/user/project]
3904
lfoo = loc-foo/{relpath}
3908
g_store = config.GlobalStore()
3909
g_store._load_from_string(b'''
3915
stack = config.LocationStack('/home/user/project/branch')
3916
self.assertEqual('glob-bar', stack.get('lbar', expand=True))
3917
self.assertEqual('loc-foo/branch', stack.get('gfoo', expand=True))
3919
def test_locals_dont_leak(self):
3920
"""Make sure we chose the right local in presence of several sections.
3922
l_store = config.LocationStore()
3923
l_store._load_from_string(b'''
3925
lfoo = loc-foo/{relpath}
3926
[/home/user/project]
3927
lfoo = loc-foo/{relpath}
3930
stack = config.LocationStack('/home/user/project/branch')
3931
self.assertEqual('loc-foo/branch', stack.get('lfoo', expand=True))
3932
stack = config.LocationStack('/home/user/bar/baz')
3933
self.assertEqual('loc-foo/bar/baz', stack.get('lfoo', expand=True))
3937
class TestStackSet(TestStackWithTransport):
3939
def test_simple_set(self):
3940
conf = self.get_stack(self)
3941
self.assertEqual(None, conf.get('foo'))
3942
conf.set('foo', 'baz')
3943
# Did we get it back ?
3944
self.assertEqual('baz', conf.get('foo'))
3946
def test_set_creates_a_new_section(self):
3947
conf = self.get_stack(self)
3948
conf.set('foo', 'baz')
3949
self.assertEqual, 'baz', conf.get('foo')
3951
def test_set_hook(self):
3955
config.ConfigHooks.install_named_hook('set', hook, None)
3956
self.assertLength(0, calls)
3957
conf = self.get_stack(self)
3958
conf.set('foo', 'bar')
3959
self.assertLength(1, calls)
3960
self.assertEqual((conf, 'foo', 'bar'), calls[0])
3963
class TestStackRemove(TestStackWithTransport):
3965
def test_remove_existing(self):
3966
conf = self.get_stack(self)
3967
conf.set('foo', 'bar')
3968
self.assertEqual('bar', conf.get('foo'))
3970
# Did we get it back ?
3971
self.assertEqual(None, conf.get('foo'))
3973
def test_remove_unknown(self):
3974
conf = self.get_stack(self)
3975
self.assertRaises(KeyError, conf.remove, 'I_do_not_exist')
3977
def test_remove_hook(self):
3981
config.ConfigHooks.install_named_hook('remove', hook, None)
3982
self.assertLength(0, calls)
3983
conf = self.get_stack(self)
3984
conf.set('foo', 'bar')
3986
self.assertLength(1, calls)
3987
self.assertEqual((conf, 'foo'), calls[0])
3990
class TestConfigGetOptions(tests.TestCaseWithTransport, TestOptionsMixin):
3993
super(TestConfigGetOptions, self).setUp()
3994
create_configs(self)
3996
def test_no_variable(self):
3997
# Using branch should query branch, locations and breezy
3998
self.assertOptions([], self.branch_config)
4000
def test_option_in_breezy(self):
4001
self.breezy_config.set_user_option('file', 'breezy')
4002
self.assertOptions([('file', 'breezy', 'DEFAULT', 'breezy')],
4005
def test_option_in_locations(self):
4006
self.locations_config.set_user_option('file', 'locations')
4008
[('file', 'locations', self.tree.basedir, 'locations')],
4009
self.locations_config)
4011
def test_option_in_branch(self):
4012
self.branch_config.set_user_option('file', 'branch')
4013
self.assertOptions([('file', 'branch', 'DEFAULT', 'branch')],
4016
def test_option_in_breezy_and_branch(self):
4017
self.breezy_config.set_user_option('file', 'breezy')
4018
self.branch_config.set_user_option('file', 'branch')
4019
self.assertOptions([('file', 'branch', 'DEFAULT', 'branch'),
4020
('file', 'breezy', 'DEFAULT', 'breezy'),],
4023
def test_option_in_branch_and_locations(self):
4024
# Hmm, locations override branch :-/
4025
self.locations_config.set_user_option('file', 'locations')
4026
self.branch_config.set_user_option('file', 'branch')
4028
[('file', 'locations', self.tree.basedir, 'locations'),
4029
('file', 'branch', 'DEFAULT', 'branch'),],
4032
def test_option_in_breezy_locations_and_branch(self):
4033
self.breezy_config.set_user_option('file', 'breezy')
4034
self.locations_config.set_user_option('file', 'locations')
4035
self.branch_config.set_user_option('file', 'branch')
4037
[('file', 'locations', self.tree.basedir, 'locations'),
4038
('file', 'branch', 'DEFAULT', 'branch'),
4039
('file', 'breezy', 'DEFAULT', 'breezy'),],
4043
class TestConfigRemoveOption(tests.TestCaseWithTransport, TestOptionsMixin):
4046
super(TestConfigRemoveOption, self).setUp()
4047
create_configs_with_file_option(self)
4049
def test_remove_in_locations(self):
4050
self.locations_config.remove_user_option('file', self.tree.basedir)
4052
[('file', 'branch', 'DEFAULT', 'branch'),
4053
('file', 'breezy', 'DEFAULT', 'breezy'),],
4056
def test_remove_in_branch(self):
4057
self.branch_config.remove_user_option('file')
4059
[('file', 'locations', self.tree.basedir, 'locations'),
4060
('file', 'breezy', 'DEFAULT', 'breezy'),],
4063
def test_remove_in_breezy(self):
4064
self.breezy_config.remove_user_option('file')
4066
[('file', 'locations', self.tree.basedir, 'locations'),
4067
('file', 'branch', 'DEFAULT', 'branch'),],
4071
class TestConfigGetSections(tests.TestCaseWithTransport):
4074
super(TestConfigGetSections, self).setUp()
4075
create_configs(self)
4077
def assertSectionNames(self, expected, conf, name=None):
4078
"""Check which sections are returned for a given config.
4080
If fallback configurations exist their sections can be included.
4082
:param expected: A list of section names.
4084
:param conf: The configuration that will be queried.
4086
:param name: An optional section name that will be passed to
4089
sections = list(conf._get_sections(name))
4090
self.assertLength(len(expected), sections)
4091
self.assertEqual(expected, [n for n, _, _ in sections])
4093
def test_breezy_default_section(self):
4094
self.assertSectionNames(['DEFAULT'], self.breezy_config)
4096
def test_locations_default_section(self):
4097
# No sections are defined in an empty file
4098
self.assertSectionNames([], self.locations_config)
4100
def test_locations_named_section(self):
4101
self.locations_config.set_user_option('file', 'locations')
4102
self.assertSectionNames([self.tree.basedir], self.locations_config)
4104
def test_locations_matching_sections(self):
4105
loc_config = self.locations_config
4106
loc_config.set_user_option('file', 'locations')
4107
# We need to cheat a bit here to create an option in sections above and
4108
# below the 'location' one.
4109
parser = loc_config._get_parser()
4110
# locations.cong deals with '/' ignoring native os.sep
4111
location_names = self.tree.basedir.split('/')
4112
parent = '/'.join(location_names[:-1])
4113
child = '/'.join(location_names + ['child'])
4115
parser[parent]['file'] = 'parent'
4117
parser[child]['file'] = 'child'
4118
self.assertSectionNames([self.tree.basedir, parent], loc_config)
4120
def test_branch_data_default_section(self):
4121
self.assertSectionNames([None],
4122
self.branch_config._get_branch_data_config())
4124
def test_branch_default_sections(self):
4125
# No sections are defined in an empty locations file
4126
self.assertSectionNames([None, 'DEFAULT'],
4128
# Unless we define an option
4129
self.branch_config._get_location_config().set_user_option(
4130
'file', 'locations')
4131
self.assertSectionNames([self.tree.basedir, None, 'DEFAULT'],
4134
def test_breezy_named_section(self):
4135
# We need to cheat as the API doesn't give direct access to sections
4136
# other than DEFAULT.
4137
self.breezy_config.set_alias('breezy', 'bzr')
4138
self.assertSectionNames(['ALIASES'], self.breezy_config, 'ALIASES')
4141
class TestSharedStores(tests.TestCaseInTempDir):
4143
def test_breezy_conf_shared(self):
4144
g1 = config.GlobalStack()
4145
g2 = config.GlobalStack()
4146
# The two stacks share the same store
4147
self.assertIs(g1.store, g2.store)
4150
class TestAuthenticationConfigFilePermissions(tests.TestCaseInTempDir):
4151
"""Test warning for permissions of authentication.conf."""
4154
super(TestAuthenticationConfigFilePermissions, self).setUp()
4155
self.path = osutils.pathjoin(self.test_dir, 'authentication.conf')
4156
with open(self.path, 'wb') as f:
4157
f.write(b"""[broken]
4160
port=port # Error: Not an int
4162
self.overrideAttr(config, 'authentication_config_filename',
4164
osutils.chmod_if_possible(self.path, 0o755)
4166
def test_check_warning(self):
4167
conf = config.AuthenticationConfig()
4168
self.assertEqual(conf._filename, self.path)
4169
self.assertContainsRe(self.get_log(),
4170
'Saved passwords may be accessible by other users.')
4172
def test_check_suppressed_warning(self):
4173
global_config = config.GlobalConfig()
4174
global_config.set_user_option('suppress_warnings',
4175
'insecure_permissions')
4176
conf = config.AuthenticationConfig()
4177
self.assertEqual(conf._filename, self.path)
4178
self.assertNotContainsRe(self.get_log(),
4179
'Saved passwords may be accessible by other users.')
1315
4182
class TestAuthenticationConfigFile(tests.TestCase):
1316
4183
"""Test the authentication.conf file matching"""