~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Vincent Ladeuil
  • Date: 2010-10-02 21:08:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5499.
  • Revision ID: v.ladeuil+lp@free.fr-20101002210835-nqy0fqas09e7ijj1
Implement config.get_sections() to clarify how sections can be used.

* bzrlib/tests/test_config.py:
(TestConfigGetSections): Expose the various supported section
use-cases.

* bzrlib/config.py:
(IniBasedConfig.get_sections, GlobalConfig.get_sections)
(LocationConfig.get_sections, BranchConfig.get_sections): Properly
capture the different section usages exposed by the various config
objects.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1471
1471
        self.assertIs(None, bzrdir_config.get_default_stack_on())
1472
1472
 
1473
1473
 
1474
 
class TestConfigGetOptions(tests.TestCaseWithTransport):
 
1474
class TestWithConfigs(tests.TestCaseWithTransport):
1475
1475
 
1476
1476
    def setUp(self):
1477
 
        super(TestConfigGetOptions, self).setUp()
 
1477
        super(TestWithConfigs, self).setUp()
1478
1478
        self.global_config = config.GlobalConfig()
1479
 
        self.tree = self.make_branch_and_tree('.')
 
1479
        self.tree = self.make_branch_and_tree('tree')
1480
1480
        self.branch_config = config.BranchConfig(self.tree.branch)
1481
1481
        self.locations_config = config.LocationConfig(self.tree.basedir)
1482
1482
 
 
1483
 
 
1484
class TestConfigGetOptions(TestWithConfigs):
 
1485
 
1483
1486
    def assertOptions(self, expected, conf):
1484
1487
        actual = list(conf.get_options())
1485
1488
        self.assertEqual(expected, actual)
1532
1535
            self.branch_config)
1533
1536
 
1534
1537
 
 
1538
class TestConfigGetSections(TestWithConfigs):
 
1539
 
 
1540
    def assertSectionNames(self, expected, conf, name=None):
 
1541
        """Check which sections are returned for a given config.
 
1542
 
 
1543
        If fallback configurations exist their sections can be included.
 
1544
 
 
1545
        :param expected: A list of section names.
 
1546
 
 
1547
        :param conf: The configuration that will be queried.
 
1548
 
 
1549
        :param name: An optional section name that will be passed to
 
1550
            get_sections().
 
1551
        """
 
1552
        sections = list(conf.get_sections(name))
 
1553
        self.assertLength(len(expected), sections)
 
1554
        self.assertEqual(expected, [name for name, section in sections])
 
1555
 
 
1556
    def test_global_default_section(self):
 
1557
        self.assertSectionNames(['DEFAULT'], self.global_config)
 
1558
 
 
1559
    def test_locations_default_section(self):
 
1560
        # No sections are defined in an empty file
 
1561
        self.assertSectionNames([], self.locations_config)
 
1562
 
 
1563
    def test_locations_named_section(self):
 
1564
        self.locations_config.set_user_option('file', 'locations')
 
1565
        self.assertSectionNames([self.tree.basedir], self.locations_config)
 
1566
 
 
1567
    def test_locations_matching_sections(self):
 
1568
        loc_config = self.locations_config
 
1569
        loc_config.set_user_option('file', 'locations')
 
1570
        # We need to cheat a bit here to create an option in sections above and
 
1571
        # below the 'location' one.
 
1572
        parser = loc_config._get_parser()
 
1573
        # locations.cong deals with '/' ignoring native os.sep
 
1574
        location_names = self.tree.basedir.split('/')
 
1575
        parent = '/'.join(location_names[:-1])
 
1576
        child = '/'.join(location_names + ['child'])
 
1577
        parser[parent] = {}
 
1578
        parser[parent]['file'] = 'parent'
 
1579
        parser[child] = {}
 
1580
        parser[child]['file'] = 'child'
 
1581
        self.assertSectionNames([self.tree.basedir, parent], loc_config)
 
1582
 
 
1583
    def test_branch_data_default_section(self):
 
1584
        self.assertSectionNames([None],
 
1585
                                self.branch_config._get_branch_data_config())
 
1586
 
 
1587
    def test_branch_default_sections(self):
 
1588
        # No sections are defined in an empty locations file
 
1589
        self.assertSectionNames([None, 'DEFAULT'],
 
1590
                                self.branch_config)
 
1591
        # Unless we define an option
 
1592
        self.branch_config._get_location_config().set_user_option(
 
1593
            'file', 'locations')
 
1594
        self.assertSectionNames([self.tree.basedir, None, 'DEFAULT'],
 
1595
                                self.branch_config)
 
1596
 
 
1597
    def test_global_named_section(self):
 
1598
        # We need to cheat as the API doesn't give direct access to sections
 
1599
        # other than DEFAULT.
 
1600
        self.global_config.set_alias('bazaar', 'bzr')
 
1601
        self.assertSectionNames(['ALIASES'], self.global_config, 'ALIASES')
 
1602
 
 
1603
 
1535
1604
class TestAuthenticationConfigFile(tests.TestCase):
1536
1605
    """Test the authentication.conf file matching"""
1537
1606