~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-16 08:26:43 UTC
  • mto: This revision was merged to the branch mainline in revision 1459.
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051016082643-a2300b765aea49b4
unify __contains__ for TransportStore classes

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"
58
57
                        "# test trailing / matching with no children\n"
59
58
                        "[/a/]\n"
60
59
                        "check_signatures=check-available\n"
61
 
                        "gpg_signing_command=false\n"
62
 
                        "user_local_option=local\n"
63
60
                        "# test trailing / matching\n"
64
61
                        "[/a/*]\n"
65
62
                        "#subdirs will match but not the parent\n"
66
63
                        "recurse=False\n"
67
64
                        "[/a/c]\n"
68
65
                        "check_signatures=ignore\n"
69
 
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
70
66
                        "#testing explicit beats globs\n")
71
67
 
72
68
 
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',))
 
69
class InstrumentedConfigParser(object):
 
70
    """A config parser look-enough-alike to record calls made to it."""
 
71
 
 
72
    def __init__(self):
 
73
        self._calls = []
 
74
 
 
75
    def read(self, filenames):
 
76
        self._calls.append(('read', filenames))
92
77
 
93
78
 
94
79
class FakeBranch(object):
158
143
                         my_config.signature_checking())
159
144
        self.assertEqual(['_get_signature_checking'], my_config._calls)
160
145
 
161
 
    def test_gpg_signing_command_default(self):
162
 
        my_config = config.Config()
163
 
        self.assertEqual('gpg', my_config.gpg_signing_command())
164
 
 
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
146
 
174
147
class TestConfigPath(TestCase):
175
148
 
176
149
    def setUp(self):
177
150
        super(TestConfigPath, self).setUp()
178
 
        self.old_home = os.environ.get('HOME', None)
179
 
        self.old_appdata = os.environ.get('APPDATA', None)
 
151
        self.oldenv = os.environ.get('HOME', None)
180
152
        os.environ['HOME'] = '/home/bogus'
181
 
        os.environ['APPDATA'] = \
182
 
            r'C:\Documents and Settings\bogus\Application Data'
183
153
 
184
154
    def tearDown(self):
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
193
 
        super(TestConfigPath, self).tearDown()
 
155
        os.environ['HOME'] = self.oldenv
194
156
    
195
157
    def test_config_dir(self):
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')
 
158
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
201
159
 
202
160
    def test_config_filename(self):
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')
 
161
        self.assertEqual(config.config_filename(),
 
162
                         '/home/bogus/.bazaar/bazaar.conf')
209
163
 
210
164
    def test_branches_config_filename(self):
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')
 
165
        self.assertEqual(config.branches_config_filename(),
 
166
                         '/home/bogus/.bazaar/branches.conf')
217
167
 
218
168
class TestIniConfig(TestCase):
219
169
 
225
175
        my_config = config.IniBasedConfig(None)
226
176
        self.failUnless(
227
177
            isinstance(my_config._get_parser(file=config_file),
228
 
                        ConfigObj))
 
178
                        ConfigParser))
229
179
 
230
180
    def test_cached(self):
231
181
        config_file = StringIO(sample_config_text)
234
184
        self.failUnless(my_config._get_parser() is parser)
235
185
 
236
186
 
 
187
 
237
188
class TestGetConfig(TestCase):
238
189
 
239
190
    def test_constructs(self):
241
192
 
242
193
    def test_calls_read_filenames(self):
243
194
        # replace the class that is constructured, to check its parameters
244
 
        oldparserclass = config.ConfigObj
245
 
        config.ConfigObj = InstrumentedConfigObj
 
195
        oldparserclass = config.ConfigParser
 
196
        config.ConfigParser = InstrumentedConfigParser
246
197
        my_config = config.GlobalConfig()
247
198
        try:
248
199
            parser = my_config._get_parser()
249
200
        finally:
250
 
            config.ConfigObj = oldparserclass
251
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
252
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
 
201
            config.ConfigParser = oldparserclass
 
202
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
203
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
253
204
 
254
205
 
255
206
class TestBranchConfig(TestCaseInTempDir):
267
218
        self.failUnless(location_config is my_config._get_location_config())
268
219
 
269
220
 
270
 
class TestGlobalConfigItems(TestCase):
 
221
class TestConfigItems(TestCase):
 
222
 
 
223
    def setUp(self):
 
224
        super(TestConfigItems, self).setUp()
 
225
        self.bzr_email = os.environ.get('BZREMAIL')
 
226
        if self.bzr_email is not None:
 
227
            del os.environ['BZREMAIL']
 
228
        self.email = os.environ.get('EMAIL')
 
229
        if self.email is not None:
 
230
            del os.environ['EMAIL']
 
231
        self.oldenv = os.environ.get('HOME', None)
 
232
        os.environ['HOME'] = os.getcwd()
 
233
 
 
234
    def tearDown(self):
 
235
        os.environ['HOME'] = self.oldenv
 
