/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to breezy/tests/test_config.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-06 23:36:14 UTC
  • mto: (7027.3.2 python3-n)
  • mto: This revision was merged to the branch mainline in revision 7030.
  • Revision ID: jelmer@jelmer.uk-20180706233614-t4m8krag15t4z4lp
Update python3.passing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2014, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for finding and reading the bzr config file[s]."""
18
 
# import system imports here
19
 
from cStringIO import StringIO
 
18
 
 
19
from textwrap import dedent
20
20
import os
21
21
import sys
22
 
 
23
 
#import bzrlib specific imports here
24
 
from bzrlib import (
 
22
import threading
 
23
 
 
24
import configobj
 
25
from testtools import matchers
 
26
 
 
27
from .. import (
25
28
    branch,
26
 
    bzrdir,
27
29
    config,
 
30
    controldir,
28
31
    diff,
29
32
    errors,
 
33
    lock,
30
34
    osutils,
31
35
    mail_client,
32
36
    ui,
33
37
    urlutils,
 
38
    registry as _mod_registry,
34
39
    tests,
35
40
    trace,
36
 
    transport,
37
 
    )
38
 
from bzrlib.util.configobj import configobj
 
41
    )
 
42
from ..bzr import (
 
43
    remote,
 
44
    )
 
45
from ..sixish import (
 
46
    BytesIO,
 
47
    text_type,
 
48
    )
 
49
from ..transport import remote as transport_remote
 
50
from . import (
 
51
    features,
 
52
    scenarios,
 
53
    test_server,
 
54
    )
 
55
 
 
56
 
 
57
def lockable_config_scenarios():
 
58
    return [
 
59
        ('global',
 
60
         {'config_class': config.GlobalConfig,
 
61
          'config_args': [],
 
62
          'config_section': 'DEFAULT'}),
 
63
        ('locations',
 
64
         {'config_class': config.LocationConfig,
 
65
          'config_args': ['.'],
 
66
          'config_section': '.'}),]
 
67
 
 
68
 
 
69
load_tests = scenarios.load_tests_apply_scenarios
 
70
 
 
71
# Register helpers to build stores
 
72
config.test_store_builder_registry.register(
 
73
    'configobj', lambda test: config.TransportIniFileStore(
 
74
        test.get_transport(), 'configobj.conf'))
 
75
config.test_store_builder_registry.register(
 
76
    'breezy', lambda test: config.GlobalStore())
 
77
config.test_store_builder_registry.register(
 
78
    'location', lambda test: config.LocationStore())
 
79
 
 
80
 
 
81
def build_backing_branch(test, relpath,
 
82
                         transport_class=None, server_class=None):
 
83
    """Test helper to create a backing branch only once.
 
84
 
 
85
    Some tests needs multiple stores/stacks to check concurrent update
 
86
    behaviours. As such, they need to build different branch *objects* even if
 
87
    they share the branch on disk.
 
88
 
 
89
    :param relpath: The relative path to the branch. (Note that the helper
 
90
        should always specify the same relpath).
 
91
 
 
92
    :param transport_class: The Transport class the test needs to use.
 
93
 
 
94
    :param server_class: The server associated with the ``transport_class``
 
95
        above.
 
96
 
 
97
    Either both or neither of ``transport_class`` and ``server_class`` should
 
98
    be specified.
 
99
    """
 
100
    if transport_class is not None and server_class is not None:
 
101
        test.transport_class = transport_class
 
102
        test.transport_server = server_class
 
103
    elif not (transport_class is None and server_class is None):
 
104
        raise AssertionError('Specify both ``transport_class`` and '
 
105
                             '``server_class`` or neither of them')
 
106
    if getattr(test, 'backing_branch', None) is None:
 
107
        # First call, let's build the branch on disk
 
108
        test.backing_branch = test.make_branch(relpath)
 
109
 
 
110
 
 
111
def build_branch_store(test):
 
112
    build_backing_branch(test, 'branch')
 
113
    b = branch.Branch.open('branch')
 
114
    return config.BranchStore(b)
 
115
config.test_store_builder_registry.register('branch', build_branch_store)
 
116
 
 
117
 
 
118
def build_control_store(test):
 
119
    build_backing_branch(test, 'branch')
 
120
    b = controldir.ControlDir.open('branch')
 
121
    return config.ControlStore(b)
 
122
config.test_store_builder_registry.register('control', build_control_store)
 
123
 
 
124
 
 
125
def build_remote_branch_store(test):
 
126
    # There is only one permutation (but we won't be able to handle more with
 
127
    # this design anyway)
 
128
    (transport_class,
 
129
     server_class) = transport_remote.get_test_permutations()[0]
 
130
    build_backing_branch(test, 'branch', transport_class, server_class)
 
131
    b = branch.Branch.open(test.get_url('branch'))
 
132
    return config.BranchStore(b)
 
133
config.test_store_builder_registry.register('remote_branch',
 
134
                                            build_remote_branch_store)
 
135
 
 
136
 
 
137
config.test_stack_builder_registry.register(
 
138
    'breezy', lambda test: config.GlobalStack())
 
139
config.test_stack_builder_registry.register(
 
140
    'location', lambda test: config.LocationStack('.'))
 
141
 
 
142
 
 
143
def build_branch_stack(test):
 
144
    build_backing_branch(test, 'branch')
 
145
    b = branch.Branch.open('branch')
 
146
    return config.BranchStack(b)
 
147
config.test_stack_builder_registry.register('branch', build_branch_stack)
 
148
 
 
149
 
 
150
def build_branch_only_stack(test):
 
151
    # There is only one permutation (but we won't be able to handle more with
 
152
    # this design anyway)
 
153
    (transport_class,
 
154
     server_class) = transport_remote.get_test_permutations()[0]
 
155
    build_backing_branch(test, 'branch', transport_class, server_class)
 
156
    b = branch.Branch.open(test.get_url('branch'))
 
157
    return config.BranchOnlyStack(b)
 
158
config.test_stack_builder_registry.register('branch_only',
 
159
                                            build_branch_only_stack)
 
160
 
 
161
def build_remote_control_stack(test):
 
162
    # There is only one permutation (but we won't be able to handle more with
 
163
    # this design anyway)
 
164
    (transport_class,
 
165
     server_class) = transport_remote.get_test_permutations()[0]
 
166
    # We need only a bzrdir for this, not a full branch, but it's not worth
 
167
    # creating a dedicated helper to create only the bzrdir
 
168
    build_backing_branch(test, 'branch', transport_class, server_class)
 
169
    b = branch.Branch.open(test.get_url('branch'))
 
170
    return config.RemoteControlStack(b.controldir)
 
171
config.test_stack_builder_registry.register('remote_control',
 
172
                                            build_remote_control_stack)
39
173
 
40
174
 
41
175
sample_long_alias="log -r-15..-1 --line"
44
178
email=Erik B\u00e5gfors <erik@bagfors.nu>
45
179
editor=vim
46
180
change_editor=vimdiff -of @new_path @old_path
47
 
gpg_signing_command=gnome-gpg
 
181
gpg_signing_key=DD4D5088
48
182
log_format=short
 
183
validate_signatures_in_log=true
 
184
acceptable_keys=amy
49
185
user_global_option=something
 
186
bzr.mergetool.sometool=sometool {base} {this} {other} -o {result}
 
187
bzr.mergetool.funkytool=funkytool "arg with spaces" {this_temp}
 
188
bzr.mergetool.newtool='"newtool with spaces" {this_temp}'
 
189
bzr.default_mergetool=sometool
50
190
[ALIASES]
51
191
h=help
52
 
ll=""" + sample_long_alias + "\n"
53
 
 
54
 
 
55
 
sample_always_signatures = """
 
192
ll=""".encode('utf-8') + sample_long_alias.encode('utf-8') + b"\n"
 
193
 
 
194
 
 
195
sample_always_signatures = b"""
56
196
[DEFAULT]
57
197
check_signatures=ignore
58
198
create_signatures=always
59
199
"""
60
200
 
61
 
sample_ignore_signatures = """
 
201
sample_ignore_signatures = b"""
62
202
[DEFAULT]
63
203
check_signatures=require
64
204
create_signatures=never
65
205
"""
66
206
 
67
 
sample_maybe_signatures = """
 
207
sample_maybe_signatures = b"""
68
208
[DEFAULT]
69
209
check_signatures=ignore
70
210
create_signatures=when-required
71
211
"""
72
212
 
73
 
