~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
35
35
 
36
36
 
37
37
sample_long_alias="log -r-15..-1 --line"
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/useglobal]\n"
68
 
                        "# different project, forces global lookup\n"
69
 
                        "recurse=false\n"
70
 
                        "[/b/]\n"
71
 
                        "check_signatures=require\n"
72
 
                        "# test trailing / matching with no children\n"
73
 
                        "[/a/]\n"
74
 
                        "check_signatures=check-available\n"
75
 
                        "gpg_signing_command=false\n"
76
 
                        "user_local_option=local\n"
77
 
                        "# test trailing / matching\n"
78
 
                        "[/a/*]\n"
79
 
                        "#subdirs will match but not the parent\n"
80
 
                        "recurse=False\n"
81
 
                        "[/a/c]\n"
82
 
                        "check_signatures=ignore\n"
83
 
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
84
 
                        "#testing explicit beats globs\n")
85
 
 
 
38
sample_config_text = u"""
 
39
[DEFAULT]
 
40
email=Erik B\u00e5gfors <erik@bagfors.nu>
 
41
editor=vim
 
42
gpg_signing_command=gnome-gpg
 
43
log_format=short
 
44
user_global_option=something
 
45
[ALIASES]
 
46
h=help
 
47
ll=""" + sample_long_alias + "\n"
 
48
 
 
49
 
 
50
sample_always_signatures = """
 
51
[DEFAULT]
 
52
check_signatures=ignore
 
53
create_signatures=always
 
54
"""
 
55
 
 
56
sample_ignore_signatures = """
 
57
[DEFAULT]
 
58
check_signatures=require
 
59
create_signatures=never
 
60
"""
 
61
 
 
62
sample_maybe_signatures = """
 
63
[DEFAULT]
 
64
check_signatures=ignore
 
65
create_signatures=when-required
 
66
"""
 
67
 
 
68
sample_branches_text = """
 
69
[http://www.example.com]
 
70
# Top level policy
 
71
email=Robert Collins <robertc@example.org>
 
72
normal_option = normal
 
73
appendpath_option = append
 
74
appendpath_option:policy = appendpath
 
75
norecurse_option = norecurse
 
76
norecurse_option:policy = norecurse
 
77
[http://www.example.com/ignoreparent]
 
78
# different project: ignore parent dir config
 
79
ignore_parents=true
 
80
[http://www.example.com/norecurse]
 
81
# configuration items that only apply to this dir
 
82
recurse=false
 
83
normal_option = norecurse
 
84
[http://www.example.com/dir]
 
85
appendpath_option = normal
 
86
[/b/]
 
87
check_signatures=require
 
88
# test trailing / matching with no children
 
89
[/a/]
 
90
check_signatures=check-available
 
91
gpg_signing_command=false
 
92
user_local_option=local
 
93
# test trailing / matching
 
94
[/a/*]
 
95
#subdirs will match but not the parent
 
96
[/a/c]
 
97
check_signatures=ignore
 
98
post_commit=bzrlib.tests.test_config.post_commit
 
99
#testing explicit beats globs
 
100
"""
86
101
 
87
102
 
88
103
class InstrumentedConfigObj(object):
102
117
    def __setitem__(self, key, value):
103
118
        self._calls.append(('__setitem__', key, value))
104
119
 
 
120
    def __delitem__(self, key):
 
121
        self._calls.append(('__delitem__', key))
 
122
 
 
123
    def keys(self):
 
124
        self._calls.append(('keys',))
 
125
        return []
 
126
 
105
127
    def write(self, arg):
106
128
        self._calls.append(('write',))
107
129
 
 
130
    def as_bool(self, value):
 
131
        self._calls.append(('as_bool', value))
 
132
        return False
 
133
 
 
134
    def get_value(self, section, name):
 
135
        self._calls.append(('get_value', section, name))
 
136
        return None
 
137
 
108
138
 
109
139
class FakeBranch(object):
110
140
 
239
269
 
240
270
    def setUp(self):
241
271
        super(TestConfigPath, self).setUp()
242
 
        self.old_home = os.environ.get('HOME', None)
243
 
        self.old_appdata = os.environ.get('APPDATA', None)
