/brz/remove-bazaar

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