236
        if os.environ.get('BZREMAIL') is not None:
 
237
            del os.environ['BZREMAIL']
 
238
        if self.bzr_email is not None:
 
239
            os.environ['BZREMAIL'] = self.bzr_email
 
240
        if self.email is not None:
 
241
            os.environ['EMAIL'] = self.email
 
242
        super(TestConfigItems, self).tearDown()
 
243
 
 
244
 
 
245
class TestGlobalConfigItems(TestConfigItems):
271
246
 
272
247
    def test_user_id(self):
273
248
        config_file = StringIO(sample_config_text)
312
287
                         my_config.signature_checking())
313
288
        self.assertEqual(False, my_config.signature_needed())
314
289
 
315
 
    def _get_sample_config(self):
316
 
        config_file = StringIO(sample_config_text)
317
 
        my_config = config.GlobalConfig()
318
 
        my_config._parser = my_config._get_parser(file=config_file)
319
 
        return my_config
320
 
 
321
 
    def test_gpg_signing_command(self):
322
 
        my_config = self._get_sample_config()
323
 
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
324
 
        self.assertEqual(False, my_config.signature_needed())
325
 
 
326
 
    def _get_empty_config(self):
327
 
        config_file = StringIO("")
328
 
        my_config = config.GlobalConfig()
329
 
        my_config._parser = my_config._get_parser(file=config_file)
330
 
        return my_config
331
 
 
332
 
    def test_gpg_signing_command_unset(self):
333
 
        my_config = self._get_empty_config()
334
 
        self.assertEqual("gpg", my_config.gpg_signing_command())
335
 
 
336
 
    def test_get_user_option_default(self):
337
 
        my_config = self._get_empty_config()
338
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
339
 
 
340
 
    def test_get_user_option_global(self):
341
 
        my_config = self._get_sample_config()
342
 
        self.assertEqual("something",
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())
348
 
 
349
 
 
350
 
class TestLocationConfig(TestCase):
 
290
 
 
291
class TestLocationConfig(TestConfigItems):
351
292
 
352
293
    def test_constructs(self):
353
294
        my_config = config.LocationConfig('http://example.com')
354
295
        self.assertRaises(TypeError, config.LocationConfig)
355
296
 
356
297
    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
 
        #
360
298
        # replace the class that is constructured, to check its parameters
361
 
        oldparserclass = config.ConfigObj
362
 
        config.ConfigObj = InstrumentedConfigObj
 
299
        oldparserclass = config.ConfigParser
 
300
        config.ConfigParser = InstrumentedConfigParser
363
301
        my_config = config.LocationConfig('http://www.example.com')
364
302
        try:
365
303
            parser = my_config._get_parser()
366
304
        finally:
367
 
            config.ConfigObj = oldparserclass
368
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
369
 
        self.assertEqual(parser._calls,
370
 
                         [('__init__', config.branches_config_filename())])
 
305
            config.ConfigParser = oldparserclass
 
306
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
307
        self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
371
308
 
372
309
    def test_get_global_config(self):
373
310
        my_config = config.LocationConfig('http://example.com')
470
407
        self.assertEqual(config.CHECK_ALWAYS,
471
408
                         self.my_config.signature_checking())
472
409
        
473
 
    def test_gpg_signing_command(self):
474
 
        self.get_location_config('/b')
475
 
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
476
 
 
477
 
    def test_gpg_signing_command_missing(self):
478
 
        self.get_location_config('/a')
479
 
        self.assertEqual("false", self.my_config.gpg_signing_command())
480
 
 
481
 
    def test_get_user_option_global(self):
482
 
        self.get_location_config('/a')
483
 
        self.assertEqual('something',
484
 
                         self.my_config.get_user_option('user_global_option'))
485
 
 
486
 
    def test_get_user_option_local(self):
487
 
        self.get_location_config('/a')
488
 
        self.assertEqual('local',
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.tests.test_config.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:])
526
 
 
527
 
 
528
 
class TestBranchConfigItems(TestCase):
 
410
 
 
411
class TestBranchConfigItems(TestConfigItems):
529
412
 
530
413
    def test_user_id(self):
531
414
        branch = FakeBranch()
561
444
        (my_config._get_location_config().
562
445
            _get_global_config()._get_parser(config_file))
563
446
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
564
 
 
565
 
    def test_gpg_signing_command(self):
566
 
        branch = FakeBranch()
567
 
        my_config = config.BranchConfig(branch)
568
 
        config_file = StringIO(sample_config_text)
569
 
        (my_config._get_location_config().
570
 
            _get_global_config()._get_parser(config_file))
571
 
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
572
 
 
573
 
    def test_get_user_option_global(self):
574
 
        branch = FakeBranch()
575
 
        my_config = config.BranchConfig(branch)
576
 
        config_file = StringIO(sample_config_text)
577
 
        (my_config._get_location_config().
578
 
            _get_global_config()._get_parser(config_file))
579
 
        self.assertEqual('something',
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.tests.test_config.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')