1471
1467
self.assertIs(None, bzrdir_config.get_default_stack_on())
1470
def create_configs(test):
1471
"""Create configuration files for a given test.
1473
This requires creating a tree (and populate the ``test.tree`` attribute)
1474
and its associated branch and will populate the following attributes:
1476
- branch_config: A BranchConfig for the associated branch.
1478
- locations_config : A LocationConfig for the associated branch
1480
- bazaar_config: A GlobalConfig.
1482
The tree and branch are created in a 'tree' subdirectory so the tests can
1483
still use the test directory to stay outside of the branch.
1485
tree = test.make_branch_and_tree('tree')
1487
test.branch_config = config.BranchConfig(tree.branch)
1488
test.locations_config = config.LocationConfig(tree.basedir)
1489
test.bazaar_config = config.GlobalConfig()
1492
def create_configs_with_file_option(test):
1493
"""Create configuration files with a ``file`` option set in each.
1495
This builds on ``create_configs`` and add one ``file`` option in each
1496
configuration with a value which allows identifying the configuration file.
1498
create_configs(test)
1499
test.bazaar_config.set_user_option('file', 'bazaar')
1500
test.locations_config.set_user_option('file', 'locations')
1501
test.branch_config.set_user_option('file', 'branch')
1504
class TestConfigGetOptions(tests.TestCaseWithTransport):
1507
super(TestConfigGetOptions, self).setUp()
1508
create_configs(self)
1510
def assertOptions(self, expected, conf):
1511
actual = list(conf._get_options())
1512
self.assertEqual(expected, actual)
1514
# One variable in none of the above
1515
def test_no_variable(self):
1516
# Using branch should query branch, locations and bazaar
1517
self.assertOptions([], self.branch_config)
1519
def test_option_in_bazaar(self):
1520
self.bazaar_config.set_user_option('file', 'bazaar')
1521
self.assertOptions([('file', 'bazaar', 'DEFAULT', 'bazaar')],
1524
def test_option_in_locations(self):
1525
self.locations_config.set_user_option('file', 'locations')
1527
[('file', 'locations', self.tree.basedir, 'locations')],
1528
self.locations_config)
1530
def test_option_in_branch(self):
1531
self.branch_config.set_user_option('file', 'branch')
1532
self.assertOptions([('file', 'branch', 'DEFAULT', 'branch')],
1535
def test_option_in_bazaar_and_branch(self):
1536
self.bazaar_config.set_user_option('file', 'bazaar')
1537
self.branch_config.set_user_option('file', 'branch')
1538
self.assertOptions([('file', 'branch', 'DEFAULT', 'branch'),
1539
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
1542
def test_option_in_branch_and_locations(self):
1543
# Hmm, locations override branch :-/
1544
self.locations_config.set_user_option('file', 'locations')
1545
self.branch_config.set_user_option('file', 'branch')
1547
[('file', 'locations', self.tree.basedir, 'locations'),
1548
('file', 'branch', 'DEFAULT', 'branch'),],
1551
def test_option_in_bazaar_locations_and_branch(self):
1552
self.bazaar_config.set_user_option('file', 'bazaar')
1553
self.locations_config.set_user_option('file', 'locations')
1554
self.branch_config.set_user_option('file', 'branch')
1556
[('file', 'locations', self.tree.basedir, 'locations'),
1557
('file', 'branch', 'DEFAULT', 'branch'),
1558
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
1562
class TestConfigRemoveOption(tests.TestCaseWithTransport):
1565
super(TestConfigRemoveOption, self).setUp()
1566
create_configs_with_file_option(self)
1568
def assertOptions(self, expected, conf):
1569
actual = list(conf._get_options())
1570
self.assertEqual(expected, actual)
1572
def test_remove_in_locations(self):
1573
self.locations_config.remove_user_option('file', self.tree.basedir)
1575
[('file', 'branch', 'DEFAULT', 'branch'),
1576
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
1579
def test_remove_in_branch(self):
1580
self.branch_config.remove_user_option('file')
1582
[('file', 'locations', self.tree.basedir, 'locations'),
1583
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
1586
def test_remove_in_bazaar(self):
1587
self.bazaar_config.remove_user_option('file')
1589
[('file', 'locations', self.tree.basedir, 'locations'),
1590
('file', 'branch', 'DEFAULT', 'branch'),],
1594
class TestConfigGetSections(tests.TestCaseWithTransport):
1597
super(TestConfigGetSections, self).setUp()
1598
create_configs(self)
1600
def assertSectionNames(self, expected, conf, name=None):
1601
"""Check which sections are returned for a given config.
1603
If fallback configurations exist their sections can be included.
1605
:param expected: A list of section names.
1607
:param conf: The configuration that will be queried.
1609
:param name: An optional section name that will be passed to
1612
sections = list(conf._get_sections(name))
1613
self.assertLength(len(expected), sections)
1614
self.assertEqual(expected, [name for name, _, _ in sections])
1616
def test_bazaar_default_section(self):
1617
self.assertSectionNames(['DEFAULT'], self.bazaar_config)
1619
def test_locations_default_section(self):
1620
# No sections are defined in an empty file
1621
self.assertSectionNames([], self.locations_config)
1623
def test_locations_named_section(self):
1624
self.locations_config.set_user_option('file', 'locations')
1625
self.assertSectionNames([self.tree.basedir], self.locations_config)
1627
def test_locations_matching_sections(self):
1628
loc_config = self.locations_config
1629
loc_config.set_user_option('file', 'locations')
1630
# We need to cheat a bit here to create an option in sections above and
1631
# below the 'location' one.
1632
parser = loc_config._get_parser()
1633
# locations.cong deals with '/' ignoring native os.sep
1634
location_names = self.tree.basedir.split('/')
1635
parent = '/'.join(location_names[:-1])
1636
child = '/'.join(location_names + ['child'])
1638
parser[parent]['file'] = 'parent'
1640
parser[child]['file'] = 'child'
1641
self.assertSectionNames([self.tree.basedir, parent], loc_config)
1643
def test_branch_data_default_section(self):
1644
self.assertSectionNames([None],
1645
self.branch_config._get_branch_data_config())
1647
def test_branch_default_sections(self):
1648
# No sections are defined in an empty locations file
1649
self.assertSectionNames([None, 'DEFAULT'],
1651
# Unless we define an option
1652
self.branch_config._get_location_config().set_user_option(
1653
'file', 'locations')
1654
self.assertSectionNames([self.tree.basedir, None, 'DEFAULT'],
1657
def test_bazaar_named_section(self):
1658
# We need to cheat as the API doesn't give direct access to sections
1659
# other than DEFAULT.
1660
self.bazaar_config.set_alias('bazaar', 'bzr')
1661
self.assertSectionNames(['ALIASES'], self.bazaar_config, 'ALIASES')
1474
1664
class TestAuthenticationConfigFile(tests.TestCase):
1475
1665
"""Test the authentication.conf file matching"""