~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: Wouter van Heyst
  • Date: 2006-06-07 16:05:27 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: larstiq@larstiq.dyndns.org-20060607160527-2b3649154d0e2e84
more code cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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"
38
 
sample_config_text = u"""
39
 
[DEFAULT]
40
 
email=Erik B\u00e5gfors <erik@bagfors.nu>
41
 
editor=vim
42
 
gpg_signing_command=gnome-gpg
43
 
log_format=short
44
 
user_global_option=something
45
 
[ALIASES]
46
 
h=help
47
 
ll=""" + sample_long_alias + "\n"
48
 
 
49
 
 
50
 
sample_always_signatures = """
51
 
[DEFAULT]
52
 
check_signatures=ignore
53
 
create_signatures=always
54
 
"""
55
 
 
56
 
sample_ignore_signatures = """
57
 
[DEFAULT]
58
 
check_signatures=require
59
 
create_signatures=never
60
 
"""
61
 
 
62
 
sample_maybe_signatures = """
63
 
[DEFAULT]
64
 
check_signatures=ignore
65
 
create_signatures=when-required
66
 
"""
67
 
 
68
 
sample_branches_text = """
69
 
[http://www.example.com]
70
 
# Top level policy
71
 
email=Robert Collins <robertc@example.org>
72
 
normal_option = normal
73
 
appendpath_option = append
74
 
appendpath_option:policy = appendpath
75
 
norecurse_option = norecurse
76
 
norecurse_option:policy = norecurse
77
 
[http://www.example.com/ignoreparent]
78
 
# different project: ignore parent dir config
79
 
ignore_parents=true
80
 
[http://www.example.com/norecurse]
81
 
# configuration items that only apply to this dir
82
 
recurse=false
83
 
normal_option = norecurse
84
 
[http://www.example.com/dir]
85
 
appendpath_option = normal
86
 
[/b/]
87
 
check_signatures=require
88
 
# test trailing / matching with no children
89
 
[/a/]
90
 
check_signatures=check-available
91
 
gpg_signing_command=false
92
 
user_local_option=local
93
 
# test trailing / matching
94
 
[/a/*]
95
 
#subdirs will match but not the parent
96
 
[/a/c]
97
 
check_signatures=ignore
98
 
post_commit=bzrlib.tests.test_config.post_commit
99
 
#testing explicit beats globs
100
 
"""
 
32
sample_config_text = ("[DEFAULT]\n"
 
33
                      u"email=Erik B\u00e5gfors <erik@bagfors.nu>\n"
 
34
                      "editor=vim\n"
 
35
                      "gpg_signing_command=gnome-gpg\n"
 
36
                      "log_format=short\n"
 
37
                      "user_global_option=something\n"
 
38
                      "[ALIASES]\n"
 
39
                      "h=help\n"
 
40
                      "ll=" + sample_long_alias + "\n")
 
41
 
 
42
 
 
43
sample_always_signatures = ("[DEFAULT]\n"
 
44
                            "check_signatures=require\n")
 
45
 
 
46
 
 
47
sample_ignore_signatures = ("[DEFAULT]\n"
 
48
                            "check_signatures=ignore\n")
 
49
 
 
50
 
 
51
sample_maybe_signatures = ("[DEFAULT]\n"
 
52
                            "check_signatures=check-available\n")
 
53
 
 
54
 
 
55
sample_branches_text = ("[http://www.example.com]\n"
 
56
                        "# Top level policy\n"
 
57
                        "email=Robert Collins <robertc@example.org>\n"
 
58
                        "[http://www.example.com/useglobal]\n"
 
59
                        "# different project, forces global lookup\n"
 
60
                        "recurse=false\n"
 
61
                        "[/b/]\n"
 
62
                        "check_signatures=require\n"
 
63
                        "# test trailing / matching with no children\n"
 
64
                        "[/a/]\n"
 
65
                        "check_signatures=check-available\n"
 
66
                        "gpg_signing_command=false\n"
 
67
                        "user_local_option=local\n"
 
68
                        "# test trailing / matching\n"
 
69
                        "[/a/*]\n"
 
70
                        "#subdirs will match but not the parent\n"
 
71
                        "recurse=False\n"
 
72
                        "[/a/c]\n"
 
73
                        "check_signatures=ignore\n"
 
74
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
 
75
                        "#testing explicit beats globs\n")
 
76
 
101
77
 
102
78
 
103
79
class InstrumentedConfigObj(object):
117
93
    def __setitem__(self, key, value):
118
94
        self._calls.append(('__setitem__', key, value))
119
95
 
120
 
    def __delitem__(self, key):
121
 
        self._calls.append(('__delitem__', key))
122
 
 
123
 
    def keys(self):
124
 
        self._calls.append(('keys',))
125
 
        return []
126
 
 
127
96
    def write(self, arg):
128
97
        self._calls.append(('write',))
129
98
 
