/brz/remove-bazaar

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