~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Vincent Ladeuil
  • Date: 2012-02-14 17:22:37 UTC
  • mfrom: (6466 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120214172237-7dv7er3n4uy8d5m4
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    mail_client,
36
36
    ui,
37
37
    urlutils,
 
38
    registry as _mod_registry,
38
39
    remote,
39
40
    tests,
40
41
    trace,
576
577
    def test_config_dir(self):
577
578
        self.assertEqual(config.config_dir(), self.bzr_home)
578
579
 
 
580
    def test_config_dir_is_unicode(self):
 
581
        self.assertIsInstance(config.config_dir(), unicode)
 
582
 
579
583
    def test_config_filename(self):
580
584
        self.assertEqual(config.config_filename(),
581
585
                         self.bzr_home + '/bazaar.conf')
1903
1907
            location='http://example.com/specific')
1904
1908
        self.assertEqual(my_config.get_user_option('option'), 'exact')
1905
1909
 
1906
 
    def test_get_mail_client(self):
1907
 
        config = self.get_branch_config()
1908
 
        client = config.get_mail_client()
1909
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1910
 
 
1911
 
        # Specific clients
1912
 
        config.set_user_option('mail_client', 'evolution')
1913
 
        client = config.get_mail_client()
1914
 
        self.assertIsInstance(client, mail_client.Evolution)
1915
 
 
1916
 
        config.set_user_option('mail_client', 'kmail')
1917
 
        client = config.get_mail_client()
1918
 
        self.assertIsInstance(client, mail_client.KMail)
1919
 
 
1920
 
        config.set_user_option('mail_client', 'mutt')
1921
 
        client = config.get_mail_client()
1922
 
        self.assertIsInstance(client, mail_client.Mutt)
1923
 
 
1924
 
        config.set_user_option('mail_client', 'thunderbird')
1925
 
        client = config.get_mail_client()
1926
 
        self.assertIsInstance(client, mail_client.Thunderbird)
1927
 
 
1928
 
        # Generic options
1929
 
        config.set_user_option('mail_client', 'default')
1930
 
        client = config.get_mail_client()
1931
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1932
 
 
1933
 
        config.set_user_option('mail_client', 'editor')
1934
 
        client = config.get_mail_client()
1935
 
        self.assertIsInstance(client, mail_client.Editor)
1936
 
 
1937
 
        config.set_user_option('mail_client', 'mapi')
1938
 
        client = config.get_mail_client()
1939
 
        self.assertIsInstance(client, mail_client.MAPIClient)
1940
 
 
1941
 
        config.set_user_option('mail_client', 'xdg-email')
1942
 
        client = config.get_mail_client()
1943
 
        self.assertIsInstance(client, mail_client.XDGEmail)
1944
 
 
1945
 
        config.set_user_option('mail_client', 'firebird')
1946
 
        self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1947
 
 
1948
1910
 
1949
1911
class TestMailAddressExtraction(tests.TestCase):
1950
1912
 
2480
2442
        self.assertConverted([u'bar'], opt, u'bar')
2481
2443
 
2482
2444
 
 
2445
class TestRegistryOption(tests.TestCase, TestOptionConverterMixin):
 
2446
 
 
2447
    def get_option(self, registry):
 
2448
        return config.RegistryOption('foo', registry,
 
2449
                help='A registry option.')
 
2450
 
 
2451
    def test_convert_invalid(self):
 
2452
        registry = _mod_registry.Registry()
 
2453
        opt = self.get_option(registry)
 
2454
        self.assertConvertInvalid(opt, [1])
 
2455
        self.assertConvertInvalid(opt, u"notregistered")
 
2456
 
 
2457
    def test_convert_valid(self):
 
2458
        registry = _mod_registry.Registry()
 
2459
        registry.register("someval", 1234)
 
2460
        opt = self.get_option(registry)
 
2461
        # Using a bare str() just in case
 
2462
        self.assertConverted(1234, opt, "someval")
 
2463
        self.assertConverted(1234, opt, u'someval')
 
2464
        self.assertConverted(None, opt, None)
 
2465
 
 
2466
    def test_help(self):
 
2467
        registry = _mod_registry.Registry()
 
2468
        registry.register("someval", 1234, help="some option")
 
2469
        registry.register("dunno", 1234, help="some other option")
 
2470
        opt = self.get_option(registry)
 
2471
        self.assertEquals(
 
2472
            'A registry option.\n'
 
2473
            '\n'
 
2474
            'The following values are supported:\n'
 
2475
            ' dunno - some other option\n'
 
2476
            ' someval - some option\n',
 
2477
            opt.help)
 
2478
 
 
2479
    def test_get_help_text(self):
 
