314
335
self.assertEqual(branch.base, location_config.location)
315
336
self.failUnless(location_config is my_config._get_location_config())
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')
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')
351
def test_has_explicit_nickname(self):
352
b = self.make_branch('.')
353
self.assertFalse(b.get_config().has_explicit_nickname())
355
self.assertTrue(b.get_config().has_explicit_nickname())
318
358
class TestGlobalConfigItems(TestCase):
452
493
config.ConfigObj = oldparserclass
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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'))
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'))
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())
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'))
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)
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
587
631
real_mkdir = os.mkdir
588
632
self.created = False
607
651
record._calls[1:])
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')
658
self.my_config.branch.control_files.files['branch.conf'],
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')
667
precedence_global = 'option = global'
668
precedence_branch = 'option = branch'
669
precedence_location = """
673
[http://example.com/specific]
678
class TestBranchConfigItems(TestCaseInTempDir):
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'] = \
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())
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())
632
716
def test_BZREMAIL_OVERRIDES(self):
637
721
my_config.username())
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())
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())
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())
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'))
666
754
def test_post_commit_default(self):
667
755
branch = FakeBranch()
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())
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')
679
785
class TestMailAddressExtraction(TestCase):