~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-18 06:42:17 UTC
  • mfrom: (0.2.1)
  • mto: This revision was merged to the branch mainline in revision 1463.
  • Revision ID: robertc@robertcollins.net-20051018064217-e810bd94c74a9ad1
Factor out the guts of 'pull' from the command into WorkingTree.pull().
(Robert Collins)

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 bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
 
20
from ConfigParser import ConfigParser
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"
35
 
                      "user_global_option=something\n")
 
34
                      "gpg_signing_command=gnome-gpg\n")
36
35
 
37
36
 
38
37
sample_always_signatures = ("[DEFAULT]\n"
59
58
                        "[/a/]\n"
60
59
                        "check_signatures=check-available\n"
61
60
                        "gpg_signing_command=false\n"
62
 
                        "user_local_option=local\n"
63
61
                        "# test trailing / matching\n"
64
62
                        "[/a/*]\n"
65
63
                        "#subdirs will match but not the parent\n"
66
64
                        "recurse=False\n"
67
65
                        "[/a/c]\n"
68
66
                        "check_signatures=ignore\n"
69
 
                        "post_commit=bzrlib.selftest.testconfig.post_commit\n"
70
67
                        "#testing explicit beats globs\n")
71
68
 
72
69
 
73
 
class InstrumentedConfigObj(object):
74
 
    """A config obj look-enough-alike to record calls made to it."""
75
 
 
76
 
    def __contains__(self, thing):
77
 
        self._calls.append(('__contains__', thing))
78
 
        return False
79
 
 
80
 
    def __getitem__(self, key):
81
 
        self._calls.append(('__getitem__', key))
82
 
        return self
83
 
 
84
 
    def __init__(self, input):
85
 
        self._calls = [('__init__', input)]
86
 
 
87
 
    def __setitem__(self, key, value):
88
 
        self._calls.append(('__setitem__', key, value))
89
 
 
90
 
    def write(self):
91
 
        self._calls.append(('write',))
 
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))
92
78
 
93
79
 
94
80
class FakeBranch(object):
162
148
        my_config = config.Config()
163
149
        self.assertEqual('gpg', my_config.gpg_signing_command())
164
150
 
165
 
    def test_get_user_option_default(self):
166
 
        my_config = config.Config()
167
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
168
 
 
169
 
    def test_post_commit_default(self):
170
 
        my_config = config.Config()
171
 
        self.assertEqual(None, my_config.post_commit())
172
 
 
173
151
 
174
152
class TestConfigPath(TestCase):
175
153
 
203
181
        my_config = config.IniBasedConfig(None)
204
182
        self.failUnless(
205
183
            isinstance(my_config._get_parser(file=config_file),
206
 
                        ConfigObj))
 
184
                        ConfigParser))
207
185
 
208
186
    def test_cached(self):
209
187
        config_file = StringIO(sample_config_text)
219
197
 
220
198
    def test_calls_read_filenames(self):
221
199
        # replace the class that is constructured, to check its parameters
222
 
        oldparserclass = config.ConfigObj
223
 
        config.ConfigObj = InstrumentedConfigObj
 
200
        oldparserclass = config.ConfigParser
 
201
        config.ConfigParser = InstrumentedConfigParser
224
202
        my_config = config.GlobalConfig()
225
203
        try:
226
204
            parser = my_config._get_parser()
227
205
        finally:
228
 
            config.ConfigObj = oldparserclass
229
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
230
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
 
206
            config.ConfigParser = oldparserclass
 
207
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
208
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
231
209
 
232
210
 
233
211
class TestBranchConfig(TestCaseInTempDir):
290
268
                         my_config.signature_checking())
291
269
        self.assertEqual(False, my_config.signature_needed())
292
270
 
293
 
    def _get_sample_config(self):
 
271
    def test_gpg_signing_command(self):
294
272
        config_file = StringIO(sample_config_text)
295
273
        my_config = config.GlobalConfig()
296
274
        my_config._parser = my_config._get_parser(file=config_file)
297
 
        return my_config
298
 
 
299
 
    def test_gpg_signing_command(self):
300
 
        my_config = self._get_sample_config()
301
275
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
302
276
        self.assertEqual(False, my_config.signature_needed())
303
277
 
304
 
    def _get_empty_config(self):
305
 
        config_file = StringIO("")
306
 
        my_config = config.GlobalConfig()
307
 
        my_config._parser = my_config._get_parser(file=config_file)
308
 
        return my_config
309
 
 
310
278
    def test_gpg_signing_command_unset(self):
311
 
        my_config = self._get_empty_config()
 