2480
        registry = _mod_registry.Registry()
 
2481
        registry.register("someval", 1234, help="some option")
 
2482
        registry.register("dunno", 1234, help="some other option")
 
2483
        opt = self.get_option(registry)
 
2484
        self.assertEquals(
 
2485
            'A registry option.\n'
 
2486
            '\n'
 
2487
            'The following values are supported:\n'
 
2488
            ' dunno - some other option\n'
 
2489
            ' someval - some option\n',
 
2490
            opt.get_help_text())
 
2491
 
 
2492
 
2483
2493
class TestOptionRegistry(tests.TestCase):
2484
2494
 
2485
2495
    def setUp(self):
3366
3376
 
3367
3377
    def get_section(self, options, extra_path):
3368
3378
        section = config.Section('foo', options)
3369
 
        # We don't care about the length so we use '0'
3370
 
        return config.LocationSection(section, 0, extra_path)
 
3379
        return config.LocationSection(section, extra_path)
3371
3380
 
3372
3381
    def test_simple_option(self):
3373
3382
        section = self.get_section({'foo': 'bar'}, '')
3410
3419
                           '/quux/quux'],
3411
3420
                          [section.id for _, section in store.get_sections()])
3412
3421
        matcher = config.LocationMatcher(store, '/foo/bar/quux')
3413
 
        sections = [section for s, section in matcher.get_sections()]
3414
 
        self.assertEquals([3, 2],
3415
 
                          [section.length for section in sections])
 
3422
        sections = [section for _, section in matcher.get_sections()]
3416
3423
        self.assertEquals(['/foo/bar', '/foo'],
3417
3424
                          [section.id for section in sections])
