~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Martin Packman
  • Date: 2011-11-28 19:07:58 UTC
  • mfrom: (6318 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6319.
  • Revision ID: martin.packman@canonical.com-20111128190758-5gj44o5uzwz5sjfq
Merge bzr.dev to resolve conflicts with updated registry help strings

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
 
68
68
# Register helpers to build stores
69
69
config.test_store_builder_registry.register(
70
 
    'configobj', lambda test: config.IniFileStore(test.get_transport(),
71
 
                                                  'configobj.conf'))
 
70
    'configobj', lambda test: config.TransportIniFileStore(
 
71
        test.get_transport(), 'configobj.conf'))
72
72
config.test_store_builder_registry.register(
73
73
    'bazaar', lambda test: config.GlobalStore())
74
74
config.test_store_builder_registry.register(
2565
2565
    scenarios = [('mutable',
2566
2566
                  {'get_section':
2567
2567
                       lambda opts: config.MutableSection('myID', opts)},),
2568
 
                 ('cmdline',
2569
 
                  {'get_section':
2570
 
                       lambda opts: config.CommandLineSection(opts)},),
2571
2568
        ]
2572
2569
 
2573
2570
    def test_set(self):
2615
2612
        self.assertEquals(config._NewlyCreatedOption, section.orig['foo'])
2616
2613
 
2617
2614
 
2618
 
class TestCommandLineSection(tests.TestCase):
 
2615
class TestCommandLineStore(tests.TestCase):
2619
2616
 
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()
 
2620
 
 
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)
 
2627
        return section
2623
2628
 
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()))
2629
2633
 
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'))
2633
2638
 
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))
2641
2646
 
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'))
2646
2652
 
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'])
2650
2656
 
2651
2657
 
2652
2658
class TestStore(tests.TestCaseWithTransport):
2653
2659
 
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)
2716
2722
        utf8_content = unicode_content.encode('utf8')
2717
2723
        # Store the raw content in the config file
2718
2724
        t.put_bytes('foo.conf', utf8_content)
2719
 
        store = config.IniFileStore(t, 'foo.conf')
 
2725
        store = config.TransportIniFileStore(t, 'foo.conf')
2720
2726
        store.load()
2721
2727
        stack = config.Stack([store.get_sections], store)
2722
2728
        self.assertEquals(unicode_user, stack.get('user'))
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)
2730
2736
 
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)
2737
2743
 
2738
2744
    def test_load_permission_denied(self):
2747
2753
        def get_bytes(relpath):
2748
2754
            raise errors.PermissionDenied(relpath, "")
2749
2755
        t.get_bytes = get_bytes
2750
 
        store = config.IniFileStore(t, 'foo.conf')
 
2756
        store = config.TransportIniFileStore(t, 'foo.conf')
2751
2757
        self.assertRaises(errors.PermissionDenied, store.load)
2752
2758
        self.assertEquals(
2753
2759
            warnings,
2907
2913
        self.assertEquals((store,), calls[0])
2908
2914
 
2909
2915
 
2910
 
class TestIniFileStore(TestStore):
 
2916
class TestTransportIniFileStore(TestStore):
2911
2917
 
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(),
 
2920
            'I-do-not-exist')
2914
2921
        self.assertRaises(errors.NoSuchFile, store.load)
2915
2922
 
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,
2928
2935
        # option names share the same name space...)
2929
2936
        # FIXME: This should be fixed by forbidding dicts as values ?
2930
2937
        # -- vila 2011-04-05
2931
 
        store = config.IniFileStore(self.get_transport(), 'foo.conf', )
 
2938
        store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
