300
373
self.assertEqual(branch.base, location_config.location)
301
374
self.failUnless(location_config is my_config._get_location_config())
304
class TestGlobalConfigItems(TestCase):
376
def test_get_config(self):
377
"""The Branch.get_config method works properly"""
378
b = bzrdir.BzrDir.create_standalone_workingtree('.').branch
379
my_config = b.get_config()
380
self.assertIs(my_config.get_user_option('wacky'), None)
381
my_config.set_user_option('wacky', 'unlikely')
382
self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
384
# Ensure we get the same thing if we start again
385
b2 = branch.Branch.open('.')
386
my_config2 = b2.get_config()
387
self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
389
def test_has_explicit_nickname(self):
390
b = self.make_branch('.')
391
self.assertFalse(b.get_config().has_explicit_nickname())
393
self.assertTrue(b.get_config().has_explicit_nickname())
395
def test_config_url(self):
396
"""The Branch.get_config will use section that uses a local url"""
397
branch = self.make_branch('branch')
398
self.assertEqual('branch', branch.nick)
400
locations = config.locations_config_filename()
401
config.ensure_config_dir_exists()
402
local_url = urlutils.local_path_to_url('branch')
403
open(locations, 'wb').write('[%s]\nnickname = foobar'
405
self.assertEqual('foobar', branch.nick)
407
def test_config_local_path(self):
408
"""The Branch.get_config will use a local system path"""
409
branch = self.make_branch('branch')
410
self.assertEqual('branch', branch.nick)
412
locations = config.locations_config_filename()
413
config.ensure_config_dir_exists()
414
open(locations, 'wb').write('[%s/branch]\nnickname = barry'
415
% (osutils.getcwd().encode('utf8'),))
416
self.assertEqual('barry', branch.nick)
418
def test_config_creates_local(self):
419
"""Creating a new entry in config uses a local path."""
420
branch = self.make_branch('branch', format='knit')
421
branch.set_push_location('http://foobar')
422
locations = config.locations_config_filename()
423
local_path = osutils.getcwd().encode('utf8')
424
# Surprisingly ConfigObj doesn't create a trailing newline
425
self.check_file_contents(locations,
427
'push_location = http://foobar\n'
428
'push_location:policy = norecurse'
431
def test_autonick_urlencoded(self):
432
b = self.make_branch('!repo')
433
self.assertEqual('!repo', b.get_config().get_nickname())
435
def test_warn_if_masked(self):
436
_warning = trace.warning
439
warnings.append(args[0] % args[1:])
441
def set_option(store, warn_masked=True):
443
conf.set_user_option('example_option', repr(store), store=store,
444
warn_masked=warn_masked)
445
def assertWarning(warning):
447
self.assertEqual(0, len(warnings))
449
self.assertEqual(1, len(warnings))
450
self.assertEqual(warning, warnings[0])
451
trace.warning = warning
453
branch = self.make_branch('.')
454
conf = branch.get_config()
455
set_option(config.STORE_GLOBAL)
457
set_option(config.STORE_BRANCH)
459
set_option(config.STORE_GLOBAL)
460
assertWarning('Value "4" is masked by "3" from branch.conf')
461
set_option(config.STORE_GLOBAL, warn_masked=False)
463
set_option(config.STORE_LOCATION)
465
set_option(config.STORE_BRANCH)
466
assertWarning('Value "3" is masked by "0" from locations.conf')
467
set_option(config.STORE_BRANCH, warn_masked=False)
470
trace.warning = _warning
473
class TestGlobalConfigItems(tests.TestCase):
306
475
def test_user_id(self):
307
476
config_file = StringIO(sample_config_text.encode('utf-8'))
407
582
# This is testing the correct file names are provided.
408
583
# TODO: consolidate with the test for GlobalConfigs filename checks.
410
# replace the class that is constructured, to check its parameters
585
# replace the class that is constructed, to check its parameters
411
586
oldparserclass = config.ConfigObj
412
587
config.ConfigObj = InstrumentedConfigObj
413
my_config = config.LocationConfig('http://www.example.com')
589
my_config = config.LocationConfig('http://www.example.com')
415
590
parser = my_config._get_parser()
417
592
config.ConfigObj = oldparserclass
418
593
self.failUnless(isinstance(parser, InstrumentedConfigObj))
419
594
self.assertEqual(parser._calls,
420
[('__init__', config.branches_config_filename(),
595
[('__init__', config.locations_config_filename(),
597
config.ensure_config_dir_exists()
598
#os.mkdir(config.config_dir())
599
f = file(config.branches_config_filename(), 'wb')
602
oldparserclass = config.ConfigObj
603
config.ConfigObj = InstrumentedConfigObj
605
my_config = config.LocationConfig('http://www.example.com')
606
parser = my_config._get_parser()
608
config.ConfigObj = oldparserclass
423
610
def test_get_global_config(self):
424
my_config = config.LocationConfig('http://example.com')
611
my_config = config.BranchConfig(FakeBranch('http://example.com'))
425
612
global_config = my_config._get_global_config()
426
613
self.failUnless(isinstance(global_config, config.GlobalConfig))
427
614
self.failUnless(global_config is my_config._get_global_config())
429
def test__get_section_no_match(self):
430
self.get_location_config('/')
431
self.assertEqual(None, self.my_config._get_section())
433
def test__get_section_exact(self):
434
self.get_location_config('http://www.example.com')
435
self.assertEqual('http://www.example.com',
436
self.my_config._get_section())
438
def test__get_section_suffix_does_not(self):
439
self.get_location_config('http://www.example.com-com')
440
self.assertEqual(None, self.my_config._get_section())
442
def test__get_section_subdir_recursive(self):
443
self.get_location_config('http://www.example.com/com')
444
self.assertEqual('http://www.example.com',
445
self.my_config._get_section())
447
def test__get_section_subdir_matches(self):
448
self.get_location_config('http://www.example.com/useglobal')
449
self.assertEqual('http://www.example.com/useglobal',
450
self.my_config._get_section())
452
def test__get_section_subdir_nonrecursive(self):
453
self.get_location_config(
454
'http://www.example.com/useglobal/childbranch')
455
self.assertEqual('http://www.example.com',
456
self.my_config._get_section())
458
def test__get_section_subdir_trailing_slash(self):
459
self.get_location_config('/b')
460
self.assertEqual('/b/', self.my_config._get_section())
462
def test__get_section_subdir_child(self):
463
self.get_location_config('/a/foo')
464
self.assertEqual('/a/*', self.my_config._get_section())
466
def test__get_section_subdir_child_child(self):
467
self.get_location_config('/a/foo/bar')
468
self.assertEqual('/a/', self.my_config._get_section())
470
def test__get_section_trailing_slash_with_children(self):
471
self.get_location_config('/a/')
472
self.assertEqual('/a/', self.my_config._get_section())
474
def test__get_section_explicit_over_glob(self):
475
self.get_location_config('/a/c')
476
self.assertEqual('/a/c', self.my_config._get_section())
616
def test__get_matching_sections_no_match(self):
617
self.get_branch_config('/')
618
self.assertEqual([], self.my_location_config._get_matching_sections())
620
def test__get_matching_sections_exact(self):
621
self.get_branch_config('http://www.example.com')
622
self.assertEqual([('http://www.example.com', '')],
623
self.my_location_config._get_matching_sections())
625
def test__get_matching_sections_suffix_does_not(self):
626
self.get_branch_config('http://www.example.com-com')
627
self.assertEqual([], self.my_location_config._get_matching_sections())
629
def test__get_matching_sections_subdir_recursive(self):
630
self.get_branch_config('http://www.example.com/com')
631
self.assertEqual([('http://www.example.com', 'com')],
632
self.my_location_config._get_matching_sections())
634
def test__get_matching_sections_ignoreparent(self):
635
self.get_branch_config('http://www.example.com/ignoreparent')
636
self.assertEqual([('http://www.example.com/ignoreparent', '')],
637
self.my_location_config._get_matching_sections())
639
def test__get_matching_sections_ignoreparent_subdir(self):
640
self.get_branch_config(
641
'http://www.example.com/ignoreparent/childbranch')
642
self.assertEqual([('http://www.example.com/ignoreparent',
644
self.my_location_config._get_matching_sections())
646
def test__get_matching_sections_subdir_trailing_slash(self):
647
self.get_branch_config('/b')
648
self.assertEqual([('/b/', '')],
649
self.my_location_config._get_matching_sections())
651
def test__get_matching_sections_subdir_child(self):
652
self.get_branch_config('/a/foo')
653
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
654
self.my_location_config._get_matching_sections())
656
def test__get_matching_sections_subdir_child_child(self):
657
self.get_branch_config('/a/foo/bar')
658
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
659
self.my_location_config._get_matching_sections())
661
def test__get_matching_sections_trailing_slash_with_children(self):
662
self.get_branch_config('/a/')
663
self.assertEqual([('/a/', '')],
664
self.my_location_config._get_matching_sections())
666
def test__get_matching_sections_explicit_over_glob(self):
667
# XXX: 2006-09-08 jamesh
668
# This test only passes because ord('c') > ord('*'). If there
669
# was a config section for '/a/?', it would get precedence
671
self.get_branch_config('/a/c')
672
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
673
self.my_location_config._get_matching_sections())
675
def test__get_option_policy_normal(self):
676
self.get_branch_config('http://www.example.com')
678
self.my_location_config._get_config_policy(
679
'http://www.example.com', 'normal_option'),
682
def test__get_option_policy_norecurse(self):
683
self.get_branch_config('http://www.example.com')
685
self.my_location_config._get_option_policy(
686
'http://www.example.com', 'norecurse_option'),
687
config.POLICY_NORECURSE)
688
# Test old recurse=False setting:
690
self.my_location_config._get_option_policy(
691
'http://www.example.com/norecurse', 'normal_option'),
692
config.POLICY_NORECURSE)
694
def test__get_option_policy_normal(self):
695
self.get_branch_config('http://www.example.com')
697
self.my_location_config._get_option_policy(
698
'http://www.example.com', 'appendpath_option'),
699
config.POLICY_APPENDPATH)
479
701
def test_location_without_username(self):
480
self.get_location_config('http://www.example.com/useglobal')
702
self.get_branch_config('http://www.example.com/ignoreparent')
481
703
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
482
704
self.my_config.username())
484
706
def test_location_not_listed(self):
485
707
"""Test that the global username is used when no location matches"""
486
self.get_location_config('/home/robertc/sources')
708
self.get_branch_config('/home/robertc/sources')
487
709
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
488
710
self.my_config.username())
490
712
def test_overriding_location(self):
491
self.get_location_config('http://www.example.com/foo')
713
self.get_branch_config('http://www.example.com/foo')
492
714
self.assertEqual('Robert Collins <robertc@example.org>',
493
715
self.my_config.username())
495
717
def test_signatures_not_set(self):
496
self.get_location_config('http://www.example.com',
718
self.get_branch_config('http://www.example.com',
497
719
global_config=sample_ignore_signatures)
498
self.assertEqual(config.CHECK_NEVER,
720
self.assertEqual(config.CHECK_ALWAYS,
499
721
self.my_config.signature_checking())
722
self.assertEqual(config.SIGN_NEVER,
723
self.my_config.signing_policy())
501
725
def test_signatures_never(self):
502
self.get_location_config('/a/c')
726
self.get_branch_config('/a/c')
503
727
self.assertEqual(config.CHECK_NEVER,
504
728
self.my_config.signature_checking())
506
730
def test_signatures_when_available(self):
507
self.get_location_config('/a/', global_config=sample_ignore_signatures)
731
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
508
732
self.assertEqual(config.CHECK_IF_POSSIBLE,
509
733
self.my_config.signature_checking())
511
735
def test_signatures_always(self):
512
self.get_location_config('/b')
736
self.get_branch_config('/b')
513
737
self.assertEqual(config.CHECK_ALWAYS,
514
738
self.my_config.signature_checking())
516
740
def test_gpg_signing_command(self):
517
self.get_location_config('/b')
741
self.get_branch_config('/b')
518
742
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
520
744
def test_gpg_signing_command_missing(self):
521
self.get_location_config('/a')
745
self.get_branch_config('/a')
522
746
self.assertEqual("false", self.my_config.gpg_signing_command())
524
748
def test_get_user_option_global(self):
525
self.get_location_config('/a')
749
self.get_branch_config('/a')
526
750
self.assertEqual('something',
527
751
self.my_config.get_user_option('user_global_option'))
529
753
def test_get_user_option_local(self):
530
self.get_location_config('/a')
754
self.get_branch_config('/a')
531
755
self.assertEqual('local',
532
756
self.my_config.get_user_option('user_local_option'))
758
def test_get_user_option_appendpath(self):
759
# returned as is for the base path:
760
self.get_branch_config('http://www.example.com')
761
self.assertEqual('append',
762
self.my_config.get_user_option('appendpath_option'))
763
# Extra path components get appended:
764
self.get_branch_config('http://www.example.com/a/b/c')
765
self.assertEqual('append/a/b/c',
766
self.my_config.get_user_option('appendpath_option'))
767
# Overriden for http://www.example.com/dir, where it is a
769
self.get_branch_config('http://www.example.com/dir/a/b/c')
770
self.assertEqual('normal',
771
self.my_config.get_user_option('appendpath_option'))
773
def test_get_user_option_norecurse(self):
774
self.get_branch_config('http://www.example.com')
775
self.assertEqual('norecurse',
776
self.my_config.get_user_option('norecurse_option'))
777
self.get_branch_config('http://www.example.com/dir')
778
self.assertEqual(None,
779
self.my_config.get_user_option('norecurse_option'))
780
# http://www.example.com/norecurse is a recurse=False section
781
# that redefines normal_option. Subdirectories do not pick up
783
self.get_branch_config('http://www.example.com/norecurse')
784
self.assertEqual('norecurse',
785
self.my_config.get_user_option('normal_option'))
786
self.get_branch_config('http://www.example.com/norecurse/subdir')
787
self.assertEqual('normal',
788
self.my_config.get_user_option('normal_option'))
790
def test_set_user_option_norecurse(self):
791
self.get_branch_config('http://www.example.com')
792
self.my_config.set_user_option('foo', 'bar',
793
store=config.STORE_LOCATION_NORECURSE)
795
self.my_location_config._get_option_policy(
796
'http://www.example.com', 'foo'),
797
config.POLICY_NORECURSE)
799
def test_set_user_option_appendpath(self):
800
self.get_branch_config('http://www.example.com')
801
self.my_config.set_user_option('foo', 'bar',
802
store=config.STORE_LOCATION_APPENDPATH)
804
self.my_location_config._get_option_policy(
805
'http://www.example.com', 'foo'),
806
config.POLICY_APPENDPATH)
808
def test_set_user_option_change_policy(self):
809
self.get_branch_config('http://www.example.com')
810
self.my_config.set_user_option('norecurse_option', 'normal',
811
store=config.STORE_LOCATION)
813
self.my_location_config._get_option_policy(
814
'http://www.example.com', 'norecurse_option'),
817
def test_set_user_option_recurse_false_section(self):
818
# The following section has recurse=False set. The test is to
819
# make sure that a normal option can be added to the section,
820
# converting recurse=False to the norecurse policy.
821
self.get_branch_config('http://www.example.com/norecurse')
822
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
823
'The section "http://www.example.com/norecurse" '
824
'has been converted to use policies.'],
825
self.my_config.set_user_option,
826
'foo', 'bar', store=config.STORE_LOCATION)
828
self.my_location_config._get_option_policy(
829
'http://www.example.com/norecurse', 'foo'),
831
# The previously existing option is still norecurse:
833
self.my_location_config._get_option_policy(
834
'http://www.example.com/norecurse', 'normal_option'),
835
config.POLICY_NORECURSE)
534
837
def test_post_commit_default(self):
535
self.get_location_config('/a/c')
838
self.get_branch_config('/a/c')
536
839
self.assertEqual('bzrlib.tests.test_config.post_commit',
537
840
self.my_config.post_commit())
539
def get_location_config(self, location, global_config=None):
842
def get_branch_config(self, location, global_config=None):
540
843
if global_config is None:
541
844
global_file = StringIO(sample_config_text.encode('utf-8'))
543
846
global_file = StringIO(global_config.encode('utf-8'))
544
847
branches_file = StringIO(sample_branches_text.encode('utf-8'))
545
self.my_config = config.LocationConfig(location)
546
self.my_config._get_parser(branches_file)
848
self.my_config = config.BranchConfig(FakeBranch(location))
849
# Force location config to use specified file
850
self.my_location_config = self.my_config._get_location_config()
851
self.my_location_config._get_parser(branches_file)
852
# Force global config to use specified file
547
853
self.my_config._get_global_config()._get_parser(global_file)
549
855
def test_set_user_setting_sets_and_saves(self):
550
self.get_location_config('/a/c')
856
self.get_branch_config('/a/c')
551
857
record = InstrumentedConfigObj("foo")
552
self.my_config._parser = record
858
self.my_location_config._parser = record
554
860
real_mkdir = os.mkdir
555
861
self.created = False
570
880
('__setitem__', '/a/c', {}),
571
881
('__getitem__', '/a/c'),
572
882
('__setitem__', 'foo', 'bar'),
883
('__getitem__', '/a/c'),
884
('as_bool', 'recurse'),
885
('__getitem__', '/a/c'),
886
('__delitem__', 'recurse'),
887
('__getitem__', '/a/c'),
889
('__getitem__', '/a/c'),
890
('__contains__', 'foo:policy'),
574
892
record._calls[1:])
577
class TestBranchConfigItems(TestCase):
894
def test_set_user_setting_sets_and_saves2(self):
895
self.get_branch_config('/a/c')
896
self.assertIs(self.my_config.get_user_option('foo'), None)
897
self.my_config.set_user_option('foo', 'bar')
899
self.my_config.branch.control_files.files['branch.conf'],
901
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
902
self.my_config.set_user_option('foo', 'baz',
903
store=config.STORE_LOCATION)
904
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
905
self.my_config.set_user_option('foo', 'qux')
906
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
908
def test_get_bzr_remote_path(self):
909
my_config = config.LocationConfig('/a/c')
910
self.assertEqual('bzr', my_config.get_bzr_remote_path())
911
my_config.set_user_option('bzr_remote_path', '/path-bzr')
912
self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
913
os.environ['BZR_REMOTE_PATH'] = '/environ-bzr'
914
self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
917
precedence_global = 'option = global'
918
precedence_branch = 'option = branch'
919
precedence_location = """
923
[http://example.com/specific]
928
class TestBranchConfigItems(tests.TestCaseInTempDir):
930
def get_branch_config(self, global_config=None, location=None,
931
location_config=None, branch_data_config=None):
932
my_config = config.BranchConfig(FakeBranch(location))
933
if global_config is not None:
934
global_file = StringIO(global_config.encode('utf-8'))
935
my_config._get_global_config()._get_parser(global_file)
936
self.my_location_config = my_config._get_location_config()
937
if location_config is not None:
938
location_file = StringIO(location_config.encode('utf-8'))
939
self.my_location_config._get_parser(location_file)
940
if branch_data_config is not None:
941
my_config.branch.control_files.files['branch.conf'] = \
579
945
def test_user_id(self):
580
branch = FakeBranch()
946
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
581
947
my_config = config.BranchConfig(branch)
582
948
self.assertEqual("Robert Collins <robertc@example.net>",
583
my_config._get_user_id())
949
my_config.username())
584
950
branch.control_files.email = "John"
585
self.assertEqual("John", my_config._get_user_id())
951
my_config.set_user_option('email',
952
"Robert Collins <robertc@example.org>")
953
self.assertEqual("John", my_config.username())
954
branch.control_files.email = None
955
self.assertEqual("Robert Collins <robertc@example.org>",
956
my_config.username())
587
958
def test_not_set_in_branch(self):
588
branch = FakeBranch()
589
my_config = config.BranchConfig(branch)
590
branch.control_files.email = None
591
config_file = StringIO(sample_config_text.encode('utf-8'))
592
(my_config._get_location_config().
593
_get_global_config()._get_parser(config_file))
959
my_config = self.get_branch_config(sample_config_text)
960
my_config.branch.control_files.email = None
594
961
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
595
962
my_config._get_user_id())
596
branch.control_files.email = "John"
963
my_config.branch.control_files.email = "John"
597
964
self.assertEqual("John", my_config._get_user_id())
599
def test_BZREMAIL_OVERRIDES(self):
600
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
966
def test_BZR_EMAIL_OVERRIDES(self):
967
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
601
968
branch = FakeBranch()
602
969
my_config = config.BranchConfig(branch)
603
970
self.assertEqual("Robert Collins <robertc@example.org>",
604
971
my_config.username())
606
973
def test_signatures_forced(self):
607
branch = FakeBranch()
608
my_config = config.BranchConfig(branch)
609
config_file = StringIO(sample_always_signatures)
610
(my_config._get_location_config().
611
_get_global_config()._get_parser(config_file))
612
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
974
my_config = self.get_branch_config(
975
global_config=sample_always_signatures)
976
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
977
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
978
self.assertTrue(my_config.signature_needed())
980
def test_signatures_forced_branch(self):
981
my_config = self.get_branch_config(
982
global_config=sample_ignore_signatures,
983
branch_data_config=sample_always_signatures)
984
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
985
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
986
self.assertTrue(my_config.signature_needed())
614
988
def test_gpg_signing_command(self):
615
branch = FakeBranch()
616
my_config = config.BranchConfig(branch)
989
my_config = self.get_branch_config(
990
# branch data cannot set gpg_signing_command
991
branch_data_config="gpg_signing_command=pgp")
617
992
config_file = StringIO(sample_config_text.encode('utf-8'))
618
(my_config._get_location_config().
619
_get_global_config()._get_parser(config_file))
993
my_config._get_global_config()._get_parser(config_file)
620
994
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
622
996
def test_get_user_option_global(self):
623
997
branch = FakeBranch()
624
998
my_config = config.BranchConfig(branch)
625
999
config_file = StringIO(sample_config_text.encode('utf-8'))
626
(my_config._get_location_config().
627
_get_global_config()._get_parser(config_file))
1000
(my_config._get_global_config()._get_parser(config_file))
628
1001
self.assertEqual('something',
629
1002
my_config.get_user_option('user_global_option'))
631
1004
def test_post_commit_default(self):
632
1005
branch = FakeBranch()
634
my_config = config.BranchConfig(branch)
635
config_file = StringIO(sample_config_text.encode('utf-8'))
636
(my_config._get_location_config().
637
_get_global_config()._get_parser(config_file))
638
branch_file = StringIO(sample_branches_text)
639
my_config._get_location_config()._get_parser(branch_file)
640
self.assertEqual('bzrlib.tests.test_config.post_commit',
641
my_config.post_commit())
644
class TestMailAddressExtraction(TestCase):
1006
my_config = self.get_branch_config(sample_config_text, '/a/c',
1007
sample_branches_text)
1008
self.assertEqual(my_config.branch.base, '/a/c')
1009
self.assertEqual('bzrlib.tests.test_config.post_commit',
1010
my_config.post_commit())
1011
my_config.set_user_option('post_commit', 'rmtree_root')
1012
# post-commit is ignored when bresent in branch data
1013
self.assertEqual('bzrlib.tests.test_config.post_commit',
1014
my_config.post_commit())
1015
my_config.set_user_option('post_commit', 'rmtree_root',
1016
store=config.STORE_LOCATION)
1017
self.assertEqual('rmtree_root', my_config.post_commit())
1019
def test_config_precedence(self):
1020
my_config = self.get_branch_config(global_config=precedence_global)
1021
self.assertEqual(my_config.get_user_option('option'), 'global')
1022
my_config = self.get_branch_config(global_config=precedence_global,
1023
branch_data_config=precedence_branch)
1024
self.assertEqual(my_config.get_user_option('option'), 'branch')
1025
my_config = self.get_branch_config(global_config=precedence_global,
1026
branch_data_config=precedence_branch,
1027
location_config=precedence_location)
1028
self.assertEqual(my_config.get_user_option('option'), 'recurse')
1029
my_config = self.get_branch_config(global_config=precedence_global,
1030
branch_data_config=precedence_branch,
1031
location_config=precedence_location,
1032
location='http://example.com/specific')
1033
self.assertEqual(my_config.get_user_option('option'), 'exact')
1035
def test_get_mail_client(self):
1036
config = self.get_branch_config()
1037
client = config.get_mail_client()
1038
self.assertIsInstance(client, mail_client.DefaultMail)
1041
config.set_user_option('mail_client', 'evolution')
1042
client = config.get_mail_client()
1043
self.assertIsInstance(client, mail_client.Evolution)
1045
config.set_user_option('mail_client', 'kmail')
1046
client = config.get_mail_client()
1047
self.assertIsInstance(client, mail_client.KMail)
1049
config.set_user_option('mail_client', 'mutt')
1050
client = config.get_mail_client()
1051
self.assertIsInstance(client, mail_client.Mutt)
1053
config.set_user_option('mail_client', 'thunderbird')
1054
client = config.get_mail_client()
1055
self.assertIsInstance(client, mail_client.Thunderbird)
1058
config.set_user_option('mail_client', 'default')
1059
client = config.get_mail_client()
1060
self.assertIsInstance(client, mail_client.DefaultMail)
1062
config.set_user_option('mail_client', 'editor')
1063
client = config.get_mail_client()
1064
self.assertIsInstance(client, mail_client.Editor)
1066
config.set_user_option('mail_client', 'mapi')
1067
client = config.get_mail_client()
1068
self.assertIsInstance(client, mail_client.MAPIClient)
1070
config.set_user_option('mail_client', 'xdg-email')
1071
client = config.get_mail_client()
1072
self.assertIsInstance(client, mail_client.XDGEmail)
1074
config.set_user_option('mail_client', 'firebird')
1075
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
1078
class TestMailAddressExtraction(tests.TestCase):
646
1080
def test_extract_email_address(self):
647
1081
self.assertEqual('jane@test.com',
648
1082
config.extract_email_address('Jane <jane@test.com>'))
649
self.assertRaises(errors.BzrError,
1083
self.assertRaises(errors.NoEmailInUsername,
650
1084
config.extract_email_address, 'Jane Tester')
1086
def test_parse_username(self):
1087
self.assertEqual(('', 'jdoe@example.com'),
1088
config.parse_username('jdoe@example.com'))
1089
self.assertEqual(('', 'jdoe@example.com'),
1090
config.parse_username('<jdoe@example.com>'))
1091
self.assertEqual(('John Doe', 'jdoe@example.com'),
1092
config.parse_username('John Doe <jdoe@example.com>'))
1093
self.assertEqual(('John Doe', ''),
1094
config.parse_username('John Doe'))
1095
self.assertEqual(('John Doe', 'jdoe@example.com'),
1096
config.parse_username('John Doe jdoe@example.com'))
1098
class TestTreeConfig(tests.TestCaseWithTransport):
1100
def test_get_value(self):
1101
"""Test that retreiving a value from a section is possible"""
1102
branch = self.make_branch('.')
1103
tree_config = config.TreeConfig(branch)
1104
tree_config.set_option('value', 'key', 'SECTION')
1105
tree_config.set_option('value2', 'key2')
1106
tree_config.set_option('value3-top', 'key3')
1107
tree_config.set_option('value3-section', 'key3', 'SECTION')
1108
value = tree_config.get_option('key', 'SECTION')
1109
self.assertEqual(value, 'value')
1110
value = tree_config.get_option('key2')
1111
self.assertEqual(value, 'value2')
1112
self.assertEqual(tree_config.get_option('non-existant'), None)
1113
value = tree_config.get_option('non-existant', 'SECTION')
1114
self.assertEqual(value, None)
1115
value = tree_config.get_option('non-existant', default='default')
1116
self.assertEqual(value, 'default')
1117
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1118
value = tree_config.get_option('key2', 'NOSECTION', default='default')
1119
self.assertEqual(value, 'default')
1120
value = tree_config.get_option('key3')
1121
self.assertEqual(value, 'value3-top')
1122
value = tree_config.get_option('key3', 'SECTION')
1123
self.assertEqual(value, 'value3-section')
1126
class TestAuthenticationConfigFile(tests.TestCase):
1127
"""Test the authentication.conf file matching"""
1129
def _got_user_passwd(self, expected_user, expected_password,
1130
config, *args, **kwargs):
1131
credentials = config.get_credentials(*args, **kwargs)
1132
if credentials is None:
1136
user = credentials['user']
1137
password = credentials['password']
1138
self.assertEquals(expected_user, user)
1139
self.assertEquals(expected_password, password)
1141
def test_empty_config(self):
1142
conf = config.AuthenticationConfig(_file=StringIO())
1143
self.assertEquals({}, conf._get_config())
1144
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1146
def test_broken_config(self):
1147
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
1148
self.assertRaises(errors.ParseConfigError, conf._get_config)
1150
conf = config.AuthenticationConfig(_file=StringIO(
1154
verify_certificates=askme # Error: Not a boolean
1156
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1157
conf = config.AuthenticationConfig(_file=StringIO(
1161
port=port # Error: Not an int
1163
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
1165
def test_credentials_for_scheme_host(self):
1166
conf = config.AuthenticationConfig(_file=StringIO(
1167
"""# Identity on foo.net
1172
password=secret-pass
1175
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
1177
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
1179
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
1181
def test_credentials_for_host_port(self):
1182
conf = config.AuthenticationConfig(_file=StringIO(
1183
"""# Identity on foo.net
1189
password=secret-pass
1192
self._got_user_passwd('joe', 'secret-pass',
1193
conf, 'ftp', 'foo.net', port=10021)
1195
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
1197
def test_for_matching_host(self):
1198
conf = config.AuthenticationConfig(_file=StringIO(
1199
"""# Identity on foo.net
1205
[sourceforge domain]
1212
self._got_user_passwd('georges', 'bendover',
1213
conf, 'bzr', 'foo.bzr.sf.net')
1215
self._got_user_passwd(None, None,
1216
conf, 'bzr', 'bbzr.sf.net')
1218
def test_for_matching_host_None(self):
1219
conf = config.AuthenticationConfig(_file=StringIO(
1220
"""# Identity on foo.net
1230
self._got_user_passwd('joe', 'joepass',
1231
conf, 'bzr', 'quux.net')
1232
# no host but different scheme
1233
self._got_user_passwd('georges', 'bendover',
1234
conf, 'ftp', 'quux.net')
1236
def test_credentials_for_path(self):
1237
conf = config.AuthenticationConfig(_file=StringIO(
1253
self._got_user_passwd(None, None,
1254
conf, 'http', host='bar.org', path='/dir3')
1256
self._got_user_passwd('georges', 'bendover',
1257
conf, 'http', host='bar.org', path='/dir2')
1259
self._got_user_passwd('jim', 'jimpass',
1260
conf, 'http', host='bar.org',path='/dir1/subdir')
1262
def test_credentials_for_user(self):
1263
conf = config.AuthenticationConfig(_file=StringIO(
1272
self._got_user_passwd('jim', 'jimpass',
1273
conf, 'http', 'bar.org')
1275
self._got_user_passwd('jim', 'jimpass',
1276
conf, 'http', 'bar.org', user='jim')
1277
# Don't get a different user if one is specified
1278
self._got_user_passwd(None, None,
1279
conf, 'http', 'bar.org', user='georges')
1281
def test_verify_certificates(self):
1282
conf = config.AuthenticationConfig(_file=StringIO(
1289
verify_certificates=False
1296
credentials = conf.get_credentials('https', 'bar.org')
1297
self.assertEquals(False, credentials.get('verify_certificates'))
1298
credentials = conf.get_credentials('https', 'foo.net')
1299
self.assertEquals(True, credentials.get('verify_certificates'))
1302
class TestAuthenticationConfig(tests.TestCase):
1303
"""Test AuthenticationConfig behaviour"""
1305
def _check_default_prompt(self, expected_prompt_format, scheme,
1306
host=None, port=None, realm=None, path=None):
1309
user, password = 'jim', 'precious'
1310
expected_prompt = expected_prompt_format % {
1311
'scheme': scheme, 'host': host, 'port': port,
1312
'user': user, 'realm': realm}
1314
stdout = tests.StringIOWrapper()
1315
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
1317
# We use an empty conf so that the user is always prompted
1318
conf = config.AuthenticationConfig()
1319
self.assertEquals(password,
1320
conf.get_password(scheme, host, user, port=port,
1321
realm=realm, path=path))
1322
self.assertEquals(stdout.getvalue(), expected_prompt)
1324
def test_default_prompts(self):
1325
# HTTP prompts can't be tested here, see test_http.py
1326
self._check_default_prompt('FTP %(user)s@%(host)s password: ', 'ftp')
1327
self._check_default_prompt('FTP %(user)s@%(host)s:%(port)d password: ',
1330
self._check_default_prompt('SSH %(user)s@%(host)s:%(port)d password: ',
1332
# SMTP port handling is a bit special (it's handled if embedded in the
1334
# FIXME: should we: forbid that, extend it to other schemes, leave
1335
# things as they are that's fine thank you ?
1336
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1338
self._check_default_prompt('SMTP %(user)s@%(host)s password: ',
1339
'smtp', host='bar.org:10025')
1340
self._check_default_prompt(
1341
'SMTP %(user)s@%(host)s:%(port)d password: ',
1345
# FIXME: Once we have a way to declare authentication to all test servers, we
1346
# can implement generic tests.
1347
# test_user_password_in_url
1348
# test_user_in_url_password_from_config
1349
# test_user_in_url_password_prompted
1350
# test_user_in_config
1351
# test_user_getpass.getuser
1352
# test_user_prompted ?
1353
class TestAuthenticationRing(tests.TestCaseWithTransport):