1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2005 by Canonical Ltd
3
# Authors: Robert Collins <robert.collins@canonical.com>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Tests for finding and reading the bzr config file[s]."""
20
# import system imports here
21
from bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
22
from cStringIO import StringIO
26
#import bzrlib specific imports here
27
import bzrlib.config as config
28
import bzrlib.errors as errors
29
from bzrlib.tests import TestCase, TestCaseInTempDir
32
sample_long_alias="log -r-15..-1 --line"
33
sample_config_text = ("[DEFAULT]\n"
34
u"email=Erik Bågfors <erik@bagfors.nu>\n"
36
"gpg_signing_command=gnome-gpg\n"
38
"user_global_option=something\n"
41
"ll=" + sample_long_alias + "\n")
44
sample_always_signatures = ("[DEFAULT]\n"
45
"check_signatures=require\n")
48
sample_ignore_signatures = ("[DEFAULT]\n"
49
"check_signatures=ignore\n")
52
sample_maybe_signatures = ("[DEFAULT]\n"
53
"check_signatures=check-available\n")
56
sample_branches_text = ("[http://www.example.com]\n"
57
"# Top level policy\n"
58
"email=Robert Collins <robertc@example.org>\n"
59
"[http://www.example.com/useglobal]\n"
60
"# different project, forces global lookup\n"
63
"check_signatures=require\n"
64
"# test trailing / matching with no children\n"
66
"check_signatures=check-available\n"
67
"gpg_signing_command=false\n"
68
"user_local_option=local\n"
69
"# test trailing / matching\n"
71
"#subdirs will match but not the parent\n"
74
"check_signatures=ignore\n"
75
"post_commit=bzrlib.tests.test_config.post_commit\n"
76
"#testing explicit beats globs\n")
80
class InstrumentedConfigObj(object):
81
"""A config obj look-enough-alike to record calls made to it."""
83
def __contains__(self, thing):
84
self._calls.append(('__contains__', thing))
87
def __getitem__(self, key):
88
self._calls.append(('__getitem__', key))
91
def __init__(self, input, encoding=None):
92
self._calls = [('__init__', input, encoding)]
94
def __setitem__(self, key, value):
95
self._calls.append(('__setitem__', key, value))
98
self._calls.append(('write',))
101
class FakeBranch(object):
104
self.base = "http://example.com/branches/demo"
105
self.control_files = FakeControlFiles()
108
class FakeControlFiles(object):
111
self.email = 'Robert Collins <robertc@example.net>\n'
113
def get_utf8(self, filename):
114
if filename != 'email':
115
raise NotImplementedError
116
if self.email is not None:
117
return StringIO(self.email)
118
raise errors.NoSuchFile(filename)
121
class InstrumentedConfig(config.Config):
122
"""An instrumented config that supplies stubs for template methods."""
125
super(InstrumentedConfig, self).__init__()
127
self._signatures = config.CHECK_NEVER
129
def _get_user_id(self):
130
self._calls.append('_get_user_id')
131
return "Robert Collins <robert.collins@example.org>"
133
def _get_signature_checking(self):
134
self._calls.append('_get_signature_checking')
135
return self._signatures
138
bool_config = """[DEFAULT]
145
class TestConfigObj(TestCase):
146
def test_get_bool(self):
147
from bzrlib.config import ConfigObj
148
co = ConfigObj(StringIO(bool_config))
149
self.assertIs(co.get_bool('DEFAULT', 'active'), True)
150
self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
151
self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
152
self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
155
class TestConfig(TestCase):
157
def test_constructs(self):
160
def test_no_default_editor(self):
161
self.assertRaises(NotImplementedError, config.Config().get_editor)
163
def test_user_email(self):
164
my_config = InstrumentedConfig()
165
self.assertEqual('robert.collins@example.org', my_config.user_email())
166
self.assertEqual(['_get_user_id'], my_config._calls)
168
def test_username(self):
169
my_config = InstrumentedConfig()
170
self.assertEqual('Robert Collins <robert.collins@example.org>',
171
my_config.username())
172
self.assertEqual(['_get_user_id'], my_config._calls)
174
def test_signatures_default(self):
175
my_config = config.Config()
176
self.assertEqual(config.CHECK_IF_POSSIBLE,
177
my_config.signature_checking())
179
def test_signatures_template_method(self):
180
my_config = InstrumentedConfig()
181
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
182
self.assertEqual(['_get_signature_checking'], my_config._calls)
184
def test_signatures_template_method_none(self):
185
my_config = InstrumentedConfig()
186
my_config._signatures = None
187
self.assertEqual(config.CHECK_IF_POSSIBLE,
188
my_config.signature_checking())
189
self.assertEqual(['_get_signature_checking'], my_config._calls)
191
def test_gpg_signing_command_default(self):
192
my_config = config.Config()
193
self.assertEqual('gpg', my_config.gpg_signing_command())
195
def test_get_user_option_default(self):
196
my_config = config.Config()
197
self.assertEqual(None, my_config.get_user_option('no_option'))
199
def test_post_commit_default(self):
200
my_config = config.Config()
201
self.assertEqual(None, my_config.post_commit())
203
def test_log_format_default(self):
204
my_config = config.Config()
205
self.assertEqual('long', my_config.log_format())
208
class TestConfigPath(TestCase):
211
super(TestConfigPath, self).setUp()
212
self.old_home = os.environ.get('HOME', None)
213
self.old_appdata = os.environ.get('APPDATA', None)
214
os.environ['HOME'] = '/home/bogus'
215
os.environ['APPDATA'] = \
216
r'C:\Documents and Settings\bogus\Application Data'
219
if self.old_home is None:
220
del os.environ['HOME']
222
os.environ['HOME'] = self.old_home
223
if self.old_appdata is None:
224
del os.environ['APPDATA']
226
os.environ['APPDATA'] = self.old_appdata
227
super(TestConfigPath, self).tearDown()
229
def test_config_dir(self):
230
if sys.platform == 'win32':
231
self.assertEqual(config.config_dir(),
232
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
234
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
236
def test_config_filename(self):
237
if sys.platform == 'win32':
238
self.assertEqual(config.config_filename(),
239
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
241
self.assertEqual(config.config_filename(),
242
'/home/bogus/.bazaar/bazaar.conf')
244
def test_branches_config_filename(self):
245
if sys.platform == 'win32':
246
self.assertEqual(config.branches_config_filename(),
247
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
249
self.assertEqual(config.branches_config_filename(),
250
'/home/bogus/.bazaar/branches.conf')
252
class TestIniConfig(TestCase):
254
def test_contructs(self):
255
my_config = config.IniBasedConfig("nothing")
257
def test_from_fp(self):
258
config_file = StringIO(sample_config_text.encode('utf-8'))
259
my_config = config.IniBasedConfig(None)
261
isinstance(my_config._get_parser(file=config_file),
264
def test_cached(self):
265
config_file = StringIO(sample_config_text.encode('utf-8'))
266
my_config = config.IniBasedConfig(None)
267
parser = my_config._get_parser(file=config_file)
268
self.failUnless(my_config._get_parser() is parser)
271
class TestGetConfig(TestCase):
273
def test_constructs(self):
274
my_config = config.GlobalConfig()
276
def test_calls_read_filenames(self):
277
# replace the class that is constructured, to check its parameters
278
oldparserclass = config.ConfigObj
279
config.ConfigObj = InstrumentedConfigObj
280
my_config = config.GlobalConfig()
282
parser = my_config._get_parser()
284
config.ConfigObj = oldparserclass
285
self.failUnless(isinstance(parser, InstrumentedConfigObj))
286
self.assertEqual(parser._calls, [('__init__', config.config_filename(),
290
class TestBranchConfig(TestCaseInTempDir):
292
def test_constructs(self):
293
branch = FakeBranch()
294
my_config = config.BranchConfig(branch)
295
self.assertRaises(TypeError, config.BranchConfig)
297
def test_get_location_config(self):
298
branch = FakeBranch()
299
my_config = config.BranchConfig(branch)
300
location_config = my_config._get_location_config()
301
self.assertEqual(branch.base, location_config.location)
302
self.failUnless(location_config is my_config._get_location_config())
305
class TestGlobalConfigItems(TestCase):
307
def test_user_id(self):
308
config_file = StringIO(sample_config_text.encode('utf-8'))
309
my_config = config.GlobalConfig()
310
my_config._parser = my_config._get_parser(file=config_file)
311
self.assertEqual(u"Erik Bågfors <erik@bagfors.nu>",
312
my_config._get_user_id())
314
def test_absent_user_id(self):
315
config_file = StringIO("")
316
my_config = config.GlobalConfig()
317
my_config._parser = my_config._get_parser(file=config_file)
318
self.assertEqual(None, my_config._get_user_id())
320
def test_configured_editor(self):
321
config_file = StringIO(sample_config_text.encode('utf-8'))
322
my_config = config.GlobalConfig()
323
my_config._parser = my_config._get_parser(file=config_file)
324
self.assertEqual("vim", my_config.get_editor())
326
def test_signatures_always(self):
327
config_file = StringIO(sample_always_signatures)
328
my_config = config.GlobalConfig()
329
my_config._parser = my_config._get_parser(file=config_file)
330
self.assertEqual(config.CHECK_ALWAYS,
331
my_config.signature_checking())
332
self.assertEqual(True, my_config.signature_needed())
334
def test_signatures_if_possible(self):
335
config_file = StringIO(sample_maybe_signatures)
336
my_config = config.GlobalConfig()
337
my_config._parser = my_config._get_parser(file=config_file)
338
self.assertEqual(config.CHECK_IF_POSSIBLE,
339
my_config.signature_checking())
340
self.assertEqual(False, my_config.signature_needed())
342
def test_signatures_ignore(self):
343
config_file = StringIO(sample_ignore_signatures)
344
my_config = config.GlobalConfig()
345
my_config._parser = my_config._get_parser(file=config_file)
346
self.assertEqual(config.CHECK_NEVER,
347
my_config.signature_checking())
348
self.assertEqual(False, my_config.signature_needed())
350
def _get_sample_config(self):
351
config_file = StringIO(sample_config_text.encode('utf-8'))
352
my_config = config.GlobalConfig()
353
my_config._parser = my_config._get_parser(file=config_file)
356
def test_gpg_signing_command(self):
357
my_config = self._get_sample_config()
358
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
359
self.assertEqual(False, my_config.signature_needed())
361
def _get_empty_config(self):
362
config_file = StringIO("")
363
my_config = config.GlobalConfig()
364
my_config._parser = my_config._get_parser(file=config_file)
367
def test_gpg_signing_command_unset(self):
368
my_config = self._get_empty_config()
369
self.assertEqual("gpg", my_config.gpg_signing_command())
371
def test_get_user_option_default(self):
372
my_config = self._get_empty_config()
373
self.assertEqual(None, my_config.get_user_option('no_option'))
375
def test_get_user_option_global(self):
376
my_config = self._get_sample_config()
377
self.assertEqual("something",
378
my_config.get_user_option('user_global_option'))
380
def test_post_commit_default(self):
381
my_config = self._get_sample_config()
382
self.assertEqual(None, my_config.post_commit())
384
def test_configured_logformat(self):
385
my_config = self._get_sample_config()
386
self.assertEqual("short", my_config.log_format())
388
def test_get_alias(self):
389
my_config = self._get_sample_config()
390
self.assertEqual('help', my_config.get_alias('h'))
392
def test_get_no_alias(self):
393
my_config = self._get_sample_config()
394
self.assertEqual(None, my_config.get_alias('foo'))
396
def test_get_long_alias(self):
397
my_config = self._get_sample_config()
398
self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
400
class TestLocationConfig(TestCase):
402
def test_constructs(self):
403
my_config = config.LocationConfig('http://example.com')
404
self.assertRaises(TypeError, config.LocationConfig)
406
def test_branch_calls_read_filenames(self):
407
# This is testing the correct file names are provided.
408
# TODO: consolidate with the test for GlobalConfigs filename checks.
410
# replace the class that is constructured, to check its parameters
411
oldparserclass = config.ConfigObj
412
config.ConfigObj = InstrumentedConfigObj
413
my_config = config.LocationConfig('http://www.example.com')
415
parser = my_config._get_parser()
417
config.ConfigObj = oldparserclass
418
self.failUnless(isinstance(parser, InstrumentedConfigObj))
419
self.assertEqual(parser._calls,
420
[('__init__', config.branches_config_filename())])
422
def test_get_global_config(self):
423
my_config = config.LocationConfig('http://example.com')
424
global_config = my_config._get_global_config()
425
self.failUnless(isinstance(global_config, config.GlobalConfig))
426
self.failUnless(global_config is my_config._get_global_config())
428
def test__get_section_no_match(self):
429
self.get_location_config('/')
430
self.assertEqual(None, self.my_config._get_section())
432
def test__get_section_exact(self):
433
self.get_location_config('http://www.example.com')
434
self.assertEqual('http://www.example.com',
435
self.my_config._get_section())
437
def test__get_section_suffix_does_not(self):
438
self.get_location_config('http://www.example.com-com')
439
self.assertEqual(None, self.my_config._get_section())
441
def test__get_section_subdir_recursive(self):
442
self.get_location_config('http://www.example.com/com')
443
self.assertEqual('http://www.example.com',
444
self.my_config._get_section())
446
def test__get_section_subdir_matches(self):
447
self.get_location_config('http://www.example.com/useglobal')
448
self.assertEqual('http://www.example.com/useglobal',
449
self.my_config._get_section())
451
def test__get_section_subdir_nonrecursive(self):
452
self.get_location_config(
453
'http://www.example.com/useglobal/childbranch')
454
self.assertEqual('http://www.example.com',
455
self.my_config._get_section())
457
def test__get_section_subdir_trailing_slash(self):
458
self.get_location_config('/b')
459
self.assertEqual('/b/', self.my_config._get_section())
461
def test__get_section_subdir_child(self):
462
self.get_location_config('/a/foo')
463
self.assertEqual('/a/*', self.my_config._get_section())
465
def test__get_section_subdir_child_child(self):
466
self.get_location_config('/a/foo/bar')
467
self.assertEqual('/a/', self.my_config._get_section())
469
def test__get_section_trailing_slash_with_children(self):
470
self.get_location_config('/a/')
471
self.assertEqual('/a/', self.my_config._get_section())
473
def test__get_section_explicit_over_glob(self):
474
self.get_location_config('/a/c')
475
self.assertEqual('/a/c', self.my_config._get_section())
477
def get_location_config(self, location, global_config=None):
478
if global_config is None:
479
global_file = StringIO(sample_config_text)
481
global_file = StringIO(global_config)
482
branches_file = StringIO(sample_branches_text)
483
self.my_config = config.LocationConfig(location)
484
self.my_config._get_parser(branches_file)
485
self.my_config._get_global_config()._get_parser(global_file)
487
def test_location_without_username(self):
488
self.get_location_config('http://www.example.com/useglobal')
489
self.assertEqual('Robert Collins <robertc@example.com>',
490
self.my_config.username())
492
def test_location_not_listed(self):
493
self.get_location_config('/home/robertc/sources')
494
self.assertEqual('Robert Collins <robertc@example.com>',
495
self.my_config.username())
497
def test_overriding_location(self):
498
self.get_location_config('http://www.example.com/foo')
499
self.assertEqual('Robert Collins <robertc@example.org>',
500
self.my_config.username())
502
def test_signatures_not_set(self):
503
self.get_location_config('http://www.example.com',
504
global_config=sample_ignore_signatures)
505
self.assertEqual(config.CHECK_NEVER,
506
self.my_config.signature_checking())
508
def test_signatures_never(self):
509
self.get_location_config('/a/c')
510
self.assertEqual(config.CHECK_NEVER,
511
self.my_config.signature_checking())
513
def test_signatures_when_available(self):
514
self.get_location_config('/a/', global_config=sample_ignore_signatures)
515
self.assertEqual(config.CHECK_IF_POSSIBLE,
516
self.my_config.signature_checking())
518
def test_signatures_always(self):
519
self.get_location_config('/b')
520
self.assertEqual(config.CHECK_ALWAYS,
521
self.my_config.signature_checking())
523
def test_gpg_signing_command(self):
524
self.get_location_config('/b')
525
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
527
def test_gpg_signing_command_missing(self):
528
self.get_location_config('/a')
529
self.assertEqual("false", self.my_config.gpg_signing_command())
531
def test_get_user_option_global(self):
532
self.get_location_config('/a')
533
self.assertEqual('something',
534
self.my_config.get_user_option('user_global_option'))
536
def test_get_user_option_local(self):
537
self.get_location_config('/a')
538
self.assertEqual('local',
539
self.my_config.get_user_option('user_local_option'))
541
def test_post_commit_default(self):
542
self.get_location_config('/a/c')
543
self.assertEqual('bzrlib.tests.test_config.post_commit',
544
self.my_config.post_commit())
547
class TestLocationConfig(TestCaseInTempDir):
549
def get_location_config(self, location, global_config=None):
550
if global_config is None:
551
global_file = StringIO(sample_config_text.encode('utf-8'))
553
global_file = StringIO(global_config.encode('utf-8'))
554
branches_file = StringIO(sample_branches_text.encode('utf-8'))
555
self.my_config = config.LocationConfig(location)
556
self.my_config._get_parser(branches_file)
557
self.my_config._get_global_config()._get_parser(global_file)
559
def test_set_user_setting_sets_and_saves(self):
560
self.get_location_config('/a/c')
561
record = InstrumentedConfigObj("foo")
562
self.my_config._parser = record
564
real_mkdir = os.mkdir
566
def checked_mkdir(path, mode=0777):
567
self.log('making directory: %s', path)
568
real_mkdir(path, mode)
571
os.mkdir = checked_mkdir
573
self.my_config.set_user_option('foo', 'bar')
575
os.mkdir = real_mkdir
577
self.failUnless(self.created, 'Failed to create ~/.bazaar')
578
self.assertEqual([('__contains__', '/a/c'),
579
('__contains__', '/a/c/'),
580
('__setitem__', '/a/c', {}),
581
('__getitem__', '/a/c'),
582
('__setitem__', 'foo', 'bar'),
587
class TestBranchConfigItems(TestCase):
589
def test_user_id(self):
590
branch = FakeBranch()
591
my_config = config.BranchConfig(branch)
592
self.assertEqual("Robert Collins <robertc@example.net>",
593
my_config._get_user_id())
594
branch.control_files.email = "John"
595
self.assertEqual("John", my_config._get_user_id())
597
def test_not_set_in_branch(self):
598
branch = FakeBranch()
599
my_config = config.BranchConfig(branch)
600
branch.control_files.email = None
601
config_file = StringIO(sample_config_text.encode('utf-8'))
602
(my_config._get_location_config().
603
_get_global_config()._get_parser(config_file))
604
self.assertEqual(u"Erik Bågfors <erik@bagfors.nu>",
605
my_config._get_user_id())
606
branch.control_files.email = "John"
607
self.assertEqual("John", my_config._get_user_id())
609
def test_BZREMAIL_OVERRIDES(self):
610
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
611
branch = FakeBranch()
612
my_config = config.BranchConfig(branch)
613
self.assertEqual("Robert Collins <robertc@example.org>",
614
my_config.username())
616
def test_signatures_forced(self):
617
branch = FakeBranch()
618
my_config = config.BranchConfig(branch)
619
config_file = StringIO(sample_always_signatures)
620
(my_config._get_location_config().
621
_get_global_config()._get_parser(config_file))
622
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
624
def test_gpg_signing_command(self):
625
branch = FakeBranch()
626
my_config = config.BranchConfig(branch)
627
config_file = StringIO(sample_config_text.encode('utf-8'))
628
(my_config._get_location_config().
629
_get_global_config()._get_parser(config_file))
630
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
632
def test_get_user_option_global(self):
633
branch = FakeBranch()
634
my_config = config.BranchConfig(branch)
635
config_file = StringIO(sample_config_text.encode('utf-8'))
636
(my_config._get_location_config().
637
_get_global_config()._get_parser(config_file))
638
self.assertEqual('something',
639
my_config.get_user_option('user_global_option'))
641
def test_post_commit_default(self):
642
branch = FakeBranch()
644
my_config = config.BranchConfig(branch)
645
config_file = StringIO(sample_config_text.encode('utf-8'))
646
(my_config._get_location_config().
647
_get_global_config()._get_parser(config_file))
648
branch_file = StringIO(sample_branches_text)
649
my_config._get_location_config()._get_parser(branch_file)
650
self.assertEqual('bzrlib.tests.test_config.post_commit',
651
my_config.post_commit())
654
class TestMailAddressExtraction(TestCase):
656
def test_extract_email_address(self):
657
self.assertEqual('jane@test.com',
658
config.extract_email_address('Jane <jane@test.com>'))
659
self.assertRaises(errors.BzrError,
660
config.extract_email_address, 'Jane Tester')