~bzr-pqm/bzr/bzr.dev

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
1474 by Robert Collins
Merge from Aaron Bentley.
20
from bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
21
from cStringIO import StringIO
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
22
import os
23
import sys
24
25
#import bzrlib specific imports here
1878.1.3 by John Arbash Meinel
some test cleanups
26
from bzrlib import (
27
    config,
28
    errors,
29
    osutils,
30
    urlutils,
31
    )
1770.2.9 by Aaron Bentley
Add Branch.get_config, update BranchConfig() callers
32
from bzrlib.branch import Branch
33
from bzrlib.bzrdir import BzrDir
1824.1.1 by Robert Collins
Add BranchConfig.has_explicit_nickname call.
34
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
35
36
1553.6.12 by Erik Bågfors
remove AliasConfig, based on input from abentley
37
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
38
sample_config_text = u"""
39
[DEFAULT]
40
email=Erik B\u00e5gfors <erik@bagfors.nu>
41
editor=vim
42
gpg_signing_command=gnome-gpg
43
log_format=short
44
user_global_option=something
45
[ALIASES]
46
h=help
47
ll=""" + sample_long_alias + "\n"
48
49
50
sample_always_signatures = """
51
[DEFAULT]
52
check_signatures=ignore
53
create_signatures=always
54
"""
55
56
sample_ignore_signatures = """
57
[DEFAULT]
58
check_signatures=require
59
create_signatures=never
60
"""
61
62
sample_maybe_signatures = """
63
[DEFAULT]
64
check_signatures=ignore
65
create_signatures=when-required
66
"""
67
68
sample_branches_text = """
69
[http://www.example.com]
70
# Top level policy
71
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
72
normal_option = normal
73
appendpath_option = append
2120.6.8 by James Henstridge
Change syntax for setting config option policies. Rather than
74
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
75
norecurse_option = norecurse
2120.6.8 by James Henstridge
Change syntax for setting config option policies. Rather than
76
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
77
[http://www.example.com/ignoreparent]
78
# different project: ignore parent dir config
79
ignore_parents=true
80
[http://www.example.com/norecurse]
81
# configuration items that only apply to this dir
82
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
83
normal_option = norecurse
84
[http://www.example.com/dir]
85
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
86
[/b/]
87
check_signatures=require
88
# test trailing / matching with no children
89
[/a/]
90
check_signatures=check-available
91
gpg_signing_command=false
92
user_local_option=local
93
# test trailing / matching
94
[/a/*]
95
#subdirs will match but not the parent
96
[/a/c]
97
check_signatures=ignore
98
post_commit=bzrlib.tests.test_config.post_commit
99
#testing explicit beats globs
100
"""
1553.6.3 by Erik Bågfors
tests for AliasesConfig
101
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
102
1474 by Robert Collins
Merge from Aaron Bentley.
103
class InstrumentedConfigObj(object):
104
    """A config obj look-enough-alike to record calls made to it."""
105
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
106
    def __contains__(self, thing):
107
        self._calls.append(('__contains__', thing))
108
        return False
109
110
    def __getitem__(self, key):
111
        self._calls.append(('__getitem__', key))
112
        return self
113
1551.2.20 by Aaron Bentley
Treated config files as utf-8
114
    def __init__(self, input, encoding=None):
115
        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.
116
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
117
    def __setitem__(self, key, value):
118
        self._calls.append(('__setitem__', key, value))
119
2120.6.4 by James Henstridge
add support for specifying policy when storing options
120
    def __delitem__(self, key):
121
        self._calls.append(('__delitem__', key))
122
123
    def keys(self):
124
        self._calls.append(('keys',))
125
        return []
126
1551.2.49 by abentley
Made ConfigObj output binary-identical files on win32 and *nix
127
    def write(self, arg):
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
128
        self._calls.append(('write',))
129
2120.6.4 by James Henstridge
add support for specifying policy when storing options
130
    def as_bool(self, value):
131
        self._calls.append(('as_bool', value))
132
        return False
133
134
    def get_value(self, section, name):
135
        self._calls.append(('get_value', section, name))
136
        return None
137
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
138
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
139
class FakeBranch(object):
140
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
141
    def __init__(self, base=None, user_id=None):
142
        if base is None:
143
            self.base = "http://example.com/branches/demo"
144
        else:
145
            self.base = base
146
        self.control_files = FakeControlFiles(user_id=user_id)
147
148
    def lock_write(self):
149
        pass
150
151
    def unlock(self):
152
        pass
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
153
154
155
class FakeControlFiles(object):
156
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
157
    def __init__(self, user_id=None):
158
        self.email = user_id
159
        self.files = {}
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
160
1185.65.29 by Robert Collins
Implement final review suggestions.
161
    def get_utf8(self, filename):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
162
        if filename != 'email':
163
            raise NotImplementedError
164
        if self.email is not None:
165
            return StringIO(self.email)
1185.31.45 by John Arbash Meinel
Refactoring Exceptions found some places where the wrong exception was caught.
166
        raise errors.NoSuchFile(filename)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
167
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
168
    def get(self, filename):
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
169
        try:
170
            return StringIO(self.files[filename])
171
        except KeyError:
