~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
23
23
import sys
24
24
 
25
25
#import bzrlib specific imports here
26
 
from bzrlib import (
27
 
    config,
28
 
    errors,
29
 
    osutils,
30
 
    urlutils,
31
 
    )
32
 
from bzrlib.branch import Branch
33
 
from bzrlib.bzrdir import BzrDir
34
 
from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
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
 
"""
 
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")
101
71
 
102
72
 
103
73
class InstrumentedConfigObj(object):
111
81
        self._calls.append(('__getitem__', key))
112
82
        return self
113
83
 
114
 
    def __init__(self, input, encoding=None):
115
 
        self._calls = [('__init__', input, encoding)]
 
84
    def __init__(self, input):
 
85
        self._calls = [('__init__', input)]
116
86
 
117
87
    def __setitem__(self, key, value):
118
88
        self._calls.append(('__setitem__', key, value))
119
89
 
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):
 
90
    def write(self):
128
91
        self._calls.append(('write',))
129
92
 
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
93
 
139
94
class FakeBranch(object):
140
95
 
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
 
96
    def __init__(self):
 
97
        self.base = "http://example.com/branches/demo"
 
98
        self.control_files = FakeControlFiles()
153
99
 
154
100
 
155
101
class FakeControlFiles(object):
156
102
 
157
 
    def __init__(self, user_id=None):
158
 
        self.email = user_id
159
 
        self.files = {}
 
103
    def __init__(self):
 
104
        self.email = 'Robert Collins <robertc@example.net>\n'
160
105
 
161
106
    def get_utf8(self, filename):
162
107
        if filename != 'email':
165
110
            return StringIO(self.email)
166
111
        raise errors.NoSuchFile(filename)
167
112
 
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
113
 
178
114
class InstrumentedConfig(config.Config):
179
115
    """An instrumented config that supplies stubs for template methods."""
192
128
        return self._signatures
193
129
 
194
130
 
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
 
 
212
131
class TestConfig(TestCase):
213
132
 
214
133
    def test_constructs(self):
230
149
 
231
150
    def test_signatures_default(self):
232
151
        my_config = config.Config()
233
 
        self.assertFalse(my_config.signature_needed())
234
152
        self.assertEqual(config.CHECK_IF_POSSIBLE,
235
153
                         my_config.signature_checking())
236
 
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
237
 
                         my_config.signing_policy())
238
154
 
239
155
    def test_signatures_template_method(self):
240
156
        my_config = InstrumentedConfig()
260
176
        my_config = config.Config()
261
177
        self.assertEqual(None, my_config.post_commit())
262
178
 
263
 
    def test_log_format_default(self):
264
 
        my_config = config.Config()
265
 
        self.assertEqual('long', my_config.log_format())
266
 
 
267
179
 
268
180
class TestConfigPath(TestCase):
269
181
 
309
221
            self.assertEqual(config.branches_config_filename(),
310
222
                             '/home/bogus/.bazaar/branches.conf')
311
223
 
312
 
    def test_locations_config_filename(self):
313
 
        if sys.platform == 'win32':
314
 
            self.assertEqual(config.locations_config_filename(), 
315
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
316
 
        else:
317
 
            self.assertEqual(config.locations_config_filename(),
318
 
                             '/home/bogus/.bazaar/locations.conf')
319
 
 
320
224
class TestIniConfig(TestCase):
321
225
 
322
226
    def test_contructs(self):
323
227
        my_config = config.IniBasedConfig("nothing")
324
228
 
325
229
    def test_from_fp(self):
326
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
230
        config_file = StringIO(sample_config_text)
327
231
        my_config = config.IniBasedConfig(None)
328
232
        self.failUnless(
329
233
            isinstance(my_config._get_parser(file=config_file),
330
234
                        ConfigObj))
331
235
 
332
236
    def test_cached(self):
333
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
237
        config_file = StringIO(sample_config_text)
334
238
        my_config = config.IniBasedConfig(None)
335
239
        parser = my_config._get_parser(file=config_file)
336
240
        self.failUnless(my_config._get_parser() is parser)
351
255
        finally:
352
256
            config.ConfigObj = oldparserclass
353
257
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
354
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
355
 
                                          'utf-8')])
356
 
 
357
 
 
358
 
class TestBranchConfig(TestCaseWithTransport):
 
258
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
 
259
 
 
260
 
 
261
class TestBranchConfig(TestCaseInTempDir):
359
262
 
360
263
    def test_constructs(self):
361
264
        branch = FakeBranch()
369
272
        self.assertEqual(branch.base, location_config.location)
370
273
        self.failUnless(location_config is my_config._get_location_config())
371
274
 
372
 
    def test_get_config(self):
373
 
        """The Branch.get_config method works properly"""
374
 
        b = BzrDir.create_standalone_workingtree('.').branch
375
 
        my_config = b.get_config()
376
 
        self.assertIs(my_config.get_user_option('wacky'), None)
377
 
        my_config.set_user_option('wacky', 'unlikely')
378
 
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
379
 
 
380
 
        # Ensure we get the same thing if we start again
381
 
        b2 = Branch.open('.')
382
 
        my_config2 = b2.get_config()
383
 
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
384
 
 
385
 
    def test_has_explicit_nickname(self):
386
 
        b = self.make_branch('.')
387
 
        self.assertFalse(b.get_config().has_explicit_nickname())
388
 
        b.nick = 'foo'
389
 
        self.assertTrue(b.get_config().has_explicit_nickname())
390
 
 
391
 
    def test_config_url(self):
392
 
        """The Branch.get_config will use section that uses a local url"""
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
 
        local_url = urlutils.local_path_to_url('branch')
399
 
        open(locations, 'wb').write('[%s]\nnickname = foobar' 
400
 
                                    % (local_url,))
401
 
        self.assertEqual('foobar', branch.nick)
402
 
 
403
 
    def test_config_local_path(self):
404
 
        """The Branch.get_config will use a local system path"""
405
 
        branch = self.make_branch('branch')
406
 
        self.assertEqual('branch', branch.nick)
407
 
 
408
 
        locations = config.locations_config_filename()
409
 
        config.ensure_config_dir_exists()
410
 
        open(locations, 'wb').write('[%s/branch]\nnickname = barry' 
411
 
                                    % (osutils.getcwd().encode('utf8'),))
412
 
        self.assertEqual('barry', branch.nick)
413
 
 
414
 
    def test_config_creates_local(self):
415
 
        """Creating a new entry in config uses a local path."""
416
 
        branch = self.make_branch('branch', format='knit')
417
 
        branch.set_push_location('http://foobar')
418
 
        locations = config.locations_config_filename()
419
 
        local_path = osutils.getcwd().encode('utf8')
420
 
        # Surprisingly ConfigObj doesn't create a trailing newline
421
 
        self.check_file_contents(locations,
422
 
            '[%s/branch]\npush_location = http://foobar\npush_location:policy = norecurse' % (local_path,))
423
 
 
424
 
    def test_autonick_urlencoded(self):
425
 
        b = self.make_branch('!repo')
426
 
        self.assertEqual('!repo', b.get_config().get_nickname())
427
 
 
428
275
 
429
276
class TestGlobalConfigItems(TestCase):
430
277
 
431
278
    def test_user_id(self):
432
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
279
        config_file = StringIO(sample_config_text)
433
280
        my_config = config.GlobalConfig()
434
281
        my_config._parser = my_config._get_parser(file=config_file)
435
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
 
282
        self.assertEqual("Robert Collins <robertc@example.com>",
436
283
                         my_config._get_user_id())
437
284
 
438
285
    def test_absent_user_id(self):
442
289
        self.assertEqual(None, my_config._get_user_id())
443
290
 
444
291
    def test_configured_editor(self):
445
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
292
        config_file = StringIO(sample_config_text)
446
293
        my_config = config.GlobalConfig()
447
294
        my_config._parser = my_config._get_parser(file=config_file)
448
295
        self.assertEqual("vim", my_config.get_editor())
451
298
        config_file = StringIO(sample_always_signatures)
452
299
        my_config = config.GlobalConfig()
453
300
        my_config._parser = my_config._get_parser(file=config_file)
454
 
        self.assertEqual(config.CHECK_NEVER,
 
301
        self.assertEqual(config.CHECK_ALWAYS,
455
302
                         my_config.signature_checking())
456
 
        self.assertEqual(config.SIGN_ALWAYS,
457
 
                         my_config.signing_policy())
458
303
        self.assertEqual(True, my_config.signature_needed())
459
304
 
460
305
    def test_signatures_if_possible(self):
461
306
        config_file = StringIO(sample_maybe_signatures)
462
307
        my_config = config.GlobalConfig()
463
308
        my_config._parser = my_config._get_parser(file=config_file)
464
 
        self.assertEqual(config.CHECK_NEVER,
 
309
        self.assertEqual(config.CHECK_IF_POSSIBLE,
465
310
                         my_config.signature_checking())
466
 
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
467
 
                         my_config.signing_policy())
468
311
        self.assertEqual(False, my_config.signature_needed())
469
312
 
470
313
    def test_signatures_ignore(self):
471
314
        config_file = StringIO(sample_ignore_signatures)
472
315
        my_config = config.GlobalConfig()
473
316
        my_config._parser = my_config._get_parser(file=config_file)
474
 
        self.assertEqual(config.CHECK_ALWAYS,
 
317
        self.assertEqual(config.CHECK_NEVER,
475
318
                         my_config.signature_checking())
476
 
        self.assertEqual(config.SIGN_NEVER,
477
 
                         my_config.signing_policy())
478
319
        self.assertEqual(False, my_config.signature_needed())
479
320
 
480
321
    def _get_sample_config(self):
481
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
322
        config_file = StringIO(sample_config_text)
482
323
        my_config = config.GlobalConfig()
483
324
        my_config._parser = my_config._get_parser(file=config_file)
484
325
        return my_config
511
352
        my_config = self._get_sample_config()
512
353
        self.assertEqual(None, my_config.post_commit())
513
354
 
514
 
    def test_configured_logformat(self):
515
 
        my_config = self._get_sample_config()
516
 
        self.assertEqual("short", my_config.log_format())
517
 
 
518
 
    def test_get_alias(self):
519
 
        my_config = self._get_sample_config()
520
 
        self.assertEqual('help', my_config.get_alias('h'))
521
 
 
522
 
    def test_get_no_alias(self):
523
 
        my_config = self._get_sample_config()
524
 
        self.assertEqual(None, my_config.get_alias('foo'))
525
 
 
526
 
    def test_get_long_alias(self):
527
 
        my_config = self._get_sample_config()
528
 
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
529
 
 
530
 
 
531
 
class TestLocationConfig(TestCaseInTempDir):
 
355
 
 
356
class TestLocationConfig(TestCase):
532
357
 
533
358
    def test_constructs(self):
534
359
        my_config = config.LocationConfig('http://example.com')
541
366
        # replace the class that is constructured, to check its parameters
542
367
        oldparserclass = config.ConfigObj
543
368
        config.ConfigObj = InstrumentedConfigObj
 
369
        my_config = config.LocationConfig('http://www.example.com')
544
370
        try:
545
 
            my_config = config.LocationConfig('http://www.example.com')
546
371
            parser = my_config._get_parser()
547
372
        finally:
548
373
            config.ConfigObj = oldparserclass
549
374
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
550
375
        self.assertEqual(parser._calls,
551
 
                         [('__init__', config.locations_config_filename(),
552
 
                           'utf-8')])
553
 
        config.ensure_config_dir_exists()
554
 
        #os.mkdir(config.config_dir())
555
 
        f = file(config.branches_config_filename(), 'wb')
556
 
        f.write('')
557
 
        f.close()
558
 
        oldparserclass = config.ConfigObj
559
 
        config.ConfigObj = InstrumentedConfigObj
560
 
        try:
561
 
            my_config = config.LocationConfig('http://www.example.com')
562
 
            parser = my_config._get_parser()
563
 
        finally:
564
 
            config.ConfigObj = oldparserclass
 
376
                         [('__init__', config.branches_config_filename())])
565
377
 
566
378
    def test_get_global_config(self):
567
 
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
 
379
        my_config = config.LocationConfig('http://example.com')
568
380
        global_config = my_config._get_global_config()
569
381
        self.failUnless(isinstance(global_config, config.GlobalConfig))
570
382
        self.failUnless(global_config is my_config._get_global_config())
571
383
 
572
 
    def test__get_matching_sections_no_match(self):
573
 
        self.get_branch_config('/')
574
 
        self.assertEqual([], self.my_location_config._get_matching_sections())
 
384
    def test__get_section_no_match(self):
 
385
        self.get_location_config('/')
 
386
        self.assertEqual(None, self.my_config._get_section())
575
387
        
576
 
    def test__get_matching_sections_exact(self):
577
 
        self.get_branch_config('http://www.example.com')
578
 
        self.assertEqual([('http://www.example.com', '')],
579
 
                         self.my_location_config._get_matching_sections())
 
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())
580
392
   
581
 
    def test__get_matching_sections_suffix_does_not(self):
582
 
        self.get_branch_config('http://www.example.com-com')
583
 
        self.assertEqual([], self.my_location_config._get_matching_sections())
584
 
 
585
 
    def test__get_matching_sections_subdir_recursive(self):
586
 
        self.get_branch_config('http://www.example.com/com')
587
 
        self.assertEqual([('http://www.example.com', 'com')],
588
 
                         self.my_location_config._get_matching_sections())
589
 
 
590
 
    def test__get_matching_sections_ignoreparent(self):
591
 
        self.get_branch_config('http://www.example.com/ignoreparent')
592
 
        self.assertEqual([('http://www.example.com/ignoreparent', '')],
593
 
                         self.my_location_config._get_matching_sections())
594
 
 
595
 
    def test__get_matching_sections_ignoreparent_subdir(self):
596
 
        self.get_branch_config(
597
 
            'http://www.example.com/ignoreparent/childbranch')
598
 
        self.assertEqual([('http://www.example.com/ignoreparent', 'childbranch')],
599
 
                         self.my_location_config._get_matching_sections())
600
 
 
601
 
    def test__get_matching_sections_subdir_trailing_slash(self):
602
 
        self.get_branch_config('/b')
603
 
        self.assertEqual([('/b/', '')],
604
 
                         self.my_location_config._get_matching_sections())
605
 
 
606
 
    def test__get_matching_sections_subdir_child(self):
607
 
        self.get_branch_config('/a/foo')
608
 
        self.assertEqual([('/a/*', ''), ('/a/', 'foo')],
609
 
                         self.my_location_config._get_matching_sections())
610
 
 
611
 
    def test__get_matching_sections_subdir_child_child(self):
612
 
        self.get_branch_config('/a/foo/bar')
613
 
        self.assertEqual([('/a/*', 'bar'), ('/a/', 'foo/bar')],
614
 
                         self.my_location_config._get_matching_sections())
615
 
 
616
 
    def test__get_matching_sections_trailing_slash_with_children(self):
617
 
        self.get_branch_config('/a/')
618
 
        self.assertEqual([('/a/', '')],
619
 
                         self.my_location_config._get_matching_sections())
620
 
 
621
 
    def test__get_matching_sections_explicit_over_glob(self):
622
 
        # XXX: 2006-09-08 jamesh
623
 
        # This test only passes because ord('c') > ord('*').  If there
624
 
        # was a config section for '/a/?', it would get precedence
625
 
        # over '/a/c'.
626
 
        self.get_branch_config('/a/c')
627
 
        self.assertEqual([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')],
628
 
                         self.my_location_config._get_matching_sections())
629
 
 
630
 
    def test__get_option_policy_normal(self):
631
 
        self.get_branch_config('http://www.example.com')
632
 
        self.assertEqual(
633
 
            self.my_location_config._get_config_policy(
634
 
            'http://www.example.com', 'normal_option'),
635
 
            config.POLICY_NONE)
636
 
 
637
 
    def test__get_option_policy_norecurse(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', 'norecurse_option'),
642
 
            config.POLICY_NORECURSE)
643
 
        # Test old recurse=False setting:
644
 
        self.assertEqual(
645
 
            self.my_location_config._get_option_policy(
646
 
            'http://www.example.com/norecurse', 'normal_option'),
647
 
            config.POLICY_NORECURSE)
648
 
 
649
 
    def test__get_option_policy_normal(self):
650
 
        self.get_branch_config('http://www.example.com')
651
 
        self.assertEqual(
652
 
            self.my_location_config._get_option_policy(
653
 
            'http://www.example.com', 'appendpath_option'),
654
 
            config.POLICY_APPENDPATH)
 
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)
655
442
 
656
443
    def test_location_without_username(self):
657
 
        self.get_branch_config('http://www.example.com/ignoreparent')
658
 
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
 
444
        self.get_location_config('http://www.example.com/useglobal')
 
445
        self.assertEqual('Robert Collins <robertc@example.com>',
659
446
                         self.my_config.username())
660
447
 
661
448
    def test_location_not_listed(self):
662
 
        """Test that the global username is used when no location matches"""
663
 
        self.get_branch_config('/home/robertc/sources')
664
 
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
 
449
        self.get_location_config('/home/robertc/sources')
 
450
        self.assertEqual('Robert Collins <robertc@example.com>',
665
451
                         self.my_config.username())
666
452
 
667
453
    def test_overriding_location(self):
668
 
        self.get_branch_config('http://www.example.com/foo')
 
454
        self.get_location_config('http://www.example.com/foo')
669
455
        self.assertEqual('Robert Collins <robertc@example.org>',
670
456
                         self.my_config.username())
671
457
 
672
458
    def test_signatures_not_set(self):
673
 
        self.get_branch_config('http://www.example.com',
 
459
        self.get_location_config('http://www.example.com',
674
460
                                 global_config=sample_ignore_signatures)
675
 
        self.assertEqual(config.CHECK_ALWAYS,
 
461
        self.assertEqual(config.CHECK_NEVER,
676
462
                         self.my_config.signature_checking())
677
 
        self.assertEqual(config.SIGN_NEVER,
678
 
                         self.my_config.signing_policy())
679
463
 
680
464
    def test_signatures_never(self):
681
 
        self.get_branch_config('/a/c')
 
465
        self.get_location_config('/a/c')
682
466
        self.assertEqual(config.CHECK_NEVER,
683
467
                         self.my_config.signature_checking())
684
468
        
685
469
    def test_signatures_when_available(self):
686
 
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
 
470
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
687
471
        self.assertEqual(config.CHECK_IF_POSSIBLE,
688
472
                         self.my_config.signature_checking())
689
473
        
690
474
    def test_signatures_always(self):
691
 
        self.get_branch_config('/b')
 
475
        self.get_location_config('/b')
692
476
        self.assertEqual(config.CHECK_ALWAYS,
693
477
                         self.my_config.signature_checking())
694
478
        
695
479
    def test_gpg_signing_command(self):
696
 
        self.get_branch_config('/b')
 
480
        self.get_location_config('/b')
697
481
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
698
482
 
699
483
    def test_gpg_signing_command_missing(self):
700
 
        self.get_branch_config('/a')
 
484
        self.get_location_config('/a')
701
485
        self.assertEqual("false", self.my_config.gpg_signing_command())
702
486
 
703
487
    def test_get_user_option_global(self):
704
 
        self.get_branch_config('/a')
 
488
        self.get_location_config('/a')
705
489
        self.assertEqual('something',
706
490
                         self.my_config.get_user_option('user_global_option'))
707
491
 
708
492
    def test_get_user_option_local(self):
709
 
        self.get_branch_config('/a')
 
493
        self.get_location_config('/a')
710
494
        self.assertEqual('local',
711
495
                         self.my_config.get_user_option('user_local_option'))
712
 
 
713
 
    def test_get_user_option_appendpath(self):
714
 
        # returned as is for the base path:
715
 
        self.get_branch_config('http://www.example.com')
716
 
        self.assertEqual('append',
717
 
                         self.my_config.get_user_option('appendpath_option'))
718
 
        # Extra path components get appended:
719
 
        self.get_branch_config('http://www.example.com/a/b/c')
720
 
        self.assertEqual('append/a/b/c',
721
 
                         self.my_config.get_user_option('appendpath_option'))
722
 
        # Overriden for http://www.example.com/dir, where it is a
723
 
        # normal option:
724
 
        self.get_branch_config('http://www.example.com/dir/a/b/c')
725
 
        self.assertEqual('normal',
726
 
                         self.my_config.get_user_option('appendpath_option'))
727
 
 
728
 
    def test_get_user_option_norecurse(self):
729
 
        self.get_branch_config('http://www.example.com')
730
 
        self.assertEqual('norecurse',
731
 
                         self.my_config.get_user_option('norecurse_option'))
732
 
        self.get_branch_config('http://www.example.com/dir')
733
 
        self.assertEqual(None,
734
 
                         self.my_config.get_user_option('norecurse_option'))
735
 
        # http://www.example.com/norecurse is a recurse=False section
736
 
        # that redefines normal_option.  Subdirectories do not pick up
737
 
        # this redefinition.
738
 
        self.get_branch_config('http://www.example.com/norecurse')
739
 
        self.assertEqual('norecurse',
740
 
                         self.my_config.get_user_option('normal_option'))
741
 
        self.get_branch_config('http://www.example.com/norecurse/subdir')
742
 
        self.assertEqual('normal',
743
 
                         self.my_config.get_user_option('normal_option'))
744
 
 
745
 
    def test_set_user_option_norecurse(self):
746
 
        self.get_branch_config('http://www.example.com')
747
 
        self.my_config.set_user_option('foo', 'bar',
748
 
                                       store=config.STORE_LOCATION_NORECURSE)
749
 
        self.assertEqual(
750
 
            self.my_location_config._get_option_policy(
751
 
            'http://www.example.com', 'foo'),
752
 
            config.POLICY_NORECURSE)
753
 
 
754
 
    def test_set_user_option_appendpath(self):
755
 
        self.get_branch_config('http://www.example.com')
756
 
        self.my_config.set_user_option('foo', 'bar',
757
 
                                       store=config.STORE_LOCATION_APPENDPATH)
758
 
        self.assertEqual(
759
 
            self.my_location_config._get_option_policy(
760
 
            'http://www.example.com', 'foo'),
761
 
            config.POLICY_APPENDPATH)
762
 
 
763
 
    def test_set_user_option_change_policy(self):
764
 
        self.get_branch_config('http://www.example.com')
765
 
        self.my_config.set_user_option('norecurse_option', 'normal',
766
 
                                       store=config.STORE_LOCATION)
767
 
        self.assertEqual(
768
 
            self.my_location_config._get_option_policy(
769
 
            'http://www.example.com', 'norecurse_option'),
770
 
            config.POLICY_NONE)
771
 
 
772
 
    def test_set_user_option_recurse_false_section(self):
773
 
        # The following section has recurse=False set.  The test is to
774
 
        # make sure that a normal option can be added to the section,
775
 
        # converting recurse=False to the norecurse policy.
776
 
        self.get_branch_config('http://www.example.com/norecurse')
777
 
        self.callDeprecated(['The recurse option is deprecated as of 0.14.  '
778
 
                             'The section "http://www.example.com/norecurse" '
779
 
                             'has been converted to use policies.'],
780
 
                            self.my_config.set_user_option,
781
 
                            'foo', 'bar', store=config.STORE_LOCATION)
782
 
        self.assertEqual(
783
 
            self.my_location_config._get_option_policy(
784
 
            'http://www.example.com/norecurse', 'foo'),
785
 
            config.POLICY_NONE)
786
 
        # The previously existing option is still norecurse:
787
 
        self.assertEqual(
788
 
            self.my_location_config._get_option_policy(
789
 
            'http://www.example.com/norecurse', 'normal_option'),
790
 
            config.POLICY_NORECURSE)
791
496
        
792
 
 
793
497
    def test_post_commit_default(self):
794
 
        self.get_branch_config('/a/c')
 
498
        self.get_location_config('/a/c')
795
499
        self.assertEqual('bzrlib.tests.test_config.post_commit',
796
500
                         self.my_config.post_commit())
797
501
 
798
 
    def get_branch_config(self, location, global_config=None):
 
502
 
 
503
class TestLocationConfig(TestCaseInTempDir):
 
504
 
 
505
    def get_location_config(self, location, global_config=None):
799
506
        if global_config is None:
800
 
            global_file = StringIO(sample_config_text.encode('utf-8'))
 
507
            global_file = StringIO(sample_config_text)
801
508
        else:
802
 
            global_file = StringIO(global_config.encode('utf-8'))
803
 
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
804
 
        self.my_config = config.BranchConfig(FakeBranch(location))
805
 
        # Force location config to use specified file
806
 
        self.my_location_config = self.my_config._get_location_config()
807
 
        self.my_location_config._get_parser(branches_file)
808
 
        # Force global config to use specified file
 
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)
809
513
        self.my_config._get_global_config()._get_parser(global_file)
810
514
 
811
515
    def test_set_user_setting_sets_and_saves(self):
812
 
        self.get_branch_config('/a/c')
 
516
        self.get_location_config('/a/c')
813
517
        record = InstrumentedConfigObj("foo")
814
 
        self.my_location_config._parser = record
 
518
        self.my_config._parser = record
815
519
 
816
520
        real_mkdir = os.mkdir
817
521
        self.created = False
822
526
 
823
527
        os.mkdir = checked_mkdir
824
528
        try:
825
 
            self.callDeprecated(['The recurse option is deprecated as of '
826
 
                                 '0.14.  The section "/a/c" has been '
827
 
                                 'converted to use policies.'],
828
 
                                self.my_config.set_user_option,
829
 
                                'foo', 'bar', store=config.STORE_LOCATION)
 
529
            self.my_config.set_user_option('foo', 'bar')
830
530
        finally:
831
531
            os.mkdir = real_mkdir
832
532
 
836
536
                          ('__setitem__', '/a/c', {}),
837
537
                          ('__getitem__', '/a/c'),
838
538
                          ('__setitem__', 'foo', 'bar'),
839
 
                          ('__getitem__', '/a/c'),
840
 
                          ('as_bool', 'recurse'),
841
 
                          ('__getitem__', '/a/c'),
842
 
                          ('__delitem__', 'recurse'),
843
 
                          ('__getitem__', '/a/c'),
844
 
                          ('keys',),
845
 
                          ('__getitem__', '/a/c'),
846
 
                          ('__contains__', 'foo:policy'),
847
539
                          ('write',)],
848
540
                         record._calls[1:])
849
541
 
850
 
    def test_set_user_setting_sets_and_saves2(self):
851
 
        self.get_branch_config('/a/c')
852
 
        self.assertIs(self.my_config.get_user_option('foo'), None)
853
 
        self.my_config.set_user_option('foo', 'bar')
854
 
        self.assertEqual(
855
 
            self.my_config.branch.control_files.files['branch.conf'], 
856
 
            'foo = bar')
857
 
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
858
 
        self.my_config.set_user_option('foo', 'baz',
859
 
                                       store=config.STORE_LOCATION)
860
 
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
861
 
        self.my_config.set_user_option('foo', 'qux')
862
 
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
863
 
        
864
 
 
865
 
precedence_global = 'option = global'
866
 
precedence_branch = 'option = branch'
867
 
precedence_location = """
868
 
