~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: John Arbash Meinel
  • Date: 2006-10-24 14:12:53 UTC
  • mto: This revision was merged to the branch mainline in revision 2095.
  • Revision ID: john@arbash-meinel.com-20061024141253-783fba812b197b70
(John Arbash Meinel) Update version information for 0.13 development

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
    errors,
29
29
    osutils,
30
30
    urlutils,
31
 
    trace,
32
31
    )
33
32
from bzrlib.branch import Branch
34
33
from bzrlib.bzrdir import BzrDir
36
35
 
37
36
 
38
37
sample_long_alias="log -r-15..-1 --line"
39
 
sample_config_text = u"""
40
 
[DEFAULT]
41
 
email=Erik B\u00e5gfors <erik@bagfors.nu>
42
 
editor=vim
43
 
gpg_signing_command=gnome-gpg
44
 
log_format=short
45
 
user_global_option=something
46
 
[ALIASES]
47
 
h=help
48
 
ll=""" + sample_long_alias + "\n"
49
 
 
50
 
 
51
 
sample_always_signatures = """
52
 
[DEFAULT]
53
 
check_signatures=ignore
54
 
create_signatures=always
55
 
"""
56
 
 
57
 
sample_ignore_signatures = """
58
 
[DEFAULT]
59
 
check_signatures=require
60
 
create_signatures=never
61
 
"""
62
 
 
63
 
sample_maybe_signatures = """
64
 
[DEFAULT]
65
 
check_signatures=ignore
66
 
create_signatures=when-required
67
 
"""
68
 
 
69
 
sample_branches_text = """
70
 
[http://www.example.com]
71
 
# Top level policy
72
 
email=Robert Collins <robertc@example.org>
73
 
normal_option = normal
74
 
appendpath_option = append
75
 
appendpath_option:policy = appendpath
76
 
norecurse_option = norecurse
77
 
norecurse_option:policy = norecurse
78
 
[http://www.example.com/ignoreparent]
79
 
# different project: ignore parent dir config
80
 
ignore_parents=true
81
 
[http://www.example.com/norecurse]
82
 
# configuration items that only apply to this dir
83
 
recurse=false
84
 
normal_option = norecurse
85
 
[http://www.example.com/dir]
86
 
appendpath_option = normal
87
 
[/b/]
88
 
check_signatures=require
89
 
# test trailing / matching with no children
90
 
[/a/]
91
 
check_signatures=check-available
92
 
gpg_signing_command=false
93
 
user_local_option=local
94
 
# test trailing / matching
95
 
[/a/*]
96
 
#subdirs will match but not the parent
97
 
[/a/c]
98
 
check_signatures=ignore
99
 
post_commit=bzrlib.tests.test_config.post_commit
100
 
#testing explicit beats globs
101
 
"""
 
38
sample_config_text = ("[DEFAULT]\n"
 
39
                      u"email=Erik B\u00e5gfors <erik@bagfors.nu>\n"
 
40
                      "editor=vim\n"
 
41
                      "gpg_signing_command=gnome-gpg\n"
 
42
                      "log_format=short\n"
 
43
                      "user_global_option=something\n"
 
44
                      "[ALIASES]\n"
 
45
                      "h=help\n"
 
46
                      "ll=" + sample_long_alias + "\n")
 
47
 
 
48
 
 
49
sample_always_signatures = ("[DEFAULT]\n"
 
50
                            "check_signatures=ignore\n"
 
51
                            "create_signatures=always")
 
52
 
 
53
 
 
54
sample_ignore_signatures = ("[DEFAULT]\n"
 
55
                            "check_signatures=require\n"
 
56
                            "create_signatures=never")
 
57
 
 
58
 
 
59
sample_maybe_signatures = ("[DEFAULT]\n"
 
60
                            "check_signatures=ignore\n"
 
61
                            "create_signatures=when-required")
 
