~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

Fix BzrDir.create_workingtree for NULL_REVISION

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 by Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
23
23
import sys
24
24
 
25
25
#import bzrlib specific imports here
26
 
import bzrlib.config as config
27
 
import bzrlib.errors as errors
28
 
from bzrlib.tests import TestCase, TestCaseInTempDir
29
 
 
30
 
 
 
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"
31
38
sample_config_text = ("[DEFAULT]\n"
32
 
                      "email=Robert Collins <robertc@example.com>\n"
 
39
                      u"email=Erik B\u00e5gfors <erik@bagfors.nu>\n"
33
40
                      "editor=vim\n"
34
41
                      "gpg_signing_command=gnome-gpg\n"
35
 
                      "user_global_option=something\n")
 
42
                      "log_format=short\n"
 
43
                      "user_global_option=something\n"
 
44
                      "[ALIASES]\n"
 
45
                      "h=help\n"
 
46
                      "ll=" + sample_long_alias + "\n")
36
47
 
37
48
 
38
49
sample_always_signatures = ("[DEFAULT]\n"
39
 
                            "check_signatures=require\n")
 
50
                            "check_signatures=ignore\n"
 
51
                            "create_signatures=always")
40
52
 
41
53
 
42
54
sample_ignore_signatures = ("[DEFAULT]\n"
43
 
                            "check_signatures=ignore\n")
 
55
                            "check_signatures=require\n"
 
56
                            "create_signatures=never")
44
57
 
45
58
 
46
59
sample_maybe_signatures = ("[DEFAULT]\n"
47
 
                            "check_signatures=check-available\n")
 
60
                            "check_signatures=ignore\n"
 
61
                            "create_signatures=when-required")
48
62
 
49
63
 
50
64
sample_branches_text = ("[http://www.example.com]\n"
70
84
                        "#testing explicit beats globs\n")
71
85
 
72
86
 
 
87
 
73
88
class InstrumentedConfigObj(object):
74
89
    """A config obj look-enough-alike to record calls made to it."""
75
90
 
81
96
        self._calls.append(('__getitem__', key))
82
97
        return self
83
98
 
84
 
    def __init__(self, input):
85
 
        self._calls = [('__init__', input)]
 
99
    def __init__(self, input, encoding=None):
 
100
        self._calls = [('__init__', input, encoding)]
86
101
 
87
102
    def __setitem__(self, key, value):
88
103
        self._calls.append(('__setitem__', key, value))
89
104
 
90
 
    def write(self):
 
105
    def write(self, arg):
91
106
        self._calls.append(('write',))
92
107
 
93
108
 
94
109
class FakeBranch(object):
95
110
 
96
 
    def __init__(self):
97
 
        self.base = "http://example.com/branches/demo"
98
 
        self.email = 'Robert Collins <robertc@example.net>\n'
99
 
 
100
 
    def controlfile(self, filename, mode):
 
111
    def __init__(self, base=None, user_id=None):
 
112
        if base is None:
 
113
            self.base = "http://example.com/branches/demo"
 
114
        else:
 
115
            self.base = base
 
116
        self.control_files = FakeControlFiles(user_id=user_id)
 
117
 
 
118
    def lock_write(self):
 
119
        pass
 
120
 
 
121
    def unlock(self):
 
122
        pass
 
123
 
 
124
 
 
125
class FakeControlFiles(object):
 
126
 
 
127
    def __init__(self, user_id=None):
 
128
        self.email = user_id
 
129
        self.files = {}
 
130
 
 
131
    def get_utf8(self, filename):
101
132
        if filename != 'email':
102
133
            raise NotImplementedError
103
134
        if self.email is not None:
104
135
            return StringIO(self.email)
105
136
        raise errors.NoSuchFile(filename)
106
137
 
 
138
    def get(self, filename):
 
139
        try:
 
140
            return StringIO(self.files[filename])
 
141
        except KeyError:
 
142
            raise errors.NoSuchFile(filename)
 
143
 
 
144
    def put(self, filename, fileobj):
 
145
        self.files[filename] = fileobj.read()
 
146
 
107
147
 
108
148
class InstrumentedConfig(config.Config):
109
149
    """An instrumented config that supplies stubs for template methods."""
