/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Aaron Bentley
  • Date: 2006-02-21 02:26:30 UTC
  • mto: (1558.1.1 bzr.ab.integration)
  • mto: This revision was merged to the branch mainline in revision 1559.
  • Revision ID: aaron.bentley@utoronto.ca-20060221022630-c74618c305be4ffd
Switched to ConfigObj 4.2.0

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
                      "log_format=short\n"
 
36
                      "user_global_option=something\n")
 
37
 
 
38
 
 
39
sample_always_signatures = ("[DEFAULT]\n"
 
40
                            "check_signatures=require\n")
 
41
 
 
42
 
 
43
sample_ignore_signatures = ("[DEFAULT]\n"
 
44
                            "check_signatures=ignore\n")
 
45
 
 
46
 
 
47
sample_maybe_signatures = ("[DEFAULT]\n"
 
48
                            "check_signatures=check-available\n")
 
49
 
 
50
 
 
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"
 
56
                        "recurse=false\n"
 
57
                        "[/b/]\n"
 
58
                        "check_signatures=require\n"
 
59
                        "# test trailing / matching with no children\n"
 
60
                        "[/a/]\n"
 
61
                        "check_signatures=check-available\n"
 
62
                        "gpg_signing_command=false\n"
 
63
                        "user_local_option=local\n"
 
64
                        "# test trailing / matching\n"
 
65
                        "[/a/*]\n"
 
66
                        "#subdirs will match but not the parent\n"
 
67
                        "recurse=False\n"
 
68
                        "[/a/c]\n"
 
69
                        "check_signatures=ignore\n"
 
70
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
 
71
                        "#testing explicit beats globs\n")
 
72
 
 
73
 
 
74
class InstrumentedConfigObj(object):
 
75
    """A config obj look-enough-alike to record calls made to it."""
 
76
 
 
77
    def __contains__(self, thing):
 
78
        self._calls.append(('__contains__', thing))
 
79
        return False
 
80
 
 
81
    def __getitem__(self, key):
 
82
        self._calls.append(('__getitem__', key))
 
83
        return self
 
84
 
 
85
    def __init__(self, input):
 
86
        self._calls = [('__init__', input)]
 
87
 
 
88
    def __setitem__(self, key, value):
 
89
        self._calls.append(('__setitem__', key, value))
 
90
 
 
91
    def write(self):
 
92
        self._calls.append(('write',))
 
93
 
 
94
 
 
95
class FakeBranch(object):
 
96
 
 
97
    def __init__(self):
 
98
        self.base = "http://example.com/branches/demo"
 
99
        self.control_files = FakeControlFiles()
 
100
 
 
101
 
 
102
class FakeControlFiles(object):
 
103
 
 
104
    def __init__(self):
 
105
        self.email = 'Robert Collins <robertc@example.net>\n'
 
106
 
 
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)
 
113
 
 
114
 
 
115
class InstrumentedConfig(config.Config):
 
116
    """An instrumented config that supplies stubs for template methods."""
 
117
    
 
118
    def __init__(self):
 
119
        super(InstrumentedConfig, self).__init__()
 
120
        self._calls = []
 
121
        self._signatures = config.CHECK_NEVER
 
122
 
 
123
    def _get_user_id(self):
 
124
        self._calls.append('_get_user_id')
 
125
        return "Robert Collins <robert.collins@example.org>"
 
126
 
 
127
    def _get_signature_checking(self):
 
128
        self._calls.append('_get_signature_checking')
 
129
        return self._signatures
 
130
 
 
131
 
 
132
class TestConfig(TestCase):
 
133
 
 
134
    def test_constructs(self):
 
135
        config.Config()
 
136
 
 
137
    def test_no_default_editor(self):
 
138
        self.assertRaises(NotImplementedError, config.Config().get_editor)
 
139
 
 
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)
 
144
 
 
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)
 
150
 
 
151
    def test_signatures_default(self):
 
