~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testconfig.py

  • Committer: Robert Collins
  • Date: 2005-10-17 06:22:23 UTC
  • mto: This revision was merged to the branch mainline in revision 1459.
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051017062223-ef02def7780ccfb7
gpg_signing_command configuration item

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
20
from ConfigParser import ConfigParser
 
21
from cStringIO import StringIO
 
22
import os
 
23
import sys
 
24
 
 
25
#import bzrlib specific imports here
 
26
import bzrlib.config as config
 
27
import bzrlib.errors as errors
 
28
from bzrlib.selftest import TestCase, TestCaseInTempDir
 
29
 
 
30
 
 
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
 
 
36
 
 
37
sample_always_signatures = ("[DEFAULT]\n"
 
38
                            "check_signatures=require\n")
 
39
 
 
40
 
 
41
sample_ignore_signatures = ("[DEFAULT]\n"
 
42
                            "check_signatures=ignore\n")
 
43
 
 
44
 
 
45
sample_maybe_signatures = ("[DEFAULT]\n"
 
46
                            "check_signatures=check-available\n")
 
47
 
 
48
 
 
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"
 
56
                        "check_signatures=require\n"
 
57
                        "# test trailing / matching with no children\n"
 
58
                        "[/a/]\n"
 
59
                        "check_signatures=check-available\n"
 
60
                        "gpg_signing_command=false\n"
 
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"
 
66
                        "check_signatures=ignore\n"
 
67
                        "#testing explicit beats globs\n")
 
68
 
 
69
 
 
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
 
 
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 = []
 
100
        self._signatures = config.CHECK_NEVER
 
101
 
 
102
    def _get_user_id(self):
 
103
        self._calls.append('_get_user_id')
 
104
        return "Robert Collins <robert.collins@example.org>"
 
105
 
 
106
    def _get_signature_checking(self):
 
107
        self._calls.append('_get_signature_checking')
 
108
        return self._signatures
 
109
 
 
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)
 
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
 
 
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
 
 
147
    def test_gpg_signing_command_default(self):
 
148
        my_config = config.Config()
 
149
        self.assertEqual('gpg', my_config.gpg_signing_command())
 
150
 
 
151
 
 
152
class TestConfigPath(TestCase):
 
153
 
 
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
 
161
        super(TestConfigPath, self).tearDown()
 
162
    
 
163
    def test_config_dir(self):
 
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
 
 
170
    def test_branches_config_filename(self):
 
171
        self.assertEqual(config.branches_config_filename(),
 
172
                         '/home/bogus/.bazaar/branches.conf')
 
173
 
 
174
class TestIniConfig(TestCase):
 
175
 
 
176
    def test_contructs(self):
 
177
        my_config = config.IniBasedConfig("nothing")
 
178
 
 
179
    def test_from_fp(self):
 
180
        config_file = StringIO(sample_config_text)
 
181
        my_config = config.IniBasedConfig(None)
 
182
        self.failUnless(
 
183
            isinstance(my_config._get_parser(file=config_file),
 
184
                        ConfigParser))
 
185
 
 
186
    def test_cached(self):
 
187
        config_file = StringIO(sample_config_text)
 
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):
 
196
        my_config = config.GlobalConfig()
 
197
 
 
198
    def test_calls_read_filenames(self):
 
199
        # replace the class that is constructured, to check its parameters
 
200
        oldparserclass = config.ConfigParser
 
201
        config.ConfigParser = InstrumentedConfigParser
 
202
        my_config = config.GlobalConfig()
 
203
        try:
 
204
            parser = my_config._get_parser()
 
205
        finally:
 
206
            config.ConfigParser = oldparserclass
 
207
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
208
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
 
209
 
 
210
 
 
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
 
 
226
class TestGlobalConfigItems(TestCase):
 
227
 
 
228
    def test_user_id(self):
 
229
        config_file = StringIO(sample_config_text)
 
230
        my_config = config.GlobalConfig()
 
231
        my_config._parser = my_config._get_parser(file=config_file)
 
232
        self.assertEqual("Robert Collins <robertc@example.com>",
 
233
                         my_config._get_user_id())
 
234
 
 
235
    def test_absent_user_id(self):
 
236
        config_file = StringIO("")
 
237
        my_config = config.GlobalConfig()
 
238
        my_config._parser = my_config._get_parser(file=config_file)
 
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()
 
244
        my_config._parser = my_config._get_parser(file=config_file)
 
245
        self.assertEqual("vim", my_config.get_editor())
 
