339
300
self.assertEqual(branch.base, location_config.location)
340
301
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,))
395
304
class TestGlobalConfigItems(TestCase):
417
326
config_file = StringIO(sample_always_signatures)
418
327
my_config = config.GlobalConfig()
419
328
my_config._parser = my_config._get_parser(file=config_file)
420
self.assertEqual(config.CHECK_NEVER,
329
self.assertEqual(config.CHECK_ALWAYS,
421
330
my_config.signature_checking())
422
self.assertEqual(config.SIGN_ALWAYS,
423
my_config.signing_policy())
424
331
self.assertEqual(True, my_config.signature_needed())
426
333
def test_signatures_if_possible(self):
427
334
config_file = StringIO(sample_maybe_signatures)
428
335
my_config = config.GlobalConfig()
429
336
my_config._parser = my_config._get_parser(file=config_file)
430
self.assertEqual(config.CHECK_NEVER,
337
self.assertEqual(config.CHECK_IF_POSSIBLE,
431
338
my_config.signature_checking())
432
self.assertEqual(config.SIGN_WHEN_REQUIRED,
433
my_config.signing_policy())
434
339
self.assertEqual(False, my_config.signature_needed())
436
341
def test_signatures_ignore(self):
437
342
config_file = StringIO(sample_ignore_signatures)
438
343
my_config = config.GlobalConfig()
439
344
my_config._parser = my_config._get_parser(file=config_file)
440
self.assertEqual(config.CHECK_ALWAYS,
345
self.assertEqual(config.CHECK_NEVER,
441
346
my_config.signature_checking())
442
self.assertEqual(config.SIGN_NEVER,
443
my_config.signing_policy())
444
347
self.assertEqual(False, my_config.signature_needed())
446
349
def _get_sample_config(self):
507
410
# replace the class that is constructured, to check its parameters
508
411
oldparserclass = config.ConfigObj
509
412
config.ConfigObj = InstrumentedConfigObj
413
my_config = config.LocationConfig('http://www.example.com')
511
my_config = config.LocationConfig('http://www.example.com')
512
415
parser = my_config._get_parser()
514
417
config.ConfigObj = oldparserclass
515
418
self.failUnless(isinstance(parser, InstrumentedConfigObj))
516
419
self.assertEqual(parser._calls,
517
[('__init__', config.locations_config_filename(),
420
[('__init__', config.branches_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
532
423
def test_get_global_config(self):
533
my_config = config.BranchConfig(FakeBranch('http://example.com'))
424
my_config = config.LocationConfig('http://example.com')
534
425
global_config = my_config._get_global_config()
535
426
self.failUnless(isinstance(global_config, config.GlobalConfig))
536
427
self.failUnless(global_config is my_config._get_global_config())
538
429
def test__get_section_no_match(self):
539
self.get_branch_config('/')
540
self.assertEqual(None, self.my_location_config._get_section())
430
self.get_location_config('/')
431
self.assertEqual(None, self.my_config._get_section())
542
433
def test__get_section_exact(self):
543
self.get_branch_config('http://www.example.com')
434
self.get_location_config('http://www.example.com')
544
435
self.assertEqual('http://www.example.com',
545
self.my_location_config._get_section())
436
self.my_config._get_section())
547
438
def test__get_section_suffix_does_not(self):
548
self.get_branch_config('http://www.example.com-com')
549
self.assertEqual(None, self.my_location_config._get_section())
439
self.get_location_config('http://www.example.com-com')
440
self.assertEqual(None, self.my_config._get_section())
551
442
def test__get_section_subdir_recursive(self):
552
self.get_branch_config('http://www.example.com/com')
443
self.get_location_config('http://www.example.com/com')
553
444
self.assertEqual('http://www.example.com',
554
self.my_location_config._get_section())
445
self.my_config._get_section())
556
447
def test__get_section_subdir_matches(self):
557
self.get_branch_config('http://www.example.com/useglobal')
448
self.get_location_config('http://www.example.com/useglobal')
558
449
self.assertEqual('http://www.example.com/useglobal',
559
self.my_location_config._get_section())
450
self.my_config._get_section())
561
452
def test__get_section_subdir_nonrecursive(self):
562
self.get_branch_config(
453
self.get_location_config(
563
454
'http://www.example.com/useglobal/childbranch')
564
455
self.assertEqual('http://www.example.com',
565
self.my_location_config._get_section())
456
self.my_config._get_section())
567
458
def test__get_section_subdir_trailing_slash(self):
568
self.get_branch_config('/b')
569
self.assertEqual('/b/', self.my_location_config._get_section())
459
self.get_location_config('/b')
460
self.assertEqual('/b/', self.my_config._get_section())
571
462
def test__get_section_subdir_child(self):
572
self.get_branch_config('/a/foo')
573
self.assertEqual('/a/*', self.my_location_config._get_section())
463
self.get_location_config('/a/foo')
464
self.assertEqual('/a/*', self.my_config._get_section())
575
466
def test__get_section_subdir_child_child(self):
576
self.get_branch_config('/a/foo/bar')
577
self.assertEqual('/a/', self.my_location_config._get_section())
467
self.get_location_config('/a/foo/bar')
468
self.assertEqual('/a/', self.my_config._get_section())
579
470
def test__get_section_trailing_slash_with_children(self):
580
self.get_branch_config('/a/')
581
self.assertEqual('/a/', self.my_location_config._get_section())
471
self.get_location_config('/a/')
472
self.assertEqual('/a/', self.my_config._get_section())
583
474
def test__get_section_explicit_over_glob(self):
584
self.get_branch_config('/a/c')
585
self.assertEqual('/a/c', self.my_location_config._get_section())
475
self.get_location_config('/a/c')
476
self.assertEqual('/a/c', self.my_config._get_section())
588
479
def test_location_without_username(self):
589
self.get_branch_config('http://www.example.com/useglobal')
480
self.get_location_config('http://www.example.com/useglobal')
590
481
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
591
482
self.my_config.username())
593
484
def test_location_not_listed(self):
594
485
"""Test that the global username is used when no location matches"""
595
self.get_branch_config('/home/robertc/sources')
486
self.get_location_config('/home/robertc/sources')
596
487
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
597
488
self.my_config.username())
599
490
def test_overriding_location(self):
600
self.get_branch_config('http://www.example.com/foo')
491
self.get_location_config('http://www.example.com/foo')
601
492
self.assertEqual('Robert Collins <robertc@example.org>',
602
493
self.my_config.username())
604
495
def test_signatures_not_set(self):
605
self.get_branch_config('http://www.example.com',
496
self.get_location_config('http://www.example.com',
606
497
global_config=sample_ignore_signatures)
607
self.assertEqual(config.CHECK_ALWAYS,
498
self.assertEqual(config.CHECK_NEVER,
608
499
self.my_config.signature_checking())
609
self.assertEqual(config.SIGN_NEVER,
610
self.my_config.signing_policy())
612
501
def test_signatures_never(self):
613
self.get_branch_config('/a/c')
502
self.get_location_config('/a/c')
614
503
self.assertEqual(config.CHECK_NEVER,
615
504
self.my_config.signature_checking())
617
506
def test_signatures_when_available(self):
618
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
507
self.get_location_config('/a/', global_config=sample_ignore_signatures)
619
508
self.assertEqual(config.CHECK_IF_POSSIBLE,
620
509
self.my_config.signature_checking())
622
511
def test_signatures_always(self):
623
self.get_branch_config('/b')
512
self.get_location_config('/b')
624
513
self.assertEqual(config.CHECK_ALWAYS,
625
514
self.my_config.signature_checking())
627
516
def test_gpg_signing_command(self):
628
self.get_branch_config('/b')
517
self.get_location_config('/b')
629
518
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
631
520
def test_gpg_signing_command_missing(self):
632
self.get_branch_config('/a')
521
self.get_location_config('/a')
633
522
self.assertEqual("false", self.my_config.gpg_signing_command())
635
524
def test_get_user_option_global(self):
636
self.get_branch_config('/a')
525
self.get_location_config('/a')
637
526
self.assertEqual('something',
638
527
self.my_config.get_user_option('user_global_option'))
640
529
def test_get_user_option_local(self):
641
self.get_branch_config('/a')
530
self.get_location_config('/a')
642
531
self.assertEqual('local',
643
532
self.my_config.get_user_option('user_local_option'))
645
534
def test_post_commit_default(self):
646
self.get_branch_config('/a/c')
535
self.get_location_config('/a/c')
647
536
self.assertEqual('bzrlib.tests.test_config.post_commit',
648
537
self.my_config.post_commit())
650
def get_branch_config(self, location, global_config=None):
539
def get_location_config(self, location, global_config=None):
651
540
if global_config is None:
652
541
global_file = StringIO(sample_config_text.encode('utf-8'))
654
543
global_file = StringIO(global_config.encode('utf-8'))
655
544
branches_file = StringIO(sample_branches_text.encode('utf-8'))
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
545
self.my_config = config.LocationConfig(location)
546
self.my_config._get_parser(branches_file)
661
547
self.my_config._get_global_config()._get_parser(global_file)
663
549
def test_set_user_setting_sets_and_saves(self):
664
self.get_branch_config('/a/c')
550
self.get_location_config('/a/c')
665
551
record = InstrumentedConfigObj("foo")
666
self.my_location_config._parser = record
552
self.my_config._parser = record
668
554
real_mkdir = os.mkdir
669
555
self.created = False
688
574
record._calls[1:])
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'] = \
577
class TestBranchConfigItems(TestCase):
732
579
def test_user_id(self):
733
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
580
branch = FakeBranch()
734
581
my_config = config.BranchConfig(branch)
735
582
self.assertEqual("Robert Collins <robertc@example.net>",
736
my_config.username())
583
my_config._get_user_id())
737
584
branch.control_files.email = "John"
738
my_config.set_user_option('email',
739
"Robert Collins <robertc@example.org>")
740
self.assertEqual("John", my_config.username())
585
self.assertEqual("John", my_config._get_user_id())
587
def test_not_set_in_branch(self):
588
branch = FakeBranch()
589
my_config = config.BranchConfig(branch)
741
590
branch.control_files.email = None
742
self.assertEqual("Robert Collins <robertc@example.org>",
743
my_config.username())
745
def test_not_set_in_branch(self):
746
my_config = self.get_branch_config(sample_config_text)
747
my_config.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))
748
594
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
749
595
my_config._get_user_id())
750
my_config.branch.control_files.email = "John"
596
branch.control_files.email = "John"
751
597
self.assertEqual("John", my_config._get_user_id())
753
def test_BZR_EMAIL_OVERRIDES(self):
754
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
599
def test_BZREMAIL_OVERRIDES(self):
600
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
755
601
branch = FakeBranch()
756
602
my_config = config.BranchConfig(branch)
757
603
self.assertEqual("Robert Collins <robertc@example.org>",
758
604
my_config.username())
760
606
def test_signatures_forced(self):
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())
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())
775
614
def test_gpg_signing_command(self):
776
my_config = self.get_branch_config(
777
# branch data cannot set gpg_signing_command
778
branch_data_config="gpg_signing_command=pgp")
615
branch = FakeBranch()
616
my_config = config.BranchConfig(branch)
779
617
config_file = StringIO(sample_config_text.encode('utf-8'))
780
my_config._get_global_config()._get_parser(config_file)
618
(my_config._get_location_config().
619
_get_global_config()._get_parser(config_file))
781
620
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
783
622
def test_get_user_option_global(self):
784
623
branch = FakeBranch()
785
624
my_config = config.BranchConfig(branch)
786
625
config_file = StringIO(sample_config_text.encode('utf-8'))
787
(my_config._get_global_config()._get_parser(config_file))
626
(my_config._get_location_config().
627
_get_global_config()._get_parser(config_file))
788
628
self.assertEqual('something',
789
629
my_config.get_user_option('user_global_option'))
791
631
def test_post_commit_default(self):
792
632
branch = FakeBranch()
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')
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())
822
644
class TestMailAddressExtraction(TestCase):