/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
1
# Copyright (C) 2005, 2006 by Canonical Ltd
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
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
1474 by Robert Collins
Merge from Aaron Bentley.
20
from bzrlib.util.configobj.configobj import ConfigObj, ConfigObjError
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
21
from cStringIO import StringIO
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
22
import os
23
import sys
24
25
#import bzrlib specific imports here
26
import bzrlib.config as config
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
27
import bzrlib.errors as errors
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
28
from bzrlib.tests import TestCase, TestCaseInTempDir
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
29
30
1553.6.12 by Erik Bågfors
remove AliasConfig, based on input from abentley
31
sample_long_alias="log -r-15..-1 --line"
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
32
sample_config_text = ("[DEFAULT]\n"
1551.2.21 by Aaron Bentley
Formatted unicode config tests as ASCII
33
                      u"email=Erik B\u00e5gfors <erik@bagfors.nu>\n"
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
34
                      "editor=vim\n"
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
35
                      "gpg_signing_command=gnome-gpg\n"
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
36
                      "log_format=short\n"
1553.6.12 by Erik Bågfors
remove AliasConfig, based on input from abentley
37
                      "user_global_option=something\n"
38
                      "[ALIASES]\n"
39
                      "h=help\n"
40
                      "ll=" + sample_long_alias + "\n")
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
41
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
42
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
43
sample_always_signatures = ("[DEFAULT]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
44
                            "check_signatures=require\n")
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
45
46
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
47
sample_ignore_signatures = ("[DEFAULT]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
48
                            "check_signatures=ignore\n")
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
49
50
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
51
sample_maybe_signatures = ("[DEFAULT]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
52
                            "check_signatures=check-available\n")
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
53
54
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
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"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
62
                        "check_signatures=require\n"
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
63
                        "# test trailing / matching with no children\n"
64
                        "[/a/]\n"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
65
                        "check_signatures=check-available\n"
1442.1.56 by Robert Collins
gpg_signing_command configuration item
66
                        "gpg_signing_command=false\n"
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
67
                        "user_local_option=local\n"
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
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"
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
73
                        "check_signatures=ignore\n"
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
74
                        "post_commit=bzrlib.tests.test_config.post_commit\n"
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
75
                        "#testing explicit beats globs\n")
76
1553.6.3 by Erik Bågfors
tests for AliasesConfig
77
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
78
1474 by Robert Collins
Merge from Aaron Bentley.
79
class InstrumentedConfigObj(object):
80
    """A config obj look-enough-alike to record calls made to it."""
81
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
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
1551.2.20 by Aaron Bentley
Treated config files as utf-8
90
    def __init__(self, input, encoding=None):
91
        self._calls = [('__init__', input, encoding)]
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
92
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
93
    def __setitem__(self, key, value):
94
        self._calls.append(('__setitem__', key, value))
95
1551.2.49 by abentley
Made ConfigObj output binary-identical files on win32 and *nix
96
    def write(self, arg):
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
97
        self._calls.append(('write',))
98
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
99
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
100
class FakeBranch(object):
101
102
    def __init__(self):
103
        self.base = "http://example.com/branches/demo"
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
104
        self.control_files = FakeControlFiles()
105
106
107
class FakeControlFiles(object):
108
109
    def __init__(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
110
        self.email = 'Robert Collins <robertc@example.net>\n'
111
1185.65.29 by Robert Collins
Implement final review suggestions.
112
    def get_utf8(self, filename):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
113
        if filename != 'email':
114
            raise NotImplementedError
115
        if self.email is not None:
116
            return StringIO(self.email)
1185.31.45 by John Arbash Meinel
Refactoring Exceptions found some places where the wrong exception was caught.
117
        raise errors.NoSuchFile(filename)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
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__()
125
        self._calls = []
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
126
        self._signatures = config.CHECK_NEVER
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
127
128
    def _get_user_id(self):
129
        self._calls.append('_get_user_id')
130
        return "Robert Collins <robert.collins@example.org>"
131
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
132
    def _get_signature_checking(self):
133
        self._calls.append('_get_signature_checking')
134
        return self._signatures
135
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
136
1556.2.2 by Aaron Bentley
Fixed get_bool
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
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
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)
1442.1.14 by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE
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
1442.1.15 by Robert Collins
make getting the signature checking policy a template method
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
1442.1.56 by Robert Collins
gpg_signing_command configuration item
190
    def test_gpg_signing_command_default(self):
