~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-30 00:00:09 UTC
  • mfrom: (1185.16.134)
  • Revision ID: robertc@robertcollins.net-20051030000009-9db99a338a0dfdac
MergeĀ fromĀ Martin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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"
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
                        "post_commit=bzrlib.selftest.testconfig.post_commit\n"
70
70
                        "#testing explicit beats globs\n")
71
71
 
72
72
 
102
102
            raise NotImplementedError
103
103
        if self.email is not None:
104
104
            return StringIO(self.email)
105
 
        raise errors.NoSuchFile(filename)
 
105
        raise errors.NoSuchFile
106
106
 
107
107
 
108
108
class InstrumentedConfig(config.Config):
175
175
 
176
176
    def setUp(self):
177
177
        super(TestConfigPath, self).setUp()
178
 
        self.old_home = os.environ.get('HOME', None)
179
 
        self.old_appdata = os.environ.get('APPDATA', None)
 
178
        self.oldenv = os.environ.get('HOME', None)
180
179
        os.environ['HOME'] = '/home/bogus'
181
 
        os.environ['APPDATA'] = \
182
 
            r'C:\Documents and Settings\bogus\Application Data'
183
180
 
184
181
    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
 
182
        os.environ['HOME'] = self.oldenv
193
183
        super(TestConfigPath, self).tearDown()
194
184
    
195
185
    def test_config_dir(self):
196
 
        if sys.platform == 'win32':
197
 
            self.assertEqual(config.config_dir(), 
198
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
199
 
        else:
200
 
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
 
186
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
201
187
 
202
188
    def test_config_filename(self):
203
 
        if sys.platform == 'win32':
204
 
            self.assertEqual(config.config_filename(), 
205
 
                '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')
 
189
        self.assertEqual(config.config_filename(),
 
190
                         '/home/bogus/.bazaar/bazaar.conf')
209
191
 
210
192
    def test_branches_config_filename(self):
211
 
        if sys.platform == 'win32':
212
 
            self.assertEqual(config.branches_config_filename(), 
213
 
                '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')
 
193
        self.assertEqual(config.branches_config_filename(),
 
194
                         '/home/bogus/.bazaar/branches.conf')
217
195
 
218
196
class TestIniConfig(TestCase):
219
197
 
347
325
        self.assertEqual(None, my_config.post_commit())
348
326
 
349
327
 
 
328
 
350
329
class TestLocationConfig(TestCase):
351
330
 
352
331
    def test_constructs(self):
490
469
        
491
470
    def test_post_commit_default(self):
492
471
        self.get_location_config('/a/c')
493
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
 
472
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
494
473
                         self.my_config.post_commit())
495
474
 
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
475
    def test_set_user_setting_sets_and_saves(self):
 
476
        # TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
510
477
        self.get_location_config('/a/c')
511
478
        record = InstrumentedConfigObj("foo")
512
479
        self.my_config._parser = record
513
 
 
514
 
        real_mkdir = os.mkdir
515
 
        self.created = False
516
 
        def checked_mkdir(path, mode=0777):
517
 
            self.log('making directory: %s', path)
518
 
            real_mkdir(path, mode)
519
 
            self.created = True
520
 
 
521
 
        os.mkdir = checked_mkdir
522
 
        try:
523
 
            self.my_config.set_user_option('foo', 'bar')
524
 
        finally:
525
 
            os.mkdir = real_mkdir
526
 
 
527
 
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
 
480
        self.my_config.set_user_option('foo', 'bar')
528
481
        self.assertEqual([('__contains__', '/a/c'),
529
482
                          ('__contains__', '/a/c/'),
530
483
                          ('__setitem__', '/a/c', {}),
597
550
            _get_global_config()._get_parser(config_file))
598
551
        branch_file = StringIO(sample_branches_text)
599
552
        my_config._get_location_config()._get_parser(branch_file)
600
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
 
553
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
601
554
                         my_config.post_commit())
602
 
 
603
 
 
604
 
class TestMailAddressExtraction(TestCase):
605
 
 
606
 
    def test_extract_email_address(self):
607
 
        self.assertEqual('jane@test.com',
608
 
                         config.extract_email_address('Jane <jane@test.com>'))
609
 
        self.assertRaises(errors.BzrError,
610
 
                          config.extract_email_address, 'Jane Tester')