~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testconfig.py

Exclude more files from dumb-rsync upload

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
66
66
                        "recurse=False\n"
67
67
                        "[/a/c]\n"
68
68
                        "check_signatures=ignore\n"
 
69
                        "post_commit=bzrlib.selftest.testconfig.post_commit\n"
69
70
                        "#testing explicit beats globs\n")
70
71
 
71
72
 
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))
 
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',))
80
92
 
81
93
 
82
94
class FakeBranch(object):
154
166
        my_config = config.Config()
155
167
        self.assertEqual(None, my_config.get_user_option('no_option'))
156
168
 
 
169
    def test_post_commit_default(self):
 
170
        my_config = config.Config()
 
171
        self.assertEqual(None, my_config.post_commit())
 
172
 
157
173
 
158
174
class TestConfigPath(TestCase):
159
175
 
160
176
    def setUp(self):
161
177
        super(TestConfigPath, self).setUp()
162
 
        self.oldenv = os.environ.get('HOME', None)
 
178
        self.old_home = os.environ.get('HOME', None)
 
179
        self.old_appdata = os.environ.get('APPDATA', None)
163
180
        os.environ['HOME'] = '/home/bogus'
 
181
        os.environ['APPDATA'] = \
 
182
            r'C:\Documents and Settings\bogus\Application Data'
164
183
 
165
184
    def tearDown(self):
166
 
        os.environ['HOME'] = self.oldenv
 
185
        if self.old_home is None:
 
186
            del os.environ['HOME']
 
187
        else:
 
188
            os.environ['HOME'] = self.old_home
 
189
        if self.old_appdata is None:
 
190
            del os.environ['APPDATA']
 
191
        else:
 
192
            os.environ['APPDATA'] = self.old_appdata
167
193
        super(TestConfigPath, self).tearDown()
168
194
    
169
195
    def test_config_dir(self):
170
 
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
 
196
        if sys.platform == 'win32':
 
197
            self.assertEqual(config.config_dir(), 
 
198
                r'C:\Documents and Settings\bogus\Application Data\bazaar\2.0')
 
199
        else:
 
200
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
171
201
 
172
202
    def test_config_filename(self):
173
 
        self.assertEqual(config.config_filename(),
174
 
                         '/home/bogus/.bazaar/bazaar.conf')
 
203
        if sys.platform == 'win32':
 
204
            self.assertEqual(config.config_filename(), 
 
205
                r'C:\Documents and Settings\bogus\Application Data\bazaar\2.0\bazaar.conf')
 
206
        else:
 
207
            self.assertEqual(config.config_filename(),
 
208
                             '/home/bogus/.bazaar/bazaar.conf')
175
209
 
176
210
    def test_branches_config_filename(self):
177
 
        self.assertEqual(config.branches_config_filename(),
178
 
                         '/home/bogus/.bazaar/branches.conf')
 
211
        if sys.platform == 'win32':
 
212
            self.assertEqual(config.branches_config_filename(), 
 
213
                r'C:\Documents and Settings\bogus\Application Data\bazaar\2.0\branches.conf')
 
214
        else:
 
215
            self.assertEqual(config.branches_config_filename(),
 
216
                             '/home/bogus/.bazaar/branches.conf')
179
217
 
180
218
class TestIniConfig(TestCase):
181
219
 
187
225
        my_config = config.IniBasedConfig(None)
188
226
        self.failUnless(
189
227
            isinstance(my_config._get_parser(file=config_file),
190
 
                        ConfigParser))
 
228
                        ConfigObj))
191
229
 
192
230
    def test_cached(self):
193
231
        config_file = StringIO(sample_config_text)
203
241
 
204
242
    def test_calls_read_filenames(self):
205
243
        # replace the class that is constructured, to check its parameters
206
 
        oldparserclass = config.ConfigParser
207
 
        config.ConfigParser = InstrumentedConfigParser
 
244
        oldparserclass = config.ConfigObj
 
245
        config.ConfigObj = InstrumentedConfigObj
208
246
        my_config = config.GlobalConfig()
209
247
        try:
210
248
            parser = my_config._get_parser()
211
249
        finally:
212
 
            config.ConfigParser = oldparserclass
213
 
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
214
 
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
 
250
            config.ConfigObj = oldparserclass
 
251
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
 
252
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
215
253
 
216
254
 
217
255
class TestBranchConfig(TestCaseInTempDir):
303
341
        my_config = self._get_sample_config()