122
162
        return self._signatures
123
163
 
124
164
 
 
165
bool_config = """[DEFAULT]
 
166
active = true
 
167
inactive = false
 
168
[UPPERCASE]
 
169
active = True
 
170
nonactive = False
 
171
"""
 
172
class TestConfigObj(TestCase):
 
173
    def test_get_bool(self):
 
174
        from bzrlib.config import ConfigObj
 
175
        co = ConfigObj(StringIO(bool_config))
 
176
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
 
177
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
 
178
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
 
179
        self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
 
180
 
 
181
 
125
182
class TestConfig(TestCase):
126
183
 
127
184
    def test_constructs(self):
143
200
 
144
201
    def test_signatures_default(self):
145
202
        my_config = config.Config()
 
203
        self.assertFalse(my_config.signature_needed())
146
204
        self.assertEqual(config.CHECK_IF_POSSIBLE,
147
205
                         my_config.signature_checking())
 
206
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
 
207
                         my_config.signing_policy())
148
208
 
149
209
    def test_signatures_template_method(self):
150
210
        my_config = InstrumentedConfig()
170
230
        my_config = config.Config()
171
231
        self.assertEqual(None, my_config.post_commit())
172
232
 
 
233
    def test_log_format_default(self):
 
234
        my_config = config.Config()
 
235
        self.assertEqual('long', my_config.log_format())
 
236
 
173
237
 
174
238
class TestConfigPath(TestCase):
175
239
 
215
279
            self.assertEqual(config.branches_config_filename(),
216
280
                             '/home/bogus/.bazaar/branches.conf')
217
281
 
 
282
    def test_locations_config_filename(self):
 
283
        if sys.platform == 'win32':
 
284
            self.assertEqual(config.locations_config_filename(), 
 
285
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/locations.conf')
 
286
        else:
 
287
            self.assertEqual(config.locations_config_filename(),
 
288
                             '/home/bogus/.bazaar/locations.conf')
 
289
 
218
290
class TestIniConfig(TestCase):
219
291
 
220
292
    def test_contructs(self):
221
293
        my_config = config.IniBasedConfig("nothing")
222
294
 
223
295
    def test_from_fp(self):
224
 
        config_file = StringIO(sample_config_text)
 
296
        config_file = StringIO(sample_config_text.encode('utf-8'))
225
297
        my_config = config.IniBasedConfig(None)
226
298
        self.failUnless(
227
299
            isinstance(my_config._get_parser(file=config_file),
228
300
                        ConfigObj))
229
301
 
230
302
    def test_cached(self):
231
 
        config_file = StringIO(sample_config_text)
 
303
        config_file = StringIO(sample_config_text.encode('utf-8'))
232
304
        my_config = config.IniBasedConfig(None)
233
305
        parser = my_config._get_parser(file=config_file)
234
306
        self.failUnless(my_config._get_parser() is parser)
249
321
        finally:
250
322
            config.ConfigObj = oldparserclass
251
323
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
252
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename())])
253
 
 
254
 
 
255
 
class TestBranchConfig(TestCaseInTempDir):
 
324
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
 
325
                                          'utf-8')])
 
326
 
 
327
 
 
328
class TestBranchConfig(TestCaseWithTransport):
256
329
 
257
330
    def test_constructs(self):
258
331
        branch = FakeBranch()
266
339
        self.assertEqual(branch.base, location_config.location)
267
340
        self.failUnless(location_config is my_config._get_location_config())
268
341
 
 
342
    def test_get_config(self):
 
343
        """The Branch.get_config method works properly"""
 
344
        b = BzrDir.create_standalone_workingtree('.').branch
 
345
        my_config = b.get_config()
 
346
        self.assertIs(my_config.get_user_option('wacky'), None)
 
347
        my_config.set_user_option('wacky', 'unlikely')
 
348
        self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
 
349
 
 
350
        # Ensure we get the same thing if we start again
 
351
        b2 = Branch.open('.')
 
352
        my_config2 = b2.get_config()
 
353
        self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
 
354
 
 
355
    def test_has_explicit_nickname(self):
 
356
        b = self.make_branch('.')
 
357
        self.assertFalse(b.get_config().has_explicit_nickname())
 
