~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testconfig.py

- rules for using destructors

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
                      "user_global_option=something\n")
 
36
 
 
37
 
 
38
sample_always_signatures = ("[DEFAULT]\n"
 
39
                            "check_signatures=require\n")
 
40
 
 
41
 
 
42
sample_ignore_signatures = ("[DEFAULT]\n"
 
43
                            "check_signatures=ignore\n")
 
44
 
 
45
 
 
46
sample_maybe_signatures = ("[DEFAULT]\n"
 
47
                            "check_signatures=check-available\n")
 
48
 
 
49
 
 
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"
 
57
                        "check_signatures=require\n"
 
58
                        "# test trailing / matching with no children\n"
 
59
                        "[/a/]\n"
 
60
                        "check_signatures=check-available\n"
 
61
                        "gpg_signing_command=false\n"
 
62
                        "user_local_option=local\n"
 
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"
 
68
                        "check_signatures=ignore\n"
 
69
                        "#testing explicit beats globs\n")
 
70
 
 
71
 
 
72
class InstrumentedConfigParser(object):
 
73
    """A config parser look-enough-alike to record calls made to it."""
 
74
 
 
75
    def __init__(self):
 
76
        self._calls = []
 
77
 
 
78
    def read(self, filenames):
 
79
        self._calls.append(('read', filenames))
 
80
 
 
81
 
 
82
class FakeBranch(object):
 
83
 
 
84
    def __init__(self):
 
85
        self.base = "http://example.com/branches/demo"
 
86
        self.email = 'Robert Collins <robertc@example.net>\n'
 
87
 
 
88
    def controlfile(self, filename, mode):
 
89
        if filename != 'email':
 
90
            raise NotImplementedError
 
91
        if self.email is not None:
 
92
            return StringIO(self.email)
 
93
        raise errors.NoSuchFile
 
94
 
 
95
 
 
96
class InstrumentedConfig(config.Config):
 
97
    """An instrumented config that supplies stubs for template methods."""
 
98
    
 
99
    def __init__(self):
 
100
        super(InstrumentedConfig, self).__init__()
 
101
        self._calls = []
 
102
        self._signatures = config.CHECK_NEVER
 
103
 
 
104
    def _get_user_id(self):
 
105
        self._calls.append('_get_user_id')
 
106
        return "Robert Collins <robert.collins@example.org>"
 
107
 
 
108
    def _get_signature_checking(self):
 
109
        self._calls.append('_get_signature_checking')
 
110
        return self._signatures
 
111
 
 
112
 
 
113
class TestConfig(TestCase):
 
114
 
 
115
    def test_constructs(self):
 
116
        config.Config()
 
117
 
 
118
    def test_no_default_editor(self):
 
119
        self.assertRaises(NotImplementedError, config.Config().get_editor)
 
120
 
 
121
    def test_user_email(self):
 
122
        my_config = InstrumentedConfig()
 
123
        self.assertEqual('robert.collins@example.org', my_config.user_email())
 
124
        self.assertEqual(['_get_user_id'], my_config._calls)
 
125
 
 
126
    def test_username(self):
 
127
        my_config = InstrumentedConfig()
 
128
        self.assertEqual('Robert Collins <robert.collins@example.org>',
 
129
                         my_config.username())
 
130
        self.assertEqual(['_get_user_id'], my_config._calls)
 
131
 
 
132
    def test_signatures_default(self):
 
133
        my_config = config.Config()
 
134
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
135
                         my_config.signature_checking())
 
136
 
 
137
    def test_signatures_template_method(self):
 
138
        my_config = InstrumentedConfig()
 
139
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
 
140
        self.assertEqual(['_get_signature_checking'], my_config._calls)
 
141
 
 
142
    def test_signatures_template_method_none(self):
 
143
        my_config = InstrumentedConfig()
 
144
        my_config._signatures = None
 
145
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
146
                         my_config.signature_checking())
 
