~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: mbp at sourcefrog
  • Date: 2005-04-11 02:53:57 UTC
  • Revision ID: mbp@sourcefrog.net-20050411025357-af577721308648ae
- remove profiler temporary file when done

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 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
 
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
 
"""
103
 
 
104
 
 
105
 
class InstrumentedConfigObj(object):
106
 
    """A config obj look-enough-alike to record calls made to it."""
107
 
 
108
 
    def __contains__(self, thing):
109
 
        self._calls.append(('__contains__', thing))
110
 
        return False
111
 
 
112
 
    def __getitem__(self, key):
113
 
        self._calls.append(('__getitem__', key))
114
 
        return self
115
 
 
116
 
    def __init__(self, input, encoding=None):
117
 
        self._calls = [('__init__', input, encoding)]
118
 
 
119
 
    def __setitem__(self, key, value):
120
 
        self._calls.append(('__setitem__', key, value))
121
 
 
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):
130
 
        self._calls.append(('write',))
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
 
 
140
 
 
141
 
class FakeBranch(object):
142
 
 
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
155
 
 
156
 
 
157
 
class FakeControlFiles(object):
158
 
 
159
 
    def __init__(self, user_id=None):
160
 
        self.email = user_id
161
 
        self.files = {}
162
 
 
163
 
    def get_utf8(self, filename):
164
 
        if filename != 'email':
165
 
            raise NotImplementedError
166
 
        if self.email is not None:
167
 
            return StringIO(self.email)
168
 
        raise errors.NoSuchFile(filename)
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
 
 
179
 
 
180
 
class InstrumentedConfig(config.Config):
181
 
    """An instrumented config that supplies stubs for template methods."""
182
 
    
183
 
    def __init__(self):
184
 
        super(InstrumentedConfig, self).__init__()
185
 
        self._calls = []
186
 
        self._signatures = config.CHECK_NEVER
187
 
 
188
 
    def _get_user_id(self):
189
 
        self._calls.append('_get_user_id')
190
 
        return "Robert Collins <robert.collins@example.org>"
191
 
 
192
 
    def _get_signature_checking(self):
193
 
        self._calls.append('_get_signature_checking')
194
 
        return self._signatures
195
 
 
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
 
 
214
 
erroneous_config = """[section] # line 1
215
 
good=good # line 2
216
 
[section] # line 3
217
 
whocares=notme # line 4
218
 
