303
474
my_config = self._get_sample_config()
304
475
self.assertEqual("something",
305
476
my_config.get_user_option('user_global_option'))
308
class TestLocationConfig(TestCase):
478
def test_post_commit_default(self):
479
my_config = self._get_sample_config()
480
self.assertEqual(None, my_config.post_commit())
482
def test_configured_logformat(self):
483
my_config = self._get_sample_config()
484
self.assertEqual("short", my_config.log_format())
486
def test_get_alias(self):
487
my_config = self._get_sample_config()
488
self.assertEqual('help', my_config.get_alias('h'))
490
def test_get_no_alias(self):
491
my_config = self._get_sample_config()
492
self.assertEqual(None, my_config.get_alias('foo'))
494
def test_get_long_alias(self):
495
my_config = self._get_sample_config()
496
self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
499
class TestLocationConfig(TestCaseInTempDir):
310
501
def test_constructs(self):
311
502
my_config = config.LocationConfig('http://example.com')
312
503
self.assertRaises(TypeError, config.LocationConfig)
314
505
def test_branch_calls_read_filenames(self):
506
# This is testing the correct file names are provided.
507
# TODO: consolidate with the test for GlobalConfigs filename checks.
315
509
# replace the class that is constructured, to check its parameters
316
oldparserclass = config.ConfigParser
317
config.ConfigParser = InstrumentedConfigParser
318
my_config = config.LocationConfig('http://www.example.com')
320
parser = my_config._get_parser()
322
config.ConfigParser = oldparserclass
323
self.failUnless(isinstance(parser, InstrumentedConfigParser))
324
self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
510
oldparserclass = config.ConfigObj
511
config.ConfigObj = InstrumentedConfigObj
513
my_config = config.LocationConfig('http://www.example.com')
514
parser = my_config._get_parser()
516
config.ConfigObj = oldparserclass
517
self.failUnless(isinstance(parser, InstrumentedConfigObj))
518
self.assertEqual(parser._calls,
519
[('__init__', config.locations_config_filename(),
521
config.ensure_config_dir_exists()
522
#os.mkdir(config.config_dir())
523
f = file(config.branches_config_filename(), 'wb')
526
oldparserclass = config.ConfigObj
527
config.ConfigObj = InstrumentedConfigObj
529
my_config = config.LocationConfig('http://www.example.com')
530
parser = my_config._get_parser()
532
config.ConfigObj = oldparserclass
326
534
def test_get_global_config(self):
327
my_config = config.LocationConfig('http://example.com')
535
my_config = config.BranchConfig(FakeBranch('http://example.com'))
328
536
global_config = my_config._get_global_config()
329
537
self.failUnless(isinstance(global_config, config.GlobalConfig))
330
538
self.failUnless(global_config is my_config._get_global_config())
332
def test__get_section_no_match(self):
333
self.get_location_config('/')
334
self.assertEqual(None, self.my_config._get_section())
540
def test__get_matching_sections_no_match(self):
541
self.get_branch_config('/')
542
self.assertEqual([], self.my_location_config._get_matching_sections())
336
def test__get_section_exact(self):
337
self.get_location_config('http://www.example.com')
338
self.assertEqual('http://www.example.com',
339
self.my_config._get_section())
544
def test__get_matching_sections_exact(self):
545
self.get_branch_config('http://www.example.com')
546
self.assertEqual([('http://www.example.com', '')],
547
self.my_location_config._get_matching_sections())
341
def test__get_section_suffix_does_not(self):
342
self.get_location_config('http://www.example.com-com')
343
self.assertEqual(None, self.my_config._get_section())
345
def test__get_section_subdir_recursive(self):
346
self.get_location_config('http://www.example.com/com')
347
self.assertEqual('http://www.example.com',
348
self.my_config._get_section())
350
def test__get_section_subdir_matches(self):
351
self.get_location_config('http://www.example.com/useglobal')
352
self.assertEqual('http://www.example.com/useglobal',
353
self.my_config._get_section())
355
def test__get_section_subdir_nonrecursive(self):
356
self.get_location_config(
357
'http://www.example.com/useglobal/childbranch')
358
self.assertEqual('http://www.example.com',
359
self.my_config._get_section())
361
def test__get_section_subdir_trailing_slash(self):
362
self.get_location_config('/b')
363
self.assertEqual('/b/', self.my_config._get_section())
365
def test__get_section_subdir_child(self):
366
self.get_location_config('/a/foo')
367
self.assertEqual('/a/*', self.my_config._get_section())
369
def test__get_section_subdir_child_child(self):
370
self.get_location_config('/a/foo/bar')
371
self.assertEqual('/a/', self.my_config._get_section())
373
def test__get_section_trailing_slash_with_children(self):
374
self.get_location_config('/a/')
375
self.assertEqual('/a/', self.my_config._get_section())
377
def test__get_section_explicit_over_glob(self):
378
self.get_location_config('/a/c')
379
self.assertEqual('/a/c', self.my_config._get_section())
381
def get_location_config(self, location, global_config=None):
382
if global_config is None:
383
global_file = StringIO(sample_config_text)
385
global_file = StringIO(global_config)
386
branches_file = StringIO(sample_branches_text)
387
self.my_config = config.LocationConfig(location)
388
self.my_config._get_parser(branches_file)
389
self.my_config._get_global_config()._get_parser(global_file)
549
def test__get_matching_sections_suffix_does_not(self):
550
self.get_branch_config('http://www.example.com-com')
551
self.assertEqual([], self.my_location_config._get_matching_sections())
553
def test__get_matching_sections_subdir_recursive(self):
554
self.get_branch_config('http://www.example.com/com')
555
self.assertEqual([('http://www.example.com', 'com')],
556
self.my_location_config._get_matching_sections())
558
def test__get_matching_sections_ignoreparent(self):
559
self.get_branch_config('http://www.example.com/ignoreparent')
560
self.assertEqual([('http://www.example.com/ignoreparent', '')],
561
self.my_location_config._get_matching_sections())
563
def test__get_matching_sections_ignoreparent_subdir(self):
564
self.get_branch_config(
565
'http://www.example.com/ignoreparent/childbranch')
566
self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
567
self.my_location_config._get_matching_sections())
569
def test__get_matching_sections_norecurse(self):
570
self.get_branch_config('http://www.example.com/norecurse')
571
self.assertEqual([('http://www.example.com/norecurse', ''),
572
('http://www.example.com', 'norecurse')],
573
self.my_location_config._get_matching_sections())
575
def test__get_matching_sections_norecurse_subdir(self):
576
self.get_branch_config(
577
'http://www.example.com/norecurse/childbranch')
578
self.assertEqual([('http://www.example.com', 'norecurse/childbranch')],
579
self.my_location_config._get_matching_sections())
581
def test__get_matching_sections_subdir_trailing_slash(self):
582
self.get_branch_config('/b')
583
self.assertEqual([('/b/', '')],
584
self.my_location_config._get_matching_sections())
586
def test__get_matching_sections_subdir_child(self):
587
self.get_branch_config('/a/foo')
588
self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
589
self.my_location_config._get_matching_sections())
591
def test__get_matching_sections_subdir_child_child(self):
592
self.get_branch_config('/a/foo/bar')
593
self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
594
self.my_location_config._get_matching_sections())
596
def test__get_matching_sections_trailing_slash_with_children(self):
597
self.get_branch_config('/a/')
598
self.assertEqual([('/a/', '')],
599
self.my_location_config._get_matching_sections())
601
def test__get_matching_sections_explicit_over_glob(self):
602
# XXX: 2006-09-08 jamesh
603
# This test only passes because ord('c') > ord('*'). If there
604
# was a config section for '/a/?', it would get precedence
606
self.get_branch_config('/a/c')
607
self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
608
self.my_location_config._get_matching_sections())
391
610
def test_location_without_username(self):
392
self.get_location_config('http://www.example.com/useglobal')
393
self.assertEqual('Robert Collins <robertc@example.com>',
611
self.get_branch_config('http://www.example.com/ignoreparent')
612
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
394
613
self.my_config.username())
396
615
def test_location_not_listed(self):
397
self.get_location_config('/home/robertc/sources')
398
self.assertEqual('Robert Collins <robertc@example.com>',
616
"""Test that the global username is used when no location matches"""
617
self.get_branch_config('/home/robertc/sources')
618
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
399
619
self.my_config.username())
401
621
def test_overriding_location(self):
402
self.get_location_config('http://www.example.com/foo')
622
self.get_branch_config('http://www.example.com/foo')
403
623
self.assertEqual('Robert Collins <robertc@example.org>',
404
624
self.my_config.username())
406
626
def test_signatures_not_set(self):
407
self.get_location_config('http://www.example.com',
627
self.get_branch_config('http://www.example.com',
408
628
global_config=sample_ignore_signatures)
409
self.assertEqual(config.CHECK_NEVER,
629
self.assertEqual(config.CHECK_ALWAYS,
410
630
self.my_config.signature_checking())
631
self.assertEqual(config.SIGN_NEVER,
632
self.my_config.signing_policy())
412
634
def test_signatures_never(self):
413
self.get_location_config('/a/c')
635
self.get_branch_config('/a/c')
414
636
self.assertEqual(config.CHECK_NEVER,
415
637
self.my_config.signature_checking())
417
639
def test_signatures_when_available(self):
418
self.get_location_config('/a/', global_config=sample_ignore_signatures)
640
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
419
641
self.assertEqual(config.CHECK_IF_POSSIBLE,
420
642
self.my_config.signature_checking())
422
644
def test_signatures_always(self):
423
self.get_location_config('/b')
645
self.get_branch_config('/b')
424
646
self.assertEqual(config.CHECK_ALWAYS,
425
647
self.my_config.signature_checking())
427
649
def test_gpg_signing_command(self):
428
self.get_location_config('/b')
650
self.get_branch_config('/b')
429
651
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
431
653
def test_gpg_signing_command_missing(self):
432
self.get_location_config('/a')
654
self.get_branch_config('/a')
433
655
self.assertEqual("false", self.my_config.gpg_signing_command())
435
657
def test_get_user_option_global(self):
436
self.get_location_config('/a')
658
self.get_branch_config('/a')
437
659
self.assertEqual('something',
438
660
self.my_config.get_user_option('user_global_option'))
440
662
def test_get_user_option_local(self):
441
self.get_location_config('/a')
663
self.get_branch_config('/a')
442
664
self.assertEqual('local',
443
665
self.my_config.get_user_option('user_local_option'))
446
class TestBranchConfigItems(TestCase):
667
def test_post_commit_default(self):
668
self.get_branch_config('/a/c')
669
self.assertEqual('bzrlib.tests.test_config.post_commit',
670
self.my_config.post_commit())
672
def get_branch_config(self, location, global_config=None):
673
if global_config is None:
674
global_file = StringIO(sample_config_text.encode('utf-8'))
676
global_file = StringIO(global_config.encode('utf-8'))
677
branches_file = StringIO(sample_branches_text.encode('utf-8'))
678
self.my_config = config.BranchConfig(FakeBranch(location))
679
# Force location config to use specified file
680
self.my_location_config = self.my_config._get_location_config()
681
self.my_location_config._get_parser(branches_file)
682
# Force global config to use specified file
683
self.my_config._get_global_config()._get_parser(global_file)
685
def test_set_user_setting_sets_and_saves(self):
686
self.get_branch_config('/a/c')
687
record = InstrumentedConfigObj("foo")
688
self.my_location_config._parser = record
690
real_mkdir = os.mkdir
692
def checked_mkdir(path, mode=0777):
693
self.log('making directory: %s', path)
694
real_mkdir(path, mode)
697
os.mkdir = checked_mkdir
699
self.my_config.set_user_option('foo', 'bar', local=True)
701
os.mkdir = real_mkdir
703
self.failUnless(self.created, 'Failed to create ~/.bazaar')
704
self.assertEqual([('__contains__', '/a/c'),
705
('__contains__', '/a/c/'),
706
('__setitem__', '/a/c', {}),
707
('__getitem__', '/a/c'),
708
('__setitem__', 'foo', 'bar'),
712
def test_set_user_setting_sets_and_saves2(self):
713
self.get_branch_config('/a/c')
714
self.assertIs(self.my_config.get_user_option('foo'), None)
715
self.my_config.set_user_option('foo', 'bar')
717
self.my_config.branch.control_files.files['branch.conf'],
719
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
720
self.my_config.set_user_option('foo', 'baz', local=True)
721
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
722
self.my_config.set_user_option('foo', 'qux')
723
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
726
precedence_global = 'option = global'
727
precedence_branch = 'option = branch'
728
precedence_location = """
732
[http://example.com/specific]
737
class TestBranchConfigItems(TestCaseInTempDir):
739
def get_branch_config(self, global_config=None, location=None,
740
location_config=None, branch_data_config=None):
741
my_config = config.BranchConfig(FakeBranch(location))
742
if global_config is not None:
743
global_file = StringIO(global_config.encode('utf-8'))
744
my_config._get_global_config()._get_parser(global_file)
745
self.my_location_config = my_config._get_location_config()
746
if location_config is not None:
747
location_file = StringIO(location_config.encode('utf-8'))
748
self.my_location_config._get_parser(location_file)
749
if branch_data_config is not None:
750
my_config.branch.control_files.files['branch.conf'] = \
448
754
def test_user_id(self):
449
branch = FakeBranch()
755
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
450
756
my_config = config.BranchConfig(branch)
451
757
self.assertEqual("Robert Collins <robertc@example.net>",
452
my_config._get_user_id())
453
branch.email = "John"
454
self.assertEqual("John", my_config._get_user_id())
758
my_config.username())
759
branch.control_files.email = "John"
760
my_config.set_user_option('email',
761
"Robert Collins <robertc@example.org>")
762
self.assertEqual("John", my_config.username())
763
branch.control_files.email = None
764
self.assertEqual("Robert Collins <robertc@example.org>",
765
my_config.username())
456
767
def test_not_set_in_branch(self):
457
branch = FakeBranch()
458
my_config = config.BranchConfig(branch)
460
config_file = StringIO(sample_config_text)
461
(my_config._get_location_config().
462
_get_global_config()._get_parser(config_file))
463
self.assertEqual("Robert Collins <robertc@example.com>",
768
my_config = self.get_branch_config(sample_config_text)
769
my_config.branch.control_files.email = None
770
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
464
771
my_config._get_user_id())
465
branch.email = "John"
772
my_config.branch.control_files.email = "John"
466
773
self.assertEqual("John", my_config._get_user_id())
468
def test_BZREMAIL_OVERRIDES(self):
469
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
775
def test_BZR_EMAIL_OVERRIDES(self):
776
os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
470
777
branch = FakeBranch()
471
778
my_config = config.BranchConfig(branch)
472
779
self.assertEqual("Robert Collins <robertc@example.org>",
473
780
my_config.username())
475
782
def test_signatures_forced(self):
476
branch = FakeBranch()
477
my_config = config.BranchConfig(branch)
478
config_file = StringIO(sample_always_signatures)
479
(my_config._get_location_config().
480
_get_global_config()._get_parser(config_file))
481
self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
783
my_config = self.get_branch_config(
784
global_config=sample_always_signatures)
785
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
786
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
787
self.assertTrue(my_config.signature_needed())
789
def test_signatures_forced_branch(self):
790
my_config = self.get_branch_config(
791
global_config=sample_ignore_signatures,
792
branch_data_config=sample_always_signatures)
793
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
794
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
795
self.assertTrue(my_config.signature_needed())
483
797
def test_gpg_signing_command(self):
484
branch = FakeBranch()
485
my_config = config.BranchConfig(branch)
486
config_file = StringIO(sample_config_text)
487
(my_config._get_location_config().
488
_get_global_config()._get_parser(config_file))
798
my_config = self.get_branch_config(
799
# branch data cannot set gpg_signing_command
800
branch_data_config="gpg_signing_command=pgp")
801
config_file = StringIO(sample_config_text.encode('utf-8'))
802
my_config._get_global_config()._get_parser(config_file)
489
803
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
491
805
def test_get_user_option_global(self):
492
806
branch = FakeBranch()
493
807
my_config = config.BranchConfig(branch)
494
config_file = StringIO(sample_config_text)
495
(my_config._get_location_config().
496
_get_global_config()._get_parser(config_file))
808
config_file = StringIO(sample_config_text.encode('utf-8'))
809
(my_config._get_global_config()._get_parser(config_file))
497
810
self.assertEqual('something',
498
811
my_config.get_user_option('user_global_option'))
813
def test_post_commit_default(self):
814
branch = FakeBranch()
815
my_config = self.get_branch_config(sample_config_text, '/a/c',
816
sample_branches_text)
817
self.assertEqual(my_config.branch.base, '/a/c')
818
self.assertEqual('bzrlib.tests.test_config.post_commit',
819
my_config.post_commit())
820
my_config.set_user_option('post_commit', 'rmtree_root')
821
# post-commit is ignored when bresent in branch data
822
self.assertEqual('bzrlib.tests.test_config.post_commit',
823
my_config.post_commit())
824
my_config.set_user_option('post_commit', 'rmtree_root', local=True)
825
self.assertEqual('rmtree_root', my_config.post_commit())
827
def test_config_precedence(self):
828
my_config = self.get_branch_config(global_config=precedence_global)
829
self.assertEqual(my_config.get_user_option('option'), 'global')
830
my_config = self.get_branch_config(global_config=precedence_global,
831
branch_data_config=precedence_branch)
832
self.assertEqual(my_config.get_user_option('option'), 'branch')
833
my_config = self.get_branch_config(global_config=precedence_global,
834
branch_data_config=precedence_branch,
835
location_config=precedence_location)
836
self.assertEqual(my_config.get_user_option('option'), 'recurse')
837
my_config = self.get_branch_config(global_config=precedence_global,
838
branch_data_config=precedence_branch,
839
location_config=precedence_location,
840
location='http://example.com/specific')
841
self.assertEqual(my_config.get_user_option('option'), 'exact')
844
class TestMailAddressExtraction(TestCase):
846
def test_extract_email_address(self):
847
self.assertEqual('jane@test.com',
848
config.extract_email_address('Jane <jane@test.com>'))
849
self.assertRaises(errors.NoEmailInUsername,
850
config.extract_email_address, 'Jane Tester')