130
 
    def as_bool(self, value):
131
 
        self._calls.append(('as_bool', value))
132
 
        return False
133
 
 
134
 
    def get_value(self, section, name):
135
 
        self._calls.append(('get_value', section, name))
136
 
        return None
137
 
 
138
99
 
139
100
class FakeBranch(object):
140
101
 
141
 
    def __init__(self, base=None, user_id=None):
142
 
        if base is None:
143
 
            self.base = "http://example.com/branches/demo"
144
 
        else:
145
 
            self.base = base
146
 
        self.control_files = FakeControlFiles(user_id=user_id)
147
 
 
148
 
    def lock_write(self):
149
 
        pass
150
 
 
151
 
    def unlock(self):
152
 
        pass
 
102
    def __init__(self):
 
103
        self.base = "http://example.com/branches/demo"
 
104
        self.control_files = FakeControlFiles()
153
105
 
154
106
 
155
107
class FakeControlFiles(object):
156
108
 
157
 
    def __init__(self, user_id=None):
158
 
        self.email = user_id
159
 
        self.files = {}
 
109
    def __init__(self):
 
110
        self.email = 'Robert Collins <robertc@example.net>\n'
160
111
 
161
112
    def get_utf8(self, filename):
162
113
        if filename != 'email':
165
116
            return StringIO(self.email)
166
117
        raise errors.NoSuchFile(filename)
167
118
 
168
 
    def get(self, filename):
169
 
        try:
170
 
            return StringIO(self.files[filename])
171
 
        except KeyError:
172
 
            raise errors.NoSuchFile(filename)
173
 
 
174
 
    def put(self, filename, fileobj):
175
 
        self.files[filename] = fileobj.read()
176
 
 
177
119
 
178
120
class InstrumentedConfig(config.Config):
179
121
    """An instrumented config that supplies stubs for template methods."""
230
172
 
231
173
    def test_signatures_default(self):
232
174
        my_config = config.Config()
233
 
        self.assertFalse(my_config.signature_needed())
234
175
        self.assertEqual(config.CHECK_IF_POSSIBLE,
235
176
                         my_config.signature_checking())
236
 
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
237
 
                         my_config.signing_policy())
238
177
 
239
178
    def test_signatures_template_method(self):
240
179
        my_config = InstrumentedConfig()
269
208
 
270
209
    def setUp(self):
271
210
        super(TestConfigPath, self).setUp()
 
211
        self.old_home = os.environ.get('HOME', None)
 
212
        self.old_appdata = os.environ.get('APPDATA', None)
272
213
        os.environ['HOME'] = '/home/bogus'
273
 
        if sys.platform == 'win32':
274
 
            os.environ['BZR_HOME'] = \
275
 
                r'C:\Documents and Settings\bogus\Application Data'
 
214
        os.environ['APPDATA'] = \
 
215
            r'C:\Documents and Settings\bogus\Application Data'
276
216
 
 
217
    def tearDown(self):
 
218
        if self.old_home is None:
 
219
            del os.environ['HOME']
 
220
        else:
 
221
            os.environ['HOME'] = self.old_home
 
222
        if self.old_appdata is None:
 
223
            del os.environ['APPDATA']
 
224
        else:
 
225
            os.environ['APPDATA'] = self.old_appdata
 
226
        super(TestConfigPath, self).tearDown()
 
227
    
277
228
    def test_config_dir(self):
278
229
        if sys.platform == 'win32':
279
230
            self.assertEqual(config.config_dir(), 
297
248
            self.assertEqual(config.branches_config_filename(),
298
249
                             '/home/bogus/.bazaar/branches.conf')
299
250
 
300
 
    def test_locations_config_filename(self):
301
 
        if sys.platform == 'win32':
302
 
            self.assertEqual(config.locations_config_filename(), 
303
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
304
 
        else:
305
 
            self.assertEqual(config.locations_config_filename(),
306
 
                             '/home/bogus/.bazaar/locations.conf')
307
 
 
308
251
class TestIniConfig(TestCase):
309
252
 
310
253
    def test_contructs(self):
343
286
                                          'utf-8')])
344
287
 
345
288
 
346
 
class TestBranchConfig(TestCaseWithTransport):
 
289
class TestBranchConfig(TestCaseInTempDir):
347
290
 
348
291
    def test_constructs(self):
349
292
        branch = FakeBranch()
357
300
        self.assertEqual(branch.base, location_config.location)
358
301
        self.failUnless(location_config is my_config._get_location_config())
359
302
 
360
 
    def test_get_config(self):
361
 
        """The Branch.get_config method works properly"""
362
 
        b = BzrDir.create_standalone_workingtree('.').branch
363
 
        my_config = b.get_config()
364
 
        self.assertIs(my_config.get_user_option('wacky'), None)
365
 
        my_config.set_user_option('wacky', 'unlikely')
366
 
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
367
 
 
368
 
        # Ensure we get the same thing if we start again
