/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_config.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-21 17:52:22 UTC
  • mto: This revision was merged to the branch mainline in revision 1882.
  • Revision ID: john@arbash-meinel.com-20060721175222-bf7499a71473f2f0
Entries in locations.conf should prefer local paths if available (bug #53653)

Show diffs side-by-side

added added

removed removed

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