~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-04-29 09:19:50 UTC
  • mfrom: (1185.50.89 bzr-jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060429091950-d5ec09cd6e7c591a
Small help-text fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
23
23
import sys
24
24
 
25
25
#import bzrlib specific imports here
26
 
from bzrlib import (
27
 
    config,
28
 
    errors,
29
 
    osutils,
30
 
    urlutils,
31
 
    )
32
 
from bzrlib.branch import Branch
33
 
from bzrlib.bzrdir import BzrDir
34
 
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
 
26
import bzrlib.config as config
 
27
import bzrlib.errors as errors
 
28
from bzrlib.tests import TestCase, TestCaseInTempDir
35
29
 
36
30
 
37
31
sample_long_alias="log -r-15..-1 --line"
47
41
 
48
42
 
49
43
sample_always_signatures = ("[DEFAULT]\n"
50
 
                            "check_signatures=ignore\n"
51
 
                            "create_signatures=always")
 
44
                            "check_signatures=require\n")
52
45
 
53
46
 
54
47
sample_ignore_signatures = ("[DEFAULT]\n"
55
 
                            "check_signatures=require\n"
56
 
                            "create_signatures=never")
 
48
                            "check_signatures=ignore\n")
57
49
 
58
50
 
59
51
sample_maybe_signatures = ("[DEFAULT]\n"
60
 
                            "check_signatures=ignore\n"
61
 
                            "create_signatures=when-required")
 
52
                            "check_signatures=check-available\n")
62
53
 
63
54
 
64
55
sample_branches_text = ("[http://www.example.com]\n"
108
99
 
109
100
class FakeBranch(object):
110
101
 
111
 
    def __init__(self, base=None, user_id=None):
112
 
        if base is None:
113
 
            self.base = "http://example.com/branches/demo"
114
 
        else:
115
 
            self.base = base
116
 
        self.control_files = FakeControlFiles(user_id=user_id)
117
 
 
118
 
    def lock_write(self):
119
 
        pass
120
 
 
121
 
    def unlock(self):
122
 
        pass
 
102
    def __init__(self):
 
103
        self.base = "http://example.com/branches/demo"
 
104
        self.control_files = FakeControlFiles()
123
105
 
124
106
 
125
107
class FakeControlFiles(object):
126
108
 
127
 
    def __init__(self, user_id=None):
128
 
        self.email = user_id
129
 
        self.files = {}
 
109
    def __init__(self):
 
110
        self.email = 'Robert Collins <robertc@example.net>\n'
130
111
 
131
112
    def get_utf8(self, filename):
132
113
        if filename != 'email':
135
116
            return StringIO(self.email)
136
117
        raise errors.NoSuchFile(filename)
137
118
 
138
 
    def get(self, filename):
139
 
        try:
140
 
            return StringIO(self.files[filename])
141
 
        except KeyError:
142
 
            raise errors.NoSuchFile(filename)
143
 
 
144
 
    def put(self, filename, fileobj):
145
 
        self.files[filename] = fileobj.read()
146
 
 
147
119
 
148
120
class InstrumentedConfig(config.Config):
149
121
    """An instrumented config that supplies stubs for template methods."""
200
172
 
201
173
    def test_signatures_default(self):
202
174
        my_config = config.Config()
203
 
        self.assertFalse(my_config.signature_needed())
204
175
        self.assertEqual(config.CHECK_IF_POSSIBLE,
205
176
                         my_config.signature_checking())
206
 
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
207
 
                         my_config.signing_policy())
208
177
 
209
178
    def test_signatures_template_method(self):
210
179
        my_config = InstrumentedConfig()
279
248
            self.assertEqual(config.branches_config_filename(),
280
249
                             '/home/bogus/.bazaar/branches.conf')
281
250
 
282
 
    def test_locations_config_filename(self):
283
 
        if sys.platform == 'win32':
284
 
            self.assertEqual(config.locations_config_filename(), 
285
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
286
 
        else:
287
 
            self.assertEqual(config.locations_config_filename(),
288
 
                             '/home/bogus/.bazaar/locations.conf')
289
 
 
290
251
class TestIniConfig(TestCase):
291
252
 
292
253
    def test_contructs(self):
325
286
                                          'utf-8')])
326
287
 
327
288
 
328
 
class TestBranchConfig(TestCaseWithTransport):
 
289
class TestBranchConfig(TestCaseInTempDir):
329
290
 
330
291
    def test_constructs(self):
331
292
        branch = FakeBranch()
339
300
        self.assertEqual(branch.base, location_config.location)
340
301
        self.failUnless(location_config is my_config._get_location_config())