62
 
 
63
 
 
64
sample_branches_text = ("[http://www.example.com]\n"
 
65
                        "# Top level policy\n"
 
66
                        "email=Robert Collins <robertc@example.org>\n"
 
67
                        "[http://www.example.com/ignoreparent]\n"
 
68
                        "# different project: ignore parent dir config\n"
 
69
                        "ignore_parents=true\n"
 
70
                        "[http://www.example.com/norecurse]\n"
 
71
                        "# configuration items that only apply to this dir\n"
 
72
                        "recurse=false\n"
 
73
                        "[/b/]\n"
 
74
                        "check_signatures=require\n"
 
75
                        "# test trailing / matching with no children\n"
 
76
                        "[/a/]\n"
 
77
                        "check_signatures=check-available\n"
 
78
                        "gpg_signing_command=false\n"
 
79
                        "user_local_option=local\n"
 
80
                        "# test trailing / matching\n"
 
81
                        "[/a/*]\n"
 
82
                        "#subdirs will match but not the parent\n"
 
83
                        "[/a/c]\n"
 
84
                        "check_signatures=ignore\n"
 
85
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
 
86
                        "#testing explicit beats globs\n")
 
87
 
102
88
 
103
89
 
104
90
class InstrumentedConfigObj(object):
118
104
    def __setitem__(self, key, value):
119
105
        self._calls.append(('__setitem__', key, value))
120
106
 
121
 
    def __delitem__(self, key):
122
 
        self._calls.append(('__delitem__', key))
123
 
 
124
 
    def keys(self):
125
 
        self._calls.append(('keys',))
126
 
        return []
127
 
 
128
107
    def write(self, arg):
129
108
        self._calls.append(('write',))
130
109
 
131
 
    def as_bool(self, value):
132
 
        self._calls.append(('as_bool', value))
133
 
        return False
134
 
 
135
 
    def get_value(self, section, name):
136
 
        self._calls.append(('get_value', section, name))
137
 
        return None
138
 
 
139
110
 
140
111
class FakeBranch(object):
141
112
 
270
241
 
271
242
    def setUp(self):
272
243
        super(TestConfigPath, self).setUp()
 
244
        self.old_home = os.environ.get('HOME', None)
 
245
        self.old_appdata = os.environ.get('APPDATA', None)
273
246
        os.environ['HOME'] = '/home/bogus'
274
 
        if sys.platform == 'win32':
275
 
            os.environ['BZR_HOME'] = \
276
 
                r'C:\Documents and Settings\bogus\Application Data'
 
247
        os.environ['APPDATA'] = \
 
248
            r'C:\Documents and Settings\bogus\Application Data'
277
249
 
 
250
    def tearDown(self):
 
251
        if self.old_home is None:
 
252
            del os.environ['HOME']
 
253
        else:
 
254
            os.environ['HOME'] = self.old_home
 
255
        if self.old_appdata is None:
 
256
            del os.environ['APPDATA']
 
257
        else:
 
258
            os.environ['APPDATA'] = self.old_appdata
 
259
        super(TestConfigPath, self).tearDown()
 
260
    
278
261
    def test_config_dir(self):
279
262
        if sys.platform == 'win32':
