/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

Treated config files as utf-8

Show diffs side-by-side

added added

removed removed

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