/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
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
20
from ConfigParser import ConfigParser
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"
34
                      "gpg_signing_command=gnome-gpg\n")
35
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
36
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
37
sample_always_signatures = ("[DEFAULT]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
38
                            "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
39
40
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
41
sample_ignore_signatures = ("[DEFAULT]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
42
                            "check_signatures=ignore\n")
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
43
44
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
45
sample_maybe_signatures = ("[DEFAULT]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
46
                            "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
47
48
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
49
sample_branches_text = ("[http://www.example.com]\n"
50
                        "# Top level policy\n"
51
                        "email=Robert Collins <robertc@example.org>\n"
52
                        "[http://www.example.com/useglobal]\n"
53
                        "# different project, forces global lookup\n"
54
                        "recurse=false\n"
55
                        "[/b/]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
56
                        "check_signatures=require\n"
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
57
                        "# test trailing / matching with no children\n"
58
                        "[/a/]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
59
                        "check_signatures=check-available\n"
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
60
                        "# test trailing / matching\n"
61
                        "[/a/*]\n"
62
                        "#subdirs will match but not the parent\n"
63
                        "recurse=False\n"
64
                        "[/a/c]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
65
                        "check_signatures=ignore\n"
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
66
                        "#testing explicit beats globs\n")
67
68
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
69
class InstrumentedConfigParser(object):
70
    """A config parser look-enough-alike to record calls made to it."""
71
72
    def __init__(self):
73
        self._calls = []
74
75
    def read(self, filenames):
76
        self._calls.append(('read', filenames))
77
78
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
79
class FakeBranch(object):
80
81
    def __init__(self):
82
        self.base = "http://example.com/branches/demo"
83
        self.email = 'Robert Collins <robertc@example.net>\n'
84
85
    def controlfile(self, filename, mode):
86
        if filename != 'email':
87
            raise NotImplementedError
88
        if self.email is not None:
89
            return StringIO(self.email)
90
        raise errors.NoSuchFile
91
92
93
class InstrumentedConfig(config.Config):
94
    """An instrumented config that supplies stubs for template methods."""
95
    
96
    def __init__(self):
97
        super(InstrumentedConfig, self).__init__()
98
        self._calls = []
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
99
        self._signatures = config.CHECK_NEVER
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
100
101
    def _get_user_id(self):
102
        self._calls.append('_get_user_id')
103
        return "Robert Collins <robert.collins@example.org>"
104
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
105
    def _get_signature_checking(self):
106
        self._calls.append('_get_signature_checking')
107
        return self._signatures
108
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
109
110
class TestConfig(TestCase):
111
112
    def test_constructs(self):
113
        config.Config()
114
 
115
    def test_no_default_editor(self):
116
        self.assertRaises(NotImplementedError, config.Config().get_editor)
117
118
    def test_user_email(self):
119
        my_config = InstrumentedConfig()
120
        self.assertEqual('robert.collins@example.org', my_config.user_email())
121
        self.assertEqual(['_get_user_id'], my_config._calls)
122
123
    def test_username(self):
124
        my_config = InstrumentedConfig()
125
        self.assertEqual('Robert Collins <robert.collins@example.org>',
126
                         my_config.username())
127
        self.assertEqual(['_get_user_id'], my_config._calls)
1442.1.14 by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE
128
129
    def test_signatures_default(self):
130
        my_config = config.Config()
131
        self.assertEqual(config.CHECK_IF_POSSIBLE,
132
                         my_config.signature_checking())
133
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
134
    def test_signatures_template_method(self):
135
        my_config = InstrumentedConfig()
136
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
137
        self.assertEqual(['_get_signature_checking'], my_config._calls)
138
139
    def test_signatures_template_method_none(self):
140
        my_config = InstrumentedConfig()
141
        my_config._signatures = None
142
        self.assertEqual(config.CHECK_IF_POSSIBLE,
143
                         my_config.signature_checking())
144
        self.assertEqual(['_get_signature_checking'], my_config._calls)
145
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
146
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
147
class TestConfigPath(TestCase):
148
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
149
    def setUp(self):
150
        super(TestConfigPath, self).setUp()
151
        self.oldenv = os.environ.get('HOME', None)
152
        os.environ['HOME'] = '/home/bogus'
153
154
    def tearDown(self):
155
        os.environ['HOME'] = self.oldenv
156
    
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
157
    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.
158
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
159
160
    def test_config_filename(self):
161
        self.assertEqual(config.config_filename(),
162
                         '/home/bogus/.bazaar/bazaar.conf')
163
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
164
    def test_branches_config_filename(self):
165
        self.assertEqual(config.branches_config_filename(),
166
                         '/home/bogus/.bazaar/branches.conf')
167
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
168
class TestIniConfig(TestCase):
169
170
    def test_contructs(self):
171
        my_config = config.IniBasedConfig("nothing")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
172
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
173
    def test_from_fp(self):
1185.12.49 by Aaron Bentley
Switched to ConfigObj
174
        print "Skip ConfigParser-specific test"
175
        return
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
        config_file = StringIO(sample_config_text)
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
177
        my_config = config.IniBasedConfig(None)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
178
        self.failUnless(
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
179
            isinstance(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.
180
                        ConfigParser))
181
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
182
    def test_cached(self):
183
        config_file = StringIO(sample_config_text)
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
184
        my_config = config.IniBasedConfig(None)
185
        parser = my_config._get_parser(file=config_file)
186
        self.failUnless(my_config._get_parser() is parser)
187
188
189
190
class TestGetConfig(TestCase):
191
192
    def test_constructs(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
193
        my_config = config.GlobalConfig()
194
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
195
    def test_calls_read_filenames(self):
1185.12.49 by Aaron Bentley
Switched to ConfigObj
196
        print "Skip ConfigParser-specific test"
197
        return
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
198
        # replace the class that is constructured, to check its parameters
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
199
        oldparserclass = config.ConfigParser
200
        config.ConfigParser = InstrumentedConfigParser
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
201
        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.
202
        try:
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
203
            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.
204
        finally:
205
            config.ConfigParser = oldparserclass
206
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
207
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
208
209
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
210
class TestBranchConfig(TestCaseInTempDir):
211
212
    def test_constructs(self):
213
        branch = FakeBranch()
214
        my_config = config.BranchConfig(branch)
215
        self.assertRaises(TypeError, config.BranchConfig)
216
217
    def test_get_location_config(self):
218
        branch = FakeBranch()
219
        my_config = config.BranchConfig(branch)
220
        location_config = my_config._get_location_config()
221
        self.assertEqual(branch.base, location_config.location)
222
        self.failUnless(location_config is my_config._get_location_config())
223
224
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
class TestConfigItems(TestCase):
226
227
    def setUp(self):
228
        super(TestConfigItems, self).setUp()
229
        self.bzr_email = os.environ.get('BZREMAIL')
230
        if self.bzr_email is not None:
231
            del os.environ['BZREMAIL']
232
        self.email = os.environ.get('EMAIL')
233
        if self.email is not None:
234
            del os.environ['EMAIL']
235
        self.oldenv = os.environ.get('HOME', None)
236
        os.environ['HOME'] = os.getcwd()
237
238
    def tearDown(self):
239
        os.environ['HOME'] = self.oldenv
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
240
        if os.environ.get('BZREMAIL') is not None:
241
            del os.environ['BZREMAIL']
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
        if self.bzr_email is not None:
1185.16.30 by mbp at sourcefrog
- fix broken config test
243
            os.environ['BZREMAIL'] = self.bzr_email
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
244
        if self.email is not None:
1185.16.30 by mbp at sourcefrog
- fix broken config test
245
            os.environ['EMAIL'] = self.email
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
246
        super(TestConfigItems, self).tearDown()
247
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
248
249
class TestGlobalConfigItems(TestConfigItems):
250
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
251
    def test_user_id(self):
252
        config_file = StringIO(sample_config_text)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
253
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
254
        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.
255
        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
256
                         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.
257
258
    def test_absent_user_id(self):
259
        config_file = StringIO("")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
260
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
261
        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
262
        self.assertEqual(None, my_config._get_user_id())
263
264
    def test_configured_editor(self):
265
        config_file = StringIO(sample_config_text)
266
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
267
        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
268
        self.assertEqual("vim", my_config.get_editor())
269
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
270
    def test_signatures_always(self):
271
        config_file = StringIO(sample_always_signatures)
272
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
273
        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
274
        self.assertEqual(config.CHECK_ALWAYS,
275
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
276
        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
277
278
    def test_signatures_if_possible(self):
279
        config_file = StringIO(sample_maybe_signatures)
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.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
282
        self.assertEqual(config.CHECK_IF_POSSIBLE,
283
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
284
        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
285
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
286
    def test_signatures_ignore(self):
287
        config_file = StringIO(sample_ignore_signatures)
288
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
289
        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
290
        self.assertEqual(config.CHECK_NEVER,
291
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
292
        self.assertEqual(False, my_config.signature_needed())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
293
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
294
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
295
class TestLocationConfig(TestConfigItems):
296
297
    def test_constructs(self):
298
        my_config = config.LocationConfig('http://example.com')
299
        self.assertRaises(TypeError, config.LocationConfig)
300
301
    def test_branch_calls_read_filenames(self):
302
        # replace the class that is constructured, to check its parameters
1185.12.49 by Aaron Bentley
Switched to ConfigObj
303
        print "Skip ConfigParser-specific test"
304
        return
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
305
        oldparserclass = config.ConfigParser
306
        config.ConfigParser = InstrumentedConfigParser
307
        my_config = config.LocationConfig('http://www.example.com')
308
        try:
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
309
            parser = my_config._get_parser()
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
310
        finally:
311
            config.ConfigParser = oldparserclass
312
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
313
        self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
314
315
    def test_get_global_config(self):
316
        my_config = config.LocationConfig('http://example.com')
317
        global_config = my_config._get_global_config()
318
        self.failUnless(isinstance(global_config, config.GlobalConfig))
319
        self.failUnless(global_config is my_config._get_global_config())
320
321
    def test__get_section_no_match(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
322
        self.get_location_config('/')
323
        self.assertEqual(None, self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
324
        
325
    def test__get_section_exact(self):
1442.1.9 by Robert Collins
exact section test passes
326
        self.get_location_config('http://www.example.com')
327
        self.assertEqual('http://www.example.com',
328
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
329
   
330
    def test__get_section_suffix_does_not(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
331
        self.get_location_config('http://www.example.com-com')
332
        self.assertEqual(None, self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
333
334
    def test__get_section_subdir_recursive(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
335
        self.get_location_config('http://www.example.com/com')
336
        self.assertEqual('http://www.example.com',
337
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
338
339
    def test__get_section_subdir_matches(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
340
        self.get_location_config('http://www.example.com/useglobal')
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
341
        self.assertEqual('http://www.example.com/useglobal',
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
342
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
343
344
    def test__get_section_subdir_nonrecursive(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
345
        self.get_location_config(
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
346
            'http://www.example.com/useglobal/childbranch')
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
347
        self.assertEqual('http://www.example.com',
348
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
349
350
    def test__get_section_subdir_trailing_slash(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
351
        self.get_location_config('/b')
352
        self.assertEqual('/b/', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
353
354
    def test__get_section_subdir_child(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
355
        self.get_location_config('/a/foo')
356
        self.assertEqual('/a/*', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
357
358
    def test__get_section_subdir_child_child(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
359
        self.get_location_config('/a/foo/bar')
360
        self.assertEqual('/a/', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
361
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
362
    def test__get_section_trailing_slash_with_children(self):
363
        self.get_location_config('/a/')
364
        self.assertEqual('/a/', self.my_config._get_section())
365
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
366
    def test__get_section_explicit_over_glob(self):
1442.1.10 by Robert Collins
explicit over glob test passes
367
        self.get_location_config('/a/c')
368
        self.assertEqual('/a/c', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
369
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
370
    def get_location_config(self, location, global_config=None):
371
        if global_config is None:
372
            global_file = StringIO(sample_config_text)
373
        else:
374
            global_file = StringIO(global_config)
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
375
        branches_file = StringIO(sample_branches_text)
376
        self.my_config = config.LocationConfig(location)
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
377
        self.my_config._get_parser(branches_file)
378
        self.my_config._get_global_config()._get_parser(global_file)
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
379
380
    def test_location_without_username(self):
381
        self.get_location_config('http://www.example.com/useglobal')
382
        self.assertEqual('Robert Collins <robertc@example.com>',
383
                         self.my_config.username())
384
385
    def test_location_not_listed(self):
386
        self.get_location_config('/home/robertc/sources')
387
        self.assertEqual('Robert Collins <robertc@example.com>',
388
                         self.my_config.username())
389
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
390
    def test_overriding_location(self):
391
        self.get_location_config('http://www.example.com/foo')
392
        self.assertEqual('Robert Collins <robertc@example.org>',
393
                         self.my_config.username())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
394
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
395
    def test_signatures_not_set(self):
396
        self.get_location_config('http://www.example.com',
397
                                 global_config=sample_ignore_signatures)
398
        self.assertEqual(config.CHECK_NEVER,
399
                         self.my_config.signature_checking())
400
401
    def test_signatures_never(self):
402
        self.get_location_config('/a/c')
403
        self.assertEqual(config.CHECK_NEVER,
404
                         self.my_config.signature_checking())
405
        
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
406
    def test_signatures_when_available(self):
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
407
        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
408
        self.assertEqual(config.CHECK_IF_POSSIBLE,
409
                         self.my_config.signature_checking())
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
410
        
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
411
    def test_signatures_always(self):
412
        self.get_location_config('/b')
413
        self.assertEqual(config.CHECK_ALWAYS,
414
                         self.my_config.signature_checking())
415
        
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
416
417
class TestBranchConfigItems(TestConfigItems):
418
419
    def test_user_id(self):
420
        branch = FakeBranch()
421
        my_config = config.BranchConfig(branch)
422
        self.assertEqual("Robert Collins <robertc@example.net>",
423
                         my_config._get_user_id())
424
        branch.email = "John"
425
        self.assertEqual("John", my_config._get_user_id())
426
427
    def test_not_set_in_branch(self):
428
        branch = FakeBranch()
429
        my_config = config.BranchConfig(branch)
430
        branch.email = None
431
        config_file = StringIO(sample_config_text)
432
        (my_config._get_location_config().
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
433
            _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
434
        self.assertEqual("Robert Collins <robertc@example.com>",
435
                         my_config._get_user_id())
436
        branch.email = "John"
437
        self.assertEqual("John", my_config._get_user_id())
438
439
    def test_BZREMAIL_OVERRIDES(self):
440
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
441
        branch = FakeBranch()
442
        my_config = config.BranchConfig(branch)
443
        self.assertEqual("Robert Collins <robertc@example.org>",
444
                         my_config.username())
445
    
1442.1.19 by Robert Collins
BranchConfigs inherit signature_checking policy from their LocationConfig.
446
    def test_signatures_forced(self):
447
        branch = FakeBranch()
448
        my_config = config.BranchConfig(branch)
449
        config_file = StringIO(sample_always_signatures)
450
        (my_config._get_location_config().
451
            _get_global_config()._get_parser(config_file))
452
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())