~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testconfig.py

- refactor handling of short option names

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Tests for finding and reading the bzr config file[s]."""
19
19
# import system imports here
20
 
from bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
 
20
from ConfigParser import ConfigParser
21
21
from cStringIO import StringIO
22
22
import os
23
23
import sys
24
24
 
25
25
#import bzrlib specific imports here
26
26
import bzrlib.config as config
27
 
import bzrlib.errors as errors
28
 
from bzrlib.tests import TestCase, TestCaseInTempDir
29
 
 
30
 
 
31
 
sample_long_alias="log -r-15..-1 --line"
 
27
from bzrlib.selftest import TestCase, TestCaseInTempDir
 
28
 
 
29
 
32
30
sample_config_text = ("[DEFAULT]\n"
33
 
                      u"email=Erik B\u00e5gfors <erik@bagfors.nu>\n"
 
31
                      "email=Robert Collins <robertc@example.com>\n"
34
32
                      "editor=vim\n"
35
 
                      "gpg_signing_command=gnome-gpg\n"
36
 
                      "log_format=short\n"
37
 
                      "user_global_option=something\n"
38
 
                      "[ALIASES]\n"
39
 
                      "h=help\n"
40
 
                      "ll=" + sample_long_alias + "\n")
41
 
 
42
 
 
43
 
sample_always_signatures = ("[DEFAULT]\n"
44
 
                            "check_signatures=require\n")
45
 
 
46
 
 
47
 
sample_ignore_signatures = ("[DEFAULT]\n"
48
 
                            "check_signatures=ignore\n")
49
 
 
50
 
 
51
 
sample_maybe_signatures = ("[DEFAULT]\n"
52
 
                            "check_signatures=check-available\n")
53
 
 
54
 
 
55
 
sample_branches_text = ("[http://www.example.com]\n"
56
 
                        "# Top level policy\n"
57
 
                        "email=Robert Collins <robertc@example.org>\n"
58
 
                        "[http://www.example.com/useglobal]\n"
59
 
                        "# different project, forces global lookup\n"
60
 
                        "recurse=false\n"
61
 
                        "[/b/]\n"
62
 
                        "check_signatures=require\n"
63
 
                        "# test trailing / matching with no children\n"
64
 
                        "[/a/]\n"
65
 
                        "check_signatures=check-available\n"
66
 
                        "gpg_signing_command=false\n"
67
 
                        "user_local_option=local\n"
68
 
                        "# test trailing / matching\n"
69
 
                        "[/a/*]\n"
70
 
                        "#subdirs will match but not the parent\n"
71
 
                        "recurse=False\n"
72
 
                        "[/a/c]\n"
73
 
                        "check_signatures=ignore\n"
74
 
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
75
 
                        "#testing explicit beats globs\n")
76
 
 
77
 
 
78
 
 
79
 
class InstrumentedConfigObj(object):
80
 
    """A config obj look-enough-alike to record calls made to it."""
81
 
 
82
 
    def __contains__(self, thing):
83
 
        self._calls.append(('__contains__', thing))
84
 
        return False
85
 
 
86
 
    def __getitem__(self, key):
87
 
        self._calls.append(('__getitem__', key))
88
 
        return self
89
 
 
90
 
    def __init__(self, input, encoding=None):
91
 
        self._calls = [('__init__', input, encoding)]
92
 
 
93
 
    def __setitem__(self, key, value):
94
 
        self._calls.append(('__setitem__', key, value))
95
 
 
96
 
    def write(self, arg):
97
 
        self._calls.append(('write',))
98
 
 
99
 
 
100
 
class FakeBranch(object):
101
 
 
102
 
    def __init__(self):
103
 
        self.base = "http://example.com/branches/demo"
104
 
        self.control_files = FakeControlFiles()
105
 
 
106
 
 
107
 
class FakeControlFiles(object):
108
 
 
109
 
    def __init__(self):
110
 
        self.email = 'Robert Collins <robertc@example.net>\n'
111
 
 
112
 
    def get_utf8(self, filename):
113
 
        if filename != 'email':
114
 
            raise NotImplementedError
115
 
        if self.email is not None:
116
 
            return StringIO(self.email)
117
 
        raise errors.NoSuchFile(filename)
118
 
 
119
 
 
120
 
class InstrumentedConfig(config.Config):
121
 
    """An instrumented config that supplies stubs for template methods."""
122
 
    
123
 
    def __init__(self):
124
 
        super(InstrumentedConfig, self).__init__()
 
33
                      "gpg_signing_command=gnome-gpg\n")
 
34
 
 
35
class InstrumentedConfigParser(object):
 
36
    """A config parser look-enough-alike to record calls made to it."""
 
37
 
 
38
    def __init__(self):
125
39
        self._calls = []
126
 
        self._signatures = config.CHECK_NEVER
127
 
 
128
 
    def _get_user_id(self):
129
 
        self._calls.append('_get_user_id')
130
 
        return "Robert Collins <robert.collins@example.org>"
131
 
 
132
 
    def _get_signature_checking(self):
133
 
        self._calls.append('_get_signature_checking')
134
 
        return self._signatures
135
 
 
136
 
 
137
 
bool_config = """[DEFAULT]
138
 
