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 ConfigParser import ConfigParser
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.selftest import TestCase, TestCaseInTempDir
31
sample_config_text = ("[DEFAULT]\n"
32
"email=Robert Collins <robertc@example.com>\n"
34
"gpg_signing_command=gnome-gpg\n"
35
"user_global_option=something\n")
38
sample_always_signatures = ("[DEFAULT]\n"
39
"check_signatures=require\n")
42
sample_ignore_signatures = ("[DEFAULT]\n"
43
"check_signatures=ignore\n")
46
sample_maybe_signatures = ("[DEFAULT]\n"
47
"check_signatures=check-available\n")
50
sample_branches_text = ("[http://www.example.com]\n"
51
"# Top level policy\n"
52
"email=Robert Collins <robertc@example.org>\n"
53
"[http://www.example.com/useglobal]\n"
54
"# different project, forces global lookup\n"
57
"check_signatures=require\n"
58
"# test trailing / matching with no children\n"
60
"check_signatures=check-available\n"
61
"gpg_signing_command=false\n"
62
"user_local_option=local\n"
63
"# test trailing / matching\n"
65
"#subdirs will match but not the parent\n"
68
"check_signatures=ignore\n"
69
"#testing explicit beats globs\n")
72
class InstrumentedConfigParser(object):
73
"""A config parser look-enough-alike to record calls made to it."""
78
def read(self, filenames):
79
self._calls.append(('read', filenames))
82
class FakeBranch(object):
85
self.base = "http://example.com/branches/demo"
86
self.email = 'Robert Collins <robertc@example.net>\n'
88
def controlfile(self, filename, mode):
89
if filename != 'email':
90
raise NotImplementedError
91
if self.email is not None:
92
return StringIO(self.email)
93
raise errors.NoSuchFile
96
class InstrumentedConfig(config.Config):
97
"""An instrumented config that supplies stubs for template methods."""
100
super(InstrumentedConfig, self).__init__()
102
self._signatures = config.CHECK_NEVER
104
def _get_user_id(self):
105
self._calls.append('_get_user_id')
106
return "Robert Collins <robert.collins@example.org>"
108
def _get_signature_checking(self):
109
self._calls.append('_get_signature_checking')
110
return self._signatures
113
class TestConfig(TestCase):
115
def test_constructs(self):
118
def test_no_default_editor(self):
119
self.assertRaises(NotImplementedError, config.Config().get_editor)
121
def test_user_email(self):
122
my_config = InstrumentedConfig()
123
self.assertEqual('robert.collins@example.org', my_config.user_email())
124
self.assertEqual(['_get_user_id'], my_config._calls)
126
def test_username(self):
127
my_config = InstrumentedConfig()
128
self.assertEqual('Robert Collins <robert.collins@example.org>',
129
my_config.username())
130
self.assertEqual(['_get_user_id'], my_config._calls)
132
def test_signatures_default(self):
133
my_config = config.Config()
134
self.assertEqual(config.CHECK_IF_POSSIBLE,
135
my_config.signature_checking())
137
def test_signatures_template_method(self):
138
my_config = InstrumentedConfig()
139
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
140
self.assertEqual(['_get_signature_checking'], my_config._calls)
142
def test_signatures_template_method_none(self):
143
my_config = InstrumentedConfig()
144
my_config._signatures = None
145
self.assertEqual(config.CHECK_IF_POSSIBLE,
146
my_config.signature_checking())
147
self.assertEqual(['_get_signature_checking'], my_config._calls)
149
def test_gpg_signing_command_default(self):
150
my_config = config.Config()
151
self.assertEqual('gpg', my_config.gpg_signing_command())
153
def test_get_user_option_default(self):
154
my_config = config.Config()
155
self.assertEqual(None, my_config.get_user_option('no_option'))
158
class TestConfigPath(TestCase):
161
super(TestConfigPath, self).setUp()
162
self.oldenv = os.environ.get('HOME', None)
163
os.environ['HOME'] = '/home/bogus'
166
os.environ['HOME'] = self.oldenv
167
super(TestConfigPath, self).tearDown()
169
def test_config_dir(self):
170
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
172
def test_config_filename(self):
173
self.assertEqual(config.config_filename(),
174
'/home/bogus/.bazaar/bazaar.conf')
176
def test_branches_config_filename(self):
177
self.assertEqual(config.branches_config_filename(),
178
'/home/bogus/.bazaar/branches.conf')
180
class TestIniConfig(TestCase):
182
def test_contructs(self):
183
my_config = config.IniBasedConfig("nothing")
185
def test_from_fp(self):
186
print "Skip ConfigParser-specific test"
188
config_file = StringIO(sample_config_text)
189
my_config = config.IniBasedConfig(None)
191
isinstance(my_config._get_parser(file=config_file),
194
def test_cached(self):
195
config_file = StringIO(sample_config_text)
196
my_config = config.IniBasedConfig(None)
197
parser = my_config._get_parser(file=config_file)
198
self.failUnless(my_config._get_parser() is parser)
201
class TestGetConfig(TestCase):
203
def test_constructs(self):
204
my_config = config.GlobalConfig()
206
def test_calls_read_filenames(self):
207
print "Skip ConfigParser-specific test"
209
# replace the class that is constructured, to check its parameters
210
oldparserclass = config.ConfigParser
211
config.ConfigParser = InstrumentedConfigParser
212
my_config = config.GlobalConfig()
214
parser = my_config._get_parser()
216
config.ConfigParser = oldparserclass
217
self.failUnless(isinstance(parser, InstrumentedConfigParser))
218
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
221
class TestBranchConfig(TestCaseInTempDir):
223
def test_constructs(self):
224
branch = FakeBranch()
225
my_config = config.BranchConfig(branch)
226
self.assertRaises(TypeError, config.BranchConfig)
228
def test_get_location_config(self):
229
branch = FakeBranch()
230
my_config = config.BranchConfig(branch)
231
location_config = my_config._get_location_config()
232
self.assertEqual(branch.base, location_config.location)
233
self.failUnless(location_config is my_config._get_location_config())
236
class TestGlobalConfigItems(TestCase):
238
def test_user_id(self):
239
config_file = StringIO(sample_config_text)
240
my_config = config.GlobalConfig()
241
my_config._parser = my_config._get_parser(file=config_file)
242
self.assertEqual("Robert Collins <robertc@example.com>",
243
my_config._get_user_id())
245
def test_absent_user_id(self):
246
config_file = StringIO("")
247
my_config = config.GlobalConfig()
248
my_config._parser = my_config._get_parser(file=config_file)
249
self.assertEqual(None, my_config._get_user_id())
251
def test_configured_editor(self):
252
config_file = StringIO(sample_config_text)
253
my_config = config.GlobalConfig()
254
my_config._parser = my_config._get_parser(file=config_file)
255
self.assertEqual("vim", my_config.get_editor())
257
def test_signatures_always(self):
258
config_file = StringIO(sample_always_signatures)
259
my_config = config.GlobalConfig()
260
my_config._parser = my_config._get_parser(file=config_file)
261
self.assertEqual(config.CHECK_ALWAYS,
262
my_config.signature_checking())
263
self.assertEqual(True, my_config.signature_needed())
265
def test_signatures_if_possible(self):
266
config_file = StringIO(sample_maybe_signatures)
267
my_config = config.GlobalConfig()
268
my_config._parser = my_config._get_parser(file=config_file)
269
self.assertEqual(config.CHECK_IF_POSSIBLE,
270
my_config.signature_checking())
271
self.assertEqual(False, my_config.signature_needed())
273
def test_signatures_ignore(self):
274
config_file = StringIO(sample_ignore_signatures)
275
my_config = config.GlobalConfig()
276
my_config._parser = my_config._get_parser(file=config_file)
277
self.assertEqual(config.CHECK_NEVER,
278
my_config.signature_checking())
279
self.assertEqual(False, my_config.signature_needed())
281
def _get_sample_config(self):
282
config_file = StringIO(sample_config_text)
283
my_config = config.GlobalConfig()
284
my_config._parser = my_config._get_parser(file=config_file)
287
def test_gpg_signing_command(self):
288
my_config = self._get_sample_config()
289
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
290
self.assertEqual(False, my_config.signature_needed())
292
def _get_empty_config(self):
293
config_file = StringIO("")
294
my_config = config.GlobalConfig()
295
my_config._parser = my_config._get_parser(file=config_file)
298
def test_gpg_signing_command_unset(self):
299
my_config = self._get_empty_config()
300
self.assertEqual("gpg", my_config.gpg_signing_command())
302
def test_get_user_option_default(self):
303
my_config = self._get_empty_config()
304
self.assertEqual(None, my_config.get_user_option('no_option'))
306
def test_get_user_option_global(self):
307
my_config = self._get_sample_config()
308
self.assertEqual("something",
309
my_config.get_user_option('user_global_option'))
312
class TestLocationConfig(TestCase):
314
def test_constructs(self):
315
my_config = config.LocationConfig('http://example.com')
316
self.assertRaises(TypeError, config.LocationConfig)
318
def test_branch_calls_read_filenames(self):
319
# replace the class that is constructured, to check its parameters
320
print "Skip ConfigParser-specific test"
322
oldparserclass = config.ConfigParser
323
config.ConfigParser = InstrumentedConfigParser
324
my_config = config.LocationConfig('http://www.example.com')
326
parser = my_config._get_parser()
328
config.ConfigParser = oldparserclass
329
self.failUnless(isinstance(parser, InstrumentedConfigParser))
330
self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
332
def test_get_global_config(self):
333
my_config = config.LocationConfig('http://example.com')
334
global_config = my_config._get_global_config()
335
self.failUnless(isinstance(global_config, config.GlobalConfig))
336
self.failUnless(global_config is my_config._get_global_config())
338
def test__get_section_no_match(self):
339
self.get_location_config('/')
340
self.assertEqual(None, self.my_config._get_section())
342
def test__get_section_exact(self):
343
self.get_location_config('http://www.example.com')
344
self.assertEqual('http://www.example.com',
345
self.my_config._get_section())
347
def test__get_section_suffix_does_not(self):
348
self.get_location_config('http://www.example.com-com')
349
self.assertEqual(None, self.my_config._get_section())
351
def test__get_section_subdir_recursive(self):
352
self.get_location_config('http://www.example.com/com')
353
self.assertEqual('http://www.example.com',
354
self.my_config._get_section())
356
def test__get_section_subdir_matches(self):
357
self.get_location_config('http://www.example.com/useglobal')
358
self.assertEqual('http://www.example.com/useglobal',
359
self.my_config._get_section())
361
def test__get_section_subdir_nonrecursive(self):
362
self.get_location_config(
363
'http://www.example.com/useglobal/childbranch')
364
self.assertEqual('http://www.example.com',
365
self.my_config._get_section())
367
def test__get_section_subdir_trailing_slash(self):
368
self.get_location_config('/b')
369
self.assertEqual('/b/', self.my_config._get_section())
371
def test__get_section_subdir_child(self):
372
self.get_location_config('/a/foo')
373
self.assertEqual('/a/*', self.my_config._get_section())
375
def test__get_section_subdir_child_child(self):
376
self.get_location_config('/a/foo/bar')
377
self.assertEqual('/a/', self.my_config._get_section())
379
def test__get_section_trailing_slash_with_children(self):
380
self.get_location_config('/a/')
381
self.assertEqual('/a/', self.my_config._get_section())
383
def test__get_section_explicit_over_glob(self):
384
self.get_location_config('/a/c')
385
self.assertEqual('/a/c', self.my_config._get_section())
387
def get_location_config(self, location, global_config=None):
388
if global_config is None:
389
global_file = StringIO(sample_config_text)
391
global_file = StringIO(global_config)
392
branches_file = StringIO(sample_branches_text)
393
self.my_config = config.LocationConfig(location)
394
self.my_config._get_parser(branches_file)
395
self.my_config._get_global_config()._get_parser(global_file)
397
def test_location_without_username(self):
398
self.get_location_config('http://www.example.com/useglobal')
399
self.assertEqual('Robert Collins <robertc@example.com>',
400
self.my_config.username())
402
def test_location_not_listed(self):
403
self.get_location_config('/home/robertc/sources')
404
self.assertEqual('Robert Collins <robertc@example.com>',
405
self.my_config.username())
407
def test_overriding_location(self):
408
self.get_location_config('http://www.example.com/foo')
409
self.assertEqual('Robert Collins <robertc@example.org>',
410
self.my_config.username())
412
def test_signatures_not_set(self):
413
self.get_location_config('http://www.example.com',
414
global_config=sample_ignore_signatures)
415
self.assertEqual(config.CHECK_NEVER,
416
self.my_config.signature_checking())
418
def test_signatures_never(self):
419
self.get_location_config('/a/c')
420
self.assertEqual(config.CHECK_NEVER,
421
self.my_config.signature_checking())
423
def test_signatures_when_available(self):
424
self.get_location_config('/a/', global_config=sample_ignore_signatures)
425
self.assertEqual(config.CHECK_IF_POSSIBLE,
426
self.my_config.signature_checking())
428
def test_signatures_always(self):
429
self.get_location_config('/b')
430
self.assertEqual(config.CHECK_ALWAYS,
431
self.my_config.signature_checking())
433
def test_gpg_signing_command(self):
434
self.get_location_config('/b')
435
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
437
def test_gpg_signing_command_missing(self):
438
self.get_location_config('/a')
439
self.assertEqual("false", self.my_config.gpg_signing_command())
441
def test_get_user_option_global(self):
442
self.get_location_config('/a')
443
self.assertEqual('something',
444
self.my_config.get_user_option('user_global_option'))
446
def test_get_user_option_local(self):
447
self.get_location_config('/a')
448
self.assertEqual('local',
449
self.my_config.get_user_option('user_local_option'))
452
class TestBranchConfigItems(TestCase):
454
def test_user_id(self):
455
branch = FakeBranch()
456
my_config = config.BranchConfig(branch)
457
self.assertEqual("Robert Collins <robertc@example.net>",
458
my_config._get_user_id())
459
branch.email = "John"
460
self.assertEqual("John", my_config._get_user_id())
462
def test_not_set_in_branch(self):
463
branch = FakeBranch()
464
my_config = config.BranchConfig(branch)
466
config_file = StringIO(sample_config_text)
467
(my_config._get_location_config().
468
_get_global_config()._get_parser(config_file))
469
self.assertEqual("Robert Collins <robertc@example.com>",
470
my_config._get_user_id())
471
branch.email = "John"
472
self.assertEqual("John", my_config._get_user_id())
474
def test_BZREMAIL_OVERRIDES(self):
475
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
476
branch = FakeBranch()
477
my_config = config.BranchConfig(branch)
478
self.assertEqual("Robert Collins <robertc@example.org>",
479
my_config.username())
481
def test_signatures_forced(self):
482
branch = FakeBranch()
483
my_config = config.BranchConfig(branch)
484
config_file = StringIO(sample_always_signatures)
485
(my_config._get_location_config().
486
_get_global_config()._get_parser(config_file))
487
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
489
def test_gpg_signing_command(self):
490
branch = FakeBranch()
491
my_config = config.BranchConfig(branch)
492
config_file = StringIO(sample_config_text)
493
(my_config._get_location_config().
494
_get_global_config()._get_parser(config_file))
495
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
497
def test_get_user_option_global(self):
498
branch = FakeBranch()
499
my_config = config.BranchConfig(branch)
500
config_file = StringIO(sample_config_text)
501
(my_config._get_location_config().
502
_get_global_config()._get_parser(config_file))
503
self.assertEqual('something',
504
my_config.get_user_option('user_global_option'))