"""
219
 
class TestConfigObjErrors(TestCase):
220
 
 
221
 
    def test_duplicate_section_name_error_line(self):
222
 
        try:
223
 
            co = ConfigObj(StringIO(erroneous_config), raise_errors=True)
224
 
        except config.configobj.DuplicateError, e:
225
 
            self.assertEqual(3, e.line_number)
226
 
        else:
227
 
            self.fail('Error in config file not detected')
228
 
 
229
 
class TestConfig(TestCase):
230
 
 
231
 
    def test_constructs(self):
232
 
        config.Config()
233
 
 
234
 
    def test_no_default_editor(self):
235
 
        self.assertRaises(NotImplementedError, config.Config().get_editor)
236
 
 
237
 
    def test_user_email(self):
238
 
        my_config = InstrumentedConfig()
239
 
        self.assertEqual('robert.collins@example.org', my_config.user_email())
240
 
        self.assertEqual(['_get_user_id'], my_config._calls)
241
 
 
242
 
    def test_username(self):
243
 
        my_config = InstrumentedConfig()
244
 
        self.assertEqual('Robert Collins <robert.collins@example.org>',
245
 
                         my_config.username())
246
 
        self.assertEqual(['_get_user_id'], my_config._calls)
247
 
 
248
 
    def test_signatures_default(self):
249
 
        my_config = config.Config()
250
 
        self.assertFalse(my_config.signature_needed())
251
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
252
 
                         my_config.signature_checking())
253
 
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
254
 
                         my_config.signing_policy())
255
 
 
256
 
    def test_signatures_template_method(self):
257
 
        my_config = InstrumentedConfig()
258
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
259
 
        self.assertEqual(['_get_signature_checking'], my_config._calls)
260
 
 
261
 
    def test_signatures_template_method_none(self):
262
 
        my_config = InstrumentedConfig()
263
 
        my_config._signatures = None
264
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
265
 
                         my_config.signature_checking())
266
 
        self.assertEqual(['_get_signature_checking'], my_config._calls)
267
 
 
268
 
    def test_gpg_signing_command_default(self):
269
 
        my_config = config.Config()
270
 
        self.assertEqual('gpg', my_config.gpg_signing_command())
271
 
 
272
 
    def test_get_user_option_default(self):
273
 
        my_config = config.Config()
274
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
275
 
 
276
 
    def test_post_commit_default(self):
277
 
        my_config = config.Config()
278
 
        self.assertEqual(None, my_config.post_commit())
279
 
 
280
 
    def test_log_format_default(self):
281
 
        my_config = config.Config()
282
 
        self.assertEqual('long', my_config.log_format())
283
 
 
284
 
 
285
 
class TestConfigPath(TestCase):
286
 
 
287
 
    def setUp(self):
288
 
        super(TestConfigPath, self).setUp()
289
 
        os.environ['HOME'] = '/home/bogus'
290
 
        if sys.platform == 'win32':
291
 
            os.environ['BZR_HOME'] = \
292
 
                r'C:\Documents and Settings\bogus\Application Data'
293
 
 
294
 
    def test_config_dir(self):
295
 
        if sys.platform == 'win32':
296
 
            self.assertEqual(config.config_dir(), 
297
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
298
 
        else:
299
 
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
300
 
 
301
 
    def test_config_filename(self):
302
 
        if sys.platform == 'win32':
303
 
            self.assertEqual(config.config_filename(), 
304
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
305
 
        else:
306
 
            self.assertEqual(config.config_filename(),
307
 
                             '/home/bogus/.bazaar/bazaar.conf')
308
 
 
309
 
    def test_branches_config_filename(self):
310
 
        if sys.platform == 'win32':
311
 
            self.assertEqual(config.branches_config_filename(), 
312
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
313
 
        else:
314
 
            self.assertEqual(config.branches_config_filename(),
315
 
                             '/home/bogus/.bazaar/branches.conf')
316
 
 
317
 
    def test_locations_config_filename(self):
318
 
        if sys.platform == 'win32':
319
 
            self.assertEqual(config.locations_config_filename(), 
320
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
321
 
        else:
322
 
            self.assertEqual(config.locations_config_filename(),
323
 
                             '/home/bogus/.bazaar/locations.conf')
324
 
 
325
 
class TestIniConfig(TestCase):
326
 
 
327
 
    def test_contructs(self):
328
 
        my_config = config.IniBasedConfig("nothing")
329
 
 
330
 
    def test_from_fp(self):
331
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
332
 
        my_config = config.IniBasedConfig(None)
333
 
        self.failUnless(
334
 
            isinstance(my_config._get_parser(file=config_file),
335
 
                        ConfigObj))
336
 
 
337
 
    def test_cached(self):
338
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
339
 
        my_config = config.IniBasedConfig(None)
340
 
        parser = my_config._get_parser(file=config_file)
341
 
        self.failUnless(my_config._get_parser() is parser)
342
 
 
343
 
 
344
 
class TestGetConfig(TestCase):
345
 
 
346
 
    def test_constructs(self):
347
 
        my_config = config.GlobalConfig()
348
 
 
349
 
    def test_calls_read_filenames(self):
350
 
        # replace the class that is constructured, to check its parameters
351
 
        oldparserclass = config.ConfigObj
352
 
        config.ConfigObj = InstrumentedConfigObj
353
 
        my_config = config.GlobalConfig()
354
 
        try:
355
 
            parser = my_config._get_parser()
356
 
        finally:
357
 
            config.ConfigObj = oldparserclass
358
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
359
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
360
 
                                          'utf-8')])
361
 
 
362
 
 
363
 
class TestBranchConfig(TestCaseWithTransport):
364
 
 
365
 
    def test_constructs(self):
366
 
        branch = FakeBranch()
367
 
        my_config = config.BranchConfig(branch)
368
 
        self.assertRaises(TypeError, config.BranchConfig)
369
 
 
370
 
    def test_get_location_config(self):
371
 
        branch = FakeBranch()
372
 
        my_config = config.BranchConfig(branch)
373
 
        location_config = my_config._get_location_config()
374
 
        self.assertEqual(branch.base, location_config.location)
375
 
        self.failUnless(location_config is my_config._get_location_config())
376
 
 
377
 
    def test_get_config(self):
378
 
        """The Branch.get_config method works properly"""
379
 
        b = BzrDir.create_standalone_workingtree('.').branch
380
 
        my_config = b.get_config()
381
 
        self.assertIs(my_config.get_user_option('wacky'), None)
382
 
        my_config.set_user_option('wacky', 'unlikely')
383
 
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
384
 
 
385
 
        # Ensure we get the same thing if we start again
386
 
        b2 = Branch.open('.')
387
 
        my_config2 = b2.get_config()
388
 
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
389
 
 
390
 
    def test_has_explicit_nickname(self):
391
 
        b = self.make_branch('.')
392
 
        self.assertFalse(b.get_config().has_explicit_nickname())
393
 
        b.nick = 'foo'
394
 
        self.assertTrue(b.get_config().has_explicit_nickname())
395
 
 
396
 
    def test_config_url(self):
397
 
        """The Branch.get_config will use section that uses a local url"""
398
 
        branch = self.make_branch('branch')
399
 
        self.assertEqual('branch', branch.nick)
400
 
 
401
 
        locations = config.locations_config_filename()
402
 
        config.ensure_config_dir_exists()
403
 
        local_url = urlutils.local_path_to_url('branch')
404
 
        open(locations, 'wb').write('[%s]\nnickname = foobar' 
405
 
                                    % (local_url,))
406
 
        self.assertEqual('foobar', branch.nick)
407
 
 
408
 
    def test_config_local_path(self):
409
 
        """The Branch.get_config will use a local system path"""
410
 
        branch = self.make_branch('branch')
411
 
        self.assertEqual('branch', branch.nick)
412
 
 
413
 
        locations = config.locations_config_filename()
414
 
        config.ensure_config_dir_exists()
415
 
        open(locations, 'wb').write('[%s/branch]\nnickname = barry' 
416
 
                                    % (osutils.getcwd().encode('utf8'),))
417
 
        self.assertEqual('barry', branch.nick)
418
 
 
419
 
    def test_config_creates_local(self):
420
 
        """Creating a new entry in config uses a local path."""
421
 
        branch = self.make_branch('branch', format='knit')
422
 
        branch.set_push_location('http://foobar')
423
 
        locations = config.locations_config_filename()
424
 
        local_path = osutils.getcwd().encode('utf8')
425
 
        # Surprisingly ConfigObj doesn't create a trailing newline
426
 
        self.check_file_contents(locations,
427
 
            '[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (local_path,))
428
 
 
429
 
    def test_autonick_urlencoded(self):
430
 
        b = self.make_branch('!repo')
431
 
        self.assertEqual('!repo', b.get_config().get_nickname())
432
 
 
433
 
    def test_warn_if_masked(self):
434
 
        _warning = trace.warning
435
 
        warnings = []
436
 
        def warning(*args):
437
 
            warnings.append(args[0] % args[1:])
438
 
 
439
 
        def set_option(store, warn_masked=True):
440
 
            warnings[:] = []
441
 
            conf.set_user_option('example_option', repr(store), store=store,
442
 
                                 warn_masked=warn_masked)
443
 
        def assertWarning(warning):
444
 
            if warning is None:
445
 
                self.assertEqual(0, len(warnings))
446
 
            else:
447
 
                self.assertEqual(1, len(warnings))
448
 
                self.assertEqual(warning, warnings[0])
449
 
        trace.warning = warning
450
 
        try:
451
 
            branch = self.make_branch('.')
452
 
            conf = branch.get_config()
453
 
            set_option(config.STORE_GLOBAL)
454
 
            assertWarning(None)
455
 
            set_option(config.STORE_BRANCH)
456
 
            assertWarning(None)
457
 
            set_option(config.STORE_GLOBAL)
458
 
            assertWarning('Value "4" is masked by "3" from branch.conf')
459
 
            set_option(config.STORE_GLOBAL, warn_masked=False)
460
 
            assertWarning(None)
461
 
            set_option(config.STORE_LOCATION)
462
 
            assertWarning(None)
463
 
            set_option(config.STORE_BRANCH)
464
 
            assertWarning('Value "3" is masked by "0" from locations.conf')
465
 
            set_option(config.STORE_BRANCH, warn_masked=False)
466
 
            assertWarning(None)
467
 
        finally:
468
 
            trace.warning = _warning
469
 
 
470
 
 
471
 
class TestGlobalConfigItems(TestCase):
472
 
 
473
 
    def test_user_id(self):
474
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
475
 
        my_config = config.GlobalConfig()
476
 
        my_config._parser = my_config._get_parser(file=config_file)
477
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
478
 
                         my_config._get_user_id())
479
 
 
480
 
    def test_absent_user_id(self):
481
 
        config_file = StringIO("")
482
 
        my_config = config.GlobalConfig()
483
 
        my_config._parser = my_config._get_parser(file=config_file)
484
 
        self.assertEqual(None, my_config._get_user_id())
485
 
 
486
 
    def test_configured_editor(self):
487
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
488
 
        my_config = config.GlobalConfig()
489
 
        my_config._parser = my_config._get_parser(file=config_file)
490
 
        self.assertEqual("vim", my_config.get_editor())
491
 
 
492
 
    def test_signatures_always(self):
493
 
        config_file = StringIO(sample_always_signatures)
494
 
        my_config = config.GlobalConfig()
495
 
        my_config._parser = my_config._get_parser(file=config_file)
496
 
        self.assertEqual(config.CHECK_NEVER,
497
 
                         my_config.signature_checking())
498
 
        self.assertEqual(config.SIGN_ALWAYS,
499
 
                         my_config.signing_policy())
500
 
        self.assertEqual(True, my_config.signature_needed())
501
 
 
502
 
    def test_signatures_if_possible(self):
503
 
        config_file = StringIO(sample_maybe_signatures)
504
 
        my_config = config.GlobalConfig()
505
 
        my_config._parser = my_config._get_parser(file=config_file)
506
 
        self.assertEqual(config.CHECK_NEVER,
507
 
                         my_config.signature_checking())
508
 
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
509
 
                         my_config.signing_policy())
510
 
        self.assertEqual(False, my_config.signature_needed())
511
 
 
512
 
    def test_signatures_ignore(self):
513
 
        config_file = StringIO(sample_ignore_signatures)
514
 
        my_config = config.GlobalConfig()
515
 
        my_config._parser = my_config._get_parser(file=config_file)
516
 
        self.assertEqual(config.CHECK_ALWAYS,
517
 
                         my_config.signature_checking())
518
 
        self.assertEqual(config.SIGN_NEVER,
519
 
                         my_config.signing_policy())
520
 
        self.assertEqual(False, my_config.signature_needed())
521
 
 
522
 
    def _get_sample_config(self):
523
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
524
 
        my_config = config.GlobalConfig()
525
 
        my_config._parser = my_config._get_parser(file=config_file)
526
 
        return my_config
527
 
 
528
 
    def test_gpg_signing_command(self):
529
 
        my_config = self._get_sample_config()
530
 
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
531
 
        self.assertEqual(False, my_config.signature_needed())
532
 
 
533
 
    def _get_empty_config(self):
534
 
        config_file = StringIO("")
535
 
        my_config = config.GlobalConfig()
536
 
        my_config._parser = my_config._get_parser(file=config_file)
537
 
        return my_config
538
 
 
539
 
    def test_gpg_signing_command_unset(self):
540
 
        my_config = self._get_empty_config()
541
 
        self.assertEqual("gpg", my_config.gpg_signing_command())
542
 
 
543
 
    def test_get_user_option_default(self):
544
 
        my_config = self._get_empty_config()
545
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
546
 
 
547
 
    def test_get_user_option_global(self):
548
 
        my_config = self._get_sample_config()
549
 
        self.assertEqual("something",
550
 
                         my_config.get_user_option('user_global_option'))
551
 
        
552
 
    def test_post_commit_default(self):
553
 
        my_config = self._get_sample_config()
554
 
        self.assertEqual(None, my_config.post_commit())
555
 
 
556
 
    def test_configured_logformat(self):
557
 
        my_config = self._get_sample_config()
558
 
        self.assertEqual("short", my_config.log_format())
559
 
 
560
 
    def test_get_alias(self):
561
 
        my_config = self._get_sample_config()
562
 
        self.assertEqual('help', my_config.get_alias('h'))
563
 
 
564
 
    def test_get_no_alias(self):
565
 
        my_config = self._get_sample_config()
566
 
        self.assertEqual(None, my_config.get_alias('foo'))
567
 
 
568
 
    def test_get_long_alias(self):
569
 
        my_config = self._get_sample_config()
570
 
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
571
 
 
572
 
 
573
 
class TestLocationConfig(TestCaseInTempDir):
574
 
 
575
 
    def test_constructs(self):
576
 
        my_config = config.LocationConfig('http://example.com')
577
 
        self.assertRaises(TypeError, config.LocationConfig)
578
 
 
579
 
    def test_branch_calls_read_filenames(self):
580
 
        # This is testing the correct file names are provided.
581
 
        # TODO: consolidate with the test for GlobalConfigs filename checks.
582
 
        #
583
 
        # replace the class that is constructured, to check its parameters
584
 
        oldparserclass = config.ConfigObj
585
 
        config.ConfigObj = InstrumentedConfigObj
586
 
        try:
587
 
            my_config = config.LocationConfig('http://www.example.com')
588
 
            parser = my_config._get_parser()
589
 
        finally:
590
 
            config.ConfigObj = oldparserclass
591
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
592
 
        self.assertEqual(parser._calls,
593
 
                         [('__init__', config.locations_config_filename(),
594
 
                           'utf-8')])
595
 
        config.ensure_config_dir_exists()
596
 
        #os.mkdir(config.config_dir())
597
 
        f = file(config.branches_config_filename(), 'wb')
598
 
        f.write('')
599
 
        f.close()
600
 
        oldparserclass = config.ConfigObj
601
 
        config.ConfigObj = InstrumentedConfigObj
602
 
        try:
603
 
            my_config = config.LocationConfig('http://www.example.com')
604
 
            parser = my_config._get_parser()
605
 
        finally:
606
 
            config.ConfigObj = oldparserclass
607
 
 
608
 
    def test_get_global_config(self):
609
 
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
610
 
        global_config = my_config._get_global_config()
611
 
        self.failUnless(isinstance(global_config, config.GlobalConfig))
612
 
        self.failUnless(global_config is my_config._get_global_config())
613
 
 
614
 
    def test__get_matching_sections_no_match(self):
615
 
        self.get_branch_config('/')
616
 
        self.assertEqual([], self.my_location_config._get_matching_sections())
617
 
        
618
 
    def test__get_matching_sections_exact(self):
619
 
        self.get_branch_config('http://www.example.com')
620
 
        self.assertEqual([('http://www.example.com', '')],
621
 
                         self.my_location_config._get_matching_sections())
622
 
   
623
 
    def test__get_matching_sections_suffix_does_not(self):
624
 
        self.get_branch_config('http://www.example.com-com')
625
 
        self.assertEqual([], self.my_location_config._get_matching_sections())
626
 
 
627
 
    def test__get_matching_sections_subdir_recursive(self):
628
 
        self.get_branch_config('http://www.example.com/com')
629
 
        self.assertEqual([('http://www.example.com', 'com')],
630
 
                         self.my_location_config._get_matching_sections())
631
 
 
632
 
    def test__get_matching_sections_ignoreparent(self):
633
 
        self.get_branch_config('http://www.example.com/ignoreparent')
634
 
        self.assertEqual([('http://www.example.com/ignoreparent', '')],
635
 
                         self.my_location_config._get_matching_sections())
636
 
 
637
 
    def test__get_matching_sections_ignoreparent_subdir(self):
638
 
        self.get_branch_config(
639
 
            'http://www.example.com/ignoreparent/childbranch')
640
 
        self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
641
 
                         self.my_location_config._get_matching_sections())
642
 
 
643
 
    def test__get_matching_sections_subdir_trailing_slash(self):
644
 
        self.get_branch_config('/b')
645
 
        self.assertEqual([('/b/', '')],
646
 
                         self.my_location_config._get_matching_sections())
647
 
 
648
 
    def test__get_matching_sections_subdir_child(self):
649
 
        self.get_branch_config('/a/foo')
650
 
        self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
651
 
                         self.my_location_config._get_matching_sections())
652
 
 
653
 
    def test__get_matching_sections_subdir_child_child(self):
654
 
        self.get_branch_config('/a/foo/bar')
655
 
        self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
656
 
                         self.my_location_config._get_matching_sections())
657
 
 
658
 
    def test__get_matching_sections_trailing_slash_with_children(self):
659
 
        self.get_branch_config('/a/')
660
 
        self.assertEqual([('/a/', '')],
661
 
                         self.my_location_config._get_matching_sections())
662
 
 
663
 
    def test__get_matching_sections_explicit_over_glob(self):
664
 
        # XXX: 2006-09-08 jamesh
665
 
        # This test only passes because ord('c') > ord('*').  If there
666
 
        # was a config section for '/a/?', it would get precedence
667
 
        # over '/a/c'.
668
 
        self.get_branch_config('/a/c')
669
 
        self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
670
 
                         self.my_location_config._get_matching_sections())
671
 
 
672
 
    def test__get_option_policy_normal(self):
673
 
        self.get_branch_config('http://www.example.com')
674
 
        self.assertEqual(
675
 
            self.my_location_config._get_config_policy(
676
 
            'http://www.example.com', 'normal_option'),
677
 
            config.POLICY_NONE)
678
 
 
679
 
    def test__get_option_policy_norecurse(self):
680
 
        self.get_branch_config('http://www.example.com')
681
 
        self.assertEqual(
682
 
            self.my_location_config._get_option_policy(
683
 
            'http://www.example.com', 'norecurse_option'),
684
 
            config.POLICY_NORECURSE)
685
 
        # Test old recurse=False setting:
686
 
        self.assertEqual(
687
 
            self.my_location_config._get_option_policy(
688
 
            'http://www.example.com/norecurse', 'normal_option'),
689
 
            config.POLICY_NORECURSE)
690
 
 
691
 
    def test__get_option_policy_normal(self):
692
 
        self.get_branch_config('http://www.example.com')
693
 
        self.assertEqual(
694
 
            self.my_location_config._get_option_policy(
695
 
            'http://www.example.com', 'appendpath_option'),
696
 
            config.POLICY_APPENDPATH)
697
 
 
698
 
    def test_location_without_username(self):
699
 
        self.get_branch_config('http://www.example.com/ignoreparent')
700
 
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
701
 
                         self.my_config.username())
702
 
 
703
 
    def test_location_not_listed(self):
704
 
        """Test that the global username is used when no location matches"""
705
 
        self.get_branch_config('/home/robertc/sources')
706
 
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
707
 
                         self.my_config.username())
708
 
 
709
 
    def test_overriding_location(self):
710
 
        self.get_branch_config('http://www.example.com/foo')
711
 
        self.assertEqual('Robert Collins <robertc@example.org>',
712
 
                         self.my_config.username())
713
 
 
714
 
    def test_signatures_not_set(self):
715
 
        self.get_branch_config('http://www.example.com',
716
 
                                 global_config=sample_ignore_signatures)
717
 
        self.assertEqual(config.CHECK_ALWAYS,
718
 
                         self.my_config.signature_checking())
719
 
        self.assertEqual(config.SIGN_NEVER,
720
 
                         self.my_config.signing_policy())
721
 
 
722
 
    def test_signatures_never(self):
723
 
        self.get_branch_config('/a/c')
724
 
        self.assertEqual(config.CHECK_NEVER,
725
 
                         self.my_config.signature_checking())
726
 
        
727
 
    def test_signatures_when_available(self):
728
 
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
729
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
730
 
                         self.my_config.signature_checking())
731
 
        
732
 
    def test_signatures_always(self):
733
 
        self.get_branch_config('/b')
734
 
        self.assertEqual(config.CHECK_ALWAYS,
735
 
                         self.my_config.signature_checking())
736
 
        
737
 
    def test_gpg_signing_command(self):
738
 
        self.get_branch_config('/b')
739
 
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
740
 
 
741
 
    def test_gpg_signing_command_missing(self):
742
 
        self.get_branch_config('/a')
743
 
        self.assertEqual("false", self.my_config.gpg_signing_command())
744
 
 
745
 
    def test_get_user_option_global(self):
746
 
        self.get_branch_config('/a')
747
 
        self.assertEqual('something',
748
 
                         self.my_config.get_user_option('user_global_option'))
749
 
 
750
 
    def test_get_user_option_local(self):
751
 
        self.get_branch_config('/a')
752
 
        self.assertEqual('local',
753
 
                         self.my_config.get_user_option('user_local_option'))
754
 
 
755
 
    def test_get_user_option_appendpath(self):
756
 
        # returned as is for the base path:
757
 
        self.get_branch_config('http://www.example.com')
758
 
        self.assertEqual('append',
759
 
                         self.my_config.get_user_option('appendpath_option'))
760
 
        # Extra path components get appended:
761
 
        self.get_branch_config('http://www.example.com/a/b/c')
762
 
        self.assertEqual('append/a/b/c',
763
 
                         self.my_config.get_user_option('appendpath_option'))
764
 
        # Overriden for http://www.example.com/dir, where it is a
765
 
        # normal option:
766
 
        self.get_branch_config('http://www.example.com/dir/a/b/c')
767
 
        self.assertEqual('normal',
768
 
                         self.my_config.get_user_option('appendpath_option'))
769
 
 
770
 
    def test_get_user_option_norecurse(self):
771
 
        self.get_branch_config('http://www.example.com')
772
 
        self.assertEqual('norecurse',
773
 
                         self.my_config.get_user_option('norecurse_option'))
774
 
        self.get_branch_config('http://www.example.com/dir')
775
 
        self.assertEqual(None,
776
 
                         self.my_config.get_user_option('norecurse_option'))
777
 
        # http://www.example.com/norecurse is a recurse=False section
778
 
        # that redefines normal_option.  Subdirectories do not pick up
779
 
        # this redefinition.
780
 
        self.get_branch_config('http://www.example.com/norecurse')
781
 
        self.assertEqual('norecurse',
782
 
                         self.my_config.get_user_option('normal_option'))
783
 
        self.get_branch_config('http://www.example.com/norecurse/subdir')
784
 
        self.assertEqual('normal',
785
 
                         self.my_config.get_user_option('normal_option'))
786
 
 
787
 
    def test_set_user_option_norecurse(self):
788
 
        self.get_branch_config('http://www.example.com')
789
 
        self.my_config.set_user_option('foo', 'bar',
790
 
                                       store=config.STORE_LOCATION_NORECURSE)
791
 
        self.assertEqual(
792
 
            self.my_location_config._get_option_policy(
793
 
            'http://www.example.com', 'foo'),
794
 
            config.POLICY_NORECURSE)
795
 
 
796
 
    def test_set_user_option_appendpath(self):
797
 
        self.get_branch_config('http://www.example.com')
798
 
        self.my_config.set_user_option('foo', 'bar',
799
 
                                       store=config.STORE_LOCATION_APPENDPATH)
800
 
        self.assertEqual(
801
 
            self.my_location_config._get_option_policy(
802
 
            'http://www.example.com', 'foo'),
803
 
            config.POLICY_APPENDPATH)
804
 
 
805
 
    def test_set_user_option_change_policy(self):
806
 
        self.get_branch_config('http://www.example.com')
807
 
        self.my_config.set_user_option('norecurse_option', 'normal',
808
 
                                       store=config.STORE_LOCATION)
809
 
        self.assertEqual(
810
 
            self.my_location_config._get_option_policy(
811
 
            'http://www.example.com', 'norecurse_option'),
812
 
            config.POLICY_NONE)
813
 
 
814
 
    def test_set_user_option_recurse_false_section(self):
815
 
        # The following section has recurse=False set.  The test is to
816
 
        # make sure that a normal option can be added to the section,
817
 
        # converting recurse=False to the norecurse policy.
818
 
        self.get_branch_config('http://www.example.com/norecurse')
819
 
        self.callDeprecated(['The recurse option is deprecated as of 0.14.  '
820
 
                             'The section "http://www.example.com/norecurse" '
821
 
                             'has been converted to use policies.'],
822
 
                            self.my_config.set_user_option,
823
 
                            'foo', 'bar', store=config.STORE_LOCATION)
824
 
        self.assertEqual(
825
 
            self.my_location_config._get_option_policy(
826
 
            'http://www.example.com/norecurse', 'foo'),
827
 
            config.POLICY_NONE)
828
 
        # The previously existing option is still norecurse:
829
 
        self.assertEqual(
830
 
            self.my_location_config._get_option_policy(
831
 
            'http://www.example.com/norecurse', 'normal_option'),
832
 
            config.POLICY_NORECURSE)
833
 
 
834
 
    def test_post_commit_default(self):
835
 
        self.get_branch_config('/a/c')
836
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
837
 
                         self.my_config.post_commit())
838
 
 
839
 
    def get_branch_config(self, location, global_config=None):
840
 
        if global_config is None:
841
 
            global_file = StringIO(sample_config_text.encode('utf-8'))
842
 
        else:
843
 
            global_file = StringIO(global_config.encode('utf-8'))
844
 
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
845
 
        self.my_config = config.BranchConfig(FakeBranch(location))
846
 
        # Force location config to use specified file
847
 
        self.my_location_config = self.my_config._get_location_config()
848
 
        self.my_location_config._get_parser(branches_file)
849
 
        # Force global config to use specified file
850
 
        self.my_config._get_global_config()._get_parser(global_file)
851
 
 
852
 
    def test_set_user_setting_sets_and_saves(self):
853
 
        self.get_branch_config('/a/c')
854
 
        record = InstrumentedConfigObj("foo")
855
 
        self.my_location_config._parser = record
856
 
 
857
 
        real_mkdir = os.mkdir
858
 
        self.created = False
859
 
        def checked_mkdir(path, mode=0777):
860
 
            self.log('making directory: %s', path)
861
 
            real_mkdir(path, mode)
862
 
            self.created = True
863
 
 
864
 
        os.mkdir = checked_mkdir
865
 
        try:
866
 
            self.callDeprecated(['The recurse option is deprecated as of '
867
 
                                 '0.14.  The section "/a/c" has been '
868
 
                                 'converted to use policies.'],
869
 
                                self.my_config.set_user_option,
870
 
                                'foo', 'bar', store=config.STORE_LOCATION)
871
 
        finally:
872
 
            os.mkdir = real_mkdir
873
 
 
874
 
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
875
 
        self.assertEqual([('__contains__', '/a/c'),
876
 
                          ('__contains__', '/a/c/'),
877
 
                          ('__setitem__', '/a/c', {}),
878
 
                          ('__getitem__', '/a/c'),
879
 
                          ('__setitem__', 'foo', 'bar'),
880
 
                          ('__getitem__', '/a/c'),
881
 
                          ('as_bool', 'recurse'),
882
 
                          ('__getitem__', '/a/c'),
883
 
                          ('__delitem__', 'recurse'),
884
 
                          ('__getitem__', '/a/c'),
885
 
                          ('keys',),
886
 
                          ('__getitem__', '/a/c'),
887
 
                          ('__contains__', 'foo:policy'),
888
 
                          ('write',)],
889
 
                         record._calls[1:])
890
 
 
891
 
    def test_set_user_setting_sets_and_saves2(self):
892
 
        self.get_branch_config('/a/c')
893
 
        self.assertIs(self.my_config.get_user_option('foo'), None)
894
 
        self.my_config.set_user_option('foo', 'bar')
895
 
        self.assertEqual(
896
 
            self.my_config.branch.control_files.files['branch.conf'], 
897
 
            'foo = bar')
898
 
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
899
 
        self.my_config.set_user_option('foo', 'baz',
900
 
                                       store=config.STORE_LOCATION)
901
 
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
902
 
        self.my_config.set_user_option('foo', 'qux')
903
 
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
904
 
        
905
 
    def test_get_bzr_remote_path(self):
906
 
        my_config = config.LocationConfig('/a/c')
907
 
        self.assertEqual('bzr', my_config.get_bzr_remote_path())
908
 
        my_config.set_user_option('bzr_remote_path', '/path-bzr')
909
 
        self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
910
 
        os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
911
 
        self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
912
 
 
913
 
 
914
 
precedence_global = 'option = global'
915
 
precedence_branch = 'option = branch'
916
 
precedence_location = """
917
 
