300
339
self.assertEqual(branch.base, location_config.location)
301
340
self.failUnless(location_config is my_config._get_location_config())
342
def test_get_config(self):
343
"""The Branch.get_config method works properly"""
344
b = BzrDir.create_standalone_workingtree('.').branch
345
my_config = b.get_config()
346
self.assertIs(my_config.get_user_option('wacky'), None)
347
my_config.set_user_option('wacky', 'unlikely')
348
self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
350
# Ensure we get the same thing if we start again
351
b2 = Branch.open('.')
352
my_config2 = b2.get_config()
353
self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
355
def test_has_explicit_nickname(self):
356
b = self.make_branch('.')
357
self.assertFalse(b.get_config().has_explicit_nickname())
359
self.assertTrue(b.get_config().has_explicit_nickname())
361
def test_config_url(self):
362
"""The Branch.get_config will use section that uses a local url"""
363
branch = self.make_branch('branch')
364
self.assertEqual('branch', branch.nick)
366
locations = config.locations_config_filename()
367
config.ensure_config_dir_exists()
368
local_url = urlutils.local_path_to_url('branch')
369
open(locations, 'wb').write('[%s]\nnickname = foobar'
371
self.assertEqual('foobar', branch.nick)
373
def test_config_local_path(self):
374
"""The Branch.get_config will use a local system path"""
375
branch = self.make_branch('branch')
376
self.assertEqual('branch', branch.nick)
378
locations = config.locations_config_filename()
379
config.ensure_config_dir_exists()
380
open(locations, 'wb').write('[%s/branch]\nnickname = barry'
381
% (osutils.getcwd().encode('utf8'),))
382
self.assertEqual('barry', branch.nick)
384
def test_config_creates_local(self):
385
"""Creating a new entry in config uses a local path."""
386
branch = self.make_branch('branch')
387
branch.set_push_location('http://foobar')
388
locations = config.locations_config_filename()
389
local_path = osutils.getcwd().encode('utf8')
390
# Surprisingly ConfigObj doesn't create a trailing newline
391
self.check_file_contents(locations,
392
'[%s/branch]\npush_location = http://foobar' % (local_path,))
304
395
class TestGlobalConfigItems(TestCase):
409
507
# replace the class that is constructured, to check its parameters
410
508
oldparserclass = config.ConfigObj
411
509
config.ConfigObj = InstrumentedConfigObj
412
my_config = config.LocationConfig('http://www.example.com')
511
my_config = config.LocationConfig('http://www.example.com')
414
512
parser = my_config._get_parser()
416
514
config.ConfigObj = oldparserclass
417
515
self.failUnless(isinstance(parser, InstrumentedConfigObj))
418
516
self.assertEqual(parser._calls,
419
[('__init__', config.branches_config_filename())])
517
[('__init__', config.locations_config_filename(),
519
config.ensure_config_dir_exists()
520
#os.mkdir(config.config_dir())
521
f = file(config.branches_config_filename(), 'wb')
524
oldparserclass = config.ConfigObj
525
config.ConfigObj = InstrumentedConfigObj
527
my_config = config.LocationConfig('http://www.example.com')
528
parser = my_config._get_parser()
530
config.ConfigObj = oldparserclass
421
532
def test_get_global_config(self):
422
my_config = config.LocationConfig('http://example.com')
533
my_config = config.BranchConfig(FakeBranch('http://example.com'))
423
534
global_config = my_config._get_global_config()
424
535
self.failUnless(isinstance(global_config, config.GlobalConfig))
425
536
self.failUnless(global_config is my_config._get_global_config())
427
538
def test__get_section_no_match(self):
428
self.get_location_config('/')
429
self.assertEqual(None, self.my_config._get_section())
539
self.get_branch_config('/')
540
self.assertEqual(None, self.my_location_config._get_section())
431
542
def test__get_section_exact(self):
432
self.get_location_config('http://www.example.com')
543
self.get_branch_config('http://www.example.com')
433
544
self.assertEqual('http://www.example.com',
434
self.my_config._get_section())
545
self.my_location_config._get_section())
436
547
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())
548
self.get_branch_config('http://www.example.com-com')
549
self.assertEqual(None, self.my_location_config._get_section())
440
551
def test__get_section_subdir_recursive(self):
441
self.get_location_config('http://www.example.com/com')
552
self.get_branch_config('http://www.example.com/com')
442
553
self.assertEqual('http://www.example.com',
443
self.my_config._get_section())
554
self.my_location_config._get_section())
445
556
def test__get_section_subdir_matches(self):
446
self.get_location_config('http://www.example.com/useglobal')
557
self.get_branch_config('http://www.example.com/useglobal')
447
558
self.assertEqual('http://www.example.com/useglobal',
448
self.my_config._get_section())
559
self.my_location_config._get_section())
450
561
def test__get_section_subdir_nonrecursive(self):
451
self.get_location_config(
562
self.get_branch_config(
452
563
'http://www.example.com/useglobal/childbranch')
453
564
self.assertEqual('http://www.example.com',
454
self.my_config._get_section())
565
self.my_location_config._get_section())
456
567
def test__get_section_subdir_trailing_slash(self):
457
self.get_location_config('/b')
458
self.assertEqual('/b/', self.my_config._get_section())
568
self.get_branch_config('/b')
569
self.assertEqual('/b/', self.my_location_config._get_section())
460
571
def test__get_section_subdir_child(self):
461
self.get_location_config('/a/foo')
462
self.assertEqual('/a/*', self.my_config._get_section())
572
self.get_branch_config('/a/foo')
573
self.assertEqual('/a/*', self.my_location_config._get_section())
464
575
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())
576
self.get_branch_config('/a/foo/bar')
577
self.assertEqual('/a/', self.my_location_config._get_section())
468
579
def test__get_section_trailing_slash_with_children(self):
469
self.get_location_config('/a/')
470
self.assertEqual('/a/', self.my_config._get_section())
580
self.get_branch_config('/a/')
581
self.assertEqual('/a/', self.my_location_config._get_section())
472
583
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())
584
self.get_branch_config('/a/c')
585
self.assertEqual('/a/c', self.my_location_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)
486
588
def test_location_without_username(self):
487
self.get_location_config('http://www.example.com/useglobal')
488
self.assertEqual('Robert Collins <robertc@example.com>',
589
self.get_branch_config('http://www.example.com/useglobal')
590
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
489
591
self.my_config.username())
491
593
def test_location_not_listed(self):
492
self.get_location_config('/home/robertc/sources')
493
self.assertEqual('Robert Collins <robertc@example.com>',
594
"""Test that the global username is used when no location matches"""
595
self.get_branch_config('/home/robertc/sources')
596
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
494
597
self.my_config.username())
496
599
def test_overriding_location(self):
497
self.get_location_config('http://www.example.com/foo')
600
self.get_branch_config('http://www.example.com/foo')
498
601
self.assertEqual('Robert Collins <robertc@example.org>',
499
602
self.my_config.username())
501
604
def test_signatures_not_set(self):
502
self.get_location_config('http://www.example.com',
605
self.get_branch_config('http://www.example.com',
503
606
global_config=sample_ignore_signatures)
504
self.assertEqual(config.CHECK_NEVER,
607
self.assertEqual(config.CHECK_ALWAYS,
505
608
self.my_config.signature_checking())
609
self.assertEqual(config.SIGN_NEVER,
610
self.my_config.signing_policy())
507
612
def test_signatures_never(self):
508
self.get_location_config('/a/c')
613
self.get_branch_config('/a/c')
509
614
self.assertEqual(config.CHECK_NEVER,
510
615
self.my_config.signature_checking())
512
617
def test_signatures_when_available(self):
513
self.get_location_config('/a/', global_config=sample_ignore_signatures)
618
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
514
619
self.assertEqual(config.CHECK_IF_POSSIBLE,
515
620
self.my_config.signature_checking())
517
622
def test_signatures_always(self):
518
self.get_location_config('/b')
623
self.get_branch_config('/b')
519
624
self.assertEqual(config.CHECK_ALWAYS,
520
625
self.my_config.signature_checking())
522
627
def test_gpg_signing_command(self):
523
self.get_location_config('/b')
628
self.get_branch_config('/b')
524
629
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
526
631
def test_gpg_signing_command_missing(self):
527
self.get_location_config('/a')
632
self.get_branch_config('/a')
528
633
self.assertEqual("false", self.my_config.gpg_signing_command())
530
635
def test_get_user_option_global(self):
531
self.get_location_config('/a')
636
self.get_branch_config('/a')
532
637
self.assertEqual('something',
533
638
self.my_config.get_user_option('user_global_option'))
535
640
def test_get_user_option_local(self):
536
self.get_location_config('/a')
641
self.get_branch_config('/a')
537
642
self.assertEqual('local',
538
643
self.my_config.get_user_option('user_local_option'))
540
645
def test_post_commit_default(self):
541
self.get_location_config('/a/c')
646
self.get_branch_config('/a/c')
542
647
self.assertEqual('bzrlib.tests.test_config.post_commit',
543
648
self.my_config.post_commit())
546
class TestLocationConfig(TestCaseInTempDir):
548
def get_location_config(self, location, global_config=None):
650
def get_branch_config(self, location, global_config=None):
549
651
if global_config is None:
550
652
global_file = StringIO(sample_config_text.encode('utf-8'))
552
654
global_file = StringIO(global_config.encode('utf-8'))
553
655
branches_file = StringIO(sample_branches_text.encode('utf-8'))
554
self.my_config = config.LocationConfig(location)
555
self.my_config._get_parser(branches_file)
656
self.my_config = config.BranchConfig(FakeBranch(location))
657
# Force location config to use specified file
658
self.my_location_config = self.my_config._get_location_config()
659
self.my_location_config._get_parser(branches_file)
660
# Force global config to use specified file
556
661
self.my_config._get_global_config()._get_parser(global_file)
558
663
def test_set_user_setting_sets_and_saves(self):
559
self.get_location_config('/a/c')
664
self.get_branch_config('/a/c')
560
665
record = InstrumentedConfigObj("foo")
561
self.my_config._parser = record
666
self.my_location_config._parser = record
563
668
real_mkdir = os.mkdir
564
669
self.created = False
583
688
record._calls[1:])
586
class TestBranchConfigItems(TestCase):
690
def test_set_user_setting_sets_and_saves2(self):
691
self.get_branch_config('/a/c')
692
self.assertIs(self.my_config.get_user_option('foo'), None)
693
self.my_config.set_user_option('foo', 'bar')
695
self.my_config.branch.control_files.files['branch.conf'],
697
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
698
self.my_config.set_user_option('foo', 'baz', local=True)
699
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
700
self.my_config.set_user_option('foo', 'qux')
701
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
704
precedence_global = 'option = global'
705
precedence_branch = 'option = branch'
706
precedence_location = """
710
[http://example.com/specific]
715
class TestBranchConfigItems(TestCaseInTempDir):
717
def get_branch_config(self, global_config=None, location=None,
718
location_config=None, branch_data_config=None):
719
my_config = config.BranchConfig(FakeBranch(location))
720
if global_config is not None:
721
global_file = StringIO(global_config.encode('utf-8'))
722
my_config._get_global_config()._get_parser(global_file)
723
self.my_location_config = my_config._get_location_config()
724
if location_config is not None:
725
location_file = StringIO(location_config.encode('utf-8'))
726
self.my_location_config._get_parser(location_file)
727
if branch_data_config is not None:
728
my_config.branch.control_files.files['branch.conf'] = \
588
732
def test_user_id(self):
589
branch = FakeBranch()
733
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
590
734
my_config = config.BranchConfig(branch)
591
735
self.assertEqual("Robert Collins <robertc@example.net>",
592
my_config._get_user_id())
736
my_config.username())
593
737
branch.control_files.email = "John"
594
self.assertEqual("John", my_config._get_user_id())
738
my_config.set_user_option('email',
739
"Robert Collins <robertc@example.org>")
740
self.assertEqual("John", my_config.username())
741
branch.control_files.email = None
742
self.assertEqual("Robert Collins <robertc@example.org>",
743
my_config.username())
596
745
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))
746
my_config = self.get_branch_config(sample_config_text)
747
my_config.branch.control_files.email = None
603
748
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
604
749
my_config._get_user_id())
605
branch.control_files.email = "John"
750
my_config.branch.control_files.email = "John"
606
751
self.assertEqual("John", my_config._get_user_id())
608
def test_BZREMAIL_OVERRIDES(self):
609
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
753
def test_BZR_EMAIL_OVERRIDES(self):
754
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
610
755
branch = FakeBranch()
611
756
my_config = config.BranchConfig(branch)
612
757
self.assertEqual("Robert Collins <robertc@example.org>",
613
758
my_config.username())
615
760
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())
761
my_config = self.get_branch_config(
762
global_config=sample_always_signatures)
763
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
764
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
765
self.assertTrue(my_config.signature_needed())
767
def test_signatures_forced_branch(self):
768
my_config = self.get_branch_config(
769
global_config=sample_ignore_signatures,
770
branch_data_config=sample_always_signatures)
771
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
772
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
773
self.assertTrue(my_config.signature_needed())
623
775
def test_gpg_signing_command(self):
624
branch = FakeBranch()
625
my_config = config.BranchConfig(branch)
776
my_config = self.get_branch_config(
777
# branch data cannot set gpg_signing_command
778
branch_data_config="gpg_signing_command=pgp")
626
779
config_file = StringIO(sample_config_text.encode('utf-8'))
627
(my_config._get_location_config().
628
_get_global_config()._get_parser(config_file))
780
my_config._get_global_config()._get_parser(config_file)
629
781
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
631
783
def test_get_user_option_global(self):
632
784
branch = FakeBranch()
633
785
my_config = config.BranchConfig(branch)
634
786
config_file = StringIO(sample_config_text.encode('utf-8'))
635
(my_config._get_location_config().
636
_get_global_config()._get_parser(config_file))
787
(my_config._get_global_config()._get_parser(config_file))
637
788
self.assertEqual('something',
638
789
my_config.get_user_option('user_global_option'))
640
791
def test_post_commit_default(self):
641
792
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())
793
my_config = self.get_branch_config(sample_config_text, '/a/c',
794
sample_branches_text)
795
self.assertEqual(my_config.branch.base, '/a/c')
796
self.assertEqual('bzrlib.tests.test_config.post_commit',
797
my_config.post_commit())
798
my_config.set_user_option('post_commit', 'rmtree_root')
799
# post-commit is ignored when bresent in branch data
800
self.assertEqual('bzrlib.tests.test_config.post_commit',
801
my_config.post_commit())
802
my_config.set_user_option('post_commit', 'rmtree_root', local=True)
803
self.assertEqual('rmtree_root', my_config.post_commit())
805
def test_config_precedence(self):
806
my_config = self.get_branch_config(global_config=precedence_global)
807
self.assertEqual(my_config.get_user_option('option'), 'global')
808
my_config = self.get_branch_config(global_config=precedence_global,
809
branch_data_config=precedence_branch)
810
self.assertEqual(my_config.get_user_option('option'), 'branch')
811
my_config = self.get_branch_config(global_config=precedence_global,
812
branch_data_config=precedence_branch,
813
location_config=precedence_location)
814
self.assertEqual(my_config.get_user_option('option'), 'recurse')
815
my_config = self.get_branch_config(global_config=precedence_global,
816
branch_data_config=precedence_branch,
817
location_config=precedence_location,
818
location='http://example.com/specific')
819
self.assertEqual(my_config.get_user_option('option'), 'exact')
653
822
class TestMailAddressExtraction(TestCase):