~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-08-23 00:18:12 UTC
  • mfrom: (2743.1.5 misc-fixen)
  • Revision ID: pqm@pqm.ubuntu.com-20070823001812-eermtd2vfr5o6b7o
(robertc) Several minor miscellaneous code cleanup fixes. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
23
23
import sys
24
24
 
25
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")
 
26
from bzrlib import (
 
27
    config,
 
28
    errors,
 
29
    osutils,
 
30
    mail_client,
 
31
    urlutils,
 
32
    trace,
 
33
    )
 
34
from bzrlib.branch import Branch
 
35
from bzrlib.bzrdir import BzrDir
 
36
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
 
37
 
 
38
 
 
39
sample_long_alias="log -r-15..-1 --line"
 
40
sample_config_text = u"""
 
41
[DEFAULT]
 
42
email=Erik B\u00e5gfors <erik@bagfors.nu>
 
43
editor=vim
 
44
gpg_signing_command=gnome-gpg
 
45
log_format=short
 
46
user_global_option=something
 
47
[ALIASES]
 
48
h=help
 
49
ll=""" + sample_long_alias + "\n"
 
50
 
 
51
 
 
52
sample_always_signatures = """
 
53
[DEFAULT]
 
54
check_signatures=ignore
 
55
create_signatures=always
 
56
"""
 
57
 
 
58
sample_ignore_signatures = """
 
59
[DEFAULT]
 
60
check_signatures=require
 
61
create_signatures=never
 
62
"""
 
63
 
 
64
sample_maybe_signatures = """
 
65
[DEFAULT]
 
66
check_signatures=ignore
 
67
create_signatures=when-required
 
68
"""
 
69
 
 
70
sample_branches_text = """
 
71
[http://www.example.com]
 
72
# Top level policy
 
73
email=Robert Collins <robertc@example.org>
 
74
normal_option = normal
 
75
appendpath_option = append
 
76
appendpath_option:policy = appendpath
 
77
norecurse_option = norecurse
 
78
norecurse_option:policy = norecurse
 
79
[http://www.example.com/ignoreparent]
 
80
# different project: ignore parent dir config
 
81
ignore_parents=true
 
82
[http://www.example.com/norecurse]
 
83
# configuration items that only apply to this dir
 
84
recurse=false
 
85
normal_option = norecurse
 
86
[http://www.example.com/dir]
 
87
appendpath_option = normal
 
88
[/b/]
 
89
check_signatures=require
 
90
# test trailing / matching with no children
 
91
[/a/]
 
92
check_signatures=check-available
 
93
gpg_signing_command=false
 
94
user_local_option=local
 
95
# test trailing / matching
 
96
[/a/*]
 
97
#subdirs will match but not the parent
 
98
[/a/c]
 
99
check_signatures=ignore
 
100
post_commit=bzrlib.tests.test_config.post_commit
 
101
#testing explicit beats globs
 
102
"""
71
103
 
72
104
 
73
105
class InstrumentedConfigObj(object):
81
113
        self._calls.append(('__getitem__', key))
82
114
        return self
83
115
 
84
 
    def __init__(self, input):
85
 
        self._calls = [('__init__', input)]
 
116
    def __init__(self, input, encoding=None):
 
117
        self._calls = [('__init__', input, encoding)]
86
118
 
87
119
    def __setitem__(self, key, value):
88
120
        self._calls.append(('__setitem__', key, value))
89
121
 
90
 
    def write(self):
 
122
    def __delitem__(self, key):
 
123
        self._calls.append(('__delitem__', key))
 
124
 
 
125
    def keys(self):
 
126
        self._calls.append(('keys',))
 
127
        return []
 
128
 
 
129
    def write(self, arg):
91
130
        self._calls.append(('write',))
92
131
 
 
132
    def as_bool(self, value):
 
133
        self._calls.append(('as_bool', value))
 
134
        return False
 
135
 
 
136
    def get_value(self, section, name):
 
137
        self._calls.append(('get_value', section, name))
 
138
        return None
 
139
 
93
140
 
94
141
class FakeBranch(object):
95
142
 
96
 
    def __init__(self):
97
 
        self.base = "http://example.com/branches/demo"
98
 
        self.control_files = FakeControlFiles()
 
143
    def __init__(self, base=None, user_id=None):
 
144
        if base is None:
 
145
            self.base = "http://example.com/branches/demo"
 
146
        else:
 
147
            self.base = base
 
148
        self.control_files = FakeControlFiles(user_id=user_id)
 
149
 
 
150
    def lock_write(self):
 
151
        pass
 
152
 
 
153
    def unlock(self):
 
154
        pass
99
155
 
100
156
 
101
157
class FakeControlFiles(object):
102
158
 
103
 
    def __init__(self):
104
 
        self.email = 'Robert Collins <robertc@example.net>\n'
 
159
    def __init__(self, user_id=None):
 
160
        self.email = user_id
 
161
        self.files = {}
105
162
 
106
163
    def get_utf8(self, filename):
107
164
        if filename != 'email':
110
167
            return StringIO(self.email)
111
168
        raise errors.NoSuchFile(filename)
112
169
 
 
170
    def get(self, filename):
 
171
        try:
 
172
            return StringIO(self.files[filename])
 
173
        except KeyError:
 
174
            raise errors.NoSuchFile(filename)
 
175
 
 
176
    def put(self, filename, fileobj):
 
177
        self.files[filename] = fileobj.read()
 
178
 
113
179
 
114
180
class InstrumentedConfig(config.Config):
115
181
    """An instrumented config that supplies stubs for template methods."""
