~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-15 11:43:21 UTC
  • mfrom: (1442.1.21)
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051015114321-538f440683c51672
merge in branches.conf support

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
                      "editor=vim\n"
34
34
                      "gpg_signing_command=gnome-gpg\n")
35
35
 
 
36
 
 
37
sample_always_signatures = ("[DEFAULT]\n"
 
38
                            "check_signatures=require\n")
 
39
 
 
40
 
 
41
sample_ignore_signatures = ("[DEFAULT]\n"
 
42
                            "check_signatures=ignore\n")
 
43
 
 
44
 
 
45
sample_maybe_signatures = ("[DEFAULT]\n"
 
46
                            "check_signatures=check-available\n")
 
47
 
 
48
 
 
49
sample_branches_text = ("[http://www.example.com]\n"
 
50
                        "# Top level policy\n"
 
51
                        "email=Robert Collins <robertc@example.org>\n"
 
52
                        "[http://www.example.com/useglobal]\n"
 
53
                        "# different project, forces global lookup\n"
 
54
                        "recurse=false\n"
 
55
                        "[/b/]\n"
 
56
                        "check_signatures=require\n"
 
57
                        "# test trailing / matching with no children\n"
 
58
                        "[/a/]\n"
 
59
                        "check_signatures=check-available\n"
 
60
                        "# test trailing / matching\n"
 
61
                        "[/a/*]\n"
 
62
                        "#subdirs will match but not the parent\n"
 
63
                        "recurse=False\n"
 
64
                        "[/a/c]\n"
 
65
                        "check_signatures=ignore\n"
 
66
                        "#testing explicit beats globs\n")
 
67
 
 
68
 
36
69
class InstrumentedConfigParser(object):
37
70
    """A config parser look-enough-alike to record calls made to it."""
38
71
 
63
96
    def __init__(self):
64
97
        super(InstrumentedConfig, self).__init__()
65
98
        self._calls = []
 
99
        self._signatures = config.CHECK_NEVER
66
100
 
67
101
    def _get_user_id(self):
68
102
        self._calls.append('_get_user_id')
69
103
        return "Robert Collins <robert.collins@example.org>"
70
104
 
 
105
    def _get_signature_checking(self):
 
106
        self._calls.append('_get_signature_checking')
 
107
        return self._signatures
 
108
 
71
109
 
72
110
class TestConfig(TestCase):
73
111
 
87
125
        self.assertEqual('Robert Collins <robert.collins@example.org>',
88
126
                         my_config.username())
89
127
        self.assertEqual(['_get_user_id'], my_config._calls)
90
 
 
 
128
 
 
129
    def test_signatures_default(self):
 
130
        my_config = config.Config()
 
131
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
132
                         my_config.signature_checking())
 
133
 
 
134
    def test_signatures_template_method(self):
 
135
        my_config = InstrumentedConfig()
 
136
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
 
137
        self.assertEqual(['_get_signature_checking'], my_config._calls)
 
138
 
 
139
    def test_signatures_template_method_none(self):
 
140
        my_config = InstrumentedConfig()
 
141
        my_config._signatures = None
 
142
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
143
                         my_config.signature_checking())
 
144
        self.assertEqual(['_get_signature_checking'], my_config._calls)
 
145
 
91
146
 
92
147
class TestConfigPath(TestCase):
93
148
 
110
165
        self.assertEqual(config.branches_config_filename(),
111
166
                         '/home/bogus/.bazaar/branches.conf')
112
167
 
113
 
 
114
 
class TestGetConfig(TestCase):
115
 
 
116
 
    def test_constructs(self):
117
 
        my_config = config.GlobalConfig()
 
168
class TestIniConfig(TestCase):
 
169
 
 
170
    def test_contructs(self):
 
171
        my_config = config.IniBasedConfig("nothing")
118
172
 
119
173
    def test_from_fp(self):
120
174
        config_file = StringIO(sample_config_text)
121
 
        my_config = config.GlobalConfig()
 
175
        my_config = config.IniBasedConfig(None)
