/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: Robey Pointer
  • Date: 2006-07-01 19:03:33 UTC
  • mfrom: (1829 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1830.
  • Revision ID: robey@lag.net-20060701190333-f58465aec4bd3412
merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#import bzrlib specific imports here
26
26
import bzrlib.config as config
 
27
from bzrlib.branch import Branch
 
28
from bzrlib.bzrdir import BzrDir
27
29
import bzrlib.errors as errors
28
 
from bzrlib.tests import TestCase, TestCaseInTempDir
 
30
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
29
31
 
30
32
 
31
33
sample_long_alias="log -r-15..-1 --line"
102
104
 
103
105
class FakeBranch(object):
104
106
 
105
 
    def __init__(self):
106
 
        self.base = "http://example.com/branches/demo"
107
 
        self.control_files = FakeControlFiles()
 
107
    def __init__(self, base=None, user_id=None):
 
108
        if base is None:
 
109
            self.base = "http://example.com/branches/demo"
 
110
        else:
 
111
            self.base = base
 
112
        self.control_files = FakeControlFiles(user_id=user_id)
 
113
 
 
114
    def lock_write(self):
 
115
        pass
 
116
 
 
117
    def unlock(self):
 
118
        pass
108
119
 
109
120
 
110
121
class FakeControlFiles(object):
111
122
 
112
 
    def __init__(self):
113
 
        self.email = 'Robert Collins <robertc@example.net>\n'
 
123
    def __init__(self, user_id=None):
 
124
        self.email = user_id
 
125
        self.files = {}
114
126
 
115
127
    def get_utf8(self, filename):
116
128
        if filename != 'email':
119
131
            return StringIO(self.email)
120
132
        raise errors.NoSuchFile(filename)
121
133
 
 
134
    def get(self, filename):
 
135
        try:
 
136
            return StringIO(self.files[filename])
 
137
        except KeyError:
 
138
            raise errors.NoSuchFile(filename)
 
139
 
 
140
    def put(self, filename, fileobj):
 
141
        self.files[filename] = fileobj.read()
 
142
 
122
143
 
123
144
class InstrumentedConfig(config.Config):
124
145
    """An instrumented config that supplies stubs for template methods."""
300
321
                                          'utf-8')])
301
322
 
302
323
 
303
 
class TestBranchConfig(TestCaseInTempDir):
 
324
class TestBranchConfig(TestCaseWithTransport):
304
325
 
305
326
    def test_constructs(self):
306
327
        branch = FakeBranch()
314
335
        self.assertEqual(branch.base, location_config.location)
315
336
        self.failUnless(location_config is my_config._get_location_config())
316
337
 
 
338
    def test_get_config(self):
 
339
        """The Branch.get_config method works properly"""
 
340
        b = BzrDir.create_standalone_workingtree('.').branch
 
341
        my_config = b.get_config()
 
342
        self.assertIs(my_config.get_user_option('wacky'), None)
 
343
        my_config.set_user_option('wacky', 'unlikely')
 
344
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
 
345
 
 
346
        # Ensure we get the same thing if we start again
 
347
        b2 = Branch.open('.')
 
348
        my_config2 = b2.get_config()
 
349
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
 
350
 
 
351
    def test_has_explicit_nickname(self):
 
352
        b = self.make_branch('.')
 
353
        self.assertFalse(b.get_config().has_explicit_nickname())
 
354
        b.nick = 'foo'
 
355
        self.assertTrue(b.get_config().has_explicit_nickname())
 
356
 
317
357
 
318
358
class TestGlobalConfigItems(TestCase):
319
359
 
439
479
        self.assertEqual(parser._calls,
440
480
                         [('__init__', config.locations_config_filename(),
441
481
                           'utf-8')])
442
 
        os.mkdir(config.config_dir())
 
482
        config.ensure_config_dir_exists()
 
483
        #os.mkdir(config.config_dir())
443
484
        f = file(config.branches_config_filename(), 'wb')
444
485
        f.write('')
445
486
        f.close()
452
493
            config.ConfigObj = oldparserclass
453
494
 
