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
class TestConfig(TestCase):
216
def test_constructs(self):
219
def test_no_default_editor(self):
220
self.assertRaises(NotImplementedError, config.Config().get_editor)
222
def test_user_email(self):
223
my_config = InstrumentedConfig()
224
self.assertEqual('robert.collins@example.org', my_config.user_email())
225
self.assertEqual(['_get_user_id'], my_config._calls)
227
def test_username(self):
228
my_config = InstrumentedConfig()
229
self.assertEqual('Robert Collins <robert.collins@example.org>',
230
my_config.username())
231
self.assertEqual(['_get_user_id'], my_config._calls)
233
def test_signatures_default(self):
234
my_config = config.Config()
235
self.assertFalse(my_config.signature_needed())
236
self.assertEqual(config.CHECK_IF_POSSIBLE,
237
my_config.signature_checking())
238
self.assertEqual(config.SIGN_WHEN_REQUIRED,
239
my_config.signing_policy())
241
def test_signatures_template_method(self):
242
my_config = InstrumentedConfig()
243
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
244
self.assertEqual(['_get_signature_checking'], my_config._calls)
246
def test_signatures_template_method_none(self):
247
my_config = InstrumentedConfig()
248
my_config._signatures = None
249
self.assertEqual(config.CHECK_IF_POSSIBLE,
250
my_config.signature_checking())
251
self.assertEqual(['_get_signature_checking'], my_config._calls)
253
def test_gpg_signing_command_default(self):
254
my_config = config.Config()
255
self.assertEqual('gpg', my_config.gpg_signing_command())
257
def test_get_user_option_default(self):
258
my_config = config.Config()
259
self.assertEqual(None, my_config.get_user_option('no_option'))
261
def test_post_commit_default(self):
262
my_config = config.Config()
263
self.assertEqual(None, my_config.post_commit())
265
def test_log_format_default(self):
266
my_config = config.Config()
267
self.assertEqual('long', my_config.log_format())
270
class TestConfigPath(TestCase):
273
super(TestConfigPath, self).setUp()
274
os.environ['HOME'] = '/home/bogus'
275
if sys.platform == 'win32':
276
os.environ['BZR_HOME'] = \
277
r'C:\Documents and Settings\bogus\Application Data'
279
def test_config_dir(self):
280
if sys.platform == 'win32':
281
self.assertEqual(config.config_dir(),
282
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
284
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
286
def test_config_filename(self):
287
if sys.platform == 'win32':
288
self.assertEqual(config.config_filename(),
289
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
291
self.assertEqual(config.config_filename(),
292
'/home/bogus/.bazaar/bazaar.conf')
294
def test_branches_config_filename(self):
295
if sys.platform == 'win32':
296
self.assertEqual(config.branches_config_filename(),
297
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
299
self.assertEqual(config.branches_config_filename(),
300
'/home/bogus/.bazaar/branches.conf')
302
def test_locations_config_filename(self):
303
if sys.platform == 'win32':
304
self.assertEqual(config.locations_config_filename(),
305
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
307
self.assertEqual(config.locations_config_filename(),
308
'/home/bogus/.bazaar/locations.conf')
310
class TestIniConfig(TestCase):
312
def test_contructs(self):
313
my_config = config.IniBasedConfig("nothing")
315
def test_from_fp(self):
316
config_file = StringIO(sample_config_text.encode('utf-8'))
317
my_config = config.IniBasedConfig(None)
319
isinstance(my_config._get_parser(file=config_file),
322
def test_cached(self):
323
config_file = StringIO(sample_config_text.encode('utf-8'))
324
my_config = config.IniBasedConfig(None)
325
parser = my_config._get_parser(file=config_file)
326
self.failUnless(my_config._get_parser() is parser)
329
class TestGetConfig(TestCase):
331
def test_constructs(self):
332
my_config = config.GlobalConfig()
334
def test_calls_read_filenames(self):
335
# replace the class that is constructured, to check its parameters
336
oldparserclass = config.ConfigObj
337
config.ConfigObj = InstrumentedConfigObj
338
my_config = config.GlobalConfig()
340
parser = my_config._get_parser()
342
config.ConfigObj = oldparserclass
343
self.failUnless(isinstance(parser, InstrumentedConfigObj))
344
self.assertEqual(parser._calls, [('__init__', config.config_filename(),
348
class TestBranchConfig(TestCaseWithTransport):
350
def test_constructs(self):
351
branch = FakeBranch()
352
my_config = config.BranchConfig(branch)
353
self.assertRaises(TypeError, config.BranchConfig)
355
def test_get_location_config(self):
356
branch = FakeBranch()
357
my_config = config.BranchConfig(branch)
358
location_config = my_config._get_location_config()
359
self.assertEqual(branch.base, location_config.location)
360
self.failUnless(location_config is my_config._get_location_config())
362
def test_get_config(self):
363
"""The Branch.get_config method works properly"""
364
b = BzrDir.create_standalone_workingtree('.').branch
365
my_config = b.get_config()
366
self.assertIs(my_config.get_user_option('wacky'), None)
367
my_config.set_user_option('wacky', 'unlikely')
368
self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
370
# Ensure we get the same thing if we start again
371
b2 = Branch.open('.')
372
my_config2 = b2.get_config()
373
self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
375
def test_has_explicit_nickname(self):
376
b = self.make_branch('.')
377
self.assertFalse(b.get_config().has_explicit_nickname())
379
self.assertTrue(b.get_config().has_explicit_nickname())
381
def test_config_url(self):
382
"""The Branch.get_config will use section that uses a local url"""
383
branch = self.make_branch('branch')
384
self.assertEqual('branch', branch.nick)
386
locations = config.locations_config_filename()
387
config.ensure_config_dir_exists()
388
local_url = urlutils.local_path_to_url('branch')
389
open(locations, 'wb').write('[%s]\nnickname = foobar'
391
self.assertEqual('foobar', branch.nick)
393
def test_config_local_path(self):
394
"""The Branch.get_config will use a local system path"""
395
branch = self.make_branch('branch')
396
self.assertEqual('branch', branch.nick)
398
locations = config.locations_config_filename()
399
config.ensure_config_dir_exists()
400
open(locations, 'wb').write('[%s/branch]\nnickname = barry'
401
% (osutils.getcwd().encode('utf8'),))
402
self.assertEqual('barry', branch.nick)
404
def test_config_creates_local(self):
405
"""Creating a new entry in config uses a local path."""
406
branch = self.make_branch('branch', format='knit')
407
branch.set_push_location('http://foobar')
408
locations = config.locations_config_filename()
409
local_path = osutils.getcwd().encode('utf8')
410
# Surprisingly ConfigObj doesn't create a trailing newline
411
self.check_file_contents(locations,
412
'[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (local_path,))
414
def test_autonick_urlencoded(self):
415
b = self.make_branch('!repo')
416
self.assertEqual('!repo', b.get_config().get_nickname())
418
def test_warn_if_masked(self):
419
_warning = trace.warning
422
warnings.append(args[0] % args[1:])
424
def set_option(store, warn_masked=True):
426
conf.set_user_option('example_option', repr(store), store=store,
427
warn_masked=warn_masked)
428
def assertWarning(warning):
430
self.assertEqual(0, len(warnings))
432
self.assertEqual(1, len(warnings))
433
self.assertEqual(warning, warnings[0])
434
trace.warning = warning
436
branch = self.make_branch('.')
437
conf = branch.get_config()
438
set_option(config.STORE_GLOBAL)
440
set_option(config.STORE_BRANCH)
442
set_option(config.STORE_GLOBAL)
443
assertWarning('Value "4" is masked by "3" from branch.conf')
444
set_option(config.STORE_GLOBAL, warn_masked=False)
446
set_option(config.STORE_LOCATION)
448
set_option(config.STORE_BRANCH)
449
assertWarning('Value "3" is masked by "0" from locations.conf')
450
set_option(config.STORE_BRANCH, warn_masked=False)
453
trace.warning = _warning
456
class TestGlobalConfigItems(TestCase):
458
def test_user_id(self):
459
config_file = StringIO(sample_config_text.encode('utf-8'))
460
my_config = config.GlobalConfig()
461
my_config._parser = my_config._get_parser(file=config_file)
462
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
463
my_config._get_user_id())
465
def test_absent_user_id(self):
466
config_file = StringIO("")
467
my_config = config.GlobalConfig()
468
my_config._parser = my_config._get_parser(file=config_file)
469
self.assertEqual(None, my_config._get_user_id())
471
def test_configured_editor(self):
472
config_file = StringIO(sample_config_text.encode('utf-8'))
473
my_config = config.GlobalConfig()
474
my_config._parser = my_config._get_parser(file=config_file)
475
self.assertEqual("vim", my_config.get_editor())
477
def test_signatures_always(self):
478
config_file = StringIO(sample_always_signatures)
479
my_config = config.GlobalConfig()
480
my_config._parser = my_config._get_parser(file=config_file)
481
self.assertEqual(config.CHECK_NEVER,
482
my_config.signature_checking())
483
self.assertEqual(config.SIGN_ALWAYS,
484
my_config.signing_policy())
485
self.assertEqual(True, my_config.signature_needed())
487
def test_signatures_if_possible(self):
488
config_file = StringIO(sample_maybe_signatures)
489
my_config = config.GlobalConfig()
490
my_config._parser = my_config._get_parser(file=config_file)
491
self.assertEqual(config.CHECK_NEVER,
492
my_config.signature_checking())
493
self.assertEqual(config.SIGN_WHEN_REQUIRED,
494
my_config.signing_policy())
495
self.assertEqual(False, my_config.signature_needed())
497
def test_signatures_ignore(self):
498
config_file = StringIO(sample_ignore_signatures)
499
my_config = config.GlobalConfig()
500
my_config._parser = my_config._get_parser(file=config_file)
501
self.assertEqual(config.CHECK_ALWAYS,
502
my_config.signature_checking())
503
self.assertEqual(config.SIGN_NEVER,
504
my_config.signing_policy())
505
self.assertEqual(False, my_config.signature_needed())
507
def _get_sample_config(self):
508
config_file = StringIO(sample_config_text.encode('utf-8'))
509
my_config = config.GlobalConfig()
510
my_config._parser = my_config._get_parser(file=config_file)
513
def test_gpg_signing_command(self):
514
my_config = self._get_sample_config()
515
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
516
self.assertEqual(False, my_config.signature_needed())
518
def _get_empty_config(self):
519
config_file = StringIO("")
520
my_config = config.GlobalConfig()
521
my_config._parser = my_config._get_parser(file=config_file)
524
def test_gpg_signing_command_unset(self):
525
my_config = self._get_empty_config()
526
self.assertEqual("gpg", my_config.gpg_signing_command())
528
def test_get_user_option_default(self):
529
my_config = self._get_empty_config()
530
self.assertEqual(None, my_config.get_user_option('no_option'))
532
def test_get_user_option_global(self):
533
my_config = self._get_sample_config()
534
self.assertEqual("something",
535
my_config.get_user_option('user_global_option'))
537
def test_post_commit_default(self):
538
my_config = self._get_sample_config()
539
self.assertEqual(None, my_config.post_commit())
541
def test_configured_logformat(self):
542
my_config = self._get_sample_config()
543
self.assertEqual("short", my_config.log_format())
545
def test_get_alias(self):
546
my_config = self._get_sample_config()
547
self.assertEqual('help', my_config.get_alias('h'))
549
def test_get_no_alias(self):
550
my_config = self._get_sample_config()
551
self.assertEqual(None, my_config.get_alias('foo'))
553
def test_get_long_alias(self):
554
my_config = self._get_sample_config()
555
self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
558
class TestLocationConfig(TestCaseInTempDir):
560
def test_constructs(self):
561
my_config = config.LocationConfig('http://example.com')
562
self.assertRaises(TypeError, config.LocationConfig)
564
def test_branch_calls_read_filenames(self):
565
# This is testing the correct file names are provided.
566
# TODO: consolidate with the test for GlobalConfigs filename checks.
568
# replace the class that is constructured, to check its parameters
569
oldparserclass = config.ConfigObj
570
config.ConfigObj = InstrumentedConfigObj
572
my_config = config.LocationConfig('http://www.example.com')
573
parser = my_config._get_parser()
575
config.ConfigObj = oldparserclass
576
self.failUnless(isinstance(parser, InstrumentedConfigObj))
577
self.assertEqual(parser._calls,
578
[('__init__', config.locations_config_filename(),
580
config.ensure_config_dir_exists()
581
#os.mkdir(config.config_dir())
582
f = file(config.branches_config_filename(), 'wb')
585
oldparserclass = config.ConfigObj
586
config.ConfigObj = InstrumentedConfigObj
588
my_config = config.LocationConfig('http://www.example.com')
589
parser = my_config._get_parser()
591
config.ConfigObj = oldparserclass
593
def test_get_global_config(self):
594
my_config = config.BranchConfig(FakeBranch('http://example.com'))
595
global_config = my_config._get_global_config()
596
self.failUnless(isinstance(global_config, config.GlobalConfig))
597
self.failUnless(global_config is my_config._get_global_config())
599
def test__get_matching_sections_no_match(self):
600
self.get_branch_config('/')
601
self.assertEqual([], self.my_location_config._get_matching_sections())
603
def test__get_matching_sections_exact(self):
604
self.get_branch_config('http://www.example.com')
605
self.assertEqual([('http://www.example.com', '')],
606
self.my_location_config._get_matching_sections())
608
def test__get_matching_sections_suffix_does_not(self):
609
self.get_branch_config('http://www.example.com-com')
610
self.assertEqual([], self.my_location_config._get_matching_sections())
612
def test__get_matching_sections_subdir_recursive(self):
613
self.get_branch_config('http://www.example.com/com')
614
self.assertEqual([('http://www.example.com', 'com')],
615
self.my_location_config._get_matching_sections())
617
def test__get_matching_sections_ignoreparent(self):
618
self.get_branch_config('http://www.example.com/ignoreparent')
619
self.assertEqual([('http://www.example.com/ignoreparent', '')],
620
self.my_location_config._get_matching_sections())
622
def test__get_matching_sections_ignoreparent_subdir(self):
623
self.get_branch_config(
624
'http://www.example.com/ignoreparent/childbranch')
625
self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
626
self.my_location_config._get_matching_sections())
628
def test__get_matching_sections_subdir_trailing_slash(self):
629
self.get_branch_config('/b')
630
self.assertEqual([('/b/', '')],
631
self.my_location_config._get_matching_sections())
633
def test__get_matching_sections_subdir_child(self):
634
self.get_branch_config('/a/foo')
635
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
636
self.my_location_config._get_matching_sections())
638
def test__get_matching_sections_subdir_child_child(self):
639
self.get_branch_config('/a/foo/bar')
640
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
641
self.my_location_config._get_matching_sections())
643
def test__get_matching_sections_trailing_slash_with_children(self):
644
self.get_branch_config('/a/')
645
self.assertEqual([('/a/', '')],
646
self.my_location_config._get_matching_sections())
648
def test__get_matching_sections_explicit_over_glob(self):
649
# XXX: 2006-09-08 jamesh
650
# This test only passes because ord('c') > ord('*'). If there
651
# was a config section for '/a/?', it would get precedence
653
self.get_branch_config('/a/c')
654
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
655
self.my_location_config._get_matching_sections())
657
def test__get_option_policy_normal(self):
658
self.get_branch_config('http://www.example.com')
660
self.my_location_config._get_config_policy(
661
'http://www.example.com', 'normal_option'),
664
def test__get_option_policy_norecurse(self):
665
self.get_branch_config('http://www.example.com')
667
self.my_location_config._get_option_policy(
668
'http://www.example.com', 'norecurse_option'),
669
config.POLICY_NORECURSE)
670
# Test old recurse=False setting:
672
self.my_location_config._get_option_policy(
673
'http://www.example.com/norecurse', 'normal_option'),
674
config.POLICY_NORECURSE)
676
def test__get_option_policy_normal(self):
677
self.get_branch_config('http://www.example.com')
679
self.my_location_config._get_option_policy(
680
'http://www.example.com', 'appendpath_option'),
681
config.POLICY_APPENDPATH)
683
def test_location_without_username(self):
684
self.get_branch_config('http://www.example.com/ignoreparent')
685
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
686
self.my_config.username())
688
def test_location_not_listed(self):
689
"""Test that the global username is used when no location matches"""
690
self.get_branch_config('/home/robertc/sources')
691
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
692
self.my_config.username())
694
def test_overriding_location(self):
695
self.get_branch_config('http://www.example.com/foo')
696
self.assertEqual('Robert Collins <robertc@example.org>',
697
self.my_config.username())
699
def test_signatures_not_set(self):
700
self.get_branch_config('http://www.example.com',
701
global_config=sample_ignore_signatures)
702
self.assertEqual(config.CHECK_ALWAYS,
703
self.my_config.signature_checking())
704
self.assertEqual(config.SIGN_NEVER,
705
self.my_config.signing_policy())
707
def test_signatures_never(self):
708
self.get_branch_config('/a/c')
709
self.assertEqual(config.CHECK_NEVER,
710
self.my_config.signature_checking())
712
def test_signatures_when_available(self):
713
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
714
self.assertEqual(config.CHECK_IF_POSSIBLE,
715
self.my_config.signature_checking())
717
def test_signatures_always(self):
718
self.get_branch_config('/b')
719
self.assertEqual(config.CHECK_ALWAYS,
720
self.my_config.signature_checking())
722
def test_gpg_signing_command(self):
723
self.get_branch_config('/b')
724
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
726
def test_gpg_signing_command_missing(self):
727
self.get_branch_config('/a')
728
self.assertEqual("false", self.my_config.gpg_signing_command())
730
def test_get_user_option_global(self):
731
self.get_branch_config('/a')
732
self.assertEqual('something',
733
self.my_config.get_user_option('user_global_option'))
735
def test_get_user_option_local(self):
736
self.get_branch_config('/a')
737
self.assertEqual('local',
738
self.my_config.get_user_option('user_local_option'))
740
def test_get_user_option_appendpath(self):
741
# returned as is for the base path:
742
self.get_branch_config('http://www.example.com')
743
self.assertEqual('append',
744
self.my_config.get_user_option('appendpath_option'))
745
# Extra path components get appended:
746
self.get_branch_config('http://www.example.com/a/b/c')
747
self.assertEqual('append/a/b/c',
748
self.my_config.get_user_option('appendpath_option'))
749
# Overriden for http://www.example.com/dir, where it is a
751
self.get_branch_config('http://www.example.com/dir/a/b/c')
752
self.assertEqual('normal',
753
self.my_config.get_user_option('appendpath_option'))
755
def test_get_user_option_norecurse(self):
756
self.get_branch_config('http://www.example.com')
757
self.assertEqual('norecurse',
758
self.my_config.get_user_option('norecurse_option'))
759
self.get_branch_config('http://www.example.com/dir')
760
self.assertEqual(None,
761
self.my_config.get_user_option('norecurse_option'))
762
# http://www.example.com/norecurse is a recurse=False section
763
# that redefines normal_option. Subdirectories do not pick up
765
self.get_branch_config('http://www.example.com/norecurse')
766
self.assertEqual('norecurse',
767
self.my_config.get_user_option('normal_option'))
768
self.get_branch_config('http://www.example.com/norecurse/subdir')
769
self.assertEqual('normal',
770
self.my_config.get_user_option('normal_option'))
772
def test_set_user_option_norecurse(self):
773
self.get_branch_config('http://www.example.com')
774
self.my_config.set_user_option('foo', 'bar',
775
store=config.STORE_LOCATION_NORECURSE)
777
self.my_location_config._get_option_policy(
778
'http://www.example.com', 'foo'),
779
config.POLICY_NORECURSE)
781
def test_set_user_option_appendpath(self):
782
self.get_branch_config('http://www.example.com')
783
self.my_config.set_user_option('foo', 'bar',
784
store=config.STORE_LOCATION_APPENDPATH)
786
self.my_location_config._get_option_policy(
787
'http://www.example.com', 'foo'),
788
config.POLICY_APPENDPATH)
790
def test_set_user_option_change_policy(self):
791
self.get_branch_config('http://www.example.com')
792
self.my_config.set_user_option('norecurse_option', 'normal',
793
store=config.STORE_LOCATION)
795
self.my_location_config._get_option_policy(
796
'http://www.example.com', 'norecurse_option'),
799
def test_set_user_option_recurse_false_section(self):
800
# The following section has recurse=False set. The test is to
801
# make sure that a normal option can be added to the section,
802
# converting recurse=False to the norecurse policy.
803
self.get_branch_config('http://www.example.com/norecurse')
804
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
805
'The section "http://www.example.com/norecurse" '
806
'has been converted to use policies.'],
807
self.my_config.set_user_option,
808
'foo', 'bar', store=config.STORE_LOCATION)
810
self.my_location_config._get_option_policy(
811
'http://www.example.com/norecurse', 'foo'),
813
# The previously existing option is still norecurse:
815
self.my_location_config._get_option_policy(
816
'http://www.example.com/norecurse', 'normal_option'),
817
config.POLICY_NORECURSE)
819
def test_post_commit_default(self):
820
self.get_branch_config('/a/c')
821
self.assertEqual('bzrlib.tests.test_config.post_commit',
822
self.my_config.post_commit())
824
def get_branch_config(self, location, global_config=None):
825
if global_config is None:
826
global_file = StringIO(sample_config_text.encode('utf-8'))
828
global_file = StringIO(global_config.encode('utf-8'))
829
branches_file = StringIO(sample_branches_text.encode('utf-8'))
830
self.my_config = config.BranchConfig(FakeBranch(location))
831
# Force location config to use specified file
832
self.my_location_config = self.my_config._get_location_config()
833
self.my_location_config._get_parser(branches_file)
834
# Force global config to use specified file
835
self.my_config._get_global_config()._get_parser(global_file)
837
def test_set_user_setting_sets_and_saves(self):
838
self.get_branch_config('/a/c')
839
record = InstrumentedConfigObj("foo")
840
self.my_location_config._parser = record
842
real_mkdir = os.mkdir
844
def checked_mkdir(path, mode=0777):
845
self.log('making directory: %s', path)
846
real_mkdir(path, mode)
849
os.mkdir = checked_mkdir
851
self.callDeprecated(['The recurse option is deprecated as of '
852
'0.14. The section "/a/c" has been '
853
'converted to use policies.'],
854
self.my_config.set_user_option,
855
'foo', 'bar', store=config.STORE_LOCATION)
857
os.mkdir = real_mkdir
859
self.failUnless(self.created, 'Failed to create ~/.bazaar')
860
self.assertEqual([('__contains__', '/a/c'),
861
('__contains__', '/a/c/'),
862
('__setitem__', '/a/c', {}),
863
('__getitem__', '/a/c'),
864
('__setitem__', 'foo', 'bar'),
865
('__getitem__', '/a/c'),
866
('as_bool', 'recurse'),
867
('__getitem__', '/a/c'),
868
('__delitem__', 'recurse'),
869
('__getitem__', '/a/c'),
871
('__getitem__', '/a/c'),
872
('__contains__', 'foo:policy'),
876
def test_set_user_setting_sets_and_saves2(self):
877
self.get_branch_config('/a/c')
878
self.assertIs(self.my_config.get_user_option('foo'), None)
879
self.my_config.set_user_option('foo', 'bar')
881
self.my_config.branch.control_files.files['branch.conf'],
883
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
884
self.my_config.set_user_option('foo', 'baz',
885
store=config.STORE_LOCATION)
886
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
887
self.my_config.set_user_option('foo', 'qux')
888
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
891
precedence_global = 'option = global'
892
precedence_branch = 'option = branch'
893
precedence_location = """
897
[http://example.com/specific]
902
class TestBranchConfigItems(TestCaseInTempDir):
904
def get_branch_config(self, global_config=None, location=None,
905
location_config=None, branch_data_config=None):
906
my_config = config.BranchConfig(FakeBranch(location))
907
if global_config is not None:
908
global_file = StringIO(global_config.encode('utf-8'))
909
my_config._get_global_config()._get_parser(global_file)
910
self.my_location_config = my_config._get_location_config()
911
if location_config is not None:
912
location_file = StringIO(location_config.encode('utf-8'))
913
self.my_location_config._get_parser(location_file)
914
if branch_data_config is not None:
915
my_config.branch.control_files.files['branch.conf'] = \
919
def test_user_id(self):
920
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
921
my_config = config.BranchConfig(branch)
922
self.assertEqual("Robert Collins <robertc@example.net>",
923
my_config.username())
924
branch.control_files.email = "John"
925
my_config.set_user_option('email',
926
"Robert Collins <robertc@example.org>")
927
self.assertEqual("John", my_config.username())
928
branch.control_files.email = None
929
self.assertEqual("Robert Collins <robertc@example.org>",
930
my_config.username())
932
def test_not_set_in_branch(self):
933
my_config = self.get_branch_config(sample_config_text)
934
my_config.branch.control_files.email = None
935
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
936
my_config._get_user_id())
937
my_config.branch.control_files.email = "John"
938
self.assertEqual("John", my_config._get_user_id())
940
def test_BZR_EMAIL_OVERRIDES(self):
941
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
942
branch = FakeBranch()
943
my_config = config.BranchConfig(branch)
944
self.assertEqual("Robert Collins <robertc@example.org>",
945
my_config.username())
947
def test_signatures_forced(self):
948
my_config = self.get_branch_config(
949
global_config=sample_always_signatures)
950
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
951
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
952
self.assertTrue(my_config.signature_needed())
954
def test_signatures_forced_branch(self):
955
my_config = self.get_branch_config(
956
global_config=sample_ignore_signatures,
957
branch_data_config=sample_always_signatures)
958
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
959
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
960
self.assertTrue(my_config.signature_needed())
962
def test_gpg_signing_command(self):
963
my_config = self.get_branch_config(
964
# branch data cannot set gpg_signing_command
965
branch_data_config="gpg_signing_command=pgp")
966
config_file = StringIO(sample_config_text.encode('utf-8'))
967
my_config._get_global_config()._get_parser(config_file)
968
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
970
def test_get_user_option_global(self):
971
branch = FakeBranch()
972
my_config = config.BranchConfig(branch)
973
config_file = StringIO(sample_config_text.encode('utf-8'))
974
(my_config._get_global_config()._get_parser(config_file))
975
self.assertEqual('something',
976
my_config.get_user_option('user_global_option'))
978
def test_post_commit_default(self):
979
branch = FakeBranch()
980
my_config = self.get_branch_config(sample_config_text, '/a/c',
981
sample_branches_text)
982
self.assertEqual(my_config.branch.base, '/a/c')
983
self.assertEqual('bzrlib.tests.test_config.post_commit',
984
my_config.post_commit())
985
my_config.set_user_option('post_commit', 'rmtree_root')
986
# post-commit is ignored when bresent in branch data
987
self.assertEqual('bzrlib.tests.test_config.post_commit',
988
my_config.post_commit())
989
my_config.set_user_option('post_commit', 'rmtree_root',
990
store=config.STORE_LOCATION)
991
self.assertEqual('rmtree_root', my_config.post_commit())
993
def test_config_precedence(self):
994
my_config = self.get_branch_config(global_config=precedence_global)
995
self.assertEqual(my_config.get_user_option('option'), 'global')
996
my_config = self.get_branch_config(global_config=precedence_global,
997
branch_data_config=precedence_branch)
998
self.assertEqual(my_config.get_user_option('option'), 'branch')
999
my_config = self.get_branch_config(global_config=precedence_global,
1000
branch_data_config=precedence_branch,
1001
location_config=precedence_location)
1002
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1003
my_config = self.get_branch_config(global_config=precedence_global,
1004
branch_data_config=precedence_branch,
1005
location_config=precedence_location,
1006
location='http://example.com/specific')
1007
self.assertEqual(my_config.get_user_option('option'), 'exact')
1009
def test_get_mail_client(self):
1010
config = self.get_branch_config()
1011
client = config.get_mail_client()
1012
self.assertIsInstance(client, mail_client.DefaultMail)
1015
config.set_user_option('mail_client', 'evolution')
1016
client = config.get_mail_client()
1017
self.assertIsInstance(client, mail_client.Evolution)
1019
config.set_user_option('mail_client', 'kmail')
1020
client = config.get_mail_client()
1021
self.assertIsInstance(client, mail_client.KMail)
1023
config.set_user_option('mail_client', 'mutt')
1024
client = config.get_mail_client()
1025
self.assertIsInstance(client, mail_client.Mutt)
1027
config.set_user_option('mail_client', 'thunderbird')
1028
client = config.get_mail_client()
1029
self.assertIsInstance(client, mail_client.Thunderbird)
1032
config.set_user_option('mail_client', 'default')
1033
client = config.get_mail_client()
1034
self.assertIsInstance(client, mail_client.DefaultMail)
1036
config.set_user_option('mail_client', 'editor')
1037
client = config.get_mail_client()
1038
self.assertIsInstance(client, mail_client.Editor)
1040
config.set_user_option('mail_client', 'mapi')
1041
client = config.get_mail_client()
1042
self.assertIsInstance(client, mail_client.MAPIClient)
1044
config.set_user_option('mail_client', 'xdg-email')
1045
client = config.get_mail_client()
1046
self.assertIsInstance(client, mail_client.XDGEmail)
1048
config.set_user_option('mail_client', 'firebird')
1049
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1052
class TestMailAddressExtraction(TestCase):
1054
def test_extract_email_address(self):
1055
self.assertEqual('jane@test.com',
1056
config.extract_email_address('Jane <jane@test.com>'))
1057
self.assertRaises(errors.NoEmailInUsername,
1058
config.extract_email_address, 'Jane Tester')
1061
class TestTreeConfig(TestCaseWithTransport):
1063
def test_get_value(self):
1064
"""Test that retreiving a value from a section is possible"""
1065
branch = self.make_branch('.')
1066
tree_config = config.TreeConfig(branch)
1067
tree_config.set_option('value', 'key', 'SECTION')
1068
tree_config.set_option('value2', 'key2')
1069
tree_config.set_option('value3-top', 'key3')
1070
tree_config.set_option('value3-section', 'key3', 'SECTION')
1071
value = tree_config.get_option('key', 'SECTION')
1072
self.assertEqual(value, 'value')
1073
value = tree_config.get_option('key2')
1074
self.assertEqual(value, 'value2')
1075
self.assertEqual(tree_config.get_option('non-existant'), None)
1076
value = tree_config.get_option('non-existant', 'SECTION')
1077
self.assertEqual(value, None)
1078
value = tree_config.get_option('non-existant', default='default')
1079
self.assertEqual(value, 'default')
1080
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1081
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1082
self.assertEqual(value, 'default')
1083
value = tree_config.get_option('key3')
1084
self.assertEqual(value, 'value3-top')
1085
value = tree_config.get_option('key3', 'SECTION')
1086
self.assertEqual(value, 'value3-section')