147
        self.assertEqual(['_get_signature_checking'], my_config._calls)
 
148
 
 
149
    def test_gpg_signing_command_default(self):
 
150
        my_config = config.Config()
 
151
        self.assertEqual('gpg', my_config.gpg_signing_command())
 
152
 
 
153
    def test_get_user_option_default(self):
 
154
        my_config = config.Config()
 
155
        self.assertEqual(None, my_config.get_user_option('no_option'))
 
156
 
 
157
 
 
158
class TestConfigPath(TestCase):
 
159
 
 
160
    def setUp(self):
 
161
        super(TestConfigPath, self).setUp()
 
162
        self.oldenv = os.environ.get('HOME', None)
 
163
        os.environ['HOME'] = '/home/bogus'
 
164
 
 
165
    def tearDown(self):
 
166
        os.environ['HOME'] = self.oldenv
 
167
        super(TestConfigPath, self).tearDown()
 
168
    
 
169
    def test_config_dir(self):
 
170
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
 
171
 
 
172
    def test_config_filename(self):
 
173
        self.assertEqual(config.config_filename(),
 
174
                         '/home/bogus/.bazaar/bazaar.conf')
 
175
 
 
176
    def test_branches_config_filename(self):
 
177
        self.assertEqual(config.branches_config_filename(),
 
178
                         '/home/bogus/.bazaar/branches.conf')
 
179
 
 
180
class TestIniConfig(TestCase):
 
181
 
 
182
    def test_contructs(self):
 
183
        my_config = config.IniBasedConfig("nothing")
 
184
 
 
185
    def test_from_fp(self):
 
186
        config_file = StringIO(sample_config_text)
 
187
        my_config = config.IniBasedConfig(None)
 
188
        self.failUnless(
 
189
            isinstance(my_config._get_parser(file=config_file),
 
190
                        ConfigParser))
 
191
 
 
192
    def test_cached(self):
 
193
        config_file = StringIO(sample_config_text)
 
194
        my_config = config.IniBasedConfig(None)
 
195
        parser = my_config._get_parser(file=config_file)
 
196
        self.failUnless(my_config._get_parser() is parser)
 
197
 
 
198
 
 
199
class TestGetConfig(TestCase):
 
200
 
 
201
    def test_constructs(self):
 
202
        my_config = config.GlobalConfig()
 
203
 
 
204
    def test_calls_read_filenames(self):
 
205
        # replace the class that is constructured, to check its parameters
 
206
        oldparserclass = config.ConfigParser
 
207
        config.ConfigParser = InstrumentedConfigParser
 
208
        my_config = config.GlobalConfig()
 
209
        try:
 
210
            parser = my_config._get_parser()
 
211
        finally:
 
212
            config.ConfigParser = oldparserclass
 
213
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
214
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
 
215
 
 
216
 
 
217
class TestBranchConfig(TestCaseInTempDir):
 
218
 
 
219
    def test_constructs(self):
 
220
        branch = FakeBranch()
 
221
        my_config = config.BranchConfig(branch)
 
222
        self.assertRaises(TypeError, config.BranchConfig)
 
223
 
 
224
    def test_get_location_config(self):
 
225
        branch = FakeBranch()
 
226
        my_config = config.BranchConfig(branch)
 
227
        location_config = my_config._get_location_config()
 
228
        self.assertEqual(branch.base, location_config.location)
 
229
        self.failUnless(location_config is my_config._get_location_config())
 
230
 
 
231
 
 
232
class TestGlobalConfigItems(TestCase):
 
233
 
 
234
    def test_user_id(self):
 
235
        config_file = StringIO(sample_config_text)
 
236
        my_config = config.GlobalConfig()
 
237
        my_config._parser = my_config._get_parser(file=config_file)
 
238
        self.assertEqual("Robert Collins <robertc@example.com>",
 
239
                         my_config._get_user_id())
 