280
263
            self.assertEqual(config.config_dir(), 
402
385
 
403
386
    def test_config_creates_local(self):
404
387
        """Creating a new entry in config uses a local path."""
405
 
        branch = self.make_branch('branch', format='knit')
 
388
        branch = self.make_branch('branch')
406
389
        branch.set_push_location('http://foobar')
407
390
        locations = config.locations_config_filename()
408
391
        local_path = osutils.getcwd().encode('utf8')
409
392
        # Surprisingly ConfigObj doesn't create a trailing newline
410
393
        self.check_file_contents(locations,
411
 
            '[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (local_path,))
412
 
 
413
 
    def test_autonick_urlencoded(self):
414
 
        b = self.make_branch('!repo')
415
 
        self.assertEqual('!repo', b.get_config().get_nickname())
416
 
 
417
 
    def test_warn_if_masked(self):
418
 
        _warning = trace.warning
419
 
        warnings = []
420
 
        def warning(*args):
421
 
            warnings.append(args[0] % args[1:])
422
 
 
423
 
        def set_option(store, warn_masked=True):
424
 
            warnings[:] = []
425
 
            conf.set_user_option('example_option', repr(store), store=store,
426
 
                                 warn_masked=warn_masked)
427
 
        def assertWarning(warning):
428
 
            if warning is None:
429
 
                self.assertEqual(0, len(warnings))
430
 
            else:
431
 
                self.assertEqual(1, len(warnings))
432
 
                self.assertEqual(warning, warnings[0])
433
 
        trace.warning = warning
434
 
        try:
435
 
            branch = self.make_branch('.')
436
 
            conf = branch.get_config()
437
 
            set_option(config.STORE_GLOBAL)
438
 
            assertWarning(None)
439
 
            set_option(config.STORE_BRANCH)
440
 
            assertWarning(None)
441
 
            set_option(config.STORE_GLOBAL)
442
 
            assertWarning('Value "4" is masked by "3" from branch.conf')
443
 
            set_option(config.STORE_GLOBAL, warn_masked=False)
444
 
            assertWarning(None)
445
 
            set_option(config.STORE_LOCATION)
446
 
            assertWarning(None)
447
 
            set_option(config.STORE_BRANCH)
448
 
            assertWarning('Value "3" is masked by "0" from locations.conf')
449
 
            set_option(config.STORE_BRANCH, warn_masked=False)
450
 
            assertWarning(None)
451
 
        finally:
452
 
            trace.warning = _warning
 
394
            '[%s/branch]\npush_location = http://foobar' % (local_path,))
453
395
 
454
396
 
455
397
class TestGlobalConfigItems(TestCase):
624
566
        self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
625
567
                         self.my_location_config._get_matching_sections())
626
568
 
 
569
    def test__get_matching_sections_norecurse(self):
 
570
        self.get_branch_config('http://www.example.com/norecurse')
 
571
        self.assertEqual([('http://www.example.com/norecurse', ''),
 
572
                          ('http://www.example.com', 'norecurse')],
 
573
                         self.my_location_config._get_matching_sections())
 
574
 
 
575
    def test__get_matching_sections_norecurse_subdir(self):
 
576
        self.get_branch_config(
 
577
            'http://www.example.com/norecurse/childbranch')
 
578
        self.assertEqual([('http://www.example.com', 'norecurse/childbranch')],
 
579
                         self.my_location_config._get_matching_sections())
 
580
 
627
581
    def test__get_matching_sections_subdir_trailing_slash(self):
628
582
        self.get_branch_config('/b')
629
583
        self.assertEqual([('/b/', '')],
653
607
        self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
654
608
                         self.my_location_config._get_matching_sections())
655
609
 
656
 
    def test__get_option_policy_normal(self):
657
 
        self.get_branch_config('http://www.example.com')
658
 
        self.assertEqual(
659
 
            self.my_location_config._get_config_policy(
660
 
            'http://www.example.com', 'normal_option'),
661
 
            config.POLICY_NONE)
662
 
 
663
 
    def test__get_option_policy_norecurse(self):
664
 
        self.get_branch_config('http://www.example.com')
665
 
        self.assertEqual(
666
 
            self.my_location_config._get_option_policy(
667
 
            'http://www.example.com', 'norecurse_option'),
668
 
            config.POLICY_NORECURSE)
669
 
        # Test old recurse=False setting:
670
 
        self.assertEqual(
671
 
            self.my_location_config._get_option_policy(
672
 
            'http://www.example.com/norecurse', 'normal_option'),
673
 
            config.POLICY_NORECURSE)
674
 
 
675
 
    def test__get_option_policy_normal(self):
676
 
        self.get_branch_config('http://www.example.com')
677
 
        self.assertEqual(
678
 
            self.my_location_config._get_option_policy(
679
 
            'http://www.example.com', 'appendpath_option'),
680
 
            config.POLICY_APPENDPATH)
681
 
 
682
610
    def test_location_without_username(self):
683
611
        self.get_branch_config('http://www.example.com/ignoreparent')
684
612
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
736
664
        self.assertEqual('local',
737
665
                         self.my_config.get_user_option('user_local_option'))
738
666
 
739
 
    def test_get_user_option_appendpath(self):
740
 
        # returned as is for the base path:
741
 
        self.get_branch_config('http://www.example.com')