128
194
        return self._signatures
129
195
 
130
196
 
 
197
bool_config = """[DEFAULT]
 
198
active = true
 
199
inactive = false
 
200
[UPPERCASE]
 
201
active = True
 
202
nonactive = False
 
203
"""
 
204
class TestConfigObj(TestCase):
 
205
    def test_get_bool(self):
 
206
        from bzrlib.config import ConfigObj
 
207
        co = ConfigObj(StringIO(bool_config))
 
208
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
 
209
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
 
210
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
 
211
        self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
 
212
 
 
213
 
131
214
class TestConfig(TestCase):
132
215
 
133
216
    def test_constructs(self):
149
232
 
150
233
    def test_signatures_default(self):
151
234
        my_config = config.Config()
 
235
        self.assertFalse(my_config.signature_needed())
152
236
        self.assertEqual(config.CHECK_IF_POSSIBLE,
153
237
                         my_config.signature_checking())
 
238
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
 
239
                         my_config.signing_policy())
154
240
 
155
241
    def test_signatures_template_method(self):
156
242
        my_config = InstrumentedConfig()
176
262
        my_config = config.Config()
177
263
        self.assertEqual(None, my_config.post_commit())
178
264
 
 
265
    def test_log_format_default(self):
 
266
        my_config = config.Config()
 
267
        self.assertEqual('long', my_config.log_format())
 
268
 
179
269
 
180
270
class TestConfigPath(TestCase):
181
271
 
182
272
    def setUp(self):
183
273
        super(TestConfigPath, self).setUp()
184
 
        self.old_home = os.environ.get('HOME', None)
185
 
        self.old_appdata = os.environ.get('APPDATA', None)
186
274
        os.environ['HOME'] = '/home/bogus'
187
 
        os.environ['APPDATA'] = \
188
 
            r'C:\Documents and Settings\bogus\Application Data'
 
275
        if sys.platform == 'win32':
 
276
            os.environ['BZR_HOME'] = \
 
277
                r'C:\Documents and Settings\bogus\Application Data'
189
278
 
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
279
    def test_config_dir(self):
202
280
        if sys.platform == 'win32':
203
281
            self.assertEqual(config.config_dir(), 
221
299
            self.assertEqual(config.branches_config_filename(),
222
300
                             '/home/bogus/.bazaar/branches.conf')
223
301
 
 
302
    def test_locations_config_filename(self):
 
303
        if sys.platform == 'win32':
 
304
            self.assertEqual(config.locations_config_filename(), 
 
305
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
 
306
        else:
 
307
            self.assertEqual(config.locations_config_filename(),
 
308
                             '/home/bogus/.bazaar/locations.conf')
 
309
 
224
310
class TestIniConfig(TestCase):
225
311
 
226
312
    def test_contructs(self):
227
313
        my_config = config.IniBasedConfig("nothing")
228
314
 
229
315
    def test_from_fp(self):
230
 
        config_file = StringIO(sample_config_text)
 
316
        config_file = StringIO(sample_config_text.encode('utf-8'))
231
317
        my_config = config.IniBasedConfig(None)
232
318
        self.failUnless(
233
319
            isinstance(my_config._get_parser(file=config_file),
234
320
                        ConfigObj))
235
321
 
236
322
    def test_cached(self):
237
 
        config_file = StringIO(sample_config_text)
 
323
        config_file = StringIO(sample_config_text.encode('utf-8'))
238
324
        my_config = config.IniBasedConfig(None)
239
325
        parser = my_config._get_parser(file=config_file)
240
326
        self.failUnless(my_config._get_parser() is parser)
255
341
        finally:
256
342
            config.ConfigObj = oldparserclass
257
343
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
258
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
259
 
 
260
 
 
261
 
class TestBranchConfig(TestCaseInTempDir):
 
344
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
 
345
                                          'utf-8')])
 
346
 
 
347
 
 
348
class TestBranchConfig(TestCaseWithTransport):
262
349
 
263
350
    def test_constructs(self):
264
351
        branch = FakeBranch()
272
359
        self.assertEqual(branch.base, location_config.location)
273
360
        self.failUnless(location_config is my_config._get_location_config())
274
361
 
 
362
    def test_get_config(self):
 
363
        """The Branch.get_config method works properly"""
 
364
        b = BzrDir.create_standalone_workingtree('.').branch
 
365
        my_config = b.get_config()
 
366
        self.assertIs(my_config.get_user_option('wacky'), None)
 
367
        my_config.set_user_option('wacky', 'unlikely')
 
368
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
 
369
 
 
370
        # Ensure we get the same thing if we start again
 
371
        b2 = Branch.open('.')
 
372
        my_config2 = b2.get_config()
 
373
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
 
374
 
 
375
    def test_has_explicit_nickname(self):
 
376
        b = self.make_branch('.')
 
377
        self.assertFalse(b.get_config().has_explicit_nickname())
 
378
        b.nick = 'foo'
 
379
        self.assertTrue(b.get_config().has_explicit_nickname())
 
380
 
 
381
    def test_config_url(self):
 
382
        """The Branch.get_config will use section that uses a local url"""
 
383
        branch = self.make_branch('branch')
 
384
        self.assertEqual('branch', branch.nick)
 
385
 
 
386
        locations = config.locations_config_filename()
 
387
        config.ensure_config_dir_exists()
 
388
        local_url = urlutils.local_path_to_url('branch')
 
389
        open(locations, 'wb').write('[%s]\nnickname = foobar' 
 
390
                                    % (local_url,))
 