sample_branches_text = """
 
213
sample_branches_text = b"""
74
214
[http://www.example.com]
75
215
# Top level policy
76
216
email=Robert Collins <robertc@example.org>
93
233
# test trailing / matching with no children
94
234
[/a/]
95
235
check_signatures=check-available
96
 
gpg_signing_command=false
 
236
gpg_signing_key=default
97
237
user_local_option=local
98
238
# test trailing / matching
99
239
[/a/*]
100
240
#subdirs will match but not the parent
101
241
[/a/c]
102
242
check_signatures=ignore
103
 
post_commit=bzrlib.tests.test_config.post_commit
 
243
post_commit=breezy.tests.test_config.post_commit
104
244
#testing explicit beats globs
105
245
"""
106
246
 
107
247
 
 
248
def create_configs(test):
 
249
    """Create configuration files for a given test.
 
250
 
 
251
    This requires creating a tree (and populate the ``test.tree`` attribute)
 
252
    and its associated branch and will populate the following attributes:
 
253
 
 
254
    - branch_config: A BranchConfig for the associated branch.
 
255
 
 
256
    - locations_config : A LocationConfig for the associated branch
 
257
 
 
258
    - breezy_config: A GlobalConfig.
 
259
 
 
260
    The tree and branch are created in a 'tree' subdirectory so the tests can
 
261
    still use the test directory to stay outside of the branch.
 
262
    """
 
263
    tree = test.make_branch_and_tree('tree')
 
264
    test.tree = tree
 
265
    test.branch_config = config.BranchConfig(tree.branch)
 
266
    test.locations_config = config.LocationConfig(tree.basedir)
 
267
    test.breezy_config = config.GlobalConfig()
 
268
 
 
269
 
 
270
def create_configs_with_file_option(test):
 
271
    """Create configuration files with a ``file`` option set in each.
 
272
 
 
273
    This builds on ``create_configs`` and add one ``file`` option in each
 
274
    configuration with a value which allows identifying the configuration file.
 
275
    """
 
276
    create_configs(test)
 
277
    test.breezy_config.set_user_option('file', 'breezy')
 
278
    test.locations_config.set_user_option('file', 'locations')
 
279
    test.branch_config.set_user_option('file', 'branch')
 
280
 
 
281
 
 
282
class TestOptionsMixin:
 
283
 
 
284
    def assertOptions(self, expected, conf):
 
285
        # We don't care about the parser (as it will make tests hard to write
 
286
        # and error-prone anyway)
 
287
        self.assertThat([opt[:4] for opt in conf._get_options()],
 
288
                        matchers.Equals(expected))
 
289
 
 
290
 
108
291
class InstrumentedConfigObj(object):
109
292
    """A config obj look-enough-alike to record calls made to it."""
110
293
 
129
312
        self._calls.append(('keys',))
130
313
        return []
131
314
 
 
315
    def reload(self):
 
316
        self._calls.append(('reload',))
 
317
 
132
318
    def write(self, arg):
133
319
        self._calls.append(('write',))
134
320
 
143
329
 
144
330
class FakeBranch(object):
145
331
 
146
 
    def __init__(self, base=None, user_id=None):
 
332
    def __init__(self, base=None):
147
333
        if base is None:
148
334
            self.base = "http://example.com/branches/demo"
149
335
        else:
150
336
            self.base = base
151
337
        self._transport = self.control_files = \
152
 
            FakeControlFilesAndTransport(user_id=user_id)
 
338
            FakeControlFilesAndTransport()
153
339
 
154
340
    def _get_config(self):
155
341
        return config.TransportConfig(self._transport, 'branch.conf')
156
342
 
157
343
    def lock_write(self):
158
 
        pass
 
344
        return lock.LogicalLockResult(self.unlock)
159
345
 
160
346
    def unlock(self):
161
347
        pass
163
349
 
164
350
class FakeControlFilesAndTransport(object):
165
351
 
166
 
    def __init__(self, user_id=None):
 
352
    def __init__(self):
167
353
        self.files = {}
168
 
        if user_id:
169
 
            self.files['email'] = user_id
170
354
        self._transport = self
171
355
 
172
 
    def get_utf8(self, filename):
173
 
        # from LockableFiles
174
 
        raise AssertionError("get_utf8 should no longer be used")
175
 
 
176
356
    def get(self, filename):
177
357
        # from Transport
178
358
        try:
179
 
            return StringIO(self.files[filename])
 
359
            return BytesIO(self.files[filename])
180
360
        except KeyError:
181
361
            raise errors.NoSuchFile(filename)
182
362
 
215
395
        return 'vimdiff -fo @new_path @old_path'
216
396
 
217
397
 
218
 
bool_config = """[DEFAULT]
 
398
bool_config = b"""[DEFAULT]
219
399
active = true
220
400
inactive = false
221
401
[UPPERCASE]
227
407
class TestConfigObj(tests.TestCase):
228
408
 
229
409
    def test_get_bool(self):
230
 
        co = config.ConfigObj(StringIO(bool_config))
 
410
        co = config.ConfigObj(BytesIO(bool_config))
231
411
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
232
412
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
233
413
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
240
420
        """
241
421
        co = config.ConfigObj()
242
422
        co['test'] = 'foo#bar'
243
 
        lines = co.write()
244
 
        self.assertEqual(lines, ['test = "foo#bar"'])
 
423
        outfile = BytesIO()
 
424
        co.write(outfile=outfile)
 
425
        lines = outfile.getvalue().splitlines()
 
426
        self.assertEqual(lines, [b'test = "foo#bar"'])
245
427
        co2 = config.ConfigObj(lines)
246
428
        self.assertEqual(co2['test'], 'foo#bar')
247
429
 
248
 
 
249
 
erroneous_config = """[section] # line 1
 
430
    def test_triple_quotes(self):
 
431
        # Bug #710410: if the value string has triple quotes
 
432
        # then ConfigObj versions up to 4.7.2 will quote them wrong
 
433
        # and won't able to read them back
 
434
        triple_quotes_value = '''spam
 
435
""" that's my spam """
 
436
eggs'''
 
437
        co = config.ConfigObj()
 
438
        co['test'] = triple_quotes_value
 
439
        # While writing this test another bug in ConfigObj has been found:
 
440
        # method co.write() without arguments produces list of lines
 
441
        # one option per line, and multiline values are not split
 
442
        # across multiple lines,
 
443
        # and that breaks the parsing these lines back by ConfigObj.
 
444
        # This issue only affects test, but it's better to avoid
 
445
        # `co.write()` construct at all.
 
446
        # [bialix 20110222] bug report sent to ConfigObj's author
 
447
        outfile = BytesIO()
 
448
        co.write(outfile=outfile)
 
449
        output = outfile.getvalue()
 
450
        # now we're trying to read it back
 
451
        co2 = config.ConfigObj(BytesIO(output))
 
452
        self.assertEqual(triple_quotes_value, co2['test'])
 
453
 
 
454
 
 
455
erroneous_config = b"""[section] # line 1
250
456
good=good # line 2
251
457
[section] # line 3
252
458
whocares=notme # line 4
257
463
 
258
464
    def test_duplicate_section_name_error_line(self):
259
465
        try:
260
 
            co = configobj.ConfigObj(StringIO(erroneous_config),
 
466
            co = configobj.ConfigObj(BytesIO(erroneous_config),
261
467
                                     raise_errors=True)
262
 
        except config.configobj.DuplicateError, e:
 
468
        except config.configobj.DuplicateError as e:
263
469
            self.assertEqual(3, e.line_number)
264
470
        else:
265
471
            self.fail('Error in config file not detected')
270
476
    def test_constructs(self):
271
477
        config.Config()
272
478
 
273
 
    def test_no_default_editor(self):
274
 
        self.assertRaises(NotImplementedError, config.Config().get_editor)
275
 
 
276
479
    def test_user_email(self):
277
480
        my_config = InstrumentedConfig()
278
481
        self.assertEqual('robert.collins@example.org', my_config.user_email())
284
487
                         my_config.username())
285
488
        self.assertEqual(['_get_user_id'], my_config._calls)
286
489
 
287
 
    def test_signatures_default(self):
288
 
        my_config = config.Config()
289
 
        self.assertFalse(my_config.signature_needed())
290
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
291
 
                         my_config.signature_checking())
292
 
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
293
 
                         my_config.signing_policy())
294
 
 
295
 
    def test_signatures_template_method(self):
296
 
        my_config = InstrumentedConfig()
297
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
298
 
        self.assertEqual(['_get_signature_checking'], my_config._calls)
299
 
 
300
 
    def test_signatures_template_method_none(self):
301
 
        my_config = InstrumentedConfig()
302
 
        my_config._signatures = None
303
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
304
 
                         my_config.signature_checking())
305
 
        self.assertEqual(['_get_signature_checking'], my_config._calls)
306
 
 
307
 
    def test_gpg_signing_command_default(self):
308
 
        my_config = config.Config()
309
 
        self.assertEqual('gpg', my_config.gpg_signing_command())
310
 
 
311
490
    def test_get_user_option_default(self):
312
491
        my_config = config.Config()
313
492
        self.assertEqual(None, my_config.get_user_option('no_option'))
314
493
 
315
 
    def test_post_commit_default(self):
316
 
        my_config = config.Config()
317
 
        self.assertEqual(None, my_config.post_commit())
318
 
 
319
 
    def test_log_format_default(self):
320
 
        my_config = config.Config()
321
 
        self.assertEqual('long', my_config.log_format())
 
494
    def test_validate_signatures_in_log_default(self):
 
495
        my_config = config.Config()
 
496
        self.assertEqual(False, my_config.validate_signatures_in_log())
322
497
 
323
498
    def test_get_change_editor(self):
324
499
        my_config = InstrumentedConfig()
333
508
 
334
509
    def setUp(self):
335
510
        super(TestConfigPath, self).setUp()
336
 
        os.environ['HOME'] = '/home/bogus'
337
 
        os.environ['XDG_CACHE_DIR'] = ''
 
511
        self.overrideEnv('HOME', '/home/bogus')
 
512
        self.overrideEnv('XDG_CACHE_HOME', '')
338
513
        if sys.platform == 'win32':
339
 
            os.environ['BZR_HOME'] = \
340
 
                r'C:\Documents and Settings\bogus\Application Data'
341
 
            self.bzr_home = \
342
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
 
514
            self.overrideEnv(
 
515
                'BRZ_HOME',
 
516
                r'C:\Documents and Settings\bogus\Application Data')
 
517
            self.brz_home = \
 
518
                'C:/Documents and Settings/bogus/Application Data/breezy'
343
519
        else:
344
 
            self.bzr_home = '/home/bogus/.bazaar'
 
520
            self.brz_home = '/home/bogus/.config/breezy'
 
521
 
 
522
    def test_config_dir(self):
 
523
        self.assertEqual(config.config_dir(), self.brz_home)
 
524
 
 
525
    def test_config_dir_is_unicode(self):
 
526
        self.assertIsInstance(config.config_dir(), text_type)
 
527
 
 
528
    def test_config_filename(self):
 
529
        self.assertEqual(config.config_filename(),
 
530
                         self.brz_home + '/breezy.conf')
 
531
 
 
532
    def test_locations_config_filename(self):
 
533
        self.assertEqual(config.locations_config_filename(),
 
534
                         self.brz_home + '/locations.conf')
 
535
 
 
536
    def test_authentication_config_filename(self):
 
537
        self.assertEqual(config.authentication_config_filename(),
 
538
                         self.brz_home + '/authentication.conf')
 
539
 
 
540
    def test_xdg_cache_dir(self):
 
541
        self.assertEqual(config.xdg_cache_dir(),
 
542
            '/home/bogus/.cache')
 
543
 
 
544
 
 
545
class TestConfigPathFallback(tests.TestCaseInTempDir):
 
546
 
 
547
    def setUp(self):
 
548
        super(TestConfigPathFallback, self).setUp()
 
549
        self.overrideEnv('HOME', self.test_dir)
 
550
        self.overrideEnv('XDG_CACHE_HOME', '')
 
551
        self.bzr_home = os.path.join(self.test_dir, '.bazaar')
 
552
        os.mkdir(self.bzr_home)
345
553
 
346
554
    def test_config_dir(self):
347
555
        self.assertEqual(config.config_dir(), self.bzr_home)
348
556
 
 
557
    def test_config_dir_is_unicode(self):
 
558
        self.assertIsInstance(config.config_dir(), text_type)
 
559
 
349
560
    def test_config_filename(self):
350
561
        self.assertEqual(config.config_filename(),
351
562
                         self.bzr_home + '/bazaar.conf')
352
563
 
353
 
    def test_branches_config_filename(self):
354
 
        self.assertEqual(config.branches_config_filename(),
355
 
                         self.bzr_home + '/branches.conf')
356
 
 
357
564
    def test_locations_config_filename(self):
358
565
        self.assertEqual(config.locations_config_filename(),
359
566
                         self.bzr_home + '/locations.conf')
364
571
 
365
572
    def test_xdg_cache_dir(self):
366
573
        self.assertEqual(config.xdg_cache_dir(),
367
 
            '/home/bogus/.cache')
368
 
 
369
 
 
370
 
class TestIniConfig(tests.TestCase):
 
574
            os.path.join(self.test_dir, '.cache'))
 
575
 
 
576
 
 
577
class TestXDGConfigDir(tests.TestCaseInTempDir):
 
578
    # must be in temp dir because config tests for the existence of the bazaar
 
579
    # subdirectory of $XDG_CONFIG_HOME
 
580
 
 
581
    def setUp(self):
 
582
        if sys.platform == 'win32':
 
583
            raise tests.TestNotApplicable(
 
584
                'XDG config dir not used on this platform')
 
585
        super(TestXDGConfigDir, self).setUp()
 
586
        self.overrideEnv('HOME', self.test_home_dir)
 
587
        # BRZ_HOME overrides everything we want to test so unset it.
 
588
        self.overrideEnv('BRZ_HOME', None)
 
589
 
 
590
    def test_xdg_config_dir_exists(self):
 
591
        """When ~/.config/bazaar exists, use it as the config dir."""
 
592
        newdir = osutils.pathjoin(self.test_home_dir, '.config', 'bazaar')
 
593
        os.makedirs(newdir)
 
594
        self.assertEqual(config.config_dir(), newdir)
 
595
 
 
596
    def test_xdg_config_home(self):
 
597
        """When XDG_CONFIG_HOME is set, use it."""
 
598
        xdgconfigdir = osutils.pathjoin(self.test_home_dir, 'xdgconfig')
 
599
        self.overrideEnv('XDG_CONFIG_HOME', xdgconfigdir)
 
600
        newdir = osutils.pathjoin(xdgconfigdir, 'bazaar')
 
601
        os.makedirs(newdir)
 
602
        self.assertEqual(config.config_dir(), newdir)
 
603
 
 
604
 
 
605
class TestIniConfig(tests.TestCaseInTempDir):
371
606
 
372
607
    def make_config_parser(self, s):
373
 
        conf = config.IniBasedConfig(None)
374
 
        parser = conf._get_parser(file=StringIO(s.encode('utf-8')))
375
 
        return conf, parser
 
608
        conf = config.IniBasedConfig.from_string(s)
 
609
        return conf, conf._get_parser()
376
610
 
377
611
 
378
612
class TestIniConfigBuilding(TestIniConfig):
379
613
 
380
614
    def test_contructs(self):
381
 
        my_config = config.IniBasedConfig("nothing")
 
615
        config.IniBasedConfig()
382
616
 
383
617
    def test_from_fp(self):
384
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
385
 
        my_config = config.IniBasedConfig(None)
386
 
        self.failUnless(
387
 
            isinstance(my_config._get_parser(file=config_file),
388
 
                        configobj.ConfigObj))
 
618
        my_config = config.IniBasedConfig.from_string(sample_config_text)
 
619
        self.assertIsInstance(my_config._get_parser(), configobj.ConfigObj)
389
620
 
390
621
    def test_cached(self):
391
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
392
 
        my_config = config.IniBasedConfig(None)
393
 
        parser = my_config._get_parser(file=config_file)
394
 
        self.failUnless(my_config._get_parser() is parser)
 
622
        my_config = config.IniBasedConfig.from_string(sample_config_text)
 
623
        parser = my_config._get_parser()
 
624
        self.assertTrue(my_config._get_parser() is parser)
 
625
 
 
626
    def _dummy_chown(self, path, uid, gid):
 
627
        self.path, self.uid, self.gid = path, uid, gid
 
628
 
 
629
    def test_ini_config_ownership(self):
 
630
        """Ensure that chown is happening during _write_config_file"""
 
631
        self.requireFeature(features.chown_feature)
 
632
        self.overrideAttr(os, 'chown', self._dummy_chown)
 
633
        self.path = self.uid = self.gid = None
 
634
        conf = config.IniBasedConfig(file_name='./foo.conf')
 
635
        conf._write_config_file()
 
636
        self.assertEqual(self.path, './foo.conf')
 
637
        self.assertTrue(isinstance(self.uid, int))
 
638
        self.assertTrue(isinstance(self.gid, int))
 
639
 
 
640
 
 
641
class TestIniConfigSaving(tests.TestCaseInTempDir):
 
642
 
 
643
    def test_cant_save_without_a_file_name(self):
 
644
        conf = config.IniBasedConfig()
 
645
        self.assertRaises(AssertionError, conf._write_config_file)
 
646
 
 
647
    def test_saved_with_content(self):
 
648
        content = b'foo = bar\n'
 
649
        config.IniBasedConfig.from_string(content, file_name='./test.conf',
 
650
                                          save=True)
 
651
        self.assertFileEqual(content, 'test.conf')
 
652
 
 
653
 
 
654
class TestIniConfigOptionExpansion(tests.TestCase):
 
655
    """Test option expansion from the IniConfig level.
 
656
 
 
657
    What we really want here is to test the Config level, but the class being
 
658
    abstract as far as storing values is concerned, this can't be done
 
659
    properly (yet).
 
660
    """
 
661
    # FIXME: This should be rewritten when all configs share a storage
 
662
    # implementation -- vila 2011-02-18
 
663
 
 
664
    def get_config(self, string=None):
 
665
        if string is None:
 
666
            string = b''
 
667
        c = config.IniBasedConfig.from_string(string)
 
668
        return c
 
669
 
 
670
    def assertExpansion(self, expected, conf, string, env=None):
 
671
        self.assertEqual(expected, conf.expand_options(string, env))
 
672
 
 
673
    def test_no_expansion(self):
 
674
        c = self.get_config('')
 
675
        self.assertExpansion('foo', c, 'foo')
 
676
 
 
677
    def test_env_adding_options(self):
 
678
        c = self.get_config('')
 
679
        self.assertExpansion('bar', c, '{foo}', {'foo': 'bar'})
 
680
 
 
681
    def test_env_overriding_options(self):
 
682
        c = self.get_config('foo=baz')
 
683
        self.assertExpansion('bar', c, '{foo}', {'foo': 'bar'})
 
684
 
 
685
    def test_simple_ref(self):
 
686
        c = self.get_config('foo=xxx')
 
687
        self.assertExpansion('xxx', c, '{foo}')
 
688
 
 
689
    def test_unknown_ref(self):
 
690
        c = self.get_config('')
 
691
        self.assertRaises(config.ExpandingUnknownOption,
 
692
                          c.expand_options, '{foo}')
 
693
 
 
694
    def test_indirect_ref(self):
 
695
        c = self.get_config('''
 
696
foo=xxx
 
697
bar={foo}
 
698
''')
 
699
        self.assertExpansion('xxx', c, '{bar}')
 
700
 
 
701
    def test_embedded_ref(self):
 
702
        c = self.get_config('''
 
703
foo=xxx
 
704
bar=foo
 
705
''')
 
706
        self.assertExpansion('xxx', c, '{{bar}}')
 
707
 
 
708
    def test_simple_loop(self):
 
709
        c = self.get_config('foo={foo}')
 
710
        self.assertRaises(config.OptionExpansionLoop, c.expand_options,
 
711
                          '{foo}')
 
712
 
 
713
    def test_indirect_loop(self):
 
714
        c = self.get_config('''
 
715
foo={bar}
 
716
bar={baz}
 
717
baz={foo}''')
 
718
        e = self.assertRaises(config.OptionExpansionLoop,
 
719
                              c.expand_options, '{foo}')
 
720
        self.assertEqual('foo->bar->baz', e.refs)
 
721
        self.assertEqual('{foo}', e.string)
 
722
 
 
723
    def test_list(self):
 
724
        conf = self.get_config('''
 
725
foo=start
 
726
bar=middle
 
727
baz=end
 
728
list={foo},{bar},{baz}
 
729
''')
 
730
        self.assertEqual(['start', 'middle', 'end'],
 
731
                           conf.get_user_option('list', expand=True))
 
732
 
 
733
    def test_cascading_list(self):
 
734
        conf = self.get_config('''
 
735
foo=start,{bar}
 
736
bar=middle,{baz}
 
737
baz=end
 
738
list={foo}
 
739
''')
 
740
        self.assertEqual(['start', 'middle', 'end'],
 
741
                           conf.get_user_option('list', expand=True))
 
742
 
 
743
    def test_pathological_hidden_list(self):
 
744
        conf = self.get_config('''
 
745
foo=bin
 
746
bar=go
 
747
start={foo
 
748
middle=},{
 
749
end=bar}
 
750
hidden={start}{middle}{end}
 
751
''')
 
752
        # Nope, it's either a string or a list, and the list wins as soon as a
 
753
        # ',' appears, so the string concatenation never occur.
 
754
        self.assertEqual(['{foo', '}', '{', 'bar}'],
 
755
                          conf.get_user_option('hidden', expand=True))
 
756
 
 
757
 
 
758
class TestLocationConfigOptionExpansion(tests.TestCaseInTempDir):
 
759
 
 
760
    def get_config(self, location, string=None):
 
761
        if string is None:
 
762
            string = ''
 
763
        # Since we don't save the config we won't strictly require to inherit
 
764
        # from TestCaseInTempDir, but an error occurs so quickly...
 
765
        c = config.LocationConfig.from_string(string, location)
 
766
        return c
 
767
 
 
768
    def test_dont_cross_unrelated_section(self):
 
769
        c = self.get_config('/another/branch/path', '''
 
770
[/one/branch/path]
 
771
foo = hello
 
772
bar = {foo}/2
 
773
 
 
774
[/another/branch/path]
 
775
bar = {foo}/2
 
776
''')
 
777
        self.assertRaises(config.ExpandingUnknownOption,
 
778
                          c.get_user_option, 'bar', expand=True)
 
779
 
 
780
    def test_cross_related_sections(self):
 
781
        c = self.get_config('/project/branch/path', '''
 
782
[/project]
 
783
foo = qu
 
784
 
 
785
[/project/branch/path]
 
786
bar = {foo}ux
 
787
''')
 
788
        self.assertEqual('quux', c.get_user_option('bar', expand=True))
 
789
 
 
790
 
 
791
class TestIniBaseConfigOnDisk(tests.TestCaseInTempDir):
 
792
 
 
793
    def test_cannot_reload_without_name(self):
 
794
        conf = config.IniBasedConfig.from_string(sample_config_text)
 
795
        self.assertRaises(AssertionError, conf.reload)
 
796
 
 
797
    def test_reload_see_new_value(self):
 
798
        c1 = config.IniBasedConfig.from_string('editor=vim\n',
 
799
                                               file_name='./test/conf')
 
800
        c1._write_config_file()
 
801
        c2 = config.IniBasedConfig.from_string('editor=emacs\n',
 
802
                                               file_name='./test/conf')
 
803
        c2._write_config_file()
 
804
        self.assertEqual('vim', c1.get_user_option('editor'))
 
805
        self.assertEqual('emacs', c2.get_user_option('editor'))
 
806
        # Make sure we get the Right value
 
807
        c1.reload()
 
808
        self.assertEqual('emacs', c1.get_user_option('editor'))
 
809
 
 
810
 
 
811
class TestLockableConfig(tests.TestCaseInTempDir):
 
812
 
 
813
    scenarios = lockable_config_scenarios()
 
814
 
 
815
    # Set by load_tests
 
816
    config_class = None
 
817
    config_args = None
 
818
    config_section = None
 
819
 
 
820
    def setUp(self):
 
821
        super(TestLockableConfig, self).setUp()
 
822
        self._content = '[%s]\none=1\ntwo=2\n' % (self.config_section,)
 
823
        self.config = self.create_config(self._content)
 
824
 
 
825
    def get_existing_config(self):
 
826
        return self.config_class(*self.config_args)
 
827
 
 
828
    def create_config(self, content):
 
829
        kwargs = dict(save=True)
 
830
        c = self.config_class.from_string(content, *self.config_args, **kwargs)
 
831
        return c
 
832
 
 
833
    def test_simple_read_access(self):
 
834
        self.assertEqual('1', self.config.get_user_option('one'))
 
835
 
 
836
    def test_simple_write_access(self):
 
837
        self.config.set_user_option('one', 'one')
 
838
        self.assertEqual('one', self.config.get_user_option('one'))
 
839
 
 
840
    def test_listen_to_the_last_speaker(self):
 
841
        c1 = self.config
 
842
        c2 = self.get_existing_config()
 
843
        c1.set_user_option('one', 'ONE')
 
844
        c2.set_user_option('two', 'TWO')
 
845
        self.assertEqual('ONE', c1.get_user_option('one'))
 
846
        self.assertEqual('TWO', c2.get_user_option('two'))
 
847
        # The second update respect the first one
 
848
        self.assertEqual('ONE', c2.get_user_option('one'))
 
849
 
 
850
    def test_last_speaker_wins(self):
 
851
        # If the same config is not shared, the same variable modified twice
 
852
        # can only see a single result.
 
853
        c1 = self.config
 
854
        c2 = self.get_existing_config()
 
855
        c1.set_user_option('one', 'c1')
 
856
        c2.set_user_option('one', 'c2')
 
857
        self.assertEqual('c2', c2._get_user_option('one'))
 
858
        # The first modification is still available until another refresh
 
859
        # occur
 
860
        self.assertEqual('c1', c1._get_user_option('one'))
 
861
        c1.set_user_option('two', 'done')
 
862
        self.assertEqual('c2', c1._get_user_option('one'))
 
863
 
 
864
    def test_writes_are_serialized(self):
 
865
        c1 = self.config
 
866
        c2 = self.get_existing_config()
 
867
 
 
868
        # We spawn a thread that will pause *during* the write
 
869
        before_writing = threading.Event()
 
870
        after_writing = threading.Event()
 
871
        writing_done = threading.Event()
 
872
        c1_orig = c1._write_config_file
 
873
        def c1_write_config_file():
 
874
            before_writing.set()
 
875
            c1_orig()
 
876
            # The lock is held. We wait for the main thread to decide when to
 
877
            # continue
 
878
            after_writing.wait()
 
879
        c1._write_config_file = c1_write_config_file
 
880
        def c1_set_option():
 
881
            c1.set_user_option('one', 'c1')
 
882
            writing_done.set()
 
883
        t1 = threading.Thread(target=c1_set_option)
 
884
        # Collect the thread after the test
 
885
        self.addCleanup(t1.join)
 
886
        # Be ready to unblock the thread if the test goes wrong
 
887
        self.addCleanup(after_writing.set)
 
888
        t1.start()
 
889
        before_writing.wait()
 
890
        self.assertTrue(c1._lock.is_held)
 
891
        self.assertRaises(errors.LockContention,
 
892
                          c2.set_user_option, 'one', 'c2')
 
893
        self.assertEqual('c1', c1.get_user_option('one'))
 
894
        # Let the lock be released
 
895
        after_writing.set()
 
896
        writing_done.wait()
 
897
        c2.set_user_option('one', 'c2')
 
898
        self.assertEqual('c2', c2.get_user_option('one'))
 
899
 
 
900
    def test_read_while_writing(self):
 
901
       c1 = self.config
 
902
       # We spawn a thread that will pause *during* the write
 
903
       ready_to_write = threading.Event()
 
904
       do_writing = threading.Event()
 
905
       writing_done = threading.Event()
 
906
       c1_orig = c1._write_config_file
 
907
       def c1_write_config_file():
 
908
           ready_to_write.set()
 
909
           # The lock is held. We wait for the main thread to decide when to
 
910
           # continue
 
911
           do_writing.wait()
 
912
           c1_orig()
 
913
           writing_done.set()
 
914
       c1._write_config_file = c1_write_config_file
 
915
       def c1_set_option():
 
916
           c1.set_user_option('one', 'c1')
 
917
       t1 = threading.Thread(target=c1_set_option)
 
918
       # Collect the thread after the test
 
919
       self.addCleanup(t1.join)
 
920
       # Be ready to unblock the thread if the test goes wrong
 
921
       self.addCleanup(do_writing.set)
 
922
       t1.start()
 
923
       # Ensure the thread is ready to write
 
924
       ready_to_write.wait()
 
925
       self.assertTrue(c1._lock.is_held)
 
926
       self.assertEqual('c1', c1.get_user_option('one'))
 
927
       # If we read during the write, we get the old value
 
928
       c2 = self.get_existing_config()
 
929
       self.assertEqual('1', c2.get_user_option('one'))
 
930
       # Let the writing occur and ensure it occurred
 
931
       do_writing.set()
 
932
       writing_done.wait()
 
933
       # Now we get the updated value
 
934
       c3 = self.get_existing_config()
 
935
       self.assertEqual('c1', c3.get_user_option('one'))
395
936
 
396
937
 
397
938
class TestGetUserOptionAs(TestIniConfig):
412
953
        self.overrideAttr(trace, 'warning', warning)
413
954
        msg = 'Value "%s" is not a boolean for "%s"'
414
955
        self.assertIs(None, get_bool('an_invalid_bool'))
415
 
        self.assertEquals(msg % ('maybe', 'an_invalid_bool'), warnings[0])
 
956
        self.assertEqual(msg % ('maybe', 'an_invalid_bool'), warnings[0])
416
957
        warnings = []
417
958
        self.assertIs(None, get_bool('not_defined_in_this_config'))
418
 
        self.assertEquals([], warnings)
 
959
        self.assertEqual([], warnings)
419
960
 
420
961
    def test_get_user_option_as_list(self):
421
962
        conf, parser = self.make_config_parser("""
448
989
        self.assertEqual(True, suppress_warning('b'))
449
990
 
450
991
 
451
 
class TestGetConfig(tests.TestCase):
 
992
class TestGetConfig(tests.TestCaseInTempDir):
452
993
 
453
994
    def test_constructs(self):
454
 
        my_config = config.GlobalConfig()
 
995
        config.GlobalConfig()
455
996
 
456
997
    def test_calls_read_filenames(self):
457
998
        # replace the class that is constructed, to check its parameters
462
1003
            parser = my_config._get_parser()
463
1004
        finally:
464
1005
            config.ConfigObj = oldparserclass
465
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
 
1006
        self.assertIsInstance(parser, InstrumentedConfigObj)
466
1007
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
467
1008
                                          'utf-8')])
468
1009
 
469
1010
 
470
1011
class TestBranchConfig(tests.TestCaseWithTransport):
471
1012
 
472
 
    def test_constructs(self):
 
1013
    def test_constructs_valid(self):
473
1014
        branch = FakeBranch()
474
1015
        my_config = config.BranchConfig(branch)
 
1016
        self.assertIsNot(None, my_config)
 
1017
 
 
1018
    def test_constructs_error(self):
475
1019
        self.assertRaises(TypeError, config.BranchConfig)
476
1020
 
477
1021
    def test_get_location_config(self):
479
1023
        my_config = config.BranchConfig(branch)
480
1024
        location_config = my_config._get_location_config()
481
1025
        self.assertEqual(branch.base, location_config.location)
482
 
        self.failUnless(location_config is my_config._get_location_config())
 
1026
        self.assertIs(location_config, my_config._get_location_config())
483
1027
 
484
1028
    def test_get_config(self):
485
1029
        """The Branch.get_config method works properly"""
486
 
        b = bzrdir.BzrDir.create_standalone_workingtree('.').branch
 
1030
        b = controldir.ControlDir.create_standalone_workingtree('.').branch
487
1031
        my_config = b.get_config()
488
1032
        self.assertIs(my_config.get_user_option('wacky'), None)
489
1033
        my_config.set_user_option('wacky', 'unlikely')
505
1049
        branch = self.make_branch('branch')
506
1050
        self.assertEqual('branch', branch.nick)
507
1051
 
508
 
        locations = config.locations_config_filename()
509
 
        config.ensure_config_dir_exists()
510
1052
        local_url = urlutils.local_path_to_url('branch')
511
 
        open(locations, 'wb').write('[%s]\nnickname = foobar'
512
 
                                    % (local_url,))
 
1053
        conf = config.LocationConfig.from_string(
 
1054
            '[%s]\nnickname = foobar' % (local_url,),
 
1055
            local_url, save=True)
 
1056
        self.assertIsNot(None, conf)
513
1057
        self.assertEqual('foobar', branch.nick)
514
1058
 
515
1059
    def test_config_local_path(self):
517
1061
        branch = self.make_branch('branch')
518
1062
        self.assertEqual('branch', branch.nick)
519
1063
 
520
 
        locations = config.locations_config_filename()
521
 
        config.ensure_config_dir_exists()
522
 
        open(locations, 'wb').write('[%s/branch]\nnickname = barry'
523
 
                                    % (osutils.getcwd().encode('utf8'),))
 
1064
        local_path = osutils.getcwd().encode('utf8')
 
1065
        config.LocationConfig.from_string(
 
1066
            b'[%s/branch]\nnickname = barry' % (local_path,),
 
1067
            'branch',  save=True)
 
1068
        # Now the branch will find its nick via the location config
524
1069
        self.assertEqual('barry', branch.nick)
525
1070
 
526
1071
    def test_config_creates_local(self):
527
1072
        """Creating a new entry in config uses a local path."""
528
1073
        branch = self.make_branch('branch', format='knit')
529
1074
        branch.set_push_location('http://foobar')
530
 
        locations = config.locations_config_filename()
531
1075
        local_path = osutils.getcwd().encode('utf8')
532
1076
        # Surprisingly ConfigObj doesn't create a trailing newline
533
 
        self.check_file_contents(locations,
534
 
                                 '[%s/branch]\n'
535
 
                                 'push_location = http://foobar\n'
536
 
                                 'push_location:policy = norecurse\n'
 
1077
        self.check_file_contents(config.locations_config_filename(),
 
1078
                                 b'[%s/branch]\n'
 
1079
                                 b'push_location = http://foobar\n'
 
1080
                                 b'push_location:policy = norecurse\n'
537
1081
                                 % (local_path,))
538
1082
 
539
1083
    def test_autonick_urlencoded(self):
540
1084
        b = self.make_branch('!repo')
541
1085
        self.assertEqual('!repo', b.get_config().get_nickname())
542
1086
 
 
1087
    def test_autonick_uses_branch_name(self):
 
1088
        b = self.make_branch('foo', name='bar')
 
1089
        self.assertEqual('bar', b.get_config().get_nickname())
 
1090
 
543
1091
    def test_warn_if_masked(self):
544
 
        _warning = trace.warning
545
1092
        warnings = []
546
1093
        def warning(*args):
547
1094
            warnings.append(args[0] % args[1:])
 
1095
        self.overrideAttr(trace, 'warning', warning)
548
1096
 
549
1097
        def set_option(store, warn_masked=True):
550
1098
            warnings[:] = []
556
1104
            else:
557
1105
                self.assertEqual(1, len(warnings))
558
1106
                self.assertEqual(warning, warnings[0])
559
 
        trace.warning = warning
560
 
        try:
561
 
            branch = self.make_branch('.')
562
 
            conf = branch.get_config()
563
 
            set_option(config.STORE_GLOBAL)
564
 
            assertWarning(None)
565
 
            set_option(config.STORE_BRANCH)
566
 
            assertWarning(None)
567
 
            set_option(config.STORE_GLOBAL)
568
 
            assertWarning('Value "4" is masked by "3" from branch.conf')
569
 
            set_option(config.STORE_GLOBAL, warn_masked=False)
570
 
            assertWarning(None)
571
 
            set_option(config.STORE_LOCATION)
572
 
            assertWarning(None)
573
 
            set_option(config.STORE_BRANCH)
574
 
            assertWarning('Value "3" is masked by "0" from locations.conf')
575
 
            set_option(config.STORE_BRANCH, warn_masked=False)
576
 
            assertWarning(None)
577
 
        finally:
578
 
            trace.warning = _warning
579
 
 
580
 
 
581
 
class TestGlobalConfigItems(tests.TestCase):
 
1107
        branch = self.make_branch('.')
 
1108
        conf = branch.get_config()
 
1109
        set_option(config.STORE_GLOBAL)
 
1110
        assertWarning(None)
 
1111
        set_option(config.STORE_BRANCH)
 
1112
        assertWarning(None)
 
1113
        set_option(config.STORE_GLOBAL)
 
1114
        assertWarning('Value "4" is masked by "3" from branch.conf')
 
1115
        set_option(config.STORE_GLOBAL, warn_masked=False)
 
1116
        assertWarning(None)
 
1117
        set_option(config.STORE_LOCATION)
 
1118
        assertWarning(None)
 
1119
        set_option(config.STORE_BRANCH)
 
1120
        assertWarning('Value "3" is masked by "0" from locations.conf')
 
1121
        set_option(config.STORE_BRANCH, warn_masked=False)
 
1122
        assertWarning(None)
 
1123
 
 
1124
 
 
1125
class TestGlobalConfigItems(tests.TestCaseInTempDir):
 
1126
 
 
1127
    def _get_empty_config(self):
 
1128
        my_config = config.GlobalConfig()
 
1129
        return my_config
 
1130
 
 
1131
    def _get_sample_config(self):
 
1132
        my_config = config.GlobalConfig.from_string(sample_config_text)
 
1133
        return my_config
582
1134
 
583
1135
    def test_user_id(self):
584
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
585
 
        my_config = config.GlobalConfig()
586
 
        my_config._parser = my_config._get_parser(file=config_file)
 
1136
        my_config = config.GlobalConfig.from_string(sample_config_text)
587
1137
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
588
1138
                         my_config._get_user_id())
589
1139
 
590
1140
    def test_absent_user_id(self):
591
 
        config_file = StringIO("")
592
1141
        my_config = config.GlobalConfig()
593
 
        my_config._parser = my_config._get_parser(file=config_file)
594
1142
        self.assertEqual(None, my_config._get_user_id())
595
1143
 
596
 
    def test_configured_editor(self):
597
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
598
 
        my_config = config.GlobalConfig()
599
 
        my_config._parser = my_config._get_parser(file=config_file)
600
 
        self.assertEqual("vim", my_config.get_editor())
601
 
 
602
 
    def test_signatures_always(self):
603
 
        config_file = StringIO(sample_always_signatures)
604
 
        my_config = config.GlobalConfig()
605
 
        my_config._parser = my_config._get_parser(file=config_file)
606
 
        self.assertEqual(config.CHECK_NEVER,
607
 
                         my_config.signature_checking())
608
 
        self.assertEqual(config.SIGN_ALWAYS,
609
 
                         my_config.signing_policy())
610
 
        self.assertEqual(True, my_config.signature_needed())
611
 
 
612
 
    def test_signatures_if_possible(self):
613
 
        config_file = StringIO(sample_maybe_signatures)
614
 
        my_config = config.GlobalConfig()
615
 
        my_config._parser = my_config._get_parser(file=config_file)
616
 
        self.assertEqual(config.CHECK_NEVER,
617
 
                         my_config.signature_checking())
618
 
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
619
 
                         my_config.signing_policy())
620
 
        self.assertEqual(False, my_config.signature_needed())
621
 
 
622
 
    def test_signatures_ignore(self):
623
 
        config_file = StringIO(sample_ignore_signatures)
624
 
        my_config = config.GlobalConfig()
625
 
        my_config._parser = my_config._get_parser(file=config_file)
626
 
        self.assertEqual(config.CHECK_ALWAYS,
627
 
                         my_config.signature_checking())
628
 
        self.assertEqual(config.SIGN_NEVER,
629
 
                         my_config.signing_policy())
630
 
        self.assertEqual(False, my_config.signature_needed())
631
 
 
632
 
    def _get_sample_config(self):
633
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
634
 
        my_config = config.GlobalConfig()
635
 
        my_config._parser = my_config._get_parser(file=config_file)
636
 
        return my_config
637
 
 
638
 
    def test_gpg_signing_command(self):
639
 
        my_config = self._get_sample_config()
640
 
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
641
 
        self.assertEqual(False, my_config.signature_needed())
642
 
 
643
 
    def _get_empty_config(self):
644
 
        config_file = StringIO("")
645
 
        my_config = config.GlobalConfig()
646
 
        my_config._parser = my_config._get_parser(file=config_file)
647
 
        return my_config
648
 
 
649
 
    def test_gpg_signing_command_unset(self):
650
 
        my_config = self._get_empty_config()
651
 
        self.assertEqual("gpg", my_config.gpg_signing_command())
652
 
 
653
1144
    def test_get_user_option_default(self):
654
1145
        my_config = self._get_empty_config()
655
1146
        self.assertEqual(None, my_config.get_user_option('no_option'))
659
1150
        self.assertEqual("something",
660
1151
                         my_config.get_user_option('user_global_option'))
661
1152
 
662
 
    def test_post_commit_default(self):
663
 
        my_config = self._get_sample_config()
664
 
        self.assertEqual(None, my_config.post_commit())
665
 
 
666
 
    def test_configured_logformat(self):
667
 
        my_config = self._get_sample_config()
668
 
        self.assertEqual("short", my_config.log_format())
 
1153
    def test_configured_validate_signatures_in_log(self):
 
1154
        my_config = self._get_sample_config()
 
1155
        self.assertEqual(True, my_config.validate_signatures_in_log())
669
1156
 
670
1157
    def test_get_alias(self):
671
1158
        my_config = self._get_sample_config()
699
1186
        change_editor = my_config.get_change_editor('old', 'new')
700
1187
        self.assertIs(None, change_editor)
701
1188
 
 
1189
    def test_get_merge_tools(self):
 
1190
        conf = self._get_sample_config()
 
1191
        tools = conf.get_merge_tools()
 
1192
        self.log(repr(tools))
 
1193
        self.assertEqual(
 
1194
            {u'funkytool' : u'funkytool "arg with spaces" {this_temp}',
 
1195
            u'sometool' : u'sometool {base} {this} {other} -o {result}',
 
1196
            u'newtool' : u'"newtool with spaces" {this_temp}'},
 
1197
            tools)
 
1198
 
 
1199
    def test_get_merge_tools_empty(self):
 
1200
        conf = self._get_empty_config()
 
1201
        tools = conf.get_merge_tools()
 
1202
        self.assertEqual({}, tools)
 
1203
 
 
1204
    def test_find_merge_tool(self):
 
1205
        conf = self._get_sample_config()
 
1206
        cmdline = conf.find_merge_tool('sometool')
 
1207
        self.assertEqual('sometool {base} {this} {other} -o {result}', cmdline)
 
1208
 
 
1209
    def test_find_merge_tool_not_found(self):
 
1210
        conf = self._get_sample_config()
 
1211
        cmdline = conf.find_merge_tool('DOES NOT EXIST')
 
1212
        self.assertIs(cmdline, None)
 
1213
 
 
1214
    def test_find_merge_tool_known(self):
 
1215
        conf = self._get_empty_config()
 
1216
        cmdline = conf.find_merge_tool('kdiff3')
 
1217
        self.assertEqual('kdiff3 {base} {this} {other} -o {result}', cmdline)
 
1218
 
 
1219
    def test_find_merge_tool_override_known(self):
 
1220
        conf = self._get_empty_config()
 
1221
        conf.set_user_option('bzr.mergetool.kdiff3', 'kdiff3 blah')
 
1222
        cmdline = conf.find_merge_tool('kdiff3')
 
1223
        self.assertEqual('kdiff3 blah', cmdline)
 
1224
 
702
1225
 
703
1226
class TestGlobalConfigSavingOptions(tests.TestCaseInTempDir):
704
1227
 
722
1245
        self.assertIs(None, new_config.get_alias('commit'))
723
1246
 
724
1247
 
725
 
class TestLocationConfig(tests.TestCaseInTempDir):
726
 
 
727
 
    def test_constructs(self):
728
 
        my_config = config.LocationConfig('http://example.com')
 
1248
class TestLocationConfig(tests.TestCaseInTempDir, TestOptionsMixin):
 
1249
 
 
1250
    def test_constructs_valid(self):
 
1251
        config.LocationConfig('http://example.com')
 
1252
 
 
1253
    def test_constructs_error(self):
729
1254
        self.assertRaises(TypeError, config.LocationConfig)
730
1255
 
731
1256
    def test_branch_calls_read_filenames(self):
740
1265
            parser = my_config._get_parser()
741
1266
        finally:
742
1267
            config.ConfigObj = oldparserclass
743
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
 
1268
        self.assertIsInstance(parser, InstrumentedConfigObj)
744
1269
        self.assertEqual(parser._calls,
745
1270
                         [('__init__', config.locations_config_filename(),
746
1271
                           'utf-8')])
747
 
        config.ensure_config_dir_exists()
748
 
        #os.mkdir(config.config_dir())
749
 
        f = file(config.branches_config_filename(), 'wb')
750
 
        f.write('')
751
 
        f.close()
752
 
        oldparserclass = config.ConfigObj
753
 
        config.ConfigObj = InstrumentedConfigObj
754
 
        try:
755
 
            my_config = config.LocationConfig('http://www.example.com')
756
 
            parser = my_config._get_parser()
757
 
        finally:
758
 
            config.ConfigObj = oldparserclass
759
1272
 
760
1273
    def test_get_global_config(self):
761
1274
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
762
1275
        global_config = my_config._get_global_config()
763
 
        self.failUnless(isinstance(global_config, config.GlobalConfig))
764
 
        self.failUnless(global_config is my_config._get_global_config())
 
1276
        self.assertIsInstance(global_config, config.GlobalConfig)
 
1277
        self.assertIs(global_config, my_config._get_global_config())
 
1278
 
 
1279
    def assertLocationMatching(self, expected):
 
1280
        self.assertEqual(expected,
 
1281
                         list(self.my_location_config._get_matching_sections()))
765
1282
 
766
1283
    def test__get_matching_sections_no_match(self):
767
1284
        self.get_branch_config('/')
768
 
        self.assertEqual([], self.my_location_config._get_matching_sections())
 
1285
        self.assertLocationMatching([])
769
1286
 
770
1287
    def test__get_matching_sections_exact(self):
771
1288
        self.get_branch_config('http://www.example.com')
772
 
        self.assertEqual([('http://www.example.com', '')],
773
 
                         self.my_location_config._get_matching_sections())
 
1289
        self.assertLocationMatching([('http://www.example.com', '')])
774
1290
 
775
1291
    def test__get_matching_sections_suffix_does_not(self):
776
1292
        self.get_branch_config('http://www.example.com-com')
777
 
        self.assertEqual([], self.my_location_config._get_matching_sections())
 
1293
        self.assertLocationMatching([])
778
1294
 
779
1295
    def test__get_matching_sections_subdir_recursive(self):
780
1296
        self.get_branch_config('http://www.example.com/com')
781
 
        self.assertEqual([('http://www.example.com', 'com')],
782
 
                         self.my_location_config._get_matching_sections())
 
1297
        self.assertLocationMatching([('http://www.example.com', 'com')])
783
1298
 
784
1299
    def test__get_matching_sections_ignoreparent(self):
785
1300
        self.get_branch_config('http://www.example.com/ignoreparent')
786
 
        self.assertEqual([('http://www.example.com/ignoreparent', '')],
787
 
                         self.my_location_config._get_matching_sections())
 
1301
        self.assertLocationMatching([('http://www.example.com/ignoreparent',
 
1302
                                      '')])
788
1303
 
789
1304
    def test__get_matching_sections_ignoreparent_subdir(self):
790
1305
        self.get_branch_config(
791
1306
            'http://www.example.com/ignoreparent/childbranch')
792
 
        self.assertEqual([('http://www.example.com/ignoreparent',
793
 
                           'childbranch')],
794
 
                         self.my_location_config._get_matching_sections())
 
1307
        self.assertLocationMatching([('http://www.example.com/ignoreparent',
 
1308
                                      'childbranch')])
795
1309
 
796
1310
    def test__get_matching_sections_subdir_trailing_slash(self):
797
1311
        self.get_branch_config('/b')
798
 
        self.assertEqual([('/b/', '')],
799
 
                         self.my_location_config._get_matching_sections())
 
1312
        self.assertLocationMatching([('/b/', '')])
800
1313
 
801
1314
    def test__get_matching_sections_subdir_child(self):
802
1315
        self.get_branch_config('/a/foo')
803
 
        self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
804
 
                         self.my_location_config._get_matching_sections())
 
1316
        self.assertLocationMatching([('/a/*', ''), ('/a/', 'foo')])
805
1317
 
806
1318
    def test__get_matching_sections_subdir_child_child(self):
807
1319
        self.get_branch_config('/a/foo/bar')
808
 
        self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
809
 
                         self.my_location_config._get_matching_sections())
 
1320
        self.assertLocationMatching([('/a/*', 'bar'), ('/a/', 'foo/bar')])
810
1321
 
811
1322
    def test__get_matching_sections_trailing_slash_with_children(self):
812
1323
        self.get_branch_config('/a/')
813
 
        self.assertEqual([('/a/', '')],
814
 
                         self.my_location_config._get_matching_sections())
 
1324
        self.assertLocationMatching([('/a/', '')])
815
1325
 
816
1326
    def test__get_matching_sections_explicit_over_glob(self):
817
1327
        # XXX: 2006-09-08 jamesh
819
1329
        # was a config section for '/a/?', it would get precedence
820
1330
        # over '/a/c'.
821
1331
        self.get_branch_config('/a/c')
822
 
        self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
823
 
                         self.my_location_config._get_matching_sections())
 
1332
        self.assertLocationMatching([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')])
824
1333
 
825
1334
    def test__get_option_policy_normal(self):
826
1335
        self.get_branch_config('http://www.example.com')
848
1357
            'http://www.example.com', 'appendpath_option'),
849
1358
            config.POLICY_APPENDPATH)
850
1359
 
 
1360
    def test__get_options_with_policy(self):
 
1361
        self.get_branch_config('/dir/subdir',
 
1362
                               location_config="""\
 
1363
[/dir]
 
1364
other_url = /other-dir
 
1365
other_url:policy = appendpath
 
1366
[/dir/subdir]
 
1367
other_url = /other-subdir
 
1368
""")
 
1369
        self.assertOptions(
 
1370
            [(u'other_url', u'/other-subdir', u'/dir/subdir', 'locations'),
 
1371
             (u'other_url', u'/other-dir', u'/dir', 'locations'),
 
1372
             (u'other_url:policy', u'appendpath', u'/dir', 'locations')],
 
1373
            self.my_location_config)
 
1374
 
851
1375
    def test_location_without_username(self):
852
1376
        self.get_branch_config('http://www.example.com/ignoreparent')
853
1377
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
864
1388
        self.assertEqual('Robert Collins <robertc@example.org>',
865
1389
                         self.my_config.username())
866
1390
 
867
 
    def test_signatures_not_set(self):
868
 
        self.get_branch_config('http://www.example.com',
869
 
                                 global_config=sample_ignore_signatures)
870
 
        self.assertEqual(config.CHECK_ALWAYS,
871
 
                         self.my_config.signature_checking())
872
 
        self.assertEqual(config.SIGN_NEVER,
873
 
                         self.my_config.signing_policy())
874
 
 
875
 
    def test_signatures_never(self):
876
 
        self.get_branch_config('/a/c')
877
 
        self.assertEqual(config.CHECK_NEVER,
878
 
                         self.my_config.signature_checking())
879
 
 
880
 
    def test_signatures_when_available(self):
881
 
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
882
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
883
 
                         self.my_config.signature_checking())
884
 
 
885
 
    def test_signatures_always(self):
886
 
        self.get_branch_config('/b')
887
 
        self.assertEqual(config.CHECK_ALWAYS,
888
 
                         self.my_config.signature_checking())
889
 
 
890
 
    def test_gpg_signing_command(self):
891
 
        self.get_branch_config('/b')
892
 
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
893
 
 
894
 
    def test_gpg_signing_command_missing(self):
895
 
        self.get_branch_config('/a')
896
 
        self.assertEqual("false", self.my_config.gpg_signing_command())
897
 
 
898
1391
    def test_get_user_option_global(self):
899
1392
        self.get_branch_config('/a')
900
1393
        self.assertEqual('something',
964
1457
            'http://www.example.com', 'norecurse_option'),
965
1458
            config.POLICY_NONE)
966
1459
 
967
 
    def test_set_user_option_recurse_false_section(self):
968
 
        # The following section has recurse=False set.  The test is to
969
 
        # make sure that a normal option can be added to the section,
970
 
        # converting recurse=False to the norecurse policy.
971
 
        self.get_branch_config('http://www.example.com/norecurse')
972
 
        self.callDeprecated(['The recurse option is deprecated as of 0.14.  '
973
 
                             'The section "http://www.example.com/norecurse" '
974
 
                             'has been converted to use policies.'],
975
 
                            self.my_config.set_user_option,
976
 
                            'foo', 'bar', store=config.STORE_LOCATION)
977
 
        self.assertEqual(
978
 
            self.my_location_config._get_option_policy(
979
 
            'http://www.example.com/norecurse', 'foo'),
980
 
            config.POLICY_NONE)
981
 
        # The previously existing option is still norecurse:
982
 
        self.assertEqual(
983
 
            self.my_location_config._get_option_policy(
984
 
            'http://www.example.com/norecurse', 'normal_option'),
985
 
            config.POLICY_NORECURSE)
986
 
 
987
 
    def test_post_commit_default(self):
988
 
        self.get_branch_config('/a/c')
989
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
990
 
                         self.my_config.post_commit())
991
 
 
992
 
    def get_branch_config(self, location, global_config=None):
 
1460
    def get_branch_config(self, location, global_config=None,
 
1461
                          location_config=None):
 
1462
        my_branch = FakeBranch(location)
993
1463
        if global_config is None:
994
 
            global_file = StringIO(sample_config_text.encode('utf-8'))
995
 
        else:
996
 
            global_file = StringIO(global_config.encode('utf-8'))
997
 
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
998
 
        self.my_config = config.BranchConfig(FakeBranch(location))
999
 
        # Force location config to use specified file
1000
 
        self.my_location_config = self.my_config._get_location_config()
1001
 
        self.my_location_config._get_parser(branches_file)
1002
 
        # Force global config to use specified file
1003
 
        self.my_config._get_global_config()._get_parser(global_file)
1004
 
 
1005
 
    def test_set_user_setting_sets_and_saves(self):
1006
 
        self.get_branch_config('/a/c')
1007
 
        record = InstrumentedConfigObj("foo")
1008
 
        self.my_location_config._parser = record
1009
 
 
1010
 
        real_mkdir = os.mkdir
1011
 
        self.created = False
1012
 
        def checked_mkdir(path, mode=0777):
1013
 
            self.log('making directory: %s', path)
1014
 
            real_mkdir(path, mode)
1015
 
            self.created = True
1016
 
 
1017
 
        os.mkdir = checked_mkdir
1018
 
        try:
1019
 
            self.callDeprecated(['The recurse option is deprecated as of '
1020
 
                                 '0.14.  The section "/a/c" has been '
1021
 
                                 'converted to use policies.'],
1022
 
                                self.my_config.set_user_option,
1023
 
                                'foo', 'bar', store=config.STORE_LOCATION)
1024
 
        finally:
1025
 
            os.mkdir = real_mkdir
1026
 
 
1027
 
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
1028
 
        self.assertEqual([('__contains__', '/a/c'),
1029
 
                          ('__contains__', '/a/c/'),
1030
 
                          ('__setitem__', '/a/c', {}),
1031
 
                          ('__getitem__', '/a/c'),
1032
 
                          ('__setitem__', 'foo', 'bar'),
1033
 
                          ('__getitem__', '/a/c'),
1034
 
                          ('as_bool', 'recurse'),
1035
 
                          ('__getitem__', '/a/c'),
1036
 
                          ('__delitem__', 'recurse'),
1037
 
                          ('__getitem__', '/a/c'),
1038
 
                          ('keys',),
1039
 
                          ('__getitem__', '/a/c'),
1040
 
                          ('__contains__', 'foo:policy'),
1041
 
                          ('write',)],
1042
 
                         record._calls[1:])
 
1464
            global_config = sample_config_text
 
1465
        if location_config is None:
 
1466
            location_config = sample_branches_text
 
1467
 
 
1468
        config.GlobalConfig.from_string(global_config, save=True)
 
1469
        config.LocationConfig.from_string(location_config, my_branch.base,
 
1470
                                          save=True)
 
1471
        my_config = config.BranchConfig(my_branch)
 
1472
        self.my_config = my_config
 
1473
        self.my_location_config = my_config._get_location_config()
1043
1474
 
1044
1475
    def test_set_user_setting_sets_and_saves2(self):
1045
1476
        self.get_branch_config('/a/c')
1047
1478
        self.my_config.set_user_option('foo', 'bar')
1048
1479
        self.assertEqual(
1049
1480
            self.my_config.branch.control_files.files['branch.conf'].strip(),
1050
 
            'foo = bar')
 
1481
            b'foo = bar')
1051
1482
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
1052
1483
        self.my_config.set_user_option('foo', 'baz',
1053
1484
                                       store=config.STORE_LOCATION)
1060
1491
        self.assertEqual('bzr', my_config.get_bzr_remote_path())
1061
1492
        my_config.set_user_option('bzr_remote_path', '/path-bzr')
1062
1493
        self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
1063
 
        os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
 
1494
        self.overrideEnv('BZR_REMOTE_PATH', '/environ-bzr')
1064
1495
        self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
1065
1496
 
1066
1497
 
1074
1505
option = exact
1075
1506
"""
1076
1507
 
1077
 
 
1078
1508
class TestBranchConfigItems(tests.TestCaseInTempDir):
1079
1509
 
1080
1510
    def get_branch_config(self, global_config=None, location=None,
1081
1511
                          location_config=None, branch_data_config=None):
1082
 
        my_config = config.BranchConfig(FakeBranch(location))
 
1512
        my_branch = FakeBranch(location)
1083
1513
        if global_config is not None:
1084
 
            global_file = StringIO(global_config.encode('utf-8'))
1085
 
            my_config._get_global_config()._get_parser(global_file)
1086
 
        self.my_location_config = my_config._get_location_config()
 
1514
            config.GlobalConfig.from_string(global_config, save=True)
1087
1515
        if location_config is not None:
1088
 
            location_file = StringIO(location_config.encode('utf-8'))
1089
 
            self.my_location_config._get_parser(location_file)
 
1516
            config.LocationConfig.from_string(location_config, my_branch.base,
 
1517
                                              save=True)
 
1518
        my_config = config.BranchConfig(my_branch)
1090
1519
        if branch_data_config is not None:
1091
1520
            my_config.branch.control_files.files['branch.conf'] = \
1092
1521
                branch_data_config
1093
1522
        return my_config
1094
1523
 
1095
1524
    def test_user_id(self):
1096
 
        branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
 
1525
        branch = FakeBranch()
1097
1526
        my_config = config.BranchConfig(branch)
1098
 
        self.assertEqual("Robert Collins <robertc@example.net>",
1099
 
                         my_config.username())
 
1527
        self.assertIsNot(None, my_config.username())
1100
1528
        my_config.branch.control_files.files['email'] = "John"
1101
1529
        my_config.set_user_option('email',
1102
1530
                                  "Robert Collins <robertc@example.org>")
1103
 
        self.assertEqual("John", my_config.username())
1104
 
        del my_config.branch.control_files.files['email']
1105
1531
        self.assertEqual("Robert Collins <robertc@example.org>",
1106
 
                         my_config.username())
1107
 
 
1108
 
    def test_not_set_in_branch(self):
1109
 
        my_config = self.get_branch_config(sample_config_text)
1110
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
1111
 
                         my_config._get_user_id())
1112
 
        my_config.branch.control_files.files['email'] = "John"
1113
 
        self.assertEqual("John", my_config._get_user_id())
1114
 
 
1115
 
    def test_BZR_EMAIL_OVERRIDES(self):
1116
 
        os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
 
1532
                        my_config.username())
 
1533
 
 
1534
    def test_BRZ_EMAIL_OVERRIDES(self):
 
1535
        self.overrideEnv('BRZ_EMAIL', "Robert Collins <robertc@example.org>")
1117
1536
        branch = FakeBranch()
1118
1537
        my_config = config.BranchConfig(branch)
1119
1538
        self.assertEqual("Robert Collins <robertc@example.org>",
1120
1539
                         my_config.username())
1121
1540
 
1122
 
    def test_signatures_forced(self):
1123
 
        my_config = self.get_branch_config(
1124
 
            global_config=sample_always_signatures)
1125
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1126
 
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1127
 
        self.assertTrue(my_config.signature_needed())
1128
 
 
1129
 
    def test_signatures_forced_branch(self):
1130
 
        my_config = self.get_branch_config(
1131
 
            global_config=sample_ignore_signatures,
1132
 
            branch_data_config=sample_always_signatures)
1133
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1134
 
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1135
 
        self.assertTrue(my_config.signature_needed())
1136
 
 
1137
 
    def test_gpg_signing_command(self):
1138
 
        my_config = self.get_branch_config(
1139
 
            # branch data cannot set gpg_signing_command
1140
 
            branch_data_config="gpg_signing_command=pgp")
1141
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
1142
 
        my_config._get_global_config()._get_parser(config_file)
1143
 
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
1144
 
 
1145
1541
    def test_get_user_option_global(self):
1146
 
        branch = FakeBranch()
1147
 
        my_config = config.BranchConfig(branch)
1148
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
1149
 
        (my_config._get_global_config()._get_parser(config_file))
 
1542
        my_config = self.get_branch_config(global_config=sample_config_text)
1150
1543
        self.assertEqual('something',
1151
1544
                         my_config.get_user_option('user_global_option'))
1152
1545
 
1153
 
    def test_post_commit_default(self):
1154
 
        branch = FakeBranch()
1155
 
        my_config = self.get_branch_config(sample_config_text, '/a/c',
1156
 
                                           sample_branches_text)
1157
 
        self.assertEqual(my_config.branch.base, '/a/c')
1158
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1159
 
                         my_config.post_commit())
1160
 
        my_config.set_user_option('post_commit', 'rmtree_root')
1161
 
        # post-commit is ignored when bresent in branch data
1162
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1163
 
                         my_config.post_commit())
1164
 
        my_config.set_user_option('post_commit', 'rmtree_root',
1165
 
                                  store=config.STORE_LOCATION)
1166
 
        self.assertEqual('rmtree_root', my_config.post_commit())
1167
 
 
1168
1546
    def test_config_precedence(self):
 
1547
        # FIXME: eager test, luckily no persitent config file makes it fail
 
1548
        # -- vila 20100716
1169
1549
        my_config = self.get_branch_config(global_config=precedence_global)
1170
1550
        self.assertEqual(my_config.get_user_option('option'), 'global')
1171
1551
        my_config = self.get_branch_config(global_config=precedence_global,
1172
 
                                      branch_data_config=precedence_branch)
 
1552
                                           branch_data_config=precedence_branch)
1173
1553
        self.assertEqual(my_config.get_user_option('option'), 'branch')
1174
 
        my_config = self.get_branch_config(global_config=precedence_global,
1175
 
                                      branch_data_config=precedence_branch,
1176
 
                                      location_config=precedence_location)
 
1554
        my_config = self.get_branch_config(
 
1555
            global_config=precedence_global,
 
1556
            branch_data_config=precedence_branch,
 
1557
            location_config=precedence_location)
1177
1558
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
1178
 
        my_config = self.get_branch_config(global_config=precedence_global,
1179
 
                                      branch_data_config=precedence_branch,
1180
 
                                      location_config=precedence_location,
1181
 
                                      location='http://example.com/specific')
 
1559
        my_config = self.get_branch_config(
 
1560
            global_config=precedence_global,
 
1561
            branch_data_config=precedence_branch,
 
1562
            location_config=precedence_location,
 
1563
            location='http://example.com/specific')
1182
1564
        self.assertEqual(my_config.get_user_option('option'), 'exact')
1183
1565
 
1184
 
    def test_get_mail_client(self):
1185
 
        config = self.get_branch_config()
1186
 
        client = config.get_mail_client()
1187
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1188
 
 
1189
 
        # Specific clients
1190
 
        config.set_user_option('mail_client', 'evolution')
1191
 
        client = config.get_mail_client()
1192
 
        self.assertIsInstance(client, mail_client.Evolution)
1193
 
 
1194
 
        config.set_user_option('mail_client', 'kmail')
1195
 
        client = config.get_mail_client()
1196
 
        self.assertIsInstance(client, mail_client.KMail)
1197
 
 
1198
 
        config.set_user_option('mail_client', 'mutt')
1199
 
        client = config.get_mail_client()
1200
 
        self.assertIsInstance(client, mail_client.Mutt)
1201
 
 
1202
 
        config.set_user_option('mail_client', 'thunderbird')
1203
 
        client = config.get_mail_client()
1204
 
        self.assertIsInstance(client, mail_client.Thunderbird)
1205
 
 
1206
 
        # Generic options
1207
 
        config.set_user_option('mail_client', 'default')
1208
 
        client = config.get_mail_client()
1209
 
        self.assertIsInstance(client, mail_client.DefaultMail)
1210
 
 
1211
 
        config.set_user_option('mail_client', 'editor')
1212
 
        client = config.get_mail_client()
1213
 
        self.assertIsInstance(client, mail_client.Editor)
1214
 
 
1215
 
        config.set_user_option('mail_client', 'mapi')
1216
 
        client = config.get_mail_client()
1217
 
        self.assertIsInstance(client, mail_client.MAPIClient)
1218
 
 
1219
 
        config.set_user_option('mail_client', 'xdg-email')
1220
 
        client = config.get_mail_client()
1221
 
        self.assertIsInstance(client, mail_client.XDGEmail)
1222
 
 
1223
 
        config.set_user_option('mail_client', 'firebird')
1224
 
        self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1225
 
 
1226
1566
 
1227
1567
class TestMailAddressExtraction(tests.TestCase):
1228
1568
 
1229
1569
    def test_extract_email_address(self):
1230
1570
        self.assertEqual('jane@test.com',
1231
1571
                         config.extract_email_address('Jane <jane@test.com>'))
1232
 
        self.assertRaises(errors.NoEmailInUsername,
 
1572
        self.assertRaises(config.NoEmailInUsername,
1233
1573
                          config.extract_email_address, 'Jane Tester')
1234
1574
 
1235
1575
    def test_parse_username(self):
1274
1614
 
1275
1615
class TestTransportConfig(tests.TestCaseWithTransport):
1276
1616
 
 
1617
    def test_load_utf8(self):
 
1618
        """Ensure we can load an utf8-encoded file."""
 
1619
        t = self.get_transport()
 
1620
        unicode_user = u'b\N{Euro Sign}ar'
 
1621
        unicode_content = u'user=%s' % (unicode_user,)
 
1622
        utf8_content = unicode_content.encode('utf8')
 
1623
        # Store the raw content in the config file
 
1624
        t.put_bytes('foo.conf', utf8_content)
 
1625
        conf = config.TransportConfig(t, 'foo.conf')
 
1626
        self.assertEqual(unicode_user, conf.get_option('user'))
 
1627
 
 
1628
    def test_load_non_ascii(self):
 
1629
        """Ensure we display a proper error on non-ascii, non utf-8 content."""
 
1630
        t = self.get_transport()
 
1631
        t.put_bytes('foo.conf', b'user=foo\n#\xff\n')
 
1632
        conf = config.TransportConfig(t, 'foo.conf')
 
1633
        self.assertRaises(config.ConfigContentError, conf._get_configobj)
 
1634
 
 
1635
    def test_load_erroneous_content(self):
 
1636
        """Ensure we display a proper error on content that can't be parsed."""
 
1637
        t = self.get_transport()
 
1638
        t.put_bytes('foo.conf', b'[open_section\n')
 
1639
        conf = config.TransportConfig(t, 'foo.conf')
 
1640
        self.assertRaises(config.ParseConfigError, conf._get_configobj)
 
1641
 
 
1642
    def test_load_permission_denied(self):
 
1643
        """Ensure we get an empty config file if the file is inaccessible."""
 
1644
        warnings = []
 
1645
        def warning(*args):
 
1646
            warnings.append(args[0] % args[1:])
 
1647
        self.overrideAttr(trace, 'warning', warning)
 
1648
 
 
1649
        class DenyingTransport(object):
 
1650
 
 
1651
            def __init__(self, base):
 
1652
                self.base = base
 
1653
 
 
1654
            def get_bytes(self, relpath):
 
1655
                raise errors.PermissionDenied(relpath, "")
 
1656
 
 
1657
        cfg = config.TransportConfig(
 
1658
            DenyingTransport("nonexisting://"), 'control.conf')
 
1659
        self.assertIs(None, cfg.get_option('non-existant', 'SECTION'))
 
1660
        self.assertEqual(
 
1661
            warnings,
 
1662
            [u'Permission denied while trying to open configuration file '
 
1663
             u'nonexisting:///control.conf.'])
 
1664
 
1277
1665
    def test_get_value(self):
1278
1666
        """Test that retreiving a value from a section is possible"""
1279
 
        bzrdir_config = config.TransportConfig(transport.get_transport('.'),
 
1667
        bzrdir_config = config.TransportConfig(self.get_transport('.'),
1280
1668
                                               'control.conf')
1281
1669
        bzrdir_config.set_option('value', 'key', 'SECTION')
1282
1670
        bzrdir_config.set_option('value2', 'key2')
1301
1689
        self.assertEqual(value, 'value3-section')
1302
1690
 
1303
1691
    def test_set_unset_default_stack_on(self):
1304
 
        my_dir = self.make_bzrdir('.')
 
1692
        my_dir = self.make_controldir('.')
1305
1693
        bzrdir_config = config.BzrDirConfig(my_dir)
1306
1694
        self.assertIs(None, bzrdir_config.get_default_stack_on())
1307
1695
        bzrdir_config.set_default_stack_on('Foo')
1312
1700
        self.assertIs(None, bzrdir_config.get_default_stack_on())
1313
1701
 
1314
1702
 
 
1703
class TestOldConfigHooks(tests.TestCaseWithTransport):
 
1704
 
 
1705
    def setUp(self):
 
1706
        super(TestOldConfigHooks, self).setUp()
 
1707
        create_configs_with_file_option(self)
 
1708
 
 
1709
    def assertGetHook(self, conf, name, value):
 
1710
        calls = []
 
1711
        def hook(*args):
 
1712
            calls.append(args)
 
1713
        config.OldConfigHooks.install_named_hook('get', hook, None)
 
1714
        self.addCleanup(
 
1715
            config.OldConfigHooks.uninstall_named_hook, 'get', None)
 
1716
        self.assertLength(0, calls)
 
1717
        actual_value = conf.get_user_option(name)
 
1718
        self.assertEqual(value, actual_value)
 
1719
        self.assertLength(1, calls)
 
1720
        self.assertEqual((conf, name, value), calls[0])
 
1721
 
 
1722
    def test_get_hook_breezy(self):
 
1723
        self.assertGetHook(self.breezy_config, 'file', 'breezy')
 
1724
 
 
1725
    def test_get_hook_locations(self):
 
1726
        self.assertGetHook(self.locations_config, 'file', 'locations')
 
1727
 
 
1728
    def test_get_hook_branch(self):
 
1729
        # Since locations masks branch, we define a different option
 
1730
        self.branch_config.set_user_option('file2', 'branch')
 
1731
        self.assertGetHook(self.branch_config, 'file2', 'branch')
 
1732
 
 
1733
    def assertSetHook(self, conf, name, value):
 
1734
        calls = []
 
1735
        def hook(*args):
 
1736
            calls.append(args)
 
1737
        config.OldConfigHooks.install_named_hook('set', hook, None)
 
1738
        self.addCleanup(
 
1739
            config.OldConfigHooks.uninstall_named_hook, 'set', None)
 
1740
        self.assertLength(0, calls)
 
1741
        conf.set_user_option(name, value)
 
1742
        self.assertLength(1, calls)
 
1743
        # We can't assert the conf object below as different configs use
 
1744
        # different means to implement set_user_option and we care only about
 
1745
        # coverage here.
 
1746
        self.assertEqual((name, value), calls[0][1:])
 
1747
 
 
1748
    def test_set_hook_breezy(self):
 
1749
        self.assertSetHook(self.breezy_config, 'foo', 'breezy')
 
1750
 
 
1751
    def test_set_hook_locations(self):
 
1752
        self.assertSetHook(self.locations_config, 'foo', 'locations')
 
1753
 
 
1754
    def test_set_hook_branch(self):
 
1755
        self.assertSetHook(self.branch_config, 'foo', 'branch')
 
1756
 
 
1757
    def assertRemoveHook(self, conf, name, section_name=None):
 
1758
        calls = []
 
1759
        def hook(*args):
 
1760
            calls.append(args)
 
1761
        config.OldConfigHooks.install_named_hook('remove', hook, None)
 
1762
        self.addCleanup(
 
1763
            config.OldConfigHooks.uninstall_named_hook, 'remove', None)
 
1764
        self.assertLength(0, calls)
 
1765
        conf.remove_user_option(name, section_name)
 
1766
        self.assertLength(1, calls)
 
1767
        # We can't assert the conf object below as different configs use
 
1768
        # different means to implement remove_user_option and we care only about
 
1769
        # coverage here.
 
1770
        self.assertEqual((name,), calls[0][1:])
 
1771
 
 
1772
    def test_remove_hook_breezy(self):
 
1773
        self.assertRemoveHook(self.breezy_config, 'file')
 
1774
 
 
1775
    def test_remove_hook_locations(self):
 
1776
        self.assertRemoveHook(self.locations_config, 'file',
 
1777
                              self.locations_config.location)
 
1778
 
 
1779
    def test_remove_hook_branch(self):
 
1780
        self.assertRemoveHook(self.branch_config, 'file')
 
1781
 
 
1782
    def assertLoadHook(self, name, conf_class, *conf_args):
 
1783
        calls = []
 
1784
        def hook(*args):
 
1785
            calls.append(args)
 
1786
        config.OldConfigHooks.install_named_hook('load', hook, None)
 
1787
        self.addCleanup(
 
1788
            config.OldConfigHooks.uninstall_named_hook, 'load', None)
 
1789
        self.assertLength(0, calls)
 
1790
        # Build a config
 
1791
        conf = conf_class(*conf_args)
 
1792
        # Access an option to trigger a load
 
1793
        conf.get_user_option(name)
 
1794
        self.assertLength(1, calls)
 
1795
        # Since we can't assert about conf, we just use the number of calls ;-/
 
1796
 
 
1797
    def test_load_hook_breezy(self):
 
1798
        self.assertLoadHook('file', config.GlobalConfig)
 
1799
 
 
1800
    def test_load_hook_locations(self):
 
1801
        self.assertLoadHook('file', config.LocationConfig, self.tree.basedir)
 
1802
 
 
1803
    def test_load_hook_branch(self):
 
1804
        self.assertLoadHook('file', config.BranchConfig, self.tree.branch)
 
1805
 
 
1806
    def assertSaveHook(self, conf):
 
1807
        calls = []
 
1808
        def hook(*args):
 
1809
            calls.append(args)
 
1810
        config.OldConfigHooks.install_named_hook('save', hook, None)
 
1811
        self.addCleanup(
 
1812
            config.OldConfigHooks.uninstall_named_hook, 'save', None)
 
1813
        self.assertLength(0, calls)
 
1814
        # Setting an option triggers a save
 
1815
        conf.set_user_option('foo', 'bar')
 
1816
        self.assertLength(1, calls)
 
1817
        # Since we can't assert about conf, we just use the number of calls ;-/
 
1818
 
 
1819
    def test_save_hook_breezy(self):
 
1820
        self.assertSaveHook(self.breezy_config)
 
1821
 
 
1822
    def test_save_hook_locations(self):
 
1823
        self.assertSaveHook(self.locations_config)
 
1824
 
 
1825
    def test_save_hook_branch(self):
 
1826
        self.assertSaveHook(self.branch_config)
 
1827
 
 
1828
 
 
1829
class TestOldConfigHooksForRemote(tests.TestCaseWithTransport):
 
1830
    """Tests config hooks for remote configs.
 
1831
 
 
1832
    No tests for the remove hook as this is not implemented there.
 
1833
    """
 
1834
 
 
1835
    def setUp(self):
 
1836
        super(TestOldConfigHooksForRemote, self).setUp()
 
1837
        self.transport_server = test_server.SmartTCPServer_for_testing
 
1838
        create_configs_with_file_option(self)
 
1839
 
 
1840
    def assertGetHook(self, conf, name, value):
 
1841
        calls = []
 
1842
        def hook(*args):
 
1843
            calls.append(args)
 
1844
        config.OldConfigHooks.install_named_hook('get', hook, None)
 
1845
        self.addCleanup(
 
1846
            config.OldConfigHooks.uninstall_named_hook, 'get', None)
 
1847
        self.assertLength(0, calls)
 
1848
        actual_value = conf.get_option(name)
 
1849
        self.assertEqual(value, actual_value)
 
1850
        self.assertLength(1, calls)
 
1851
        self.assertEqual((conf, name, value), calls[0])
 
1852
 
 
1853
    def test_get_hook_remote_branch(self):
 
1854
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
1855
        self.assertGetHook(remote_branch._get_config(), 'file', 'branch')
 
1856
 
 
1857
    def test_get_hook_remote_bzrdir(self):
 
1858
        remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
 
1859
        conf = remote_bzrdir._get_config()
 
1860
        conf.set_option('remotedir', 'file')
 
1861
        self.assertGetHook(conf, 'file', 'remotedir')
 
1862
 
 
1863
    def assertSetHook(self, conf, name, value):
 
1864
        calls = []
 
1865
        def hook(*args):
 
1866
            calls.append(args)
 
1867
        config.OldConfigHooks.install_named_hook('set', hook, None)
 
1868
        self.addCleanup(
 
1869
            config.OldConfigHooks.uninstall_named_hook, 'set', None)
 
1870
        self.assertLength(0, calls)
 
1871
        conf.set_option(value, name)
 
1872
        self.assertLength(1, calls)
 
1873
        # We can't assert the conf object below as different configs use
 
1874
        # different means to implement set_user_option and we care only about
 
1875
        # coverage here.
 
1876
        self.assertEqual((name, value), calls[0][1:])
 
1877
 
 
1878
    def test_set_hook_remote_branch(self):
 
1879
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
1880
        self.addCleanup(remote_branch.lock_write().unlock)
 
1881
        self.assertSetHook(remote_branch._get_config(), 'file', 'remote')
 
1882
 
 
1883
    def test_set_hook_remote_bzrdir(self):
 
1884
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
1885
        self.addCleanup(remote_branch.lock_write().unlock)
 
1886
        remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
 
1887
        self.assertSetHook(remote_bzrdir._get_config(), 'file', 'remotedir')
 
1888
 
 
1889
    def assertLoadHook(self, expected_nb_calls, name, conf_class, *conf_args):
 
1890
        calls = []
 
1891
        def hook(*args):
 
1892
            calls.append(args)
 
1893
        config.OldConfigHooks.install_named_hook('load', hook, None)
 
1894
        self.addCleanup(
 
1895
            config.OldConfigHooks.uninstall_named_hook, 'load', None)
 
1896
        self.assertLength(0, calls)
 
1897
        # Build a config
 
1898
        conf = conf_class(*conf_args)
 
1899
        # Access an option to trigger a load
 
1900
        conf.get_option(name)
 
1901
        self.assertLength(expected_nb_calls, calls)
 
1902
        # Since we can't assert about conf, we just use the number of calls ;-/
 
1903
 
 
1904
    def test_load_hook_remote_branch(self):
 
1905
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
1906
        self.assertLoadHook(1, 'file', remote.RemoteBranchConfig, remote_branch)
 
1907
 
 
1908
    def test_load_hook_remote_bzrdir(self):
 
1909
        remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
 
1910
        # The config file doesn't exist, set an option to force its creation
 
1911
        conf = remote_bzrdir._get_config()
 
1912
        conf.set_option('remotedir', 'file')
 
1913
        # We get one call for the server and one call for the client, this is
 
1914
        # caused by the differences in implementations betwen
 
1915
        # SmartServerBzrDirRequestConfigFile (in smart/bzrdir.py) and
 
1916
        # SmartServerBranchGetConfigFile (in smart/branch.py)
 
1917
        self.assertLoadHook(2, 'file', remote.RemoteBzrDirConfig, remote_bzrdir)
 
1918
 
 
1919
    def assertSaveHook(self, conf):
 
1920
        calls = []
 
1921
        def hook(*args):
 
1922
            calls.append(args)
 
1923
        config.OldConfigHooks.install_named_hook('save', hook, None)
 
1924
        self.addCleanup(
 
1925
            config.OldConfigHooks.uninstall_named_hook, 'save', None)
 
1926
        self.assertLength(0, calls)
 
1927
        # Setting an option triggers a save
 
1928
        conf.set_option('foo', 'bar')
 
1929
        self.assertLength(1, calls)
 
1930
        # Since we can't assert about conf, we just use the number of calls ;-/
 
1931
 
 
1932
    def test_save_hook_remote_branch(self):
 
1933
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
1934
        self.addCleanup(remote_branch.lock_write().unlock)
 
1935
        self.assertSaveHook(remote_branch._get_config())
 
1936
 
 
1937
    def test_save_hook_remote_bzrdir(self):
 
1938
        remote_branch = branch.Branch.open(self.get_url('tree'))
 
1939
        self.addCleanup(remote_branch.lock_write().unlock)
 
1940
        remote_bzrdir = controldir.ControlDir.open(self.get_url('tree'))
 
1941
        self.assertSaveHook(remote_bzrdir._get_config())
 
1942
 
 
1943
 
 
1944
class TestOptionNames(tests.TestCase):
 
1945
 
 
1946
    def is_valid(self, name):
 
1947
        return config._option_ref_re.match('{%s}' % name) is not None
 
1948
 
 
1949
    def test_valid_names(self):
 
1950
        self.assertTrue(self.is_valid('foo'))
 
1951
        self.assertTrue(self.is_valid('foo.bar'))
 
1952
        self.assertTrue(self.is_valid('f1'))
 
1953
        self.assertTrue(self.is_valid('_'))
 
1954
        self.assertTrue(self.is_valid('__bar__'))
 
1955
        self.assertTrue(self.is_valid('a_'))
 
1956
        self.assertTrue(self.is_valid('a1'))
 
1957
        # Don't break bzr-svn for no good reason
 
1958
        self.assertTrue(self.is_valid('guessed-layout'))
 
1959
 
 
1960
    def test_invalid_names(self):
 
1961
        self.assertFalse(self.is_valid(' foo'))
 
1962
        self.assertFalse(self.is_valid('foo '))
 
1963
        self.assertFalse(self.is_valid('1'))
 
1964
        self.assertFalse(self.is_valid('1,2'))
 
1965
        self.assertFalse(self.is_valid('foo$'))
 
1966
        self.assertFalse(self.is_valid('!foo'))
 
1967
        self.assertFalse(self.is_valid('foo.'))
 
1968
        self.assertFalse(self.is_valid('foo..bar'))
 
1969
        self.assertFalse(self.is_valid('{}'))
 
1970
        self.assertFalse(self.is_valid('{a}'))
 
1971
        self.assertFalse(self.is_valid('a\n'))
 
1972
        self.assertFalse(self.is_valid('-'))
 
1973
        self.assertFalse(self.is_valid('-a'))
 
1974
        self.assertFalse(self.is_valid('a-'))
 
1975
        self.assertFalse(self.is_valid('a--a'))
 
1976
 
 
1977
    def assertSingleGroup(self, reference):
 
1978
        # the regexp is used with split and as such should match the reference
 
1979
        # *only*, if more groups needs to be defined, (?:...) should be used.
 
1980
        m = config._option_ref_re.match('{a}')
 
1981
        self.assertLength(1, m.groups())
 
1982
 
 
1983
    def test_valid_references(self):
 
1984
        self.assertSingleGroup('{a}')
 
1985
        self.assertSingleGroup('{{a}}')
 
1986
 
 
1987
 
 
1988
class TestOption(tests.TestCase):
 
1989
 
 
1990
    def test_default_value(self):
 
1991
        opt = config.Option('foo', default='bar')
 
1992
        self.assertEqual('bar', opt.get_default())
 
1993
 
 
1994
    def test_callable_default_value(self):
 
1995
        def bar_as_unicode():
 
1996
            return u'bar'
 
1997
        opt = config.Option('foo', default=bar_as_unicode)
 
1998
        self.assertEqual('bar', opt.get_default())
 
1999
 
 
2000
    def test_default_value_from_env(self):
 
2001
        opt = config.Option('foo', default='bar', default_from_env=['FOO'])
 
2002
        self.overrideEnv('FOO', 'quux')
 
2003
        # Env variable provides a default taking over the option one
 
2004
        self.assertEqual('quux', opt.get_default())
 
2005
 
 
2006
    def test_first_default_value_from_env_wins(self):
 
2007
        opt = config.Option('foo', default='bar',
 
2008
                            default_from_env=['NO_VALUE', 'FOO', 'BAZ'])
 
2009
        self.overrideEnv('FOO', 'foo')
 
2010
        self.overrideEnv('BAZ', 'baz')
 
2011
        # The first env var set wins
 
2012
        self.assertEqual('foo', opt.get_default())
 
2013
 
 
2014
    def test_not_supported_list_default_value(self):
 
2015
        self.assertRaises(AssertionError, config.Option, 'foo', default=[1])
 
2016
 
 
2017
    def test_not_supported_object_default_value(self):
 
2018
        self.assertRaises(AssertionError, config.Option, 'foo',
 
2019
                          default=object())
 
2020
 
 
2021
    def test_not_supported_callable_default_value_not_unicode(self):
 
2022
        def bar_not_unicode():
 
2023
            return b'bar'
 
2024
        opt = config.Option('foo', default=bar_not_unicode)
 
2025
        self.assertRaises(AssertionError, opt.get_default)
 
2026
 
 
2027
    def test_get_help_topic(self):
 
2028
        opt = config.Option('foo')
 
2029
        self.assertEqual('foo', opt.get_help_topic())
 
2030
 
 
2031
 
 
2032
class TestOptionConverter(tests.TestCase):
 
2033
 
 
2034
    def assertConverted(self, expected, opt, value):
 
2035
        self.assertEqual(expected, opt.convert_from_unicode(None, value))
 
2036
 
 
2037
    def assertCallsWarning(self, opt, value):
 
2038
        warnings = []
 
2039
 
 
2040
        def warning(*args):
 
2041
            warnings.append(args[0] % args[1:])
 
2042
        self.overrideAttr(trace, 'warning', warning)
 
2043
        self.assertEqual(None, opt.convert_from_unicode(None, value))
 
2044
        self.assertLength(1, warnings)
 
2045
        self.assertEqual(
 
2046
            'Value "%s" is not valid for "%s"' % (value, opt.name),
 
2047
            warnings[0])
 
2048
 
 
2049
    def assertCallsError(self, opt, value):
 
2050
        self.assertRaises(config.ConfigOptionValueError,
 
2051
                          opt.convert_from_unicode, None, value)
 
2052
 
 
2053
    def assertConvertInvalid(self, opt, invalid_value):
 
2054
        opt.invalid = None
 
2055
        self.assertEqual(None, opt.convert_from_unicode(None, invalid_value))
 
2056
        opt.invalid = 'warning'
 
2057
        self.assertCallsWarning(opt, invalid_value)
 
2058
        opt.invalid = 'error'
 
2059
        self.assertCallsError(opt, invalid_value)
 
2060
 
 
2061
 
 
2062
class TestOptionWithBooleanConverter(TestOptionConverter):
 
2063
 
 
2064
    def get_option(self):
 
2065
        return config.Option('foo', help='A boolean.',
 
2066
                             from_unicode=config.bool_from_store)
 
2067
 
 
2068
    def test_convert_invalid(self):
 
2069
        opt = self.get_option()
 
2070
        # A string that is not recognized as a boolean
 
2071
        self.assertConvertInvalid(opt, u'invalid-boolean')
 
2072
        # A list of strings is never recognized as a boolean
 
2073
        self.assertConvertInvalid(opt, [u'not', u'a', u'boolean'])
 
2074
 
 
2075
    def test_convert_valid(self):
 
2076
        opt = self.get_option()
 
2077
        self.assertConverted(True, opt, u'True')
 
2078
        self.assertConverted(True, opt, u'1')
 
2079
        self.assertConverted(False, opt, u'False')
 
2080
 
 
2081
 
 
2082
class TestOptionWithIntegerConverter(TestOptionConverter):
 
2083
 
 
2084
    def get_option(self):
 
2085
        return config.Option('foo', help='An integer.',
 
2086
                             from_unicode=config.int_from_store)
 
2087
 
 
2088
    def test_convert_invalid(self):
 
2089
        opt = self.get_option()
 
2090
        # A string that is not recognized as an integer
 
2091
        self.assertConvertInvalid(opt, u'forty-two')
 
2092
        # A list of strings is never recognized as an integer
 
2093
        self.assertConvertInvalid(opt, [u'a', u'list'])
 
2094
 
 
2095
    def test_convert_valid(self):
 
2096
        opt = self.get_option()
 
2097
        self.assertConverted(16, opt, u'16')
 
2098
 
 
2099
 
 
2100
class TestOptionWithSIUnitConverter(TestOptionConverter):
 
2101
 
 
2102
    def get_option(self):
 
2103
        return config.Option('foo', help='An integer in SI units.',
 
2104
                             from_unicode=config.int_SI_from_store)
 
2105
 
 
2106
    def test_convert_invalid(self):
 
2107
        opt = self.get_option()
 
2108
        self.assertConvertInvalid(opt, u'not-a-unit')
 
2109
        self.assertConvertInvalid(opt, u'Gb')  # Forgot the value
 
2110
        self.assertConvertInvalid(opt, u'1b')  # Forgot the unit
 
2111
        self.assertConvertInvalid(opt, u'1GG')
 
2112
        self.assertConvertInvalid(opt, u'1Mbb')
 
2113
        self.assertConvertInvalid(opt, u'1MM')
 
2114
 
 
2115
    def test_convert_valid(self):
 
2116
        opt = self.get_option()
 
2117
        self.assertConverted(int(5e3), opt, u'5kb')
 
2118
        self.assertConverted(int(5e6), opt, u'5M')
 
2119
        self.assertConverted(int(5e6), opt, u'5MB')
 
2120
        self.assertConverted(int(5e9), opt, u'5g')
 
2121
        self.assertConverted(int(5e9), opt, u'5gB')
 
2122
        self.assertConverted(100, opt, u'100')
 
2123
 
 
2124
 
 
2125
class TestListOption(TestOptionConverter):
 
2126
 
 
2127
    def get_option(self):
 
2128
        return config.ListOption('foo', help='A list.')
 
2129
 
 
2130
    def test_convert_invalid(self):
 
2131
        opt = self.get_option()
 
2132
        # We don't even try to convert a list into a list, we only expect
 
2133
        # strings
 
2134
        self.assertConvertInvalid(opt, [1])
 
2135
        # No string is invalid as all forms can be converted to a list
 
2136
 
 
2137
    def test_convert_valid(self):
 
2138
        opt = self.get_option()
 
2139
        # An empty string is an empty list
 
2140
        self.assertConverted([], opt, '')  # Using a bare str() just in case
 
2141
        self.assertConverted([], opt, u'')
 
2142
        # A boolean
 
2143
        self.assertConverted([u'True'], opt, u'True')
 
2144
        # An integer
 
2145
        self.assertConverted([u'42'], opt, u'42')
 
2146
        # A single string
 
2147
        self.assertConverted([u'bar'], opt, u'bar')
 
2148
 
 
2149
 
 
2150
class TestRegistryOption(TestOptionConverter):
 
2151
 
 
2152
    def get_option(self, registry):
 
2153
        return config.RegistryOption('foo', registry,
 
2154
                                     help='A registry option.')
 
2155
 
 
2156
    def test_convert_invalid(self):
 
2157
        registry = _mod_registry.Registry()
 
2158
        opt = self.get_option(registry)
 
2159
        self.assertConvertInvalid(opt, [1])
 
2160
        self.assertConvertInvalid(opt, u"notregistered")
 
2161
 
 
2162
    def test_convert_valid(self):
 
2163
        registry = _mod_registry.Registry()
 
2164
        registry.register("someval", 1234)
 
2165
        opt = self.get_option(registry)
 
2166
        # Using a bare str() just in case
 
2167
        self.assertConverted(1234, opt, "someval")
 
2168
        self.assertConverted(1234, opt, u'someval')
 
2169
        self.assertConverted(None, opt, None)
 
2170
 
 
2171
    def test_help(self):
 
2172
        registry = _mod_registry.Registry()
 
2173
        registry.register("someval", 1234, help="some option")
 
2174
        registry.register("dunno", 1234, help="some other option")
 
2175
        opt = self.get_option(registry)
 
2176
        self.assertEqual(
 
2177
            'A registry option.\n'
 
2178
            '\n'
 
2179
            'The following values are supported:\n'
 
2180
            ' dunno - some other option\n'
 
2181
            ' someval - some option\n',
 
2182
            opt.help)
 
2183
 
 
2184
    def test_get_help_text(self):
 
2185
        registry = _mod_registry.Registry()
 
2186
        registry.register("someval", 1234, help="some option")
 
2187
        registry.register("dunno", 1234, help="some other option")
 
2188
        opt = self.get_option(registry)
 
2189
        self.assertEqual(
 
2190
            'A registry option.\n'
 
2191
            '\n'
 
2192
            'The following values are supported:\n'
 
2193
            ' dunno - some other option\n'
 
2194
            ' someval - some option\n',
 
2195
            opt.get_help_text())
 
2196
 
 
2197
 
 
2198
class TestOptionRegistry(tests.TestCase):
 
2199
 
 
2200
    def setUp(self):
 
2201
        super(TestOptionRegistry, self).setUp()
 
2202
        # Always start with an empty registry
 
2203
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
 
2204
        self.registry = config.option_registry
 
2205
 
 
2206
    def test_register(self):
 
2207
        opt = config.Option('foo')
 
2208
        self.registry.register(opt)
 
2209
        self.assertIs(opt, self.registry.get('foo'))
 
2210
 
 
2211
    def test_registered_help(self):
 
2212
        opt = config.Option('foo', help='A simple option')
 
2213
        self.registry.register(opt)
 
2214
        self.assertEqual('A simple option', self.registry.get_help('foo'))
 
2215
 
 
2216
    def test_dont_register_illegal_name(self):
 
2217
        self.assertRaises(config.IllegalOptionName,
 
2218
                          self.registry.register, config.Option(' foo'))
 
2219
        self.assertRaises(config.IllegalOptionName,
 
2220
                          self.registry.register, config.Option('bar,'))
 
2221
 
 
2222
    lazy_option = config.Option('lazy_foo', help='Lazy help')
 
2223
 
 
2224
    def test_register_lazy(self):
 
2225
        self.registry.register_lazy('lazy_foo', self.__module__,
 
2226
                                    'TestOptionRegistry.lazy_option')
 
2227
        self.assertIs(self.lazy_option, self.registry.get('lazy_foo'))
 
2228
 
 
2229
    def test_registered_lazy_help(self):
 
2230
        self.registry.register_lazy('lazy_foo', self.__module__,
 
2231
                                    'TestOptionRegistry.lazy_option')
 
2232
        self.assertEqual('Lazy help', self.registry.get_help('lazy_foo'))
 
2233
 
 
2234
    def test_dont_lazy_register_illegal_name(self):
 
2235
        # This is where the root cause of http://pad.lv/1235099 is better
 
2236
        # understood: 'register_lazy' doc string mentions that key should match
 
2237
        # the option name which indirectly requires that the option name is a
 
2238
        # valid python identifier. We violate that rule here (using a key that
 
2239
        # doesn't match the option name) to test the option name checking.
 
2240
        self.assertRaises(config.IllegalOptionName,
 
2241
                          self.registry.register_lazy, ' foo', self.__module__,
 
2242
                          'TestOptionRegistry.lazy_option')
 
2243
        self.assertRaises(config.IllegalOptionName,
 
2244
                          self.registry.register_lazy, '1,2', self.__module__,
 
2245
                          'TestOptionRegistry.lazy_option')
 
2246
 
 
2247
 
 
2248
class TestRegisteredOptions(tests.TestCase):
 
2249
    """All registered options should verify some constraints."""
 
2250
 
 
2251
    scenarios = [(key, {'option_name': key, 'option': option}) for key, option
 
2252
                 in config.option_registry.iteritems()]
 
2253
 
 
2254
    def setUp(self):
 
2255
        super(TestRegisteredOptions, self).setUp()
 
2256
        self.registry = config.option_registry
 
2257
 
 
2258
    def test_proper_name(self):
 
2259
        # An option should be registered under its own name, this can't be
 
2260
        # checked at registration time for the lazy ones.
 
2261
        self.assertEqual(self.option_name, self.option.name)
 
2262
 
 
2263
    def test_help_is_set(self):
 
2264
        option_help = self.registry.get_help(self.option_name)
 
2265
        # Come on, think about the user, he really wants to know what the
 
2266
        # option is about
 
2267
        self.assertIsNot(None, option_help)
 
2268
        self.assertNotEqual('', option_help)
 
2269
 
 
2270
 
 
2271
class TestSection(tests.TestCase):
 
2272
 
 
2273
    # FIXME: Parametrize so that all sections produced by Stores run these
 
2274
    # tests -- vila 2011-04-01
 
2275
 
 
2276
    def test_get_a_value(self):
 
2277
        a_dict = dict(foo='bar')
 
2278
        section = config.Section('myID', a_dict)
 
2279
        self.assertEqual('bar', section.get('foo'))
 
2280
 
 
2281
    def test_get_unknown_option(self):
 
2282
        a_dict = dict()
 
2283
        section = config.Section(None, a_dict)
 
2284
        self.assertEqual('out of thin air',
 
2285
                          section.get('foo', 'out of thin air'))
 
2286
 
 
2287
    def test_options_is_shared(self):
 
2288
        a_dict = dict()
 
2289
        section = config.Section(None, a_dict)
 
2290
        self.assertIs(a_dict, section.options)
 
2291
 
 
2292
 
 
2293
class TestMutableSection(tests.TestCase):
 
2294
 
 
2295
    scenarios = [('mutable',
 
2296
                  {'get_section':
 
2297
                       lambda opts: config.MutableSection('myID', opts)},),
 
2298
        ]
 
2299
 
 
2300
    def test_set(self):
 
2301
        a_dict = dict(foo='bar')
 
2302
        section = self.get_section(a_dict)
 
2303
        section.set('foo', 'new_value')
 
2304
        self.assertEqual('new_value', section.get('foo'))
 
2305
        # The change appears in the shared section
 
2306
        self.assertEqual('new_value', a_dict.get('foo'))
 
2307
        # We keep track of the change
 
2308
        self.assertTrue('foo' in section.orig)
 
2309
        self.assertEqual('bar', section.orig.get('foo'))
 
2310
 
 
2311
    def test_set_preserve_original_once(self):
 
2312
        a_dict = dict(foo='bar')
 
2313
        section = self.get_section(a_dict)
 
2314
        section.set('foo', 'first_value')
 
2315
        section.set('foo', 'second_value')
 
2316
        # We keep track of the original value
 
2317
        self.assertTrue('foo' in section.orig)
 
2318
        self.assertEqual('bar', section.orig.get('foo'))
 
2319
 
 
2320
    def test_remove(self):
 
2321
        a_dict = dict(foo='bar')
 
2322
        section = self.get_section(a_dict)
 
2323
        section.remove('foo')
 
2324
        # We get None for unknown options via the default value
 
2325
        self.assertEqual(None, section.get('foo'))
 
2326
        # Or we just get the default value
 
2327
        self.assertEqual('unknown', section.get('foo', 'unknown'))
 
2328
        self.assertFalse('foo' in section.options)
 
2329
        # We keep track of the deletion
 
2330
        self.assertTrue('foo' in section.orig)
 
2331
        self.assertEqual('bar', section.orig.get('foo'))
 
2332
 
 
2333
    def test_remove_new_option(self):
 
2334
        a_dict = dict()
 
2335
        section = self.get_section(a_dict)
 
2336
        section.set('foo', 'bar')
 
2337
        section.remove('foo')
 
2338
        self.assertFalse('foo' in section.options)
 
2339
        # The option didn't exist initially so it we need to keep track of it
 
2340
        # with a special value
 
2341
        self.assertTrue('foo' in section.orig)
 
2342
        self.assertEqual(config._NewlyCreatedOption, section.orig['foo'])
 
2343
 
 
2344
 
 
2345
class TestCommandLineStore(tests.TestCase):
 
2346
 
 
2347
    def setUp(self):
 
2348
        super(TestCommandLineStore, self).setUp()
 
2349
        self.store = config.CommandLineStore()
 
2350
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
 
2351
 
 
2352
    def get_section(self):
 
2353
        """Get the unique section for the command line overrides."""
 
2354
        sections = list(self.store.get_sections())
 
2355
        self.assertLength(1, sections)
 
2356
        store, section = sections[0]
 
2357
        self.assertEqual(self.store, store)
 
2358
        return section
 
2359
 
 
2360
    def test_no_override(self):
 
2361
        self.store._from_cmdline([])
 
2362
        section = self.get_section()
 
2363
        self.assertLength(0, list(section.iter_option_names()))
 
2364
 
 
2365
    def test_simple_override(self):
 
2366
        self.store._from_cmdline(['a=b'])
 
2367
        section = self.get_section()
 
2368
        self.assertEqual('b', section.get('a'))
 
2369
 
 
2370
    def test_list_override(self):
 
2371
        opt = config.ListOption('l')
 
2372
        config.option_registry.register(opt)
 
2373
        self.store._from_cmdline(['l=1,2,3'])
 
2374
        val = self.get_section().get('l')
 
2375
        self.assertEqual('1,2,3', val)
 
2376
        # Reminder: lists should be registered as such explicitely, otherwise
 
2377
        # the conversion needs to be done afterwards.
 
2378
        self.assertEqual(['1', '2', '3'],
 
2379
                         opt.convert_from_unicode(self.store, val))
 
2380
 
 
2381
    def test_multiple_overrides(self):
 
2382
        self.store._from_cmdline(['a=b', 'x=y'])
 
2383
        section = self.get_section()
 
2384
        self.assertEqual('b', section.get('a'))
 
2385
        self.assertEqual('y', section.get('x'))
 
2386
 
 
2387
    def test_wrong_syntax(self):
 
2388
        self.assertRaises(errors.BzrCommandError,
 
2389
                          self.store._from_cmdline, ['a=b', 'c'])
 
2390
 
 
2391
class TestStoreMinimalAPI(tests.TestCaseWithTransport):
 
2392
 
 
2393
    scenarios = [(key, {'get_store': builder}) for key, builder
 
2394
                 in config.test_store_builder_registry.iteritems()] + [
 
2395
        ('cmdline', {'get_store': lambda test: config.CommandLineStore()})]
 
2396
 
 
2397
    def test_id(self):
 
2398
        store = self.get_store(self)
 
2399
        if isinstance(store, config.TransportIniFileStore):
 
2400
            raise tests.TestNotApplicable(
 
2401
                "%s is not a concrete Store implementation"
 
2402
                " so it doesn't need an id" % (store.__class__.__name__,))
 
2403
        self.assertIsNot(None, store.id)
 
2404
 
 
2405
 
 
2406
class TestStore(tests.TestCaseWithTransport):
 
2407
 
 
2408
    def assertSectionContent(self, expected, store_and_section):
 
2409
        """Assert that some options have the proper values in a section."""
 
2410
        _, section = store_and_section
 
2411
        expected_name, expected_options = expected
 
2412
        self.assertEqual(expected_name, section.id)
 
2413
        self.assertEqual(
 
2414
            expected_options,
 
2415
            dict([(k, section.get(k)) for k in expected_options.keys()]))
 
2416
 
 
2417
 
 
2418
class TestReadonlyStore(TestStore):
 
2419
 
 
2420
    scenarios = [(key, {'get_store': builder}) for key, builder
 
2421
                 in config.test_store_builder_registry.iteritems()]
 
2422
 
 
2423
    def test_building_delays_load(self):
 
2424
        store = self.get_store(self)
 
2425
        self.assertEqual(False, store.is_loaded())
 
2426
        store._load_from_string(b'')
 
2427
        self.assertEqual(True, store.is_loaded())
 
2428
 
 
2429
    def test_get_no_sections_for_empty(self):
 
2430
        store = self.get_store(self)
 
2431
        store._load_from_string(b'')
 
2432
        self.assertEqual([], list(store.get_sections()))
 
2433
 
 
2434
    def test_get_default_section(self):
 
2435
        store = self.get_store(self)
 
2436
        store._load_from_string(b'foo=bar')
 
2437
        sections = list(store.get_sections())
 
2438
        self.assertLength(1, sections)
 
2439
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
 
2440
 
 
2441
    def test_get_named_section(self):
 
2442
        store = self.get_store(self)
 
2443
        store._load_from_string(b'[baz]\nfoo=bar')
 
2444
        sections = list(store.get_sections())
 
2445
        self.assertLength(1, sections)
 
2446
        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
 
2447
 
 
2448
    def test_load_from_string_fails_for_non_empty_store(self):
 
2449
        store = self.get_store(self)
 
2450
        store._load_from_string(b'foo=bar')
 
2451
        self.assertRaises(AssertionError, store._load_from_string, b'bar=baz')
 
2452
 
 
2453
 
 
2454
class TestStoreQuoting(TestStore):
 
2455
 
 
2456
    scenarios = [(key, {'get_store': builder}) for key, builder
 
2457
                 in config.test_store_builder_registry.iteritems()]
 
2458
 
 
2459
    def setUp(self):
 
2460
        super(TestStoreQuoting, self).setUp()
 
2461
        self.store = self.get_store(self)
 
2462
        # We need a loaded store but any content will do
 
2463
        self.store._load_from_string(b'')
 
2464
 
 
2465
    def assertIdempotent(self, s):
 
2466
        """Assert that quoting an unquoted string is a no-op and vice-versa.
 
2467
 
 
2468
        What matters here is that option values, as they appear in a store, can
 
2469
        be safely round-tripped out of the store and back.
 
2470
 
 
2471
        :param s: A string, quoted if required.
 
2472
        """
 
2473
        self.assertEqual(s, self.store.quote(self.store.unquote(s)))
 
2474
        self.assertEqual(s, self.store.unquote(self.store.quote(s)))
 
2475
 
 
2476
    def test_empty_string(self):
 
2477
        if isinstance(self.store, config.IniFileStore):
 
2478
            # configobj._quote doesn't handle empty values
 
2479
            self.assertRaises(AssertionError,
 
2480
                              self.assertIdempotent, '')
 
2481
        else:
 
2482
            self.assertIdempotent('')
 
2483
        # But quoted empty strings are ok
 
2484
        self.assertIdempotent('""')
 
2485
 
 
2486
    def test_embedded_spaces(self):
 
2487
        self.assertIdempotent('" a b c "')
 
2488
 
 
2489
    def test_embedded_commas(self):
 
2490
        self.assertIdempotent('" a , b c "')
 
2491
 
 
2492
    def test_simple_comma(self):
 
2493
        if isinstance(self.store, config.IniFileStore):
 
2494
            # configobj requires that lists are special-cased
 
2495
           self.assertRaises(AssertionError,
 
2496
                             self.assertIdempotent, ',')
 
2497
        else:
 
2498
            self.assertIdempotent(',')
 
2499
        # When a single comma is required, quoting is also required
 
2500
        self.assertIdempotent('","')
 
2501
 
 
2502
    def test_list(self):
 
2503
        if isinstance(self.store, config.IniFileStore):
 
2504
            # configobj requires that lists are special-cased
 
2505
            self.assertRaises(AssertionError,
 
2506
                              self.assertIdempotent, 'a,b')
 
2507
        else:
 
2508
            self.assertIdempotent('a,b')
 
2509
 
 
2510
 
 
2511
class TestDictFromStore(tests.TestCase):
 
2512
 
 
2513
    def test_unquote_not_string(self):
 
2514
        conf = config.MemoryStack(b'x=2\n[a_section]\na=1\n')
 
2515
        value = conf.get('a_section')
 
2516
        # Urgh, despite 'conf' asking for the no-name section, we get the
 
2517
        # content of another section as a dict o_O
 
2518
        self.assertEqual({'a': '1'}, value)
 
2519
        unquoted = conf.store.unquote(value)
 
2520
        # Which cannot be unquoted but shouldn't crash either (the use cases
 
2521
        # are getting the value or displaying it. In the later case, '%s' will
 
2522
        # do).
 
2523
        self.assertEqual({'a': '1'}, unquoted)
 
2524
        self.assertIn('%s' % (unquoted,), ("{u'a': u'1'}", "{'a': '1'}"))
 
2525
 
 
2526
 
 
2527
class TestIniFileStoreContent(tests.TestCaseWithTransport):
 
2528
    """Simulate loading a config store with content of various encodings.
 
2529
 
 
2530
    All files produced by bzr are in utf8 content.
 
2531
 
 
2532
    Users may modify them manually and end up with a file that can't be
 
2533
    loaded. We need to issue proper error messages in this case.
 
2534
    """
 
2535
 
 
2536
    invalid_utf8_char = b'\xff'
 
2537
 
 
2538
    def test_load_utf8(self):
 
2539
        """Ensure we can load an utf8-encoded file."""
 
2540
        t = self.get_transport()
 
2541
        # From http://pad.lv/799212
 
2542
        unicode_user = u'b\N{Euro Sign}ar'
 
2543
        unicode_content = u'user=%s' % (unicode_user,)
 
2544
        utf8_content = unicode_content.encode('utf8')
 
2545
        # Store the raw content in the config file
 
2546
        t.put_bytes('foo.conf', utf8_content)
 
2547
        store = config.TransportIniFileStore(t, 'foo.conf')
 
2548
        store.load()
 
2549
        stack = config.Stack([store.get_sections], store)
 
2550
        self.assertEqual(unicode_user, stack.get('user'))
 
2551
 
 
2552
    def test_load_non_ascii(self):
 
2553
        """Ensure we display a proper error on non-ascii, non utf-8 content."""
 
2554
        t = self.get_transport()
 
2555
        t.put_bytes('foo.conf', b'user=foo\n#%s\n' % (self.invalid_utf8_char,))
 
2556
        store = config.TransportIniFileStore(t, 'foo.conf')
 
2557
        self.assertRaises(config.ConfigContentError, store.load)
 
2558
 
 
2559
    def test_load_erroneous_content(self):
 
2560
        """Ensure we display a proper error on content that can't be parsed."""
 
2561
        t = self.get_transport()
 
2562
        t.put_bytes('foo.conf', b'[open_section\n')
 
2563
        store = config.TransportIniFileStore(t, 'foo.conf')
 
2564
        self.assertRaises(config.ParseConfigError, store.load)
 
2565
 
 
2566
    def test_load_permission_denied(self):
 
2567
        """Ensure we get warned when trying to load an inaccessible file."""
 
2568
        warnings = []
 
2569
        def warning(*args):
 
2570
            warnings.append(args[0] % args[1:])
 
2571
        self.overrideAttr(trace, 'warning', warning)
 
2572
 
 
2573
        t = self.get_transport()
 
2574
 
 
2575
        def get_bytes(relpath):
 
2576
            raise errors.PermissionDenied(relpath, "")
 
2577
        t.get_bytes = get_bytes
 
2578
        store = config.TransportIniFileStore(t, 'foo.conf')
 
2579
        self.assertRaises(errors.PermissionDenied, store.load)
 
2580
        self.assertEqual(
 
2581
            warnings,
 
2582
            [u'Permission denied while trying to load configuration store %s.'
 
2583
             % store.external_url()])
 
2584
 
 
2585
 
 
2586
class TestIniConfigContent(tests.TestCaseWithTransport):
 
2587
    """Simulate loading a IniBasedConfig with content of various encodings.
 
2588
 
 
2589
    All files produced by bzr are in utf8 content.
 
2590
 
 
2591
    Users may modify them manually and end up with a file that can't be
 
2592
    loaded. We need to issue proper error messages in this case.
 
2593
    """
 
2594
 
 
2595
    invalid_utf8_char = b'\xff'
 
2596
 
 
2597
    def test_load_utf8(self):
 
2598
        """Ensure we can load an utf8-encoded file."""
 
2599
        # From http://pad.lv/799212
 
2600
        unicode_user = u'b\N{Euro Sign}ar'
 
2601
        unicode_content = u'user=%s' % (unicode_user,)
 
2602
        utf8_content = unicode_content.encode('utf8')
 
2603
        # Store the raw content in the config file
 
2604
        with open('foo.conf', 'wb') as f:
 
2605
            f.write(utf8_content)
 
2606
        conf = config.IniBasedConfig(file_name='foo.conf')
 
2607
        self.assertEqual(unicode_user, conf.get_user_option('user'))
 
2608
 
 
2609
    def test_load_badly_encoded_content(self):
 
2610
        """Ensure we display a proper error on non-ascii, non utf-8 content."""
 
2611
        with open('foo.conf', 'wb') as f:
 
2612
            f.write(b'user=foo\n#%s\n' % (self.invalid_utf8_char,))
 
2613
        conf = config.IniBasedConfig(file_name='foo.conf')
 
2614
        self.assertRaises(config.ConfigContentError, conf._get_parser)
 
2615
 
 
2616
    def test_load_erroneous_content(self):
 
2617
        """Ensure we display a proper error on content that can't be parsed."""
 
2618
        with open('foo.conf', 'wb') as f:
 
2619
            f.write(b'[open_section\n')
 
2620
        conf = config.IniBasedConfig(file_name='foo.conf')
 
2621
        self.assertRaises(config.ParseConfigError, conf._get_parser)
 
2622
 
 
2623
 
 
2624
class TestMutableStore(TestStore):
 
2625
 
 
2626
    scenarios = [(key, {'store_id': key, 'get_store': builder}) for key, builder
 
2627
                 in config.test_store_builder_registry.iteritems()]
 
2628
 
 
2629
    def setUp(self):
 
2630
        super(TestMutableStore, self).setUp()
 
2631
        self.transport = self.get_transport()
 
2632
 
 
2633
    def has_store(self, store):
 
2634
        store_basename = urlutils.relative_url(self.transport.external_url(),
 
2635
                                               store.external_url())
 
2636
        return self.transport.has(store_basename)
 
2637
 
 
2638
    def test_save_empty_creates_no_file(self):
 
2639
        # FIXME: There should be a better way than relying on the test
 
2640
        # parametrization to identify branch.conf -- vila 2011-0526
 
2641
        if self.store_id in ('branch', 'remote_branch'):
 
2642
            raise tests.TestNotApplicable(
 
2643
                'branch.conf is *always* created when a branch is initialized')
 
2644
        store = self.get_store(self)
 
2645
        store.save()
 
2646
        self.assertEqual(False, self.has_store(store))
 
2647
 
 
2648
    def test_mutable_section_shared(self):
 
2649
        store = self.get_store(self)
 
2650
        store._load_from_string(b'foo=bar\n')
 
2651
        # FIXME: There should be a better way than relying on the test
 
2652
        # parametrization to identify branch.conf -- vila 2011-0526
 
2653
        if self.store_id in ('branch', 'remote_branch'):
 
2654
            # branch stores requires write locked branches
 
2655
            self.addCleanup(store.branch.lock_write().unlock)
 
2656
        section1 = store.get_mutable_section(None)
 
2657
        section2 = store.get_mutable_section(None)
 
2658
        # If we get different sections, different callers won't share the
 
2659
        # modification
 
2660
        self.assertIs(section1, section2)
 
2661
 
 
2662
    def test_save_emptied_succeeds(self):
 
2663
        store = self.get_store(self)
 
2664
        store._load_from_string(b'foo=bar\n')
 
2665
        # FIXME: There should be a better way than relying on the test
 
2666
        # parametrization to identify branch.conf -- vila 2011-0526
 
2667
        if self.store_id in ('branch', 'remote_branch'):
 
2668
            # branch stores requires write locked branches
 
2669
            self.addCleanup(store.branch.lock_write().unlock)
 
2670
        section = store.get_mutable_section(None)
 
2671
        section.remove('foo')
 
2672
        store.save()
 
2673
        self.assertEqual(True, self.has_store(store))
 
2674
        modified_store = self.get_store(self)
 
2675
        sections = list(modified_store.get_sections())
 
2676
        self.assertLength(0, sections)
 
2677
 
 
2678
    def test_save_with_content_succeeds(self):
 
2679
        # FIXME: There should be a better way than relying on the test
 
2680
        # parametrization to identify branch.conf -- vila 2011-0526
 
2681
        if self.store_id in ('branch', 'remote_branch'):
 
2682
            raise tests.TestNotApplicable(
 
2683
                'branch.conf is *always* created when a branch is initialized')
 
2684
        store = self.get_store(self)
 
2685
        store._load_from_string(b'foo=bar\n')
 
2686
        self.assertEqual(False, self.has_store(store))
 
2687
        store.save()
 
2688
        self.assertEqual(True, self.has_store(store))
 
2689
        modified_store = self.get_store(self)
 
2690
        sections = list(modified_store.get_sections())
 
2691
        self.assertLength(1, sections)
 
2692
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
 
2693
 
 
2694
    def test_set_option_in_empty_store(self):
 
2695
        store = self.get_store(self)
 
2696
        # FIXME: There should be a better way than relying on the test
 
2697
        # parametrization to identify branch.conf -- vila 2011-0526
 
2698
        if self.store_id in ('branch', 'remote_branch'):
 
2699
            # branch stores requires write locked branches
 
2700
            self.addCleanup(store.branch.lock_write().unlock)
 
2701
        section = store.get_mutable_section(None)
 
2702
        section.set('foo', 'bar')
 
2703
        store.save()
 
2704
        modified_store = self.get_store(self)
 
2705
        sections = list(modified_store.get_sections())
 
2706
        self.assertLength(1, sections)
 
2707
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
 
2708
 
 
2709
    def test_set_option_in_default_section(self):
 
2710
        store = self.get_store(self)
 
2711
        store._load_from_string(b'')
 
2712
        # FIXME: There should be a better way than relying on the test
 
2713
        # parametrization to identify branch.conf -- vila 2011-0526
 
2714
        if self.store_id in ('branch', 'remote_branch'):
 
2715
            # branch stores requires write locked branches
 
2716
            self.addCleanup(store.branch.lock_write().unlock)
 
2717
        section = store.get_mutable_section(None)
 
2718
        section.set('foo', 'bar')
 
2719
        store.save()
 
2720
        modified_store = self.get_store(self)
 
2721
        sections = list(modified_store.get_sections())
 
2722
        self.assertLength(1, sections)
 
2723
        self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
 
2724
 
 
2725
    def test_set_option_in_named_section(self):
 
2726
        store = self.get_store(self)
 
2727
        store._load_from_string(b'')
 
2728
        # FIXME: There should be a better way than relying on the test
 
2729
        # parametrization to identify branch.conf -- vila 2011-0526
 
2730
        if self.store_id in ('branch', 'remote_branch'):
 
2731
            # branch stores requires write locked branches
 
2732
            self.addCleanup(store.branch.lock_write().unlock)
 
2733
        section = store.get_mutable_section('baz')
 
2734
        section.set('foo', 'bar')
 
2735
        store.save()
 
2736
        modified_store = self.get_store(self)
 
2737
        sections = list(modified_store.get_sections())
 
2738
        self.assertLength(1, sections)
 
2739
        self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
 
2740
 
 
2741
    def test_load_hook(self):
 
2742
        # First, we need to ensure that the store exists
 
2743
        store = self.get_store(self)
 
2744
        # FIXME: There should be a better way than relying on the test
 
2745
        # parametrization to identify branch.conf -- vila 2011-0526
 
2746
        if self.store_id in ('branch', 'remote_branch'):
 
2747
            # branch stores requires write locked branches
 
2748
            self.addCleanup(store.branch.lock_write().unlock)
 
2749
        section = store.get_mutable_section('baz')
 
2750
        section.set('foo', 'bar')
 
2751
        store.save()
 
2752
        # Now we can try to load it
 
2753
        store = self.get_store(self)
 
2754
        calls = []
 
2755
        def hook(*args):
 
2756
            calls.append(args)
 
2757
        config.ConfigHooks.install_named_hook('load', hook, None)
 
2758
        self.assertLength(0, calls)
 
2759
        store.load()
 
2760
        self.assertLength(1, calls)
 
2761
        self.assertEqual((store,), calls[0])
 
2762
 
 
2763
    def test_save_hook(self):
 
2764
        calls = []
 
2765
        def hook(*args):
 
2766
            calls.append(args)
 
2767
        config.ConfigHooks.install_named_hook('save', hook, None)
 
2768
        self.assertLength(0, calls)
 
2769
        store = self.get_store(self)
 
2770
        # FIXME: There should be a better way than relying on the test
 
2771
        # parametrization to identify branch.conf -- vila 2011-0526
 
2772
        if self.store_id in ('branch', 'remote_branch'):
 
2773
            # branch stores requires write locked branches
 
2774
            self.addCleanup(store.branch.lock_write().unlock)
 
2775
        section = store.get_mutable_section('baz')
 
2776
        section.set('foo', 'bar')
 
2777
        store.save()
 
2778
        self.assertLength(1, calls)
 
2779
        self.assertEqual((store,), calls[0])
 
2780
 
 
2781
    def test_set_mark_dirty(self):
 
2782
        stack = config.MemoryStack(b'')
 
2783
        self.assertLength(0, stack.store.dirty_sections)
 
2784
        stack.set('foo', 'baz')
 
2785
        self.assertLength(1, stack.store.dirty_sections)
 
2786
        self.assertTrue(stack.store._need_saving())
 
2787
 
 
2788
    def test_remove_mark_dirty(self):
 
2789
        stack = config.MemoryStack(b'foo=bar')
 
2790
        self.assertLength(0, stack.store.dirty_sections)
 
2791
        stack.remove('foo')
 
2792
        self.assertLength(1, stack.store.dirty_sections)
 
2793
        self.assertTrue(stack.store._need_saving())
 
2794
 
 
2795
 
 
2796
class TestStoreSaveChanges(tests.TestCaseWithTransport):
 
2797
    """Tests that config changes are kept in memory and saved on-demand."""
 
2798
 
 
2799
    def setUp(self):
 
2800
        super(TestStoreSaveChanges, self).setUp()
 
2801
        self.transport = self.get_transport()
 
2802
        # Most of the tests involve two stores pointing to the same persistent
 
2803
        # storage to observe the effects of concurrent changes
 
2804
        self.st1 = config.TransportIniFileStore(self.transport, 'foo.conf')
 
2805
        self.st2 = config.TransportIniFileStore(self.transport, 'foo.conf')
 
2806
        self.warnings = []
 
2807
        def warning(*args):
 
2808
            self.warnings.append(args[0] % args[1:])
 
2809
        self.overrideAttr(trace, 'warning', warning)
 
2810
 
 
2811
    def has_store(self, store):
 
2812
        store_basename = urlutils.relative_url(self.transport.external_url(),
 
2813
                                               store.external_url())
 
2814
        return self.transport.has(store_basename)
 
2815
 
 
2816
    def get_stack(self, store):
 
2817
        # Any stack will do as long as it uses the right store, just a single
 
2818
        # no-name section is enough
 
2819
        return config.Stack([store.get_sections], store)
 
2820
 
 
2821
    def test_no_changes_no_save(self):
 
2822
        s = self.get_stack(self.st1)
 
2823
        s.store.save_changes()
 
2824
        self.assertEqual(False, self.has_store(self.st1))
 
2825
 
 
2826
    def test_unrelated_concurrent_update(self):
 
2827
        s1 = self.get_stack(self.st1)
 
2828
        s2 = self.get_stack(self.st2)
 
2829
        s1.set('foo', 'bar')
 
2830
        s2.set('baz', 'quux')
 
2831
        s1.store.save()
 
2832
        # Changes don't propagate magically
 
2833
        self.assertEqual(None, s1.get('baz'))
 
2834
        s2.store.save_changes()
 
2835
        self.assertEqual('quux', s2.get('baz'))
 
2836
        # Changes are acquired when saving
 
2837
        self.assertEqual('bar', s2.get('foo'))
 
2838
        # Since there is no overlap, no warnings are emitted
 
2839
        self.assertLength(0, self.warnings)
 
2840
 
 
2841
    def test_concurrent_update_modified(self):
 
2842
        s1 = self.get_stack(self.st1)
 
2843
        s2 = self.get_stack(self.st2)
 
2844
        s1.set('foo', 'bar')
 
2845
        s2.set('foo', 'baz')
 
2846
        s1.store.save()
 
2847
        # Last speaker wins
 
2848
        s2.store.save_changes()
 
2849
        self.assertEqual('baz', s2.get('foo'))
 
2850
        # But the user get a warning
 
2851
        self.assertLength(1, self.warnings)
 
2852
        warning = self.warnings[0]
 
2853
        self.assertStartsWith(warning, 'Option foo in section None')
 
2854
        self.assertEndsWith(warning, 'was changed from <CREATED> to bar.'
 
2855
                            ' The baz value will be saved.')
 
2856
 
 
2857
    def test_concurrent_deletion(self):
 
2858
        self.st1._load_from_string(b'foo=bar')
 
2859
        self.st1.save()
 
2860
        s1 = self.get_stack(self.st1)
 
2861
        s2 = self.get_stack(self.st2)
 
2862
        s1.remove('foo')
 
2863
        s2.remove('foo')
 
2864
        s1.store.save_changes()
 
2865
        # No warning yet
 
2866
        self.assertLength(0, self.warnings)
 
2867
        s2.store.save_changes()
 
2868
        # Now we get one
 
2869
        self.assertLength(1, self.warnings)
 
2870
        warning = self.warnings[0]
 
2871
        self.assertStartsWith(warning, 'Option foo in section None')
 
2872
        self.assertEndsWith(warning, 'was changed from bar to <CREATED>.'
 
2873
                            ' The <DELETED> value will be saved.')
 
2874
 
 
2875
 
 
2876
class TestQuotingIniFileStore(tests.TestCaseWithTransport):
 
2877
 
 
2878
    def get_store(self):
 
2879
        return config.TransportIniFileStore(self.get_transport(), 'foo.conf')
 
2880
 
 
2881
    def test_get_quoted_string(self):
 
2882
        store = self.get_store()
 
2883
        store._load_from_string(b'foo= " abc "')
 
2884
        stack = config.Stack([store.get_sections])
 
2885
        self.assertEqual(' abc ', stack.get('foo'))
 
2886
 
 
2887
    def test_set_quoted_string(self):
 
2888
        store = self.get_store()
 
2889
        stack = config.Stack([store.get_sections], store)
 
2890
        stack.set('foo', ' a b c ')
 
2891
        store.save()
 
2892
        self.assertFileEqual(b'foo = " a b c "' + os.linesep.encode('ascii'), 'foo.conf')
 
2893
 
 
2894
 
 
2895
class TestTransportIniFileStore(TestStore):
 
2896
 
 
2897
    def test_loading_unknown_file_fails(self):
 
2898
        store = config.TransportIniFileStore(self.get_transport(),
 
2899
            'I-do-not-exist')
 
2900
        self.assertRaises(errors.NoSuchFile, store.load)
 
2901
 
 
2902
    def test_invalid_content(self):
 
2903
        store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
 
2904
        self.assertEqual(False, store.is_loaded())
 
2905
        exc = self.assertRaises(
 
2906
            config.ParseConfigError, store._load_from_string,
 
2907
            b'this is invalid !')
 
2908
        self.assertEndsWith(exc.filename, 'foo.conf')
 
2909
        # And the load failed
 
2910
        self.assertEqual(False, store.is_loaded())
 
2911
 
 
2912
    def test_get_embedded_sections(self):
 
2913
        # A more complicated example (which also shows that section names and
 
2914
        # option names share the same name space...)
 
2915
        # FIXME: This should be fixed by forbidding dicts as values ?
 
2916
        # -- vila 2011-04-05
 
2917
        store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
 
2918
        store._load_from_string(b'''
 
2919
foo=bar
 
2920
l=1,2
 
2921
[DEFAULT]
 
2922
foo_in_DEFAULT=foo_DEFAULT
 
2923
[bar]
 
2924
foo_in_bar=barbar
 
2925
[baz]
 
2926
foo_in_baz=barbaz
 
2927
[[qux]]
 
2928
foo_in_qux=quux
 
2929
''')
 
2930
        sections = list(store.get_sections())
 
2931
        self.assertLength(4, sections)
 
2932
        # The default section has no name.
 
2933
        # List values are provided as strings and need to be explicitly
 
2934
        # converted by specifying from_unicode=list_from_store at option
 
2935
        # registration
 
2936
        self.assertSectionContent((None, {'foo': 'bar', 'l': u'1,2'}),
 
2937
                                  sections[0])
 
2938
        self.assertSectionContent(
 
2939
            ('DEFAULT', {'foo_in_DEFAULT': 'foo_DEFAULT'}), sections[1])
 
2940
        self.assertSectionContent(
 
2941
            ('bar', {'foo_in_bar': 'barbar'}), sections[2])
 
2942
        # sub sections are provided as embedded dicts.
 
2943
        self.assertSectionContent(
 
2944
            ('baz', {'foo_in_baz': 'barbaz', 'qux': {'foo_in_qux': 'quux'}}),
 
2945
            sections[3])
 
2946
 
 
2947
 
 
2948
class TestLockableIniFileStore(TestStore):
 
2949
 
 
2950
    def test_create_store_in_created_dir(self):
 
2951
        self.assertPathDoesNotExist('dir')
 
2952
        t = self.get_transport('dir/subdir')
 
2953
        store = config.LockableIniFileStore(t, 'foo.conf')
 
2954
        store.get_mutable_section(None).set('foo', 'bar')
 
2955
        store.save()
 
2956
        self.assertPathExists('dir/subdir')
 
2957
 
 
2958
 
 
2959
class TestConcurrentStoreUpdates(TestStore):
 
2960
    """Test that Stores properly handle conccurent updates.
 
2961
 
 
2962
    New Store implementation may fail some of these tests but until such
 
2963
    implementations exist it's hard to properly filter them from the scenarios
 
2964
    applied here. If you encounter such a case, contact the bzr devs.
 
2965
    """
 
2966
 
 
2967
    scenarios = [(key, {'get_stack': builder}) for key, builder
 
2968
                 in config.test_stack_builder_registry.iteritems()]
 
2969
 
 
2970
    def setUp(self):
 
2971
        super(TestConcurrentStoreUpdates, self).setUp()
 
2972
        self.stack = self.get_stack(self)
 
2973
        if not isinstance(self.stack, config._CompatibleStack):
 
2974
            raise tests.TestNotApplicable(
 
2975
                '%s is not meant to be compatible with the old config design'
 
2976
                % (self.stack,))
 
2977
        self.stack.set('one', '1')
 
2978
        self.stack.set('two', '2')
 
2979
        # Flush the store
 
2980
        self.stack.store.save()
 
2981
 
 
2982
    def test_simple_read_access(self):
 
2983
        self.assertEqual('1', self.stack.get('one'))
 
2984
 
 
2985
    def test_simple_write_access(self):
 
2986
        self.stack.set('one', 'one')
 
2987
        self.assertEqual('one', self.stack.get('one'))
 
2988
 
 
2989
    def test_listen_to_the_last_speaker(self):
 
2990
        c1 = self.stack
 
2991
        c2 = self.get_stack(self)
 
2992
        c1.set('one', 'ONE')
 
2993
        c2.set('two', 'TWO')
 
2994
        self.assertEqual('ONE', c1.get('one'))
 
2995
        self.assertEqual('TWO', c2.get('two'))
 
2996
        # The second update respect the first one
 
2997
        self.assertEqual('ONE', c2.get('one'))
 
2998
 
 
2999
    def test_last_speaker_wins(self):
 
3000
        # If the same config is not shared, the same variable modified twice
 
3001
        # can only see a single result.
 
3002
        c1 = self.stack
 
3003
        c2 = self.get_stack(self)
 
3004
        c1.set('one', 'c1')
 
3005
        c2.set('one', 'c2')
 
3006
        self.assertEqual('c2', c2.get('one'))
 
3007
        # The first modification is still available until another refresh
 
3008
        # occur
 
3009
        self.assertEqual('c1', c1.get('one'))
 
3010
        c1.set('two', 'done')
 
3011
        self.assertEqual('c2', c1.get('one'))
 
3012
 
 
3013
    def test_writes_are_serialized(self):
 
3014
        c1 = self.stack
 
3015
        c2 = self.get_stack(self)
 
3016
 
 
3017
        # We spawn a thread that will pause *during* the config saving.
 
3018
        before_writing = threading.Event()
 
3019
        after_writing = threading.Event()
 
3020
        writing_done = threading.Event()
 
3021
        c1_save_without_locking_orig = c1.store.save_without_locking
 
3022
        def c1_save_without_locking():
 
3023
            before_writing.set()
 
3024
            c1_save_without_locking_orig()
 
3025
            # The lock is held. We wait for the main thread to decide when to
 
3026
            # continue
 
3027
            after_writing.wait()
 
3028
        c1.store.save_without_locking = c1_save_without_locking
 
3029
        def c1_set():
 
3030
            c1.set('one', 'c1')
 
3031
            writing_done.set()
 
3032
        t1 = threading.Thread(target=c1_set)
 
3033
        # Collect the thread after the test
 
3034
        self.addCleanup(t1.join)
 
3035
        # Be ready to unblock the thread if the test goes wrong
 
3036
        self.addCleanup(after_writing.set)
 
3037
        t1.start()
 
3038
        before_writing.wait()
 
3039
        self.assertRaises(errors.LockContention,
 
3040
                          c2.set, 'one', 'c2')
 
3041
        self.assertEqual('c1', c1.get('one'))
 
3042
        # Let the lock be released
 
3043
        after_writing.set()
 
3044
        writing_done.wait()
 
3045
        c2.set('one', 'c2')
 
3046
        self.assertEqual('c2', c2.get('one'))
 
3047
 
 
3048
    def test_read_while_writing(self):
 
3049
       c1 = self.stack
 
3050
       # We spawn a thread that will pause *during* the write
 
3051
       ready_to_write = threading.Event()
 
3052
       do_writing = threading.Event()
 
3053
       writing_done = threading.Event()
 
3054
       # We override the _save implementation so we know the store is locked
 
3055
       c1_save_without_locking_orig = c1.store.save_without_locking
 
3056
       def c1_save_without_locking():
 
3057
           ready_to_write.set()
 
3058
           # The lock is held. We wait for the main thread to decide when to
 
3059
           # continue
 
3060
           do_writing.wait()
 
3061
           c1_save_without_locking_orig()
 
3062
           writing_done.set()
 
3063
       c1.store.save_without_locking = c1_save_without_locking
 
3064
       def c1_set():
 
3065
           c1.set('one', 'c1')
 
3066
       t1 = threading.Thread(target=c1_set)
 
3067
       # Collect the thread after the test
 
3068
       self.addCleanup(t1.join)
 
3069
       # Be ready to unblock the thread if the test goes wrong
 
3070
       self.addCleanup(do_writing.set)
 
3071
       t1.start()
 
3072
       # Ensure the thread is ready to write
 
3073
       ready_to_write.wait()
 
3074
       self.assertEqual('c1', c1.get('one'))
 
3075
       # If we read during the write, we get the old value
 
3076
       c2 = self.get_stack(self)
 
3077
       self.assertEqual('1', c2.get('one'))
 
3078
       # Let the writing occur and ensure it occurred
 
3079
       do_writing.set()
 
3080
       writing_done.wait()
 
3081
       # Now we get the updated value
 
3082
       c3 = self.get_stack(self)
 
3083
       self.assertEqual('c1', c3.get('one'))
 
3084
 
 
3085
    # FIXME: It may be worth looking into removing the lock dir when it's not
 
3086
    # needed anymore and look at possible fallouts for concurrent lockers. This
 
3087
    # will matter if/when we use config files outside of breezy directories
 
3088
    # (.config/breezy or .bzr) -- vila 20110-04-111
 
3089
 
 
3090
 
 
3091
class TestSectionMatcher(TestStore):
 
3092
 
 
3093
    scenarios = [('location', {'matcher': config.LocationMatcher}),
 
3094
                 ('id', {'matcher': config.NameMatcher}),]
 
3095
 
 
3096
    def setUp(self):
 
3097
        super(TestSectionMatcher, self).setUp()
 
3098
        # Any simple store is good enough
 
3099
        self.get_store = config.test_store_builder_registry.get('configobj')
 
3100
 
 
3101
    def test_no_matches_for_empty_stores(self):
 
3102
        store = self.get_store(self)
 
3103
        store._load_from_string(b'')
 
3104
        matcher = self.matcher(store, '/bar')
 
3105
        self.assertEqual([], list(matcher.get_sections()))
 
3106
 
 
3107
    def test_build_doesnt_load_store(self):
 
3108
        store = self.get_store(self)
 
3109
        self.matcher(store, '/bar')
 
3110
        self.assertFalse(store.is_loaded())
 
3111
 
 
3112
 
 
3113
class TestLocationSection(tests.TestCase):
 
3114
 
 
3115
    def get_section(self, options, extra_path):
 
3116
        section = config.Section('foo', options)
 
3117
        return config.LocationSection(section, extra_path)
 
3118
 
 
3119
    def test_simple_option(self):
 
3120
        section = self.get_section({'foo': 'bar'}, '')
 
3121
        self.assertEqual('bar', section.get('foo'))
 
3122
 
 
3123
    def test_option_with_extra_path(self):
 
3124
        section = self.get_section({'foo': 'bar', 'foo:policy': 'appendpath'},
 
3125
                                   'baz')
 
3126
        self.assertEqual('bar/baz', section.get('foo'))
 
3127
 
 
3128
    def test_invalid_policy(self):
 
3129
        section = self.get_section({'foo': 'bar', 'foo:policy': 'die'},
 
3130
                                   'baz')
 
3131
        # invalid policies are ignored
 
3132
        self.assertEqual('bar', section.get('foo'))
 
3133
 
 
3134
 
 
3135
class TestLocationMatcher(TestStore):
 
3136
 
 
3137
    def setUp(self):
 
3138
        super(TestLocationMatcher, self).setUp()
 
3139
        # Any simple store is good enough
 
3140
        self.get_store = config.test_store_builder_registry.get('configobj')
 
3141
 
 
3142
    def test_unrelated_section_excluded(self):
 
3143
        store = self.get_store(self)
 
3144
        store._load_from_string(b'''
 
3145
[/foo]
 
3146
section=/foo
 
3147
[/foo/baz]
 
3148
section=/foo/baz
 
3149
[/foo/bar]
 
3150
section=/foo/bar
 
3151
[/foo/bar/baz]
 
3152
section=/foo/bar/baz
 
3153
[/quux/quux]
 
3154
section=/quux/quux
 
3155
''')
 
3156
        self.assertEqual(['/foo', '/foo/baz', '/foo/bar', '/foo/bar/baz',
 
3157
                           '/quux/quux'],
 
3158
                          [section.id for _, section in store.get_sections()])
 
3159
        matcher = config.LocationMatcher(store, '/foo/bar/quux')
 
3160
        sections = [section for _, section in matcher.get_sections()]
 
3161
        self.assertEqual(['/foo/bar', '/foo'],
 
3162
                          [section.id for section in sections])
 
3163
        self.assertEqual(['quux', 'bar/quux'],
 
3164
                          [section.extra_path for section in sections])
 
3165
 
 
3166
    def test_more_specific_sections_first(self):
 
3167
        store = self.get_store(self)
 
3168
        store._load_from_string(b'''
 
3169
[/foo]
 
3170
section=/foo
 
3171
[/foo/bar]
 
3172
section=/foo/bar
 
3173
''')
 
3174
        self.assertEqual(['/foo', '/foo/bar'],
 
3175
                          [section.id for _, section in store.get_sections()])
 
3176
        matcher = config.LocationMatcher(store, '/foo/bar/baz')
 
3177
        sections = [section for _, section in matcher.get_sections()]
 
3178
        self.assertEqual(['/foo/bar', '/foo'],
 
3179
                          [section.id for section in sections])
 
3180
        self.assertEqual(['baz', 'bar/baz'],
 
3181
                          [section.extra_path for section in sections])
 
3182
 
 
3183
    def test_appendpath_in_no_name_section(self):
 
3184
        # It's a bit weird to allow appendpath in a no-name section, but
 
3185
        # someone may found a use for it
 
3186
        store = self.get_store(self)
 
3187
        store._load_from_string(b'''
 
3188
foo=bar
 
3189
foo:policy = appendpath
 
3190
''')
 
3191
        matcher = config.LocationMatcher(store, 'dir/subdir')
 
3192
        sections = list(matcher.get_sections())
 
3193
        self.assertLength(1, sections)
 
3194
        self.assertEqual('bar/dir/subdir', sections[0][1].get('foo'))
 
3195
 
 
3196
    def test_file_urls_are_normalized(self):
 
3197
        store = self.get_store(self)
 
3198
        if sys.platform == 'win32':
 
3199
            expected_url = 'file:///C:/dir/subdir'
 
3200
            expected_location = 'C:/dir/subdir'
 
3201
        else:
 
3202
            expected_url = 'file:///dir/subdir'
 
3203
            expected_location = '/dir/subdir'
 
3204
        matcher = config.LocationMatcher(store, expected_url)
 
3205
        self.assertEqual(expected_location, matcher.location)
 
3206
 
 
3207
    def test_branch_name_colo(self):
 
3208
        store = self.get_store(self)
 
3209
        store._load_from_string(dedent("""\
 
3210
            [/]
 
3211
            push_location=my{branchname}
 
3212
        """).encode('ascii'))
 
3213
        matcher = config.LocationMatcher(store, 'file:///,branch=example%3c')
 
3214
        self.assertEqual('example<', matcher.branch_name)
 
3215
        ((_, section),) = matcher.get_sections()
 
3216
        self.assertEqual('example<', section.locals['branchname'])
 
3217
 
 
3218
    def test_branch_name_basename(self):
 
3219
        store = self.get_store(self)
 
3220
        store._load_from_string(dedent("""\
 
3221
            [/]
 
3222
            push_location=my{branchname}
 
3223
        """).encode('ascii'))
 
3224
        matcher = config.LocationMatcher(store, 'file:///parent/example%3c')
 
3225
        self.assertEqual('example<', matcher.branch_name)
 
3226
        ((_, section),) = matcher.get_sections()
 
3227
        self.assertEqual('example<', section.locals['branchname'])
 
3228
 
 
3229
 
 
3230
class TestStartingPathMatcher(TestStore):
 
3231
 
 
3232
    def setUp(self):
 
3233
        super(TestStartingPathMatcher, self).setUp()
 
3234
        # Any simple store is good enough
 
3235
        self.store = config.IniFileStore()
 
3236
 
 
3237
    def assertSectionIDs(self, expected, location, content):
 
3238
        self.store._load_from_string(content)
 
3239
        matcher = config.StartingPathMatcher(self.store, location)
 
3240
        sections = list(matcher.get_sections())
 
3241
        self.assertLength(len(expected), sections)
 
3242
        self.assertEqual(expected, [section.id for _, section in sections])
 
3243
        return sections
 
3244
 
 
3245
    def test_empty(self):
 
3246
        self.assertSectionIDs([], self.get_url(), b'')
 
3247
 
 
3248
    def test_url_vs_local_paths(self):
 
3249
        # The matcher location is an url and the section names are local paths
 
3250
        self.assertSectionIDs(['/foo/bar', '/foo'],
 
3251
                              'file:///foo/bar/baz', b'''\
 
3252
[/foo]
 
3253
[/foo/bar]
 
3254
''')
 
3255
 
 
3256
    def test_local_path_vs_url(self):
 
3257
        # The matcher location is a local path and the section names are urls
 
3258
        self.assertSectionIDs(['file:///foo/bar', 'file:///foo'],
 
3259
                              '/foo/bar/baz', b'''\
 
3260
[file:///foo]
 
3261
[file:///foo/bar]
 
3262
''')
 
3263
 
 
3264
 
 
3265
    def test_no_name_section_included_when_present(self):
 
3266
        # Note that other tests will cover the case where the no-name section
 
3267
        # is empty and as such, not included.
 
3268
        sections = self.assertSectionIDs(['/foo/bar', '/foo', None],
 
3269
                                         '/foo/bar/baz', b'''\
 
3270
option = defined so the no-name section exists
 
3271
[/foo]
 
3272
[/foo/bar]
 
3273
''')
 
3274
        self.assertEqual(['baz', 'bar/baz', '/foo/bar/baz'],
 
3275
                          [s.locals['relpath'] for _, s in sections])
 
3276
 
 
3277
    def test_order_reversed(self):
 
3278
        self.assertSectionIDs(['/foo/bar', '/foo'], '/foo/bar/baz', b'''\
 
3279
[/foo]
 
3280
[/foo/bar]
 
3281
''')
 
3282
 
 
3283
    def test_unrelated_section_excluded(self):
 
3284
        self.assertSectionIDs(['/foo/bar', '/foo'], '/foo/bar/baz', b'''\
 
3285
[/foo]
 
3286
[/foo/qux]
 
3287
[/foo/bar]
 
3288
''')
 
3289
 
 
3290
    def test_glob_included(self):
 
3291
        sections = self.assertSectionIDs(['/foo/*/baz', '/foo/b*', '/foo'],
 
3292
                                         '/foo/bar/baz', b'''\
 
3293
[/foo]
 
3294
[/foo/qux]
 
3295
[/foo/b*]
 
3296
[/foo/*/baz]
 
3297
''')
 
3298
        # Note that 'baz' as a relpath for /foo/b* is not fully correct, but
 
3299
        # nothing really is... as far using {relpath} to append it to something
 
3300
        # else, this seems good enough though.
 
3301
        self.assertEqual(['', 'baz', 'bar/baz'],
 
3302
                          [s.locals['relpath'] for _, s in sections])
 
3303
 
 
3304
    def test_respect_order(self):
 
3305
        self.assertSectionIDs(['/foo', '/foo/b*', '/foo/*/baz'],
 
3306
                              '/foo/bar/baz', b'''\
 
3307
[/foo/*/baz]
 
3308
[/foo/qux]
 
3309
[/foo/b*]
 
3310
[/foo]
 
3311
''')
 
3312
 
 
3313
 
 
3314
class TestNameMatcher(TestStore):
 
3315
 
 
3316
    def setUp(self):
 
3317
        super(TestNameMatcher, self).setUp()
 
3318
        self.matcher = config.NameMatcher
 
3319
        # Any simple store is good enough
 
3320
        self.get_store = config.test_store_builder_registry.get('configobj')
 
3321
 
 
3322
    def get_matching_sections(self, name):
 
3323
        store = self.get_store(self)
 
3324
        store._load_from_string(b'''
 
3325
[foo]
 
3326
option=foo
 
3327
[foo/baz]
 
3328
option=foo/baz
 
3329
[bar]
 
3330
option=bar
 
3331
''')
 
3332
        matcher = self.matcher(store, name)
 
3333
        return list(matcher.get_sections())
 
3334
 
 
3335
    def test_matching(self):
 
3336
        sections = self.get_matching_sections('foo')
 
3337
        self.assertLength(1, sections)
 
3338
        self.assertSectionContent(('foo', {'option': 'foo'}), sections[0])
 
3339
 
 
3340
    def test_not_matching(self):
 
3341
        sections = self.get_matching_sections('baz')
 
3342
        self.assertLength(0, sections)
 
3343
 
 
3344
 
 
3345
class TestBaseStackGet(tests.TestCase):
 
3346
 
 
3347
    def setUp(self):
 
3348
        super(TestBaseStackGet, self).setUp()
 
3349
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
 
3350
 
 
3351
    def test_get_first_definition(self):
 
3352
        store1 = config.IniFileStore()
 
3353
        store1._load_from_string(b'foo=bar')
 
3354
        store2 = config.IniFileStore()
 
3355
        store2._load_from_string(b'foo=baz')
 
3356
        conf = config.Stack([store1.get_sections, store2.get_sections])
 
3357
        self.assertEqual('bar', conf.get('foo'))
 
3358
 
 
3359
    def test_get_with_registered_default_value(self):
 
3360
        config.option_registry.register(config.Option('foo', default='bar'))
 
3361
        conf_stack = config.Stack([])
 
3362
        self.assertEqual('bar', conf_stack.get('foo'))
 
3363
 
 
3364
    def test_get_without_registered_default_value(self):
 
3365
        config.option_registry.register(config.Option('foo'))
 
3366
        conf_stack = config.Stack([])
 
3367
        self.assertEqual(None, conf_stack.get('foo'))
 
3368
 
 
3369
    def test_get_without_default_value_for_not_registered(self):
 
3370
        conf_stack = config.Stack([])
 
3371
        self.assertEqual(None, conf_stack.get('foo'))
 
3372
 
 
3373
    def test_get_for_empty_section_callable(self):
 
3374
        conf_stack = config.Stack([lambda : []])
 
3375
        self.assertEqual(None, conf_stack.get('foo'))
 
3376
 
 
3377
    def test_get_for_broken_callable(self):
 
3378
        # Trying to use and invalid callable raises an exception on first use
 
3379
        conf_stack = config.Stack([object])
 
3380
        self.assertRaises(TypeError, conf_stack.get, 'foo')
 
3381
 
 
3382
 
 
3383
class TestStackWithSimpleStore(tests.TestCase):
 
3384
 
 
3385
    def setUp(self):
 
3386
        super(TestStackWithSimpleStore, self).setUp()
 
3387
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
 
3388
        self.registry = config.option_registry
 
3389
 
 
3390
    def get_conf(self, content=None):
 
3391
        return config.MemoryStack(content)
 
3392
 
 
3393
    def test_override_value_from_env(self):
 
3394
        self.overrideEnv('FOO', None)
 
3395
        self.registry.register(
 
3396
            config.Option('foo', default='bar', override_from_env=['FOO']))
 
3397
        self.overrideEnv('FOO', 'quux')
 
3398
        # Env variable provides a default taking over the option one
 
3399
        conf = self.get_conf(b'foo=store')
 
3400
        self.assertEqual('quux', conf.get('foo'))
 
3401
 
 
3402
    def test_first_override_value_from_env_wins(self):
 
3403
        self.overrideEnv('NO_VALUE', None)
 
3404
        self.overrideEnv('FOO', None)
 
3405
        self.overrideEnv('BAZ', None)
 
3406
        self.registry.register(
 
3407
            config.Option('foo', default='bar',
 
3408
                          override_from_env=['NO_VALUE', 'FOO', 'BAZ']))
 
3409
        self.overrideEnv('FOO', 'foo')
 
3410
        self.overrideEnv('BAZ', 'baz')
 
3411
        # The first env var set wins
 
3412
        conf = self.get_conf(b'foo=store')
 
3413
        self.assertEqual('foo', conf.get('foo'))
 
3414
 
 
3415
 
 
3416
class TestMemoryStack(tests.TestCase):
 
3417
 
 
3418
    def test_get(self):
 
3419
        conf = config.MemoryStack(b'foo=bar')
 
3420
        self.assertEqual('bar', conf.get('foo'))
 
3421
 
 
3422
    def test_set(self):
 
3423
        conf = config.MemoryStack(b'foo=bar')
 
3424
        conf.set('foo', 'baz')
 
3425
        self.assertEqual('baz', conf.get('foo'))
 
3426
 
 
3427
    def test_no_content(self):
 
3428
        conf = config.MemoryStack()
 
3429
        # No content means no loading
 
3430
        self.assertFalse(conf.store.is_loaded())
 
3431
        self.assertRaises(NotImplementedError, conf.get, 'foo')
 
3432
        # But a content can still be provided
 
3433
        conf.store._load_from_string(b'foo=bar')
 
3434
        self.assertEqual('bar', conf.get('foo'))
 
3435
 
 
3436
 
 
3437
class TestStackIterSections(tests.TestCase):
 
3438
 
 
3439
    def test_empty_stack(self):
 
3440
        conf = config.Stack([])
 
3441
        sections = list(conf.iter_sections())
 
3442
        self.assertLength(0, sections)
 
3443
 
 
3444
    def test_empty_store(self):
 
3445
        store = config.IniFileStore()
 
3446
        store._load_from_string(b'')
 
3447
        conf = config.Stack([store.get_sections])
 
3448
        sections = list(conf.iter_sections())
 
3449
        self.assertLength(0, sections)
 
3450
 
 
3451
    def test_simple_store(self):
 
3452
        store = config.IniFileStore()
 
3453
        store._load_from_string(b'foo=bar')
 
3454
        conf = config.Stack([store.get_sections])
 
3455
        tuples = list(conf.iter_sections())
 
3456
        self.assertLength(1, tuples)
 
3457
        (found_store, found_section) = tuples[0]
 
3458
        self.assertIs(store, found_store)
 
3459
 
 
3460
    def test_two_stores(self):
 
3461
        store1 = config.IniFileStore()
 
3462
        store1._load_from_string(b'foo=bar')
 
3463
        store2 = config.IniFileStore()
 
3464
        store2._load_from_string(b'bar=qux')
 
3465
        conf = config.Stack([store1.get_sections, store2.get_sections])
 
3466
        tuples = list(conf.iter_sections())
 
3467
        self.assertLength(2, tuples)
 
3468
        self.assertIs(store1, tuples[0][0])
 
3469
        self.assertIs(store2, tuples[1][0])
 
3470
 
 
3471
 
 
3472
class TestStackWithTransport(tests.TestCaseWithTransport):
 
3473
 
 
3474
    scenarios = [(key, {'get_stack': builder}) for key, builder
 
3475
                 in config.test_stack_builder_registry.iteritems()]
 
3476
 
 
3477
 
 
3478
class TestConcreteStacks(TestStackWithTransport):
 
3479
 
 
3480
    def test_build_stack(self):
 
3481
        # Just a smoke test to help debug builders
 
3482
        self.get_stack(self)
 
3483
 
 
3484
 
 
3485
class TestStackGet(TestStackWithTransport):
 
3486
 
 
3487
    def setUp(self):
 
3488
        super(TestStackGet, self).setUp()
 
3489
        self.conf = self.get_stack(self)
 
3490
 
 
3491
    def test_get_for_empty_stack(self):
 
3492
        self.assertEqual(None, self.conf.get('foo'))
 
3493
 
 
3494
    def test_get_hook(self):
 
3495
        self.conf.set('foo', 'bar')
 
3496
        calls = []
 
3497
        def hook(*args):
 
3498
            calls.append(args)
 
3499
        config.ConfigHooks.install_named_hook('get', hook, None)
 
3500
        self.assertLength(0, calls)
 
3501
        value = self.conf.get('foo')
 
3502
        self.assertEqual('bar', value)
 
3503
        self.assertLength(1, calls)
 
3504
        self.assertEqual((self.conf, 'foo', 'bar'), calls[0])
 
3505
 
 
3506
 
 
3507
class TestStackGetWithConverter(tests.TestCase):
 
3508
 
 
3509
    def setUp(self):
 
3510
        super(TestStackGetWithConverter, self).setUp()
 
3511
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
 
3512
        self.registry = config.option_registry
 
3513
 
 
3514
    def get_conf(self, content=None):
 
3515
        return config.MemoryStack(content)
 
3516
 
 
3517
    def register_bool_option(self, name, default=None, default_from_env=None):
 
3518
        b = config.Option(name, help='A boolean.',
 
3519
                          default=default, default_from_env=default_from_env,
 
3520
                          from_unicode=config.bool_from_store)
 
3521
        self.registry.register(b)
 
3522
 
 
3523
    def test_get_default_bool_None(self):
 
3524
        self.register_bool_option('foo')
 
3525
        conf = self.get_conf(b'')
 
3526
        self.assertEqual(None, conf.get('foo'))
 
3527
 
 
3528
    def test_get_default_bool_True(self):
 
3529
        self.register_bool_option('foo', u'True')
 
3530
        conf = self.get_conf(b'')
 
3531
        self.assertEqual(True, conf.get('foo'))
 
3532
 
 
3533
    def test_get_default_bool_False(self):
 
3534
        self.register_bool_option('foo', False)
 
3535
        conf = self.get_conf(b'')
 
3536
        self.assertEqual(False, conf.get('foo'))
 
3537
 
 
3538
    def test_get_default_bool_False_as_string(self):
 
3539
        self.register_bool_option('foo', u'False')
 
3540
        conf = self.get_conf(b'')
 
3541
        self.assertEqual(False, conf.get('foo'))
 
3542
 
 
3543
    def test_get_default_bool_from_env_converted(self):
 
3544
        self.register_bool_option('foo', u'True', default_from_env=['FOO'])
 
3545
        self.overrideEnv('FOO', 'False')
 
3546
        conf = self.get_conf(b'')
 
3547
        self.assertEqual(False, conf.get('foo'))
 
3548
 
 
3549
    def test_get_default_bool_when_conversion_fails(self):
 
3550
        self.register_bool_option('foo', default='True')
 
3551
        conf = self.get_conf(b'foo=invalid boolean')
 
3552
        self.assertEqual(True, conf.get('foo'))
 
3553
 
 
3554
    def register_integer_option(self, name,
 
3555
                                default=None, default_from_env=None):
 
3556
        i = config.Option(name, help='An integer.',
 
3557
                          default=default, default_from_env=default_from_env,
 
3558
                          from_unicode=config.int_from_store)
 
3559
        self.registry.register(i)
 
3560
 
 
3561
    def test_get_default_integer_None(self):
 
3562
        self.register_integer_option('foo')
 
3563
        conf = self.get_conf(b'')
 
3564
        self.assertEqual(None, conf.get('foo'))
 
3565
 
 
3566
    def test_get_default_integer(self):
 
3567
        self.register_integer_option('foo', 42)
 
3568
        conf = self.get_conf(b'')
 
3569
        self.assertEqual(42, conf.get('foo'))
 
3570
 
 
3571
    def test_get_default_integer_as_string(self):
 
3572
        self.register_integer_option('foo', u'42')
 
3573
        conf = self.get_conf(b'')
 
3574
        self.assertEqual(42, conf.get('foo'))
 
3575
 
 
3576
    def test_get_default_integer_from_env(self):
 
3577
        self.register_integer_option('foo', default_from_env=['FOO'])
 
3578
        self.overrideEnv('FOO', '18')
 
3579
        conf = self.get_conf(b'')
 
3580
        self.assertEqual(18, conf.get('foo'))
 
3581
 
 
3582
    def test_get_default_integer_when_conversion_fails(self):
 
3583
        self.register_integer_option('foo', default='12')
 
3584
        conf = self.get_conf(b'foo=invalid integer')
 
3585
        self.assertEqual(12, conf.get('foo'))
 
3586
 
 
3587
    def register_list_option(self, name, default=None, default_from_env=None):
 
3588
        l = config.ListOption(name, help='A list.', default=default,
 
3589
                              default_from_env=default_from_env)
 
3590
        self.registry.register(l)
 
3591
 
 
3592
    def test_get_default_list_None(self):
 
3593
        self.register_list_option('foo')
 
3594
        conf = self.get_conf(b'')
 
3595
        self.assertEqual(None, conf.get('foo'))
 
3596
 
 
3597
    def test_get_default_list_empty(self):
 
3598
        self.register_list_option('foo', '')
 
3599
        conf = self.get_conf(b'')
 
3600
        self.assertEqual([], conf.get('foo'))
 
3601
 
 
3602
    def test_get_default_list_from_env(self):
 
3603
        self.register_list_option('foo', default_from_env=['FOO'])
 
3604
        self.overrideEnv('FOO', '')
 
3605
        conf = self.get_conf(b'')
 
3606
        self.assertEqual([], conf.get('foo'))
 
3607
 
 
3608
    def test_get_with_list_converter_no_item(self):
 
3609
        self.register_list_option('foo', None)
 
3610
        conf = self.get_conf(b'foo=,')
 
3611
        self.assertEqual([], conf.get('foo'))
 
3612
 
 
3613
    def test_get_with_list_converter_many_items(self):
 
3614
        self.register_list_option('foo', None)
 
3615
        conf = self.get_conf(b'foo=m,o,r,e')
 
3616
        self.assertEqual(['m', 'o', 'r', 'e'], conf.get('foo'))
 
3617
 
 
3618
    def test_get_with_list_converter_embedded_spaces_many_items(self):
 
3619
        self.register_list_option('foo', None)
 
3620
        conf = self.get_conf(b'foo=" bar", "baz "')
 
3621
        self.assertEqual([' bar', 'baz '], conf.get('foo'))
 
3622
 
 
3623
    def test_get_with_list_converter_stripped_spaces_many_items(self):
 
3624
        self.register_list_option('foo', None)
 
3625
        conf = self.get_conf(b'foo= bar ,  baz ')
 
3626
        self.assertEqual(['bar', 'baz'], conf.get('foo'))
 
3627
 
 
3628
 
 
3629
class TestIterOptionRefs(tests.TestCase):
 
3630
    """iter_option_refs is a bit unusual, document some cases."""
 
3631
 
 
3632
    def assertRefs(self, expected, string):
 
3633
        self.assertEqual(expected, list(config.iter_option_refs(string)))
 
3634
 
 
3635
    def test_empty(self):
 
3636
        self.assertRefs([(False, '')], '')
 
3637
 
 
3638
    def test_no_refs(self):
 
3639
        self.assertRefs([(False, 'foo bar')], 'foo bar')
 
3640
 
 
3641
    def test_single_ref(self):
 
3642
        self.assertRefs([(False, ''), (True, '{foo}'), (False, '')], '{foo}')
 
3643
 
 
3644
    def test_broken_ref(self):
 
3645
        self.assertRefs([(False, '{foo')], '{foo')
 
3646
 
 
3647
    def test_embedded_ref(self):
 
3648
        self.assertRefs([(False, '{'), (True, '{foo}'), (False, '}')],
 
3649
                        '{{foo}}')
 
3650
 
 
3651
    def test_two_refs(self):
 
3652
        self.assertRefs([(False, ''), (True, '{foo}'),
 
3653
                         (False, ''), (True, '{bar}'),
 
3654
                         (False, ''),],
 
3655
                        '{foo}{bar}')
 
3656
 
 
3657
    def test_newline_in_refs_are_not_matched(self):
 
3658
        self.assertRefs([(False, '{\nxx}{xx\n}{{\n}}')], '{\nxx}{xx\n}{{\n}}')
 
3659
 
 
3660
 
 
3661
class TestStackExpandOptions(tests.TestCaseWithTransport):
 
3662
 
 
3663
    def setUp(self):
 
3664
        super(TestStackExpandOptions, self).setUp()
 
3665
        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
 
3666
        self.registry = config.option_registry
 
3667
        store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
 
3668
        self.conf = config.Stack([store.get_sections], store)
 
3669
 
 
3670
    def assertExpansion(self, expected, string, env=None):
 
3671
        self.assertEqual(expected, self.conf.expand_options(string, env))
 
3672
 
 
3673
    def test_no_expansion(self):
 
3674
        self.assertExpansion('foo', 'foo')
 
3675
 
 
3676
    def test_expand_default_value(self):
 
3677
        self.conf.store._load_from_string(b'bar=baz')
 
3678
        self.registry.register(config.Option('foo', default=u'{bar}'))
 
3679
        self.assertEqual('baz', self.conf.get('foo', expand=True))
 
3680
 
 
3681
    def test_expand_default_from_env(self):
 
3682
        self.conf.store._load_from_string(b'bar=baz')
 
3683
        self.registry.register(config.Option('foo', default_from_env=['FOO']))
 
3684
        self.overrideEnv('FOO', '{bar}')
 
3685
        self.assertEqual('baz', self.conf.get('foo', expand=True))
 
3686
 
 
3687
    def test_expand_default_on_failed_conversion(self):
 
3688
        self.conf.store._load_from_string(b'baz=bogus\nbar=42\nfoo={baz}')
 
3689
        self.registry.register(
 
3690
            config.Option('foo', default=u'{bar}',
 
3691
                          from_unicode=config.int_from_store))
 
3692
        self.assertEqual(42, self.conf.get('foo', expand=True))
 
3693
 
 
3694
    def test_env_adding_options(self):
 
3695
        self.assertExpansion('bar', '{foo}', {'foo': 'bar'})
 
3696
 
 
3697
    def test_env_overriding_options(self):
 
3698
        self.conf.store._load_from_string(b'foo=baz')
 
3699
        self.assertExpansion('bar', '{foo}', {'foo': 'bar'})
 
3700
 
 
3701
    def test_simple_ref(self):
 
3702
        self.conf.store._load_from_string(b'foo=xxx')
 
3703
        self.assertExpansion('xxx', '{foo}')
 
3704
 
 
3705
    def test_unknown_ref(self):
 
3706
        self.assertRaises(config.ExpandingUnknownOption,
 
3707
                          self.conf.expand_options, '{foo}')
 
3708
 
 
3709
    def test_illegal_def_is_ignored(self):
 
3710
        self.assertExpansion('{1,2}', '{1,2}')
 
3711
        self.assertExpansion('{ }', '{ }')
 
3712
        self.assertExpansion('${Foo,f}', '${Foo,f}')
 
3713
 
 
3714
    def test_indirect_ref(self):
 
3715
        self.conf.store._load_from_string(b'''
 
3716
foo=xxx
 
3717
bar={foo}
 
3718
''')
 
3719
        self.assertExpansion('xxx', '{bar}')
 
3720
 
 
3721
    def test_embedded_ref(self):
 
3722
        self.conf.store._load_from_string(b'''
 
3723
foo=xxx
 
3724
bar=foo
 
3725
''')
 
3726
        self.assertExpansion('xxx', '{{bar}}')
 
3727
 
 
3728
    def test_simple_loop(self):
 
3729
        self.conf.store._load_from_string(b'foo={foo}')
 
3730
        self.assertRaises(config.OptionExpansionLoop,
 
3731
                          self.conf.expand_options, '{foo}')
 
3732
 
 
3733
    def test_indirect_loop(self):
 
3734
        self.conf.store._load_from_string(b'''
 
3735
foo={bar}
 
3736
bar={baz}
 
3737
baz={foo}''')
 
3738
        e = self.assertRaises(config.OptionExpansionLoop,
 
3739
                              self.conf.expand_options, '{foo}')
 
3740
        self.assertEqual('foo->bar->baz', e.refs)
 
3741
        self.assertEqual('{foo}', e.string)
 
3742
 
 
3743
    def test_list(self):
 
3744
        self.conf.store._load_from_string(b'''
 
3745
foo=start
 
3746
bar=middle
 
3747
baz=end
 
3748
list={foo},{bar},{baz}
 
3749
''')
 
3750
        self.registry.register(
 
3751
            config.ListOption('list'))
 
3752
        self.assertEqual(['start', 'middle', 'end'],
 
3753
                           self.conf.get('list', expand=True))
 
3754
 
 
3755
    def test_cascading_list(self):
 
3756
        self.conf.store._load_from_string(b'''
 
3757
foo=start,{bar}
 
3758
bar=middle,{baz}
 
3759
baz=end
 
3760
list={foo}
 
3761
''')
 
3762
        self.registry.register(config.ListOption('list'))
 
3763
        # Register an intermediate option as a list to ensure no conversion
 
3764
        # happen while expanding. Conversion should only occur for the original
 
3765
        # option ('list' here).
 
3766
        self.registry.register(config.ListOption('baz'))
 
3767
        self.assertEqual(['start', 'middle', 'end'],
 
3768
                           self.conf.get('list', expand=True))
 
3769
 
 
3770
    def test_pathologically_hidden_list(self):
 
3771
        self.conf.store._load_from_string(b'''
 
3772
foo=bin
 
3773
bar=go
 
3774
start={foo
 
3775
middle=},{
 
3776
end=bar}
 
3777
hidden={start}{middle}{end}
 
3778
''')
 
3779
        # What matters is what the registration says, the conversion happens
 
3780
        # only after all expansions have been performed
 
3781
        self.registry.register(config.ListOption('hidden'))
 
3782
        self.assertEqual(['bin', 'go'],
 
3783
                          self.conf.get('hidden', expand=True))
 
3784
 
 
3785
 
 
3786
class TestStackCrossSectionsExpand(tests.TestCaseWithTransport):
 
3787
 
 
3788
    def setUp(self):
 
3789
        super(TestStackCrossSectionsExpand, self).setUp()
 
3790
 
 
3791
    def get_config(self, location, string):
 
3792
        if string is None:
 
3793
            string = b''
 
3794
        # Since we don't save the config we won't strictly require to inherit
 
3795
        # from TestCaseInTempDir, but an error occurs so quickly...
 
3796
        c = config.LocationStack(location)
 
3797
        c.store._load_from_string(string)
 
3798
        return c
 
3799
 
 
3800
    def test_dont_cross_unrelated_section(self):
 
3801
        c = self.get_config('/another/branch/path', b'''
 
3802
[/one/branch/path]
 
3803
foo = hello
 
3804
bar = {foo}/2
 
3805
 
 
3806
[/another/branch/path]
 
3807
bar = {foo}/2
 
3808
''')
 
3809
        self.assertRaises(config.ExpandingUnknownOption,
 
3810
                          c.get, 'bar', expand=True)
 
3811
 
 
3812
    def test_cross_related_sections(self):
 
3813
        c = self.get_config('/project/branch/path', b'''
 
3814
[/project]
 
3815
foo = qu
 
3816
 
 
3817
[/project/branch/path]
 
3818
bar = {foo}ux
 
3819
''')
 
3820
        self.assertEqual('quux', c.get('bar', expand=True))
 
3821
 
 
3822
 
 
3823
class TestStackCrossStoresExpand(tests.TestCaseWithTransport):
 
3824
 
 
3825
    def test_cross_global_locations(self):
 
3826
        l_store = config.LocationStore()
 
3827
        l_store._load_from_string(b'''
 
3828
[/branch]
 
3829
lfoo = loc-foo
 
3830
lbar = {gbar}
 
3831
''')
 
3832
        l_store.save()
 
3833
        g_store = config.GlobalStore()
 
3834
        g_store._load_from_string(b'''
 
3835
[DEFAULT]
 
3836
gfoo = {lfoo}
 
3837
gbar = glob-bar
 
3838
''')
 
3839
        g_store.save()
 
3840
        stack = config.LocationStack('/branch')
 
3841
        self.assertEqual('glob-bar', stack.get('lbar', expand=True))
 
3842
        self.assertEqual('loc-foo', stack.get('gfoo', expand=True))
 
3843
 
 
3844
 
 
3845
class TestStackExpandSectionLocals(tests.TestCaseWithTransport):
 
3846
 
 
3847
    def test_expand_locals_empty(self):
 
3848
        l_store = config.LocationStore()
 
3849
        l_store._load_from_string(b'''
 
3850
[/home/user/project]
 
3851
base = {basename}
 
3852
rel = {relpath}
 
3853
''')
 
3854
        l_store.save()
 
3855
        stack = config.LocationStack('/home/user/project/')
 
3856
        self.assertEqual('', stack.get('base', expand=True))
 
3857
        self.assertEqual('', stack.get('rel', expand=True))
 
3858
 
 
3859
    def test_expand_basename_locally(self):
 
3860
        l_store = config.LocationStore()
 
3861
        l_store._load_from_string(b'''
 
3862
[/home/user/project]
 
3863
bfoo = {basename}
 
3864
''')
 
3865
        l_store.save()
 
3866
        stack = config.LocationStack('/home/user/project/branch')
 
3867
        self.assertEqual('branch', stack.get('bfoo', expand=True))
 
3868
 
 
3869
    def test_expand_basename_locally_longer_path(self):
 
3870
        l_store = config.LocationStore()
 
3871
        l_store._load_from_string(b'''
 
3872
[/home/user]
 
3873
bfoo = {basename}
 
3874
''')
 
3875
        l_store.save()
 
3876
        stack = config.LocationStack('/home/user/project/dir/branch')
 
3877
        self.assertEqual('branch', stack.get('bfoo', expand=True))
 
3878
 
 
3879
    def test_expand_relpath_locally(self):
 
3880
        l_store = config.LocationStore()
 
3881
        l_store._load_from_string(b'''
 
3882
[/home/user/project]
 
3883
lfoo = loc-foo/{relpath}
 
3884
''')
 
3885
        l_store.save()
 
3886
        stack = config.LocationStack('/home/user/project/branch')
 
3887
        self.assertEqual('loc-foo/branch', stack.get('lfoo', expand=True))
 
3888
 
 
3889
    def test_expand_relpath_unknonw_in_global(self):
 
3890
        g_store = config.GlobalStore()
 
3891
        g_store._load_from_string(b'''
 
3892
[DEFAULT]
 
3893
gfoo = {relpath}
 
3894
''')
 
3895
        g_store.save()
 
3896
        stack = config.LocationStack('/home/user/project/branch')
 
3897
        self.assertRaises(config.ExpandingUnknownOption,
 
3898
                          stack.get, 'gfoo', expand=True)
 
3899
 
 
3900
    def test_expand_local_option_locally(self):
 
3901
        l_store = config.LocationStore()
 
3902
        l_store._load_from_string(b'''
 
3903
[/home/user/project]
 
3904
lfoo = loc-foo/{relpath}
 
3905
lbar = {gbar}
 
3906
''')
 
3907
        l_store.save()
 
3908
        g_store = config.GlobalStore()
 
3909
        g_store._load_from_string(b'''
 
3910
[DEFAULT]
 
3911
gfoo = {lfoo}
 
3912
gbar = glob-bar
 
3913
''')
 
3914
        g_store.save()
 
3915
        stack = config.LocationStack('/home/user/project/branch')
 
3916
        self.assertEqual('glob-bar', stack.get('lbar', expand=True))
 
3917
        self.assertEqual('loc-foo/branch', stack.get('gfoo', expand=True))
 
3918
 
 
3919
    def test_locals_dont_leak(self):
 
3920
        """Make sure we chose the right local in presence of several sections.
 
3921
        """
 
3922
        l_store = config.LocationStore()
 
3923
        l_store._load_from_string(b'''
 
3924
[/home/user]
 
3925
lfoo = loc-foo/{relpath}
 
3926
[/home/user/project]
 
3927
lfoo = loc-foo/{relpath}
 
3928
''')
 
3929
        l_store.save()
 
3930
        stack = config.LocationStack('/home/user/project/branch')
 
3931
        self.assertEqual('loc-foo/branch', stack.get('lfoo', expand=True))
 
3932
        stack = config.LocationStack('/home/user/bar/baz')
 
3933
        self.assertEqual('loc-foo/bar/baz', stack.get('lfoo', expand=True))
 
3934
 
 
3935
 
 
3936
 
 
3937
class TestStackSet(TestStackWithTransport):
 
3938
 
 
3939
    def test_simple_set(self):
 
3940
        conf = self.get_stack(self)
 
3941
        self.assertEqual(None, conf.get('foo'))
 
3942
        conf.set('foo', 'baz')
 
3943
        # Did we get it back ?
 
3944
        self.assertEqual('baz', conf.get('foo'))
 
3945
 
 
3946
    def test_set_creates_a_new_section(self):
 
3947
        conf = self.get_stack(self)
 
3948
        conf.set('foo', 'baz')
 
3949
        self.assertEqual, 'baz', conf.get('foo')
 
3950
 
 
3951
    def test_set_hook(self):
 
3952
        calls = []
 
3953
        def hook(*args):
 
3954
            calls.append(args)
 
3955
        config.ConfigHooks.install_named_hook('set', hook, None)
 
3956
        self.assertLength(0, calls)
 
3957
        conf = self.get_stack(self)
 
3958
        conf.set('foo', 'bar')
 
3959
        self.assertLength(1, calls)
 
3960
        self.assertEqual((conf, 'foo', 'bar'), calls[0])
 
3961
 
 
3962
 
 
3963
class TestStackRemove(TestStackWithTransport):
 
3964
 
 
3965
    def test_remove_existing(self):
 
3966
        conf = self.get_stack(self)
 
3967
        conf.set('foo', 'bar')
 
3968
        self.assertEqual('bar', conf.get('foo'))
 
3969
        conf.remove('foo')
 
3970
        # Did we get it back ?
 
3971
        self.assertEqual(None, conf.get('foo'))
 
3972
 
 
3973
    def test_remove_unknown(self):
 
3974
        conf = self.get_stack(self)
 
3975
        self.assertRaises(KeyError, conf.remove, 'I_do_not_exist')
 
3976
 
 
3977
    def test_remove_hook(self):
 
3978
        calls = []
 
3979
        def hook(*args):
 
3980
            calls.append(args)
 
3981
        config.ConfigHooks.install_named_hook('remove', hook, None)
 
3982
        self.assertLength(0, calls)
 
3983
        conf = self.get_stack(self)
 
3984
        conf.set('foo', 'bar')
 
3985
        conf.remove('foo')
 
3986
        self.assertLength(1, calls)
 
3987
        self.assertEqual((conf, 'foo'), calls[0])
 
3988
 
 
3989
 
 
3990
class TestConfigGetOptions(tests.TestCaseWithTransport, TestOptionsMixin):
 
3991
 
 
3992
    def setUp(self):
 
3993
        super(TestConfigGetOptions, self).setUp()
 
3994
        create_configs(self)
 
3995
 
 
3996
    def test_no_variable(self):
 
3997
        # Using branch should query branch, locations and breezy
 
3998
        self.assertOptions([], self.branch_config)
 
3999
 
 
4000
    def test_option_in_breezy(self):
 
4001
        self.breezy_config.set_user_option('file', 'breezy')
 
4002
        self.assertOptions([('file', 'breezy', 'DEFAULT', 'breezy')],
 
4003
                           self.breezy_config)
 
4004
 
 
4005
    def test_option_in_locations(self):
 
4006
        self.locations_config.set_user_option('file', 'locations')
 
4007
        self.assertOptions(
 
4008
            [('file', 'locations', self.tree.basedir, 'locations')],
 
4009
            self.locations_config)
 
4010
 
 
4011
    def test_option_in_branch(self):
 
4012
        self.branch_config.set_user_option('file', 'branch')
 
4013
        self.assertOptions([('file', 'branch', 'DEFAULT', 'branch')],
 
4014
                           self.branch_config)
 
4015
 
 
4016
    def test_option_in_breezy_and_branch(self):
 
4017
        self.breezy_config.set_user_option('file', 'breezy')
 
4018
        self.branch_config.set_user_option('file', 'branch')
 
4019
        self.assertOptions([('file', 'branch', 'DEFAULT', 'branch'),
 
4020
                            ('file', 'breezy', 'DEFAULT', 'breezy'),],
 
4021
                           self.branch_config)
 
4022
 
 
4023
    def test_option_in_branch_and_locations(self):
 
4024
        # Hmm, locations override branch :-/
 
4025
        self.locations_config.set_user_option('file', 'locations')
 
4026
        self.branch_config.set_user_option('file', 'branch')
 
4027
        self.assertOptions(
 
4028
            [('file', 'locations', self.tree.basedir, 'locations'),
 
4029
             ('file', 'branch', 'DEFAULT', 'branch'),],
 
4030
            self.branch_config)
 
4031
 
 
4032
    def test_option_in_breezy_locations_and_branch(self):
 
4033
        self.breezy_config.set_user_option('file', 'breezy')
 
4034
        self.locations_config.set_user_option('file', 'locations')
 
4035
        self.branch_config.set_user_option('file', 'branch')
 
4036
        self.assertOptions(
 
4037
            [('file', 'locations', self.tree.basedir, 'locations'),
 
4038
             ('file', 'branch', 'DEFAULT', 'branch'),
 
4039
             ('file', 'breezy', 'DEFAULT', 'breezy'),],
 
4040
            self.branch_config)
 
4041
 
 
4042
 
 
4043
class TestConfigRemoveOption(tests.TestCaseWithTransport, TestOptionsMixin):
 
4044
 
 
4045
    def setUp(self):
 
4046
        super(TestConfigRemoveOption, self).setUp()
 
4047
        create_configs_with_file_option(self)
 
4048
 
 
4049
    def test_remove_in_locations(self):
 
4050
        self.locations_config.remove_user_option('file', self.tree.basedir)
 
4051
        self.assertOptions(
 
4052
            [('file', 'branch', 'DEFAULT', 'branch'),
 
4053
             ('file', 'breezy', 'DEFAULT', 'breezy'),],
 
4054
            self.branch_config)
 
4055
 
 
4056
    def test_remove_in_branch(self):
 
4057
        self.branch_config.remove_user_option('file')
 
4058
        self.assertOptions(
 
4059
            [('file', 'locations', self.tree.basedir, 'locations'),
 
4060
             ('file', 'breezy', 'DEFAULT', 'breezy'),],
 
4061
            self.branch_config)
 
4062
 
 
4063
    def test_remove_in_breezy(self):
 
4064
        self.breezy_config.remove_user_option('file')
 
4065
        self.assertOptions(
 
4066
            [('file', 'locations', self.tree.basedir, 'locations'),
 
4067
             ('file', 'branch', 'DEFAULT', 'branch'),],
 
4068
            self.branch_config)
 
4069
 
 
4070
 
 
4071
class TestConfigGetSections(tests.TestCaseWithTransport):
 
4072
 
 
4073
    def setUp(self):
 
4074
        super(TestConfigGetSections, self).setUp()
 
4075
        create_configs(self)
 
4076
 
 
4077
    def assertSectionNames(self, expected, conf, name=None):
 
4078
        """Check which sections are returned for a given config.
 
4079
 
 
4080
        If fallback configurations exist their sections can be included.
 
4081
 
 
4082
        :param expected: A list of section names.
 
4083
 
 
4084
        :param conf: The configuration that will be queried.
 
4085
 
 
4086
        :param name: An optional section name that will be passed to
 
4087
            get_sections().
 
4088
        """
 
4089
        sections = list(conf._get_sections(name))
 
4090
        self.assertLength(len(expected), sections)
 
4091
        self.assertEqual(expected, [n for n, _, _ in sections])
 
4092
 
 
4093
    def test_breezy_default_section(self):
 
4094
        self.assertSectionNames(['DEFAULT'], self.breezy_config)
 
4095
 
 
4096
    def test_locations_default_section(self):
 
4097
        # No sections are defined in an empty file
 
4098
        self.assertSectionNames([], self.locations_config)
 
4099
 
 
4100
    def test_locations_named_section(self):
 
4101
        self.locations_config.set_user_option('file', 'locations')
 
4102
        self.assertSectionNames([self.tree.basedir], self.locations_config)
 
4103
 
 
4104
    def test_locations_matching_sections(self):
 
4105
        loc_config = self.locations_config
 
4106
        loc_config.set_user_option('file', 'locations')
 
4107
        # We need to cheat a bit here to create an option in sections above and
 
4108
        # below the 'location' one.
 
4109
        parser = loc_config._get_parser()
 
4110
        # locations.cong deals with '/' ignoring native os.sep
 
4111
        location_names = self.tree.basedir.split('/')
 
4112
        parent = '/'.join(location_names[:-1])
 
4113
        child = '/'.join(location_names + ['child'])
 
4114
        parser[parent] = {}
 
4115
        parser[parent]['file'] = 'parent'
 
4116
        parser[child] = {}
 
4117
        parser[child]['file'] = 'child'
 
4118
        self.assertSectionNames([self.tree.basedir, parent], loc_config)
 
4119
 
 
4120
    def test_branch_data_default_section(self):
 
4121
        self.assertSectionNames([None],
 
4122
                                self.branch_config._get_branch_data_config())
 
4123
 
 
4124
    def test_branch_default_sections(self):
 
4125
        # No sections are defined in an empty locations file
 
4126
        self.assertSectionNames([None, 'DEFAULT'],
 
4127
                                self.branch_config)
 
4128
        # Unless we define an option
 
4129
        self.branch_config._get_location_config().set_user_option(
 
4130
            'file', 'locations')
 
4131
        self.assertSectionNames([self.tree.basedir, None, 'DEFAULT'],
 
4132
                                self.branch_config)
 
4133
 
 
4134
    def test_breezy_named_section(self):
 
4135
        # We need to cheat as the API doesn't give direct access to sections
 
4136
        # other than DEFAULT.
 
4137
        self.breezy_config.set_alias('breezy', 'bzr')
 
4138
        self.assertSectionNames(['ALIASES'], self.breezy_config, 'ALIASES')
 
4139
 
 
4140
 
 
4141
class TestSharedStores(tests.TestCaseInTempDir):
 
4142
 
 
4143
    def test_breezy_conf_shared(self):
 
4144
        g1 = config.GlobalStack()
 
4145
        g2 = config.GlobalStack()
 
4146
        # The two stacks share the same store
 
4147
        self.assertIs(g1.store, g2.store)
 
4148
 
 
4149
 
 
4150
class TestAuthenticationConfigFilePermissions(tests.TestCaseInTempDir):
 
4151
    """Test warning for permissions of authentication.conf."""
 
4152
 
 
4153
    def setUp(self):
 
4154
        super(TestAuthenticationConfigFilePermissions, self).setUp()
 
4155
        self.path = osutils.pathjoin(self.test_dir, 'authentication.conf')
 
4156
        with open(self.path, 'wb') as f:
 
4157
            f.write(b"""[broken]
 
4158
scheme=ftp
 
4159
user=joe
 
4160
port=port # Error: Not an int
 
4161
""")
 
4162
        self.overrideAttr(config, 'authentication_config_filename',
 
4163
            lambda: self.path)
 
4164
        osutils.chmod_if_possible(self.path, 0o755)
 
4165
 
 
4166
    def test_check_warning(self):
 
4167
        conf = config.AuthenticationConfig()
 
4168
        self.assertEqual(conf._filename, self.path)
 
4169
        self.assertContainsRe(self.get_log(),
 
4170
            'Saved passwords may be accessible by other users.')
 
4171
 
 
4172
    def test_check_suppressed_warning(self):
 
4173
        global_config = config.GlobalConfig()
 
4174
        global_config.set_user_option('suppress_warnings',
 
4175
            'insecure_permissions')
 
4176
        conf = config.AuthenticationConfig()
 
4177
        self.assertEqual(conf._filename, self.path)
 
4178
        self.assertNotContainsRe(self.get_log(),
 
4179
            'Saved passwords may be accessible by other users.')
 
4180
 
 
4181
 
1315
4182
class TestAuthenticationConfigFile(tests.TestCase):
1316
4183
    """Test the authentication.conf file matching"""
1317
4184
 
1324
4191
        else:
1325
4192
            user = credentials['user']
1326
4193
            password = credentials['password']
1327
 
        self.assertEquals(expected_user, user)
1328
 
        self.assertEquals(expected_password, password)
 
4194
        self.assertEqual(expected_user, user)
 
4195
        self.assertEqual(expected_password, password)
1329
4196
 
1330
4197
    def test_empty_config(self):
1331
 
        conf = config.AuthenticationConfig(_file=StringIO())
1332
 
        self.assertEquals({}, conf._get_config())
 
4198
        conf = config.AuthenticationConfig(_file=BytesIO())
 
4199
        self.assertEqual({}, conf._get_config())
1333
4200
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1334
4201
 
 
4202
    def test_non_utf8_config(self):
 
4203
        conf = config.AuthenticationConfig(_file=BytesIO(b'foo = bar\xff'))
 
4204
        self.assertRaises(config.ConfigContentError, conf._get_config)
 
4205
 
1335
4206
    def test_missing_auth_section_header(self):
1336
 
        conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
 
4207
        conf = config.AuthenticationConfig(_file=BytesIO(b'foo = bar'))
1337
4208
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1338
4209
 
1339
4210
    def test_auth_section_header_not_closed(self):
1340
 
        conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1341
 
        self.assertRaises(errors.ParseConfigError, conf._get_config)
 
4211
        conf = config.AuthenticationConfig(_file=BytesIO(b'[DEF'))
 
4212
        self.assertRaises(config.ParseConfigError, conf._get_config)
1342
4213
 
1343
4214
    def test_auth_value_not_boolean(self):
1344
 
        conf = config.AuthenticationConfig(_file=StringIO(
1345
 
                """[broken]
 
4215
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4216
[broken]
1346
4217
scheme=ftp
1347
4218
user=joe
1348
4219
verify_certificates=askme # Error: Not a boolean
1350
4221
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1351
4222
 
1352
4223
    def test_auth_value_not_int(self):
1353
 
        conf = config.AuthenticationConfig(_file=StringIO(
1354
 
                """[broken]
 
4224
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4225
[broken]
1355
4226
scheme=ftp
1356
4227
user=joe
1357
4228
port=port # Error: Not an int
1359
4230
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1360
4231
 
1361
4232
    def test_unknown_password_encoding(self):
1362
 
        conf = config.AuthenticationConfig(_file=StringIO(
1363
 
                """[broken]
 
4233
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4234
[broken]
1364
4235
scheme=ftp
1365
4236
user=joe
1366
4237
password_encoding=unknown
1369
4240
                          'ftp', 'foo.net', 'joe')
1370
4241
 
1371
4242
    def test_credentials_for_scheme_host(self):
1372
 
        conf = config.AuthenticationConfig(_file=StringIO(
1373
 
                """# Identity on foo.net
 
4243
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4244
# Identity on foo.net
1374
4245
[ftp definition]
1375
4246
scheme=ftp
1376
4247
host=foo.net
1385
4256
        self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1386
4257
 
1387
4258
    def test_credentials_for_host_port(self):
1388
 
        conf = config.AuthenticationConfig(_file=StringIO(
1389
 
                """# Identity on foo.net
 
4259
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4260
# Identity on foo.net
1390
4261
[ftp definition]
1391
4262
scheme=ftp
1392
4263
port=10021
1401
4272
        self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1402
4273
 
1403
4274
    def test_for_matching_host(self):
1404
 
        conf = config.AuthenticationConfig(_file=StringIO(
1405
 
                """# Identity on foo.net
 
4275
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4276
# Identity on foo.net
1406
4277
[sourceforge]
1407
4278
scheme=bzr
1408
4279
host=bzr.sf.net
1422
4293
                              conf, 'bzr', 'bbzr.sf.net')
1423
4294
 
1424
4295
    def test_for_matching_host_None(self):
1425
 
        conf = config.AuthenticationConfig(_file=StringIO(
1426
 
                """# Identity on foo.net
 
4296
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4297
# Identity on foo.net
1427
4298
[catchup bzr]
1428
4299
scheme=bzr
1429
4300
user=joe
1440
4311
                              conf, 'ftp', 'quux.net')
1441
4312
 
1442
4313
    def test_credentials_for_path(self):
1443
 
        conf = config.AuthenticationConfig(_file=StringIO(
1444
 
                """
 
4314
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
1445
4315
[http dir1]
1446
4316
scheme=http
1447
4317
host=bar.org
1463
4333
                              conf, 'http', host='bar.org', path='/dir2')
1464
4334
        # matching subdir
1465
4335
        self._got_user_passwd('jim', 'jimpass',
1466
 
                              conf, 'http', host='bar.org',path='/dir1/subdir')
 
4336
                              conf, 'http', host='bar.org', path='/dir1/subdir')
1467
4337
 
1468
4338
    def test_credentials_for_user(self):
1469
 
        conf = config.AuthenticationConfig(_file=StringIO(
1470
 
                """
 
4339
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
1471
4340
[with user]
1472
4341
scheme=http
1473
4342
host=bar.org
1485
4354
                              conf, 'http', 'bar.org', user='georges')
1486
4355
 
1487
4356
    def test_credentials_for_user_without_password(self):
1488
 
        conf = config.AuthenticationConfig(_file=StringIO(
1489
 
                """
 
4357
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
1490
4358
[without password]
1491
4359
scheme=http
1492
4360
host=bar.org
1497
4365
                              conf, 'http', 'bar.org')
1498
4366
 
1499
4367
    def test_verify_certificates(self):
1500
 
        conf = config.AuthenticationConfig(_file=StringIO(
1501
 
                """
 
4368
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
1502
4369
[self-signed]
1503
4370
scheme=https
1504
4371
host=bar.org
1512
4379
password=bendover
1513
4380
"""))
1514
4381
        credentials = conf.get_credentials('https', 'bar.org')
1515
 
        self.assertEquals(False, credentials.get('verify_certificates'))
 
4382
        self.assertEqual(False, credentials.get('verify_certificates'))
1516
4383
        credentials = conf.get_credentials('https', 'foo.net')
1517
 
        self.assertEquals(True, credentials.get('verify_certificates'))
 
4384
        self.assertEqual(True, credentials.get('verify_certificates'))
1518
4385
 
1519
4386
 
1520
4387
class TestAuthenticationStorage(tests.TestCaseInTempDir):
1527
4394
                                           port=99, path='/foo',
1528
4395
                                           realm='realm')
1529
4396
        CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
1530
 
                       'verify_certificates': False, 'scheme': 'scheme', 
1531
 
                       'host': 'host', 'port': 99, 'path': '/foo', 
 
4397
                       'verify_certificates': False, 'scheme': 'scheme',
 
4398
                       'host': 'host', 'port': 99, 'path': '/foo',
1532
4399
                       'realm': 'realm'}
1533
4400
        self.assertEqual(CREDENTIALS, credentials)
1534
4401
        credentials_from_disk = config.AuthenticationConfig().get_credentials(
1542
4409
        self.assertIs(None, conf._get_config().get('name'))
1543
4410
        credentials = conf.get_credentials(host='host', scheme='scheme')
1544
4411
        CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
1545
 
                       'password', 'verify_certificates': True, 
1546
 
                       'scheme': 'scheme', 'host': 'host', 'port': None, 
 
4412
                       'password', 'verify_certificates': True,
 
4413
                       'scheme': 'scheme', 'host': 'host', 'port': None,
1547
4414
                       'path': None, 'realm': None}
1548
4415
        self.assertEqual(CREDENTIALS, credentials)
1549
4416
 
1550
4417
 
1551
 
class TestAuthenticationConfig(tests.TestCase):
 
4418
class TestAuthenticationConfig(tests.TestCaseInTempDir):
1552
4419
    """Test AuthenticationConfig behaviour"""
1553
4420
 
1554
4421
    def _check_default_password_prompt(self, expected_prompt_format, scheme,
1561
4428
            'scheme': scheme, 'host': host, 'port': port,
1562
4429
            'user': user, 'realm': realm}
1563
4430
 
1564
 
        stdout = tests.StringIOWrapper()
1565
 
        stderr = tests.StringIOWrapper()
1566
 
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1567
 
                                            stdout=stdout, stderr=stderr)
 
4431
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n')
1568
4432
        # We use an empty conf so that the user is always prompted
1569
4433
        conf = config.AuthenticationConfig()
1570
 
        self.assertEquals(password,
 
4434
        self.assertEqual(password,
1571
4435
                          conf.get_password(scheme, host, user, port=port,
1572
4436
                                            realm=realm, path=path))
1573
 
        self.assertEquals(expected_prompt, stderr.getvalue())
1574
 
        self.assertEquals('', stdout.getvalue())
 
4437
        self.assertEqual(expected_prompt, ui.ui_factory.stderr.getvalue())
 
4438
        self.assertEqual('', ui.ui_factory.stdout.getvalue())
1575
4439
 
1576
4440
    def _check_default_username_prompt(self, expected_prompt_format, scheme,
1577
4441
                                       host=None, port=None, realm=None,
1582
4446
        expected_prompt = expected_prompt_format % {
1583
4447
            'scheme': scheme, 'host': host, 'port': port,
1584
4448
            'realm': realm}
1585
 
        stdout = tests.StringIOWrapper()
1586
 
        stderr = tests.StringIOWrapper()
1587
 
        ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
1588
 
                                            stdout=stdout, stderr=stderr)
 
4449
        ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n')
1589
4450
        # We use an empty conf so that the user is always prompted
1590
4451
        conf = config.AuthenticationConfig()
1591
 
        self.assertEquals(username, conf.get_user(scheme, host, port=port,
 
4452
        self.assertEqual(username, conf.get_user(scheme, host, port=port,
1592
4453
                          realm=realm, path=path, ask=True))
1593
 
        self.assertEquals(expected_prompt, stderr.getvalue())
1594
 
        self.assertEquals('', stdout.getvalue())
 
4454
        self.assertEqual(expected_prompt, ui.ui_factory.stderr.getvalue())
 
4455
        self.assertEqual('', ui.ui_factory.stdout.getvalue())
1595
4456
 
1596
4457
    def test_username_defaults_prompts(self):
1597
4458
        # HTTP prompts can't be tested here, see test_http.py
1598
 
        self._check_default_username_prompt('FTP %(host)s username: ', 'ftp')
1599
 
        self._check_default_username_prompt(
1600
 
            'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
1601
 
        self._check_default_username_prompt(
1602
 
            'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
 
4459
        self._check_default_username_prompt(u'FTP %(host)s username: ', 'ftp')
 
4460
        self._check_default_username_prompt(
 
4461
            u'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
 
4462
        self._check_default_username_prompt(
 
4463
            u'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
1603
4464
 
1604
4465
    def test_username_default_no_prompt(self):
1605
4466
        conf = config.AuthenticationConfig()
1606
 
        self.assertEquals(None,
 
4467
        self.assertEqual(None,
1607
4468
            conf.get_user('ftp', 'example.com'))
1608
 
        self.assertEquals("explicitdefault",
 
4469
        self.assertEqual("explicitdefault",
1609
4470
            conf.get_user('ftp', 'example.com', default="explicitdefault"))
1610
4471
 
1611
4472
    def test_password_default_prompts(self):
1612
4473
        # HTTP prompts can't be tested here, see test_http.py
1613
4474
        self._check_default_password_prompt(
1614
 
            'FTP %(user)s@%(host)s password: ', 'ftp')
1615
 
        self._check_default_password_prompt(
1616
 
            'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
1617
 
        self._check_default_password_prompt(
1618
 
            'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
 
4475
            u'FTP %(user)s@%(host)s password: ', 'ftp')
 
4476
        self._check_default_password_prompt(
 
4477
            u'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
 
4478
        self._check_default_password_prompt(
 
4479
            u'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
1619
4480
        # SMTP port handling is a bit special (it's handled if embedded in the
1620
4481
        # host too)
1621
4482
        # FIXME: should we: forbid that, extend it to other schemes, leave
1622
4483
        # things as they are that's fine thank you ?
1623
 
        self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1624
 
                                            'smtp')
1625
 
        self._check_default_password_prompt('SMTP %(user)s@%(host)s password: ',
1626
 
                                            'smtp', host='bar.org:10025')
1627
 
        self._check_default_password_prompt(
1628
 
            'SMTP %(user)s@%(host)s:%(port)d password: ',
1629
 
            'smtp', port=10025)
 
4484
        self._check_default_password_prompt(
 
4485
            u'SMTP %(user)s@%(host)s password: ', 'smtp')
 
4486
        self._check_default_password_prompt(
 
4487
            u'SMTP %(user)s@%(host)s password: ', 'smtp', host='bar.org:10025')
 
4488
        self._check_default_password_prompt(
 
4489
            u'SMTP %(user)s@%(host)s:%(port)d password: ', 'smtp', port=10025)
1630
4490
 
1631
4491
    def test_ssh_password_emits_warning(self):
1632
 
        conf = config.AuthenticationConfig(_file=StringIO(
1633
 
                """
 
4492
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
1634
4493
[ssh with password]
1635
4494
scheme=ssh
1636
4495
host=bar.org
1638
4497
password=jimpass
1639
4498
"""))
1640
4499
        entered_password = 'typed-by-hand'
1641
 
        stdout = tests.StringIOWrapper()
1642
 
        stderr = tests.StringIOWrapper()
1643
 
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1644
 
                                            stdout=stdout, stderr=stderr)
 
4500
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n')
1645
4501
 
1646
4502
        # Since the password defined in the authentication config is ignored,
1647
4503
        # the user is prompted
1648
 
        self.assertEquals(entered_password,
 
4504
        self.assertEqual(entered_password,
1649
4505
                          conf.get_password('ssh', 'bar.org', user='jim'))
1650
4506
        self.assertContainsRe(
1651
4507
            self.get_log(),
1652
 
            'password ignored in section \[ssh with password\]')
 
4508
            'password ignored in section \\[ssh with password\\]')
1653
4509
 
1654
4510
    def test_ssh_without_password_doesnt_emit_warning(self):
1655
 
        conf = config.AuthenticationConfig(_file=StringIO(
1656
 
                """
 
4511
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
1657
4512
[ssh with password]
1658
4513
scheme=ssh
1659
4514
host=bar.org
1660
4515
user=jim
1661
4516
"""))
1662
4517
        entered_password = 'typed-by-hand'
1663
 
        stdout = tests.StringIOWrapper()
1664
 
        stderr = tests.StringIOWrapper()
1665
 
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1666
 
                                            stdout=stdout,
1667
 
                                            stderr=stderr)
 
4518
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n')
1668
4519
 
1669
4520
        # Since the password defined in the authentication config is ignored,
1670
4521
        # the user is prompted
1671
 
        self.assertEquals(entered_password,
 
4522
        self.assertEqual(entered_password,
1672
4523
                          conf.get_password('ssh', 'bar.org', user='jim'))
1673
4524
        # No warning shoud be emitted since there is no password. We are only
1674
4525
        # providing "user".
1675
4526
        self.assertNotContainsRe(
1676
4527
            self.get_log(),
1677
 
            'password ignored in section \[ssh with password\]')
 
4528
            'password ignored in section \\[ssh with password\\]')
1678
4529
 
1679
4530
    def test_uses_fallback_stores(self):
1680
4531
        self.overrideAttr(config, 'credential_store_registry',
1682
4533
        store = StubCredentialStore()
1683
4534
        store.add_credentials("http", "example.com", "joe", "secret")
1684
4535
        config.credential_store_registry.register("stub", store, fallback=True)
1685
 
        conf = config.AuthenticationConfig(_file=StringIO())
 
4536
        conf = config.AuthenticationConfig(_file=BytesIO())
1686
4537
        creds = conf.get_credentials("http", "example.com")
1687
 
        self.assertEquals("joe", creds["user"])
1688
 
        self.assertEquals("secret", creds["password"])
 
4538
        self.assertEqual("joe", creds["user"])
 
4539
        self.assertEqual("secret", creds["password"])
1689
4540
 
1690
4541
 
1691
4542
class StubCredentialStore(config.CredentialStore):
1736
4587
 
1737
4588
    def test_fallback_none_registered(self):
1738
4589
        r = config.CredentialStoreRegistry()
1739
 
        self.assertEquals(None,
 
4590
        self.assertEqual(None,
1740
4591
                          r.get_fallback_credentials("http", "example.com"))
1741
4592
 
1742
4593
    def test_register(self):
1743
4594
        r = config.CredentialStoreRegistry()
1744
4595
        r.register("stub", StubCredentialStore(), fallback=False)
1745
4596
        r.register("another", StubCredentialStore(), fallback=True)
1746
 
        self.assertEquals(["another", "stub"], r.keys())
 
4597
        self.assertEqual(["another", "stub"], r.keys())
1747
4598
 
1748
4599
    def test_register_lazy(self):
1749
4600
        r = config.CredentialStoreRegistry()
1750
 
        r.register_lazy("stub", "bzrlib.tests.test_config",
 
4601
        r.register_lazy("stub", "breezy.tests.test_config",
1751
4602
                        "StubCredentialStore", fallback=False)
1752
 
        self.assertEquals(["stub"], r.keys())
 
4603
        self.assertEqual(["stub"], r.keys())
1753
4604
        self.assertIsInstance(r.get_credential_store("stub"),
1754
4605
                              StubCredentialStore)
1755
4606
 
1757
4608
        r = config.CredentialStoreRegistry()
1758
4609
        r.register("stub1", None, fallback=False)
1759
4610
        r.register("stub2", None, fallback=True)
1760
 
        self.assertEquals(False, r.is_fallback("stub1"))
1761
 
        self.assertEquals(True, r.is_fallback("stub2"))
 
4611
        self.assertEqual(False, r.is_fallback("stub1"))
 
4612
        self.assertEqual(True, r.is_fallback("stub2"))
1762
4613
 
1763
4614
    def test_no_fallback(self):
1764
4615
        r = config.CredentialStoreRegistry()
1765
4616
        store = CountingCredentialStore()
1766
4617
        r.register("count", store, fallback=False)
1767
 
        self.assertEquals(None,
 
4618
        self.assertEqual(None,
1768
4619
                          r.get_fallback_credentials("http", "example.com"))
1769
 
        self.assertEquals(0, store._calls)
 
4620
        self.assertEqual(0, store._calls)
1770
4621
 
1771
4622
    def test_fallback_credentials(self):
1772
4623
        r = config.CredentialStoreRegistry()
1775
4626
                              "somebody", "geheim")
1776
4627
        r.register("stub", store, fallback=True)
1777
4628
        creds = r.get_fallback_credentials("http", "example.com")
1778
 
        self.assertEquals("somebody", creds["user"])
1779
 
        self.assertEquals("geheim", creds["password"])
 
4629
        self.assertEqual("somebody", creds["user"])
 
4630
        self.assertEqual("geheim", creds["password"])
1780
4631
 
1781
4632
    def test_fallback_first_wins(self):
1782
4633
        r = config.CredentialStoreRegistry()
1789
4640
                              "somebody", "stub2")
1790
4641
        r.register("stub2", stub1, fallback=True)
1791
4642
        creds = r.get_fallback_credentials("http", "example.com")
1792
 
        self.assertEquals("somebody", creds["user"])
1793
 
        self.assertEquals("stub1", creds["password"])
 
4643
        self.assertEqual("somebody", creds["user"])
 
4644
        self.assertEqual("stub1", creds["password"])
1794
4645
 
1795
4646
 
1796
4647
class TestPlainTextCredentialStore(tests.TestCase):
1799
4650
        r = config.credential_store_registry
1800
4651
        plain_text = r.get_credential_store()
1801
4652
        decoded = plain_text.decode_password(dict(password='secret'))
1802
 
        self.assertEquals('secret', decoded)
 
4653
        self.assertEqual('secret', decoded)
 
4654
 
 
4655
 
 
4656
class TestBase64CredentialStore(tests.TestCase):
 
4657
 
 
4658
    def test_decode_password(self):
 
4659
        r = config.credential_store_registry
 
4660
        plain_text = r.get_credential_store('base64')
 
4661
        decoded = plain_text.decode_password(dict(password='c2VjcmV0'))
 
4662
        self.assertEqual(b'secret', decoded)
1803
4663
 
1804
4664
 
1805
4665
# FIXME: Once we have a way to declare authentication to all test servers, we
1812
4672
# test_user_prompted ?
1813
4673
class TestAuthenticationRing(tests.TestCaseWithTransport):
1814
4674
    pass
 
4675
 
 
4676
 
 
4677
class TestAutoUserId(tests.TestCase):
 
4678
    """Test inferring an automatic user name."""
 
4679
 
 
4680
    def test_auto_user_id(self):
 
4681
        """Automatic inference of user name.
 
4682
 
 
4683
        This is a bit hard to test in an isolated way, because it depends on
 
4684
        system functions that go direct to /etc or perhaps somewhere else.
 
4685
        But it's reasonable to say that on Unix, with an /etc/mailname, we ought
 
4686
        to be able to choose a user name with no configuration.
 
4687
        """
 
4688
        if sys.platform == 'win32':
 
4689
            raise tests.TestSkipped(
 
4690
                "User name inference not implemented on win32")
 
4691
        realname, address = config._auto_user_id()
 
4692
        if os.path.exists('/etc/mailname'):
 
4693
            self.assertIsNot(None, realname)
 
4694
            self.assertIsNot(None, address)
 
4695
        else:
 
4696
            self.assertEqual((None, None), (realname, address))
 
4697
 
 
4698
 
 
4699
class TestDefaultMailDomain(tests.TestCaseInTempDir):
 
4700
    """Test retrieving default domain from mailname file"""
 
4701
 
 
4702
    def test_default_mail_domain_simple(self):
 
4703
        with open('simple', 'w') as f:
 
4704
            f.write("domainname.com\n")
 
4705
        r = config._get_default_mail_domain('simple')
 
4706
        self.assertEqual('domainname.com', r)
 
4707
 
 
4708
    def test_default_mail_domain_no_eol(self):
 
4709
        with open('no_eol', 'w') as f:
 
4710
            f.write("domainname.com")
 
4711
        r = config._get_default_mail_domain('no_eol')
 
4712
        self.assertEqual('domainname.com', r)
 
4713
 
 
4714
    def test_default_mail_domain_multiple_lines(self):
 
4715
        with open('multiple_lines', 'w') as f:
 
4716
            f.write("domainname.com\nsome other text\n")
 
4717
        r = config._get_default_mail_domain('multiple_lines')
 
4718
        self.assertEqual('domainname.com', r)
 
4719
 
 
4720
 
 
4721
class EmailOptionTests(tests.TestCase):
 
4722
 
 
4723
    def test_default_email_uses_BRZ_EMAIL(self):
 
4724
        conf = config.MemoryStack(b'email=jelmer@debian.org')
 
4725
        # BRZ_EMAIL takes precedence over EMAIL
 
4726
        self.overrideEnv('BRZ_EMAIL', 'jelmer@samba.org')
 
4727
        self.overrideEnv('EMAIL', 'jelmer@apache.org')
 
4728
        self.assertEqual('jelmer@samba.org', conf.get('email'))
 
4729
 
 
4730
    def test_default_email_uses_EMAIL(self):
 
4731
        conf = config.MemoryStack(b'')
 
4732
        self.overrideEnv('BRZ_EMAIL', None)
 
4733
        self.overrideEnv('EMAIL', 'jelmer@apache.org')
 
4734
        self.assertEqual('jelmer@apache.org', conf.get('email'))
 
4735
 
 
4736
    def test_BRZ_EMAIL_overrides(self):
 
4737
        conf = config.MemoryStack(b'email=jelmer@debian.org')
 
4738
        self.overrideEnv('BRZ_EMAIL', 'jelmer@apache.org')
 
4739
        self.assertEqual('jelmer@apache.org', conf.get('email'))
 
4740
        self.overrideEnv('BRZ_EMAIL', None)
 
4741
        self.overrideEnv('EMAIL', 'jelmer@samba.org')
 
4742
        self.assertEqual('jelmer@debian.org', conf.get('email'))
 
4743
 
 
4744
 
 
4745
class MailClientOptionTests(tests.TestCase):
 
4746
 
 
4747
    def test_default(self):
 
4748
        conf = config.MemoryStack(b'')
 
4749
        client = conf.get('mail_client')
 
4750
        self.assertIs(client, mail_client.DefaultMail)
 
4751
 
 
4752
    def test_evolution(self):
 
4753
        conf = config.MemoryStack(b'mail_client=evolution')
 
4754
        client = conf.get('mail_client')
 
4755
        self.assertIs(client, mail_client.Evolution)
 
4756
 
 
4757
    def test_kmail(self):
 
4758
        conf = config.MemoryStack(b'mail_client=kmail')
 
4759
        client = conf.get('mail_client')
 
4760
        self.assertIs(client, mail_client.KMail)
 
4761
 
 
4762
    def test_mutt(self):
 
4763
        conf = config.MemoryStack(b'mail_client=mutt')
 
4764
        client = conf.get('mail_client')
 
4765
        self.assertIs(client, mail_client.Mutt)
 
4766
 
 
4767
    def test_thunderbird(self):
 
4768
        conf = config.MemoryStack(b'mail_client=thunderbird')
 
4769
        client = conf.get('mail_client')
 
4770
        self.assertIs(client, mail_client.Thunderbird)
 
4771
 
 
4772
    def test_explicit_default(self):
 
4773
        conf = config.MemoryStack(b'mail_client=default')
 
4774
        client = conf.get('mail_client')
 
4775
        self.assertIs(client, mail_client.DefaultMail)
 
4776
 
 
4777
    def test_editor(self):
 
4778
        conf = config.MemoryStack(b'mail_client=editor')
 
4779
        client = conf.get('mail_client')
 
4780
        self.assertIs(client, mail_client.Editor)
 
4781
 
 
4782
    def test_mapi(self):
 
4783
        conf = config.MemoryStack(b'mail_client=mapi')
 
4784
        client = conf.get('mail_client')
 
4785
        self.assertIs(client, mail_client.MAPIClient)
 
4786
 
 
4787
    def test_xdg_email(self):
 
4788
        conf = config.MemoryStack(b'mail_client=xdg-email')
 
4789
        client = conf.get('mail_client')
 
4790
        self.assertIs(client, mail_client.XDGEmail)
 
4791
 
 
4792
    def test_unknown(self):
 
4793
        conf = config.MemoryStack(b'mail_client=firebird')
 
4794
        self.assertRaises(config.ConfigOptionValueError, conf.get,
 
4795
                'mail_client')