152
        my_config = config.Config()
 
153
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
154
                         my_config.signature_checking())
 
155
 
 
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)
 
160
 
 
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)
 
167
 
 
168
    def test_gpg_signing_command_default(self):
 
169
        my_config = config.Config()
 
170
        self.assertEqual('gpg', my_config.gpg_signing_command())
 
171
 
 
172
    def test_get_user_option_default(self):
 
173
        my_config = config.Config()
 
174
        self.assertEqual(None, my_config.get_user_option('no_option'))
 
175
 
 
176
    def test_post_commit_default(self):
 
177
        my_config = config.Config()
 
178
        self.assertEqual(None, my_config.post_commit())
 
179
 
 
180
    def test_log_format_default(self):
 
181
        my_config = config.Config()
 
182
        self.assertEqual('long', my_config.log_format())
 
183
 
 
184
 
 
185
class TestConfigPath(TestCase):
 
186
 
 
187
    def setUp(self):
 
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'
 
194
 
 
195
    def tearDown(self):
 
196
        if self.old_home is None:
 
197
            del os.environ['HOME']
 
198
        else:
 
199
            os.environ['HOME'] = self.old_home
 
200
        if self.old_appdata is None:
 
201
            del os.environ['APPDATA']
 
202
        else:
 
203
            os.environ['APPDATA'] = self.old_appdata
 
204
        super(TestConfigPath, self).tearDown()
 
205
    
 
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')
 
210
        else:
 
211
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
 
212
 
 
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')
 
217
        else:
 
218
            self.assertEqual(config.config_filename(),
 
219
                             '/home/bogus/.bazaar/bazaar.conf')
 
220
 
 
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')
 
225
        else:
 
226
            self.assertEqual(config.branches_config_filename(),
 
227
                             '/home/bogus/.bazaar/branches.conf')
 
228
 
 
229
class TestIniConfig(TestCase):
 
230
 
 
231
    def test_contructs(self):
 
232
        my_config = config.IniBasedConfig("nothing")
 
233
 
 
234
    def test_from_fp(self):
 
235
        config_file = StringIO(sample_config_text)
 
236
        my_config = config.IniBasedConfig(None)
 
237
        self.failUnless(
 
238
            isinstance(my_config._get_parser(file=config_file),
 
239
                        ConfigObj))
 
240
 
 
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)
 
246
 
 
247
 
 
248
class TestGetConfig(TestCase):
 
249
 
 
250
    def test_constructs(self):
 
251
        my_config = config.GlobalConfig()
 
252
 
 
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()
 
258
        try:
 
259
            parser = my_config._get_parser()
 
260
        finally:
 
261
            config.ConfigObj = oldparserclass
 
262
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
 
263
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
 
264
 
 
265
 
 
266
class TestBranchConfig(TestCaseInTempDir):
 
267
 
 
268
    def test_constructs(self):
 
269
        branch = FakeBranch()
 
270
        my_config = config.BranchConfig(branch)
 
271
        self.assertRaises(TypeError, config.BranchConfig)
 
272
 
 
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())
 
279
 
 
280
 
 
281
class TestGlobalConfigItems(TestCase):
 
282
 
 
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())
 
289
 
 
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())
 
295
 
 
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())
 
301
 
 
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())
 
309
 
 
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())
 
317
 
 
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())
 
325
 
 
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)
 
330
        return my_config
 
331
 
 
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())
 
336
 
 
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)
 
341
        return my_config
 
342
 
 
343
    def test_gpg_signing_command_unset(self):
 
344
        my_config = self._get_empty_config()
 
345
        self.assertEqual("gpg", my_config.gpg_signing_command())
 
346
 
 
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'))
 
350
 
 
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'))
 
355
        
 
356
    def test_post_commit_default(self):
 
357
        my_config = self._get_sample_config()
 
358
        self.assertEqual(None, my_config.post_commit())
 
359
 
 
360
    def test_configured_logformat(self):
 
