~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-19 19:15:58 UTC
  • mfrom: (6388 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6404.
  • Revision ID: jelmer@canonical.com-20111219191558-p1k7cvhjq8l6v2gm
Merge bzr.dev.

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