1
# Copyright (C) 2005, 2006 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
34
from bzrlib.branch import Branch
35
from bzrlib.bzrdir import BzrDir
36
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
39
sample_long_alias="log -r-15..-1 --line"
40
sample_config_text = u"""
42
email=Erik B\u00e5gfors <erik@bagfors.nu>
44
gpg_signing_command=gnome-gpg
46
user_global_option=something
49
ll=""" + sample_long_alias + "\n"
52
sample_always_signatures = """
54
check_signatures=ignore
55
create_signatures=always
58
sample_ignore_signatures = """
60
check_signatures=require
61
create_signatures=never
64
sample_maybe_signatures = """
66
check_signatures=ignore
67
create_signatures=when-required
70
sample_branches_text = """
71
[http://www.example.com]
73
email=Robert Collins <robertc@example.org>
74
normal_option = normal
75
appendpath_option = append
76
appendpath_option:policy = appendpath
77
norecurse_option = norecurse
78
norecurse_option:policy = norecurse
79
[http://www.example.com/ignoreparent]
80
# different project: ignore parent dir config
82
[http://www.example.com/norecurse]
83
# configuration items that only apply to this dir
85
normal_option = norecurse
86
[http://www.example.com/dir]
87
appendpath_option = normal
89
check_signatures=require
90
# test trailing / matching with no children
92
check_signatures=check-available
93
gpg_signing_command=false
94
user_local_option=local
95
# test trailing / matching
97
#subdirs will match but not the parent
99
check_signatures=ignore
100
post_commit=bzrlib.tests.test_config.post_commit
101
#testing explicit beats globs
105
class InstrumentedConfigObj(object):
106
"""A config obj look-enough-alike to record calls made to it."""
108
def __contains__(self, thing):
109
self._calls.append(('__contains__', thing))
112
def __getitem__(self, key):
113
self._calls.append(('__getitem__', key))
116
def __init__(self, input, encoding=None):
117
self._calls = [('__init__', input, encoding)]
119
def __setitem__(self, key, value):
120
self._calls.append(('__setitem__', key, value))
122
def __delitem__(self, key):
123
self._calls.append(('__delitem__', key))
126
self._calls.append(('keys',))
129
def write(self, arg):
130
self._calls.append(('write',))
132
def as_bool(self, value):
133
self._calls.append(('as_bool', value))
136
def get_value(self, section, name):
137
self._calls.append(('get_value', section, name))
141
class FakeBranch(object):
143
def __init__(self, base=None, user_id=None):
145
self.base = "http://example.com/branches/demo"
148
self.control_files = FakeControlFiles(user_id=user_id)
150
def lock_write(self):
157
class FakeControlFiles(object):
159
def __init__(self, user_id=None):
163
def get_utf8(self, filename):
164
if filename != 'email':
165
raise NotImplementedError
166
if self.email is not None:
167
return StringIO(self.email)
168
raise errors.NoSuchFile(filename)
170
def get(self, filename):
172
return StringIO(self.files[filename])
174
raise errors.NoSuchFile(filename)
176
def put(self, filename, fileobj):
177
self.files[filename] = fileobj.read()
180
class InstrumentedConfig(config.Config):
181
"""An instrumented config that supplies stubs for template methods."""
184
super(InstrumentedConfig, self).__init__()
186
self._signatures = config.CHECK_NEVER
188
def _get_user_id(self):
189
self._calls.append('_get_user_id')
190
return "Robert Collins <robert.collins@example.org>"
192
def _get_signature_checking(self):
193
self._calls.append('_get_signature_checking')
194
return self._signatures
197
bool_config = """[DEFAULT]
204
class TestConfigObj(TestCase):
205
def test_get_bool(self):
206
from bzrlib.config import ConfigObj
207
co = ConfigObj(StringIO(bool_config))
208
self.assertIs(co.get_bool('DEFAULT', 'active'), True)
209
self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
210
self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
211
self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
214
erroneous_config = """[section] # line 1
217
whocares=notme # line 4
219
class TestConfigObjErrors(TestCase):
221
def test_duplicate_section_name_error_line(self):
223
co = ConfigObj(StringIO(erroneous_config), raise_errors=True)
224
except config.configobj.DuplicateError, e:
225
self.assertEqual(3, e.line_number)
227
self.fail('Error in config file not detected')
229
class TestConfig(TestCase):
231
def test_constructs(self):
234
def test_no_default_editor(self):
235
self.assertRaises(NotImplementedError, config.Config().get_editor)
237
def test_user_email(self):
238
my_config = InstrumentedConfig()
239
self.assertEqual('robert.collins@example.org', my_config.user_email())
240
self.assertEqual(['_get_user_id'], my_config._calls)
242
def test_username(self):
243
my_config = InstrumentedConfig()
244
self.assertEqual('Robert Collins <robert.collins@example.org>',
245
my_config.username())
246
self.assertEqual(['_get_user_id'], my_config._calls)
248
def test_signatures_default(self):
249
my_config = config.Config()
250
self.assertFalse(my_config.signature_needed())
251
self.assertEqual(config.CHECK_IF_POSSIBLE,
252
my_config.signature_checking())
253
self.assertEqual(config.SIGN_WHEN_REQUIRED,
254
my_config.signing_policy())
256
def test_signatures_template_method(self):
257
my_config = InstrumentedConfig()
258
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
259
self.assertEqual(['_get_signature_checking'], my_config._calls)
261
def test_signatures_template_method_none(self):
262
my_config = InstrumentedConfig()
263
my_config._signatures = None
264
self.assertEqual(config.CHECK_IF_POSSIBLE,
265
my_config.signature_checking())
266
self.assertEqual(['_get_signature_checking'], my_config._calls)
268
def test_gpg_signing_command_default(self):
269
my_config = config.Config()
270
self.assertEqual('gpg', my_config.gpg_signing_command())
272
def test_get_user_option_default(self):
273
my_config = config.Config()
274
self.assertEqual(None, my_config.get_user_option('no_option'))
276
def test_post_commit_default(self):
277
my_config = config.Config()
278
self.assertEqual(None, my_config.post_commit())
280
def test_log_format_default(self):
281
my_config = config.Config()
282
self.assertEqual('long', my_config.log_format())
285
class TestConfigPath(TestCase):
288
super(TestConfigPath, self).setUp()
289
os.environ['HOME'] = '/home/bogus'
290
if sys.platform == 'win32':
291
os.environ['BZR_HOME'] = \
292
r'C:\Documents and Settings\bogus\Application Data'
294
def test_config_dir(self):
295
if sys.platform == 'win32':
296
self.assertEqual(config.config_dir(),
297
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
299
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
301
def test_config_filename(self):
302
if sys.platform == 'win32':
303
self.assertEqual(config.config_filename(),
304
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
306
self.assertEqual(config.config_filename(),
307
'/home/bogus/.bazaar/bazaar.conf')
309
def test_branches_config_filename(self):
310
if sys.platform == 'win32':
311
self.assertEqual(config.branches_config_filename(),
312
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
314
self.assertEqual(config.branches_config_filename(),
315
'/home/bogus/.bazaar/branches.conf')
317
def test_locations_config_filename(self):
318
if sys.platform == 'win32':
319
self.assertEqual(config.locations_config_filename(),
320
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
322
self.assertEqual(config.locations_config_filename(),
323
'/home/bogus/.bazaar/locations.conf')
325
class TestIniConfig(TestCase):
327
def test_contructs(self):
328
my_config = config.IniBasedConfig("nothing")
330
def test_from_fp(self):
331
config_file = StringIO(sample_config_text.encode('utf-8'))
332
my_config = config.IniBasedConfig(None)
334
isinstance(my_config._get_parser(file=config_file),
337
def test_cached(self):
338
config_file = StringIO(sample_config_text.encode('utf-8'))
339
my_config = config.IniBasedConfig(None)
340
parser = my_config._get_parser(file=config_file)
341
self.failUnless(my_config._get_parser() is parser)
344
class TestGetConfig(TestCase):
346
def test_constructs(self):
347
my_config = config.GlobalConfig()
349
def test_calls_read_filenames(self):
350
# replace the class that is constructured, to check its parameters
351
oldparserclass = config.ConfigObj
352
config.ConfigObj = InstrumentedConfigObj
353
my_config = config.GlobalConfig()
355
parser = my_config._get_parser()
357
config.ConfigObj = oldparserclass
358
self.failUnless(isinstance(parser, InstrumentedConfigObj))
359
self.assertEqual(parser._calls, [('__init__', config.config_filename(),
363
class TestBranchConfig(TestCaseWithTransport):
365
def test_constructs(self):
366
branch = FakeBranch()
367
my_config = config.BranchConfig(branch)
368
self.assertRaises(TypeError, config.BranchConfig)
370
def test_get_location_config(self):
371
branch = FakeBranch()
372
my_config = config.BranchConfig(branch)
373
location_config = my_config._get_location_config()
374
self.assertEqual(branch.base, location_config.location)
375
self.failUnless(location_config is my_config._get_location_config())
377
def test_get_config(self):
378
"""The Branch.get_config method works properly"""
379
b = BzrDir.create_standalone_workingtree('.').branch
380
my_config = b.get_config()
381
self.assertIs(my_config.get_user_option('wacky'), None)
382
my_config.set_user_option('wacky', 'unlikely')
383
self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
385
# Ensure we get the same thing if we start again
386
b2 = Branch.open('.')
387
my_config2 = b2.get_config()
388
self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
390
def test_has_explicit_nickname(self):
391
b = self.make_branch('.')
392
self.assertFalse(b.get_config().has_explicit_nickname())
394
self.assertTrue(b.get_config().has_explicit_nickname())
396
def test_config_url(self):
397
"""The Branch.get_config will use section that uses a local url"""
398
branch = self.make_branch('branch')
399
self.assertEqual('branch', branch.nick)
401
locations = config.locations_config_filename()
402
config.ensure_config_dir_exists()
403
local_url = urlutils.local_path_to_url('branch')
404
open(locations, 'wb').write('[%s]\nnickname = foobar'
406
self.assertEqual('foobar', branch.nick)
408
def test_config_local_path(self):
409
"""The Branch.get_config will use a local system path"""
410
branch = self.make_branch('branch')
411
self.assertEqual('branch', branch.nick)
413
locations = config.locations_config_filename()
414
config.ensure_config_dir_exists()
415
open(locations, 'wb').write('[%s/branch]\nnickname = barry'
416
% (osutils.getcwd().encode('utf8'),))
417
self.assertEqual('barry', branch.nick)
419
def test_config_creates_local(self):
420
"""Creating a new entry in config uses a local path."""
421
branch = self.make_branch('branch', format='knit')
422
branch.set_push_location('http://foobar')
423
locations = config.locations_config_filename()
424
local_path = osutils.getcwd().encode('utf8')
425
# Surprisingly ConfigObj doesn't create a trailing newline
426
self.check_file_contents(locations,
427
'[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (local_path,))
429
def test_autonick_urlencoded(self):
430
b = self.make_branch('!repo')
431
self.assertEqual('!repo', b.get_config().get_nickname())
433
def test_warn_if_masked(self):
434
_warning = trace.warning
437
warnings.append(args[0] % args[1:])
439
def set_option(store, warn_masked=True):
441
conf.set_user_option('example_option', repr(store), store=store,
442
warn_masked=warn_masked)
443
def assertWarning(warning):
445
self.assertEqual(0, len(warnings))
447
self.assertEqual(1, len(warnings))
448
self.assertEqual(warning, warnings[0])
449
trace.warning = warning
451
branch = self.make_branch('.')
452
conf = branch.get_config()
453
set_option(config.STORE_GLOBAL)
455
set_option(config.STORE_BRANCH)
457
set_option(config.STORE_GLOBAL)
458
assertWarning('Value "4" is masked by "3" from branch.conf')
459
set_option(config.STORE_GLOBAL, warn_masked=False)
461
set_option(config.STORE_LOCATION)
463
set_option(config.STORE_BRANCH)
464
assertWarning('Value "3" is masked by "0" from locations.conf')
465
set_option(config.STORE_BRANCH, warn_masked=False)
468
trace.warning = _warning
471
class TestGlobalConfigItems(TestCase):
473
def test_user_id(self):
474
config_file = StringIO(sample_config_text.encode('utf-8'))
475
my_config = config.GlobalConfig()
476
my_config._parser = my_config._get_parser(file=config_file)
477
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
478
my_config._get_user_id())
480
def test_absent_user_id(self):
481
config_file = StringIO("")
482
my_config = config.GlobalConfig()
483
my_config._parser = my_config._get_parser(file=config_file)
484
self.assertEqual(None, my_config._get_user_id())
486
def test_configured_editor(self):
487
config_file = StringIO(sample_config_text.encode('utf-8'))
488
my_config = config.GlobalConfig()
489
my_config._parser = my_config._get_parser(file=config_file)
490
self.assertEqual("vim", my_config.get_editor())
492
def test_signatures_always(self):
493
config_file = StringIO(sample_always_signatures)
494
my_config = config.GlobalConfig()
495
my_config._parser = my_config._get_parser(file=config_file)
496
self.assertEqual(config.CHECK_NEVER,
497
my_config.signature_checking())
498
self.assertEqual(config.SIGN_ALWAYS,
499
my_config.signing_policy())
500
self.assertEqual(True, my_config.signature_needed())
502
def test_signatures_if_possible(self):
503
config_file = StringIO(sample_maybe_signatures)
504
my_config = config.GlobalConfig()
505
my_config._parser = my_config._get_parser(file=config_file)
506
self.assertEqual(config.CHECK_NEVER,
507
my_config.signature_checking())
508
self.assertEqual(config.SIGN_WHEN_REQUIRED,
509
my_config.signing_policy())
510
self.assertEqual(False, my_config.signature_needed())
512
def test_signatures_ignore(self):
513
config_file = StringIO(sample_ignore_signatures)
514
my_config = config.GlobalConfig()
515
my_config._parser = my_config._get_parser(file=config_file)
516
self.assertEqual(config.CHECK_ALWAYS,
517
my_config.signature_checking())
518
self.assertEqual(config.SIGN_NEVER,
519
my_config.signing_policy())
520
self.assertEqual(False, my_config.signature_needed())
522
def _get_sample_config(self):
523
config_file = StringIO(sample_config_text.encode('utf-8'))
524
my_config = config.GlobalConfig()
525
my_config._parser = my_config._get_parser(file=config_file)
528
def test_gpg_signing_command(self):
529
my_config = self._get_sample_config()
530
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
531
self.assertEqual(False, my_config.signature_needed())
533
def _get_empty_config(self):
534
config_file = StringIO("")
535
my_config = config.GlobalConfig()
536
my_config._parser = my_config._get_parser(file=config_file)
539
def test_gpg_signing_command_unset(self):
540
my_config = self._get_empty_config()
541
self.assertEqual("gpg", my_config.gpg_signing_command())
543
def test_get_user_option_default(self):
544
my_config = self._get_empty_config()
545
self.assertEqual(None, my_config.get_user_option('no_option'))
547
def test_get_user_option_global(self):
548
my_config = self._get_sample_config()
549
self.assertEqual("something",
550
my_config.get_user_option('user_global_option'))
552
def test_post_commit_default(self):
553
my_config = self._get_sample_config()
554
self.assertEqual(None, my_config.post_commit())
556
def test_configured_logformat(self):
557
my_config = self._get_sample_config()
558
self.assertEqual("short", my_config.log_format())
560
def test_get_alias(self):
561
my_config = self._get_sample_config()
562
self.assertEqual('help', my_config.get_alias('h'))
564
def test_get_no_alias(self):
565
my_config = self._get_sample_config()
566
self.assertEqual(None, my_config.get_alias('foo'))
568
def test_get_long_alias(self):
569
my_config = self._get_sample_config()
570
self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
573
class TestLocationConfig(TestCaseInTempDir):
575
def test_constructs(self):
576
my_config = config.LocationConfig('http://example.com')
577
self.assertRaises(TypeError, config.LocationConfig)
579
def test_branch_calls_read_filenames(self):
580
# This is testing the correct file names are provided.
581
# TODO: consolidate with the test for GlobalConfigs filename checks.
583
# replace the class that is constructured, to check its parameters
584
oldparserclass = config.ConfigObj
585
config.ConfigObj = InstrumentedConfigObj
587
my_config = config.LocationConfig('http://www.example.com')
588
parser = my_config._get_parser()
590
config.ConfigObj = oldparserclass
591
self.failUnless(isinstance(parser, InstrumentedConfigObj))
592
self.assertEqual(parser._calls,
593
[('__init__', config.locations_config_filename(),
595
config.ensure_config_dir_exists()
596
#os.mkdir(config.config_dir())
597
f = file(config.branches_config_filename(), 'wb')
600
oldparserclass = config.ConfigObj
601
config.ConfigObj = InstrumentedConfigObj
603
my_config = config.LocationConfig('http://www.example.com')
604
parser = my_config._get_parser()
606
config.ConfigObj = oldparserclass
608
def test_get_global_config(self):
609
my_config = config.BranchConfig(FakeBranch('http://example.com'))
610
global_config = my_config._get_global_config()
611
self.failUnless(isinstance(global_config, config.GlobalConfig))
612
self.failUnless(global_config is my_config._get_global_config())
614
def test__get_matching_sections_no_match(self):
615
self.get_branch_config('/')
616
self.assertEqual([], self.my_location_config._get_matching_sections())
618
def test__get_matching_sections_exact(self):
619
self.get_branch_config('http://www.example.com')
620
self.assertEqual([('http://www.example.com', '')],
621
self.my_location_config._get_matching_sections())
623
def test__get_matching_sections_suffix_does_not(self):
624
self.get_branch_config('http://www.example.com-com')
625
self.assertEqual([], self.my_location_config._get_matching_sections())
627
def test__get_matching_sections_subdir_recursive(self):
628
self.get_branch_config('http://www.example.com/com')
629
self.assertEqual([('http://www.example.com', 'com')],
630
self.my_location_config._get_matching_sections())
632
def test__get_matching_sections_ignoreparent(self):
633
self.get_branch_config('http://www.example.com/ignoreparent')
634
self.assertEqual([('http://www.example.com/ignoreparent', '')],
635
self.my_location_config._get_matching_sections())
637
def test__get_matching_sections_ignoreparent_subdir(self):
638
self.get_branch_config(
639
'http://www.example.com/ignoreparent/childbranch')
640
self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
641
self.my_location_config._get_matching_sections())
643
def test__get_matching_sections_subdir_trailing_slash(self):
644
self.get_branch_config('/b')
645
self.assertEqual([('/b/', '')],
646
self.my_location_config._get_matching_sections())
648
def test__get_matching_sections_subdir_child(self):
649
self.get_branch_config('/a/foo')
650
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
651
self.my_location_config._get_matching_sections())
653
def test__get_matching_sections_subdir_child_child(self):
654
self.get_branch_config('/a/foo/bar')
655
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
656
self.my_location_config._get_matching_sections())
658
def test__get_matching_sections_trailing_slash_with_children(self):
659
self.get_branch_config('/a/')
660
self.assertEqual([('/a/', '')],
661
self.my_location_config._get_matching_sections())
663
def test__get_matching_sections_explicit_over_glob(self):
664
# XXX: 2006-09-08 jamesh
665
# This test only passes because ord('c') > ord('*'). If there
666
# was a config section for '/a/?', it would get precedence
668
self.get_branch_config('/a/c')
669
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
670
self.my_location_config._get_matching_sections())
672
def test__get_option_policy_normal(self):
673
self.get_branch_config('http://www.example.com')
675
self.my_location_config._get_config_policy(
676
'http://www.example.com', 'normal_option'),
679
def test__get_option_policy_norecurse(self):
680
self.get_branch_config('http://www.example.com')
682
self.my_location_config._get_option_policy(
683
'http://www.example.com', 'norecurse_option'),
684
config.POLICY_NORECURSE)
685
# Test old recurse=False setting:
687
self.my_location_config._get_option_policy(
688
'http://www.example.com/norecurse', 'normal_option'),
689
config.POLICY_NORECURSE)
691
def test__get_option_policy_normal(self):
692
self.get_branch_config('http://www.example.com')
694
self.my_location_config._get_option_policy(
695
'http://www.example.com', 'appendpath_option'),
696
config.POLICY_APPENDPATH)
698
def test_location_without_username(self):
699
self.get_branch_config('http://www.example.com/ignoreparent')
700
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
701
self.my_config.username())
703
def test_location_not_listed(self):
704
"""Test that the global username is used when no location matches"""
705
self.get_branch_config('/home/robertc/sources')
706
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
707
self.my_config.username())
709
def test_overriding_location(self):
710
self.get_branch_config('http://www.example.com/foo')
711
self.assertEqual('Robert Collins <robertc@example.org>',
712
self.my_config.username())
714
def test_signatures_not_set(self):
715
self.get_branch_config('http://www.example.com',
716
global_config=sample_ignore_signatures)
717
self.assertEqual(config.CHECK_ALWAYS,
718
self.my_config.signature_checking())
719
self.assertEqual(config.SIGN_NEVER,
720
self.my_config.signing_policy())
722
def test_signatures_never(self):
723
self.get_branch_config('/a/c')
724
self.assertEqual(config.CHECK_NEVER,
725
self.my_config.signature_checking())
727
def test_signatures_when_available(self):
728
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
729
self.assertEqual(config.CHECK_IF_POSSIBLE,
730
self.my_config.signature_checking())
732
def test_signatures_always(self):
733
self.get_branch_config('/b')
734
self.assertEqual(config.CHECK_ALWAYS,
735
self.my_config.signature_checking())
737
def test_gpg_signing_command(self):
738
self.get_branch_config('/b')
739
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
741
def test_gpg_signing_command_missing(self):
742
self.get_branch_config('/a')
743
self.assertEqual("false", self.my_config.gpg_signing_command())
745
def test_get_user_option_global(self):
746
self.get_branch_config('/a')
747
self.assertEqual('something',
748
self.my_config.get_user_option('user_global_option'))
750
def test_get_user_option_local(self):
751
self.get_branch_config('/a')
752
self.assertEqual('local',
753
self.my_config.get_user_option('user_local_option'))
755
def test_get_user_option_appendpath(self):
756
# returned as is for the base path:
757
self.get_branch_config('http://www.example.com')
758
self.assertEqual('append',
759
self.my_config.get_user_option('appendpath_option'))
760
# Extra path components get appended:
761
self.get_branch_config('http://www.example.com/a/b/c')
762
self.assertEqual('append/a/b/c',
763
self.my_config.get_user_option('appendpath_option'))
764
# Overriden for http://www.example.com/dir, where it is a
766
self.get_branch_config('http://www.example.com/dir/a/b/c')
767
self.assertEqual('normal',
768
self.my_config.get_user_option('appendpath_option'))
770
def test_get_user_option_norecurse(self):
771
self.get_branch_config('http://www.example.com')
772
self.assertEqual('norecurse',
773
self.my_config.get_user_option('norecurse_option'))
774
self.get_branch_config('http://www.example.com/dir')
775
self.assertEqual(None,
776
self.my_config.get_user_option('norecurse_option'))
777
# http://www.example.com/norecurse is a recurse=False section
778
# that redefines normal_option. Subdirectories do not pick up
780
self.get_branch_config('http://www.example.com/norecurse')
781
self.assertEqual('norecurse',
782
self.my_config.get_user_option('normal_option'))
783
self.get_branch_config('http://www.example.com/norecurse/subdir')
784
self.assertEqual('normal',
785
self.my_config.get_user_option('normal_option'))
787
def test_set_user_option_norecurse(self):
788
self.get_branch_config('http://www.example.com')
789
self.my_config.set_user_option('foo', 'bar',
790
store=config.STORE_LOCATION_NORECURSE)
792
self.my_location_config._get_option_policy(
793
'http://www.example.com', 'foo'),
794
config.POLICY_NORECURSE)
796
def test_set_user_option_appendpath(self):
797
self.get_branch_config('http://www.example.com')
798
self.my_config.set_user_option('foo', 'bar',
799
store=config.STORE_LOCATION_APPENDPATH)
801
self.my_location_config._get_option_policy(
802
'http://www.example.com', 'foo'),
803
config.POLICY_APPENDPATH)
805
def test_set_user_option_change_policy(self):
806
self.get_branch_config('http://www.example.com')
807
self.my_config.set_user_option('norecurse_option', 'normal',
808
store=config.STORE_LOCATION)
810
self.my_location_config._get_option_policy(
811
'http://www.example.com', 'norecurse_option'),
814
def test_set_user_option_recurse_false_section(self):
815
# The following section has recurse=False set. The test is to
816
# make sure that a normal option can be added to the section,
817
# converting recurse=False to the norecurse policy.
818
self.get_branch_config('http://www.example.com/norecurse')
819
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
820
'The section "http://www.example.com/norecurse" '
821
'has been converted to use policies.'],
822
self.my_config.set_user_option,
823
'foo', 'bar', store=config.STORE_LOCATION)
825
self.my_location_config._get_option_policy(
826
'http://www.example.com/norecurse', 'foo'),
828
# The previously existing option is still norecurse:
830
self.my_location_config._get_option_policy(
831
'http://www.example.com/norecurse', 'normal_option'),
832
config.POLICY_NORECURSE)
834
def test_post_commit_default(self):
835
self.get_branch_config('/a/c')
836
self.assertEqual('bzrlib.tests.test_config.post_commit',
837
self.my_config.post_commit())
839
def get_branch_config(self, location, global_config=None):
840
if global_config is None:
841
global_file = StringIO(sample_config_text.encode('utf-8'))
843
global_file = StringIO(global_config.encode('utf-8'))
844
branches_file = StringIO(sample_branches_text.encode('utf-8'))
845
self.my_config = config.BranchConfig(FakeBranch(location))
846
# Force location config to use specified file
847
self.my_location_config = self.my_config._get_location_config()
848
self.my_location_config._get_parser(branches_file)
849
# Force global config to use specified file
850
self.my_config._get_global_config()._get_parser(global_file)
852
def test_set_user_setting_sets_and_saves(self):
853
self.get_branch_config('/a/c')
854
record = InstrumentedConfigObj("foo")
855
self.my_location_config._parser = record
857
real_mkdir = os.mkdir
859
def checked_mkdir(path, mode=0777):
860
self.log('making directory: %s', path)
861
real_mkdir(path, mode)
864
os.mkdir = checked_mkdir
866
self.callDeprecated(['The recurse option is deprecated as of '
867
'0.14. The section "/a/c" has been '
868
'converted to use policies.'],
869
self.my_config.set_user_option,
870
'foo', 'bar', store=config.STORE_LOCATION)
872
os.mkdir = real_mkdir
874
self.failUnless(self.created, 'Failed to create ~/.bazaar')
875
self.assertEqual([('__contains__', '/a/c'),
876
('__contains__', '/a/c/'),
877
('__setitem__', '/a/c', {}),
878
('__getitem__', '/a/c'),
879
('__setitem__', 'foo', 'bar'),
880
('__getitem__', '/a/c'),
881
('as_bool', 'recurse'),
882
('__getitem__', '/a/c'),
883
('__delitem__', 'recurse'),
884
('__getitem__', '/a/c'),
886
('__getitem__', '/a/c'),
887
('__contains__', 'foo:policy'),
891
def test_set_user_setting_sets_and_saves2(self):
892
self.get_branch_config('/a/c')
893
self.assertIs(self.my_config.get_user_option('foo'), None)
894
self.my_config.set_user_option('foo', 'bar')
896
self.my_config.branch.control_files.files['branch.conf'],
898
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
899
self.my_config.set_user_option('foo', 'baz',
900
store=config.STORE_LOCATION)
901
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
902
self.my_config.set_user_option('foo', 'qux')
903
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
905
def test_get_bzr_remote_path(self):
906
my_config = config.LocationConfig('/a/c')
907
self.assertEqual('bzr', my_config.get_bzr_remote_path())
908
my_config.set_user_option('bzr_remote_path', '/path-bzr')
909
self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
910
os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
911
self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
914
precedence_global = 'option = global'
915
precedence_branch = 'option = branch'
916
precedence_location = """
920
[http://example.com/specific]
925
class TestBranchConfigItems(TestCaseInTempDir):
927
def get_branch_config(self, global_config=None, location=None,
928
location_config=None, branch_data_config=None):
929
my_config = config.BranchConfig(FakeBranch(location))
930
if global_config is not None:
931
global_file = StringIO(global_config.encode('utf-8'))
932
my_config._get_global_config()._get_parser(global_file)
933
self.my_location_config = my_config._get_location_config()
934
if location_config is not None:
935
location_file = StringIO(location_config.encode('utf-8'))
936
self.my_location_config._get_parser(location_file)
937
if branch_data_config is not None:
938
my_config.branch.control_files.files['branch.conf'] = \
942
def test_user_id(self):
943
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
944
my_config = config.BranchConfig(branch)
945
self.assertEqual("Robert Collins <robertc@example.net>",
946
my_config.username())
947
branch.control_files.email = "John"
948
my_config.set_user_option('email',
949
"Robert Collins <robertc@example.org>")
950
self.assertEqual("John", my_config.username())
951
branch.control_files.email = None
952
self.assertEqual("Robert Collins <robertc@example.org>",
953
my_config.username())
955
def test_not_set_in_branch(self):
956
my_config = self.get_branch_config(sample_config_text)
957
my_config.branch.control_files.email = None
958
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
959
my_config._get_user_id())
960
my_config.branch.control_files.email = "John"
961
self.assertEqual("John", my_config._get_user_id())
963
def test_BZR_EMAIL_OVERRIDES(self):
964
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
965
branch = FakeBranch()
966
my_config = config.BranchConfig(branch)
967
self.assertEqual("Robert Collins <robertc@example.org>",
968
my_config.username())
970
def test_signatures_forced(self):
971
my_config = self.get_branch_config(
972
global_config=sample_always_signatures)
973
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
974
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
975
self.assertTrue(my_config.signature_needed())
977
def test_signatures_forced_branch(self):
978
my_config = self.get_branch_config(
979
global_config=sample_ignore_signatures,
980
branch_data_config=sample_always_signatures)
981
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
982
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
983
self.assertTrue(my_config.signature_needed())
985
def test_gpg_signing_command(self):
986
my_config = self.get_branch_config(
987
# branch data cannot set gpg_signing_command
988
branch_data_config="gpg_signing_command=pgp")
989
config_file = StringIO(sample_config_text.encode('utf-8'))
990
my_config._get_global_config()._get_parser(config_file)
991
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
993
def test_get_user_option_global(self):
994
branch = FakeBranch()
995
my_config = config.BranchConfig(branch)
996
config_file = StringIO(sample_config_text.encode('utf-8'))
997
(my_config._get_global_config()._get_parser(config_file))
998
self.assertEqual('something',
999
my_config.get_user_option('user_global_option'))
1001
def test_post_commit_default(self):
1002
branch = FakeBranch()
1003
my_config = self.get_branch_config(sample_config_text, '/a/c',
1004
sample_branches_text)
1005
self.assertEqual(my_config.branch.base, '/a/c')
1006
self.assertEqual('bzrlib.tests.test_config.post_commit',
1007
my_config.post_commit())
1008
my_config.set_user_option('post_commit', 'rmtree_root')
1009
# post-commit is ignored when bresent in branch data
1010
self.assertEqual('bzrlib.tests.test_config.post_commit',
1011
my_config.post_commit())
1012
my_config.set_user_option('post_commit', 'rmtree_root',
1013
store=config.STORE_LOCATION)
1014
self.assertEqual('rmtree_root', my_config.post_commit())
1016
def test_config_precedence(self):
1017
my_config = self.get_branch_config(global_config=precedence_global)
1018
self.assertEqual(my_config.get_user_option('option'), 'global')
1019
my_config = self.get_branch_config(global_config=precedence_global,
1020
branch_data_config=precedence_branch)
1021
self.assertEqual(my_config.get_user_option('option'), 'branch')
1022
my_config = self.get_branch_config(global_config=precedence_global,
1023
branch_data_config=precedence_branch,
1024
location_config=precedence_location)
1025
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1026
my_config = self.get_branch_config(global_config=precedence_global,
1027
branch_data_config=precedence_branch,
1028
location_config=precedence_location,
1029
location='http://example.com/specific')
1030
self.assertEqual(my_config.get_user_option('option'), 'exact')
1032
def test_get_mail_client(self):
1033
config = self.get_branch_config()
1034
client = config.get_mail_client()
1035
self.assertIsInstance(client, mail_client.DefaultMail)
1038
config.set_user_option('mail_client', 'evolution')
1039
client = config.get_mail_client()
1040
self.assertIsInstance(client, mail_client.Evolution)
1042
config.set_user_option('mail_client', 'kmail')
1043
client = config.get_mail_client()
1044
self.assertIsInstance(client, mail_client.KMail)
1046
config.set_user_option('mail_client', 'mutt')
1047
client = config.get_mail_client()
1048
self.assertIsInstance(client, mail_client.Mutt)
1050
config.set_user_option('mail_client', 'thunderbird')
1051
client = config.get_mail_client()
1052
self.assertIsInstance(client, mail_client.Thunderbird)
1055
config.set_user_option('mail_client', 'default')
1056
client = config.get_mail_client()
1057
self.assertIsInstance(client, mail_client.DefaultMail)
1059
config.set_user_option('mail_client', 'editor')
1060
client = config.get_mail_client()
1061
self.assertIsInstance(client, mail_client.Editor)
1063
config.set_user_option('mail_client', 'mapi')
1064
client = config.get_mail_client()
1065
self.assertIsInstance(client, mail_client.MAPIClient)
1067
config.set_user_option('mail_client', 'xdg-email')
1068
client = config.get_mail_client()
1069
self.assertIsInstance(client, mail_client.XDGEmail)
1071
config.set_user_option('mail_client', 'firebird')
1072
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1075
class TestMailAddressExtraction(TestCase):
1077
def test_extract_email_address(self):
1078
self.assertEqual('jane@test.com',
1079
config.extract_email_address('Jane <jane@test.com>'))
1080
self.assertRaises(errors.NoEmailInUsername,
1081
config.extract_email_address, 'Jane Tester')
1084
class TestTreeConfig(TestCaseWithTransport):
1086
def test_get_value(self):
1087
"""Test that retreiving a value from a section is possible"""
1088
branch = self.make_branch('.')
1089
tree_config = config.TreeConfig(branch)
1090
tree_config.set_option('value', 'key', 'SECTION')
1091
tree_config.set_option('value2', 'key2')
1092
tree_config.set_option('value3-top', 'key3')
1093
tree_config.set_option('value3-section', 'key3', 'SECTION')
1094
value = tree_config.get_option('key', 'SECTION')
1095
self.assertEqual(value, 'value')
1096
value = tree_config.get_option('key2')
1097
self.assertEqual(value, 'value2')
1098
self.assertEqual(tree_config.get_option('non-existant'), None)
1099
value = tree_config.get_option('non-existant', 'SECTION')
1100
self.assertEqual(value, None)
1101
value = tree_config.get_option('non-existant', default='default')
1102
self.assertEqual(value, 'default')
1103
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1104
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1105
self.assertEqual(value, 'default')
1106
value = tree_config.get_option('key3')
1107
self.assertEqual(value, 'value3-top')
1108
value = tree_config.get_option('key3', 'SECTION')
1109
self.assertEqual(value, 'value3-section')