304
342
        self.assertEqual("something",
305
343
                         my_config.get_user_option('user_global_option'))
 
344
        
 
345
    def test_post_commit_default(self):
 
346
        my_config = self._get_sample_config()
 
347
        self.assertEqual(None, my_config.post_commit())
306
348
 
307
349
 
308
350
class TestLocationConfig(TestCase):
312
354
        self.assertRaises(TypeError, config.LocationConfig)
313
355
 
314
356
    def test_branch_calls_read_filenames(self):
 
357
        # This is testing the correct file names are provided.
 
358
        # TODO: consolidate with the test for GlobalConfigs filename checks.
 
359
        #
315
360
        # replace the class that is constructured, to check its parameters
316
 
        oldparserclass = config.ConfigParser
317
 
        config.ConfigParser = InstrumentedConfigParser
 
361
        oldparserclass = config.ConfigObj
 
362
        config.ConfigObj = InstrumentedConfigObj
318
363
        my_config = config.LocationConfig('http://www.example.com')
319
364
        try:
320
365
            parser = my_config._get_parser()
321
366
        finally:
322
 
            config.ConfigParser = oldparserclass
323
 
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
324
 
        self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
 
367
            config.ConfigObj = oldparserclass
 
368
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
 
369
        self.assertEqual(parser._calls,
 
370
                         [('__init__', config.branches_config_filename())])
325
371
 
326
372
    def test_get_global_config(self):
327
373
        my_config = config.LocationConfig('http://example.com')
441
487
        self.get_location_config('/a')
442
488
        self.assertEqual('local',
443
489
                         self.my_config.get_user_option('user_local_option'))
 
490
        
 
491
    def test_post_commit_default(self):
 
492
        self.get_location_config('/a/c')
 
493
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
 
494
                         self.my_config.post_commit())
 
495
 
 
496
 
 
497
class TestLocationConfig(TestCaseInTempDir):
 
498
 
 
499
    def get_location_config(self, location, global_config=None):
 
500
        if global_config is None:
 
501
            global_file = StringIO(sample_config_text)
 
502
        else:
 
503
            global_file = StringIO(global_config)
 
504
        branches_file = StringIO(sample_branches_text)
 
505
        self.my_config = config.LocationConfig(location)
 
506
        self.my_config._get_parser(branches_file)
 
507
        self.my_config._get_global_config()._get_parser(global_file)
 
508
 
 
509
    def test_set_user_setting_sets_and_saves(self):
 
510
        # TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
 
511
        self.get_location_config('/a/c')
 
512
        record = InstrumentedConfigObj("foo")
 
513
        self.my_config._parser = record
 
514
        return
 
515
        # XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
516
        # broken: creates .bazaar in the top-level directory, not 
 
517
        # inside the test directory
 
518
        self.my_config.set_user_option('foo', 'bar')
 
519
        self.assertEqual([('__contains__', '/a/c'),
 
520
                          ('__contains__', '/a/c/'),
 
521
                          ('__setitem__', '/a/c', {}),
 
522
                          ('__getitem__', '/a/c'),
 
523
                          ('__setitem__', 'foo', 'bar'),
 
524
                          ('write',)],
 
525
                         record._calls[1:])
444
526
 
445
527
 
446
528
class TestBranchConfigItems(TestCase):
496
578
            _get_global_config()._get_parser(config_file))
497
579
        self.assertEqual('something',
498
580
                         my_config.get_user_option('user_global_option'))
 
581
 
 
582
    def test_post_commit_default(self):
 
583
        branch = FakeBranch()
 
584
        branch.base='/a/c'
 
585
        my_config = config.BranchConfig(branch)
 
586
        config_file = StringIO(sample_config_text)
 
587
        (my_config._get_location_config().
 
588
            _get_global_config()._get_parser(config_file))
 
589
        branch_file = StringIO(sample_branches_text)
 
590
        my_config._get_location_config()._get_parser(branch_file)
 
591
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
 
592
                         my_config.post_commit())
 
593
 
 
594
 
 
595
class TestMailAddressExtraction(TestCase):
 
596
 
 
597
    def test_extract_email_address(self):
 
598
        self.assertEqual('jane@test.com',
 
599
                         config.extract_email_address('Jane <jane@test.com>'))
 
600
        self.assertRaises(errors.BzrError,
 
601
                          config.extract_email_address, 'Jane Tester')