~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

Merge bzr.dev

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
21
20
from cStringIO import StringIO
22
21
import os
23
22
import sys
35
34
    tests,
36
35
    trace,
37
36
    )
 
37
from bzrlib.util.configobj import configobj
38
38
 
39
39
 
40
40
sample_long_alias="log -r-15..-1 --line"
180
180
 
181
181
class InstrumentedConfig(config.Config):
182
182
    """An instrumented config that supplies stubs for template methods."""
183
 
    
 
183
 
184
184
    def __init__(self):
185
185
        super(InstrumentedConfig, self).__init__()
186
186
        self._calls = []
202
202
active = True
203
203
nonactive = False
204
204
"""
 
205
 
 
206
 
205
207
class TestConfigObj(tests.TestCase):
206
208
    def test_get_bool(self):
207
 
        from bzrlib.config import ConfigObj
208
 
        co = ConfigObj(StringIO(bool_config))
 
209
        co = config.ConfigObj(StringIO(bool_config))
209
210
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
210
211
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
211
212
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
217
218
[section] # line 3
218
219
whocares=notme # line 4
219
220
"""
 
221
 
 
222
 
220
223
class TestConfigObjErrors(tests.TestCase):
221
224
 
222
225
    def test_duplicate_section_name_error_line(self):
223
226
        try:
224
 
            co = ConfigObj(StringIO(erroneous_config), raise_errors=True)
 
227
            co = configobj.ConfigObj(StringIO(erroneous_config),
 
228
                                     raise_errors=True)
225
229
        except config.configobj.DuplicateError, e:
226
230
            self.assertEqual(3, e.line_number)
227
231
        else:
228
232
            self.fail('Error in config file not detected')
229
233
 
 
234
 
230
235
class TestConfig(tests.TestCase):
231
236
 
232
237
    def test_constructs(self):
233
238
        config.Config()
234
 
 
 
239
 
235
240
    def test_no_default_editor(self):
236
241
        self.assertRaises(NotImplementedError, config.Config().get_editor)
237
242
 
291
296
        if sys.platform == 'win32':
292
297
            os.environ['BZR_HOME'] = \
293
298
                r'C:\Documents and Settings\bogus\Application Data'
 
299
            self.bzr_home = \
 
300
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
 
301
        else:
 
302
            self.bzr_home = '/home/bogus/.bazaar'
294
303
 
295
304
    def test_config_dir(self):
296
 
        if sys.platform == 'win32':
