~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Vincent Ladeuil
  • Date: 2011-12-21 14:25:26 UTC
  • mto: This revision was merged to the branch mainline in revision 6397.
  • Revision ID: v.ladeuil+lp@free.fr-20111221142526-pnwau0xnalimujts
Provides MemoryStack to simplify configuration setup in tests

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
        with testtools.ExpectedException(NotImplementedError):
 
3414
            conf.get('foo')
 
3415
        # But a content can still be provided
 
3416
        conf.store._load_from_string('foo=bar')
 
3417
        self.assertEquals('bar', conf.get('foo'))
 
3418
 
 
3419
 
3414
3420
class TestStackWithTransport(tests.TestCaseWithTransport):
3415
3421
 
3416
3422
    scenarios = [(key, {'get_stack': builder}) for key, builder
3446
3452
        self.assertEquals((self.conf, 'foo', 'bar'), calls[0])
3447
3453
 
3448
3454
 
3449
 
class TestStackGetWithConverter(tests.TestCaseWithTransport):
 
3455
class TestStackGetWithConverter(tests.TestCase):
3450
3456
 
3451
3457
    def setUp(self):
3452
3458
        super(TestStackGetWithConverter, self).setUp()
3453
3459
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
3454
3460
        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)
 
3461
 
 
3462
    def get_conf(self, content=None):
 
3463
        return config.MemoryStack(content)
3460
3464
 
3461
3465
    def register_bool_option(self, name, default=None, default_from_env=None):
3462
3466
        b = config.Option(name, help='A boolean.',
3466
3470
 
3467
3471
    def test_get_default_bool_None(self):
3468
3472
        self.register_bool_option('foo')
3469
 
        self.assertEquals(None, self.conf.get('foo'))
 
3473
        conf = self.get_conf('')
 
3474
        self.assertEquals(None, conf.get('foo'))
3470
3475
 
3471
3476
    def test_get_default_bool_True(self):
3472
3477
        self.register_bool_option('foo', u'True')
3473
 
        self.assertEquals(True, self.conf.get('foo'))
 
3478
        conf = self.get_conf('')
 
3479
        self.assertEquals(True, conf.get('foo'))
3474
3480
 
3475
3481
    def test_get_default_bool_False(self):
3476
3482
        self.register_bool_option('foo', False)
3477
 
        self.assertEquals(False, self.conf.get('foo'))
 
3483
        conf = self.get_conf('')
 
3484
        self.assertEquals(False, conf.get('foo'))
3478
3485
 
3479
3486
    def test_get_default_bool_False_as_string(self):
3480
3487
        self.register_bool_option('foo', u'False')
3481
 
        self.assertEquals(False, self.conf.get('foo'))
 
3488
        conf = self.get_conf('')
 
3489
        self.assertEquals(False, conf.get('foo'))
3482
3490
 
3483
3491
    def test_get_default_bool_from_env_converted(self):
3484
3492
        self.register_bool_option('foo', u'True', default_from_env=['FOO'])
3485
3493
        self.overrideEnv('FOO', 'False')
3486
 
        self.assertEquals(False, self.conf.get('foo'))
 
3494
        conf = self.get_conf('')
 
3495
        self.assertEquals(False, conf.get('foo'))
3487
3496
 
3488
3497
    def test_get_default_bool_when_conversion_fails(self):
3489
3498
        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'))
 
3499
        conf = self.get_conf('foo=invalid boolean')
 
3500
        self.assertEquals(True, conf.get('foo'))
3492
3501
 
3493
3502
    def register_integer_option(self, name,
3494
3503
                                default=None, default_from_env=None):
3499
3508
 
3500
3509
    def test_get_default_integer_None(self):
3501
3510
        self.register_integer_option('foo')
3502
 
        self.assertEquals(None, self.conf.get('foo'))
 
3511
        conf = self.get_conf('')
 
3512
        self.assertEquals(None, conf.get('foo'))
3503
3513
 
3504
3514
    def test_get_default_integer(self):
3505
3515
        self.register_integer_option('foo', 42)
3506
 
        self.assertEquals(42, self.conf.get('foo'))
 
3516
        conf = self.get_conf('')
 
3517
        self.assertEquals(42, conf.get('foo'))
3507
3518
 
3508
3519
    def test_get_default_integer_as_string(self):
3509
3520
        self.register_integer_option('foo', u'42')
3510
 
        self.assertEquals(42, self.conf.get('foo'))
 
3521
        conf = self.get_conf('')
 
3522
        self.assertEquals(42, conf.get('foo'))
3511
3523
 
3512
3524
    def test_get_default_integer_from_env(self):
3513
3525
        self.register_integer_option('foo', default_from_env=['FOO'])
3514
3526
        self.overrideEnv('FOO', '18')
3515
 
        self.assertEquals(18, self.conf.get('foo'))
 
3527
        conf = self.get_conf('')
 
3528
        self.assertEquals(18, conf.get('foo'))
3516
3529
 
3517
3530
    def test_get_default_integer_when_conversion_fails(self):
3518
3531
        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'))
 
3532
        conf = self.get_conf('foo=invalid integer')
 
3533
        self.assertEquals(12, conf.get('foo'))
3521
3534
 
3522
3535
    def register_list_option(self, name, default=None, default_from_env=None):
3523
3536
        l = config.Option(name, help='A list.',
3527
3540
 
3528
3541
    def test_get_default_list_None(self):
3529
3542
        self.register_list_option('foo')
3530
 
        self.assertEquals(None, self.conf.get('foo'))
 
3543
        conf = self.get_conf('')
 
3544
        self.assertEquals(None, conf.get('foo'))
3531
3545
 
3532
3546
    def test_get_default_list_empty(self):
3533
3547
        self.register_list_option('foo', '')
3534
 
        self.assertEquals([], self.conf.get('foo'))
 
3548
        conf = self.get_conf('')
 
3549
        self.assertEquals([], conf.get('foo'))
3535
3550
 
3536
3551
    def test_get_default_list_from_env(self):
3537
3552
        self.register_list_option('foo', default_from_env=['FOO'])
3538
3553
        self.overrideEnv('FOO', '')
3539
 
        self.assertEquals([], self.conf.get('foo'))
 
3554
        conf = self.get_conf('')
 
3555
        self.assertEquals([], conf.get('foo'))
3540
3556
 
3541
3557
    def test_get_with_list_converter_no_item(self):
3542
3558
        self.register_list_option('foo', None)
3543
 
        self.conf.store._load_from_string('foo=,')
3544
 
        self.assertEquals([], self.conf.get('foo'))
 
3559
        conf = self.get_conf('foo=,')
 
3560
        self.assertEquals([], conf.get('foo'))
3545
3561
 
3546
3562
    def test_get_with_list_converter_many_items(self):
3547
3563
        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'))
 
3564
        conf = self.get_conf('foo=m,o,r,e')
 
3565
        self.assertEquals(['m', 'o', 'r', 'e'], conf.get('foo'))
3550
3566
 
3551
3567
    def test_get_with_list_converter_embedded_spaces_many_items(self):
3552
3568
        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'))
 
3569
        conf = self.get_conf('foo=" bar", "baz "')
 
3570
        self.assertEquals([' bar', 'baz '], conf.get('foo'))
3555
3571
 
3556
3572
    def test_get_with_list_converter_stripped_spaces_many_items(self):
3557
3573
        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'))
 
3574
        conf = self.get_conf('foo= bar ,  baz ')
 
3575
        self.assertEquals(['bar', 'baz'], conf.get('foo'))
3560
3576
 
3561
3577
 
3562
3578
class TestIterOptionRefs(tests.TestCase):