316
321
my_config = config.Config()
317
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)
320
333
class TestConfigPath(tests.TestCase):
323
336
super(TestConfigPath, self).setUp()
324
337
os.environ['HOME'] = '/home/bogus'
338
os.environ['XDG_CACHE_DIR'] = ''
325
339
if sys.platform == 'win32':
326
340
os.environ['BZR_HOME'] = \
327
341
r'C:\Documents and Settings\bogus\Application Data'
349
363
self.assertEqual(config.authentication_config_filename(),
350
364
self.bzr_home + '/authentication.conf')
353
class TestIniConfig(tests.TestCase):
366
def test_xdg_cache_dir(self):
367
self.assertEqual(config.xdg_cache_dir(),
368
'/home/bogus/.cache')
371
class TestIniConfig(tests.TestCaseInTempDir):
373
def make_config_parser(self, s):
374
conf = config.IniBasedConfig(None)
375
parser = conf._get_parser(file=StringIO(s.encode('utf-8')))
379
class TestIniConfigBuilding(TestIniConfig):
355
381
def test_contructs(self):
356
382
my_config = config.IniBasedConfig("nothing")
368
394
parser = my_config._get_parser(file=config_file)
369
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'))
372
468
class TestGetConfig(tests.TestCase):
608
704
my_config = self._get_sample_config()
609
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)
612
720
class TestGlobalConfigSavingOptions(tests.TestCaseInTempDir):
1549
1657
entered_password = 'typed-by-hand'
1550
1658
stdout = tests.StringIOWrapper()
1659
stderr = tests.StringIOWrapper()
1551
1660
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1661
stdout=stdout, stderr=stderr)
1554
1663
# Since the password defined in the authentication config is ignored,
1555
1664
# the user is prompted
1556
1665
self.assertEquals(entered_password,
1557
1666
conf.get_password('ssh', 'bar.org', user='jim'))
1558
1667
self.assertContainsRe(
1559
self._get_log(keep_log_file=True),
1560
1669
'password ignored in section \[ssh with password\]')
1562
1671
def test_ssh_without_password_doesnt_emit_warning(self):
1579
1690
# No warning shoud be emitted since there is no password. We are only
1580
1691
# providing "user".
1581
1692
self.assertNotContainsRe(
1582
self._get_log(keep_log_file=True),
1583
1694
'password ignored in section \[ssh with password\]')
1585
1696
def test_uses_fallback_stores(self):
1586
self._old_cs_registry = config.credential_store_registry
1588
config.credential_store_registry = self._old_cs_registry
1589
self.addCleanup(restore)
1590
config.credential_store_registry = config.CredentialStoreRegistry()
1697
self.overrideAttr(config, 'credential_store_registry',
1698
config.CredentialStoreRegistry())
1591
1699
store = StubCredentialStore()
1592
1700
store.add_credentials("http", "example.com", "joe", "secret")
1593
1701
config.credential_store_registry.register("stub", store, fallback=True)