~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Martin Pool
  • Date: 2005-04-28 07:24:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050428072453-7b99afa993a1e549
todo

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
3
 
#
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.
8
 
#
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.
13
 
#
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
17
 
 
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
22
 
import os
23
 
import sys
24
 
 
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
29
 
 
30
 
 
31
 
sample_config_text = ("[DEFAULT]\n"
32
 
                      "email=Robert Collins <robertc@example.com>\n"
33
 
                      "editor=vim\n"
34
 
                      "gpg_signing_command=gnome-gpg\n"
35
 
                      "user_global_option=something\n")
36
 
 
37
 
 
38
 
sample_always_signatures = ("[DEFAULT]\n"
39
 
                            "check_signatures=require\n")
40
 
 
41
 
 
42
 
sample_ignore_signatures = ("[DEFAULT]\n"
43
 
                            "check_signatures=ignore\n")
44
 
 
45
 
 
46
 
sample_maybe_signatures = ("[DEFAULT]\n"
47
 
                            "check_signatures=check-available\n")
48
 
 
49
 
 
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"
55
 
                        "recurse=false\n"
56
 
                        "[/b/]\n"
57
 
                        "check_signatures=require\n"
58
 
                        "# test trailing / matching with no children\n"
59
 
                        "[/a/]\n"
60
 
                        "check_signatures=check-available\n"
61
 
                        "gpg_signing_command=false\n"
62
 
                        "user_local_option=local\n"
63
 
                        "# test trailing / matching\n"
64
 
                        "[/a/*]\n"
65
 
                        "#subdirs will match but not the parent\n"
66
 
                        "recurse=False\n"
67
 
                        "[/a/c]\n"
68
 
                        "check_signatures=ignore\n"
69
 
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
70
 
                        "#testing explicit beats globs\n")
71
 
 
72
 
 
73
 
class InstrumentedConfigObj(object):
74
 
    """A config obj look-enough-alike to record calls made to it."""
75
 
 
76
 
    def __contains__(self, thing):
77
 
        self._calls.append(('__contains__', thing))
78
 
        return False
79
 
 
80
 
    def __getitem__(self, key):
81
 
        self._calls.append(('__getitem__', key))
82
 
        return self
83
 
 
84
 
    def __init__(self, input):
85
 
        self._calls = [('__init__', input)]
86
 
 
87
 
    def __setitem__(self, key, value):
88
 
        self._calls.append(('__setitem__', key, value))
89
 
 
90
 
    def write(self):
91
 
        self._calls.append(('write',))
92
 
 
93
 
 
94
 
class FakeBranch(object):
95
 
 
96
 
    def __init__(self):
97
 
        self.base = "http://example.com/branches/demo"
98
 
        self.control_files = FakeControlFiles()
99
 
 
100
 
 
101
 
class FakeControlFiles(object):
102
 
 
103
 
    def __init__(self):
104
 
        self.email = 'Robert Collins <robertc@example.net>\n'
105
 
 
106
 
    def controlfile(self, filename, mode):
107
 
        if filename != 'email':
108
 
            raise NotImplementedError
109
 
        if self.email is not None:
110
 
            return StringIO(self.email)
111
 
        raise errors.NoSuchFile(filename)
112
 
 
113
 
 
114
 
class InstrumentedConfig(config.Config):
115
 
    """An instrumented config that supplies stubs for template methods."""
116
 
    
117
 
    def __init__(self):
118
 
        super(InstrumentedConfig, self).__init__()
119
 
        self._calls = []
120
 
        self._signatures = config.CHECK_NEVER
121
 
 
122
 
    def _get_user_id(self):
123
 
        self._calls.append('_get_user_id')
124
 
        return "Robert Collins <robert.collins@example.org>"
125
 
 
126
 
    def _get_signature_checking(self):
127
 
        self._calls.append('_get_signature_checking')
128
 
        return self._signatures
129
 
 
130
 
 
131
 
class TestConfig(TestCase):
132
 
 
133
 
    def test_constructs(self):
134
 
        config.Config()
135
 
 
136
 
    def test_no_default_editor(self):
137
 
        self.assertRaises(NotImplementedError, config.Config().get_editor)