122
176
        self.failUnless(
123
 
            isinstance(my_config._get_config_parser(file=config_file),
 
177
            isinstance(my_config._get_parser(file=config_file),
124
178
                        ConfigParser))
125
179
 
126
180
    def test_cached(self):
127
181
        config_file = StringIO(sample_config_text)
 
182
        my_config = config.IniBasedConfig(None)
 
183
        parser = my_config._get_parser(file=config_file)
 
184
        self.failUnless(my_config._get_parser() is parser)
 
185
 
 
186
 
 
187
 
 
188
class TestGetConfig(TestCase):
 
189
 
 
190
    def test_constructs(self):
128
191
        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
192
 
132
193
    def test_calls_read_filenames(self):
133
194
        # replace the class that is constructured, to check its parameters
135
196
        config.ConfigParser = InstrumentedConfigParser
136
197
        my_config = config.GlobalConfig()
137
198
        try:
138
 
            parser = my_config._get_config_parser()
 
199
            parser = my_config._get_parser()
139
200
        finally:
140
201
            config.ConfigParser = oldparserclass
141
202
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
142
203
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
143
204
 
144
205
 
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
206
class TestBranchConfig(TestCaseInTempDir):
184
207
 
185
208
    def test_constructs(self):
224
247
    def test_user_id(self):
225
248
        config_file = StringIO(sample_config_text)
226
249
        my_config = config.GlobalConfig()
227
 
        my_config._parser = my_config._get_config_parser(file=config_file)
 
250
        my_config._parser = my_config._get_parser(file=config_file)
228
251
        self.assertEqual("Robert Collins <robertc@example.com>",
229
252
                         my_config._get_user_id())
230
253
 
231
254
    def test_absent_user_id(self):
232
255
        config_file = StringIO("")
233
256
        my_config = config.GlobalConfig()
234
 
        my_config._parser = my_config._get_config_parser(file=config_file)
 
257
        my_config._parser = my_config._get_parser(file=config_file)
235
258
        self.assertEqual(None, my_config._get_user_id())
236
259
 
237
260
    def test_configured_editor(self):
238
261
        config_file = StringIO(sample_config_text)
239
262
        my_config = config.GlobalConfig()
240
 
        my_config._parser = my_config._get_config_parser(file=config_file)
 
263
        my_config._parser = my_config._get_parser(file=config_file)
241
264
        self.assertEqual("vim", my_config.get_editor())
242
265
 
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
 
 
 
266
    def test_signatures_always(self):
 
267
        config_file = StringIO(sample_always_signatures)
 
268
        my_config = config.GlobalConfig()
 
269
        my_config._parser = my_config._get_parser(file=config_file)
 
270
        self.assertEqual(config.CHECK_ALWAYS,
 
271
                         my_config.signature_checking())
 
272
        self.assertEqual(True, my_config.signature_needed())
 
273
 
 
274
    def test_signatures_if_possible(self):
 
275
        config_file = StringIO(sample_maybe_signatures)
 
276
        my_config = config.GlobalConfig()
 
277
        my_config._parser = my_config._get_parser(file=config_file)
 
278
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
279
                         my_config.signature_checking())
 
280
        self.assertEqual(False, my_config.signature_needed())
 
281
 
 
282
    def test_signatures_ignore(self):
 
283
        config_file = StringIO(sample_ignore_signatures)
 
284
        my_config = config.GlobalConfig()
 
285
        my_config._parser = my_config._get_parser(file=config_file)
 
286
        self.assertEqual(config.CHECK_NEVER,
 
287
                         my_config.signature_checking())
 
288
        self.assertEqual(False, my_config.signature_needed())
 
289
 
 
290
 
 
291
class TestLocationConfig(TestConfigItems):
 
292
 
 
293
    def test_constructs(self):
 
294
        my_config = config.LocationConfig('http://example.com')
 
295
        self.assertRaises(TypeError, config.LocationConfig)
 
296
 
 
297
    def test_branch_calls_read_filenames(self):
 
298
        # replace the class that is constructured, to check its parameters
 
299
        oldparserclass = config.ConfigParser
 
300
        config.ConfigParser = InstrumentedConfigParser
 
301
        my_config = config.LocationConfig('http://www.example.com')
 
302
        try:
 
303
            parser = my_config._get_parser()
 
304
        finally:
 
305
            config.ConfigParser = oldparserclass
 
306
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
307
        self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
 
308
 
 
309
    def test_get_global_config(self):
 
310
        my_config = config.LocationConfig('http://example.com')
 
311
        global_config = my_config._get_global_config()
 
312
        self.failUnless(isinstance(global_config, config.GlobalConfig))
 
313
        self.failUnless(global_config is my_config._get_global_config())
 
314
 
 
315
    def test__get_section_no_match(self):
 
316
        self.get_location_config('/')
 
317
        self.assertEqual(None, self.my_config._get_section())
 
318
        
 
319
    def test__get_section_exact(self):
 
320
        self.get_location_config('http://www.example.com')
 
321
        self.assertEqual('http://www.example.com',
 
322
                         self.my_config._get_section())
 
323
   
 
324
    def test__get_section_suffix_does_not(self):
 
325
        self.get_location_config('http://www.example.com-com')
 
326
        self.assertEqual(None, self.my_config._get_section())
 
327
 
 
328
    def test__get_section_subdir_recursive(self):
 
