/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
28
from bzrlib.selftest import TestCase, TestCaseInTempDir
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"
1472 by Robert Collins
post commit hook, first pass implementation
69
                        "post_commit=bzrlib.selftest.testconfig.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)
105
        raise errors.NoSuchFile
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()
178
        self.oldenv = os.environ.get('HOME', None)
179
        os.environ['HOME'] = '/home/bogus'
180
181
    def tearDown(self):
182
        os.environ['HOME'] = self.oldenv
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
183
        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.
184
    
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
185
    def test_config_dir(self):
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
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
187
188
    def test_config_filename(self):
189
        self.assertEqual(config.config_filename(),
190
                         '/home/bogus/.bazaar/bazaar.conf')
191
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
192
    def test_branches_config_filename(self):
193
        self.assertEqual(config.branches_config_filename(),
194
                         '/home/bogus/.bazaar/branches.conf')
195
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
196
class TestIniConfig(TestCase):
197
198
    def test_contructs(self):
199
        my_config = config.IniBasedConfig("nothing")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
200
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
    def test_from_fp(self):
202
        config_file = StringIO(sample_config_text)
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
203
        my_config = config.IniBasedConfig(None)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
204
        self.failUnless(
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
205
            isinstance(my_config._get_parser(file=config_file),
1474 by Robert Collins
Merge from Aaron Bentley.
206
                        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.
207
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
208
    def test_cached(self):
209
        config_file = StringIO(sample_config_text)
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
210
        my_config = config.IniBasedConfig(None)
211
        parser = my_config._get_parser(file=config_file)
212
        self.failUnless(my_config._get_parser() is parser)
213
214
215
class TestGetConfig(TestCase):
216
217
    def test_constructs(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
218
        my_config = config.GlobalConfig()
219
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
220
    def test_calls_read_filenames(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
221
        # replace the class that is constructured, to check its parameters
1474 by Robert Collins
Merge from Aaron Bentley.
222
        oldparserclass = config.ConfigObj
223
        config.ConfigObj = InstrumentedConfigObj
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
224
        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.
225
        try:
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
226
            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.
227
        finally:
1474 by Robert Collins
Merge from Aaron Bentley.
228
            config.ConfigObj = oldparserclass
229
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
230
        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.
231
232
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
233
class TestBranchConfig(TestCaseInTempDir):
234
235
    def test_constructs(self):
236
        branch = FakeBranch()
237
        my_config = config.BranchConfig(branch)
238
        self.assertRaises(TypeError, config.BranchConfig)
239
240
    def test_get_location_config(self):
241
        branch = FakeBranch()
242
        my_config = config.BranchConfig(branch)
243
        location_config = my_config._get_location_config()
244
        self.assertEqual(branch.base, location_config.location)
245
        self.failUnless(location_config is my_config._get_location_config())
246
247
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
248
class TestGlobalConfigItems(TestCase):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
249
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
250
    def test_user_id(self):
251
        config_file = StringIO(sample_config_text)
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.18 by Robert Collins
permit per branch location overriding of signature checking policy
253
        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.
254
        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
255
                         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.
256
257
    def test_absent_user_id(self):
258
        config_file = StringIO("")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
259
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
260
        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
261
        self.assertEqual(None, my_config._get_user_id())
262
263
    def test_configured_editor(self):
264
        config_file = StringIO(sample_config_text)
265
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
266
        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
267
        self.assertEqual("vim", my_config.get_editor())
268
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
269
    def test_signatures_always(self):
270
        config_file = StringIO(sample_always_signatures)
271
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
272
        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
273
        self.assertEqual(config.CHECK_ALWAYS,
274
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
275
        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
276
277
    def test_signatures_if_possible(self):
278
        config_file = StringIO(sample_maybe_signatures)
279
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
280
        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
281
        self.assertEqual(config.CHECK_IF_POSSIBLE,
282
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
283
        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
284
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
285
    def test_signatures_ignore(self):
286
        config_file = StringIO(sample_ignore_signatures)
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.16 by Robert Collins
allow global overriding of signature policy to never check
289
        self.assertEqual(config.CHECK_NEVER,
290
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
291
        self.assertEqual(False, my_config.signature_needed())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
292
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
293
    def _get_sample_config(self):
294
        config_file = StringIO(sample_config_text)
295
        my_config = config.GlobalConfig()
296
        my_config._parser = my_config._get_parser(file=config_file)
297
        return my_config
298
1442.1.56 by Robert Collins
gpg_signing_command configuration item
299
    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.
300
        my_config = self._get_sample_config()
1442.1.56 by Robert Collins
gpg_signing_command configuration item
301
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
302
        self.assertEqual(False, my_config.signature_needed())
303
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
304
    def _get_empty_config(self):
305
        config_file = StringIO("")
306
        my_config = config.GlobalConfig()
307
        my_config._parser = my_config._get_parser(file=config_file)
308
        return my_config
309
1442.1.59 by Robert Collins
Add re-sign command to generate a digital signature on a single revision.
310
    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.
311
        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.
312
        self.assertEqual("gpg", my_config.gpg_signing_command())
313
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
314
    def test_get_user_option_default(self):
315
        my_config = self._get_empty_config()
316
        self.assertEqual(None, my_config.get_user_option('no_option'))
317
318
    def test_get_user_option_global(self):
319
        my_config = self._get_sample_config()
320
        self.assertEqual("something",
321
                         my_config.get_user_option('user_global_option'))
1472 by Robert Collins
post commit hook, first pass implementation
322
        
323
    def test_post_commit_default(self):
324
        my_config = self._get_sample_config()
325
        self.assertEqual(None, my_config.post_commit())
326
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
327
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
328
class TestLocationConfig(TestCase):
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
329
330
    def test_constructs(self):
331
        my_config = config.LocationConfig('http://example.com')
332
        self.assertRaises(TypeError, config.LocationConfig)
333
334
    def test_branch_calls_read_filenames(self):
1474 by Robert Collins
Merge from Aaron Bentley.
335
        # This is testing the correct file names are provided.
336
        # TODO: consolidate with the test for GlobalConfigs filename checks.
337
        #
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
338
        # replace the class that is constructured, to check its parameters
1474 by Robert Collins
Merge from Aaron Bentley.
339
        oldparserclass = config.ConfigObj
340
        config.ConfigObj = InstrumentedConfigObj
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
341
        my_config = config.LocationConfig('http://www.example.com')
342
        try:
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
343
            parser = my_config._get_parser()
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
344
        finally:
1474 by Robert Collins
Merge from Aaron Bentley.
345
            config.ConfigObj = oldparserclass
346
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
347
        self.assertEqual(parser._calls,
348
                         [('__init__', config.branches_config_filename())])
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
349
350
    def test_get_global_config(self):
351
        my_config = config.LocationConfig('http://example.com')
352
        global_config = my_config._get_global_config()
353
        self.failUnless(isinstance(global_config, config.GlobalConfig))
354
        self.failUnless(global_config is my_config._get_global_config())
355
356
    def test__get_section_no_match(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
357
        self.get_location_config('/')
358
        self.assertEqual(None, self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
359
        
360
    def test__get_section_exact(self):
1442.1.9 by Robert Collins
exact section test passes
361
        self.get_location_config('http://www.example.com')
362
        self.assertEqual('http://www.example.com',
363
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
364
   
365
    def test__get_section_suffix_does_not(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
366
        self.get_location_config('http://www.example.com-com')
367
        self.assertEqual(None, self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
368
369
    def test__get_section_subdir_recursive(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
370
        self.get_location_config('http://www.example.com/com')
371
        self.assertEqual('http://www.example.com',
372
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
373
374
    def test__get_section_subdir_matches(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
375
        self.get_location_config('http://www.example.com/useglobal')
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
376
        self.assertEqual('http://www.example.com/useglobal',
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
377
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
378
379
    def test__get_section_subdir_nonrecursive(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
380
        self.get_location_config(
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
381
            'http://www.example.com/useglobal/childbranch')
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
382
        self.assertEqual('http://www.example.com',
383
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
384
385
    def test__get_section_subdir_trailing_slash(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
386
        self.get_location_config('/b')
387
        self.assertEqual('/b/', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
388
389
    def test__get_section_subdir_child(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
390
        self.get_location_config('/a/foo')
391
        self.assertEqual('/a/*', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
392
393
    def test__get_section_subdir_child_child(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
394
        self.get_location_config('/a/foo/bar')
395
        self.assertEqual('/a/', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
396
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
397
    def test__get_section_trailing_slash_with_children(self):
398
        self.get_location_config('/a/')
399
        self.assertEqual('/a/', self.my_config._get_section())
400
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
401
    def test__get_section_explicit_over_glob(self):
1442.1.10 by Robert Collins
explicit over glob test passes
402
        self.get_location_config('/a/c')
403
        self.assertEqual('/a/c', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
404
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
405
    def get_location_config(self, location, global_config=None):
406
        if global_config is None:
407
            global_file = StringIO(sample_config_text)
408
        else:
409
            global_file = StringIO(global_config)
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
410
        branches_file = StringIO(sample_branches_text)
411
        self.my_config = config.LocationConfig(location)
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
412
        self.my_config._get_parser(branches_file)
413
        self.my_config._get_global_config()._get_parser(global_file)
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
414
415
    def test_location_without_username(self):
416
        self.get_location_config('http://www.example.com/useglobal')
417
        self.assertEqual('Robert Collins <robertc@example.com>',
418
                         self.my_config.username())
419
420
    def test_location_not_listed(self):
421
        self.get_location_config('/home/robertc/sources')
422
        self.assertEqual('Robert Collins <robertc@example.com>',
423
                         self.my_config.username())
424
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
425
    def test_overriding_location(self):
426
        self.get_location_config('http://www.example.com/foo')
427
        self.assertEqual('Robert Collins <robertc@example.org>',
428
                         self.my_config.username())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
429
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
430
    def test_signatures_not_set(self):
431
        self.get_location_config('http://www.example.com',
432
                                 global_config=sample_ignore_signatures)
433
        self.assertEqual(config.CHECK_NEVER,
434
                         self.my_config.signature_checking())
435
436
    def test_signatures_never(self):
437
        self.get_location_config('/a/c')
438
        self.assertEqual(config.CHECK_NEVER,
439
                         self.my_config.signature_checking())
440
        
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
441
    def test_signatures_when_available(self):
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
442
        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
443
        self.assertEqual(config.CHECK_IF_POSSIBLE,
444
                         self.my_config.signature_checking())
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
445
        
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
446
    def test_signatures_always(self):
447
        self.get_location_config('/b')
448
        self.assertEqual(config.CHECK_ALWAYS,
449
                         self.my_config.signature_checking())
450
        
1442.1.56 by Robert Collins
gpg_signing_command configuration item
451
    def test_gpg_signing_command(self):
452
        self.get_location_config('/b')
453
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
454
455
    def test_gpg_signing_command_missing(self):
456
        self.get_location_config('/a')
457
        self.assertEqual("false", self.my_config.gpg_signing_command())
458
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
459
    def test_get_user_option_global(self):
460
        self.get_location_config('/a')
461
        self.assertEqual('something',
462
                         self.my_config.get_user_option('user_global_option'))
463
464
    def test_get_user_option_local(self):
465
        self.get_location_config('/a')
466
        self.assertEqual('local',
467
                         self.my_config.get_user_option('user_local_option'))
1472 by Robert Collins
post commit hook, first pass implementation
468
        
469
    def test_post_commit_default(self):
470
        self.get_location_config('/a/c')
471
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
472
                         self.my_config.post_commit())
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
473
1502 by Robert Collins
Bugfix the config test suite to not create .bazaar in the dir where it is run.
474
475
class TestLocationConfig(TestCaseInTempDir):
476
477
    def get_location_config(self, location, global_config=None):
478
        if global_config is None:
479
            global_file = StringIO(sample_config_text)
480
        else:
481
            global_file = StringIO(global_config)
482
        branches_file = StringIO(sample_branches_text)
483
        self.my_config = config.LocationConfig(location)
484
        self.my_config._get_parser(branches_file)
485
        self.my_config._get_global_config()._get_parser(global_file)
486
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
487
    def test_set_user_setting_sets_and_saves(self):
488
        # TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
489
        self.get_location_config('/a/c')
490
        record = InstrumentedConfigObj("foo")
491
        self.my_config._parser = record
1185.33.4 by Martin Pool
Disable selftest that was creating .bazaar in source directory
492
        return
493
        # XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
494
        # broken: creates .bazaar in the top-level directory, not 
495
        # inside the test directory
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
496
        self.my_config.set_user_option('foo', 'bar')
497
        self.assertEqual([('__contains__', '/a/c'),
498
                          ('__contains__', '/a/c/'),
499
                          ('__setitem__', '/a/c', {}),
500
                          ('__getitem__', '/a/c'),
501
                          ('__setitem__', 'foo', 'bar'),
502
                          ('write',)],
503
                         record._calls[1:])
504
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
505
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
506
class TestBranchConfigItems(TestCase):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
507
508
    def test_user_id(self):
509
        branch = FakeBranch()
510
        my_config = config.BranchConfig(branch)
511
        self.assertEqual("Robert Collins <robertc@example.net>",
512
                         my_config._get_user_id())
513
        branch.email = "John"
514
        self.assertEqual("John", my_config._get_user_id())
515
516
    def test_not_set_in_branch(self):
517
        branch = FakeBranch()
518
        my_config = config.BranchConfig(branch)
519
        branch.email = None
520
        config_file = StringIO(sample_config_text)
521
        (my_config._get_location_config().
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
522
            _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
523
        self.assertEqual("Robert Collins <robertc@example.com>",
524
                         my_config._get_user_id())
525
        branch.email = "John"
526
        self.assertEqual("John", my_config._get_user_id())
527
528
    def test_BZREMAIL_OVERRIDES(self):
529
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
530
        branch = FakeBranch()
531
        my_config = config.BranchConfig(branch)
532
        self.assertEqual("Robert Collins <robertc@example.org>",
533
                         my_config.username())
534
    
1442.1.19 by Robert Collins
BranchConfigs inherit signature_checking policy from their LocationConfig.
535
    def test_signatures_forced(self):
536
        branch = FakeBranch()
537
        my_config = config.BranchConfig(branch)
538
        config_file = StringIO(sample_always_signatures)
539
        (my_config._get_location_config().
540
            _get_global_config()._get_parser(config_file))
541
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
1442.1.56 by Robert Collins
gpg_signing_command configuration item
542
543
    def test_gpg_signing_command(self):
544
        branch = FakeBranch()
545
        my_config = config.BranchConfig(branch)
546
        config_file = StringIO(sample_config_text)
547
        (my_config._get_location_config().
548
            _get_global_config()._get_parser(config_file))
549
        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.
550
551
    def test_get_user_option_global(self):
552
        branch = FakeBranch()
553
        my_config = config.BranchConfig(branch)
554
        config_file = StringIO(sample_config_text)
555
        (my_config._get_location_config().
556
            _get_global_config()._get_parser(config_file))
557
        self.assertEqual('something',
558
                         my_config.get_user_option('user_global_option'))
1472 by Robert Collins
post commit hook, first pass implementation
559
560
    def test_post_commit_default(self):
561
        branch = FakeBranch()
562
        branch.base='/a/c'
563
        my_config = config.BranchConfig(branch)
564
        config_file = StringIO(sample_config_text)
565
        (my_config._get_location_config().
566
            _get_global_config()._get_parser(config_file))
567
        branch_file = StringIO(sample_branches_text)
568
        my_config._get_location_config()._get_parser(branch_file)
569
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
570
                         my_config.post_commit())
1185.33.31 by Martin Pool
Make annotate cope better with revisions committed without a valid
571
572
573
class TestMailAddressExtraction(TestCase):
574
575
    def test_extract_email_address(self):
576
        self.assertEqual('jane@test.com',
577
                         config.extract_email_address('Jane <jane@test.com>'))
578
        self.assertRaises(errors.BzrError,
579
                          config.extract_email_address, 'Jane Tester')