595
336
# This is testing the correct file names are provided.
596
337
# TODO: consolidate with the test for GlobalConfigs filename checks.
598
# replace the class that is constructed, to check its parameters
339
# replace the class that is constructured, to check its parameters
599
340
oldparserclass = config.ConfigObj
600
341
config.ConfigObj = InstrumentedConfigObj
342
my_config = config.LocationConfig('http://www.example.com')
602
my_config = config.LocationConfig('http://www.example.com')
603
344
parser = my_config._get_parser()
605
346
config.ConfigObj = oldparserclass
606
347
self.failUnless(isinstance(parser, InstrumentedConfigObj))
607
348
self.assertEqual(parser._calls,
608
[('__init__', config.locations_config_filename(),
610
config.ensure_config_dir_exists()
611
#os.mkdir(config.config_dir())
612
f = file(config.branches_config_filename(), 'wb')
615
oldparserclass = config.ConfigObj
616
config.ConfigObj = InstrumentedConfigObj
618
my_config = config.LocationConfig('http://www.example.com')
619
parser = my_config._get_parser()
621
config.ConfigObj = oldparserclass
349
[('__init__', config.branches_config_filename())])
623
351
def test_get_global_config(self):
624
my_config = config.BranchConfig(FakeBranch('http://example.com'))
352
my_config = config.LocationConfig('http://example.com')
625
353
global_config = my_config._get_global_config()
626
354
self.failUnless(isinstance(global_config, config.GlobalConfig))
627
355
self.failUnless(global_config is my_config._get_global_config())
629
def test__get_matching_sections_no_match(self):
630
self.get_branch_config('/')
631
self.assertEqual([], self.my_location_config._get_matching_sections())
633
def test__get_matching_sections_exact(self):
634
self.get_branch_config('http://www.example.com')
635
self.assertEqual([('http://www.example.com', '')],
636
self.my_location_config._get_matching_sections())
638
def test__get_matching_sections_suffix_does_not(self):
639
self.get_branch_config('http://www.example.com-com')
640
self.assertEqual([], self.my_location_config._get_matching_sections())
642
def test__get_matching_sections_subdir_recursive(self):
643
self.get_branch_config('http://www.example.com/com')
644
self.assertEqual([('http://www.example.com', 'com')],
645
self.my_location_config._get_matching_sections())
647
def test__get_matching_sections_ignoreparent(self):
648
self.get_branch_config('http://www.example.com/ignoreparent')
649
self.assertEqual([('http://www.example.com/ignoreparent', '')],
650
self.my_location_config._get_matching_sections())
652
def test__get_matching_sections_ignoreparent_subdir(self):
653
self.get_branch_config(
654
'http://www.example.com/ignoreparent/childbranch')
655
self.assertEqual([('http://www.example.com/ignoreparent',
657
self.my_location_config._get_matching_sections())
659
def test__get_matching_sections_subdir_trailing_slash(self):
660
self.get_branch_config('/b')
661
self.assertEqual([('/b/', '')],
662
self.my_location_config._get_matching_sections())
664
def test__get_matching_sections_subdir_child(self):
665
self.get_branch_config('/a/foo')
666
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
667
self.my_location_config._get_matching_sections())
669
def test__get_matching_sections_subdir_child_child(self):
670
self.get_branch_config('/a/foo/bar')
671
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
672
self.my_location_config._get_matching_sections())
674
def test__get_matching_sections_trailing_slash_with_children(self):
675
self.get_branch_config('/a/')
676
self.assertEqual([('/a/', '')],
677
self.my_location_config._get_matching_sections())
679
def test__get_matching_sections_explicit_over_glob(self):
680
# XXX: 2006-09-08 jamesh
681
# This test only passes because ord('c') > ord('*'). If there
682
# was a config section for '/a/?', it would get precedence
684
self.get_branch_config('/a/c')
685
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
686
self.my_location_config._get_matching_sections())
688
def test__get_option_policy_normal(self):
689
self.get_branch_config('http://www.example.com')
691
self.my_location_config._get_config_policy(
692
'http://www.example.com', 'normal_option'),
695
def test__get_option_policy_norecurse(self):
696
self.get_branch_config('http://www.example.com')
698
self.my_location_config._get_option_policy(
699
'http://www.example.com', 'norecurse_option'),
700
config.POLICY_NORECURSE)
701
# Test old recurse=False setting:
703
self.my_location_config._get_option_policy(
704
'http://www.example.com/norecurse', 'normal_option'),
705
config.POLICY_NORECURSE)
707
def test__get_option_policy_normal(self):
708
self.get_branch_config('http://www.example.com')
710
self.my_location_config._get_option_policy(
711
'http://www.example.com', 'appendpath_option'),
712
config.POLICY_APPENDPATH)
357
def test__get_section_no_match(self):
358
self.get_location_config('/')
359
self.assertEqual(None, self.my_config._get_section())
361
def test__get_section_exact(self):
362
self.get_location_config('http://www.example.com')
363
self.assertEqual('http://www.example.com',
364
self.my_config._get_section())
366
def test__get_section_suffix_does_not(self):
367
self.get_location_config('http://www.example.com-com')
368
self.assertEqual(None, self.my_config._get_section())
370
def test__get_section_subdir_recursive(self):
371
self.get_location_config('http://www.example.com/com')
372
self.assertEqual('http://www.example.com',
373
self.my_config._get_section())
375
def test__get_section_subdir_matches(self):
376
self.get_location_config('http://www.example.com/useglobal')
377
self.assertEqual('http://www.example.com/useglobal',
378
self.my_config._get_section())
380
def test__get_section_subdir_nonrecursive(self):
381
self.get_location_config(
382
'http://www.example.com/useglobal/childbranch')
383
self.assertEqual('http://www.example.com',
384
self.my_config._get_section())
386
def test__get_section_subdir_trailing_slash(self):
387
self.get_location_config('/b')
388
self.assertEqual('/b/', self.my_config._get_section())
390
def test__get_section_subdir_child(self):
391
self.get_location_config('/a/foo')
392
self.assertEqual('/a/*', self.my_config._get_section())
394
def test__get_section_subdir_child_child(self):
395
self.get_location_config('/a/foo/bar')
396
self.assertEqual('/a/', self.my_config._get_section())
398
def test__get_section_trailing_slash_with_children(self):
399
self.get_location_config('/a/')
400
self.assertEqual('/a/', self.my_config._get_section())
402
def test__get_section_explicit_over_glob(self):
403
self.get_location_config('/a/c')
404
self.assertEqual('/a/c', self.my_config._get_section())
406
def get_location_config(self, location, global_config=None):
407
if global_config is None:
408
global_file = StringIO(sample_config_text)
410
global_file = StringIO(global_config)
411
branches_file = StringIO(sample_branches_text)
412
self.my_config = config.LocationConfig(location)
413
self.my_config._get_parser(branches_file)
414
self.my_config._get_global_config()._get_parser(global_file)
714
416
def test_location_without_username(self):
715
self.get_branch_config('http://www.example.com/ignoreparent')
716
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
417
self.get_location_config('http://www.example.com/useglobal')
418
self.assertEqual('Robert Collins <robertc@example.com>',
717
419
self.my_config.username())
719
421
def test_location_not_listed(self):
720
"""Test that the global username is used when no location matches"""
721
self.get_branch_config('/home/robertc/sources')
722
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
422
self.get_location_config('/home/robertc/sources')
423
self.assertEqual('Robert Collins <robertc@example.com>',
723
424
self.my_config.username())
725
426
def test_overriding_location(self):
726
self.get_branch_config('http://www.example.com/foo')
427
self.get_location_config('http://www.example.com/foo')
727
428
self.assertEqual('Robert Collins <robertc@example.org>',
728
429
self.my_config.username())
730
431
def test_signatures_not_set(self):
731
self.get_branch_config('http://www.example.com',
432
self.get_location_config('http://www.example.com',
732
433
global_config=sample_ignore_signatures)
733
self.assertEqual(config.CHECK_ALWAYS,
434
self.assertEqual(config.CHECK_NEVER,
734
435
self.my_config.signature_checking())
735
self.assertEqual(config.SIGN_NEVER,
736
self.my_config.signing_policy())
738
437
def test_signatures_never(self):
739
self.get_branch_config('/a/c')
438
self.get_location_config('/a/c')
740
439
self.assertEqual(config.CHECK_NEVER,
741
440
self.my_config.signature_checking())
743
442
def test_signatures_when_available(self):
744
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
443
self.get_location_config('/a/', global_config=sample_ignore_signatures)
745
444
self.assertEqual(config.CHECK_IF_POSSIBLE,
746
445
self.my_config.signature_checking())
748
447
def test_signatures_always(self):
749
self.get_branch_config('/b')
448
self.get_location_config('/b')
750
449
self.assertEqual(config.CHECK_ALWAYS,
751
450
self.my_config.signature_checking())
753
452
def test_gpg_signing_command(self):
754
self.get_branch_config('/b')
453
self.get_location_config('/b')
755
454
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
757
456
def test_gpg_signing_command_missing(self):
758
self.get_branch_config('/a')
457
self.get_location_config('/a')
759
458
self.assertEqual("false", self.my_config.gpg_signing_command())
761
460
def test_get_user_option_global(self):
762
self.get_branch_config('/a')
461
self.get_location_config('/a')
763
462
self.assertEqual('something',
764
463
self.my_config.get_user_option('user_global_option'))
766
465
def test_get_user_option_local(self):
767
self.get_branch_config('/a')
466
self.get_location_config('/a')
768
467
self.assertEqual('local',
769
468
self.my_config.get_user_option('user_local_option'))
771
def test_get_user_option_appendpath(self):
772
# returned as is for the base path:
773
self.get_branch_config('http://www.example.com')
774
self.assertEqual('append',
775
self.my_config.get_user_option('appendpath_option'))
776
# Extra path components get appended:
777
self.get_branch_config('http://www.example.com/a/b/c')
778
self.assertEqual('append/a/b/c',
779
self.my_config.get_user_option('appendpath_option'))
780
# Overriden for http://www.example.com/dir, where it is a
782
self.get_branch_config('http://www.example.com/dir/a/b/c')
783
self.assertEqual('normal',
784
self.my_config.get_user_option('appendpath_option'))
786
def test_get_user_option_norecurse(self):
787
self.get_branch_config('http://www.example.com')
788
self.assertEqual('norecurse',
789
self.my_config.get_user_option('norecurse_option'))
790
self.get_branch_config('http://www.example.com/dir')
791
self.assertEqual(None,
792
self.my_config.get_user_option('norecurse_option'))
793
# http://www.example.com/norecurse is a recurse=False section
794
# that redefines normal_option. Subdirectories do not pick up
796
self.get_branch_config('http://www.example.com/norecurse')
797
self.assertEqual('norecurse',
798
self.my_config.get_user_option('normal_option'))
799
self.get_branch_config('http://www.example.com/norecurse/subdir')
800
self.assertEqual('normal',
801
self.my_config.get_user_option('normal_option'))
803
def test_set_user_option_norecurse(self):
804
self.get_branch_config('http://www.example.com')
805
self.my_config.set_user_option('foo', 'bar',
806
store=config.STORE_LOCATION_NORECURSE)
808
self.my_location_config._get_option_policy(
809
'http://www.example.com', 'foo'),
810
config.POLICY_NORECURSE)
812
def test_set_user_option_appendpath(self):
813
self.get_branch_config('http://www.example.com')
814
self.my_config.set_user_option('foo', 'bar',
815
store=config.STORE_LOCATION_APPENDPATH)
817
self.my_location_config._get_option_policy(
818
'http://www.example.com', 'foo'),
819
config.POLICY_APPENDPATH)
821
def test_set_user_option_change_policy(self):
822
self.get_branch_config('http://www.example.com')
823
self.my_config.set_user_option('norecurse_option', 'normal',
824
store=config.STORE_LOCATION)
826
self.my_location_config._get_option_policy(
827
'http://www.example.com', 'norecurse_option'),
830
def test_set_user_option_recurse_false_section(self):
831
# The following section has recurse=False set. The test is to
832
# make sure that a normal option can be added to the section,
833
# converting recurse=False to the norecurse policy.
834
self.get_branch_config('http://www.example.com/norecurse')
835
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
836
'The section "http://www.example.com/norecurse" '
837
'has been converted to use policies.'],
838
self.my_config.set_user_option,
839
'foo', 'bar', store=config.STORE_LOCATION)
841
self.my_location_config._get_option_policy(
842
'http://www.example.com/norecurse', 'foo'),
844
# The previously existing option is still norecurse:
846
self.my_location_config._get_option_policy(
847
'http://www.example.com/norecurse', 'normal_option'),
848
config.POLICY_NORECURSE)
850
470
def test_post_commit_default(self):
851
self.get_branch_config('/a/c')
852
self.assertEqual('bzrlib.tests.test_config.post_commit',
471
self.get_location_config('/a/c')
472
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
853
473
self.my_config.post_commit())
855
def get_branch_config(self, location, global_config=None):
856
if global_config is None:
857
global_file = StringIO(sample_config_text.encode('utf-8'))
859
global_file = StringIO(global_config.encode('utf-8'))
860
branches_file = StringIO(sample_branches_text.encode('utf-8'))
861
self.my_config = config.BranchConfig(FakeBranch(location))
862
# Force location config to use specified file
863
self.my_location_config = self.my_config._get_location_config()
864
self.my_location_config._get_parser(branches_file)
865
# Force global config to use specified file
866
self.my_config._get_global_config()._get_parser(global_file)
868
475
def test_set_user_setting_sets_and_saves(self):
869
self.get_branch_config('/a/c')
476
# TODO RBC 20051029 test hat mkdir ~/.bazaar is called ..
477
self.get_location_config('/a/c')
870
478
record = InstrumentedConfigObj("foo")
871
self.my_location_config._parser = record
873
real_mkdir = os.mkdir
875
def checked_mkdir(path, mode=0777):
876
self.log('making directory: %s', path)
877
real_mkdir(path, mode)
880
os.mkdir = checked_mkdir
882
self.callDeprecated(['The recurse option is deprecated as of '
883
'0.14. The section "/a/c" has been '
884
'converted to use policies.'],
885
self.my_config.set_user_option,
886
'foo', 'bar', store=config.STORE_LOCATION)
888
os.mkdir = real_mkdir
890
self.failUnless(self.created, 'Failed to create ~/.bazaar')
479
self.my_config._parser = record
480
self.my_config.set_user_option('foo', 'bar')
891
481
self.assertEqual([('__contains__', '/a/c'),
892
482
('__contains__', '/a/c/'),
893
483
('__setitem__', '/a/c', {}),
894
484
('__getitem__', '/a/c'),
895
485
('__setitem__', 'foo', 'bar'),
896
('__getitem__', '/a/c'),
897
('as_bool', 'recurse'),
898
('__getitem__', '/a/c'),
899
('__delitem__', 'recurse'),
900
('__getitem__', '/a/c'),
902
('__getitem__', '/a/c'),
903
('__contains__', 'foo:policy'),
905
487
record._calls[1:])
907
def test_set_user_setting_sets_and_saves2(self):
908
self.get_branch_config('/a/c')
909
self.assertIs(self.my_config.get_user_option('foo'), None)
910
self.my_config.set_user_option('foo', 'bar')
912
self.my_config.branch.control_files.files['branch.conf'],
914
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
915
self.my_config.set_user_option('foo', 'baz',
916
store=config.STORE_LOCATION)
917
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
918
self.my_config.set_user_option('foo', 'qux')
919
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
921
def test_get_bzr_remote_path(self):
922
my_config = config.LocationConfig('/a/c')
923
self.assertEqual('bzr', my_config.get_bzr_remote_path())
924
my_config.set_user_option('bzr_remote_path', '/path-bzr')
925
self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
926
os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
927
self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
930
precedence_global = 'option = global'
931
precedence_branch = 'option = branch'
932
precedence_location = """
936
[http://example.com/specific]
941
class TestBranchConfigItems(tests.TestCaseInTempDir):
943
def get_branch_config(self, global_config=None, location=None,
944
location_config=None, branch_data_config=None):
945
my_config = config.BranchConfig(FakeBranch(location))
946
if global_config is not None:
947
global_file = StringIO(global_config.encode('utf-8'))
948
my_config._get_global_config()._get_parser(global_file)
949
self.my_location_config = my_config._get_location_config()
950
if location_config is not None:
951
location_file = StringIO(location_config.encode('utf-8'))
952
self.my_location_config._get_parser(location_file)
953
if branch_data_config is not None:
954
my_config.branch.control_files.files['branch.conf'] = \
490
class TestBranchConfigItems(TestCase):
958
492
def test_user_id(self):
959
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
493
branch = FakeBranch()
960
494
my_config = config.BranchConfig(branch)
961
495
self.assertEqual("Robert Collins <robertc@example.net>",
962
my_config.username())
963
branch.control_files.email = "John"
964
my_config.set_user_option('email',
965
"Robert Collins <robertc@example.org>")
966
self.assertEqual("John", my_config.username())
967
branch.control_files.email = None
968
self.assertEqual("Robert Collins <robertc@example.org>",
969
my_config.username())
496
my_config._get_user_id())
497
branch.email = "John"
498
self.assertEqual("John", my_config._get_user_id())
971
500
def test_not_set_in_branch(self):
972
my_config = self.get_branch_config(sample_config_text)
973
my_config.branch.control_files.email = None
974
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
501
branch = FakeBranch()
502
my_config = config.BranchConfig(branch)
504
config_file = StringIO(sample_config_text)
505
(my_config._get_location_config().
506
_get_global_config()._get_parser(config_file))
507
self.assertEqual("Robert Collins <robertc@example.com>",
975
508
my_config._get_user_id())
976
my_config.branch.control_files.email = "John"
509
branch.email = "John"
977
510
self.assertEqual("John", my_config._get_user_id())
979
def test_BZR_EMAIL_OVERRIDES(self):
980
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
512
def test_BZREMAIL_OVERRIDES(self):
513
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
981
514
branch = FakeBranch()
982
515
my_config = config.BranchConfig(branch)
983
516
self.assertEqual("Robert Collins <robertc@example.org>",
984
517
my_config.username())
986
519
def test_signatures_forced(self):
987
my_config = self.get_branch_config(
988
global_config=sample_always_signatures)
989
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
990
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
991
self.assertTrue(my_config.signature_needed())
993
def test_signatures_forced_branch(self):
994
my_config = self.get_branch_config(
995
global_config=sample_ignore_signatures,
996
branch_data_config=sample_always_signatures)
997
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
998
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
999
self.assertTrue(my_config.signature_needed())
520
branch = FakeBranch()
521
my_config = config.BranchConfig(branch)
522
config_file = StringIO(sample_always_signatures)
523
(my_config._get_location_config().
524
_get_global_config()._get_parser(config_file))
525
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
1001
527
def test_gpg_signing_command(self):
1002
my_config = self.get_branch_config(
1003
# branch data cannot set gpg_signing_command
1004
branch_data_config="gpg_signing_command=pgp")
1005
config_file = StringIO(sample_config_text.encode('utf-8'))
1006
my_config._get_global_config()._get_parser(config_file)
528
branch = FakeBranch()
529
my_config = config.BranchConfig(branch)
530
config_file = StringIO(sample_config_text)
531
(my_config._get_location_config().
532
_get_global_config()._get_parser(config_file))
1007
533
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
1009
535
def test_get_user_option_global(self):
1010
536
branch = FakeBranch()
1011
537
my_config = config.BranchConfig(branch)
1012
config_file = StringIO(sample_config_text.encode('utf-8'))
1013
(my_config._get_global_config()._get_parser(config_file))
538
config_file = StringIO(sample_config_text)
539
(my_config._get_location_config().
540
_get_global_config()._get_parser(config_file))
1014
541
self.assertEqual('something',
1015
542
my_config.get_user_option('user_global_option'))
1017
544
def test_post_commit_default(self):
1018
545
branch = FakeBranch()
1019
my_config = self.get_branch_config(sample_config_text, '/a/c',
1020
sample_branches_text)
1021
self.assertEqual(my_config.branch.base, '/a/c')
1022
self.assertEqual('bzrlib.tests.test_config.post_commit',
1023
my_config.post_commit())
1024
my_config.set_user_option('post_commit', 'rmtree_root')
1025
# post-commit is ignored when bresent in branch data
1026
self.assertEqual('bzrlib.tests.test_config.post_commit',
1027
my_config.post_commit())
1028
my_config.set_user_option('post_commit', 'rmtree_root',
1029
store=config.STORE_LOCATION)
1030
self.assertEqual('rmtree_root', my_config.post_commit())
1032
def test_config_precedence(self):
1033
my_config = self.get_branch_config(global_config=precedence_global)
1034
self.assertEqual(my_config.get_user_option('option'), 'global')
1035
my_config = self.get_branch_config(global_config=precedence_global,
1036
branch_data_config=precedence_branch)
1037
self.assertEqual(my_config.get_user_option('option'), 'branch')
1038
my_config = self.get_branch_config(global_config=precedence_global,
1039
branch_data_config=precedence_branch,
1040
location_config=precedence_location)
1041
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1042
my_config = self.get_branch_config(global_config=precedence_global,
1043
branch_data_config=precedence_branch,
1044
location_config=precedence_location,
1045
location='http://example.com/specific')
1046
self.assertEqual(my_config.get_user_option('option'), 'exact')
1048
def test_get_mail_client(self):
1049
config = self.get_branch_config()
1050
client = config.get_mail_client()
1051
self.assertIsInstance(client, mail_client.DefaultMail)
1054
config.set_user_option('mail_client', 'evolution')
1055
client = config.get_mail_client()
1056
self.assertIsInstance(client, mail_client.Evolution)
1058
config.set_user_option('mail_client', 'kmail')
1059
client = config.get_mail_client()
1060
self.assertIsInstance(client, mail_client.KMail)
1062
config.set_user_option('mail_client', 'mutt')
1063
client = config.get_mail_client()
1064
self.assertIsInstance(client, mail_client.Mutt)
1066
config.set_user_option('mail_client', 'thunderbird')
1067
client = config.get_mail_client()
1068
self.assertIsInstance(client, mail_client.Thunderbird)
1071
config.set_user_option('mail_client', 'default')
1072
client = config.get_mail_client()
1073
self.assertIsInstance(client, mail_client.DefaultMail)
1075
config.set_user_option('mail_client', 'editor')
1076
client = config.get_mail_client()
1077
self.assertIsInstance(client, mail_client.Editor)
1079
config.set_user_option('mail_client', 'mapi')
1080
client = config.get_mail_client()
1081
self.assertIsInstance(client, mail_client.MAPIClient)
1083
config.set_user_option('mail_client', 'xdg-email')
1084
client = config.get_mail_client()
1085
self.assertIsInstance(client, mail_client.XDGEmail)
1087
config.set_user_option('mail_client', 'firebird')
1088
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1091
class TestMailAddressExtraction(tests.TestCase):
1093
def test_extract_email_address(self):
1094
self.assertEqual('jane@test.com',
1095
config.extract_email_address('Jane <jane@test.com>'))
1096
self.assertRaises(errors.NoEmailInUsername,
1097
config.extract_email_address, 'Jane Tester')
1099
def test_parse_username(self):
1100
self.assertEqual(('', 'jdoe@example.com'),
1101
config.parse_username('jdoe@example.com'))
1102
self.assertEqual(('', 'jdoe@example.com'),
1103
config.parse_username('<jdoe@example.com>'))
1104
self.assertEqual(('John Doe', 'jdoe@example.com'),
1105
config.parse_username('John Doe <jdoe@example.com>'))
1106
self.assertEqual(('John Doe', ''),
1107
config.parse_username('John Doe'))
1108
self.assertEqual(('John Doe', 'jdoe@example.com'),
1109
config.parse_username('John Doe jdoe@example.com'))
1111
class TestTreeConfig(tests.TestCaseWithTransport):
1113
def test_get_value(self):
1114
"""Test that retreiving a value from a section is possible"""
1115
branch = self.make_branch('.')
1116
tree_config = config.TreeConfig(branch)
1117
tree_config.set_option('value', 'key', 'SECTION')
1118
tree_config.set_option('value2', 'key2')
1119
tree_config.set_option('value3-top', 'key3')
1120
tree_config.set_option('value3-section', 'key3', 'SECTION')
1121
value = tree_config.get_option('key', 'SECTION')
1122
self.assertEqual(value, 'value')
1123
value = tree_config.get_option('key2')
1124
self.assertEqual(value, 'value2')
1125
self.assertEqual(tree_config.get_option('non-existant'), None)
1126
value = tree_config.get_option('non-existant', 'SECTION')
1127
self.assertEqual(value, None)
1128
value = tree_config.get_option('non-existant', default='default')
1129
self.assertEqual(value, 'default')
1130
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1131
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1132
self.assertEqual(value, 'default')
1133
value = tree_config.get_option('key3')
1134
self.assertEqual(value, 'value3-top')
1135
value = tree_config.get_option('key3', 'SECTION')
1136
self.assertEqual(value, 'value3-section')
1139
class TestAuthenticationConfigFile(tests.TestCase):
1140
"""Test the authentication.conf file matching"""
1142
def _got_user_passwd(self, expected_user, expected_password,
1143
config, *args, **kwargs):
1144
credentials = config.get_credentials(*args, **kwargs)
1145
if credentials is None:
1149
user = credentials['user']
1150
password = credentials['password']
1151
self.assertEquals(expected_user, user)
1152
self.assertEquals(expected_password, password)
1154
def test_empty_config(self):
1155
conf = config.AuthenticationConfig(_file=StringIO())
1156
self.assertEquals({}, conf._get_config())
1157
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1159
def test_broken_config(self):
1160
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1161
self.assertRaises(errors.ParseConfigError, conf._get_config)
1163
conf = config.AuthenticationConfig(_file=StringIO(
1167
verify_certificates=askme # Error: Not a boolean
1169
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1170
conf = config.AuthenticationConfig(_file=StringIO(
1174
port=port # Error: Not an int
1176
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1178
def test_credentials_for_scheme_host(self):
1179
conf = config.AuthenticationConfig(_file=StringIO(
1180
"""# Identity on foo.net
1185
password=secret-pass
1188
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1190
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1192
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1194
def test_credentials_for_host_port(self):
1195
conf = config.AuthenticationConfig(_file=StringIO(
1196
"""# Identity on foo.net
1202
password=secret-pass
1205
self._got_user_passwd('joe', 'secret-pass',
1206
conf, 'ftp', 'foo.net', port=10021)
1208
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1210
def test_for_matching_host(self):
1211
conf = config.AuthenticationConfig(_file=StringIO(
1212
"""# Identity on foo.net
1218
[sourceforge domain]
1225
self._got_user_passwd('georges', 'bendover',
1226
conf, 'bzr', 'foo.bzr.sf.net')
1228
self._got_user_passwd(None, None,
1229
conf, 'bzr', 'bbzr.sf.net')
1231
def test_for_matching_host_None(self):
1232
conf = config.AuthenticationConfig(_file=StringIO(
1233
"""# Identity on foo.net
1243
self._got_user_passwd('joe', 'joepass',
1244
conf, 'bzr', 'quux.net')
1245
# no host but different scheme
1246
self._got_user_passwd('georges', 'bendover',
1247
conf, 'ftp', 'quux.net')
1249
def test_credentials_for_path(self):
1250
conf = config.AuthenticationConfig(_file=StringIO(
1266
self._got_user_passwd(None, None,
1267
conf, 'http', host='bar.org', path='/dir3')
1269
self._got_user_passwd('georges', 'bendover',
1270
conf, 'http', host='bar.org', path='/dir2')
1272
self._got_user_passwd('jim', 'jimpass',
1273
conf, 'http', host='bar.org',path='/dir1/subdir')
1275
def test_credentials_for_user(self):
1276
conf = config.AuthenticationConfig(_file=StringIO(
1285
self._got_user_passwd('jim', 'jimpass',
1286
conf, 'http', 'bar.org')
1288
self._got_user_passwd('jim', 'jimpass',
1289
conf, 'http', 'bar.org', user='jim')
1290
# Don't get a different user if one is specified
1291
self._got_user_passwd(None, None,
1292
conf, 'http', 'bar.org', user='georges')
1294
def test_verify_certificates(self):
1295
conf = config.AuthenticationConfig(_file=StringIO(
1302
verify_certificates=False
1309
credentials = conf.get_credentials('https', 'bar.org')
1310
self.assertEquals(False, credentials.get('verify_certificates'))
1311
credentials = conf.get_credentials('https', 'foo.net')
1312
self.assertEquals(True, credentials.get('verify_certificates'))
1315
class TestAuthenticationConfig(tests.TestCase):
1316
"""Test AuthenticationConfig behaviour"""
1318
def _check_default_prompt(self, expected_prompt_format, scheme,
1319
host=None, port=None, realm=None, path=None):
1322
user, password = 'jim', 'precious'
1323
expected_prompt = expected_prompt_format % {
1324
'scheme': scheme, 'host': host, 'port': port,
1325
'user': user, 'realm': realm}
1327
stdout = tests.StringIOWrapper()
1328
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1330
# We use an empty conf so that the user is always prompted
1331
conf = config.AuthenticationConfig()
1332
self.assertEquals(password,
1333
conf.get_password(scheme, host, user, port=port,
1334
realm=realm, path=path))
1335
self.assertEquals(stdout.getvalue(), expected_prompt)
1337
def test_default_prompts(self):
1338
# HTTP prompts can't be tested here, see test_http.py
1339
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1340
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1343
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1345
# SMTP port handling is a bit special (it's handled if embedded in the
1347
# FIXME: should we: forbid that, extend it to other schemes, leave
1348
# things as they are that's fine thank you ?
1349
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1351
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1352
'smtp', host='bar.org:10025')
1353
self._check_default_prompt(
1354
'SMTP %(user)s@%(host)s:%(port)d password: ',
1358
# FIXME: Once we have a way to declare authentication to all test servers, we
1359
# can implement generic tests.
1360
# test_user_password_in_url
1361
# test_user_in_url_password_from_config
1362
# test_user_in_url_password_prompted
1363
# test_user_in_config
1364
# test_user_getpass.getuser
1365
# test_user_prompted ?
1366
class TestAuthenticationRing(tests.TestCaseWithTransport):
547
my_config = config.BranchConfig(branch)
548
config_file = StringIO(sample_config_text)
549
(my_config._get_location_config().
550
_get_global_config()._get_parser(config_file))
551
branch_file = StringIO(sample_branches_text)
552
my_config._get_location_config()._get_parser(branch_file)
553
self.assertEqual('bzrlib.selftest.testconfig.post_commit',
554
my_config.post_commit())