1
# Copyright (C) 2005 by Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Tests for finding and reading the bzr config file[s]."""
19
# import system imports here
20
from bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
21
from cStringIO import StringIO
25
#import bzrlib specific imports here
26
import bzrlib.config as config
27
import bzrlib.errors as errors
28
from bzrlib.tests import TestCase, TestCaseInTempDir
31
sample_long_alias="log -r-15..-1 --line"
32
sample_config_text = ("[DEFAULT]\n"
33
"email=Robert Collins <robertc@example.com>\n"
35
"gpg_signing_command=gnome-gpg\n"
37
"user_global_option=something\n"
40
"ll=" + sample_long_alias + "\n")
43
sample_always_signatures = ("[DEFAULT]\n"
44
"check_signatures=require\n")
47
sample_ignore_signatures = ("[DEFAULT]\n"
48
"check_signatures=ignore\n")
51
sample_maybe_signatures = ("[DEFAULT]\n"
52
"check_signatures=check-available\n")
55
sample_branches_text = ("[http://www.example.com]\n"
56
"# Top level policy\n"
57
"email=Robert Collins <robertc@example.org>\n"
58
"[http://www.example.com/useglobal]\n"
59
"# different project, forces global lookup\n"
62
"check_signatures=require\n"
63
"# test trailing / matching with no children\n"
65
"check_signatures=check-available\n"
66
"gpg_signing_command=false\n"
67
"user_local_option=local\n"
68
"# test trailing / matching\n"
70
"#subdirs will match but not the parent\n"
73
"check_signatures=ignore\n"
74
"post_commit=bzrlib.tests.test_config.post_commit\n"
75
"#testing explicit beats globs\n")
79
class InstrumentedConfigObj(object):
80
"""A config obj look-enough-alike to record calls made to it."""
82
def __contains__(self, thing):
83
self._calls.append(('__contains__', thing))
86
def __getitem__(self, key):
87
self._calls.append(('__getitem__', key))
90
def __init__(self, input):
91
self._calls = [('__init__', input)]
93
def __setitem__(self, key, value):
94
self._calls.append(('__setitem__', key, value))
97
self._calls.append(('write',))
100
class FakeBranch(object):
103
self.base = "http://example.com/branches/demo"
104
self.control_files = FakeControlFiles()
107
class FakeControlFiles(object):
110
self.email = 'Robert Collins <robertc@example.net>\n'
112
def get_utf8(self, filename):
113
if filename != 'email':
114
raise NotImplementedError
115
if self.email is not None:
116
return StringIO(self.email)
117
raise errors.NoSuchFile(filename)
120
class InstrumentedConfig(config.Config):
121
"""An instrumented config that supplies stubs for template methods."""
124
super(InstrumentedConfig, self).__init__()
126
self._signatures = config.CHECK_NEVER
128
def _get_user_id(self):
129
self._calls.append('_get_user_id')
130
return "Robert Collins <robert.collins@example.org>"
132
def _get_signature_checking(self):
133
self._calls.append('_get_signature_checking')
134
return self._signatures
137
bool_config = """[DEFAULT]
144
class TestConfigObj(TestCase):
145
def test_get_bool(self):
146
from bzrlib.config import ConfigObj
147
co = ConfigObj(StringIO(bool_config))
148
self.assertIs(co.get_bool('DEFAULT', 'active'), True)
149
self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
150
self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
151
self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
154
class TestConfig(TestCase):
156
def test_constructs(self):
159
def test_no_default_editor(self):
160
self.assertRaises(NotImplementedError, config.Config().get_editor)
162
def test_user_email(self):
163
my_config = InstrumentedConfig()
164
self.assertEqual('robert.collins@example.org', my_config.user_email())
165
self.assertEqual(['_get_user_id'], my_config._calls)
167
def test_username(self):
168
my_config = InstrumentedConfig()
169
self.assertEqual('Robert Collins <robert.collins@example.org>',
170
my_config.username())
171
self.assertEqual(['_get_user_id'], my_config._calls)
173
def test_signatures_default(self):
174
my_config = config.Config()
175
self.assertEqual(config.CHECK_IF_POSSIBLE,
176
my_config.signature_checking())
178
def test_signatures_template_method(self):
179
my_config = InstrumentedConfig()
180
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
181
self.assertEqual(['_get_signature_checking'], my_config._calls)
183
def test_signatures_template_method_none(self):
184
my_config = InstrumentedConfig()
185
my_config._signatures = None
186
self.assertEqual(config.CHECK_IF_POSSIBLE,
187
my_config.signature_checking())
188
self.assertEqual(['_get_signature_checking'], my_config._calls)
190
def test_gpg_signing_command_default(self):
191
my_config = config.Config()
192
self.assertEqual('gpg', my_config.gpg_signing_command())
194
def test_get_user_option_default(self):
195
my_config = config.Config()
196
self.assertEqual(None, my_config.get_user_option('no_option'))
198
def test_post_commit_default(self):
199
my_config = config.Config()
200
self.assertEqual(None, my_config.post_commit())
202
def test_log_format_default(self):
203
my_config = config.Config()
204
self.assertEqual('long', my_config.log_format())
207
class TestConfigPath(TestCase):
210
super(TestConfigPath, self).setUp()
211
self.old_home = os.environ.get('HOME', None)
212
self.old_appdata = os.environ.get('APPDATA', None)
213
os.environ['HOME'] = '/home/bogus'
214
os.environ['APPDATA'] = \
215
r'C:\Documents and Settings\bogus\Application Data'
218
if self.old_home is None:
219
del os.environ['HOME']
221
os.environ['HOME'] = self.old_home
222
if self.old_appdata is None:
223
del os.environ['APPDATA']
225
os.environ['APPDATA'] = self.old_appdata
226
super(TestConfigPath, self).tearDown()
228
def test_config_dir(self):
229
if sys.platform == 'win32':
230
self.assertEqual(config.config_dir(),
231
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
233
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
235
def test_config_filename(self):
236
if sys.platform == 'win32':
237
self.assertEqual(config.config_filename(),
238
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
240
self.assertEqual(config.config_filename(),
241
'/home/bogus/.bazaar/bazaar.conf')
243
def test_branches_config_filename(self):
244
if sys.platform == 'win32':
245
self.assertEqual(config.branches_config_filename(),
246
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
248
self.assertEqual(config.branches_config_filename(),
249
'/home/bogus/.bazaar/branches.conf')
251
class TestIniConfig(TestCase):
253
def test_contructs(self):
254
my_config = config.IniBasedConfig("nothing")
256
def test_from_fp(self):
257
config_file = StringIO(sample_config_text)
258
my_config = config.IniBasedConfig(None)
260
isinstance(my_config._get_parser(file=config_file),
263
def test_cached(self):
264
config_file = StringIO(sample_config_text)
265
my_config = config.IniBasedConfig(None)
266
parser = my_config._get_parser(file=config_file)
267
self.failUnless(my_config._get_parser() is parser)
270
class TestGetConfig(TestCase):
272
def test_constructs(self):
273
my_config = config.GlobalConfig()
275
def test_calls_read_filenames(self):
276
# replace the class that is constructured, to check its parameters
277
oldparserclass = config.ConfigObj
278
config.ConfigObj = InstrumentedConfigObj
279
my_config = config.GlobalConfig()
281
parser = my_config._get_parser()
283
config.ConfigObj = oldparserclass
284
self.failUnless(isinstance(parser, InstrumentedConfigObj))
285
self.assertEqual(parser._calls, [('__init__', config.config_filename())])
288
class TestBranchConfig(TestCaseInTempDir):
290
def test_constructs(self):
291
branch = FakeBranch()
292
my_config = config.BranchConfig(branch)
293
self.assertRaises(TypeError, config.BranchConfig)
295
def test_get_location_config(self):
296
branch = FakeBranch()
297
my_config = config.BranchConfig(branch)
298
location_config = my_config._get_location_config()
299
self.assertEqual(branch.base, location_config.location)
300
self.failUnless(location_config is my_config._get_location_config())
303
class TestGlobalConfigItems(TestCase):
305
def test_user_id(self):
306
config_file = StringIO(sample_config_text)
307
my_config = config.GlobalConfig()
308
my_config._parser = my_config._get_parser(file=config_file)
309
self.assertEqual("Robert Collins <robertc@example.com>",
310
my_config._get_user_id())
312
def test_absent_user_id(self):
313
config_file = StringIO("")
314
my_config = config.GlobalConfig()
315
my_config._parser = my_config._get_parser(file=config_file)
316
self.assertEqual(None, my_config._get_user_id())
318
def test_configured_editor(self):
319
config_file = StringIO(sample_config_text)
320
my_config = config.GlobalConfig()
321
my_config._parser = my_config._get_parser(file=config_file)
322
self.assertEqual("vim", my_config.get_editor())
324
def test_signatures_always(self):
325
config_file = StringIO(sample_always_signatures)
326
my_config = config.GlobalConfig()
327
my_config._parser = my_config._get_parser(file=config_file)
328
self.assertEqual(config.CHECK_ALWAYS,
329
my_config.signature_checking())
330
self.assertEqual(True, my_config.signature_needed())
332
def test_signatures_if_possible(self):
333
config_file = StringIO(sample_maybe_signatures)
334
my_config = config.GlobalConfig()
335
my_config._parser = my_config._get_parser(file=config_file)
336
self.assertEqual(config.CHECK_IF_POSSIBLE,
337
my_config.signature_checking())
338
self.assertEqual(False, my_config.signature_needed())
340
def test_signatures_ignore(self):
341
config_file = StringIO(sample_ignore_signatures)
342
my_config = config.GlobalConfig()
343
my_config._parser = my_config._get_parser(file=config_file)
344
self.assertEqual(config.CHECK_NEVER,
345
my_config.signature_checking())
346
self.assertEqual(False, my_config.signature_needed())
348
def _get_sample_config(self):
349
config_file = StringIO(sample_config_text)
350
my_config = config.GlobalConfig()
351
my_config._parser = my_config._get_parser(file=config_file)
354
def test_gpg_signing_command(self):
355
my_config = self._get_sample_config()
356
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
357
self.assertEqual(False, my_config.signature_needed())
359
def _get_empty_config(self):
360
config_file = StringIO("")
361
my_config = config.GlobalConfig()
362
my_config._parser = my_config._get_parser(file=config_file)
365
def test_gpg_signing_command_unset(self):
366
my_config = self._get_empty_config()
367
self.assertEqual("gpg", my_config.gpg_signing_command())
369
def test_get_user_option_default(self):
370
my_config = self._get_empty_config()
371
self.assertEqual(None, my_config.get_user_option('no_option'))
373
def test_get_user_option_global(self):
374
my_config = self._get_sample_config()
375
self.assertEqual("something",
376
my_config.get_user_option('user_global_option'))
378
def test_post_commit_default(self):
379
my_config = self._get_sample_config()
380
self.assertEqual(None, my_config.post_commit())
382
def test_configured_logformat(self):
383
my_config = self._get_sample_config()
384
self.assertEqual("short", my_config.log_format())
386
def test_get_alias(self):
387
my_config = self._get_sample_config()
388
self.assertEqual('help', my_config.get_alias('h'))
390
def test_get_no_alias(self):
391
my_config = self._get_sample_config()
392
self.assertEqual(None, my_config.get_alias('foo'))
394
def test_get_long_alias(self):
395
my_config = self._get_sample_config()
396
self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
398
class TestLocationConfig(TestCase):
400
def test_constructs(self):
401
my_config = config.LocationConfig('http://example.com')
402
self.assertRaises(TypeError, config.LocationConfig)
404
def test_branch_calls_read_filenames(self):
405
# This is testing the correct file names are provided.
406
# TODO: consolidate with the test for GlobalConfigs filename checks.
408
# replace the class that is constructured, to check its parameters
409
oldparserclass = config.ConfigObj
410
config.ConfigObj = InstrumentedConfigObj
411
my_config = config.LocationConfig('http://www.example.com')
413
parser = my_config._get_parser()
415
config.ConfigObj = oldparserclass
416
self.failUnless(isinstance(parser, InstrumentedConfigObj))
417
self.assertEqual(parser._calls,
418
[('__init__', config.branches_config_filename())])
420
def test_get_global_config(self):
421
my_config = config.LocationConfig('http://example.com')
422
global_config = my_config._get_global_config()
423
self.failUnless(isinstance(global_config, config.GlobalConfig))
424
self.failUnless(global_config is my_config._get_global_config())
426
def test__get_section_no_match(self):
427
self.get_location_config('/')
428
self.assertEqual(None, self.my_config._get_section())
430
def test__get_section_exact(self):
431
self.get_location_config('http://www.example.com')
432
self.assertEqual('http://www.example.com',
433
self.my_config._get_section())
435
def test__get_section_suffix_does_not(self):
436
self.get_location_config('http://www.example.com-com')
437
self.assertEqual(None, self.my_config._get_section())
439
def test__get_section_subdir_recursive(self):
440
self.get_location_config('http://www.example.com/com')
441
self.assertEqual('http://www.example.com',
442
self.my_config._get_section())
444
def test__get_section_subdir_matches(self):
445
self.get_location_config('http://www.example.com/useglobal')
446
self.assertEqual('http://www.example.com/useglobal',
447
self.my_config._get_section())
449
def test__get_section_subdir_nonrecursive(self):
450
self.get_location_config(
451
'http://www.example.com/useglobal/childbranch')
452
self.assertEqual('http://www.example.com',
453
self.my_config._get_section())
455
def test__get_section_subdir_trailing_slash(self):
456
self.get_location_config('/b')
457
self.assertEqual('/b/', self.my_config._get_section())
459
def test__get_section_subdir_child(self):
460
self.get_location_config('/a/foo')
461
self.assertEqual('/a/*', self.my_config._get_section())
463
def test__get_section_subdir_child_child(self):
464
self.get_location_config('/a/foo/bar')
465
self.assertEqual('/a/', self.my_config._get_section())
467
def test__get_section_trailing_slash_with_children(self):
468
self.get_location_config('/a/')
469
self.assertEqual('/a/', self.my_config._get_section())
471
def test__get_section_explicit_over_glob(self):
472
self.get_location_config('/a/c')
473
self.assertEqual('/a/c', self.my_config._get_section())
475
def get_location_config(self, location, global_config=None):
476
if global_config is None:
477
global_file = StringIO(sample_config_text)
479
global_file = StringIO(global_config)
480
branches_file = StringIO(sample_branches_text)
481
self.my_config = config.LocationConfig(location)
482
self.my_config._get_parser(branches_file)
483
self.my_config._get_global_config()._get_parser(global_file)
485
def test_location_without_username(self):
486
self.get_location_config('http://www.example.com/useglobal')
487
self.assertEqual('Robert Collins <robertc@example.com>',
488
self.my_config.username())
490
def test_location_not_listed(self):
491
self.get_location_config('/home/robertc/sources')
492
self.assertEqual('Robert Collins <robertc@example.com>',
493
self.my_config.username())
495
def test_overriding_location(self):
496
self.get_location_config('http://www.example.com/foo')
497
self.assertEqual('Robert Collins <robertc@example.org>',
498
self.my_config.username())
500
def test_signatures_not_set(self):
501
self.get_location_config('http://www.example.com',
502
global_config=sample_ignore_signatures)
503
self.assertEqual(config.CHECK_NEVER,
504
self.my_config.signature_checking())
506
def test_signatures_never(self):
507
self.get_location_config('/a/c')
508
self.assertEqual(config.CHECK_NEVER,
509
self.my_config.signature_checking())
511
def test_signatures_when_available(self):
512
self.get_location_config('/a/', global_config=sample_ignore_signatures)
513
self.assertEqual(config.CHECK_IF_POSSIBLE,
514
self.my_config.signature_checking())
516
def test_signatures_always(self):
517
self.get_location_config('/b')
518
self.assertEqual(config.CHECK_ALWAYS,
519
self.my_config.signature_checking())
521
def test_gpg_signing_command(self):
522
self.get_location_config('/b')
523
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
525
def test_gpg_signing_command_missing(self):
526
self.get_location_config('/a')
527
self.assertEqual("false", self.my_config.gpg_signing_command())
529
def test_get_user_option_global(self):
530
self.get_location_config('/a')
531
self.assertEqual('something',
532
self.my_config.get_user_option('user_global_option'))
534
def test_get_user_option_local(self):
535
self.get_location_config('/a')
536
self.assertEqual('local',
537
self.my_config.get_user_option('user_local_option'))
539
def test_post_commit_default(self):
540
self.get_location_config('/a/c')
541
self.assertEqual('bzrlib.tests.test_config.post_commit',
542
self.my_config.post_commit())
545
class TestLocationConfig(TestCaseInTempDir):
547
def get_location_config(self, location, global_config=None):
548
if global_config is None:
549
global_file = StringIO(sample_config_text)
551
global_file = StringIO(global_config)
552
branches_file = StringIO(sample_branches_text)
553
self.my_config = config.LocationConfig(location)
554
self.my_config._get_parser(branches_file)
555
self.my_config._get_global_config()._get_parser(global_file)
557
def test_set_user_setting_sets_and_saves(self):
558
self.get_location_config('/a/c')
559
record = InstrumentedConfigObj("foo")
560
self.my_config._parser = record
562
real_mkdir = os.mkdir
564
def checked_mkdir(path, mode=0777):
565
self.log('making directory: %s', path)
566
real_mkdir(path, mode)
569
os.mkdir = checked_mkdir
571
self.my_config.set_user_option('foo', 'bar')
573
os.mkdir = real_mkdir
575
self.failUnless(self.created, 'Failed to create ~/.bazaar')
576
self.assertEqual([('__contains__', '/a/c'),
577
('__contains__', '/a/c/'),
578
('__setitem__', '/a/c', {}),
579
('__getitem__', '/a/c'),
580
('__setitem__', 'foo', 'bar'),
585
class TestBranchConfigItems(TestCase):
587
def test_user_id(self):
588
branch = FakeBranch()
589
my_config = config.BranchConfig(branch)
590
self.assertEqual("Robert Collins <robertc@example.net>",
591
my_config._get_user_id())
592
branch.control_files.email = "John"
593
self.assertEqual("John", my_config._get_user_id())
595
def test_not_set_in_branch(self):
596
branch = FakeBranch()
597
my_config = config.BranchConfig(branch)
598
branch.control_files.email = None
599
config_file = StringIO(sample_config_text)
600
(my_config._get_location_config().
601
_get_global_config()._get_parser(config_file))
602
self.assertEqual("Robert Collins <robertc@example.com>",
603
my_config._get_user_id())
604
branch.control_files.email = "John"
605
self.assertEqual("John", my_config._get_user_id())
607
def test_BZREMAIL_OVERRIDES(self):
608
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
609
branch = FakeBranch()
610
my_config = config.BranchConfig(branch)
611
self.assertEqual("Robert Collins <robertc@example.org>",
612
my_config.username())
614
def test_signatures_forced(self):
615
branch = FakeBranch()
616
my_config = config.BranchConfig(branch)
617
config_file = StringIO(sample_always_signatures)
618
(my_config._get_location_config().
619
_get_global_config()._get_parser(config_file))
620
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
622
def test_gpg_signing_command(self):
623
branch = FakeBranch()
624
my_config = config.BranchConfig(branch)
625
config_file = StringIO(sample_config_text)
626
(my_config._get_location_config().
627
_get_global_config()._get_parser(config_file))
628
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
630
def test_get_user_option_global(self):
631
branch = FakeBranch()
632
my_config = config.BranchConfig(branch)
633
config_file = StringIO(sample_config_text)
634
(my_config._get_location_config().
635
_get_global_config()._get_parser(config_file))
636
self.assertEqual('something',
637
my_config.get_user_option('user_global_option'))
639
def test_post_commit_default(self):
640
branch = FakeBranch()
642
my_config = config.BranchConfig(branch)
643
config_file = StringIO(sample_config_text)
644
(my_config._get_location_config().
645
_get_global_config()._get_parser(config_file))
646
branch_file = StringIO(sample_branches_text)
647
my_config._get_location_config()._get_parser(branch_file)
648
self.assertEqual('bzrlib.tests.test_config.post_commit',
649
my_config.post_commit())
652
class TestMailAddressExtraction(TestCase):
654
def test_extract_email_address(self):
655
self.assertEqual('jane@test.com',
656
config.extract_email_address('Jane <jane@test.com>'))
657
self.assertRaises(errors.BzrError,
658
config.extract_email_address, 'Jane Tester')