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_config_text = ("[DEFAULT]\n"
32
"email=Robert Collins <robertc@example.com>\n"
34
"gpg_signing_command=gnome-gpg\n"
36
"user_global_option=something\n")
39
sample_always_signatures = ("[DEFAULT]\n"
40
"check_signatures=require\n")
43
sample_ignore_signatures = ("[DEFAULT]\n"
44
"check_signatures=ignore\n")
47
sample_maybe_signatures = ("[DEFAULT]\n"
48
"check_signatures=check-available\n")
51
sample_branches_text = ("[http://www.example.com]\n"
52
"# Top level policy\n"
53
"email=Robert Collins <robertc@example.org>\n"
54
"[http://www.example.com/useglobal]\n"
55
"# different project, forces global lookup\n"
58
"check_signatures=require\n"
59
"# test trailing / matching with no children\n"
61
"check_signatures=check-available\n"
62
"gpg_signing_command=false\n"
63
"user_local_option=local\n"
64
"# test trailing / matching\n"
66
"#subdirs will match but not the parent\n"
69
"check_signatures=ignore\n"
70
"post_commit=bzrlib.tests.test_config.post_commit\n"
71
"#testing explicit beats globs\n")
74
class InstrumentedConfigObj(object):
75
"""A config obj look-enough-alike to record calls made to it."""
77
def __contains__(self, thing):
78
self._calls.append(('__contains__', thing))
81
def __getitem__(self, key):
82
self._calls.append(('__getitem__', key))
85
def __init__(self, input):
86
self._calls = [('__init__', input)]
88
def __setitem__(self, key, value):
89
self._calls.append(('__setitem__', key, value))
92
self._calls.append(('write',))
95
class FakeBranch(object):
98
self.base = "http://example.com/branches/demo"
99
self.control_files = FakeControlFiles()
102
class FakeControlFiles(object):
105
self.email = 'Robert Collins <robertc@example.net>\n'
107
def get_utf8(self, filename):
108
if filename != 'email':
109
raise NotImplementedError
110
if self.email is not None:
111
return StringIO(self.email)
112
raise errors.NoSuchFile(filename)
115
class InstrumentedConfig(config.Config):
116
"""An instrumented config that supplies stubs for template methods."""
119
super(InstrumentedConfig, self).__init__()
121
self._signatures = config.CHECK_NEVER
123
def _get_user_id(self):
124
self._calls.append('_get_user_id')
125
return "Robert Collins <robert.collins@example.org>"
127
def _get_signature_checking(self):
128
self._calls.append('_get_signature_checking')
129
return self._signatures
132
class TestConfig(TestCase):
134
def test_constructs(self):
137
def test_no_default_editor(self):
138
self.assertRaises(NotImplementedError, config.Config().get_editor)
140
def test_user_email(self):
141
my_config = InstrumentedConfig()
142
self.assertEqual('robert.collins@example.org', my_config.user_email())
143
self.assertEqual(['_get_user_id'], my_config._calls)
145
def test_username(self):
146
my_config = InstrumentedConfig()
147
self.assertEqual('Robert Collins <robert.collins@example.org>',
148
my_config.username())
149
self.assertEqual(['_get_user_id'], my_config._calls)
151
def test_signatures_default(self):
152
my_config = config.Config()
153
self.assertEqual(config.CHECK_IF_POSSIBLE,
154
my_config.signature_checking())
156
def test_signatures_template_method(self):
157
my_config = InstrumentedConfig()
158
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
159
self.assertEqual(['_get_signature_checking'], my_config._calls)
161
def test_signatures_template_method_none(self):
162
my_config = InstrumentedConfig()
163
my_config._signatures = None
164
self.assertEqual(config.CHECK_IF_POSSIBLE,
165
my_config.signature_checking())
166
self.assertEqual(['_get_signature_checking'], my_config._calls)
168
def test_gpg_signing_command_default(self):
169
my_config = config.Config()
170
self.assertEqual('gpg', my_config.gpg_signing_command())
172
def test_get_user_option_default(self):
173
my_config = config.Config()
174
self.assertEqual(None, my_config.get_user_option('no_option'))
176
def test_post_commit_default(self):
177
my_config = config.Config()
178
self.assertEqual(None, my_config.post_commit())
180
def test_log_format_default(self):
181
my_config = config.Config()
182
self.assertEqual('long', my_config.log_format())
185
class TestConfigPath(TestCase):
188
super(TestConfigPath, self).setUp()
189
self.old_home = os.environ.get('HOME', None)
190
self.old_appdata = os.environ.get('APPDATA', None)
191
os.environ['HOME'] = '/home/bogus'
192
os.environ['APPDATA'] = \
193
r'C:\Documents and Settings\bogus\Application Data'
196
if self.old_home is None:
197
del os.environ['HOME']
199
os.environ['HOME'] = self.old_home
200
if self.old_appdata is None:
201
del os.environ['APPDATA']
203
os.environ['APPDATA'] = self.old_appdata
204
super(TestConfigPath, self).tearDown()
206
def test_config_dir(self):
207
if sys.platform == 'win32':
208
self.assertEqual(config.config_dir(),
209
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
211
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
213
def test_config_filename(self):
214
if sys.platform == 'win32':
215
self.assertEqual(config.config_filename(),
216
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
218
self.assertEqual(config.config_filename(),
219
'/home/bogus/.bazaar/bazaar.conf')
221
def test_branches_config_filename(self):
222
if sys.platform == 'win32':
223
self.assertEqual(config.branches_config_filename(),
224
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
226
self.assertEqual(config.branches_config_filename(),
227
'/home/bogus/.bazaar/branches.conf')
229
class TestIniConfig(TestCase):
231
def test_contructs(self):
232
my_config = config.IniBasedConfig("nothing")
234
def test_from_fp(self):
235
config_file = StringIO(sample_config_text)
236
my_config = config.IniBasedConfig(None)
238
isinstance(my_config._get_parser(file=config_file),
241
def test_cached(self):
242
config_file = StringIO(sample_config_text)
243
my_config = config.IniBasedConfig(None)
244
parser = my_config._get_parser(file=config_file)
245
self.failUnless(my_config._get_parser() is parser)
248
class TestGetConfig(TestCase):
250
def test_constructs(self):
251
my_config = config.GlobalConfig()
253
def test_calls_read_filenames(self):
254
# replace the class that is constructured, to check its parameters
255
oldparserclass = config.ConfigObj
256
config.ConfigObj = InstrumentedConfigObj
257
my_config = config.GlobalConfig()
259
parser = my_config._get_parser()
261
config.ConfigObj = oldparserclass
262
self.failUnless(isinstance(parser, InstrumentedConfigObj))
263
self.assertEqual(parser._calls, [('__init__', config.config_filename())])
266
class TestBranchConfig(TestCaseInTempDir):
268
def test_constructs(self):
269
branch = FakeBranch()
270
my_config = config.BranchConfig(branch)
271
self.assertRaises(TypeError, config.BranchConfig)
273
def test_get_location_config(self):
274
branch = FakeBranch()
275
my_config = config.BranchConfig(branch)
276
location_config = my_config._get_location_config()
277
self.assertEqual(branch.base, location_config.location)
278
self.failUnless(location_config is my_config._get_location_config())
281
class TestGlobalConfigItems(TestCase):
283
def test_user_id(self):
284
config_file = StringIO(sample_config_text)
285
my_config = config.GlobalConfig()
286
my_config._parser = my_config._get_parser(file=config_file)
287
self.assertEqual("Robert Collins <robertc@example.com>",
288
my_config._get_user_id())
290
def test_absent_user_id(self):
291
config_file = StringIO("")
292
my_config = config.GlobalConfig()
293
my_config._parser = my_config._get_parser(file=config_file)
294
self.assertEqual(None, my_config._get_user_id())
296
def test_configured_editor(self):
297
config_file = StringIO(sample_config_text)
298
my_config = config.GlobalConfig()
299
my_config._parser = my_config._get_parser(file=config_file)
300
self.assertEqual("vim", my_config.get_editor())
302
def test_signatures_always(self):
303
config_file = StringIO(sample_always_signatures)
304
my_config = config.GlobalConfig()
305
my_config._parser = my_config._get_parser(file=config_file)
306
self.assertEqual(config.CHECK_ALWAYS,
307
my_config.signature_checking())
308
self.assertEqual(True, my_config.signature_needed())
310
def test_signatures_if_possible(self):
311
config_file = StringIO(sample_maybe_signatures)
312
my_config = config.GlobalConfig()
313
my_config._parser = my_config._get_parser(file=config_file)
314
self.assertEqual(config.CHECK_IF_POSSIBLE,
315
my_config.signature_checking())
316
self.assertEqual(False, my_config.signature_needed())
318
def test_signatures_ignore(self):
319
config_file = StringIO(sample_ignore_signatures)
320
my_config = config.GlobalConfig()
321
my_config._parser = my_config._get_parser(file=config_file)
322
self.assertEqual(config.CHECK_NEVER,
323
my_config.signature_checking())
324
self.assertEqual(False, my_config.signature_needed())
326
def _get_sample_config(self):
327
config_file = StringIO(sample_config_text)
328
my_config = config.GlobalConfig()
329
my_config._parser = my_config._get_parser(file=config_file)
332
def test_gpg_signing_command(self):
333
my_config = self._get_sample_config()
334
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
335
self.assertEqual(False, my_config.signature_needed())
337
def _get_empty_config(self):
338
config_file = StringIO("")
339
my_config = config.GlobalConfig()
340
my_config._parser = my_config._get_parser(file=config_file)
343
def test_gpg_signing_command_unset(self):
344
my_config = self._get_empty_config()
345
self.assertEqual("gpg", my_config.gpg_signing_command())
347
def test_get_user_option_default(self):
348
my_config = self._get_empty_config()
349
self.assertEqual(None, my_config.get_user_option('no_option'))
351
def test_get_user_option_global(self):
352
my_config = self._get_sample_config()
353
self.assertEqual("something",
354
my_config.get_user_option('user_global_option'))
356
def test_post_commit_default(self):
357
my_config = self._get_sample_config()
358
self.assertEqual(None, my_config.post_commit())
360
def test_configured_logformat(self):
361
my_config = self._get_sample_config()
362
self.assertEqual("short", my_config.log_format())
365
class TestLocationConfig(TestCase):
367
def test_constructs(self):
368
my_config = config.LocationConfig('http://example.com')
369
self.assertRaises(TypeError, config.LocationConfig)
371
def test_branch_calls_read_filenames(self):
372
# This is testing the correct file names are provided.
373
# TODO: consolidate with the test for GlobalConfigs filename checks.
375
# replace the class that is constructured, to check its parameters
376
oldparserclass = config.ConfigObj
377
config.ConfigObj = InstrumentedConfigObj
378
my_config = config.LocationConfig('http://www.example.com')
380
parser = my_config._get_parser()
382
config.ConfigObj = oldparserclass
383
self.failUnless(isinstance(parser, InstrumentedConfigObj))
384
self.assertEqual(parser._calls,
385
[('__init__', config.branches_config_filename())])
387
def test_get_global_config(self):
388
my_config = config.LocationConfig('http://example.com')
389
global_config = my_config._get_global_config()
390
self.failUnless(isinstance(global_config, config.GlobalConfig))
391
self.failUnless(global_config is my_config._get_global_config())
393
def test__get_section_no_match(self):
394
self.get_location_config('/')
395
self.assertEqual(None, self.my_config._get_section())
397
def test__get_section_exact(self):
398
self.get_location_config('http://www.example.com')
399
self.assertEqual('http://www.example.com',
400
self.my_config._get_section())
402
def test__get_section_suffix_does_not(self):
403
self.get_location_config('http://www.example.com-com')
404
self.assertEqual(None, self.my_config._get_section())
406
def test__get_section_subdir_recursive(self):
407
self.get_location_config('http://www.example.com/com')
408
self.assertEqual('http://www.example.com',
409
self.my_config._get_section())
411
def test__get_section_subdir_matches(self):
412
self.get_location_config('http://www.example.com/useglobal')
413
self.assertEqual('http://www.example.com/useglobal',
414
self.my_config._get_section())
416
def test__get_section_subdir_nonrecursive(self):
417
self.get_location_config(
418
'http://www.example.com/useglobal/childbranch')
419
self.assertEqual('http://www.example.com',
420
self.my_config._get_section())
422
def test__get_section_subdir_trailing_slash(self):
423
self.get_location_config('/b')
424
self.assertEqual('/b/', self.my_config._get_section())
426
def test__get_section_subdir_child(self):
427
self.get_location_config('/a/foo')
428
self.assertEqual('/a/*', self.my_config._get_section())
430
def test__get_section_subdir_child_child(self):
431
self.get_location_config('/a/foo/bar')
432
self.assertEqual('/a/', self.my_config._get_section())
434
def test__get_section_trailing_slash_with_children(self):
435
self.get_location_config('/a/')
436
self.assertEqual('/a/', self.my_config._get_section())
438
def test__get_section_explicit_over_glob(self):
439
self.get_location_config('/a/c')
440
self.assertEqual('/a/c', self.my_config._get_section())
442
def get_location_config(self, location, global_config=None):
443
if global_config is None:
444
global_file = StringIO(sample_config_text)
446
global_file = StringIO(global_config)
447
branches_file = StringIO(sample_branches_text)
448
self.my_config = config.LocationConfig(location)
449
self.my_config._get_parser(branches_file)
450
self.my_config._get_global_config()._get_parser(global_file)
452
def test_location_without_username(self):
453
self.get_location_config('http://www.example.com/useglobal')
454
self.assertEqual('Robert Collins <robertc@example.com>',
455
self.my_config.username())
457
def test_location_not_listed(self):
458
self.get_location_config('/home/robertc/sources')
459
self.assertEqual('Robert Collins <robertc@example.com>',
460
self.my_config.username())
462
def test_overriding_location(self):
463
self.get_location_config('http://www.example.com/foo')
464
self.assertEqual('Robert Collins <robertc@example.org>',
465
self.my_config.username())
467
def test_signatures_not_set(self):
468
self.get_location_config('http://www.example.com',
469
global_config=sample_ignore_signatures)
470
self.assertEqual(config.CHECK_NEVER,
471
self.my_config.signature_checking())
473
def test_signatures_never(self):
474
self.get_location_config('/a/c')
475
self.assertEqual(config.CHECK_NEVER,
476
self.my_config.signature_checking())
478
def test_signatures_when_available(self):
479
self.get_location_config('/a/', global_config=sample_ignore_signatures)
480
self.assertEqual(config.CHECK_IF_POSSIBLE,
481
self.my_config.signature_checking())
483
def test_signatures_always(self):
484
self.get_location_config('/b')
485
self.assertEqual(config.CHECK_ALWAYS,
486
self.my_config.signature_checking())
488
def test_gpg_signing_command(self):
489
self.get_location_config('/b')
490
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
492
def test_gpg_signing_command_missing(self):
493
self.get_location_config('/a')
494
self.assertEqual("false", self.my_config.gpg_signing_command())
496
def test_get_user_option_global(self):
497
self.get_location_config('/a')
498
self.assertEqual('something',
499
self.my_config.get_user_option('user_global_option'))
501
def test_get_user_option_local(self):
502
self.get_location_config('/a')
503
self.assertEqual('local',
504
self.my_config.get_user_option('user_local_option'))
506
def test_post_commit_default(self):
507
self.get_location_config('/a/c')
508
self.assertEqual('bzrlib.tests.test_config.post_commit',
509
self.my_config.post_commit())
512
class TestLocationConfig(TestCaseInTempDir):
514
def get_location_config(self, location, global_config=None):
515
if global_config is None:
516
global_file = StringIO(sample_config_text)
518
global_file = StringIO(global_config)
519
branches_file = StringIO(sample_branches_text)
520
self.my_config = config.LocationConfig(location)
521
self.my_config._get_parser(branches_file)
522
self.my_config._get_global_config()._get_parser(global_file)
524
def test_set_user_setting_sets_and_saves(self):
525
self.get_location_config('/a/c')
526
record = InstrumentedConfigObj("foo")
527
self.my_config._parser = record
529
real_mkdir = os.mkdir
531
def checked_mkdir(path, mode=0777):
532
self.log('making directory: %s', path)
533
real_mkdir(path, mode)
536
os.mkdir = checked_mkdir
538
self.my_config.set_user_option('foo', 'bar')
540
os.mkdir = real_mkdir
542
self.failUnless(self.created, 'Failed to create ~/.bazaar')
543
self.assertEqual([('__contains__', '/a/c'),
544
('__contains__', '/a/c/'),
545
('__setitem__', '/a/c', {}),
546
('__getitem__', '/a/c'),
547
('__setitem__', 'foo', 'bar'),
552
class TestBranchConfigItems(TestCase):
554
def test_user_id(self):
555
branch = FakeBranch()
556
my_config = config.BranchConfig(branch)
557
self.assertEqual("Robert Collins <robertc@example.net>",
558
my_config._get_user_id())
559
branch.control_files.email = "John"
560
self.assertEqual("John", my_config._get_user_id())
562
def test_not_set_in_branch(self):
563
branch = FakeBranch()
564
my_config = config.BranchConfig(branch)
565
branch.control_files.email = None
566
config_file = StringIO(sample_config_text)
567
(my_config._get_location_config().
568
_get_global_config()._get_parser(config_file))
569
self.assertEqual("Robert Collins <robertc@example.com>",
570
my_config._get_user_id())
571
branch.control_files.email = "John"
572
self.assertEqual("John", my_config._get_user_id())
574
def test_BZREMAIL_OVERRIDES(self):
575
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
576
branch = FakeBranch()
577
my_config = config.BranchConfig(branch)
578
self.assertEqual("Robert Collins <robertc@example.org>",
579
my_config.username())
581
def test_signatures_forced(self):
582
branch = FakeBranch()
583
my_config = config.BranchConfig(branch)
584
config_file = StringIO(sample_always_signatures)
585
(my_config._get_location_config().
586
_get_global_config()._get_parser(config_file))
587
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
589
def test_gpg_signing_command(self):
590
branch = FakeBranch()
591
my_config = config.BranchConfig(branch)
592
config_file = StringIO(sample_config_text)
593
(my_config._get_location_config().
594
_get_global_config()._get_parser(config_file))
595
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
597
def test_get_user_option_global(self):
598
branch = FakeBranch()
599
my_config = config.BranchConfig(branch)
600
config_file = StringIO(sample_config_text)
601
(my_config._get_location_config().
602
_get_global_config()._get_parser(config_file))
603
self.assertEqual('something',
604
my_config.get_user_option('user_global_option'))
606
def test_post_commit_default(self):
607
branch = FakeBranch()
609
my_config = config.BranchConfig(branch)
610
config_file = StringIO(sample_config_text)
611
(my_config._get_location_config().
612
_get_global_config()._get_parser(config_file))
613
branch_file = StringIO(sample_branches_text)
614
my_config._get_location_config()._get_parser(branch_file)
615
self.assertEqual('bzrlib.tests.test_config.post_commit',
616
my_config.post_commit())
619
class TestMailAddressExtraction(TestCase):
621
def test_extract_email_address(self):
622
self.assertEqual('jane@test.com',
623
config.extract_email_address('Jane <jane@test.com>'))
624
self.assertRaises(errors.BzrError,
625
config.extract_email_address, 'Jane Tester')