active = true
139
 
inactive = false
140
 
[UPPERCASE]
141
 
active = True
142
 
nonactive = False
143
 
"""
144
 
class TestConfigObj(TestCase):
145
 
    def test_get_bool(self):
146
 
        from bzrlib.config import ConfigObj
147
 
        co = ConfigObj(StringIO(bool_config))
148
 
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
149
 
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
150
 
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
151
 
        self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
152
 
 
153
 
 
154
 
class TestConfig(TestCase):
155
 
 
156
 
    def test_constructs(self):
157
 
        config.Config()
158
 
 
159
 
    def test_no_default_editor(self):
160
 
        self.assertRaises(NotImplementedError, config.Config().get_editor)
161
 
 
162
 
    def test_user_email(self):
163
 
        my_config = InstrumentedConfig()
164
 
        self.assertEqual('robert.collins@example.org', my_config.user_email())
165
 
        self.assertEqual(['_get_user_id'], my_config._calls)
166
 
 
167
 
    def test_username(self):
168
 
        my_config = InstrumentedConfig()
169
 
        self.assertEqual('Robert Collins <robert.collins@example.org>',
170
 
                         my_config.username())
171
 
        self.assertEqual(['_get_user_id'], my_config._calls)
172
 
 
173
 
    def test_signatures_default(self):
174
 
        my_config = config.Config()
175
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
176
 
                         my_config.signature_checking())
177
 
 
178
 
    def test_signatures_template_method(self):
179
 
        my_config = InstrumentedConfig()
180
 
        self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
181
 
        self.assertEqual(['_get_signature_checking'], my_config._calls)
182
 
 
183
 
    def test_signatures_template_method_none(self):
184
 
        my_config = InstrumentedConfig()
185
 
        my_config._signatures = None
186
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
187
 
                         my_config.signature_checking())
188
 
        self.assertEqual(['_get_signature_checking'], my_config._calls)
189
 
 
190
 
    def test_gpg_signing_command_default(self):
191
 
        my_config = config.Config()
192
 
        self.assertEqual('gpg', my_config.gpg_signing_command())
193
 
 
194
 
    def test_get_user_option_default(self):
195
 
        my_config = config.Config()
196
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
197
 
 
198
 
    def test_post_commit_default(self):
199
 
        my_config = config.Config()
200
 
        self.assertEqual(None, my_config.post_commit())
201
 
 
202
 
    def test_log_format_default(self):
203
 
        my_config = config.Config()
204
 
        self.assertEqual('long', my_config.log_format())
 
40
 
 
41
    def read(self, filenames):
 
42
        self._calls.append(('read', filenames))
205
43
 
206
44
 
207
45
class TestConfigPath(TestCase):
208
46
 
209
47
    def setUp(self):
210
48
        super(TestConfigPath, self).setUp()
211
 
        self.old_home = os.environ.get('HOME', None)
212
 
        self.old_appdata = os.environ.get('APPDATA', None)
 
49
        self.oldenv = os.environ.get('HOME', None)
213
50
        os.environ['HOME'] = '/home/bogus'
214
 
        os.environ['APPDATA'] = \
215
 
            r'C:\Documents and Settings\bogus\Application Data'
216
51
 
217
52
    def tearDown(self):
218
 
        if self.old_home is None:
219
 
            del os.environ['HOME']
220
 
        else:
221
 
            os.environ['HOME'] = self.old_home
222
 
        if self.old_appdata is None:
223
 
            del os.environ['APPDATA']
224
 
        else:
225
 
            os.environ['APPDATA'] = self.old_appdata
226
 
        super(TestConfigPath, self).tearDown()
 
53
        os.environ['HOME'] = self.oldenv
227
54
    
228
55
    def test_config_dir(self):
229
 
        if sys.platform == 'win32':
230
 
            self.assertEqual(config.config_dir(), 
231
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
232
 
        else:
233
 
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
 
56
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
234
57
 
235
58
    def test_config_filename(self):
236
 
        if sys.platform == 'win32':
237
 
            self.assertEqual(config.config_filename(), 
238
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
239
 
        else:
240
 
            self.assertEqual(config.config_filename(),
241
 
                             '/home/bogus/.bazaar/bazaar.conf')
242
 
 
243
 
    def test_branches_config_filename(self):
244
 
        if sys.platform == 'win32':
245
 
            self.assertEqual(config.branches_config_filename(), 
246
 
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
247
 
        else:
248
 
            self.assertEqual(config.branches_config_filename(),
249
 
                             '/home/bogus/.bazaar/branches.conf')
250
 
 
251
 
class TestIniConfig(TestCase):
252
 
 
253
 
    def test_contructs(self):
254
 
        my_config = config.IniBasedConfig("nothing")
 
59
        self.assertEqual(config.config_filename(),
 
60
                         '/home/bogus/.bazaar/bazaar.conf')
 
61
 
 
62
 
 
63
class TestGetConfig(TestCase):
255
64
 
256
65
    def test_from_fp(self):
257
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
258
 
        my_config = config.IniBasedConfig(None)
259
 
        self.failUnless(
260
 
            isinstance(my_config._get_parser(file=config_file),
261
 
                        ConfigObj))
262
 
 
263
 
    def test_cached(self):
264
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
265
 
        my_config = config.IniBasedConfig(None)
266
 
        parser = my_config._get_parser(file=config_file)
267
 
        self.failUnless(my_config._get_parser() is parser)
268
 
 
269
 
 
270
 
class TestGetConfig(TestCase):
271
 
 
272
 
    def test_constructs(self):
273
 
        my_config = config.GlobalConfig()
 
66
        config_file = StringIO(sample_config_text)
 
67
        self.failUnless(isinstance(config._get_config_parser(file=config_file),
 
68
                        ConfigParser))
274
69
 
275
70
    def test_calls_read_filenames(self):
276
 
        # replace the class that is constructured, to check its parameters
277
 
        oldparserclass = config.ConfigObj
278
 
        config.ConfigObj = InstrumentedConfigObj
279
 
        my_config = config.GlobalConfig()
 
71
        # note the monkey patching. if config access was via a class instance,
 
72
        # we would not have to - if this changes in future, be sure to stop 
 
73
        # monkey patching RBC 20051011
 
74
        oldparserclass = config.ConfigParser
 
75
        config.ConfigParser = InstrumentedConfigParser
280
76
        try:
281
 
            parser = my_config._get_parser()
 
77
            parser = config._get_config_parser()
282
78
        finally:
283
 
            config.ConfigObj = oldparserclass
284
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
285
 
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
286
 
                                          'utf-8')])
287
 
 
288
 
 
289
 
class TestBranchConfig(TestCaseInTempDir):
290
 
 
291
 
    def test_constructs(self):
292
 
        branch = FakeBranch()
293
 
        my_config = config.BranchConfig(branch)
294
 
        self.assertRaises(TypeError, config.BranchConfig)
295
 
 
296
 
    def test_get_location_config(self):
297
 
        branch = FakeBranch()
298
 
        my_config = config.BranchConfig(branch)
299
 
        location_config = my_config._get_location_config()
300
 
        self.assertEqual(branch.base, location_config.location)
301
 
        self.failUnless(location_config is my_config._get_location_config())
302
 
 
303
 
 
304
 
class TestGlobalConfigItems(TestCase):
 
79
            config.ConfigParser = oldparserclass
 
80
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
 
81
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
 
82
 
 
83
 
 
84
class TestConfigItems(TestCase):
 
85
 
 
86
    def setUp(self):
 
87
        super(TestConfigItems, self).setUp()
 
88
        self.bzr_email = os.environ.get('BZREMAIL')
 
89
        if self.bzr_email is not None:
 
90
            del os.environ['BZREMAIL']
 
91
        self.email = os.environ.get('EMAIL')
 
92
        if self.email is not None:
 
93
            del os.environ['EMAIL']
 
94
        self.oldenv = os.environ.get('HOME', None)
 
95
        os.environ['HOME'] = os.getcwd()
 
96
 
 
97
    def tearDown(self):
 
98
        os.environ['HOME'] = self.oldenv
 
99
        if self.bzr_email is not None:
 
100
            os.environ['BZREMAIL'] = self.bzr_email
 
101
        if self.email is not None:
 
102
            os.environ['EMAIL'] = self.email
 
103
        super(TestConfigItems, self).tearDown()
305
104
 
306
105
    def test_user_id(self):
307
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
308
 
        my_config = config.GlobalConfig()
309
 
        my_config._parser = my_config._get_parser(file=config_file)
310
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
311
 
                         my_config._get_user_id())
 
106
        config_file = StringIO(sample_config_text)
 
107
        parser = config._get_config_parser(file=config_file)
 
108
        self.assertEqual("Robert Collins <robertc@example.com>",
 
109
                         config._get_user_id(parser = parser))
312
110
 
313
111
    def test_absent_user_id(self):
314
112
        config_file = StringIO("")
315
 
        my_config = config.GlobalConfig()
316
 
        my_config._parser = my_config._get_parser(file=config_file)
317
 
        self.assertEqual(None, my_config._get_user_id())
318
 
 
319
 
    def test_configured_editor(self):
320
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
321
 
        my_config = config.GlobalConfig()
322
 
        my_config._parser = my_config._get_parser(file=config_file)
323
 
        self.assertEqual("vim", my_config.get_editor())
324
 
 
325
 
    def test_signatures_always(self):
326
 
        config_file = StringIO(sample_always_signatures)
327
 
        my_config = config.GlobalConfig()
328
 
        my_config._parser = my_config._get_parser(file=config_file)
329
 
        self.assertEqual(config.CHECK_ALWAYS,
330
 
                         my_config.signature_checking())
331
 
        self.assertEqual(True, my_config.signature_needed())
332
 
 
333
 
    def test_signatures_if_possible(self):
334
 
        config_file = StringIO(sample_maybe_signatures)
335
 
        my_config = config.GlobalConfig()
336
 
        my_config._parser = my_config._get_parser(file=config_file)
337
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
338
 
                         my_config.signature_checking())
339
 
        self.assertEqual(False, my_config.signature_needed())
340
 
 
341
 
    def test_signatures_ignore(self):
342
 
        config_file = StringIO(sample_ignore_signatures)
343
 
        my_config = config.GlobalConfig()
344
 
        my_config._parser = my_config._get_parser(file=config_file)
345
 
        self.assertEqual(config.CHECK_NEVER,
346
 
                         my_config.signature_checking())
347
 
        self.assertEqual(False, my_config.signature_needed())
348
 
 
349
 
    def _get_sample_config(self):
350
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
351
 
        my_config = config.GlobalConfig()
352
 
        my_config._parser = my_config._get_parser(file=config_file)
353
 
        return my_config
354
 
 
355
 
    def test_gpg_signing_command(self):
356
 
        my_config = self._get_sample_config()
357
 
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
358
 
        self.assertEqual(False, my_config.signature_needed())
359
 
 
360
 
    def _get_empty_config(self):
361
 
        config_file = StringIO("")
362
 
        my_config = config.GlobalConfig()
363
 
        my_config._parser = my_config._get_parser(file=config_file)
364
 
        return my_config
365
 
 
366
 
    def test_gpg_signing_command_unset(self):
367
 
        my_config = self._get_empty_config()
368
 
        self.assertEqual("gpg", my_config.gpg_signing_command())
369
 
 
370
 
    def test_get_user_option_default(self):
371
 
        my_config = self._get_empty_config()
372
 
        self.assertEqual(None, my_config.get_user_option('no_option'))
373
 
 
374
 
    def test_get_user_option_global(self):
375
 
        my_config = self._get_sample_config()
376
 
        self.assertEqual("something",
377
 
                         my_config.get_user_option('user_global_option'))
378
 
        
379
 
    def test_post_commit_default(self):
380
 
        my_config = self._get_sample_config()
381
 
        self.assertEqual(None, my_config.post_commit())
382
 
 
383
 
    def test_configured_logformat(self):
384
 
        my_config = self._get_sample_config()
385
 
        self.assertEqual("short", my_config.log_format())
386
 
 
387
 
    def test_get_alias(self):
388
 
        my_config = self._get_sample_config()
389
 
        self.assertEqual('help', my_config.get_alias('h'))
390
 
 
391
 
    def test_get_no_alias(self):
392
 
        my_config = self._get_sample_config()
393
 
        self.assertEqual(None, my_config.get_alias('foo'))
394
 
 
395
 
    def test_get_long_alias(self):
396
 
        my_config = self._get_sample_config()
397
 
        self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
398
 
 
399
 
class TestLocationConfig(TestCase):
400
 
 
401
 
    def test_constructs(self):
402
 
        my_config = config.LocationConfig('http://example.com')
403
 
        self.assertRaises(TypeError, config.LocationConfig)
404
 
 
405
 
    def test_branch_calls_read_filenames(self):
406
 
        # This is testing the correct file names are provided.
407
 
        # TODO: consolidate with the test for GlobalConfigs filename checks.
408
 
        #
409
 
        # replace the class that is constructured, to check its parameters
410
 
        oldparserclass = config.ConfigObj
411
 
        config.ConfigObj = InstrumentedConfigObj
412
 
        my_config = config.LocationConfig('http://www.example.com')
413
 
        try:
414
 
            parser = my_config._get_parser()
415
 
        finally:
416
 
            config.ConfigObj = oldparserclass
417
 
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
418
 
        self.assertEqual(parser._calls,
419
 
                         [('__init__', config.branches_config_filename())])
420
 
 
421
 
    def test_get_global_config(self):
422
 
        my_config = config.LocationConfig('http://example.com')
423
 
        global_config = my_config._get_global_config()
424
 
        self.failUnless(isinstance(global_config, config.GlobalConfig))
425
 
        self.failUnless(global_config is my_config._get_global_config())
426
 
 
427
 
    def test__get_section_no_match(self):
428
 
        self.get_location_config('/')
429
 
        self.assertEqual(None, self.my_config._get_section())
430
 
        
431
 
    def test__get_section_exact(self):
432
 
        self.get_location_config('http://www.example.com')
433
 
        self.assertEqual('http://www.example.com',
434
 
                         self.my_config._get_section())
435
 
   
436
 
    def test__get_section_suffix_does_not(self):
437
 
        self.get_location_config('http://www.example.com-com')
438
 
        self.assertEqual(None, self.my_config._get_section())
439
 
 
440
 
    def test__get_section_subdir_recursive(self):
441
 
        self.get_location_config('http://www.example.com/com')
442
 
        self.assertEqual('http://www.example.com',
443
 
                         self.my_config._get_section())
444
 
 
445
 
    def test__get_section_subdir_matches(self):
446
 
        self.get_location_config('http://www.example.com/useglobal')
447
 
        self.assertEqual('http://www.example.com/useglobal',
448
 
                         self.my_config._get_section())
449
 
 
450
 
    def test__get_section_subdir_nonrecursive(self):
451
 
        self.get_location_config(
452
 
            'http://www.example.com/useglobal/childbranch')
453
 
        self.assertEqual('http://www.example.com',
454
 
                         self.my_config._get_section())
455
 
 
456
 
    def test__get_section_subdir_trailing_slash(self):
457
 
        self.get_location_config('/b')
458
 
        self.assertEqual('/b/', self.my_config._get_section())
459
 
 
460
 
    def test__get_section_subdir_child(self):
461
 
        self.get_location_config('/a/foo')
462
 
        self.assertEqual('/a/*', self.my_config._get_section())
463
 
 
464
 
    def test__get_section_subdir_child_child(self):
465
 
        self.get_location_config('/a/foo/bar')
466
 
        self.assertEqual('/a/', self.my_config._get_section())
467
 
 
468
 
    def test__get_section_trailing_slash_with_children(self):
469
 
        self.get_location_config('/a/')
470
 
        self.assertEqual('/a/', self.my_config._get_section())
471
 
 
472
 
    def test__get_section_explicit_over_glob(self):
473
 
        self.get_location_config('/a/c')
474
 
        self.assertEqual('/a/c', self.my_config._get_section())
475
 
 
476
 
    def get_location_config(self, location, global_config=None):
477
 
        if global_config is None:
478
 
            global_file = StringIO(sample_config_text)
479
 
        else:
480
 
            global_file = StringIO(global_config)
481
 
        branches_file = StringIO(sample_branches_text)
482
 
        self.my_config = config.LocationConfig(location)
483
 
        self.my_config._get_parser(branches_file)
484
 
        self.my_config._get_global_config()._get_parser(global_file)
485
 
 
486
 
    def test_location_without_username(self):
487
 
        self.get_location_config('http://www.example.com/useglobal')
488
 
        self.assertEqual('Robert Collins <robertc@example.com>',
489
 
                         self.my_config.username())
490
 
 
491
 
    def test_location_not_listed(self):
492
 
        self.get_location_config('/home/robertc/sources')
493
 
        self.assertEqual('Robert Collins <robertc@example.com>',
494
 
                         self.my_config.username())
495
 
 
496
 
    def test_overriding_location(self):
497
 
        self.get_location_config('http://www.example.com/foo')
498
 
        self.assertEqual('Robert Collins <robertc@example.org>',
499
 
                         self.my_config.username())
500
 
 
501
 
    def test_signatures_not_set(self):
502
 
        self.get_location_config('http://www.example.com',
503
 
                                 global_config=sample_ignore_signatures)
504
 
        self.assertEqual(config.CHECK_NEVER,
505
 
                         self.my_config.signature_checking())
506
 
 
507
 
    def test_signatures_never(self):
508
 
        self.get_location_config('/a/c')
509
 
        self.assertEqual(config.CHECK_NEVER,
510
 
                         self.my_config.signature_checking())
511
 
        
512
 
    def test_signatures_when_available(self):
513
 
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
514
 
        self.assertEqual(config.CHECK_IF_POSSIBLE,
515
 
                         self.my_config.signature_checking())
516
 
        
517
 
    def test_signatures_always(self):
518
 
        self.get_location_config('/b')
519
 
        self.assertEqual(config.CHECK_ALWAYS,
520
 
                         self.my_config.signature_checking())
521
 
        
522
 
    def test_gpg_signing_command(self):
523
 
        self.get_location_config('/b')
524
 
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
525
 
 
526
 
    def test_gpg_signing_command_missing(self):
527
 
        self.get_location_config('/a')
528
 
        self.assertEqual("false", self.my_config.gpg_signing_command())
529
 
 
530
 
    def test_get_user_option_global(self):
531
 
        self.get_location_config('/a')
532
 
        self.assertEqual('something',
533
 
                         self.my_config.get_user_option('user_global_option'))
534
 
 
535
 
    def test_get_user_option_local(self):
536
 
        self.get_location_config('/a')
537
 
        self.assertEqual('local',
538
 
                         self.my_config.get_user_option('user_local_option'))
539
 
        
540
 
    def test_post_commit_default(self):
541
 
        self.get_location_config('/a/c')
542
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
543
 
                         self.my_config.post_commit())
544
 
 
545
 
 
546
 
class TestLocationConfig(TestCaseInTempDir):
547
 
 
548
 
    def get_location_config(self, location, global_config=None):
549
 
        if global_config is None:
550
 
            global_file = StringIO(sample_config_text.encode('utf-8'))
551
 
        else:
552
 
            global_file = StringIO(global_config.encode('utf-8'))
553
 
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
554
 
        self.my_config = config.LocationConfig(location)
555
 
        self.my_config._get_parser(branches_file)
556
 
        self.my_config._get_global_config()._get_parser(global_file)
557
 
 
558
 
    def test_set_user_setting_sets_and_saves(self):
559
 
        self.get_location_config('/a/c')
560
 
        record = InstrumentedConfigObj("foo")
561
 
        self.my_config._parser = record
562
 
 
563
 
        real_mkdir = os.mkdir
564
 
        self.created = False
565
 
        def checked_mkdir(path, mode=0777):
566
 
            self.log('making directory: %s', path)
567
 
            real_mkdir(path, mode)
568
 
            self.created = True
569
 
 
570
 
        os.mkdir = checked_mkdir
571
 
        try:
572
 
            self.my_config.set_user_option('foo', 'bar')
573
 
        finally:
574
 
            os.mkdir = real_mkdir
575
 
 
576
 
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
577
 
        self.assertEqual([('__contains__', '/a/c'),
578
 
                          ('__contains__', '/a/c/'),
579
 
                          ('__setitem__', '/a/c', {}),
580
 
                          ('__getitem__', '/a/c'),
581
 
                          ('__setitem__', 'foo', 'bar'),
582
 
                          ('write',)],
583
 
                         record._calls[1:])
584
 
 
585
 
 
586
 
class TestBranchConfigItems(TestCase):
587
 
 
588
 
    def test_user_id(self):
589
 
        branch = FakeBranch()
590
 
        my_config = config.BranchConfig(branch)
591
 
        self.assertEqual("Robert Collins <robertc@example.net>",
592
 
                         my_config._get_user_id())
593
 
        branch.control_files.email = "John"
594
 
        self.assertEqual("John", my_config._get_user_id())
595
 
 
596
 
    def test_not_set_in_branch(self):
597
 
        branch = FakeBranch()
598
 
        my_config = config.BranchConfig(branch)
599
 
        branch.control_files.email = None
600
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
601
 
        (my_config._get_location_config().
602
 
            _get_global_config()._get_parser(config_file))
603
 
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
604
 
                         my_config._get_user_id())
605
 
        branch.control_files.email = "John"
606
 
        self.assertEqual("John", my_config._get_user_id())
607
 
 
608
 
    def test_BZREMAIL_OVERRIDES(self):
609
 
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
610
 
        branch = FakeBranch()
611
 
        my_config = config.BranchConfig(branch)
612
 
        self.assertEqual("Robert Collins <robertc@example.org>",
613
 
                         my_config.username())
614
 
    
615
 
    def test_signatures_forced(self):
616
 
        branch = FakeBranch()
617
 
        my_config = config.BranchConfig(branch)
618
 
        config_file = StringIO(sample_always_signatures)
619
 
        (my_config._get_location_config().
620
 
            _get_global_config()._get_parser(config_file))
621
 
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
622
 
 
623
 
    def test_gpg_signing_command(self):
624
 
        branch = FakeBranch()
625
 
        my_config = config.BranchConfig(branch)
626
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
627
 
        (my_config._get_location_config().
628
 
            _get_global_config()._get_parser(config_file))
629
 
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
630
 
 
631
 
    def test_get_user_option_global(self):
632
 
        branch = FakeBranch()
633
 
        my_config = config.BranchConfig(branch)
634
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
635
 
        (my_config._get_location_config().
636
 
            _get_global_config()._get_parser(config_file))
637
 
        self.assertEqual('something',
638
 
                         my_config.get_user_option('user_global_option'))
639
 
 
640
 
    def test_post_commit_default(self):
641
 
        branch = FakeBranch()
642
 
        branch.base='/a/c'
643
 
        my_config = config.BranchConfig(branch)
644
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
645
 
        (my_config._get_location_config().
646
 
            _get_global_config()._get_parser(config_file))
647
 
        branch_file = StringIO(sample_branches_text)
648
 
        my_config._get_location_config()._get_parser(branch_file)
649
 
        self.assertEqual('bzrlib.tests.test_config.post_commit',
650
 
                         my_config.post_commit())
651
 
 
652
 
 
653
 
class TestMailAddressExtraction(TestCase):
654
 
 
655
 
    def test_extract_email_address(self):
656
 
        self.assertEqual('jane@test.com',
657
 
                         config.extract_email_address('Jane <jane@test.com>'))
658
 
        self.assertRaises(errors.BzrError,
659
 
                          config.extract_email_address, 'Jane Tester')
 
113
        parser = config._get_config_parser(file=config_file)
 
114
        self.assertEqual(None,
 
115
                         config._get_user_id(parser = parser))
 
116
 
 
117
    def test_configured_edit(self):
 
118
        config_file = StringIO(sample_config_text)
 
119
        parser = config._get_config_parser(file=config_file)
 
120
        self.assertEqual("vim", config.get_editor(parser = parser))