~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 05:26:22 UTC
  • mto: This revision was merged to the branch mainline in revision 1463.
  • Revision ID: robertc@robertcollins.net-20051018052622-653d638c9e26fde4
fix broken tests

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
29
 
 
30
 
 
31
 
sample_long_alias="log -r-15..-1 --line"
 
28
from bzrlib.selftest import TestCase, TestCaseInTempDir
 
29
 
 
30
 
32
31
sample_config_text = ("[DEFAULT]\n"
33
 
                      u"email=Erik B\u00e5gfors <erik@bagfors.nu>\n"
 
32
                      "email=Robert Collins <robertc@example.com>\n"
34
33
                      "editor=vim\n"
35
 
                      "gpg_signing_command=gnome-gpg\n"
36
 
                      "log_format=short\n"
37
 
                      "user_global_option=something\n"
38
 
                      "[ALIASES]\n"
39
 
                      "h=help\n"
40
 
                      "ll=" + sample_long_alias + "\n")
 
34
                      "gpg_signing_command=gnome-gpg\n")
41
35
 
42
36
 
43
37
sample_always_signatures = ("[DEFAULT]\n"
64
58
                        "[/a/]\n"
65
59
                        "check_signatures=check-available\n"
66
60
                        "gpg_signing_command=false\n"
67
 
                        "user_local_option=local\n"
68
61
                        "# test trailing / matching\n"
69
62
                        "[/a/*]\n"
70
63
                        "#subdirs will match but not the parent\n"
71
64
                        "recurse=False\n"
72
65
                        "[/a/c]\n"
73
66
                        "check_signatures=ignore\n"
74
 
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
75
67
                        "#testing explicit beats globs\n")
76
68
 
77
69
 
78
 
 
79
 
class InstrumentedConfigObj(object):
80
 
    """A config obj look-enough-alike to record calls made to it."""
81
 
 
82
 
    def __contains__(self, thing):
83
 
        self._calls.append(('__contains__', thing))
84
 
        return False
85
 
 
86
 
    def __getitem__(self, key):
87
 
        self._calls.append(('__getitem__', key))
88
 
        return self
89
 
 
90
 
    def __init__(self, input, encoding=None):
91
 
        self._calls = [('__init__', input, encoding)]
92
 
 
93
 
    def __setitem__(self, key, value):
94
 
        self._calls.append(('__setitem__', key, value))
95
 
 
96
 
    def write(self, arg):
97
 
        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))
98
78
 
99
79
 
100
80
class FakeBranch(object):
101
81
 
102
82
    def __init__(self):
103
83
        self.base = "http://example.com/branches/demo"
104
 
        self.control_files = FakeControlFiles()
105
 
 
106
 
 
107
 
class FakeControlFiles(object):
108
 
 
109
 
    def __init__(self):
110
84
        self.email = 'Robert Collins <robertc@example.net>\n'
111
85
 
112
 
    def get_utf8(self, filename):
 
86
    def controlfile(self, filename, mode):
113
87
        if filename != 'email':
114
88
            raise NotImplementedError
115
89
        if self.email is not None:
116
90
            return StringIO(self.email)
117
 
        raise errors.NoSuchFile(filename)
 
91
        raise errors.NoSuchFile
118
92
 
119
93
 
120
94
class InstrumentedConfig(config.Config):
134
108
        return self._signatures
135
109
 
136
110
 
137
 
bool_config = """[DEFAULT]
138
 
active = true
139
 
inactive = false
140
 
[UPPERCASE]
141
 
active = True
142
 
nonactive = False
143
 
"""
144
 
class TestConfigObj(TestCase):
145
 
    def test_get_bool(self):
146
 
        from bzrlib.config import ConfigObj
147
 
        co = ConfigObj(StringIO(bool_config))
148
 
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
149
 
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
150
 
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
151
 
        self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
152
 
 
153
 
 
154
111
class TestConfig(TestCase):
155
112
 
156
113
    def test_constructs(self):
191
148
        my_config = config.Config()
192
149
        self.assertEqual('gpg', my_config.gpg_signing_command())
193
150
 
194
 
    def test_get_user_option_default(self):
195
 
        my_config = config.Config()
196
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
197
 
 
198
 
    def test_post_commit_default(self):
199
 
        my_config = config.Config()
200
 
        self.assertEqual(None, my_config.post_commit())
201
 
 
202
 
    def test_log_format_default(self):
