406
637
# This is testing the correct file names are provided.
407
638
# TODO: consolidate with the test for GlobalConfigs filename checks.
409
# replace the class that is constructured, to check its parameters
640
# replace the class that is constructed, to check its parameters
410
641
oldparserclass = config.ConfigObj
411
642
config.ConfigObj = InstrumentedConfigObj
412
my_config = config.LocationConfig('http://www.example.com')
644
my_config = config.LocationConfig('http://www.example.com')
414
645
parser = my_config._get_parser()
416
647
config.ConfigObj = oldparserclass
417
648
self.failUnless(isinstance(parser, InstrumentedConfigObj))
418
649
self.assertEqual(parser._calls,
419
[('__init__', config.branches_config_filename())])
650
[('__init__', config.locations_config_filename(),
652
config.ensure_config_dir_exists()
653
#os.mkdir(config.config_dir())
654
f = file(config.branches_config_filename(), 'wb')
657
oldparserclass = config.ConfigObj
658
config.ConfigObj = InstrumentedConfigObj
660
my_config = config.LocationConfig('http://www.example.com')
661
parser = my_config._get_parser()
663
config.ConfigObj = oldparserclass
421
665
def test_get_global_config(self):
422
my_config = config.LocationConfig('http://example.com')
666
my_config = config.BranchConfig(FakeBranch('http://example.com'))
423
667
global_config = my_config._get_global_config()
424
668
self.failUnless(isinstance(global_config, config.GlobalConfig))
425
669
self.failUnless(global_config is my_config._get_global_config())
427
def test__get_section_no_match(self):
428
self.get_location_config('/')
429
self.assertEqual(None, self.my_config._get_section())
431
def test__get_section_exact(self):
432
self.get_location_config('http://www.example.com')
433
self.assertEqual('http://www.example.com',
434
self.my_config._get_section())
436
def test__get_section_suffix_does_not(self):
437
self.get_location_config('http://www.example.com-com')
438
self.assertEqual(None, self.my_config._get_section())
440
def test__get_section_subdir_recursive(self):
441
self.get_location_config('http://www.example.com/com')
442
self.assertEqual('http://www.example.com',
443
self.my_config._get_section())
445
def test__get_section_subdir_matches(self):
446
self.get_location_config('http://www.example.com/useglobal')
447
self.assertEqual('http://www.example.com/useglobal',
448
self.my_config._get_section())
450
def test__get_section_subdir_nonrecursive(self):
451
self.get_location_config(
452
'http://www.example.com/useglobal/childbranch')
453
self.assertEqual('http://www.example.com',
454
self.my_config._get_section())
456
def test__get_section_subdir_trailing_slash(self):
457
self.get_location_config('/b')
458
self.assertEqual('/b/', self.my_config._get_section())
460
def test__get_section_subdir_child(self):
461
self.get_location_config('/a/foo')
462
self.assertEqual('/a/*', self.my_config._get_section())
464
def test__get_section_subdir_child_child(self):
465
self.get_location_config('/a/foo/bar')
466
self.assertEqual('/a/', self.my_config._get_section())
468
def test__get_section_trailing_slash_with_children(self):
469
self.get_location_config('/a/')
470
self.assertEqual('/a/', self.my_config._get_section())
472
def test__get_section_explicit_over_glob(self):
473
self.get_location_config('/a/c')
474
self.assertEqual('/a/c', self.my_config._get_section())
476
def get_location_config(self, location, global_config=None):
477
if global_config is None:
478
global_file = StringIO(sample_config_text)
480
global_file = StringIO(global_config)
481
branches_file = StringIO(sample_branches_text)
482
self.my_config = config.LocationConfig(location)
483
self.my_config._get_parser(branches_file)
484
self.my_config._get_global_config()._get_parser(global_file)
671
def test__get_matching_sections_no_match(self):
672
self.get_branch_config('/')
673
self.assertEqual([], self.my_location_config._get_matching_sections())
675
def test__get_matching_sections_exact(self):
676
self.get_branch_config('http://www.example.com')
677
self.assertEqual([('http://www.example.com', '')],
678
self.my_location_config._get_matching_sections())
680
def test__get_matching_sections_suffix_does_not(self):
681
self.get_branch_config('http://www.example.com-com')
682
self.assertEqual([], self.my_location_config._get_matching_sections())
684
def test__get_matching_sections_subdir_recursive(self):
685
self.get_branch_config('http://www.example.com/com')
686
self.assertEqual([('http://www.example.com', 'com')],
687
self.my_location_config._get_matching_sections())
689
def test__get_matching_sections_ignoreparent(self):
690
self.get_branch_config('http://www.example.com/ignoreparent')
691
self.assertEqual([('http://www.example.com/ignoreparent', '')],
692
self.my_location_config._get_matching_sections())
694
def test__get_matching_sections_ignoreparent_subdir(self):
695
self.get_branch_config(
696
'http://www.example.com/ignoreparent/childbranch')
697
self.assertEqual([('http://www.example.com/ignoreparent',
699
self.my_location_config._get_matching_sections())
701
def test__get_matching_sections_subdir_trailing_slash(self):
702
self.get_branch_config('/b')
703
self.assertEqual([('/b/', '')],
704
self.my_location_config._get_matching_sections())
706
def test__get_matching_sections_subdir_child(self):
707
self.get_branch_config('/a/foo')
708
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
709
self.my_location_config._get_matching_sections())
711
def test__get_matching_sections_subdir_child_child(self):
712
self.get_branch_config('/a/foo/bar')
713
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
714
self.my_location_config._get_matching_sections())
716
def test__get_matching_sections_trailing_slash_with_children(self):
717
self.get_branch_config('/a/')
718
self.assertEqual([('/a/', '')],
719
self.my_location_config._get_matching_sections())
721
def test__get_matching_sections_explicit_over_glob(self):
722
# XXX: 2006-09-08 jamesh
723
# This test only passes because ord('c') > ord('*'). If there
724
# was a config section for '/a/?', it would get precedence
726
self.get_branch_config('/a/c')
727
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
728
self.my_location_config._get_matching_sections())
730
def test__get_option_policy_normal(self):
731
self.get_branch_config('http://www.example.com')
733
self.my_location_config._get_config_policy(
734
'http://www.example.com', 'normal_option'),
737
def test__get_option_policy_norecurse(self):
738
self.get_branch_config('http://www.example.com')
740
self.my_location_config._get_option_policy(
741
'http://www.example.com', 'norecurse_option'),
742
config.POLICY_NORECURSE)
743
# Test old recurse=False setting:
745
self.my_location_config._get_option_policy(
746
'http://www.example.com/norecurse', 'normal_option'),
747
config.POLICY_NORECURSE)
749
def test__get_option_policy_normal(self):
750
self.get_branch_config('http://www.example.com')
752
self.my_location_config._get_option_policy(
753
'http://www.example.com', 'appendpath_option'),
754
config.POLICY_APPENDPATH)
486
756
def test_location_without_username(self):
487
self.get_location_config('http://www.example.com/useglobal')
488
self.assertEqual('Robert Collins <robertc@example.com>',
757
self.get_branch_config('http://www.example.com/ignoreparent')
758
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
489
759
self.my_config.username())
491
761
def test_location_not_listed(self):
492
self.get_location_config('/home/robertc/sources')
493
self.assertEqual('Robert Collins <robertc@example.com>',
762
"""Test that the global username is used when no location matches"""
763
self.get_branch_config('/home/robertc/sources')
764
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
494
765
self.my_config.username())
496
767
def test_overriding_location(self):
497
self.get_location_config('http://www.example.com/foo')
768
self.get_branch_config('http://www.example.com/foo')
498
769
self.assertEqual('Robert Collins <robertc@example.org>',
499
770
self.my_config.username())
501
772
def test_signatures_not_set(self):
502
self.get_location_config('http://www.example.com',
773
self.get_branch_config('http://www.example.com',
503
774
global_config=sample_ignore_signatures)
504
self.assertEqual(config.CHECK_NEVER,
775
self.assertEqual(config.CHECK_ALWAYS,
505
776
self.my_config.signature_checking())
777
self.assertEqual(config.SIGN_NEVER,
778
self.my_config.signing_policy())
507
780
def test_signatures_never(self):
508
self.get_location_config('/a/c')
781
self.get_branch_config('/a/c')
509
782
self.assertEqual(config.CHECK_NEVER,
510
783
self.my_config.signature_checking())
512
785
def test_signatures_when_available(self):
513
self.get_location_config('/a/', global_config=sample_ignore_signatures)
786
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
514
787
self.assertEqual(config.CHECK_IF_POSSIBLE,
515
788
self.my_config.signature_checking())
517
790
def test_signatures_always(self):
518
self.get_location_config('/b')
791
self.get_branch_config('/b')
519
792
self.assertEqual(config.CHECK_ALWAYS,
520
793
self.my_config.signature_checking())
522
795
def test_gpg_signing_command(self):
523
self.get_location_config('/b')
796
self.get_branch_config('/b')
524
797
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
526
799
def test_gpg_signing_command_missing(self):
527
self.get_location_config('/a')
800
self.get_branch_config('/a')
528
801
self.assertEqual("false", self.my_config.gpg_signing_command())
530
803
def test_get_user_option_global(self):
531
self.get_location_config('/a')
804
self.get_branch_config('/a')
532
805
self.assertEqual('something',
533
806
self.my_config.get_user_option('user_global_option'))
535
808
def test_get_user_option_local(self):
536
self.get_location_config('/a')
809
self.get_branch_config('/a')
537
810
self.assertEqual('local',
538
811
self.my_config.get_user_option('user_local_option'))
813
def test_get_user_option_appendpath(self):
814
# returned as is for the base path:
815
self.get_branch_config('http://www.example.com')
816
self.assertEqual('append',
817
self.my_config.get_user_option('appendpath_option'))
818
# Extra path components get appended:
819
self.get_branch_config('http://www.example.com/a/b/c')
820
self.assertEqual('append/a/b/c',
821
self.my_config.get_user_option('appendpath_option'))
822
# Overriden for http://www.example.com/dir, where it is a
824
self.get_branch_config('http://www.example.com/dir/a/b/c')
825
self.assertEqual('normal',
826
self.my_config.get_user_option('appendpath_option'))
828
def test_get_user_option_norecurse(self):
829
self.get_branch_config('http://www.example.com')
830
self.assertEqual('norecurse',
831
self.my_config.get_user_option('norecurse_option'))
832
self.get_branch_config('http://www.example.com/dir')
833
self.assertEqual(None,
834
self.my_config.get_user_option('norecurse_option'))
835
# http://www.example.com/norecurse is a recurse=False section
836
# that redefines normal_option. Subdirectories do not pick up
838
self.get_branch_config('http://www.example.com/norecurse')
839
self.assertEqual('norecurse',
840
self.my_config.get_user_option('normal_option'))
841
self.get_branch_config('http://www.example.com/norecurse/subdir')
842
self.assertEqual('normal',
843
self.my_config.get_user_option('normal_option'))
845
def test_set_user_option_norecurse(self):
846
self.get_branch_config('http://www.example.com')
847
self.my_config.set_user_option('foo', 'bar',
848
store=config.STORE_LOCATION_NORECURSE)
850
self.my_location_config._get_option_policy(
851
'http://www.example.com', 'foo'),
852
config.POLICY_NORECURSE)
854
def test_set_user_option_appendpath(self):
855
self.get_branch_config('http://www.example.com')
856
self.my_config.set_user_option('foo', 'bar',
857
store=config.STORE_LOCATION_APPENDPATH)
859
self.my_location_config._get_option_policy(
860
'http://www.example.com', 'foo'),
861
config.POLICY_APPENDPATH)
863
def test_set_user_option_change_policy(self):
864
self.get_branch_config('http://www.example.com')
865
self.my_config.set_user_option('norecurse_option', 'normal',
866
store=config.STORE_LOCATION)
868
self.my_location_config._get_option_policy(
869
'http://www.example.com', 'norecurse_option'),
872
def test_set_user_option_recurse_false_section(self):
873
# The following section has recurse=False set. The test is to
874
# make sure that a normal option can be added to the section,
875
# converting recurse=False to the norecurse policy.
876
self.get_branch_config('http://www.example.com/norecurse')
877
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
878
'The section "http://www.example.com/norecurse" '
879
'has been converted to use policies.'],
880
self.my_config.set_user_option,
881
'foo', 'bar', store=config.STORE_LOCATION)
883
self.my_location_config._get_option_policy(
884
'http://www.example.com/norecurse', 'foo'),
886
# The previously existing option is still norecurse:
888
self.my_location_config._get_option_policy(
889
'http://www.example.com/norecurse', 'normal_option'),
890
config.POLICY_NORECURSE)
540
892
def test_post_commit_default(self):
541
self.get_location_config('/a/c')
893
self.get_branch_config('/a/c')
542
894
self.assertEqual('bzrlib.tests.test_config.post_commit',
543
895
self.my_config.post_commit())
546
class TestLocationConfig(TestCaseInTempDir):
548
def get_location_config(self, location, global_config=None):
897
def get_branch_config(self, location, global_config=None):
549
898
if global_config is None:
550
899
global_file = StringIO(sample_config_text.encode('utf-8'))
552
901
global_file = StringIO(global_config.encode('utf-8'))
553
902
branches_file = StringIO(sample_branches_text.encode('utf-8'))
554
self.my_config = config.LocationConfig(location)
555
self.my_config._get_parser(branches_file)
903
self.my_config = config.BranchConfig(FakeBranch(location))
904
# Force location config to use specified file
905
self.my_location_config = self.my_config._get_location_config()
906
self.my_location_config._get_parser(branches_file)
907
# Force global config to use specified file
556
908
self.my_config._get_global_config()._get_parser(global_file)
558
910
def test_set_user_setting_sets_and_saves(self):
559
self.get_location_config('/a/c')
911
self.get_branch_config('/a/c')
560
912
record = InstrumentedConfigObj("foo")
561
self.my_config._parser = record
913
self.my_location_config._parser = record
563
915
real_mkdir = os.mkdir
564
916
self.created = False
579
935
('__setitem__', '/a/c', {}),
580
936
('__getitem__', '/a/c'),
581
937
('__setitem__', 'foo', 'bar'),
938
('__getitem__', '/a/c'),
939
('as_bool', 'recurse'),
940
('__getitem__', '/a/c'),
941
('__delitem__', 'recurse'),
942
('__getitem__', '/a/c'),
944
('__getitem__', '/a/c'),
945
('__contains__', 'foo:policy'),
583
947
record._calls[1:])
586
class TestBranchConfigItems(TestCase):
949
def test_set_user_setting_sets_and_saves2(self):
950
self.get_branch_config('/a/c')
951
self.assertIs(self.my_config.get_user_option('foo'), None)
952
self.my_config.set_user_option('foo', 'bar')
954
self.my_config.branch.control_files.files['branch.conf'],
956
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
957
self.my_config.set_user_option('foo', 'baz',
958
store=config.STORE_LOCATION)
959
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
960
self.my_config.set_user_option('foo', 'qux')
961
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
963
def test_get_bzr_remote_path(self):
964
my_config = config.LocationConfig('/a/c')
965
self.assertEqual('bzr', my_config.get_bzr_remote_path())
966
my_config.set_user_option('bzr_remote_path', '/path-bzr')
967
self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
968
os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
969
self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
972
precedence_global = 'option = global'
973
precedence_branch = 'option = branch'
974
precedence_location = """
978
[http://example.com/specific]
983
class TestBranchConfigItems(tests.TestCaseInTempDir):
985
def get_branch_config(self, global_config=None, location=None,
986
location_config=None, branch_data_config=None):
987
my_config = config.BranchConfig(FakeBranch(location))
988
if global_config is not None:
989
global_file = StringIO(global_config.encode('utf-8'))
990
my_config._get_global_config()._get_parser(global_file)
991
self.my_location_config = my_config._get_location_config()
992
if location_config is not None:
993
location_file = StringIO(location_config.encode('utf-8'))
994
self.my_location_config._get_parser(location_file)
995
if branch_data_config is not None:
996
my_config.branch.control_files.files['branch.conf'] = \
588
1000
def test_user_id(self):
589
branch = FakeBranch()
1001
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
590
1002
my_config = config.BranchConfig(branch)
591
1003
self.assertEqual("Robert Collins <robertc@example.net>",
592
my_config._get_user_id())
593
branch.control_files.email = "John"
594
self.assertEqual("John", my_config._get_user_id())
1004
my_config.username())
1005
my_config.branch.control_files.files['email'] = "John"
1006
my_config.set_user_option('email',
1007
"Robert Collins <robertc@example.org>")
1008
self.assertEqual("John", my_config.username())
1009
del my_config.branch.control_files.files['email']
1010
self.assertEqual("Robert Collins <robertc@example.org>",
1011
my_config.username())
596
1013
def test_not_set_in_branch(self):
597
branch = FakeBranch()
598
my_config = config.BranchConfig(branch)
599
branch.control_files.email = None
600
config_file = StringIO(sample_config_text.encode('utf-8'))
601
(my_config._get_location_config().
602
_get_global_config()._get_parser(config_file))
1014
my_config = self.get_branch_config(sample_config_text)
603
1015
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
604
1016
my_config._get_user_id())
605
branch.control_files.email = "John"
1017
my_config.branch.control_files.files['email'] = "John"
606
1018
self.assertEqual("John", my_config._get_user_id())
608
def test_BZREMAIL_OVERRIDES(self):
609
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
1020
def test_BZR_EMAIL_OVERRIDES(self):
1021
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
610
1022
branch = FakeBranch()
611
1023
my_config = config.BranchConfig(branch)
612
1024
self.assertEqual("Robert Collins <robertc@example.org>",
613
1025
my_config.username())
615
1027
def test_signatures_forced(self):
616
branch = FakeBranch()
617
my_config = config.BranchConfig(branch)
618
config_file = StringIO(sample_always_signatures)
619
(my_config._get_location_config().
620
_get_global_config()._get_parser(config_file))
621
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
1028
my_config = self.get_branch_config(
1029
global_config=sample_always_signatures)
1030
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1031
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1032
self.assertTrue(my_config.signature_needed())
1034
def test_signatures_forced_branch(self):
1035
my_config = self.get_branch_config(
1036
global_config=sample_ignore_signatures,
1037
branch_data_config=sample_always_signatures)
1038
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
1039
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
1040
self.assertTrue(my_config.signature_needed())
623
1042
def test_gpg_signing_command(self):
624
branch = FakeBranch()
625
my_config = config.BranchConfig(branch)
1043
my_config = self.get_branch_config(
1044
# branch data cannot set gpg_signing_command
1045
branch_data_config="gpg_signing_command=pgp")
626
1046
config_file = StringIO(sample_config_text.encode('utf-8'))
627
(my_config._get_location_config().
628
_get_global_config()._get_parser(config_file))
1047
my_config._get_global_config()._get_parser(config_file)
629
1048
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
631
1050
def test_get_user_option_global(self):
632
1051
branch = FakeBranch()
633
1052
my_config = config.BranchConfig(branch)
634
1053
config_file = StringIO(sample_config_text.encode('utf-8'))
635
(my_config._get_location_config().
636
_get_global_config()._get_parser(config_file))
1054
(my_config._get_global_config()._get_parser(config_file))
637
1055
self.assertEqual('something',
638
1056
my_config.get_user_option('user_global_option'))
640
1058
def test_post_commit_default(self):
641
1059
branch = FakeBranch()
643
my_config = config.BranchConfig(branch)
644
config_file = StringIO(sample_config_text.encode('utf-8'))
645
(my_config._get_location_config().
646
_get_global_config()._get_parser(config_file))
647
branch_file = StringIO(sample_branches_text)
648
my_config._get_location_config()._get_parser(branch_file)
649
self.assertEqual('bzrlib.tests.test_config.post_commit',
650
my_config.post_commit())
653
class TestMailAddressExtraction(TestCase):
1060
my_config = self.get_branch_config(sample_config_text, '/a/c',
1061
sample_branches_text)
1062
self.assertEqual(my_config.branch.base, '/a/c')
1063
self.assertEqual('bzrlib.tests.test_config.post_commit',
1064
my_config.post_commit())
1065
my_config.set_user_option('post_commit', 'rmtree_root')
1066
# post-commit is ignored when bresent in branch data
1067
self.assertEqual('bzrlib.tests.test_config.post_commit',
1068
my_config.post_commit())
1069
my_config.set_user_option('post_commit', 'rmtree_root',
1070
store=config.STORE_LOCATION)
1071
self.assertEqual('rmtree_root', my_config.post_commit())
1073
def test_config_precedence(self):
1074
my_config = self.get_branch_config(global_config=precedence_global)
1075
self.assertEqual(my_config.get_user_option('option'), 'global')
1076
my_config = self.get_branch_config(global_config=precedence_global,
1077
branch_data_config=precedence_branch)
1078
self.assertEqual(my_config.get_user_option('option'), 'branch')
1079
my_config = self.get_branch_config(global_config=precedence_global,
1080
branch_data_config=precedence_branch,
1081
location_config=precedence_location)
1082
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1083
my_config = self.get_branch_config(global_config=precedence_global,
1084
branch_data_config=precedence_branch,
1085
location_config=precedence_location,
1086
location='http://example.com/specific')
1087
self.assertEqual(my_config.get_user_option('option'), 'exact')
1089
def test_get_mail_client(self):
1090
config = self.get_branch_config()
1091
client = config.get_mail_client()
1092
self.assertIsInstance(client, mail_client.DefaultMail)
1095
config.set_user_option('mail_client', 'evolution')
1096
client = config.get_mail_client()
1097
self.assertIsInstance(client, mail_client.Evolution)
1099
config.set_user_option('mail_client', 'kmail')
1100
client = config.get_mail_client()
1101
self.assertIsInstance(client, mail_client.KMail)
1103
config.set_user_option('mail_client', 'mutt')
1104
client = config.get_mail_client()
1105
self.assertIsInstance(client, mail_client.Mutt)
1107
config.set_user_option('mail_client', 'thunderbird')
1108
client = config.get_mail_client()
1109
self.assertIsInstance(client, mail_client.Thunderbird)
1112
config.set_user_option('mail_client', 'default')
1113
client = config.get_mail_client()
1114
self.assertIsInstance(client, mail_client.DefaultMail)
1116
config.set_user_option('mail_client', 'editor')
1117
client = config.get_mail_client()
1118
self.assertIsInstance(client, mail_client.Editor)
1120
config.set_user_option('mail_client', 'mapi')
1121
client = config.get_mail_client()
1122
self.assertIsInstance(client, mail_client.MAPIClient)
1124
config.set_user_option('mail_client', 'xdg-email')
1125
client = config.get_mail_client()
1126
self.assertIsInstance(client, mail_client.XDGEmail)
1128
config.set_user_option('mail_client', 'firebird')
1129
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1132
class TestMailAddressExtraction(tests.TestCase):
655
1134
def test_extract_email_address(self):
656
1135
self.assertEqual('jane@test.com',
657
1136
config.extract_email_address('Jane <jane@test.com>'))
658
self.assertRaises(errors.BzrError,
1137
self.assertRaises(errors.NoEmailInUsername,
659
1138
config.extract_email_address, 'Jane Tester')
1140
def test_parse_username(self):
1141
self.assertEqual(('', 'jdoe@example.com'),
1142
config.parse_username('jdoe@example.com'))
1143
self.assertEqual(('', 'jdoe@example.com'),
1144
config.parse_username('<jdoe@example.com>'))
1145
self.assertEqual(('John Doe', 'jdoe@example.com'),
1146
config.parse_username('John Doe <jdoe@example.com>'))
1147
self.assertEqual(('John Doe', ''),
1148
config.parse_username('John Doe'))
1149
self.assertEqual(('John Doe', 'jdoe@example.com'),
1150
config.parse_username('John Doe jdoe@example.com'))
1152
class TestTreeConfig(tests.TestCaseWithTransport):
1154
def test_get_value(self):
1155
"""Test that retreiving a value from a section is possible"""
1156
branch = self.make_branch('.')
1157
tree_config = config.TreeConfig(branch)
1158
tree_config.set_option('value', 'key', 'SECTION')
1159
tree_config.set_option('value2', 'key2')
1160
tree_config.set_option('value3-top', 'key3')
1161
tree_config.set_option('value3-section', 'key3', 'SECTION')
1162
value = tree_config.get_option('key', 'SECTION')
1163
self.assertEqual(value, 'value')
1164
value = tree_config.get_option('key2')
1165
self.assertEqual(value, 'value2')
1166
self.assertEqual(tree_config.get_option('non-existant'), None)
1167
value = tree_config.get_option('non-existant', 'SECTION')
1168
self.assertEqual(value, None)
1169
value = tree_config.get_option('non-existant', default='default')
1170
self.assertEqual(value, 'default')
1171
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1172
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1173
self.assertEqual(value, 'default')
1174
value = tree_config.get_option('key3')
1175
self.assertEqual(value, 'value3-top')
1176
value = tree_config.get_option('key3', 'SECTION')
1177
self.assertEqual(value, 'value3-section')
1180
class TestTransportConfig(tests.TestCaseWithTransport):
1182
def test_get_value(self):
1183
"""Test that retreiving a value from a section is possible"""
1184
bzrdir_config = config.TransportConfig(transport.get_transport('.'),
1186
bzrdir_config.set_option('value', 'key', 'SECTION')
1187
bzrdir_config.set_option('value2', 'key2')
1188
bzrdir_config.set_option('value3-top', 'key3')
1189
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
1190
value = bzrdir_config.get_option('key', 'SECTION')
1191
self.assertEqual(value, 'value')
1192
value = bzrdir_config.get_option('key2')
1193
self.assertEqual(value, 'value2')
1194
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
1195
value = bzrdir_config.get_option('non-existant', 'SECTION')
1196
self.assertEqual(value, None)
1197
value = bzrdir_config.get_option('non-existant', default='default')
1198
self.assertEqual(value, 'default')
1199
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
1200
value = bzrdir_config.get_option('key2', 'NOSECTION',
1202
self.assertEqual(value, 'default')
1203
value = bzrdir_config.get_option('key3')
1204
self.assertEqual(value, 'value3-top')
1205
value = bzrdir_config.get_option('key3', 'SECTION')
1206
self.assertEqual(value, 'value3-section')
1209
class TestAuthenticationConfigFile(tests.TestCase):
1210
"""Test the authentication.conf file matching"""
1212
def _got_user_passwd(self, expected_user, expected_password,
1213
config, *args, **kwargs):
1214
credentials = config.get_credentials(*args, **kwargs)
1215
if credentials is None:
1219
user = credentials['user']
1220
password = credentials['password']
1221
self.assertEquals(expected_user, user)
1222
self.assertEquals(expected_password, password)
1224
def test_empty_config(self):
1225
conf = config.AuthenticationConfig(_file=StringIO())
1226
self.assertEquals({}, conf._get_config())
1227
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1229
def test_missing_auth_section_header(self):
1230
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
1231
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1233
def test_auth_section_header_not_closed(self):
1234
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1235
self.assertRaises(errors.ParseConfigError, conf._get_config)
1237
def test_auth_value_not_boolean(self):
1238
conf = config.AuthenticationConfig(_file=StringIO(
1242
verify_certificates=askme # Error: Not a boolean
1244
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1246
def test_auth_value_not_int(self):
1247
conf = config.AuthenticationConfig(_file=StringIO(
1251
port=port # Error: Not an int
1253
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1255
def test_credentials_for_scheme_host(self):
1256
conf = config.AuthenticationConfig(_file=StringIO(
1257
"""# Identity on foo.net
1262
password=secret-pass
1265
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1267
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1269
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1271
def test_credentials_for_host_port(self):
1272
conf = config.AuthenticationConfig(_file=StringIO(
1273
"""# Identity on foo.net
1279
password=secret-pass
1282
self._got_user_passwd('joe', 'secret-pass',
1283
conf, 'ftp', 'foo.net', port=10021)
1285
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1287
def test_for_matching_host(self):
1288
conf = config.AuthenticationConfig(_file=StringIO(
1289
"""# Identity on foo.net
1295
[sourceforge domain]
1302
self._got_user_passwd('georges', 'bendover',
1303
conf, 'bzr', 'foo.bzr.sf.net')
1305
self._got_user_passwd(None, None,
1306
conf, 'bzr', 'bbzr.sf.net')
1308
def test_for_matching_host_None(self):
1309
conf = config.AuthenticationConfig(_file=StringIO(
1310
"""# Identity on foo.net
1320
self._got_user_passwd('joe', 'joepass',
1321
conf, 'bzr', 'quux.net')
1322
# no host but different scheme
1323
self._got_user_passwd('georges', 'bendover',
1324
conf, 'ftp', 'quux.net')
1326
def test_credentials_for_path(self):
1327
conf = config.AuthenticationConfig(_file=StringIO(
1343
self._got_user_passwd(None, None,
1344
conf, 'http', host='bar.org', path='/dir3')
1346
self._got_user_passwd('georges', 'bendover',
1347
conf, 'http', host='bar.org', path='/dir2')
1349
self._got_user_passwd('jim', 'jimpass',
1350
conf, 'http', host='bar.org',path='/dir1/subdir')
1352
def test_credentials_for_user(self):
1353
conf = config.AuthenticationConfig(_file=StringIO(
1362
self._got_user_passwd('jim', 'jimpass',
1363
conf, 'http', 'bar.org')
1365
self._got_user_passwd('jim', 'jimpass',
1366
conf, 'http', 'bar.org', user='jim')
1367
# Don't get a different user if one is specified
1368
self._got_user_passwd(None, None,
1369
conf, 'http', 'bar.org', user='georges')
1371
def test_credentials_for_user_without_password(self):
1372
conf = config.AuthenticationConfig(_file=StringIO(
1379
# Get user but no password
1380
self._got_user_passwd('jim', None,
1381
conf, 'http', 'bar.org')
1383
def test_verify_certificates(self):
1384
conf = config.AuthenticationConfig(_file=StringIO(
1391
verify_certificates=False
1398
credentials = conf.get_credentials('https', 'bar.org')
1399
self.assertEquals(False, credentials.get('verify_certificates'))
1400
credentials = conf.get_credentials('https', 'foo.net')
1401
self.assertEquals(True, credentials.get('verify_certificates'))
1404
class TestAuthenticationConfig(tests.TestCase):
1405
"""Test AuthenticationConfig behaviour"""
1407
def _check_default_prompt(self, expected_prompt_format, scheme,
1408
host=None, port=None, realm=None, path=None):
1411
user, password = 'jim', 'precious'
1412
expected_prompt = expected_prompt_format % {
1413
'scheme': scheme, 'host': host, 'port': port,
1414
'user': user, 'realm': realm}
1416
stdout = tests.StringIOWrapper()
1417
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1419
# We use an empty conf so that the user is always prompted
1420
conf = config.AuthenticationConfig()
1421
self.assertEquals(password,
1422
conf.get_password(scheme, host, user, port=port,
1423
realm=realm, path=path))
1424
self.assertEquals(stdout.getvalue(), expected_prompt)
1426
def test_default_prompts(self):
1427
# HTTP prompts can't be tested here, see test_http.py
1428
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1429
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1432
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1434
# SMTP port handling is a bit special (it's handled if embedded in the
1436
# FIXME: should we: forbid that, extend it to other schemes, leave
1437
# things as they are that's fine thank you ?
1438
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1440
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1441
'smtp', host='bar.org:10025')
1442
self._check_default_prompt(
1443
'SMTP %(user)s@%(host)s:%(port)d password: ',
1446
def test_ssh_password_emits_warning(self):
1447
conf = config.AuthenticationConfig(_file=StringIO(
1455
entered_password = 'typed-by-hand'
1456
stdout = tests.StringIOWrapper()
1457
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1460
# Since the password defined in the authentication config is ignored,
1461
# the user is prompted
1462
self.assertEquals(entered_password,
1463
conf.get_password('ssh', 'bar.org', user='jim'))
1464
self.assertContainsRe(
1465
self._get_log(keep_log_file=True),
1466
'password ignored in section \[ssh with password\]')
1468
def test_ssh_without_password_doesnt_emit_warning(self):
1469
conf = config.AuthenticationConfig(_file=StringIO(
1476
entered_password = 'typed-by-hand'
1477
stdout = tests.StringIOWrapper()
1478
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
1481
# Since the password defined in the authentication config is ignored,
1482
# the user is prompted
1483
self.assertEquals(entered_password,
1484
conf.get_password('ssh', 'bar.org', user='jim'))
1485
# No warning shoud be emitted since there is no password. We are only
1487
self.assertNotContainsRe(
1488
self._get_log(keep_log_file=True),
1489
'password ignored in section \[ssh with password\]')
1492
# FIXME: Once we have a way to declare authentication to all test servers, we
1493
# can implement generic tests.
1494
# test_user_password_in_url
1495
# test_user_in_url_password_from_config
1496
# test_user_in_url_password_prompted
1497
# test_user_in_config
1498
# test_user_getpass.getuser
1499
# test_user_prompted ?
1500
class TestAuthenticationRing(tests.TestCaseWithTransport):