3418
3425
        self.assertEquals(['quux', 'bar/quux'],
3429
3436
        self.assertEquals(['/foo', '/foo/bar'],
3430
3437
                          [section.id for _, section in store.get_sections()])
3431
3438
        matcher = config.LocationMatcher(store, '/foo/bar/baz')
3432
 
        sections = [section for s, section in matcher.get_sections()]
3433
 
        self.assertEquals([3, 2],
3434
 
                          [section.length for section in sections])
 
3439
        sections = [section for _, section in matcher.get_sections()]
3435
3440
        self.assertEquals(['/foo/bar', '/foo'],
3436
3441
                          [section.id for section in sections])
3437
3442
        self.assertEquals(['baz', 'bar/baz'],
3462
3467
        self.assertEquals(expected_location, matcher.location)
3463
3468
 
3464
3469
 
 
3470
class TestStartingPathMatcher(TestStore):
 
3471
 
 
3472
    def setUp(self):
 
3473
        super(TestStartingPathMatcher, self).setUp()
 
3474
        # Any simple store is good enough
 
3475
        self.store = config.IniFileStore()
 
3476
 
 
3477
    def assertSectionIDs(self, expected, location, content):
 
3478
        self.store._load_from_string(content)
 
3479
        matcher = config.StartingPathMatcher(self.store, location)
 
3480
        sections = list(matcher.get_sections())
 
3481
        self.assertLength(len(expected), sections)
 
3482
        self.assertEqual(expected, [section.id for _, section in sections])
 
3483
        return sections
 
3484
 
 
3485
    def test_empty(self):
 
3486
        self.assertSectionIDs([], self.get_url(), '')
 
3487
 
 
3488
    def test_url_vs_local_paths(self):
 
3489
        # The matcher location is an url and the section names are local paths
 
3490
        sections = self.assertSectionIDs(['/foo/bar', '/foo'],
 
3491
                                         'file:///foo/bar/baz', '''\
 
3492
[/foo]
 
3493
[/foo/bar]
 
3494
''')
 
3495
 
 
3496
    def test_local_path_vs_url(self):
 
3497
        # The matcher location is a local path and the section names are urls
 
3498
        sections = self.assertSectionIDs(['file:///foo/bar', 'file:///foo'],
 
3499
                                         '/foo/bar/baz', '''\
 
3500
[file:///foo]
 
3501
[file:///foo/bar]
 
3502
''')
 
3503
 
 
3504
 
 
3505
    def test_no_name_section_included_when_present(self):
 
3506
        # Note that other tests will cover the case where the no-name section
 
3507
        # is empty and as such, not included.
 
3508
        sections = self.assertSectionIDs(['/foo/bar', '/foo', None],
 
3509
                                         '/foo/bar/baz', '''\
 
3510
option = defined so the no-name section exists
 
3511
[/foo]
 
3512
[/foo/bar]
 
3513
''')
 
3514
        self.assertEquals(['baz', 'bar/baz', '/foo/bar/baz'],
 
3515
                          [s.locals['relpath'] for _, s in sections])
 
3516
 
 
3517
    def test_order_reversed(self):
 
3518
        self.assertSectionIDs(['/foo/bar', '/foo'], '/foo/bar/baz', '''\
 
3519
[/foo]
 
3520
[/foo/bar]
 
3521
''')
 
3522
 
 
3523
    def test_unrelated_section_excluded(self):
 
3524
        self.assertSectionIDs(['/foo/bar', '/foo'], '/foo/bar/baz', '''\
 
3525
[/foo]
 
3526
[/foo/qux]
 
3527
[/foo/bar]
 
3528
''')
 
3529
 
 
3530
    def test_glob_included(self):
 
3531
        sections = self.assertSectionIDs(['/foo/*/baz', '/foo/b*', '/foo'],
 
3532
                                         '/foo/bar/baz', '''\
 
3533
[/foo]
 
3534
[/foo/qux]
 
3535
[/foo/b*]
 
3536
[/foo/*/baz]
 
3537
''')
 
3538
        # Note that 'baz' as a relpath for /foo/b* is not fully correct, but
 
3539
        # nothing really is... as far using {relpath} to append it to something
 
3540
        # else, this seems good enough though.
 
3541
        self.assertEquals(['', 'baz', 'bar/baz'],
 
3542
                          [s.locals['relpath'] for _, s in sections])
 
3543
 
 
3544
    def test_respect_order(self):
 
3545
        self.assertSectionIDs(['/foo', '/foo/b*', '/foo/*/baz'],
 
3546
                              '/foo/bar/baz', '''\
 
3547
[/foo/*/baz]
 
3548
[/foo/qux]
 
3549
[/foo/b*]
 
3550
[/foo]
 
3551
''')
 
3552
 
 
3553
 
3465
3554
class TestNameMatcher(TestStore):
3466
3555
 
3467
3556
    def setUp(self):
4792
4881
        self.overrideEnv('BZR_EMAIL', None)
4793
4882
        self.overrideEnv('EMAIL', 'jelmer@samba.org')
4794
4883
        self.assertEquals('jelmer@debian.org', conf.get('email'))
 
4884
 
 
4885
 
 
4886
class MailClientOptionTests(tests.TestCase):
 
4887
 
 
4888
    def test_default(self):
 
4889
        conf = config.MemoryStack('')
 
4890
        client = conf.get('mail_client')
 
4891
        self.assertIs(client, mail_client.DefaultMail)
 
4892
 
 
4893
    def test_evolution(self):
 
4894
        conf = config.MemoryStack('mail_client=evolution')
 
4895
        client = conf.get('mail_client')
 
4896
        self.assertIs(client, mail_client.Evolution)
 
4897
 
 
4898
    def test_kmail(self):
 
4899
        conf = config.MemoryStack('mail_client=kmail')
 
4900
        client = conf.get('mail_client')
 
4901
        self.assertIs(client, mail_client.KMail)
 
4902
 
 
4903
    def test_mutt(self):
 
4904
        conf = config.MemoryStack('mail_client=mutt')
 
4905
        client = conf.get('mail_client')
 
4906
        self.assertIs(client, mail_client.Mutt)
 
4907
 
 
4908
    def test_thunderbird(self):
 
4909
        conf = config.MemoryStack('mail_client=thunderbird')
 
4910
        client = conf.get('mail_client')
 
4911
        self.assertIs(client, mail_client.Thunderbird)
 
4912
 
 
4913
    def test_explicit_default(self):
 
4914
        conf = config.MemoryStack('mail_client=default')
 
4915
        client = conf.get('mail_client')
 
4916
        self.assertIs(client, mail_client.DefaultMail)
 
4917
 
 
4918
    def test_editor(self):
 
4919
        conf = config.MemoryStack('mail_client=editor')
 
4920
        client = conf.get('mail_client')
 
4921
        self.assertIs(client, mail_client.Editor)
 
4922
 
 
4923
    def test_mapi(self):
 
4924
        conf = config.MemoryStack('mail_client=mapi')
 
4925
        client = conf.get('mail_client')
 
4926
        self.assertIs(client, mail_client.MAPIClient)
 
4927
 
 
4928
    def test_xdg_email(self):
 
4929
        conf = config.MemoryStack('mail_client=xdg-email')
 
4930
        client = conf.get('mail_client')
 
4931
        self.assertIs(client, mail_client.XDGEmail)
 
4932
 
 
4933
    def test_unknown(self):
 
4934
        conf = config.MemoryStack('mail_client=firebird')
 
4935
        self.assertRaises(errors.ConfigOptionValueError, conf.get,
 
4936
                'mail_client')