369
 
        b2 = Branch.open('.')
370
 
        my_config2 = b2.get_config()
371
 
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
372
 
 
373
 
    def test_has_explicit_nickname(self):
374
 
        b = self.make_branch('.')
375
 
        self.assertFalse(b.get_config().has_explicit_nickname())
376
 
        b.nick = 'foo'
377
 
        self.assertTrue(b.get_config().has_explicit_nickname())
378
 
 
379
 
    def test_config_url(self):
380
 
        """The Branch.get_config will use section that uses a local url"""
381
 
        branch = self.make_branch('branch')
382
 
        self.assertEqual('branch', branch.nick)
383
 
 
384
 
        locations = config.locations_config_filename()
385
 
        config.ensure_config_dir_exists()
386
 
        local_url = urlutils.local_path_to_url('branch')
387
 
        open(locations, 'wb').write('[%s]\nnickname = foobar' 
388
 
                                    % (local_url,))
389
 
        self.assertEqual('foobar', branch.nick)
390
 
 
391
 
    def test_config_local_path(self):
392
 
        """The Branch.get_config will use a local system path"""
393
 
        branch = self.make_branch('branch')
394
 
        self.assertEqual('branch', branch.nick)
395
 
 
396
 
        locations = config.locations_config_filename()
397
 
        config.ensure_config_dir_exists()
398
 
        open(locations, 'wb').write('[%s/branch]\nnickname = barry' 
399
 
                                    % (osutils.getcwd().encode('utf8'),))
400
 
        self.assertEqual('barry', branch.nick)
401
 
 
402
 
    def test_config_creates_local(self):
403
 
        """Creating a new entry in config uses a local path."""
404
 
        branch = self.make_branch('branch', format='knit')
405
 
        branch.set_push_location('http://foobar')
406
 
        locations = config.locations_config_filename()
407
 
        local_path = osutils.getcwd().encode('utf8')
408
 
        # Surprisingly ConfigObj doesn't create a trailing newline
