68
68
"check_signatures=ignore\n"
69
"post_commit=bzrlib.selftest.testconfig.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):
154
166
my_config = config.Config()
155
167
self.assertEqual(None, my_config.get_user_option('no_option'))
169
def test_post_commit_default(self):
170
my_config = config.Config()
171
self.assertEqual(None, my_config.post_commit())
158
174
class TestConfigPath(TestCase):
161
177
super(TestConfigPath, self).setUp()
162
self.oldenv = os.environ.get('HOME', None)
178
self.old_home = os.environ.get('HOME', None)
179
self.old_appdata = os.environ.get('APPDATA', None)
163
180
os.environ['HOME'] = '/home/bogus'
181
os.environ['APPDATA'] = \
182
r'C:\Documents and Settings\bogus\Application Data'
165
184
def tearDown(self):
166
os.environ['HOME'] = self.oldenv
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
167
193
super(TestConfigPath, self).tearDown()
169
195
def test_config_dir(self):
170
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
196
if sys.platform == 'win32':
197
self.assertEqual(config.config_dir(),
198
r'C:\Documents and Settings\bogus\Application Data\bazaar\2.0')
200
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
172
202
def test_config_filename(self):
173
self.assertEqual(config.config_filename(),
174
'/home/bogus/.bazaar/bazaar.conf')
203
if sys.platform == 'win32':
204
self.assertEqual(config.config_filename(),
205
r'C:\Documents and Settings\bogus\Application Data\bazaar\2.0\bazaar.conf')
207
self.assertEqual(config.config_filename(),
208
'/home/bogus/.bazaar/bazaar.conf')
176
210
def test_branches_config_filename(self):
177
self.assertEqual(config.branches_config_filename(),
178
'/home/bogus/.bazaar/branches.conf')
211
if sys.platform == 'win32':
212
self.assertEqual(config.branches_config_filename(),
213
r'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')
180
218
class TestIniConfig(TestCase):
204
242
def test_calls_read_filenames(self):
205
243
# replace the class that is constructured, to check its parameters
206
oldparserclass = config.ConfigParser
207
config.ConfigParser = InstrumentedConfigParser
244
oldparserclass = config.ConfigObj
245
config.ConfigObj = InstrumentedConfigObj
208
246
my_config = config.GlobalConfig()
210
248
parser = my_config._get_parser()
212
config.ConfigParser = oldparserclass
213
self.failUnless(isinstance(parser, InstrumentedConfigParser))
214
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
250
config.ConfigObj = oldparserclass
251
self.failUnless(isinstance(parser, InstrumentedConfigObj))
252
self.assertEqual(parser._calls, [('__init__', config.config_filename())])
217
255
class TestBranchConfig(TestCaseInTempDir):
303
341
my_config = self._get_sample_config()
304
342
self.assertEqual("something",
305
343
my_config.get_user_option('user_global_option'))
345
def test_post_commit_default(self):
346
my_config = self._get_sample_config()
347
self.assertEqual(None, my_config.post_commit())
308
350
class TestLocationConfig(TestCase):
312
354
self.assertRaises(TypeError, config.LocationConfig)
314
356
def test_branch_calls_read_filenames(self):
357
# This is testing the correct file names are provided.
358
# TODO: consolidate with the test for GlobalConfigs filename checks.
315
360
# replace the class that is constructured, to check its parameters
316
oldparserclass = config.ConfigParser
317
config.ConfigParser = InstrumentedConfigParser
361
oldparserclass = config.ConfigObj
362
config.ConfigObj = InstrumentedConfigObj
318
363
my_config = config.LocationConfig('http://www.example.com')
320
365
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()])])
367
config.ConfigObj = oldparserclass
368
self.failUnless(isinstance(parser, InstrumentedConfigObj))
369
self.assertEqual(parser._calls,
370
[('__init__', config.branches_config_filename())])
326
372
def test_get_global_config(self):
327
373
my_config = config.LocationConfig('http://example.com')
441
487
self.get_location_config('/a')
442
488
self.assertEqual('local',
443
489
self.my_config.get_user_option('user_local_option'))
491
def test_post_commit_default(self):
492
self.get_location_config('/a/c')
493
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
494
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
def test_set_user_setting_sets_and_saves(self):
510
# TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
511
self.get_location_config('/a/c')
512
record = InstrumentedConfigObj("foo")
513
self.my_config._parser = record
515
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
516
# broken: creates .bazaar in the top-level directory, not
517
# inside the test directory
518
self.my_config.set_user_option('foo', 'bar')
519
self.assertEqual([('__contains__', '/a/c'),
520
('__contains__', '/a/c/'),
521
('__setitem__', '/a/c', {}),
522
('__getitem__', '/a/c'),
523
('__setitem__', 'foo', 'bar'),
446
528
class TestBranchConfigItems(TestCase):
496
578
_get_global_config()._get_parser(config_file))
497
579
self.assertEqual('something',
498
580
my_config.get_user_option('user_global_option'))
582
def test_post_commit_default(self):
583
branch = FakeBranch()
585
my_config = config.BranchConfig(branch)
586
config_file = StringIO(sample_config_text)
587
(my_config._get_location_config().
588
_get_global_config()._get_parser(config_file))
589
branch_file = StringIO(sample_branches_text)
590
my_config._get_location_config()._get_parser(branch_file)
591
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
592
my_config.post_commit())
595
class TestMailAddressExtraction(TestCase):
597
def test_extract_email_address(self):
598
self.assertEqual('jane@test.com',
599
config.extract_email_address('Jane <jane@test.com>'))
600
self.assertRaises(errors.BzrError,
601
config.extract_email_address, 'Jane Tester')