138
 
 
139
 
    def test_user_email(self):
140
 
        my_config = InstrumentedConfig()
141
 
        self.assertEqual('robert.collins@example.org', my_config.user_email())
142
 
        self.assertEqual(['_get_user_id'], my_config._calls)
143
 
 
144
 
    def test_username(self):
145
 
        my_config = InstrumentedConfig()
146
 
        self.assertEqual('Robert Collins <robert.collins@example.org>',
147
 
                         my_config.username())
148
 
        self.assertEqual(['_get_user_id'], my_config._calls)
149
 
 
150
 
    def test_signatures_default(self):
151
 
        my_config = config.Config()
152
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
153
 
                         my_config.signature_checking())
154
 
 
155
 
    def test_signatures_template_method(self):
156
 
        my_config = InstrumentedConfig()
157
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
158
 
        self.assertEqual(['_get_signature_checking'], my_config._calls)
159
 
 
160
 
    def test_signatures_template_method_none(self):
161
 
        my_config = InstrumentedConfig()
162
 
        my_config._signatures = None
163
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
164
 
                         my_config.signature_checking())
165
 
        self.assertEqual(['_get_signature_checking'], my_config._calls)
166
 
 
167
 
    def test_gpg_signing_command_default(self):
168
 
        my_config = config.Config()
169
 
        self.assertEqual('gpg', my_config.gpg_signing_command())
170
 
 
171
 
    def test_get_user_option_default(self):
172
 
        my_config = config.Config()
173
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
174
 
 
175
 
    def test_post_commit_default(self):
176
 
        my_config = config.Config()
177
 
        self.assertEqual(None, my_config.post_commit())
178
 
 
179
 
 
180
 
class TestConfigPath(TestCase):
181
 
 
182
 
    def setUp(self):
183
 
        super(TestConfigPath, self).setUp()
184
 
        self.old_home = os.environ.get('HOME', None)
185
 
        self.old_appdata = os.environ.get('APPDATA', None)
186
 
        os.environ['HOME'] = '/home/bogus'
187
 
        os.environ['APPDATA'] = \
188
 
            r'C:\Documents and Settings\bogus\Application Data'
189
 
 
190
 
    def tearDown(self):
191
 
        if self.old_home is None:
192
 
            del os.environ['HOME']
193
 
        else:
194
 
            os.environ['HOME'] = self.old_home
195
 
        if self.old_appdata is None:
196
 
            del os.environ['APPDATA']
197
 
        else:
198
 
            os.environ['APPDATA'] = self.old_appdata
199
 
        super(TestConfigPath, self).tearDown()
200
 
    
201
 
    def test_config_dir(self):
202
 
        if sys.platform == 'win32':
