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