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"
32
32
"email=Robert Collins <robertc@example.com>\n"
34
"gpg_signing_command=gnome-gpg\n"
35
"user_global_option=something\n")
34
"gpg_signing_command=gnome-gpg\n")
38
37
sample_always_signatures = ("[DEFAULT]\n"
58
57
"# test trailing / matching with no children\n"
60
59
"check_signatures=check-available\n"
61
"gpg_signing_command=false\n"
62
"user_local_option=local\n"
63
60
"# test trailing / matching\n"
65
62
"#subdirs will match but not the parent\n"
68
65
"check_signatures=ignore\n"
69
"post_commit=bzrlib.tests.test_config.post_commit\n"
70
66
"#testing explicit beats globs\n")
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',))
69
class InstrumentedConfigParser(object):
70
"""A config parser look-enough-alike to record calls made to it."""
75
def read(self, filenames):
76
self._calls.append(('read', filenames))
94
79
class FakeBranch(object):
158
143
my_config.signature_checking())
159
144
self.assertEqual(['_get_signature_checking'], my_config._calls)
161
def test_gpg_signing_command_default(self):
162
my_config = config.Config()
163
self.assertEqual('gpg', my_config.gpg_signing_command())
165
def test_get_user_option_default(self):
166
my_config = config.Config()
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())
174
147
class TestConfigPath(TestCase):
177
150
super(TestConfigPath, self).setUp()
178
self.old_home = os.environ.get('HOME', None)
179
self.old_appdata = os.environ.get('APPDATA', None)
151
self.oldenv = os.environ.get('HOME', None)
180
152
os.environ['HOME'] = '/home/bogus'
181
os.environ['APPDATA'] = \
182
r'C:\Documents and Settings\bogus\Application Data'
184
154
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
193
super(TestConfigPath, self).tearDown()
155
os.environ['HOME'] = self.oldenv
195
157
def test_config_dir(self):
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')
158
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
202
160
def test_config_filename(self):
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')
161
self.assertEqual(config.config_filename(),
162
'/home/bogus/.bazaar/bazaar.conf')
210
164
def test_branches_config_filename(self):
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')
165
self.assertEqual(config.branches_config_filename(),
166
'/home/bogus/.bazaar/branches.conf')
218
168
class TestIniConfig(TestCase):
242
193
def test_calls_read_filenames(self):
243
194
# replace the class that is constructured, to check its parameters
244
oldparserclass = config.ConfigObj
245
config.ConfigObj = InstrumentedConfigObj
195
oldparserclass = config.ConfigParser
196
config.ConfigParser = InstrumentedConfigParser
246
197
my_config = config.GlobalConfig()
248
199
parser = my_config._get_parser()
250
config.ConfigObj = oldparserclass
251
self.failUnless(isinstance(parser, InstrumentedConfigObj))
252
self.assertEqual(parser._calls, [('__init__', config.config_filename())])
201
config.ConfigParser = oldparserclass
202
self.failUnless(isinstance(parser, InstrumentedConfigParser))
203
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
255
206
class TestBranchConfig(TestCaseInTempDir):
267
218
self.failUnless(location_config is my_config._get_location_config())
270
class TestGlobalConfigItems(TestCase):
221
class TestConfigItems(TestCase):
224
super(TestConfigItems, self).setUp()
225
self.bzr_email = os.environ.get('BZREMAIL')
226
if self.bzr_email is not None:
227
del os.environ['BZREMAIL']
228
self.email = os.environ.get('EMAIL')
229
if self.email is not None:
230
del os.environ['EMAIL']
231
self.oldenv = os.environ.get('HOME', None)
232
os.environ['HOME'] = os.getcwd()
235
os.environ['HOME'] = self.oldenv
236
if os.environ.get('BZREMAIL') is not None:
237
del os.environ['BZREMAIL']
238
if self.bzr_email is not None:
239
os.environ['BZREMAIL'] = self.bzr_email
240
if self.email is not None:
241
os.environ['EMAIL'] = self.email
242
super(TestConfigItems, self).tearDown()
245
class TestGlobalConfigItems(TestConfigItems):
272
247
def test_user_id(self):
273
248
config_file = StringIO(sample_config_text)
312
287
my_config.signature_checking())
313
288
self.assertEqual(False, my_config.signature_needed())
315
def _get_sample_config(self):
316
config_file = StringIO(sample_config_text)
317
my_config = config.GlobalConfig()
318
my_config._parser = my_config._get_parser(file=config_file)
321
def test_gpg_signing_command(self):
322
my_config = self._get_sample_config()
323
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
324
self.assertEqual(False, my_config.signature_needed())
326
def _get_empty_config(self):
327
config_file = StringIO("")
328
my_config = config.GlobalConfig()
329
my_config._parser = my_config._get_parser(file=config_file)
332
def test_gpg_signing_command_unset(self):
333
my_config = self._get_empty_config()
334
self.assertEqual("gpg", my_config.gpg_signing_command())
336
def test_get_user_option_default(self):
337
my_config = self._get_empty_config()
338
self.assertEqual(None, my_config.get_user_option('no_option'))
340
def test_get_user_option_global(self):
341
my_config = self._get_sample_config()
342
self.assertEqual("something",
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())
350
class TestLocationConfig(TestCase):
291
class TestLocationConfig(TestConfigItems):
352
293
def test_constructs(self):
353
294
my_config = config.LocationConfig('http://example.com')
354
295
self.assertRaises(TypeError, config.LocationConfig)
356
297
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.
360
298
# replace the class that is constructured, to check its parameters
361
oldparserclass = config.ConfigObj
362
config.ConfigObj = InstrumentedConfigObj
299
oldparserclass = config.ConfigParser
300
config.ConfigParser = InstrumentedConfigParser
363
301
my_config = config.LocationConfig('http://www.example.com')
365
303
parser = my_config._get_parser()
367
config.ConfigObj = oldparserclass
368
self.failUnless(isinstance(parser, InstrumentedConfigObj))
369
self.assertEqual(parser._calls,
370
[('__init__', config.branches_config_filename())])
305
config.ConfigParser = oldparserclass
306
self.failUnless(isinstance(parser, InstrumentedConfigParser))
307
self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
372
309
def test_get_global_config(self):
373
310
my_config = config.LocationConfig('http://example.com')
470
407
self.assertEqual(config.CHECK_ALWAYS,
471
408
self.my_config.signature_checking())
473
def test_gpg_signing_command(self):
474
self.get_location_config('/b')
475
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
477
def test_gpg_signing_command_missing(self):
478
self.get_location_config('/a')
479
self.assertEqual("false", self.my_config.gpg_signing_command())
481
def test_get_user_option_global(self):
482
self.get_location_config('/a')
483
self.assertEqual('something',
484
self.my_config.get_user_option('user_global_option'))
486
def test_get_user_option_local(self):
487
self.get_location_config('/a')
488
self.assertEqual('local',
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.tests.test_config.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'),
528
class TestBranchConfigItems(TestCase):
411
class TestBranchConfigItems(TestConfigItems):
530
413
def test_user_id(self):
531
414
branch = FakeBranch()
561
444
(my_config._get_location_config().
562
445
_get_global_config()._get_parser(config_file))
563
446
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
565
def test_gpg_signing_command(self):
566
branch = FakeBranch()
567
my_config = config.BranchConfig(branch)
568
config_file = StringIO(sample_config_text)
569
(my_config._get_location_config().
570
_get_global_config()._get_parser(config_file))
571
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
573
def test_get_user_option_global(self):
574
branch = FakeBranch()
575
my_config = config.BranchConfig(branch)
576
config_file = StringIO(sample_config_text)
577
(my_config._get_location_config().
578
_get_global_config()._get_parser(config_file))
579
self.assertEqual('something',
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.tests.test_config.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')