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