329
        self.get_location_config('http://www.example.com/com')
 
330
        self.assertEqual('http://www.example.com',
 
331
                         self.my_config._get_section())
 
332
 
 
333
    def test__get_section_subdir_matches(self):
 
334
        self.get_location_config('http://www.example.com/useglobal')
 
335
        self.assertEqual('http://www.example.com/useglobal',
 
336
                         self.my_config._get_section())
 
337
 
 
338
    def test__get_section_subdir_nonrecursive(self):
 
339
        self.get_location_config(
 
340
            'http://www.example.com/useglobal/childbranch')
 
341
        self.assertEqual('http://www.example.com',
 
342
                         self.my_config._get_section())
 
343
 
 
344
    def test__get_section_subdir_trailing_slash(self):
 
345
        self.get_location_config('/b')
 
346
        self.assertEqual('/b/', self.my_config._get_section())
 
347
 
 
348
    def test__get_section_subdir_child(self):
 
349
        self.get_location_config('/a/foo')
 
350
        self.assertEqual('/a/*', self.my_config._get_section())
 
351
 
 
352
    def test__get_section_subdir_child_child(self):
 
353
        self.get_location_config('/a/foo/bar')
 
354
        self.assertEqual('/a/', self.my_config._get_section())
 
355
 
 
356
    def test__get_section_trailing_slash_with_children(self):
 
357
        self.get_location_config('/a/')
 
358
        self.assertEqual('/a/', self.my_config._get_section())
 
359
 
 
360
    def test__get_section_explicit_over_glob(self):
 
361
        self.get_location_config('/a/c')
 
362
        self.assertEqual('/a/c', self.my_config._get_section())
 
363
 
 
364
    def get_location_config(self, location, global_config=None):
 
365
        if global_config is None:
 
366
            global_file = StringIO(sample_config_text)
 
367
        else:
 
368
            global_file = StringIO(global_config)
 
369
        branches_file = StringIO(sample_branches_text)
 
370
        self.my_config = config.LocationConfig(location)
 
371
        self.my_config._get_parser(branches_file)
 
372
        self.my_config._get_global_config()._get_parser(global_file)
 
373
 
 
374
    def test_location_without_username(self):
 
375
        self.get_location_config('http://www.example.com/useglobal')
 
376
        self.assertEqual('Robert Collins <robertc@example.com>',
 
377
                         self.my_config.username())
 
378
 
 
379
    def test_location_not_listed(self):
 
380
        self.get_location_config('/home/robertc/sources')
 
381
        self.assertEqual('Robert Collins <robertc@example.com>',
 
382
                         self.my_config.username())
 
383
 
 
384
    def test_overriding_location(self):
 
385
        self.get_location_config('http://www.example.com/foo')
 
386
        self.assertEqual('Robert Collins <robertc@example.org>',
 
387
                         self.my_config.username())
 
388
 
 
389
    def test_signatures_not_set(self):
 
390
        self.get_location_config('http://www.example.com',
 
391
                                 global_config=sample_ignore_signatures)
 
392
        self.assertEqual(config.CHECK_NEVER,
 
393
                         self.my_config.signature_checking())
 
394
 
 
395
    def test_signatures_never(self):
 
396
        self.get_location_config('/a/c')
 
397
        self.assertEqual(config.CHECK_NEVER,
 
398
                         self.my_config.signature_checking())
 
399
        
 
400
    def test_signatures_when_available(self):
 
401
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
 
402
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
403
                         self.my_config.signature_checking())
 
404
        
 
405
    def test_signatures_always(self):
 
406
        self.get_location_config('/b')
 
407
        self.assertEqual(config.CHECK_ALWAYS,
 
408
                         self.my_config.signature_checking())
 
409
        
253
410
 
254
411
class TestBranchConfigItems(TestConfigItems):
255
412
 
267
424
        branch.email = None
268
425
        config_file = StringIO(sample_config_text)
269
426
        (my_config._get_location_config().
270
 
            _get_global_config()._get_config_parser(config_file))
 
427
            _get_global_config()._get_parser(config_file))
271
428
        self.assertEqual("Robert Collins <robertc@example.com>",
272
429
                         my_config._get_user_id())
273
430
        branch.email = "John"
280
437
        self.assertEqual("Robert Collins <robertc@example.org>",
281
438
                         my_config.username())
282
439
    
 
440
    def test_signatures_forced(self):
 
441
        branch = FakeBranch()
 
442
        my_config = config.BranchConfig(branch)
 
443
        config_file = StringIO(sample_always_signatures)
 
444
        (my_config._get_location_config().
 
445
            _get_global_config()._get_parser(config_file))
 
446
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())