~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testconfig.py

Update news and readme

- better explanation of dependencies

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
25
25
#import bzrlib specific imports here
26
26
import bzrlib.config as config
27
27
import bzrlib.errors as errors
28
 
from bzrlib.tests import TestCase, TestCaseInTempDir
 
28
from bzrlib.selftest import TestCase, TestCaseInTempDir
29
29
 
30
30
 
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.tests.test_config.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):
95
81
 
96
82
    def __init__(self):
97
83
        self.base = "http://example.com/branches/demo"
98
 
        self.control_files = FakeControlFiles()
99
 
 
100
 
 
101
 
class FakeControlFiles(object):
102
 
 
103
 
    def __init__(self):
104
84
        self.email = 'Robert Collins <robertc@example.net>\n'
105
85
 
106
 
    def get_utf8(self, filename):
 
86
    def controlfile(self, filename, mode):
107
87
        if filename != 'email':
108
88
            raise NotImplementedError
109
89
        if self.email is not None:
110
90
            return StringIO(self.email)
111
 
        raise errors.NoSuchFile(filename)
 
91
        raise errors.NoSuchFile
112
92
 
113
93
 
114
94
class InstrumentedConfig(config.Config):
168
148
        my_config = config.Config()
169
149
        self.assertEqual('gpg', my_config.gpg_signing_command())
170
150
 
171
 
    def test_get_user_option_default(self):
172
 
        my_config = config.Config()
173
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
174
 
 
175
 
    def test_post_commit_default(self):
176
 
        my_config = config.Config()
177
 
        self.assertEqual(None, my_config.post_commit())
178
 
 
179
151
 
180
152
class TestConfigPath(TestCase):
181
153
 
182
154
    def setUp(self):
183
155
        super(TestConfigPath, self).setUp()
184
 
        self.old_home = os.environ.get('HOME', None)
185
 
        self.old_appdata = os.environ.get('APPDATA', None)
 
156
        self.oldenv = os.environ.get('HOME', None)
186
157
        os.environ['HOME'] = '/home/bogus'
187
 
        os.environ['APPDATA'] = \
188
 
            r'C:\Documents and Settings\bogus\Application Data'
189
158
 
190
159
    def tearDown(self):
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
 
160
        os.environ['HOME'] = self.oldenv
199
161
        super(TestConfigPath, self).tearDown()
200
162
    
201
163
    def test_config_dir(self):
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')
 
164
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
207
165
 
208
166
    def test_config_filename(self):
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')
 
167
        self.assertEqual(config.config_filename(),
 
168
                         '/home/bogus/.bazaar/bazaar.conf')
215
169
 
216
170
    def test_branches_config_filename(self):
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')
 
171
        self.assertEqual(config.branches_config_filename(),
 
172
                         '/home/bogus/.bazaar/branches.conf')
223
173
 
224
174
class TestIniConfig(TestCase):
225
175
 
231
181
        my_config = config.IniBasedConfig(None)
232
182
        self.failUnless(
233
183
            isinstance(my_config._get_parser(file=config_file),
234
 
                        ConfigObj))
 
184
                        ConfigParser))
235
185
 
236
186
    def test_cached(self):
237
187
        config_file = StringIO(sample_config_text)
247
197
 
248
198
    def test_calls_read_filenames(self):
249
199
        # replace the class that is constructured, to check its parameters
250
 
        oldparserclass = config.ConfigObj
251
 
        config.ConfigObj = InstrumentedConfigObj
 
200
        oldparserclass = config.ConfigParser
 
201
        config.ConfigParser = InstrumentedConfigParser
252
202
        my_config = config.GlobalConfig()
253
203
        try:
254
204
            parser = my_config._get_parser()
255
205
        finally:
256
 
            config.ConfigObj = oldparserclass
257
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
258
 
        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()])])
259
209
 
260
210
 
261
211
class TestBranchConfig(TestCaseInTempDir):
318
268
                         my_config.signature_checking())
319
269
        self.assertEqual(False, my_config.signature_needed())
320
270
 
321
 
    def _get_sample_config(self):
 
271
    def test_gpg_signing_command(self):