203
 
            self.assertEqual(config.config_dir(), 
204
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
205
 
        else:
206
 
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
207
 
 
208
 
    def test_config_filename(self):
209
 
        if sys.platform == 'win32':
210
 
            self.assertEqual(config.config_filename(), 
211
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
212
 
        else:
213
 
            self.assertEqual(config.config_filename(),
214
 
                             '/home/bogus/.bazaar/bazaar.conf')
215
 
 
216
 
    def test_branches_config_filename(self):
217
 
        if sys.platform == 'win32':
218
 
            self.assertEqual(config.branches_config_filename(), 
219
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
220
 
        else:
221
 
            self.assertEqual(config.branches_config_filename(),
222
 
                             '/home/bogus/.bazaar/branches.conf')
223
 
 
224
 
class TestIniConfig(TestCase):
225
 
 
226
 
    def test_contructs(self):
227
 
        my_config = config.IniBasedConfig("nothing")
228
 
 
229
 
    def test_from_fp(self):
230
 
        config_file = StringIO(sample_config_text)
231
 
        my_config = config.IniBasedConfig(None)
232
 
        self.failUnless(
233
 
            isinstance(my_config._get_parser(file=config_file),
234
 
                        ConfigObj))
235
 
 
236
 
    def test_cached(self):
237
 
        config_file = StringIO(sample_config_text)
238
 
        my_config = config.IniBasedConfig(None)
239
 
        parser = my_config._get_parser(file=config_file)
240
 
        self.failUnless(my_config._get_parser() is parser)
241
 
 
242
 
 
243
 
class TestGetConfig(TestCase):
244
 
 
245
 
    def test_constructs(self):
246
 
        my_config = config.GlobalConfig()
247
 
 
248
 
    def test_calls_read_filenames(self):
249
 
        # replace the class that is constructured, to check its parameters
250
 
        oldparserclass = config.ConfigObj
251
 
        config.ConfigObj = InstrumentedConfigObj
252
 
        my_config = config.GlobalConfig()
253
 
        try:
254
 
            parser = my_config._get_parser()
255
 
        finally:
256
 
            config.ConfigObj = oldparserclass
257
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
258
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
259
 
 
260
 
 
261
 
class TestBranchConfig(TestCaseInTempDir):
262
 
 
263
 
    def test_constructs(self):
264
 
        branch = FakeBranch()
265
 
        my_config = config.BranchConfig(branch)
266
 
        self.assertRaises(TypeError, config.BranchConfig)
267
 
 
268
 
    def test_get_location_config(self):
269
 
        branch = FakeBranch()
270
 
        my_config = config.BranchConfig(branch)
271
 
        location_config = my_config._get_location_config()
272
 
        self.assertEqual(branch.base, location_config.location)
273
 
        self.failUnless(location_config is my_config._get_location_config())
274
 
 
275
 
 
276
 
class TestGlobalConfigItems(TestCase):
277
 
 
278
 
    def test_user_id(self):
279
 
        config_file = StringIO(sample_config_text)
280
 
        my_config = config.GlobalConfig()
281
 
        my_config._parser = my_config._get_parser(file=config_file)
282
 
        self.assertEqual("Robert Collins <robertc@example.com>",
283
 
                         my_config._get_user_id())
284
 
 
285
 
    def test_absent_user_id(self):
286
 
        config_file = StringIO("")
287
 
        my_config = config.GlobalConfig()
288
 
        my_config._parser = my_config._get_parser(file=config_file)
289
 
        self.assertEqual(None, my_config._get_user_id())
290
 
 
291
 
    def test_configured_editor(self):
292
 
        config_file = StringIO(sample_config_text)
293
 
        my_config = config.GlobalConfig()
294
 
        my_config._parser = my_config._get_parser(file=config_file)
295
 
        self.assertEqual("vim", my_config.get_editor())
296
 
 
297
 
    def test_signatures_always(self):
298
 
        config_file = StringIO(sample_always_signatures)
299
 
        my_config = config.GlobalConfig()
300
 
        my_config._parser = my_config._get_parser(file=config_file)
301
 
        self.assertEqual(config.CHECK_ALWAYS,
302
 
                         my_config.signature_checking())
303
 
        self.assertEqual(True, my_config.signature_needed())
304
 
 
305
 
    def test_signatures_if_possible(self):
306
 
        config_file = StringIO(sample_maybe_signatures)
307
 
        my_config = config.GlobalConfig()
308
 
        my_config._parser = my_config._get_parser(file=config_file)
309
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
310
 
                         my_config.signature_checking())
311
 
        self.assertEqual(False, my_config.signature_needed())
312
 
 
313
 
    def test_signatures_ignore(self):
314
 
        config_file = StringIO(sample_ignore_signatures)
315
 
        my_config = config.GlobalConfig()
316
 
        my_config._parser = my_config._get_parser(file=config_file)
317
 
        self.assertEqual(config.CHECK_NEVER,
318
 
                         my_config.signature_checking())
319
 
        self.assertEqual(False, my_config.signature_needed())
320
 
 
321
 
    def _get_sample_config(self):
322
 
        config_file = StringIO(sample_config_text)
323
 
        my_config = config.GlobalConfig()
324
 
        my_config._parser = my_config._get_parser(file=config_file)
325
 
        return my_config
326
 
 
327
 
    def test_gpg_signing_command(self):
328
 
        my_config = self._get_sample_config()
329
 
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
330
 
        self.assertEqual(False, my_config.signature_needed())
331
 
 
332
 
    def _get_empty_config(self):
333
 
        config_file = StringIO("")
334
 
        my_config = config.GlobalConfig()
335
 
        my_config._parser = my_config._get_parser(file=config_file)
336
 
        return my_config
337
 
 
338
 
    def test_gpg_signing_command_unset(self):
339
 
        my_config = self._get_empty_config()
340
 
        self.assertEqual("gpg", my_config.gpg_signing_command())
341
 
 
342
 
    def test_get_user_option_default(self):
343
 
        my_config = self._get_empty_config()
344
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
345
 
 
346
 
    def test_get_user_option_global(self):
347
 
        my_config = self._get_sample_config()
348
 
        self.assertEqual("something",
349
 
                         my_config.get_user_option('user_global_option'))
350
 
        
351
 
    def test_post_commit_default(self):
352
 
        my_config = self._get_sample_config()
353
 
        self.assertEqual(None, my_config.post_commit())
354
 
 
355
 
 
356
 
class TestLocationConfig(TestCase):
357
 
 
358
 
    def test_constructs(self):
359
 
        my_config = config.LocationConfig('http://example.com')
360
 
        self.assertRaises(TypeError, config.LocationConfig)
361
 
 
362
 
    def test_branch_calls_read_filenames(self):
363
 
        # This is testing the correct file names are provided.
364
 
        # TODO: consolidate with the test for GlobalConfigs filename checks.
365
 
        #
366
 
        # replace the class that is constructured, to check its parameters
367
 
        oldparserclass = config.ConfigObj
368
 
        config.ConfigObj = InstrumentedConfigObj
369
 
        my_config = config.LocationConfig('http://www.example.com')
370
 
        try:
371
 
            parser = my_config._get_parser()
372
 
        finally:
373
 
            config.ConfigObj = oldparserclass
374
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
375
 
        self.assertEqual(parser._calls,
376
 
                         [('__init__', config.branches_config_filename())])
377
 
 
378
 
    def test_get_global_config(self):
379
 
        my_config = config.LocationConfig('http://example.com')
380
 
        global_config = my_config._get_global_config()
381
 
        self.failUnless(isinstance(global_config, config.GlobalConfig))
382
 
        self.failUnless(global_config is my_config._get_global_config())
383
 
 
384
 
    def test__get_section_no_match(self):
385
 
        self.get_location_config('/')
386
 
        self.assertEqual(None, self.my_config._get_section())
387
 
        
388
 
    def test__get_section_exact(self):
389
 
        self.get_location_config('http://www.example.com')
390
 
        self.assertEqual('http://www.example.com',
391
 
                         self.my_config._get_section())
392
 
   
393
 
    def test__get_section_suffix_does_not(self):
394
 
        self.get_location_config('http://www.example.com-com')
395
 
        self.assertEqual(None, self.my_config._get_section())
396
 
 
397
 
    def test__get_section_subdir_recursive(self):
398
 
        self.get_location_config('http://www.example.com/com')
399
 
        self.assertEqual('http://www.example.com',
400
 
                         self.my_config._get_section())
401
 
 
402
 
    def test__get_section_subdir_matches(self):
403
 
        self.get_location_config('http://www.example.com/useglobal')
404
 
        self.assertEqual('http://www.example.com/useglobal',
405
 
                         self.my_config._get_section())
406
 
 
407
 
    def test__get_section_subdir_nonrecursive(self):
408
 
        self.get_location_config(
409
 
            'http://www.example.com/useglobal/childbranch')
410
 
        self.assertEqual('http://www.example.com',
411
 
                         self.my_config._get_section())
412
 
 
413
 
    def test__get_section_subdir_trailing_slash(self):
414
 
        self.get_location_config('/b')
415
 
        self.assertEqual('/b/', self.my_config._get_section())
416
 
 
417
 
    def test__get_section_subdir_child(self):
418
 
        self.get_location_config('/a/foo')
419
 
        self.assertEqual('/a/*', self.my_config._get_section())
420
 
 
421
 
    def test__get_section_subdir_child_child(self):
422
 
        self.get_location_config('/a/foo/bar')
423
 
        self.assertEqual('/a/', self.my_config._get_section())
424
 
 
425
 
    def test__get_section_trailing_slash_with_children(self):
426
 
        self.get_location_config('/a/')
427
 
        self.assertEqual('/a/', self.my_config._get_section())
428
 
 
429
 
    def test__get_section_explicit_over_glob(self):
430
 
        self.get_location_config('/a/c')
431
 
        self.assertEqual('/a/c', self.my_config._get_section())
432
 
 
433
 
    def get_location_config(self, location, global_config=None):
434
 
        if global_config is None:
435
 
            global_file = StringIO(sample_config_text)
436
 
        else:
437
 
            global_file = StringIO(global_config)
438
 
        branches_file = StringIO(sample_branches_text)
439
 
        self.my_config = config.LocationConfig(location)
440
 
        self.my_config._get_parser(branches_file)
441
 
        self.my_config._get_global_config()._get_parser(global_file)
442
 
 
443
 
    def test_location_without_username(self):
444
 
        self.get_location_config('http://www.example.com/useglobal')
445
 
        self.assertEqual('Robert Collins <robertc@example.com>',
446
 
                         self.my_config.username())
447
 
 
448
 
    def test_location_not_listed(self):
449
 
        self.get_location_config('/home/robertc/sources')
450
 
        self.assertEqual('Robert Collins <robertc@example.com>',
451
 
                         self.my_config.username())
452
 
 
453
 
    def test_overriding_location(self):
454
 
        self.get_location_config('http://www.example.com/foo')
455
 
        self.assertEqual('Robert Collins <robertc@example.org>',
456
 
                         self.my_config.username())
457
 
 
458
 
    def test_signatures_not_set(self):
459
 
        self.get_location_config('http://www.example.com',
460
 
                                 global_config=sample_ignore_signatures)
461
 
        self.assertEqual(config.CHECK_NEVER,
462
 
                         self.my_config.signature_checking())
463
 
 
464
 
    def test_signatures_never(self):
465
 
        self.get_location_config('/a/c')
466
 
        self.assertEqual(config.CHECK_NEVER,
467
 
                         self.my_config.signature_checking())
468
 
        
469
 
    def test_signatures_when_available(self):
470
 
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
471
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
472
 
                         self.my_config.signature_checking())
473
 
        
474
 
    def test_signatures_always(self):
475
 
        self.get_location_config('/b')
476
 
        self.assertEqual(config.CHECK_ALWAYS,
477
 
                         self.my_config.signature_checking())
478
 
        
479
 
    def test_gpg_signing_command(self):
480
 
        self.get_location_config('/b')
481
 
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
482
 
 
483
 
    def test_gpg_signing_command_missing(self):
484
 
        self.get_location_config('/a')
485
 
        self.assertEqual("false", self.my_config.gpg_signing_command())
486
 
 
487
 
    def test_get_user_option_global(self):
488
 
        self.get_location_config('/a')
489
 
        self.assertEqual('something',
490
 
                         self.my_config.get_user_option('user_global_option'))
491
 
 
492
 
    def test_get_user_option_local(self):
493
 
        self.get_location_config('/a')
494
 
        self.assertEqual('local',
495
 
                         self.my_config.get_user_option('user_local_option'))
496
 
        
497
 
    def test_post_commit_default(self):
498
 
        self.get_location_config('/a/c')
499
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
500
 
                         self.my_config.post_commit())