391
        self.assertEqual('foobar', branch.nick)
 
392
 
 
393
    def test_config_local_path(self):
 
394
        """The Branch.get_config will use a local system path"""
 
395
        branch = self.make_branch('branch')
 
396
        self.assertEqual('branch', branch.nick)
 
397
 
 
398
        locations = config.locations_config_filename()
 
399
        config.ensure_config_dir_exists()
 
400
        open(locations, 'wb').write('[%s/branch]\nnickname = barry' 
 
401
                                    % (osutils.getcwd().encode('utf8'),))
 
402
        self.assertEqual('barry', branch.nick)
 
403
 
 
404
    def test_config_creates_local(self):
 
405
        """Creating a new entry in config uses a local path."""
 
406
        branch = self.make_branch('branch', format='knit')
 
407
        branch.set_push_location('http://foobar')
 
408
        locations = config.locations_config_filename()
 
409
        local_path = osutils.getcwd().encode('utf8')
 
410
        # Surprisingly ConfigObj doesn't create a trailing newline
 
411
        self.check_file_contents(locations,
 
412
            '[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (local_path,))
 
413
 
 
414
    def test_autonick_urlencoded(self):
 
415
        b = self.make_branch('!repo')
 
416
        self.assertEqual('!repo', b.get_config().get_nickname())
 
417
 
 
418
    def test_warn_if_masked(self):
 
419
        _warning = trace.warning
 
420
        warnings = []
 
421
        def warning(*args):
 
422
            warnings.append(args[0] % args[1:])
 
423
 
 
424
        def set_option(store, warn_masked=True):
 
425
            warnings[:] = []
 
426
            conf.set_user_option('example_option', repr(store), store=store,
 
427
                                 warn_masked=warn_masked)
 
428
        def assertWarning(warning):
 
429
            if warning is None:
 
430
                self.assertEqual(0, len(warnings))
 
431
            else:
 
432
                self.assertEqual(1, len(warnings))
 
433
                self.assertEqual(warning, warnings[0])
 
434
        trace.warning = warning
 
435
        try:
 
436
            branch = self.make_branch('.')
 
437
            conf = branch.get_config()
 
438
            set_option(config.STORE_GLOBAL)
 
439
            assertWarning(None)
 
440
            set_option(config.STORE_BRANCH)
 
441
            assertWarning(None)
 
442
            set_option(config.STORE_GLOBAL)
 
443
            assertWarning('Value "4" is masked by "3" from branch.conf')
 
444
            set_option(config.STORE_GLOBAL, warn_masked=False)
 
445
            assertWarning(None)
 
446
            set_option(config.STORE_LOCATION)
 
447
            assertWarning(None)
 
448
            set_option(config.STORE_BRANCH)
 
449
            assertWarning('Value "3" is masked by "0" from locations.conf')
 
450
            set_option(config.STORE_BRANCH, warn_masked=False)
 
451
            assertWarning(None)
 
452
        finally:
 
453
            trace.warning = _warning
 
454
 
275
455
 
276
456
class TestGlobalConfigItems(TestCase):
277
457
 
278
458
    def test_user_id(self):
279
 
        config_file = StringIO(sample_config_text)
 
459
        config_file = StringIO(sample_config_text.encode('utf-8'))
280
460
        my_config = config.GlobalConfig()
281
461
        my_config._parser = my_config._get_parser(file=config_file)
282
 
        self.assertEqual("Robert Collins <robertc@example.com>",
 
462
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
283
463
                         my_config._get_user_id())
284
464
 
285
465
    def test_absent_user_id(self):
289
469
        self.assertEqual(None, my_config._get_user_id())
290
470
 
291
471
    def test_configured_editor(self):
292
 
        config_file = StringIO(sample_config_text)
 
472
        config_file = StringIO(sample_config_text.encode('utf-8'))
293
473
        my_config = config.GlobalConfig()
294
474
        my_config._parser = my_config._get_parser(file=config_file)
295
475
        self.assertEqual("vim", my_config.get_editor())
298
478
        config_file = StringIO(sample_always_signatures)
299
479
        my_config = config.GlobalConfig()
300
480
        my_config._parser = my_config._get_parser(file=config_file)
301
 
        self.assertEqual(config.CHECK_ALWAYS,
 
481
        self.assertEqual(config.CHECK_NEVER,
302
482
                         my_config.signature_checking())
 
483
        self.assertEqual(config.SIGN_ALWAYS,
 
484
                         my_config.signing_policy())
303
485
        self.assertEqual(True, my_config.signature_needed())
304
486
 
305
487
    def test_signatures_if_possible(self):
306
488
        config_file = StringIO(sample_maybe_signatures)
307
489
        my_config = config.GlobalConfig()
308
490
        my_config._parser = my_config._get_parser(file=config_file)
309
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
491
        self.assertEqual(config.CHECK_NEVER,
310
492
                         my_config.signature_checking())
 
493
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
 
494
                         my_config.signing_policy())
311
495
        self.assertEqual(False, my_config.signature_needed())
312
496
 
313
497
    def test_signatures_ignore(self):
314
498
        config_file = StringIO(sample_ignore_signatures)
315
499
        my_config = config.GlobalConfig()
316
500
        my_config._parser = my_config._get_parser(file=config_file)
317
 
        self.assertEqual(config.CHECK_NEVER,
 
501
        self.assertEqual(config.CHECK_ALWAYS,
318
502
                         my_config.signature_checking())
 
503
        self.assertEqual(config.SIGN_NEVER,
 
504
                         my_config.signing_policy())
319
505
        self.assertEqual(False, my_config.signature_needed())
320
506
 
321
507
    def _get_sample_config(self):
322
 
        config_file = StringIO(sample_config_text)
 
508
        config_file = StringIO(sample_config_text.encode('utf-8'))
323
509
        my_config = config.GlobalConfig()
324
510
        my_config._parser = my_config._get_parser(file=config_file)
325
511
        return my_config
352
538
        my_config = self._get_sample_config()
353
539
        self.assertEqual(None, my_config.post_commit())
354
540
 
355
 
 
356
 
class TestLocationConfig(TestCase):
 
541
    def test_configured_logformat(self):
 
542
        my_config = self._get_sample_config()
 
543
        self.assertEqual("short", my_config.log_format())
 
544
 
 
545
    def test_get_alias(self):
 
546
        my_config = self._get_sample_config()
 
547
        self.assertEqual('help', my_config.get_alias('h'))
 
548
 
 
549
    def test_get_no_alias(self):
 
550
        my_config = self._get_sample_config()
 
551
        self.assertEqual(None, my_config.get_alias('foo'))
 
552
 
 
553
    def test_get_long_alias(self):
 
554
        my_config = self._get_sample_config()
 
555
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
 
556
 
 
557
 
 
558
class TestLocationConfig(TestCaseInTempDir):
357
559
 
358
560
    def test_constructs(self):
359
561
        my_config = config.LocationConfig('http://example.com')
366
568
        # replace the class that is constructured, to check its parameters
367
569
        oldparserclass = config.ConfigObj
368
570
        config.ConfigObj = InstrumentedConfigObj
369
 
        my_config = config.LocationConfig('http://www.example.com')
370
571
        try:
 
572
            my_config = config.LocationConfig('http://www.example.com')
371
573
            parser = my_config._get_parser()
372
574
        finally:
373
575
            config.ConfigObj = oldparserclass
374
576
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
375
577
        self.assertEqual(parser._calls,
376
 
                         [('__init__', config.branches_config_filename())])
 
578
                         [('__init__', config.locations_config_filename(),
 
579
                           'utf-8')])
 
580
        config.ensure_config_dir_exists()
 
581
        #os.mkdir(config.config_dir())
 
582
        f = file(config.branches_config_filename(), 'wb')
 
583
        f.write('')
 
584
        f.close()
 
585
        oldparserclass = config.ConfigObj
 
586
        config.ConfigObj = InstrumentedConfigObj
 
587
        try:
 
588
            my_config = config.LocationConfig('http://www.example.com')
 
589
            parser = my_config._get_parser()
 
590
        finally:
 
591
            config.ConfigObj = oldparserclass
377
592
 
378
593
    def test_get_global_config(self):
379
 
        my_config = config.LocationConfig('http://example.com')
 
594
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
380
595
        global_config = my_config._get_global_config()
381
596
        self.failUnless(isinstance(global_config, config.GlobalConfig))
382
597
        self.failUnless(global_config is my_config._get_global_config())
383
598
 
384
 
    def test__get_section_no_match(self):
385
 
        self.get_location_config('/')
386
 
        self.assertEqual(None, self.my_config._get_section())
 
599
    def test__get_matching_sections_no_match(self):
 
600
        self.get_branch_config('/')
 
601
        self.assertEqual([], self.my_location_config._get_matching_sections())
387
602
        
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())
 
