~bzr-pqm/bzr/bzr.dev

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