454
495
    def test_get_global_config(self):
455
 
        my_config = config.LocationConfig('http://example.com')
 
496
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
456
497
        global_config = my_config._get_global_config()
457
498
        self.failUnless(isinstance(global_config, config.GlobalConfig))
458
499
        self.failUnless(global_config is my_config._get_global_config())
459
500
 
460
501
    def test__get_section_no_match(self):
461
 
        self.get_location_config('/')
462
 
        self.assertEqual(None, self.my_config._get_section())
 
502
        self.get_branch_config('/')
 
503
        self.assertEqual(None, self.my_location_config._get_section())
463
504
        
464
505
    def test__get_section_exact(self):
465
 
        self.get_location_config('http://www.example.com')
 
506
        self.get_branch_config('http://www.example.com')
466
507
        self.assertEqual('http://www.example.com',
467
 
                         self.my_config._get_section())
 
508
                         self.my_location_config._get_section())
468
509
   
469
510
    def test__get_section_suffix_does_not(self):
470
 
        self.get_location_config('http://www.example.com-com')
471
 
        self.assertEqual(None, self.my_config._get_section())
 
511
        self.get_branch_config('http://www.example.com-com')
 
512
        self.assertEqual(None, self.my_location_config._get_section())
472
513
 
473
514
    def test__get_section_subdir_recursive(self):
474
 
        self.get_location_config('http://www.example.com/com')
 
515
        self.get_branch_config('http://www.example.com/com')
475
516
        self.assertEqual('http://www.example.com',
476
 
                         self.my_config._get_section())
 
517
                         self.my_location_config._get_section())
477
518
 
478
519
    def test__get_section_subdir_matches(self):
479
 
        self.get_location_config('http://www.example.com/useglobal')
 
520
        self.get_branch_config('http://www.example.com/useglobal')
480
521
        self.assertEqual('http://www.example.com/useglobal',
481
 
                         self.my_config._get_section())
 
522
                         self.my_location_config._get_section())
482
523
 
483
524
    def test__get_section_subdir_nonrecursive(self):
484
 
        self.get_location_config(
 
525
        self.get_branch_config(
485
526
            'http://www.example.com/useglobal/childbranch')
486
527
        self.assertEqual('http://www.example.com',
487
 
                         self.my_config._get_section())
 
528
                         self.my_location_config._get_section())
488
529
 
489
530
    def test__get_section_subdir_trailing_slash(self):
490
 
        self.get_location_config('/b')
491
 
        self.assertEqual('/b/', self.my_config._get_section())
 
531
        self.get_branch_config('/b')
 
532
        self.assertEqual('/b/', self.my_location_config._get_section())
492
533
 
493
534
    def test__get_section_subdir_child(self):
494
 
        self.get_location_config('/a/foo')
495
 
        self.assertEqual('/a/*', self.my_config._get_section())
 
535
        self.get_branch_config('/a/foo')
 
536
        self.assertEqual('/a/*', self.my_location_config._get_section())
496
537
 
497
538
    def test__get_section_subdir_child_child(self):
498
 
        self.get_location_config('/a/foo/bar')
499
 
        self.assertEqual('/a/', self.my_config._get_section())
 
539
        self.get_branch_config('/a/foo/bar')
 
540
        self.assertEqual('/a/', self.my_location_config._get_section())
500
541
 
501
542
    def test__get_section_trailing_slash_with_children(self):
502
 
        self.get_location_config('/a/')
503
 
        self.assertEqual('/a/', self.my_config._get_section())
 
543
        self.get_branch_config('/a/')
 
544
        self.assertEqual('/a/', self.my_location_config._get_section())
504
545
 
505
546
    def test__get_section_explicit_over_glob(self):
506
 
        self.get_location_config('/a/c')
507
 
        self.assertEqual('/a/c', self.my_config._get_section())
 
547
        self.get_branch_config('/a/c')
 
548
        self.assertEqual('/a/c', self.my_location_config._get_section())
508
549
 
509
550
 
510
551
    def test_location_without_username(self):
511
 
        self.get_location_config('http://www.example.com/useglobal')
 