501
 
 
502
 
 
503
 
class TestLocationConfig(TestCaseInTempDir):
504
 
 
505
 
    def get_location_config(self, location, global_config=None):
506
 
        if global_config is None:
507
 
            global_file = StringIO(sample_config_text)
508
 
        else:
509
 
            global_file = StringIO(global_config)
510
 
        branches_file = StringIO(sample_branches_text)
511
 
        self.my_config = config.LocationConfig(location)
512
 
        self.my_config._get_parser(branches_file)
513
 
        self.my_config._get_global_config()._get_parser(global_file)
514
 
 
515
 
    def test_set_user_setting_sets_and_saves(self):
516
 
        self.get_location_config('/a/c')
517
 
        record = InstrumentedConfigObj("foo")
518
 
        self.my_config._parser = record
519
 
 
520
 
        real_mkdir = os.mkdir
521
 
        self.created = False
522
 
        def checked_mkdir(path, mode=0777):
523
 
            self.log('making directory: %s', path)
524
 
            real_mkdir(path, mode)
525
 
            self.created = True
526
 
 
527
 
        os.mkdir = checked_mkdir
528
 
        try:
529
 
            self.my_config.set_user_option('foo', 'bar')
530
 
        finally:
531
 
            os.mkdir = real_mkdir
532
 
 
533
 
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
534
 
        self.assertEqual([('__contains__', '/a/c'),
535
 
                          ('__contains__', '/a/c/'),
536
 
                          ('__setitem__', '/a/c', {}),
537
 
                          ('__getitem__', '/a/c'),
538
 
                          ('__setitem__', 'foo', 'bar'),
539
 
                          ('write',)],
540
 
                         record._calls[1:])