341
302
 
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')
349
 
 
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')
354
 
 
355
 
    def test_has_explicit_nickname(self):
356
 
        b = self.make_branch('.')
357
 
        self.assertFalse(b.get_config().has_explicit_nickname())
358
 
        b.nick = 'foo'
359
 
        self.assertTrue(b.get_config().has_explicit_nickname())
360
 
 
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)
365
 
 
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' 
370
 
                                    % (local_url,))
371
 
        self.assertEqual('foobar', branch.nick)
372
 
 
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)
377
 
 
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)
383
 
 
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,))
393
 
 
394
303
 
395
304
class TestGlobalConfigItems(TestCase):
396
305
 
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())
425
332
 
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())
435
340
 
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())
445
348
 
446
349
    def _get_sample_config(self):
493
396
        my_config = self._get_sample_config()
494
397
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
495
398
 
496
 
 
497
 
class TestLocationConfig(TestCaseInTempDir):
 
399
class TestLocationConfig(TestCase):
498
400
 
499
401
    def test_constructs(self):
500
402
        my_config = config.LocationConfig('http://example.com')
507
409
        # replace the class that is constructured, to check its parameters
508
410
        oldparserclass = config.ConfigObj
509
411
        config.ConfigObj = InstrumentedConfigObj
 
412
        my_config = config.LocationConfig('http://www.example.com')
510
413
        try:
511
 
            my_config = config.LocationConfig('http://www.example.com')
512
414
            parser = my_config._get_parser()
513
415
        finally:
514
416
            config.ConfigObj = oldparserclass
515
417
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
516
418
        self.assertEqual(parser._calls,
517
 
                         [('__init__', config.locations_config_filename(),
518
 
                           'utf-8')])
519
 
        config.ensure_config_dir_exists()
520
 
        #os.mkdir(config.config_dir())
521
 
        f = file(config.branches_config_filename(), 'wb')
522
 
        f.write('')
523
 
        f.close()
524
 
        oldparserclass = config.ConfigObj
525
 
        config.ConfigObj = InstrumentedConfigObj
526
 
        try:
527
 
            my_config = config.LocationConfig('http://www.example.com')
528
 
            parser = my_config._get_parser()
529
 
        finally:
530
 
            config.ConfigObj = oldparserclass
 
419
                         [('__init__', config.branches_config_filename())])
531
420
 
532
421
    def test_get_global_config(self):
533
 
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
 
422
        my_config = config.LocationConfig('http://example.com')
534
423
        global_config = my_config._get_global_config()
535
424
        self.failUnless(isinstance(global_config, config.GlobalConfig))
536
425
        self.failUnless(global_config is my_config._get_global_config())
537
426
 
538
427
    def test__get_section_no_match(self):
539
 
        self.get_branch_config('/')
540
 
        self.assertEqual(None, self.my_location_config._get_section())
 
428
        self.get_location_config('/')
 
429
        self.assertEqual(None, self.my_config._get_section())
541
430
        
542
431
    def test__get_section_exact(self):
543
 
        self.get_branch_config('http://www.example.com')
 
432
        self.get_location_config('http://www.example.com')
544
433
        self.assertEqual('http://www.example.com',
545
 
                         self.my_location_config._get_section())
 
434
                         self.my_config._get_section())
546
435
   
547
436
    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())
 
437
        self.get_location_config('http://www.example.com-com')
 
438
        self.assertEqual(None, self.my_config._get_section())
550
439
 
551
440
    def test__get_section_subdir_recursive(self):
552
 
        self.get_branch_config('http://www.example.com/com')
 
441
        self.get_location_config('http://www.example.com/com')
553
442
        self.assertEqual('http://www.example.com',
554
 
                         self.my_location_config._get_section())
 
443
                         self.my_config._get_section())
555
444
 
556
445
    def test__get_section_subdir_matches(self):
557
 
        self.get_branch_config('http://www.example.com/useglobal')
 
446
        self.get_location_config('http://www.example.com/useglobal')
558
447
        self.assertEqual('http://www.example.com/useglobal',
559
 
                         self.my_location_config._get_section())
 
448
                         self.my_config._get_section())
560
449
 
561
450
    def test__get_section_subdir_nonrecursive(self):
562
 
        self.get_branch_config(
 
451
        self.get_location_config(
563
452
            'http://www.example.com/useglobal/childbranch')
564
453
        self.assertEqual('http://www.example.com',
565
 
                         self.my_location_config._get_section())
 
454
                         self.my_config._get_section())
566
455
 
567
456
    def test__get_section_subdir_trailing_slash(self):
568
 
        self.get_branch_config('/b')
569
 
        self.assertEqual('/b/', self.my_location_config._get_section())
 
