173
59
self.assertEqual(config.config_filename(),
174
60
'/home/bogus/.bazaar/bazaar.conf')
176
def test_branches_config_filename(self):
177
self.assertEqual(config.branches_config_filename(),
178
'/home/bogus/.bazaar/branches.conf')
180
class TestIniConfig(TestCase):
182
def test_contructs(self):
183
my_config = config.IniBasedConfig("nothing")
63
class TestGetConfig(TestCase):
185
65
def test_from_fp(self):
186
66
config_file = StringIO(sample_config_text)
187
my_config = config.IniBasedConfig(None)
189
isinstance(my_config._get_parser(file=config_file),
67
self.failUnless(isinstance(config._get_config_parser(file=config_file),
192
def test_cached(self):
193
config_file = StringIO(sample_config_text)
194
my_config = config.IniBasedConfig(None)
195
parser = my_config._get_parser(file=config_file)
196
self.failUnless(my_config._get_parser() is parser)
199
class TestGetConfig(TestCase):
201
def test_constructs(self):
202
my_config = config.GlobalConfig()
204
70
def test_calls_read_filenames(self):
205
# replace the class that is constructured, to check its parameters
71
# note the monkey patching. if config access was via a class instance,
72
# we would not have to - if this changes in future, be sure to stop
73
# monkey patching RBC 20051011
206
74
oldparserclass = config.ConfigParser
207
75
config.ConfigParser = InstrumentedConfigParser
208
my_config = config.GlobalConfig()
210
parser = my_config._get_parser()
77
parser = config._get_config_parser()
212
79
config.ConfigParser = oldparserclass
213
80
self.failUnless(isinstance(parser, InstrumentedConfigParser))
214
81
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
217
class TestBranchConfig(TestCaseInTempDir):
219
def test_constructs(self):
220
branch = FakeBranch()
221
my_config = config.BranchConfig(branch)
222
self.assertRaises(TypeError, config.BranchConfig)
224
def test_get_location_config(self):
225
branch = FakeBranch()
226
my_config = config.BranchConfig(branch)
227
location_config = my_config._get_location_config()
228
self.assertEqual(branch.base, location_config.location)
229
self.failUnless(location_config is my_config._get_location_config())
232
class TestGlobalConfigItems(TestCase):
84
class TestConfigItems(TestCase):
87
super(TestConfigItems, self).setUp()
88
self.bzr_email = os.environ.get('BZREMAIL')
89
if self.bzr_email is not None:
90
del os.environ['BZREMAIL']
91
self.email = os.environ.get('EMAIL')
92
if self.email is not None:
93
del os.environ['EMAIL']
94
self.oldenv = os.environ.get('HOME', None)
95
os.environ['HOME'] = os.getcwd()
98
os.environ['HOME'] = self.oldenv
99
if self.bzr_email is not None:
100
os.environ['BZREMAIL'] = self.bzr_email
101
if self.email is not None:
102
os.environ['EMAIL'] = self.email
103
super(TestConfigItems, self).tearDown()
234
105
def test_user_id(self):
235
106
config_file = StringIO(sample_config_text)
236
my_config = config.GlobalConfig()
237
my_config._parser = my_config._get_parser(file=config_file)
107
parser = config._get_config_parser(file=config_file)
238
108
self.assertEqual("Robert Collins <robertc@example.com>",
239
my_config._get_user_id())
109
config._get_user_id(parser = parser))
241
111
def test_absent_user_id(self):
242
112
config_file = StringIO("")
243
my_config = config.GlobalConfig()
244
my_config._parser = my_config._get_parser(file=config_file)
245
self.assertEqual(None, my_config._get_user_id())
247
def test_configured_editor(self):
248
config_file = StringIO(sample_config_text)
249
my_config = config.GlobalConfig()
250
my_config._parser = my_config._get_parser(file=config_file)
251
self.assertEqual("vim", my_config.get_editor())
253
def test_signatures_always(self):
254
config_file = StringIO(sample_always_signatures)
255
my_config = config.GlobalConfig()
256
my_config._parser = my_config._get_parser(file=config_file)
257
self.assertEqual(config.CHECK_ALWAYS,
258
my_config.signature_checking())
259
self.assertEqual(True, my_config.signature_needed())
261
def test_signatures_if_possible(self):
262
config_file = StringIO(sample_maybe_signatures)
263
my_config = config.GlobalConfig()
264
my_config._parser = my_config._get_parser(file=config_file)
265
self.assertEqual(config.CHECK_IF_POSSIBLE,
266
my_config.signature_checking())
267
self.assertEqual(False, my_config.signature_needed())
269
def test_signatures_ignore(self):
270
config_file = StringIO(sample_ignore_signatures)
271
my_config = config.GlobalConfig()
272
my_config._parser = my_config._get_parser(file=config_file)
273
self.assertEqual(config.CHECK_NEVER,
274
my_config.signature_checking())
275
self.assertEqual(False, my_config.signature_needed())
277
def _get_sample_config(self):
278
config_file = StringIO(sample_config_text)
279
my_config = config.GlobalConfig()
280
my_config._parser = my_config._get_parser(file=config_file)
283
def test_gpg_signing_command(self):
284
my_config = self._get_sample_config()
285
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
286
self.assertEqual(False, my_config.signature_needed())
288
def _get_empty_config(self):
289
config_file = StringIO("")
290
my_config = config.GlobalConfig()
291
my_config._parser = my_config._get_parser(file=config_file)
294
def test_gpg_signing_command_unset(self):
295
my_config = self._get_empty_config()
296
self.assertEqual("gpg", my_config.gpg_signing_command())
298
def test_get_user_option_default(self):
299
my_config = self._get_empty_config()
300
self.assertEqual(None, my_config.get_user_option('no_option'))
302
def test_get_user_option_global(self):
303
my_config = self._get_sample_config()
304
self.assertEqual("something",
305
my_config.get_user_option('user_global_option'))
308
class TestLocationConfig(TestCase):
310
def test_constructs(self):
311
my_config = config.LocationConfig('http://example.com')
312
self.assertRaises(TypeError, config.LocationConfig)
314
def test_branch_calls_read_filenames(self):
315
# replace the class that is constructured, to check its parameters
316
oldparserclass = config.ConfigParser
317
config.ConfigParser = InstrumentedConfigParser
318
my_config = config.LocationConfig('http://www.example.com')
320
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()])])
326
def test_get_global_config(self):
327
my_config = config.LocationConfig('http://example.com')
328
global_config = my_config._get_global_config()
329
self.failUnless(isinstance(global_config, config.GlobalConfig))
330
self.failUnless(global_config is my_config._get_global_config())
332
def test__get_section_no_match(self):
333
self.get_location_config('/')
334
self.assertEqual(None, self.my_config._get_section())
336
def test__get_section_exact(self):
337
self.get_location_config('http://www.example.com')
338
self.assertEqual('http://www.example.com',
339
self.my_config._get_section())
341
def test__get_section_suffix_does_not(self):
342
self.get_location_config('http://www.example.com-com')
343
self.assertEqual(None, self.my_config._get_section())
345
def test__get_section_subdir_recursive(self):
346
self.get_location_config('http://www.example.com/com')
347
self.assertEqual('http://www.example.com',
348
self.my_config._get_section())
350
def test__get_section_subdir_matches(self):
351
self.get_location_config('http://www.example.com/useglobal')
352
self.assertEqual('http://www.example.com/useglobal',
353
self.my_config._get_section())
355
def test__get_section_subdir_nonrecursive(self):
356
self.get_location_config(
357
'http://www.example.com/useglobal/childbranch')
358
self.assertEqual('http://www.example.com',
359
self.my_config._get_section())
361
def test__get_section_subdir_trailing_slash(self):
362
self.get_location_config('/b')
363
self.assertEqual('/b/', self.my_config._get_section())
365
def test__get_section_subdir_child(self):
366
self.get_location_config('/a/foo')
367
self.assertEqual('/a/*', self.my_config._get_section())
369
def test__get_section_subdir_child_child(self):
370
self.get_location_config('/a/foo/bar')
371
self.assertEqual('/a/', self.my_config._get_section())
373
def test__get_section_trailing_slash_with_children(self):
374
self.get_location_config('/a/')
375
self.assertEqual('/a/', self.my_config._get_section())
377
def test__get_section_explicit_over_glob(self):
378
self.get_location_config('/a/c')
379
self.assertEqual('/a/c', self.my_config._get_section())
381
def get_location_config(self, location, global_config=None):
382
if global_config is None:
383
global_file = StringIO(sample_config_text)
385
global_file = StringIO(global_config)
386
branches_file = StringIO(sample_branches_text)
387
self.my_config = config.LocationConfig(location)
388
self.my_config._get_parser(branches_file)
389
self.my_config._get_global_config()._get_parser(global_file)
391
def test_location_without_username(self):
392
self.get_location_config('http://www.example.com/useglobal')
393
self.assertEqual('Robert Collins <robertc@example.com>',
394
self.my_config.username())
396
def test_location_not_listed(self):
397
self.get_location_config('/home/robertc/sources')
398
self.assertEqual('Robert Collins <robertc@example.com>',
399
self.my_config.username())
401
def test_overriding_location(self):
402
self.get_location_config('http://www.example.com/foo')
403
self.assertEqual('Robert Collins <robertc@example.org>',
404
self.my_config.username())
406
def test_signatures_not_set(self):
407
self.get_location_config('http://www.example.com',
408
global_config=sample_ignore_signatures)
409
self.assertEqual(config.CHECK_NEVER,
410
self.my_config.signature_checking())
412
def test_signatures_never(self):
413
self.get_location_config('/a/c')
414
self.assertEqual(config.CHECK_NEVER,
415
self.my_config.signature_checking())
417
def test_signatures_when_available(self):
418
self.get_location_config('/a/', global_config=sample_ignore_signatures)
419
self.assertEqual(config.CHECK_IF_POSSIBLE,
420
self.my_config.signature_checking())
422
def test_signatures_always(self):
423
self.get_location_config('/b')
424
self.assertEqual(config.CHECK_ALWAYS,
425
self.my_config.signature_checking())
427
def test_gpg_signing_command(self):
428
self.get_location_config('/b')
429
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
431
def test_gpg_signing_command_missing(self):
432
self.get_location_config('/a')
433
self.assertEqual("false", self.my_config.gpg_signing_command())
435
def test_get_user_option_global(self):
436
self.get_location_config('/a')
437
self.assertEqual('something',
438
self.my_config.get_user_option('user_global_option'))
440
def test_get_user_option_local(self):
441
self.get_location_config('/a')
442
self.assertEqual('local',
443
self.my_config.get_user_option('user_local_option'))
446
class TestBranchConfigItems(TestCase):
448
def test_user_id(self):
449
branch = FakeBranch()
450
my_config = config.BranchConfig(branch)
451
self.assertEqual("Robert Collins <robertc@example.net>",
452
my_config._get_user_id())
453
branch.email = "John"
454
self.assertEqual("John", my_config._get_user_id())
456
def test_not_set_in_branch(self):
457
branch = FakeBranch()
458
my_config = config.BranchConfig(branch)
460
config_file = StringIO(sample_config_text)
461
(my_config._get_location_config().
462
_get_global_config()._get_parser(config_file))
463
self.assertEqual("Robert Collins <robertc@example.com>",
464
my_config._get_user_id())
465
branch.email = "John"
466
self.assertEqual("John", my_config._get_user_id())
468
def test_BZREMAIL_OVERRIDES(self):
469
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
470
branch = FakeBranch()
471
my_config = config.BranchConfig(branch)
472
self.assertEqual("Robert Collins <robertc@example.org>",
473
my_config.username())
475
def test_signatures_forced(self):
476
branch = FakeBranch()
477
my_config = config.BranchConfig(branch)
478
config_file = StringIO(sample_always_signatures)
479
(my_config._get_location_config().
480
_get_global_config()._get_parser(config_file))
481
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
483
def test_gpg_signing_command(self):
484
branch = FakeBranch()
485
my_config = config.BranchConfig(branch)
486
config_file = StringIO(sample_config_text)
487
(my_config._get_location_config().
488
_get_global_config()._get_parser(config_file))
489
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
491
def test_get_user_option_global(self):
492
branch = FakeBranch()
493
my_config = config.BranchConfig(branch)
494
config_file = StringIO(sample_config_text)
495
(my_config._get_location_config().
496
_get_global_config()._get_parser(config_file))
497
self.assertEqual('something',
498
my_config.get_user_option('user_global_option'))
113
parser = config._get_config_parser(file=config_file)
114
self.assertEqual(None,
115
config._get_user_id(parser = parser))
117
def test_configured_edit(self):
118
config_file = StringIO(sample_config_text)
119
parser = config._get_config_parser(file=config_file)
120
self.assertEqual("vim", config.get_editor(parser = parser))