~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-12-21 17:42:34 UTC
  • mfrom: (6393.1.2 memory-stack)
  • Revision ID: pqm@pqm.ubuntu.com-20111221174234-06h0xlw6qqb03al2
(vila) Provides config.MemoryStack to simplify configuration setup in tests
 (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import threading
23
23
 
24
24
 
 
25
import testtools
25
26
from testtools import matchers
26
27
 
27
28
#import bzrlib specific imports here
3356
3357
        self.assertLength(0, sections)
3357
3358
 
3358
3359
 
3359
 
class TestStackGet(tests.TestCase):
3360
 
 
3361
 
    # FIXME: This should be parametrized for all known Stack or dedicated
3362
 
    # paramerized tests created to avoid bloating -- vila 2011-03-31
3363
 
 
3364
 
    def overrideOptionRegistry(self):
 
3360
class TestBaseStackGet(tests.TestCase):
 
3361
 
 
3362
    def setUp(self):
 
3363
        super(TestBaseStackGet, self).setUp()
3365
3364
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3366
3365
 
3367
 
    def test_single_config_get(self):
3368
 
        conf = dict(foo='bar')
3369
 
        conf_stack = config.Stack([conf])
3370
 
        self.assertEquals('bar', conf_stack.get('foo'))
 
3366
    def test_get_first_definition(self):
 
3367
        store1 = config.IniFileStore()
 
3368
        store1._load_from_string('foo=bar')
 
3369
        store2 = config.IniFileStore()
 
3370
        store2._load_from_string('foo=baz')
 
3371
        conf = config.Stack([store1.get_sections, store2.get_sections])
 
3372
        self.assertEquals('bar', conf.get('foo'))
3371
3373
 
3372
3374
    def test_get_with_registered_default_value(self):
3373
 
        conf_stack = config.Stack([dict()])
3374
 
        opt = config.Option('foo', default='bar')
3375
 
        self.overrideOptionRegistry()
3376
 
        config.option_registry.register('foo', opt)
 
3375
        config.option_registry.register(config.Option('foo', default='bar'))
 
3376
        conf_stack = config.Stack([])
3377
3377
        self.assertEquals('bar', conf_stack.get('foo'))
3378
3378
 
3379
3379
    def test_get_without_registered_default_value(self):
3380
 
        conf_stack = config.Stack([dict()])
3381
 
        opt = config.Option('foo')
3382
 
        self.overrideOptionRegistry()
3383
 
        config.option_registry.register('foo', opt)
 
3380
        config.option_registry.register(config.Option('foo'))
 
3381
        conf_stack = config.Stack([])
3384
3382
        self.assertEquals(None, conf_stack.get('foo'))
3385
3383
 
3386
3384
    def test_get_without_default_value_for_not_registered(self):
3387
 
        conf_stack = config.Stack([dict()])
3388
 
        opt = config.Option('foo')
3389
 
        self.overrideOptionRegistry()
 
3385
        conf_stack = config.Stack([])
3390
3386
        self.assertEquals(None, conf_stack.get('foo'))
3391
3387
 
3392
 
    def test_get_first_definition(self):
3393
 
        conf1 = dict(foo='bar')
3394
 
        conf2 = dict(foo='baz')
3395
 
        conf_stack = config.Stack([conf1, conf2])
3396
 
        self.assertEquals('bar', conf_stack.get('foo'))
3397
 
 
3398
 
    def test_get_embedded_definition(self):
3399
 
        conf1 = dict(yy='12')
3400
 
        conf2 = config.Stack([dict(xx='42'), dict(foo='baz')])
3401
 
        conf_stack = config.Stack([conf1, conf2])
3402
 
        self.assertEquals('baz', conf_stack.get('foo'))
3403
 
 
3404
3388
    def test_get_for_empty_section_callable(self):
3405
3389
        conf_stack = config.Stack([lambda : []])
3406
3390
        self.assertEquals(None, conf_stack.get('foo'))
3407
3391
 
3408
3392
    def test_get_for_broken_callable(self):
3409
3393
        # Trying to use and invalid callable raises an exception on first use
3410
 
        conf_stack = config.Stack([lambda : object()])
 
3394
        conf_stack = config.Stack([object])
3411
3395
        self.assertRaises(TypeError, conf_stack.get, 'foo')
3412
3396
 
3413
3397
 
 
3398
class TestMemoryStack(tests.TestCase):
 
3399
 
 
3400
    def test_get(self):
 
3401
        conf = config.MemoryStack('foo=bar')
 
3402
        self.assertEquals('bar', conf.get('foo'))
 
3403
 
 
3404
    def test_set(self):
 
3405
        conf = config.MemoryStack('foo=bar')
 
3406
        conf.set('foo', 'baz')
 
3407
        self.assertEquals('baz', conf.get('foo'))
 
3408
 
 
3409
    def test_no_content(self):
 
3410
        conf = config.MemoryStack()
 
3411
        # No content means no loading
 
3412
        self.assertFalse(conf.store.is_loaded())
 
3413
        self.assertRaises(NotImplementedError, conf.get, 'foo')
 
3414
        # But a content can still be provided
 
3415
        conf.store._load_from_string('foo=bar')
 
3416
        self.assertEquals('bar', conf.get('foo'))
 
3417
 
 
3418
 
3414
3419
class TestStackWithTransport(tests.TestCaseWithTransport):
3415
3420
 
3416
3421
    scenarios = [(key, {'get_stack': builder}) for key, builder
3446
3451
        self.assertEquals((self.conf, 'foo', 'bar'), calls[0])
3447
3452
 
3448
3453
 
3449
 
class TestStackGetWithConverter(tests.TestCaseWithTransport):
 
3454
class TestStackGetWithConverter(tests.TestCase):
3450
3455
 
3451
3456
    def setUp(self):
3452
3457
        super(TestStackGetWithConverter, self).setUp()
3453
3458
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3454
3459
        self.registry = config.option_registry
3455
 
        # We just want a simple stack with a simple store so we can inject
3456
 
        # whatever content the tests need without caring about what section
3457
 
        # names are valid for a given store/stack.
3458
 
        store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
3459
 
        self.conf = config.Stack([store.get_sections], store)
 
3460
 
 
3461
    def get_conf(self, content=None):
 
3462
        return config.MemoryStack(content)
3460
3463
 
3461
3464
    def register_bool_option(self, name, default=None, default_from_env=None):
3462
3465
        b = config.Option(name, help='A boolean.',
3466
3469
 
3467
3470
    def test_get_default_bool_None(self):
3468
3471
        self.register_bool_option('foo')
3469
 
        self.assertEquals(None, self.conf.get('foo'))
 
3472
        conf = self.get_conf('')
 
3473
        self.assertEquals(None, conf.get('foo'))
3470
3474
 
3471
3475
    def test_get_default_bool_True(self):
3472
3476
        self.register_bool_option('foo', u'True')
3473
 
        self.assertEquals(True, self.conf.get('foo'))
 
3477
        conf = self.get_conf('')
 
3478
        self.assertEquals(True, conf.get('foo'))
3474
3479
 
3475
3480
    def test_get_default_bool_False(self):
3476
3481
        self.register_bool_option('foo', False)
3477
 
        self.assertEquals(False, self.conf.get('foo'))
 
3482
        conf = self.get_conf('')
 
3483
        self.assertEquals(False, conf.get('foo'))
3478
3484
 
3479
3485
    def test_get_default_bool_False_as_string(self):
3480
3486
        self.register_bool_option('foo', u'False')
3481
 
        self.assertEquals(False, self.conf.get('foo'))
 
3487
        conf = self.get_conf('')
 
3488
        self.assertEquals(False, conf.get('foo'))
3482
3489
 
3483
3490
    def test_get_default_bool_from_env_converted(self):
3484
3491
        self.register_bool_option('foo', u'True', default_from_env=['FOO'])
3485
3492
        self.overrideEnv('FOO', 'False')
3486
 
        self.assertEquals(False, self.conf.get('foo'))
 
3493
        conf = self.get_conf('')
 
3494
        self.assertEquals(False, conf.get('foo'))
3487
3495
 
3488
3496
    def test_get_default_bool_when_conversion_fails(self):
3489
3497
        self.register_bool_option('foo', default='True')
3490
 
        self.conf.store._load_from_string('foo=invalid boolean')
3491
 
        self.assertEquals(True, self.conf.get('foo'))
 
3498
        conf = self.get_conf('foo=invalid boolean')
 
3499
        self.assertEquals(True, conf.get('foo'))
3492
3500
 
3493
3501
    def register_integer_option(self, name,
3494
3502
                                default=None, default_from_env=None):
3499
3507
 
3500
3508
    def test_get_default_integer_None(self):
3501
3509
        self.register_integer_option('foo')
3502
 
        self.assertEquals(None, self.conf.get('foo'))
 
3510
        conf = self.get_conf('')
 
3511
        self.assertEquals(None, conf.get('foo'))
3503
3512
 
3504
3513
    def test_get_default_integer(self):
3505
3514
        self.register_integer_option('foo', 42)
3506
 
        self.assertEquals(42, self.conf.get('foo'))
 
3515
        conf = self.get_conf('')
 
3516
        self.assertEquals(42, conf.get('foo'))
3507
3517
 
3508
3518
    def test_get_default_integer_as_string(self):
3509
3519
        self.register_integer_option('foo', u'42')
3510
 
        self.assertEquals(42, self.conf.get('foo'))
 
3520
        conf = self.get_conf('')
 
3521
        self.assertEquals(42, conf.get('foo'))
3511
3522
 
3512
3523
    def test_get_default_integer_from_env(self):
3513
3524
        self.register_integer_option('foo', default_from_env=['FOO'])
3514
3525
        self.overrideEnv('FOO', '18')
3515
 
        self.assertEquals(18, self.conf.get('foo'))
 
3526
        conf = self.get_conf('')
 
3527
        self.assertEquals(18, conf.get('foo'))
3516
3528
 
3517
3529
    def test_get_default_integer_when_conversion_fails(self):
3518
3530
        self.register_integer_option('foo', default='12')
3519
 
        self.conf.store._load_from_string('foo=invalid integer')
3520
 
        self.assertEquals(12, self.conf.get('foo'))
 
3531
        conf = self.get_conf('foo=invalid integer')
 
3532
        self.assertEquals(12, conf.get('foo'))
3521
3533
 
3522
3534
    def register_list_option(self, name, default=None, default_from_env=None):
3523
3535
        l = config.Option(name, help='A list.',
3527
3539
 
3528
3540
    def test_get_default_list_None(self):
3529
3541
        self.register_list_option('foo')
3530
 
        self.assertEquals(None, self.conf.get('foo'))
 
3542
        conf = self.get_conf('')
 
3543
        self.assertEquals(None, conf.get('foo'))
3531
3544
 
3532
3545
    def test_get_default_list_empty(self):
3533
3546
        self.register_list_option('foo', '')
3534
 
        self.assertEquals([], self.conf.get('foo'))
 
3547
        conf = self.get_conf('')
 
3548
        self.assertEquals([], conf.get('foo'))
3535
3549
 
3536
3550
    def test_get_default_list_from_env(self):
3537
3551
        self.register_list_option('foo', default_from_env=['FOO'])
3538
3552
        self.overrideEnv('FOO', '')
3539
 
        self.assertEquals([], self.conf.get('foo'))
 
3553
        conf = self.get_conf('')
 
3554
        self.assertEquals([], conf.get('foo'))
3540
3555
 
3541
3556
    def test_get_with_list_converter_no_item(self):
3542
3557
        self.register_list_option('foo', None)
3543
 
        self.conf.store._load_from_string('foo=,')
3544
 
        self.assertEquals([], self.conf.get('foo'))
 
3558
        conf = self.get_conf('foo=,')
 
3559
        self.assertEquals([], conf.get('foo'))
3545
3560
 
3546
3561
    def test_get_with_list_converter_many_items(self):
3547
3562
        self.register_list_option('foo', None)
3548
 
        self.conf.store._load_from_string('foo=m,o,r,e')
3549
 
        self.assertEquals(['m', 'o', 'r', 'e'], self.conf.get('foo'))
 
3563
        conf = self.get_conf('foo=m,o,r,e')
 
3564
        self.assertEquals(['m', 'o', 'r', 'e'], conf.get('foo'))
3550
3565
 
3551
3566
    def test_get_with_list_converter_embedded_spaces_many_items(self):
3552
3567
        self.register_list_option('foo', None)
3553
 
        self.conf.store._load_from_string('foo=" bar", "baz "')
3554
 
        self.assertEquals([' bar', 'baz '], self.conf.get('foo'))
 
3568
        conf = self.get_conf('foo=" bar", "baz "')
 
3569
        self.assertEquals([' bar', 'baz '], conf.get('foo'))
3555
3570
 
3556
3571
    def test_get_with_list_converter_stripped_spaces_many_items(self):
3557
3572
        self.register_list_option('foo', None)
3558
 
        self.conf.store._load_from_string('foo= bar ,  baz ')
3559
 
        self.assertEquals(['bar', 'baz'], self.conf.get('foo'))
 
3573
        conf = self.get_conf('foo= bar ,  baz ')
 
3574
        self.assertEquals(['bar', 'baz'], conf.get('foo'))
3560
3575
 
3561
3576
 
3562
3577
class TestIterOptionRefs(tests.TestCase):