603
    def test__get_matching_sections_exact(self):
 
604
        self.get_branch_config('http://www.example.com')
 
605
        self.assertEqual([('http://www.example.com', '')],
 
606
                         self.my_location_config._get_matching_sections())
392
607
   
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)
 
608
    def test__get_matching_sections_suffix_does_not(self):
 
609
        self.get_branch_config('http://www.example.com-com')
 
610
        self.assertEqual([], self.my_location_config._get_matching_sections())
 
611
 
 
612
    def test__get_matching_sections_subdir_recursive(self):
 
613
        self.get_branch_config('http://www.example.com/com')
 
614
        self.assertEqual([('http://www.example.com', 'com')],
 
615
                         self.my_location_config._get_matching_sections())
 
616
 
 
617
    def test__get_matching_sections_ignoreparent(self):
 
618
        self.get_branch_config('http://www.example.com/ignoreparent')
 
619
        self.assertEqual([('http://www.example.com/ignoreparent', '')],
 
620
                         self.my_location_config._get_matching_sections())
 
621
 
 
622
    def test__get_matching_sections_ignoreparent_subdir(self):
 
623
        self.get_branch_config(
 
624
            'http://www.example.com/ignoreparent/childbranch')
 
625
        self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
 
626
                         self.my_location_config._get_matching_sections())
 
627
 
 
628
    def test__get_matching_sections_subdir_trailing_slash(self):
 
629
        self.get_branch_config('/b')
 
630
        self.assertEqual([('/b/', '')],
 
631
                         self.my_location_config._get_matching_sections())
 
632
 
 
633
    def test__get_matching_sections_subdir_child(self):
 
634
        self.get_branch_config('/a/foo')
 
635
        self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
 
636
                         self.my_location_config._get_matching_sections())
 
637
 
 
638
    def test__get_matching_sections_subdir_child_child(self):
 
639
        self.get_branch_config('/a/foo/bar')
 
640
        self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
 
641
                         self.my_location_config._get_matching_sections())
 
642
 
 
643
    def test__get_matching_sections_trailing_slash_with_children(self):
 