191
        my_config = config.Config()
192
        self.assertEqual('gpg', my_config.gpg_signing_command())
193
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
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
1472 by Robert Collins
post commit hook, first pass implementation
198
    def test_post_commit_default(self):
199
        my_config = config.Config()
200
        self.assertEqual(None, my_config.post_commit())
201
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
202
    def test_log_format_default(self):
1553.2.8 by Erik Bågfors
tests for config log_formatter
203
        my_config = config.Config()
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
204
        self.assertEqual('long', my_config.log_format())
1553.2.8 by Erik Bågfors
tests for config log_formatter
205
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
206
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
207
class TestConfigPath(TestCase):
208
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
209
    def setUp(self):
210
        super(TestConfigPath, self).setUp()
1185.38.5 by John Arbash Meinel
Updating testconfig to handle missing environment variables
211
        self.old_home = os.environ.get('HOME', None)
212
        self.old_appdata = os.environ.get('APPDATA', None)
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
213
        os.environ['HOME'] = '/home/bogus'
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
214
        os.environ['APPDATA'] = \
215
            r'C:\Documents and Settings\bogus\Application Data'
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
216
217
    def tearDown(self):
1185.38.5 by John Arbash Meinel
Updating testconfig to handle missing environment variables
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
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
226
        super(TestConfigPath, self).tearDown()
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
227
    
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
228
    def test_config_dir(self):
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
229
        if sys.platform == 'win32':
