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"
68
68
"check_signatures=ignore\n"
69
"post_commit=bzrlib.tests.test_config.post_commit\n"
69
70
"#testing explicit beats globs\n")
72
class InstrumentedConfigParser(object):
73
"""A config parser look-enough-alike to record calls made to it."""
78
def read(self, filenames):
79
self._calls.append(('read', filenames))
73
class InstrumentedConfigObj(object):
74
"""A config obj look-enough-alike to record calls made to it."""
76
def __contains__(self, thing):
77
self._calls.append(('__contains__', thing))
80
def __getitem__(self, key):
81
self._calls.append(('__getitem__', key))
84
def __init__(self, input):
85
self._calls = [('__init__', input)]
87
def __setitem__(self, key, value):
88
self._calls.append(('__setitem__', key, value))
91
self._calls.append(('write',))
82
94
class FakeBranch(object):
84
96
def __init__(self):
85
97
self.base = "http://example.com/branches/demo"
98
self.control_files = FakeControlFiles()
101
class FakeControlFiles(object):
86
104
self.email = 'Robert Collins <robertc@example.net>\n'
88
def controlfile(self, filename, mode):
106
def get_utf8(self, filename):
89
107
if filename != 'email':
90
108
raise NotImplementedError
91
109
if self.email is not None:
92
110
return StringIO(self.email)
93
raise errors.NoSuchFile
111
raise errors.NoSuchFile(filename)
96
114
class InstrumentedConfig(config.Config):
154
172
my_config = config.Config()
155
173
self.assertEqual(None, my_config.get_user_option('no_option'))
175
def test_post_commit_default(self):
176
my_config = config.Config()
177
self.assertEqual(None, my_config.post_commit())
158
180
class TestConfigPath(TestCase):
161
183
super(TestConfigPath, self).setUp()
162
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)
163
186
os.environ['HOME'] = '/home/bogus'
187
os.environ['APPDATA'] = \
188
r'C:\Documents and Settings\bogus\Application Data'
165
190
def tearDown(self):
166
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
167
199
super(TestConfigPath, self).tearDown()
169
201
def test_config_dir(self):
170
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')
172
208
def test_config_filename(self):
173
self.assertEqual(config.config_filename(),
174
'/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')
176
216
def test_branches_config_filename(self):
177
self.assertEqual(config.branches_config_filename(),
178
'/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')
180
224
class TestIniConfig(TestCase):
204
248
def test_calls_read_filenames(self):
205
249
# replace the class that is constructured, to check its parameters
206
oldparserclass = config.ConfigParser
207
config.ConfigParser = InstrumentedConfigParser
250
oldparserclass = config.ConfigObj
251
config.ConfigObj = InstrumentedConfigObj
208
252
my_config = config.GlobalConfig()
210
254
parser = my_config._get_parser()
212
config.ConfigParser = oldparserclass
213
self.failUnless(isinstance(parser, InstrumentedConfigParser))
214
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
256
config.ConfigObj = oldparserclass
257
self.failUnless(isinstance(parser, InstrumentedConfigObj))
258
self.assertEqual(parser._calls, [('__init__', config.config_filename())])
217
261
class TestBranchConfig(TestCaseInTempDir):
303
347
my_config = self._get_sample_config()
304
348
self.assertEqual("something",
305
349
my_config.get_user_option('user_global_option'))
351
def test_post_commit_default(self):
352
my_config = self._get_sample_config()
353
self.assertEqual(None, my_config.post_commit())
308
356
class TestLocationConfig(TestCase):
312
360
self.assertRaises(TypeError, config.LocationConfig)
314
362
def test_branch_calls_read_filenames(self):
363
# This is testing the correct file names are provided.
364
# TODO: consolidate with the test for GlobalConfigs filename checks.
315
366
# replace the class that is constructured, to check its parameters
316
oldparserclass = config.ConfigParser
317
config.ConfigParser = InstrumentedConfigParser
367
oldparserclass = config.ConfigObj
368
config.ConfigObj = InstrumentedConfigObj
318
369
my_config = config.LocationConfig('http://www.example.com')
320
371
parser = my_config._get_parser()
322
config.ConfigParser = oldparserclass
323
self.failUnless(isinstance(parser, InstrumentedConfigParser))
324
self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
373
config.ConfigObj = oldparserclass
374
self.failUnless(isinstance(parser, InstrumentedConfigObj))
375
self.assertEqual(parser._calls,
376
[('__init__', config.branches_config_filename())])
326
378
def test_get_global_config(self):
327
379
my_config = config.LocationConfig('http://example.com')
441
493
self.get_location_config('/a')
442
494
self.assertEqual('local',
443
495
self.my_config.get_user_option('user_local_option'))
497
def test_post_commit_default(self):
498
self.get_location_config('/a/c')
499
self.assertEqual('bzrlib.tests.test_config.post_commit',
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)
515
def test_set_user_setting_sets_and_saves(self):
516
self.get_location_config('/a/c')
517
record = InstrumentedConfigObj("foo")
518
self.my_config._parser = record
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')
534
self.assertEqual([('__contains__', '/a/c'),
535
('__contains__', '/a/c/'),
536
('__setitem__', '/a/c', {}),
537
('__getitem__', '/a/c'),
538
('__setitem__', 'foo', 'bar'),
446
543
class TestBranchConfigItems(TestCase):
450
547
my_config = config.BranchConfig(branch)
451
548
self.assertEqual("Robert Collins <robertc@example.net>",
452
549
my_config._get_user_id())
453
branch.email = "John"
550
branch.control_files.email = "John"
454
551
self.assertEqual("John", my_config._get_user_id())
456
553
def test_not_set_in_branch(self):
457
554
branch = FakeBranch()
458
555
my_config = config.BranchConfig(branch)
556
branch.control_files.email = None
460
557
config_file = StringIO(sample_config_text)
461
558
(my_config._get_location_config().
462
559
_get_global_config()._get_parser(config_file))
463
560
self.assertEqual("Robert Collins <robertc@example.com>",
464
561
my_config._get_user_id())
465
branch.email = "John"
562
branch.control_files.email = "John"
466
563
self.assertEqual("John", my_config._get_user_id())
468
565
def test_BZREMAIL_OVERRIDES(self):
496
593
_get_global_config()._get_parser(config_file))
497
594
self.assertEqual('something',
498
595
my_config.get_user_option('user_global_option'))
597
def test_post_commit_default(self):
598
branch = FakeBranch()
600
my_config = config.BranchConfig(branch)
601
config_file = StringIO(sample_config_text)
602
(my_config._get_location_config().
603
_get_global_config()._get_parser(config_file))
604
branch_file = StringIO(sample_branches_text)
605
my_config._get_location_config()._get_parser(branch_file)
606
self.assertEqual('bzrlib.tests.test_config.post_commit',
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')