644
        self.get_branch_config('/a/')
 
645
        self.assertEqual([('/a/', '')],
 
646
                         self.my_location_config._get_matching_sections())
 
647
 
 
648
    def test__get_matching_sections_explicit_over_glob(self):
 
649
        # XXX: 2006-09-08 jamesh
 
650
        # This test only passes because ord('c') > ord('*').  If there
 
651
        # was a config section for '/a/?', it would get precedence
 
652
        # over '/a/c'.
 
653
        self.get_branch_config('/a/c')
 
654
        self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
 
655
                         self.my_location_config._get_matching_sections())
 
656
 
 
657
    def test__get_option_policy_normal(self):
 
658
        self.get_branch_config('http://www.example.com')
 
659
        self.assertEqual(
 
660
            self.my_location_config._get_config_policy(
 
661
            'http://www.example.com', 'normal_option'),
 
662
            config.POLICY_NONE)
 
663
 
 
664
    def test__get_option_policy_norecurse(self):
 
665
        self.get_branch_config('http://www.example.com')
 
666
        self.assertEqual(
 
667
            self.my_location_config._get_option_policy(
 
668
            'http://www.example.com', 'norecurse_option'),
 
669
            config.POLICY_NORECURSE)
 
670
        # Test old recurse=False setting:
 
671
        self.assertEqual(
 
672
            self.my_location_config._get_option_policy(
 
673
            'http://www.example.com/norecurse', 'normal_option'),
 
674
            config.POLICY_NORECURSE)
 
675
 
 
676
    def test__get_option_policy_normal(self):
 
677
        self.get_branch_config('http://www.example.com')
 
678
        self.assertEqual(
 
679
            self.my_location_config._get_option_policy(
 
680
            'http://www.example.com', 'appendpath_option'),
 
681
            config.POLICY_APPENDPATH)
442
682
 
443
683
    def test_location_without_username(self):
444
 
        self.get_location_config('http://www.example.com/useglobal')
445
 
        self.assertEqual('Robert Collins <robertc@example.com>',
 
684
        self.get_branch_config('http://www.example.com/ignoreparent')
 
685
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
446
686
                         self.my_config.username())
447
687
 
448
688
    def test_location_not_listed(self):
449
 
        self.get_location_config('/home/robertc/sources')
450
 
        self.assertEqual('Robert Collins <robertc@example.com>',
 
689
        """Test that the global username is used when no location matches"""
 
690
        self.get_branch_config('/home/robertc/sources')
 
691
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
451
692
                         self.my_config.username())
452
693
 
453
694
    def test_overriding_location(self):
454
 
        self.get_location_config('http://www.example.com/foo')
 
695
        self.get_branch_config('http://www.example.com/foo')
455
696
        self.assertEqual('Robert Collins <robertc@example.org>',
456
697
                         self.my_config.username())
457
698
 
458
699
    def test_signatures_not_set(self):