541
 
 
542
 
 
543
 
class TestBranchConfigItems(TestCase):
544
 
 
545
 
    def test_user_id(self):
546
 
        branch = FakeBranch()
547
 
        my_config = config.BranchConfig(branch)
548
 
        self.assertEqual("Robert Collins <robertc@example.net>",
549
 
                         my_config._get_user_id())
550
 
        branch.control_files.email = "John"
551
 
        self.assertEqual("John", my_config._get_user_id())
552
 
 
553
 
    def test_not_set_in_branch(self):
554
 
        branch = FakeBranch()
555
 
        my_config = config.BranchConfig(branch)
556
 
        branch.control_files.email = None
557
 
        config_file = StringIO(sample_config_text)
558
 
        (my_config._get_location_config().
559
 
            _get_global_config()._get_parser(config_file))
560
 
        self.assertEqual("Robert Collins <robertc@example.com>",
561
 
                         my_config._get_user_id())
562
 
        branch.control_files.email = "John"
563
 
        self.assertEqual("John", my_config._get_user_id())
564
 
 
565
 
    def test_BZREMAIL_OVERRIDES(self):
566
 
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
567
 
        branch = FakeBranch()
568
 
        my_config = config.BranchConfig(branch)
569
 
        self.assertEqual("Robert Collins <robertc@example.org>",
570
 
                         my_config.username())
