529
360
# replace the class that is constructured, to check its parameters
530
361
oldparserclass = config.ConfigObj
531
362
config.ConfigObj = InstrumentedConfigObj
363
my_config = config.LocationConfig('http://www.example.com')
533
my_config = config.LocationConfig('http://www.example.com')
534
365
parser = my_config._get_parser()
536
367
config.ConfigObj = oldparserclass
537
368
self.failUnless(isinstance(parser, InstrumentedConfigObj))
538
369
self.assertEqual(parser._calls,
539
[('__init__', config.locations_config_filename(),
541
config.ensure_config_dir_exists()
542
#os.mkdir(config.config_dir())
543
f = file(config.branches_config_filename(), 'wb')
546
oldparserclass = config.ConfigObj
547
config.ConfigObj = InstrumentedConfigObj
549
my_config = config.LocationConfig('http://www.example.com')
550
parser = my_config._get_parser()
552
config.ConfigObj = oldparserclass
370
[('__init__', config.branches_config_filename())])
554
372
def test_get_global_config(self):
555
my_config = config.BranchConfig(FakeBranch('http://example.com'))
373
my_config = config.LocationConfig('http://example.com')
556
374
global_config = my_config._get_global_config()
557
375
self.failUnless(isinstance(global_config, config.GlobalConfig))
558
376
self.failUnless(global_config is my_config._get_global_config())
560
def test__get_matching_sections_no_match(self):
561
self.get_branch_config('/')
562
self.assertEqual([], self.my_location_config._get_matching_sections())
378
def test__get_section_no_match(self):
379
self.get_location_config('/')
380
self.assertEqual(None, self.my_config._get_section())
564
def test__get_matching_sections_exact(self):
565
self.get_branch_config('http://www.example.com')
566
self.assertEqual([('http://www.example.com', '')],
567
self.my_location_config._get_matching_sections())
382
def test__get_section_exact(self):
383
self.get_location_config('http://www.example.com')
384
self.assertEqual('http://www.example.com',
385
self.my_config._get_section())
569
def test__get_matching_sections_suffix_does_not(self):
570
self.get_branch_config('http://www.example.com-com')
571
self.assertEqual([], self.my_location_config._get_matching_sections())
573
def test__get_matching_sections_subdir_recursive(self):
574
self.get_branch_config('http://www.example.com/com')
575
self.assertEqual([('http://www.example.com', 'com')],
576
self.my_location_config._get_matching_sections())
578
def test__get_matching_sections_ignoreparent(self):
579
self.get_branch_config('http://www.example.com/ignoreparent')
580
self.assertEqual([('http://www.example.com/ignoreparent', '')],
581
self.my_location_config._get_matching_sections())
583
def test__get_matching_sections_ignoreparent_subdir(self):
584
self.get_branch_config(
585
'http://www.example.com/ignoreparent/childbranch')
586
self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
587
self.my_location_config._get_matching_sections())
589
def test__get_matching_sections_subdir_trailing_slash(self):
590
self.get_branch_config('/b')
591
self.assertEqual([('/b/', '')],
592
self.my_location_config._get_matching_sections())
594
def test__get_matching_sections_subdir_child(self):
595
self.get_branch_config('/a/foo')
596
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
597
self.my_location_config._get_matching_sections())
599
def test__get_matching_sections_subdir_child_child(self):
600
self.get_branch_config('/a/foo/bar')
601
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
602
self.my_location_config._get_matching_sections())
604
def test__get_matching_sections_trailing_slash_with_children(self):
605
self.get_branch_config('/a/')
606
self.assertEqual([('/a/', '')],
607
self.my_location_config._get_matching_sections())
609
def test__get_matching_sections_explicit_over_glob(self):
610
# XXX: 2006-09-08 jamesh
611
# This test only passes because ord('c') > ord('*'). If there
612
# was a config section for '/a/?', it would get precedence
614
self.get_branch_config('/a/c')
615
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
616
self.my_location_config._get_matching_sections())
618
def test__get_option_policy_normal(self):
619
self.get_branch_config('http://www.example.com')
621
self.my_location_config._get_config_policy(
622
'http://www.example.com', 'normal_option'),
625
def test__get_option_policy_norecurse(self):
626
self.get_branch_config('http://www.example.com')
628
self.my_location_config._get_option_policy(
629
'http://www.example.com', 'norecurse_option'),
630
config.POLICY_NORECURSE)
631
# Test old recurse=False setting:
633
self.my_location_config._get_option_policy(
634
'http://www.example.com/norecurse', 'normal_option'),
635
config.POLICY_NORECURSE)
637
def test__get_option_policy_normal(self):
638
self.get_branch_config('http://www.example.com')
640
self.my_location_config._get_option_policy(
641
'http://www.example.com', 'appendpath_option'),
642
config.POLICY_APPENDPATH)
387
def test__get_section_suffix_does_not(self):
388
self.get_location_config('http://www.example.com-com')
389
self.assertEqual(None, self.my_config._get_section())
391
def test__get_section_subdir_recursive(self):
392
self.get_location_config('http://www.example.com/com')
393
self.assertEqual('http://www.example.com',
394
self.my_config._get_section())
396
def test__get_section_subdir_matches(self):
397
self.get_location_config('http://www.example.com/useglobal')
398
self.assertEqual('http://www.example.com/useglobal',
399
self.my_config._get_section())
401
def test__get_section_subdir_nonrecursive(self):
402
self.get_location_config(
403
'http://www.example.com/useglobal/childbranch')
404
self.assertEqual('http://www.example.com',
405
self.my_config._get_section())
407
def test__get_section_subdir_trailing_slash(self):
408
self.get_location_config('/b')
409
self.assertEqual('/b/', self.my_config._get_section())
411
def test__get_section_subdir_child(self):
412
self.get_location_config('/a/foo')
413
self.assertEqual('/a/*', self.my_config._get_section())
415
def test__get_section_subdir_child_child(self):
416
self.get_location_config('/a/foo/bar')
417
self.assertEqual('/a/', self.my_config._get_section())
419
def test__get_section_trailing_slash_with_children(self):
420
self.get_location_config('/a/')
421
self.assertEqual('/a/', self.my_config._get_section())
423
def test__get_section_explicit_over_glob(self):
424
self.get_location_config('/a/c')
425
self.assertEqual('/a/c', self.my_config._get_section())
427
def get_location_config(self, location, global_config=None):
428
if global_config is None:
429
global_file = StringIO(sample_config_text)
431
global_file = StringIO(global_config)
432
branches_file = StringIO(sample_branches_text)
433
self.my_config = config.LocationConfig(location)
434
self.my_config._get_parser(branches_file)
435
self.my_config._get_global_config()._get_parser(global_file)
644
437
def test_location_without_username(self):
645
self.get_branch_config('http://www.example.com/ignoreparent')
646
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
438
self.get_location_config('http://www.example.com/useglobal')
439
self.assertEqual('Robert Collins <robertc@example.com>',
647
440
self.my_config.username())
649
442
def test_location_not_listed(self):
650
"""Test that the global username is used when no location matches"""
651
self.get_branch_config('/home/robertc/sources')
652
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
443
self.get_location_config('/home/robertc/sources')
444
self.assertEqual('Robert Collins <robertc@example.com>',
653
445
self.my_config.username())
655
447
def test_overriding_location(self):
656
self.get_branch_config('http://www.example.com/foo')
448
self.get_location_config('http://www.example.com/foo')
657
449
self.assertEqual('Robert Collins <robertc@example.org>',
658
450
self.my_config.username())
660
452
def test_signatures_not_set(self):
661
self.get_branch_config('http://www.example.com',
453
self.get_location_config('http://www.example.com',
662
454
global_config=sample_ignore_signatures)
663
self.assertEqual(config.CHECK_ALWAYS,
455
self.assertEqual(config.CHECK_NEVER,
664
456
self.my_config.signature_checking())
665
self.assertEqual(config.SIGN_NEVER,
666
self.my_config.signing_policy())
668
458
def test_signatures_never(self):
669
self.get_branch_config('/a/c')
459
self.get_location_config('/a/c')
670
460
self.assertEqual(config.CHECK_NEVER,
671
461
self.my_config.signature_checking())
673
463
def test_signatures_when_available(self):
674
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
464
self.get_location_config('/a/', global_config=sample_ignore_signatures)
675
465
self.assertEqual(config.CHECK_IF_POSSIBLE,
676
466
self.my_config.signature_checking())
678
468
def test_signatures_always(self):
679
self.get_branch_config('/b')
469
self.get_location_config('/b')
680
470
self.assertEqual(config.CHECK_ALWAYS,
681
471
self.my_config.signature_checking())
683
473
def test_gpg_signing_command(self):
684
self.get_branch_config('/b')
474
self.get_location_config('/b')
685
475
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
687
477
def test_gpg_signing_command_missing(self):
688
self.get_branch_config('/a')
478
self.get_location_config('/a')
689
479
self.assertEqual("false", self.my_config.gpg_signing_command())
691
481
def test_get_user_option_global(self):
692
self.get_branch_config('/a')
482
self.get_location_config('/a')
693
483
self.assertEqual('something',
694
484
self.my_config.get_user_option('user_global_option'))
696
486
def test_get_user_option_local(self):
697
self.get_branch_config('/a')
487
self.get_location_config('/a')
698
488
self.assertEqual('local',
699
489
self.my_config.get_user_option('user_local_option'))
701
def test_get_user_option_appendpath(self):
702
# returned as is for the base path:
703
self.get_branch_config('http://www.example.com')
704
self.assertEqual('append',
705
self.my_config.get_user_option('appendpath_option'))
706
# Extra path components get appended:
707
self.get_branch_config('http://www.example.com/a/b/c')
708
self.assertEqual('append/a/b/c',
709
self.my_config.get_user_option('appendpath_option'))
710
# Overriden for http://www.example.com/dir, where it is a
712
self.get_branch_config('http://www.example.com/dir/a/b/c')
713
self.assertEqual('normal',
714
self.my_config.get_user_option('appendpath_option'))
716
def test_get_user_option_norecurse(self):
717
self.get_branch_config('http://www.example.com')
718
self.assertEqual('norecurse',
719
self.my_config.get_user_option('norecurse_option'))
720
self.get_branch_config('http://www.example.com/dir')
721
self.assertEqual(None,
722
self.my_config.get_user_option('norecurse_option'))
723
# http://www.example.com/norecurse is a recurse=False section
724
# that redefines normal_option. Subdirectories do not pick up
726
self.get_branch_config('http://www.example.com/norecurse')
727
self.assertEqual('norecurse',
728
self.my_config.get_user_option('normal_option'))
729
self.get_branch_config('http://www.example.com/norecurse/subdir')
730
self.assertEqual('normal',
731
self.my_config.get_user_option('normal_option'))
733
def test_set_user_option_norecurse(self):
734
self.get_branch_config('http://www.example.com')
735
self.my_config.set_user_option('foo', 'bar',
736
store=config.STORE_LOCATION_NORECURSE)
738
self.my_location_config._get_option_policy(
739
'http://www.example.com', 'foo'),
740
config.POLICY_NORECURSE)
742
def test_set_user_option_appendpath(self):
743
self.get_branch_config('http://www.example.com')
744
self.my_config.set_user_option('foo', 'bar',
745
store=config.STORE_LOCATION_APPENDPATH)
747
self.my_location_config._get_option_policy(
748
'http://www.example.com', 'foo'),
749
config.POLICY_APPENDPATH)
751
def test_set_user_option_change_policy(self):
752
self.get_branch_config('http://www.example.com')
753
self.my_config.set_user_option('norecurse_option', 'normal',
754
store=config.STORE_LOCATION)
756
self.my_location_config._get_option_policy(
757
'http://www.example.com', 'norecurse_option'),
760
def test_set_user_option_recurse_false_section(self):
761
# The following section has recurse=False set. The test is to
762
# make sure that a normal option can be added to the section,
763
# converting recurse=False to the norecurse policy.
764
self.get_branch_config('http://www.example.com/norecurse')
765
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
766
'The section "http://www.example.com/norecurse" '
767
'has been converted to use policies.'],
768
self.my_config.set_user_option,
769
'foo', 'bar', store=config.STORE_LOCATION)
771
self.my_location_config._get_option_policy(
772
'http://www.example.com/norecurse', 'foo'),
774
# The previously existing option is still norecurse:
776
self.my_location_config._get_option_policy(
777
'http://www.example.com/norecurse', 'normal_option'),
778
config.POLICY_NORECURSE)
781
491
def test_post_commit_default(self):
782
self.get_branch_config('/a/c')
783
self.assertEqual('bzrlib.tests.test_config.post_commit',
492
self.get_location_config('/a/c')
493
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
784
494
self.my_config.post_commit())
786
def get_branch_config(self, location, global_config=None):
497
class TestLocationConfig(TestCaseInTempDir):
499
def get_location_config(self, location, global_config=None):
787
500
if global_config is None:
788
global_file = StringIO(sample_config_text.encode('utf-8'))
501
global_file = StringIO(sample_config_text)
790
global_file = StringIO(global_config.encode('utf-8'))
791
branches_file = StringIO(sample_branches_text.encode('utf-8'))
792
self.my_config = config.BranchConfig(FakeBranch(location))
793
# Force location config to use specified file
794
self.my_location_config = self.my_config._get_location_config()
795
self.my_location_config._get_parser(branches_file)
796
# Force global config to use specified file
503
global_file = StringIO(global_config)
504
branches_file = StringIO(sample_branches_text)
505
self.my_config = config.LocationConfig(location)
506
self.my_config._get_parser(branches_file)
797
507
self.my_config._get_global_config()._get_parser(global_file)
799
509
def test_set_user_setting_sets_and_saves(self):
800
self.get_branch_config('/a/c')
510
# TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
511
self.get_location_config('/a/c')
801
512
record = InstrumentedConfigObj("foo")
802
self.my_location_config._parser = record
804
real_mkdir = os.mkdir
806
def checked_mkdir(path, mode=0777):
807
self.log('making directory: %s', path)
808
real_mkdir(path, mode)
811
os.mkdir = checked_mkdir
813
self.callDeprecated(['The recurse option is deprecated as of '
814
'0.14. The section "/a/c" has been '
815
'converted to use policies.'],
816
self.my_config.set_user_option,
817
'foo', 'bar', store=config.STORE_LOCATION)
819
os.mkdir = real_mkdir
821
self.failUnless(self.created, 'Failed to create ~/.bazaar')
513
self.my_config._parser = record
515
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
516
# broken: creates .bazaar in the top-level directory, not
517
# inside the test directory
518
self.my_config.set_user_option('foo', 'bar')
822
519
self.assertEqual([('__contains__', '/a/c'),
823
520
('__contains__', '/a/c/'),
824
521
('__setitem__', '/a/c', {}),
825
522
('__getitem__', '/a/c'),
826
523
('__setitem__', 'foo', 'bar'),
827
('__getitem__', '/a/c'),
828
('as_bool', 'recurse'),
829
('__getitem__', '/a/c'),
830
('__delitem__', 'recurse'),
831
('__getitem__', '/a/c'),
833
('__getitem__', '/a/c'),
834
('__contains__', 'foo:policy'),
836
525
record._calls[1:])
838
def test_set_user_setting_sets_and_saves2(self):
839
self.get_branch_config('/a/c')
840
self.assertIs(self.my_config.get_user_option('foo'), None)
841
self.my_config.set_user_option('foo', 'bar')
843
self.my_config.branch.control_files.files['branch.conf'],
845
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
846
self.my_config.set_user_option('foo', 'baz',
847
store=config.STORE_LOCATION)
848
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
849
self.my_config.set_user_option('foo', 'qux')
850
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
853
precedence_global = 'option = global'
854
precedence_branch = 'option = branch'
855
precedence_location = """
859
[http://example.com/specific]
864
class TestBranchConfigItems(TestCaseInTempDir):
866
def get_branch_config(self, global_config=None, location=None,
867
location_config=None, branch_data_config=None):
868
my_config = config.BranchConfig(FakeBranch(location))
869
if global_config is not None:
870
global_file = StringIO(global_config.encode('utf-8'))
871
my_config._get_global_config()._get_parser(global_file)
872
self.my_location_config = my_config._get_location_config()
873
if location_config is not None:
874
location_file = StringIO(location_config.encode('utf-8'))
875
self.my_location_config._get_parser(location_file)
876
if branch_data_config is not None:
877
my_config.branch.control_files.files['branch.conf'] = \
528
class TestBranchConfigItems(TestCase):
881
530
def test_user_id(self):
882
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
531
branch = FakeBranch()
883
532
my_config = config.BranchConfig(branch)
884
533
self.assertEqual("Robert Collins <robertc@example.net>",
885
my_config.username())
886
branch.control_files.email = "John"
887
my_config.set_user_option('email',
888
"Robert Collins <robertc@example.org>")
889
self.assertEqual("John", my_config.username())
890
branch.control_files.email = None
891
self.assertEqual("Robert Collins <robertc@example.org>",
892
my_config.username())
534
my_config._get_user_id())
535
branch.email = "John"
536
self.assertEqual("John", my_config._get_user_id())
894
538
def test_not_set_in_branch(self):
895
my_config = self.get_branch_config(sample_config_text)
896
my_config.branch.control_files.email = None
897
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
539
branch = FakeBranch()
540
my_config = config.BranchConfig(branch)
542
config_file = StringIO(sample_config_text)
543
(my_config._get_location_config().
544
_get_global_config()._get_parser(config_file))
545
self.assertEqual("Robert Collins <robertc@example.com>",
898
546
my_config._get_user_id())
899
my_config.branch.control_files.email = "John"
547
branch.email = "John"
900
548
self.assertEqual("John", my_config._get_user_id())
902
def test_BZR_EMAIL_OVERRIDES(self):
903
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
550
def test_BZREMAIL_OVERRIDES(self):
551
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
904
552
branch = FakeBranch()
905
553
my_config = config.BranchConfig(branch)
906
554
self.assertEqual("Robert Collins <robertc@example.org>",
907
555
my_config.username())
909
557
def test_signatures_forced(self):
910
my_config = self.get_branch_config(
911
global_config=sample_always_signatures)
912
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
913
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
914
self.assertTrue(my_config.signature_needed())
916
def test_signatures_forced_branch(self):
917
my_config = self.get_branch_config(
918
global_config=sample_ignore_signatures,
919
branch_data_config=sample_always_signatures)
920
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
921
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
922
self.assertTrue(my_config.signature_needed())
558
branch = FakeBranch()
559
my_config = config.BranchConfig(branch)
560
config_file = StringIO(sample_always_signatures)
561
(my_config._get_location_config().
562
_get_global_config()._get_parser(config_file))
563
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
924
565
def test_gpg_signing_command(self):
925
my_config = self.get_branch_config(
926
# branch data cannot set gpg_signing_command
927
branch_data_config="gpg_signing_command=pgp")
928
config_file = StringIO(sample_config_text.encode('utf-8'))
929
my_config._get_global_config()._get_parser(config_file)
566
branch = FakeBranch()
567
my_config = config.BranchConfig(branch)
568
config_file = StringIO(sample_config_text)
569
(my_config._get_location_config().
570
_get_global_config()._get_parser(config_file))
930
571
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
932
573
def test_get_user_option_global(self):
933
574
branch = FakeBranch()
934
575
my_config = config.BranchConfig(branch)
935
config_file = StringIO(sample_config_text.encode('utf-8'))
936
(my_config._get_global_config()._get_parser(config_file))
576
config_file = StringIO(sample_config_text)
577
(my_config._get_location_config().
578
_get_global_config()._get_parser(config_file))
937
579
self.assertEqual('something',
938
580
my_config.get_user_option('user_global_option'))
940
582
def test_post_commit_default(self):
941
583
branch = FakeBranch()
942
my_config = self.get_branch_config(sample_config_text, '/a/c',
943
sample_branches_text)
944
self.assertEqual(my_config.branch.base, '/a/c')
945
self.assertEqual('bzrlib.tests.test_config.post_commit',
946
my_config.post_commit())
947
my_config.set_user_option('post_commit', 'rmtree_root')
948
# post-commit is ignored when bresent in branch data
949
self.assertEqual('bzrlib.tests.test_config.post_commit',
950
my_config.post_commit())
951
my_config.set_user_option('post_commit', 'rmtree_root',
952
store=config.STORE_LOCATION)
953
self.assertEqual('rmtree_root', my_config.post_commit())
955
def test_config_precedence(self):
956
my_config = self.get_branch_config(global_config=precedence_global)
957
self.assertEqual(my_config.get_user_option('option'), 'global')
958
my_config = self.get_branch_config(global_config=precedence_global,
959
branch_data_config=precedence_branch)
960
self.assertEqual(my_config.get_user_option('option'), 'branch')
961
my_config = self.get_branch_config(global_config=precedence_global,
962
branch_data_config=precedence_branch,
963
location_config=precedence_location)
964
self.assertEqual(my_config.get_user_option('option'), 'recurse')
965
my_config = self.get_branch_config(global_config=precedence_global,
966
branch_data_config=precedence_branch,
967
location_config=precedence_location,
968
location='http://example.com/specific')
969
self.assertEqual(my_config.get_user_option('option'), 'exact')
585
my_config = config.BranchConfig(branch)
586
config_file = StringIO(sample_config_text)
587
(my_config._get_location_config().
588
_get_global_config()._get_parser(config_file))
589
branch_file = StringIO(sample_branches_text)
590
my_config._get_location_config()._get_parser(branch_file)
591
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
592
my_config.post_commit())
972
595
class TestMailAddressExtraction(TestCase):