~ubuntu-branches/ubuntu/lucid/bzr/lucid-proposed

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testconfig.py

Tags: upstream-0.7
ImportĀ upstreamĀ versionĀ 0.7

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 bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
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
 
                        "post_commit=bzrlib.selftest.testconfig.post_commit\n"
70
 
                        "#testing explicit beats globs\n")
71
 
 
72
 
 
73
 
class InstrumentedConfigObj(object):
74
 
    """A config obj look-enough-alike to record calls made to it."""
75
 
 
76
 
    def __init__(self, input):
77
 
        self._calls = [('__init__', input)]
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
 
    def test_get_user_option_default(self):
152
 
        my_config = config.Config()
153
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
154
 
 
155
 
    def test_post_commit_default(self):
156
 
        my_config = config.Config()
157
 
        self.assertEqual(None, my_config.post_commit())
158
 
 
159
 
 
160
 
class TestConfigPath(TestCase):
161
 
 
162
 
    def setUp(self):
163
 
        super(TestConfigPath, self).setUp()
164
 
        self.oldenv = os.environ.get('HOME', None)
165
 
        os.environ['HOME'] = '/home/bogus'
166
 
 
167
 
    def tearDown(self):
168
 
        os.environ['HOME'] = self.oldenv
169
 
        super(TestConfigPath, self).tearDown()
170
 
    
171
 
    def test_config_dir(self):
172
 
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
173
 
 
174
 
    def test_config_filename(self):
175
 
        self.assertEqual(config.config_filename(),
176
 
                         '/home/bogus/.bazaar/bazaar.conf')
177
 
 
178
 
    def test_branches_config_filename(self):
179
 
        self.assertEqual(config.branches_config_filename(),
180
 
                         '/home/bogus/.bazaar/branches.conf')
181
 
 
182
 
class TestIniConfig(TestCase):
183
 
 
184
 
    def test_contructs(self):
185
 
        my_config = config.IniBasedConfig("nothing")
186
 
 
187
 
    def test_from_fp(self):
188
 
        config_file = StringIO(sample_config_text)
189
 
        my_config = config.IniBasedConfig(None)
190
 
        self.failUnless(
191
 
            isinstance(my_config._get_parser(file=config_file),
192
 
                        ConfigObj))
193
 
 
194
 
    def test_cached(self):
195
 
        config_file = StringIO(sample_config_text)
196
 
        my_config = config.IniBasedConfig(None)
197
 
        parser = my_config._get_parser(file=config_file)
198
 
        self.failUnless(my_config._get_parser() is parser)
199
 
 
200
 
 
201
 
class TestGetConfig(TestCase):
202
 
 
203
 
    def test_constructs(self):
204
 
        my_config = config.GlobalConfig()
205
 
 
206
 
    def test_calls_read_filenames(self):
207
 
        # replace the class that is constructured, to check its parameters
208
 
        oldparserclass = config.ConfigObj
209
 
        config.ConfigObj = InstrumentedConfigObj
210
 
        my_config = config.GlobalConfig()
211
 
        try:
212
 
            parser = my_config._get_parser()
213
 
        finally:
214
 
            config.ConfigObj = oldparserclass
215
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
216
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
217
 
 
218
 
 
219
 
class TestBranchConfig(TestCaseInTempDir):
220
 
 
221
 
    def test_constructs(self):
222
 
        branch = FakeBranch()
223
 
        my_config = config.BranchConfig(branch)
224
 
        self.assertRaises(TypeError, config.BranchConfig)
225
 
 
226
 
    def test_get_location_config(self):
227
 
        branch = FakeBranch()
228
 
        my_config = config.BranchConfig(branch)
229
 
        location_config = my_config._get_location_config()
230
 
        self.assertEqual(branch.base, location_config.location)
231
 
        self.failUnless(location_config is my_config._get_location_config())
232
 
 
233
 
 
234
 
class TestGlobalConfigItems(TestCase):
235
 
 
236
 
    def test_user_id(self):
237
 
        config_file = StringIO(sample_config_text)
238
 
        my_config = config.GlobalConfig()
239
 
        my_config._parser = my_config._get_parser(file=config_file)
240
 
        self.assertEqual("Robert Collins <robertc@example.com>",
241
 
                         my_config._get_user_id())
