~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: 2007-03-12 17:30:53 UTC
  • mfrom: (2338.3.1 hide-nested)
  • Revision ID: pqm@pqm.ubuntu.com-20070312173053-4cdb4cd14190d29e
Hide nested-tree commands and improve their docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
 
import bzrlib.config as config
27
 
import bzrlib.errors as errors
28
 
from bzrlib.tests import TestCase, TestCaseInTempDir
29
 
 
30
 
 
31
 
sample_config_text = ("[DEFAULT]\n"
32
 
                      "email=Robert Collins <robertc@example.com>\n"
33
 
                      "editor=vim\n"
34
 
                      "gpg_signing_command=gnome-gpg\n"
35
 
                      "user_global_option=something\n")
36
 
 
37
 
 
38
 
sample_always_signatures = ("[DEFAULT]\n"
39
 
                            "check_signatures=require\n")
40
 
 
41
 
 
42
 
sample_ignore_signatures = ("[DEFAULT]\n"
43
 
                            "check_signatures=ignore\n")
44
 
 
45
 
 
46
 
sample_maybe_signatures = ("[DEFAULT]\n"
47
 
                            "check_signatures=check-available\n")
48
 
 
49
 
 
50
 
sample_branches_text = ("[http://www.example.com]\n"
51
 
                        "# Top level policy\n"
52
 
                        "email=Robert Collins <robertc@example.org>\n"
53
 
                        "[http://www.example.com/useglobal]\n"
54
 
                        "# different project, forces global lookup\n"
55
 
                        "recurse=false\n"
56
 
                        "[/b/]\n"
57
 
                        "check_signatures=require\n"
58
 
                        "# test trailing / matching with no children\n"
59
 
                        "[/a/]\n"
60
 
                        "check_signatures=check-available\n"
61
 
                        "gpg_signing_command=false\n"
62
 
                        "user_local_option=local\n"
63
 
                        "# test trailing / matching\n"
64
 
                        "[/a/*]\n"
65
 
                        "#subdirs will match but not the parent\n"
66
 
                        "recurse=False\n"
67
 
                        "[/a/c]\n"
68
 
                        "check_signatures=ignore\n"
69
 
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
70
 
                        "#testing explicit beats globs\n")
 
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
 
35
 
 
36
 
 
37
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
"""
71
101
 
72
102
 
73
103
class InstrumentedConfigObj(object):
81
111
        self._calls.append(('__getitem__', key))
82
112
        return self
83
113
 
84
 
    def __init__(self, input):
85
 
        self._calls = [('__init__', input)]
 
114
    def __init__(self, input, encoding=None):
 
115
        self._calls = [('__init__', input, encoding)]
86
116
 
87
117
    def __setitem__(self, key, value):
88
118
        self._calls.append(('__setitem__', key, value))
89
119
 
90
 
    def write(self):
 
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
    def write(self, arg):
91
128
        self._calls.append(('write',))
92
129
 
 
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
 
93
138
 
94
139
class FakeBranch(object):
95
140
 
96
 
    def __init__(self):
97
 
        self.base = "http://example.com/branches/demo"
98
 
        self.control_files = FakeControlFiles()
 
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
99
153
 
100
154
 
101
155
class FakeControlFiles(object):
102
156
 
103
 
    def __init__(self):
104
 
        self.email = 'Robert Collins <robertc@example.net>\n'
 
157
    def __init__(self, user_id=None):
 
158
        self.email = user_id
 
159
        self.files = {}
105
160
 
106
161
    def get_utf8(self, filename):
107
162
        if filename != 'email':
110
165
            return StringIO(self.email)
111
166
        raise errors.NoSuchFile(filename)
112
167
 
 
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
 
113
177
 
114
178
class InstrumentedConfig(config.Config):
115
179
    """An instrumented config that supplies stubs for template methods."""
128
192
        return self._signatures
129
193
 
130
194
 
 
195
bool_config = """[DEFAULT]
 
196
active = true
 
197
inactive = false
 
198
[UPPERCASE]
 
199
active = True
 
200
nonactive = False
 