358
        b.nick = 'foo'
 
359
        self.assertTrue(b.get_config().has_explicit_nickname())
 
360
 
 
361
    def test_config_url(self):
 
362
        """The Branch.get_config will use section that uses a local url"""
 
363
        branch = self.make_branch('branch')
 
364
        self.assertEqual('branch', branch.nick)
 
365
 
 
366
        locations = config.locations_config_filename()
 
367
        config.ensure_config_dir_exists()
 
368
        local_url = urlutils.local_path_to_url('branch')
 
369
        open(locations, 'wb').write('[%s]\nnickname = foobar' 
 
370
                                    % (local_url,))
 
371
        self.assertEqual('foobar', branch.nick)
 
372
 
 
373
    def test_config_local_path(self):
 
374
        """The Branch.get_config will use a local system path"""
 
375
        branch = self.make_branch('branch')
 
376
        self.assertEqual('branch', branch.nick)
 
377
 
 
378
        locations = config.locations_config_filename()
 
379
        config.ensure_config_dir_exists()
 
380
        open(locations, 'wb').write('[%s/branch]\nnickname = barry' 
 
381
                                    % (osutils.getcwd().encode('utf8'),))
 
382
        self.assertEqual('barry', branch.nick)
 
383
 
 
384
    def test_config_creates_local(self):
 
385
        """Creating a new entry in config uses a local path."""
 
386
        branch = self.make_branch('branch')
 
387
        branch.set_push_location('http://foobar')
 
388
        locations = config.locations_config_filename()
 
389
        local_path = osutils.getcwd().encode('utf8')
 
390
        # Surprisingly ConfigObj doesn't create a trailing newline
 
391
        self.check_file_contents(locations,
 
392
            '[%s/branch]\npush_location = http://foobar' % (local_path,))
 
393
 
269
394
 
270
395
class TestGlobalConfigItems(TestCase):
271
396
 
272
397
    def test_user_id(self):
273
 
        config_file = StringIO(sample_config_text)
 
398
        config_file = StringIO(sample_config_text.encode('utf-8'))
274
399
        my_config = config.GlobalConfig()
275
400
        my_config._parser = my_config._get_parser(file=config_file)
276
 
        self.assertEqual("Robert Collins <robertc@example.com>",
 
401
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
277
402
                         my_config._get_user_id())
278
403
 
279
404
    def test_absent_user_id(self):
283
408
        self.assertEqual(None, my_config._get_user_id())
284
409
 
285
410
    def test_configured_editor(self):
286
 
        config_file = StringIO(sample_config_text)
 
411
        config_file = StringIO(sample_config_text.encode('utf-8'))
287
412
        my_config = config.GlobalConfig()
288
413
        my_config._parser = my_config._get_parser(file=config_file)
289
414
        self.assertEqual("vim", my_config.get_editor())
292
417
        config_file = StringIO(sample_always_signatures)
293
418
        my_config = config.GlobalConfig()
294
419
        my_config._parser = my_config._get_parser(file=config_file)
