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 ConfigParser import ConfigParser
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 InstrumentedConfigParser(object):
74
"""A config parser look-enough-alike to record calls made to it."""
79
def read(self, filenames):
80
self._calls.append(('read', filenames))
83
class FakeBranch(object):
86
self.base = "http://example.com/branches/demo"
87
self.email = 'Robert Collins <robertc@example.net>\n'
89
def controlfile(self, filename, mode):
90
if filename != 'email':
91
raise NotImplementedError
92
if self.email is not None:
93
return StringIO(self.email)
94
raise errors.NoSuchFile
97
class InstrumentedConfig(config.Config):
98
"""An instrumented config that supplies stubs for template methods."""
101
super(InstrumentedConfig, self).__init__()
103
self._signatures = config.CHECK_NEVER
105
def _get_user_id(self):
106
self._calls.append('_get_user_id')
107
return "Robert Collins <robert.collins@example.org>"
109
def _get_signature_checking(self):
110
self._calls.append('_get_signature_checking')
111
return self._signatures
114
class TestConfig(TestCase):
116
def test_constructs(self):
119
def test_no_default_editor(self):
120
self.assertRaises(NotImplementedError, config.Config().get_editor)
122
def test_user_email(self):
123
my_config = InstrumentedConfig()
124
self.assertEqual('robert.collins@example.org', my_config.user_email())
125
self.assertEqual(['_get_user_id'], my_config._calls)
127
def test_username(self):
128
my_config = InstrumentedConfig()
129
self.assertEqual('Robert Collins <robert.collins@example.org>',
130
my_config.username())
131
self.assertEqual(['_get_user_id'], my_config._calls)
133
def test_signatures_default(self):
134
my_config = config.Config()
135
self.assertEqual(config.CHECK_IF_POSSIBLE,
136
my_config.signature_checking())
138
def test_signatures_template_method(self):
139
my_config = InstrumentedConfig()
140
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
141
self.assertEqual(['_get_signature_checking'], my_config._calls)
143
def test_signatures_template_method_none(self):
144
my_config = InstrumentedConfig()
145
my_config._signatures = None
146
self.assertEqual(config.CHECK_IF_POSSIBLE,
147
my_config.signature_checking())
148
self.assertEqual(['_get_signature_checking'], my_config._calls)
150
def test_gpg_signing_command_default(self):
151
my_config = config.Config()
152
self.assertEqual('gpg', my_config.gpg_signing_command())
154
def test_get_user_option_default(self):
155
my_config = config.Config()
156
self.assertEqual(None, my_config.get_user_option('no_option'))
158
def test_post_commit_default(self):
159
my_config = config.Config()
160
self.assertEqual(None, my_config.post_commit())
163
class TestConfigPath(TestCase):
166
super(TestConfigPath, self).setUp()
167
self.oldenv = os.environ.get('HOME', None)
168
os.environ['HOME'] = '/home/bogus'
171
os.environ['HOME'] = self.oldenv
172
super(TestConfigPath, self).tearDown()
174
def test_config_dir(self):
175
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
177
def test_config_filename(self):
178
self.assertEqual(config.config_filename(),
179
'/home/bogus/.bazaar/bazaar.conf')
181
def test_branches_config_filename(self):
182
self.assertEqual(config.branches_config_filename(),
183
'/home/bogus/.bazaar/branches.conf')
185
class TestIniConfig(TestCase):
187
def test_contructs(self):
188
my_config = config.IniBasedConfig("nothing")
190
def test_from_fp(self):
191
config_file = StringIO(sample_config_text)
192
my_config = config.IniBasedConfig(None)
194
isinstance(my_config._get_parser(file=config_file),
197
def test_cached(self):
198
config_file = StringIO(sample_config_text)
199
my_config = config.IniBasedConfig(None)
200
parser = my_config._get_parser(file=config_file)
201
self.failUnless(my_config._get_parser() is parser)
204
class TestGetConfig(TestCase):
206
def test_constructs(self):
207
my_config = config.GlobalConfig()
209
def test_calls_read_filenames(self):
210
# replace the class that is constructured, to check its parameters
211
oldparserclass = config.ConfigParser
212
config.ConfigParser = InstrumentedConfigParser
213
my_config = config.GlobalConfig()
215
parser = my_config._get_parser()
217
config.ConfigParser = oldparserclass
218
self.failUnless(isinstance(parser, InstrumentedConfigParser))
219
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
222
class TestBranchConfig(TestCaseInTempDir):
224
def test_constructs(self):
225
branch = FakeBranch()
226
my_config = config.BranchConfig(branch)
227
self.assertRaises(TypeError, config.BranchConfig)
229
def test_get_location_config(self):
230
branch = FakeBranch()
231
my_config = config.BranchConfig(branch)
232
location_config = my_config._get_location_config()
233
self.assertEqual(branch.base, location_config.location)
234
self.failUnless(location_config is my_config._get_location_config())
237
class TestGlobalConfigItems(TestCase):
239
def test_user_id(self):
240
config_file = StringIO(sample_config_text)
241
my_config = config.GlobalConfig()
242
my_config._parser = my_config._get_parser(file=config_file)
243
self.assertEqual("Robert Collins <robertc@example.com>",
244
my_config._get_user_id())
246
def test_absent_user_id(self):
247
config_file = StringIO("")
248
my_config = config.GlobalConfig()
249
my_config._parser = my_config._get_parser(file=config_file)
250
self.assertEqual(None, my_config._get_user_id())
252
def test_configured_editor(self):
253
config_file = StringIO(sample_config_text)
254
my_config = config.GlobalConfig()
255
my_config._parser = my_config._get_parser(file=config_file)
256
self.assertEqual("vim", my_config.get_editor())
258
def test_signatures_always(self):
259
config_file = StringIO(sample_always_signatures)
260
my_config = config.GlobalConfig()
261
my_config._parser = my_config._get_parser(file=config_file)
262
self.assertEqual(config.CHECK_ALWAYS,
263
my_config.signature_checking())
264
self.assertEqual(True, my_config.signature_needed())
266
def test_signatures_if_possible(self):
267
config_file = StringIO(sample_maybe_signatures)
268
my_config = config.GlobalConfig()
269
my_config._parser = my_config._get_parser(file=config_file)
270
self.assertEqual(config.CHECK_IF_POSSIBLE,
271
my_config.signature_checking())
272
self.assertEqual(False, my_config.signature_needed())
274
def test_signatures_ignore(self):
275
config_file = StringIO(sample_ignore_signatures)
276
my_config = config.GlobalConfig()
277
my_config._parser = my_config._get_parser(file=config_file)
278
self.assertEqual(config.CHECK_NEVER,
279
my_config.signature_checking())
280
self.assertEqual(False, my_config.signature_needed())
282
def _get_sample_config(self):
283
config_file = StringIO(sample_config_text)
284
my_config = config.GlobalConfig()
285
my_config._parser = my_config._get_parser(file=config_file)
288
def test_gpg_signing_command(self):
289
my_config = self._get_sample_config()
290
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
291
self.assertEqual(False, my_config.signature_needed())
293
def _get_empty_config(self):
294
config_file = StringIO("")
295
my_config = config.GlobalConfig()
296
my_config._parser = my_config._get_parser(file=config_file)
299
def test_gpg_signing_command_unset(self):
300
my_config = self._get_empty_config()
301
self.assertEqual("gpg", my_config.gpg_signing_command())
303
def test_get_user_option_default(self):
304
my_config = self._get_empty_config()
305
self.assertEqual(None, my_config.get_user_option('no_option'))
307
def test_get_user_option_global(self):
308
my_config = self._get_sample_config()
309
self.assertEqual("something",
310
my_config.get_user_option('user_global_option'))
312
def test_post_commit_default(self):
313
my_config = self._get_sample_config()
314
self.assertEqual(None, my_config.post_commit())
318
class TestLocationConfig(TestCase):
320
def test_constructs(self):
321
my_config = config.LocationConfig('http://example.com')
322
self.assertRaises(TypeError, config.LocationConfig)
324
def test_branch_calls_read_filenames(self):
325
# replace the class that is constructured, to check its parameters
326
oldparserclass = config.ConfigParser
327
config.ConfigParser = InstrumentedConfigParser
328
my_config = config.LocationConfig('http://www.example.com')
330
parser = my_config._get_parser()
332
config.ConfigParser = oldparserclass
333
self.failUnless(isinstance(parser, InstrumentedConfigParser))
334
self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
336
def test_get_global_config(self):
337
my_config = config.LocationConfig('http://example.com')
338
global_config = my_config._get_global_config()
339
self.failUnless(isinstance(global_config, config.GlobalConfig))
340
self.failUnless(global_config is my_config._get_global_config())
342
def test__get_section_no_match(self):
343
self.get_location_config('/')
344
self.assertEqual(None, self.my_config._get_section())
346
def test__get_section_exact(self):
347
self.get_location_config('http://www.example.com')
348
self.assertEqual('http://www.example.com',
349
self.my_config._get_section())
351
def test__get_section_suffix_does_not(self):
352
self.get_location_config('http://www.example.com-com')
353
self.assertEqual(None, self.my_config._get_section())
355
def test__get_section_subdir_recursive(self):
356
self.get_location_config('http://www.example.com/com')
357
self.assertEqual('http://www.example.com',
358
self.my_config._get_section())
360
def test__get_section_subdir_matches(self):
361
self.get_location_config('http://www.example.com/useglobal')
362
self.assertEqual('http://www.example.com/useglobal',
363
self.my_config._get_section())
365
def test__get_section_subdir_nonrecursive(self):
366
self.get_location_config(
367
'http://www.example.com/useglobal/childbranch')
368
self.assertEqual('http://www.example.com',
369
self.my_config._get_section())
371
def test__get_section_subdir_trailing_slash(self):
372
self.get_location_config('/b')
373
self.assertEqual('/b/', self.my_config._get_section())
375
def test__get_section_subdir_child(self):
376
self.get_location_config('/a/foo')
377
self.assertEqual('/a/*', self.my_config._get_section())
379
def test__get_section_subdir_child_child(self):
380
self.get_location_config('/a/foo/bar')
381
self.assertEqual('/a/', self.my_config._get_section())
383
def test__get_section_trailing_slash_with_children(self):
384
self.get_location_config('/a/')
385
self.assertEqual('/a/', self.my_config._get_section())
387
def test__get_section_explicit_over_glob(self):
388
self.get_location_config('/a/c')
389
self.assertEqual('/a/c', self.my_config._get_section())
391
def get_location_config(self, location, global_config=None):
392
if global_config is None:
393
global_file = StringIO(sample_config_text)
395
global_file = StringIO(global_config)
396
branches_file = StringIO(sample_branches_text)
397
self.my_config = config.LocationConfig(location)
398
self.my_config._get_parser(branches_file)
399
self.my_config._get_global_config()._get_parser(global_file)
401
def test_location_without_username(self):
402
self.get_location_config('http://www.example.com/useglobal')
403
self.assertEqual('Robert Collins <robertc@example.com>',
404
self.my_config.username())
406
def test_location_not_listed(self):
407
self.get_location_config('/home/robertc/sources')
408
self.assertEqual('Robert Collins <robertc@example.com>',
409
self.my_config.username())
411
def test_overriding_location(self):
412
self.get_location_config('http://www.example.com/foo')
413
self.assertEqual('Robert Collins <robertc@example.org>',
414
self.my_config.username())
416
def test_signatures_not_set(self):
417
self.get_location_config('http://www.example.com',
418
global_config=sample_ignore_signatures)
419
self.assertEqual(config.CHECK_NEVER,
420
self.my_config.signature_checking())
422
def test_signatures_never(self):
423
self.get_location_config('/a/c')
424
self.assertEqual(config.CHECK_NEVER,
425
self.my_config.signature_checking())
427
def test_signatures_when_available(self):
428
self.get_location_config('/a/', global_config=sample_ignore_signatures)
429
self.assertEqual(config.CHECK_IF_POSSIBLE,
430
self.my_config.signature_checking())
432
def test_signatures_always(self):
433
self.get_location_config('/b')
434
self.assertEqual(config.CHECK_ALWAYS,
435
self.my_config.signature_checking())
437
def test_gpg_signing_command(self):
438
self.get_location_config('/b')
439
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
441
def test_gpg_signing_command_missing(self):
442
self.get_location_config('/a')
443
self.assertEqual("false", self.my_config.gpg_signing_command())
445
def test_get_user_option_global(self):
446
self.get_location_config('/a')
447
self.assertEqual('something',
448
self.my_config.get_user_option('user_global_option'))
450
def test_get_user_option_local(self):
451
self.get_location_config('/a')
452
self.assertEqual('local',
453
self.my_config.get_user_option('user_local_option'))
455
def test_post_commit_default(self):
456
self.get_location_config('/a/c')
457
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
458
self.my_config.post_commit())
461
class TestBranchConfigItems(TestCase):
463
def test_user_id(self):
464
branch = FakeBranch()
465
my_config = config.BranchConfig(branch)
466
self.assertEqual("Robert Collins <robertc@example.net>",
467
my_config._get_user_id())
468
branch.email = "John"
469
self.assertEqual("John", my_config._get_user_id())
471
def test_not_set_in_branch(self):
472
branch = FakeBranch()
473
my_config = config.BranchConfig(branch)
475
config_file = StringIO(sample_config_text)
476
(my_config._get_location_config().
477
_get_global_config()._get_parser(config_file))
478
self.assertEqual("Robert Collins <robertc@example.com>",
479
my_config._get_user_id())
480
branch.email = "John"
481
self.assertEqual("John", my_config._get_user_id())
483
def test_BZREMAIL_OVERRIDES(self):
484
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
485
branch = FakeBranch()
486
my_config = config.BranchConfig(branch)
487
self.assertEqual("Robert Collins <robertc@example.org>",
488
my_config.username())
490
def test_signatures_forced(self):
491
branch = FakeBranch()
492
my_config = config.BranchConfig(branch)
493
config_file = StringIO(sample_always_signatures)
494
(my_config._get_location_config().
495
_get_global_config()._get_parser(config_file))
496
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
498
def test_gpg_signing_command(self):
499
branch = FakeBranch()
500
my_config = config.BranchConfig(branch)
501
config_file = StringIO(sample_config_text)
502
(my_config._get_location_config().
503
_get_global_config()._get_parser(config_file))
504
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
506
def test_get_user_option_global(self):
507
branch = FakeBranch()
508
my_config = config.BranchConfig(branch)
509
config_file = StringIO(sample_config_text)
510
(my_config._get_location_config().
511
_get_global_config()._get_parser(config_file))
512
self.assertEqual('something',
513
my_config.get_user_option('user_global_option'))
515
def test_post_commit_default(self):
516
branch = FakeBranch()
518
my_config = config.BranchConfig(branch)
519
config_file = StringIO(sample_config_text)
520
(my_config._get_location_config().
521
_get_global_config()._get_parser(config_file))
522
branch_file = StringIO(sample_branches_text)
523
my_config._get_location_config()._get_parser(branch_file)
524
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
525
my_config.post_commit())