459
 
        self.get_location_config('http://www.example.com',
 
700
        self.get_branch_config('http://www.example.com',
460
701
                                 global_config=sample_ignore_signatures)
461
 
        self.assertEqual(config.CHECK_NEVER,
 
702
        self.assertEqual(config.CHECK_ALWAYS,
462
703
                         self.my_config.signature_checking())
 
704
        self.assertEqual(config.SIGN_NEVER,
 
705
                         self.my_config.signing_policy())
463
706
 
464
707
    def test_signatures_never(self):
465
 
        self.get_location_config('/a/c')
 
708
        self.get_branch_config('/a/c')
466
709
        self.assertEqual(config.CHECK_NEVER,
467
710
                         self.my_config.signature_checking())
468
711
        
469
712
    def test_signatures_when_available(self):
470
 
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
 
713
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
471
714
        self.assertEqual(config.CHECK_IF_POSSIBLE,
472
715
                         self.my_config.signature_checking())
473
716
        
474
717
    def test_signatures_always(self):
475
 
        self.get_location_config('/b')
 
718
        self.get_branch_config('/b')
476
719
        self.assertEqual(config.CHECK_ALWAYS,
477
720
                         self.my_config.signature_checking())
478
721
        
479
722
    def test_gpg_signing_command(self):
480
 
        self.get_location_config('/b')
 
723
        self.get_branch_config('/b')
481
724
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
482
725
 
483
726
    def test_gpg_signing_command_missing(self):
484
 
        self.get_location_config('/a')
 
727
        self.get_branch_config('/a')
485
728
        self.assertEqual("false", self.my_config.gpg_signing_command())
486
729
 
487
730
    def test_get_user_option_global(self):
488
 
        self.get_location_config('/a')
 
731
        self.get_branch_config('/a')
489
732
        self.assertEqual('something',
490
733
                         self.my_config.get_user_option('user_global_option'))
491
734
 
492
735
    def test_get_user_option_local(self):
493
 
        self.get_location_config('/a')
 
736
        self.get_branch_config('/a')
494
737
        self.assertEqual('local',
495
738
                         self.my_config.get_user_option('user_local_option'))
496
 
        
 
739
 
 
740
    def test_get_user_option_appendpath(self):
 
741
        # returned as is for the base path:
 
742
        self.get_branch_config('http://www.example.com')
 
743
        self.assertEqual('append',
 
744
                         self.my_config.get_user_option('appendpath_option'))
 
745
        # Extra path components get appended:
 
746
        self.get_branch_config('http://www.example.com/a/b/c')
 
747
        self.assertEqual('append/a/b/c',
 
748
                         self.my_config.get_user_option('appendpath_option'))
 
749
        # Overriden for http://www.example.com/dir, where it is a
 
750
        # normal option:
 
751
        self.get_branch_config('http://www.example.com/dir/a/b/c')
 
752
        self.assertEqual('normal',
 
753
                         self.my_config.get_user_option('appendpath_option'))
 
754
 
 
755
    def test_get_user_option_norecurse(self):
 
756
        self.get_branch_config('http://www.example.com')
 
757
        self.assertEqual('norecurse',
 
758
                         self.my_config.get_user_option('norecurse_option'))
 
759
        self.get_branch_config('http://www.example.com/dir')
 
760
        self.assertEqual(None,
 
761
                         self.my_config.get_user_option('norecurse_option'))
 
762
        # http://www.example.com/norecurse is a recurse=False section
 
763
        # that redefines normal_option.  Subdirectories do not pick up
 
764
        # this redefinition.
 
765
        self.get_branch_config('http://www.example.com/norecurse')
 
766
        self.assertEqual('norecurse',
 
767
                         self.my_config.get_user_option('normal_option'))
 
768
        self.get_branch_config('http://www.example.com/norecurse/subdir')
 
769
        self.assertEqual('normal',
 
770
                         self.my_config.get_user_option('normal_option'))
 
771
 
 
772
    def test_set_user_option_norecurse(self):
 
773
        self.get_branch_config('http://www.example.com')
 
774
        self.my_config.set_user_option('foo', 'bar',
 
775
                                       store=config.STORE_LOCATION_NORECURSE)
 
776
        self.assertEqual(
 
777
            self.my_location_config._get_option_policy(
 
778
            'http://www.example.com', 'foo'),
 
779
            config.POLICY_NORECURSE)
 
780
 
 
781
    def test_set_user_option_appendpath(self):
 
782
        self.get_branch_config('http://www.example.com')
 
783
        self.my_config.set_user_option('foo', 'bar',
 
784
                                       store=config.STORE_LOCATION_APPENDPATH)
 
785
        self.assertEqual(
 
786
            self.my_location_config._get_option_policy(
 
787
            'http://www.example.com', 'foo'),
 
788
            config.POLICY_APPENDPATH)
 
789
 
 
790
    def test_set_user_option_change_policy(self):
 
791
        self.get_branch_config('http://www.example.com')
 
792
        self.my_config.set_user_option('norecurse_option', 'normal',
 
793
                                       store=config.STORE_LOCATION)
 
794
        self.assertEqual(
 
795
            self.my_location_config._get_option_policy(
 
796
            'http://www.example.com', 'norecurse_option'),
 
797
            config.POLICY_NONE)
 
798
 
 
799
    def test_set_user_option_recurse_false_section(self):
 
800
        # The following section has recurse=False set.  The test is to
 
801
        # make sure that a normal option can be added to the section,
 
802
        # converting recurse=False to the norecurse policy.
 
803
        self.get_branch_config('http://www.example.com/norecurse')
 
804
        self.callDeprecated(['The recurse option is deprecated as of 0.14.  '
 
805
                             'The section "http://www.example.com/norecurse" '
 
806
                             'has been converted to use policies.'],
 
807
                            self.my_config.set_user_option,
 
808
                            'foo', 'bar', store=config.STORE_LOCATION)
 
809
        self.assertEqual(
 
810
            self.my_location_config._get_option_policy(
 
811
            'http://www.example.com/norecurse', 'foo'),
 
812
            config.POLICY_NONE)
 
813
        # The previously existing option is still norecurse:
 
814
        self.assertEqual(
 
815
            self.my_location_config._get_option_policy(
 
816
            'http://www.example.com/norecurse', 'normal_option'),
 
817
            config.POLICY_NORECURSE)
 
818
 
497
819
    def test_post_commit_default(self):
498
 
        self.get_location_config('/a/c')
 
820
        self.get_branch_config('/a/c')
499
821
        self.assertEqual('bzrlib.tests.test_config.post_commit',
500
822
                         self.my_config.post_commit())
501
823
 
502
 
 
503
 
class TestLocationConfig(TestCaseInTempDir):
504
 
 
505
 
    def get_location_config(self, location, global_config=None):
 
824
    def get_branch_config(self, location, global_config=None):
506
825
        if global_config is None:
507
 
            global_file = StringIO(sample_config_text)
 
826
            global_file = StringIO(sample_config_text.encode('utf-8'))
508
827
        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)
 
828
            global_file = StringIO(global_config.encode('utf-8'))
 
829
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
 
830
        self.my_config = config.BranchConfig(FakeBranch(location))
 
831
        # Force location config to use specified file
 
832
        self.my_location_config = self.my_config._get_location_config()
 
833
        self.my_location_config._get_parser(branches_file)
 
834
        # Force global config to use specified file
513
835
        self.my_config._get_global_config()._get_parser(global_file)
514
836
 
515
837
    def test_set_user_setting_sets_and_saves(self):
516
 
        self.get_location_config('/a/c')
 
838
        self.get_branch_config('/a/c')
517
839
        record = InstrumentedConfigObj("foo")
518
 
        self.my_config._parser = record
 
840
        self.my_location_config._parser = record
519
841
 
520
842
        real_mkdir = os.mkdir
521
843
        self.created = False
526
848
 
527
849
        os.mkdir = checked_mkdir
528
850
        try:
529
 
            self.my_config.set_user_option('foo', 'bar')
 
851
            self.callDeprecated(['The recurse option is deprecated as of '
 
852
                                 '0.14.  The section "/a/c" has been '
 
853
                                 'converted to use policies.'],
 
854
                                self.my_config.set_user_option,
 
855
                                'foo', 'bar', store=config.STORE_LOCATION)
530
856
        finally:
531
857
            os.mkdir = real_mkdir
532
858
 
536
862
                          ('__setitem__', '/a/c', {}),
537
863
                          ('__getitem__', '/a/c'),
538
864
                          ('__setitem__', 'foo', 'bar'),
 
865
                          ('__getitem__', '/a/c'),
 
866
                          ('as_bool', 'recurse'),
 
867
                          ('__getitem__', '/a/c'),
 
868
                          ('__delitem__', 'recurse'),
 
869
                          ('__getitem__', '/a/c'),
 
870
                          ('keys',),
 
871
                          ('__getitem__', '/a/c'),
 
872
                          ('__contains__', 'foo:policy'),
539
873
                          ('write',)],