295
 
        self.assertEqual(config.CHECK_ALWAYS,
 
420
        self.assertEqual(config.CHECK_NEVER,
296
421
                         my_config.signature_checking())
 
422
        self.assertEqual(config.SIGN_ALWAYS,
 
423
                         my_config.signing_policy())
297
424
        self.assertEqual(True, my_config.signature_needed())
298
425
 
299
426
    def test_signatures_if_possible(self):
300
427
        config_file = StringIO(sample_maybe_signatures)
301
428
        my_config = config.GlobalConfig()
302
429
        my_config._parser = my_config._get_parser(file=config_file)
303
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
 
430
        self.assertEqual(config.CHECK_NEVER,
304
431
                         my_config.signature_checking())
 
432
        self.assertEqual(config.SIGN_WHEN_REQUIRED,
 
433
                         my_config.signing_policy())
305
434
        self.assertEqual(False, my_config.signature_needed())
306
435
 
307
436
    def test_signatures_ignore(self):
308
437
        config_file = StringIO(sample_ignore_signatures)
309
438
        my_config = config.GlobalConfig()
310
439
        my_config._parser = my_config._get_parser(file=config_file)
311
 
        self.assertEqual(config.CHECK_NEVER,
 
440
        self.assertEqual(config.CHECK_ALWAYS,
312
441
                         my_config.signature_checking())
 
442
        self.assertEqual(config.SIGN_NEVER,
 
443
                         my_config.signing_policy())
313
444
        self.assertEqual(False, my_config.signature_needed())
314
445
 
315
446
    def _get_sample_config(self):
316
 
        config_file = StringIO(sample_config_text)
 
447
        config_file = StringIO(sample_config_text.encode('utf-8'))
317
448
        my_config = config.GlobalConfig()
318
449
        my_config._parser = my_config._get_parser(file=config_file)
319
450
        return my_config
346
477
        my_config = self._get_sample_config()
347
478
        self.assertEqual(None, my_config.post_commit())
348
479
 
349
 
 
350
 
class TestLocationConfig(TestCase):
 
480
    def test_configured_logformat(self):
 
481
        my_config = self._get_sample_config()
 
482
        self.assertEqual("short", my_config.log_format())
 
483
 
 
484
    def test_get_alias(self):
 
485
        my_config = self._get_sample_config()
 
486
        self.assertEqual('help', my_config.get_alias('h'))
 
487
 
 
488
    def test_get_no_alias(self):
 
489
        my_config = self._get_sample_config()
 
490
        self.assertEqual(None, my_config.get_alias('foo'))
 
491
 
 
492
    def test_get_long_alias(self):
 
493
        my_config = self._get_sample_config()
 
494
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
 
495
 
 
496
 
 
497
class TestLocationConfig(TestCaseInTempDir):
351
498
 
352
499
    def test_constructs(self):
353
500
        my_config = config.LocationConfig('http://example.com')
360
507
        # replace the class that is constructured, to check its parameters
361
508
        oldparserclass = config.ConfigObj
362
509
        config.ConfigObj = InstrumentedConfigObj
363
 
        my_config = config.LocationConfig('http://www.example.com')
364
510
        try:
 
511
            my_config = config.LocationConfig('http://www.example.com')
365
512
            parser = my_config._get_parser()
366
513
        finally:
367
514
            config.ConfigObj = oldparserclass
368
515
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
369
516
        self.assertEqual(parser._calls,
370
 
                         [('__init__', config.branches_config_filename())])
 
517
                         [('__init__', config.locations_config_filename(),
 
518
                           'utf-8')])
 
519
        config.ensure_config_dir_exists()
 
520
        #os.mkdir(config.config_dir())
 
521
        f = file(config.branches_config_filename(), 'wb')
 
522
        f.write('')
 
523
        f.close()
 
524
        oldparserclass = config.ConfigObj
 
525
        config.ConfigObj = InstrumentedConfigObj
 
526
        try:
 
527
            my_config = config.LocationConfig('http://www.example.com')
 
528
            parser = my_config._get_parser()
 
529
        finally:
 
530
            config.ConfigObj = oldparserclass
371
531
 
372
532
    def test_get_global_config(self):
373
 
        my_config = config.LocationConfig('http://example.com')
 
533
        my_config = config.BranchConfig(FakeBranch('http://example.com'))
374
534
        global_config = my_config._get_global_config()
375
535
        self.failUnless(isinstance(global_config, config.GlobalConfig))
376
536
        self.failUnless(global_config is my_config._get_global_config())
377
537
 
378
538
    def test__get_section_no_match(self):
379
 
        self.get_location_config('/')
380
 
        self.assertEqual(None, self.my_config._get_section())
 
539
        self.get_branch_config('/')
 
540
        self.assertEqual(None, self.my_location_config._get_section())
381
541
        
382
542
    def test__get_section_exact(self):
383
 
        self.get_location_config('http://www.example.com')
 
543
        self.get_branch_config('http://www.example.com')
384
544
        self.assertEqual('http://www.example.com',
385
 
                         self.my_config._get_section())
 
545
                         self.my_location_config._get_section())
386
546
   
387
547
    def test__get_section_suffix_does_not(self):
388
 
        self.get_location_config('http://www.example.com-com')
389
 
        self.assertEqual(None, self.my_config._get_section())
 
548
        self.get_branch_config('http://www.example.com-com')
 
