~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-08-30 07:36:41 UTC
  • mfrom: (5394.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100830073641-a8gnwgcuonlkdqqg
(vila) Config files in bazaar home now use a lock (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from cStringIO import StringIO
20
20
import os
21
21
import sys
 
22
import threading
22
23
 
23
24
#import bzrlib specific imports here
24
25
from bzrlib import (
39
40
from bzrlib.util.configobj import configobj
40
41
 
41
42
 
 
43
def lockable_config_scenarios():
 
44
    return [
 
45
        ('global',
 
46
         {'config_class': config.GlobalConfig,
 
47
          'config_args': [],
 
48
          'config_section': 'DEFAULT'}),
 
49
        ('locations',
 
50
         {'config_class': config.LocationConfig,
 
51
          'config_args': ['.'],
 
52
          'config_section': '.'}),]
 
53
 
 
54
 
 
55
def load_tests(standard_tests, module, loader):
 
56
    suite = loader.suiteClass()
 
57
 
 
58
    lc_tests, remaining_tests = tests.split_suite_by_condition(
 
59
        standard_tests, tests.condition_isinstance((
 
60
                TestLockableConfig,
 
61
                )))
 
62
    tests.multiply_tests(lc_tests, lockable_config_scenarios(), suite)
 
63
    suite.addTest(remaining_tests)
 
64
    return suite
 
65
 
 
66
 
42
67
sample_long_alias="log -r-15..-1 --line"
43
68
sample_config_text = u"""
44
69
[DEFAULT]
130
155
        self._calls.append(('keys',))
131
156
        return []
132
157
 
 
158
    def reload(self):
 
159
        self._calls.append(('reload',))
 
160
 
133
161
    def write(self, arg):
134
162
        self._calls.append(('write',))
135
163
 
351
379
        self.assertEqual(config.config_filename(),
352
380
                         self.bzr_home + '/bazaar.conf')
353
381
 
354
 
    def test_branches_config_filename(self):
355
 
        self.assertEqual(config.branches_config_filename(),
356
 
                         self.bzr_home + '/branches.conf')
357
 
 
358
382
    def test_locations_config_filename(self):
359
383
        self.assertEqual(config.locations_config_filename(),
360
384
                         self.bzr_home + '/locations.conf')
371
395
class TestIniConfig(tests.TestCaseInTempDir):
372
396
 
373
397
    def make_config_parser(self, s):
374
 
        conf = config.IniBasedConfig(None)
375
 
        parser = conf._get_parser(file=StringIO(s.encode('utf-8')))
376
 
        return conf, parser
 
398
        conf = config.IniBasedConfig.from_string(s)
 
399
        return conf, conf._get_parser()
377
400
 
378
401
 
379
402
class TestIniConfigBuilding(TestIniConfig):
380
403
 
381
404
    def test_contructs(self):
382
 
        my_config = config.IniBasedConfig("nothing")
 
405
        my_config = config.IniBasedConfig()
383
406
 
384
407
    def test_from_fp(self):
385
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
386
 
        my_config = config.IniBasedConfig(None)
387
 
        self.failUnless(
388
 
            isinstance(my_config._get_parser(file=config_file),
389
 
                        configobj.ConfigObj))
 
408
        my_config = config.IniBasedConfig.from_string(sample_config_text)
 
409
        self.assertIsInstance(my_config._get_parser(), configobj.ConfigObj)
390
410
 
391
411
    def test_cached(self):
392
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
393
 
        my_config = config.IniBasedConfig(None)
394
 
        parser = my_config._get_parser(file=config_file)
 
412
        my_config = config.IniBasedConfig.from_string(sample_config_text)
 
413
        parser = my_config._get_parser()
395
414
        self.failUnless(my_config._get_parser() is parser)
396
415
 
397
416
    def _dummy_chown(self, path, uid, gid):
398
417
        self.path, self.uid, self.gid = path, uid, gid
399
418
 
400
419
    def test_ini_config_ownership(self):
401
 
        """Ensure that chown is happening during _write_config_file.
402
 
        """
 
420
        """Ensure that chown is happening during _write_config_file"""
403
421
        self.requireFeature(features.chown_feature)
404
422
        self.overrideAttr(os, 'chown', self._dummy_chown)
405
423
        self.path = self.uid = self.gid = None
406
 
        def get_filename():
407
 
            return 'foo.conf'
408
 
        conf = config.IniBasedConfig(get_filename)
 
424
        conf = config.IniBasedConfig(file_name='./foo.conf')
409
425
        conf._write_config_file()
410
 
        self.assertEquals(self.path, 'foo.conf')
 
426
        self.assertEquals(self.path, './foo.conf')
411
427
        self.assertTrue(isinstance(self.uid, int))
412
428
        self.assertTrue(isinstance(self.gid, int))
413
429
 
 
430
    def test_get_filename_parameter_is_deprecated_(self):
 
431
        conf = self.callDeprecated([
 
432
            'IniBasedConfig.__init__(get_filename) was deprecated in 2.3.'
 
433
            ' Use file_name instead.'],
 
434
            config.IniBasedConfig, lambda: 'ini.conf')
 
435
        self.assertEqual('ini.conf', conf.file_name)
 
436
 
 
437
    def test_get_parser_file_parameter_is_deprecated_(self):
 
438
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
439
        conf = config.IniBasedConfig.from_string(sample_config_text)
 
440
        conf = self.callDeprecated([
 
441
            'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
 
442
            ' Use IniBasedConfig(_content=xxx) instead.'],
 
443
            conf._get_parser, file=config_file)
 
444
 
 
445
class TestIniConfigSaving(tests.TestCaseInTempDir):
 
446
 
 
447
    def test_cant_save_without_a_file_name(self):
 
448
        conf = config.IniBasedConfig()
 
449
        self.assertRaises(AssertionError, conf._write_config_file)
 
450
 
 
451
    def test_saved_with_content(self):
 
452
        content = 'foo = bar\n'
 
453
        conf = config.IniBasedConfig.from_string(
 
454
            content, file_name='./test.conf', save=True)
 
455
        self.assertFileEqual(content, 'test.conf')
 
456
 
 
457
 
 
458
class TestIniBaseConfigOnDisk(tests.TestCaseInTempDir):
 
459
 
 
460
    def test_cannot_reload_without_name(self):
 
461
        conf = config.IniBasedConfig.from_string(sample_config_text)
 
462
        self.assertRaises(AssertionError, conf.reload)
 
463
 
 
464
    def test_reload_see_new_value(self):
 
465
        c1 = config.IniBasedConfig.from_string('editor=vim\n',
 
466
                                               file_name='./test/conf')
 
467
        c1._write_config_file()
 
468
        c2 = config.IniBasedConfig.from_string('editor=emacs\n',
 
469
                                               file_name='./test/conf')
 
470
        c2._write_config_file()
 
471
        self.assertEqual('vim', c1.get_user_option('editor'))
 
472
        self.assertEqual('emacs', c2.get_user_option('editor'))
 
473
        # Make sure we get the Right value
 
474
        c1.reload()
 
475
        self.assertEqual('emacs', c1.get_user_option('editor'))
 
476
 
 
477
 
 
478
class TestLockableConfig(tests.TestCaseInTempDir):
 
479
 
 
480
    # Set by load_tests
 
481
    config_class = None
 
482
    config_args = None
 
483
    config_section = None
 
484
 
 
485
    def setUp(self):
 
486
        super(TestLockableConfig, self).setUp()
 
487
        self._content = '[%s]\none=1\ntwo=2\n' % (self.config_section,)
 
488
        self.config = self.create_config(self._content)
 
489
 
 
490
    def get_existing_config(self):
 
491
        return self.config_class(*self.config_args)
 
492
 
 
493
    def create_config(self, content):
 
494
        c = self.config_class.from_string(content, *self.config_args, save=True)
 
495
        return c
 
496
 
 
497
    def test_simple_read_access(self):
 
498
        self.assertEquals('1', self.config.get_user_option('one'))
 
499
 
 
500
    def test_simple_write_access(self):
 
501
        self.config.set_user_option('one', 'one')
 
502
        self.assertEquals('one', self.config.get_user_option('one'))
 
503
 
 
504
    def test_listen_to_the_last_speaker(self):
 
505
        c1 = self.config
 
506
        c2 = self.get_existing_config()
 
507
        c1.set_user_option('one', 'ONE')
 
508
        c2.set_user_option('two', 'TWO')
 
509
        self.assertEquals('ONE', c1.get_user_option('one'))
 
510
        self.assertEquals('TWO', c2.get_user_option('two'))
 
511
        # The second update respect the first one
 
512
        self.assertEquals('ONE', c2.get_user_option('one'))
 
513
 
 
514
    def test_last_speaker_wins(self):
 
515
        # If the same config is not shared, the same variable modified twice
 
516
        # can only see a single result.
 
517
        c1 = self.config
 
518
        c2 = self.get_existing_config()
 
519
        c1.set_user_option('one', 'c1')
 
520
        c2.set_user_option('one', 'c2')
 
521
        self.assertEquals('c2', c2._get_user_option('one'))
 
522
        # The first modification is still available until another refresh
 
523
        # occur
 
524
        self.assertEquals('c1', c1._get_user_option('one'))
 
525
        c1.set_user_option('two', 'done')
 
526
        self.assertEquals('c2', c1._get_user_option('one'))
 
527
 
 
528
    def test_writes_are_serialized(self):
 
529
        c1 = self.config
 
530
        c2 = self.get_existing_config()
 
531
 
 
532
        # We spawn a thread that will pause *during* the write
 
533
        before_writing = threading.Event()
 
534
        after_writing = threading.Event()
 
535
        writing_done = threading.Event()
 
536
        c1_orig = c1._write_config_file
 
537
        def c1_write_config_file():
 
538
            before_writing.set()
 
539
            c1_orig()
 
540
            # The lock is held we wait for the main thread to decide when to
 
541
            # continue
 
542
            after_writing.wait()
 
543
        c1._write_config_file = c1_write_config_file
 
544
        def c1_set_option():
 
545
            c1.set_user_option('one', 'c1')
 
546
            writing_done.set()
 
547
        t1 = threading.Thread(target=c1_set_option)
 
548
        # Collect the thread after the test
 
549
        self.addCleanup(t1.join)
 
550
        # Be ready to unblock the thread if the test goes wrong
 
551
        self.addCleanup(after_writing.set)
 
552
        t1.start()
 
553
        before_writing.wait()
 
554
        self.assertTrue(c1._lock.is_held)
 
555
        self.assertRaises(errors.LockContention,
 
556
                          c2.set_user_option, 'one', 'c2')
 
557
        self.assertEquals('c1', c1.get_user_option('one'))
 
558
        # Let the lock be released
 
559
        after_writing.set()
 
560
        writing_done.wait()
 
561
        c2.set_user_option('one', 'c2')
 
562
        self.assertEquals('c2', c2.get_user_option('one'))
 
563
 
 
564
    def test_read_while_writing(self):
 
565
       c1 = self.config
 
566
       # We spawn a thread that will pause *during* the write
 
567
       ready_to_write = threading.Event()
 
568
       do_writing = threading.Event()
 
569
       writing_done = threading.Event()
 
570
       c1_orig = c1._write_config_file
 
571
       def c1_write_config_file():
 
572
           ready_to_write.set()
 
573
           # The lock is held we wait for the main thread to decide when to
 
574
           # continue
 
575
           do_writing.wait()
 
576
           c1_orig()
 
577
           writing_done.set()
 
578
       c1._write_config_file = c1_write_config_file
 
579
       def c1_set_option():
 
580
           c1.set_user_option('one', 'c1')
 
581
       t1 = threading.Thread(target=c1_set_option)
 
582
       # Collect the thread after the test
 
583
       self.addCleanup(t1.join)
 
584
       # Be ready to unblock the thread if the test goes wrong
 
585
       self.addCleanup(do_writing.set)
 
586
       t1.start()
 
587
       # Ensure the thread is ready to write
 
588
       ready_to_write.wait()
 
589
       self.assertTrue(c1._lock.is_held)
 
590
       self.assertEquals('c1', c1.get_user_option('one'))
 
591
       # If we read during the write, we get the old value
 
592
       c2 = self.get_existing_config()
 
593
       self.assertEquals('1', c2.get_user_option('one'))
 
594
       # Let the writing occur and ensure it occurred
 
595
       do_writing.set()
 
596
       writing_done.wait()
 
597
       # Now we get the updated value
 
598
       c3 = self.get_existing_config()
 
599
       self.assertEquals('c1', c3.get_user_option('one'))
 
600
 
 
601
 
414
602
class TestGetUserOptionAs(TestIniConfig):
415
603
 
416
604
    def test_get_user_option_as_bool(self):
522
710
        branch = self.make_branch('branch')
523
711
        self.assertEqual('branch', branch.nick)
524
712
 
525
 
        locations = config.locations_config_filename()
526
 
        config.ensure_config_dir_exists()
527
713
        local_url = urlutils.local_path_to_url('branch')
528
 
        open(locations, 'wb').write('[%s]\nnickname = foobar'
529
 
                                    % (local_url,))
 
714
        conf = config.LocationConfig.from_string(
 
715
            '[%s]\nnickname = foobar' % (local_url,),
 
716
            local_url, save=True)
530
717
        self.assertEqual('foobar', branch.nick)
531
718
 
532
719
    def test_config_local_path(self):
534
721
        branch = self.make_branch('branch')
535
722
        self.assertEqual('branch', branch.nick)
536
723
 
537
 
        locations = config.locations_config_filename()
538
 
        config.ensure_config_dir_exists()
539
 
        open(locations, 'wb').write('[%s/branch]\nnickname = barry'
540
 
                                    % (osutils.getcwd().encode('utf8'),))
 
724
        local_path = osutils.getcwd().encode('utf8')
 
725
        conf = config.LocationConfig.from_string(
 
726
            '[%s/branch]\nnickname = barry' % (local_path,),
 
727
            'branch',  save=True)
541
728
        self.assertEqual('barry', branch.nick)
542
729
 
543
730
    def test_config_creates_local(self):
544
731
        """Creating a new entry in config uses a local path."""
545
732
        branch = self.make_branch('branch', format='knit')
546
733
        branch.set_push_location('http://foobar')
547
 
        locations = config.locations_config_filename()
548
734
        local_path = osutils.getcwd().encode('utf8')
549
735
        # Surprisingly ConfigObj doesn't create a trailing newline
550
 
        self.check_file_contents(locations,
 
736
        self.check_file_contents(config.locations_config_filename(),
551
737
                                 '[%s/branch]\n'
552
738
                                 'push_location = http://foobar\n'
553
739
                                 'push_location:policy = norecurse\n'
558
744
        self.assertEqual('!repo', b.get_config().get_nickname())
559
745
 
560
746
    def test_warn_if_masked(self):
561
 
        _warning = trace.warning
562
747
        warnings = []
563
748
        def warning(*args):
564
749
            warnings.append(args[0] % args[1:])
 
750
        self.overrideAttr(trace, 'warning', warning)
565
751
 
566
752
        def set_option(store, warn_masked=True):
567
753
            warnings[:] = []
573
759
            else:
574
760
                self.assertEqual(1, len(warnings))
575
761
                self.assertEqual(warning, warnings[0])
576
 
        trace.warning = warning
577
 
        try:
578
 
            branch = self.make_branch('.')
579
 
            conf = branch.get_config()
580
 
            set_option(config.STORE_GLOBAL)
581
 
            assertWarning(None)
582
 
            set_option(config.STORE_BRANCH)
583
 
            assertWarning(None)
584
 
            set_option(config.STORE_GLOBAL)
585
 
            assertWarning('Value "4" is masked by "3" from branch.conf')
586
 
            set_option(config.STORE_GLOBAL, warn_masked=False)
587
 
            assertWarning(None)
588
 
            set_option(config.STORE_LOCATION)
589
 
            assertWarning(None)
590
 
            set_option(config.STORE_BRANCH)
591
 
            assertWarning('Value "3" is masked by "0" from locations.conf')
592
 
            set_option(config.STORE_BRANCH, warn_masked=False)
593
 
            assertWarning(None)
594
 
        finally:
595
 
            trace.warning = _warning
 
762
        branch = self.make_branch('.')
 
763
        conf = branch.get_config()
 
764
        set_option(config.STORE_GLOBAL)
 
765
        assertWarning(None)
 
766
        set_option(config.STORE_BRANCH)
 
767
        assertWarning(None)
 
768
        set_option(config.STORE_GLOBAL)
 
769
        assertWarning('Value "4" is masked by "3" from branch.conf')
 
770
        set_option(config.STORE_GLOBAL, warn_masked=False)
 
771
        assertWarning(None)
 
772
        set_option(config.STORE_LOCATION)
 
773
        assertWarning(None)
 
774
        set_option(config.STORE_BRANCH)
 
775
        assertWarning('Value "3" is masked by "0" from locations.conf')
 
776
        set_option(config.STORE_BRANCH, warn_masked=False)
 
777
        assertWarning(None)
596
778
 
597
779
 
598
780
class TestGlobalConfigItems(tests.TestCase):
599
781
 
600
782
    def test_user_id(self):
601
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
602
 
        my_config = config.GlobalConfig()
603
 
        my_config._parser = my_config._get_parser(file=config_file)
 
783
        my_config = config.GlobalConfig.from_string(sample_config_text)
604
784
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
605
785
                         my_config._get_user_id())
606
786
 
607
787
    def test_absent_user_id(self):
608
 
        config_file = StringIO("")
609
788
        my_config = config.GlobalConfig()
610
 
        my_config._parser = my_config._get_parser(file=config_file)
611
789
        self.assertEqual(None, my_config._get_user_id())
612
790
 
613
791
    def test_configured_editor(self):
614
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
615
 
        my_config = config.GlobalConfig()
616
 
        my_config._parser = my_config._get_parser(file=config_file)
 
792
        my_config = config.GlobalConfig.from_string(sample_config_text)
617
793
        self.assertEqual("vim", my_config.get_editor())
618
794
 
619
795
    def test_signatures_always(self):
620
 
        config_file = StringIO(sample_always_signatures)
621
 
        my_config = config.GlobalConfig()
622
 
        my_config._parser = my_config._get_parser(file=config_file)
 
796
        my_config = config.GlobalConfig.from_string(sample_always_signatures)
623
797
        self.assertEqual(config.CHECK_NEVER,
624
798
                         my_config.signature_checking())
625
799
        self.assertEqual(config.SIGN_ALWAYS,
627
801
        self.assertEqual(True, my_config.signature_needed())
628
802
 
629
803
    def test_signatures_if_possible(self):
630
 
        config_file = StringIO(sample_maybe_signatures)
631
 
        my_config = config.GlobalConfig()
632
 
        my_config._parser = my_config._get_parser(file=config_file)
 
804
        my_config = config.GlobalConfig.from_string(sample_maybe_signatures)
633
805
        self.assertEqual(config.CHECK_NEVER,
634
806
                         my_config.signature_checking())
635
807
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
637
809
        self.assertEqual(False, my_config.signature_needed())
638
810
 
639
811
    def test_signatures_ignore(self):
640
 
        config_file = StringIO(sample_ignore_signatures)
641
 
        my_config = config.GlobalConfig()
642
 
        my_config._parser = my_config._get_parser(file=config_file)
 
812
        my_config = config.GlobalConfig.from_string(sample_ignore_signatures)
643
813
        self.assertEqual(config.CHECK_ALWAYS,
644
814
                         my_config.signature_checking())
645
815
        self.assertEqual(config.SIGN_NEVER,
647
817
        self.assertEqual(False, my_config.signature_needed())
648
818
 
649
819
    def _get_sample_config(self):
650
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
651
 
        my_config = config.GlobalConfig()
652
 
        my_config._parser = my_config._get_parser(file=config_file)
 
820
        my_config = config.GlobalConfig.from_string(sample_config_text)
653
821
        return my_config
654
822
 
655
823
    def test_gpg_signing_command(self):
658
826
        self.assertEqual(False, my_config.signature_needed())
659
827
 
660
828
    def _get_empty_config(self):
661
 
        config_file = StringIO("")
662
829
        my_config = config.GlobalConfig()
663
 
        my_config._parser = my_config._get_parser(file=config_file)
664
830
        return my_config
665
831
 
666
832
    def test_gpg_signing_command_unset(self):
761
927
        self.assertEqual(parser._calls,
762
928
                         [('__init__', config.locations_config_filename(),
763
929
                           'utf-8')])
764
 
        config.ensure_config_dir_exists()
765
 
        #os.mkdir(config.config_dir())
766
 
        f = file(config.branches_config_filename(), 'wb')
767
 
        f.write('')
768
 
        f.close()
769
 
        oldparserclass = config.ConfigObj
770
 
        config.ConfigObj = InstrumentedConfigObj
771
 
        try:
772
 
            my_config = config.LocationConfig('http://www.example.com')
773
 
            parser = my_config._get_parser()
774
 
        finally:
775
 
            config.ConfigObj = oldparserclass
776
930
 
777
931
    def test_get_global_config(self):
778
932
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
1007
1161
                         self.my_config.post_commit())
1008
1162
 
1009
1163
    def get_branch_config(self, location, global_config=None):
 
1164
        my_branch = FakeBranch(location)
1010
1165
        if global_config is None:
1011
 
            global_file = StringIO(sample_config_text.encode('utf-8'))
1012
 
        else:
1013
 
            global_file = StringIO(global_config.encode('utf-8'))
1014
 
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
1015
 
        self.my_config = config.BranchConfig(FakeBranch(location))
1016
 
        # Force location config to use specified file
1017
 
        self.my_location_config = self.my_config._get_location_config()
1018
 
        self.my_location_config._get_parser(branches_file)
1019
 
        # Force global config to use specified file
1020
 
        self.my_config._get_global_config()._get_parser(global_file)
 
1166
            global_config = sample_config_text
 
1167
 
 
1168
        my_global_config = config.GlobalConfig.from_string(global_config,
 
1169
                                                           save=True)
 
1170
        my_location_config = config.LocationConfig.from_string(
 
1171
            sample_branches_text, my_branch.base, save=True)
 
1172
        my_config = config.BranchConfig(my_branch)
 
1173
        self.my_config = my_config
 
1174
        self.my_location_config = my_config._get_location_config()
1021
1175
 
1022
1176
    def test_set_user_setting_sets_and_saves(self):
1023
1177
        self.get_branch_config('/a/c')
1024
1178
        record = InstrumentedConfigObj("foo")
1025
1179
        self.my_location_config._parser = record
1026
1180
 
1027
 
        real_mkdir = os.mkdir
1028
 
        self.created = False
1029
 
        def checked_mkdir(path, mode=0777):
1030
 
            self.log('making directory: %s', path)
1031
 
            real_mkdir(path, mode)
1032
 
            self.created = True
1033
 
 
1034
 
        os.mkdir = checked_mkdir
1035
 
        try:
1036
 
            self.callDeprecated(['The recurse option is deprecated as of '
1037
 
                                 '0.14.  The section "/a/c" has been '
1038
 
                                 'converted to use policies.'],
1039
 
                                self.my_config.set_user_option,
1040
 
                                'foo', 'bar', store=config.STORE_LOCATION)
1041
 
        finally:
1042
 
            os.mkdir = real_mkdir
1043
 
 
1044
 
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
1045
 
        self.assertEqual([('__contains__', '/a/c'),
 
1181
        self.callDeprecated(['The recurse option is deprecated as of '
 
1182
                             '0.14.  The section "/a/c" has been '
 
1183
                             'converted to use policies.'],
 
1184
                            self.my_config.set_user_option,
 
1185
                            'foo', 'bar', store=config.STORE_LOCATION)
 
1186
        self.assertEqual([('reload',),
 
1187
                          ('__contains__', '/a/c'),
1046
1188
                          ('__contains__', '/a/c/'),
1047
1189
                          ('__setitem__', '/a/c', {}),
1048
1190
                          ('__getitem__', '/a/c'),
1091
1233
option = exact
1092
1234
"""
1093
1235
 
1094
 
 
1095
1236
class TestBranchConfigItems(tests.TestCaseInTempDir):
1096
1237
 
1097
1238
    def get_branch_config(self, global_config=None, location=None,
1098
1239
                          location_config=None, branch_data_config=None):
1099
 
        my_config = config.BranchConfig(FakeBranch(location))
 
1240
        my_branch = FakeBranch(location)
1100
1241
        if global_config is not None:
1101
 
            global_file = StringIO(global_config.encode('utf-8'))
1102
 
            my_config._get_global_config()._get_parser(global_file)
1103
 
        self.my_location_config = my_config._get_location_config()
 
1242
            my_global_config = config.GlobalConfig.from_string(global_config,
 
1243
                                                               save=True)
1104
1244
        if location_config is not None:
1105
 
            location_file = StringIO(location_config.encode('utf-8'))
1106
 
            self.my_location_config._get_parser(location_file)
 
1245
            my_location_config = config.LocationConfig.from_string(
 
1246
                location_config, my_branch.base, save=True)
 
1247
        my_config = config.BranchConfig(my_branch)
1107
1248
        if branch_data_config is not None:
1108
1249
            my_config.branch.control_files.files['branch.conf'] = \
1109
1250
                branch_data_config
1123
1264
                         my_config.username())
1124
1265
 
1125
1266
    def test_not_set_in_branch(self):
1126
 
        my_config = self.get_branch_config(sample_config_text)
 
1267
        my_config = self.get_branch_config(global_config=sample_config_text)
1127
1268
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
1128
1269
                         my_config._get_user_id())
1129
1270
        my_config.branch.control_files.files['email'] = "John"
1153
1294
 
1154
1295
    def test_gpg_signing_command(self):
1155
1296
        my_config = self.get_branch_config(
 
1297
            global_config=sample_config_text,
1156
1298
            # branch data cannot set gpg_signing_command
1157
1299
            branch_data_config="gpg_signing_command=pgp")
1158
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
1159
 
        my_config._get_global_config()._get_parser(config_file)
1160
1300
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
1161
1301
 
1162
1302
    def test_get_user_option_global(self):
1163
 
        branch = FakeBranch()
1164
 
        my_config = config.BranchConfig(branch)
1165
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
1166
 
        (my_config._get_global_config()._get_parser(config_file))
 
1303
        my_config = self.get_branch_config(global_config=sample_config_text)
1167
1304
        self.assertEqual('something',
1168
1305
                         my_config.get_user_option('user_global_option'))
1169
1306
 
1170
1307
    def test_post_commit_default(self):
1171
 
        branch = FakeBranch()
1172
 
        my_config = self.get_branch_config(sample_config_text, '/a/c',
1173
 
                                           sample_branches_text)
 
1308
        my_config = self.get_branch_config(global_config=sample_config_text,
 
1309
                                      location='/a/c',
 
1310
                                      location_config=sample_branches_text)
1174
1311
        self.assertEqual(my_config.branch.base, '/a/c')
1175
1312
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1176
1313
                         my_config.post_commit())
