~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

(vila) Fix test failures blocking package builds. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

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