457
        self.get_location_config('/b')
 
458
        self.assertEqual('/b/', self.my_config._get_section())
570
459
 
571
460
    def test__get_section_subdir_child(self):
572
 
        self.get_branch_config('/a/foo')
573
 
        self.assertEqual('/a/*', self.my_location_config._get_section())
 
461
        self.get_location_config('/a/foo')
 
462
        self.assertEqual('/a/*', self.my_config._get_section())
574
463
 
575
464
    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())
 
465
        self.get_location_config('/a/foo/bar')
 
466
        self.assertEqual('/a/', self.my_config._get_section())
578
467
 
579
468
    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())
 
469
        self.get_location_config('/a/')
 
470
        self.assertEqual('/a/', self.my_config._get_section())
582
471
 
583
472
    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())
 
473
        self.get_location_config('/a/c')
 
474
        self.assertEqual('/a/c', self.my_config._get_section())
586
475
 
 
476
    def get_location_config(self, location, global_config=None):
 
477
        if global_config is None:
 
478
            global_file = StringIO(sample_config_text)
 
479
        else:
 
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)
587
485
 
588
486
    def test_location_without_username(self):
589
 
        self.get_branch_config('http://www.example.com/useglobal')
590
 
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
 
487
        self.get_location_config('http://www.example.com/useglobal')
 
488
        self.assertEqual('Robert Collins <robertc@example.com>',
591
489
                         self.my_config.username())
592
490
 
593
491
    def test_location_not_listed(self):
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>',
 
492
        self.get_location_config('/home/robertc/sources')
 
493
        self.assertEqual('Robert Collins <robertc@example.com>',
597
494
                         self.my_config.username())
598
495
 
599
496
    def test_overriding_location(self):
600
 
        self.get_branch_config('http://www.example.com/foo')
 
497
        self.get_location_config('http://www.example.com/foo')
601
498
        self.assertEqual('Robert Collins <robertc@example.org>',
602
499
                         self.my_config.username())
603
500
 
604
501
    def test_signatures_not_set(self):
605
 
        self.get_branch_config('http://www.example.com',
 
502
        self.get_location_config('http://www.example.com',
606
503
                                 global_config=sample_ignore_signatures)
607
 
        self.assertEqual(config.CHECK_ALWAYS,
 
504
        self.assertEqual(config.CHECK_NEVER,
608
505
                         self.my_config.signature_checking())
609
 
        self.assertEqual(config.SIGN_NEVER,
610
 
                         self.my_config.signing_policy())
611
506
 
612
507
    def test_signatures_never(self):
613
 
        self.get_branch_config('/a/c')
 
508
        self.get_location_config('/a/c')
614
509
        self.assertEqual(config.CHECK_NEVER,
615
510
                         self.my_config.signature_checking())
616
511
        
617
512
    def test_signatures_when_available(self):
618
 
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
 
513
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
619
514
        self.assertEqual(config.CHECK_IF_POSSIBLE,
620
515
                         self.my_config.signature_checking())
621
516
        
622
517
    def test_signatures_always(self):
623
 
        self.get_branch_config('/b')
 
518
        self.get_location_config('/b')
624
519
        self.assertEqual(config.CHECK_ALWAYS,
625
520
                         self.my_config.signature_checking())
626
521
        
627
522
    def test_gpg_signing_command(self):
628
 
        self.get_branch_config('/b')
 
523
        self.get_location_config('/b')
629
524
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
630
525
 
631
526
    def test_gpg_signing_command_missing(self):
632
 
        self.get_branch_config('/a')
 
527
        self.get_location_config('/a')
633
528
        self.assertEqual("false", self.my_config.gpg_signing_command())
634
529
 
635
530
    def test_get_user_option_global(self):
636
 
        self.get_branch_config('/a')
 
531
        self.get_location_config('/a')
637
532
        self.assertEqual('something',
638
533
                         self.my_config.get_user_option('user_global_option'))
639
534
 
640
535
    def test_get_user_option_local(self):
641
 
        self.get_branch_config('/a')
 
536
        self.get_location_config('/a')
642
537
        self.assertEqual('local',
643
538
                         self.my_config.get_user_option('user_local_option'))
644
539
        
645
540
    def test_post_commit_default(self):
646
 
        self.get_branch_config('/a/c')
 
541
        self.get_location_config('/a/c')
647
542
        self.assertEqual('bzrlib.tests.test_config.post_commit',
648
543
                         self.my_config.post_commit())
649
544
 
650
 
    def get_branch_config(self, location, global_config=None):
 
545
 
 
546
class TestLocationConfig(TestCaseInTempDir):
 
547
 
 
548
    def get_location_config(self, location, global_config=None):
651
549
        if global_config is None:
652
550
            global_file = StringIO(sample_config_text.encode('utf-8'))
653
551
        else:
654
552
            global_file = StringIO(global_config.encode('utf-8'))
655
553
        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
 
554
        self.my_config = config.LocationConfig(location)
 
555
        self.my_config._get_parser(branches_file)
661
556
        self.my_config._get_global_config()._get_parser(global_file)
662
557
 
663
558
    def test_set_user_setting_sets_and_saves(self):
664
 
        self.get_branch_config('/a/c')
 
559
        self.get_location_config('/a/c')
665
560
        record = InstrumentedConfigObj("foo")
666
 
        self.my_location_config._parser = record
 
561
        self.my_config._parser = record
667
562
 
668
563
        real_mkdir = os.mkdir
669
564
        self.created = False
674
569
 
675
570
        os.mkdir = checked_mkdir
676
571
        try:
677
 
            self.my_config.set_user_option('foo', 'bar', local=True)
 
572
            self.my_config.set_user_option('foo', 'bar')
678
573
        finally:
679
574
            os.mkdir = real_mkdir
680
575
 
687
582
                          ('write',)],
688
583
                         record._calls[1:])