203
 
        my_config = config.Config()
204
 
        self.assertEqual('long', my_config.log_format())
205
 
 
206
151
 
207
152
class TestConfigPath(TestCase):
208
153
 
209
154
    def setUp(self):
210
155
        super(TestConfigPath, self).setUp()
211
 
        self.old_home = os.environ.get('HOME', None)
212
 
        self.old_appdata = os.environ.get('APPDATA', None)
 
156
        self.oldenv = os.environ.get('HOME', None)
213
157
        os.environ['HOME'] = '/home/bogus'
214
 
        os.environ['APPDATA'] = \
215
 
            r'C:\Documents and Settings\bogus\Application Data'
216
158
 
217
159
    def tearDown(self):
218
 
        if self.old_home is None:
219
 
            del os.environ['HOME']
220
 
        else:
221
 
            os.environ['HOME'] = self.old_home
222
 
        if self.old_appdata is None:
223
 
            del os.environ['APPDATA']
224
 
        else:
225
 
            os.environ['APPDATA'] = self.old_appdata
 
160
        os.environ['HOME'] = self.oldenv
226
161
        super(TestConfigPath, self).tearDown()
227
162
    
228
163
    def test_config_dir(self):
229
 
        if sys.platform == 'win32':
230
 
            self.assertEqual(config.config_dir(), 
231
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
232
 
        else:
233
 
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
 
164
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
234
165
 
235
166
    def test_config_filename(self):
236
 
        if sys.platform == 'win32':
237
 
            self.assertEqual(config.config_filename(), 
238
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
239
 
        else:
240
 
            self.assertEqual(config.config_filename(),
241
 
                             '/home/bogus/.bazaar/bazaar.conf')
 
167
        self.assertEqual(config.config_filename(),
 
168
                         '/home/bogus/.bazaar/bazaar.conf')
242
169
 
243
170
    def test_branches_config_filename(self):
244
 
        if sys.platform == 'win32':
245
 
            self.assertEqual(config.branches_config_filename(), 
246
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
247
 
        else:
248
 
            self.assertEqual(config.branches_config_filename(),
249
 
                             '/home/bogus/.bazaar/branches.conf')
 
171
        self.assertEqual(config.branches_config_filename(),
 
172
                         '/home/bogus/.bazaar/branches.conf')
250
173
 
251
174
class TestIniConfig(TestCase):
252
175
 
254
177
        my_config = config.IniBasedConfig("nothing")
255
178
 
256
179
    def test_from_fp(self):
257
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
180
        config_file = StringIO(sample_config_text)
258
181
        my_config = config.IniBasedConfig(None)
259
182
        self.failUnless(
260
183
            isinstance(my_config._get_parser(file=config_file),
261
 
                        ConfigObj))
 
184
                        ConfigParser))
262
185
 
263
186
    def test_cached(self):
264
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
187
        config_file = StringIO(sample_config_text)
265
188
        my_config = config.IniBasedConfig(None)
266
189
        parser = my_config._get_parser(file=config_file)
267
190
        self.failUnless(my_config._get_parser() is parser)
274
197
 
275
198
    def test_calls_read_filenames(self):
276
199
        # replace the class that is constructured, to check its parameters
277
 
        oldparserclass = config.ConfigObj
278
 
        config.ConfigObj = InstrumentedConfigObj
 
200
        oldparserclass = config.ConfigParser
 
201
        config.ConfigParser = InstrumentedConfigParser
279
202
        my_config = config.GlobalConfig()
280
203
        try:
281
204
            parser = my_config._get_parser()
282
205
        finally:
283
 
            config.ConfigObj = oldparserclass
284
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
285
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
286
 
                                          'utf-8')])
 
206
            config.ConfigParser = oldparserclass
 
207
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
208
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
287
209
 
288
210
 
289
211
class TestBranchConfig(TestCaseInTempDir):
304
226
class TestGlobalConfigItems(TestCase):
305
227
 
306
228
    def test_user_id(self):
307
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
229
        config_file = StringIO(sample_config_text)
308
230
        my_config = config.GlobalConfig()
309
231
        my_config._parser = my_config._get_parser(file=config_file)
310
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
 
232
        self.assertEqual("Robert Collins <robertc@example.com>",
311
233
                         my_config._get_user_id())
312
234
 
313
235
    def test_absent_user_id(self):
317
239
        self.assertEqual(None, my_config._get_user_id())