549
        self.assertEqual(None, self.my_location_config._get_section())
390
550
 
391
551
    def test__get_section_subdir_recursive(self):
392
 
        self.get_location_config('http://www.example.com/com')
 
552
        self.get_branch_config('http://www.example.com/com')
393
553
        self.assertEqual('http://www.example.com',
394
 
                         self.my_config._get_section())
 
554
                         self.my_location_config._get_section())
395
555
 
396
556
    def test__get_section_subdir_matches(self):
397
 
        self.get_location_config('http://www.example.com/useglobal')
 
557
        self.get_branch_config('http://www.example.com/useglobal')
398
558
        self.assertEqual('http://www.example.com/useglobal',
399
 
                         self.my_config._get_section())
 
559
                         self.my_location_config._get_section())
400
560
 
401
561
    def test__get_section_subdir_nonrecursive(self):
402
 
        self.get_location_config(
 
562
        self.get_branch_config(
403
563
            'http://www.example.com/useglobal/childbranch')
404
564
        self.assertEqual('http://www.example.com',
405
 
                         self.my_config._get_section())
 
565
                         self.my_location_config._get_section())
406
566
 
407
567
    def test__get_section_subdir_trailing_slash(self):
408
 
        self.get_location_config('/b')
409
 
        self.assertEqual('/b/', self.my_config._get_section())
 
568
        self.get_branch_config('/b')
 
569
        self.assertEqual('/b/', self.my_location_config._get_section())
410
570
 
411
571
    def test__get_section_subdir_child(self):
412
 
        self.get_location_config('/a/foo')
413
 
        self.assertEqual('/a/*', self.my_config._get_section())
 
572
        self.get_branch_config('/a/foo')
 
573
        self.assertEqual('/a/*', self.my_location_config._get_section())
414
574
 
415
575
    def test__get_section_subdir_child_child(self):
416
 
        self.get_location_config('/a/foo/bar')
417
 
        self.assertEqual('/a/', self.my_config._get_section())
 
576
        self.get_branch_config('/a/foo/bar')
 
577
        self.assertEqual('/a/', self.my_location_config._get_section())
418
578
 
419
579
    def test__get_section_trailing_slash_with_children(self):
420
 
        self.get_location_config('/a/')
421
 
        self.assertEqual('/a/', self.my_config._get_section())
 
580
        self.get_branch_config('/a/')
 
581
        self.assertEqual('/a/', self.my_location_config._get_section())
422
582
 
423
583
    def test__get_section_explicit_over_glob(self):
424
 
        self.get_location_config('/a/c')
425
 
        self.assertEqual('/a/c', self.my_config._get_section())
 
584
        self.get_branch_config('/a/c')
 
585
        self.assertEqual('/a/c', self.my_location_config._get_section())
426
586
 
427
 
    def get_location_config(self, location, global_config=None):
428
 
        if global_config is None:
429
 
            global_file = StringIO(sample_config_text)
430
 
        else:
431
 
            global_file = StringIO(global_config)
432
 
        branches_file = StringIO(sample_branches_text)
433
 
        self.my_config = config.LocationConfig(location)
434
 
        self.my_config._get_parser(branches_file)
435
 
        self.my_config._get_global_config()._get_parser(global_file)
436
587
 
437
588
    def test_location_without_username(self):
438
 
        self.get_location_config('http://www.example.com/useglobal')
439
 
        self.assertEqual('Robert Collins <robertc@example.com>',
 
589
        self.get_branch_config('http://www.example.com/useglobal')
 
590
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
440
591
                         self.my_config.username())
441
592
 
442
593
    def test_location_not_listed(self):
443
 
        self.get_location_config('/home/robertc/sources')
444
 
        self.assertEqual('Robert Collins <robertc@example.com>',
 
594
        """Test that the global username is used when no location matches"""
 
595
        self.get_branch_config('/home/robertc/sources')
 
596
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
445
597
                         self.my_config.username())
446
598
 
447
599
    def test_overriding_location(self):
448
 
        self.get_location_config('http://www.example.com/foo')
 
600
        self.get_branch_config('http://www.example.com/foo')
449
601
        self.assertEqual('Robert Collins <robertc@example.org>',
450
602
                         self.my_config.username())
