1471
1471
self.assertIs(None, bzrdir_config.get_default_stack_on())
1474
def create_configs(test):
1475
"""Create configuration files for a given test.
1477
This requires creating a tree (and populate the ``test.tree`` attribute and
1478
its associated branch and will populate the following attributes:
1480
- branch_config: A BranchConfig for the associated branch.
1482
- locations_config : A LocationConfig for the associated branch
1484
- bazaar_config: A GlobalConfig.
1486
The tree and branch are created in a 'tree' subdirectory so the tests can
1487
still use the test directory to stay outside of the branch.
1489
tree = test.make_branch_and_tree('tree')
1491
test.branch_config = config.BranchConfig(tree.branch)
1492
test.locations_config = config.LocationConfig(tree.basedir)
1493
test.bazaar_config = config.GlobalConfig()
1496
def create_configs_with_file_option(test):
1497
"""Create configuration files with a ``file`` option set in each.
1499
This builds on ``create_configs`` and add one ``file`` option in each
1500
configuration with a value which allows identifying the configuration file.
1502
create_configs(test)
1503
test.bazaar_config.set_user_option('file', 'bazaar')
1504
test.locations_config.set_user_option('file', 'locations')
1505
test.branch_config.set_user_option('file', 'branch')
1508
class TestConfigGetOptions(tests.TestCaseWithTransport):
1511
super(TestConfigGetOptions, self).setUp()
1512
create_configs(self)
1514
def assertOptions(self, expected, conf):
1515
actual = list(conf._get_options())
1516
self.assertEqual(expected, actual)
1518
# One variable in none of the above
1519
def test_no_variable(self):
1520
# Using branch should query branch, locations and bazaar
1521
self.assertOptions([], self.branch_config)
1523
def test_option_in_bazaar(self):
1524
self.bazaar_config.set_user_option('file', 'bazaar')
1525
self.assertOptions([('file', 'bazaar', 'DEFAULT', 'bazaar')],
1528
def test_option_in_locations(self):
1529
self.locations_config.set_user_option('file', 'locations')
1531
[('file', 'locations', self.tree.basedir, 'locations')],
1532
self.locations_config)
1534
def test_option_in_branch(self):
1535
self.branch_config.set_user_option('file', 'branch')
1536
self.assertOptions([('file', 'branch', 'DEFAULT', 'branch')],
1539
def test_option_in_bazaar_and_branch(self):
1540
self.bazaar_config.set_user_option('file', 'bazaar')
1541
self.branch_config.set_user_option('file', 'branch')
1542
self.assertOptions([('file', 'branch', 'DEFAULT', 'branch'),
1543
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
1546
def test_option_in_branch_and_locations(self):
1547
# Hmm, locations override branch :-/
1548
self.locations_config.set_user_option('file', 'locations')
1549
self.branch_config.set_user_option('file', 'branch')
1551
[('file', 'locations', self.tree.basedir, 'locations'),
1552
('file', 'branch', 'DEFAULT', 'branch'),],
1555
def test_option_in_bazaar_locations_and_branch(self):
1556
self.bazaar_config.set_user_option('file', 'bazaar')
1557
self.locations_config.set_user_option('file', 'locations')
1558
self.branch_config.set_user_option('file', 'branch')
1560
[('file', 'locations', self.tree.basedir, 'locations'),
1561
('file', 'branch', 'DEFAULT', 'branch'),
1562
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
1566
class TestConfigRemoveOption(tests.TestCaseWithTransport):
1569
super(TestConfigRemoveOption, self).setUp()
1570
create_configs_with_file_option(self)
1572
def assertOptions(self, expected, conf):
1573
actual = list(conf._get_options())
1574
self.assertEqual(expected, actual)
1576
def test_remove_in_locations(self):
1577
self.locations_config.remove_user_option('file', self.tree.basedir)
1579
[('file', 'branch', 'DEFAULT', 'branch'),
1580
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
1583
def test_remove_in_branch(self):
1584
self.branch_config.remove_user_option('file')
1586
[('file', 'locations', self.tree.basedir, 'locations'),
1587
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
1590
def test_remove_in_bazaar(self):
1591
self.bazaar_config.remove_user_option('file')
1593
[('file', 'locations', self.tree.basedir, 'locations'),
1594
('file', 'branch', 'DEFAULT', 'branch'),],
1598
class TestConfigGetSections(tests.TestCaseWithTransport):
1601
super(TestConfigGetSections, self).setUp()
1602
create_configs(self)
1604
def assertSectionNames(self, expected, conf, name=None):
1605
"""Check which sections are returned for a given config.
1607
If fallback configurations exist their sections can be included.
1609
:param expected: A list of section names.
1611
:param conf: The configuration that will be queried.
1613
:param name: An optional section name that will be passed to
1616
sections = list(conf._get_sections(name))
1617
self.assertLength(len(expected), sections)
1618
self.assertEqual(expected, [name for name, _, _ in sections])
1620
def test_bazaar_default_section(self):
1621
self.assertSectionNames(['DEFAULT'], self.bazaar_config)
1623
def test_locations_default_section(self):
1624
# No sections are defined in an empty file
1625
self.assertSectionNames([], self.locations_config)
1627
def test_locations_named_section(self):
1628
self.locations_config.set_user_option('file', 'locations')
1629
self.assertSectionNames([self.tree.basedir], self.locations_config)
1631
def test_locations_matching_sections(self):
1632
loc_config = self.locations_config
1633
loc_config.set_user_option('file', 'locations')
1634
# We need to cheat a bit here to create an option in sections above and
1635
# below the 'location' one.
1636
parser = loc_config._get_parser()
1637
# locations.cong deals with '/' ignoring native os.sep
1638
location_names = self.tree.basedir.split('/')
1639
parent = '/'.join(location_names[:-1])
1640
child = '/'.join(location_names + ['child'])
1642
parser[parent]['file'] = 'parent'
1644
parser[child]['file'] = 'child'
1645
self.assertSectionNames([self.tree.basedir, parent], loc_config)
1647
def test_branch_data_default_section(self):
1648
self.assertSectionNames([None],
1649
self.branch_config._get_branch_data_config())
1651
def test_branch_default_sections(self):
1652
# No sections are defined in an empty locations file
1653
self.assertSectionNames([None, 'DEFAULT'],
1655
# Unless we define an option
1656
self.branch_config._get_location_config().set_user_option(
1657
'file', 'locations')
1658
self.assertSectionNames([self.tree.basedir, None, 'DEFAULT'],
1661
def test_bazaar_named_section(self):
1662
# We need to cheat as the API doesn't give direct access to sections
1663
# other than DEFAULT.
1664
self.bazaar_config.set_alias('bazaar', 'bzr')
1665
self.assertSectionNames(['ALIASES'], self.bazaar_config, 'ALIASES')
1474
1668
class TestAuthenticationConfigFile(tests.TestCase):
1475
1669
"""Test the authentication.conf file matching"""