279
        config_file = StringIO("")
 
280
        my_config = config.GlobalConfig()
 
281
        my_config._parser = my_config._get_parser(file=config_file)
312
282
        self.assertEqual("gpg", my_config.gpg_signing_command())
313
283
 
314
 
    def test_get_user_option_default(self):
315
 
        my_config = self._get_empty_config()
316
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
317
 
 
318
 
    def test_get_user_option_global(self):
319
 
        my_config = self._get_sample_config()
320
 
        self.assertEqual("something",
321
 
                         my_config.get_user_option('user_global_option'))
322
 
        
323
 
    def test_post_commit_default(self):
324
 
        my_config = self._get_sample_config()
325
 
        self.assertEqual(None, my_config.post_commit())
326
 
 
327
 
 
328
284
 
329
285
class TestLocationConfig(TestCase):
330
286
 
333
289
        self.assertRaises(TypeError, config.LocationConfig)
334
290
 
335
291
    def test_branch_calls_read_filenames(self):
336
 
        # This is testing the correct file names are provided.
337
 
        # TODO: consolidate with the test for GlobalConfigs filename checks.
338
 
        #
339
292
        # replace the class that is constructured, to check its parameters
340
 
        oldparserclass = config.ConfigObj
341
 
        config.ConfigObj = InstrumentedConfigObj
 
293
        oldparserclass = config.ConfigParser
 
294
        config.ConfigParser = InstrumentedConfigParser
342
295
        my_config = config.LocationConfig('http://www.example.com')
343
296
        try:
344
297
            parser = my_config._get_parser()
345
298
        finally:
346
 
            config.ConfigObj = oldparserclass
347
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
348
 
        self.assertEqual(parser._calls,
349
 
                         [('__init__', config.branches_config_filename())])
 
299
            config.ConfigParser = oldparserclass
 
300
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
301
        self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
350
302
 
351
303
    def test_get_global_config(self):
352
304
        my_config = config.LocationConfig('http://example.com')
457
409
        self.get_location_config('/a')
458
410
        self.assertEqual("false", self.my_config.gpg_signing_command())
459
411
 
460
 
    def test_get_user_option_global(self):
461
 
        self.get_location_config('/a')
462
 
        self.assertEqual('something',
463
 
                         self.my_config.get_user_option('user_global_option'))
464
 
 
465
 
    def test_get_user_option_local(self):
466
 
        self.get_location_config('/a')
467
 
        self.assertEqual('local',
468
 
                         self.my_config.get_user_option('user_local_option'))
469
 
        
470
 
    def test_post_commit_default(self):
471
 
        self.get_location_config('/a/c')
472
 
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
473
 
                         self.my_config.post_commit())
474
 
 
475
 
    def test_set_user_setting_sets_and_saves(self):
476
 
        # TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
477
 
        self.get_location_config('/a/c')
478
 
        record = InstrumentedConfigObj("foo")
479
 
        self.my_config._parser = record
480
 
        self.my_config.set_user_option('foo', 'bar')
481
 
        self.assertEqual([('__contains__', '/a/c'),
482
 
                          ('__contains__', '/a/c/'),
483
 
                          ('__setitem__', '/a/c', {}),
484
 
                          ('__getitem__', '/a/c'),
485
 
                          ('__setitem__', 'foo', 'bar'),
486
 
                          ('write',)],
487
 
                         record._calls[1:])
488
 
 
489
412
 
490
413
class TestBranchConfigItems(TestCase):
491
414
 
531
454
        (my_config._get_location_config().
532
455
            _get_global_config()._get_parser(config_file))
533
456
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
534
 
 
535
 
    def test_get_user_option_global(self):
536
 
        branch = FakeBranch()
537
 
        my_config = config.BranchConfig(branch)
538
 
        config_file = StringIO(sample_config_text)
539
 
        (my_config._get_location_config().
540
 
            _get_global_config()._get_parser(config_file))
541
 
        self.assertEqual('something',
542
 
                         my_config.get_user_option('user_global_option'))
543
 
 
544
 
    def test_post_commit_default(self):
545
 
        branch = FakeBranch()
546
 
        branch.base='/a/c'
547
 
        my_config = config.BranchConfig(branch)
548
 
        config_file = StringIO(sample_config_text)
549
 
        (my_config._get_location_config().
550
 
            _get_global_config()._get_parser(config_file))
551
 
        branch_file = StringIO(sample_branches_text)
552
 
        my_config._get_location_config()._get_parser(branch_file)
553
 
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
554
 
                         my_config.post_commit())