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"
60
59
"check_signatures=check-available\n"
61
60
"gpg_signing_command=false\n"
62
"user_local_option=local\n"
63
61
"# test trailing / matching\n"
65
63
"#subdirs will match but not the parent\n"
68
66
"check_signatures=ignore\n"
69
"post_commit=bzrlib.tests.test_config.post_commit\n"
70
67
"#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',))
70
class InstrumentedConfigParser(object):
71
"""A config parser look-enough-alike to record calls made to it."""
76
def read(self, filenames):
77
self._calls.append(('read', filenames))
94
80
class FakeBranch(object):
96
82
def __init__(self):
97
83
self.base = "http://example.com/branches/demo"
98
self.control_files = FakeControlFiles()
101
class FakeControlFiles(object):
104
84
self.email = 'Robert Collins <robertc@example.net>\n'
106
def get_utf8(self, filename):
86
def controlfile(self, filename, mode):
107
87
if filename != 'email':
108
88
raise NotImplementedError
109
89
if self.email is not None:
110
90
return StringIO(self.email)
111
raise errors.NoSuchFile(filename)
91
raise errors.NoSuchFile
114
94
class InstrumentedConfig(config.Config):
168
148
my_config = config.Config()
169
149
self.assertEqual('gpg', my_config.gpg_signing_command())
171
def test_get_user_option_default(self):
172
my_config = config.Config()
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())
180
152
class TestConfigPath(TestCase):
183
155
super(TestConfigPath, self).setUp()
184
self.old_home = os.environ.get('HOME', None)
185
self.old_appdata = os.environ.get('APPDATA', None)
156
self.oldenv = os.environ.get('HOME', None)
186
157
os.environ['HOME'] = '/home/bogus'
187
os.environ['APPDATA'] = \
188
r'C:\Documents and Settings\bogus\Application Data'
190
159
def tearDown(self):
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
160
os.environ['HOME'] = self.oldenv
199
161
super(TestConfigPath, self).tearDown()
201
163
def test_config_dir(self):
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')
164
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
208
166
def test_config_filename(self):
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')
167
self.assertEqual(config.config_filename(),
168
'/home/bogus/.bazaar/bazaar.conf')
216
170
def test_branches_config_filename(self):
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')
171
self.assertEqual(config.branches_config_filename(),
172
'/home/bogus/.bazaar/branches.conf')
224
174
class TestIniConfig(TestCase):
248
198
def test_calls_read_filenames(self):
249
199
# replace the class that is constructured, to check its parameters
250
oldparserclass = config.ConfigObj
251
config.ConfigObj = InstrumentedConfigObj
200
oldparserclass = config.ConfigParser
201
config.ConfigParser = InstrumentedConfigParser
252
202
my_config = config.GlobalConfig()
254
204
parser = my_config._get_parser()
256
config.ConfigObj = oldparserclass
257
self.failUnless(isinstance(parser, InstrumentedConfigObj))
258
self.assertEqual(parser._calls, [('__init__', config.config_filename())])
206
config.ConfigParser = oldparserclass
207
self.failUnless(isinstance(parser, InstrumentedConfigParser))
208
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
261
211
class TestBranchConfig(TestCaseInTempDir):
318
268
my_config.signature_checking())
319
269
self.assertEqual(False, my_config.signature_needed())
321
def _get_sample_config(self):
271
def test_gpg_signing_command(self):
322
272
config_file = StringIO(sample_config_text)
323
273
my_config = config.GlobalConfig()
324
274
my_config._parser = my_config._get_parser(file=config_file)
327
def test_gpg_signing_command(self):
328
my_config = self._get_sample_config()
329
275
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
330
276
self.assertEqual(False, my_config.signature_needed())
332
def _get_empty_config(self):
333
config_file = StringIO("")
334
my_config = config.GlobalConfig()
335
my_config._parser = my_config._get_parser(file=config_file)
338
278
def test_gpg_signing_command_unset(self):
339
my_config = self._get_empty_config()
279
config_file = StringIO("")
280
my_config = config.GlobalConfig()
281
my_config._parser = my_config._get_parser(file=config_file)
340
282
self.assertEqual("gpg", my_config.gpg_signing_command())
342
def test_get_user_option_default(self):
343
my_config = self._get_empty_config()
344
self.assertEqual(None, my_config.get_user_option('no_option'))
346
def test_get_user_option_global(self):
347
my_config = self._get_sample_config()
348
self.assertEqual("something",
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())
356
285
class TestLocationConfig(TestCase):
360
289
self.assertRaises(TypeError, config.LocationConfig)
362
291
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.
366
292
# replace the class that is constructured, to check its parameters
367
oldparserclass = config.ConfigObj
368
config.ConfigObj = InstrumentedConfigObj
293
oldparserclass = config.ConfigParser
294
config.ConfigParser = InstrumentedConfigParser
369
295
my_config = config.LocationConfig('http://www.example.com')
371
297
parser = my_config._get_parser()
373
config.ConfigObj = oldparserclass
374
self.failUnless(isinstance(parser, InstrumentedConfigObj))
375
self.assertEqual(parser._calls,
376
[('__init__', config.branches_config_filename())])
299
config.ConfigParser = oldparserclass
300
self.failUnless(isinstance(parser, InstrumentedConfigParser))
301
self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
378
303
def test_get_global_config(self):
379
304
my_config = config.LocationConfig('http://example.com')
484
409
self.get_location_config('/a')
485
410
self.assertEqual("false", self.my_config.gpg_signing_command())
487
def test_get_user_option_global(self):
488
self.get_location_config('/a')
489
self.assertEqual('something',
490
self.my_config.get_user_option('user_global_option'))
492
def test_get_user_option_local(self):
493
self.get_location_config('/a')
494
self.assertEqual('local',
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'),
543
413
class TestBranchConfigItems(TestCase):
547
417
my_config = config.BranchConfig(branch)
548
418
self.assertEqual("Robert Collins <robertc@example.net>",
549
419
my_config._get_user_id())
550
branch.control_files.email = "John"
420
branch.email = "John"
551
421
self.assertEqual("John", my_config._get_user_id())
553
423
def test_not_set_in_branch(self):
554
424
branch = FakeBranch()
555
425
my_config = config.BranchConfig(branch)
556
branch.control_files.email = None
557
427
config_file = StringIO(sample_config_text)
558
428
(my_config._get_location_config().
559
429
_get_global_config()._get_parser(config_file))
560
430
self.assertEqual("Robert Collins <robertc@example.com>",
561
431
my_config._get_user_id())
562
branch.control_files.email = "John"
432
branch.email = "John"
563
433
self.assertEqual("John", my_config._get_user_id())
565
435
def test_BZREMAIL_OVERRIDES(self):
584
454
(my_config._get_location_config().
585
455
_get_global_config()._get_parser(config_file))
586
456
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
588
def test_get_user_option_global(self):
589
branch = FakeBranch()
590
my_config = config.BranchConfig(branch)
591
config_file = StringIO(sample_config_text)
592
(my_config._get_location_config().
593
_get_global_config()._get_parser(config_file))
594
self.assertEqual('something',
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')