451
603
 
452
604
    def test_signatures_not_set(self):
453
 
        self.get_location_config('http://www.example.com',
 
605
        self.get_branch_config('http://www.example.com',
454
606
                                 global_config=sample_ignore_signatures)
455
 
        self.assertEqual(config.CHECK_NEVER,
 
607
        self.assertEqual(config.CHECK_ALWAYS,
456
608
                         self.my_config.signature_checking())
 
609
        self.assertEqual(config.SIGN_NEVER,
 
610
                         self.my_config.signing_policy())
457
611
 
458
612
    def test_signatures_never(self):
459
 
        self.get_location_config('/a/c')
 
613
        self.get_branch_config('/a/c')
460
614
        self.assertEqual(config.CHECK_NEVER,
461
615
                         self.my_config.signature_checking())
462
616
        
463
617
    def test_signatures_when_available(self):
464
 
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
 
618
        self.get_branch_config('/a/', global_config=sample_ignore_signatures)
465
619
        self.assertEqual(config.CHECK_IF_POSSIBLE,
466
620
                         self.my_config.signature_checking())
467
621
        
468
622
    def test_signatures_always(self):
469
 
        self.get_location_config('/b')
 
623
        self.get_branch_config('/b')
470
624
        self.assertEqual(config.CHECK_ALWAYS,
471
625
                         self.my_config.signature_checking())
472
626
        
473
627
    def test_gpg_signing_command(self):
474
 
        self.get_location_config('/b')
 
628
        self.get_branch_config('/b')
475
629
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
476
630
 
477
631
    def test_gpg_signing_command_missing(self):
478
 
        self.get_location_config('/a')
 
632
        self.get_branch_config('/a')
479
633
        self.assertEqual("false", self.my_config.gpg_signing_command())
480
634
 
481
635
    def test_get_user_option_global(self):
482
 
        self.get_location_config('/a')
 
636
        self.get_branch_config('/a')
483
637
        self.assertEqual('something',
484
638
                         self.my_config.get_user_option('user_global_option'))
485
639
 
486
640
    def test_get_user_option_local(self):
487
 
        self.get_location_config('/a')
 
641
        self.get_branch_config('/a')
488
642
        self.assertEqual('local',
489
643
                         self.my_config.get_user_option('user_local_option'))
490
644
        
491
645
    def test_post_commit_default(self):
492
 
        self.get_location_config('/a/c')
 
646
        self.get_branch_config('/a/c')
493
647
        self.assertEqual('bzrlib.tests.test_config.post_commit',
494
648
                         self.my_config.post_commit())
495
649
 
496
 
 
497
 
class TestLocationConfig(TestCaseInTempDir):
498
 
 
499
 
    def get_location_config(self, location, global_config=None):
 
650
    def get_branch_config(self, location, global_config=None):
500
651
        if global_config is None:
501
 
            global_file = StringIO(sample_config_text)
 
652
            global_file = StringIO(sample_config_text.encode('utf-8'))
502
653
        else:
503
 
            global_file = StringIO(global_config)
504
 
        branches_file = StringIO(sample_branches_text)
505
 
        self.my_config = config.LocationConfig(location)
506
 
        self.my_config._get_parser(branches_file)
 
654
            global_file = StringIO(global_config.encode('utf-8'))
 
655
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
 
656
        self.my_config = config.BranchConfig(FakeBranch(location))
 
657
        # Force location config to use specified file
 
658
        self.my_location_config = self.my_config._get_location_config()
 
659
        self.my_location_config._get_parser(branches_file)
 
660
        # Force global config to use specified file
507
661
        self.my_config._get_global_config()._get_parser(global_file)
508
662
 
509
663
    def test_set_user_setting_sets_and_saves(self):
510
 
        self.get_location_config('/a/c')
 
664
        self.get_branch_config('/a/c')
511
665
        record = InstrumentedConfigObj("foo")
512
 
        self.my_config._parser = record
 
666
        self.my_location_config._parser = record
513
667
 
514
668
        real_mkdir = os.mkdir
515
669
        self.created = False
520
674
 
521
675
        os.mkdir = checked_mkdir
522
676
        try:
523
 
            self.my_config.set_user_option('foo', 'bar')
 
677
            self.my_config.set_user_option('foo', 'bar', local=True)
524
678
        finally:
525
679
            os.mkdir = real_mkdir
526
680
 
533
687
                          ('write',)],
534
688
                         record._calls[1:])
