1
# Copyright (C) 2005 by Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Tests for finding and reading the bzr config file[s]."""
19
# import system imports here
20
from bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
21
from cStringIO import StringIO
25
#import bzrlib specific imports here
26
import bzrlib.config as config
27
import bzrlib.errors as errors
28
from bzrlib.selftest import TestCase, TestCaseInTempDir
31
sample_config_text = ("[DEFAULT]\n"
32
"email=Robert Collins <robertc@example.com>\n"
34
"gpg_signing_command=gnome-gpg\n"
35
"user_global_option=something\n")
38
sample_always_signatures = ("[DEFAULT]\n"
39
"check_signatures=require\n")
42
sample_ignore_signatures = ("[DEFAULT]\n"
43
"check_signatures=ignore\n")
46
sample_maybe_signatures = ("[DEFAULT]\n"
47
"check_signatures=check-available\n")
50
sample_branches_text = ("[http://www.example.com]\n"
51
"# Top level policy\n"
52
"email=Robert Collins <robertc@example.org>\n"
53
"[http://www.example.com/useglobal]\n"
54
"# different project, forces global lookup\n"
57
"check_signatures=require\n"
58
"# test trailing / matching with no children\n"
60
"check_signatures=check-available\n"
61
"gpg_signing_command=false\n"
62
"user_local_option=local\n"
63
"# test trailing / matching\n"
65
"#subdirs will match but not the parent\n"
68
"check_signatures=ignore\n"
69
"post_commit=bzrlib.selftest.testconfig.post_commit\n"
70
"#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',))
94
class FakeBranch(object):
97
self.base = "http://example.com/branches/demo"
98
self.email = 'Robert Collins <robertc@example.net>\n'
100
def controlfile(self, filename, mode):
101
if filename != 'email':
102
raise NotImplementedError
103
if self.email is not None:
104
return StringIO(self.email)
105
raise errors.NoSuchFile
108
class InstrumentedConfig(config.Config):
109
"""An instrumented config that supplies stubs for template methods."""
112
super(InstrumentedConfig, self).__init__()
114
self._signatures = config.CHECK_NEVER
116
def _get_user_id(self):
117
self._calls.append('_get_user_id')
118
return "Robert Collins <robert.collins@example.org>"
120
def _get_signature_checking(self):
121
self._calls.append('_get_signature_checking')
122
return self._signatures
125
class TestConfig(TestCase):
127
def test_constructs(self):
130
def test_no_default_editor(self):
131
self.assertRaises(NotImplementedError, config.Config().get_editor)
133
def test_user_email(self):
134
my_config = InstrumentedConfig()
135
self.assertEqual('robert.collins@example.org', my_config.user_email())
136
self.assertEqual(['_get_user_id'], my_config._calls)
138
def test_username(self):
139
my_config = InstrumentedConfig()
140
self.assertEqual('Robert Collins <robert.collins@example.org>',
141
my_config.username())
142
self.assertEqual(['_get_user_id'], my_config._calls)
144
def test_signatures_default(self):
145
my_config = config.Config()
146
self.assertEqual(config.CHECK_IF_POSSIBLE,
147
my_config.signature_checking())
149
def test_signatures_template_method(self):
150
my_config = InstrumentedConfig()
151
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
152
self.assertEqual(['_get_signature_checking'], my_config._calls)
154
def test_signatures_template_method_none(self):
155
my_config = InstrumentedConfig()
156
my_config._signatures = None
157
self.assertEqual(config.CHECK_IF_POSSIBLE,
158
my_config.signature_checking())
159
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
class TestConfigPath(TestCase):
177
super(TestConfigPath, self).setUp()
178
self.oldenv = os.environ.get('HOME', None)
179
os.environ['HOME'] = '/home/bogus'
182
os.environ['HOME'] = self.oldenv
183
super(TestConfigPath, self).tearDown()
185
def test_config_dir(self):
186
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
188
def test_config_filename(self):
189
self.assertEqual(config.config_filename(),
190
'/home/bogus/.bazaar/bazaar.conf')
192
def test_branches_config_filename(self):
193
self.assertEqual(config.branches_config_filename(),
194
'/home/bogus/.bazaar/branches.conf')
196
class TestIniConfig(TestCase):
198
def test_contructs(self):
199
my_config = config.IniBasedConfig("nothing")
201
def test_from_fp(self):
202
config_file = StringIO(sample_config_text)
203
my_config = config.IniBasedConfig(None)
205
isinstance(my_config._get_parser(file=config_file),
208
def test_cached(self):
209
config_file = StringIO(sample_config_text)
210
my_config = config.IniBasedConfig(None)
211
parser = my_config._get_parser(file=config_file)
212
self.failUnless(my_config._get_parser() is parser)
215
class TestGetConfig(TestCase):
217
def test_constructs(self):
218
my_config = config.GlobalConfig()
220
def test_calls_read_filenames(self):
221
# replace the class that is constructured, to check its parameters
222
oldparserclass = config.ConfigObj
223
config.ConfigObj = InstrumentedConfigObj
224
my_config = config.GlobalConfig()
226
parser = my_config._get_parser()
228
config.ConfigObj = oldparserclass
229
self.failUnless(isinstance(parser, InstrumentedConfigObj))
230
self.assertEqual(parser._calls, [('__init__', config.config_filename())])
233
class TestBranchConfig(TestCaseInTempDir):
235
def test_constructs(self):
236
branch = FakeBranch()
237
my_config = config.BranchConfig(branch)
238
self.assertRaises(TypeError, config.BranchConfig)
240
def test_get_location_config(self):
241
branch = FakeBranch()
242
my_config = config.BranchConfig(branch)
243
location_config = my_config._get_location_config()
244
self.assertEqual(branch.base, location_config.location)
245
self.failUnless(location_config is my_config._get_location_config())
248
class TestGlobalConfigItems(TestCase):
250
def test_user_id(self):
251
config_file = StringIO(sample_config_text)
252
my_config = config.GlobalConfig()
253
my_config._parser = my_config._get_parser(file=config_file)
254
self.assertEqual("Robert Collins <robertc@example.com>",
255
my_config._get_user_id())
257
def test_absent_user_id(self):
258
config_file = StringIO("")
259
my_config = config.GlobalConfig()
260
my_config._parser = my_config._get_parser(file=config_file)
261
self.assertEqual(None, my_config._get_user_id())
263
def test_configured_editor(self):
264
config_file = StringIO(sample_config_text)
265
my_config = config.GlobalConfig()
266
my_config._parser = my_config._get_parser(file=config_file)
267
self.assertEqual("vim", my_config.get_editor())
269
def test_signatures_always(self):
270
config_file = StringIO(sample_always_signatures)
271
my_config = config.GlobalConfig()
272
my_config._parser = my_config._get_parser(file=config_file)
273
self.assertEqual(config.CHECK_ALWAYS,
274
my_config.signature_checking())
275
self.assertEqual(True, my_config.signature_needed())
277
def test_signatures_if_possible(self):
278
config_file = StringIO(sample_maybe_signatures)
279
my_config = config.GlobalConfig()
280
my_config._parser = my_config._get_parser(file=config_file)
281
self.assertEqual(config.CHECK_IF_POSSIBLE,
282
my_config.signature_checking())
283
self.assertEqual(False, my_config.signature_needed())
285
def test_signatures_ignore(self):
286
config_file = StringIO(sample_ignore_signatures)
287
my_config = config.GlobalConfig()
288
my_config._parser = my_config._get_parser(file=config_file)
289
self.assertEqual(config.CHECK_NEVER,
290
my_config.signature_checking())
291
self.assertEqual(False, my_config.signature_needed())
293
def _get_sample_config(self):
294
config_file = StringIO(sample_config_text)
295
my_config = config.GlobalConfig()
296
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
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
302
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
def test_gpg_signing_command_unset(self):
311
my_config = self._get_empty_config()
312
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
class TestLocationConfig(TestCase):
331
def test_constructs(self):
332
my_config = config.LocationConfig('http://example.com')
333
self.assertRaises(TypeError, config.LocationConfig)
335
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
# replace the class that is constructured, to check its parameters
340
oldparserclass = config.ConfigObj
341
config.ConfigObj = InstrumentedConfigObj
342
my_config = config.LocationConfig('http://www.example.com')
344
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())])
351
def test_get_global_config(self):
352
my_config = config.LocationConfig('http://example.com')
353
global_config = my_config._get_global_config()
354
self.failUnless(isinstance(global_config, config.GlobalConfig))
355
self.failUnless(global_config is my_config._get_global_config())
357
def test__get_section_no_match(self):
358
self.get_location_config('/')
359
self.assertEqual(None, self.my_config._get_section())
361
def test__get_section_exact(self):
362
self.get_location_config('http://www.example.com')
363
self.assertEqual('http://www.example.com',
364
self.my_config._get_section())
366
def test__get_section_suffix_does_not(self):
367
self.get_location_config('http://www.example.com-com')
368
self.assertEqual(None, self.my_config._get_section())
370
def test__get_section_subdir_recursive(self):
371
self.get_location_config('http://www.example.com/com')
372
self.assertEqual('http://www.example.com',
373
self.my_config._get_section())
375
def test__get_section_subdir_matches(self):
376
self.get_location_config('http://www.example.com/useglobal')
377
self.assertEqual('http://www.example.com/useglobal',
378
self.my_config._get_section())
380
def test__get_section_subdir_nonrecursive(self):
381
self.get_location_config(
382
'http://www.example.com/useglobal/childbranch')
383
self.assertEqual('http://www.example.com',
384
self.my_config._get_section())
386
def test__get_section_subdir_trailing_slash(self):
387
self.get_location_config('/b')
388
self.assertEqual('/b/', self.my_config._get_section())
390
def test__get_section_subdir_child(self):
391
self.get_location_config('/a/foo')
392
self.assertEqual('/a/*', self.my_config._get_section())
394
def test__get_section_subdir_child_child(self):
395
self.get_location_config('/a/foo/bar')
396
self.assertEqual('/a/', self.my_config._get_section())
398
def test__get_section_trailing_slash_with_children(self):
399
self.get_location_config('/a/')
400
self.assertEqual('/a/', self.my_config._get_section())
402
def test__get_section_explicit_over_glob(self):
403
self.get_location_config('/a/c')
404
self.assertEqual('/a/c', self.my_config._get_section())
406
def get_location_config(self, location, global_config=None):
407
if global_config is None:
408
global_file = StringIO(sample_config_text)
410
global_file = StringIO(global_config)
411
branches_file = StringIO(sample_branches_text)
412
self.my_config = config.LocationConfig(location)
413
self.my_config._get_parser(branches_file)
414
self.my_config._get_global_config()._get_parser(global_file)
416
def test_location_without_username(self):
417
self.get_location_config('http://www.example.com/useglobal')
418
self.assertEqual('Robert Collins <robertc@example.com>',
419
self.my_config.username())
421
def test_location_not_listed(self):
422
self.get_location_config('/home/robertc/sources')
423
self.assertEqual('Robert Collins <robertc@example.com>',
424
self.my_config.username())
426
def test_overriding_location(self):
427
self.get_location_config('http://www.example.com/foo')
428
self.assertEqual('Robert Collins <robertc@example.org>',
429
self.my_config.username())
431
def test_signatures_not_set(self):
432
self.get_location_config('http://www.example.com',
433
global_config=sample_ignore_signatures)
434
self.assertEqual(config.CHECK_NEVER,
435
self.my_config.signature_checking())
437
def test_signatures_never(self):
438
self.get_location_config('/a/c')
439
self.assertEqual(config.CHECK_NEVER,
440
self.my_config.signature_checking())
442
def test_signatures_when_available(self):
443
self.get_location_config('/a/', global_config=sample_ignore_signatures)
444
self.assertEqual(config.CHECK_IF_POSSIBLE,
445
self.my_config.signature_checking())
447
def test_signatures_always(self):
448
self.get_location_config('/b')
449
self.assertEqual(config.CHECK_ALWAYS,
450
self.my_config.signature_checking())
452
def test_gpg_signing_command(self):
453
self.get_location_config('/b')
454
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
456
def test_gpg_signing_command_missing(self):
457
self.get_location_config('/a')
458
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
class TestBranchConfigItems(TestCase):
492
def test_user_id(self):
493
branch = FakeBranch()
494
my_config = config.BranchConfig(branch)
495
self.assertEqual("Robert Collins <robertc@example.net>",
496
my_config._get_user_id())
497
branch.email = "John"
498
self.assertEqual("John", my_config._get_user_id())
500
def test_not_set_in_branch(self):
501
branch = FakeBranch()
502
my_config = config.BranchConfig(branch)
504
config_file = StringIO(sample_config_text)
505
(my_config._get_location_config().
506
_get_global_config()._get_parser(config_file))
507
self.assertEqual("Robert Collins <robertc@example.com>",
508
my_config._get_user_id())
509
branch.email = "John"
510
self.assertEqual("John", my_config._get_user_id())
512
def test_BZREMAIL_OVERRIDES(self):
513
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
514
branch = FakeBranch()
515
my_config = config.BranchConfig(branch)
516
self.assertEqual("Robert Collins <robertc@example.org>",
517
my_config.username())
519
def test_signatures_forced(self):
520
branch = FakeBranch()
521
my_config = config.BranchConfig(branch)
522
config_file = StringIO(sample_always_signatures)
523
(my_config._get_location_config().
524
_get_global_config()._get_parser(config_file))
525
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
527
def test_gpg_signing_command(self):
528
branch = FakeBranch()
529
my_config = config.BranchConfig(branch)
530
config_file = StringIO(sample_config_text)
531
(my_config._get_location_config().
532
_get_global_config()._get_parser(config_file))
533
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())