246
 
 
247
    def test_signatures_always(self):
 
248
        config_file = StringIO(sample_always_signatures)
 
249
        my_config = config.GlobalConfig()
 
250
        my_config._parser = my_config._get_parser(file=config_file)
 
251
        self.assertEqual(config.CHECK_ALWAYS,
 
252
                         my_config.signature_checking())
 
253
        self.assertEqual(True, my_config.signature_needed())
 
254
 
 
255
    def test_signatures_if_possible(self):
 
256
        config_file = StringIO(sample_maybe_signatures)
 
257
        my_config = config.GlobalConfig()
 
258
        my_config._parser = my_config._get_parser(file=config_file)
 
259
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
260
                         my_config.signature_checking())
 
261
        self.assertEqual(False, my_config.signature_needed())
 
262
 
 
263
    def test_signatures_ignore(self):
 
264
        config_file = StringIO(sample_ignore_signatures)
 
265
        my_config = config.GlobalConfig()
 
266
        my_config._parser = my_config._get_parser(file=config_file)
 
267
        self.assertEqual(config.CHECK_NEVER,
 
268
                         my_config.signature_checking())
 
269
        self.assertEqual(False, my_config.signature_needed())
 
270
 
 
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
 
 
278
 
 
279
class TestLocationConfig(TestCase):
 
280
 
 
281
    def test_constructs(self):
 
282
        my_config = config.LocationConfig('http://example.com')
 
283
        self.assertRaises(TypeError, config.LocationConfig)
 
284
 
 
285
    def test_branch_calls_read_filenames(self):
 
286
        # replace the class that is constructured, to check its parameters
 
287
        oldparserclass = config.ConfigParser
 
288
        config.ConfigParser = InstrumentedConfigParser
 
289
        my_config = config.LocationConfig('http://www.example.com')
 
290
        try:
 
291
            parser = my_config._get_parser()
 
292
        finally:
 
293
            config.ConfigParser = oldparserclass
 
294
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
295
        self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
 
296
 
 
297
    def test_get_global_config(self):
 
298
        my_config = config.LocationConfig('http://example.com')
 
299
        global_config = my_config._get_global_config()
 
300
        self.failUnless(isinstance(global_config, config.GlobalConfig))
 
301
        self.failUnless(global_config is my_config._get_global_config())
 
302
 
 
303
    def test__get_section_no_match(self):
 
304
        self.get_location_config('/')
 
305
        self.assertEqual(None, self.my_config._get_section())
 
306
        
 
307
    def test__get_section_exact(self):
 
308
        self.get_location_config('http://www.example.com')
 
309
        self.assertEqual('http://www.example.com',
 
310
                         self.my_config._get_section())
 
311
   
 
312
    def test__get_section_suffix_does_not(self):
 
313
        self.get_location_config('http://www.example.com-com')
 
314
        self.assertEqual(None, self.my_config._get_section())
 
315
 
 
316
    def test__get_section_subdir_recursive(self):
 
317
        self.get_location_config('http://www.example.com/com')
 
318
        self.assertEqual('http://www.example.com',
 
319
                         self.my_config._get_section())
 
320
 
 
321
    def test__get_section_subdir_matches(self):
 
322
        self.get_location_config('http://www.example.com/useglobal')
 
323
        self.assertEqual('http://www.example.com/useglobal',
 
324
                         self.my_config._get_section())
 
325
 
 
326
    def test__get_section_subdir_nonrecursive(self):
 
327
        self.get_location_config(
 
328
            'http://www.example.com/useglobal/childbranch')
 
329
        self.assertEqual('http://www.example.com',
 
330
                         self.my_config._get_section())
 
331
 
 
332
    def test__get_section_subdir_trailing_slash(self):
 
333
        self.get_location_config('/b')
 
334
        self.assertEqual('/b/', self.my_config._get_section())
 
335
 
 
336
    def test__get_section_subdir_child(self):
 
337
        self.get_location_config('/a/foo')
 
338
        self.assertEqual('/a/*', self.my_config._get_section())
 
339
 
 
340
    def test__get_section_subdir_child_child(self):
 
341
        self.get_location_config('/a/foo/bar')
 
342
        self.assertEqual('/a/', self.my_config._get_section())
 
343
 
 
344
    def test__get_section_trailing_slash_with_children(self):
 
345
        self.get_location_config('/a/')
 
346
        self.assertEqual('/a/', self.my_config._get_section())
 
347
 
 
348
    def test__get_section_explicit_over_glob(self):
 