535
689
 
536
 
 
537
 
class TestBranchConfigItems(TestCase):
 
690
    def test_set_user_setting_sets_and_saves2(self):
 
691
        self.get_branch_config('/a/c')
 
692
        self.assertIs(self.my_config.get_user_option('foo'), None)
 
693
        self.my_config.set_user_option('foo', 'bar')
 
694
        self.assertEqual(
 
695
            self.my_config.branch.control_files.files['branch.conf'], 
 
696
            'foo = bar')
 
697
        self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
 
698
        self.my_config.set_user_option('foo', 'baz', local=True)
 
699
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
 
700
        self.my_config.set_user_option('foo', 'qux')
 
701
        self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
 
702
        
 
703
 
 
704
precedence_global = 'option = global'
 
705
precedence_branch = 'option = branch'
 
706
precedence_location = """
 
707
[http://]
 
708
recurse = true
 
709
option = recurse
 
710
[http://example.com/specific]
 
711
option = exact
 
712
"""
 
713
 
 
714
 
 
715
class TestBranchConfigItems(TestCaseInTempDir):
 
716
 
 
717
    def get_branch_config(self, global_config=None, location=None, 
 
718
                          location_config=None, branch_data_config=None):
 
719
        my_config = config.BranchConfig(FakeBranch(location))
 
720
        if global_config is not None:
 
721
            global_file = StringIO(global_config.encode('utf-8'))
 
722
            my_config._get_global_config()._get_parser(global_file)
 
723
        self.my_location_config = my_config._get_location_config()
 
724
        if location_config is not None:
 
725
            location_file = StringIO(location_config.encode('utf-8'))
 
726
            self.my_location_config._get_parser(location_file)
 
727
        if branch_data_config is not None:
 
728
            my_config.branch.control_files.files['branch.conf'] = \
 
729
                branch_data_config
 
730
        return my_config
538
731
 
539
732
    def test_user_id(self):
540
 
        branch = FakeBranch()
 
733
        branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
541
734
        my_config = config.BranchConfig(branch)
542
735
        self.assertEqual("Robert Collins <robertc@example.net>",
543
 
                         my_config._get_user_id())
544
 
        branch.email = "John"
545
 
        self.assertEqual("John", my_config._get_user_id())
 
736
                         my_config.username())
 
737
        branch.control_files.email = "John"
 
738
        my_config.set_user_option('email', 
 
739
                                  "Robert Collins <robertc@example.org>")
 
740
        self.assertEqual("John", my_config.username())
 
741
        branch.control_files.email = None
 
742
        self.assertEqual("Robert Collins <robertc@example.org>",
 
743
                         my_config.username())
546
744
 
547
745
    def test_not_set_in_branch(self):
548
 
        branch = FakeBranch()
549
 
        my_config = config.BranchConfig(branch)
550
 
        branch.email = None
551
 
        config_file = StringIO(sample_config_text)
552
 
        (my_config._get_location_config().
553
 
            _get_global_config()._get_parser(config_file))
554
 
        self.assertEqual("Robert Collins <robertc@example.com>",
 
746
        my_config = self.get_branch_config(sample_config_text)
 
747
        my_config.branch.control_files.email = None
 
748
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
555
749
                         my_config._get_user_id())
556
 
        branch.email = "John"
 
750
        my_config.branch.control_files.email = "John"
557
751
        self.assertEqual("John", my_config._get_user_id())
558
752
 
559
 
    def test_BZREMAIL_OVERRIDES(self):
560
 
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
 
753
    def test_BZR_EMAIL_OVERRIDES(self):
 
754
        os.environ['BZR_EMAIL'] = "Robert Collins <robertc@example.org>"
561
755
        branch = FakeBranch()
562
756
        my_config = config.BranchConfig(branch)
563
757
        self.assertEqual("Robert Collins <robertc@example.org>",
564
758
                         my_config.username())
565
759
    
566
760
    def test_signatures_forced(self):
