~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Aaron Bentley
  • Date: 2006-06-14 19:45:57 UTC
  • mto: This revision was merged to the branch mainline in revision 1777.
  • Revision ID: abentley@panoramicfeedback.com-20060614194557-6b499aa1cf03f7e6
Use create_signature for signing policy, deprecate check_signatures for this

Show diffs side-by-side

added added

removed removed

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