172
            raise errors.NoSuchFile(filename)
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
173
174
    def put(self, filename, fileobj):
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
175
        self.files[filename] = fileobj.read()
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
176
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
177
178
class InstrumentedConfig(config.Config):
179
    """An instrumented config that supplies stubs for template methods."""
180
    
181
    def __init__(self):
182
        super(InstrumentedConfig, self).__init__()
183
        self._calls = []
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
184
        self._signatures = config.CHECK_NEVER
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
185
186
    def _get_user_id(self):
187
        self._calls.append('_get_user_id')
188
        return "Robert Collins <robert.collins@example.org>"
189
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
190
    def _get_signature_checking(self):
191
        self._calls.append('_get_signature_checking')
192
        return self._signatures
193
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
194
1556.2.2 by Aaron Bentley
Fixed get_bool
195
bool_config = """[DEFAULT]
196
active = true
197
inactive = false
198
[UPPERCASE]
199
active = True
200
nonactive = False
201
"""
202
class TestConfigObj(TestCase):
203
    def test_get_bool(self):
204
        from bzrlib.config import ConfigObj
205
        co = ConfigObj(StringIO(bool_config))
206
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
207
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
208
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
209
        self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
210
211
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
212
class TestConfig(TestCase):
213
214
    def test_constructs(self):
215
        config.Config()
216
 
217
    def test_no_default_editor(self):
218
        self.assertRaises(NotImplementedError, config.Config().get_editor)
219
220
    def test_user_email(self):
221
        my_config = InstrumentedConfig()
222
        self.assertEqual('robert.collins@example.org', my_config.user_email())
223
        self.assertEqual(['_get_user_id'], my_config._calls)
224
225
    def test_username(self):
226
        my_config = InstrumentedConfig()
227
        self.assertEqual('Robert Collins <robert.collins@example.org>',
228
                         my_config.username())
229
        self.assertEqual(['_get_user_id'], my_config._calls)
1442.1.14 by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE
230
231
    def test_signatures_default(self):
232
        my_config = config.Config()
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
233
        self.assertFalse(my_config.signature_needed())
1442.1.14 by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE
234
        self.assertEqual(config.CHECK_IF_POSSIBLE,
235
                         my_config.signature_checking())
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
236
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
237
                         my_config.signing_policy())
1442.1.14 by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE
238
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
239
    def test_signatures_template_method(self):
240
        my_config = InstrumentedConfig()
241
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
242
        self.assertEqual(['_get_signature_checking'], my_config._calls)
243
244
    def test_signatures_template_method_none(self):
245
        my_config = InstrumentedConfig()
246
        my_config._signatures = None
247
        self.assertEqual(config.CHECK_IF_POSSIBLE,
248
                         my_config.signature_checking())
249
        self.assertEqual(['_get_signature_checking'], my_config._calls)
250
1442.1.56 by Robert Collins
gpg_signing_command configuration item
251
    def test_gpg_signing_command_default(self):
252
        my_config = config.Config()
253
        self.assertEqual('gpg', my_config.gpg_signing_command())
254
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
255
    def test_get_user_option_default(self):
256
        my_config = config.Config()
257
        self.assertEqual(None, my_config.get_user_option('no_option'))
258
1472 by Robert Collins
post commit hook, first pass implementation
259
    def test_post_commit_default(self):
260
        my_config = config.Config()
261
        self.assertEqual(None, my_config.post_commit())
262
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
263
    def test_log_format_default(self):
1553.2.8 by Erik Bågfors
tests for config log_formatter
264
        my_config = config.Config()
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
265
        self.assertEqual('long', my_config.log_format())
1553.2.8 by Erik Bågfors
tests for config log_formatter
266
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
267
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
268
class TestConfigPath(TestCase):
269
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
270
    def setUp(self):
271
        super(TestConfigPath, self).setUp()
272
        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
273
        if sys.platform == 'win32':
274
            os.environ['BZR_HOME'] = \
275
                r'C:\Documents and Settings\bogus\Application Data'
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
276
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
277
    def test_config_dir(self):
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
278
        if sys.platform == 'win32':
