~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

first cut at merge from integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005 by 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
28
28
from bzrlib.tests import TestCase, TestCaseInTempDir
29
29
 
30
30
 
31
 
sample_long_alias="log -r-15..-1 --line"
32
31
sample_config_text = ("[DEFAULT]\n"
33
 
                      u"email=Erik B\u00e5gfors <erik@bagfors.nu>\n"
 
32
                      "email=Robert Collins <robertc@example.com>\n"
34
33
                      "editor=vim\n"
35
34
                      "gpg_signing_command=gnome-gpg\n"
36
 
                      "log_format=short\n"
37
 
                      "user_global_option=something\n"
38
 
                      "[ALIASES]\n"
39
 
                      "h=help\n"
40
 
                      "ll=" + sample_long_alias + "\n")
 
35
                      "user_global_option=something\n")
41
36
 
42
37
 
43
38
sample_always_signatures = ("[DEFAULT]\n"
75
70
                        "#testing explicit beats globs\n")
76
71
 
77
72
 
78
 
 
79
73
class InstrumentedConfigObj(object):
80
74
    """A config obj look-enough-alike to record calls made to it."""
81
75
 
87
81
        self._calls.append(('__getitem__', key))
88
82
        return self
89
83
 
90
 
    def __init__(self, input, encoding=None):
91
 
        self._calls = [('__init__', input, encoding)]
 
84
    def __init__(self, input):
 
85
        self._calls = [('__init__', input)]
92
86
 
93
87
    def __setitem__(self, key, value):
94
88
        self._calls.append(('__setitem__', key, value))
95
89
 
96
 
    def write(self, arg):
 
90
    def write(self):
97
91
        self._calls.append(('write',))
98
92
 
99
93
 
134
128
        return self._signatures
135
129
 
136
130
 
137
 
bool_config = """[DEFAULT]
138
 
active = true
139
 
inactive = false
140
 
[UPPERCASE]
141
 
active = True
142
 
nonactive = False
143
 
"""
144
 
class TestConfigObj(TestCase):
145
 
    def test_get_bool(self):
146
 
        from bzrlib.config import ConfigObj
147
 
        co = ConfigObj(StringIO(bool_config))
148
 
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
149
 
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
150
 
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
151
 
        self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
152
 
 
153
 
 
154
131
class TestConfig(TestCase):
155
132
 
156
133
    def test_constructs(self):
199
176
        my_config = config.Config()
200
177
        self.assertEqual(None, my_config.post_commit())
201
178
 
202
 
    def test_log_format_default(self):
203
 
        my_config = config.Config()
204
 
        self.assertEqual('long', my_config.log_format())
205
 
 
206
179
 
207
180
class TestConfigPath(TestCase):
208
181
 
254
227
        my_config = config.IniBasedConfig("nothing")
255
228
 
256
229
    def test_from_fp(self):
257
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
230
        config_file = StringIO(sample_config_text)
258
231
        my_config = config.IniBasedConfig(None)
259
232
        self.failUnless(
260
233
            isinstance(my_config._get_parser(file=config_file),
261
234
                        ConfigObj))
262
235
 
263
236
    def test_cached(self):
264
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
237
        config_file = StringIO(sample_config_text)
265
238
        my_config = config.IniBasedConfig(None)
266
239
        parser = my_config._get_parser(file=config_file)
267
240
        self.failUnless(my_config._get_parser() is parser)
282
255
        finally:
283
256
            config.ConfigObj = oldparserclass
284
257
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
285
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
286
 
                                          'utf-8')])
 
258
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
287
259
 
288
260
 
289
261
class TestBranchConfig(TestCaseInTempDir):
304
276
class TestGlobalConfigItems(TestCase):
305
277
 
306
278
    def test_user_id(self):
307
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
279
        config_file = StringIO(sample_config_text)
308
280
        my_config = config.GlobalConfig()
309
281
        my_config._parser = my_config._get_parser(file=config_file)
310
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
 
282
        self.assertEqual("Robert Collins <robertc@example.com>",
311
283
                         my_config._get_user_id())
312
284
 
313
285
    def test_absent_user_id(self):
317
289
        self.assertEqual(None, my_config._get_user_id())
318
290
 
319
291
    def test_configured_editor(self):
320
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
292
        config_file = StringIO(sample_config_text)
321
293
        my_config = config.GlobalConfig()
322
294
        my_config._parser = my_config._get_parser(file=config_file)
323
295
        self.assertEqual("vim", my_config.get_editor())
347
319
        self.assertEqual(False, my_config.signature_needed())
348
320
 
349
321
    def _get_sample_config(self):
350
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
322
        config_file = StringIO(sample_config_text)
351
323
        my_config = config.GlobalConfig()
352
324
        my_config._parser = my_config._get_parser(file=config_file)
353
325
        return my_config
380
352
        my_config = self._get_sample_config()
381
353
        self.assertEqual(None, my_config.post_commit())
382
354
 