552
        self.get_branch_config('http://www.example.com/useglobal')
512
553
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
513
554
                         self.my_config.username())
514
555
 
515
556
    def test_location_not_listed(self):
516
557
        """Test that the global username is used when no location matches"""
517
 
        self.get_location_config('/home/robertc/sources')
 
558
        self.get_branch_config('/home/robertc/sources')
518
559
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
519
560
                         self.my_config.username())
520
561
 
521
562
    def test_overriding_location(self):
522
 
        self.get_location_config('http://www.example.com/foo')
 
563
        self.get_branch_config('http://www.example.com/foo')
523
564
        self.assertEqual('Robert Collins <robertc@example.org>',
524
565
                         self.my_config.username())
525
566
 
526
567
    def test_signatures_not_set(self):
527
 
        self.get_location_config('http://www.example.com',
 
568
        self.get_branch_config('http://www.example.com',
528
569
                                 global_config=sample_ignore_signatures)
529
570
        self.assertEqual(config.CHECK_ALWAYS,
530
571
                         self.my_config.signature_checking())
532
573
                         self.my_config.signing_policy())
533
574
 
534
575
    def test_signatures_never(self):
535
 
        self.get_location_config('/a/c')
 
576
        self.get_branch_config('/a/c')
536
577
        self.assertEqual(config.CHECK_NEVER,
537
578
                         self.my_config.signature_checking())
538
579
        
539
580
    def test_signatures_when_available(self):
540
 
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
 
581
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
541
582
        self.assertEqual(config.CHECK_IF_POSSIBLE,
542
583
                         self.my_config.signature_checking())
543
584
        
544
585
    def test_signatures_always(self):
545
 
        self.get_location_config('/b')
 
586
        self.get_branch_config('/b')
546
587
        self.assertEqual(config.CHECK_ALWAYS,
547
588
                         self.my_config.signature_checking())
548
589
        
549
590
    def test_gpg_signing_command(self):
550
 
        self.get_location_config('/b')
 
591
        self.get_branch_config('/b')
551
592
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
552
593
 
553
594
    def test_gpg_signing_command_missing(self):
554
 
        self.get_location_config('/a')
 
595
        self.get_branch_config('/a')
555
596
        self.assertEqual("false", self.my_config.gpg_signing_command())
556
597
 
557
598
    def test_get_user_option_global(self):
558
 
        self.get_location_config('/a')
 
599
        self.get_branch_config('/a')
559
600
        self.assertEqual('something',
560
601
                         self.my_config.get_user_option('user_global_option'))
561
602
 
562
603
    def test_get_user_option_local(self):
563
 
        self.get_location_config('/a')
 
604
        self.get_branch_config('/a')
564
605
        self.assertEqual('local',
565
606
                         self.my_config.get_user_option('user_local_option'))
566
607
        
567
608
    def test_post_commit_default(self):
568
 
        self.get_location_config('/a/c')
 
609
        self.get_branch_config('/a/c')
569
610
        self.assertEqual('bzrlib.tests.test_config.post_commit',
570
611
                         self.my_config.post_commit())
571
612
 
572
 
    def get_location_config(self, location, global_config=None):
 
613
    def get_branch_config(self, location, global_config=None):
573
614
        if global_config is None:
574
615
            global_file = StringIO(sample_config_text.encode('utf-8'))
575
616
        else:
576
617
            global_file = StringIO(global_config.encode('utf-8'))
577
618
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
578
 
        self.my_config = config.LocationConfig(location)
579
 
        self.my_config._get_parser(branches_file)
 
619
        self.my_config = config.BranchConfig(FakeBranch(location))
 
620
        # Force location config to use specified file
 
621
        self.my_location_config = self.my_config._get_location_config()
 
622
        self.my_location_config._get_parser(branches_file)
 
623
        # Force global config to use specified file
580
624
        self.my_config._get_global_config()._get_parser(global_file)
581
625
 
582
626
    def test_set_user_setting_sets_and_saves(self):
583
 
        self.get_location_config('/a/c')
 
627
        self.get_branch_config('/a/c')