540
874
                         record._calls[1:])
541
875
 
542
 
 
543
 
class TestBranchConfigItems(TestCase):
 
876
    def test_set_user_setting_sets_and_saves2(self):
 
877
        self.get_branch_config('/a/c')
 
878
        self.assertIs(self.my_config.get_user_option('foo'), None)
 
879
        self.my_config.set_user_option('foo', 'bar')
 
880
        self.assertEqual(
 
881
            self.my_config.branch.control_files.files['branch.conf'], 
 
882
            'foo = bar')
 
883
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
 
884
        self.my_config.set_user_option('foo', 'baz',
 
885
                                       store=config.STORE_LOCATION)
 
886
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
 
887
        self.my_config.set_user_option('foo', 'qux')
 
888
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
 
889
        
 
890
 
 
891
precedence_global = 'option = global'
 
892
precedence_branch = 'option = branch'
 
893
precedence_location = """
 
894
[http://]
 
895
recurse = true
 
896
option = recurse
 
897
[http://example.com/specific]
 
898
option = exact
 
899
"""
 
900
 
 
901
 
 
902
class TestBranchConfigItems(TestCaseInTempDir):
 
903
 
 
904
    def get_branch_config(self, global_config=None, location=None, 
 
905
                          location_config=None, branch_data_config=None):
 
906
        my_config = config.BranchConfig(FakeBranch(location))
 
907
        if global_config is not None:
 
908
            global_file = StringIO(global_config.encode('utf-8'))
 
909
            my_config._get_global_config()._get_parser(global_file)
 
910
        self.my_location_config = my_config._get_location_config()
 
911
        if location_config is not None:
 
912
            location_file = StringIO(location_config.encode('utf-8'))
 
913
            self.my_location_config._get_parser(location_file)
 
914
        if branch_data_config is not None:
 
915
            my_config.branch.control_files.files['branch.conf'] = \
 
916
                branch_data_config
 
917
        return my_config
544
918
 
545
919
    def test_user_id(self):
546
 
        branch = FakeBranch()
 
920
        branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
547
921
        my_config = config.BranchConfig(branch)
548
922
        self.assertEqual("Robert Collins <robertc@example.net>",
549
 
                         my_config._get_user_id())
 
923
                         my_config.username())
550
924
        branch.control_files.email = "John"
551
 
        self.assertEqual("John", my_config._get_user_id())
 
925
        my_config.set_user_option('email', 
 
926
                                  "Robert Collins <robertc@example.org>")
 
927
        self.assertEqual("John", my_config.username())
 
928
        branch.control_files.email = None
 
929
        self.assertEqual("Robert Collins <robertc@example.org>",
 
930
                         my_config.username())
552
931
 
553
932
    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>",
 
933
        my_config = self.get_branch_config(sample_config_text)
 
934
        my_config.branch.control_files.email = None
 
935
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
561
936
                         my_config._get_user_id())
562
 
        branch.control_files.email = "John"
 
937
        my_config.branch.control_files.email = "John"
563
938
        self.assertEqual("John", my_config._get_user_id())
564
939
 
565
 
    def test_BZREMAIL_OVERRIDES(self):
566
 
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
 
940
    def test_BZR_EMAIL_OVERRIDES(self):
 
941
        os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
567
942
        branch = FakeBranch()
568
943
        my_config = config.BranchConfig(branch)
569
944
        self.assertEqual("Robert Collins <robertc@example.org>",
570
945
                         my_config.username())
571
946
    
572
947
    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())
 
948
        my_config = self.get_branch_config(
 
949
            global_config=sample_always_signatures)
 
950
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
 
951
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
 
952
        self.assertTrue(my_config.signature_needed())
 
953
 
 
954
    def test_signatures_forced_branch(self):
 
955
        my_config = self.get_branch_config(
 
956
            global_config=sample_ignore_signatures,
 
957
            branch_data_config=sample_always_signatures)
 
958
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
 
959
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
 
960
        self.assertTrue(my_config.signature_needed())
579
961
 
580
962
    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))
 
963
        my_config = self.get_branch_config(
 
964
            # branch data cannot set gpg_signing_command
 
965
            branch_data_config="gpg_signing_command=pgp")
 
966
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
967
        my_config._get_global_config()._get_parser(config_file)
586
968
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
587
969
 
588
970
    def test_get_user_option_global(self):
589
971
        branch = FakeBranch()
590
972
        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))
 
973
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
974
        (my_config._get_global_config()._get_parser(config_file))
594
975
        self.assertEqual('something',
595
976
                         my_config.get_user_option('user_global_option'))
596
977
 
597
978
    def test_post_commit_default(self):
