~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Patch Queue Manager
  • Date: 2011-11-17 17:41:45 UTC
  • mfrom: (6260.3.3 config-command)
  • Revision ID: pqm@pqm.ubuntu.com-20111117174145-2nb7jko9c5t7llcb
(vila) Switch ``bzr config`` to the stack-based config implementation
 (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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)
2983
2989
 
2984
2990
    def setUp(self):
2985
2991
        super(TestConcurrentStoreUpdates, self).setUp()
2986
 
        self._content = 'one=1\ntwo=2\n'
2987
2992
        self.stack = self.get_stack(self)
2988
2993
        if not isinstance(self.stack, config._CompatibleStack):
2989
2994
            raise tests.TestNotApplicable(
2990
2995
                '%s is not meant to be compatible with the old config design'
2991
2996
                % (self.stack,))
2992
 
        self.stack.store._load_from_string(self._content)
 
2997
        self.stack.set('one', '1')
 
2998
        self.stack.set('two', '2')
2993
2999
        # Flush the store
2994
3000
        self.stack.store.save()
2995
3001
 
3170
3176
''')
3171
3177
        self.assertEquals(['/foo', '/foo/baz', '/foo/bar', '/foo/bar/baz',
3172
3178
                           '/quux/quux'],
3173
 
                          [section.id for section in store.get_sections()])
 
3179
                          [section.id for _, section in store.get_sections()])
3174
3180
        matcher = config.LocationMatcher(store, '/foo/bar/quux')
3175
 
        sections = list(matcher.get_sections())
 
3181
        sections = [section for s, section in matcher.get_sections()]
3176
3182
        self.assertEquals([3, 2],
3177
3183
                          [section.length for section in sections])
3178
3184
        self.assertEquals(['/foo/bar', '/foo'],
3189
3195
section=/foo/bar
3190
3196
''')
3191
3197
        self.assertEquals(['/foo', '/foo/bar'],
3192
 
                          [section.id for section in store.get_sections()])
 
3198
                          [section.id for _, section in store.get_sections()])
3193
3199
        matcher = config.LocationMatcher(store, '/foo/bar/baz')
3194
 
        sections = list(matcher.get_sections())
 
3200
        sections = [section for s, section in matcher.get_sections()]
3195
3201
        self.assertEquals([3, 2],
3196
3202
                          [section.length for section in sections])
3197
3203
        self.assertEquals(['/foo/bar', '/foo'],
3210
3216
        matcher = config.LocationMatcher(store, 'dir/subdir')
3211
3217
        sections = list(matcher.get_sections())
3212
3218
        self.assertLength(1, sections)
3213
 
        self.assertEquals('bar/dir/subdir', sections[0].get('foo'))
 
3219
        self.assertEquals('bar/dir/subdir', sections[0][1].get('foo'))
3214
3220
 
3215
3221
    def test_file_urls_are_normalized(self):
3216
3222
        store = self.get_store(self)
3333
3339
        self.assertEquals(None, self.conf.get('foo'))
3334
3340
 
3335
3341
    def test_get_hook(self):
3336
 
        self.conf.store._load_from_string('foo=bar')
 
3342
        self.conf.set('foo', 'bar')
3337
3343
        calls = []
3338
3344
        def hook(*args):
3339
3345
            calls.append(args)
3345
3351
        self.assertEquals((self.conf, 'foo', 'bar'), calls[0])
3346
3352
 
3347
3353
 
3348
 
class TestStackGetWithConverter(TestStackGet):
 
3354
class TestStackGetWithConverter(tests.TestCaseWithTransport):
3349
3355
 
3350
3356
    def setUp(self):
3351
3357
        super(TestStackGetWithConverter, self).setUp()
3352
3358
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3353
3359
        self.registry = config.option_registry
 
3360
        # We just want a simple stack with a simple store so we can inject
 
3361
        # whatever content the tests need without caring about what section
 
3362
        # names are valid for a given store/stack.
 
3363
        store = config.IniFileStore(self.get_transport(), 'foo.conf')
 
3364
        self.conf = config.Stack([store.get_sections], store)
3354
3365
 
3355
3366
    def register_bool_option(self, name, default=None, default_from_env=None):
3356
3367
        b = config.Option(name, help='A boolean.',
3722
3733
 
3723
3734
    def test_simple_set(self):
3724
3735
        conf = self.get_stack(self)
3725
 
        conf.store._load_from_string('foo=bar')
3726
 
        self.assertEquals('bar', conf.get('foo'))
 
3736
        self.assertEquals(None, conf.get('foo'))
3727
3737
        conf.set('foo', 'baz')
3728
3738
        # Did we get it back ?
3729
3739
        self.assertEquals('baz', conf.get('foo'))