244
272
        os.environ['HOME'] = '/home/bogus'
245
 
        os.environ['APPDATA'] = \
246
 
            r'C:\Documents and Settings\bogus\Application Data'
 
273
        if sys.platform == 'win32':
 
274
            os.environ['BZR_HOME'] = \
 
275
                r'C:\Documents and Settings\bogus\Application Data'
247
276
 
248
 
    def tearDown(self):
249
 
        if self.old_home is None:
250
 
            del os.environ['HOME']
251
 
        else:
252
 
            os.environ['HOME'] = self.old_home
253
 
        if self.old_appdata is None:
254
 
            del os.environ['APPDATA']
255
 
        else:
256
 
            os.environ['APPDATA'] = self.old_appdata
257
 
        super(TestConfigPath, self).tearDown()
258
 
    
259
277
    def test_config_dir(self):
260
278
        if sys.platform == 'win32':
261
279
            self.assertEqual(config.config_dir(), 
383
401
 
384
402
    def test_config_creates_local(self):
385
403
        """Creating a new entry in config uses a local path."""
386
 
        branch = self.make_branch('branch')
 
404
        branch = self.make_branch('branch', format='knit')
387
405
        branch.set_push_location('http://foobar')
388
406
        locations = config.locations_config_filename()
389
407
        local_path = osutils.getcwd().encode('utf8')
390
408
        # Surprisingly ConfigObj doesn't create a trailing newline
391
409
        self.check_file_contents(locations,
392
 
            '[%s/branch]\npush_location = http://foobar' % (local_path,))
 
410
            '[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (local_path,))
 
411
 
 
412
    def test_autonick_urlencoded(self):
 
413
        b = self.make_branch('!repo')
 
414
        self.assertEqual('!repo', b.get_config().get_nickname())
393
415
 
394
416
 
395
417
class TestGlobalConfigItems(TestCase):
535
557
        self.failUnless(isinstance(global_config, config.GlobalConfig))
536
558
        self.failUnless(global_config is my_config._get_global_config())
537
559
 
538
 
    def test__get_section_no_match(self):
 
560
    def test__get_matching_sections_no_match(self):
539
561
        self.get_branch_config('/')
540
 
        self.assertEqual(None, self.my_location_config._get_section())
 
562
        self.assertEqual([], self.my_location_config._get_matching_sections())
541
563
        
542
 
    def test__get_section_exact(self):
 
564
    def test__get_matching_sections_exact(self):
543
565
        self.get_branch_config('http://www.example.com')
544
 
        self.assertEqual('http://www.example.com',
545
 
                         self.my_location_config._get_section())
 
566
        self.assertEqual([('http://www.example.com', '')],
 
567
                         self.my_location_config._get_matching_sections())
546
568
   
547
 
    def test__get_section_suffix_does_not(self):
 
569
    def test__get_matching_sections_suffix_does_not(self):
548
570
        self.get_branch_config('http://www.example.com-com')
549
 
        self.assertEqual(None, self.my_location_config._get_section())
 
571
        self.assertEqual([], self.my_location_config._get_matching_sections())
550
572
 
551
 
    def test__get_section_subdir_recursive(self):
 
573
    def test__get_matching_sections_subdir_recursive(self):
552
574
        self.get_branch_config('http://www.example.com/com')
553
 
        self.assertEqual('http://www.example.com',
554
 
                         self.my_location_config._get_section())
555
 
 
556
 
    def test__get_section_subdir_matches(self):
557
 
        self.get_branch_config('http://www.example.com/useglobal')
558
 
        self.assertEqual('http://www.example.com/useglobal',
559
 
                         self.my_location_config._get_section())
560
 
 
561
 
    def test__get_section_subdir_nonrecursive(self):
 
575
        self.assertEqual([('http://www.example.com', 'com')],
 
576
                         self.my_location_config._get_matching_sections())
 
577
 
 
578
    def test__get_matching_sections_ignoreparent(self):
 
579
        self.get_branch_config('http://www.example.com/ignoreparent')
 
580
        self.assertEqual([('http://www.example.com/ignoreparent', '')],
 
581
                         self.my_location_config._get_matching_sections())
 
582
 
 
583
    def test__get_matching_sections_ignoreparent_subdir(self):
562
584
        self.get_branch_config(
563
 
            'http://www.example.com/useglobal/childbranch')
564
 
        self.assertEqual('http://www.example.com',
565
 
                         self.my_location_config._get_section())
 
585
            'http://www.example.com/ignoreparent/childbranch')
 