318
240
 
319
241
    def test_configured_editor(self):
320
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
242
        config_file = StringIO(sample_config_text)
321
243
        my_config = config.GlobalConfig()
322
244
        my_config._parser = my_config._get_parser(file=config_file)
323
245
        self.assertEqual("vim", my_config.get_editor())
346
268
                         my_config.signature_checking())
347
269
        self.assertEqual(False, my_config.signature_needed())
348
270
 
349
 
    def _get_sample_config(self):
350
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
351
 
        my_config = config.GlobalConfig()
352
 
        my_config._parser = my_config._get_parser(file=config_file)
353
 
        return my_config
354
 
 
355
271
    def test_gpg_signing_command(self):
356
 
        my_config = self._get_sample_config()
 
272
        config_file = StringIO(sample_config_text)
 
273
        my_config = config.GlobalConfig()
 
274
        my_config._parser = my_config._get_parser(file=config_file)
357
275
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
358
276
        self.assertEqual(False, my_config.signature_needed())
359
277
 
360
 
    def _get_empty_config(self):
361
 
        config_file = StringIO("")
362
 
        my_config = config.GlobalConfig()
363
 
        my_config._parser = my_config._get_parser(file=config_file)
364
 
        return my_config
365
 
 
366
278
    def test_gpg_signing_command_unset(self):
367
 
        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)
368
282
        self.assertEqual("gpg", my_config.gpg_signing_command())
369
283
 
370
 
    def test_get_user_option_default(self):
371
 
        my_config = self._get_empty_config()
372
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
373
 
 
374
 
    def test_get_user_option_global(self):
375
 
        my_config = self._get_sample_config()
376
 
        self.assertEqual("something",
377
 
                         my_config.get_user_option('user_global_option'))
378
 
        
379
 
    def test_post_commit_default(self):
380
 
        my_config = self._get_sample_config()
381
 
        self.assertEqual(None, my_config.post_commit())
382
 
 
383
 
    def test_configured_logformat(self):
384
 
        my_config = self._get_sample_config()
385
 
        self.assertEqual("short", my_config.log_format())
386
 
 
387
 
    def test_get_alias(self):
388
 
        my_config = self._get_sample_config()
389
 
        self.assertEqual('help', my_config.get_alias('h'))
390
 
 
391
 
    def test_get_no_alias(self):
392
 
        my_config = self._get_sample_config()
393
 
        self.assertEqual(None, my_config.get_alias('foo'))
394
 
 
395
 
    def test_get_long_alias(self):
396
 
        my_config = self._get_sample_config()
397
 
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
398
284
 
399
285
class TestLocationConfig(TestCase):
400
286
 
403
289
        self.assertRaises(TypeError, config.LocationConfig)
404
290
 
405
291
    def test_branch_calls_read_filenames(self):
406
 
        # This is testing the correct file names are provided.
407
 
        # TODO: consolidate with the test for GlobalConfigs filename checks.
408
 
        #
409
292
        # replace the class that is constructured, to check its parameters
410
 
        oldparserclass = config.ConfigObj
411
 
        config.ConfigObj = InstrumentedConfigObj
 
293
        oldparserclass = config.ConfigParser
 
294
        config.ConfigParser = InstrumentedConfigParser
412
295
        my_config = config.LocationConfig('http://www.example.com')
413
296
        try:
414
297
            parser = my_config._get_parser()
415
298
        finally:
416
 
            config.ConfigObj = oldparserclass