409
 
        self.check_file_contents(locations,
410
 
            '[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (local_path,))
411
 
 
412
 
    def test_autonick_urlencoded(self):
413
 
        b = self.make_branch('!repo')
414
 
        self.assertEqual('!repo', b.get_config().get_nickname())
415
 
 
416
303
 
417
304
class TestGlobalConfigItems(TestCase):
418
305
 
439
326
        config_file = StringIO(sample_always_signatures)
440
327
        my_config = config.GlobalConfig()
441
328
        my_config._parser = my_config._get_parser(file=config_file)
442
 
        self.assertEqual(config.CHECK_NEVER,
 
329
        self.assertEqual(config.CHECK_ALWAYS,
443
330
                         my_config.signature_checking())
444
 
        self.assertEqual(config.SIGN_ALWAYS,
445
 
                         my_config.signing_policy())
446
331
        self.assertEqual(True, my_config.signature_needed())
447
332
 
448
333
    def test_signatures_if_possible(self):
449
334
        config_file = StringIO(sample_maybe_signatures)
450
335
        my_config = config.GlobalConfig()
451
336
        my_config._parser = my_config._get_parser(file=config_file)
452
 
        self.assertEqual(config.CHECK_NEVER,
 
337
        self.assertEqual(config.CHECK_IF_POSSIBLE,
453
338
                         my_config.signature_checking())
454
 
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
455
 
                         my_config.signing_policy())
456
339
        self.assertEqual(False, my_config.signature_needed())
457
340
 
458
341
    def test_signatures_ignore(self):
459
342
        config_file = StringIO(sample_ignore_signatures)
460
343
        my_config = config.GlobalConfig()
461
344
        my_config._parser = my_config._get_parser(file=config_file)
462
 
        self.assertEqual(config.CHECK_ALWAYS,
 
345
        self.assertEqual(config.CHECK_NEVER,
463
346
                         my_config.signature_checking())
464
 
        self.assertEqual(config.SIGN_NEVER,
465
 
                         my_config.signing_policy())
466
347
        self.assertEqual(False, my_config.signature_needed())
467
348
 
468
349
    def _get_sample_config(self):
529
410
        # replace the class that is constructured, to check its parameters
530
411
        oldparserclass = config.ConfigObj
531
412
        config.ConfigObj = InstrumentedConfigObj
 
413
        my_config = config.LocationConfig('http://www.example.com')
532
414
        try:
533
 
            my_config = config.LocationConfig('http://www.example.com')
534
415
            parser = my_config._get_parser()
535
416
        finally:
536
417
            config.ConfigObj = oldparserclass
537
418
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
538
419
        self.assertEqual(parser._calls,
539
 
                         [('__init__', config.locations_config_filename(),
 
420
                         [('__init__', config.branches_config_filename(),
540
421
                           'utf-8')])
541
 
        config.ensure_config_dir_exists()
542
 
        #os.mkdir(config.config_dir())
543
 
        f = file(config.branches_config_filename(), 'wb')
544
 
        f.write('')
545
 
        f.close()
546
 
        oldparserclass = config.ConfigObj
547
 
        config.ConfigObj = InstrumentedConfigObj
548
 
        try:
549
 
            my_config = config.LocationConfig('http://www.example.com')
550
 
            parser = my_config._get_parser()
551
 
        finally:
552
 
            config.ConfigObj = oldparserclass
553
422
 
554
423
    def test_get_global_config(self):
555
 
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
 
424
        my_config = config.LocationConfig('http://example.com')
556
425
        global_config = my_config._get_global_config()
557
426
        self.failUnless(isinstance(global_config, config.GlobalConfig))
558
427
        self.failUnless(global_config is my_config._get_global_config())
559
428
 
560
 
    def test__get_matching_sections_no_match(self):
561
 
        self.get_branch_config('/')
562
 
        self.assertEqual([], self.my_location_config._get_matching_sections())
 
429
    def test__get_section_no_match(self):
 
430
        self.get_location_config('/')
 
431
        self.assertEqual(None, self.my_config._get_section())
563
432
        
564
 
    def test__get_matching_sections_exact(self):
565
 
        self.get_branch_config('http://www.example.com')
566
 
        self.assertEqual([('http://www.example.com', '')],
567
 
                         self.my_location_config._get_matching_sections())
 
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())
568
437
   
569
 
    def test__get_matching_sections_suffix_does_not(self):
570
 
        self.get_branch_config('http://www.example.com-com')
571
 
        self.assertEqual([], self.my_location_config._get_matching_sections())
572
 
 
573
 
    def test__get_matching_sections_subdir_recursive(self):
574
 
        self.get_branch_config('http://www.example.com/com')
575
 
        self.assertEqual([('http://www.example.com', 'com')],
576
 
                         self.my_location_config._get_matching_sections())
577
 
 
578
 
    def test__get_matching_sections_ignoreparent(self):
579
 
        self.get_branch_config('http://www.example.com/ignoreparent')
580
 
        self.assertEqual([('http://www.example.com/ignoreparent', '')],
581
 
                         self.my_location_config._get_matching_sections())
582
 
 
583
 
    def test__get_matching_sections_ignoreparent_subdir(self):
584
 
        self.get_branch_config(
585
 
            'http://www.example.com/ignoreparent/childbranch')
586
 
        self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
587
 
                         self.my_location_config._get_matching_sections())
588
 
 
589
 
    def test__get_matching_sections_subdir_trailing_slash(self):
590
 
        self.get_branch_config('/b')
591
 
        self.assertEqual([('/b/', '')],
592
 
                         self.my_location_config._get_matching_sections())
593
 
 
594
 
    def test__get_matching_sections_subdir_child(self):
595
 
        self.get_branch_config('/a/foo')
596
 
        self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
597
 
                         self.my_location_config._get_matching_sections())
598
 
 
599
 
    def test__get_matching_sections_subdir_child_child(self):
600
 
        self.get_branch_config('/a/foo/bar')
601
 
        self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
602
 
                         self.my_location_config._get_matching_sections())
603
 
 
604
 
    def test__get_matching_sections_trailing_slash_with_children(self):
605
 
        self.get_branch_config('/a/')
606
 
        self.assertEqual([('/a/', '')],
607
 
                         self.my_location_config._get_matching_sections())
608
 
 
609
 
    def test__get_matching_sections_explicit_over_glob(self):
610
 
        # XXX: 2006-09-08 jamesh
611
 
        # This test only passes because ord('c') > ord('*').  If there
612
 
        # was a config section for '/a/?', it would get precedence
613
 
        # over '/a/c'.
614
 
        self.get_branch_config('/a/c')
615
 
        self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
616
 
                         self.my_location_config._get_matching_sections())
617
 
 
618
 
    def test__get_option_policy_normal(self):
619
 
        self.get_branch_config('http://www.example.com')
620
 
        self.assertEqual(
621
 
            self.my_location_config._get_config_policy(
622
 
            'http://www.example.com', 'normal_option'),
623
 
            config.POLICY_NONE)
624
 
 
625
 
    def test__get_option_policy_norecurse(self):
626
 
        self.get_branch_config('http://www.example.com')
627
 
        self.assertEqual(
628
 
            self.my_location_config._get_option_policy(
629
 
            'http://www.example.com', 'norecurse_option'),
630
 
            config.POLICY_NORECURSE)
631
 
        # Test old recurse=False setting:
632
 
        self.assertEqual(
633
 
            self.my_location_config._get_option_policy(
634
 
            'http://www.example.com/norecurse', 'normal_option'),
635
 
            config.POLICY_NORECURSE)
636
 
 
637
 
    def test__get_option_policy_normal(self):
638
 
        self.get_branch_config('http://www.example.com')
