18
18
"""Tests for finding and reading the bzr config file[s]."""
19
19
# import system imports here
20
from bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
20
from ConfigParser import ConfigParser
21
21
from cStringIO import StringIO
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.selftest.testconfig.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):
162
148
my_config = config.Config()
163
149
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
152
class TestConfigPath(TestCase):
220
198
def test_calls_read_filenames(self):
221
199
# replace the class that is constructured, to check its parameters
222
oldparserclass = config.ConfigObj
223
config.ConfigObj = InstrumentedConfigObj
200
oldparserclass = config.ConfigParser
201
config.ConfigParser = InstrumentedConfigParser
224
202
my_config = config.GlobalConfig()
226
204
parser = my_config._get_parser()
228
config.ConfigObj = oldparserclass
229
self.failUnless(isinstance(parser, InstrumentedConfigObj))
230
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()])])
233
211
class TestBranchConfig(TestCaseInTempDir):
290
268
my_config.signature_checking())
291
269
self.assertEqual(False, my_config.signature_needed())
293
def _get_sample_config(self):
271
def test_gpg_signing_command(self):
294
272
config_file = StringIO(sample_config_text)
295
273
my_config = config.GlobalConfig()
296
274
my_config._parser = my_config._get_parser(file=config_file)
299
def test_gpg_signing_command(self):
300
my_config = self._get_sample_config()
301
275
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
302
276
self.assertEqual(False, my_config.signature_needed())
304
def _get_empty_config(self):
305
config_file = StringIO("")
306
my_config = config.GlobalConfig()
307
my_config._parser = my_config._get_parser(file=config_file)
310
278
def test_gpg_signing_command_unset(self):
311
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)
312
282
self.assertEqual("gpg", my_config.gpg_signing_command())
314
def test_get_user_option_default(self):
315
my_config = self._get_empty_config()
316
self.assertEqual(None, my_config.get_user_option('no_option'))
318
def test_get_user_option_global(self):
319
my_config = self._get_sample_config()
320
self.assertEqual("something",
321
my_config.get_user_option('user_global_option'))
323
def test_post_commit_default(self):
324
my_config = self._get_sample_config()
325
self.assertEqual(None, my_config.post_commit())
329
285
class TestLocationConfig(TestCase):
333
289
self.assertRaises(TypeError, config.LocationConfig)
335
291
def test_branch_calls_read_filenames(self):
336
# This is testing the correct file names are provided.
337
# TODO: consolidate with the test for GlobalConfigs filename checks.
339
292
# replace the class that is constructured, to check its parameters
340
oldparserclass = config.ConfigObj
341
config.ConfigObj = InstrumentedConfigObj
293
oldparserclass = config.ConfigParser
294
config.ConfigParser = InstrumentedConfigParser
342
295
my_config = config.LocationConfig('http://www.example.com')
344
297
parser = my_config._get_parser()
346
config.ConfigObj = oldparserclass
347
self.failUnless(isinstance(parser, InstrumentedConfigObj))
348
self.assertEqual(parser._calls,
349
[('__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()])])
351
303
def test_get_global_config(self):
352
304
my_config = config.LocationConfig('http://example.com')
457
409
self.get_location_config('/a')
458
410
self.assertEqual("false", self.my_config.gpg_signing_command())
460
def test_get_user_option_global(self):
461
self.get_location_config('/a')
462
self.assertEqual('something',
463
self.my_config.get_user_option('user_global_option'))
465
def test_get_user_option_local(self):
466
self.get_location_config('/a')
467
self.assertEqual('local',
468
self.my_config.get_user_option('user_local_option'))
470
def test_post_commit_default(self):
471
self.get_location_config('/a/c')
472
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
473
self.my_config.post_commit())
475
def test_set_user_setting_sets_and_saves(self):
476
# TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
477
self.get_location_config('/a/c')
478
record = InstrumentedConfigObj("foo")
479
self.my_config._parser = record
480
self.my_config.set_user_option('foo', 'bar')
481
self.assertEqual([('__contains__', '/a/c'),
482
('__contains__', '/a/c/'),
483
('__setitem__', '/a/c', {}),
484
('__getitem__', '/a/c'),
485
('__setitem__', 'foo', 'bar'),
490
413
class TestBranchConfigItems(TestCase):
531
454
(my_config._get_location_config().
532
455
_get_global_config()._get_parser(config_file))
533
456
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
535
def test_get_user_option_global(self):
536
branch = FakeBranch()
537
my_config = config.BranchConfig(branch)
538
config_file = StringIO(sample_config_text)
539
(my_config._get_location_config().
540
_get_global_config()._get_parser(config_file))
541
self.assertEqual('something',
542
my_config.get_user_option('user_global_option'))
544
def test_post_commit_default(self):
545
branch = FakeBranch()
547
my_config = config.BranchConfig(branch)
548
config_file = StringIO(sample_config_text)
549
(my_config._get_location_config().
550
_get_global_config()._get_parser(config_file))
551
branch_file = StringIO(sample_branches_text)
552
my_config._get_location_config()._get_parser(branch_file)
553
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
554
my_config.post_commit())