/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005, 2006 Canonical Ltd
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
"""Tests for finding and reading the bzr config file[s]."""
19
# 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.
20
from cStringIO import StringIO
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
21
import os
22
import sys
23
24
#import bzrlib specific imports here
1878.1.3 by John Arbash Meinel
some test cleanups
25
from bzrlib import (
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
26
    branch,
27
    bzrdir,
1878.1.3 by John Arbash Meinel
some test cleanups
28
    config,
29
    errors,
30
    osutils,
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
31
    mail_client,
2900.2.14 by Vincent Ladeuil
More tests.
32
    ui,
1878.1.3 by John Arbash Meinel
some test cleanups
33
    urlutils,
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
34
    tests,
1551.15.35 by Aaron Bentley
Warn when setting config values that will be masked (#122286)
35
    trace,
3242.1.2 by Aaron Bentley
Turn BzrDirConfig into TransportConfig, reduce code duplication
36
    transport,
1878.1.3 by John Arbash Meinel
some test cleanups
37
    )
2991.2.4 by Vincent Ladeuil
Various fixes following local testing environment rebuild.
38
from bzrlib.util.configobj import configobj
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
39
40
1553.6.12 by Erik Bågfors
remove AliasConfig, based on input from abentley
41
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
42
sample_config_text = u"""
43
[DEFAULT]
44
email=Erik B\u00e5gfors <erik@bagfors.nu>
45
editor=vim
46
gpg_signing_command=gnome-gpg
47
log_format=short
48
user_global_option=something
49
[ALIASES]
50
h=help
51
ll=""" + sample_long_alias + "\n"
52
53
54
sample_always_signatures = """
55
[DEFAULT]
56
check_signatures=ignore
57
create_signatures=always
58
"""
59
60
sample_ignore_signatures = """
61
[DEFAULT]
62
check_signatures=require
63
create_signatures=never
64
"""
65
66
sample_maybe_signatures = """
67
[DEFAULT]
68
check_signatures=ignore
69
create_signatures=when-required
70
"""
71
72
sample_branches_text = """
73
[http://www.example.com]
74
# Top level policy
75
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
76
normal_option = normal
77
appendpath_option = append
2120.6.8 by James Henstridge
Change syntax for setting config option policies. Rather than
78
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
79
norecurse_option = norecurse
2120.6.8 by James Henstridge
Change syntax for setting config option policies. Rather than
80
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
81
[http://www.example.com/ignoreparent]
82
# different project: ignore parent dir config
83
ignore_parents=true
84
[http://www.example.com/norecurse]
85
# configuration items that only apply to this dir
86
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
87
normal_option = norecurse
88
[http://www.example.com/dir]
89
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
90
[/b/]
91
check_signatures=require
92
# test trailing / matching with no children
93
[/a/]
94
check_signatures=check-available
95
gpg_signing_command=false
96
user_local_option=local
97
# test trailing / matching
98
[/a/*]
99
#subdirs will match but not the parent
100
[/a/c]
101
check_signatures=ignore
102
post_commit=bzrlib.tests.test_config.post_commit
103
#testing explicit beats globs
104
"""
1553.6.3 by Erik Bågfors
tests for AliasesConfig
105
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
106
1474 by Robert Collins
Merge from Aaron Bentley.
107
class InstrumentedConfigObj(object):
108
    """A config obj look-enough-alike to record calls made to it."""
109
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
110
    def __contains__(self, thing):
111
        self._calls.append(('__contains__', thing))
112
        return False
113
114
    def __getitem__(self, key):
115
        self._calls.append(('__getitem__', key))
116
        return self
117
1551.2.20 by Aaron Bentley
Treated config files as utf-8
118
    def __init__(self, input, encoding=None):
119
        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.
120
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
121
    def __setitem__(self, key, value):
122
        self._calls.append(('__setitem__', key, value))
123
2120.6.4 by James Henstridge
add support for specifying policy when storing options
124
    def __delitem__(self, key):
125
        self._calls.append(('__delitem__', key))
126
127
    def keys(self):
128
        self._calls.append(('keys',))
129
        return []
130
1551.2.49 by abentley
Made ConfigObj output binary-identical files on win32 and *nix
131
    def write(self, arg):
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
132
        self._calls.append(('write',))
133
2120.6.4 by James Henstridge
add support for specifying policy when storing options
134
    def as_bool(self, value):
135
        self._calls.append(('as_bool', value))
136
        return False
137
138
    def get_value(self, section, name):
139
        self._calls.append(('get_value', section, name))
140
        return None
141
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
142
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
143
class FakeBranch(object):
144
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
145
    def __init__(self, base=None, user_id=None):
146
        if base is None:
147
            self.base = "http://example.com/branches/demo"
148
        else:
149
            self.base = base
3407.2.13 by Martin Pool
Remove indirection through control_files to get transports
150
        self._transport = self.control_files = \
151
            FakeControlFilesAndTransport(user_id=user_id)
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
152
153
    def lock_write(self):
154
        pass
155
156
    def unlock(self):
157
        pass
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
158
159
3407.2.13 by Martin Pool
Remove indirection through control_files to get transports
160
class FakeControlFilesAndTransport(object):
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
161
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
162
    def __init__(self, user_id=None):
163
        self.files = {}
3388.2.3 by Martin Pool
Fix up more uses of LockableFiles.get_utf8 in tests
164
        if user_id:
165
            self.files['email'] = user_id
3242.1.2 by Aaron Bentley
Turn BzrDirConfig into TransportConfig, reduce code duplication
166
        self._transport = self
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
167
1185.65.29 by Robert Collins
Implement final review suggestions.
168
    def get_utf8(self, filename):
3388.2.3 by Martin Pool
Fix up more uses of LockableFiles.get_utf8 in tests
169
        # from LockableFiles
170
        raise AssertionError("get_utf8 should no longer be used")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
171
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
172
    def get(self, filename):
3388.2.3 by Martin Pool
Fix up more uses of LockableFiles.get_utf8 in tests
173
        # from Transport
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
174
        try:
175
            return StringIO(self.files[filename])
176
        except KeyError:
177
            raise errors.NoSuchFile(filename)
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
178
3388.2.3 by Martin Pool
Fix up more uses of LockableFiles.get_utf8 in tests
179
    def get_bytes(self, filename):
180
        # from Transport
181
        try:
182
            return self.files[filename]
183
        except KeyError:
184
            raise errors.NoSuchFile(filename)
185
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
186
    def put(self, filename, fileobj):
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
187
        self.files[filename] = fileobj.read()
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
188
3242.1.2 by Aaron Bentley
Turn BzrDirConfig into TransportConfig, reduce code duplication
189
    def put_file(self, filename, fileobj):
190
        return self.put(filename, fileobj)
191
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
192
193
class InstrumentedConfig(config.Config):
194
    """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.
195
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
196
    def __init__(self):
197
        super(InstrumentedConfig, self).__init__()
198
        self._calls = []
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
199
        self._signatures = config.CHECK_NEVER
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
200
201
    def _get_user_id(self):
202
        self._calls.append('_get_user_id')
203
        return "Robert Collins <robert.collins@example.org>"
204
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
205
    def _get_signature_checking(self):
206
        self._calls.append('_get_signature_checking')
207
        return self._signatures
208
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
209
1556.2.2 by Aaron Bentley
Fixed get_bool
210
bool_config = """[DEFAULT]
211
active = true
212
inactive = false
213
[UPPERCASE]
214
active = True
215
nonactive = False
216
"""
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
217
218
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
219
class TestConfigObj(tests.TestCase):
3221.7.4 by Matt Nordhoff
Add test for bug #86838.
220
1556.2.2 by Aaron Bentley
Fixed get_bool
221
    def test_get_bool(self):
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
222
        co = config.ConfigObj(StringIO(bool_config))
1556.2.2 by Aaron Bentley
Fixed get_bool
223
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
224
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
225
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
226
        self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
227
3221.7.4 by Matt Nordhoff
Add test for bug #86838.
228
    def test_hash_sign_in_value(self):
229
        """
230
        Before 4.5.0, ConfigObj did not quote # signs in values, so they'd be
231
        treated as comments when read in again. (#86838)
232
        """
233
        co = config.ConfigObj()
234
        co['test'] = 'foo#bar'
235
        lines = co.write()
236
        self.assertEqual(lines, ['test = "foo#bar"'])
237
        co2 = config.ConfigObj(lines)
238
        self.assertEqual(co2['test'], 'foo#bar')
239
1556.2.2 by Aaron Bentley
Fixed get_bool
240
2900.1.1 by Vincent Ladeuil
241
erroneous_config = """[section] # line 1
242
good=good # line 2
243
[section] # line 3
244
whocares=notme # line 4
245
"""
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
246
247
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
248
class TestConfigObjErrors(tests.TestCase):
2900.1.1 by Vincent Ladeuil
249
250
    def test_duplicate_section_name_error_line(self):
251
        try:
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
252
            co = configobj.ConfigObj(StringIO(erroneous_config),
253
                                     raise_errors=True)
2900.1.1 by Vincent Ladeuil
254
        except config.configobj.DuplicateError, e:
255
            self.assertEqual(3, e.line_number)
256
        else:
257
            self.fail('Error in config file not detected')
258
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
259
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
260
class TestConfig(tests.TestCase):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
261
262
    def test_constructs(self):
263
        config.Config()
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
264
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
265
    def test_no_default_editor(self):
266
        self.assertRaises(NotImplementedError, config.Config().get_editor)
267
268
    def test_user_email(self):
269
        my_config = InstrumentedConfig()
270
        self.assertEqual('robert.collins@example.org', my_config.user_email())
271
        self.assertEqual(['_get_user_id'], my_config._calls)
272
273
    def test_username(self):
274
        my_config = InstrumentedConfig()
275
        self.assertEqual('Robert Collins <robert.collins@example.org>',
276
                         my_config.username())
277
        self.assertEqual(['_get_user_id'], my_config._calls)
1442.1.14 by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE
278
279
    def test_signatures_default(self):
280
        my_config = config.Config()
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
281
        self.assertFalse(my_config.signature_needed())
1442.1.14 by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE
282
        self.assertEqual(config.CHECK_IF_POSSIBLE,
283
                         my_config.signature_checking())
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
284
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
285
                         my_config.signing_policy())
1442.1.14 by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE
286
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
287
    def test_signatures_template_method(self):
288
        my_config = InstrumentedConfig()
289
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
290
        self.assertEqual(['_get_signature_checking'], my_config._calls)
291
292
    def test_signatures_template_method_none(self):
293
        my_config = InstrumentedConfig()
294
        my_config._signatures = None
295
        self.assertEqual(config.CHECK_IF_POSSIBLE,
296
                         my_config.signature_checking())
297
        self.assertEqual(['_get_signature_checking'], my_config._calls)
298
1442.1.56 by Robert Collins
gpg_signing_command configuration item
299
    def test_gpg_signing_command_default(self):
300
        my_config = config.Config()
301
        self.assertEqual('gpg', my_config.gpg_signing_command())
302
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
303
    def test_get_user_option_default(self):
304
        my_config = config.Config()
305
        self.assertEqual(None, my_config.get_user_option('no_option'))
306
1472 by Robert Collins
post commit hook, first pass implementation
307
    def test_post_commit_default(self):
308
        my_config = config.Config()
309
        self.assertEqual(None, my_config.post_commit())
310
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
311
    def test_log_format_default(self):
1553.2.8 by Erik Bågfors
tests for config log_formatter
312
        my_config = config.Config()
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
313
        self.assertEqual('long', my_config.log_format())
1553.2.8 by Erik Bågfors
tests for config log_formatter
314
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
315
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
316
class TestConfigPath(tests.TestCase):
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
317
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
318
    def setUp(self):
319
        super(TestConfigPath, self).setUp()
320
        os.environ['HOME'] = '/home/bogus'
2309.2.6 by Alexander Belchenko
bzr now use Win32 API to determine Application Data location, and don't rely solely on $APPDATA
321
        if sys.platform == 'win32':
322
            os.environ['BZR_HOME'] = \
323
                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.
324
            self.bzr_home = \
325
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
326
        else:
327
            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.
328
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
329
    def test_config_dir(self):
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
330
        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.
331
332
    def test_config_filename(self):
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
333
        self.assertEqual(config.config_filename(),
334
                         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.
335
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
336
    def test_branches_config_filename(self):
2991.2.4 by Vincent Ladeuil
Various fixes following local testing environment rebuild.
337
        self.assertEqual(config.branches_config_filename(),
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
338
                         self.bzr_home + '/branches.conf')
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
339
1770.2.2 by Aaron Bentley
Rename branches.conf to locations.conf
340
    def test_locations_config_filename(self):
2991.2.4 by Vincent Ladeuil
Various fixes following local testing environment rebuild.
341
        self.assertEqual(config.locations_config_filename(),
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
342
                         self.bzr_home + '/locations.conf')
1770.2.2 by Aaron Bentley
Rename branches.conf to locations.conf
343
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
344
    def test_authentication_config_filename(self):
2991.2.4 by Vincent Ladeuil
Various fixes following local testing environment rebuild.
345
        self.assertEqual(config.authentication_config_filename(),
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
346
                         self.bzr_home + '/authentication.conf')
347
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
348
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
349
class TestIniConfig(tests.TestCase):
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
350
351
    def test_contructs(self):
352
        my_config = config.IniBasedConfig("nothing")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
353
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
354
    def test_from_fp(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
355
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
356
        my_config = config.IniBasedConfig(None)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
357
        self.failUnless(
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
358
            isinstance(my_config._get_parser(file=config_file),
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
359
                        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.
360
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
361
    def test_cached(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
362
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
363
        my_config = config.IniBasedConfig(None)
364
        parser = my_config._get_parser(file=config_file)
365
        self.failUnless(my_config._get_parser() is parser)
366
367
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
368
class TestGetConfig(tests.TestCase):
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
369
370
    def test_constructs(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
371
        my_config = config.GlobalConfig()
372
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
373
    def test_calls_read_filenames(self):
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
374
        # replace the class that is constructed, to check its parameters
1474 by Robert Collins
Merge from Aaron Bentley.
375
        oldparserclass = config.ConfigObj
376
        config.ConfigObj = InstrumentedConfigObj
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
377
        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.
378
        try:
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
379
            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.
380
        finally:
1474 by Robert Collins
Merge from Aaron Bentley.
381
            config.ConfigObj = oldparserclass
382
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
1551.2.20 by Aaron Bentley
Treated config files as utf-8
383
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
384
                                          '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.
385
386
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
387
class TestBranchConfig(tests.TestCaseWithTransport):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
388
389
    def test_constructs(self):
390
        branch = FakeBranch()
391
        my_config = config.BranchConfig(branch)
392
        self.assertRaises(TypeError, config.BranchConfig)
393
394
    def test_get_location_config(self):
395
        branch = FakeBranch()
396
        my_config = config.BranchConfig(branch)
397
        location_config = my_config._get_location_config()
398
        self.assertEqual(branch.base, location_config.location)
399
        self.failUnless(location_config is my_config._get_location_config())
400
1770.2.9 by Aaron Bentley
Add Branch.get_config, update BranchConfig() callers
401
    def test_get_config(self):
402
        """The Branch.get_config method works properly"""
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
403
        b = bzrdir.BzrDir.create_standalone_workingtree('.').branch
1770.2.9 by Aaron Bentley
Add Branch.get_config, update BranchConfig() callers
404
        my_config = b.get_config()
405
        self.assertIs(my_config.get_user_option('wacky'), None)
406
        my_config.set_user_option('wacky', 'unlikely')
407
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
408
409
        # Ensure we get the same thing if we start again
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
410
        b2 = branch.Branch.open('.')
1770.2.9 by Aaron Bentley
Add Branch.get_config, update BranchConfig() callers
411
        my_config2 = b2.get_config()
412
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
413
1824.1.1 by Robert Collins
Add BranchConfig.has_explicit_nickname call.
414
    def test_has_explicit_nickname(self):
415
        b = self.make_branch('.')
416
        self.assertFalse(b.get_config().has_explicit_nickname())
417
        b.nick = 'foo'
418
        self.assertTrue(b.get_config().has_explicit_nickname())
419
1878.1.1 by John Arbash Meinel
Entries in locations.conf should prefer local paths if available (bug #53653)
420
    def test_config_url(self):
421
        """The Branch.get_config will use section that uses a local url"""
422
        branch = self.make_branch('branch')
423
        self.assertEqual('branch', branch.nick)
424
425
        locations = config.locations_config_filename()
426
        config.ensure_config_dir_exists()
427
        local_url = urlutils.local_path_to_url('branch')
428
        open(locations, 'wb').write('[%s]\nnickname = foobar' 
429
                                    % (local_url,))
430
        self.assertEqual('foobar', branch.nick)
431
432
    def test_config_local_path(self):
433
        """The Branch.get_config will use a local system path"""
434
        branch = self.make_branch('branch')
435
        self.assertEqual('branch', branch.nick)
436
437
        locations = config.locations_config_filename()
438
        config.ensure_config_dir_exists()
439
        open(locations, 'wb').write('[%s/branch]\nnickname = barry' 
440
                                    % (osutils.getcwd().encode('utf8'),))
441
        self.assertEqual('barry', branch.nick)
442
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
443
    def test_config_creates_local(self):
444
        """Creating a new entry in config uses a local path."""
2230.3.6 by Aaron Bentley
work in progress bind stuff
445
        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
446
        branch.set_push_location('http://foobar')
447
        locations = config.locations_config_filename()
448
        local_path = osutils.getcwd().encode('utf8')
449
        # Surprisingly ConfigObj doesn't create a trailing newline
450
        self.check_file_contents(locations,
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
451
                                 '[%s/branch]\n'
452
                                 'push_location = http://foobar\n'
3221.7.1 by Matt Nordhoff
Upgrade ConfigObj to version 4.5.1.
453
                                 'push_location:policy = norecurse\n'
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
454
                                 % (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
455
2120.5.4 by Alexander Belchenko
Whitebox test for Config.get_nickname (req. by Aaron Bentley)
456
    def test_autonick_urlencoded(self):
457
        b = self.make_branch('!repo')
458
        self.assertEqual('!repo', b.get_config().get_nickname())
459
1551.15.35 by Aaron Bentley
Warn when setting config values that will be masked (#122286)
460
    def test_warn_if_masked(self):
461
        _warning = trace.warning
462
        warnings = []
463
        def warning(*args):
464
            warnings.append(args[0] % args[1:])
465
466
        def set_option(store, warn_masked=True):
467
            warnings[:] = []
468
            conf.set_user_option('example_option', repr(store), store=store,
469
                                 warn_masked=warn_masked)
470
        def assertWarning(warning):
471
            if warning is None:
472
                self.assertEqual(0, len(warnings))
473
            else:
474
                self.assertEqual(1, len(warnings))
475
                self.assertEqual(warning, warnings[0])
476
        trace.warning = warning
477
        try:
478
            branch = self.make_branch('.')
479
            conf = branch.get_config()
480
            set_option(config.STORE_GLOBAL)
481
            assertWarning(None)
482
            set_option(config.STORE_BRANCH)
483
            assertWarning(None)
484
            set_option(config.STORE_GLOBAL)
485
            assertWarning('Value "4" is masked by "3" from branch.conf')
486
            set_option(config.STORE_GLOBAL, warn_masked=False)
487
            assertWarning(None)
488
            set_option(config.STORE_LOCATION)
489
            assertWarning(None)
490
            set_option(config.STORE_BRANCH)
491
            assertWarning('Value "3" is masked by "0" from locations.conf')
492
            set_option(config.STORE_BRANCH, warn_masked=False)
493
            assertWarning(None)
494
        finally:
495
            trace.warning = _warning
496
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
497
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
498
class TestGlobalConfigItems(tests.TestCase):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
499
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
500
    def test_user_id(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
501
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
502
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
503
        my_config._parser = my_config._get_parser(file=config_file)
1551.2.21 by Aaron Bentley
Formatted unicode config tests as ASCII
504
        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
505
                         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.
506
507
    def test_absent_user_id(self):
508
        config_file = StringIO("")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
509
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
510
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
511
        self.assertEqual(None, my_config._get_user_id())
512
513
    def test_configured_editor(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
514
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
515
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
516
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
517
        self.assertEqual("vim", my_config.get_editor())
518
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
519
    def test_signatures_always(self):
520
        config_file = StringIO(sample_always_signatures)
521
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
522
        my_config._parser = my_config._get_parser(file=config_file)
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
523
        self.assertEqual(config.CHECK_NEVER,
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
524
                         my_config.signature_checking())
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
525
        self.assertEqual(config.SIGN_ALWAYS,
526
                         my_config.signing_policy())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
527
        self.assertEqual(True, 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
528
529
    def test_signatures_if_possible(self):
530
        config_file = StringIO(sample_maybe_signatures)
531
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
532
        my_config._parser = my_config._get_parser(file=config_file)
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
533
        self.assertEqual(config.CHECK_NEVER,
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
534
                         my_config.signature_checking())
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
535
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
536
                         my_config.signing_policy())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
537
        self.assertEqual(False, 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
538
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
539
    def test_signatures_ignore(self):
540
        config_file = StringIO(sample_ignore_signatures)
541
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
542
        my_config._parser = my_config._get_parser(file=config_file)
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
543
        self.assertEqual(config.CHECK_ALWAYS,
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
544
                         my_config.signature_checking())
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
545
        self.assertEqual(config.SIGN_NEVER,
546
                         my_config.signing_policy())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
547
        self.assertEqual(False, my_config.signature_needed())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
548
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
549
    def _get_sample_config(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
550
        config_file = StringIO(sample_config_text.encode('utf-8'))
1534.7.154 by Aaron Bentley
Removed changes from bzr.ab 1529..1536
551
        my_config = config.GlobalConfig()
552
        my_config._parser = my_config._get_parser(file=config_file)
553
        return my_config
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
554
1442.1.56 by Robert Collins
gpg_signing_command configuration item
555
    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.
556
        my_config = self._get_sample_config()
1442.1.56 by Robert Collins
gpg_signing_command configuration item
557
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
558
        self.assertEqual(False, my_config.signature_needed())
559
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
560
    def _get_empty_config(self):
561
        config_file = StringIO("")
562
        my_config = config.GlobalConfig()
563
        my_config._parser = my_config._get_parser(file=config_file)
564
        return my_config
565
1442.1.59 by Robert Collins
Add re-sign command to generate a digital signature on a single revision.
566
    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.
567
        my_config = self._get_empty_config()
1442.1.59 by Robert Collins
Add re-sign command to generate a digital signature on a single revision.
568
        self.assertEqual("gpg", my_config.gpg_signing_command())
569
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
570
    def test_get_user_option_default(self):
571
        my_config = self._get_empty_config()
572
        self.assertEqual(None, my_config.get_user_option('no_option'))
573
574
    def test_get_user_option_global(self):
575
        my_config = self._get_sample_config()
576
        self.assertEqual("something",
577
                         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.
578
1472 by Robert Collins
post commit hook, first pass implementation
579
    def test_post_commit_default(self):
580
        my_config = self._get_sample_config()
581
        self.assertEqual(None, my_config.post_commit())
582
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
583
    def test_configured_logformat(self):
1553.2.8 by Erik Bågfors
tests for config log_formatter
584
        my_config = self._get_sample_config()
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
585
        self.assertEqual("short", my_config.log_format())
1553.2.8 by Erik Bågfors
tests for config log_formatter
586
1553.6.12 by Erik Bågfors
remove AliasConfig, based on input from abentley
587
    def test_get_alias(self):
588
        my_config = self._get_sample_config()
589
        self.assertEqual('help', my_config.get_alias('h'))
590
591
    def test_get_no_alias(self):
592
        my_config = self._get_sample_config()
593
        self.assertEqual(None, my_config.get_alias('foo'))
594
595
    def test_get_long_alias(self):
596
        my_config = self._get_sample_config()
597
        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.
598
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
599
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
600
class TestLocationConfig(tests.TestCaseInTempDir):
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
601
602
    def test_constructs(self):
603
        my_config = config.LocationConfig('http://example.com')
604
        self.assertRaises(TypeError, config.LocationConfig)
605
606
    def test_branch_calls_read_filenames(self):
1474 by Robert Collins
Merge from Aaron Bentley.
607
        # This is testing the correct file names are provided.
608
        # TODO: consolidate with the test for GlobalConfigs filename checks.
609
        #
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
610
        # replace the class that is constructed, to check its parameters
1474 by Robert Collins
Merge from Aaron Bentley.
611
        oldparserclass = config.ConfigObj
612
        config.ConfigObj = InstrumentedConfigObj
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
613
        try:
1770.2.2 by Aaron Bentley
Rename branches.conf to locations.conf
614
            my_config = config.LocationConfig('http://www.example.com')
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
615
            parser = my_config._get_parser()
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
616
        finally:
1474 by Robert Collins
Merge from Aaron Bentley.
617
            config.ConfigObj = oldparserclass
618
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
619
        self.assertEqual(parser._calls,
1770.2.2 by Aaron Bentley
Rename branches.conf to locations.conf
620
                         [('__init__', config.locations_config_filename(),
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
621
                           'utf-8')])
1711.4.29 by John Arbash Meinel
Alexander Belchenko, fix test_config to use ensure_config_dir, rather than os.mkdir()
622
        config.ensure_config_dir_exists()
623
        #os.mkdir(config.config_dir())
1770.2.2 by Aaron Bentley
Rename branches.conf to locations.conf
624
        f = file(config.branches_config_filename(), 'wb')
625
        f.write('')
626
        f.close()
627
        oldparserclass = config.ConfigObj
628
        config.ConfigObj = InstrumentedConfigObj
629
        try:
630
            my_config = config.LocationConfig('http://www.example.com')
631
            parser = my_config._get_parser()
632
        finally:
633
            config.ConfigObj = oldparserclass
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
634
635
    def test_get_global_config(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
636
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
637
        global_config = my_config._get_global_config()
638
        self.failUnless(isinstance(global_config, config.GlobalConfig))
639
        self.failUnless(global_config is my_config._get_global_config())
640
1993.3.1 by James Henstridge
first go at making location config lookup recursive
641
    def test__get_matching_sections_no_match(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
642
        self.get_branch_config('/')
1993.3.1 by James Henstridge
first go at making location config lookup recursive
643
        self.assertEqual([], self.my_location_config._get_matching_sections())
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
644
1993.3.1 by James Henstridge
first go at making location config lookup recursive
645
    def test__get_matching_sections_exact(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
646
        self.get_branch_config('http://www.example.com')
1993.3.3 by James Henstridge
make _get_matching_sections() return (section, extra_path) tuples, and adjust other code to match
647
        self.assertEqual([('http://www.example.com', '')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
648
                         self.my_location_config._get_matching_sections())
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
649
1993.3.1 by James Henstridge
first go at making location config lookup recursive
650
    def test__get_matching_sections_suffix_does_not(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
651
        self.get_branch_config('http://www.example.com-com')
1993.3.1 by James Henstridge
first go at making location config lookup recursive
652
        self.assertEqual([], self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
653
1993.3.1 by James Henstridge
first go at making location config lookup recursive
654
    def test__get_matching_sections_subdir_recursive(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
655
        self.get_branch_config('http://www.example.com/com')
1993.3.3 by James Henstridge
make _get_matching_sections() return (section, extra_path) tuples, and adjust other code to match
656
        self.assertEqual([('http://www.example.com', 'com')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
657
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
658
1993.3.5 by James Henstridge
add back recurse=False option to config file
659
    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
660
        self.get_branch_config('http://www.example.com/ignoreparent')
661
        self.assertEqual([('http://www.example.com/ignoreparent', '')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
662
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
663
1993.3.5 by James Henstridge
add back recurse=False option to config file
664
    def test__get_matching_sections_ignoreparent_subdir(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
665
        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
666
            'http://www.example.com/ignoreparent/childbranch')
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
667
        self.assertEqual([('http://www.example.com/ignoreparent',
668
                           'childbranch')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
669
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
670
1993.3.1 by James Henstridge
first go at making location config lookup recursive
671
    def test__get_matching_sections_subdir_trailing_slash(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
672
        self.get_branch_config('/b')
1993.3.3 by James Henstridge
make _get_matching_sections() return (section, extra_path) tuples, and adjust other code to match
673
        self.assertEqual([('/b/', '')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
674
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
675
1993.3.1 by James Henstridge
first go at making location config lookup recursive
676
    def test__get_matching_sections_subdir_child(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
677
        self.get_branch_config('/a/foo')
1993.3.3 by James Henstridge
make _get_matching_sections() return (section, extra_path) tuples, and adjust other code to match
678
        self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
679
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
680
1993.3.1 by James Henstridge
first go at making location config lookup recursive
681
    def test__get_matching_sections_subdir_child_child(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
682
        self.get_branch_config('/a/foo/bar')
1993.3.3 by James Henstridge
make _get_matching_sections() return (section, extra_path) tuples, and adjust other code to match
683
        self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
684
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
685
1993.3.1 by James Henstridge
first go at making location config lookup recursive
686
    def test__get_matching_sections_trailing_slash_with_children(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
687
        self.get_branch_config('/a/')
1993.3.3 by James Henstridge
make _get_matching_sections() return (section, extra_path) tuples, and adjust other code to match
688
        self.assertEqual([('/a/', '')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
689
                         self.my_location_config._get_matching_sections())
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
690
1993.3.1 by James Henstridge
first go at making location config lookup recursive
691
    def test__get_matching_sections_explicit_over_glob(self):
692
        # XXX: 2006-09-08 jamesh
693
        # This test only passes because ord('c') > ord('*').  If there
694
        # was a config section for '/a/?', it would get precedence
695
        # over '/a/c'.
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
696
        self.get_branch_config('/a/c')
1993.3.3 by James Henstridge
make _get_matching_sections() return (section, extra_path) tuples, and adjust other code to match
697
        self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
698
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
699
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
700
    def test__get_option_policy_normal(self):
701
        self.get_branch_config('http://www.example.com')
702
        self.assertEqual(
703
            self.my_location_config._get_config_policy(
704
            'http://www.example.com', 'normal_option'),
705
            config.POLICY_NONE)
706
707
    def test__get_option_policy_norecurse(self):
708
        self.get_branch_config('http://www.example.com')
709
        self.assertEqual(
710
            self.my_location_config._get_option_policy(
711
            'http://www.example.com', 'norecurse_option'),
712
            config.POLICY_NORECURSE)
713
        # Test old recurse=False setting:
714
        self.assertEqual(
715
            self.my_location_config._get_option_policy(
716
            'http://www.example.com/norecurse', 'normal_option'),
717
            config.POLICY_NORECURSE)
718
719
    def test__get_option_policy_normal(self):
720
        self.get_branch_config('http://www.example.com')
721
        self.assertEqual(
722
            self.my_location_config._get_option_policy(
723
            'http://www.example.com', 'appendpath_option'),
724
            config.POLICY_APPENDPATH)
725
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
726
    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
727
        self.get_branch_config('http://www.example.com/ignoreparent')
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
728
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
729
                         self.my_config.username())
730
731
    def test_location_not_listed(self):
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
732
        """Test that the global username is used when no location matches"""
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
733
        self.get_branch_config('/home/robertc/sources')
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
734
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
735
                         self.my_config.username())
736
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
737
    def test_overriding_location(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
738
        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
739
        self.assertEqual('Robert Collins <robertc@example.org>',
740
                         self.my_config.username())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
741
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
742
    def test_signatures_not_set(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
743
        self.get_branch_config('http://www.example.com',
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
744
                                 global_config=sample_ignore_signatures)
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
745
        self.assertEqual(config.CHECK_ALWAYS,
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
746
                         self.my_config.signature_checking())
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
747
        self.assertEqual(config.SIGN_NEVER,
748
                         self.my_config.signing_policy())
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
749
750
    def test_signatures_never(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
751
        self.get_branch_config('/a/c')
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
752
        self.assertEqual(config.CHECK_NEVER,
753
                         self.my_config.signature_checking())
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
754
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
755
    def test_signatures_when_available(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
756
        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
757
        self.assertEqual(config.CHECK_IF_POSSIBLE,
758
                         self.my_config.signature_checking())
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
759
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
760
    def test_signatures_always(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
761
        self.get_branch_config('/b')
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
762
        self.assertEqual(config.CHECK_ALWAYS,
763
                         self.my_config.signature_checking())
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
764
1442.1.56 by Robert Collins
gpg_signing_command configuration item
765
    def test_gpg_signing_command(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
766
        self.get_branch_config('/b')
1442.1.56 by Robert Collins
gpg_signing_command configuration item
767
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
768
769
    def test_gpg_signing_command_missing(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
770
        self.get_branch_config('/a')
1442.1.56 by Robert Collins
gpg_signing_command configuration item
771
        self.assertEqual("false", self.my_config.gpg_signing_command())
772
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
773
    def test_get_user_option_global(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
774
        self.get_branch_config('/a')
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
775
        self.assertEqual('something',
776
                         self.my_config.get_user_option('user_global_option'))
777
778
    def test_get_user_option_local(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
779
        self.get_branch_config('/a')
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
780
        self.assertEqual('local',
781
                         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
782
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
783
    def test_get_user_option_appendpath(self):
784
        # returned as is for the base path:
785
        self.get_branch_config('http://www.example.com')
786
        self.assertEqual('append',
787
                         self.my_config.get_user_option('appendpath_option'))
788
        # Extra path components get appended:
789
        self.get_branch_config('http://www.example.com/a/b/c')
790
        self.assertEqual('append/a/b/c',
791
                         self.my_config.get_user_option('appendpath_option'))
792
        # Overriden for http://www.example.com/dir, where it is a
793
        # normal option:
794
        self.get_branch_config('http://www.example.com/dir/a/b/c')
795
        self.assertEqual('normal',
796
                         self.my_config.get_user_option('appendpath_option'))
797
798
    def test_get_user_option_norecurse(self):
799
        self.get_branch_config('http://www.example.com')
800
        self.assertEqual('norecurse',
801
                         self.my_config.get_user_option('norecurse_option'))
802
        self.get_branch_config('http://www.example.com/dir')
803
        self.assertEqual(None,
804
                         self.my_config.get_user_option('norecurse_option'))
805
        # http://www.example.com/norecurse is a recurse=False section
806
        # that redefines normal_option.  Subdirectories do not pick up
807
        # this redefinition.
808
        self.get_branch_config('http://www.example.com/norecurse')
809
        self.assertEqual('norecurse',
810
                         self.my_config.get_user_option('normal_option'))
811
        self.get_branch_config('http://www.example.com/norecurse/subdir')
812
        self.assertEqual('normal',
813
                         self.my_config.get_user_option('normal_option'))
814
2120.6.4 by James Henstridge
add support for specifying policy when storing options
815
    def test_set_user_option_norecurse(self):
816
        self.get_branch_config('http://www.example.com')
817
        self.my_config.set_user_option('foo', 'bar',
818
                                       store=config.STORE_LOCATION_NORECURSE)
819
        self.assertEqual(
820
            self.my_location_config._get_option_policy(
821
            'http://www.example.com', 'foo'),
822
            config.POLICY_NORECURSE)
823
824
    def test_set_user_option_appendpath(self):
825
        self.get_branch_config('http://www.example.com')
826
        self.my_config.set_user_option('foo', 'bar',
827
                                       store=config.STORE_LOCATION_APPENDPATH)
828
        self.assertEqual(
829
            self.my_location_config._get_option_policy(
830
            'http://www.example.com', 'foo'),
831
            config.POLICY_APPENDPATH)
832
833
    def test_set_user_option_change_policy(self):
834
        self.get_branch_config('http://www.example.com')
835
        self.my_config.set_user_option('norecurse_option', 'normal',
836
                                       store=config.STORE_LOCATION)
837
        self.assertEqual(
838
            self.my_location_config._get_option_policy(
839
            'http://www.example.com', 'norecurse_option'),
840
            config.POLICY_NONE)
841
842
    def test_set_user_option_recurse_false_section(self):
2120.6.9 by James Henstridge
Fixes for issues brought up in John's review
843
        # The following section has recurse=False set.  The test is to
844
        # make sure that a normal option can be added to the section,
845
        # converting recurse=False to the norecurse policy.
2120.6.4 by James Henstridge
add support for specifying policy when storing options
846
        self.get_branch_config('http://www.example.com/norecurse')
2120.6.11 by James Henstridge
s/0.13/0.14/ in deprecation warning
847
        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
848
                             'The section "http://www.example.com/norecurse" '
849
                             'has been converted to use policies.'],
850
                            self.my_config.set_user_option,
851
                            'foo', 'bar', store=config.STORE_LOCATION)
2120.6.4 by James Henstridge
add support for specifying policy when storing options
852
        self.assertEqual(
853
            self.my_location_config._get_option_policy(
854
            'http://www.example.com/norecurse', 'foo'),
855
            config.POLICY_NONE)
856
        # The previously existing option is still norecurse:
857
        self.assertEqual(
858
            self.my_location_config._get_option_policy(
859
            'http://www.example.com/norecurse', 'normal_option'),
860
            config.POLICY_NORECURSE)
861
1472 by Robert Collins
post commit hook, first pass implementation
862
    def test_post_commit_default(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
863
        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
864
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1472 by Robert Collins
post commit hook, first pass implementation
865
                         self.my_config.post_commit())
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
866
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
867
    def get_branch_config(self, location, global_config=None):
1502 by Robert Collins
Bugfix the config test suite to not create .bazaar in the dir where it is run.
868
        if global_config is None:
1551.2.20 by Aaron Bentley
Treated config files as utf-8
869
            global_file = StringIO(sample_config_text.encode('utf-8'))
1502 by Robert Collins
Bugfix the config test suite to not create .bazaar in the dir where it is run.
870
        else:
1551.2.20 by Aaron Bentley
Treated config files as utf-8
871
            global_file = StringIO(global_config.encode('utf-8'))
872
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
873
        self.my_config = config.BranchConfig(FakeBranch(location))
874
        # Force location config to use specified file
875
        self.my_location_config = self.my_config._get_location_config()
876
        self.my_location_config._get_parser(branches_file)
877
        # Force global config to use specified file
1502 by Robert Collins
Bugfix the config test suite to not create .bazaar in the dir where it is run.
878
        self.my_config._get_global_config()._get_parser(global_file)
879
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
880
    def test_set_user_setting_sets_and_saves(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
881
        self.get_branch_config('/a/c')
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
882
        record = InstrumentedConfigObj("foo")
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
883
        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
884
885
        real_mkdir = os.mkdir
886
        self.created = False
887
        def checked_mkdir(path, mode=0777):
888
            self.log('making directory: %s', path)
889
            real_mkdir(path, mode)
890
            self.created = True
891
892
        os.mkdir = checked_mkdir
893
        try:
2120.6.10 by James Henstridge
Catch another deprecation warning, and more cleanup
894
            self.callDeprecated(['The recurse option is deprecated as of '
2120.6.11 by James Henstridge
s/0.13/0.14/ in deprecation warning
895
                                 '0.14.  The section "/a/c" has been '
2120.6.10 by James Henstridge
Catch another deprecation warning, and more cleanup
896
                                 'converted to use policies.'],
897
                                self.my_config.set_user_option,
898
                                'foo', 'bar', store=config.STORE_LOCATION)
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
899
        finally:
900
            os.mkdir = real_mkdir
901
902
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
903
        self.assertEqual([('__contains__', '/a/c'),
904
                          ('__contains__', '/a/c/'),
905
                          ('__setitem__', '/a/c', {}),
906
                          ('__getitem__', '/a/c'),
907
                          ('__setitem__', 'foo', 'bar'),
2120.6.4 by James Henstridge
add support for specifying policy when storing options
908
                          ('__getitem__', '/a/c'),
909
                          ('as_bool', 'recurse'),
910
                          ('__getitem__', '/a/c'),
911
                          ('__delitem__', 'recurse'),
912
                          ('__getitem__', '/a/c'),
913
                          ('keys',),
2120.6.8 by James Henstridge
Change syntax for setting config option policies. Rather than
914
                          ('__getitem__', '/a/c'),
915
                          ('__contains__', 'foo:policy'),
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
916
                          ('write',)],
917
                         record._calls[1:])
918
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
919
    def test_set_user_setting_sets_and_saves2(self):
920
        self.get_branch_config('/a/c')
921
        self.assertIs(self.my_config.get_user_option('foo'), None)
922
        self.my_config.set_user_option('foo', 'bar')
923
        self.assertEqual(
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
924
            self.my_config.branch.control_files.files['branch.conf'],
3221.7.1 by Matt Nordhoff
Upgrade ConfigObj to version 4.5.1.
925
            'foo = bar\n')
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
926
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
2120.6.4 by James Henstridge
add support for specifying policy when storing options
927
        self.my_config.set_user_option('foo', 'baz',
928
                                       store=config.STORE_LOCATION)
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
929
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
930
        self.my_config.set_user_option('foo', 'qux')
931
        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.
932
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
933
    def test_get_bzr_remote_path(self):
934
        my_config = config.LocationConfig('/a/c')
935
        self.assertEqual('bzr', my_config.get_bzr_remote_path())
936
        my_config.set_user_option('bzr_remote_path', '/path-bzr')
937
        self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
938
        os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
939
        self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
940
1185.62.7 by John Arbash Meinel
Whitespace cleanup.
941
1770.2.8 by Aaron Bentley
Add precedence test
942
precedence_global = 'option = global'
943
precedence_branch = 'option = branch'
944
precedence_location = """
945
[http://]
946
recurse = true
947
option = recurse
948
[http://example.com/specific]
949
option = exact
950
"""
951
952
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
953
class TestBranchConfigItems(tests.TestCaseInTempDir):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
954
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
955
    def get_branch_config(self, global_config=None, location=None,
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
956
                          location_config=None, branch_data_config=None):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
957
        my_config = config.BranchConfig(FakeBranch(location))
958
        if global_config is not None:
959
            global_file = StringIO(global_config.encode('utf-8'))
960
            my_config._get_global_config()._get_parser(global_file)
961
        self.my_location_config = my_config._get_location_config()
962
        if location_config is not None:
963
            location_file = StringIO(location_config.encode('utf-8'))
964
            self.my_location_config._get_parser(location_file)
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
965
        if branch_data_config is not None:
966
            my_config.branch.control_files.files['branch.conf'] = \
967
                branch_data_config
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
968
        return my_config
969
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
970
    def test_user_id(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
971
        branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
972
        my_config = config.BranchConfig(branch)
973
        self.assertEqual("Robert Collins <robertc@example.net>",
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
974
                         my_config.username())
3388.2.3 by Martin Pool
Fix up more uses of LockableFiles.get_utf8 in tests
975
        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.
976
        my_config.set_user_option('email',
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
977
                                  "Robert Collins <robertc@example.org>")
978
        self.assertEqual("John", my_config.username())
3388.2.3 by Martin Pool
Fix up more uses of LockableFiles.get_utf8 in tests
979
        del my_config.branch.control_files.files['email']
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
980
        self.assertEqual("Robert Collins <robertc@example.org>",
981
                         my_config.username())
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
982
983
    def test_not_set_in_branch(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
984
        my_config = self.get_branch_config(sample_config_text)
1551.2.21 by Aaron Bentley
Formatted unicode config tests as ASCII
985
        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
986
                         my_config._get_user_id())
3388.2.3 by Martin Pool
Fix up more uses of LockableFiles.get_utf8 in tests
987
        my_config.branch.control_files.files['email'] = "John"
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
988
        self.assertEqual("John", my_config._get_user_id())
989
1861.4.1 by Matthieu Moy
BZREMAIL renamed to BZR_EMAIL.
990
    def test_BZR_EMAIL_OVERRIDES(self):
991
        os.environ['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
992
        branch = FakeBranch()
993
        my_config = config.BranchConfig(branch)
994
        self.assertEqual("Robert Collins <robertc@example.org>",
995
                         my_config.username())
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
996
1442.1.19 by Robert Collins
BranchConfigs inherit signature_checking policy from their LocationConfig.
997
    def test_signatures_forced(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
998
        my_config = self.get_branch_config(
999
            global_config=sample_always_signatures)
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
1000
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1001
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1002
        self.assertTrue(my_config.signature_needed())
1442.1.56 by Robert Collins
gpg_signing_command configuration item
1003
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
1004
    def test_signatures_forced_branch(self):
1005
        my_config = self.get_branch_config(
1006
            global_config=sample_ignore_signatures,
1007
            branch_data_config=sample_always_signatures)
1008
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1009
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1010
        self.assertTrue(my_config.signature_needed())
1011
1442.1.56 by Robert Collins
gpg_signing_command configuration item
1012
    def test_gpg_signing_command(self):
1770.2.10 by Aaron Bentley
Added test that branch_config can't influence gpg_signing_command
1013
        my_config = self.get_branch_config(
1014
            # branch data cannot set gpg_signing_command
1015
            branch_data_config="gpg_signing_command=pgp")
1551.2.20 by Aaron Bentley
Treated config files as utf-8
1016
        config_file = StringIO(sample_config_text.encode('utf-8'))
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
1017
        my_config._get_global_config()._get_parser(config_file)
1442.1.56 by Robert Collins
gpg_signing_command configuration item
1018
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
1019
1020
    def test_get_user_option_global(self):
1021
        branch = FakeBranch()
1022
        my_config = config.BranchConfig(branch)
1551.2.20 by Aaron Bentley
Treated config files as utf-8
1023
        config_file = StringIO(sample_config_text.encode('utf-8'))
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
1024
        (my_config._get_global_config()._get_parser(config_file))
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
1025
        self.assertEqual('something',
1026
                         my_config.get_user_option('user_global_option'))
1472 by Robert Collins
post commit hook, first pass implementation
1027
1028
    def test_post_commit_default(self):
1029
        branch = FakeBranch()
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
1030
        my_config = self.get_branch_config(sample_config_text, '/a/c',
1031
                                           sample_branches_text)
1032
        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
1033
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1472 by Robert Collins
post commit hook, first pass implementation
1034
                         my_config.post_commit())
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
1035
        my_config.set_user_option('post_commit', 'rmtree_root')
1036
        # post-commit is ignored when bresent in branch data
1037
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1038
                         my_config.post_commit())
2120.6.4 by James Henstridge
add support for specifying policy when storing options
1039
        my_config.set_user_option('post_commit', 'rmtree_root',
1040
                                  store=config.STORE_LOCATION)
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
1041
        self.assertEqual('rmtree_root', my_config.post_commit())
1185.33.31 by Martin Pool
Make annotate cope better with revisions committed without a valid
1042
1770.2.8 by Aaron Bentley
Add precedence test
1043
    def test_config_precedence(self):
1044
        my_config = self.get_branch_config(global_config=precedence_global)
1045
        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.
1046
        my_config = self.get_branch_config(global_config=precedence_global,
1770.2.8 by Aaron Bentley
Add precedence test
1047
                                      branch_data_config=precedence_branch)
1048
        self.assertEqual(my_config.get_user_option('option'), 'branch')
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
1049
        my_config = self.get_branch_config(global_config=precedence_global,
1770.2.8 by Aaron Bentley
Add precedence test
1050
                                      branch_data_config=precedence_branch,
1051
                                      location_config=precedence_location)
1052
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
2991.2.2 by Vincent Ladeuil
No tests worth adding after upgrading to configobj-4.4.0.
1053
        my_config = self.get_branch_config(global_config=precedence_global,
1770.2.8 by Aaron Bentley
Add precedence test
1054
                                      branch_data_config=precedence_branch,
1055
                                      location_config=precedence_location,
1056
                                      location='http://example.com/specific')
1057
        self.assertEqual(my_config.get_user_option('option'), 'exact')
1058
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
1059
    def test_get_mail_client(self):
1060
        config = self.get_branch_config()
1061
        client = config.get_mail_client()
2681.1.24 by Aaron Bentley
Handle default mail client by trying xdg-email, falling back to editor
1062
        self.assertIsInstance(client, mail_client.DefaultMail)
1063
2790.2.2 by Keir Mierle
Change alphabetic ordering into two categories; one for specific clients the other for generic options.
1064
        # Specific clients
2681.1.21 by Aaron Bentley
Refactor prompt generation to make it testable, test it with unicode
1065
        config.set_user_option('mail_client', 'evolution')
1066
        client = config.get_mail_client()
1067
        self.assertIsInstance(client, mail_client.Evolution)
1068
2681.5.1 by ghigo
Add KMail support to bzr send
1069
        config.set_user_option('mail_client', 'kmail')
1070
        client = config.get_mail_client()
1071
        self.assertIsInstance(client, mail_client.KMail)
1072
2790.2.1 by Keir Mierle
Add Mutt as a supported client email program. Also rearranges various listings
1073
        config.set_user_option('mail_client', 'mutt')
1074
        client = config.get_mail_client()
1075
        self.assertIsInstance(client, mail_client.Mutt)
1076
1077
        config.set_user_option('mail_client', 'thunderbird')
1078
        client = config.get_mail_client()
1079
        self.assertIsInstance(client, mail_client.Thunderbird)
1080
2790.2.2 by Keir Mierle
Change alphabetic ordering into two categories; one for specific clients the other for generic options.
1081
        # Generic options
1082
        config.set_user_option('mail_client', 'default')
1083
        client = config.get_mail_client()
1084
        self.assertIsInstance(client, mail_client.DefaultMail)
1085
1086
        config.set_user_option('mail_client', 'editor')
1087
        client = config.get_mail_client()
1088
        self.assertIsInstance(client, mail_client.Editor)
1089
1090
        config.set_user_option('mail_client', 'mapi')
1091
        client = config.get_mail_client()
1092
        self.assertIsInstance(client, mail_client.MAPIClient)
1093
2681.1.23 by Aaron Bentley
Add support for xdg-email
1094
        config.set_user_option('mail_client', 'xdg-email')
1095
        client = config.get_mail_client()
1096
        self.assertIsInstance(client, mail_client.XDGEmail)
1097
2681.1.10 by Aaron Bentley
Clean up handling of unknown mail clients
1098
        config.set_user_option('mail_client', 'firebird')
1099
        self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1100
1185.33.31 by Martin Pool
Make annotate cope better with revisions committed without a valid
1101
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
1102
class TestMailAddressExtraction(tests.TestCase):
1185.33.31 by Martin Pool
Make annotate cope better with revisions committed without a valid
1103
1104
    def test_extract_email_address(self):
1105
        self.assertEqual('jane@test.com',
1106
                         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
1107
        self.assertRaises(errors.NoEmailInUsername,
1185.33.31 by Martin Pool
Make annotate cope better with revisions committed without a valid
1108
                          config.extract_email_address, 'Jane Tester')
2533.1.1 by James Westby
Fix TreeConfig to return values from sections.
1109
3063.3.2 by Lukáš Lalinský
Move the name and e-mail address extraction logic to config.parse_username.
1110
    def test_parse_username(self):
1111
        self.assertEqual(('', 'jdoe@example.com'),
1112
                         config.parse_username('jdoe@example.com'))
1113
        self.assertEqual(('', 'jdoe@example.com'),
1114
                         config.parse_username('<jdoe@example.com>'))
1115
        self.assertEqual(('John Doe', 'jdoe@example.com'),
1116
                         config.parse_username('John Doe <jdoe@example.com>'))
1117
        self.assertEqual(('John Doe', ''),
1118
                         config.parse_username('John Doe'))
3063.3.3 by Lukáš Lalinský
Add one more test for config.parse_username().
1119
        self.assertEqual(('John Doe', 'jdoe@example.com'),
1120
                         config.parse_username('John Doe jdoe@example.com'))
2562.1.2 by John Arbash Meinel
Clean up whitespace
1121
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
1122
class TestTreeConfig(tests.TestCaseWithTransport):
2533.1.1 by James Westby
Fix TreeConfig to return values from sections.
1123
1124
    def test_get_value(self):
1125
        """Test that retreiving a value from a section is possible"""
1126
        branch = self.make_branch('.')
1127
        tree_config = config.TreeConfig(branch)
1128
        tree_config.set_option('value', 'key', 'SECTION')
1129
        tree_config.set_option('value2', 'key2')
1130
        tree_config.set_option('value3-top', 'key3')
1131
        tree_config.set_option('value3-section', 'key3', 'SECTION')
1132
        value = tree_config.get_option('key', 'SECTION')
1133
        self.assertEqual(value, 'value')
1134
        value = tree_config.get_option('key2')
1135
        self.assertEqual(value, 'value2')
1136
        self.assertEqual(tree_config.get_option('non-existant'), None)
1137
        value = tree_config.get_option('non-existant', 'SECTION')
1138
        self.assertEqual(value, None)
1139
        value = tree_config.get_option('non-existant', default='default')
1140
        self.assertEqual(value, 'default')
1141
        self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1142
        value = tree_config.get_option('key2', 'NOSECTION', default='default')
1143
        self.assertEqual(value, 'default')
1144
        value = tree_config.get_option('key3')
1145
        self.assertEqual(value, 'value3-top')
1146
        value = tree_config.get_option('key3', 'SECTION')
1147
        self.assertEqual(value, 'value3-section')
2900.2.3 by Vincent Ladeuil
Credentials matching implementation.
1148
1149
3242.1.2 by Aaron Bentley
Turn BzrDirConfig into TransportConfig, reduce code duplication
1150
class TestTransportConfig(tests.TestCaseWithTransport):
3242.1.1 by Aaron Bentley
Implement BzrDir configuration
1151
1152
    def test_get_value(self):
1153
        """Test that retreiving a value from a section is possible"""
3242.1.2 by Aaron Bentley
Turn BzrDirConfig into TransportConfig, reduce code duplication
1154
        bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1155
                                               'control.conf')
3242.1.1 by Aaron Bentley
Implement BzrDir configuration
1156
        bzrdir_config.set_option('value', 'key', 'SECTION')
1157
        bzrdir_config.set_option('value2', 'key2')
1158
        bzrdir_config.set_option('value3-top', 'key3')
1159
        bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1160
        value = bzrdir_config.get_option('key', 'SECTION')
1161
        self.assertEqual(value, 'value')
1162
        value = bzrdir_config.get_option('key2')
1163
        self.assertEqual(value, 'value2')
1164
        self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1165
        value = bzrdir_config.get_option('non-existant', 'SECTION')
1166
        self.assertEqual(value, None)
1167
        value = bzrdir_config.get_option('non-existant', default='default')
1168
        self.assertEqual(value, 'default')
1169
        self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1170
        value = bzrdir_config.get_option('key2', 'NOSECTION',
1171
                                         default='default')
1172
        self.assertEqual(value, 'default')
1173
        value = bzrdir_config.get_option('key3')
1174
        self.assertEqual(value, 'value3-top')
1175
        value = bzrdir_config.get_option('key3', 'SECTION')
1176
        self.assertEqual(value, 'value3-section')
1177
1178
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
1179
class TestAuthenticationConfigFile(tests.TestCase):
2900.2.14 by Vincent Ladeuil
More tests.
1180
    """Test the authentication.conf file matching"""
2900.2.3 by Vincent Ladeuil
Credentials matching implementation.
1181
1182
    def _got_user_passwd(self, expected_user, expected_password,
1183
                         config, *args, **kwargs):
1184
        credentials = config.get_credentials(*args, **kwargs)
1185
        if credentials is None:
1186
            user = None
1187
            password = None
1188
        else:
1189
            user = credentials['user']
1190
            password = credentials['password']
1191
        self.assertEquals(expected_user, user)
1192
        self.assertEquals(expected_password, password)
1193
2978.5.1 by John Arbash Meinel
Fix bug #162494, 'bzr register-branch' needs proper auth handling.
1194
    def test_empty_config(self):
2900.2.3 by Vincent Ladeuil
Credentials matching implementation.
1195
        conf = config.AuthenticationConfig(_file=StringIO())
1196
        self.assertEquals({}, conf._get_config())
1197
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1198
3418.2.1 by Vincent Ladeuil
Fix #217650 by catching declarations outside sections.
1199
    def test_missing_auth_section_header(self):
1200
        conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1201
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1202
1203
    def test_auth_section_header_not_closed(self):
2900.2.3 by Vincent Ladeuil
Credentials matching implementation.
1204
        conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1205
        self.assertRaises(errors.ParseConfigError, conf._get_config)
2900.2.15 by Vincent Ladeuil
AuthenticationConfig can be queried for logins too (first step).
1206
3418.2.1 by Vincent Ladeuil
Fix #217650 by catching declarations outside sections.
1207
    def test_auth_value_not_boolean(self):
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
1208
        conf = config.AuthenticationConfig(_file=StringIO(
1209
                """[broken]
1210
scheme=ftp
1211
user=joe
2900.2.15 by Vincent Ladeuil
AuthenticationConfig can be queried for logins too (first step).
1212
verify_certificates=askme # Error: Not a boolean
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
1213
"""))
1214
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
3418.2.1 by Vincent Ladeuil
Fix #217650 by catching declarations outside sections.
1215
1216
    def test_auth_value_not_int(self):
2900.2.22 by Vincent Ladeuil
Polishing.
1217
        conf = config.AuthenticationConfig(_file=StringIO(
1218
                """[broken]
1219
scheme=ftp
1220
user=joe
1221
port=port # Error: Not an int
1222
"""))
1223
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
2900.2.3 by Vincent Ladeuil
Credentials matching implementation.
1224
1225
    def test_credentials_for_scheme_host(self):
1226
        conf = config.AuthenticationConfig(_file=StringIO(
1227
                """# Identity on foo.net
1228
[ftp definition]
1229
scheme=ftp
1230
host=foo.net
1231
user=joe
1232
password=secret-pass
1233
"""))
1234
        # Basic matching
1235
        self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1236
        # different scheme
1237
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1238
        # different host
1239
        self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1240
1241
    def test_credentials_for_host_port(self):
1242
        conf = config.AuthenticationConfig(_file=StringIO(
1243
                """# Identity on foo.net
1244
[ftp definition]
1245
scheme=ftp
1246
port=10021
1247
host=foo.net
1248
user=joe
1249
password=secret-pass
1250
"""))
1251
        # No port
1252
        self._got_user_passwd('joe', 'secret-pass',
1253
                              conf, 'ftp', 'foo.net', port=10021)
1254
        # different port
1255
        self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1256
1257
    def test_for_matching_host(self):
1258
        conf = config.AuthenticationConfig(_file=StringIO(
1259
                """# Identity on foo.net
1260
[sourceforge]
1261
scheme=bzr
1262
host=bzr.sf.net
1263
user=joe
1264
password=joepass
1265
[sourceforge domain]
1266
scheme=bzr
1267
host=.bzr.sf.net
1268
user=georges
1269
password=bendover
1270
"""))
1271
        # matching domain
1272
        self._got_user_passwd('georges', 'bendover',
1273
                              conf, 'bzr', 'foo.bzr.sf.net')
1274
        # phishing attempt
1275
        self._got_user_passwd(None, None,
1276
                              conf, 'bzr', 'bbzr.sf.net')
1277
1278
    def test_for_matching_host_None(self):
1279
        conf = config.AuthenticationConfig(_file=StringIO(
1280
                """# Identity on foo.net
1281
[catchup bzr]
1282
scheme=bzr
1283
user=joe
1284
password=joepass
1285
[DEFAULT]
1286
user=georges
1287
password=bendover
1288
"""))
1289
        # match no host
1290
        self._got_user_passwd('joe', 'joepass',
1291
                              conf, 'bzr', 'quux.net')
1292
        # no host but different scheme
1293
        self._got_user_passwd('georges', 'bendover',
1294
                              conf, 'ftp', 'quux.net')
1295
1296
    def test_credentials_for_path(self):
1297
        conf = config.AuthenticationConfig(_file=StringIO(
1298
                """
1299
[http dir1]
1300
scheme=http
1301
host=bar.org
1302
path=/dir1
1303
user=jim
1304
password=jimpass
1305
[http dir2]
1306
scheme=http
1307
host=bar.org
1308
path=/dir2
1309
user=georges
1310
password=bendover
1311
"""))
1312
        # no path no dice
1313
        self._got_user_passwd(None, None,
1314
                              conf, 'http', host='bar.org', path='/dir3')
1315
        # matching path
1316
        self._got_user_passwd('georges', 'bendover',
1317
                              conf, 'http', host='bar.org', path='/dir2')
1318
        # matching subdir
1319
        self._got_user_passwd('jim', 'jimpass',
1320
                              conf, 'http', host='bar.org',path='/dir1/subdir')
1321
1322
    def test_credentials_for_user(self):
1323
        conf = config.AuthenticationConfig(_file=StringIO(
1324
                """
1325
[with user]
1326
scheme=http
1327
host=bar.org
1328
user=jim
1329
password=jimpass
1330
"""))
1331
        # Get user
1332
        self._got_user_passwd('jim', 'jimpass',
1333
                              conf, 'http', 'bar.org')
1334
        # Get same user
1335
        self._got_user_passwd('jim', 'jimpass',
1336
                              conf, 'http', 'bar.org', user='jim')
1337
        # Don't get a different user if one is specified
1338
        self._got_user_passwd(None, None,
1339
                              conf, 'http', 'bar.org', user='georges')
1340
3418.4.1 by Vincent Ladeuil
Reproduce bug 199440.
1341
    def test_credentials_for_user_without_password(self):
1342
        conf = config.AuthenticationConfig(_file=StringIO(
1343
                """
1344
[without password]
1345
scheme=http
1346
host=bar.org
1347
user=jim
1348
"""))
1349
        # Get user but no password
1350
        self._got_user_passwd('jim', None,
1351
                              conf, 'http', 'bar.org')
1352
2900.2.3 by Vincent Ladeuil
Credentials matching implementation.
1353
    def test_verify_certificates(self):
1354
        conf = config.AuthenticationConfig(_file=StringIO(
1355
                """
1356
[self-signed]
1357
scheme=https
1358
host=bar.org
1359
user=jim
1360
password=jimpass
1361
verify_certificates=False
1362
[normal]
1363
scheme=https
1364
host=foo.net
1365
user=georges
1366
password=bendover
1367
"""))
1368
        credentials = conf.get_credentials('https', 'bar.org')
1369
        self.assertEquals(False, credentials.get('verify_certificates'))
1370
        credentials = conf.get_credentials('https', 'foo.net')
1371
        self.assertEquals(True, credentials.get('verify_certificates'))
2900.2.4 by Vincent Ladeuil
Cosmetic changes.
1372
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
1373
2900.2.14 by Vincent Ladeuil
More tests.
1374
class TestAuthenticationConfig(tests.TestCase):
1375
    """Test AuthenticationConfig behaviour"""
1376
1377
    def _check_default_prompt(self, expected_prompt_format, scheme,
1378
                              host=None, port=None, realm=None, path=None):
1379
        if host is None:
1380
            host = 'bar.org'
1381
        user, password = 'jim', 'precious'
1382
        expected_prompt = expected_prompt_format % {
1383
            'scheme': scheme, 'host': host, 'port': port,
1384
            'user': user, 'realm': realm}
1385
1386
        stdout = tests.StringIOWrapper()
1387
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1388
                                            stdout=stdout)
1389
        # We use an empty conf so that the user is always prompted
1390
        conf = config.AuthenticationConfig()
1391
        self.assertEquals(password,
1392
                          conf.get_password(scheme, host, user, port=port,
1393
                                            realm=realm, path=path))
1394
        self.assertEquals(stdout.getvalue(), expected_prompt)
1395
1396
    def test_default_prompts(self):
2900.2.19 by Vincent Ladeuil
Mention proxy and https in the password prompts, with tests.
1397
        # HTTP prompts can't be tested here, see test_http.py
2900.2.14 by Vincent Ladeuil
More tests.
1398
        self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1399
        self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1400
                                   'ftp', port=10020)
1401
1402
        self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1403
                                   'ssh', port=12345)
1404
        # SMTP port handling is a bit special (it's handled if embedded in the
1405
        # host too)
2900.2.22 by Vincent Ladeuil
Polishing.
1406
        # FIXME: should we: forbid that, extend it to other schemes, leave
1407
        # things as they are that's fine thank you ?
2900.2.14 by Vincent Ladeuil
More tests.
1408
        self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1409
                                   'smtp')
1410
        self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1411
                                   'smtp', host='bar.org:10025')
1412
        self._check_default_prompt(
1413
            'SMTP %(user)s@%(host)s:%(port)d password: ',
1414
            'smtp', port=10025)
1415
3420.1.2 by Vincent Ladeuil
Fix bug #203186 by ignoring passwords for ssh and warning user.
1416
    def test_ssh_password_emits_warning(self):
1417
        conf = config.AuthenticationConfig(_file=StringIO(
1418
                """
1419
[ssh with password]
1420
scheme=ssh
1421
host=bar.org
1422
user=jim
1423
password=jimpass
1424
"""))
1425
        entered_password = 'typed-by-hand'
1426
        stdout = tests.StringIOWrapper()
1427
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1428
                                            stdout=stdout)
1429
1430
        # Since the password defined in the authentication config is ignored,
1431
        # the user is prompted
1432
        self.assertEquals(entered_password,
1433
                          conf.get_password('ssh', 'bar.org', user='jim'))
1434
        self.assertContainsRe(
1435
            self._get_log(keep_log_file=True),
1436
            'password ignored in section \[ssh with password\]')
1437
3420.1.3 by Vincent Ladeuil
John's review feedback.
1438
    def test_ssh_without_password_doesnt_emit_warning(self):
1439
        conf = config.AuthenticationConfig(_file=StringIO(
1440
                """
1441
[ssh with password]
1442
scheme=ssh
1443
host=bar.org
1444
user=jim
1445
"""))
1446
        entered_password = 'typed-by-hand'
1447
        stdout = tests.StringIOWrapper()
1448
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1449
                                            stdout=stdout)
1450
1451
        # Since the password defined in the authentication config is ignored,
1452
        # the user is prompted
1453
        self.assertEquals(entered_password,
1454
                          conf.get_password('ssh', 'bar.org', user='jim'))
3420.1.4 by Vincent Ladeuil
Fix comment.
1455
        # No warning shoud be emitted since there is no password. We are only
1456
        # providing "user".
3420.1.3 by Vincent Ladeuil
John's review feedback.
1457
        self.assertNotContainsRe(
1458
            self._get_log(keep_log_file=True),
1459
            'password ignored in section \[ssh with password\]')
1460
2900.2.14 by Vincent Ladeuil
More tests.
1461
1462
# 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.
1463
# can implement generic tests.
2900.2.15 by Vincent Ladeuil
AuthenticationConfig can be queried for logins too (first step).
1464
# test_user_password_in_url
1465
# test_user_in_url_password_from_config
1466
# test_user_in_url_password_prompted
1467
# test_user_in_config
1468
# test_user_getpass.getuser
1469
# test_user_prompted ?
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
1470
class TestAuthenticationRing(tests.TestCaseWithTransport):
1471
    pass