689
584
 
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')
694
 
        self.assertEqual(
695
 
            self.my_config.branch.control_files.files['branch.conf'], 
696
 
            'foo = bar')
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')
702
 
        
703
 
 
704
 
precedence_global = 'option = global'
705
 
precedence_branch = 'option = branch'
706
 
precedence_location = """
707
 
[http://]
708
 
recurse = true
709
 
option = recurse
710
 
[http://example.com/specific]
711
 
option = exact
712
 
"""
713
 
 
714
 
 
715
 
class TestBranchConfigItems(TestCaseInTempDir):
716
 
 
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'] = \
729
 
                branch_data_config
730
 
        return my_config
 
585
 
 
586
class TestBranchConfigItems(TestCase):
731
587
 
732
588
    def test_user_id(self):
733
 
        branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
 
589
        branch = FakeBranch()
734
590
        my_config = config.BranchConfig(branch)
735
591
        self.assertEqual("Robert Collins <robertc@example.net>",
736
 
                         my_config.username())
 
592
                         my_config._get_user_id())
737
593
        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())
 
594
        self.assertEqual("John", my_config._get_user_id())
 
595
 
 
596
    def test_not_set_in_branch(self):
 
597
        branch = FakeBranch()
 
598
        my_config = config.BranchConfig(branch)
741
599
        branch.control_files.email = None
742
 
        self.assertEqual("Robert Collins <robertc@example.org>",
743
 
                         my_config.username())
744
 
 
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
 
600
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
601
        (my_config._get_location_config().
 
602
            _get_global_config()._get_parser(config_file))
748
603
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
749
604
                         my_config._get_user_id())
750
 
        my_config.branch.control_files.email = "John"
 
605
        branch.control_files.email = "John"
751
606
        self.assertEqual("John", my_config._get_user_id())
752
607
 
753
 
    def test_BZR_EMAIL_OVERRIDES(self):
754
 
        os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
 
608
    def test_BZREMAIL_OVERRIDES(self):
 
609
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
755
610
        branch = FakeBranch()
756
611
        my_config = config.BranchConfig(branch)
757
612
        self.assertEqual("Robert Collins <robertc@example.org>",
758
613
                         my_config.username())
759
614
    
760
615
    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())
766
 
 
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())
 
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())
774
622
 
775
623
    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")
 
624
        branch = FakeBranch()
 
625
        my_config = config.BranchConfig(branch)
779
626
        config_file = StringIO(sample_config_text.encode('utf-8'))
780
 
        my_config._get_global_config()._get_parser(config_file)
 
627
        (my_config._get_location_config().
 
628
            _get_global_config()._get_parser(config_file))
781
629
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
782
630
 
783
631
    def test_get_user_option_global(self):
784
632
        branch = FakeBranch()
785
633
        my_config = config.BranchConfig(branch)
786
634
        config_file = StringIO(sample_config_text.encode('utf-8'))
787
 
        (my_config._get_global_config()._get_parser(config_file))
 
635
        (my_config._get_location_config().
 
636
            _get_global_config()._get_parser(config_file))
788
637
        self.assertEqual('something',
789
638
                         my_config.get_user_option('user_global_option'))
790
639
 
791
640
    def test_post_commit_default(self):
792
641
        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())
804
 
 
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')
 
642
        branch.base='/a/c'
 
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())
820
651
 
821
652
 
822
653
class TestMailAddressExtraction(TestCase):