25
25
#import bzrlib specific imports here
26
26
import bzrlib.config as config
27
27
import bzrlib.errors as errors
28
from bzrlib.tests import TestCase, TestCaseInTempDir
28
from bzrlib.selftest import TestCase, TestCaseInTempDir
31
31
sample_config_text = ("[DEFAULT]\n"
177
177
super(TestConfigPath, self).setUp()
178
self.old_home = os.environ.get('HOME', None)
179
self.old_appdata = os.environ.get('APPDATA', None)
178
self.oldenv = os.environ.get('HOME', None)
180
179
os.environ['HOME'] = '/home/bogus'
181
os.environ['APPDATA'] = \
182
r'C:\Documents and Settings\bogus\Application Data'
184
181
def tearDown(self):
185
if self.old_home is None:
186
del os.environ['HOME']
188
os.environ['HOME'] = self.old_home
189
if self.old_appdata is None:
190
del os.environ['APPDATA']
192
os.environ['APPDATA'] = self.old_appdata
182
os.environ['HOME'] = self.oldenv
193
183
super(TestConfigPath, self).tearDown()
195
185
def test_config_dir(self):
196
if sys.platform == 'win32':
197
self.assertEqual(config.config_dir(),
198
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
200
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
186
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
202
188
def test_config_filename(self):
203
if sys.platform == 'win32':
204
self.assertEqual(config.config_filename(),
205
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
207
self.assertEqual(config.config_filename(),
208
'/home/bogus/.bazaar/bazaar.conf')
189
self.assertEqual(config.config_filename(),
190
'/home/bogus/.bazaar/bazaar.conf')
210
192
def test_branches_config_filename(self):
211
if sys.platform == 'win32':
212
self.assertEqual(config.branches_config_filename(),
213
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
215
self.assertEqual(config.branches_config_filename(),
216
'/home/bogus/.bazaar/branches.conf')
193
self.assertEqual(config.branches_config_filename(),
194
'/home/bogus/.bazaar/branches.conf')
218
196
class TestIniConfig(TestCase):
491
470
def test_post_commit_default(self):
492
471
self.get_location_config('/a/c')
493
self.assertEqual('bzrlib.tests.test_config.post_commit',
472
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
494
473
self.my_config.post_commit())
497
class TestLocationConfig(TestCaseInTempDir):
499
def get_location_config(self, location, global_config=None):
500
if global_config is None:
501
global_file = StringIO(sample_config_text)
503
global_file = StringIO(global_config)
504
branches_file = StringIO(sample_branches_text)
505
self.my_config = config.LocationConfig(location)
506
self.my_config._get_parser(branches_file)
507
self.my_config._get_global_config()._get_parser(global_file)
509
475
def test_set_user_setting_sets_and_saves(self):
476
# TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
510
477
self.get_location_config('/a/c')
511
478
record = InstrumentedConfigObj("foo")
512
479
self.my_config._parser = record
514
real_mkdir = os.mkdir
516
def checked_mkdir(path, mode=0777):
517
self.log('making directory: %s', path)
518
real_mkdir(path, mode)
521
os.mkdir = checked_mkdir
523
self.my_config.set_user_option('foo', 'bar')
525
os.mkdir = real_mkdir
527
self.failUnless(self.created, 'Failed to create ~/.bazaar')
480
self.my_config.set_user_option('foo', 'bar')
528
481
self.assertEqual([('__contains__', '/a/c'),
529
482
('__contains__', '/a/c/'),
530
483
('__setitem__', '/a/c', {}),
597
550
_get_global_config()._get_parser(config_file))
598
551
branch_file = StringIO(sample_branches_text)
599
552
my_config._get_location_config()._get_parser(branch_file)
600
self.assertEqual('bzrlib.tests.test_config.post_commit',
553
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
601
554
my_config.post_commit())
604
class TestMailAddressExtraction(TestCase):
606
def test_extract_email_address(self):
607
self.assertEqual('jane@test.com',
608
config.extract_email_address('Jane <jane@test.com>'))
609
self.assertRaises(errors.BzrError,
610
config.extract_email_address, 'Jane Tester')