242
 
 
243
 
    def test_absent_user_id(self):
244
 
        config_file = StringIO("")
245
 
        my_config = config.GlobalConfig()
246
 
        my_config._parser = my_config._get_parser(file=config_file)
247
 
        self.assertEqual(None, my_config._get_user_id())
248
 
 
249
 
    def test_configured_editor(self):
250
 
        config_file = StringIO(sample_config_text)
251
 
        my_config = config.GlobalConfig()
252
 
        my_config._parser = my_config._get_parser(file=config_file)
253
 
        self.assertEqual("vim", my_config.get_editor())
254
 
 
255
 
    def test_signatures_always(self):
256
 
        config_file = StringIO(sample_always_signatures)
257
 
        my_config = config.GlobalConfig()
258
 
        my_config._parser = my_config._get_parser(file=config_file)
259
 
        self.assertEqual(config.CHECK_ALWAYS,
260
 
                         my_config.signature_checking())
261
 
        self.assertEqual(True, my_config.signature_needed())
262
 
 
263
 
    def test_signatures_if_possible(self):
264
 
        config_file = StringIO(sample_maybe_signatures)
265
 
        my_config = config.GlobalConfig()
266
 
        my_config._parser = my_config._get_parser(file=config_file)
267
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
268
 
                         my_config.signature_checking())
269
 
        self.assertEqual(False, my_config.signature_needed())
270
 
 
271
 
    def test_signatures_ignore(self):
272
 
        config_file = StringIO(sample_ignore_signatures)
273
 
        my_config = config.GlobalConfig()
274
 
        my_config._parser = my_config._get_parser(file=config_file)
275
 
        self.assertEqual(config.CHECK_NEVER,
276
 
                         my_config.signature_checking())
277
 
        self.assertEqual(False, my_config.signature_needed())
278
 
 
279
 
    def _get_sample_config(self):
280
 
        config_file = StringIO(sample_config_text)
281
 
        my_config = config.GlobalConfig()
282
 
        my_config._parser = my_config._get_parser(file=config_file)
283
 
        return my_config
284
 
 
285
 
    def test_gpg_signing_command(self):
286
 
        my_config = self._get_sample_config()
287
 
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
288
 
        self.assertEqual(False, my_config.signature_needed())
289
 
 
290
 
    def _get_empty_config(self):
291
 
        config_file = StringIO("")
292
 
        my_config = config.GlobalConfig()
293
 
        my_config._parser = my_config._get_parser(file=config_file)
294
 
        return my_config
295
 
 
296
 
    def test_gpg_signing_command_unset(self):
297
 
        my_config = self._get_empty_config()
298
 
        self.assertEqual("gpg", my_config.gpg_signing_command())
299
 
 
300
 
    def test_get_user_option_default(self):
301
 
        my_config = self._get_empty_config()
302
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
303
 
 
304
 
    def test_get_user_option_global(self):
305
 
        my_config = self._get_sample_config()
306
 
        self.assertEqual("something",
307
 
                         my_config.get_user_option('user_global_option'))
308
 
        
309
 
    def test_post_commit_default(self):
310
 
        my_config = self._get_sample_config()
311
 
        self.assertEqual(None, my_config.post_commit())
312
 
 
313
 
 
314
 
 
315
 
class TestLocationConfig(TestCase):
316
 
 
317
 
    def test_constructs(self):
318
 
        my_config = config.LocationConfig('http://example.com')
319
 
        self.assertRaises(TypeError, config.LocationConfig)
320
 
 
321
 
    def test_branch_calls_read_filenames(self):
322
 
        # This is testing the correct file names are provided.
323
 
        # TODO: consolidate with the test for GlobalConfigs filename checks.
324
 
        #
325
 
        # replace the class that is constructured, to check its parameters
326
 
        oldparserclass = config.ConfigObj
327
 
        config.ConfigObj = InstrumentedConfigObj
328
 
        my_config = config.LocationConfig('http://www.example.com')
329
 
        try:
330
 
            parser = my_config._get_parser()
331
 
        finally:
332
 
            config.ConfigObj = oldparserclass
333
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
334
 
        self.assertEqual(parser._calls,
335
 
                         [('__init__', config.branches_config_filename())])