586
        self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
 
587
                         self.my_location_config._get_matching_sections())
566
588
 
567
 
    def test__get_section_subdir_trailing_slash(self):
 
589
    def test__get_matching_sections_subdir_trailing_slash(self):
568
590
        self.get_branch_config('/b')
569
 
        self.assertEqual('/b/', self.my_location_config._get_section())
 
591
        self.assertEqual([('/b/', '')],
 
592
                         self.my_location_config._get_matching_sections())
570
593
 
571
 
    def test__get_section_subdir_child(self):
 
594
    def test__get_matching_sections_subdir_child(self):
572
595
        self.get_branch_config('/a/foo')
573
 
        self.assertEqual('/a/*', self.my_location_config._get_section())
 
596
        self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
 
597
                         self.my_location_config._get_matching_sections())
574
598
 
575
 
    def test__get_section_subdir_child_child(self):
 
599
    def test__get_matching_sections_subdir_child_child(self):
576
600
        self.get_branch_config('/a/foo/bar')
577
 
        self.assertEqual('/a/', self.my_location_config._get_section())
 
601
        self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
 
602
                         self.my_location_config._get_matching_sections())
578
603
 
579
 
    def test__get_section_trailing_slash_with_children(self):
 
604
    def test__get_matching_sections_trailing_slash_with_children(self):
580
605
        self.get_branch_config('/a/')
581
 
        self.assertEqual('/a/', self.my_location_config._get_section())
 
606
        self.assertEqual([('/a/', '')],
 
607
                         self.my_location_config._get_matching_sections())
582
608
 
583
 
    def test__get_section_explicit_over_glob(self):
 
609
    def test__get_matching_sections_explicit_over_glob(self):
 
610
        # XXX: 2006-09-08 jamesh
 
611
        # This test only passes because ord('c') > ord('*').  If there
 
612
        # was a config section for '/a/?', it would get precedence
 
613
        # over '/a/c'.
584
614
        self.get_branch_config('/a/c')
585
 
        self.assertEqual('/a/c', self.my_location_config._get_section())
586
 
 
 
615
        self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
 
616
                         self.my_location_config._get_matching_sections())
 
617
 
 
618
    def test__get_option_policy_normal(self):
 
619
        self.get_branch_config('http://www.example.com')
 
620
        self.assertEqual(
 
621
            self.my_location_config._get_config_policy(
 
622
            'http://www.example.com', 'normal_option'),
 
623
            config.POLICY_NONE)
 
624
 
 
625
    def test__get_option_policy_norecurse(self):
 
626
        self.get_branch_config('http://www.example.com')
 
627
        self.assertEqual(
 
628
            self.my_location_config._get_option_policy(
 
629
            'http://www.example.com', 'norecurse_option'),
 
630
            config.POLICY_NORECURSE)
 
631
        # Test old recurse=False setting:
 
632
        self.assertEqual(
 
633
            self.my_location_config._get_option_policy(
 
634
            'http://www.example.com/norecurse', 'normal_option'),
 
635
            config.POLICY_NORECURSE)
 
636
 
 
637
    def test__get_option_policy_normal(self):
 
638
        self.get_branch_config('http://www.example.com')
 
639
        self.assertEqual(
 
640
            self.my_location_config._get_option_policy(
 
641
            'http://www.example.com', 'appendpath_option'),
 
642
            config.POLICY_APPENDPATH)
587
643
 
588
644
    def test_location_without_username(self):
589
 
        self.get_branch_config('http://www.example.com/useglobal')
 
645
        self.get_branch_config('http://www.example.com/ignoreparent')
590
646
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
591
647
                         self.my_config.username())
592
648
 
641
697
        self.get_branch_config('/a')
642
698
        self.assertEqual('local',
643
699
                         self.my_config.get_user_option('user_local_option'))
 
700
 
 
701
    def test_get_user_option_appendpath(self):
 
702
        # returned as is for the base path:
 