639
 
        self.assertEqual(
640
 
            self.my_location_config._get_option_policy(
641
 
            'http://www.example.com', 'appendpath_option'),
642
 
            config.POLICY_APPENDPATH)
 
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())
 
441
 
 
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())
 
446
 
 
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())
 
451
 
 
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())
 
457
 
 
458
    def test__get_section_subdir_trailing_slash(self):
 
459
        self.get_location_config('/b')
 
460
        self.assertEqual('/b/', self.my_config._get_section())
 
461
 
 
462
    def test__get_section_subdir_child(self):
 
463
        self.get_location_config('/a/foo')
 
464
        self.assertEqual('/a/*', self.my_config._get_section())
 
465
 
 
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())
 
469
 
 
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())
 
473
 
 
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())
 
477
 
643
478
 
644
479
    def test_location_without_username(self):
645
 
        self.get_branch_config('http://www.example.com/ignoreparent')
 
480
        self.get_location_config('http://www.example.com/useglobal')
646
481
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
647
482
                         self.my_config.username())
648
483
 
649
484
    def test_location_not_listed(self):
650
485
        """Test that the global username is used when no location matches"""
651
 
        self.get_branch_config('/home/robertc/sources')
 
486
        self.get_location_config('/home/robertc/sources')
652
487
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
653
488
                         self.my_config.username())
654
489
 
655
490
    def test_overriding_location(self):
656
 
        self.get_branch_config('http://www.example.com/foo')
 
491
        self.get_location_config('http://www.example.com/foo')
657
492
        self.assertEqual('Robert Collins <robertc@example.org>',
658
493
                         self.my_config.username())
659
494
 
660
495
    def test_signatures_not_set(self):
