1
# Copyright (C) 2005, 2006 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
from bzrlib import config, errors, osutils, urlutils
27
from bzrlib.branch import Branch
28
from bzrlib.bzrdir import BzrDir
29
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
32
sample_long_alias="log -r-15..-1 --line"
33
sample_config_text = ("[DEFAULT]\n"
34
u"email=Erik B\u00e5gfors <erik@bagfors.nu>\n"
36
"gpg_signing_command=gnome-gpg\n"
38
"user_global_option=something\n"
41
"ll=" + sample_long_alias + "\n")
44
sample_always_signatures = ("[DEFAULT]\n"
45
"check_signatures=ignore\n"
46
"create_signatures=always")
49
sample_ignore_signatures = ("[DEFAULT]\n"
50
"check_signatures=require\n"
51
"create_signatures=never")
54
sample_maybe_signatures = ("[DEFAULT]\n"
55
"check_signatures=ignore\n"
56
"create_signatures=when-required")
59
sample_branches_text = ("[http://www.example.com]\n"
60
"# Top level policy\n"
61
"email=Robert Collins <robertc@example.org>\n"
62
"[http://www.example.com/useglobal]\n"
63
"# different project, forces global lookup\n"
66
"check_signatures=require\n"
67
"# test trailing / matching with no children\n"
69
"check_signatures=check-available\n"
70
"gpg_signing_command=false\n"
71
"user_local_option=local\n"
72
"# test trailing / matching\n"
74
"#subdirs will match but not the parent\n"
77
"check_signatures=ignore\n"
78
"post_commit=bzrlib.tests.test_config.post_commit\n"
79
"#testing explicit beats globs\n")
83
class InstrumentedConfigObj(object):
84
"""A config obj look-enough-alike to record calls made to it."""
86
def __contains__(self, thing):
87
self._calls.append(('__contains__', thing))
90
def __getitem__(self, key):
91
self._calls.append(('__getitem__', key))
94
def __init__(self, input, encoding=None):
95
self._calls = [('__init__', input, encoding)]
97
def __setitem__(self, key, value):
98
self._calls.append(('__setitem__', key, value))
100
def write(self, arg):
101
self._calls.append(('write',))
104
class FakeBranch(object):
106
def __init__(self, base=None, user_id=None):
108
self.base = "http://example.com/branches/demo"
111
self.control_files = FakeControlFiles(user_id=user_id)
113
def lock_write(self):
120
class FakeControlFiles(object):
122
def __init__(self, user_id=None):
126
def get_utf8(self, filename):
127
if filename != 'email':
128
raise NotImplementedError
129
if self.email is not None:
130
return StringIO(self.email)
131
raise errors.NoSuchFile(filename)
133
def get(self, filename):
135
return StringIO(self.files[filename])
137
raise errors.NoSuchFile(filename)
139
def put(self, filename, fileobj):
140
self.files[filename] = fileobj.read()
143
class InstrumentedConfig(config.Config):
144
"""An instrumented config that supplies stubs for template methods."""
147
super(InstrumentedConfig, self).__init__()
149
self._signatures = config.CHECK_NEVER
151
def _get_user_id(self):
152
self._calls.append('_get_user_id')
153
return "Robert Collins <robert.collins@example.org>"
155
def _get_signature_checking(self):
156
self._calls.append('_get_signature_checking')
157
return self._signatures
160
bool_config = """[DEFAULT]
167
class TestConfigObj(TestCase):
168
def test_get_bool(self):
169
from bzrlib.config import ConfigObj
170
co = ConfigObj(StringIO(bool_config))
171
self.assertIs(co.get_bool('DEFAULT', 'active'), True)
172
self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
173
self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
174
self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
177
class TestConfig(TestCase):
179
def test_constructs(self):
182
def test_no_default_editor(self):
183
self.assertRaises(NotImplementedError, config.Config().get_editor)
185
def test_user_email(self):
186
my_config = InstrumentedConfig()
187
self.assertEqual('robert.collins@example.org', my_config.user_email())
188
self.assertEqual(['_get_user_id'], my_config._calls)
190
def test_username(self):
191
my_config = InstrumentedConfig()
192
self.assertEqual('Robert Collins <robert.collins@example.org>',
193
my_config.username())
194
self.assertEqual(['_get_user_id'], my_config._calls)
196
def test_signatures_default(self):
197
my_config = config.Config()
198
self.assertFalse(my_config.signature_needed())
199
self.assertEqual(config.CHECK_IF_POSSIBLE,
200
my_config.signature_checking())
201
self.assertEqual(config.SIGN_WHEN_REQUIRED,
202
my_config.signing_policy())
204
def test_signatures_template_method(self):
205
my_config = InstrumentedConfig()
206
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
207
self.assertEqual(['_get_signature_checking'], my_config._calls)
209
def test_signatures_template_method_none(self):
210
my_config = InstrumentedConfig()
211
my_config._signatures = None
212
self.assertEqual(config.CHECK_IF_POSSIBLE,
213
my_config.signature_checking())
214
self.assertEqual(['_get_signature_checking'], my_config._calls)
216
def test_gpg_signing_command_default(self):
217
my_config = config.Config()
218
self.assertEqual('gpg', my_config.gpg_signing_command())
220
def test_get_user_option_default(self):
221
my_config = config.Config()
222
self.assertEqual(None, my_config.get_user_option('no_option'))
224
def test_post_commit_default(self):
225
my_config = config.Config()
226
self.assertEqual(None, my_config.post_commit())
228
def test_log_format_default(self):
229
my_config = config.Config()
230
self.assertEqual('long', my_config.log_format())
233
class TestConfigPath(TestCase):
236
super(TestConfigPath, self).setUp()
237
self.old_home = os.environ.get('HOME', None)
238
self.old_appdata = os.environ.get('APPDATA', None)
239
os.environ['HOME'] = '/home/bogus'
240
os.environ['APPDATA'] = \
241
r'C:\Documents and Settings\bogus\Application Data'
244
if self.old_home is None:
245
del os.environ['HOME']
247
os.environ['HOME'] = self.old_home
248
if self.old_appdata is None:
249
del os.environ['APPDATA']
251
os.environ['APPDATA'] = self.old_appdata
252
super(TestConfigPath, self).tearDown()
254
def test_config_dir(self):
255
if sys.platform == 'win32':
256
self.assertEqual(config.config_dir(),
257
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
259
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
261
def test_config_filename(self):
262
if sys.platform == 'win32':
263
self.assertEqual(config.config_filename(),
264
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
266
self.assertEqual(config.config_filename(),
267
'/home/bogus/.bazaar/bazaar.conf')
269
def test_branches_config_filename(self):
270
if sys.platform == 'win32':
271
self.assertEqual(config.branches_config_filename(),
272
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
274
self.assertEqual(config.branches_config_filename(),
275
'/home/bogus/.bazaar/branches.conf')
277
def test_locations_config_filename(self):
278
if sys.platform == 'win32':
279
self.assertEqual(config.locations_config_filename(),
280
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
282
self.assertEqual(config.locations_config_filename(),
283
'/home/bogus/.bazaar/locations.conf')
285
class TestIniConfig(TestCase):
287
def test_contructs(self):
288
my_config = config.IniBasedConfig("nothing")
290
def test_from_fp(self):
291
config_file = StringIO(sample_config_text.encode('utf-8'))
292
my_config = config.IniBasedConfig(None)
294
isinstance(my_config._get_parser(file=config_file),
297
def test_cached(self):
298
config_file = StringIO(sample_config_text.encode('utf-8'))
299
my_config = config.IniBasedConfig(None)
300
parser = my_config._get_parser(file=config_file)
301
self.failUnless(my_config._get_parser() is parser)
304
class TestGetConfig(TestCase):
306
def test_constructs(self):
307
my_config = config.GlobalConfig()
309
def test_calls_read_filenames(self):
310
# replace the class that is constructured, to check its parameters
311
oldparserclass = config.ConfigObj
312
config.ConfigObj = InstrumentedConfigObj
313
my_config = config.GlobalConfig()
315
parser = my_config._get_parser()
317
config.ConfigObj = oldparserclass
318
self.failUnless(isinstance(parser, InstrumentedConfigObj))
319
self.assertEqual(parser._calls, [('__init__', config.config_filename(),
323
class TestBranchConfig(TestCaseWithTransport):
325
def test_constructs(self):
326
branch = FakeBranch()
327
my_config = config.BranchConfig(branch)
328
self.assertRaises(TypeError, config.BranchConfig)
330
def test_get_location_config(self):
331
branch = FakeBranch()
332
my_config = config.BranchConfig(branch)
333
location_config = my_config._get_location_config()
334
self.assertEqual(branch.base, location_config.location)
335
self.failUnless(location_config is my_config._get_location_config())
337
def test_get_config(self):
338
"""The Branch.get_config method works properly"""
339
b = BzrDir.create_standalone_workingtree('.').branch
340
my_config = b.get_config()
341
self.assertIs(my_config.get_user_option('wacky'), None)
342
my_config.set_user_option('wacky', 'unlikely')
343
self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
345
# Ensure we get the same thing if we start again
346
b2 = Branch.open('.')
347
my_config2 = b2.get_config()
348
self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
350
def test_has_explicit_nickname(self):
351
b = self.make_branch('.')
352
self.assertFalse(b.get_config().has_explicit_nickname())
354
self.assertTrue(b.get_config().has_explicit_nickname())
356
def test_config_url(self):
357
"""The Branch.get_config will use section that uses a local url"""
358
branch = self.make_branch('branch')
359
self.assertEqual('branch', branch.nick)
361
locations = config.locations_config_filename()
362
config.ensure_config_dir_exists()
363
local_url = urlutils.local_path_to_url('branch')
364
open(locations, 'wb').write('[%s]\nnickname = foobar'
366
self.assertEqual('foobar', branch.nick)
368
def test_config_local_path(self):
369
"""The Branch.get_config will use a local system path"""
370
branch = self.make_branch('branch')
371
self.assertEqual('branch', branch.nick)
373
locations = config.locations_config_filename()
374
config.ensure_config_dir_exists()
375
open(locations, 'wb').write('[%s/branch]\nnickname = barry'
376
% (osutils.getcwd().encode('utf8'),))
377
self.assertEqual('barry', branch.nick)
380
class TestGlobalConfigItems(TestCase):
382
def test_user_id(self):
383
config_file = StringIO(sample_config_text.encode('utf-8'))
384
my_config = config.GlobalConfig()
385
my_config._parser = my_config._get_parser(file=config_file)
386
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
387
my_config._get_user_id())
389
def test_absent_user_id(self):
390
config_file = StringIO("")
391
my_config = config.GlobalConfig()
392
my_config._parser = my_config._get_parser(file=config_file)
393
self.assertEqual(None, my_config._get_user_id())
395
def test_configured_editor(self):
396
config_file = StringIO(sample_config_text.encode('utf-8'))
397
my_config = config.GlobalConfig()
398
my_config._parser = my_config._get_parser(file=config_file)
399
self.assertEqual("vim", my_config.get_editor())
401
def test_signatures_always(self):
402
config_file = StringIO(sample_always_signatures)
403
my_config = config.GlobalConfig()
404
my_config._parser = my_config._get_parser(file=config_file)
405
self.assertEqual(config.CHECK_NEVER,
406
my_config.signature_checking())
407
self.assertEqual(config.SIGN_ALWAYS,
408
my_config.signing_policy())
409
self.assertEqual(True, my_config.signature_needed())
411
def test_signatures_if_possible(self):
412
config_file = StringIO(sample_maybe_signatures)
413
my_config = config.GlobalConfig()
414
my_config._parser = my_config._get_parser(file=config_file)
415
self.assertEqual(config.CHECK_NEVER,
416
my_config.signature_checking())
417
self.assertEqual(config.SIGN_WHEN_REQUIRED,
418
my_config.signing_policy())
419
self.assertEqual(False, my_config.signature_needed())
421
def test_signatures_ignore(self):
422
config_file = StringIO(sample_ignore_signatures)
423
my_config = config.GlobalConfig()
424
my_config._parser = my_config._get_parser(file=config_file)
425
self.assertEqual(config.CHECK_ALWAYS,
426
my_config.signature_checking())
427
self.assertEqual(config.SIGN_NEVER,
428
my_config.signing_policy())
429
self.assertEqual(False, my_config.signature_needed())
431
def _get_sample_config(self):
432
config_file = StringIO(sample_config_text.encode('utf-8'))
433
my_config = config.GlobalConfig()
434
my_config._parser = my_config._get_parser(file=config_file)
437
def test_gpg_signing_command(self):
438
my_config = self._get_sample_config()
439
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
440
self.assertEqual(False, my_config.signature_needed())
442
def _get_empty_config(self):
443
config_file = StringIO("")
444
my_config = config.GlobalConfig()
445
my_config._parser = my_config._get_parser(file=config_file)
448
def test_gpg_signing_command_unset(self):
449
my_config = self._get_empty_config()
450
self.assertEqual("gpg", my_config.gpg_signing_command())
452
def test_get_user_option_default(self):
453
my_config = self._get_empty_config()
454
self.assertEqual(None, my_config.get_user_option('no_option'))
456
def test_get_user_option_global(self):
457
my_config = self._get_sample_config()
458
self.assertEqual("something",
459
my_config.get_user_option('user_global_option'))
461
def test_post_commit_default(self):
462
my_config = self._get_sample_config()
463
self.assertEqual(None, my_config.post_commit())
465
def test_configured_logformat(self):
466
my_config = self._get_sample_config()
467
self.assertEqual("short", my_config.log_format())
469
def test_get_alias(self):
470
my_config = self._get_sample_config()
471
self.assertEqual('help', my_config.get_alias('h'))
473
def test_get_no_alias(self):
474
my_config = self._get_sample_config()
475
self.assertEqual(None, my_config.get_alias('foo'))
477
def test_get_long_alias(self):
478
my_config = self._get_sample_config()
479
self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
482
class TestLocationConfig(TestCaseInTempDir):
484
def test_constructs(self):
485
my_config = config.LocationConfig('http://example.com')
486
self.assertRaises(TypeError, config.LocationConfig)
488
def test_branch_calls_read_filenames(self):
489
# This is testing the correct file names are provided.
490
# TODO: consolidate with the test for GlobalConfigs filename checks.
492
# replace the class that is constructured, to check its parameters
493
oldparserclass = config.ConfigObj
494
config.ConfigObj = InstrumentedConfigObj
496
my_config = config.LocationConfig('http://www.example.com')
497
parser = my_config._get_parser()
499
config.ConfigObj = oldparserclass
500
self.failUnless(isinstance(parser, InstrumentedConfigObj))
501
self.assertEqual(parser._calls,
502
[('__init__', config.locations_config_filename(),
504
config.ensure_config_dir_exists()
505
#os.mkdir(config.config_dir())
506
f = file(config.branches_config_filename(), 'wb')
509
oldparserclass = config.ConfigObj
510
config.ConfigObj = InstrumentedConfigObj
512
my_config = config.LocationConfig('http://www.example.com')
513
parser = my_config._get_parser()
515
config.ConfigObj = oldparserclass
517
def test_get_global_config(self):
518
my_config = config.BranchConfig(FakeBranch('http://example.com'))
519
global_config = my_config._get_global_config()
520
self.failUnless(isinstance(global_config, config.GlobalConfig))
521
self.failUnless(global_config is my_config._get_global_config())
523
def test__get_section_no_match(self):
524
self.get_branch_config('/')
525
self.assertEqual(None, self.my_location_config._get_section())
527
def test__get_section_exact(self):
528
self.get_branch_config('http://www.example.com')
529
self.assertEqual('http://www.example.com',
530
self.my_location_config._get_section())
532
def test__get_section_suffix_does_not(self):
533
self.get_branch_config('http://www.example.com-com')
534
self.assertEqual(None, self.my_location_config._get_section())
536
def test__get_section_subdir_recursive(self):
537
self.get_branch_config('http://www.example.com/com')
538
self.assertEqual('http://www.example.com',
539
self.my_location_config._get_section())
541
def test__get_section_subdir_matches(self):
542
self.get_branch_config('http://www.example.com/useglobal')
543
self.assertEqual('http://www.example.com/useglobal',
544
self.my_location_config._get_section())
546
def test__get_section_subdir_nonrecursive(self):
547
self.get_branch_config(
548
'http://www.example.com/useglobal/childbranch')
549
self.assertEqual('http://www.example.com',
550
self.my_location_config._get_section())
552
def test__get_section_subdir_trailing_slash(self):
553
self.get_branch_config('/b')
554
self.assertEqual('/b/', self.my_location_config._get_section())
556
def test__get_section_subdir_child(self):
557
self.get_branch_config('/a/foo')
558
self.assertEqual('/a/*', self.my_location_config._get_section())
560
def test__get_section_subdir_child_child(self):
561
self.get_branch_config('/a/foo/bar')
562
self.assertEqual('/a/', self.my_location_config._get_section())
564
def test__get_section_trailing_slash_with_children(self):
565
self.get_branch_config('/a/')
566
self.assertEqual('/a/', self.my_location_config._get_section())
568
def test__get_section_explicit_over_glob(self):
569
self.get_branch_config('/a/c')
570
self.assertEqual('/a/c', self.my_location_config._get_section())
573
def test_location_without_username(self):
574
self.get_branch_config('http://www.example.com/useglobal')
575
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
576
self.my_config.username())
578
def test_location_not_listed(self):
579
"""Test that the global username is used when no location matches"""
580
self.get_branch_config('/home/robertc/sources')
581
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
582
self.my_config.username())
584
def test_overriding_location(self):
585
self.get_branch_config('http://www.example.com/foo')
586
self.assertEqual('Robert Collins <robertc@example.org>',
587
self.my_config.username())
589
def test_signatures_not_set(self):
590
self.get_branch_config('http://www.example.com',
591
global_config=sample_ignore_signatures)
592
self.assertEqual(config.CHECK_ALWAYS,
593
self.my_config.signature_checking())
594
self.assertEqual(config.SIGN_NEVER,
595
self.my_config.signing_policy())
597
def test_signatures_never(self):
598
self.get_branch_config('/a/c')
599
self.assertEqual(config.CHECK_NEVER,
600
self.my_config.signature_checking())
602
def test_signatures_when_available(self):
603
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
604
self.assertEqual(config.CHECK_IF_POSSIBLE,
605
self.my_config.signature_checking())
607
def test_signatures_always(self):
608
self.get_branch_config('/b')
609
self.assertEqual(config.CHECK_ALWAYS,
610
self.my_config.signature_checking())
612
def test_gpg_signing_command(self):
613
self.get_branch_config('/b')
614
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
616
def test_gpg_signing_command_missing(self):
617
self.get_branch_config('/a')
618
self.assertEqual("false", self.my_config.gpg_signing_command())
620
def test_get_user_option_global(self):
621
self.get_branch_config('/a')
622
self.assertEqual('something',
623
self.my_config.get_user_option('user_global_option'))
625
def test_get_user_option_local(self):
626
self.get_branch_config('/a')
627
self.assertEqual('local',
628
self.my_config.get_user_option('user_local_option'))
630
def test_post_commit_default(self):
631
self.get_branch_config('/a/c')
632
self.assertEqual('bzrlib.tests.test_config.post_commit',
633
self.my_config.post_commit())
635
def get_branch_config(self, location, global_config=None):
636
if global_config is None:
637
global_file = StringIO(sample_config_text.encode('utf-8'))
639
global_file = StringIO(global_config.encode('utf-8'))
640
branches_file = StringIO(sample_branches_text.encode('utf-8'))
641
self.my_config = config.BranchConfig(FakeBranch(location))
642
# Force location config to use specified file
643
self.my_location_config = self.my_config._get_location_config()
644
self.my_location_config._get_parser(branches_file)
645
# Force global config to use specified file
646
self.my_config._get_global_config()._get_parser(global_file)
648
def test_set_user_setting_sets_and_saves(self):
649
self.get_branch_config('/a/c')
650
record = InstrumentedConfigObj("foo")
651
self.my_location_config._parser = record
653
real_mkdir = os.mkdir
655
def checked_mkdir(path, mode=0777):
656
self.log('making directory: %s', path)
657
real_mkdir(path, mode)
660
os.mkdir = checked_mkdir
662
self.my_config.set_user_option('foo', 'bar', local=True)
664
os.mkdir = real_mkdir
666
self.failUnless(self.created, 'Failed to create ~/.bazaar')
667
self.assertEqual([('__contains__', '/a/c'),
668
('__contains__', '/a/c/'),
669
('__setitem__', '/a/c', {}),
670
('__getitem__', '/a/c'),
671
('__setitem__', 'foo', 'bar'),
675
def test_set_user_setting_sets_and_saves2(self):
676
self.get_branch_config('/a/c')
677
self.assertIs(self.my_config.get_user_option('foo'), None)
678
self.my_config.set_user_option('foo', 'bar')
680
self.my_config.branch.control_files.files['branch.conf'],
682
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
683
self.my_config.set_user_option('foo', 'baz', local=True)
684
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
685
self.my_config.set_user_option('foo', 'qux')
686
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
689
precedence_global = 'option = global'
690
precedence_branch = 'option = branch'
691
precedence_location = """
695
[http://example.com/specific]
700
class TestBranchConfigItems(TestCaseInTempDir):
702
def get_branch_config(self, global_config=None, location=None,
703
location_config=None, branch_data_config=None):
704
my_config = config.BranchConfig(FakeBranch(location))
705
if global_config is not None:
706
global_file = StringIO(global_config.encode('utf-8'))
707
my_config._get_global_config()._get_parser(global_file)
708
self.my_location_config = my_config._get_location_config()
709
if location_config is not None:
710
location_file = StringIO(location_config.encode('utf-8'))
711
self.my_location_config._get_parser(location_file)
712
if branch_data_config is not None:
713
my_config.branch.control_files.files['branch.conf'] = \
717
def test_user_id(self):
718
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
719
my_config = config.BranchConfig(branch)
720
self.assertEqual("Robert Collins <robertc@example.net>",
721
my_config.username())
722
branch.control_files.email = "John"
723
my_config.set_user_option('email',
724
"Robert Collins <robertc@example.org>")
725
self.assertEqual("John", my_config.username())
726
branch.control_files.email = None
727
self.assertEqual("Robert Collins <robertc@example.org>",
728
my_config.username())
730
def test_not_set_in_branch(self):
731
my_config = self.get_branch_config(sample_config_text)
732
my_config.branch.control_files.email = None
733
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
734
my_config._get_user_id())
735
my_config.branch.control_files.email = "John"
736
self.assertEqual("John", my_config._get_user_id())
738
def test_BZREMAIL_OVERRIDES(self):
739
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
740
branch = FakeBranch()
741
my_config = config.BranchConfig(branch)
742
self.assertEqual("Robert Collins <robertc@example.org>",
743
my_config.username())
745
def test_signatures_forced(self):
746
my_config = self.get_branch_config(
747
global_config=sample_always_signatures)
748
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
749
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
750
self.assertTrue(my_config.signature_needed())
752
def test_signatures_forced_branch(self):
753
my_config = self.get_branch_config(
754
global_config=sample_ignore_signatures,
755
branch_data_config=sample_always_signatures)
756
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
757
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
758
self.assertTrue(my_config.signature_needed())
760
def test_gpg_signing_command(self):
761
my_config = self.get_branch_config(
762
# branch data cannot set gpg_signing_command
763
branch_data_config="gpg_signing_command=pgp")
764
config_file = StringIO(sample_config_text.encode('utf-8'))
765
my_config._get_global_config()._get_parser(config_file)
766
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
768
def test_get_user_option_global(self):
769
branch = FakeBranch()
770
my_config = config.BranchConfig(branch)
771
config_file = StringIO(sample_config_text.encode('utf-8'))
772
(my_config._get_global_config()._get_parser(config_file))
773
self.assertEqual('something',
774
my_config.get_user_option('user_global_option'))
776
def test_post_commit_default(self):
777
branch = FakeBranch()
778
my_config = self.get_branch_config(sample_config_text, '/a/c',
779
sample_branches_text)
780
self.assertEqual(my_config.branch.base, '/a/c')
781
self.assertEqual('bzrlib.tests.test_config.post_commit',
782
my_config.post_commit())
783
my_config.set_user_option('post_commit', 'rmtree_root')
784
# post-commit is ignored when bresent in branch data
785
self.assertEqual('bzrlib.tests.test_config.post_commit',
786
my_config.post_commit())
787
my_config.set_user_option('post_commit', 'rmtree_root', local=True)
788
self.assertEqual('rmtree_root', my_config.post_commit())
790
def test_config_precedence(self):
791
my_config = self.get_branch_config(global_config=precedence_global)
792
self.assertEqual(my_config.get_user_option('option'), 'global')
793
my_config = self.get_branch_config(global_config=precedence_global,
794
branch_data_config=precedence_branch)
795
self.assertEqual(my_config.get_user_option('option'), 'branch')
796
my_config = self.get_branch_config(global_config=precedence_global,
797
branch_data_config=precedence_branch,
798
location_config=precedence_location)
799
self.assertEqual(my_config.get_user_option('option'), 'recurse')
800
my_config = self.get_branch_config(global_config=precedence_global,
801
branch_data_config=precedence_branch,
802
location_config=precedence_location,
803
location='http://example.com/specific')
804
self.assertEqual(my_config.get_user_option('option'), 'exact')
807
class TestMailAddressExtraction(TestCase):
809
def test_extract_email_address(self):
810
self.assertEqual('jane@test.com',
811
config.extract_email_address('Jane <jane@test.com>'))
812
self.assertRaises(errors.BzrError,
813
config.extract_email_address, 'Jane Tester')