703
        self.get_branch_config('http://www.example.com')
 
704
        self.assertEqual('append',
 
705
                         self.my_config.get_user_option('appendpath_option'))
 
706
        # Extra path components get appended:
 
707
        self.get_branch_config('http://www.example.com/a/b/c')
 
708
        self.assertEqual('append/a/b/c',
 
709
                         self.my_config.get_user_option('appendpath_option'))
 
710
        # Overriden for http://www.example.com/dir, where it is a
 
711
        # normal option:
 
712
        self.get_branch_config('http://www.example.com/dir/a/b/c')
 
713
        self.assertEqual('normal',
 
714
                         self.my_config.get_user_option('appendpath_option'))
 
715
 
 
716
    def test_get_user_option_norecurse(self):
 
717
        self.get_branch_config('http://www.example.com')
 
718
        self.assertEqual('norecurse',
 
719
                         self.my_config.get_user_option('norecurse_option'))
 
720
        self.get_branch_config('http://www.example.com/dir')
 
721
        self.assertEqual(None,
 
722
                         self.my_config.get_user_option('norecurse_option'))
 
723
        # http://www.example.com/norecurse is a recurse=False section
 
724
        # that redefines normal_option.  Subdirectories do not pick up
 
725
        # this redefinition.
 
726
        self.get_branch_config('http://www.example.com/norecurse')
 
727
        self.assertEqual('norecurse',
 
728
                         self.my_config.get_user_option('normal_option'))
 
729
        self.get_branch_config('http://www.example.com/norecurse/subdir')
 
730
        self.assertEqual('normal',
 
731
                         self.my_config.get_user_option('normal_option'))
 
732
 
 
733
    def test_set_user_option_norecurse(self):
 
734
        self.get_branch_config('http://www.example.com')
 
735
        self.my_config.set_user_option('foo', 'bar',
 
736
                                       store=config.STORE_LOCATION_NORECURSE)
 
737
        self.assertEqual(
 
738
            self.my_location_config._get_option_policy(
 
739
            'http://www.example.com', 'foo'),
 
740
            config.POLICY_NORECURSE)
 
741
 
 
742
    def test_set_user_option_appendpath(self):
 
743
        self.get_branch_config('http://www.example.com')
 
744
        self.my_config.set_user_option('foo', 'bar',
 
745
                                       store=config.STORE_LOCATION_APPENDPATH)
 
746
        self.assertEqual(
 
747
            self.my_location_config._get_option_policy(
 
748
            'http://www.example.com', 'foo'),
 
749
            config.POLICY_APPENDPATH)
 
750
 
 
751
    def test_set_user_option_change_policy(self):
 
752
        self.get_branch_config('http://www.example.com')
 
753
        self.my_config.set_user_option('norecurse_option', 'normal',
 
754
                                       store=config.STORE_LOCATION)
 
755
        self.assertEqual(
 
756
            self.my_location_config._get_option_policy(
 
757
            'http://www.example.com', 'norecurse_option'),
 
758
            config.POLICY_NONE)
 
759
 
 
760
    def test_set_user_option_recurse_false_section(self):
 
761
        # The following section has recurse=False set.  The test is to
 
762
        # make sure that a normal option can be added to the section,
 
763
        # converting recurse=False to the norecurse policy.
 
764
        self.get_branch_config('http://www.example.com/norecurse')
 
765
        self.callDeprecated(['The recurse option is deprecated as of 0.14.  '
 
766
                             'The section "http://www.example.com/norecurse" '
 
767
                             'has been converted to use policies.'],
 
768
                            self.my_config.set_user_option,
 
769
                            'foo', 'bar', store=config.STORE_LOCATION)
 
770
        self.assertEqual(
 
771
            self.my_location_config._get_option_policy(
 
772
            'http://www.example.com/norecurse', 'foo'),
 
773
            config.POLICY_NONE)
 
774
        # The previously existing option is still norecurse:
 
775
        self.assertEqual(
 
776
            self.my_location_config._get_option_policy(
 
777
            'http://www.example.com/norecurse', 'normal_option'),
 
778
            config.POLICY_NORECURSE)
644
779
        
 
780
 
645
781
    def test_post_commit_default(self):
