/brz/remove-bazaar

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