361
        my_config = self._get_sample_config()
 
362
        self.assertEqual("short", my_config.log_format())
 
363
 
 
364
 
 
365
class TestLocationConfig(TestCase):
 
366
 
 
367
    def test_constructs(self):
 
368
        my_config = config.LocationConfig('http://example.com')
 
369
        self.assertRaises(TypeError, config.LocationConfig)
 
370
 
 
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.
 
374
        #
 
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')
 
379
        try:
 
380
            parser = my_config._get_parser()
 
381
        finally:
 
382
            config.ConfigObj = oldparserclass
 
383
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
 
384
        self.assertEqual(parser._calls,
 
385
                         [('__init__', config.branches_config_filename())])
 
386
 
 
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())
 
392
 
 
393
    def test__get_section_no_match(self):
 
394
        self.get_location_config('/')
 
395
        self.assertEqual(None, self.my_config._get_section())
 
396
        
 
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())
 
401
   
 
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())
 
405
 
 
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())
 
410
 
 
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())
 
415
 
 
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())
 
421
 
 
422
    def test__get_section_subdir_trailing_slash(self):
 
423
        self.get_location_config('/b')
 
424
        self.assertEqual('/b/', self.my_config._get_section())
 
425
 
 
426
    def test__get_section_subdir_child(self):
 
427
        self.get_location_config('/a/foo')
 
428
        self.assertEqual('/a/*', self.my_config._get_section())
 
429
 
 
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())
 
433
 
 
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())
 
437
 
 
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())
 
441
 
 
442
    def get_location_config(self, location, global_config=None):
 
443
        if global_config is None:
 
444
            global_file = StringIO(sample_config_text)
 
445
        else:
 
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)
 
451
 
 
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())
 
456
 
 
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())
 
461
 
 
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())
 
466
 
 
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())
 
472
 
 
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())
 
477
        
 
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())
 
482
        
 
483
    def test_signatures_always(self):
 
484
        self.get_location_config('/b')
 
485
        self.assertEqual(config.CHECK_ALWAYS,
 
486
                         self.my_config.signature_checking())
 
487
        
 
488
    def test_gpg_signing_command(self):
 
489
        self.get_location_config('/b')
 
490
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
 
491
 
 
492
    def test_gpg_signing_command_missing(self):
 
493
        self.get_location_config('/a')
 
494
        self.assertEqual("false", self.my_config.gpg_signing_command())
 
495
 
 
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'))
 
500
 
 
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'))
 
505
        
 
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())
 
510
 
 
511
 
 
512
class TestLocationConfig(TestCaseInTempDir):
 
513
 
 
514
    def get_location_config(self, location, global_config=None):
 
515
        if global_config is None:
 
516
            global_file = StringIO(sample_config_text)
 
517
        else:
 
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)
 
523
 
 
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
 
528
 
 
529
        real_mkdir = os.mkdir
 
530
        self.created = False
 
531
        def checked_mkdir(path, mode=0777):
 
532
            self.log('making directory: %s', path)
 
533
            real_mkdir(path, mode)
 
534
            self.created = True
 
535
 
 
536
        os.mkdir = checked_mkdir
 
537
        try:
 
538
            self.my_config.set_user_option('foo', 'bar')
 
539
        finally:
 
540
            os.mkdir = real_mkdir
 
541
 
 
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'),
 
548
                          ('write',)],
 
549
                         record._calls[1:])
 
550
 
 
551
 
 
552
class TestBranchConfigItems(TestCase):
 
553
 
 
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())
 
561
 
 
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())
 
573
 
 
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())
 
580
    
 
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())
 
588
 
 
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())
 
596
 
 
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'))
 
605
 
 
606
    def test_post_commit_default(self):
 
607
        branch = FakeBranch()
 
608
        branch.base='/a/c'
 
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())
 
617
 
 
618
 
 
619
class TestMailAddressExtraction(TestCase):
 
620
 
 
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')