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())
279
class TestLocationConfig(TestCase):
281
def test_constructs(self):
282
my_config = config.LocationConfig('http://example.com')
283
self.assertRaises(TypeError, config.LocationConfig)
285
def test_branch_calls_read_filenames(self):
286
# replace the class that is constructured, to check its parameters
287
oldparserclass = config.ConfigParser
288
config.ConfigParser = InstrumentedConfigParser
289
my_config = config.LocationConfig('http://www.example.com')
291
parser = my_config._get_parser()
293
config.ConfigParser = oldparserclass
294
self.failUnless(isinstance(parser, InstrumentedConfigParser))
295
self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
297
def test_get_global_config(self):
298
my_config = config.LocationConfig('http://example.com')
299
global_config = my_config._get_global_config()
300
self.failUnless(isinstance(global_config, config.GlobalConfig))
301
self.failUnless(global_config is my_config._get_global_config())
303
def test__get_section_no_match(self):
304
self.get_location_config('/')
305
self.assertEqual(None, self.my_config._get_section())
307
def test__get_section_exact(self):
308
self.get_location_config('http://www.example.com')
309
self.assertEqual('http://www.example.com',
310
self.my_config._get_section())
312
def test__get_section_suffix_does_not(self):
313
self.get_location_config('http://www.example.com-com')
314
self.assertEqual(None, self.my_config._get_section())
316
def test__get_section_subdir_recursive(self):
317
self.get_location_config('http://www.example.com/com')
318
self.assertEqual('http://www.example.com',
319
self.my_config._get_section())
321
def test__get_section_subdir_matches(self):
322
self.get_location_config('http://www.example.com/useglobal')
323
self.assertEqual('http://www.example.com/useglobal',
324
self.my_config._get_section())
326
def test__get_section_subdir_nonrecursive(self):
327
self.get_location_config(
328
'http://www.example.com/useglobal/childbranch')
329
self.assertEqual('http://www.example.com',
330
self.my_config._get_section())
332
def test__get_section_subdir_trailing_slash(self):
333
self.get_location_config('/b')
334
self.assertEqual('/b/', self.my_config._get_section())
336
def test__get_section_subdir_child(self):
337
self.get_location_config('/a/foo')
338
self.assertEqual('/a/*', self.my_config._get_section())
340
def test__get_section_subdir_child_child(self):
341
self.get_location_config('/a/foo/bar')
342
self.assertEqual('/a/', self.my_config._get_section())
344
def test__get_section_trailing_slash_with_children(self):
345
self.get_location_config('/a/')
346
self.assertEqual('/a/', self.my_config._get_section())
348
def test__get_section_explicit_over_glob(self):
349
self.get_location_config('/a/c')
350
self.assertEqual('/a/c', self.my_config._get_section())
352
def get_location_config(self, location, global_config=None):
353
if global_config is None:
354
global_file = StringIO(sample_config_text)
356
global_file = StringIO(global_config)
357
branches_file = StringIO(sample_branches_text)
358
self.my_config = config.LocationConfig(location)
359
self.my_config._get_parser(branches_file)
360
self.my_config._get_global_config()._get_parser(global_file)
362
def test_location_without_username(self):
363
self.get_location_config('http://www.example.com/useglobal')
364
self.assertEqual('Robert Collins <robertc@example.com>',
365
self.my_config.username())
367
def test_location_not_listed(self):
368
self.get_location_config('/home/robertc/sources')
369
self.assertEqual('Robert Collins <robertc@example.com>',
370
self.my_config.username())
372
def test_overriding_location(self):
373
self.get_location_config('http://www.example.com/foo')
374
self.assertEqual('Robert Collins <robertc@example.org>',
375
self.my_config.username())
377
def test_signatures_not_set(self):
378
self.get_location_config('http://www.example.com',
379
global_config=sample_ignore_signatures)
380
self.assertEqual(config.CHECK_NEVER,
381
self.my_config.signature_checking())
383
def test_signatures_never(self):
384
self.get_location_config('/a/c')
385
self.assertEqual(config.CHECK_NEVER,
386
self.my_config.signature_checking())
388
def test_signatures_when_available(self):
389
self.get_location_config('/a/', global_config=sample_ignore_signatures)
390
self.assertEqual(config.CHECK_IF_POSSIBLE,
391
self.my_config.signature_checking())
393
def test_signatures_always(self):
394
self.get_location_config('/b')
395
self.assertEqual(config.CHECK_ALWAYS,
396
self.my_config.signature_checking())
398
def test_gpg_signing_command(self):
399
self.get_location_config('/b')
400
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
402
def test_gpg_signing_command_missing(self):
403
self.get_location_config('/a')
404
self.assertEqual("false", self.my_config.gpg_signing_command())
407
class TestBranchConfigItems(TestCase):
409
def test_user_id(self):
410
branch = FakeBranch()
411
my_config = config.BranchConfig(branch)
412
self.assertEqual("Robert Collins <robertc@example.net>",
413
my_config._get_user_id())
414
branch.email = "John"
415
self.assertEqual("John", my_config._get_user_id())
417
def test_not_set_in_branch(self):
418
branch = FakeBranch()
419
my_config = config.BranchConfig(branch)
421
config_file = StringIO(sample_config_text)
422
(my_config._get_location_config().
423
_get_global_config()._get_parser(config_file))
424
self.assertEqual("Robert Collins <robertc@example.com>",
425
my_config._get_user_id())
426
branch.email = "John"
427
self.assertEqual("John", my_config._get_user_id())
429
def test_BZREMAIL_OVERRIDES(self):
430
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
431
branch = FakeBranch()
432
my_config = config.BranchConfig(branch)
433
self.assertEqual("Robert Collins <robertc@example.org>",
434
my_config.username())
436
def test_signatures_forced(self):
437
branch = FakeBranch()
438
my_config = config.BranchConfig(branch)
439
config_file = StringIO(sample_always_signatures)
440
(my_config._get_location_config().
441
_get_global_config()._get_parser(config_file))
442
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
444
def test_gpg_signing_command(self):
445
branch = FakeBranch()
446
my_config = config.BranchConfig(branch)
447
config_file = StringIO(sample_config_text)
448
(my_config._get_location_config().
449
_get_global_config()._get_parser(config_file))
450
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())