279
            self.assertEqual(config.config_dir(), 
1185.31.36 by John Arbash Meinel
Fixup test_config.py to handle new paths
280
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
281
        else:
282
            self.assertEqual(config.config_dir(), '/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.
283
284
    def test_config_filename(self):
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
285
        if sys.platform == 'win32':
286
            self.assertEqual(config.config_filename(), 
1185.31.36 by John Arbash Meinel
Fixup test_config.py to handle new paths
287
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
288
        else:
289
            self.assertEqual(config.config_filename(),
290
                             '/home/bogus/.bazaar/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.
291
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
292
    def test_branches_config_filename(self):
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
293
        if sys.platform == 'win32':
294
            self.assertEqual(config.branches_config_filename(), 
1185.31.36 by John Arbash Meinel
Fixup test_config.py to handle new paths
295
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
296
        else:
297
            self.assertEqual(config.branches_config_filename(),
298
                             '/home/bogus/.bazaar/branches.conf')
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
299
1770.2.2 by Aaron Bentley
Rename branches.conf to locations.conf
300
    def test_locations_config_filename(self):
301
        if sys.platform == 'win32':
302
            self.assertEqual(config.locations_config_filename(), 
303
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
304
        else:
305
            self.assertEqual(config.locations_config_filename(),
306
                             '/home/bogus/.bazaar/locations.conf')
307
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
308
class TestIniConfig(TestCase):
309
310
    def test_contructs(self):
311
        my_config = config.IniBasedConfig("nothing")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
312
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
313
    def test_from_fp(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
314
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
315
        my_config = config.IniBasedConfig(None)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
316
        self.failUnless(
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
317
            isinstance(my_config._get_parser(file=config_file),
1474 by Robert Collins
Merge from Aaron Bentley.
318
                        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.
319
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
320
    def test_cached(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
321
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
322
        my_config = config.IniBasedConfig(None)
323
        parser = my_config._get_parser(file=config_file)
324
        self.failUnless(my_config._get_parser() is parser)
325
326
327
class TestGetConfig(TestCase):
328
329
    def test_constructs(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
330
        my_config = config.GlobalConfig()
331
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
332
    def test_calls_read_filenames(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
333
        # replace the class that is constructured, to check its parameters
1474 by Robert Collins
Merge from Aaron Bentley.
334
        oldparserclass = config.ConfigObj
335
        config.ConfigObj = InstrumentedConfigObj
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
336
        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.
337
        try:
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
338
            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.
339
        finally:
1474 by Robert Collins
Merge from Aaron Bentley.
340
            config.ConfigObj = oldparserclass
341
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
1551.2.20 by Aaron Bentley
Treated config files as utf-8
342
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
343
                                          '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.
344
345
1824.1.1 by Robert Collins
Add BranchConfig.has_explicit_nickname call.
346
class TestBranchConfig(TestCaseWithTransport):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
347
348
    def test_constructs(self):
349
        branch = FakeBranch()
350
        my_config = config.BranchConfig(branch)
351
        self.assertRaises(TypeError, config.BranchConfig)
352
353
    def test_get_location_config(self):
354
        branch = FakeBranch()
355
        my_config = config.BranchConfig(branch)
356
        location_config = my_config._get_location_config()
357
        self.assertEqual(branch.base, location_config.location)
358
        self.failUnless(location_config is my_config._get_location_config())
359
1770.2.9 by Aaron Bentley
Add Branch.get_config, update BranchConfig() callers
360
    def test_get_config(self):
361
        """The Branch.get_config method works properly"""
362
        b = BzrDir.create_standalone_workingtree('.').branch
363
        my_config = b.get_config()
364
        self.assertIs(my_config.get_user_option('wacky'), None)
365
        my_config.set_user_option('wacky', 'unlikely')
366
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
367
368
        # Ensure we get the same thing if we start again
369
        b2 = Branch.open('.')
370
        my_config2 = b2.get_config()
371
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
372
1824.1.1 by Robert Collins
Add BranchConfig.has_explicit_nickname call.
373
    def test_has_explicit_nickname(self):
374
        b = self.make_branch('.')
375
        self.assertFalse(b.get_config().has_explicit_nickname())
376
        b.nick = 'foo'
377
        self.assertTrue(b.get_config().has_explicit_nickname())
378
1878.1.1 by John Arbash Meinel
Entries in locations.conf should prefer local paths if available (bug #53653)
379
    def test_config_url(self):
380
        """The Branch.get_config will use section that uses a local url"""
381
        branch = self.make_branch('branch')
382
        self.assertEqual('branch', branch.nick)
383
384
        locations = config.locations_config_filename()
385
        config.ensure_config_dir_exists()
386
        local_url = urlutils.local_path_to_url('branch')
387
        open(locations, 'wb').write('[%s]\nnickname = foobar' 
388
                                    % (local_url,))
389
        self.assertEqual('foobar', branch.nick)
390
391
    def test_config_local_path(self):
392
        """The Branch.get_config will use a local system path"""
393
        branch = self.make_branch('branch')
394
        self.assertEqual('branch', branch.nick)
395
396
        locations = config.locations_config_filename()
397
        config.ensure_config_dir_exists()
398
        open(locations, 'wb').write('[%s/branch]\nnickname = barry' 
399
                                    % (osutils.getcwd().encode('utf8'),))
400
        self.assertEqual('barry', branch.nick)
401
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
402
    def test_config_creates_local(self):
403
        """Creating a new entry in config uses a local path."""
2230.3.6 by Aaron Bentley
work in progress bind stuff
404
        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
405
        branch.set_push_location('http://foobar')
406
        locations = config.locations_config_filename()
407
        local_path = osutils.getcwd().encode('utf8')
408
        # Surprisingly ConfigObj doesn't create a trailing newline
409
        self.check_file_contents(locations,
2120.6.8 by James Henstridge
Change syntax for setting config option policies. Rather than
410
            '[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (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
411
2120.5.4 by Alexander Belchenko
Whitebox test for Config.get_nickname (req. by Aaron Bentley)
412
    def test_autonick_urlencoded(self):
413
        b = self.make_branch('!repo')
414
        self.assertEqual('!repo', b.get_config().get_nickname())
415
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
416
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
417
class TestGlobalConfigItems(TestCase):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
418
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
419
    def test_user_id(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
420
        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
421
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
422
        my_config._parser = my_config._get_parser(file=config_file)
1551.2.21 by Aaron Bentley
Formatted unicode config tests as ASCII
423
        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
424
                         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.
425
426
    def test_absent_user_id(self):
427
        config_file = StringIO("")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
428
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
429
        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
430
        self.assertEqual(None, my_config._get_user_id())
431
432
    def test_configured_editor(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
433
        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
434
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
435
        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
436
        self.assertEqual("vim", my_config.get_editor())
437
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
438
    def test_signatures_always(self):
439
        config_file = StringIO(sample_always_signatures)
440
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
441
        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
442
        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
443
                         my_config.signature_checking())
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
444
        self.assertEqual(config.SIGN_ALWAYS,
445
                         my_config.signing_policy())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
446
        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
447
448
    def test_signatures_if_possible(self):
449
        config_file = StringIO(sample_maybe_signatures)
450
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
451
        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
452
        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
453
                         my_config.signature_checking())
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
454
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
455
                         my_config.signing_policy())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
456
        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
457
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
458
    def test_signatures_ignore(self):
459
        config_file = StringIO(sample_ignore_signatures)
460
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
461
        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
462
        self.assertEqual(config.CHECK_ALWAYS,
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
463
                         my_config.signature_checking())
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
464
        self.assertEqual(config.SIGN_NEVER,
465
                         my_config.signing_policy())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
466
        self.assertEqual(False, my_config.signature_needed())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
467
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
468
    def _get_sample_config(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
469
        config_file = StringIO(sample_config_text.encode('utf-8'))
1534.7.154 by Aaron Bentley
Removed changes from bzr.ab 1529..1536
470
        my_config = config.GlobalConfig()
471
        my_config._parser = my_config._get_parser(file=config_file)
472
        return my_config
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
473
1442.1.56 by Robert Collins
gpg_signing_command configuration item
474
    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.
475
        my_config = self._get_sample_config()
1442.1.56 by Robert Collins
gpg_signing_command configuration item
476
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
477
        self.assertEqual(False, my_config.signature_needed())
478
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
479
    def _get_empty_config(self):
480
        config_file = StringIO("")
481
        my_config = config.GlobalConfig()
482
        my_config._parser = my_config._get_parser(file=config_file)
483
        return my_config
484
1442.1.59 by Robert Collins
Add re-sign command to generate a digital signature on a single revision.
485
    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.
486
        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.
487
        self.assertEqual("gpg", my_config.gpg_signing_command())
488
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
489
    def test_get_user_option_default(self):
490
        my_config = self._get_empty_config()
491
        self.assertEqual(None, my_config.get_user_option('no_option'))
492
493
    def test_get_user_option_global(self):
494
        my_config = self._get_sample_config()
495
        self.assertEqual("something",
496
                         my_config.get_user_option('user_global_option'))
1472 by Robert Collins
post commit hook, first pass implementation
497
        
498
    def test_post_commit_default(self):
499
        my_config = self._get_sample_config()
500
        self.assertEqual(None, my_config.post_commit())
501
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
502
    def test_configured_logformat(self):
1553.2.8 by Erik Bågfors
tests for config log_formatter
503
        my_config = self._get_sample_config()
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
504
        self.assertEqual("short", my_config.log_format())
1553.2.8 by Erik Bågfors
tests for config log_formatter
505
1553.6.12 by Erik Bågfors
remove AliasConfig, based on input from abentley
506
    def test_get_alias(self):
507
        my_config = self._get_sample_config()
508
        self.assertEqual('help', my_config.get_alias('h'))
509
510
    def test_get_no_alias(self):
511
        my_config = self._get_sample_config()
512
        self.assertEqual(None, my_config.get_alias('foo'))
513
514
    def test_get_long_alias(self):
515
        my_config = self._get_sample_config()
516
        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.
517
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
518
519
class TestLocationConfig(TestCaseInTempDir):
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
520
521
    def test_constructs(self):
522
        my_config = config.LocationConfig('http://example.com')
523
        self.assertRaises(TypeError, config.LocationConfig)
524
525
    def test_branch_calls_read_filenames(self):
1474 by Robert Collins
Merge from Aaron Bentley.
526
        # This is testing the correct file names are provided.
527
        # TODO: consolidate with the test for GlobalConfigs filename checks.
528
        #
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
529
        # replace the class that is constructured, to check its parameters
1474 by Robert Collins
Merge from Aaron Bentley.
530
        oldparserclass = config.ConfigObj
531
        config.ConfigObj = InstrumentedConfigObj
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
532
        try:
1770.2.2 by Aaron Bentley
Rename branches.conf to locations.conf
533
            my_config = config.LocationConfig('http://www.example.com')
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
534
            parser = my_config._get_parser()
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
535
        finally:
1474 by Robert Collins
Merge from Aaron Bentley.
536
            config.ConfigObj = oldparserclass
537
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
538
        self.assertEqual(parser._calls,
1770.2.2 by Aaron Bentley
Rename branches.conf to locations.conf
539
                         [('__init__', config.locations_config_filename(),
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
540
                           'utf-8')])
1711.4.29 by John Arbash Meinel
Alexander Belchenko, fix test_config to use ensure_config_dir, rather than os.mkdir()
541
        config.ensure_config_dir_exists()
542
        #os.mkdir(config.config_dir())
1770.2.2 by Aaron Bentley
Rename branches.conf to locations.conf
543
        f = file(config.branches_config_filename(), 'wb')
544
        f.write('')
545
        f.close()
546
        oldparserclass = config.ConfigObj
547
        config.ConfigObj = InstrumentedConfigObj
548
        try:
549
            my_config = config.LocationConfig('http://www.example.com')
550
            parser = my_config._get_parser()
551
        finally:
552
            config.ConfigObj = oldparserclass
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
553
554
    def test_get_global_config(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
555
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
556
        global_config = my_config._get_global_config()
557
        self.failUnless(isinstance(global_config, config.GlobalConfig))
558
        self.failUnless(global_config is my_config._get_global_config())
559
1993.3.1 by James Henstridge
first go at making location config lookup recursive
560
    def test__get_matching_sections_no_match(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
561
        self.get_branch_config('/')
1993.3.1 by James Henstridge
first go at making location config lookup recursive
562
        self.assertEqual([], self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
563
        
1993.3.1 by James Henstridge
first go at making location config lookup recursive
564
    def test__get_matching_sections_exact(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
565
        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
566
        self.assertEqual([('http://www.example.com', '')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
567
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
568
   
1993.3.1 by James Henstridge
first go at making location config lookup recursive
569
    def test__get_matching_sections_suffix_does_not(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
570
        self.get_branch_config('http://www.example.com-com')
1993.3.1 by James Henstridge
first go at making location config lookup recursive
571
        self.assertEqual([], self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
572
1993.3.1 by James Henstridge
first go at making location config lookup recursive
573
    def test__get_matching_sections_subdir_recursive(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
574
        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
575
        self.assertEqual([('http://www.example.com', 'com')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
576
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
577
1993.3.5 by James Henstridge
add back recurse=False option to config file
578
    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
579
        self.get_branch_config('http://www.example.com/ignoreparent')
580
        self.assertEqual([('http://www.example.com/ignoreparent', '')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
581
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
582
1993.3.5 by James Henstridge
add back recurse=False option to config file
583
    def test__get_matching_sections_ignoreparent_subdir(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
584
        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
585
            'http://www.example.com/ignoreparent/childbranch')
586
        self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
587
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
588
1993.3.1 by James Henstridge
first go at making location config lookup recursive
589
    def test__get_matching_sections_subdir_trailing_slash(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
590
        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
591
        self.assertEqual([('/b/', '')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
592
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
593
1993.3.1 by James Henstridge
first go at making location config lookup recursive
594
    def test__get_matching_sections_subdir_child(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
595
        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
596
        self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
597
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
598
1993.3.1 by James Henstridge
first go at making location config lookup recursive
599
    def test__get_matching_sections_subdir_child_child(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
600
        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
601
        self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
602
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
603
1993.3.1 by James Henstridge
first go at making location config lookup recursive
604
    def test__get_matching_sections_trailing_slash_with_children(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
605
        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
606
        self.assertEqual([('/a/', '')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
607
                         self.my_location_config._get_matching_sections())
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
608
1993.3.1 by James Henstridge
first go at making location config lookup recursive
609
    def test__get_matching_sections_explicit_over_glob(self):
610
        # XXX: 2006-09-08 jamesh
611
        # This test only passes because ord('c') > ord('*').  If there
612
        # was a config section for '/a/?', it would get precedence
613
        # over '/a/c'.
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
614
        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
615
        self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
1993.3.1 by James Henstridge
first go at making location config lookup recursive
616
                         self.my_location_config._get_matching_sections())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
617
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
618
    def test__get_option_policy_normal(self):
619
        self.get_branch_config('http://www.example.com')
620
        self.assertEqual(
621
            self.my_location_config._get_config_policy(
622
            'http://www.example.com', 'normal_option'),
623
            config.POLICY_NONE)
624
625
    def test__get_option_policy_norecurse(self):
626
        self.get_branch_config('http://www.example.com')
627
        self.assertEqual(
628
            self.my_location_config._get_option_policy(
629
            'http://www.example.com', 'norecurse_option'),
630
            config.POLICY_NORECURSE)
631
        # Test old recurse=False setting:
632
        self.assertEqual(
633
            self.my_location_config._get_option_policy(
634
            'http://www.example.com/norecurse', 'normal_option'),
635
            config.POLICY_NORECURSE)
636
637
    def test__get_option_policy_normal(self):
638
        self.get_branch_config('http://www.example.com')
639
        self.assertEqual(
640
            self.my_location_config._get_option_policy(
641
            'http://www.example.com', 'appendpath_option'),
642
            config.POLICY_APPENDPATH)
643
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
644
    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
645
        self.get_branch_config('http://www.example.com/ignoreparent')
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
646
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
647
                         self.my_config.username())
648
649
    def test_location_not_listed(self):
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
650
        """Test that the global username is used when no location matches"""
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
651
        self.get_branch_config('/home/robertc/sources')
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
652
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
653
                         self.my_config.username())
654
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
655
    def test_overriding_location(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
656
        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
657
        self.assertEqual('Robert Collins <robertc@example.org>',
658
                         self.my_config.username())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
659
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
660
    def test_signatures_not_set(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
661
        self.get_branch_config('http://www.example.com',
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
662
                                 global_config=sample_ignore_signatures)
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
663
        self.assertEqual(config.CHECK_ALWAYS,
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
664
                         self.my_config.signature_checking())
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
665
        self.assertEqual(config.SIGN_NEVER,
666
                         self.my_config.signing_policy())
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
667
668
    def test_signatures_never(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
669
        self.get_branch_config('/a/c')
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
670
        self.assertEqual(config.CHECK_NEVER,
671
                         self.my_config.signature_checking())
672
        
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
673
    def test_signatures_when_available(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
674
        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
675
        self.assertEqual(config.CHECK_IF_POSSIBLE,
676
                         self.my_config.signature_checking())
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
677
        
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
678
    def test_signatures_always(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
679
        self.get_branch_config('/b')
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
680
        self.assertEqual(config.CHECK_ALWAYS,
681
                         self.my_config.signature_checking())
682
        
1442.1.56 by Robert Collins
gpg_signing_command configuration item
683
    def test_gpg_signing_command(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
684
        self.get_branch_config('/b')
1442.1.56 by Robert Collins
gpg_signing_command configuration item
685
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
686
687
    def test_gpg_signing_command_missing(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
688
        self.get_branch_config('/a')
1442.1.56 by Robert Collins
gpg_signing_command configuration item
689
        self.assertEqual("false", self.my_config.gpg_signing_command())
690
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
691
    def test_get_user_option_global(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
692
        self.get_branch_config('/a')
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
693
        self.assertEqual('something',
694
                         self.my_config.get_user_option('user_global_option'))
695
696
    def test_get_user_option_local(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
697
        self.get_branch_config('/a')
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
698
        self.assertEqual('local',
699
                         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
700
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
701
    def test_get_user_option_appendpath(self):
702
        # returned as is for the base path:
703
        self.get_branch_config('http://www.example.com')
704
        self.assertEqual('append',
705
                         self.my_config.get_user_option('appendpath_option'))
706
        # Extra path components get appended:
707
        self.get_branch_config('http://www.example.com/a/b/c')
708
        self.assertEqual('append/a/b/c',
709
                         self.my_config.get_user_option('appendpath_option'))
710
        # Overriden for http://www.example.com/dir, where it is a
711
        # normal option:
712
        self.get_branch_config('http://www.example.com/dir/a/b/c')
713
        self.assertEqual('normal',
714
                         self.my_config.get_user_option('appendpath_option'))
715
716
    def test_get_user_option_norecurse(self):
717
        self.get_branch_config('http://www.example.com')
718
        self.assertEqual('norecurse',
719
                         self.my_config.get_user_option('norecurse_option'))
720
        self.get_branch_config('http://www.example.com/dir')
721
        self.assertEqual(None,
722
                         self.my_config.get_user_option('norecurse_option'))
723
        # http://www.example.com/norecurse is a recurse=False section
724
        # that redefines normal_option.  Subdirectories do not pick up
725
        # this redefinition.
726
        self.get_branch_config('http://www.example.com/norecurse')
727
        self.assertEqual('norecurse',
728
                         self.my_config.get_user_option('normal_option'))
729
        self.get_branch_config('http://www.example.com/norecurse/subdir')
730
        self.assertEqual('normal',
731
                         self.my_config.get_user_option('normal_option'))
732
2120.6.4 by James Henstridge
add support for specifying policy when storing options
733
    def test_set_user_option_norecurse(self):
734
        self.get_branch_config('http://www.example.com')
735
        self.my_config.set_user_option('foo', 'bar',
736
                                       store=config.STORE_LOCATION_NORECURSE)
737
        self.assertEqual(
738
            self.my_location_config._get_option_policy(
739
            'http://www.example.com', 'foo'),
740
            config.POLICY_NORECURSE)
741
742
    def test_set_user_option_appendpath(self):
743
        self.get_branch_config('http://www.example.com')
744
        self.my_config.set_user_option('foo', 'bar',
745
                                       store=config.STORE_LOCATION_APPENDPATH)
746
        self.assertEqual(
747
            self.my_location_config._get_option_policy(
748
            'http://www.example.com', 'foo'),
749
            config.POLICY_APPENDPATH)
750
751
    def test_set_user_option_change_policy(self):
752
        self.get_branch_config('http://www.example.com')
753
        self.my_config.set_user_option('norecurse_option', 'normal',
754
                                       store=config.STORE_LOCATION)
755
        self.assertEqual(
756
            self.my_location_config._get_option_policy(
757
            'http://www.example.com', 'norecurse_option'),
758
            config.POLICY_NONE)
759
760
    def test_set_user_option_recurse_false_section(self):
2120.6.9 by James Henstridge
Fixes for issues brought up in John's review
761
        # The following section has recurse=False set.  The test is to
762
        # make sure that a normal option can be added to the section,
763
        # converting recurse=False to the norecurse policy.
2120.6.4 by James Henstridge
add support for specifying policy when storing options
764
        self.get_branch_config('http://www.example.com/norecurse')
2120.6.11 by James Henstridge
s/0.13/0.14/ in deprecation warning
765
        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
766
                             'The section "http://www.example.com/norecurse" '
767
                             'has been converted to use policies.'],
768
                            self.my_config.set_user_option,
769
                            'foo', 'bar', store=config.STORE_LOCATION)
2120.6.4 by James Henstridge
add support for specifying policy when storing options
770
        self.assertEqual(
771
            self.my_location_config._get_option_policy(
772
            'http://www.example.com/norecurse', 'foo'),
773
            config.POLICY_NONE)
774
        # The previously existing option is still norecurse:
775
        self.assertEqual(
776
            self.my_location_config._get_option_policy(
777
            'http://www.example.com/norecurse', 'normal_option'),
778
            config.POLICY_NORECURSE)
779
        
780
1472 by Robert Collins
post commit hook, first pass implementation
781
    def test_post_commit_default(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
782
        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
783
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1472 by Robert Collins
post commit hook, first pass implementation
784
                         self.my_config.post_commit())
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
785
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
786
    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.
787
        if global_config is None:
1551.2.20 by Aaron Bentley
Treated config files as utf-8
788
            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.
789
        else:
1551.2.20 by Aaron Bentley
Treated config files as utf-8
790
            global_file = StringIO(global_config.encode('utf-8'))
791
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
792
        self.my_config = config.BranchConfig(FakeBranch(location))
793
        # Force location config to use specified file
794
        self.my_location_config = self.my_config._get_location_config()
795
        self.my_location_config._get_parser(branches_file)
796
        # 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.
797
        self.my_config._get_global_config()._get_parser(global_file)
798
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
799
    def test_set_user_setting_sets_and_saves(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
800
        self.get_branch_config('/a/c')
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
801
        record = InstrumentedConfigObj("foo")
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
802
        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
803
804
        real_mkdir = os.mkdir
805
        self.created = False
806
        def checked_mkdir(path, mode=0777):
807
            self.log('making directory: %s', path)
808
            real_mkdir(path, mode)
809
            self.created = True
810
811
        os.mkdir = checked_mkdir
812
        try:
2120.6.10 by James Henstridge
Catch another deprecation warning, and more cleanup
813
            self.callDeprecated(['The recurse option is deprecated as of '
2120.6.11 by James Henstridge
s/0.13/0.14/ in deprecation warning
814
                                 '0.14.  The section "/a/c" has been '
2120.6.10 by James Henstridge
Catch another deprecation warning, and more cleanup
815
                                 'converted to use policies.'],
816
                                self.my_config.set_user_option,
817
                                '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
818
        finally:
819
            os.mkdir = real_mkdir
820
821
        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.
822
        self.assertEqual([('__contains__', '/a/c'),
823
                          ('__contains__', '/a/c/'),
824
                          ('__setitem__', '/a/c', {}),
825
                          ('__getitem__', '/a/c'),
826
                          ('__setitem__', 'foo', 'bar'),
2120.6.4 by James Henstridge
add support for specifying policy when storing options
827
                          ('__getitem__', '/a/c'),
828
                          ('as_bool', 'recurse'),
829
                          ('__getitem__', '/a/c'),
830
                          ('__delitem__', 'recurse'),
831
                          ('__getitem__', '/a/c'),
832
                          ('keys',),
2120.6.8 by James Henstridge
Change syntax for setting config option policies. Rather than
833
                          ('__getitem__', '/a/c'),
834
                          ('__contains__', 'foo:policy'),
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
835
                          ('write',)],
836
                         record._calls[1:])
837
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
838
    def test_set_user_setting_sets_and_saves2(self):
839
        self.get_branch_config('/a/c')
840
        self.assertIs(self.my_config.get_user_option('foo'), None)
841
        self.my_config.set_user_option('foo', 'bar')
842
        self.assertEqual(
843
            self.my_config.branch.control_files.files['branch.conf'], 
844
            'foo = bar')
845
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
2120.6.4 by James Henstridge
add support for specifying policy when storing options
846
        self.my_config.set_user_option('foo', 'baz',
847
                                       store=config.STORE_LOCATION)
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
848
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
849
        self.my_config.set_user_option('foo', 'qux')
850
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
851
        
1185.62.7 by John Arbash Meinel
Whitespace cleanup.
852
1770.2.8 by Aaron Bentley
Add precedence test
853
precedence_global = 'option = global'
854
precedence_branch = 'option = branch'
855
precedence_location = """
856
[http://]
857
recurse = true
858
option = recurse
859
[http://example.com/specific]
860
option = exact
861
"""
862
863
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
864
class TestBranchConfigItems(TestCaseInTempDir):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
865
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
866
    def get_branch_config(self, global_config=None, location=None, 
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
867
                          location_config=None, branch_data_config=None):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
868
        my_config = config.BranchConfig(FakeBranch(location))
869
        if global_config is not None:
870
            global_file = StringIO(global_config.encode('utf-8'))
871
            my_config._get_global_config()._get_parser(global_file)
872
        self.my_location_config = my_config._get_location_config()
873
        if location_config is not None:
874
            location_file = StringIO(location_config.encode('utf-8'))
875
            self.my_location_config._get_parser(location_file)
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
876
        if branch_data_config is not None:
877
            my_config.branch.control_files.files['branch.conf'] = \
878
                branch_data_config
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
879
        return my_config
880
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
881
    def test_user_id(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
882
        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
883
        my_config = config.BranchConfig(branch)
884
        self.assertEqual("Robert Collins <robertc@example.net>",
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
885
                         my_config.username())
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
886
        branch.control_files.email = "John"
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
887
        my_config.set_user_option('email', 
888
                                  "Robert Collins <robertc@example.org>")
889
        self.assertEqual("John", my_config.username())
890
        branch.control_files.email = None
891
        self.assertEqual("Robert Collins <robertc@example.org>",
892
                         my_config.username())
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
893
894
    def test_not_set_in_branch(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
895
        my_config = self.get_branch_config(sample_config_text)
896
        my_config.branch.control_files.email = None
1551.2.21 by Aaron Bentley
Formatted unicode config tests as ASCII
897
        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
898
                         my_config._get_user_id())
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
899
        my_config.branch.control_files.email = "John"
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
900
        self.assertEqual("John", my_config._get_user_id())
901
1861.4.1 by Matthieu Moy
BZREMAIL renamed to BZR_EMAIL.
902
    def test_BZR_EMAIL_OVERRIDES(self):
903
        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
904
        branch = FakeBranch()
905
        my_config = config.BranchConfig(branch)
906
        self.assertEqual("Robert Collins <robertc@example.org>",
907
                         my_config.username())
908
    
1442.1.19 by Robert Collins
BranchConfigs inherit signature_checking policy from their LocationConfig.
909
    def test_signatures_forced(self):
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
910
        my_config = self.get_branch_config(
911
            global_config=sample_always_signatures)
1770.2.1 by Aaron Bentley
Use create_signature for signing policy, deprecate check_signatures for this
912
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
913
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
914
        self.assertTrue(my_config.signature_needed())
1442.1.56 by Robert Collins
gpg_signing_command configuration item
915
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
916
    def test_signatures_forced_branch(self):
917
        my_config = self.get_branch_config(
918
            global_config=sample_ignore_signatures,
919
            branch_data_config=sample_always_signatures)
920
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
921
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
922
        self.assertTrue(my_config.signature_needed())
923
1442.1.56 by Robert Collins
gpg_signing_command configuration item
924
    def test_gpg_signing_command(self):
1770.2.10 by Aaron Bentley
Added test that branch_config can't influence gpg_signing_command
925
        my_config = self.get_branch_config(
926
            # branch data cannot set gpg_signing_command
927
            branch_data_config="gpg_signing_command=pgp")
1551.2.20 by Aaron Bentley
Treated config files as utf-8
928
        config_file = StringIO(sample_config_text.encode('utf-8'))
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
929
        my_config._get_global_config()._get_parser(config_file)
1442.1.56 by Robert Collins
gpg_signing_command configuration item
930
        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.
931
932
    def test_get_user_option_global(self):
933
        branch = FakeBranch()
934
        my_config = config.BranchConfig(branch)
1551.2.20 by Aaron Bentley
Treated config files as utf-8
935
        config_file = StringIO(sample_config_text.encode('utf-8'))
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
936
        (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.
937
        self.assertEqual('something',
938
                         my_config.get_user_option('user_global_option'))
1472 by Robert Collins
post commit hook, first pass implementation
939
940
    def test_post_commit_default(self):
941
        branch = FakeBranch()
1770.2.5 by Aaron Bentley
Integrate branch.conf into BranchConfig
942
        my_config = self.get_branch_config(sample_config_text, '/a/c',
943
                                           sample_branches_text)
944
        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
945
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1472 by Robert Collins
post commit hook, first pass implementation
946
                         my_config.post_commit())
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
947
        my_config.set_user_option('post_commit', 'rmtree_root')
948
        # post-commit is ignored when bresent in branch data
949
        self.assertEqual('bzrlib.tests.test_config.post_commit',
950
                         my_config.post_commit())
2120.6.4 by James Henstridge
add support for specifying policy when storing options
951
        my_config.set_user_option('post_commit', 'rmtree_root',
952
                                  store=config.STORE_LOCATION)
1770.2.6 by Aaron Bentley
Ensure branch.conf works properly
953
        self.assertEqual('rmtree_root', my_config.post_commit())
1185.33.31 by Martin Pool
Make annotate cope better with revisions committed without a valid
954
1770.2.8 by Aaron Bentley
Add precedence test
955
    def test_config_precedence(self):
956
        my_config = self.get_branch_config(global_config=precedence_global)
957
        self.assertEqual(my_config.get_user_option('option'), 'global')
958
        my_config = self.get_branch_config(global_config=precedence_global, 
959
                                      branch_data_config=precedence_branch)
960
        self.assertEqual(my_config.get_user_option('option'), 'branch')
961
        my_config = self.get_branch_config(global_config=precedence_global, 
962
                                      branch_data_config=precedence_branch,
963
                                      location_config=precedence_location)
964
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
965
        my_config = self.get_branch_config(global_config=precedence_global, 
966
                                      branch_data_config=precedence_branch,
967
                                      location_config=precedence_location,
968
                                      location='http://example.com/specific')
969
        self.assertEqual(my_config.get_user_option('option'), 'exact')
970
1185.33.31 by Martin Pool
Make annotate cope better with revisions committed without a valid
971
972
class TestMailAddressExtraction(TestCase):
973
974
    def test_extract_email_address(self):
975
        self.assertEqual('jane@test.com',
976
                         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
977
        self.assertRaises(errors.NoEmailInUsername,
1185.33.31 by Martin Pool
Make annotate cope better with revisions committed without a valid
978
                          config.extract_email_address, 'Jane Tester')