240
 
 
241
    def test_absent_user_id(self):
 
242
        config_file = StringIO("")
 
243
        my_config = config.GlobalConfig()
 
244
        my_config._parser = my_config._get_parser(file=config_file)
 
245
        self.assertEqual(None, my_config._get_user_id())
 
246
 
 
247
    def test_configured_editor(self):
 
248
        config_file = StringIO(sample_config_text)
 
249
        my_config = config.GlobalConfig()
 
250
        my_config._parser = my_config._get_parser(file=config_file)
 
251
        self.assertEqual("vim", my_config.get_editor())
 
252
 
 
253
    def test_signatures_always(self):
 
254
        config_file = StringIO(sample_always_signatures)
 
255
        my_config = config.GlobalConfig()
 
256
        my_config._parser = my_config._get_parser(file=config_file)
 
257
        self.assertEqual(config.CHECK_ALWAYS,
 
258
                         my_config.signature_checking())
 
259
        self.assertEqual(True, my_config.signature_needed())
 
260
 
 
261
    def test_signatures_if_possible(self):
 
262
        config_file = StringIO(sample_maybe_signatures)
 
263
        my_config = config.GlobalConfig()
 
264
        my_config._parser = my_config._get_parser(file=config_file)
 
265
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
266
                         my_config.signature_checking())
 
267
        self.assertEqual(False, my_config.signature_needed())
 
268
 
 
269
    def test_signatures_ignore(self):
 
270
        config_file = StringIO(sample_ignore_signatures)
 
271
        my_config = config.GlobalConfig()
 
272
        my_config._parser = my_config._get_parser(file=config_file)
 
273
        self.assertEqual(config.CHECK_NEVER,
 
274
                         my_config.signature_checking())
 
275
        self.assertEqual(False, my_config.signature_needed())
 
276
 
 
277
    def _get_sample_config(self):
 
278
        config_file = StringIO(sample_config_text)
 
279
        my_config = config.GlobalConfig()
 
280
        my_config._parser = my_config._get_parser(file=config_file)
 
281
        return my_config
 
282
 
 
283
    def test_gpg_signing_command(self):
 
284
        my_config = self._get_sample_config()
 
285
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
 
286
        self.assertEqual(False, my_config.signature_needed())
 
287
 
 
288
    def _get_empty_config(self):
 
289
        config_file = StringIO("")
 
290
        my_config = config.GlobalConfig()
 
291
        my_config._parser = my_config._get_parser(file=config_file)
 
292
        return my_config
 
293
 
 
294
    def test_gpg_signing_command_unset(self):
 
295
        my_config = self._get_empty_config()
 
296
        self.assertEqual("gpg", my_config.gpg_signing_command())
 
297
 
 
298
    def test_get_user_option_default(self):
 
299
        my_config = self._get_empty_config()
 
300
        self.assertEqual(None, my_config.get_user_option('no_option'))
 
301
 
 
302
    def test_get_user_option_global(self):
 
303
        my_config = self._get_sample_config()
 
304
        self.assertEqual("something",
 
305
                         my_config.get_user_option('user_global_option'))
 
306
 
 
307
 
 
308
class TestLocationConfig(TestCase):
 
309
 
 
310
    def test_constructs(self):
 
311
        my_config = config.LocationConfig('http://example.com')
 
312
        self.assertRaises(TypeError, config.LocationConfig)
 
313
 
 
314
    def test_branch_calls_read_filenames(self):
 
315
        # replace the class that is constructured, to check its parameters
 
316
        oldparserclass = config.ConfigParser
 
317
        config.ConfigParser = InstrumentedConfigParser
 
318
        my_config = config.LocationConfig('http://www.example.com')
 
319
        try:
 
320
            parser = my_config._get_parser()
 
321
        finally:
 
322
            config.ConfigParser = oldparserclass
 
323
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
324
        self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
 
325
 
 
326
    def test_get_global_config(self):
 