661
 
        self.get_branch_config('http://www.example.com',
 
496
        self.get_location_config('http://www.example.com',
662
497
                                 global_config=sample_ignore_signatures)
663
 
        self.assertEqual(config.CHECK_ALWAYS,
 
498
        self.assertEqual(config.CHECK_NEVER,
664
499
                         self.my_config.signature_checking())
665
 
        self.assertEqual(config.SIGN_NEVER,
666
 
                         self.my_config.signing_policy())
667
500
 
668
501
    def test_signatures_never(self):
669
 
        self.get_branch_config('/a/c')
 
502
        self.get_location_config('/a/c')
670
503
        self.assertEqual(config.CHECK_NEVER,
671
504
                         self.my_config.signature_checking())
672
505
        
673
506
    def test_signatures_when_available(self):
674
 
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
 
507
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
675
508
        self.assertEqual(config.CHECK_IF_POSSIBLE,
676
509
                         self.my_config.signature_checking())
677
510
        
678
511
    def test_signatures_always(self):
679
 
        self.get_branch_config('/b')
 
512
        self.get_location_config('/b')
680
513
        self.assertEqual(config.CHECK_ALWAYS,
681
514
                         self.my_config.signature_checking())
682
515
        
683
516
    def test_gpg_signing_command(self):
684
 
        self.get_branch_config('/b')
 
517
        self.get_location_config('/b')
685
518
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
686
519
 
687
520
    def test_gpg_signing_command_missing(self):
688
 
        self.get_branch_config('/a')
 
521
        self.get_location_config('/a')
689
522
        self.assertEqual("false", self.my_config.gpg_signing_command())
690
523
 
691
524
    def test_get_user_option_global(self):
692
 
        self.get_branch_config('/a')
 
525
        self.get_location_config('/a')
693
526
        self.assertEqual('something',
694
527
                         self.my_config.get_user_option('user_global_option'))
695
528
 
696
529
    def test_get_user_option_local(self):
697
 
        self.get_branch_config('/a')
 
530
        self.get_location_config('/a')
698
531
        self.assertEqual('local',
699
532
                         self.my_config.get_user_option('user_local_option'))
700
 
 
701
 
    def test_get_user_option_appendpath(self):
702
 
        # returned as is for the base path:
703
 
        self.get_branch_config('http://www.example.com')
704
 
        self.assertEqual('append',
705
 
                         self.my_config.get_user_option('appendpath_option'))
706
 
        # Extra path components get appended:
707
 
        self.get_branch_config('http://www.example.com/a/b/c')
708
 
        self.assertEqual('append/a/b/c',
709
 
                         self.my_config.get_user_option('appendpath_option'))
710
 
        # Overriden for http://www.example.com/dir, where it is a
711
 
        # normal option:
712
 
        self.get_branch_config('http://www.example.com/dir/a/b/c')
713
 
        self.assertEqual('normal',
714
 
                         self.my_config.get_user_option('appendpath_option'))
715
 
 
716
 
    def test_get_user_option_norecurse(self):
717
 
        self.get_branch_config('http://www.example.com')
718
 
        self.assertEqual('norecurse',
719
 
                         self.my_config.get_user_option('norecurse_option'))
720
 
        self.get_branch_config('http://www.example.com/dir')
721
 
        self.assertEqual(None,
722
 
                         self.my_config.get_user_option('norecurse_option'))
723
 
        # http://www.example.com/norecurse is a recurse=False section
724
 
        # that redefines normal_option.  Subdirectories do not pick up
725
 
        # this redefinition.
726
 
        self.get_branch_config('http://www.example.com/norecurse')
727
 
        self.assertEqual('norecurse',
728
 
                         self.my_config.get_user_option('normal_option'))
729
 
        self.get_branch_config('http://www.example.com/norecurse/subdir')
730
 
        self.assertEqual('normal',
731
 
                         self.my_config.get_user_option('normal_option'))
732
 
 
733
 
    def test_set_user_option_norecurse(self):
734
 
        self.get_branch_config('http://www.example.com')
735
 
        self.my_config.set_user_option('foo', 'bar',
736
 
                                       store=config.STORE_LOCATION_NORECURSE)
737
 
        self.assertEqual(
738
 
            self.my_location_config._get_option_policy(
739
 
            'http://www.example.com', 'foo'),
740
 
            config.POLICY_NORECURSE)
741
 
 
742
 
    def test_set_user_option_appendpath(self):
743
 
        self.get_branch_config('http://www.example.com')
744
 
        self.my_config.set_user_option('foo', 'bar',
745
 
                                       store=config.STORE_LOCATION_APPENDPATH)
746
 
        self.assertEqual(
747
 
            self.my_location_config._get_option_policy(
748
 
            'http://www.example.com', 'foo'),
749
 
            config.POLICY_APPENDPATH)
750
 
 
751
 
    def test_set_user_option_change_policy(self):
752
 
        self.get_branch_config('http://www.example.com')
753
 
        self.my_config.set_user_option('norecurse_option', 'normal',
754
 
                                       store=config.STORE_LOCATION)
755
 
        self.assertEqual(
756
 
            self.my_location_config._get_option_policy(
757
 
            'http://www.example.com', 'norecurse_option'),
758
 
            config.POLICY_NONE)
759
 
 
760
 
    def test_set_user_option_recurse_false_section(self):
761
 
        # The following section has recurse=False set.  The test is to
762
 
        # make sure that a normal option can be added to the section,
763
 
        # converting recurse=False to the norecurse policy.
764
 
        self.get_branch_config('http://www.example.com/norecurse')
765
 
        self.callDeprecated(['The recurse option is deprecated as of 0.14.  '
766
 
                             'The section "http://www.example.com/norecurse" '
767
 
                             'has been converted to use policies.'],
768
 
                            self.my_config.set_user_option,
769
 
                            'foo', 'bar', store=config.STORE_LOCATION)
770
 
        self.assertEqual(
771
 
            self.my_location_config._get_option_policy(
772
 
            'http://www.example.com/norecurse', 'foo'),
773
 
            config.POLICY_NONE)
774
 
        # The previously existing option is still norecurse:
775
 
        self.assertEqual(
776
 
            self.my_location_config._get_option_policy(
777
 
            'http://www.example.com/norecurse', 'normal_option'),
778
 
            config.POLICY_NORECURSE)
779
533
        
780
 
 
781
534
    def test_post_commit_default(self):
782
 
        self.get_branch_config('/a/c')
 
535
        self.get_location_config('/a/c')
783
536
        self.assertEqual('bzrlib.tests.test_config.post_commit',
784
537
                         self.my_config.post_commit())
785
538
 
786
 
    def get_branch_config(self, location, global_config=None):
 
539
    def get_location_config(self, location, global_config=None):
787
540
        if global_config is None:
788
541
            global_file = StringIO(sample_config_text.encode('utf-8'))
789
542
        else:
790
543
            global_file = StringIO(global_config.encode('utf-8'))
791
544
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
792
 
        self.my_config = config.BranchConfig(FakeBranch(location))
793
 
        # Force location config to use specified file
794
 
        self.my_location_config = self.my_config._get_location_config()
795
 
        self.my_location_config._get_parser(branches_file)
796
 
        # Force global config to use specified file
 
545
        self.my_config = config.LocationConfig(location)
 
546
        self.my_config._get_parser(branches_file)
797
547
        self.my_config._get_global_config()._get_parser(global_file)
798
548
 
799
549
    def test_set_user_setting_sets_and_saves(self):
800
 
        self.get_branch_config('/a/c')
 
550
        self.get_location_config('/a/c')
801
551
        record = InstrumentedConfigObj("foo")
802
 
        self.my_location_config._parser = record
 
552
        self.my_config._parser = record
803
553
 
804
554
        real_mkdir = os.mkdir
805
555
        self.created = False
810
560
 
811
561
        os.mkdir = checked_mkdir
812
562
        try:
813
 
            self.callDeprecated(['The recurse option is deprecated as of '
814
 
                                 '0.14.  The section "/a/c" has been '
815
 
                                 'converted to use policies.'],
816
 
                                self.my_config.set_user_option,
817
 
                                'foo', 'bar', store=config.STORE_LOCATION)
 
563
            self.my_config.set_user_option('foo', 'bar')
818
564
        finally:
819
565
            os.mkdir = real_mkdir
820
566
 
824
570
                          ('__setitem__', '/a/c', {}),
825
571
                          ('__getitem__', '/a/c'),
826
572
                          ('__setitem__', 'foo', 'bar'),
827
 
                          ('__getitem__', '/a/c'),
828
 
                          ('as_bool', 'recurse'),
829
 
                          ('__getitem__', '/a/c'),
830
 
                          ('__delitem__', 'recurse'),
831
 
                          ('__getitem__', '/a/c'),
832
 
                          ('keys',),
833
 
                          ('__getitem__', '/a/c'),
834
 
                          ('__contains__', 'foo:policy'),
835
573
                          ('write',)],
836
574
                         record._calls[1:])