201
"""
 
202
class TestConfigObj(TestCase):
 
203
    def test_get_bool(self):
 
204
        from bzrlib.config import ConfigObj
 
205
        co = ConfigObj(StringIO(bool_config))
 
206
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
 
207
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
 
208
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
 
209
        self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
 
210
 
 
211
 
131
212
class TestConfig(TestCase):
132
213
 
133
214
    def test_constructs(self):
149
230
 
150
231
    def test_signatures_default(self):
151
232
        my_config = config.Config()
 
233
        self.assertFalse(my_config.signature_needed())
152
234
        self.assertEqual(config.CHECK_IF_POSSIBLE,
153
235
                         my_config.signature_checking())
 
236
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
 
237
                         my_config.signing_policy())
154
238
 
155
239
    def test_signatures_template_method(self):
156
240
        my_config = InstrumentedConfig()
176
260
        my_config = config.Config()
177
261
        self.assertEqual(None, my_config.post_commit())
178
262
 
 
263
    def test_log_format_default(self):
 
264
        my_config = config.Config()
 
265
        self.assertEqual('long', my_config.log_format())
 
266
 
179
267
 
180
268
class TestConfigPath(TestCase):
181
269
 
182
270
    def setUp(self):
183
271
        super(TestConfigPath, self).setUp()
184
 
        self.old_home = os.environ.get('HOME', None)
185
 
        self.old_appdata = os.environ.get('APPDATA', None)
186
272
        os.environ['HOME'] = '/home/bogus'
187
 
        os.environ['APPDATA'] = \
188
 
            r'C:\Documents and Settings\bogus\Application Data'
 
273
        if sys.platform == 'win32':
 
274
            os.environ['BZR_HOME'] = \
 
275
                r'C:\Documents and Settings\bogus\Application Data'
189
276
 
190
 
    def tearDown(self):
191
 
        if self.old_home is None:
192
 
            del os.environ['HOME']
193
 
        else:
194
 
            os.environ['HOME'] = self.old_home
195
 
        if self.old_appdata is None:
196
 
            del os.environ['APPDATA']
197
 
        else:
198
 
            os.environ['APPDATA'] = self.old_appdata
199
 
        super(TestConfigPath, self).tearDown()
200
 
    
201
277
    def test_config_dir(self):
202
278
        if sys.platform == 'win32':
203
279
            self.assertEqual(config.config_dir(), 
221
297
            self.assertEqual(config.branches_config_filename(),
222
298
                             '/home/bogus/.bazaar/branches.conf')
223
299
 
 
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
 
224
308
class TestIniConfig(TestCase):
225
309
 
226
310
    def test_contructs(self):
227
311
        my_config = config.IniBasedConfig("nothing")
228
312
 
229
313
    def test_from_fp(self):
230
 
        config_file = StringIO(sample_config_text)
 
314
        config_file = StringIO(sample_config_text.encode('utf-8'))
231
315
        my_config = config.IniBasedConfig(None)
232
316
        self.failUnless(
233
317
            isinstance(my_config._get_parser(file=config_file),
234
318
                        ConfigObj))
235
319
 
236
320
    def test_cached(self):
237
 
        config_file = StringIO(sample_config_text)
 
321
        config_file = StringIO(sample_config_text.encode('utf-8'))
238
322
        my_config = config.IniBasedConfig(None)
239
323
        parser = my_config._get_parser(file=config_file)
240
324
        self.failUnless(my_config._get_parser() is parser)
255
339
        finally:
256
340
            config.ConfigObj = oldparserclass
257
341
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
258
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
259
 
 
260
 
 
261
 
class TestBranchConfig(TestCaseInTempDir):
 
342
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
 
343
                                          'utf-8')])
 
344
 
 
345
 
 
346
class TestBranchConfig(TestCaseWithTransport):
262
347
 
263
348
    def test_constructs(self):
264
349
        branch = FakeBranch()
272
357
        self.assertEqual(branch.base, location_config.location)
273
358
        self.failUnless(location_config is my_config._get_location_config())
274
359
 
 
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
 
275
416
 
276
417
class TestGlobalConfigItems(TestCase):
277
418
 
278
419
    def test_user_id(self):
279
 
        config_file = StringIO(sample_config_text)
 
420
        config_file = StringIO(sample_config_text.encode('utf-8'))
280
421
        my_config = config.GlobalConfig()
281
422
        my_config._parser = my_config._get_parser(file=config_file)
282
 
        self.assertEqual("Robert Collins <robertc@example.com>",
 
423
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
283
424
                         my_config._get_user_id())
284
425
 
285
426
    def test_absent_user_id(self):
289
430
        self.assertEqual(None, my_config._get_user_id())
290
431
 
291
432
    def test_configured_editor(self):
292
 
        config_file = StringIO(sample_config_text)
 
433
        config_file = StringIO(sample_config_text.encode('utf-8'))
293
434
        my_config = config.GlobalConfig()
294
435
        my_config._parser = my_config._get_parser(file=config_file)
295
436
        self.assertEqual("vim", my_config.get_editor())
298
439
        config_file = StringIO(sample_always_signatures)
299
440
        my_config = config.GlobalConfig()
300
441
        my_config._parser = my_config._get_parser(file=config_file)
301
 
        self.assertEqual(config.CHECK_ALWAYS,
 
442
        self.assertEqual(config.CHECK_NEVER,
302
443
                         my_config.signature_checking())
 
444
        self.assertEqual(config.SIGN_ALWAYS,
 
445
                         my_config.signing_policy())
303
446
        self.assertEqual(True, my_config.signature_needed())
304
447
 
305
448
    def test_signatures_if_possible(self):
306
449
        config_file = StringIO(sample_maybe_signatures)
307
450
        my_config = config.GlobalConfig()
308
451
        my_config._parser = my_config._get_parser(file=config_file)
309
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
452
        self.assertEqual(config.CHECK_NEVER,
310
453
                         my_config.signature_checking())
 
454
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
 
455
                         my_config.signing_policy())
311
456
        self.assertEqual(False, my_config.signature_needed())
312
457
 
313
458
    def test_signatures_ignore(self):
314
459
        config_file = StringIO(sample_ignore_signatures)
315
460
        my_config = config.GlobalConfig()
316
461
        my_config._parser = my_config._get_parser(file=config_file)
317
 
        self.assertEqual(config.CHECK_NEVER,
 
462
        self.assertEqual(config.CHECK_ALWAYS,
318
463
                         my_config.signature_checking())
 
464
        self.assertEqual(config.SIGN_NEVER,
 
465
                         my_config.signing_policy())
319
466
        self.assertEqual(False, my_config.signature_needed())
320
467
 
321
468
    def _get_sample_config(self):
322
 
        config_file = StringIO(sample_config_text)
 
469
        config_file = StringIO(sample_config_text.encode('utf-8'))
323
470
        my_config = config.GlobalConfig()
324
471
        my_config._parser = my_config._get_parser(file=config_file)
325
472
        return my_config
352
499
        my_config = self._get_sample_config()
353
500
        self.assertEqual(None, my_config.post_commit())
354
501
 
355
 
 
356
 
class TestLocationConfig(TestCase):
 
502
    def test_configured_logformat(self):
 
503
        my_config = self._get_sample_config()
 
504
        self.assertEqual("short", my_config.log_format())
 
505
 
 
506
    def test_get_alias(self):
 
507
        my_config = self._get_sample_config()
 
508
        self.assertEqual('help', my_config.get_alias('h'))
 
509
 
 
510
    def test_get_no_alias(self):
 
511
        my_config = self._get_sample_config()
 
512
        self.assertEqual(None, my_config.get_alias('foo'))
 
513
 
 
514
    def test_get_long_alias(self):
 
515
        my_config = self._get_sample_config()
 
516
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
 
517
 
 
518
 
 
519
class TestLocationConfig(TestCaseInTempDir):
357
520
 
358
521
    def test_constructs(self):
359
522
        my_config = config.LocationConfig('http://example.com')
366
529
        # replace the class that is constructured, to check its parameters
367
530
        oldparserclass = config.ConfigObj
368
531
        config.ConfigObj = InstrumentedConfigObj
369
 
        my_config = config.LocationConfig('http://www.example.com')
370
532
        try:
 
533
            my_config = config.LocationConfig('http://www.example.com')
371
534
            parser = my_config._get_parser()
372
535
        finally:
373
536
            config.ConfigObj = oldparserclass
374
537
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
375
538
        self.assertEqual(parser._calls,
376
 
                         [('__init__', config.branches_config_filename())])
 
539
                         [('__init__', config.locations_config_filename(),
 
540
                           '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
377
553
 
378
554
    def test_get_global_config(self):
379
 
        my_config = config.LocationConfig('http://example.com')
 
555
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
380
556
        global_config = my_config._get_global_config()
381
557
        self.failUnless(isinstance(global_config, config.GlobalConfig))
382
558
        self.failUnless(global_config is my_config._get_global_config())
383
559
 
384
 
    def test__get_section_no_match(self):
385
 
        self.get_location_config('/')
386
 
        self.assertEqual(None, self.my_config._get_section())
 
560
    def test__get_matching_sections_no_match(self):
 
561
        self.get_branch_config('/')
 
562
        self.assertEqual([], self.my_location_config._get_matching_sections())
387
563
        
388
 
    def test__get_section_exact(self):
389
 
        self.get_location_config('http://www.example.com')
390
 
        self.assertEqual('http://www.example.com',
391
 
                         self.my_config._get_section())
 
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())
392
568
   
393
 
    def test__get_section_suffix_does_not(self):
394
 
        self.get_location_config('http://www.example.com-com')
395
 
        self.assertEqual(None, self.my_config._get_section())
396
 
 
397
 
    def test__get_section_subdir_recursive(self):
398
 
        self.get_location_config('http://www.example.com/com')
399
 
        self.assertEqual('http://www.example.com',
400
 
                         self.my_config._get_section())
401
 
 
402
 
    def test__get_section_subdir_matches(self):
403
 
        self.get_location_config('http://www.example.com/useglobal')
404
 
        self.assertEqual('http://www.example.com/useglobal',
405
 
                         self.my_config._get_section())
406
 
 
407
 
    def test__get_section_subdir_nonrecursive(self):
408
 
        self.get_location_config(
409
 
            'http://www.example.com/useglobal/childbranch')
410
 
        self.assertEqual('http://www.example.com',
411
 
                         self.my_config._get_section())
412
 
 
413
 
    def test__get_section_subdir_trailing_slash(self):
414
 
        self.get_location_config('/b')
415
 
        self.assertEqual('/b/', self.my_config._get_section())
416
 
 
417
 
    def test__get_section_subdir_child(self):
418
 
        self.get_location_config('/a/foo')
419
 
        self.assertEqual('/a/*', self.my_config._get_section())
420
 
 
421
 
    def test__get_section_subdir_child_child(self):
422
 
        self.get_location_config('/a/foo/bar')
423
 
        self.assertEqual('/a/', self.my_config._get_section())
424
 
 
425
 
    def test__get_section_trailing_slash_with_children(self):
426
 
        self.get_location_config('/a/')
427
 
        self.assertEqual('/a/', self.my_config._get_section())
428
 
 
429
 
    def test__get_section_explicit_over_glob(self):
430
 
        self.get_location_config('/a/c')
431
 
        self.assertEqual('/a/c', self.my_config._get_section())
432
 
 
433
 
    def get_location_config(self, location, global_config=None):
434
 
        if global_config is None:
435
 
            global_file = StringIO(sample_config_text)
436
 
        else:
437
 
            global_file = StringIO(global_config)
438
 
        branches_file = StringIO(sample_branches_text)
439
 
        self.my_config = config.LocationConfig(location)
440
 
        self.my_config._get_parser(branches_file)
441
 
        self.my_config._get_global_config()._get_parser(global_file)
 
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)
442
643
 
443
644
    def test_location_without_username(self):
444
 
        self.get_location_config('http://www.example.com/useglobal')
445
 
        self.assertEqual('Robert Collins <robertc@example.com>',
 
645
        self.get_branch_config('http://www.example.com/ignoreparent')
 
646
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
446
647
                         self.my_config.username())
447
648
 
448
649
    def test_location_not_listed(self):
449
 
        self.get_location_config('/home/robertc/sources')
450
 
        self.assertEqual('Robert Collins <robertc@example.com>',
 
650
        """Test that the global username is used when no location matches"""
 
651
        self.get_branch_config('/home/robertc/sources')
 
652
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
451
653
                         self.my_config.username())
452
654
 
453
655
    def test_overriding_location(self):
454
 
        self.get_location_config('http://www.example.com/foo')
 
656
        self.get_branch_config('http://www.example.com/foo')
455
657
        self.assertEqual('Robert Collins <robertc@example.org>',
456
658
                         self.my_config.username())
457
659
 
458
660
    def test_signatures_not_set(self):
459
 
        self.get_location_config('http://www.example.com',
 
661
        self.get_branch_config('http://www.example.com',
460
662
                                 global_config=sample_ignore_signatures)
461
 
        self.assertEqual(config.CHECK_NEVER,
 
663
        self.assertEqual(config.CHECK_ALWAYS,
462
664
                         self.my_config.signature_checking())
 
665
        self.assertEqual(config.SIGN_NEVER,
 
666
                         self.my_config.signing_policy())
463
667
 
464
668
    def test_signatures_never(self):
465
 
        self.get_location_config('/a/c')
 
669
        self.get_branch_config('/a/c')
466
670
        self.assertEqual(config.CHECK_NEVER,
467
671
                         self.my_config.signature_checking())
468
672
        
469
673
    def test_signatures_when_available(self):
470
 
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
 
674
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
471
675
        self.assertEqual(config.CHECK_IF_POSSIBLE,
472
676
                         self.my_config.signature_checking())
473
677
        
474
678
    def test_signatures_always(self):
475
 
        self.get_location_config('/b')
 
679
        self.get_branch_config('/b')
476
680
        self.assertEqual(config.CHECK_ALWAYS,
477
681
                         self.my_config.signature_checking())
478
682
        
479
683
    def test_gpg_signing_command(self):
480
 
        self.get_location_config('/b')
 
684
        self.get_branch_config('/b')
481
685
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
482
686
 
483
687
    def test_gpg_signing_command_missing(self):
484
 
        self.get_location_config('/a')
 
688
        self.get_branch_config('/a')
485
689
        self.assertEqual("false", self.my_config.gpg_signing_command())
486
690
 
487
691
    def test_get_user_option_global(self):
488
 
        self.get_location_config('/a')
 
692
        self.get_branch_config('/a')
489
693
        self.assertEqual('something',
490
694
                         self.my_config.get_user_option('user_global_option'))
491
695
 
492
696
    def test_get_user_option_local(self):
493
 
        self.get_location_config('/a')
 
697
        self.get_branch_config('/a')
494
698
        self.assertEqual('local',
495
699
                         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)
496
779
        
 
780
 
497
781
    def test_post_commit_default(self):
498
 
        self.get_location_config('/a/c')
 
782
        self.get_branch_config('/a/c')
499
783
        self.assertEqual('bzrlib.tests.test_config.post_commit',
500
784
                         self.my_config.post_commit())
501
785
 
502
 
 
503
 
class TestLocationConfig(TestCaseInTempDir):
504
 
 
505
 
    def get_location_config(self, location, global_config=None):
 
786
    def get_branch_config(self, location, global_config=None):
506
787
        if global_config is None:
507
 
            global_file = StringIO(sample_config_text)
 
788
            global_file = StringIO(sample_config_text.encode('utf-8'))
508
789
        else:
509
 
            global_file = StringIO(global_config)
510
 
        branches_file = StringIO(sample_branches_text)
511
 
        self.my_config = config.LocationConfig(location)
512
 
        self.my_config._get_parser(branches_file)
 
790
            global_file = StringIO(global_config.encode('utf-8'))
 
791
        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
513
797
        self.my_config._get_global_config()._get_parser(global_file)
514
798
 
515
799
    def test_set_user_setting_sets_and_saves(self):
516
 
        self.get_location_config('/a/c')
 
800
        self.get_branch_config('/a/c')
517
801
        record = InstrumentedConfigObj("foo")
518
 
        self.my_config._parser = record
 
802
        self.my_location_config._parser = record
519
803
 
520
804
        real_mkdir = os.mkdir
521
805
        self.created = False
526
810
 
527
811
        os.mkdir = checked_mkdir
528
812
        try:
529
 
            self.my_config.set_user_option('foo', 'bar')
 
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)
530
818
        finally:
531
819
            os.mkdir = real_mkdir
532
820
 
536
824
                          ('__setitem__', '/a/c', {}),
537
825
                          ('__getitem__', '/a/c'),
538
826
                          ('__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'),
539
835
                          ('write',)],
540
836
                         record._calls[1:])
541
837
 
542
 
 
543
 
class TestBranchConfigItems(TestCase):
 
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
544
880
 
545
881
    def test_user_id(self):
546
 
        branch = FakeBranch()
 
882
        branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
547
883
        my_config = config.BranchConfig(branch)
548
884
        self.assertEqual("Robert Collins <robertc@example.net>",
549
 
                         my_config._get_user_id())
 
885
                         my_config.username())
550
886
        branch.control_files.email = "John"
551
 
        self.assertEqual("John", my_config._get_user_id())
 
887
        my_config.set_user_option('email', 
 
888
                                  "Robert Collins <robertc@example.org>")
 
889
        self.assertEqual("John", my_config.username())
 
890
        branch.control_files.email = None
 
891
        self.assertEqual("Robert Collins <robertc@example.org>",
 
892
                         my_config.username())
552
893
 
553
894
    def test_not_set_in_branch(self):
554
 
        branch = FakeBranch()
555
 
        my_config = config.BranchConfig(branch)
556
 
        branch.control_files.email = None
557
 
        config_file = StringIO(sample_config_text)
558
 
        (my_config._get_location_config().
559
 
            _get_global_config()._get_parser(config_file))
560
 
        self.assertEqual("Robert Collins <robertc@example.com>",
 
895
        my_config = self.get_branch_config(sample_config_text)
 
896
        my_config.branch.control_files.email = None
 
897
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
561
898
                         my_config._get_user_id())
562
 
        branch.control_files.email = "John"
 
899
        my_config.branch.control_files.email = "John"
563
900
        self.assertEqual("John", my_config._get_user_id())
564
901
 
565
 
    def test_BZREMAIL_OVERRIDES(self):
566
 
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
 
902
    def test_BZR_EMAIL_OVERRIDES(self):
 
903
        os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
567
904
        branch = FakeBranch()
568
905
        my_config = config.BranchConfig(branch)
569
906
        self.assertEqual("Robert Collins <robertc@example.org>",
570
907
                         my_config.username())
571
908
    
572
909
    def test_signatures_forced(self):
573
 
        branch = FakeBranch()
574
 
        my_config = config.BranchConfig(branch)
575
 
        config_file = StringIO(sample_always_signatures)
576
 
        (my_config._get_location_config().
577
 
            _get_global_config()._get_parser(config_file))
578
 
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
 
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())
579
923
 
580
924
    def test_gpg_signing_command(self):
581
 
        branch = FakeBranch()
582
 
        my_config = config.BranchConfig(branch)
583
 
        config_file = StringIO(sample_config_text)
584
 
        (my_config._get_location_config().
585
 
            _get_global_config()._get_parser(config_file))
 
925
        my_config = self.get_branch_config(
 
926
            # branch data cannot set gpg_signing_command
 
927
            branch_data_config="gpg_signing_command=pgp")
 
928
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
929
        my_config._get_global_config()._get_parser(config_file)
586
930
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
587
931
 
588
932
    def test_get_user_option_global(self):
589
933
        branch = FakeBranch()
590
934
        my_config = config.BranchConfig(branch)
591
 
        config_file = StringIO(sample_config_text)
592
 
        (my_config._get_location_config().
593
 
            _get_global_config()._get_parser(config_file))
 
935
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
936
        (my_config._get_global_config()._get_parser(config_file))
594
937
        self.assertEqual('something',
595
938
                         my_config.get_user_option('user_global_option'))
596
939
 
597
940
    def test_post_commit_default(self):
598
941
        branch = FakeBranch()
599
 
        branch.base='/a/c'
600
 
        my_config = config.BranchConfig(branch)
601
 
        config_file = StringIO(sample_config_text)
602
 
        (my_config._get_location_config().
603
 
            _get_global_config()._get_parser(config_file))
604
 
        branch_file = StringIO(sample_branches_text)
605
 
        my_config._get_location_config()._get_parser(branch_file)
606
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
607
 
                         my_config.post_commit())
 
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')
608
970
 
609
971
 
610
972
class TestMailAddressExtraction(TestCase):
612
974
    def test_extract_email_address(self):
613
975
        self.assertEqual('jane@test.com',
614
976
                         config.extract_email_address('Jane <jane@test.com>'))
615
 
        self.assertRaises(errors.BzrError,
 
977
        self.assertRaises(errors.NoEmailInUsername,
616
978
                          config.extract_email_address, 'Jane Tester')