312
321
my_config = config.Config()
313
322
self.assertEqual('long', my_config.log_format())
324
def test_get_change_editor(self):
325
my_config = InstrumentedConfig()
326
change_editor = my_config.get_change_editor('old_tree', 'new_tree')
327
self.assertEqual(['_get_change_editor'], my_config._calls)
328
self.assertIs(diff.DiffFromTool, change_editor.__class__)
329
self.assertEqual(['vimdiff', '-fo', '@new_path', '@old_path'],
330
change_editor.command_template)
316
333
class TestConfigPath(tests.TestCase):
319
336
super(TestConfigPath, self).setUp()
320
337
os.environ['HOME'] = '/home/bogus'
338
os.environ['XDG_CACHE_DIR'] = ''
321
339
if sys.platform == 'win32':
322
340
os.environ['BZR_HOME'] = \
323
341
r'C:\Documents and Settings\bogus\Application Data'
364
394
parser = my_config._get_parser(file=config_file)
365
395
self.failUnless(my_config._get_parser() is parser)
397
def _dummy_chown(self, path, uid, gid):
398
self.path, self.uid, self.gid = path, uid, gid
400
def test_ini_config_ownership(self):
401
"""Ensure that chown is happening during _write_config_file.
403
self.requireFeature(features.chown_feature)
404
self.overrideAttr(os, 'chown', self._dummy_chown)
405
self.path = self.uid = self.gid = None
408
conf = config.IniBasedConfig(get_filename)
409
conf._write_config_file()
410
self.assertEquals(self.path, 'foo.conf')
411
self.assertTrue(isinstance(self.uid, int))
412
self.assertTrue(isinstance(self.gid, int))
414
class TestGetUserOptionAs(TestIniConfig):
416
def test_get_user_option_as_bool(self):
417
conf, parser = self.make_config_parser("""
420
an_invalid_bool = maybe
421
a_list = hmm, who knows ? # This is interpreted as a list !
423
get_bool = conf.get_user_option_as_bool
424
self.assertEqual(True, get_bool('a_true_bool'))
425
self.assertEqual(False, get_bool('a_false_bool'))
428
warnings.append(args[0] % args[1:])
429
self.overrideAttr(trace, 'warning', warning)
430
msg = 'Value "%s" is not a boolean for "%s"'
431
self.assertIs(None, get_bool('an_invalid_bool'))
432
self.assertEquals(msg % ('maybe', 'an_invalid_bool'), warnings[0])
434
self.assertIs(None, get_bool('not_defined_in_this_config'))
435
self.assertEquals([], warnings)
437
def test_get_user_option_as_list(self):
438
conf, parser = self.make_config_parser("""
443
get_list = conf.get_user_option_as_list
444
self.assertEqual(['a', 'b', 'c'], get_list('a_list'))
445
self.assertEqual(['1'], get_list('length_1'))
446
self.assertEqual('x', conf.get_user_option('one_item'))
447
# automatically cast to list
448
self.assertEqual(['x'], get_list('one_item'))
451
class TestSupressWarning(TestIniConfig):
453
def make_warnings_config(self, s):
454
conf, parser = self.make_config_parser(s)
455
return conf.suppress_warning
457
def test_suppress_warning_unknown(self):
458
suppress_warning = self.make_warnings_config('')
459
self.assertEqual(False, suppress_warning('unknown_warning'))
461
def test_suppress_warning_known(self):
462
suppress_warning = self.make_warnings_config('suppress_warnings=a,b')
463
self.assertEqual(False, suppress_warning('c'))
464
self.assertEqual(True, suppress_warning('a'))
465
self.assertEqual(True, suppress_warning('b'))
368
468
class TestGetConfig(tests.TestCase):
604
704
my_config = self._get_sample_config()
605
705
self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
707
def test_get_change_editor(self):
708
my_config = self._get_sample_config()
709
change_editor = my_config.get_change_editor('old', 'new')
710
self.assertIs(diff.DiffFromTool, change_editor.__class__)
711
self.assertEqual('vimdiff -of @new_path @old_path',
712
' '.join(change_editor.command_template))
714
def test_get_no_change_editor(self):
715
my_config = self._get_empty_config()
716
change_editor = my_config.get_change_editor('old', 'new')
717
self.assertIs(None, change_editor)
608
720
class TestGlobalConfigSavingOptions(tests.TestCaseInTempDir):
1466
1579
'user': user, 'realm': realm}
1468
1581
stdout = tests.StringIOWrapper()
1582
stderr = tests.StringIOWrapper()
1469
1583
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1584
stdout=stdout, stderr=stderr)
1471
1585
# We use an empty conf so that the user is always prompted
1472
1586
conf = config.AuthenticationConfig()
1473
1587
self.assertEquals(password,
1474
1588
conf.get_password(scheme, host, user, port=port,
1475
1589
realm=realm, path=path))
1476
self.assertEquals(stdout.getvalue(), expected_prompt)
1478
def test_default_prompts(self):
1479
# HTTP prompts can't be tested here, see test_http.py
1480
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1481
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1484
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1590
self.assertEquals(expected_prompt, stderr.getvalue())
1591
self.assertEquals('', stdout.getvalue())
1593
def _check_default_username_prompt(self, expected_prompt_format, scheme,
1594
host=None, port=None, realm=None,
1599
expected_prompt = expected_prompt_format % {
1600
'scheme': scheme, 'host': host, 'port': port,
1602
stdout = tests.StringIOWrapper()
1603
stderr = tests.StringIOWrapper()
1604
ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
1605
stdout=stdout, stderr=stderr)
1606
# We use an empty conf so that the user is always prompted
1607
conf = config.AuthenticationConfig()
1608
self.assertEquals(username, conf.get_user(scheme, host, port=port,
1609
realm=realm, path=path, ask=True))
1610
self.assertEquals(expected_prompt, stderr.getvalue())
1611
self.assertEquals('', stdout.getvalue())
1613
def test_username_defaults_prompts(self):
1614
# HTTP prompts can't be tested here, see test_http.py
1615
self._check_default_username_prompt('FTP %(host)s username: ', 'ftp')
1616
self._check_default_username_prompt(
1617
'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
1618
self._check_default_username_prompt(
1619
'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
1621
def test_username_default_no_prompt(self):
1622
conf = config.AuthenticationConfig()
1623
self.assertEquals(None,
1624
conf.get_user('ftp', 'example.com'))
1625
self.assertEquals("explicitdefault",
1626
conf.get_user('ftp', 'example.com', default="explicitdefault"))
1628
def test_password_default_prompts(self):
1629
# HTTP prompts can't be tested here, see test_http.py
1630
self._check_default_password_prompt(
1631
'FTP %(user)s@%(host)s password: ', 'ftp')
1632
self._check_default_password_prompt(
1633
'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
1634
self._check_default_password_prompt(
1635
'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
1486
1636
# SMTP port handling is a bit special (it's handled if embedded in the
1488
1638
# FIXME: should we: forbid that, extend it to other schemes, leave
1489
1639
# things as they are that's fine thank you ?
1490
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1492
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1493
'smtp', host='bar.org:10025')
1494
self._check_default_prompt(
1640
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1642
self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1643
'smtp', host='bar.org:10025')
1644
self._check_default_password_prompt(
1495
1645
'SMTP %(user)s@%(host)s:%(port)d password: ',
1496
1646
'smtp', port=10025)
1537
1690
# No warning shoud be emitted since there is no password. We are only
1538
1691
# providing "user".
1539
1692
self.assertNotContainsRe(
1540
self._get_log(keep_log_file=True),
1541
1694
'password ignored in section \[ssh with password\]')
1696
def test_uses_fallback_stores(self):
1697
self.overrideAttr(config, 'credential_store_registry',
1698
config.CredentialStoreRegistry())
1699
store = StubCredentialStore()
1700
store.add_credentials("http", "example.com", "joe", "secret")
1701
config.credential_store_registry.register("stub", store, fallback=True)
1702
conf = config.AuthenticationConfig(_file=StringIO())
1703
creds = conf.get_credentials("http", "example.com")
1704
self.assertEquals("joe", creds["user"])
1705
self.assertEquals("secret", creds["password"])
1708
class StubCredentialStore(config.CredentialStore):
1714
def add_credentials(self, scheme, host, user, password=None):
1715
self._username[(scheme, host)] = user
1716
self._password[(scheme, host)] = password
1718
def get_credentials(self, scheme, host, port=None, user=None,
1719
path=None, realm=None):
1720
key = (scheme, host)
1721
if not key in self._username:
1723
return { "scheme": scheme, "host": host, "port": port,
1724
"user": self._username[key], "password": self._password[key]}
1727
class CountingCredentialStore(config.CredentialStore):
1732
def get_credentials(self, scheme, host, port=None, user=None,
1733
path=None, realm=None):
1544
1738
class TestCredentialStoreRegistry(tests.TestCase):
1557
1751
# 'unknown' so we use that as an never registered key.
1558
1752
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
1754
def test_fallback_none_registered(self):
1755
r = config.CredentialStoreRegistry()
1756
self.assertEquals(None,
1757
r.get_fallback_credentials("http", "example.com"))
1759
def test_register(self):
1760
r = config.CredentialStoreRegistry()
1761
r.register("stub", StubCredentialStore(), fallback=False)
1762
r.register("another", StubCredentialStore(), fallback=True)
1763
self.assertEquals(["another", "stub"], r.keys())
1765
def test_register_lazy(self):
1766
r = config.CredentialStoreRegistry()
1767
r.register_lazy("stub", "bzrlib.tests.test_config",
1768
"StubCredentialStore", fallback=False)
1769
self.assertEquals(["stub"], r.keys())
1770
self.assertIsInstance(r.get_credential_store("stub"),
1771
StubCredentialStore)
1773
def test_is_fallback(self):
1774
r = config.CredentialStoreRegistry()
1775
r.register("stub1", None, fallback=False)
1776
r.register("stub2", None, fallback=True)
1777
self.assertEquals(False, r.is_fallback("stub1"))
1778
self.assertEquals(True, r.is_fallback("stub2"))
1780
def test_no_fallback(self):
1781
r = config.CredentialStoreRegistry()
1782
store = CountingCredentialStore()
1783
r.register("count", store, fallback=False)
1784
self.assertEquals(None,
1785
r.get_fallback_credentials("http", "example.com"))
1786
self.assertEquals(0, store._calls)
1788
def test_fallback_credentials(self):
1789
r = config.CredentialStoreRegistry()
1790
store = StubCredentialStore()
1791
store.add_credentials("http", "example.com",
1792
"somebody", "geheim")
1793
r.register("stub", store, fallback=True)
1794
creds = r.get_fallback_credentials("http", "example.com")
1795
self.assertEquals("somebody", creds["user"])
1796
self.assertEquals("geheim", creds["password"])
1798
def test_fallback_first_wins(self):
1799
r = config.CredentialStoreRegistry()
1800
stub1 = StubCredentialStore()
1801
stub1.add_credentials("http", "example.com",
1802
"somebody", "stub1")
1803
r.register("stub1", stub1, fallback=True)
1804
stub2 = StubCredentialStore()
1805
stub2.add_credentials("http", "example.com",
1806
"somebody", "stub2")
1807
r.register("stub2", stub1, fallback=True)
1808
creds = r.get_fallback_credentials("http", "example.com")
1809
self.assertEquals("somebody", creds["user"])
1810
self.assertEquals("stub1", creds["password"])
1561
1813
class TestPlainTextCredentialStore(tests.TestCase):