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.tests 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"
36
"[COMMAND_DEFAULTS]\n"
37
'commit=-m "log message" #evil? possibly.')
40
sample_always_signatures = ("[DEFAULT]\n"
41
"check_signatures=require\n")
44
sample_ignore_signatures = ("[DEFAULT]\n"
45
"check_signatures=ignore\n")
48
sample_maybe_signatures = ("[DEFAULT]\n"
49
"check_signatures=check-available\n")
52
sample_branches_text = ("[http://www.example.com]\n"
53
"# Top level policy\n"
54
"email=Robert Collins <robertc@example.org>\n"
55
"[http://www.example.com/useglobal]\n"
56
"# different project, forces global lookup\n"
59
"check_signatures=require\n"
60
"# test trailing / matching with no children\n"
62
"check_signatures=check-available\n"
63
"gpg_signing_command=false\n"
64
"user_local_option=local\n"
65
"# test trailing / matching\n"
67
"#subdirs will match but not the parent\n"
70
"check_signatures=ignore\n"
71
"post_commit=bzrlib.tests.test_config.post_commit\n"
72
"#testing explicit beats globs\n")
75
class InstrumentedConfigObj(object):
76
"""A config obj look-enough-alike to record calls made to it."""
78
def __contains__(self, thing):
79
self._calls.append(('__contains__', thing))
82
def __getitem__(self, key):
83
self._calls.append(('__getitem__', key))
86
def __init__(self, input):
87
self._calls = [('__init__', input)]
89
def __setitem__(self, key, value):
90
self._calls.append(('__setitem__', key, value))
93
self._calls.append(('write',))
96
class FakeBranch(object):
99
self.base = "http://example.com/branches/demo"
100
self.control_files = FakeControlFiles()
103
class FakeControlFiles(object):
106
self.email = 'Robert Collins <robertc@example.net>\n'
108
def get_utf8(self, filename):
109
if filename != 'email':
110
raise NotImplementedError
111
if self.email is not None:
112
return StringIO(self.email)
113
raise errors.NoSuchFile(filename)
116
class InstrumentedConfig(config.Config):
117
"""An instrumented config that supplies stubs for template methods."""
120
super(InstrumentedConfig, self).__init__()
122
self._signatures = config.CHECK_NEVER
124
def _get_user_id(self):
125
self._calls.append('_get_user_id')
126
return "Robert Collins <robert.collins@example.org>"
128
def _get_signature_checking(self):
129
self._calls.append('_get_signature_checking')
130
return self._signatures
133
class TestConfig(TestCase):
135
def test_constructs(self):
138
def test_no_default_editor(self):
139
self.assertRaises(NotImplementedError, config.Config().get_editor)
141
def test_user_email(self):
142
my_config = InstrumentedConfig()
143
self.assertEqual('robert.collins@example.org', my_config.user_email())
144
self.assertEqual(['_get_user_id'], my_config._calls)
146
def test_username(self):
147
my_config = InstrumentedConfig()
148
self.assertEqual('Robert Collins <robert.collins@example.org>',
149
my_config.username())
150
self.assertEqual(['_get_user_id'], my_config._calls)
152
def test_signatures_default(self):
153
my_config = config.Config()
154
self.assertEqual(config.CHECK_IF_POSSIBLE,
155
my_config.signature_checking())
157
def test_signatures_template_method(self):
158
my_config = InstrumentedConfig()
159
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
160
self.assertEqual(['_get_signature_checking'], my_config._calls)
162
def test_signatures_template_method_none(self):
163
my_config = InstrumentedConfig()
164
my_config._signatures = None
165
self.assertEqual(config.CHECK_IF_POSSIBLE,
166
my_config.signature_checking())
167
self.assertEqual(['_get_signature_checking'], my_config._calls)
169
def test_gpg_signing_command_default(self):
170
my_config = config.Config()
171
self.assertEqual('gpg', my_config.gpg_signing_command())
173
def test_get_user_option_default(self):
174
my_config = config.Config()
175
self.assertEqual(None, my_config.get_user_option('no_option'))
177
def test_post_commit_default(self):
178
my_config = config.Config()
179
self.assertEqual(None, my_config.post_commit())
182
class TestConfigPath(TestCase):
185
super(TestConfigPath, self).setUp()
186
self.old_home = os.environ.get('HOME', None)
187
self.old_appdata = os.environ.get('APPDATA', None)
188
os.environ['HOME'] = '/home/bogus'
189
os.environ['APPDATA'] = \
190
r'C:\Documents and Settings\bogus\Application Data'
193
if self.old_home is None:
194
del os.environ['HOME']
196
os.environ['HOME'] = self.old_home
197
if self.old_appdata is None:
198
del os.environ['APPDATA']
200
os.environ['APPDATA'] = self.old_appdata
201
super(TestConfigPath, self).tearDown()
203
def test_config_dir(self):
204
if sys.platform == 'win32':
205
self.assertEqual(config.config_dir(),
206
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
208
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
210
def test_config_filename(self):
211
if sys.platform == 'win32':
212
self.assertEqual(config.config_filename(),
213
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
215
self.assertEqual(config.config_filename(),
216
'/home/bogus/.bazaar/bazaar.conf')
218
def test_branches_config_filename(self):
219
if sys.platform == 'win32':
220
self.assertEqual(config.branches_config_filename(),
221
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
223
self.assertEqual(config.branches_config_filename(),
224
'/home/bogus/.bazaar/branches.conf')
226
class TestIniConfig(TestCase):
228
def test_contructs(self):
229
my_config = config.IniBasedConfig("nothing")
231
def test_from_fp(self):
232
config_file = StringIO(sample_config_text)
233
my_config = config.IniBasedConfig(None)
235
isinstance(my_config._get_parser(file=config_file),
238
def test_cached(self):
239
config_file = StringIO(sample_config_text)
240
my_config = config.IniBasedConfig(None)
241
parser = my_config._get_parser(file=config_file)
242
self.failUnless(my_config._get_parser() is parser)
245
class TestGetConfig(TestCase):
247
def test_constructs(self):
248
my_config = config.GlobalConfig()
250
def test_calls_read_filenames(self):
251
# replace the class that is constructured, to check its parameters
252
oldparserclass = config.ConfigObj
253
config.ConfigObj = InstrumentedConfigObj
254
my_config = config.GlobalConfig()
256
parser = my_config._get_parser()
258
config.ConfigObj = oldparserclass
259
self.failUnless(isinstance(parser, InstrumentedConfigObj))
260
self.assertEqual(parser._calls, [('__init__', config.config_filename())])
263
class TestBranchConfig(TestCaseInTempDir):
265
def test_constructs(self):
266
branch = FakeBranch()
267
my_config = config.BranchConfig(branch)
268
self.assertRaises(TypeError, config.BranchConfig)
270
def test_get_location_config(self):
271
branch = FakeBranch()
272
my_config = config.BranchConfig(branch)
273
location_config = my_config._get_location_config()
274
self.assertEqual(branch.base, location_config.location)
275
self.failUnless(location_config is my_config._get_location_config())
278
class TestGlobalConfigItems(TestCase):
280
def test_user_id(self):
281
config_file = StringIO(sample_config_text)
282
my_config = config.GlobalConfig()
283
my_config._parser = my_config._get_parser(file=config_file)
284
self.assertEqual("Robert Collins <robertc@example.com>",
285
my_config._get_user_id())
287
def test_absent_user_id(self):
288
config_file = StringIO("")
289
my_config = config.GlobalConfig()
290
my_config._parser = my_config._get_parser(file=config_file)
291
self.assertEqual(None, my_config._get_user_id())
293
def test_configured_editor(self):
294
config_file = StringIO(sample_config_text)
295
my_config = config.GlobalConfig()
296
my_config._parser = my_config._get_parser(file=config_file)
297
self.assertEqual("vim", my_config.get_editor())
299
def test_signatures_always(self):
300
config_file = StringIO(sample_always_signatures)
301
my_config = config.GlobalConfig()
302
my_config._parser = my_config._get_parser(file=config_file)
303
self.assertEqual(config.CHECK_ALWAYS,
304
my_config.signature_checking())
305
self.assertEqual(True, my_config.signature_needed())
307
def test_signatures_if_possible(self):
308
config_file = StringIO(sample_maybe_signatures)
309
my_config = config.GlobalConfig()
310
my_config._parser = my_config._get_parser(file=config_file)
311
self.assertEqual(config.CHECK_IF_POSSIBLE,
312
my_config.signature_checking())
313
self.assertEqual(False, my_config.signature_needed())
315
def test_signatures_ignore(self):
316
config_file = StringIO(sample_ignore_signatures)
317
my_config = config.GlobalConfig()
318
my_config._parser = my_config._get_parser(file=config_file)
319
self.assertEqual(config.CHECK_NEVER,
320
my_config.signature_checking())
321
self.assertEqual(False, my_config.signature_needed())
323
def _config_from_str(self, config_str):
324
config_file = StringIO(config_str)
325
my_config = config.GlobalConfig()
326
my_config._parser = my_config._get_parser(file=config_file)
329
def _get_sample_config(self):
330
return self._config_from_str(sample_config_text)
332
def test_gpg_signing_command(self):
333
my_config = self._get_sample_config()
334
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
335
self.assertEqual(False, my_config.signature_needed())
337
def _get_empty_config(self):
338
config_file = StringIO("")
339
my_config = config.GlobalConfig()
340
my_config._parser = my_config._get_parser(file=config_file)
343
def test_gpg_signing_command_unset(self):
344
my_config = self._get_empty_config()
345
self.assertEqual("gpg", my_config.gpg_signing_command())
347
def test_get_user_option_default(self):
348
my_config = self._get_empty_config()
349
self.assertEqual(None, my_config.get_user_option('no_option'))
351
def test_get_user_option_global(self):
352
my_config = self._get_sample_config()
353
self.assertEqual("something",
354
my_config.get_user_option('user_global_option'))
356
def test_post_commit_default(self):
357
my_config = self._get_sample_config()
358
self.assertEqual(None, my_config.post_commit())
360
def test_command_defaults(self):
361
my_config = self._get_sample_config()
362
self.assertEqual(['-m', 'log message'],
363
my_config.get_command_defaults('commit'))
364
self.assertEqual([], my_config.get_command_defaults('log'))
366
def test_bogus_command_default(self):
367
my_config = self._config_from_str("""[COMMAND_DEFAULTS]
368
merge=--merge-type 'weave'
369
remerge=--merge-type 'weave
371
self.assertEqual(['--merge-type', 'weave'],
372
my_config.get_command_defaults('merge'))
373
self.assertRaises(errors.CommandDefaultSyntax,
374
my_config.get_command_defaults, 'remerge')
377
class TestLocationConfig(TestCase):
379
def test_constructs(self):
380
my_config = config.LocationConfig('http://example.com')
381
self.assertRaises(TypeError, config.LocationConfig)
383
def test_branch_calls_read_filenames(self):
384
# This is testing the correct file names are provided.
385
# TODO: consolidate with the test for GlobalConfigs filename checks.
387
# replace the class that is constructured, to check its parameters
388
oldparserclass = config.ConfigObj
389
config.ConfigObj = InstrumentedConfigObj
390
my_config = config.LocationConfig('http://www.example.com')
392
parser = my_config._get_parser()
394
config.ConfigObj = oldparserclass
395
self.failUnless(isinstance(parser, InstrumentedConfigObj))
396
self.assertEqual(parser._calls,
397
[('__init__', config.branches_config_filename())])
399
def test_get_global_config(self):
400
my_config = config.LocationConfig('http://example.com')
401
global_config = my_config._get_global_config()
402
self.failUnless(isinstance(global_config, config.GlobalConfig))
403
self.failUnless(global_config is my_config._get_global_config())
405
def test__get_section_no_match(self):
406
self.get_location_config('/')
407
self.assertEqual(None, self.my_config._get_section())
409
def test__get_section_exact(self):
410
self.get_location_config('http://www.example.com')
411
self.assertEqual('http://www.example.com',
412
self.my_config._get_section())
414
def test__get_section_suffix_does_not(self):
415
self.get_location_config('http://www.example.com-com')
416
self.assertEqual(None, self.my_config._get_section())
418
def test__get_section_subdir_recursive(self):
419
self.get_location_config('http://www.example.com/com')
420
self.assertEqual('http://www.example.com',
421
self.my_config._get_section())
423
def test__get_section_subdir_matches(self):
424
self.get_location_config('http://www.example.com/useglobal')
425
self.assertEqual('http://www.example.com/useglobal',
426
self.my_config._get_section())
428
def test__get_section_subdir_nonrecursive(self):
429
self.get_location_config(
430
'http://www.example.com/useglobal/childbranch')
431
self.assertEqual('http://www.example.com',
432
self.my_config._get_section())
434
def test__get_section_subdir_trailing_slash(self):
435
self.get_location_config('/b')
436
self.assertEqual('/b/', self.my_config._get_section())
438
def test__get_section_subdir_child(self):
439
self.get_location_config('/a/foo')
440
self.assertEqual('/a/*', self.my_config._get_section())
442
def test__get_section_subdir_child_child(self):
443
self.get_location_config('/a/foo/bar')
444
self.assertEqual('/a/', self.my_config._get_section())
446
def test__get_section_trailing_slash_with_children(self):
447
self.get_location_config('/a/')
448
self.assertEqual('/a/', self.my_config._get_section())
450
def test__get_section_explicit_over_glob(self):
451
self.get_location_config('/a/c')
452
self.assertEqual('/a/c', self.my_config._get_section())
454
def get_location_config(self, location, global_config=None):
455
if global_config is None:
456
global_file = StringIO(sample_config_text)
458
global_file = StringIO(global_config)
459
branches_file = StringIO(sample_branches_text)
460
self.my_config = config.LocationConfig(location)
461
self.my_config._get_parser(branches_file)
462
self.my_config._get_global_config()._get_parser(global_file)
464
def test_location_without_username(self):
465
self.get_location_config('http://www.example.com/useglobal')
466
self.assertEqual('Robert Collins <robertc@example.com>',
467
self.my_config.username())
469
def test_location_not_listed(self):
470
self.get_location_config('/home/robertc/sources')
471
self.assertEqual('Robert Collins <robertc@example.com>',
472
self.my_config.username())
474
def test_overriding_location(self):
475
self.get_location_config('http://www.example.com/foo')
476
self.assertEqual('Robert Collins <robertc@example.org>',
477
self.my_config.username())
479
def test_signatures_not_set(self):
480
self.get_location_config('http://www.example.com',
481
global_config=sample_ignore_signatures)
482
self.assertEqual(config.CHECK_NEVER,
483
self.my_config.signature_checking())
485
def test_signatures_never(self):
486
self.get_location_config('/a/c')
487
self.assertEqual(config.CHECK_NEVER,
488
self.my_config.signature_checking())
490
def test_signatures_when_available(self):
491
self.get_location_config('/a/', global_config=sample_ignore_signatures)
492
self.assertEqual(config.CHECK_IF_POSSIBLE,
493
self.my_config.signature_checking())
495
def test_signatures_always(self):
496
self.get_location_config('/b')
497
self.assertEqual(config.CHECK_ALWAYS,
498
self.my_config.signature_checking())
500
def test_gpg_signing_command(self):
501
self.get_location_config('/b')
502
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
504
def test_gpg_signing_command_missing(self):
505
self.get_location_config('/a')
506
self.assertEqual("false", self.my_config.gpg_signing_command())
508
def test_get_user_option_global(self):
509
self.get_location_config('/a')
510
self.assertEqual('something',
511
self.my_config.get_user_option('user_global_option'))
513
def test_get_user_option_local(self):
514
self.get_location_config('/a')
515
self.assertEqual('local',
516
self.my_config.get_user_option('user_local_option'))
518
def test_post_commit_default(self):
519
self.get_location_config('/a/c')
520
self.assertEqual('bzrlib.tests.test_config.post_commit',
521
self.my_config.post_commit())
524
class TestLocationConfig(TestCaseInTempDir):
526
def get_location_config(self, location, global_config=None):
527
if global_config is None:
528
global_file = StringIO(sample_config_text)
530
global_file = StringIO(global_config)
531
branches_file = StringIO(sample_branches_text)
532
self.my_config = config.LocationConfig(location)
533
self.my_config._get_parser(branches_file)
534
self.my_config._get_global_config()._get_parser(global_file)
536
def test_set_user_setting_sets_and_saves(self):
537
self.get_location_config('/a/c')
538
record = InstrumentedConfigObj("foo")
539
self.my_config._parser = record
541
real_mkdir = os.mkdir
543
def checked_mkdir(path, mode=0777):
544
self.log('making directory: %s', path)
545
real_mkdir(path, mode)
548
os.mkdir = checked_mkdir
550
self.my_config.set_user_option('foo', 'bar')
552
os.mkdir = real_mkdir
554
self.failUnless(self.created, 'Failed to create ~/.bazaar')
555
self.assertEqual([('__contains__', '/a/c'),
556
('__contains__', '/a/c/'),
557
('__setitem__', '/a/c', {}),
558
('__getitem__', '/a/c'),
559
('__setitem__', 'foo', 'bar'),
564
class TestBranchConfigItems(TestCase):
566
def test_user_id(self):
567
branch = FakeBranch()
568
my_config = config.BranchConfig(branch)
569
self.assertEqual("Robert Collins <robertc@example.net>",
570
my_config._get_user_id())
571
branch.control_files.email = "John"
572
self.assertEqual("John", my_config._get_user_id())
574
def test_not_set_in_branch(self):
575
branch = FakeBranch()
576
my_config = config.BranchConfig(branch)
577
branch.control_files.email = None
578
config_file = StringIO(sample_config_text)
579
(my_config._get_location_config().
580
_get_global_config()._get_parser(config_file))
581
self.assertEqual("Robert Collins <robertc@example.com>",
582
my_config._get_user_id())
583
branch.control_files.email = "John"
584
self.assertEqual("John", my_config._get_user_id())
586
def test_BZREMAIL_OVERRIDES(self):
587
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
588
branch = FakeBranch()
589
my_config = config.BranchConfig(branch)
590
self.assertEqual("Robert Collins <robertc@example.org>",
591
my_config.username())
593
def test_signatures_forced(self):
594
branch = FakeBranch()
595
my_config = config.BranchConfig(branch)
596
config_file = StringIO(sample_always_signatures)
597
(my_config._get_location_config().
598
_get_global_config()._get_parser(config_file))
599
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
601
def test_gpg_signing_command(self):
602
branch = FakeBranch()
603
my_config = config.BranchConfig(branch)
604
config_file = StringIO(sample_config_text)
605
(my_config._get_location_config().
606
_get_global_config()._get_parser(config_file))
607
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
609
def test_get_user_option_global(self):
610
branch = FakeBranch()
611
my_config = config.BranchConfig(branch)
612
config_file = StringIO(sample_config_text)
613
(my_config._get_location_config().
614
_get_global_config()._get_parser(config_file))
615
self.assertEqual('something',
616
my_config.get_user_option('user_global_option'))
618
def test_post_commit_default(self):
619
branch = FakeBranch()
621
my_config = config.BranchConfig(branch)
622
config_file = StringIO(sample_config_text)
623
(my_config._get_location_config().
624
_get_global_config()._get_parser(config_file))
625
branch_file = StringIO(sample_branches_text)
626
my_config._get_location_config()._get_parser(branch_file)
627
self.assertEqual('bzrlib.tests.test_config.post_commit',
628
my_config.post_commit())
631
class TestMailAddressExtraction(TestCase):
633
def test_extract_email_address(self):
634
self.assertEqual('jane@test.com',
635
config.extract_email_address('Jane <jane@test.com>'))
636
self.assertRaises(errors.BzrError,
637
config.extract_email_address, 'Jane Tester')