571
 
    
572
 
    def test_signatures_forced(self):
573
 
        branch = FakeBranch()
574
 
        my_config = config.BranchConfig(branch)
575
 
        config_file = StringIO(sample_always_signatures)
576
 
        (my_config._get_location_config().
577
 
            _get_global_config()._get_parser(config_file))
578
 
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
579
 
 
580
 
    def test_gpg_signing_command(self):
581
 
        branch = FakeBranch()
582
 
        my_config = config.BranchConfig(branch)
583
 
        config_file = StringIO(sample_config_text)
584
 
        (my_config._get_location_config().
585
 
            _get_global_config()._get_parser(config_file))
586
 
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
587
 
 
588
 
    def test_get_user_option_global(self):
589
 
        branch = FakeBranch()
590
 
        my_config = config.BranchConfig(branch)
591
 
        config_file = StringIO(sample_config_text)
592
 
        (my_config._get_location_config().
593
 
            _get_global_config()._get_parser(config_file))
594
 
        self.assertEqual('something',
595
 
                         my_config.get_user_option('user_global_option'))
596
 
 
597
 
    def test_post_commit_default(self):
598
 
        branch = FakeBranch()
599
 
        branch.base='/a/c'
600
 
        my_config = config.BranchConfig(branch)
601
 
        config_file = StringIO(sample_config_text)
602
 
        (my_config._get_location_config().
603
 
            _get_global_config()._get_parser(config_file))
604
 
        branch_file = StringIO(sample_branches_text)
605
 
        my_config._get_location_config()._get_parser(branch_file)
606
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
607
 
                         my_config.post_commit())
608
 
 
609
 
 
610
 
class TestMailAddressExtraction(TestCase):
611
 
 
612
 
    def test_extract_email_address(self):
613
 
        self.assertEqual('jane@test.com',
614
 
                         config.extract_email_address('Jane <jane@test.com>'))
615
 
        self.assertRaises(errors.BzrError,
616
 
                          config.extract_email_address, 'Jane Tester')