~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-14 01:56:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1453.
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051014015608-2970671a76324ad8
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#import bzrlib specific imports here
26
26
import bzrlib.config as config
 
27
import bzrlib.errors as errors
27
28
from bzrlib.selftest import TestCase, TestCaseInTempDir
28
29
 
29
30
 
42
43
        self._calls.append(('read', filenames))
43
44
 
44
45
 
 
46
class FakeBranch(object):
 
47
 
 
48
    def __init__(self):
 
49
        self.base = "http://example.com/branches/demo"
 
50
        self.email = 'Robert Collins <robertc@example.net>\n'
 
51
 
 
52
    def controlfile(self, filename, mode):
 
53
        if filename != 'email':
 
54
            raise NotImplementedError
 
55
        if self.email is not None:
 
56
            return StringIO(self.email)
 
57
        raise errors.NoSuchFile
 
58
 
 
59
 
 
60
class InstrumentedConfig(config.Config):
 
61
    """An instrumented config that supplies stubs for template methods."""
 
62
    
 
63
    def __init__(self):
 
64
        super(InstrumentedConfig, self).__init__()
 
65
        self._calls = []
 
66
 
 
67
    def _get_user_id(self):
 
68
        self._calls.append('_get_user_id')
 
69
        return "Robert Collins <robert.collins@example.org>"
 
70
 
 
71
 
 
72
class TestConfig(TestCase):
 
73
 
 
74
    def test_constructs(self):
 
75
        config.Config()
 
76
 
 
77
    def test_no_default_editor(self):
 
78
        self.assertRaises(NotImplementedError, config.Config().get_editor)
 
79
 
 
80
    def test_user_email(self):
 
81
        my_config = InstrumentedConfig()
 
82
        self.assertEqual('robert.collins@example.org', my_config.user_email())
 
83
        self.assertEqual(['_get_user_id'], my_config._calls)
 
84
 
 
85
    def test_username(self):
 
86
        my_config = InstrumentedConfig()
 
87
        self.assertEqual('Robert Collins <robert.collins@example.org>',
 
88
                         my_config.username())
 
89
        self.assertEqual(['_get_user_id'], my_config._calls)
 
90
 
 
91
 
45
92
class TestConfigPath(TestCase):
46
93
 
47
94
    def setUp(self):
59
106
        self.assertEqual(config.config_filename(),
60
107
                         '/home/bogus/.bazaar/bazaar.conf')
61
108
 
 
109
    def test_branches_config_filename(self):
 
110
        self.assertEqual(config.branches_config_filename(),
 
111
                         '/home/bogus/.bazaar/branches.conf')
 
112
 
62
113
 
63
114
class TestGetConfig(TestCase):
64
115
 
 
116
    def test_constructs(self):
 
117
        my_config = config.GlobalConfig()
 
118
 
65
119
    def test_from_fp(self):
66
120
        config_file = StringIO(sample_config_text)