567
 
        branch = FakeBranch()
568
 
        my_config = config.BranchConfig(branch)
569
 
        config_file = StringIO(sample_always_signatures)
570
 
        (my_config._get_location_config().
571
 
            _get_global_config()._get_parser(config_file))
572
 
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
 
761
        my_config = self.get_branch_config(
 
762
            global_config=sample_always_signatures)
 
763
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
 
764
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
 
765
        self.assertTrue(my_config.signature_needed())
 
766
 
 
767
    def test_signatures_forced_branch(self):
 
768
        my_config = self.get_branch_config(
 
769
            global_config=sample_ignore_signatures,
 
770
            branch_data_config=sample_always_signatures)
 
771
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
 
772
        self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
 
773
        self.assertTrue(my_config.signature_needed())
573
774
 
574
775
    def test_gpg_signing_command(self):
575
 
        branch = FakeBranch()
576
 
        my_config = config.BranchConfig(branch)
577
 
        config_file = StringIO(sample_config_text)
578
 
        (my_config._get_location_config().
579
 
            _get_global_config()._get_parser(config_file))
 
776
        my_config = self.get_branch_config(
 
777
            # branch data cannot set gpg_signing_command
 
778
            branch_data_config="gpg_signing_command=pgp")
 
779
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
780
        my_config._get_global_config()._get_parser(config_file)
580
781
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
581
782
 
582
783
    def test_get_user_option_global(self):
583
784
        branch = FakeBranch()
584
785
        my_config = config.BranchConfig(branch)
585
 
        config_file = StringIO(sample_config_text)
586
 
        (my_config._get_location_config().
587
 
            _get_global_config()._get_parser(config_file))
 
786
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
787
        (my_config._get_global_config()._get_parser(config_file))
588
788
        self.assertEqual('something',
589
789
                         my_config.get_user_option('user_global_option'))
590
790
 
591
791
    def test_post_commit_default(self):
592
792
        branch = FakeBranch()
593
 
        branch.base='/a/c'
594
 
        my_config = config.BranchConfig(branch)
595
 
        config_file = StringIO(sample_config_text)
596
 
        (my_config._get_location_config().
597
 
            _get_global_config()._get_parser(config_file))
598
 
        branch_file = StringIO(sample_branches_text)
599
 
        my_config._get_location_config()._get_parser(branch_file)
600
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
601
 
                         my_config.post_commit())
 
793
        my_config = self.get_branch_config(sample_config_text, '/a/c',
 
794
                                           sample_branches_text)
 
795
        self.assertEqual(my_config.branch.base, '/a/c')
 
796
        self.assertEqual('bzrlib.tests.test_config.post_commit',
 
797
                         my_config.post_commit())
 
798
        my_config.set_user_option('post_commit', 'rmtree_root')
 
799
        # post-commit is ignored when bresent in branch data
 
800
        self.assertEqual('bzrlib.tests.test_config.post_commit',
 
801
                         my_config.post_commit())
 
802
        my_config.set_user_option('post_commit', 'rmtree_root', local=True)
 
803
        self.assertEqual('rmtree_root', my_config.post_commit())
 
804
 
 
805
    def test_config_precedence(self):
 
806
        my_config = self.get_branch_config(global_config=precedence_global)
 
807
        self.assertEqual(my_config.get_user_option('option'), 'global')
 
808
        my_config = self.get_branch_config(global_config=precedence_global, 
 
809
                                      branch_data_config=precedence_branch)
 
810
        self.assertEqual(my_config.get_user_option('option'), 'branch')
 
811
        my_config = self.get_branch_config(global_config=precedence_global, 
 
812
                                      branch_data_config=precedence_branch,
 
813
                                      location_config=precedence_location)
 
814
        self.assertEqual(my_config.get_user_option('option'), 'recurse')
 
815
        my_config = self.get_branch_config(global_config=precedence_global, 
 
816
                                      branch_data_config=precedence_branch,
 
817
                                      location_config=precedence_location,
 
818
                                      location='http://example.com/specific')
 
819
        self.assertEqual(my_config.get_user_option('option'), 'exact')
602
820
 
603
821
 
604
822
class TestMailAddressExtraction(TestCase):