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 cStringIO import StringIO
24
#import bzrlib specific imports here
38
from bzrlib.util.configobj import configobj
41
sample_long_alias="log -r-15..-1 --line"
42
sample_config_text = u"""
44
email=Erik B\u00e5gfors <erik@bagfors.nu>
46
gpg_signing_command=gnome-gpg
48
user_global_option=something
51
ll=""" + sample_long_alias + "\n"
54
sample_always_signatures = """
56
check_signatures=ignore
57
create_signatures=always
60
sample_ignore_signatures = """
62
check_signatures=require
63
create_signatures=never
66
sample_maybe_signatures = """
68
check_signatures=ignore
69
create_signatures=when-required
72
sample_branches_text = """
73
[http://www.example.com]
75
email=Robert Collins <robertc@example.org>
76
normal_option = normal
77
appendpath_option = append
78
appendpath_option:policy = appendpath
79
norecurse_option = norecurse
80
norecurse_option:policy = norecurse
81
[http://www.example.com/ignoreparent]
82
# different project: ignore parent dir config
84
[http://www.example.com/norecurse]
85
# configuration items that only apply to this dir
87
normal_option = norecurse
88
[http://www.example.com/dir]
89
appendpath_option = normal
91
check_signatures=require
92
# test trailing / matching with no children
94
check_signatures=check-available
95
gpg_signing_command=false
96
user_local_option=local
97
# test trailing / matching
99
#subdirs will match but not the parent
101
check_signatures=ignore
102
post_commit=bzrlib.tests.test_config.post_commit
103
#testing explicit beats globs
107
class InstrumentedConfigObj(object):
108
"""A config obj look-enough-alike to record calls made to it."""
110
def __contains__(self, thing):
111
self._calls.append(('__contains__', thing))
114
def __getitem__(self, key):
115
self._calls.append(('__getitem__', key))
118
def __init__(self, input, encoding=None):
119
self._calls = [('__init__', input, encoding)]
121
def __setitem__(self, key, value):
122
self._calls.append(('__setitem__', key, value))
124
def __delitem__(self, key):
125
self._calls.append(('__delitem__', key))
128
self._calls.append(('keys',))
131
def write(self, arg):
132
self._calls.append(('write',))
134
def as_bool(self, value):
135
self._calls.append(('as_bool', value))
138
def get_value(self, section, name):
139
self._calls.append(('get_value', section, name))
143
class FakeBranch(object):
145
def __init__(self, base=None, user_id=None):
147
self.base = "http://example.com/branches/demo"
150
self.control_files = FakeControlFiles(user_id=user_id)
152
def lock_write(self):
159
class FakeControlFiles(object):
161
def __init__(self, user_id=None):
164
self.files['email'] = user_id
165
self._transport = self
167
def get_utf8(self, filename):
169
raise AssertionError("get_utf8 should no longer be used")
171
def get(self, filename):
174
return StringIO(self.files[filename])
176
raise errors.NoSuchFile(filename)
178
def get_bytes(self, filename):
181
return self.files[filename]
183
raise errors.NoSuchFile(filename)
185
def put(self, filename, fileobj):
186
self.files[filename] = fileobj.read()
188
def put_file(self, filename, fileobj):
189
return self.put(filename, fileobj)
192
class InstrumentedConfig(config.Config):
193
"""An instrumented config that supplies stubs for template methods."""
196
super(InstrumentedConfig, self).__init__()
198
self._signatures = config.CHECK_NEVER
200
def _get_user_id(self):
201
self._calls.append('_get_user_id')
202
return "Robert Collins <robert.collins@example.org>"
204
def _get_signature_checking(self):
205
self._calls.append('_get_signature_checking')
206
return self._signatures
209
bool_config = """[DEFAULT]
218
class TestConfigObj(tests.TestCase):
220
def test_get_bool(self):
221
co = config.ConfigObj(StringIO(bool_config))
222
self.assertIs(co.get_bool('DEFAULT', 'active'), True)
223
self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
224
self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
225
self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
227
def test_hash_sign_in_value(self):
229
Before 4.5.0, ConfigObj did not quote # signs in values, so they'd be
230
treated as comments when read in again. (#86838)
232
co = config.ConfigObj()
233
co['test'] = 'foo#bar'
235
self.assertEqual(lines, ['test = "foo#bar"'])
236
co2 = config.ConfigObj(lines)
237
self.assertEqual(co2['test'], 'foo#bar')
240
erroneous_config = """[section] # line 1
243
whocares=notme # line 4
247
class TestConfigObjErrors(tests.TestCase):
249
def test_duplicate_section_name_error_line(self):
251
co = configobj.ConfigObj(StringIO(erroneous_config),
253
except config.configobj.DuplicateError, e:
254
self.assertEqual(3, e.line_number)
256
self.fail('Error in config file not detected')
259
class TestConfig(tests.TestCase):
261
def test_constructs(self):
264
def test_no_default_editor(self):
265
self.assertRaises(NotImplementedError, config.Config().get_editor)
267
def test_user_email(self):
268
my_config = InstrumentedConfig()
269
self.assertEqual('robert.collins@example.org', my_config.user_email())
270
self.assertEqual(['_get_user_id'], my_config._calls)
272
def test_username(self):
273
my_config = InstrumentedConfig()
274
self.assertEqual('Robert Collins <robert.collins@example.org>',
275
my_config.username())
276
self.assertEqual(['_get_user_id'], my_config._calls)
278
def test_signatures_default(self):
279
my_config = config.Config()
280
self.assertFalse(my_config.signature_needed())
281
self.assertEqual(config.CHECK_IF_POSSIBLE,
282
my_config.signature_checking())
283
self.assertEqual(config.SIGN_WHEN_REQUIRED,
284
my_config.signing_policy())
286
def test_signatures_template_method(self):
287
my_config = InstrumentedConfig()
288
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
289
self.assertEqual(['_get_signature_checking'], my_config._calls)
291
def test_signatures_template_method_none(self):
292
my_config = InstrumentedConfig()
293
my_config._signatures = None
294
self.assertEqual(config.CHECK_IF_POSSIBLE,
295
my_config.signature_checking())
296
self.assertEqual(['_get_signature_checking'], my_config._calls)
298
def test_gpg_signing_command_default(self):
299
my_config = config.Config()
300
self.assertEqual('gpg', my_config.gpg_signing_command())
302
def test_get_user_option_default(self):
303
my_config = config.Config()
304
self.assertEqual(None, my_config.get_user_option('no_option'))
306
def test_post_commit_default(self):
307
my_config = config.Config()
308
self.assertEqual(None, my_config.post_commit())
310
def test_log_format_default(self):
311
my_config = config.Config()
312
self.assertEqual('long', my_config.log_format())
315
class TestConfigPath(tests.TestCase):
318
super(TestConfigPath, self).setUp()
319
os.environ['HOME'] = '/home/bogus'
320
if sys.platform == 'win32':
321
os.environ['BZR_HOME'] = \
322
r'C:\Documents and Settings\bogus\Application Data'
324
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
326
self.bzr_home = '/home/bogus/.bazaar'
328
def test_config_dir(self):
329
self.assertEqual(config.config_dir(), self.bzr_home)
331
def test_config_filename(self):
332
self.assertEqual(config.config_filename(),
333
self.bzr_home + '/bazaar.conf')
335
def test_branches_config_filename(self):
336
self.assertEqual(config.branches_config_filename(),
337
self.bzr_home + '/branches.conf')
339
def test_locations_config_filename(self):
340
self.assertEqual(config.locations_config_filename(),
341
self.bzr_home + '/locations.conf')
343
def test_authentication_config_filename(self):
344
self.assertEqual(config.authentication_config_filename(),
345
self.bzr_home + '/authentication.conf')
348
class TestIniConfig(tests.TestCase):
350
def test_contructs(self):
351
my_config = config.IniBasedConfig("nothing")
353
def test_from_fp(self):
354
config_file = StringIO(sample_config_text.encode('utf-8'))
355
my_config = config.IniBasedConfig(None)
357
isinstance(my_config._get_parser(file=config_file),
358
configobj.ConfigObj))
360
def test_cached(self):
361
config_file = StringIO(sample_config_text.encode('utf-8'))
362
my_config = config.IniBasedConfig(None)
363
parser = my_config._get_parser(file=config_file)
364
self.failUnless(my_config._get_parser() is parser)
367
class TestGetConfig(tests.TestCase):
369
def test_constructs(self):
370
my_config = config.GlobalConfig()
372
def test_calls_read_filenames(self):
373
# replace the class that is constructed, to check its parameters
374
oldparserclass = config.ConfigObj
375
config.ConfigObj = InstrumentedConfigObj
376
my_config = config.GlobalConfig()
378
parser = my_config._get_parser()
380
config.ConfigObj = oldparserclass
381
self.failUnless(isinstance(parser, InstrumentedConfigObj))
382
self.assertEqual(parser._calls, [('__init__', config.config_filename(),
386
class TestBranchConfig(tests.TestCaseWithTransport):
388
def test_constructs(self):
389
branch = FakeBranch()
390
my_config = config.BranchConfig(branch)
391
self.assertRaises(TypeError, config.BranchConfig)
393
def test_get_location_config(self):
394
branch = FakeBranch()
395
my_config = config.BranchConfig(branch)
396
location_config = my_config._get_location_config()
397
self.assertEqual(branch.base, location_config.location)
398
self.failUnless(location_config is my_config._get_location_config())
400
def test_get_config(self):
401
"""The Branch.get_config method works properly"""
402
b = bzrdir.BzrDir.create_standalone_workingtree('.').branch
403
my_config = b.get_config()
404
self.assertIs(my_config.get_user_option('wacky'), None)
405
my_config.set_user_option('wacky', 'unlikely')
406
self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
408
# Ensure we get the same thing if we start again
409
b2 = branch.Branch.open('.')
410
my_config2 = b2.get_config()
411
self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
413
def test_has_explicit_nickname(self):
414
b = self.make_branch('.')
415
self.assertFalse(b.get_config().has_explicit_nickname())
417
self.assertTrue(b.get_config().has_explicit_nickname())
419
def test_config_url(self):
420
"""The Branch.get_config will use section that uses a local url"""
421
branch = self.make_branch('branch')
422
self.assertEqual('branch', branch.nick)
424
locations = config.locations_config_filename()
425
config.ensure_config_dir_exists()
426
local_url = urlutils.local_path_to_url('branch')
427
open(locations, 'wb').write('[%s]\nnickname = foobar'
429
self.assertEqual('foobar', branch.nick)
431
def test_config_local_path(self):
432
"""The Branch.get_config will use a local system path"""
433
branch = self.make_branch('branch')
434
self.assertEqual('branch', branch.nick)
436
locations = config.locations_config_filename()
437
config.ensure_config_dir_exists()
438
open(locations, 'wb').write('[%s/branch]\nnickname = barry'
439
% (osutils.getcwd().encode('utf8'),))
440
self.assertEqual('barry', branch.nick)
442
def test_config_creates_local(self):
443
"""Creating a new entry in config uses a local path."""
444
branch = self.make_branch('branch', format='knit')
445
branch.set_push_location('http://foobar')
446
locations = config.locations_config_filename()
447
local_path = osutils.getcwd().encode('utf8')
448
# Surprisingly ConfigObj doesn't create a trailing newline
449
self.check_file_contents(locations,
451
'push_location = http://foobar\n'
452
'push_location:policy = norecurse\n'
455
def test_autonick_urlencoded(self):
456
b = self.make_branch('!repo')
457
self.assertEqual('!repo', b.get_config().get_nickname())
459
def test_warn_if_masked(self):
460
_warning = trace.warning
463
warnings.append(args[0] % args[1:])
465
def set_option(store, warn_masked=True):
467
conf.set_user_option('example_option', repr(store), store=store,
468
warn_masked=warn_masked)
469
def assertWarning(warning):
471
self.assertEqual(0, len(warnings))
473
self.assertEqual(1, len(warnings))
474
self.assertEqual(warning, warnings[0])
475
trace.warning = warning
477
branch = self.make_branch('.')
478
conf = branch.get_config()
479
set_option(config.STORE_GLOBAL)
481
set_option(config.STORE_BRANCH)
483
set_option(config.STORE_GLOBAL)
484
assertWarning('Value "4" is masked by "3" from branch.conf')
485
set_option(config.STORE_GLOBAL, warn_masked=False)
487
set_option(config.STORE_LOCATION)
489
set_option(config.STORE_BRANCH)
490
assertWarning('Value "3" is masked by "0" from locations.conf')
491
set_option(config.STORE_BRANCH, warn_masked=False)
494
trace.warning = _warning
497
class TestGlobalConfigItems(tests.TestCase):
499
def test_user_id(self):
500
config_file = StringIO(sample_config_text.encode('utf-8'))
501
my_config = config.GlobalConfig()
502
my_config._parser = my_config._get_parser(file=config_file)
503
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
504
my_config._get_user_id())
506
def test_absent_user_id(self):
507
config_file = StringIO("")
508
my_config = config.GlobalConfig()
509
my_config._parser = my_config._get_parser(file=config_file)
510
self.assertEqual(None, my_config._get_user_id())
512
def test_configured_editor(self):
513
config_file = StringIO(sample_config_text.encode('utf-8'))
514
my_config = config.GlobalConfig()
515
my_config._parser = my_config._get_parser(file=config_file)
516
self.assertEqual("vim", my_config.get_editor())
518
def test_signatures_always(self):
519
config_file = StringIO(sample_always_signatures)
520
my_config = config.GlobalConfig()
521
my_config._parser = my_config._get_parser(file=config_file)
522
self.assertEqual(config.CHECK_NEVER,
523
my_config.signature_checking())
524
self.assertEqual(config.SIGN_ALWAYS,
525
my_config.signing_policy())
526
self.assertEqual(True, my_config.signature_needed())
528
def test_signatures_if_possible(self):
529
config_file = StringIO(sample_maybe_signatures)
530
my_config = config.GlobalConfig()
531
my_config._parser = my_config._get_parser(file=config_file)
532
self.assertEqual(config.CHECK_NEVER,
533
my_config.signature_checking())
534
self.assertEqual(config.SIGN_WHEN_REQUIRED,
535
my_config.signing_policy())
536
self.assertEqual(False, my_config.signature_needed())
538
def test_signatures_ignore(self):
539
config_file = StringIO(sample_ignore_signatures)
540
my_config = config.GlobalConfig()
541
my_config._parser = my_config._get_parser(file=config_file)
542
self.assertEqual(config.CHECK_ALWAYS,
543
my_config.signature_checking())
544
self.assertEqual(config.SIGN_NEVER,
545
my_config.signing_policy())
546
self.assertEqual(False, my_config.signature_needed())
548
def _get_sample_config(self):
549
config_file = StringIO(sample_config_text.encode('utf-8'))
550
my_config = config.GlobalConfig()
551
my_config._parser = my_config._get_parser(file=config_file)
554
def test_gpg_signing_command(self):
555
my_config = self._get_sample_config()
556
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
557
self.assertEqual(False, my_config.signature_needed())
559
def _get_empty_config(self):
560
config_file = StringIO("")
561
my_config = config.GlobalConfig()
562
my_config._parser = my_config._get_parser(file=config_file)
565
def test_gpg_signing_command_unset(self):
566
my_config = self._get_empty_config()
567
self.assertEqual("gpg", my_config.gpg_signing_command())
569
def test_get_user_option_default(self):
570
my_config = self._get_empty_config()
571
self.assertEqual(None, my_config.get_user_option('no_option'))
573
def test_get_user_option_global(self):
574
my_config = self._get_sample_config()
575
self.assertEqual("something",
576
my_config.get_user_option('user_global_option'))
578
def test_post_commit_default(self):
579
my_config = self._get_sample_config()
580
self.assertEqual(None, my_config.post_commit())
582
def test_configured_logformat(self):
583
my_config = self._get_sample_config()
584
self.assertEqual("short", my_config.log_format())
586
def test_get_alias(self):
587
my_config = self._get_sample_config()
588
self.assertEqual('help', my_config.get_alias('h'))
590
def test_get_no_alias(self):
591
my_config = self._get_sample_config()
592
self.assertEqual(None, my_config.get_alias('foo'))
594
def test_get_long_alias(self):
595
my_config = self._get_sample_config()
596
self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
599
class TestLocationConfig(tests.TestCaseInTempDir):
601
def test_constructs(self):
602
my_config = config.LocationConfig('http://example.com')
603
self.assertRaises(TypeError, config.LocationConfig)
605
def test_branch_calls_read_filenames(self):
606
# This is testing the correct file names are provided.
607
# TODO: consolidate with the test for GlobalConfigs filename checks.
609
# replace the class that is constructed, to check its parameters
610
oldparserclass = config.ConfigObj
611
config.ConfigObj = InstrumentedConfigObj
613
my_config = config.LocationConfig('http://www.example.com')
614
parser = my_config._get_parser()
616
config.ConfigObj = oldparserclass
617
self.failUnless(isinstance(parser, InstrumentedConfigObj))
618
self.assertEqual(parser._calls,
619
[('__init__', config.locations_config_filename(),
621
config.ensure_config_dir_exists()
622
#os.mkdir(config.config_dir())
623
f = file(config.branches_config_filename(), 'wb')
626
oldparserclass = config.ConfigObj
627
config.ConfigObj = InstrumentedConfigObj
629
my_config = config.LocationConfig('http://www.example.com')
630
parser = my_config._get_parser()
632
config.ConfigObj = oldparserclass
634
def test_get_global_config(self):
635
my_config = config.BranchConfig(FakeBranch('http://example.com'))
636
global_config = my_config._get_global_config()
637
self.failUnless(isinstance(global_config, config.GlobalConfig))
638
self.failUnless(global_config is my_config._get_global_config())
640
def test__get_matching_sections_no_match(self):
641
self.get_branch_config('/')
642
self.assertEqual([], self.my_location_config._get_matching_sections())
644
def test__get_matching_sections_exact(self):
645
self.get_branch_config('http://www.example.com')
646
self.assertEqual([('http://www.example.com', '')],
647
self.my_location_config._get_matching_sections())
649
def test__get_matching_sections_suffix_does_not(self):
650
self.get_branch_config('http://www.example.com-com')
651
self.assertEqual([], self.my_location_config._get_matching_sections())
653
def test__get_matching_sections_subdir_recursive(self):
654
self.get_branch_config('http://www.example.com/com')
655
self.assertEqual([('http://www.example.com', 'com')],
656
self.my_location_config._get_matching_sections())
658
def test__get_matching_sections_ignoreparent(self):
659
self.get_branch_config('http://www.example.com/ignoreparent')
660
self.assertEqual([('http://www.example.com/ignoreparent', '')],
661
self.my_location_config._get_matching_sections())
663
def test__get_matching_sections_ignoreparent_subdir(self):
664
self.get_branch_config(
665
'http://www.example.com/ignoreparent/childbranch')
666
self.assertEqual([('http://www.example.com/ignoreparent',
668
self.my_location_config._get_matching_sections())
670
def test__get_matching_sections_subdir_trailing_slash(self):
671
self.get_branch_config('/b')
672
self.assertEqual([('/b/', '')],
673
self.my_location_config._get_matching_sections())
675
def test__get_matching_sections_subdir_child(self):
676
self.get_branch_config('/a/foo')
677
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
678
self.my_location_config._get_matching_sections())
680
def test__get_matching_sections_subdir_child_child(self):
681
self.get_branch_config('/a/foo/bar')
682
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
683
self.my_location_config._get_matching_sections())
685
def test__get_matching_sections_trailing_slash_with_children(self):
686
self.get_branch_config('/a/')
687
self.assertEqual([('/a/', '')],
688
self.my_location_config._get_matching_sections())
690
def test__get_matching_sections_explicit_over_glob(self):
691
# XXX: 2006-09-08 jamesh
692
# This test only passes because ord('c') > ord('*'). If there
693
# was a config section for '/a/?', it would get precedence
695
self.get_branch_config('/a/c')
696
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
697
self.my_location_config._get_matching_sections())
699
def test__get_option_policy_normal(self):
700
self.get_branch_config('http://www.example.com')
702
self.my_location_config._get_config_policy(
703
'http://www.example.com', 'normal_option'),
706
def test__get_option_policy_norecurse(self):
707
self.get_branch_config('http://www.example.com')
709
self.my_location_config._get_option_policy(
710
'http://www.example.com', 'norecurse_option'),
711
config.POLICY_NORECURSE)
712
# Test old recurse=False setting:
714
self.my_location_config._get_option_policy(
715
'http://www.example.com/norecurse', 'normal_option'),
716
config.POLICY_NORECURSE)
718
def test__get_option_policy_normal(self):
719
self.get_branch_config('http://www.example.com')
721
self.my_location_config._get_option_policy(
722
'http://www.example.com', 'appendpath_option'),
723
config.POLICY_APPENDPATH)
725
def test_location_without_username(self):
726
self.get_branch_config('http://www.example.com/ignoreparent')
727
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
728
self.my_config.username())
730
def test_location_not_listed(self):
731
"""Test that the global username is used when no location matches"""
732
self.get_branch_config('/home/robertc/sources')
733
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
734
self.my_config.username())
736
def test_overriding_location(self):
737
self.get_branch_config('http://www.example.com/foo')
738
self.assertEqual('Robert Collins <robertc@example.org>',
739
self.my_config.username())
741
def test_signatures_not_set(self):
742
self.get_branch_config('http://www.example.com',
743
global_config=sample_ignore_signatures)
744
self.assertEqual(config.CHECK_ALWAYS,
745
self.my_config.signature_checking())
746
self.assertEqual(config.SIGN_NEVER,
747
self.my_config.signing_policy())
749
def test_signatures_never(self):
750
self.get_branch_config('/a/c')
751
self.assertEqual(config.CHECK_NEVER,
752
self.my_config.signature_checking())
754
def test_signatures_when_available(self):
755
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
756
self.assertEqual(config.CHECK_IF_POSSIBLE,
757
self.my_config.signature_checking())
759
def test_signatures_always(self):
760
self.get_branch_config('/b')
761
self.assertEqual(config.CHECK_ALWAYS,
762
self.my_config.signature_checking())
764
def test_gpg_signing_command(self):
765
self.get_branch_config('/b')
766
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
768
def test_gpg_signing_command_missing(self):
769
self.get_branch_config('/a')
770
self.assertEqual("false", self.my_config.gpg_signing_command())
772
def test_get_user_option_global(self):
773
self.get_branch_config('/a')
774
self.assertEqual('something',
775
self.my_config.get_user_option('user_global_option'))
777
def test_get_user_option_local(self):
778
self.get_branch_config('/a')
779
self.assertEqual('local',
780
self.my_config.get_user_option('user_local_option'))
782
def test_get_user_option_appendpath(self):
783
# returned as is for the base path:
784
self.get_branch_config('http://www.example.com')
785
self.assertEqual('append',
786
self.my_config.get_user_option('appendpath_option'))
787
# Extra path components get appended:
788
self.get_branch_config('http://www.example.com/a/b/c')
789
self.assertEqual('append/a/b/c',
790
self.my_config.get_user_option('appendpath_option'))
791
# Overriden for http://www.example.com/dir, where it is a
793
self.get_branch_config('http://www.example.com/dir/a/b/c')
794
self.assertEqual('normal',
795
self.my_config.get_user_option('appendpath_option'))
797
def test_get_user_option_norecurse(self):
798
self.get_branch_config('http://www.example.com')
799
self.assertEqual('norecurse',
800
self.my_config.get_user_option('norecurse_option'))
801
self.get_branch_config('http://www.example.com/dir')
802
self.assertEqual(None,
803
self.my_config.get_user_option('norecurse_option'))
804
# http://www.example.com/norecurse is a recurse=False section
805
# that redefines normal_option. Subdirectories do not pick up
807
self.get_branch_config('http://www.example.com/norecurse')
808
self.assertEqual('norecurse',
809
self.my_config.get_user_option('normal_option'))
810
self.get_branch_config('http://www.example.com/norecurse/subdir')
811
self.assertEqual('normal',
812
self.my_config.get_user_option('normal_option'))
814
def test_set_user_option_norecurse(self):
815
self.get_branch_config('http://www.example.com')
816
self.my_config.set_user_option('foo', 'bar',
817
store=config.STORE_LOCATION_NORECURSE)
819
self.my_location_config._get_option_policy(
820
'http://www.example.com', 'foo'),
821
config.POLICY_NORECURSE)
823
def test_set_user_option_appendpath(self):
824
self.get_branch_config('http://www.example.com')
825
self.my_config.set_user_option('foo', 'bar',
826
store=config.STORE_LOCATION_APPENDPATH)
828
self.my_location_config._get_option_policy(
829
'http://www.example.com', 'foo'),
830
config.POLICY_APPENDPATH)
832
def test_set_user_option_change_policy(self):
833
self.get_branch_config('http://www.example.com')
834
self.my_config.set_user_option('norecurse_option', 'normal',
835
store=config.STORE_LOCATION)
837
self.my_location_config._get_option_policy(
838
'http://www.example.com', 'norecurse_option'),
841
def test_set_user_option_recurse_false_section(self):
842
# The following section has recurse=False set. The test is to
843
# make sure that a normal option can be added to the section,
844
# converting recurse=False to the norecurse policy.
845
self.get_branch_config('http://www.example.com/norecurse')
846
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
847
'The section "http://www.example.com/norecurse" '
848
'has been converted to use policies.'],
849
self.my_config.set_user_option,
850
'foo', 'bar', store=config.STORE_LOCATION)
852
self.my_location_config._get_option_policy(
853
'http://www.example.com/norecurse', 'foo'),
855
# The previously existing option is still norecurse:
857
self.my_location_config._get_option_policy(
858
'http://www.example.com/norecurse', 'normal_option'),
859
config.POLICY_NORECURSE)
861
def test_post_commit_default(self):
862
self.get_branch_config('/a/c')
863
self.assertEqual('bzrlib.tests.test_config.post_commit',
864
self.my_config.post_commit())
866
def get_branch_config(self, location, global_config=None):
867
if global_config is None:
868
global_file = StringIO(sample_config_text.encode('utf-8'))
870
global_file = StringIO(global_config.encode('utf-8'))
871
branches_file = StringIO(sample_branches_text.encode('utf-8'))
872
self.my_config = config.BranchConfig(FakeBranch(location))
873
# Force location config to use specified file
874
self.my_location_config = self.my_config._get_location_config()
875
self.my_location_config._get_parser(branches_file)
876
# Force global config to use specified file
877
self.my_config._get_global_config()._get_parser(global_file)
879
def test_set_user_setting_sets_and_saves(self):
880
self.get_branch_config('/a/c')
881
record = InstrumentedConfigObj("foo")
882
self.my_location_config._parser = record
884
real_mkdir = os.mkdir
886
def checked_mkdir(path, mode=0777):
887
self.log('making directory: %s', path)
888
real_mkdir(path, mode)
891
os.mkdir = checked_mkdir
893
self.callDeprecated(['The recurse option is deprecated as of '
894
'0.14. The section "/a/c" has been '
895
'converted to use policies.'],
896
self.my_config.set_user_option,
897
'foo', 'bar', store=config.STORE_LOCATION)
899
os.mkdir = real_mkdir
901
self.failUnless(self.created, 'Failed to create ~/.bazaar')
902
self.assertEqual([('__contains__', '/a/c'),
903
('__contains__', '/a/c/'),
904
('__setitem__', '/a/c', {}),
905
('__getitem__', '/a/c'),
906
('__setitem__', 'foo', 'bar'),
907
('__getitem__', '/a/c'),
908
('as_bool', 'recurse'),
909
('__getitem__', '/a/c'),
910
('__delitem__', 'recurse'),
911
('__getitem__', '/a/c'),
913
('__getitem__', '/a/c'),
914
('__contains__', 'foo:policy'),
918
def test_set_user_setting_sets_and_saves2(self):
919
self.get_branch_config('/a/c')
920
self.assertIs(self.my_config.get_user_option('foo'), None)
921
self.my_config.set_user_option('foo', 'bar')
923
self.my_config.branch.control_files.files['branch.conf'],
925
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
926
self.my_config.set_user_option('foo', 'baz',
927
store=config.STORE_LOCATION)
928
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
929
self.my_config.set_user_option('foo', 'qux')
930
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
932
def test_get_bzr_remote_path(self):
933
my_config = config.LocationConfig('/a/c')
934
self.assertEqual('bzr', my_config.get_bzr_remote_path())
935
my_config.set_user_option('bzr_remote_path', '/path-bzr')
936
self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
937
os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
938
self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
941
precedence_global = 'option = global'
942
precedence_branch = 'option = branch'
943
precedence_location = """
947
[http://example.com/specific]
952
class TestBranchConfigItems(tests.TestCaseInTempDir):
954
def get_branch_config(self, global_config=None, location=None,
955
location_config=None, branch_data_config=None):
956
my_config = config.BranchConfig(FakeBranch(location))
957
if global_config is not None:
958
global_file = StringIO(global_config.encode('utf-8'))
959
my_config._get_global_config()._get_parser(global_file)
960
self.my_location_config = my_config._get_location_config()
961
if location_config is not None:
962
location_file = StringIO(location_config.encode('utf-8'))
963
self.my_location_config._get_parser(location_file)
964
if branch_data_config is not None:
965
my_config.branch.control_files.files['branch.conf'] = \
969
def test_user_id(self):
970
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
971
my_config = config.BranchConfig(branch)
972
self.assertEqual("Robert Collins <robertc@example.net>",
973
my_config.username())
974
my_config.branch.control_files.files['email'] = "John"
975
my_config.set_user_option('email',
976
"Robert Collins <robertc@example.org>")
977
self.assertEqual("John", my_config.username())
978
del my_config.branch.control_files.files['email']
979
self.assertEqual("Robert Collins <robertc@example.org>",
980
my_config.username())
982
def test_not_set_in_branch(self):
983
my_config = self.get_branch_config(sample_config_text)
984
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
985
my_config._get_user_id())
986
my_config.branch.control_files.files['email'] = "John"
987
self.assertEqual("John", my_config._get_user_id())
989
def test_BZR_EMAIL_OVERRIDES(self):
990
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
991
branch = FakeBranch()
992
my_config = config.BranchConfig(branch)
993
self.assertEqual("Robert Collins <robertc@example.org>",
994
my_config.username())
996
def test_signatures_forced(self):
997
my_config = self.get_branch_config(
998
global_config=sample_always_signatures)
999
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1000
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1001
self.assertTrue(my_config.signature_needed())
1003
def test_signatures_forced_branch(self):
1004
my_config = self.get_branch_config(
1005
global_config=sample_ignore_signatures,
1006
branch_data_config=sample_always_signatures)
1007
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1008
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1009
self.assertTrue(my_config.signature_needed())
1011
def test_gpg_signing_command(self):
1012
my_config = self.get_branch_config(
1013
# branch data cannot set gpg_signing_command
1014
branch_data_config="gpg_signing_command=pgp")
1015
config_file = StringIO(sample_config_text.encode('utf-8'))
1016
my_config._get_global_config()._get_parser(config_file)
1017
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
1019
def test_get_user_option_global(self):
1020
branch = FakeBranch()
1021
my_config = config.BranchConfig(branch)
1022
config_file = StringIO(sample_config_text.encode('utf-8'))
1023
(my_config._get_global_config()._get_parser(config_file))
1024
self.assertEqual('something',
1025
my_config.get_user_option('user_global_option'))
1027
def test_post_commit_default(self):
1028
branch = FakeBranch()
1029
my_config = self.get_branch_config(sample_config_text, '/a/c',
1030
sample_branches_text)
1031
self.assertEqual(my_config.branch.base, '/a/c')
1032
self.assertEqual('bzrlib.tests.test_config.post_commit',
1033
my_config.post_commit())
1034
my_config.set_user_option('post_commit', 'rmtree_root')
1035
# post-commit is ignored when bresent in branch data
1036
self.assertEqual('bzrlib.tests.test_config.post_commit',
1037
my_config.post_commit())
1038
my_config.set_user_option('post_commit', 'rmtree_root',
1039
store=config.STORE_LOCATION)
1040
self.assertEqual('rmtree_root', my_config.post_commit())
1042
def test_config_precedence(self):
1043
my_config = self.get_branch_config(global_config=precedence_global)
1044
self.assertEqual(my_config.get_user_option('option'), 'global')
1045
my_config = self.get_branch_config(global_config=precedence_global,
1046
branch_data_config=precedence_branch)
1047
self.assertEqual(my_config.get_user_option('option'), 'branch')
1048
my_config = self.get_branch_config(global_config=precedence_global,
1049
branch_data_config=precedence_branch,
1050
location_config=precedence_location)
1051
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1052
my_config = self.get_branch_config(global_config=precedence_global,
1053
branch_data_config=precedence_branch,
1054
location_config=precedence_location,
1055
location='http://example.com/specific')
1056
self.assertEqual(my_config.get_user_option('option'), 'exact')
1058
def test_get_mail_client(self):
1059
config = self.get_branch_config()
1060
client = config.get_mail_client()
1061
self.assertIsInstance(client, mail_client.DefaultMail)
1064
config.set_user_option('mail_client', 'evolution')
1065
client = config.get_mail_client()
1066
self.assertIsInstance(client, mail_client.Evolution)
1068
config.set_user_option('mail_client', 'kmail')
1069
client = config.get_mail_client()
1070
self.assertIsInstance(client, mail_client.KMail)
1072
config.set_user_option('mail_client', 'mutt')
1073
client = config.get_mail_client()
1074
self.assertIsInstance(client, mail_client.Mutt)
1076
config.set_user_option('mail_client', 'thunderbird')
1077
client = config.get_mail_client()
1078
self.assertIsInstance(client, mail_client.Thunderbird)
1081
config.set_user_option('mail_client', 'default')
1082
client = config.get_mail_client()
1083
self.assertIsInstance(client, mail_client.DefaultMail)
1085
config.set_user_option('mail_client', 'editor')
1086
client = config.get_mail_client()
1087
self.assertIsInstance(client, mail_client.Editor)
1089
config.set_user_option('mail_client', 'mapi')
1090
client = config.get_mail_client()
1091
self.assertIsInstance(client, mail_client.MAPIClient)
1093
config.set_user_option('mail_client', 'xdg-email')
1094
client = config.get_mail_client()
1095
self.assertIsInstance(client, mail_client.XDGEmail)
1097
config.set_user_option('mail_client', 'firebird')
1098
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1101
class TestMailAddressExtraction(tests.TestCase):
1103
def test_extract_email_address(self):
1104
self.assertEqual('jane@test.com',
1105
config.extract_email_address('Jane <jane@test.com>'))
1106
self.assertRaises(errors.NoEmailInUsername,
1107
config.extract_email_address, 'Jane Tester')
1109
def test_parse_username(self):
1110
self.assertEqual(('', 'jdoe@example.com'),
1111
config.parse_username('jdoe@example.com'))
1112
self.assertEqual(('', 'jdoe@example.com'),
1113
config.parse_username('<jdoe@example.com>'))
1114
self.assertEqual(('John Doe', 'jdoe@example.com'),
1115
config.parse_username('John Doe <jdoe@example.com>'))
1116
self.assertEqual(('John Doe', ''),
1117
config.parse_username('John Doe'))
1118
self.assertEqual(('John Doe', 'jdoe@example.com'),
1119
config.parse_username('John Doe jdoe@example.com'))
1121
class TestTreeConfig(tests.TestCaseWithTransport):
1123
def test_get_value(self):
1124
"""Test that retreiving a value from a section is possible"""
1125
branch = self.make_branch('.')
1126
tree_config = config.TreeConfig(branch)
1127
tree_config.set_option('value', 'key', 'SECTION')
1128
tree_config.set_option('value2', 'key2')
1129
tree_config.set_option('value3-top', 'key3')
1130
tree_config.set_option('value3-section', 'key3', 'SECTION')
1131
value = tree_config.get_option('key', 'SECTION')
1132
self.assertEqual(value, 'value')
1133
value = tree_config.get_option('key2')
1134
self.assertEqual(value, 'value2')
1135
self.assertEqual(tree_config.get_option('non-existant'), None)
1136
value = tree_config.get_option('non-existant', 'SECTION')
1137
self.assertEqual(value, None)
1138
value = tree_config.get_option('non-existant', default='default')
1139
self.assertEqual(value, 'default')
1140
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1141
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1142
self.assertEqual(value, 'default')
1143
value = tree_config.get_option('key3')
1144
self.assertEqual(value, 'value3-top')
1145
value = tree_config.get_option('key3', 'SECTION')
1146
self.assertEqual(value, 'value3-section')
1149
class TestTransportConfig(tests.TestCaseWithTransport):
1151
def test_get_value(self):
1152
"""Test that retreiving a value from a section is possible"""
1153
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1155
bzrdir_config.set_option('value', 'key', 'SECTION')
1156
bzrdir_config.set_option('value2', 'key2')
1157
bzrdir_config.set_option('value3-top', 'key3')
1158
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1159
value = bzrdir_config.get_option('key', 'SECTION')
1160
self.assertEqual(value, 'value')
1161
value = bzrdir_config.get_option('key2')
1162
self.assertEqual(value, 'value2')
1163
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1164
value = bzrdir_config.get_option('non-existant', 'SECTION')
1165
self.assertEqual(value, None)
1166
value = bzrdir_config.get_option('non-existant', default='default')
1167
self.assertEqual(value, 'default')
1168
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1169
value = bzrdir_config.get_option('key2', 'NOSECTION',
1171
self.assertEqual(value, 'default')
1172
value = bzrdir_config.get_option('key3')
1173
self.assertEqual(value, 'value3-top')
1174
value = bzrdir_config.get_option('key3', 'SECTION')
1175
self.assertEqual(value, 'value3-section')
1178
class TestAuthenticationConfigFile(tests.TestCase):
1179
"""Test the authentication.conf file matching"""
1181
def _got_user_passwd(self, expected_user, expected_password,
1182
config, *args, **kwargs):
1183
credentials = config.get_credentials(*args, **kwargs)
1184
if credentials is None:
1188
user = credentials['user']
1189
password = credentials['password']
1190
self.assertEquals(expected_user, user)
1191
self.assertEquals(expected_password, password)
1193
def test_empty_config(self):
1194
conf = config.AuthenticationConfig(_file=StringIO())
1195
self.assertEquals({}, conf._get_config())
1196
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1198
def test_broken_config(self):
1199
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1200
self.assertRaises(errors.ParseConfigError, conf._get_config)
1202
conf = config.AuthenticationConfig(_file=StringIO(
1206
verify_certificates=askme # Error: Not a boolean
1208
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1209
conf = config.AuthenticationConfig(_file=StringIO(
1213
port=port # Error: Not an int
1215
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1217
def test_credentials_for_scheme_host(self):
1218
conf = config.AuthenticationConfig(_file=StringIO(
1219
"""# Identity on foo.net
1224
password=secret-pass
1227
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1229
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1231
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1233
def test_credentials_for_host_port(self):
1234
conf = config.AuthenticationConfig(_file=StringIO(
1235
"""# Identity on foo.net
1241
password=secret-pass
1244
self._got_user_passwd('joe', 'secret-pass',
1245
conf, 'ftp', 'foo.net', port=10021)
1247
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1249
def test_for_matching_host(self):
1250
conf = config.AuthenticationConfig(_file=StringIO(
1251
"""# Identity on foo.net
1257
[sourceforge domain]
1264
self._got_user_passwd('georges', 'bendover',
1265
conf, 'bzr', 'foo.bzr.sf.net')
1267
self._got_user_passwd(None, None,
1268
conf, 'bzr', 'bbzr.sf.net')
1270
def test_for_matching_host_None(self):
1271
conf = config.AuthenticationConfig(_file=StringIO(
1272
"""# Identity on foo.net
1282
self._got_user_passwd('joe', 'joepass',
1283
conf, 'bzr', 'quux.net')
1284
# no host but different scheme
1285
self._got_user_passwd('georges', 'bendover',
1286
conf, 'ftp', 'quux.net')
1288
def test_credentials_for_path(self):
1289
conf = config.AuthenticationConfig(_file=StringIO(
1305
self._got_user_passwd(None, None,
1306
conf, 'http', host='bar.org', path='/dir3')
1308
self._got_user_passwd('georges', 'bendover',
1309
conf, 'http', host='bar.org', path='/dir2')
1311
self._got_user_passwd('jim', 'jimpass',
1312
conf, 'http', host='bar.org',path='/dir1/subdir')
1314
def test_credentials_for_user(self):
1315
conf = config.AuthenticationConfig(_file=StringIO(
1324
self._got_user_passwd('jim', 'jimpass',
1325
conf, 'http', 'bar.org')
1327
self._got_user_passwd('jim', 'jimpass',
1328
conf, 'http', 'bar.org', user='jim')
1329
# Don't get a different user if one is specified
1330
self._got_user_passwd(None, None,
1331
conf, 'http', 'bar.org', user='georges')
1333
def test_verify_certificates(self):
1334
conf = config.AuthenticationConfig(_file=StringIO(
1341
verify_certificates=False
1348
credentials = conf.get_credentials('https', 'bar.org')
1349
self.assertEquals(False, credentials.get('verify_certificates'))
1350
credentials = conf.get_credentials('https', 'foo.net')
1351
self.assertEquals(True, credentials.get('verify_certificates'))
1354
class TestAuthenticationConfig(tests.TestCase):
1355
"""Test AuthenticationConfig behaviour"""
1357
def _check_default_prompt(self, expected_prompt_format, scheme,
1358
host=None, port=None, realm=None, path=None):
1361
user, password = 'jim', 'precious'
1362
expected_prompt = expected_prompt_format % {
1363
'scheme': scheme, 'host': host, 'port': port,
1364
'user': user, 'realm': realm}
1366
stdout = tests.StringIOWrapper()
1367
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1369
# We use an empty conf so that the user is always prompted
1370
conf = config.AuthenticationConfig()
1371
self.assertEquals(password,
1372
conf.get_password(scheme, host, user, port=port,
1373
realm=realm, path=path))
1374
self.assertEquals(stdout.getvalue(), expected_prompt)
1376
def test_default_prompts(self):
1377
# HTTP prompts can't be tested here, see test_http.py
1378
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1379
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1382
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1384
# SMTP port handling is a bit special (it's handled if embedded in the
1386
# FIXME: should we: forbid that, extend it to other schemes, leave
1387
# things as they are that's fine thank you ?
1388
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1390
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1391
'smtp', host='bar.org:10025')
1392
self._check_default_prompt(
1393
'SMTP %(user)s@%(host)s:%(port)d password: ',
1397
# FIXME: Once we have a way to declare authentication to all test servers, we
1398
# can implement generic tests.
1399
# test_user_password_in_url
1400
# test_user_in_url_password_from_config
1401
# test_user_in_url_password_prompted
1402
# test_user_in_config
1403
# test_user_getpass.getuser
1404
# test_user_prompted ?
1405
class TestAuthenticationRing(tests.TestCaseWithTransport):