67
 
        self.failUnless(isinstance(config._get_config_parser(file=config_file),
 
121
        my_config = config.GlobalConfig()
 
122
        self.failUnless(
 
123
            isinstance(my_config._get_config_parser(file=config_file),
68
124
                        ConfigParser))
69
125
 
 
126
    def test_cached(self):
 
127
        config_file = StringIO(sample_config_text)
 
128
        my_config = config.GlobalConfig()
 
129
        parser = my_config._get_config_parser(file=config_file)
 
130
        self.failUnless(my_config._get_config_parser() is parser)
 
131
 
70
132
    def test_calls_read_filenames(self):
71
 
        # note the monkey patching. if config access was via a class instance,
72
 
        # we would not have to - if this changes in future, be sure to stop 
73
 
        # monkey patching RBC 20051011
 
133
        # replace the class that is constructured, to check its parameters
74
134
        oldparserclass = config.ConfigParser
75
135
        config.ConfigParser = InstrumentedConfigParser
 
136
        my_config = config.GlobalConfig()
76
137
        try:
77
 
            parser = config._get_config_parser()
 
138
            parser = my_config._get_config_parser()
78
139
        finally:
79
140
            config.ConfigParser = oldparserclass
80
141
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
81
142
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
82
143
 
83
144
 
 
145
class TestLocationConfig(TestCase):
 
146
 
 
147
    def test_constructs(self):
 
148
        my_config = config.LocationConfig('http://example.com')
 
149
        self.assertRaises(TypeError, config.LocationConfig)
 
150
 
 
151
    def test_cached(self):
 
152
        config_file = StringIO(sample_config_text)
 
153
        my_config = config.LocationConfig('http://example.com')
 
154
        parser = my_config._get_branches_config_parser(file=config_file)
 
155
        self.failUnless(my_config._get_branches_config_parser() is parser)
 
156
 
 
157
    def test_branches_from_fp(self):
 
158
        config_file = StringIO(sample_config_text)
 
159
        my_config = config.LocationConfig('http://example.com')
 
160
        self.failUnless(isinstance(
 
161
            my_config._get_branches_config_parser(file=config_file),
 
162
            ConfigParser))
 
163
 
 
164
    def test_branch_calls_read_filenames(self):
 
165
        # replace the class that is constructured, to check its parameters
 
166
        oldparserclass = config.ConfigParser
 
167
        config.ConfigParser = InstrumentedConfigParser
 
168
        my_config = config.LocationConfig('http://www.example.com')
 
169
        try:
 
170
            parser = my_config._get_branches_config_parser()
 
171
        finally:
 
172
            config.ConfigParser = oldparserclass
 
173
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
174
        self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
 
175
 
 
176
    def test_get_global_config(self):
 
177
        my_config = config.LocationConfig('http://example.com')
 
178
        global_config = my_config._get_global_config()
 
179
        self.failUnless(isinstance(global_config, config.GlobalConfig))
 
180
        self.failUnless(global_config is my_config._get_global_config())
 
181
 
 
182
 
 
183
class TestBranchConfig(TestCaseInTempDir):
 
184
 
 
185
    def test_constructs(self):
 
186
        branch = FakeBranch()
 
187
        my_config = config.BranchConfig(branch)
 
188
        self.assertRaises(TypeError, config.BranchConfig)
 
189
 
 
190
    def test_get_location_config(self):
 
191
        branch = FakeBranch()
 
192
        my_config = config.BranchConfig(branch)
 
193
        location_config = my_config._get_location_config()
 
194
        self.assertEqual(branch.base, location_config.location)
 
195
        self.failUnless(location_config is my_config._get_location_config())
 
196
 
 
197
 
84
198
class TestConfigItems(TestCase):
85
199
 
86
200
    def setUp(self):
96
210
 
97
211
    def tearDown(self):
98
212
        os.environ['HOME'] = self.oldenv
 
213
        if os.environ.get('BZREMAIL') is not None:
 
214
            del os.environ['BZREMAIL']
99
215
        if self.bzr_email is not None:
100
216
            os.environ['BZREMAIL'] = bzr_email
101
217
        if self.email is not None:
102
218
            os.environ['EMAIL'] = email
103
219
        super(TestConfigItems, self).tearDown()
104
220
 
 
221
 
 
222
class TestGlobalConfigItems(TestConfigItems):
 
223
 
105
224
    def test_user_id(self):
106
225
        config_file = StringIO(sample_config_text)
107
 
        parser = config._get_config_parser(file=config_file)
 
226
        my_config = config.GlobalConfig()
 
227
        my_config._parser = my_config._get_config_parser(file=config_file)
108
228
        self.assertEqual("Robert Collins <robertc@example.com>",
109
 
                         config._get_user_id(parser = parser))
 
229
                         my_config._get_user_id())
110
230
 
111
231
    def test_absent_user_id(self):
112
232
        config_file = StringIO("")
113
 
        parser = config._get_config_parser(file=config_file)
114
 
        self.assertEqual(None,
115
 
                         config._get_user_id(parser = parser))
116
 
 
117
 
    def test_configured_edit(self):
118
 
        config_file = StringIO(sample_config_text)
119
 
        parser = config._get_config_parser(file=config_file)
120
 
        self.assertEqual("vim", config.get_editor(parser = parser))
 
233
        my_config = config.GlobalConfig()
 
234
        my_config._parser = my_config._get_config_parser(file=config_file)
 
235
        self.assertEqual(None, my_config._get_user_id())
 
236
 
 
237
    def test_configured_editor(self):
 
238
        config_file = StringIO(sample_config_text)
 
239
        my_config = config.GlobalConfig()
 
240
        my_config._parser = my_config._get_config_parser(file=config_file)
 
241
        self.assertEqual("vim", my_config.get_editor())
 
242
 
 
243
 
 
244
#class TestLocationConfigItems(TestConfigItems):
 
245
#    
 
246
#    def test_location_username(self):
 
247
#        
 
248
#
 
249
#> signatures=check-if-available
 
250
#> signatures=require
 
251
#> signatures=ignore
 
252
 
 
253
 
 
254
class TestBranchConfigItems(TestConfigItems):
 
255
 
 
256
    def test_user_id(self):
 
257
        branch = FakeBranch()
 
258
        my_config = config.BranchConfig(branch)
 
259
        self.assertEqual("Robert Collins <robertc@example.net>",
 
260
                         my_config._get_user_id())
 
261
        branch.email = "John"
 
262
        self.assertEqual("John", my_config._get_user_id())
 
263
 
 
264
    def test_not_set_in_branch(self):
 
265
        branch = FakeBranch()
 
266
        my_config = config.BranchConfig(branch)
 
267
        branch.email = None
 
268
        config_file = StringIO(sample_config_text)
 
269
        (my_config._get_location_config().
 
270
            _get_global_config()._get_config_parser(config_file))
 
271
        self.assertEqual("Robert Collins <robertc@example.com>",
 
272
                         my_config._get_user_id())
 
273
        branch.email = "John"
 
274
        self.assertEqual("John", my_config._get_user_id())
 
275
 
 
276
    def test_BZREMAIL_OVERRIDES(self):
 
277
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
 
278
        branch = FakeBranch()
 
279
        my_config = config.BranchConfig(branch)
 
280
        self.assertEqual("Robert Collins <robertc@example.org>",
 
281
                         my_config.username())
 
282