~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

[merge] jam-integration

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.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.selftest.testconfig.post_commit\n"
 
69
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
70
70
                        "#testing explicit beats globs\n")
71
71
 
72
72
 
95
95
 
96
96
    def __init__(self):
97
97
        self.base = "http://example.com/branches/demo"
 
98
        self.control_files = FakeControlFiles()
 
99
 
 
100
 
 
101
class FakeControlFiles(object):
 
102
 
 
103
    def __init__(self):
98
104
        self.email = 'Robert Collins <robertc@example.net>\n'
99
105
 
100
 
    def controlfile(self, filename, mode):
 
106
    def get_utf8(self, filename):
101
107
        if filename != 'email':
102
108
            raise NotImplementedError
103
109
        if self.email is not None:
104
110
            return StringIO(self.email)
105
 
        raise errors.NoSuchFile
 
111
        raise errors.NoSuchFile(filename)
106
112
 
107
113
 
108
114
class InstrumentedConfig(config.Config):
175
181
 
176
182
    def setUp(self):
177
183
        super(TestConfigPath, self).setUp()
178
 
        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)
179
186
        os.environ['HOME'] = '/home/bogus'
 
187
        os.environ['APPDATA'] = \
 
188
            r'C:\Documents and Settings\bogus\Application Data'
180
189
 
181
190
    def tearDown(self):
182
 
        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
183
199
        super(TestConfigPath, self).tearDown()
184
200
    
185
201
    def test_config_dir(self):
186
 
        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')
187
207
 
188
208
    def test_config_filename(self):
189
 
        self.assertEqual(config.config_filename(),
190
 
                         '/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')
191
215
 
192
216
    def test_branches_config_filename(self):
193
 
        self.assertEqual(config.branches_config_filename(),
194
 
                         '/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')
195
223
 
196
224
class TestIniConfig(TestCase):
197
225
 
325
353
        self.assertEqual(None, my_config.post_commit())
326
354
 
327
355
 
328
 
 
329
356
class TestLocationConfig(TestCase):
330
357
 
331
358
    def test_constructs(self):
469
496
        
470
497
    def test_post_commit_default(self):
471
498
        self.get_location_config('/a/c')
472
 
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
 
499
        self.assertEqual('bzrlib.tests.test_config.post_commit',
473
500
                         self.my_config.post_commit())
474
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
 
475
515
    def test_set_user_setting_sets_and_saves(self):
476
 
        # TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
477
516
        self.get_location_config('/a/c')
478
517
        record = InstrumentedConfigObj("foo")
479
518
        self.my_config._parser = record
480
 
        self.my_config.set_user_option('foo', 'bar')
 
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')
481
534
        self.assertEqual([('__contains__', '/a/c'),
482
535
                          ('__contains__', '/a/c/'),
483
536
                          ('__setitem__', '/a/c', {}),
494
547
        my_config = config.BranchConfig(branch)
495
548
        self.assertEqual("Robert Collins <robertc@example.net>",
496
549
                         my_config._get_user_id())
497
 
        branch.email = "John"
 
550
        branch.control_files.email = "John"
498
551
        self.assertEqual("John", my_config._get_user_id())
499
552
 
500
553
    def test_not_set_in_branch(self):
501
554
        branch = FakeBranch()
502
555
        my_config = config.BranchConfig(branch)
503
 
        branch.email = None
 
556
        branch.control_files.email = None
504
557
        config_file = StringIO(sample_config_text)
505
558
        (my_config._get_location_config().
506
559
            _get_global_config()._get_parser(config_file))
507
560
        self.assertEqual("Robert Collins <robertc@example.com>",
508
561
                         my_config._get_user_id())
509
 
        branch.email = "John"
 
562
        branch.control_files.email = "John"
510
563
        self.assertEqual("John", my_config._get_user_id())
511
564
 
512
565
    def test_BZREMAIL_OVERRIDES(self):
550
603
            _get_global_config()._get_parser(config_file))
551
604
        branch_file = StringIO(sample_branches_text)
552
605
        my_config._get_location_config()._get_parser(branch_file)
553
 
        self.assertEqual('bzrlib.selftest.testconfig.post_commit',
 
606
        self.assertEqual('bzrlib.tests.test_config.post_commit',
554
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')