383
 
    def test_configured_logformat(self):
384
 
        my_config = self._get_sample_config()
385
 
        self.assertEqual("short", my_config.log_format())
386
 
 
387
 
    def test_get_alias(self):
388
 
        my_config = self._get_sample_config()
389
 
        self.assertEqual('help', my_config.get_alias('h'))
390
 
 
391
 
    def test_get_no_alias(self):
392
 
        my_config = self._get_sample_config()
393
 
        self.assertEqual(None, my_config.get_alias('foo'))
394
 
 
395
 
    def test_get_long_alias(self):
396
 
        my_config = self._get_sample_config()
397
 
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
398
 
 
399
 
 
400
 
class TestLocationConfig(TestCaseInTempDir):
 
355
 
 
356
class TestLocationConfig(TestCase):
401
357
 
402
358
    def test_constructs(self):
403
359
        my_config = config.LocationConfig('http://example.com')
417
373
            config.ConfigObj = oldparserclass
418
374
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
419
375
        self.assertEqual(parser._calls,
420
 
                         [('__init__', config.branches_config_filename(),
421
 
                           'utf-8')])
 
376
                         [('__init__', config.branches_config_filename())])
422
377
 
423
378
    def test_get_global_config(self):
424
379
        my_config = config.LocationConfig('http://example.com')
475
430
        self.get_location_config('/a/c')
476
431
        self.assertEqual('/a/c', self.my_config._get_section())
477
432
 
 
433
    def get_location_config(self, location, global_config=None):
 
434
        if global_config is None:
 
435
            global_file = StringIO(sample_config_text)
 
436
        else:
 
437
            global_file = StringIO(global_config)
 
438
        branches_file = StringIO(sample_branches_text)
 
439
        self.my_config = config.LocationConfig(location)
 
440
        self.my_config._get_parser(branches_file)
 
441
        self.my_config._get_global_config()._get_parser(global_file)
478
442
 
479
443
    def test_location_without_username(self):
480
444
        self.get_location_config('http://www.example.com/useglobal')
481
 
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
 
445
        self.assertEqual('Robert Collins <robertc@example.com>',
482
446
                         self.my_config.username())
483
447
 
484
448
    def test_location_not_listed(self):
485
 
        """Test that the global username is used when no location matches"""
486
449
        self.get_location_config('/home/robertc/sources')
487
 
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
 
450
        self.assertEqual('Robert Collins <robertc@example.com>',
488
451
                         self.my_config.username())
489
452
 
490
453
    def test_overriding_location(self):
536
499
        self.assertEqual('bzrlib.tests.test_config.post_commit',
537
500
                         self.my_config.post_commit())
538
501
 
 
502
 
 
503
class TestLocationConfig(TestCaseInTempDir):
 
504
 
539
505
    def get_location_config(self, location, global_config=None):
540
506
        if global_config is None:
541
 
            global_file = StringIO(sample_config_text.encode('utf-8'))
 
507
            global_file = StringIO(sample_config_text)
542
508
        else:
543
 
            global_file = StringIO(global_config.encode('utf-8'))
544
 
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
 
509
            global_file = StringIO(global_config)
 
510
        branches_file = StringIO(sample_branches_text)
545
511
        self.my_config = config.LocationConfig(location)
546
512
        self.my_config._get_parser(branches_file)
547
513
        self.my_config._get_global_config()._get_parser(global_file)
588
554
        branch = FakeBranch()
589
555
        my_config = config.BranchConfig(branch)
590
556
        branch.control_files.email = None
591
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
557
        config_file = StringIO(sample_config_text)
592
558
        (my_config._get_location_config().
593
559
            _get_global_config()._get_parser(config_file))
594
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
 
560
        self.assertEqual("Robert Collins <robertc@example.com>",
595
561
                         my_config._get_user_id())
596
562
        branch.control_files.email = "John"
597
563
        self.assertEqual("John", my_config._get_user_id())
614
580
    def test_gpg_signing_command(self):
615
581
        branch = FakeBranch()
616
582
        my_config = config.BranchConfig(branch)
617
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
583
        config_file = StringIO(sample_config_text)
618
584
        (my_config._get_location_config().
619
585
            _get_global_config()._get_parser(config_file))
620
586
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
622
588
    def test_get_user_option_global(self):
623
589
        branch = FakeBranch()
624
590
        my_config = config.BranchConfig(branch)
625
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
591
        config_file = StringIO(sample_config_text)
626
592
        (my_config._get_location_config().
627
593
            _get_global_config()._get_parser(config_file))
628
594
        self.assertEqual('something',
632
598
        branch = FakeBranch()
633
599
        branch.base='/a/c'
634
600
        my_config = config.BranchConfig(branch)
635
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
601
        config_file = StringIO(sample_config_text)
636
602
        (my_config._get_location_config().
637
603
            _get_global_config()._get_parser(config_file))
638
604
        branch_file = StringIO(sample_branches_text)