837
575
 
838
 
    def test_set_user_setting_sets_and_saves2(self):
839
 
        self.get_branch_config('/a/c')
840
 
        self.assertIs(self.my_config.get_user_option('foo'), None)
841
 
        self.my_config.set_user_option('foo', 'bar')
842
 
        self.assertEqual(
843
 
            self.my_config.branch.control_files.files['branch.conf'], 
844
 
            'foo = bar')
845
 
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
846
 
        self.my_config.set_user_option('foo', 'baz',
847
 
                                       store=config.STORE_LOCATION)
848
 
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
849
 
        self.my_config.set_user_option('foo', 'qux')
850
 
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
851
 
        
852
 
 
853
 
precedence_global = 'option = global'
854
 
precedence_branch = 'option = branch'
855
 
precedence_location = """
856
 
[http://]
857
 
recurse = true
858
 
option = recurse
859
 
[http://example.com/specific]
860
 
option = exact
861
 
"""
862
 
 
863
 
 
864
 
class TestBranchConfigItems(TestCaseInTempDir):
865
 
 
866
 
    def get_branch_config(self, global_config=None, location=None, 
867
 
                          location_config=None, branch_data_config=None):
868
 
        my_config = config.BranchConfig(FakeBranch(location))
869
 
        if global_config is not None:
870
 
            global_file = StringIO(global_config.encode('utf-8'))
871
 
            my_config._get_global_config()._get_parser(global_file)
872
 
        self.my_location_config = my_config._get_location_config()
873
 
        if location_config is not None:
874
 
            location_file = StringIO(location_config.encode('utf-8'))
875
 
            self.my_location_config._get_parser(location_file)
876
 
        if branch_data_config is not None:
877
 
            my_config.branch.control_files.files['branch.conf'] = \
878
 
                branch_data_config
879
 
        return my_config
 
576
 
 
577
class TestBranchConfigItems(TestCase):
880
578
 
881
579
    def test_user_id(self):
882
 
        branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
 
580
        branch = FakeBranch()
883
581
        my_config = config.BranchConfig(branch)
884
582
        self.assertEqual("Robert Collins <robertc@example.net>",
885
 
                         my_config.username())
 
583
                         my_config._get_user_id())
886
584
        branch.control_files.email = "John"
887
 
        my_config.set_user_option('email', 
888
 
                                  "Robert Collins <robertc@example.org>")
889
 
        self.assertEqual("John", my_config.username())
 
585
        self.assertEqual("John", my_config._get_user_id())
 
586
 
 
587
    def test_not_set_in_branch(self):
 
588
        branch = FakeBranch()
 
589
        my_config = config.BranchConfig(branch)
890
590
        branch.control_files.email = None
891
 
        self.assertEqual("Robert Collins <robertc@example.org>",
892
 
                         my_config.username())
893
 
 
894
 
    def test_not_set_in_branch(self):
895
 
        my_config = self.get_branch_config(sample_config_text)
896
 
        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))
897
594
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
898
595
                         my_config._get_user_id())
899
 
        my_config.branch.control_files.email = "John"
 
596
        branch.control_files.email = "John"
900
597
        self.assertEqual("John", my_config._get_user_id())
901
598
 
902
 
    def test_BZR_EMAIL_OVERRIDES(self):
903
 
        os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
 
599
    def test_BZREMAIL_OVERRIDES(self):
 
600
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
904
601
        branch = FakeBranch()
905
602
        my_config = config.BranchConfig(branch)
906
603
        self.assertEqual("Robert Collins <robertc@example.org>",
907
604
                         my_config.username())
908
605
    
909
606
    def test_signatures_forced(self):
910
 
        my_config = self.get_branch_config(
911
 
            global_config=sample_always_signatures)
912
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
913
 
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
914
 
        self.assertTrue(my_config.signature_needed())
915
 
 
916
 
    def test_signatures_forced_branch(self):