297
 
            self.assertEqual(config.config_dir(), 
298
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
299
 
        else:
300
 
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
 
305
        self.assertEqual(config.config_dir(), self.bzr_home)
301
306
 
302
307
    def test_config_filename(self):
303
 
        if sys.platform == 'win32':
304
 
            self.assertEqual(config.config_filename(), 
305
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
306
 
        else:
307
 
            self.assertEqual(config.config_filename(),
308
 
                             '/home/bogus/.bazaar/bazaar.conf')
 
308
        self.assertEqual(config.config_filename(),
 
309
                         self.bzr_home + '/bazaar.conf')
309
310
 
310
311
    def test_branches_config_filename(self):
311
 
        if sys.platform == 'win32':
312
 
            self.assertEqual(config.branches_config_filename(), 
313
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
314
 
        else:
315
 
            self.assertEqual(config.branches_config_filename(),
316
 
                             '/home/bogus/.bazaar/branches.conf')
 
312
        self.assertEqual(config.branches_config_filename(),
 
313
                         self.bzr_home + '/branches.conf')
317
314
 
318
315
    def test_locations_config_filename(self):
319
 
        if sys.platform == 'win32':
320
 
            self.assertEqual(config.locations_config_filename(), 
321
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
322
 
        else:
323
 
            self.assertEqual(config.locations_config_filename(),
324
 
                             '/home/bogus/.bazaar/locations.conf')
 
316
        self.assertEqual(config.locations_config_filename(),
 
317
                         self.bzr_home + '/locations.conf')
325
318
 
326
319
    def test_authentication_config_filename(self):
327
 
        if sys.platform == 'win32':
328
 
            self.assertEqual(config.authentication_config_filename(), 
329
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/authentication.conf')
330
 
        else:
331
 
            self.assertEqual(config.authentication_config_filename(),
332
 
                             '/home/bogus/.bazaar/authentication.conf')
 
320
        self.assertEqual(config.authentication_config_filename(),
 
321
                         self.bzr_home + '/authentication.conf')
 
322
 
333
323
 
334
324
class TestIniConfig(tests.TestCase):
335
325
 
341
331
        my_config = config.IniBasedConfig(None)
342
332
        self.failUnless(
343
333
            isinstance(my_config._get_parser(file=config_file),
344
 
                        ConfigObj))
 
334
                        configobj.ConfigObj))
345
335
 
346
336
    def test_cached(self):
347
337
        config_file = StringIO(sample_config_text.encode('utf-8'))
356
346
        my_config = config.GlobalConfig()
357
347
 
358
348
    def test_calls_read_filenames(self):
359
 
        # replace the class that is constructured, to check its parameters
 
349
        # replace the class that is constructed, to check its parameters
360
350
        oldparserclass = config.ConfigObj
361
351
        config.ConfigObj = InstrumentedConfigObj
362
352
        my_config = config.GlobalConfig()
433
423
        local_path = osutils.getcwd().encode('utf8')
434
424
        # Surprisingly ConfigObj doesn't create a trailing newline
435
425
        self.check_file_contents(locations,
436
 
            '[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (local_path,))
 
426
                                 '[%s/branch]\n'
 
427
                                 'push_location = http://foobar\n'
 
428
                                 'push_location:policy = norecurse'
 
429
                                 % (local_path,))
437
430
 
438
431
    def test_autonick_urlencoded(self):
439
432
        b = self.make_branch('!repo')
557
550
        my_config = self._get_sample_config()
558
551
        self.assertEqual("something",
559
552
                         my_config.get_user_option('user_global_option'))
560
 
        
 
553
 
561
554
    def test_post_commit_default(self):
562
555
        my_config = self._get_sample_config()
563
556
        self.assertEqual(None, my_config.post_commit())
589
582
        # This is testing the correct file names are provided.
590
583
        # TODO: consolidate with the test for GlobalConfigs filename checks.
591
584
        #
592
 
        # replace the class that is constructured, to check its parameters
 
585
        # replace the class that is constructed, to check its parameters
593
586
        oldparserclass = config.ConfigObj
594
587
        config.ConfigObj = InstrumentedConfigObj
595
588
        try:
623
616
    def test__get_matching_sections_no_match(self):
624
617
        self.get_branch_config('/')
625
618
        self.assertEqual([], self.my_location_config._get_matching_sections())
626
 
        
 
619
 
627
620
    def test__get_matching_sections_exact(self):
628
621
        self.get_branch_config('http://www.example.com')
629
622
        self.assertEqual([('http://www.example.com', '')],
630
623
                         self.my_location_config._get_matching_sections())
631
 
   
 
624
 
632
625
    def test__get_matching_sections_suffix_does_not(self):
633
626
        self.get_branch_config('http://www.example.com-com')
634
627
        self.assertEqual([], self.my_location_config._get_matching_sections())
646
639
    def test__get_matching_sections_ignoreparent_subdir(self):
647
640
        self.get_branch_config(
648
641
            'http://www.example.com/ignoreparent/childbranch')
649
 
        self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
 
642
        self.assertEqual([('http://www.example.com/ignoreparent',
 
643
                           'childbranch')],
650
644
                         self.my_location_config._get_matching_sections())
651
645
 
652
646
    def test__get_matching_sections_subdir_trailing_slash(self):
732
726
        self.get_branch_config('/a/c')
733
727
        self.assertEqual(config.CHECK_NEVER,
734
728
                         self.my_config.signature_checking())
735
 
        
 
729
 
736
730
    def test_signatures_when_available(self):
737
731
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
738
732
        self.assertEqual(config.CHECK_IF_POSSIBLE,
739
733
                         self.my_config.signature_checking())
740
 
        
 
734
 
741
735
    def test_signatures_always(self):
742
736
        self.get_branch_config('/b')
743
737
        self.assertEqual(config.CHECK_ALWAYS,
744
738
                         self.my_config.signature_checking())
745
 
        
 
739
 
746
740
    def test_gpg_signing_command(self):
747
741
        self.get_branch_config('/b')
748
742
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
902
896
        self.assertIs(self.my_config.get_user_option('foo'), None)
903
897
        self.my_config.set_user_option('foo', 'bar')
904
898
        self.assertEqual(
905
 
            self.my_config.branch.control_files.files['branch.conf'], 
 
899
            self.my_config.branch.control_files.files['branch.conf'],
906
900
            'foo = bar')
907
901
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
908
902
        self.my_config.set_user_option('foo', 'baz',
910
904
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
911
905
        self.my_config.set_user_option('foo', 'qux')
912
906
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
913
 
        
 
907
 
914
908
    def test_get_bzr_remote_path(self):
915
909
        my_config = config.LocationConfig('/a/c')
916
910
        self.assertEqual('bzr', my_config.get_bzr_remote_path())
933
927
 
934
928
class TestBranchConfigItems(tests.TestCaseInTempDir):
935
929
 
936
 
    def get_branch_config(self, global_config=None, location=None, 
 
930
    def get_branch_config(self, global_config=None, location=None,
937
931
                          location_config=None, branch_data_config=None):
938
932
        my_config = config.BranchConfig(FakeBranch(location))
939
933
        if global_config is not None:
954
948
        self.assertEqual("Robert Collins <robertc@example.net>",
955
949
                         my_config.username())
956
950
        branch.control_files.email = "John"
957
 
        my_config.set_user_option('email', 
 
951
        my_config.set_user_option('email',
958
952
                                  "Robert Collins <robertc@example.org>")
959
953
        self.assertEqual("John", my_config.username())
960
954
        branch.control_files.email = None
975
969
        my_config = config.BranchConfig(branch)
976
970
        self.assertEqual("Robert Collins <robertc@example.org>",
977
971
                         my_config.username())
978
 
    
 
972
 
979
973
    def test_signatures_forced(self):
980
974
        my_config = self.get_branch_config(
981
975
            global_config=sample_always_signatures)
1025
1019
    def test_config_precedence(self):
1026
1020
        my_config = self.get_branch_config(global_config=precedence_global)
1027
1021
        self.assertEqual(my_config.get_user_option('option'), 'global')
1028
 
        my_config = self.get_branch_config(global_config=precedence_global, 
 
1022
        my_config = self.get_branch_config(global_config=precedence_global,
1029
1023
                                      branch_data_config=precedence_branch)
1030
1024
        self.assertEqual(my_config.get_user_option('option'), 'branch')
1031
 
        my_config = self.get_branch_config(global_config=precedence_global, 
 
1025
        my_config = self.get_branch_config(global_config=precedence_global,
1032
1026
                                      branch_data_config=precedence_branch,
1033
1027
                                      location_config=precedence_location)
1034
1028
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
1035
 
        my_config = self.get_branch_config(global_config=precedence_global, 
 
1029
        my_config = self.get_branch_config(global_config=precedence_global,
1036
1030
                                      branch_data_config=precedence_branch,
1037
1031
                                      location_config=precedence_location,
1038
1032
                                      location='http://example.com/specific')