584
628
        record = InstrumentedConfigObj("foo")
585
 
        self.my_config._parser = record
 
629
        self.my_location_config._parser = record
586
630
 
587
631
        real_mkdir = os.mkdir
588
632
        self.created = False
593
637
 
594
638
        os.mkdir = checked_mkdir
595
639
        try:
596
 
            self.my_config.set_user_option('foo', 'bar')
 
640
            self.my_config.set_user_option('foo', 'bar', local=True)
597
641
        finally:
598
642
            os.mkdir = real_mkdir
599
643
 
606
650
                          ('write',)],
607
651
                         record._calls[1:])
608
652
 
609
 
 
610
 
class TestBranchConfigItems(TestCase):
 
653
    def test_set_user_setting_sets_and_saves2(self):
 
654
        self.get_branch_config('/a/c')
 
655
        self.assertIs(self.my_config.get_user_option('foo'), None)
 
656
        self.my_config.set_user_option('foo', 'bar')
 
657
        self.assertEqual(
 
658
            self.my_config.branch.control_files.files['branch.conf'], 
 
659
            'foo = bar')
 
660
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
 
661
        self.my_config.set_user_option('foo', 'baz', local=True)
 
662
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
 
663
        self.my_config.set_user_option('foo', 'qux')
 
664
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
 
665
        
 
666
 
 
667
precedence_global = 'option = global'
 
668
precedence_branch = 'option = branch'
 
669
precedence_location = """
 
670
[http://]
 
671
recurse = true
 
672
option = recurse
 
673
[http://example.com/specific]
 
674
option = exact
 
675
"""
 
676
 
 
677
 
 
678
class TestBranchConfigItems(TestCaseInTempDir):
 
679
 
 
680
    def get_branch_config(self, global_config=None, location=None, 
 
681
                          location_config=None, branch_data_config=None):
 
682
        my_config = config.BranchConfig(FakeBranch(location))
 
683
        if global_config is not None:
 
684
            global_file = StringIO(global_config.encode('utf-8'))
 
685
            my_config._get_global_config()._get_parser(global_file)
 
686
        self.my_location_config = my_config._get_location_config()
 
687
        if location_config is not None:
 
688
            location_file = StringIO(location_config.encode('utf-8'))
 
689
            self.my_location_config._get_parser(location_file)
 
690
        if branch_data_config is not None:
 
691
            my_config.branch.control_files.files['branch.conf'] = \
 
692
                branch_data_config
 
693
        return my_config
611
694
 
612
695
    def test_user_id(self):
613
 
        branch = FakeBranch()
 
696
        branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
614
697
        my_config = config.BranchConfig(branch)
615
698
        self.assertEqual("Robert Collins <robertc@example.net>",
616
 
                         my_config._get_user_id())
 
699
                         my_config.username())
617
700
        branch.control_files.email = "John"
618
 
        self.assertEqual("John", my_config._get_user_id())
 
701
        my_config.set_user_option('email', 
 
702
                                  "Robert Collins <robertc@example.org>")
 
703
        self.assertEqual("John", my_config.username())
 
704
        branch.control_files.email = None
 
705
        self.assertEqual("Robert Collins <robertc@example.org>",
 
706
                         my_config.username())
619
707
 
620
708
    def test_not_set_in_branch(self):
621
 
        branch = FakeBranch()
622
 
        my_config = config.BranchConfig(branch)
623
 
        branch.control_files.email = None
624
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
625
 
        (my_config._get_location_config().
626
 
            _get_global_config()._get_parser(config_file))
 
709
        my_config = self.get_branch_config(sample_config_text)
 
710
        my_config.branch.control_files.email = None
627
711
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
628
712
                         my_config._get_user_id())
629
 
        branch.control_files.email = "John"
 
713
        my_config.branch.control_files.email = "John"
630
714
        self.assertEqual("John", my_config._get_user_id())
631
715
 
632
716
    def test_BZREMAIL_OVERRIDES(self):
637
721
                         my_config.username())
638
722
    
639
723
    def test_signatures_forced(self):