[http://]
918
 
recurse = true
919
 
option = recurse
920
 
[http://example.com/specific]
921
 
option = exact
922
 
"""
923
 
 
924
 
 
925
 
class TestBranchConfigItems(TestCaseInTempDir):
926
 
 
927
 
    def get_branch_config(self, global_config=None, location=None, 
928
 
                          location_config=None, branch_data_config=None):
929
 
        my_config = config.BranchConfig(FakeBranch(location))
930
 
        if global_config is not None:
931
 
            global_file = StringIO(global_config.encode('utf-8'))
932
 
            my_config._get_global_config()._get_parser(global_file)
933
 
        self.my_location_config = my_config._get_location_config()
934
 
        if location_config is not None:
935
 
            location_file = StringIO(location_config.encode('utf-8'))
936
 
            self.my_location_config._get_parser(location_file)
937
 
        if branch_data_config is not None:
938
 
            my_config.branch.control_files.files['branch.conf'] = \
939
 
                branch_data_config
940
 
        return my_config
941
 
 
942
 
    def test_user_id(self):
943
 
        branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
944
 
        my_config = config.BranchConfig(branch)
945
 
        self.assertEqual("Robert Collins <robertc@example.net>",
946
 
                         my_config.username())
947
 
        branch.control_files.email = "John"
948
 
        my_config.set_user_option('email', 
949
 
                                  "Robert Collins <robertc@example.org>")
950
 
        self.assertEqual("John", my_config.username())
951
 
        branch.control_files.email = None
952
 
        self.assertEqual("Robert Collins <robertc@example.org>",
953
 
                         my_config.username())
954
 
 
955
 
    def test_not_set_in_branch(self):
956
 
        my_config = self.get_branch_config(sample_config_text)
957
 
        my_config.branch.control_files.email = None
958
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
959
 
                         my_config._get_user_id())
960
 
        my_config.branch.control_files.email = "John"
961
 
        self.assertEqual("John", my_config._get_user_id())
962
 
 
963
 
    def test_BZR_EMAIL_OVERRIDES(self):
964
 
        os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
965
 
        branch = FakeBranch()
966
 
        my_config = config.BranchConfig(branch)
967
 
        self.assertEqual("Robert Collins <robertc@example.org>",
968
 
                         my_config.username())
969
 
    
970
 
    def test_signatures_forced(self):
971
 
        my_config = self.get_branch_config(
972
 
            global_config=sample_always_signatures)
973
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
974
 
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
975
 
        self.assertTrue(my_config.signature_needed())
976
 
 
977
 
    def test_signatures_forced_branch(self):
978
 
        my_config = self.get_branch_config(
979
 
            global_config=sample_ignore_signatures,
980
 
            branch_data_config=sample_always_signatures)
981
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
982
 
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
983
 
        self.assertTrue(my_config.signature_needed())
984
 
 
985
 
    def test_gpg_signing_command(self):
986
 
        my_config = self.get_branch_config(
987
 
            # branch data cannot set gpg_signing_command
988
 
            branch_data_config="gpg_signing_command=pgp")
989
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
990
 
        my_config._get_global_config()._get_parser(config_file)
991
 
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
992
 
 
993
 
    def test_get_user_option_global(self):
994
 
        branch = FakeBranch()
995
 
        my_config = config.BranchConfig(branch)
996
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
997
 
        (my_config._get_global_config()._get_parser(config_file))
998
 
        self.assertEqual('something',
999
 
                         my_config.get_user_option('user_global_option'))
1000
 
 
1001
 
    def test_post_commit_default(self):
1002
 
        branch = FakeBranch()
1003
 
        my_config = self.get_branch_config(sample_config_text, '/a/c',
1004
 
                                           sample_branches_text)
1005
 
        self.assertEqual(my_config.branch.base, '/a/c')
1006
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1007
 
                         my_config.post_commit())
1008
 
        my_config.set_user_option('post_commit', 'rmtree_root')
1009
 
        # post-commit is ignored when bresent in branch data
1010
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1011
 
                         my_config.post_commit())
1012
 
        my_config.set_user_option('post_commit', 'rmtree_root',
1013
 
                                  store=config.STORE_LOCATION)
1014
 
        self.assertEqual('rmtree_root', my_config.post_commit())
1015
 
 
1016
 
    def test_config_precedence(self):
1017
 
        my_config = self.get_branch_config(global_config=precedence_global)
1018
 
        self.assertEqual(my_config.get_user_option('option'), 'global')
1019
 
        my_config = self.get_branch_config(global_config=precedence_global, 
1020
 
                                      branch_data_config=precedence_branch)
1021
 
        self.assertEqual(my_config.get_user_option('option'), 'branch')
1022
 
        my_config = self.get_branch_config(global_config=precedence_global, 
1023
 
                                      branch_data_config=precedence_branch,
1024
 
                                      location_config=precedence_location)
1025
 
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
1026
 
        my_config = self.get_branch_config(global_config=precedence_global, 
1027
 
                                      branch_data_config=precedence_branch,
1028
 
                                      location_config=precedence_location,
1029
 
                                      location='http://example.com/specific')
1030
 
        self.assertEqual(my_config.get_user_option('option'), 'exact')
1031
 
 
1032
 
    def test_get_mail_client(self):
1033
 
        config = self.get_branch_config()
1034
 
        client = config.get_mail_client()
1035
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1036
 
 
1037
 
        # Specific clients
1038
 
        config.set_user_option('mail_client', 'evolution')
1039
 
        client = config.get_mail_client()
1040
 
        self.assertIsInstance(client, mail_client.Evolution)
1041
 
 
1042
 
        config.set_user_option('mail_client', 'kmail')
1043
 
        client = config.get_mail_client()
1044
 
        self.assertIsInstance(client, mail_client.KMail)
1045
 
 
1046
 
        config.set_user_option('mail_client', 'mutt')
1047
 
        client = config.get_mail_client()
1048
 
        self.assertIsInstance(client, mail_client.Mutt)
1049
 
 
1050
 
        config.set_user_option('mail_client', 'thunderbird')
1051
 
        client = config.get_mail_client()
1052
 
        self.assertIsInstance(client, mail_client.Thunderbird)
1053
 
 
1054
 
        # Generic options
1055
 
        config.set_user_option('mail_client', 'default')
1056
 
        client = config.get_mail_client()
1057
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1058
 
 
1059
 
        config.set_user_option('mail_client', 'editor')
1060
 
        client = config.get_mail_client()
1061
 
        self.assertIsInstance(client, mail_client.Editor)
1062
 
 
1063
 
        config.set_user_option('mail_client', 'mapi')
1064
 
        client = config.get_mail_client()
1065
 
        self.assertIsInstance(client, mail_client.MAPIClient)
1066
 
 
1067
 
        config.set_user_option('mail_client', 'xdg-email')
1068
 
        client = config.get_mail_client()
1069
 
        self.assertIsInstance(client, mail_client.XDGEmail)
1070
 
 
1071
 
        config.set_user_option('mail_client', 'firebird')
1072
 
        self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1073
 
 
1074
 
 
1075
 
class TestMailAddressExtraction(TestCase):
1076
 
 
1077
 
    def test_extract_email_address(self):
1078
 
        self.assertEqual('jane@test.com',
1079
 
                         config.extract_email_address('Jane <jane@test.com>'))
1080
 
        self.assertRaises(errors.NoEmailInUsername,
1081
 
                          config.extract_email_address, 'Jane Tester')
1082
 
 
1083
 
 
1084
 
class TestTreeConfig(TestCaseWithTransport):
1085
 
 
1086
 
    def test_get_value(self):
1087
 
        """Test that retreiving a value from a section is possible"""
1088
 
        branch = self.make_branch('.')
1089
 
        tree_config = config.TreeConfig(branch)
1090
 
        tree_config.set_option('value', 'key', 'SECTION')
1091
 
        tree_config.set_option('value2', 'key2')
1092
 
        tree_config.set_option('value3-top', 'key3')
1093
 
        tree_config.set_option('value3-section', 'key3', 'SECTION')
1094
 
        value = tree_config.get_option('key', 'SECTION')
1095
 
        self.assertEqual(value, 'value')
1096
 
        value = tree_config.get_option('key2')
1097
 
        self.assertEqual(value, 'value2')
1098
 
        self.assertEqual(tree_config.get_option('non-existant'), None)
1099
 
        value = tree_config.get_option('non-existant', 'SECTION')
1100
 
        self.assertEqual(value, None)
1101
 
        value = tree_config.get_option('non-existant', default='default')
1102
 
        self.assertEqual(value, 'default')
1103
 
        self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1104
 
        value = tree_config.get_option('key2', 'NOSECTION', default='default')
1105
 
        self.assertEqual(value, 'default')
1106
 
        value = tree_config.get_option('key3')
1107
 
        self.assertEqual(value, 'value3-top')
1108
 
        value = tree_config.get_option('key3', 'SECTION')
1109
 
        self.assertEqual(value, 'value3-section')