742
 
        self.assertEqual('append',
743
 
                         self.my_config.get_user_option('appendpath_option'))
744
 
        # Extra path components get appended:
745
 
        self.get_branch_config('http://www.example.com/a/b/c')
746
 
        self.assertEqual('append/a/b/c',
747
 
                         self.my_config.get_user_option('appendpath_option'))
748
 
        # Overriden for http://www.example.com/dir, where it is a
749
 
        # normal option:
750
 
        self.get_branch_config('http://www.example.com/dir/a/b/c')
751
 
        self.assertEqual('normal',
752
 
                         self.my_config.get_user_option('appendpath_option'))
753
 
 
754
 
    def test_get_user_option_norecurse(self):
755
 
        self.get_branch_config('http://www.example.com')
756
 
        self.assertEqual('norecurse',
757
 
                         self.my_config.get_user_option('norecurse_option'))
758
 
        self.get_branch_config('http://www.example.com/dir')
759
 
        self.assertEqual(None,
760
 
                         self.my_config.get_user_option('norecurse_option'))
761
 
        # http://www.example.com/norecurse is a recurse=False section
762
 
        # that redefines normal_option.  Subdirectories do not pick up
763
 
        # this redefinition.
764
 
        self.get_branch_config('http://www.example.com/norecurse')
765
 
        self.assertEqual('norecurse',
766
 
                         self.my_config.get_user_option('normal_option'))
767
 
        self.get_branch_config('http://www.example.com/norecurse/subdir')
768
 
        self.assertEqual('normal',
769
 
                         self.my_config.get_user_option('normal_option'))
770
 
 
771
 
    def test_set_user_option_norecurse(self):
772
 
        self.get_branch_config('http://www.example.com')
773
 
        self.my_config.set_user_option('foo', 'bar',
774
 
                                       store=config.STORE_LOCATION_NORECURSE)
775
 
        self.assertEqual(
776
 
            self.my_location_config._get_option_policy(
777
 
            'http://www.example.com', 'foo'),
778
 
            config.POLICY_NORECURSE)
779
 
 
780
 
    def test_set_user_option_appendpath(self):
781
 
        self.get_branch_config('http://www.example.com')
782
 
        self.my_config.set_user_option('foo', 'bar',
783
 
                                       store=config.STORE_LOCATION_APPENDPATH)
784
 
        self.assertEqual(
785
 
            self.my_location_config._get_option_policy(
786
 
            'http://www.example.com', 'foo'),
787
 
            config.POLICY_APPENDPATH)
788
 
 
789
 
    def test_set_user_option_change_policy(self):
790
 
        self.get_branch_config('http://www.example.com')
791
 
        self.my_config.set_user_option('norecurse_option', 'normal',
792
 
                                       store=config.STORE_LOCATION)
793
 
        self.assertEqual(
794
 
            self.my_location_config._get_option_policy(
795
 
            'http://www.example.com', 'norecurse_option'),
796
 
            config.POLICY_NONE)
797
 
 
798
 
    def test_set_user_option_recurse_false_section(self):
799
 
        # The following section has recurse=False set.  The test is to
800
 
        # make sure that a normal option can be added to the section,
801
 
        # converting recurse=False to the norecurse policy.
802
 
        self.get_branch_config('http://www.example.com/norecurse')
803
 
        self.callDeprecated(['The recurse option is deprecated as of 0.14.  '
804
 
                             'The section "http://www.example.com/norecurse" '
805
 
                             'has been converted to use policies.'],
806
 
                            self.my_config.set_user_option,
807
 
                            'foo', 'bar', store=config.STORE_LOCATION)
808
 
        self.assertEqual(
809
 
            self.my_location_config._get_option_policy(
810
 
            'http://www.example.com/norecurse', 'foo'),
811
 
            config.POLICY_NONE)
812
 
        # The previously existing option is still norecurse:
813
 
        self.assertEqual(
814
 
            self.my_location_config._get_option_policy(
815
 
            'http://www.example.com/norecurse', 'normal_option'),
816
 
            config.POLICY_NORECURSE)
817
 
 
818
667
    def test_post_commit_default(self):
