42
76
self._calls.append(('read', filenames))
79
class FakeBranch(object):
82
self.base = "http://example.com/branches/demo"
83
self.email = 'Robert Collins <robertc@example.net>\n'
85
def controlfile(self, filename, mode):
86
if filename != 'email':
87
raise NotImplementedError
88
if self.email is not None:
89
return StringIO(self.email)
90
raise errors.NoSuchFile
93
class InstrumentedConfig(config.Config):
94
"""An instrumented config that supplies stubs for template methods."""
97
super(InstrumentedConfig, self).__init__()
99
self._signatures = config.CHECK_NEVER
101
def _get_user_id(self):
102
self._calls.append('_get_user_id')
103
return "Robert Collins <robert.collins@example.org>"
105
def _get_signature_checking(self):
106
self._calls.append('_get_signature_checking')
107
return self._signatures
110
class TestConfig(TestCase):
112
def test_constructs(self):
115
def test_no_default_editor(self):
116
self.assertRaises(NotImplementedError, config.Config().get_editor)
118
def test_user_email(self):
119
my_config = InstrumentedConfig()
120
self.assertEqual('robert.collins@example.org', my_config.user_email())
121
self.assertEqual(['_get_user_id'], my_config._calls)
123
def test_username(self):
124
my_config = InstrumentedConfig()
125
self.assertEqual('Robert Collins <robert.collins@example.org>',
126
my_config.username())
127
self.assertEqual(['_get_user_id'], my_config._calls)
129
def test_signatures_default(self):
130
my_config = config.Config()
131
self.assertEqual(config.CHECK_IF_POSSIBLE,
132
my_config.signature_checking())
134
def test_signatures_template_method(self):
135
my_config = InstrumentedConfig()
136
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
137
self.assertEqual(['_get_signature_checking'], my_config._calls)
139
def test_signatures_template_method_none(self):
140
my_config = InstrumentedConfig()
141
my_config._signatures = None
142
self.assertEqual(config.CHECK_IF_POSSIBLE,
143
my_config.signature_checking())
144
self.assertEqual(['_get_signature_checking'], my_config._calls)
45
147
class TestConfigPath(TestCase):
59
161
self.assertEqual(config.config_filename(),
60
162
'/home/bogus/.bazaar/bazaar.conf')
63
class TestGetConfig(TestCase):
164
def test_branches_config_filename(self):
165
self.assertEqual(config.branches_config_filename(),
166
'/home/bogus/.bazaar/branches.conf')
168
class TestIniConfig(TestCase):
170
def test_contructs(self):
171
my_config = config.IniBasedConfig("nothing")
65
173
def test_from_fp(self):
66
174
config_file = StringIO(sample_config_text)
67
self.failUnless(isinstance(config._get_config_parser(file=config_file),
175
my_config = config.IniBasedConfig(None)
177
isinstance(my_config._get_parser(file=config_file),
180
def test_cached(self):
181
config_file = StringIO(sample_config_text)
182
my_config = config.IniBasedConfig(None)
183
parser = my_config._get_parser(file=config_file)
184
self.failUnless(my_config._get_parser() is parser)
188
class TestGetConfig(TestCase):
190
def test_constructs(self):
191
my_config = config.GlobalConfig()
70
193
def test_calls_read_filenames(self):
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
194
# replace the class that is constructured, to check its parameters
74
195
oldparserclass = config.ConfigParser
75
196
config.ConfigParser = InstrumentedConfigParser
197
my_config = config.GlobalConfig()
77
parser = config._get_config_parser()
199
parser = my_config._get_parser()
79
201
config.ConfigParser = oldparserclass
80
202
self.failUnless(isinstance(parser, InstrumentedConfigParser))
81
203
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
206
class TestBranchConfig(TestCaseInTempDir):
208
def test_constructs(self):
209
branch = FakeBranch()
210
my_config = config.BranchConfig(branch)
211
self.assertRaises(TypeError, config.BranchConfig)
213
def test_get_location_config(self):
214
branch = FakeBranch()
215
my_config = config.BranchConfig(branch)
216
location_config = my_config._get_location_config()
217
self.assertEqual(branch.base, location_config.location)
218
self.failUnless(location_config is my_config._get_location_config())
84
221
class TestConfigItems(TestCase):
97
234
def tearDown(self):
98
235
os.environ['HOME'] = self.oldenv
236
if os.environ.get('BZREMAIL') is not None:
237
del os.environ['BZREMAIL']
99
238
if self.bzr_email is not None:
100
239
os.environ['BZREMAIL'] = self.bzr_email
101
240
if self.email is not None:
102
241
os.environ['EMAIL'] = self.email
103
242
super(TestConfigItems, self).tearDown()
245
class TestGlobalConfigItems(TestConfigItems):
105
247
def test_user_id(self):
106
248
config_file = StringIO(sample_config_text)
107
parser = config._get_config_parser(file=config_file)
249
my_config = config.GlobalConfig()
250
my_config._parser = my_config._get_parser(file=config_file)
108
251
self.assertEqual("Robert Collins <robertc@example.com>",
109
config._get_user_id(parser = parser))
252
my_config._get_user_id())
111
254
def test_absent_user_id(self):
112
255
config_file = StringIO("")
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))
256
my_config = config.GlobalConfig()
257
my_config._parser = my_config._get_parser(file=config_file)
258
self.assertEqual(None, my_config._get_user_id())
260
def test_configured_editor(self):
261
config_file = StringIO(sample_config_text)
262
my_config = config.GlobalConfig()
263
my_config._parser = my_config._get_parser(file=config_file)
264
self.assertEqual("vim", my_config.get_editor())
266
def test_signatures_always(self):
267
config_file = StringIO(sample_always_signatures)
268
my_config = config.GlobalConfig()
269
my_config._parser = my_config._get_parser(file=config_file)
270
self.assertEqual(config.CHECK_ALWAYS,
271
my_config.signature_checking())
272
self.assertEqual(True, my_config.signature_needed())
274
def test_signatures_if_possible(self):
275
config_file = StringIO(sample_maybe_signatures)
276
my_config = config.GlobalConfig()
277
my_config._parser = my_config._get_parser(file=config_file)
278
self.assertEqual(config.CHECK_IF_POSSIBLE,
279
my_config.signature_checking())
280
self.assertEqual(False, my_config.signature_needed())
282
def test_signatures_ignore(self):
283
config_file = StringIO(sample_ignore_signatures)
284
my_config = config.GlobalConfig()
285
my_config._parser = my_config._get_parser(file=config_file)
286
self.assertEqual(config.CHECK_NEVER,
287
my_config.signature_checking())
288
self.assertEqual(False, my_config.signature_needed())
291
class TestLocationConfig(TestConfigItems):
293
def test_constructs(self):
294
my_config = config.LocationConfig('http://example.com')
295
self.assertRaises(TypeError, config.LocationConfig)
297
def test_branch_calls_read_filenames(self):
298
# replace the class that is constructured, to check its parameters
299
oldparserclass = config.ConfigParser
300
config.ConfigParser = InstrumentedConfigParser
301
my_config = config.LocationConfig('http://www.example.com')
303
parser = my_config._get_parser()
305
config.ConfigParser = oldparserclass
306
self.failUnless(isinstance(parser, InstrumentedConfigParser))
307
self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
309
def test_get_global_config(self):
310
my_config = config.LocationConfig('http://example.com')
311
global_config = my_config._get_global_config()
312
self.failUnless(isinstance(global_config, config.GlobalConfig))
313
self.failUnless(global_config is my_config._get_global_config())
315
def test__get_section_no_match(self):
316
self.get_location_config('/')
317
self.assertEqual(None, self.my_config._get_section())
319
def test__get_section_exact(self):
320
self.get_location_config('http://www.example.com')
321
self.assertEqual('http://www.example.com',
322
self.my_config._get_section())
324
def test__get_section_suffix_does_not(self):
325
self.get_location_config('http://www.example.com-com')
326
self.assertEqual(None, self.my_config._get_section())
328
def test__get_section_subdir_recursive(self):
329
self.get_location_config('http://www.example.com/com')
330
self.assertEqual('http://www.example.com',
331
self.my_config._get_section())
333
def test__get_section_subdir_matches(self):
334
self.get_location_config('http://www.example.com/useglobal')
335
self.assertEqual('http://www.example.com/useglobal',
336
self.my_config._get_section())
338
def test__get_section_subdir_nonrecursive(self):
339
self.get_location_config(
340
'http://www.example.com/useglobal/childbranch')
341
self.assertEqual('http://www.example.com',
342
self.my_config._get_section())
344
def test__get_section_subdir_trailing_slash(self):
345
self.get_location_config('/b')
346
self.assertEqual('/b/', self.my_config._get_section())
348
def test__get_section_subdir_child(self):
349
self.get_location_config('/a/foo')
350
self.assertEqual('/a/*', self.my_config._get_section())
352
def test__get_section_subdir_child_child(self):
353
self.get_location_config('/a/foo/bar')
354
self.assertEqual('/a/', self.my_config._get_section())
356
def test__get_section_trailing_slash_with_children(self):
357
self.get_location_config('/a/')
358
self.assertEqual('/a/', self.my_config._get_section())
360
def test__get_section_explicit_over_glob(self):
361
self.get_location_config('/a/c')
362
self.assertEqual('/a/c', self.my_config._get_section())
364
def get_location_config(self, location, global_config=None):
365
if global_config is None:
366
global_file = StringIO(sample_config_text)
368
global_file = StringIO(global_config)
369
branches_file = StringIO(sample_branches_text)
370
self.my_config = config.LocationConfig(location)
371
self.my_config._get_parser(branches_file)
372
self.my_config._get_global_config()._get_parser(global_file)
374
def test_location_without_username(self):
375
self.get_location_config('http://www.example.com/useglobal')
376
self.assertEqual('Robert Collins <robertc@example.com>',
377
self.my_config.username())
379
def test_location_not_listed(self):
380
self.get_location_config('/home/robertc/sources')
381
self.assertEqual('Robert Collins <robertc@example.com>',
382
self.my_config.username())
384
def test_overriding_location(self):
385
self.get_location_config('http://www.example.com/foo')
386
self.assertEqual('Robert Collins <robertc@example.org>',
387
self.my_config.username())
389
def test_signatures_not_set(self):
390
self.get_location_config('http://www.example.com',
391
global_config=sample_ignore_signatures)
392
self.assertEqual(config.CHECK_NEVER,
393
self.my_config.signature_checking())
395
def test_signatures_never(self):
396
self.get_location_config('/a/c')
397
self.assertEqual(config.CHECK_NEVER,
398
self.my_config.signature_checking())
400
def test_signatures_when_available(self):
401
self.get_location_config('/a/', global_config=sample_ignore_signatures)
402
self.assertEqual(config.CHECK_IF_POSSIBLE,
403
self.my_config.signature_checking())
405
def test_signatures_always(self):
406
self.get_location_config('/b')
407
self.assertEqual(config.CHECK_ALWAYS,
408
self.my_config.signature_checking())
411
class TestBranchConfigItems(TestConfigItems):
413
def test_user_id(self):
414
branch = FakeBranch()
415
my_config = config.BranchConfig(branch)
416
self.assertEqual("Robert Collins <robertc@example.net>",
417
my_config._get_user_id())
418
branch.email = "John"
419
self.assertEqual("John", my_config._get_user_id())
421
def test_not_set_in_branch(self):
422
branch = FakeBranch()
423
my_config = config.BranchConfig(branch)
425
config_file = StringIO(sample_config_text)
426
(my_config._get_location_config().
427
_get_global_config()._get_parser(config_file))
428
self.assertEqual("Robert Collins <robertc@example.com>",
429
my_config._get_user_id())
430
branch.email = "John"
431
self.assertEqual("John", my_config._get_user_id())
433
def test_BZREMAIL_OVERRIDES(self):
434
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
435
branch = FakeBranch()
436
my_config = config.BranchConfig(branch)
437
self.assertEqual("Robert Collins <robertc@example.org>",
438
my_config.username())
440
def test_signatures_forced(self):
441
branch = FakeBranch()
442
my_config = config.BranchConfig(branch)
443
config_file = StringIO(sample_always_signatures)
444
(my_config._get_location_config().
445
_get_global_config()._get_parser(config_file))
446
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())