336
 
 
337
 
    def test_get_global_config(self):
338
 
        my_config = config.LocationConfig('http://example.com')
339
 
        global_config = my_config._get_global_config()
340
 
        self.failUnless(isinstance(global_config, config.GlobalConfig))
341
 
        self.failUnless(global_config is my_config._get_global_config())
342
 
 
343
 
    def test__get_section_no_match(self):
344
 
        self.get_location_config('/')
345
 
        self.assertEqual(None, self.my_config._get_section())
346
 
        
347
 
    def test__get_section_exact(self):
348
 
        self.get_location_config('http://www.example.com')
349
 
        self.assertEqual('http://www.example.com',
350
 
                         self.my_config._get_section())
351
 
   
352
 
    def test__get_section_suffix_does_not(self):
353
 
        self.get_location_config('http://www.example.com-com')
354
 
        self.assertEqual(None, self.my_config._get_section())
355
 
 
356
 
    def test__get_section_subdir_recursive(self):
357
 
        self.get_location_config('http://www.example.com/com')
358
 
        self.assertEqual('http://www.example.com',
359
 
                         self.my_config._get_section())
360
 
 
361
 
    def test__get_section_subdir_matches(self):
362
 
        self.get_location_config('http://www.example.com/useglobal')
363
 
        self.assertEqual('http://www.example.com/useglobal',
364
 
                         self.my_config._get_section())
365
 
 
366
 
    def test__get_section_subdir_nonrecursive(self):
367
 
        self.get_location_config(
368
 
            'http://www.example.com/useglobal/childbranch')
369
 
        self.assertEqual('http://www.example.com',
370
 
                         self.my_config._get_section())
371
 
 
372
 
    def test__get_section_subdir_trailing_slash(self):
373
 
        self.get_location_config('/b')
374
 
        self.assertEqual('/b/', self.my_config._get_section())
375
 
 
376
 
    def test__get_section_subdir_child(self):
377
 
        self.get_location_config('/a/foo')
378
 
        self.assertEqual('/a/*', self.my_config._get_section())
379
 
 
380
 
    def test__get_section_subdir_child_child(self):
381
 
        self.get_location_config('/a/foo/bar')
382
 
        self.assertEqual('/a/', self.my_config._get_section())
383
 
 
384
 
    def test__get_section_trailing_slash_with_children(self):
385
 
        self.get_location_config('/a/')
386
 
        self.assertEqual('/a/', self.my_config._get_section())
387
 
 
388
 
    def test__get_section_explicit_over_glob(self):
389
 
        self.get_location_config('/a/c')
390
 
        self.assertEqual('/a/c', self.my_config._get_section())
391
 
 
392
 
    def get_location_config(self, location, global_config=None):
393
 
        if global_config is None:
394
 
            global_file = StringIO(sample_config_text)
395
 
        else:
396
 
            global_file = StringIO(global_config)
397
 
        branches_file = StringIO(sample_branches_text)
398
 
        self.my_config = config.LocationConfig(location)
399
 
        self.my_config._get_parser(branches_file)
400
 
        self.my_config._get_global_config()._get_parser(global_file)
401
 
 
402
 
    def test_location_without_username(self):
403
 
        self.get_location_config('http://www.example.com/useglobal')
404
 
        self.assertEqual('Robert Collins <robertc@example.com>',
405
 
                         self.my_config.username())
406
 
 
407
 
    def test_location_not_listed(self):
408
 
        self.get_location_config('/home/robertc/sources')
409
 
        self.assertEqual('Robert Collins <robertc@example.com>',
410
 
                         self.my_config.username())
411
 
 
412
 
    def test_overriding_location(self):
413
 
        self.get_location_config('http://www.example.com/foo')
414
 
        self.assertEqual('Robert Collins <robertc@example.org>',
415
 
                         self.my_config.username())
416
 
 
417
 
    def test_signatures_not_set(self):
418
 
        self.get_location_config('http://www.example.com',
419
 
                                 global_config=sample_ignore_signatures)
420
 
        self.assertEqual(config.CHECK_NEVER,
421
 
                         self.my_config.signature_checking())
422
 
 
423
 
    def test_signatures_never(self):
424
 
        self.get_location_config('/a/c')
425
 
        self.assertEqual(config.CHECK_NEVER,
426
 
                         self.my_config.signature_checking())