917
 
        my_config = self.get_branch_config(
918
 
            global_config=sample_ignore_signatures,
919
 
            branch_data_config=sample_always_signatures)
920
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
921
 
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
922
 
        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())
923
613
 
924
614
    def test_gpg_signing_command(self):
925
 
        my_config = self.get_branch_config(
926
 
            # branch data cannot set gpg_signing_command
927
 
            branch_data_config="gpg_signing_command=pgp")
 
615
        branch = FakeBranch()
 
616
        my_config = config.BranchConfig(branch)
928
617
        config_file = StringIO(sample_config_text.encode('utf-8'))
929
 
        my_config._get_global_config()._get_parser(config_file)
 
618
        (my_config._get_location_config().
 
619
            _get_global_config()._get_parser(config_file))
930
620
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
931
621
 
932
622
    def test_get_user_option_global(self):
933
623
        branch = FakeBranch()
934
624
        my_config = config.BranchConfig(branch)
935
625
        config_file = StringIO(sample_config_text.encode('utf-8'))
936
 
        (my_config._get_global_config()._get_parser(config_file))
 
626
        (my_config._get_location_config().
 
627
            _get_global_config()._get_parser(config_file))
937
628
        self.assertEqual('something',
938
629
                         my_config.get_user_option('user_global_option'))
939
630
 
940
631
    def test_post_commit_default(self):
941
632
        branch = FakeBranch()
942
 
        my_config = self.get_branch_config(sample_config_text, '/a/c',
943
 
                                           sample_branches_text)
944
 
        self.assertEqual(my_config.branch.base, '/a/c')
945
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
946
 
                         my_config.post_commit())
947
 
        my_config.set_user_option('post_commit', 'rmtree_root')
948
 
        # post-commit is ignored when bresent in branch data
949
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
950
 
                         my_config.post_commit())
951
 
        my_config.set_user_option('post_commit', 'rmtree_root',
952
 
                                  store=config.STORE_LOCATION)
953
 
        self.assertEqual('rmtree_root', my_config.post_commit())
954
 
 
955
 
    def test_config_precedence(self):
956
 
        my_config = self.get_branch_config(global_config=precedence_global)
957
 
        self.assertEqual(my_config.get_user_option('option'), 'global')
958
 
        my_config = self.get_branch_config(global_config=precedence_global, 
959
 
                                      branch_data_config=precedence_branch)
960
 
        self.assertEqual(my_config.get_user_option('option'), 'branch')
961
 
        my_config = self.get_branch_config(global_config=precedence_global, 
962
 
                                      branch_data_config=precedence_branch,
963
 
                                      location_config=precedence_location)
964
 
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
965
 
        my_config = self.get_branch_config(global_config=precedence_global, 
966
 
                                      branch_data_config=precedence_branch,
967
 
                                      location_config=precedence_location,
968
 
                                      location='http://example.com/specific')
969
 
        self.assertEqual(my_config.get_user_option('option'), 'exact')
 
633
        branch.base='/a/c'
 
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())
970
642
 
971
643
 
972
644
class TestMailAddressExtraction(TestCase):
974
646
    def test_extract_email_address(self):
975
647
        self.assertEqual('jane@test.com',
976
648
                         config.extract_email_address('Jane <jane@test.com>'))
977
 
        self.assertRaises(errors.NoEmailInUsername,
 
649
        self.assertRaises(errors.BzrError,
978
650
                          config.extract_email_address, 'Jane Tester')
979
 
 
980
 
 
981
 
class TestTreeConfig(TestCaseWithTransport):
982
 
 
983
 
    def test_get_value(self):
984
 
        """Test that retreiving a value from a section is possible"""
985
 
        branch = self.make_branch('.')
986
 
        tree_config = config.TreeConfig(branch)
987
 
        tree_config.set_option('value', 'key', 'SECTION')
988
 
        tree_config.set_option('value2', 'key2')
989
 
        tree_config.set_option('value3-top', 'key3')
990
 
        tree_config.set_option('value3-section', 'key3', 'SECTION')
991
 
        value = tree_config.get_option('key', 'SECTION')
992
 
        self.assertEqual(value, 'value')
993
 
        value = tree_config.get_option('key2')
994
 
        self.assertEqual(value, 'value2')
995
 
        self.assertEqual(tree_config.get_option('non-existant'), None)
996
 
        value = tree_config.get_option('non-existant', 'SECTION')
997
 
        self.assertEqual(value, None)
998
 
        value = tree_config.get_option('non-existant', default='default')
999
 
        self.assertEqual(value, 'default')
1000
 
        self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
1001
 
        value = tree_config.get_option('key2', 'NOSECTION', default='default')
1002
 
        self.assertEqual(value, 'default')
1003
 
        value = tree_config.get_option('key3')
1004
 
        self.assertEqual(value, 'value3-top')
1005
 
        value = tree_config.get_option('key3', 'SECTION')
1006
 
        self.assertEqual(value, 'value3-section')