598
979
        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())
 
980
        my_config = self.get_branch_config(sample_config_text, '/a/c',
 
981
                                           sample_branches_text)
 
982
        self.assertEqual(my_config.branch.base, '/a/c')
 
983
        self.assertEqual('bzrlib.tests.test_config.post_commit',
 
984
                         my_config.post_commit())
 
985
        my_config.set_user_option('post_commit', 'rmtree_root')
 
986
        # post-commit is ignored when bresent in branch data
 
987
        self.assertEqual('bzrlib.tests.test_config.post_commit',
 
988
                         my_config.post_commit())
 
989
        my_config.set_user_option('post_commit', 'rmtree_root',
 
990
                                  store=config.STORE_LOCATION)
 
991
        self.assertEqual('rmtree_root', my_config.post_commit())
 
992
 
 
993
    def test_config_precedence(self):
 
994
        my_config = self.get_branch_config(global_config=precedence_global)
 
995
        self.assertEqual(my_config.get_user_option('option'), 'global')
 
996
        my_config = self.get_branch_config(global_config=precedence_global, 
 
997
                                      branch_data_config=precedence_branch)
 
998
        self.assertEqual(my_config.get_user_option('option'), 'branch')
 
999
        my_config = self.get_branch_config(global_config=precedence_global, 
 
1000
                                      branch_data_config=precedence_branch,
 
1001
                                      location_config=precedence_location)
 
1002
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
 
1003
        my_config = self.get_branch_config(global_config=precedence_global, 
 
1004
                                      branch_data_config=precedence_branch,
 
1005
                                      location_config=precedence_location,
 
1006
                                      location='http://example.com/specific')
 
1007
        self.assertEqual(my_config.get_user_option('option'), 'exact')
 
1008
 
 
1009
    def test_get_mail_client(self):
 
1010
        config = self.get_branch_config()
 
1011
        client = config.get_mail_client()
 
1012
        self.assertIsInstance(client, mail_client.DefaultMail)
 
1013
 
 
1014
        config.set_user_option('mail_client', 'default')
 
1015
        client = config.get_mail_client()
 
1016
        self.assertIsInstance(client, mail_client.DefaultMail)
 
1017
 
 
1018
        config.set_user_option('mail_client', 'editor')
 
1019
        client = config.get_mail_client()
 
1020
        self.assertIsInstance(client, mail_client.Editor)
 
1021
 
 
1022
        config.set_user_option('mail_client', 'thunderbird')
 
1023
        client = config.get_mail_client()
 
1024
        self.assertIsInstance(client, mail_client.Thunderbird)
 
1025
 
 
1026
        config.set_user_option('mail_client', 'evolution')
 
1027
        client = config.get_mail_client()
 
1028
        self.assertIsInstance(client, mail_client.Evolution)
 
1029
 
 
1030
        config.set_user_option('mail_client', 'kmail')
 
1031
        client = config.get_mail_client()
 
1032
        self.assertIsInstance(client, mail_client.KMail)
 
1033
 
 
1034
        config.set_user_option('mail_client', 'xdg-email')
 
1035
        client = config.get_mail_client()
 
1036
        self.assertIsInstance(client, mail_client.XDGEmail)
 
1037
 
 
1038
        config.set_user_option('mail_client', 'mapi')
 
1039
        client = config.get_mail_client()
 
1040
        self.assertIsInstance(client, mail_client.MAPIClient)
 
1041
 
 
1042
        config.set_user_option('mail_client', 'firebird')
 
1043
        self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
608
1044
 
609
1045
 
610
1046
class TestMailAddressExtraction(TestCase):
612
1048
    def test_extract_email_address(self):
613
1049
        self.assertEqual('jane@test.com',
614
1050
                         config.extract_email_address('Jane <jane@test.com>'))
615
 
        self.assertRaises(errors.BzrError,
 
1051
        self.assertRaises(errors.NoEmailInUsername,
616
1052
                          config.extract_email_address, 'Jane Tester')
 
1053
 
 
1054
 
 
1055
class TestTreeConfig(TestCaseWithTransport):
 
1056
 
 
1057
    def test_get_value(self):
 
1058
        """Test that retreiving a value from a section is possible"""
 
1059
        branch = self.make_branch('.')
 
1060
        tree_config = config.TreeConfig(branch)
 
1061
        tree_config.set_option('value', 'key', 'SECTION')
 
1062
        tree_config.set_option('value2', 'key2')
 
1063
        tree_config.set_option('value3-top', 'key3')
 
1064
        tree_config.set_option('value3-section', 'key3', 'SECTION')
 
1065
        value = tree_config.get_option('key', 'SECTION')
 
1066
        self.assertEqual(value, 'value')
 
1067
        value = tree_config.get_option('key2')
 
1068
        self.assertEqual(value, 'value2')
 
1069
        self.assertEqual(tree_config.get_option('non-existant'), None)
 
1070
        value = tree_config.get_option('non-existant', 'SECTION')
 
1071
        self.assertEqual(value, None)
 
1072
        value = tree_config.get_option('non-existant', default='default')
 
1073
        self.assertEqual(value, 'default')
 
1074
        self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
 
1075
        value = tree_config.get_option('key2', 'NOSECTION', default='default')
 
1076
        self.assertEqual(value, 'default')
 
1077
        value = tree_config.get_option('key3')
 
1078
        self.assertEqual(value, 'value3-top')
 
1079
        value = tree_config.get_option('key3', 'SECTION')
 
1080
        self.assertEqual(value, 'value3-section')