1177
1314
        my_config.set_user_option('post_commit', 'rmtree_root')
1178
 
        # post-commit is ignored when bresent in branch data
 
1315
        # post-commit is ignored when present in branch data
1179
1316
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1180
1317
                         my_config.post_commit())
1181
1318
        my_config.set_user_option('post_commit', 'rmtree_root',
1183
1320
        self.assertEqual('rmtree_root', my_config.post_commit())
1184
1321
 
1185
1322
    def test_config_precedence(self):
 
1323
        # FIXME: eager test, luckily no persitent config file makes it fail
 
1324
        # -- vila 20100716
1186
1325
        my_config = self.get_branch_config(global_config=precedence_global)
1187
1326
        self.assertEqual(my_config.get_user_option('option'), 'global')
1188
1327
        my_config = self.get_branch_config(global_config=precedence_global,
1189
 
                                      branch_data_config=precedence_branch)
 
1328
                                           branch_data_config=precedence_branch)
1190
1329
        self.assertEqual(my_config.get_user_option('option'), 'branch')
1191
 
        my_config = self.get_branch_config(global_config=precedence_global,
1192
 
                                      branch_data_config=precedence_branch,
1193
 
                                      location_config=precedence_location)
 
1330
        my_config = self.get_branch_config(
 
1331
            global_config=precedence_global,
 
1332
            branch_data_config=precedence_branch,
 
1333
            location_config=precedence_location)
1194
1334
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
1195
 
        my_config = self.get_branch_config(global_config=precedence_global,
1196
 
                                      branch_data_config=precedence_branch,
1197
 
                                      location_config=precedence_location,
1198
 
                                      location='http://example.com/specific')
 
1335
        my_config = self.get_branch_config(
 
1336
            global_config=precedence_global,
 
1337
            branch_data_config=precedence_branch,
 
1338
            location_config=precedence_location,
 
1339
            location='http://example.com/specific')
1199
1340
        self.assertEqual(my_config.get_user_option('option'), 'exact')
1200
1341
 
1201
1342
    def test_get_mail_client(self):