646
782
        self.get_branch_config('/a/c')
647
783
        self.assertEqual('bzrlib.tests.test_config.post_commit',
674
810
 
675
811
        os.mkdir = checked_mkdir
676
812
        try:
677
 
            self.my_config.set_user_option('foo', 'bar', local=True)
 
813
            self.callDeprecated(['The recurse option is deprecated as of '
 
814
                                 '0.14.  The section "/a/c" has been '
 
815
                                 'converted to use policies.'],
 
816
                                self.my_config.set_user_option,
 
817
                                'foo', 'bar', store=config.STORE_LOCATION)
678
818
        finally:
679
819
            os.mkdir = real_mkdir
680
820
 
684
824
                          ('__setitem__', '/a/c', {}),
685
825
                          ('__getitem__', '/a/c'),
686
826
                          ('__setitem__', 'foo', 'bar'),
 
827
                          ('__getitem__', '/a/c'),
 
828
                          ('as_bool', 'recurse'),
 
829
                          ('__getitem__', '/a/c'),
 
830
                          ('__delitem__', 'recurse'),
 
831
                          ('__getitem__', '/a/c'),
 
832
                          ('keys',),
 
833
                          ('__getitem__', '/a/c'),
 
834
                          ('__contains__', 'foo:policy'),
687
835
                          ('write',)],
688
836
                         record._calls[1:])
689
837
 
695
843
            self.my_config.branch.control_files.files['branch.conf'], 
696
844
            'foo = bar')
697
845
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
698
 
        self.my_config.set_user_option('foo', 'baz', local=True)
 
846
        self.my_config.set_user_option('foo', 'baz',
 
847
                                       store=config.STORE_LOCATION)
699
848
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
700
849
        self.my_config.set_user_option('foo', 'qux')
701
850
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
799
948
        # post-commit is ignored when bresent in branch data
800
949
        self.assertEqual('bzrlib.tests.test_config.post_commit',
801
950
                         my_config.post_commit())
802
 
        my_config.set_user_option('post_commit', 'rmtree_root', local=True)
 
951
        my_config.set_user_option('post_commit', 'rmtree_root',
 
952
                                  store=config.STORE_LOCATION)
803
953
        self.assertEqual('rmtree_root', my_config.post_commit())
804
954
 
805
955
    def test_config_precedence(self):
824
974
    def test_extract_email_address(self):
825
975
        self.assertEqual('jane@test.com',
826
976
                         config.extract_email_address('Jane <jane@test.com>'))
827
 
        self.assertRaises(errors.BzrError,
 
977
        self.assertRaises(errors.NoEmailInUsername,
828
978
                          config.extract_email_address, 'Jane Tester')
 
979
 
 
980
 
 
981
class TestTreeConfig(TestCaseWithTransport):
 
982
 
 
983
    def test_get_value(self):
 
984
        """Test that retreiving a value from a section is possible"""
 
985
        branch = self.make_branch('.')
 
986
        tree_config = config.TreeConfig(branch)
 
987
        tree_config.set_option('value', 'key', 'SECTION')
 
988
        tree_config.set_option('value2', 'key2')
 
989
        tree_config.set_option('value3-top', 'key3')
 
990
        tree_config.set_option('value3-section', 'key3', 'SECTION')
 
991
        value = tree_config.get_option('key', 'SECTION')
 
992
        self.assertEqual(value, 'value')
 
993
        value = tree_config.get_option('key2')
 
994
        self.assertEqual(value, 'value2')
 
995
        self.assertEqual(tree_config.get_option('non-existant'), None)
 
996
        value = tree_config.get_option('non-existant', 'SECTION')
 
997
        self.assertEqual(value, None)
 
998
        value = tree_config.get_option('non-existant', default='default')
 
999
        self.assertEqual(value, 'default')
 
1000
        self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
 
1001
        value = tree_config.get_option('key2', 'NOSECTION', default='default')
 
1002
        self.assertEqual(value, 'default')
 
1003
        value = tree_config.get_option('key3')
 
1004
        self.assertEqual(value, 'value3-top')
 
1005
        value = tree_config.get_option('key3', 'SECTION')
 
1006
        self.assertEqual(value, 'value3-section')