349
        self.get_location_config('/a/c')
 
350
        self.assertEqual('/a/c', self.my_config._get_section())
 
351
 
 
352
    def get_location_config(self, location, global_config=None):
 
353
        if global_config is None:
 
354
            global_file = StringIO(sample_config_text)
 
355
        else:
 
356
            global_file = StringIO(global_config)
 
357
        branches_file = StringIO(sample_branches_text)
 
358
        self.my_config = config.LocationConfig(location)
 
359
        self.my_config._get_parser(branches_file)
 
360
        self.my_config._get_global_config()._get_parser(global_file)
 
361
 
 
362
    def test_location_without_username(self):
 
363
        self.get_location_config('http://www.example.com/useglobal')
 
364
        self.assertEqual('Robert Collins <robertc@example.com>',
 
365
                         self.my_config.username())
 
366
 
 
367
    def test_location_not_listed(self):
 
368
        self.get_location_config('/home/robertc/sources')
 
369
        self.assertEqual('Robert Collins <robertc@example.com>',
 
370
                         self.my_config.username())
 
371
 
 
372
    def test_overriding_location(self):
 
373
        self.get_location_config('http://www.example.com/foo')
 
374
        self.assertEqual('Robert Collins <robertc@example.org>',
 
375
                         self.my_config.username())
 
376
 
 
377
    def test_signatures_not_set(self):
 
378
        self.get_location_config('http://www.example.com',
 
379
                                 global_config=sample_ignore_signatures)
 
380
        self.assertEqual(config.CHECK_NEVER,
 
381
                         self.my_config.signature_checking())
 
382
 
 
383
    def test_signatures_never(self):
 
384
        self.get_location_config('/a/c')
 
385
        self.assertEqual(config.CHECK_NEVER,
 
386
                         self.my_config.signature_checking())
 
387
        
 
388
    def test_signatures_when_available(self):
 
389
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
 
390
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
391
                         self.my_config.signature_checking())
 
392
        
 
393
    def test_signatures_always(self):
 
394
        self.get_location_config('/b')
 
395
        self.assertEqual(config.CHECK_ALWAYS,
 
396
                         self.my_config.signature_checking())
 
397
        
 
398
    def test_gpg_signing_command(self):
 
399
        self.get_location_config('/b')
 
400
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
 
401
 
 
402
    def test_gpg_signing_command_missing(self):
 
403
        self.get_location_config('/a')
 
404
        self.assertEqual("false", self.my_config.gpg_signing_command())
 
405
 
 
406
 
 
407
class TestBranchConfigItems(TestCase):
 
408
 
 
409
    def test_user_id(self):
 
410
        branch = FakeBranch()
 
411
        my_config = config.BranchConfig(branch)
 
412
        self.assertEqual("Robert Collins <robertc@example.net>",
 
413
                         my_config._get_user_id())
 
414
        branch.email = "John"
 
415
        self.assertEqual("John", my_config._get_user_id())
 
416
 
 
417
    def test_not_set_in_branch(self):
 
418
        branch = FakeBranch()
 
419
        my_config = config.BranchConfig(branch)
 
420
        branch.email = None
 
421
        config_file = StringIO(sample_config_text)
 
422
        (my_config._get_location_config().
 
423
            _get_global_config()._get_parser(config_file))
 
424
        self.assertEqual("Robert Collins <robertc@example.com>",
 
425
                         my_config._get_user_id())
 
426
        branch.email = "John"
 
427
        self.assertEqual("John", my_config._get_user_id())
 
428
 
 
429
    def test_BZREMAIL_OVERRIDES(self):
 
430
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
 
431
        branch = FakeBranch()
 
432
        my_config = config.BranchConfig(branch)
 
433
        self.assertEqual("Robert Collins <robertc@example.org>",
 
434
                         my_config.username())
 
435
    
 
436
    def test_signatures_forced(self):
 
437
        branch = FakeBranch()
 
438
        my_config = config.BranchConfig(branch)
 
439
        config_file = StringIO(sample_always_signatures)
 
440
        (my_config._get_location_config().
 
441
            _get_global_config()._get_parser(config_file))
 
442
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
 
443
 
 
444
    def test_gpg_signing_command(self):
 
445
        branch = FakeBranch()
 
446
        my_config = config.BranchConfig(branch)
 
447
        config_file = StringIO(sample_config_text)
 
448
        (my_config._get_location_config().
 
449
            _get_global_config()._get_parser(config_file))
 
450
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())