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