327
        my_config = config.LocationConfig('http://example.com')
 
328
        global_config = my_config._get_global_config()
 
329
        self.failUnless(isinstance(global_config, config.GlobalConfig))
 
330
        self.failUnless(global_config is my_config._get_global_config())
 
331
 
 
332
    def test__get_section_no_match(self):
 
333
        self.get_location_config('/')
 
334
        self.assertEqual(None, self.my_config._get_section())
 
335
        
 
336
    def test__get_section_exact(self):
 
337
        self.get_location_config('http://www.example.com')
 
338
        self.assertEqual('http://www.example.com',
 
339
                         self.my_config._get_section())
 
340
   
 
341
    def test__get_section_suffix_does_not(self):
 
342
        self.get_location_config('http://www.example.com-com')
 
343
        self.assertEqual(None, self.my_config._get_section())
 
344
 
 
345
    def test__get_section_subdir_recursive(self):
 
346
        self.get_location_config('http://www.example.com/com')
 
347
        self.assertEqual('http://www.example.com',
 
348
                         self.my_config._get_section())
 
349
 
 
350
    def test__get_section_subdir_matches(self):
 
351
        self.get_location_config('http://www.example.com/useglobal')
 
352
        self.assertEqual('http://www.example.com/useglobal',
 
353
                         self.my_config._get_section())
 
354
 
 
355
    def test__get_section_subdir_nonrecursive(self):
 
356
        self.get_location_config(
 
357
            'http://www.example.com/useglobal/childbranch')
 
358
        self.assertEqual('http://www.example.com',
 
359
                         self.my_config._get_section())
 
360
 
 
361
    def test__get_section_subdir_trailing_slash(self):
 
362
        self.get_location_config('/b')
 
363
        self.assertEqual('/b/', self.my_config._get_section())
 
364
 
 
365
    def test__get_section_subdir_child(self):
 
366
        self.get_location_config('/a/foo')
 
367
        self.assertEqual('/a/*', self.my_config._get_section())
 
368
 
 
369
    def test__get_section_subdir_child_child(self):
 
370
        self.get_location_config('/a/foo/bar')
 
371
        self.assertEqual('/a/', self.my_config._get_section())
 
372
 
 
373
    def test__get_section_trailing_slash_with_children(self):
 
374
        self.get_location_config('/a/')
 
375
        self.assertEqual('/a/', self.my_config._get_section())
 
376
 
 
377
    def test__get_section_explicit_over_glob(self):
 
378
        self.get_location_config('/a/c')
 
379
        self.assertEqual('/a/c', self.my_config._get_section())
 
380
 
 
381
    def get_location_config(self, location, global_config=None):
 
382
        if global_config is None:
 
383
            global_file = StringIO(sample_config_text)
 
384
        else:
 
385
            global_file = StringIO(global_config)
 
386
        branches_file = StringIO(sample_branches_text)
 
387
        self.my_config = config.LocationConfig(location)
 
388
        self.my_config._get_parser(branches_file)
 
389
        self.my_config._get_global_config()._get_parser(global_file)
 
390
 
 
391
    def test_location_without_username(self):
 
392
        self.get_location_config('http://www.example.com/useglobal')
 
393
        self.assertEqual('Robert Collins <robertc@example.com>',
 
394
                         self.my_config.username())
 
395
 
 
396
    def test_location_not_listed(self):
 
397
        self.get_location_config('/home/robertc/sources')
 
398
        self.assertEqual('Robert Collins <robertc@example.com>',
 
399
                         self.my_config.username())
 
400
 
 
401
    def test_overriding_location(self):
 
402
        self.get_location_config('http://www.example.com/foo')
 
403
        self.assertEqual('Robert Collins <robertc@example.org>',
 
404
                         self.my_config.username())
 
405
 
 
406
    def test_signatures_not_set(self):
 