819
668
        self.get_branch_config('/a/c')
820
669
        self.assertEqual('bzrlib.tests.test_config.post_commit',
847
696
 
848
697
        os.mkdir = checked_mkdir
849
698
        try:
850
 
            self.callDeprecated(['The recurse option is deprecated as of '
851
 
                                 '0.14.  The section "/a/c" has been '
852
 
                                 'converted to use policies.'],
853
 
                                self.my_config.set_user_option,
854
 
                                'foo', 'bar', store=config.STORE_LOCATION)
 
699
            self.my_config.set_user_option('foo', 'bar', local=True)
855
700
        finally:
856
701
            os.mkdir = real_mkdir
857
702
 
861
706
                          ('__setitem__', '/a/c', {}),
862
707
                          ('__getitem__', '/a/c'),
863
708
                          ('__setitem__', 'foo', 'bar'),
864
 
                          ('__getitem__', '/a/c'),
865
 
                          ('as_bool', 'recurse'),
866
 
                          ('__getitem__', '/a/c'),
867
 
                          ('__delitem__', 'recurse'),
868
 
                          ('__getitem__', '/a/c'),
869
 
                          ('keys',),
870
 
                          ('__getitem__', '/a/c'),
871
 
                          ('__contains__', 'foo:policy'),
872
709
                          ('write',)],
873
710
                         record._calls[1:])
874
711
 
880
717
            self.my_config.branch.control_files.files['branch.conf'], 
881
718
            'foo = bar')
882
719
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
883
 
        self.my_config.set_user_option('foo', 'baz',
884
 
                                       store=config.STORE_LOCATION)
 
720
        self.my_config.set_user_option('foo', 'baz', local=True)
885
721
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
886
722
        self.my_config.set_user_option('foo', 'qux')
887
723
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
985
821
        # post-commit is ignored when bresent in branch data
986
822
        self.assertEqual('bzrlib.tests.test_config.post_commit',
987
823
                         my_config.post_commit())
988
 
        my_config.set_user_option('post_commit', 'rmtree_root',
989
 
                                  store=config.STORE_LOCATION)
 
824
        my_config.set_user_option('post_commit', 'rmtree_root', local=True)
990
825
        self.assertEqual('rmtree_root', my_config.post_commit())
991
826
 
992
827
    def test_config_precedence(self):
1013
848
                         config.extract_email_address('Jane <jane@test.com>'))
1014
849
        self.assertRaises(errors.NoEmailInUsername,
1015
850
                          config.extract_email_address, 'Jane Tester')
1016
 
 
1017
 
 
1018
 
class TestTreeConfig(TestCaseWithTransport):
1019
 
 
1020
 
    def test_get_value(self):
1021
 
        """Test that retreiving a value from a section is possible"""
1022
 
        branch = self.make_branch('.')
1023
 
        tree_config = config.TreeConfig(branch)
1024
 
        tree_config.set_option('value', 'key', 'SECTION')
1025
 
        tree_config.set_option('value2', 'key2')
1026
 
        tree_config.set_option('value3-top', 'key3')
1027
 
        tree_config.set_option('value3-section', 'key3', 'SECTION')
1028
 
        value = tree_config.get_option('key', 'SECTION')
1029
 
        self.assertEqual(value, 'value')
1030
 
        value = tree_config.get_option('key2')
1031
 
        self.assertEqual(value, 'value2')
1032
 
        self.assertEqual(tree_config.get_option('non-existant'), None)
1033
 
        value = tree_config.get_option('non-existant', 'SECTION')
1034
 
        self.assertEqual(value, None)
1035
 
        value = tree_config.get_option('non-existant', default='default')
1036
 
        self.assertEqual(value, 'default')
1037
 
        self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1038
 
        value = tree_config.get_option('key2', 'NOSECTION', default='default')
1039
 
        self.assertEqual(value, 'default')
1040
 
        value = tree_config.get_option('key3')
1041
 
        self.assertEqual(value, 'value3-top')
1042
 
        value = tree_config.get_option('key3', 'SECTION')
1043
 
        self.assertEqual(value, 'value3-section')