2932
2939
        store._load_from_string('''
2933
2940
foo=bar
2934
2941
l=1,2
2983
2990
 
2984
2991
    def setUp(self):
2985
2992
        super(TestConcurrentStoreUpdates, self).setUp()
2986
 
        self._content = 'one=1\ntwo=2\n'
2987
2993
        self.stack = self.get_stack(self)
2988
2994
        if not isinstance(self.stack, config._CompatibleStack):
2989
2995
            raise tests.TestNotApplicable(
2990
2996
                '%s is not meant to be compatible with the old config design'
2991
2997
                % (self.stack,))
2992
 
        self.stack.store._load_from_string(self._content)
 
2998
        self.stack.set('one', '1')
 
2999
        self.stack.set('two', '2')
2993
3000
        # Flush the store
2994
3001
        self.stack.store.save()
2995
3002
 
3170
3177
''')
3171
3178
        self.assertEquals(['/foo', '/foo/baz', '/foo/bar', '/foo/bar/baz',
3172
3179
                           '/quux/quux'],
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
3190
3197
''')
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'],
3210
3217
        matcher = config.LocationMatcher(store, 'dir/subdir')
3211
3218
        sections = list(matcher.get_sections())
3212
3219
        self.assertLength(1, sections)
3213
 
        self.assertEquals('bar/dir/subdir', sections[0].get('foo'))
 
3220
        self.assertEquals('bar/dir/subdir', sections[0][1].get('foo'))
3214
3221
 
3215
3222
    def test_file_urls_are_normalized(self):
3216
3223
        store = self.get_store(self)
3333
3340
        self.assertEquals(None, self.conf.get('foo'))
3334
3341
 
3335
3342
    def test_get_hook(self):
3336
 
        self.conf.store._load_from_string('foo=bar')
 
3343
        self.conf.set('foo', 'bar')
3337
3344
        calls = []
3338
3345
        def hook(*args):
3339
3346
            calls.append(args)
3345
3352
        self.assertEquals((self.conf, 'foo', 'bar'), calls[0])
3346
3353
 
3347
3354
 
3348
 
class TestStackGetWithConverter(TestStackGet):
 
3355
class TestStackGetWithConverter(tests.TestCaseWithTransport):
3349
3356
 
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)
3354
3366
 
3355
3367
    def register_bool_option(self, name, default=None, default_from_env=None):
3356
3368
        b = config.Option(name, help='A boolean.',
3660
3672
 
3661
3673
class TestStackExpandSectionLocals(tests.TestCaseWithTransport):
3662
3674
 
 
3675
    def test_expand_locals_empty(self):
 
3676
        l_store = config.LocationStore()
 
3677
        l_store._load_from_string('''
 
3678
[/home/user/project]
 
3679
base = {basename}
 
3680
rel = {relpath}
 
3681
''')
 
3682
        l_store.save()
 
3683
        stack = config.LocationStack('/home/user/project/')
 
3684
        self.assertEquals('', stack.get('base', expand=True))
 
3685
        self.assertEquals('', stack.get('rel', expand=True))
 
3686
 
 
3687
    def test_expand_basename_locally(self):
 
3688
        l_store = config.LocationStore()
 
3689
        l_store._load_from_string('''
 
3690
[/home/user/project]
 
3691
bfoo = {basename}
 
3692
''')
 
3693
        l_store.save()
 
3694
        stack = config.LocationStack('/home/user/project/branch')
 
3695
        self.assertEquals('branch', stack.get('bfoo', expand=True))
 
3696
 
 
3697
    def test_expand_basename_locally_longer_path(self):
 
3698
        l_store = config.LocationStore()
 
3699
        l_store._load_from_string('''
 
3700
[/home/user]
 
3701
bfoo = {basename}
 
3702
''')
 
3703
        l_store.save()
 
3704
        stack = config.LocationStack('/home/user/project/dir/branch')
 
3705
        self.assertEquals('branch', stack.get('bfoo', expand=True))
 
3706
 
3663
3707
    def test_expand_relpath_locally(self):
3664
3708
        l_store = config.LocationStore()
3665
3709
        l_store._load_from_string('''
3722
3766
 
3723
3767
    def test_simple_set(self):
3724
3768
        conf = self.get_stack(self)
3725
 
        conf.store._load_from_string('foo=bar')
3726
 
        self.assertEquals('bar', conf.get('foo'))
 
3769
        self.assertEquals(None, conf.get('foo'))
3727
3770
        conf.set('foo', 'baz')
3728
3771
        # Did we get it back ?
3729
3772
        self.assertEquals('baz', conf.get('foo'))