427
 
        
428
 
    def test_signatures_when_available(self):
429
 
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
430
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
431
 
                         self.my_config.signature_checking())
432
 
        
433
 
    def test_signatures_always(self):
434
 
        self.get_location_config('/b')
435
 
        self.assertEqual(config.CHECK_ALWAYS,
436
 
                         self.my_config.signature_checking())
437
 
        
438
 
    def test_gpg_signing_command(self):
439
 
        self.get_location_config('/b')
440
 
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
441
 
 
442
 
    def test_gpg_signing_command_missing(self):
443
 
        self.get_location_config('/a')
444
 
        self.assertEqual("false", self.my_config.gpg_signing_command())
445
 
 
446
 
    def test_get_user_option_global(self):
447
 
        self.get_location_config('/a')
448
 
        self.assertEqual('something',
449
 
                         self.my_config.get_user_option('user_global_option'))
450
 
 
451
 
    def test_get_user_option_local(self):
452
 
        self.get_location_config('/a')
453
 
        self.assertEqual('local',
454
 
                         self.my_config.get_user_option('user_local_option'))
455
 
        
456
 
    def test_post_commit_default(self):
457
 
        self.get_location_config('/a/c')
458
 
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
459
 
                         self.my_config.post_commit())
460
 
 
461
 
 
462
 
class TestBranchConfigItems(TestCase):
463
 
 
464
 
    def test_user_id(self):
465
 
        branch = FakeBranch()
466
 
        my_config = config.BranchConfig(branch)
467
 
        self.assertEqual("Robert Collins <robertc@example.net>",
468
 
                         my_config._get_user_id())
469
 
        branch.email = "John"
470
 
        self.assertEqual("John", my_config._get_user_id())
471
 
 
472
 
    def test_not_set_in_branch(self):
473
 
        branch = FakeBranch()
474
 
        my_config = config.BranchConfig(branch)
475
 
        branch.email = None
476
 
        config_file = StringIO(sample_config_text)
477
 
        (my_config._get_location_config().
478
 
            _get_global_config()._get_parser(config_file))
479
 
        self.assertEqual("Robert Collins <robertc@example.com>",
480
 
                         my_config._get_user_id())
481
 
        branch.email = "John"
482
 
        self.assertEqual("John", my_config._get_user_id())
483
 
 
484
 
    def test_BZREMAIL_OVERRIDES(self):
485
 
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
486
 
        branch = FakeBranch()
487
 
        my_config = config.BranchConfig(branch)
488
 
        self.assertEqual("Robert Collins <robertc@example.org>",
489
 
                         my_config.username())
490
 
    
491
 
    def test_signatures_forced(self):
492
 
        branch = FakeBranch()
493
 
        my_config = config.BranchConfig(branch)
494
 
        config_file = StringIO(sample_always_signatures)
495
 
        (my_config._get_location_config().
496
 
            _get_global_config()._get_parser(config_file))
497
 
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
498
 
 
499
 
    def test_gpg_signing_command(self):
500
 
        branch = FakeBranch()
501
 
        my_config = config.BranchConfig(branch)
502
 
        config_file = StringIO(sample_config_text)
503
 
        (my_config._get_location_config().
504
 
            _get_global_config()._get_parser(config_file))
505
 
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
506
 
 
507
 
    def test_get_user_option_global(self):
508
 
        branch = FakeBranch()
509
 
        my_config = config.BranchConfig(branch)
510
 
        config_file = StringIO(sample_config_text)
511
 
        (my_config._get_location_config().
512
 
            _get_global_config()._get_parser(config_file))
513
 
        self.assertEqual('something',
514
 
                         my_config.get_user_option('user_global_option'))
515
 
 
516
 
    def test_post_commit_default(self):
517
 
        branch = FakeBranch()
518
 
        branch.base='/a/c'
519
 
        my_config = config.BranchConfig(branch)
520
 
        config_file = StringIO(sample_config_text)
521
 
        (my_config._get_location_config().
522
 
            _get_global_config()._get_parser(config_file))
523
 
        branch_file = StringIO(sample_branches_text)
524
 
        my_config._get_location_config()._get_parser(branch_file)
525
 
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
526
 
                         my_config.post_commit())