25
25
#import bzrlib specific imports here
26
26
import bzrlib.config as config
27
27
import bzrlib.errors as errors
28
from bzrlib.selftest import TestCase, TestCaseInTempDir
28
from bzrlib.tests import TestCase, TestCaseInTempDir
31
31
sample_config_text = ("[DEFAULT]\n"
96
96
def __init__(self):
97
97
self.base = "http://example.com/branches/demo"
98
self.control_files = FakeControlFiles()
101
class FakeControlFiles(object):
98
104
self.email = 'Robert Collins <robertc@example.net>\n'
100
def controlfile(self, filename, mode):
106
def get_utf8(self, filename):
101
107
if filename != 'email':
102
108
raise NotImplementedError
103
109
if self.email is not None:
104
110
return StringIO(self.email)
105
raise errors.NoSuchFile
111
raise errors.NoSuchFile(filename)
108
114
class InstrumentedConfig(config.Config):
177
183
super(TestConfigPath, self).setUp()
178
self.oldenv = os.environ.get('HOME', None)
184
self.old_home = os.environ.get('HOME', None)
185
self.old_appdata = os.environ.get('APPDATA', None)
179
186
os.environ['HOME'] = '/home/bogus'
187
os.environ['APPDATA'] = \
188
r'C:\Documents and Settings\bogus\Application Data'
181
190
def tearDown(self):
182
os.environ['HOME'] = self.oldenv
191
if self.old_home is None:
192
del os.environ['HOME']
194
os.environ['HOME'] = self.old_home
195
if self.old_appdata is None:
196
del os.environ['APPDATA']
198
os.environ['APPDATA'] = self.old_appdata
183
199
super(TestConfigPath, self).tearDown()
185
201
def test_config_dir(self):
186
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
202
if sys.platform == 'win32':
203
self.assertEqual(config.config_dir(),
204
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
206
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
188
208
def test_config_filename(self):
189
self.assertEqual(config.config_filename(),
190
'/home/bogus/.bazaar/bazaar.conf')
209
if sys.platform == 'win32':
210
self.assertEqual(config.config_filename(),
211
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
213
self.assertEqual(config.config_filename(),
214
'/home/bogus/.bazaar/bazaar.conf')
192
216
def test_branches_config_filename(self):
193
self.assertEqual(config.branches_config_filename(),
194
'/home/bogus/.bazaar/branches.conf')
217
if sys.platform == 'win32':
218
self.assertEqual(config.branches_config_filename(),
219
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
221
self.assertEqual(config.branches_config_filename(),
222
'/home/bogus/.bazaar/branches.conf')
196
224
class TestIniConfig(TestCase):
470
497
def test_post_commit_default(self):
471
498
self.get_location_config('/a/c')
472
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
499
self.assertEqual('bzrlib.tests.test_config.post_commit',
473
500
self.my_config.post_commit())
503
class TestLocationConfig(TestCaseInTempDir):
505
def get_location_config(self, location, global_config=None):
506
if global_config is None:
507
global_file = StringIO(sample_config_text)
509
global_file = StringIO(global_config)
510
branches_file = StringIO(sample_branches_text)
511
self.my_config = config.LocationConfig(location)
512
self.my_config._get_parser(branches_file)
513
self.my_config._get_global_config()._get_parser(global_file)
475
515
def test_set_user_setting_sets_and_saves(self):
476
# TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
477
516
self.get_location_config('/a/c')
478
517
record = InstrumentedConfigObj("foo")
479
518
self.my_config._parser = record
480
self.my_config.set_user_option('foo', 'bar')
520
real_mkdir = os.mkdir
522
def checked_mkdir(path, mode=0777):
523
self.log('making directory: %s', path)
524
real_mkdir(path, mode)
527
os.mkdir = checked_mkdir
529
self.my_config.set_user_option('foo', 'bar')
531
os.mkdir = real_mkdir
533
self.failUnless(self.created, 'Failed to create ~/.bazaar')
481
534
self.assertEqual([('__contains__', '/a/c'),
482
535
('__contains__', '/a/c/'),
483
536
('__setitem__', '/a/c', {}),
494
547
my_config = config.BranchConfig(branch)
495
548
self.assertEqual("Robert Collins <robertc@example.net>",
496
549
my_config._get_user_id())
497
branch.email = "John"
550
branch.control_files.email = "John"
498
551
self.assertEqual("John", my_config._get_user_id())
500
553
def test_not_set_in_branch(self):
501
554
branch = FakeBranch()
502
555
my_config = config.BranchConfig(branch)
556
branch.control_files.email = None
504
557
config_file = StringIO(sample_config_text)
505
558
(my_config._get_location_config().
506
559
_get_global_config()._get_parser(config_file))
507
560
self.assertEqual("Robert Collins <robertc@example.com>",
508
561
my_config._get_user_id())
509
branch.email = "John"
562
branch.control_files.email = "John"
510
563
self.assertEqual("John", my_config._get_user_id())
512
565
def test_BZREMAIL_OVERRIDES(self):
550
603
_get_global_config()._get_parser(config_file))
551
604
branch_file = StringIO(sample_branches_text)
552
605
my_config._get_location_config()._get_parser(branch_file)
553
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
606
self.assertEqual('bzrlib.tests.test_config.post_commit',
554
607
my_config.post_commit())
610
class TestMailAddressExtraction(TestCase):
612
def test_extract_email_address(self):
613
self.assertEqual('jane@test.com',
614
config.extract_email_address('Jane <jane@test.com>'))
615
self.assertRaises(errors.BzrError,
616
config.extract_email_address, 'Jane Tester')