~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

Basic BzrDir support.

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
25
25
#import bzrlib specific imports here
26
26
import bzrlib.config as config
27
27
import bzrlib.errors as errors
28
 
from bzrlib.selftest import TestCase, TestCaseInTempDir
 
28
from bzrlib.tests import TestCase, TestCaseInTempDir
29
29
 
30
30
 
31
31
sample_config_text = ("[DEFAULT]\n"
66
66
                        "recurse=False\n"
67
67
                        "[/a/c]\n"
68
68
                        "check_signatures=ignore\n"
 
69
                        "post_commit=bzrlib.tests.test_config.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):
83
95
 
84
96
    def __init__(self):
85
97
        self.base = "http://example.com/branches/demo"
 
98
        self.control_files = FakeControlFiles()
 
99
 
 
100
 
 
101
class FakeControlFiles(object):
 
102
 
 
103
    def __init__(self):
86
104
        self.email = 'Robert Collins <robertc@example.net>\n'
87
105
 
88
 
    def controlfile(self, filename, mode):
 
106
    def get_utf8(self, filename):
89
107
        if filename != 'email':
90
108
            raise NotImplementedError
91
109
        if self.email is not None:
92
110
            return StringIO(self.email)
93
 
        raise errors.NoSuchFile
 
111
        raise errors.NoSuchFile(filename)
94
112
 
95
113
 
96
114
class InstrumentedConfig(config.Config):
154
172
        my_config = config.Config()
155
173
        self.assertEqual(None, my_config.get_user_option('no_option'))
156
174
 
 
175
    def test_post_commit_default(self):
 
176
        my_config = config.Config()
 
177
        self.assertEqual(None, my_config.post_commit())
 
178
 
157
179
 
158
180
class TestConfigPath(TestCase):
159
181
 
160
182
    def setUp(self):
161
183
        super(TestConfigPath, self).setUp()
162
 
        self.oldenv = os.environ.get('HOME', None)
 
184
        self.old_home = os.environ.get('HOME', None)
 
185
        self.old_appdata = os.environ.get('APPDATA', None)
163
186
        os.environ['HOME'] = '/home/bogus'
 
187
        os.environ['APPDATA'] = \
 
188
            r'C:\Documents and Settings\bogus\Application Data'
164
189
 
165
190
    def tearDown(self):
166
 
        os.environ['HOME'] = self.oldenv
 
191
        if self.old_home is None:
 
192
            del os.environ['HOME']
 
193
        else:
 
194
            os.environ['HOME'] = self.old_home
 
195
        if self.old_appdata is None:
 
196
            del os.environ['APPDATA']
 
197
        else:
 
198
            os.environ['APPDATA'] = self.old_appdata
167
199
        super(TestConfigPath, self).tearDown()
168
200
    
169
201
    def test_config_dir(self):
170
 
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
 
202
        if sys.platform == 'win32':
 
203
            self.assertEqual(config.config_dir(), 
 
204
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
 
205
        else:
 
206
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
171
207
 
172
208
    def test_config_filename(self):
173
 
        self.assertEqual(config.config_filename(),
174
 
                         '/home/bogus/.bazaar/bazaar.conf')
 
209
        if sys.platform == 'win32':
 
210
            self.assertEqual(config.config_filename(), 
 
211
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
 
212
        else:
 
213
            self.assertEqual(config.config_filename(),
 
214
                             '/home/bogus/.bazaar/bazaar.conf')
175
215
 
176
216
    def test_branches_config_filename(self):
177
 
        self.assertEqual(config.branches_config_filename(),
178
 
                         '/home/bogus/.bazaar/branches.conf')
 
217
        if sys.platform == 'win32':
 
218
            self.assertEqual(config.branches_config_filename(), 
 
219
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
 
220
        else:
 
221
            self.assertEqual(config.branches_config_filename(),
 
222
                             '/home/bogus/.bazaar/branches.conf')
179
223
 
180
224
class TestIniConfig(TestCase):
181
225
 
187
231
        my_config = config.IniBasedConfig(None)
188
232
        self.failUnless(
189
233
            isinstance(my_config._get_parser(file=config_file),
190
 
                        ConfigParser))
 
234
                        ConfigObj))
191
235
 
192
236
    def test_cached(self):
193
237
        config_file = StringIO(sample_config_text)
203
247
 
204
248
    def test_calls_read_filenames(self):
205
249
        # replace the class that is constructured, to check its parameters
206
 
        oldparserclass = config.ConfigParser
207
 
        config.ConfigParser = InstrumentedConfigParser
 
250
        oldparserclass = config.ConfigObj
 
251
        config.ConfigObj = InstrumentedConfigObj
208
252
        my_config = config.GlobalConfig()
209
253
        try:
210
254
            parser = my_config._get_parser()
211
255
        finally:
212
 
            config.ConfigParser = oldparserclass
213
 
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
214
 
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
 
256
            config.ConfigObj = oldparserclass
 
257
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
 
258
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
215
259
 
216
260
 
217
261
class TestBranchConfig(TestCaseInTempDir):
303
347
        my_config = self._get_sample_config()
304
348
        self.assertEqual("something",
305
349
                         my_config.get_user_option('user_global_option'))
 
350
        
 
351
    def test_post_commit_default(self):
 
352
        my_config = self._get_sample_config()
 
353
        self.assertEqual(None, my_config.post_commit())
306
354
 
307
355
 
308
356
class TestLocationConfig(TestCase):
312
360
        self.assertRaises(TypeError, config.LocationConfig)
313
361
 
314
362
    def test_branch_calls_read_filenames(self):
 
363
        # This is testing the correct file names are provided.
 
364
        # TODO: consolidate with the test for GlobalConfigs filename checks.
 
365
        #
315
366
        # replace the class that is constructured, to check its parameters
316
 
        oldparserclass = config.ConfigParser
317
 
        config.ConfigParser = InstrumentedConfigParser
 
367
        oldparserclass = config.ConfigObj
 
368
        config.ConfigObj = InstrumentedConfigObj
318
369
        my_config = config.LocationConfig('http://www.example.com')
319
370
        try:
320
371
            parser = my_config._get_parser()
321
372
        finally:
322
 
            config.ConfigParser = oldparserclass
323
 
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
324
 
        self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
 
373
            config.ConfigObj = oldparserclass
 
374
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
 
375
        self.assertEqual(parser._calls,
 
376
                         [('__init__', config.branches_config_filename())])
325
377
 
326
378
    def test_get_global_config(self):
327
379
        my_config = config.LocationConfig('http://example.com')
441
493
        self.get_location_config('/a')
442
494
        self.assertEqual('local',
443
495
                         self.my_config.get_user_option('user_local_option'))
 
496
        
 
497
    def test_post_commit_default(self):
 
498
        self.get_location_config('/a/c')
 
499
        self.assertEqual('bzrlib.tests.test_config.post_commit',
 
500
                         self.my_config.post_commit())
 
501
 
 
502
 
 
503
class TestLocationConfig(TestCaseInTempDir):
 
504
 
 
505
    def get_location_config(self, location, global_config=None):
 
506
        if global_config is None:
 
507
            global_file = StringIO(sample_config_text)
 
508
        else:
 
509
            global_file = StringIO(global_config)
 
510
        branches_file = StringIO(sample_branches_text)
 
511
        self.my_config = config.LocationConfig(location)
 
512
        self.my_config._get_parser(branches_file)
 
513
        self.my_config._get_global_config()._get_parser(global_file)
 
514
 
 
515
    def test_set_user_setting_sets_and_saves(self):
 
516
        self.get_location_config('/a/c')
 
517
        record = InstrumentedConfigObj("foo")
 
518
        self.my_config._parser = record
 
519
 
 
520
        real_mkdir = os.mkdir
 
521
        self.created = False
 
522
        def checked_mkdir(path, mode=0777):
 
523
            self.log('making directory: %s', path)
 
524
            real_mkdir(path, mode)
 
525
            self.created = True
 
526
 
 
527
        os.mkdir = checked_mkdir
 
528
        try:
 
529
            self.my_config.set_user_option('foo', 'bar')
 
530
        finally:
 
531
            os.mkdir = real_mkdir
 
532
 
 
533
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
 
534
        self.assertEqual([('__contains__', '/a/c'),
 
535
                          ('__contains__', '/a/c/'),
 
536
                          ('__setitem__', '/a/c', {}),
 
537
                          ('__getitem__', '/a/c'),
 
538
                          ('__setitem__', 'foo', 'bar'),
 
539
                          ('write',)],
 
540
                         record._calls[1:])
444
541
 
445
542
 
446
543
class TestBranchConfigItems(TestCase):
450
547
        my_config = config.BranchConfig(branch)
451
548
        self.assertEqual("Robert Collins <robertc@example.net>",
452
549
                         my_config._get_user_id())
453
 
        branch.email = "John"
 
550
        branch.control_files.email = "John"
454
551
        self.assertEqual("John", my_config._get_user_id())
455
552
 
456
553
    def test_not_set_in_branch(self):
457
554
        branch = FakeBranch()
458
555
        my_config = config.BranchConfig(branch)
459
 
        branch.email = None
 
556
        branch.control_files.email = None
460
557
        config_file = StringIO(sample_config_text)
461
558
        (my_config._get_location_config().
462
559
            _get_global_config()._get_parser(config_file))
463
560
        self.assertEqual("Robert Collins <robertc@example.com>",
464
561
                         my_config._get_user_id())
465
 
        branch.email = "John"
 
562
        branch.control_files.email = "John"
466
563
        self.assertEqual("John", my_config._get_user_id())
467
564
 
468
565
    def test_BZREMAIL_OVERRIDES(self):
496
593
            _get_global_config()._get_parser(config_file))
497
594
        self.assertEqual('something',
498
595
                         my_config.get_user_option('user_global_option'))
 
596
 
 
597
    def test_post_commit_default(self):
 
598
        branch = FakeBranch()
 
599
        branch.base='/a/c'
 
600
        my_config = config.BranchConfig(branch)
 
601
        config_file = StringIO(sample_config_text)
 
602
        (my_config._get_location_config().
 
603
            _get_global_config()._get_parser(config_file))
 
604
        branch_file = StringIO(sample_branches_text)
 
605
        my_config._get_location_config()._get_parser(branch_file)
 
606
        self.assertEqual('bzrlib.tests.test_config.post_commit',
 
607
                         my_config.post_commit())
 
608
 
 
609
 
 
610
class TestMailAddressExtraction(TestCase):
 
611
 
 
612
    def test_extract_email_address(self):
 
613
        self.assertEqual('jane@test.com',
 
614
                         config.extract_email_address('Jane <jane@test.com>'))
 
615
        self.assertRaises(errors.BzrError,
 
616
                          config.extract_email_address, 'Jane Tester')