[http://]
869
 
recurse = true
870
 
option = recurse
871
 
[http://example.com/specific]
872
 
option = exact
873
 
"""
874
 
 
875
 
 
876
 
class TestBranchConfigItems(TestCaseInTempDir):
877
 
 
878
 
    def get_branch_config(self, global_config=None, location=None, 
879
 
                          location_config=None, branch_data_config=None):
880
 
        my_config = config.BranchConfig(FakeBranch(location))
881
 
        if global_config is not None:
882
 
            global_file = StringIO(global_config.encode('utf-8'))
883
 
            my_config._get_global_config()._get_parser(global_file)
884
 
        self.my_location_config = my_config._get_location_config()
885
 
        if location_config is not None:
886
 
            location_file = StringIO(location_config.encode('utf-8'))
887
 
            self.my_location_config._get_parser(location_file)
888
 
        if branch_data_config is not None:
889
 
            my_config.branch.control_files.files['branch.conf'] = \
890
 
                branch_data_config
891
 
        return my_config
 
542
 
 
543
class TestBranchConfigItems(TestCase):
892
544
 
893
545
    def test_user_id(self):
894
 
        branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
 
546
        branch = FakeBranch()
895
547
        my_config = config.BranchConfig(branch)
896
548
        self.assertEqual("Robert Collins <robertc@example.net>",
897
 
                         my_config.username())
 
549
                         my_config._get_user_id())
898
550
        branch.control_files.email = "John"
899
 
        my_config.set_user_option('email', 
900
 
                                  "Robert Collins <robertc@example.org>")
901
 
        self.assertEqual("John", my_config.username())
 
551
        self.assertEqual("John", my_config._get_user_id())
 
552
 
 
553
    def test_not_set_in_branch(self):
 
554
        branch = FakeBranch()
 
555
        my_config = config.BranchConfig(branch)
902
556
        branch.control_files.email = None
903
 
        self.assertEqual("Robert Collins <robertc@example.org>",
904
 
                         my_config.username())
905
 
 
906
 
    def test_not_set_in_branch(self):
907
 
        my_config = self.get_branch_config(sample_config_text)
908
 
        my_config.branch.control_files.email = None
909
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
 
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>",
910
561
                         my_config._get_user_id())
911
 
        my_config.branch.control_files.email = "John"
 
562
        branch.control_files.email = "John"
912
563
        self.assertEqual("John", my_config._get_user_id())
913
564
 
914
 
    def test_BZR_EMAIL_OVERRIDES(self):
915
 
        os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
 
565
    def test_BZREMAIL_OVERRIDES(self):
 
566
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
916
567
        branch = FakeBranch()
917
568
        my_config = config.BranchConfig(branch)
918
569
        self.assertEqual("Robert Collins <robertc@example.org>",
919
570
                         my_config.username())
920
571
    
921
572
    def test_signatures_forced(self):
922
 
        my_config = self.get_branch_config(
923
 
            global_config=sample_always_signatures)
924
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
925
 
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
926
 
        self.assertTrue(my_config.signature_needed())
927
 
 
928
 
    def test_signatures_forced_branch(self):
929
 
        my_config = self.get_branch_config(
930
 
            global_config=sample_ignore_signatures,
931
 
            branch_data_config=sample_always_signatures)
932
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
933
 
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
934
 
        self.assertTrue(my_config.signature_needed())
 
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())
935
579
 
936
580
    def test_gpg_signing_command(self):
937
 
        my_config = self.get_branch_config(
938
 
            # branch data cannot set gpg_signing_command
939
 
            branch_data_config="gpg_signing_command=pgp")
940
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
941
 
        my_config._get_global_config()._get_parser(config_file)
 
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))
942
586
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
943
587
 
944
588
    def test_get_user_option_global(self):
945
589
        branch = FakeBranch()
946
590
        my_config = config.BranchConfig(branch)
947
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
948
 
        (my_config._get_global_config()._get_parser(config_file))
 
591
        config_file = StringIO(sample_config_text)
 
592
        (my_config._get_location_config().
 
593
            _get_global_config()._get_parser(config_file))
949
594
        self.assertEqual('something',
950
595
                         my_config.get_user_option('user_global_option'))
951
596
 
952
597
    def test_post_commit_default(self):
953
598
        branch = FakeBranch()
954
 
        my_config = self.get_branch_config(sample_config_text, '/a/c',
955
 
                                           sample_branches_text)
956
 
        self.assertEqual(my_config.branch.base, '/a/c')
957
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
958
 
                         my_config.post_commit())
959
 
        my_config.set_user_option('post_commit', 'rmtree_root')
960
 
        # post-commit is ignored when bresent in branch data
961
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
962
 
                         my_config.post_commit())
963
 
        my_config.set_user_option('post_commit', 'rmtree_root',
964
 
                                  store=config.STORE_LOCATION)
965
 
        self.assertEqual('rmtree_root', my_config.post_commit())
966
 
 
967
 
    def test_config_precedence(self):
968
 
        my_config = self.get_branch_config(global_config=precedence_global)
969
 
        self.assertEqual(my_config.get_user_option('option'), 'global')
970
 
        my_config = self.get_branch_config(global_config=precedence_global, 
971
 
                                      branch_data_config=precedence_branch)
972
 
        self.assertEqual(my_config.get_user_option('option'), 'branch')
973
 
        my_config = self.get_branch_config(global_config=precedence_global, 
974
 
                                      branch_data_config=precedence_branch,
975
 
                                      location_config=precedence_location)
976
 
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
977
 
        my_config = self.get_branch_config(global_config=precedence_global, 
978
 
                                      branch_data_config=precedence_branch,
979
 
                                      location_config=precedence_location,
980
 
                                      location='http://example.com/specific')
981
 
        self.assertEqual(my_config.get_user_option('option'), 'exact')
 
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())
982
608
 
983
609
 
984
610
class TestMailAddressExtraction(TestCase):
986
612
    def test_extract_email_address(self):
987
613
        self.assertEqual('jane@test.com',
988
614
                         config.extract_email_address('Jane <jane@test.com>'))
989
 
        self.assertRaises(errors.NoEmailInUsername,
 
615
        self.assertRaises(errors.BzrError,
990
616
                          config.extract_email_address, 'Jane Tester')