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 __init__(self, input):
77
self._calls = [('__init__', input)]
80
class FakeBranch(object):
83
self.base = "http://example.com/branches/demo"
84
self.email = 'Robert Collins <robertc@example.net>\n'
86
def controlfile(self, filename, mode):
87
if filename != 'email':
88
raise NotImplementedError
89
if self.email is not None:
90
return StringIO(self.email)
91
raise errors.NoSuchFile
94
class InstrumentedConfig(config.Config):
95
"""An instrumented config that supplies stubs for template methods."""
98
super(InstrumentedConfig, self).__init__()
100
self._signatures = config.CHECK_NEVER
102
def _get_user_id(self):
103
self._calls.append('_get_user_id')
104
return "Robert Collins <robert.collins@example.org>"
106
def _get_signature_checking(self):
107
self._calls.append('_get_signature_checking')
108
return self._signatures
111
class TestConfig(TestCase):
113
def test_constructs(self):
116
def test_no_default_editor(self):
117
self.assertRaises(NotImplementedError, config.Config().get_editor)
119
def test_user_email(self):
120
my_config = InstrumentedConfig()
121
self.assertEqual('robert.collins@example.org', my_config.user_email())
122
self.assertEqual(['_get_user_id'], my_config._calls)
124
def test_username(self):
125
my_config = InstrumentedConfig()
126
self.assertEqual('Robert Collins <robert.collins@example.org>',
127
my_config.username())
128
self.assertEqual(['_get_user_id'], my_config._calls)
130
def test_signatures_default(self):
131
my_config = config.Config()
132
self.assertEqual(config.CHECK_IF_POSSIBLE,
133
my_config.signature_checking())
135
def test_signatures_template_method(self):
136
my_config = InstrumentedConfig()
137
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
138
self.assertEqual(['_get_signature_checking'], my_config._calls)
140
def test_signatures_template_method_none(self):
141
my_config = InstrumentedConfig()
142
my_config._signatures = None
143
self.assertEqual(config.CHECK_IF_POSSIBLE,
144
my_config.signature_checking())
145
self.assertEqual(['_get_signature_checking'], my_config._calls)
147
def test_gpg_signing_command_default(self):
148
my_config = config.Config()
149
self.assertEqual('gpg', my_config.gpg_signing_command())
151
def test_get_user_option_default(self):
152
my_config = config.Config()
153
self.assertEqual(None, my_config.get_user_option('no_option'))
155
def test_post_commit_default(self):
156
my_config = config.Config()
157
self.assertEqual(None, my_config.post_commit())
160
class TestConfigPath(TestCase):
163
super(TestConfigPath, self).setUp()
164
self.oldenv = os.environ.get('HOME', None)
165
os.environ['HOME'] = '/home/bogus'
168
os.environ['HOME'] = self.oldenv
169
super(TestConfigPath, self).tearDown()
171
def test_config_dir(self):
172
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
174
def test_config_filename(self):
175
self.assertEqual(config.config_filename(),
176
'/home/bogus/.bazaar/bazaar.conf')
178
def test_branches_config_filename(self):
179
self.assertEqual(config.branches_config_filename(),
180
'/home/bogus/.bazaar/branches.conf')
182
class TestIniConfig(TestCase):
184
def test_contructs(self):
185
my_config = config.IniBasedConfig("nothing")
187
def test_from_fp(self):
188
config_file = StringIO(sample_config_text)
189
my_config = config.IniBasedConfig(None)
191
isinstance(my_config._get_parser(file=config_file),
194
def test_cached(self):
195
config_file = StringIO(sample_config_text)
196
my_config = config.IniBasedConfig(None)
197
parser = my_config._get_parser(file=config_file)
198
self.failUnless(my_config._get_parser() is parser)
201
class TestGetConfig(TestCase):
203
def test_constructs(self):
204
my_config = config.GlobalConfig()
206
def test_calls_read_filenames(self):
207
# replace the class that is constructured, to check its parameters
208
oldparserclass = config.ConfigObj
209
config.ConfigObj = InstrumentedConfigObj
210
my_config = config.GlobalConfig()
212
parser = my_config._get_parser()
214
config.ConfigObj = oldparserclass
215
self.failUnless(isinstance(parser, InstrumentedConfigObj))
216
self.assertEqual(parser._calls, [('__init__', config.config_filename())])
219
class TestBranchConfig(TestCaseInTempDir):
221
def test_constructs(self):
222
branch = FakeBranch()
223
my_config = config.BranchConfig(branch)
224
self.assertRaises(TypeError, config.BranchConfig)
226
def test_get_location_config(self):
227
branch = FakeBranch()
228
my_config = config.BranchConfig(branch)
229
location_config = my_config._get_location_config()
230
self.assertEqual(branch.base, location_config.location)
231
self.failUnless(location_config is my_config._get_location_config())
234
class TestGlobalConfigItems(TestCase):
236
def test_user_id(self):
237
config_file = StringIO(sample_config_text)
238
my_config = config.GlobalConfig()
239
my_config._parser = my_config._get_parser(file=config_file)
240
self.assertEqual("Robert Collins <robertc@example.com>",
241
my_config._get_user_id())
243
def test_absent_user_id(self):
244
config_file = StringIO("")
245
my_config = config.GlobalConfig()
246
my_config._parser = my_config._get_parser(file=config_file)
247
self.assertEqual(None, my_config._get_user_id())
249
def test_configured_editor(self):
250
config_file = StringIO(sample_config_text)
251
my_config = config.GlobalConfig()
252
my_config._parser = my_config._get_parser(file=config_file)
253
self.assertEqual("vim", my_config.get_editor())
255
def test_signatures_always(self):
256
config_file = StringIO(sample_always_signatures)
257
my_config = config.GlobalConfig()
258
my_config._parser = my_config._get_parser(file=config_file)
259
self.assertEqual(config.CHECK_ALWAYS,
260
my_config.signature_checking())
261
self.assertEqual(True, my_config.signature_needed())
263
def test_signatures_if_possible(self):
264
config_file = StringIO(sample_maybe_signatures)
265
my_config = config.GlobalConfig()
266
my_config._parser = my_config._get_parser(file=config_file)
267
self.assertEqual(config.CHECK_IF_POSSIBLE,
268
my_config.signature_checking())
269
self.assertEqual(False, my_config.signature_needed())
271
def test_signatures_ignore(self):
272
config_file = StringIO(sample_ignore_signatures)
273
my_config = config.GlobalConfig()
274
my_config._parser = my_config._get_parser(file=config_file)
275
self.assertEqual(config.CHECK_NEVER,
276
my_config.signature_checking())
277
self.assertEqual(False, my_config.signature_needed())
279
def _get_sample_config(self):
280
config_file = StringIO(sample_config_text)
281
my_config = config.GlobalConfig()
282
my_config._parser = my_config._get_parser(file=config_file)
285
def test_gpg_signing_command(self):
286
my_config = self._get_sample_config()
287
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
288
self.assertEqual(False, my_config.signature_needed())
290
def _get_empty_config(self):
291
config_file = StringIO("")
292
my_config = config.GlobalConfig()
293
my_config._parser = my_config._get_parser(file=config_file)
296
def test_gpg_signing_command_unset(self):
297
my_config = self._get_empty_config()
298
self.assertEqual("gpg", my_config.gpg_signing_command())
300
def test_get_user_option_default(self):
301
my_config = self._get_empty_config()
302
self.assertEqual(None, my_config.get_user_option('no_option'))
304
def test_get_user_option_global(self):
305
my_config = self._get_sample_config()
306
self.assertEqual("something",
307
my_config.get_user_option('user_global_option'))
309
def test_post_commit_default(self):
310
my_config = self._get_sample_config()
311
self.assertEqual(None, my_config.post_commit())
315
class TestLocationConfig(TestCase):
317
def test_constructs(self):
318
my_config = config.LocationConfig('http://example.com')
319
self.assertRaises(TypeError, config.LocationConfig)
321
def test_branch_calls_read_filenames(self):
322
# This is testing the correct file names are provided.
323
# TODO: consolidate with the test for GlobalConfigs filename checks.
325
# replace the class that is constructured, to check its parameters
326
oldparserclass = config.ConfigObj
327
config.ConfigObj = InstrumentedConfigObj
328
my_config = config.LocationConfig('http://www.example.com')
330
parser = my_config._get_parser()
332
config.ConfigObj = oldparserclass
333
self.failUnless(isinstance(parser, InstrumentedConfigObj))
334
self.assertEqual(parser._calls,
335
[('__init__', config.branches_config_filename())])
337
def test_get_global_config(self):
338
my_config = config.LocationConfig('http://example.com')
339
global_config = my_config._get_global_config()
340
self.failUnless(isinstance(global_config, config.GlobalConfig))
341
self.failUnless(global_config is my_config._get_global_config())
343
def test__get_section_no_match(self):
344
self.get_location_config('/')
345
self.assertEqual(None, self.my_config._get_section())
347
def test__get_section_exact(self):
348
self.get_location_config('http://www.example.com')
349
self.assertEqual('http://www.example.com',
350
self.my_config._get_section())
352
def test__get_section_suffix_does_not(self):
353
self.get_location_config('http://www.example.com-com')
354
self.assertEqual(None, self.my_config._get_section())
356
def test__get_section_subdir_recursive(self):
357
self.get_location_config('http://www.example.com/com')
358
self.assertEqual('http://www.example.com',
359
self.my_config._get_section())
361
def test__get_section_subdir_matches(self):
362
self.get_location_config('http://www.example.com/useglobal')
363
self.assertEqual('http://www.example.com/useglobal',
364
self.my_config._get_section())
366
def test__get_section_subdir_nonrecursive(self):
367
self.get_location_config(
368
'http://www.example.com/useglobal/childbranch')
369
self.assertEqual('http://www.example.com',
370
self.my_config._get_section())
372
def test__get_section_subdir_trailing_slash(self):
373
self.get_location_config('/b')
374
self.assertEqual('/b/', self.my_config._get_section())
376
def test__get_section_subdir_child(self):
377
self.get_location_config('/a/foo')
378
self.assertEqual('/a/*', self.my_config._get_section())
380
def test__get_section_subdir_child_child(self):
381
self.get_location_config('/a/foo/bar')
382
self.assertEqual('/a/', self.my_config._get_section())
384
def test__get_section_trailing_slash_with_children(self):
385
self.get_location_config('/a/')
386
self.assertEqual('/a/', self.my_config._get_section())
388
def test__get_section_explicit_over_glob(self):
389
self.get_location_config('/a/c')
390
self.assertEqual('/a/c', self.my_config._get_section())
392
def get_location_config(self, location, global_config=None):
393
if global_config is None:
394
global_file = StringIO(sample_config_text)
396
global_file = StringIO(global_config)
397
branches_file = StringIO(sample_branches_text)
398
self.my_config = config.LocationConfig(location)
399
self.my_config._get_parser(branches_file)
400
self.my_config._get_global_config()._get_parser(global_file)
402
def test_location_without_username(self):
403
self.get_location_config('http://www.example.com/useglobal')
404
self.assertEqual('Robert Collins <robertc@example.com>',
405
self.my_config.username())
407
def test_location_not_listed(self):
408
self.get_location_config('/home/robertc/sources')
409
self.assertEqual('Robert Collins <robertc@example.com>',
410
self.my_config.username())
412
def test_overriding_location(self):
413
self.get_location_config('http://www.example.com/foo')
414
self.assertEqual('Robert Collins <robertc@example.org>',
415
self.my_config.username())
417
def test_signatures_not_set(self):
418
self.get_location_config('http://www.example.com',
419
global_config=sample_ignore_signatures)
420
self.assertEqual(config.CHECK_NEVER,
421
self.my_config.signature_checking())
423
def test_signatures_never(self):
424
self.get_location_config('/a/c')
425
self.assertEqual(config.CHECK_NEVER,
426
self.my_config.signature_checking())
428
def test_signatures_when_available(self):
429
self.get_location_config('/a/', global_config=sample_ignore_signatures)
430
self.assertEqual(config.CHECK_IF_POSSIBLE,
431
self.my_config.signature_checking())
433
def test_signatures_always(self):
434
self.get_location_config('/b')
435
self.assertEqual(config.CHECK_ALWAYS,
436
self.my_config.signature_checking())
438
def test_gpg_signing_command(self):
439
self.get_location_config('/b')
440
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
442
def test_gpg_signing_command_missing(self):
443
self.get_location_config('/a')
444
self.assertEqual("false", self.my_config.gpg_signing_command())
446
def test_get_user_option_global(self):
447
self.get_location_config('/a')
448
self.assertEqual('something',
449
self.my_config.get_user_option('user_global_option'))
451
def test_get_user_option_local(self):
452
self.get_location_config('/a')
453
self.assertEqual('local',
454
self.my_config.get_user_option('user_local_option'))
456
def test_post_commit_default(self):
457
self.get_location_config('/a/c')
458
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
459
self.my_config.post_commit())
462
class TestBranchConfigItems(TestCase):
464
def test_user_id(self):
465
branch = FakeBranch()
466
my_config = config.BranchConfig(branch)
467
self.assertEqual("Robert Collins <robertc@example.net>",
468
my_config._get_user_id())
469
branch.email = "John"
470
self.assertEqual("John", my_config._get_user_id())
472
def test_not_set_in_branch(self):
473
branch = FakeBranch()
474
my_config = config.BranchConfig(branch)
476
config_file = StringIO(sample_config_text)
477
(my_config._get_location_config().
478
_get_global_config()._get_parser(config_file))
479
self.assertEqual("Robert Collins <robertc@example.com>",
480
my_config._get_user_id())
481
branch.email = "John"
482
self.assertEqual("John", my_config._get_user_id())
484
def test_BZREMAIL_OVERRIDES(self):
485
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
486
branch = FakeBranch()
487
my_config = config.BranchConfig(branch)
488
self.assertEqual("Robert Collins <robertc@example.org>",
489
my_config.username())
491
def test_signatures_forced(self):
492
branch = FakeBranch()
493
my_config = config.BranchConfig(branch)
494
config_file = StringIO(sample_always_signatures)
495
(my_config._get_location_config().
496
_get_global_config()._get_parser(config_file))
497
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
499
def test_gpg_signing_command(self):
500
branch = FakeBranch()
501
my_config = config.BranchConfig(branch)
502
config_file = StringIO(sample_config_text)
503
(my_config._get_location_config().
504
_get_global_config()._get_parser(config_file))
505
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
507
def test_get_user_option_global(self):
508
branch = FakeBranch()
509
my_config = config.BranchConfig(branch)
510
config_file = StringIO(sample_config_text)
511
(my_config._get_location_config().
512
_get_global_config()._get_parser(config_file))
513
self.assertEqual('something',
514
my_config.get_user_option('user_global_option'))
516
def test_post_commit_default(self):
517
branch = FakeBranch()
519
my_config = config.BranchConfig(branch)
520
config_file = StringIO(sample_config_text)
521
(my_config._get_location_config().
522
_get_global_config()._get_parser(config_file))
523
branch_file = StringIO(sample_branches_text)
524
my_config._get_location_config()._get_parser(branch_file)
525
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
526
my_config.post_commit())