322
272
        config_file = StringIO(sample_config_text)
323
273
        my_config = config.GlobalConfig()
324
274
        my_config._parser = my_config._get_parser(file=config_file)
325
 
        return my_config
326
 
 
327
 
    def test_gpg_signing_command(self):
328
 
        my_config = self._get_sample_config()
329
275
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
330
276
        self.assertEqual(False, my_config.signature_needed())
331
277
 
332
 
    def _get_empty_config(self):
333
 
        config_file = StringIO("")
334
 
        my_config = config.GlobalConfig()
335
 
        my_config._parser = my_config._get_parser(file=config_file)
336
 
        return my_config
337
 
 
338
278
    def test_gpg_signing_command_unset(self):
339
 
        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)
340
282
        self.assertEqual("gpg", my_config.gpg_signing_command())
341
283
 
342
 
    def test_get_user_option_default(self):
343
 
        my_config = self._get_empty_config()
344
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
345
 
 
346
 
    def test_get_user_option_global(self):
347
 
        my_config = self._get_sample_config()
348
 
        self.assertEqual("something",
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())
354
 
 
355
284
 
356
285
class TestLocationConfig(TestCase):
357
286
 
360
289
        self.assertRaises(TypeError, config.LocationConfig)
361
290
 
362
291
    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
 
        #
366
292
        # replace the class that is constructured, to check its parameters
367
 
        oldparserclass = config.ConfigObj
368
 
        config.ConfigObj = InstrumentedConfigObj
 
293
        oldparserclass = config.ConfigParser
 
294
        config.ConfigParser = InstrumentedConfigParser
369
295
        my_config = config.LocationConfig('http://www.example.com')
370
296
        try:
371
297
            parser = my_config._get_parser()
372
298
        finally:
373
 
            config.ConfigObj = oldparserclass
374
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
375
 
        self.assertEqual(parser._calls,
376
 
                         [('__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()])])
377
302
 
378
303
    def test_get_global_config(self):
379
304
        my_config = config.LocationConfig('http://example.com')
484
409
        self.get_location_config('/a')
485
410
        self.assertEqual("false", self.my_config.gpg_signing_command())
486
411
 
487
 
    def test_get_user_option_global(self):
488
 
        self.get_location_config('/a')
489
 
        self.assertEqual('something',
490
 
                         self.my_config.get_user_option('user_global_option'))
491
 
 
492
 
    def test_get_user_option_local(self):
493
 
        self.get_location_config('/a')
494
 
        self.assertEqual('local',
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:])
541
 
 
542
412
 
543
413
class TestBranchConfigItems(TestCase):
544
414
 
547
417
        my_config = config.BranchConfig(branch)
548
418
        self.assertEqual("Robert Collins <robertc@example.net>",
549
419
                         my_config._get_user_id())
550
 
        branch.control_files.email = "John"
 
420
        branch.email = "John"
551
421
        self.assertEqual("John", my_config._get_user_id())
552
422
 
553
423
    def test_not_set_in_branch(self):
554
424
        branch = FakeBranch()
555
425
        my_config = config.BranchConfig(branch)
556
 
        branch.control_files.email = None
 
426
        branch.email = None
557
427
        config_file = StringIO(sample_config_text)
558
428
        (my_config._get_location_config().
559
429
            _get_global_config()._get_parser(config_file))
560
430
        self.assertEqual("Robert Collins <robertc@example.com>",
561
431
                         my_config._get_user_id())
562
 
        branch.control_files.email = "John"
 
432
        branch.email = "John"
563
433
        self.assertEqual("John", my_config._get_user_id())
564
434
 
565
435
    def test_BZREMAIL_OVERRIDES(self):
584
454
        (my_config._get_location_config().
585
455
            _get_global_config()._get_parser(config_file))
586
456
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
587
 
 
588
 
    def test_get_user_option_global(self):
589
 
        branch = FakeBranch()
590
 
        my_config = config.BranchConfig(branch)
591
 
        config_file = StringIO(sample_config_text)
592
 
        (my_config._get_location_config().
593
 
            _get_global_config()._get_parser(config_file))
594
 
        self.assertEqual('something',
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')