~bzr-pqm/bzr/bzr.dev

1442.1.1 by Robert Collins
move config_dir into bzrlib.config
1
# Copyright (C) 2005 by Canonical Ltd
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
26
import bzrlib.config as config
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
27
import bzrlib.errors as errors
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
28
from bzrlib.tests import TestCase, TestCaseInTempDir
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
29
30
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
31
sample_config_text = ("[DEFAULT]\n"
32
                      "email=Robert Collins <robertc@example.com>\n"
33
                      "editor=vim\n"
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
34
                      "gpg_signing_command=gnome-gpg\n"
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
35
                      "log_format=short\n"
1534.7.154 by Aaron Bentley
Removed changes from bzr.ab 1529..1536
36
                      "user_global_option=something\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.
37
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
38
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
39
sample_always_signatures = ("[DEFAULT]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
40
                            "check_signatures=require\n")
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
41
42
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
43
sample_ignore_signatures = ("[DEFAULT]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
44
                            "check_signatures=ignore\n")
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
45
46
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
47
sample_maybe_signatures = ("[DEFAULT]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
48
                            "check_signatures=check-available\n")
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
49
50
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
51
sample_branches_text = ("[http://www.example.com]\n"
52
                        "# Top level policy\n"
53
                        "email=Robert Collins <robertc@example.org>\n"
54
                        "[http://www.example.com/useglobal]\n"
55
                        "# different project, forces global lookup\n"
56
                        "recurse=false\n"
57
                        "[/b/]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
58
                        "check_signatures=require\n"
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
59
                        "# test trailing / matching with no children\n"
60
                        "[/a/]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
61
                        "check_signatures=check-available\n"
1442.1.56 by Robert Collins
gpg_signing_command configuration item
62
                        "gpg_signing_command=false\n"
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
63
                        "user_local_option=local\n"
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
64
                        "# test trailing / matching\n"
65
                        "[/a/*]\n"
66
                        "#subdirs will match but not the parent\n"
67
                        "recurse=False\n"
68
                        "[/a/c]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
69
                        "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
70
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
71
                        "#testing explicit beats globs\n")
72
73
1474 by Robert Collins
Merge from Aaron Bentley.
74
class InstrumentedConfigObj(object):
75
    """A config obj look-enough-alike to record calls made to it."""
76
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
77
    def __contains__(self, thing):
78
        self._calls.append(('__contains__', thing))
79
        return False
80
81
    def __getitem__(self, key):
82
        self._calls.append(('__getitem__', key))
83
        return self
84
1474 by Robert Collins
Merge from Aaron Bentley.
85
    def __init__(self, input):
86
        self._calls = [('__init__', input)]
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
87
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
88
    def __setitem__(self, key, value):
89
        self._calls.append(('__setitem__', key, value))
90
91
    def write(self):
92
        self._calls.append(('write',))
93
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
94
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
95
class FakeBranch(object):
96
97
    def __init__(self):
98
        self.base = "http://example.com/branches/demo"
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
99
        self.control_files = FakeControlFiles()
100
101
102
class FakeControlFiles(object):
103
104
    def __init__(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
105
        self.email = 'Robert Collins <robertc@example.net>\n'
106
1185.65.29 by Robert Collins
Implement final review suggestions.
107
    def get_utf8(self, filename):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
108
        if filename != 'email':
109
            raise NotImplementedError
110
        if self.email is not None:
111
            return StringIO(self.email)
1185.31.45 by John Arbash Meinel
Refactoring Exceptions found some places where the wrong exception was caught.
112
        raise errors.NoSuchFile(filename)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
113
114
115
class InstrumentedConfig(config.Config):
116
    """An instrumented config that supplies stubs for template methods."""
117
    
118
    def __init__(self):
119
        super(InstrumentedConfig, self).__init__()
120
        self._calls = []
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
121
        self._signatures = config.CHECK_NEVER
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
122
123
    def _get_user_id(self):
124
        self._calls.append('_get_user_id')
125
        return "Robert Collins <robert.collins@example.org>"
126
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
127
    def _get_signature_checking(self):
128
        self._calls.append('_get_signature_checking')
129
        return self._signatures
130
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
131
1556.2.2 by Aaron Bentley
Fixed get_bool
132
bool_config = """[DEFAULT]
133
active = true
134
inactive = false
135
[UPPERCASE]
136
active = True
137
nonactive = False
138
"""
139
class TestConfigObj(TestCase):
140
    def test_get_bool(self):
141
        from bzrlib.config import ConfigObj
142
        co = ConfigObj(StringIO(bool_config))
143
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
144
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
145
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
146
        self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
147
148
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
149
class TestConfig(TestCase):
150
151
    def test_constructs(self):
152
        config.Config()
153
 
154
    def test_no_default_editor(self):
155
        self.assertRaises(NotImplementedError, config.Config().get_editor)
156
157
    def test_user_email(self):
158
        my_config = InstrumentedConfig()
159
        self.assertEqual('robert.collins@example.org', my_config.user_email())
160
        self.assertEqual(['_get_user_id'], my_config._calls)
161
162
    def test_username(self):
163
        my_config = InstrumentedConfig()
164
        self.assertEqual('Robert Collins <robert.collins@example.org>',
165
                         my_config.username())
166
        self.assertEqual(['_get_user_id'], my_config._calls)
1442.1.14 by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE
167
168
    def test_signatures_default(self):
169
        my_config = config.Config()
170
        self.assertEqual(config.CHECK_IF_POSSIBLE,
171
                         my_config.signature_checking())
172
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
173
    def test_signatures_template_method(self):
174
        my_config = InstrumentedConfig()
175
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
176
        self.assertEqual(['_get_signature_checking'], my_config._calls)
177
178
    def test_signatures_template_method_none(self):
179
        my_config = InstrumentedConfig()
180
        my_config._signatures = None
181
        self.assertEqual(config.CHECK_IF_POSSIBLE,
182
                         my_config.signature_checking())
183
        self.assertEqual(['_get_signature_checking'], my_config._calls)
184
1442.1.56 by Robert Collins
gpg_signing_command configuration item
185
    def test_gpg_signing_command_default(self):
186
        my_config = config.Config()
187
        self.assertEqual('gpg', my_config.gpg_signing_command())
188
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
189
    def test_get_user_option_default(self):
190
        my_config = config.Config()
191
        self.assertEqual(None, my_config.get_user_option('no_option'))
192
1472 by Robert Collins
post commit hook, first pass implementation
193
    def test_post_commit_default(self):
194
        my_config = config.Config()
195
        self.assertEqual(None, my_config.post_commit())
196
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
197
    def test_log_format_default(self):
1553.2.8 by Erik Bågfors
tests for config log_formatter
198
        my_config = config.Config()
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
199
        self.assertEqual('long', my_config.log_format())
1553.2.8 by Erik Bågfors
tests for config log_formatter
200
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
201
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
202
class TestConfigPath(TestCase):
203
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
204
    def setUp(self):
205
        super(TestConfigPath, self).setUp()
1185.38.5 by John Arbash Meinel
Updating testconfig to handle missing environment variables
206
        self.old_home = os.environ.get('HOME', None)
207
        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.
208
        os.environ['HOME'] = '/home/bogus'
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
209
        os.environ['APPDATA'] = \
210
            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.
211
212
    def tearDown(self):
1185.38.5 by John Arbash Meinel
Updating testconfig to handle missing environment variables
213
        if self.old_home is None:
214
            del os.environ['HOME']
215
        else:
216
            os.environ['HOME'] = self.old_home
217
        if self.old_appdata is None:
218
            del os.environ['APPDATA']
219
        else:
220
            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
221
        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.
222
    
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
223
    def test_config_dir(self):
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
224
        if sys.platform == 'win32':
225
            self.assertEqual(config.config_dir(), 
1185.31.36 by John Arbash Meinel
Fixup test_config.py to handle new paths
226
                '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.
227
        else:
228
            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.
229
230
    def test_config_filename(self):
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
231
        if sys.platform == 'win32':
232
            self.assertEqual(config.config_filename(), 
1185.31.36 by John Arbash Meinel
Fixup test_config.py to handle new paths
233
                '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.
234
        else:
235
            self.assertEqual(config.config_filename(),
236
                             '/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.
237
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
238
    def test_branches_config_filename(self):
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
239
        if sys.platform == 'win32':
240
            self.assertEqual(config.branches_config_filename(), 
1185.31.36 by John Arbash Meinel
Fixup test_config.py to handle new paths
241
                '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.
242
        else:
243
            self.assertEqual(config.branches_config_filename(),
244
                             '/home/bogus/.bazaar/branches.conf')
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
245
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
246
class TestIniConfig(TestCase):
247
248
    def test_contructs(self):
249
        my_config = config.IniBasedConfig("nothing")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
250
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
251
    def test_from_fp(self):
252
        config_file = StringIO(sample_config_text)
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
253
        my_config = config.IniBasedConfig(None)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
254
        self.failUnless(
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
255
            isinstance(my_config._get_parser(file=config_file),
1474 by Robert Collins
Merge from Aaron Bentley.
256
                        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.
257
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
258
    def test_cached(self):
259
        config_file = StringIO(sample_config_text)
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
260
        my_config = config.IniBasedConfig(None)
261
        parser = my_config._get_parser(file=config_file)
262
        self.failUnless(my_config._get_parser() is parser)
263
264
265
class TestGetConfig(TestCase):
266
267
    def test_constructs(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
268
        my_config = config.GlobalConfig()
269
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
270
    def test_calls_read_filenames(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
271
        # replace the class that is constructured, to check its parameters
1474 by Robert Collins
Merge from Aaron Bentley.
272
        oldparserclass = config.ConfigObj
273
        config.ConfigObj = InstrumentedConfigObj
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
274
        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.
275
        try:
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
276
            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.
277
        finally:
1474 by Robert Collins
Merge from Aaron Bentley.
278
            config.ConfigObj = oldparserclass
279
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
280
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
281
282
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
283
class TestBranchConfig(TestCaseInTempDir):
284
285
    def test_constructs(self):
286
        branch = FakeBranch()
287
        my_config = config.BranchConfig(branch)
288
        self.assertRaises(TypeError, config.BranchConfig)
289
290
    def test_get_location_config(self):
291
        branch = FakeBranch()
292
        my_config = config.BranchConfig(branch)
293
        location_config = my_config._get_location_config()
294
        self.assertEqual(branch.base, location_config.location)
295
        self.failUnless(location_config is my_config._get_location_config())
296
297
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
298
class TestGlobalConfigItems(TestCase):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
299
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
300
    def test_user_id(self):
301
        config_file = StringIO(sample_config_text)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
302
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
303
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
304
        self.assertEqual("Robert Collins <robertc@example.com>",
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
305
                         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.
306
307
    def test_absent_user_id(self):
308
        config_file = StringIO("")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
309
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
310
        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
311
        self.assertEqual(None, my_config._get_user_id())
312
313
    def test_configured_editor(self):
314
        config_file = StringIO(sample_config_text)
315
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
316
        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
317
        self.assertEqual("vim", my_config.get_editor())
318
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
319
    def test_signatures_always(self):
320
        config_file = StringIO(sample_always_signatures)
321
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
322
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
323
        self.assertEqual(config.CHECK_ALWAYS,
324
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
325
        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
326
327
    def test_signatures_if_possible(self):
328
        config_file = StringIO(sample_maybe_signatures)
329
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
330
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
331
        self.assertEqual(config.CHECK_IF_POSSIBLE,
332
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
333
        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
334
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
335
    def test_signatures_ignore(self):
336
        config_file = StringIO(sample_ignore_signatures)
337
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
338
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
339
        self.assertEqual(config.CHECK_NEVER,
340
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
341
        self.assertEqual(False, my_config.signature_needed())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
342
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
343
    def _get_sample_config(self):
1534.7.154 by Aaron Bentley
Removed changes from bzr.ab 1529..1536
344
        config_file = StringIO(sample_config_text)
345
        my_config = config.GlobalConfig()
346
        my_config._parser = my_config._get_parser(file=config_file)
347
        return my_config
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
348
1442.1.56 by Robert Collins
gpg_signing_command configuration item
349
    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.
350
        my_config = self._get_sample_config()
1442.1.56 by Robert Collins
gpg_signing_command configuration item
351
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
352
        self.assertEqual(False, my_config.signature_needed())
353
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
354
    def _get_empty_config(self):
355
        config_file = StringIO("")
356
        my_config = config.GlobalConfig()
357
        my_config._parser = my_config._get_parser(file=config_file)
358
        return my_config
359
1442.1.59 by Robert Collins
Add re-sign command to generate a digital signature on a single revision.
360
    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.
361
        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.
362
        self.assertEqual("gpg", my_config.gpg_signing_command())
363
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
364
    def test_get_user_option_default(self):
365
        my_config = self._get_empty_config()
366
        self.assertEqual(None, my_config.get_user_option('no_option'))
367
368
    def test_get_user_option_global(self):
369
        my_config = self._get_sample_config()
370
        self.assertEqual("something",
371
                         my_config.get_user_option('user_global_option'))
1472 by Robert Collins
post commit hook, first pass implementation
372
        
373
    def test_post_commit_default(self):
374
        my_config = self._get_sample_config()
375
        self.assertEqual(None, my_config.post_commit())
376
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
377
    def test_configured_logformat(self):
1553.2.8 by Erik Bågfors
tests for config log_formatter
378
        my_config = self._get_sample_config()
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
379
        self.assertEqual("short", my_config.log_format())
1553.2.8 by Erik Bågfors
tests for config log_formatter
380
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
381
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
382
class TestLocationConfig(TestCase):
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
383
384
    def test_constructs(self):
385
        my_config = config.LocationConfig('http://example.com')
386
        self.assertRaises(TypeError, config.LocationConfig)
387
388
    def test_branch_calls_read_filenames(self):
1474 by Robert Collins
Merge from Aaron Bentley.
389
        # This is testing the correct file names are provided.
390
        # TODO: consolidate with the test for GlobalConfigs filename checks.
391
        #
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
392
        # replace the class that is constructured, to check its parameters
1474 by Robert Collins
Merge from Aaron Bentley.
393
        oldparserclass = config.ConfigObj
394
        config.ConfigObj = InstrumentedConfigObj
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
395
        my_config = config.LocationConfig('http://www.example.com')
396
        try:
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
397
            parser = my_config._get_parser()
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
398
        finally:
1474 by Robert Collins
Merge from Aaron Bentley.
399
            config.ConfigObj = oldparserclass
400
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
401
        self.assertEqual(parser._calls,
402
                         [('__init__', config.branches_config_filename())])
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
403
404
    def test_get_global_config(self):
405
        my_config = config.LocationConfig('http://example.com')
406
        global_config = my_config._get_global_config()
407
        self.failUnless(isinstance(global_config, config.GlobalConfig))
408
        self.failUnless(global_config is my_config._get_global_config())
409
410
    def test__get_section_no_match(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
411
        self.get_location_config('/')
412
        self.assertEqual(None, self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
413
        
414
    def test__get_section_exact(self):
1442.1.9 by Robert Collins
exact section test passes
415
        self.get_location_config('http://www.example.com')
416
        self.assertEqual('http://www.example.com',
417
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
418
   
419
    def test__get_section_suffix_does_not(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
420
        self.get_location_config('http://www.example.com-com')
421
        self.assertEqual(None, self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
422
423
    def test__get_section_subdir_recursive(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
424
        self.get_location_config('http://www.example.com/com')
425
        self.assertEqual('http://www.example.com',
426
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
427
428
    def test__get_section_subdir_matches(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
429
        self.get_location_config('http://www.example.com/useglobal')
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
430
        self.assertEqual('http://www.example.com/useglobal',
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
431
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
432
433
    def test__get_section_subdir_nonrecursive(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
434
        self.get_location_config(
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
435
            'http://www.example.com/useglobal/childbranch')
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
436
        self.assertEqual('http://www.example.com',
437
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
438
439
    def test__get_section_subdir_trailing_slash(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
440
        self.get_location_config('/b')
441
        self.assertEqual('/b/', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
442
443
    def test__get_section_subdir_child(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
444
        self.get_location_config('/a/foo')
445
        self.assertEqual('/a/*', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
446
447
    def test__get_section_subdir_child_child(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
448
        self.get_location_config('/a/foo/bar')
449
        self.assertEqual('/a/', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
450
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
451
    def test__get_section_trailing_slash_with_children(self):
452
        self.get_location_config('/a/')
453
        self.assertEqual('/a/', self.my_config._get_section())
454
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
455
    def test__get_section_explicit_over_glob(self):
1442.1.10 by Robert Collins
explicit over glob test passes
456
        self.get_location_config('/a/c')
457
        self.assertEqual('/a/c', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
458
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
459
    def get_location_config(self, location, global_config=None):
460
        if global_config is None:
461
            global_file = StringIO(sample_config_text)
462
        else:
463
            global_file = StringIO(global_config)
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
464
        branches_file = StringIO(sample_branches_text)
465
        self.my_config = config.LocationConfig(location)
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
466
        self.my_config._get_parser(branches_file)
467
        self.my_config._get_global_config()._get_parser(global_file)
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
468
469
    def test_location_without_username(self):
470
        self.get_location_config('http://www.example.com/useglobal')
471
        self.assertEqual('Robert Collins <robertc@example.com>',
472
                         self.my_config.username())
473
474
    def test_location_not_listed(self):
475
        self.get_location_config('/home/robertc/sources')
476
        self.assertEqual('Robert Collins <robertc@example.com>',
477
                         self.my_config.username())
478
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
479
    def test_overriding_location(self):
480
        self.get_location_config('http://www.example.com/foo')
481
        self.assertEqual('Robert Collins <robertc@example.org>',
482
                         self.my_config.username())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
483
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
484
    def test_signatures_not_set(self):
485
        self.get_location_config('http://www.example.com',
486
                                 global_config=sample_ignore_signatures)
487
        self.assertEqual(config.CHECK_NEVER,
488
                         self.my_config.signature_checking())
489
490
    def test_signatures_never(self):
491
        self.get_location_config('/a/c')
492
        self.assertEqual(config.CHECK_NEVER,
493
                         self.my_config.signature_checking())
494
        
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
495
    def test_signatures_when_available(self):
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
496
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
497
        self.assertEqual(config.CHECK_IF_POSSIBLE,
498
                         self.my_config.signature_checking())
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
499
        
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
500
    def test_signatures_always(self):
501
        self.get_location_config('/b')
502
        self.assertEqual(config.CHECK_ALWAYS,
503
                         self.my_config.signature_checking())
504
        
1442.1.56 by Robert Collins
gpg_signing_command configuration item
505
    def test_gpg_signing_command(self):
506
        self.get_location_config('/b')
507
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
508
509
    def test_gpg_signing_command_missing(self):
510
        self.get_location_config('/a')
511
        self.assertEqual("false", self.my_config.gpg_signing_command())
512
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
513
    def test_get_user_option_global(self):
514
        self.get_location_config('/a')
515
        self.assertEqual('something',
516
                         self.my_config.get_user_option('user_global_option'))
517
518
    def test_get_user_option_local(self):
519
        self.get_location_config('/a')
520
        self.assertEqual('local',
521
                         self.my_config.get_user_option('user_local_option'))
1472 by Robert Collins
post commit hook, first pass implementation
522
        
523
    def test_post_commit_default(self):
524
        self.get_location_config('/a/c')
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
525
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1472 by Robert Collins
post commit hook, first pass implementation
526
                         self.my_config.post_commit())
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
527
1502 by Robert Collins
Bugfix the config test suite to not create .bazaar in the dir where it is run.
528
529
class TestLocationConfig(TestCaseInTempDir):
530
531
    def get_location_config(self, location, global_config=None):
532
        if global_config is None:
533
            global_file = StringIO(sample_config_text)
534
        else:
535
            global_file = StringIO(global_config)
536
        branches_file = StringIO(sample_branches_text)
537
        self.my_config = config.LocationConfig(location)
538
        self.my_config._get_parser(branches_file)
539
        self.my_config._get_global_config()._get_parser(global_file)
540
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
541
    def test_set_user_setting_sets_and_saves(self):
542
        self.get_location_config('/a/c')
543
        record = InstrumentedConfigObj("foo")
544
        self.my_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
545
546
        real_mkdir = os.mkdir
547
        self.created = False
548
        def checked_mkdir(path, mode=0777):
549
            self.log('making directory: %s', path)
550
            real_mkdir(path, mode)
551
            self.created = True
552
553
        os.mkdir = checked_mkdir
554
        try:
555
            self.my_config.set_user_option('foo', 'bar')
556
        finally:
557
            os.mkdir = real_mkdir
558
559
        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.
560
        self.assertEqual([('__contains__', '/a/c'),
561
                          ('__contains__', '/a/c/'),
562
                          ('__setitem__', '/a/c', {}),
563
                          ('__getitem__', '/a/c'),
564
                          ('__setitem__', 'foo', 'bar'),
565
                          ('write',)],
566
                         record._calls[1:])
567
1185.62.7 by John Arbash Meinel
Whitespace cleanup.
568
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
569
class TestBranchConfigItems(TestCase):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
570
571
    def test_user_id(self):
572
        branch = FakeBranch()
573
        my_config = config.BranchConfig(branch)
574
        self.assertEqual("Robert Collins <robertc@example.net>",
575
                         my_config._get_user_id())
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
576
        branch.control_files.email = "John"
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
577
        self.assertEqual("John", my_config._get_user_id())
578
579
    def test_not_set_in_branch(self):
580
        branch = FakeBranch()
581
        my_config = config.BranchConfig(branch)
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
582
        branch.control_files.email = None
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
583
        config_file = StringIO(sample_config_text)
584
        (my_config._get_location_config().
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
585
            _get_global_config()._get_parser(config_file))
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
586
        self.assertEqual("Robert Collins <robertc@example.com>",
587
                         my_config._get_user_id())
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
588
        branch.control_files.email = "John"
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
589
        self.assertEqual("John", my_config._get_user_id())
590
591
    def test_BZREMAIL_OVERRIDES(self):
592
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
593
        branch = FakeBranch()
594
        my_config = config.BranchConfig(branch)
595
        self.assertEqual("Robert Collins <robertc@example.org>",
596
                         my_config.username())
597
    
1442.1.19 by Robert Collins
BranchConfigs inherit signature_checking policy from their LocationConfig.
598
    def test_signatures_forced(self):
599
        branch = FakeBranch()
600
        my_config = config.BranchConfig(branch)
601
        config_file = StringIO(sample_always_signatures)
602
        (my_config._get_location_config().
603
            _get_global_config()._get_parser(config_file))
604
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
1442.1.56 by Robert Collins
gpg_signing_command configuration item
605
606
    def test_gpg_signing_command(self):
607
        branch = FakeBranch()
608
        my_config = config.BranchConfig(branch)
609
        config_file = StringIO(sample_config_text)
610
        (my_config._get_location_config().
611
            _get_global_config()._get_parser(config_file))
612
        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.
613
614
    def test_get_user_option_global(self):
615
        branch = FakeBranch()
616
        my_config = config.BranchConfig(branch)
617
        config_file = StringIO(sample_config_text)
618
        (my_config._get_location_config().
619
            _get_global_config()._get_parser(config_file))
620
        self.assertEqual('something',
621
                         my_config.get_user_option('user_global_option'))
1472 by Robert Collins
post commit hook, first pass implementation
622
623
    def test_post_commit_default(self):
624
        branch = FakeBranch()
625
        branch.base='/a/c'
626
        my_config = config.BranchConfig(branch)
627
        config_file = StringIO(sample_config_text)
628
        (my_config._get_location_config().
629
            _get_global_config()._get_parser(config_file))
630
        branch_file = StringIO(sample_branches_text)
631
        my_config._get_location_config()._get_parser(branch_file)
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
632
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1472 by Robert Collins
post commit hook, first pass implementation
633
                         my_config.post_commit())
1185.33.31 by Martin Pool
Make annotate cope better with revisions committed without a valid
634
635
636
class TestMailAddressExtraction(TestCase):
637
638
    def test_extract_email_address(self):
639
        self.assertEqual('jane@test.com',
640
                         config.extract_email_address('Jane <jane@test.com>'))
641
        self.assertRaises(errors.BzrError,
642
                          config.extract_email_address, 'Jane Tester')