230
            self.assertEqual(config.config_dir(), 
1185.31.36 by John Arbash Meinel
Fixup test_config.py to handle new paths
231
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0')
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
232
        else:
233
            self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
234
235
    def test_config_filename(self):
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
236
        if sys.platform == 'win32':
237
            self.assertEqual(config.config_filename(), 
1185.31.36 by John Arbash Meinel
Fixup test_config.py to handle new paths
238
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/bazaar.conf')
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
239
        else:
240
            self.assertEqual(config.config_filename(),
241
                             '/home/bogus/.bazaar/bazaar.conf')
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
242
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
243
    def test_branches_config_filename(self):
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
244
        if sys.platform == 'win32':
245
            self.assertEqual(config.branches_config_filename(), 
1185.31.36 by John Arbash Meinel
Fixup test_config.py to handle new paths
246
                'C:/Documents and Settings/bogus/Application Data/bazaar/2.0/branches.conf')
1185.38.1 by John Arbash Meinel
Adding my win32 patch for moving the home directory.
247
        else:
248
            self.assertEqual(config.branches_config_filename(),
249
                             '/home/bogus/.bazaar/branches.conf')
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
250
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
251
class TestIniConfig(TestCase):
252
253
    def test_contructs(self):
254
        my_config = config.IniBasedConfig("nothing")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
255
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
256
    def test_from_fp(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
257
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
258
        my_config = config.IniBasedConfig(None)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
259
        self.failUnless(
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
260
            isinstance(my_config._get_parser(file=config_file),
1474 by Robert Collins
Merge from Aaron Bentley.
261
                        ConfigObj))
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
262
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
263
    def test_cached(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
264
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
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):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
273
        my_config = config.GlobalConfig()
274
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
275
    def test_calls_read_filenames(self):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
276
        # replace the class that is constructured, to check its parameters
1474 by Robert Collins
Merge from Aaron Bentley.
277
        oldparserclass = config.ConfigObj
278
        config.ConfigObj = InstrumentedConfigObj
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
279
        my_config = config.GlobalConfig()
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
280
        try:
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
281
            parser = my_config._get_parser()
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
282
        finally:
1474 by Robert Collins
Merge from Aaron Bentley.
283
            config.ConfigObj = oldparserclass
284
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
1551.2.20 by Aaron Bentley
Treated config files as utf-8
285
        self.assertEqual(parser._calls, [('__init__', config.config_filename(),
286
                                          'utf-8')])
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
287
288
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
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
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
304
class TestGlobalConfigItems(TestCase):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
305
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
306
    def test_user_id(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
307
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
308
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
309
        my_config._parser = my_config._get_parser(file=config_file)
1551.2.21 by Aaron Bentley
Formatted unicode config tests as ASCII
310
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
311
                         my_config._get_user_id())
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
312
313
    def test_absent_user_id(self):
314
        config_file = StringIO("")
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
315
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
316
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
317
        self.assertEqual(None, my_config._get_user_id())
318
319
    def test_configured_editor(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
320
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
321
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
322
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
323
        self.assertEqual("vim", my_config.get_editor())
324
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
325
    def test_signatures_always(self):
326
        config_file = StringIO(sample_always_signatures)
327
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
328
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
329
        self.assertEqual(config.CHECK_ALWAYS,
330
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
331
        self.assertEqual(True, my_config.signature_needed())
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
332
333
    def test_signatures_if_possible(self):
334
        config_file = StringIO(sample_maybe_signatures)
335
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
336
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
337
        self.assertEqual(config.CHECK_IF_POSSIBLE,
338
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
339
        self.assertEqual(False, my_config.signature_needed())
1442.1.17 by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking
340
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
341
    def test_signatures_ignore(self):
342
        config_file = StringIO(sample_ignore_signatures)
343
        my_config = config.GlobalConfig()
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
344
        my_config._parser = my_config._get_parser(file=config_file)
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
345
        self.assertEqual(config.CHECK_NEVER,
346
                         my_config.signature_checking())
1442.1.21 by Robert Collins
create signature_needed() call for commit to trigger creating signatures
347
        self.assertEqual(False, my_config.signature_needed())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
348
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
349
    def _get_sample_config(self):
1551.2.20 by Aaron Bentley
Treated config files as utf-8
350
        config_file = StringIO(sample_config_text.encode('utf-8'))
1534.7.154 by Aaron Bentley
Removed changes from bzr.ab 1529..1536
351
        my_config = config.GlobalConfig()
352
        my_config._parser = my_config._get_parser(file=config_file)
353
        return my_config
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
354
1442.1.56 by Robert Collins
gpg_signing_command configuration item
355
    def test_gpg_signing_command(self):
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
356
        my_config = self._get_sample_config()
1442.1.56 by Robert Collins
gpg_signing_command configuration item
357
        self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
358
        self.assertEqual(False, my_config.signature_needed())
359
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
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
1442.1.59 by Robert Collins
Add re-sign command to generate a digital signature on a single revision.
366
    def test_gpg_signing_command_unset(self):
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
367
        my_config = self._get_empty_config()
1442.1.59 by Robert Collins
Add re-sign command to generate a digital signature on a single revision.
368
        self.assertEqual("gpg", my_config.gpg_signing_command())
369
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
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'))
1472 by Robert Collins
post commit hook, first pass implementation
378
        
379
    def test_post_commit_default(self):
380
        my_config = self._get_sample_config()
381
        self.assertEqual(None, my_config.post_commit())
382
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
383
    def test_configured_logformat(self):
1553.2.8 by Erik Bågfors
tests for config log_formatter
384
        my_config = self._get_sample_config()
1553.2.9 by Erik Bågfors
log_formatter => log_format for "named" formatters
385
        self.assertEqual("short", my_config.log_format())
1553.2.8 by Erik Bågfors
tests for config log_formatter
386
1553.6.12 by Erik Bågfors
remove AliasConfig, based on input from abentley
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'))
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
398
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
399
400
class TestLocationConfig(TestCaseInTempDir):
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
401
402
    def test_constructs(self):
403
        my_config = config.LocationConfig('http://example.com')
404
        self.assertRaises(TypeError, config.LocationConfig)
405
406
    def test_branch_calls_read_filenames(self):
1474 by Robert Collins
Merge from Aaron Bentley.
407
        # This is testing the correct file names are provided.
408
        # TODO: consolidate with the test for GlobalConfigs filename checks.
409
        #
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
410
        # replace the class that is constructured, to check its parameters
1474 by Robert Collins
Merge from Aaron Bentley.
411
        oldparserclass = config.ConfigObj
412
        config.ConfigObj = InstrumentedConfigObj
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
413
        my_config = config.LocationConfig('http://www.example.com')
414
        try:
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
415
            parser = my_config._get_parser()
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
416
        finally:
1474 by Robert Collins
Merge from Aaron Bentley.
417
            config.ConfigObj = oldparserclass
418
        self.failUnless(isinstance(parser, InstrumentedConfigObj))
419
        self.assertEqual(parser._calls,
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
420
                         [('__init__', config.branches_config_filename(),
421
                           'utf-8')])
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
422
423
    def test_get_global_config(self):
424
        my_config = config.LocationConfig('http://example.com')
425
        global_config = my_config._get_global_config()
426
        self.failUnless(isinstance(global_config, config.GlobalConfig))
427
        self.failUnless(global_config is my_config._get_global_config())
428
429
    def test__get_section_no_match(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
430
        self.get_location_config('/')
431
        self.assertEqual(None, self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
432
        
433
    def test__get_section_exact(self):
1442.1.9 by Robert Collins
exact section test passes
434
        self.get_location_config('http://www.example.com')
435
        self.assertEqual('http://www.example.com',
436
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
437
   
438
    def test__get_section_suffix_does_not(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
439
        self.get_location_config('http://www.example.com-com')
440
        self.assertEqual(None, self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
441
442
    def test__get_section_subdir_recursive(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
443
        self.get_location_config('http://www.example.com/com')
444
        self.assertEqual('http://www.example.com',
445
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
446
447
    def test__get_section_subdir_matches(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
448
        self.get_location_config('http://www.example.com/useglobal')
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
449
        self.assertEqual('http://www.example.com/useglobal',
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
450
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
451
452
    def test__get_section_subdir_nonrecursive(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
453
        self.get_location_config(
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
454
            'http://www.example.com/useglobal/childbranch')
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
455
        self.assertEqual('http://www.example.com',
456
                         self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
457
458
    def test__get_section_subdir_trailing_slash(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
459
        self.get_location_config('/b')
460
        self.assertEqual('/b/', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
461
462
    def test__get_section_subdir_child(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
463
        self.get_location_config('/a/foo')
464
        self.assertEqual('/a/*', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
465
466
    def test__get_section_subdir_child_child(self):
1442.1.11 by Robert Collins
LocationConfig lookups for non matching prefixes pass tests
467
        self.get_location_config('/a/foo/bar')
468
        self.assertEqual('/a/', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
469
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
470
    def test__get_section_trailing_slash_with_children(self):
471
        self.get_location_config('/a/')
472
        self.assertEqual('/a/', self.my_config._get_section())
473
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
474
    def test__get_section_explicit_over_glob(self):
1442.1.10 by Robert Collins
explicit over glob test passes
475
        self.get_location_config('/a/c')
476
        self.assertEqual('/a/c', self.my_config._get_section())
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
477
478
479
    def test_location_without_username(self):
480
        self.get_location_config('http://www.example.com/useglobal')
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
481
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
482
                         self.my_config.username())
483
484
    def test_location_not_listed(self):
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
485
        """Test that the global username is used when no location matches"""
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
486
        self.get_location_config('/home/robertc/sources')
1704.2.18 by Martin Pool
Remove duplicated TestLocationConfig and update previously hidden tests. (#32587)
487
        self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
1442.1.8 by Robert Collins
preparing some tests for LocationConfig
488
                         self.my_config.username())
489
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
490
    def test_overriding_location(self):
491
        self.get_location_config('http://www.example.com/foo')
492
        self.assertEqual('Robert Collins <robertc@example.org>',
493
                         self.my_config.username())
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
494
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
495
    def test_signatures_not_set(self):
496
        self.get_location_config('http://www.example.com',
497
                                 global_config=sample_ignore_signatures)
498
        self.assertEqual(config.CHECK_NEVER,
499
                         self.my_config.signature_checking())
500
501
    def test_signatures_never(self):
502
        self.get_location_config('/a/c')
503
        self.assertEqual(config.CHECK_NEVER,
504
                         self.my_config.signature_checking())
505
        
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
506
    def test_signatures_when_available(self):
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
507
        self.get_location_config('/a/', global_config=sample_ignore_signatures)
1442.1.16 by Robert Collins
allow global overriding of signature policy to never check
508
        self.assertEqual(config.CHECK_IF_POSSIBLE,
509
                         self.my_config.signature_checking())
1442.1.13 by Robert Collins
branches.conf is now able to override the users email
510
        
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
511
    def test_signatures_always(self):
512
        self.get_location_config('/b')
513
        self.assertEqual(config.CHECK_ALWAYS,
514
                         self.my_config.signature_checking())
515
        
1442.1.56 by Robert Collins
gpg_signing_command configuration item
516
    def test_gpg_signing_command(self):
517
        self.get_location_config('/b')
518
        self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
519
520
    def test_gpg_signing_command_missing(self):
521
        self.get_location_config('/a')
522
        self.assertEqual("false", self.my_config.gpg_signing_command())
523
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
524
    def test_get_user_option_global(self):
525
        self.get_location_config('/a')
526
        self.assertEqual('something',
527
                         self.my_config.get_user_option('user_global_option'))
528
529
    def test_get_user_option_local(self):
530
        self.get_location_config('/a')
531
        self.assertEqual('local',
532
                         self.my_config.get_user_option('user_local_option'))
1472 by Robert Collins
post commit hook, first pass implementation
533
        
534
    def test_post_commit_default(self):
535
        self.get_location_config('/a/c')
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
536
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1472 by Robert Collins
post commit hook, first pass implementation
537
                         self.my_config.post_commit())
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
538
1502 by Robert Collins
Bugfix the config test suite to not create .bazaar in the dir where it is run.
539
    def get_location_config(self, location, global_config=None):
540
        if global_config is None:
1551.2.20 by Aaron Bentley
Treated config files as utf-8
541
            global_file = StringIO(sample_config_text.encode('utf-8'))
1502 by Robert Collins
Bugfix the config test suite to not create .bazaar in the dir where it is run.
542
        else:
1551.2.20 by Aaron Bentley
Treated config files as utf-8
543
            global_file = StringIO(global_config.encode('utf-8'))
544
        branches_file = StringIO(sample_branches_text.encode('utf-8'))
1502 by Robert Collins
Bugfix the config test suite to not create .bazaar in the dir where it is run.
545
        self.my_config = config.LocationConfig(location)
546
        self.my_config._get_parser(branches_file)
547
        self.my_config._get_global_config()._get_parser(global_file)
548
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
549
    def test_set_user_setting_sets_and_saves(self):
550
        self.get_location_config('/a/c')
551
        record = InstrumentedConfigObj("foo")
552
        self.my_config._parser = record
1185.62.6 by John Arbash Meinel
Updated test_set_user_setting_sets_and_saves to remove the print statement, and make sure it is doing the right thing
553
554
        real_mkdir = os.mkdir
555
        self.created = False
556
        def checked_mkdir(path, mode=0777):
557
            self.log('making directory: %s', path)
558
            real_mkdir(path, mode)
559
            self.created = True
560
561
        os.mkdir = checked_mkdir
562
        try:
563
            self.my_config.set_user_option('foo', 'bar')
564
        finally:
565
            os.mkdir = real_mkdir
566
567
        self.failUnless(self.created, 'Failed to create ~/.bazaar')
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
568
        self.assertEqual([('__contains__', '/a/c'),
569
                          ('__contains__', '/a/c/'),
570
                          ('__setitem__', '/a/c', {}),
571
                          ('__getitem__', '/a/c'),
572
                          ('__setitem__', 'foo', 'bar'),
573
                          ('write',)],
574
                         record._calls[1:])
575
1185.62.7 by John Arbash Meinel
Whitespace cleanup.
576
1442.1.55 by Robert Collins
move environment preservation up to the root test case, making it available to all tests
577
class TestBranchConfigItems(TestCase):
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
578
579
    def test_user_id(self):
580
        branch = FakeBranch()
581
        my_config = config.BranchConfig(branch)
582
        self.assertEqual("Robert Collins <robertc@example.net>",
583
                         my_config._get_user_id())
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
584
        branch.control_files.email = "John"
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
585
        self.assertEqual("John", my_config._get_user_id())
586
587
    def test_not_set_in_branch(self):
588
        branch = FakeBranch()
589
        my_config = config.BranchConfig(branch)
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
590
        branch.control_files.email = None
1551.2.20 by Aaron Bentley
Treated config files as utf-8
591
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
592
        (my_config._get_location_config().
1442.1.18 by Robert Collins
permit per branch location overriding of signature checking policy
593
            _get_global_config()._get_parser(config_file))
1551.2.21 by Aaron Bentley
Formatted unicode config tests as ASCII
594
        self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
595
                         my_config._get_user_id())
1185.65.11 by Robert Collins
Disable inheritance for getting at LockableFiles, rather use composition.
596
        branch.control_files.email = "John"
1442.1.6 by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs
597
        self.assertEqual("John", my_config._get_user_id())
598
599
    def test_BZREMAIL_OVERRIDES(self):
600
        os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
601
        branch = FakeBranch()
602
        my_config = config.BranchConfig(branch)
603
        self.assertEqual("Robert Collins <robertc@example.org>",
604
                         my_config.username())
605
    
1442.1.19 by Robert Collins
BranchConfigs inherit signature_checking policy from their LocationConfig.
606
    def test_signatures_forced(self):
607
        branch = FakeBranch()
608
        my_config = config.BranchConfig(branch)
609
        config_file = StringIO(sample_always_signatures)
610
        (my_config._get_location_config().
611
            _get_global_config()._get_parser(config_file))
612
        self.assertEqual(config.CHECK_ALWAYS, my_config.signature_checking())
1442.1.56 by Robert Collins
gpg_signing_command configuration item
613
614
    def test_gpg_signing_command(self):
615
        branch = FakeBranch()
616
        my_config = config.BranchConfig(branch)
1551.2.20 by Aaron Bentley
Treated config files as utf-8
617
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.56 by Robert Collins
gpg_signing_command configuration item
618
        (my_config._get_location_config().
619
            _get_global_config()._get_parser(config_file))
620
        self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
621
622
    def test_get_user_option_global(self):
623
        branch = FakeBranch()
624
        my_config = config.BranchConfig(branch)
1551.2.20 by Aaron Bentley
Treated config files as utf-8
625
        config_file = StringIO(sample_config_text.encode('utf-8'))
1442.1.69 by Robert Collins
config.Config has a 'get_user_option' call that accepts an option name.
626
        (my_config._get_location_config().
627
            _get_global_config()._get_parser(config_file))
628
        self.assertEqual('something',
629
                         my_config.get_user_option('user_global_option'))
1472 by Robert Collins
post commit hook, first pass implementation
630
631
    def test_post_commit_default(self):
632
        branch = FakeBranch()
633
        branch.base='/a/c'
634
        my_config = config.BranchConfig(branch)
1551.2.20 by Aaron Bentley
Treated config files as utf-8
635
        config_file = StringIO(sample_config_text.encode('utf-8'))
1472 by Robert Collins
post commit hook, first pass implementation
636
        (my_config._get_location_config().
637
            _get_global_config()._get_parser(config_file))
638
        branch_file = StringIO(sample_branches_text)
639
        my_config._get_location_config()._get_parser(branch_file)
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
640
        self.assertEqual('bzrlib.tests.test_config.post_commit',
1472 by Robert Collins
post commit hook, first pass implementation
641
                         my_config.post_commit())
1185.33.31 by Martin Pool
Make annotate cope better with revisions committed without a valid
642
643
644
class TestMailAddressExtraction(TestCase):
645
646
    def test_extract_email_address(self):
647
        self.assertEqual('jane@test.com',
648
                         config.extract_email_address('Jane <jane@test.com>'))
649
        self.assertRaises(errors.BzrError,
650
                          config.extract_email_address, 'Jane Tester')