/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testconfig.py

  • Committer: Michael Ellerman
  • Date: 2005-10-26 10:03:47 UTC
  • mfrom: (1185.16.116)
  • mto: (1185.16.126)
  • mto: This revision was merged to the branch mainline in revision 1488.
  • Revision ID: michael@ellerman.id.au-20051026100347-bb0b2bd42f7953f2
MergeĀ mainline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Tests for finding and reading the bzr config file[s]."""
19
19
# import system imports here
20
 
from ConfigParser import ConfigParser
 
20
from bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
21
21
from cStringIO import StringIO
22
22
import os
23
23
import sys
31
31
sample_config_text = ("[DEFAULT]\n"
32
32
                      "email=Robert Collins <robertc@example.com>\n"
33
33
                      "editor=vim\n"
34
 
                      "gpg_signing_command=gnome-gpg\n")
 
34
                      "gpg_signing_command=gnome-gpg\n"
 
35
                      "user_global_option=something\n")
35
36
 
36
37
 
37
38
sample_always_signatures = ("[DEFAULT]\n"
58
59
                        "[/a/]\n"
59
60
                        "check_signatures=check-available\n"
60
61
                        "gpg_signing_command=false\n"
 
62
                        "user_local_option=local\n"
61
63
                        "# test trailing / matching\n"
62
64
                        "[/a/*]\n"
63
65
                        "#subdirs will match but not the parent\n"
64
66
                        "recurse=False\n"
65
67
                        "[/a/c]\n"
66
68
                        "check_signatures=ignore\n"
 
69
                        "post_commit=bzrlib.selftest.testconfig.post_commit\n"
67
70
                        "#testing explicit beats globs\n")
68
71
 
69
72
 
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))
 
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
78
 
79
79
 
80
80
class FakeBranch(object):
148
148
        my_config = config.Config()
149
149
        self.assertEqual('gpg', my_config.gpg_signing_command())
150
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
 
151
159
 
152
160
class TestConfigPath(TestCase):
153
161
 
181
189
        my_config = config.IniBasedConfig(None)
182
190
        self.failUnless(
183
191
            isinstance(my_config._get_parser(file=config_file),
184
 
                        ConfigParser))
 
192
                        ConfigObj))
185
193
 
186
194
    def test_cached(self):
187
195
        config_file = StringIO(sample_config_text)
197
205
 
198
206
    def test_calls_read_filenames(self):
199
207
        # replace the class that is constructured, to check its parameters
200
 
        oldparserclass = config.ConfigParser
201
 
        config.ConfigParser = InstrumentedConfigParser
 
208
        oldparserclass = config.ConfigObj
 
209
        config.ConfigObj = InstrumentedConfigObj
202
210
        my_config = config.GlobalConfig()
203
211
        try:
204
212
            parser = my_config._get_parser()
205
213
        finally:
206
 
            config.ConfigParser = oldparserclass
207
 
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
208
 
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
 
214
            config.ConfigObj = oldparserclass
 
215
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
 
216
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
209
217
 
210
218
 
211
219
class TestBranchConfig(TestCaseInTempDir):
268
276
                         my_config.signature_checking())
269
277
        self.assertEqual(False, my_config.signature_needed())
270
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
 
271
285
    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)
 
286
        my_config = self._get_sample_config()
275
287
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
276
288
        self.assertEqual(False, my_config.signature_needed())
277
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
 
278
296
    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)
 
297
        my_config = self._get_empty_config()
282
298
        self.assertEqual("gpg", my_config.gpg_signing_command())
283
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
 
284
314
 
285
315
class TestLocationConfig(TestCase):
286
316
 
289
319
        self.assertRaises(TypeError, config.LocationConfig)
290
320
 
291
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
        #
292
325
        # replace the class that is constructured, to check its parameters
293
 
        oldparserclass = config.ConfigParser
294
 
        config.ConfigParser = InstrumentedConfigParser
 
326
        oldparserclass = config.ConfigObj
 
327
        config.ConfigObj = InstrumentedConfigObj
295
328
        my_config = config.LocationConfig('http://www.example.com')
296
329
        try:
297
330
            parser = my_config._get_parser()
298
331
        finally:
299
 
            config.ConfigParser = oldparserclass
300
 
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
301
 
        self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
 
332
            config.ConfigObj = oldparserclass
 
333
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
 
334
        self.assertEqual(parser._calls,
 
335
                         [('__init__', config.branches_config_filename())])
302
336
 
303
337
    def test_get_global_config(self):
304
338
        my_config = config.LocationConfig('http://example.com')
409
443
        self.get_location_config('/a')
410
444
        self.assertEqual("false", self.my_config.gpg_signing_command())
411
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
 
412
461
 
413
462
class TestBranchConfigItems(TestCase):
414
463
 
454
503
        (my_config._get_location_config().
455
504
            _get_global_config()._get_parser(config_file))
456
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())