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")
37
sample_always_signatures = ("[DEFAULT]\n"
38
"check_signatures=require\n")
41
sample_ignore_signatures = ("[DEFAULT]\n"
42
"check_signatures=ignore\n")
45
sample_maybe_signatures = ("[DEFAULT]\n"
46
"check_signatures=check-available\n")
49
sample_branches_text = ("[http://www.example.com]\n"
50
"# Top level policy\n"
51
"email=Robert Collins <robertc@example.org>\n"
52
"[http://www.example.com/useglobal]\n"
53
"# different project, forces global lookup\n"
56
"check_signatures=require\n"
57
"# test trailing / matching with no children\n"
59
"check_signatures=check-available\n"
60
"gpg_signing_command=false\n"
61
"# test trailing / matching\n"
63
"#subdirs will match but not the parent\n"
66
"check_signatures=ignore\n"
67
"#testing explicit beats globs\n")
70
class InstrumentedConfigParser(object):
71
"""A config parser look-enough-alike to record calls made to it."""
76
def read(self, filenames):
77
self._calls.append(('read', filenames))
80
class FakeBranch(object):
83
self.base = "http://example.com/branches/demo"
84
self.email = 'Robert Collins <robertc@example.net>\n'
86
def controlfile(self, filename, mode):
87
if filename != 'email':
88
raise NotImplementedError
89
if self.email is not None:
90
return StringIO(self.email)
91
raise errors.NoSuchFile
94
class InstrumentedConfig(config.Config):
95
"""An instrumented config that supplies stubs for template methods."""
98
super(InstrumentedConfig, self).__init__()
100
self._signatures = config.CHECK_NEVER
102
def _get_user_id(self):
103
self._calls.append('_get_user_id')
104
return "Robert Collins <robert.collins@example.org>"
106
def _get_signature_checking(self):
107
self._calls.append('_get_signature_checking')
108
return self._signatures
111
class TestConfig(TestCase):
113
def test_constructs(self):
116
def test_no_default_editor(self):
117
self.assertRaises(NotImplementedError, config.Config().get_editor)
119
def test_user_email(self):
120
my_config = InstrumentedConfig()
121
self.assertEqual('robert.collins@example.org', my_config.user_email())
122
self.assertEqual(['_get_user_id'], my_config._calls)
124
def test_username(self):
125
my_config = InstrumentedConfig()
126
self.assertEqual('Robert Collins <robert.collins@example.org>',
127
my_config.username())
128
self.assertEqual(['_get_user_id'], my_config._calls)
130
def test_signatures_default(self):
131
my_config = config.Config()
132
self.assertEqual(config.CHECK_IF_POSSIBLE,
133
my_config.signature_checking())
135
def test_signatures_template_method(self):
136
my_config = InstrumentedConfig()
137
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
138
self.assertEqual(['_get_signature_checking'], my_config._calls)
140
def test_signatures_template_method_none(self):
141
my_config = InstrumentedConfig()
142
my_config._signatures = None
143
self.assertEqual(config.CHECK_IF_POSSIBLE,
144
my_config.signature_checking())
145
self.assertEqual(['_get_signature_checking'], my_config._calls)
147
def test_gpg_signing_command_default(self):
148
my_config = config.Config()
149
self.assertEqual('gpg', my_config.gpg_signing_command())
152
class TestConfigPath(TestCase):
155
super(TestConfigPath, self).setUp()
156
self.oldenv = os.environ.get('HOME', None)
157
os.environ['HOME'] = '/home/bogus'
160
os.environ['HOME'] = self.oldenv
161
super(TestConfigPath, self).tearDown()
163
def test_config_dir(self):
164
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
166
def test_config_filename(self):
167
self.assertEqual(config.config_filename(),
168
'/home/bogus/.bazaar/bazaar.conf')
170
def test_branches_config_filename(self):
171
self.assertEqual(config.branches_config_filename(),
172
'/home/bogus/.bazaar/branches.conf')
174
class TestIniConfig(TestCase):
176
def test_contructs(self):
177
my_config = config.IniBasedConfig("nothing")
179
def test_from_fp(self):
180
config_file = StringIO(sample_config_text)
181
my_config = config.IniBasedConfig(None)
183
isinstance(my_config._get_parser(file=config_file),
186
def test_cached(self):
187
config_file = StringIO(sample_config_text)
188
my_config = config.IniBasedConfig(None)
189
parser = my_config._get_parser(file=config_file)
190
self.failUnless(my_config._get_parser() is parser)
193
class TestGetConfig(TestCase):
195
def test_constructs(self):
196
my_config = config.GlobalConfig()
198
def test_calls_read_filenames(self):
199
# replace the class that is constructured, to check its parameters
200
oldparserclass = config.ConfigParser
201
config.ConfigParser = InstrumentedConfigParser
202
my_config = config.GlobalConfig()
204
parser = my_config._get_parser()
206
config.ConfigParser = oldparserclass
207
self.failUnless(isinstance(parser, InstrumentedConfigParser))
208
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
211
class TestBranchConfig(TestCaseInTempDir):
213
def test_constructs(self):
214
branch = FakeBranch()
215
my_config = config.BranchConfig(branch)
216
self.assertRaises(TypeError, config.BranchConfig)
218
def test_get_location_config(self):
219
branch = FakeBranch()
220
my_config = config.BranchConfig(branch)
221
location_config = my_config._get_location_config()
222
self.assertEqual(branch.base, location_config.location)
223
self.failUnless(location_config is my_config._get_location_config())
226
class TestGlobalConfigItems(TestCase):
228
def test_user_id(self):
229
config_file = StringIO(sample_config_text)
230
my_config = config.GlobalConfig()
231
my_config._parser = my_config._get_parser(file=config_file)
232
self.assertEqual("Robert Collins <robertc@example.com>",
233
my_config._get_user_id())
235
def test_absent_user_id(self):
236
config_file = StringIO("")
237
my_config = config.GlobalConfig()
238
my_config._parser = my_config._get_parser(file=config_file)
239
self.assertEqual(None, my_config._get_user_id())
241
def test_configured_editor(self):
242
config_file = StringIO(sample_config_text)
243
my_config = config.GlobalConfig()
244
my_config._parser = my_config._get_parser(file=config_file)
245
self.assertEqual("vim", my_config.get_editor())
247
def test_signatures_always(self):
248
config_file = StringIO(sample_always_signatures)
249
my_config = config.GlobalConfig()
250
my_config._parser = my_config._get_parser(file=config_file)
251
self.assertEqual(config.CHECK_ALWAYS,
252
my_config.signature_checking())
253
self.assertEqual(True, my_config.signature_needed())
255
def test_signatures_if_possible(self):
256
config_file = StringIO(sample_maybe_signatures)
257
my_config = config.GlobalConfig()
258
my_config._parser = my_config._get_parser(file=config_file)
259
self.assertEqual(config.CHECK_IF_POSSIBLE,
260
my_config.signature_checking())
261
self.assertEqual(False, my_config.signature_needed())
263
def test_signatures_ignore(self):
264
config_file = StringIO(sample_ignore_signatures)
265
my_config = config.GlobalConfig()
266
my_config._parser = my_config._get_parser(file=config_file)
267
self.assertEqual(config.CHECK_NEVER,
268
my_config.signature_checking())
269
self.assertEqual(False, my_config.signature_needed())
271
def test_gpg_signing_command(self):
272
config_file = StringIO(sample_config_text)
273
my_config = config.GlobalConfig()
274
my_config._parser = my_config._get_parser(file=config_file)
275
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
276
self.assertEqual(False, my_config.signature_needed())
278
def test_gpg_signing_command_unset(self):
279
config_file = StringIO("")
280
my_config = config.GlobalConfig()
281
my_config._parser = my_config._get_parser(file=config_file)
282
self.assertEqual("gpg", my_config.gpg_signing_command())
285
class TestLocationConfig(TestCase):
287
def test_constructs(self):
288
my_config = config.LocationConfig('http://example.com')
289
self.assertRaises(TypeError, config.LocationConfig)
291
def test_branch_calls_read_filenames(self):
292
# replace the class that is constructured, to check its parameters
293
oldparserclass = config.ConfigParser
294
config.ConfigParser = InstrumentedConfigParser
295
my_config = config.LocationConfig('http://www.example.com')
297
parser = my_config._get_parser()
299
config.ConfigParser = oldparserclass
300
self.failUnless(isinstance(parser, InstrumentedConfigParser))
301
self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
303
def test_get_global_config(self):
304
my_config = config.LocationConfig('http://example.com')
305
global_config = my_config._get_global_config()
306
self.failUnless(isinstance(global_config, config.GlobalConfig))
307
self.failUnless(global_config is my_config._get_global_config())
309
def test__get_section_no_match(self):
310
self.get_location_config('/')
311
self.assertEqual(None, self.my_config._get_section())
313
def test__get_section_exact(self):
314
self.get_location_config('http://www.example.com')
315
self.assertEqual('http://www.example.com',
316
self.my_config._get_section())
318
def test__get_section_suffix_does_not(self):
319
self.get_location_config('http://www.example.com-com')
320
self.assertEqual(None, self.my_config._get_section())
322
def test__get_section_subdir_recursive(self):
323
self.get_location_config('http://www.example.com/com')
324
self.assertEqual('http://www.example.com',
325
self.my_config._get_section())
327
def test__get_section_subdir_matches(self):
328
self.get_location_config('http://www.example.com/useglobal')
329
self.assertEqual('http://www.example.com/useglobal',
330
self.my_config._get_section())
332
def test__get_section_subdir_nonrecursive(self):
333
self.get_location_config(
334
'http://www.example.com/useglobal/childbranch')
335
self.assertEqual('http://www.example.com',
336
self.my_config._get_section())
338
def test__get_section_subdir_trailing_slash(self):
339
self.get_location_config('/b')
340
self.assertEqual('/b/', self.my_config._get_section())
342
def test__get_section_subdir_child(self):
343
self.get_location_config('/a/foo')
344
self.assertEqual('/a/*', self.my_config._get_section())
346
def test__get_section_subdir_child_child(self):
347
self.get_location_config('/a/foo/bar')
348
self.assertEqual('/a/', self.my_config._get_section())
350
def test__get_section_trailing_slash_with_children(self):
351
self.get_location_config('/a/')
352
self.assertEqual('/a/', self.my_config._get_section())
354
def test__get_section_explicit_over_glob(self):
355
self.get_location_config('/a/c')
356
self.assertEqual('/a/c', self.my_config._get_section())
358
def get_location_config(self, location, global_config=None):
359
if global_config is None:
360
global_file = StringIO(sample_config_text)
362
global_file = StringIO(global_config)
363
branches_file = StringIO(sample_branches_text)
364
self.my_config = config.LocationConfig(location)
365
self.my_config._get_parser(branches_file)
366
self.my_config._get_global_config()._get_parser(global_file)
368
def test_location_without_username(self):
369
self.get_location_config('http://www.example.com/useglobal')
370
self.assertEqual('Robert Collins <robertc@example.com>',
371
self.my_config.username())
373
def test_location_not_listed(self):
374
self.get_location_config('/home/robertc/sources')
375
self.assertEqual('Robert Collins <robertc@example.com>',
376
self.my_config.username())
378
def test_overriding_location(self):
379
self.get_location_config('http://www.example.com/foo')
380
self.assertEqual('Robert Collins <robertc@example.org>',
381
self.my_config.username())
383
def test_signatures_not_set(self):
384
self.get_location_config('http://www.example.com',
385
global_config=sample_ignore_signatures)
386
self.assertEqual(config.CHECK_NEVER,
387
self.my_config.signature_checking())
389
def test_signatures_never(self):
390
self.get_location_config('/a/c')
391
self.assertEqual(config.CHECK_NEVER,
392
self.my_config.signature_checking())
394
def test_signatures_when_available(self):
395
self.get_location_config('/a/', global_config=sample_ignore_signatures)
396
self.assertEqual(config.CHECK_IF_POSSIBLE,
397
self.my_config.signature_checking())
399
def test_signatures_always(self):
400
self.get_location_config('/b')
401
self.assertEqual(config.CHECK_ALWAYS,
402
self.my_config.signature_checking())
404
def test_gpg_signing_command(self):
405
self.get_location_config('/b')
406
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
408
def test_gpg_signing_command_missing(self):
409
self.get_location_config('/a')
410
self.assertEqual("false", self.my_config.gpg_signing_command())
413
class TestBranchConfigItems(TestCase):
415
def test_user_id(self):
416
branch = FakeBranch()
417
my_config = config.BranchConfig(branch)
418
self.assertEqual("Robert Collins <robertc@example.net>",
419
my_config._get_user_id())
420
branch.email = "John"
421
self.assertEqual("John", my_config._get_user_id())
423
def test_not_set_in_branch(self):
424
branch = FakeBranch()
425
my_config = config.BranchConfig(branch)
427
config_file = StringIO(sample_config_text)
428
(my_config._get_location_config().
429
_get_global_config()._get_parser(config_file))
430
self.assertEqual("Robert Collins <robertc@example.com>",
431
my_config._get_user_id())
432
branch.email = "John"
433
self.assertEqual("John", my_config._get_user_id())
435
def test_BZREMAIL_OVERRIDES(self):
436
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
437
branch = FakeBranch()
438
my_config = config.BranchConfig(branch)
439
self.assertEqual("Robert Collins <robertc@example.org>",
440
my_config.username())
442
def test_signatures_forced(self):
443
branch = FakeBranch()
444
my_config = config.BranchConfig(branch)
445
config_file = StringIO(sample_always_signatures)
446
(my_config._get_location_config().
447
_get_global_config()._get_parser(config_file))
448
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
450
def test_gpg_signing_command(self):
451
branch = FakeBranch()
452
my_config = config.BranchConfig(branch)
453
config_file = StringIO(sample_config_text)
454
(my_config._get_location_config().
455
_get_global_config()._get_parser(config_file))
456
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())