407
        self.get_location_config('http://www.example.com',
 
408
                                 global_config=sample_ignore_signatures)
 
409
        self.assertEqual(config.CHECK_NEVER,
 
410
                         self.my_config.signature_checking())
 
411
 
 
412
    def test_signatures_never(self):
 
413
        self.get_location_config('/a/c')
 
414
        self.assertEqual(config.CHECK_NEVER,
 
415
                         self.my_config.signature_checking())
 
416
        
 
417
    def test_signatures_when_available(self):
 
418
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
 
419
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
420
                         self.my_config.signature_checking())
 
421
        
 
422
    def test_signatures_always(self):
 
423
        self.get_location_config('/b')
 
424
        self.assertEqual(config.CHECK_ALWAYS,
 
425
                         self.my_config.signature_checking())
 
426
        
 
427
    def test_gpg_signing_command(self):
 
428
        self.get_location_config('/b')
 
429
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
 
430
 
 
431
    def test_gpg_signing_command_missing(self):
 
432
        self.get_location_config('/a')
 
433
        self.assertEqual("false", self.my_config.gpg_signing_command())
 
434
 
 
435
    def test_get_user_option_global(self):
 
436
        self.get_location_config('/a')
 
437
        self.assertEqual('something',
 
438
                         self.my_config.get_user_option('user_global_option'))
 
439
 
 
440
    def test_get_user_option_local(self):
 
441
        self.get_location_config('/a')
 
442
        self.assertEqual('local',
 
443
                         self.my_config.get_user_option('user_local_option'))
 
444
 
 
445
 
 
446
class TestBranchConfigItems(TestCase):
 
447
 
 
448
    def test_user_id(self):
 
449
        branch = FakeBranch()
 
450
        my_config = config.BranchConfig(branch)
 
451
        self.assertEqual("Robert Collins <robertc@example.net>",
 
452
                         my_config._get_user_id())
 
453
        branch.email = "John"
 
454
        self.assertEqual("John", my_config._get_user_id())
 
455
 
 
456
    def test_not_set_in_branch(self):
 
457
        branch = FakeBranch()
 
458
        my_config = config.BranchConfig(branch)
 
459
        branch.email = None
 
460
        config_file = StringIO(sample_config_text)
 
461
        (my_config._get_location_config().
 
462
            _get_global_config()._get_parser(config_file))
 
463
        self.assertEqual("Robert Collins <robertc@example.com>",
 
464
                         my_config._get_user_id())
 
465
        branch.email = "John"
 
466
        self.assertEqual("John", my_config._get_user_id())
 
467
 
 
468
    def test_BZREMAIL_OVERRIDES(self):
 
469
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
 
470
        branch = FakeBranch()
 
471
        my_config = config.BranchConfig(branch)
 
472
        self.assertEqual("Robert Collins <robertc@example.org>",
 
473
                         my_config.username())
 
474
    
 
475
    def test_signatures_forced(self):
 
476
        branch = FakeBranch()
 
477
        my_config = config.BranchConfig(branch)
 
478
        config_file = StringIO(sample_always_signatures)
 
479
        (my_config._get_location_config().
 
480
            _get_global_config()._get_parser(config_file))
 
481
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
 
482
 
 
483
    def test_gpg_signing_command(self):
 
484
        branch = FakeBranch()
 
485
        my_config = config.BranchConfig(branch)
 
486
        config_file = StringIO(sample_config_text)
 
487
        (my_config._get_location_config().
 
488
            _get_global_config()._get_parser(config_file))
 
489
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
 
490
 
 
491
    def test_get_user_option_global(self):
 
492
        branch = FakeBranch()
 
493
        my_config = config.BranchConfig(branch)
 
494
        config_file = StringIO(sample_config_text)
 
495
        (my_config._get_location_config().
 
496
            _get_global_config()._get_parser(config_file))
 
497
        self.assertEqual('something',
 
498
                         my_config.get_user_option('user_global_option'))