/brz/remove-bazaar

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