2615
2612
self.assertEquals(config._NewlyCreatedOption, section.orig['foo'])
2618
class TestCommandLineSection(tests.TestCase):
2615
class TestCommandLineStore(tests.TestCase):
2620
2617
def setUp(self):
2621
super(TestCommandLineSection, self).setUp()
2622
self.section = config.CommandLineSection()
2618
super(TestCommandLineStore, self).setUp()
2619
self.store = config.CommandLineStore()
2621
def get_section(self):
2622
"""Get the unique section for the command line overrides."""
2623
sections = list(self.store.get_sections())
2624
self.assertLength(1, sections)
2625
store, section = sections[0]
2626
self.assertEquals(self.store, store)
2624
2629
def test_no_override(self):
2625
self.section._from_cmdline([])
2626
# FIXME: we want some iterator over all options, failing that, we peek
2627
# under the cover -- vila 2011-09026
2628
self.assertLength(0, self.section.options)
2630
self.store._from_cmdline([])
2631
section = self.get_section()
2632
self.assertLength(0, list(section.iter_option_names()))
2630
2634
def test_simple_override(self):
2631
self.section._from_cmdline(['a=b'])
2632
self.assertEqual('b', self.section.get('a'))
2635
self.store._from_cmdline(['a=b'])
2636
section = self.get_section()
2637
self.assertEqual('b', section.get('a'))
2634
2639
def test_list_override(self):
2635
self.section._from_cmdline(['l=1,2,3'])
2636
val = self.section.get('l')
2640
self.store._from_cmdline(['l=1,2,3'])
2641
val = self.get_section().get('l')
2637
2642
self.assertEqual('1,2,3', val)
2638
# Reminder: lists should registered as such explicitely, otherwise the
2639
# conversion needs to be done afterwards.
2643
# Reminder: lists should be registered as such explicitely, otherwise
2644
# the conversion needs to be done afterwards.
2640
2645
self.assertEqual(['1', '2', '3'], config.list_from_store(val))
2642
2647
def test_multiple_overrides(self):
2643
self.section._from_cmdline(['a=b', 'x=y'])
2644
self.assertEquals('b', self.section.get('a'))
2645
self.assertEquals('y', self.section.get('x'))
2648
self.store._from_cmdline(['a=b', 'x=y'])
2649
section = self.get_section()
2650
self.assertEquals('b', section.get('a'))
2651
self.assertEquals('y', section.get('x'))
2647
2653
def test_wrong_syntax(self):
2648
2654
self.assertRaises(errors.BzrCommandError,
2649
self.section._from_cmdline, ['a=b', 'c'])
2655
self.store._from_cmdline, ['a=b', 'c'])
2652
2658
class TestStore(tests.TestCaseWithTransport):
2654
def assertSectionContent(self, expected, section):
2660
def assertSectionContent(self, expected, (store, section)):
2655
2661
"""Assert that some options have the proper values in a section."""
2656
2662
expected_name, expected_options = expected
2657
2663
self.assertEquals(expected_name, section.id)
2725
2731
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
2726
2732
t = self.get_transport()
2727
2733
t.put_bytes('foo.conf', 'user=foo\n#%s\n' % (self.invalid_utf8_char,))
2728
store = config.IniFileStore(t, 'foo.conf')
2734
store = config.TransportIniFileStore(t, 'foo.conf')
2729
2735
self.assertRaises(errors.ConfigContentError, store.load)
2731
2737
def test_load_erroneous_content(self):
2732
2738
"""Ensure we display a proper error on content that can't be parsed."""
2733
2739
t = self.get_transport()
2734
2740
t.put_bytes('foo.conf', '[open_section\n')
2735
store = config.IniFileStore(t, 'foo.conf')
2741
store = config.TransportIniFileStore(t, 'foo.conf')
2736
2742
self.assertRaises(errors.ParseConfigError, store.load)
2738
2744
def test_load_permission_denied(self):
2907
2913
self.assertEquals((store,), calls[0])
2910
class TestIniFileStore(TestStore):
2916
class TestTransportIniFileStore(TestStore):
2912
2918
def test_loading_unknown_file_fails(self):
2913
store = config.IniFileStore(self.get_transport(), 'I-do-not-exist')
2919
store = config.TransportIniFileStore(self.get_transport(),
2914
2921
self.assertRaises(errors.NoSuchFile, store.load)
2916
2923
def test_invalid_content(self):
2917
store = config.IniFileStore(self.get_transport(), 'foo.conf', )
2924
store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
2918
2925
self.assertEquals(False, store.is_loaded())
2919
2926
exc = self.assertRaises(
2920
2927
errors.ParseConfigError, store._load_from_string,
3171
3178
self.assertEquals(['/foo', '/foo/baz', '/foo/bar', '/foo/bar/baz',
3173
[section.id for section in store.get_sections()])
3180
[section.id for _, section in store.get_sections()])
3174
3181
matcher = config.LocationMatcher(store, '/foo/bar/quux')
3175
sections = list(matcher.get_sections())
3182
sections = [section for s, section in matcher.get_sections()]
3176
3183
self.assertEquals([3, 2],
3177
3184
[section.length for section in sections])
3178
3185
self.assertEquals(['/foo/bar', '/foo'],
3189
3196
section=/foo/bar
3191
3198
self.assertEquals(['/foo', '/foo/bar'],
3192
[section.id for section in store.get_sections()])
3199
[section.id for _, section in store.get_sections()])
3193
3200
matcher = config.LocationMatcher(store, '/foo/bar/baz')
3194
sections = list(matcher.get_sections())
3201
sections = [section for s, section in matcher.get_sections()]
3195
3202
self.assertEquals([3, 2],
3196
3203
[section.length for section in sections])
3197
3204
self.assertEquals(['/foo/bar', '/foo'],
3345
3352
self.assertEquals((self.conf, 'foo', 'bar'), calls[0])
3348
class TestStackGetWithConverter(TestStackGet):
3355
class TestStackGetWithConverter(tests.TestCaseWithTransport):
3350
3357
def setUp(self):
3351
3358
super(TestStackGetWithConverter, self).setUp()
3352
3359
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3353
3360
self.registry = config.option_registry
3361
# We just want a simple stack with a simple store so we can inject
3362
# whatever content the tests need without caring about what section
3363
# names are valid for a given store/stack.
3364
store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
3365
self.conf = config.Stack([store.get_sections], store)
3355
3367
def register_bool_option(self, name, default=None, default_from_env=None):
3356
3368
b = config.Option(name, help='A boolean.',
3661
3673
class TestStackExpandSectionLocals(tests.TestCaseWithTransport):
3675
def test_expand_locals_empty(self):
3676
l_store = config.LocationStore()
3677
l_store._load_from_string('''
3678
[/home/user/project]
3683
stack = config.LocationStack('/home/user/project/')
3684
self.assertEquals('', stack.get('base', expand=True))
3685
self.assertEquals('', stack.get('rel', expand=True))
3687
def test_expand_basename_locally(self):
3688
l_store = config.LocationStore()
3689
l_store._load_from_string('''
3690
[/home/user/project]
3694
stack = config.LocationStack('/home/user/project/branch')
3695
self.assertEquals('branch', stack.get('bfoo', expand=True))
3697
def test_expand_basename_locally_longer_path(self):
3698
l_store = config.LocationStore()
3699
l_store._load_from_string('''
3704
stack = config.LocationStack('/home/user/project/dir/branch')
3705
self.assertEquals('branch', stack.get('bfoo', expand=True))
3663
3707
def test_expand_relpath_locally(self):
3664
3708
l_store = config.LocationStore()
3665
3709
l_store._load_from_string('''