/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Martin Pool
  • Date: 2010-10-08 04:38:25 UTC
  • mfrom: (5462 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5478.
  • Revision ID: mbp@sourcefrog.net-20101008043825-b181r8bo5r3qwb6j
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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 (
35
36
    trace,
36
37
    transport,
37
38
    )
 
39
from bzrlib.tests import features
38
40
from bzrlib.util.configobj import configobj
39
41
 
40
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
 
41
67
sample_long_alias="log -r-15..-1 --line"
42
68
sample_config_text = u"""
43
69
[DEFAULT]
129
155
        self._calls.append(('keys',))
130
156
        return []
131
157
 
 
158
    def reload(self):
 
159
        self._calls.append(('reload',))
 
160
 
132
161
    def write(self, arg):
133
162
        self._calls.append(('write',))
134
163
 
350
379
        self.assertEqual(config.config_filename(),
351
380
                         self.bzr_home + '/bazaar.conf')
352
381
 
353
 
    def test_branches_config_filename(self):
354
 
        self.assertEqual(config.branches_config_filename(),
355
 
                         self.bzr_home + '/branches.conf')
356
 
 
357
382
    def test_locations_config_filename(self):
358
383
        self.assertEqual(config.locations_config_filename(),
359
384
                         self.bzr_home + '/locations.conf')
367
392
            '/home/bogus/.cache')
368
393
 
369
394
 
370
 
class TestIniConfig(tests.TestCase):
 
395
class TestIniConfig(tests.TestCaseInTempDir):
371
396
 
372
397
    def make_config_parser(self, s):
373
 
        conf = config.IniBasedConfig(None)
374
 
        parser = conf._get_parser(file=StringIO(s.encode('utf-8')))
375
 
        return conf, parser
 
398
        conf = config.IniBasedConfig.from_string(s)
 
399
        return conf, conf._get_parser()
376
400
 
377
401
 
378
402
class TestIniConfigBuilding(TestIniConfig):
379
403
 
380
404
    def test_contructs(self):
381
 
        my_config = config.IniBasedConfig("nothing")
 
405
        my_config = config.IniBasedConfig()
382
406
 
383
407
    def test_from_fp(self):
384
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
385
 
        my_config = config.IniBasedConfig(None)
386
 
        self.failUnless(
387
 
            isinstance(my_config._get_parser(file=config_file),
388
 
                        configobj.ConfigObj))
 
408
        my_config = config.IniBasedConfig.from_string(sample_config_text)
 
409
        self.assertIsInstance(my_config._get_parser(), configobj.ConfigObj)
389
410
 
390
411
    def test_cached(self):
391
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
392
 
        my_config = config.IniBasedConfig(None)
393
 
        parser = my_config._get_parser(file=config_file)
 
412
        my_config = config.IniBasedConfig.from_string(sample_config_text)
 
413
        parser = my_config._get_parser()
394
414
        self.failUnless(my_config._get_parser() is parser)
395
415
 
 
416
    def _dummy_chown(self, path, uid, gid):
 
417
        self.path, self.uid, self.gid = path, uid, gid
 
418
 
 
419
    def test_ini_config_ownership(self):
 
420
        """Ensure that chown is happening during _write_config_file"""
 
421
        self.requireFeature(features.chown_feature)
 
422
        self.overrideAttr(os, 'chown', self._dummy_chown)
 
423
        self.path = self.uid = self.gid = None
 
424
        conf = config.IniBasedConfig(file_name='./foo.conf')
 
425
        conf._write_config_file()
 
426
        self.assertEquals(self.path, './foo.conf')
 
427
        self.assertTrue(isinstance(self.uid, int))
 
428
        self.assertTrue(isinstance(self.gid, int))
 
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
        kwargs = dict(save=True)
 
495
        c = self.config_class.from_string(content, *self.config_args, **kwargs)
 
496
        return c
 
497
 
 
498
    def test_simple_read_access(self):
 
499
        self.assertEquals('1', self.config.get_user_option('one'))
 
500
 
 
501
    def test_simple_write_access(self):
 
502
        self.config.set_user_option('one', 'one')
 
503
        self.assertEquals('one', self.config.get_user_option('one'))
 
504
 
 
505
    def test_listen_to_the_last_speaker(self):
 
506
        c1 = self.config
 
507
        c2 = self.get_existing_config()
 
508
        c1.set_user_option('one', 'ONE')
 
509
        c2.set_user_option('two', 'TWO')
 
510
        self.assertEquals('ONE', c1.get_user_option('one'))
 
511
        self.assertEquals('TWO', c2.get_user_option('two'))
 
512
        # The second update respect the first one
 
513
        self.assertEquals('ONE', c2.get_user_option('one'))
 
514
 
 
515
    def test_last_speaker_wins(self):
 
516
        # If the same config is not shared, the same variable modified twice
 
517
        # can only see a single result.
 
518
        c1 = self.config
 
519
        c2 = self.get_existing_config()
 
520
        c1.set_user_option('one', 'c1')
 
521
        c2.set_user_option('one', 'c2')
 
522
        self.assertEquals('c2', c2._get_user_option('one'))
 
523
        # The first modification is still available until another refresh
 
524
        # occur
 
525
        self.assertEquals('c1', c1._get_user_option('one'))
 
526
        c1.set_user_option('two', 'done')
 
527
        self.assertEquals('c2', c1._get_user_option('one'))
 
528
 
 
529
    def test_writes_are_serialized(self):
 
530
        c1 = self.config
 
531
        c2 = self.get_existing_config()
 
532
 
 
533
        # We spawn a thread that will pause *during* the write
 
534
        before_writing = threading.Event()
 
535
        after_writing = threading.Event()
 
536
        writing_done = threading.Event()
 
537
        c1_orig = c1._write_config_file
 
538
        def c1_write_config_file():
 
539
            before_writing.set()
 
540
            c1_orig()
 
541
            # The lock is held we wait for the main thread to decide when to
 
542
            # continue
 
543
            after_writing.wait()
 
544
        c1._write_config_file = c1_write_config_file
 
545
        def c1_set_option():
 
546
            c1.set_user_option('one', 'c1')
 
547
            writing_done.set()
 
548
        t1 = threading.Thread(target=c1_set_option)
 
549
        # Collect the thread after the test
 
550
        self.addCleanup(t1.join)
 
551
        # Be ready to unblock the thread if the test goes wrong
 
552
        self.addCleanup(after_writing.set)
 
553
        t1.start()
 
554
        before_writing.wait()
 
555
        self.assertTrue(c1._lock.is_held)
 
556
        self.assertRaises(errors.LockContention,
 
557
                          c2.set_user_option, 'one', 'c2')
 
558
        self.assertEquals('c1', c1.get_user_option('one'))
 
559
        # Let the lock be released
 
560
        after_writing.set()
 
561
        writing_done.wait()
 
562
        c2.set_user_option('one', 'c2')
 
563
        self.assertEquals('c2', c2.get_user_option('one'))
 
564
 
 
565
    def test_read_while_writing(self):
 
566
       c1 = self.config
 
567
       # We spawn a thread that will pause *during* the write
 
568
       ready_to_write = threading.Event()
 
569
       do_writing = threading.Event()
 
570
       writing_done = threading.Event()
 
571
       c1_orig = c1._write_config_file
 
572
       def c1_write_config_file():
 
573
           ready_to_write.set()
 
574
           # The lock is held we wait for the main thread to decide when to
 
575
           # continue
 
576
           do_writing.wait()
 
577
           c1_orig()
 
578
           writing_done.set()
 
579
       c1._write_config_file = c1_write_config_file
 
580
       def c1_set_option():
 
581
           c1.set_user_option('one', 'c1')
 
582
       t1 = threading.Thread(target=c1_set_option)
 
583
       # Collect the thread after the test
 
584
       self.addCleanup(t1.join)
 
585
       # Be ready to unblock the thread if the test goes wrong
 
586
       self.addCleanup(do_writing.set)
 
587
       t1.start()
 
588
       # Ensure the thread is ready to write
 
589
       ready_to_write.wait()
 
590
       self.assertTrue(c1._lock.is_held)
 
591
       self.assertEquals('c1', c1.get_user_option('one'))
 
592
       # If we read during the write, we get the old value
 
593
       c2 = self.get_existing_config()
 
594
       self.assertEquals('1', c2.get_user_option('one'))
 
595
       # Let the writing occur and ensure it occurred
 
596
       do_writing.set()
 
597
       writing_done.wait()
 
598
       # Now we get the updated value
 
599
       c3 = self.get_existing_config()
 
600
       self.assertEquals('c1', c3.get_user_option('one'))
 
601
 
396
602
 
397
603
class TestGetUserOptionAs(TestIniConfig):
398
604
 
406
612
        get_bool = conf.get_user_option_as_bool
407
613
        self.assertEqual(True, get_bool('a_true_bool'))
408
614
        self.assertEqual(False, get_bool('a_false_bool'))
 
615
        warnings = []
 
616
        def warning(*args):
 
617
            warnings.append(args[0] % args[1:])
 
618
        self.overrideAttr(trace, 'warning', warning)
 
619
        msg = 'Value "%s" is not a boolean for "%s"'
409
620
        self.assertIs(None, get_bool('an_invalid_bool'))
 
621
        self.assertEquals(msg % ('maybe', 'an_invalid_bool'), warnings[0])
 
622
        warnings = []
410
623
        self.assertIs(None, get_bool('not_defined_in_this_config'))
411
 
 
 
624
        self.assertEquals([], warnings)
412
625
 
413
626
    def test_get_user_option_as_list(self):
414
627
        conf, parser = self.make_config_parser("""
498
711
        branch = self.make_branch('branch')
499
712
        self.assertEqual('branch', branch.nick)
500
713
 
501
 
        locations = config.locations_config_filename()
502
 
        config.ensure_config_dir_exists()
503
714
        local_url = urlutils.local_path_to_url('branch')
504
 
        open(locations, 'wb').write('[%s]\nnickname = foobar'
505
 
                                    % (local_url,))
 
715
        conf = config.LocationConfig.from_string(
 
716
            '[%s]\nnickname = foobar' % (local_url,),
 
717
            local_url, save=True)
506
718
        self.assertEqual('foobar', branch.nick)
507
719
 
508
720
    def test_config_local_path(self):
510
722
        branch = self.make_branch('branch')
511
723
        self.assertEqual('branch', branch.nick)
512
724
 
513
 
        locations = config.locations_config_filename()
514
 
        config.ensure_config_dir_exists()
515
 
        open(locations, 'wb').write('[%s/branch]\nnickname = barry'
516
 
                                    % (osutils.getcwd().encode('utf8'),))
 
725
        local_path = osutils.getcwd().encode('utf8')
 
726
        conf = config.LocationConfig.from_string(
 
727
            '[%s/branch]\nnickname = barry' % (local_path,),
 
728
            'branch',  save=True)
517
729
        self.assertEqual('barry', branch.nick)
518
730
 
519
731
    def test_config_creates_local(self):
520
732
        """Creating a new entry in config uses a local path."""
521
733
        branch = self.make_branch('branch', format='knit')
522
734
        branch.set_push_location('http://foobar')
523
 
        locations = config.locations_config_filename()
524
735
        local_path = osutils.getcwd().encode('utf8')
525
736
        # Surprisingly ConfigObj doesn't create a trailing newline
526
 
        self.check_file_contents(locations,
 
737
        self.check_file_contents(config.locations_config_filename(),
527
738
                                 '[%s/branch]\n'
528
739
                                 'push_location = http://foobar\n'
529
740
                                 'push_location:policy = norecurse\n'
534
745
        self.assertEqual('!repo', b.get_config().get_nickname())
535
746
 
536
747
    def test_warn_if_masked(self):
537
 
        _warning = trace.warning
538
748
        warnings = []
539
749
        def warning(*args):
540
750
            warnings.append(args[0] % args[1:])
 
751
        self.overrideAttr(trace, 'warning', warning)
541
752
 
542
753
        def set_option(store, warn_masked=True):
543
754
            warnings[:] = []
549
760
            else:
550
761
                self.assertEqual(1, len(warnings))
551
762
                self.assertEqual(warning, warnings[0])
552
 
        trace.warning = warning
553
 
        try:
554
 
            branch = self.make_branch('.')
555
 
            conf = branch.get_config()
556
 
            set_option(config.STORE_GLOBAL)
557
 
            assertWarning(None)
558
 
            set_option(config.STORE_BRANCH)
559
 
            assertWarning(None)
560
 
            set_option(config.STORE_GLOBAL)
561
 
            assertWarning('Value "4" is masked by "3" from branch.conf')
562
 
            set_option(config.STORE_GLOBAL, warn_masked=False)
563
 
            assertWarning(None)
564
 
            set_option(config.STORE_LOCATION)
565
 
            assertWarning(None)
566
 
            set_option(config.STORE_BRANCH)
567
 
            assertWarning('Value "3" is masked by "0" from locations.conf')
568
 
            set_option(config.STORE_BRANCH, warn_masked=False)
569
 
            assertWarning(None)
570
 
        finally:
571
 
            trace.warning = _warning
572
 
 
573
 
 
574
 
class TestGlobalConfigItems(tests.TestCase):
 
763
        branch = self.make_branch('.')
 
764
        conf = branch.get_config()
 
765
        set_option(config.STORE_GLOBAL)
 
766
        assertWarning(None)
 
767
        set_option(config.STORE_BRANCH)
 
768
        assertWarning(None)
 
769
        set_option(config.STORE_GLOBAL)
 
770
        assertWarning('Value "4" is masked by "3" from branch.conf')
 
771
        set_option(config.STORE_GLOBAL, warn_masked=False)
 
772
        assertWarning(None)
 
773
        set_option(config.STORE_LOCATION)
 
774
        assertWarning(None)
 
775
        set_option(config.STORE_BRANCH)
 
776
        assertWarning('Value "3" is masked by "0" from locations.conf')
 
777
        set_option(config.STORE_BRANCH, warn_masked=False)
 
778
        assertWarning(None)
 
779
 
 
780
 
 
781
class TestGlobalConfigItems(tests.TestCaseInTempDir):
575
782
 
576
783
    def test_user_id(self):
577
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
578
 
        my_config = config.GlobalConfig()
579
 
        my_config._parser = my_config._get_parser(file=config_file)
 
784
        my_config = config.GlobalConfig.from_string(sample_config_text)
580
785
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
581
786
                         my_config._get_user_id())
582
787
 
583
788
    def test_absent_user_id(self):
584
 
        config_file = StringIO("")
585
789
        my_config = config.GlobalConfig()
586
 
        my_config._parser = my_config._get_parser(file=config_file)
587
790
        self.assertEqual(None, my_config._get_user_id())
588
791
 
589
792
    def test_configured_editor(self):
590
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
591
 
        my_config = config.GlobalConfig()
592
 
        my_config._parser = my_config._get_parser(file=config_file)
 
793
        my_config = config.GlobalConfig.from_string(sample_config_text)
593
794
        self.assertEqual("vim", my_config.get_editor())
594
795
 
595
796
    def test_signatures_always(self):
596
 
        config_file = StringIO(sample_always_signatures)
597
 
        my_config = config.GlobalConfig()
598
 
        my_config._parser = my_config._get_parser(file=config_file)
 
797
        my_config = config.GlobalConfig.from_string(sample_always_signatures)
599
798
        self.assertEqual(config.CHECK_NEVER,
600
799
                         my_config.signature_checking())
601
800
        self.assertEqual(config.SIGN_ALWAYS,
603
802
        self.assertEqual(True, my_config.signature_needed())
604
803
 
605
804
    def test_signatures_if_possible(self):
606
 
        config_file = StringIO(sample_maybe_signatures)
607
 
        my_config = config.GlobalConfig()
608
 
        my_config._parser = my_config._get_parser(file=config_file)
 
805
        my_config = config.GlobalConfig.from_string(sample_maybe_signatures)
609
806
        self.assertEqual(config.CHECK_NEVER,
610
807
                         my_config.signature_checking())
611
808
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
613
810
        self.assertEqual(False, my_config.signature_needed())
614
811
 
615
812
    def test_signatures_ignore(self):
616
 
        config_file = StringIO(sample_ignore_signatures)
617
 
        my_config = config.GlobalConfig()
618
 
        my_config._parser = my_config._get_parser(file=config_file)
 
813
        my_config = config.GlobalConfig.from_string(sample_ignore_signatures)
619
814
        self.assertEqual(config.CHECK_ALWAYS,
620
815
                         my_config.signature_checking())
621
816
        self.assertEqual(config.SIGN_NEVER,
623
818
        self.assertEqual(False, my_config.signature_needed())
624
819
 
625
820
    def _get_sample_config(self):
626
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
627
 
        my_config = config.GlobalConfig()
628
 
        my_config._parser = my_config._get_parser(file=config_file)
 
821
        my_config = config.GlobalConfig.from_string(sample_config_text)
629
822
        return my_config
630
823
 
631
824
    def test_gpg_signing_command(self):
634
827
        self.assertEqual(False, my_config.signature_needed())
635
828
 
636
829
    def _get_empty_config(self):
637
 
        config_file = StringIO("")
638
830
        my_config = config.GlobalConfig()
639
 
        my_config._parser = my_config._get_parser(file=config_file)
640
831
        return my_config
641
832
 
642
833
    def test_gpg_signing_command_unset(self):
737
928
        self.assertEqual(parser._calls,
738
929
                         [('__init__', config.locations_config_filename(),
739
930
                           'utf-8')])
740
 
        config.ensure_config_dir_exists()
741
 
        #os.mkdir(config.config_dir())
742
 
        f = file(config.branches_config_filename(), 'wb')
743
 
        f.write('')
744
 
        f.close()
745
 
        oldparserclass = config.ConfigObj
746
 
        config.ConfigObj = InstrumentedConfigObj
747
 
        try:
748
 
            my_config = config.LocationConfig('http://www.example.com')
749
 
            parser = my_config._get_parser()
750
 
        finally:
751
 
            config.ConfigObj = oldparserclass
752
931
 
753
932
    def test_get_global_config(self):
754
933
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
983
1162
                         self.my_config.post_commit())
984
1163
 
985
1164
    def get_branch_config(self, location, global_config=None):
 
1165
        my_branch = FakeBranch(location)
986
1166
        if global_config is None:
987
 
            global_file = StringIO(sample_config_text.encode('utf-8'))
988
 
        else:
989
 
            global_file = StringIO(global_config.encode('utf-8'))
990
 
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
991
 
        self.my_config = config.BranchConfig(FakeBranch(location))
992
 
        # Force location config to use specified file
993
 
        self.my_location_config = self.my_config._get_location_config()
994
 
        self.my_location_config._get_parser(branches_file)
995
 
        # Force global config to use specified file
996
 
        self.my_config._get_global_config()._get_parser(global_file)
 
1167
            global_config = sample_config_text
 
1168
 
 
1169
        my_global_config = config.GlobalConfig.from_string(global_config,
 
1170
                                                           save=True)
 
1171
        my_location_config = config.LocationConfig.from_string(
 
1172
            sample_branches_text, my_branch.base, save=True)
 
1173
        my_config = config.BranchConfig(my_branch)
 
1174
        self.my_config = my_config
 
1175
        self.my_location_config = my_config._get_location_config()
997
1176
 
998
1177
    def test_set_user_setting_sets_and_saves(self):
999
1178
        self.get_branch_config('/a/c')
1000
1179
        record = InstrumentedConfigObj("foo")
1001
1180
        self.my_location_config._parser = record
1002
1181
 
1003
 
        real_mkdir = os.mkdir
1004
 
        self.created = False
1005
 
        def checked_mkdir(path, mode=0777):
1006
 
            self.log('making directory: %s', path)
1007
 
            real_mkdir(path, mode)
1008
 
            self.created = True
1009
 
 
1010
 
        os.mkdir = checked_mkdir
1011
 
        try:
1012
 
            self.callDeprecated(['The recurse option is deprecated as of '
1013
 
                                 '0.14.  The section "/a/c" has been '
1014
 
                                 'converted to use policies.'],
1015
 
                                self.my_config.set_user_option,
1016
 
                                'foo', 'bar', store=config.STORE_LOCATION)
1017
 
        finally:
1018
 
            os.mkdir = real_mkdir
1019
 
 
1020
 
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
1021
 
        self.assertEqual([('__contains__', '/a/c'),
 
1182
        self.callDeprecated(['The recurse option is deprecated as of '
 
1183
                             '0.14.  The section "/a/c" has been '
 
1184
                             'converted to use policies.'],
 
1185
                            self.my_config.set_user_option,
 
1186
                            'foo', 'bar', store=config.STORE_LOCATION)
 
1187
        self.assertEqual([('reload',),
 
1188
                          ('__contains__', '/a/c'),
1022
1189
                          ('__contains__', '/a/c/'),
1023
1190
                          ('__setitem__', '/a/c', {}),
1024
1191
                          ('__getitem__', '/a/c'),
1067
1234
option = exact
1068
1235
"""
1069
1236
 
1070
 
 
1071
1237
class TestBranchConfigItems(tests.TestCaseInTempDir):
1072
1238
 
1073
1239
    def get_branch_config(self, global_config=None, location=None,
1074
1240
                          location_config=None, branch_data_config=None):
1075
 
        my_config = config.BranchConfig(FakeBranch(location))
 
1241
        my_branch = FakeBranch(location)
1076
1242
        if global_config is not None:
1077
 
            global_file = StringIO(global_config.encode('utf-8'))
1078
 
            my_config._get_global_config()._get_parser(global_file)
1079
 
        self.my_location_config = my_config._get_location_config()
 
1243
            my_global_config = config.GlobalConfig.from_string(global_config,
 
1244
                                                               save=True)
1080
1245
        if location_config is not None:
1081
 
            location_file = StringIO(location_config.encode('utf-8'))
1082
 
            self.my_location_config._get_parser(location_file)
 
1246
            my_location_config = config.LocationConfig.from_string(
 
1247
                location_config, my_branch.base, save=True)
 
1248
        my_config = config.BranchConfig(my_branch)
1083
1249
        if branch_data_config is not None:
1084
1250
            my_config.branch.control_files.files['branch.conf'] = \
1085
1251
                branch_data_config
1099
1265
                         my_config.username())
1100
1266
 
1101
1267
    def test_not_set_in_branch(self):
1102
 
        my_config = self.get_branch_config(sample_config_text)
 
1268
        my_config = self.get_branch_config(global_config=sample_config_text)
1103
1269
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
1104
1270
                         my_config._get_user_id())
1105
1271
        my_config.branch.control_files.files['email'] = "John"
1129
1295
 
1130
1296
    def test_gpg_signing_command(self):
1131
1297
        my_config = self.get_branch_config(
 
1298
            global_config=sample_config_text,
1132
1299
            # branch data cannot set gpg_signing_command
1133
1300
            branch_data_config="gpg_signing_command=pgp")
1134
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
1135
 
        my_config._get_global_config()._get_parser(config_file)
1136
1301
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
1137
1302
 
1138
1303
    def test_get_user_option_global(self):
1139
 
        branch = FakeBranch()
1140
 
        my_config = config.BranchConfig(branch)
1141
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
1142
 
        (my_config._get_global_config()._get_parser(config_file))
 
1304
        my_config = self.get_branch_config(global_config=sample_config_text)
1143
1305
        self.assertEqual('something',
1144
1306
                         my_config.get_user_option('user_global_option'))
1145
1307
 
1146
1308
    def test_post_commit_default(self):
1147
 
        branch = FakeBranch()
1148
 
        my_config = self.get_branch_config(sample_config_text, '/a/c',
1149
 
                                           sample_branches_text)
 
1309
        my_config = self.get_branch_config(global_config=sample_config_text,
 
1310
                                      location='/a/c',
 
1311
                                      location_config=sample_branches_text)
1150
1312
        self.assertEqual(my_config.branch.base, '/a/c')
1151
1313
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1152
1314
                         my_config.post_commit())
1153
1315
        my_config.set_user_option('post_commit', 'rmtree_root')
1154
 
        # post-commit is ignored when bresent in branch data
 
1316
        # post-commit is ignored when present in branch data
1155
1317
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1156
1318
                         my_config.post_commit())
1157
1319
        my_config.set_user_option('post_commit', 'rmtree_root',
1159
1321
        self.assertEqual('rmtree_root', my_config.post_commit())
1160
1322
 
1161
1323
    def test_config_precedence(self):
 
1324
        # FIXME: eager test, luckily no persitent config file makes it fail
 
1325
        # -- vila 20100716
1162
1326
        my_config = self.get_branch_config(global_config=precedence_global)
1163
1327
        self.assertEqual(my_config.get_user_option('option'), 'global')
1164
1328
        my_config = self.get_branch_config(global_config=precedence_global,
1165
 
                                      branch_data_config=precedence_branch)
 
1329
                                           branch_data_config=precedence_branch)
1166
1330
        self.assertEqual(my_config.get_user_option('option'), 'branch')
1167
 
        my_config = self.get_branch_config(global_config=precedence_global,
1168
 
                                      branch_data_config=precedence_branch,
1169
 
                                      location_config=precedence_location)
 
1331
        my_config = self.get_branch_config(
 
1332
            global_config=precedence_global,
 
1333
            branch_data_config=precedence_branch,
 
1334
            location_config=precedence_location)
1170
1335
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
1171
 
        my_config = self.get_branch_config(global_config=precedence_global,
1172
 
                                      branch_data_config=precedence_branch,
1173
 
                                      location_config=precedence_location,
1174
 
                                      location='http://example.com/specific')
 
1336
        my_config = self.get_branch_config(
 
1337
            global_config=precedence_global,
 
1338
            branch_data_config=precedence_branch,
 
1339
            location_config=precedence_location,
 
1340
            location='http://example.com/specific')
1175
1341
        self.assertEqual(my_config.get_user_option('option'), 'exact')
1176
1342
 
1177
1343
    def test_get_mail_client(self):
1641
1807
        self.assertEquals(entered_password,
1642
1808
                          conf.get_password('ssh', 'bar.org', user='jim'))
1643
1809
        self.assertContainsRe(
1644
 
            self._get_log(keep_log_file=True),
 
1810
            self.get_log(),
1645
1811
            'password ignored in section \[ssh with password\]')
1646
1812
 
1647
1813
    def test_ssh_without_password_doesnt_emit_warning(self):
1666
1832
        # No warning shoud be emitted since there is no password. We are only
1667
1833
        # providing "user".
1668
1834
        self.assertNotContainsRe(
1669
 
            self._get_log(keep_log_file=True),
 
1835
            self.get_log(),
1670
1836
            'password ignored in section \[ssh with password\]')
1671
1837
 
1672
1838
    def test_uses_fallback_stores(self):
1673
 
        self._old_cs_registry = config.credential_store_registry
1674
 
        def restore():
1675
 
            config.credential_store_registry = self._old_cs_registry
1676
 
        self.addCleanup(restore)
1677
 
        config.credential_store_registry = config.CredentialStoreRegistry()
 
1839
        self.overrideAttr(config, 'credential_store_registry',
 
1840
                          config.CredentialStoreRegistry())
1678
1841
        store = StubCredentialStore()
1679
1842
        store.add_credentials("http", "example.com", "joe", "secret")
1680
1843
        config.credential_store_registry.register("stub", store, fallback=True)