640
 
        branch = FakeBranch()
641
 
        my_config = config.BranchConfig(branch)
642
 
        config_file = StringIO(sample_always_signatures)
643
 
        (my_config._get_location_config().
644
 
            _get_global_config()._get_parser(config_file))
 
724
        my_config = self.get_branch_config(
 
725
            global_config=sample_always_signatures)
 
726
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
 
727
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
 
728
        self.assertTrue(my_config.signature_needed())
 
729
 
 
730
    def test_signatures_forced_branch(self):
 
731
        my_config = self.get_branch_config(
 
732
            global_config=sample_ignore_signatures,
 
733
            branch_data_config=sample_always_signatures)
645
734
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
646
735
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
647
736
        self.assertTrue(my_config.signature_needed())
648
737
 
649
738
    def test_gpg_signing_command(self):
650
 
        branch = FakeBranch()
651
 
        my_config = config.BranchConfig(branch)
 
739
        my_config = self.get_branch_config(
 
740
            # branch data cannot set gpg_signing_command
 
741
            branch_data_config="gpg_signing_command=pgp")
652
742
        config_file = StringIO(sample_config_text.encode('utf-8'))
653
 
        (my_config._get_location_config().
654
 
            _get_global_config()._get_parser(config_file))
 
743
        my_config._get_global_config()._get_parser(config_file)
655
744
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
656
745
 
657
746
    def test_get_user_option_global(self):
658
747
        branch = FakeBranch()
659
748
        my_config = config.BranchConfig(branch)
660
749
        config_file = StringIO(sample_config_text.encode('utf-8'))
661
 
        (my_config._get_location_config().
662
 
            _get_global_config()._get_parser(config_file))
 
750
        (my_config._get_global_config()._get_parser(config_file))
663
751
        self.assertEqual('something',
664
752
                         my_config.get_user_option('user_global_option'))
665
753
 
666
754
    def test_post_commit_default(self):
667
755
        branch = FakeBranch()
668
 
        branch.base='/a/c'
669
 
        my_config = config.BranchConfig(branch)
670
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
671
 
        (my_config._get_location_config().
672
 
            _get_global_config()._get_parser(config_file))
673
 
        branch_file = StringIO(sample_branches_text)
674
 
        my_config._get_location_config()._get_parser(branch_file)
675
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
676
 
                         my_config.post_commit())
 
756
        my_config = self.get_branch_config(sample_config_text, '/a/c',
 
757
                                           sample_branches_text)
 
758
        self.assertEqual(my_config.branch.base, '/a/c')
 
759
        self.assertEqual('bzrlib.tests.test_config.post_commit',
 
760
                         my_config.post_commit())
 
761
        my_config.set_user_option('post_commit', 'rmtree_root')
 
762
        # post-commit is ignored when bresent in branch data
 
763
        self.assertEqual('bzrlib.tests.test_config.post_commit',
 
764
                         my_config.post_commit())
 
765
        my_config.set_user_option('post_commit', 'rmtree_root', local=True)
 
766
        self.assertEqual('rmtree_root', my_config.post_commit())
 
767
 
 
768
    def test_config_precedence(self):
 
769
        my_config = self.get_branch_config(global_config=precedence_global)
 
770
        self.assertEqual(my_config.get_user_option('option'), 'global')
 
771
        my_config = self.get_branch_config(global_config=precedence_global, 
 
772
                                      branch_data_config=precedence_branch)
 
773
        self.assertEqual(my_config.get_user_option('option'), 'branch')
 
774
        my_config = self.get_branch_config(global_config=precedence_global, 
 
775
                                      branch_data_config=precedence_branch,
 
776
                                      location_config=precedence_location)
 
777
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
 
778
        my_config = self.get_branch_config(global_config=precedence_global, 
 
779
                                      branch_data_config=precedence_branch,
 
780
                                      location_config=precedence_location,
 
781
                                      location='http://example.com/specific')
 
782
        self.assertEqual(my_config.get_user_option('option'), 'exact')
677
783
 
678
784
 
679
785
class TestMailAddressExtraction(TestCase):