417
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
418
 
        self.assertEqual(parser._calls,
419
 
                         [('__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()])])
420
302
 
421
303
    def test_get_global_config(self):
422
304
        my_config = config.LocationConfig('http://example.com')
527
409
        self.get_location_config('/a')
528
410
        self.assertEqual("false", self.my_config.gpg_signing_command())
529
411
 
530
 
    def test_get_user_option_global(self):
531
 
        self.get_location_config('/a')
532
 
        self.assertEqual('something',
533
 
                         self.my_config.get_user_option('user_global_option'))
534
 
 
535
 
    def test_get_user_option_local(self):
536
 
        self.get_location_config('/a')
537
 
        self.assertEqual('local',
538
 
                         self.my_config.get_user_option('user_local_option'))
539
 
        
540
 
    def test_post_commit_default(self):
541
 
        self.get_location_config('/a/c')
542
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
543
 
                         self.my_config.post_commit())
544
 
 
545
 
 
546
 
class TestLocationConfig(TestCaseInTempDir):
547
 
 
548
 
    def get_location_config(self, location, global_config=None):
549
 
        if global_config is None:
550
 
            global_file = StringIO(sample_config_text.encode('utf-8'))
551
 
        else:
552
 
            global_file = StringIO(global_config.encode('utf-8'))
553
 
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
554
 
        self.my_config = config.LocationConfig(location)
555
 
        self.my_config._get_parser(branches_file)
556
 
        self.my_config._get_global_config()._get_parser(global_file)
557
 
 
558
 
    def test_set_user_setting_sets_and_saves(self):
559
 
        self.get_location_config('/a/c')
560
 
        record = InstrumentedConfigObj("foo")
561
 
        self.my_config._parser = record
562
 
 
563
 
        real_mkdir = os.mkdir
564
 
        self.created = False
565
 
        def checked_mkdir(path, mode=0777):
566
 
            self.log('making directory: %s', path)
567
 
            real_mkdir(path, mode)
568
 
            self.created = True
569
 
 
570
 
        os.mkdir = checked_mkdir
571
 
        try:
572
 
            self.my_config.set_user_option('foo', 'bar')
573
 
        finally:
574
 
            os.mkdir = real_mkdir
575
 
 
576
 
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
577
 
        self.assertEqual([('__contains__', '/a/c'),
578
 
                          ('__contains__', '/a/c/'),
579
 
                          ('__setitem__', '/a/c', {}),
580
 
                          ('__getitem__', '/a/c'),
581
 
                          ('__setitem__', 'foo', 'bar'),
582
 
                          ('write',)],
583
 
                         record._calls[1:])
584
 
 
585
412
 
586
413
class TestBranchConfigItems(TestCase):
587
414
 
590
417
        my_config = config.BranchConfig(branch)
591
418
        self.assertEqual("Robert Collins <robertc@example.net>",
592
419
                         my_config._get_user_id())
593
 
        branch.control_files.email = "John"
 
420
        branch.email = "John"
594
421
        self.assertEqual("John", my_config._get_user_id())
595
422
 
596
423
    def test_not_set_in_branch(self):
597
424
        branch = FakeBranch()
598
425
        my_config = config.BranchConfig(branch)
599
 
        branch.control_files.email = None
600
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
426
        branch.email = None
 
427
        config_file = StringIO(sample_config_text)
601
428
        (my_config._get_location_config().
602
429
            _get_global_config()._get_parser(config_file))
603
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
 
430
        self.assertEqual("Robert Collins <robertc@example.com>",
604
431
                         my_config._get_user_id())
605
 
        branch.control_files.email = "John"
 
432
        branch.email = "John"
606
433
        self.assertEqual("John", my_config._get_user_id())
607
434
 
608
435
    def test_BZREMAIL_OVERRIDES(self):
623
450
    def test_gpg_signing_command(self):
624
451
        branch = FakeBranch()
625
452
        my_config = config.BranchConfig(branch)
626
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
453
        config_file = StringIO(sample_config_text)
627
454
        (my_config._get_location_config().
628
455
            _get_global_config()._get_parser(config_file))
629
456
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
630
 
 
631
 
    def test_get_user_option_global(self):
632
 
        branch = FakeBranch()
633
 
        my_config = config.BranchConfig(branch)
634
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
635
 
        (my_config._get_location_config().
636
 
            _get_global_config()._get_parser(config_file))
637
 
        self.assertEqual('something',
638
 
                         my_config.get_user_option('user_global_option'))
639
 
 
640
 
    def test_post_commit_default(self):
641
 
        branch = FakeBranch()
642
 
        branch.base='/a/c'
643
 
        my_config = config.BranchConfig(branch)
644
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
645
 
        (my_config._get_location_config().
646
 
            _get_global_config()._get_parser(config_file))
647
 
        branch_file = StringIO(sample_branches_text)
648
 
        my_config._get_location_config()._get_parser(branch_file)
649
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
650
 
                         my_config.post_commit())
651
 
 
652
 
 
653
 
class TestMailAddressExtraction(TestCase):
654
 
 
655
 
    def test_extract_email_address(self):
656
 
        self.assertEqual('jane@test.com',
657
 
                         config.extract_email_address('Jane <jane@test.com>'))
658
 
        self.assertRaises(errors.BzrError,
659
 
                          config.extract_email_address, 'Jane Tester')