1
# Copyright (C) 2005 by Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
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.
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.
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
18
"""Tests for finding and reading the bzr config file[s]."""
19
# import system imports here
20
from ConfigParser import ConfigParser
21
from cStringIO import StringIO
25
#import bzrlib specific imports here
26
import bzrlib.config as config
27
import bzrlib.errors as errors
28
from bzrlib.selftest import TestCase, TestCaseInTempDir
31
sample_config_text = ("[DEFAULT]\n"
32
"email=Robert Collins <robertc@example.com>\n"
34
"gpg_signing_command=gnome-gpg\n")
36
class InstrumentedConfigParser(object):
37
"""A config parser look-enough-alike to record calls made to it."""
42
def read(self, filenames):
43
self._calls.append(('read', filenames))
46
class FakeBranch(object):
49
self.base = "http://example.com/branches/demo"
50
self.email = 'Robert Collins <robertc@example.net>\n'
52
def controlfile(self, filename, mode):
53
if filename != 'email':
54
raise NotImplementedError
55
if self.email is not None:
56
return StringIO(self.email)
57
raise errors.NoSuchFile
60
class InstrumentedConfig(config.Config):
61
"""An instrumented config that supplies stubs for template methods."""
64
super(InstrumentedConfig, self).__init__()
67
def _get_user_id(self):
68
self._calls.append('_get_user_id')
69
return "Robert Collins <robert.collins@example.org>"
72
class TestConfig(TestCase):
74
def test_constructs(self):
77
def test_no_default_editor(self):
78
self.assertRaises(NotImplementedError, config.Config().get_editor)
80
def test_user_email(self):
81
my_config = InstrumentedConfig()
82
self.assertEqual('robert.collins@example.org', my_config.user_email())
83
self.assertEqual(['_get_user_id'], my_config._calls)
85
def test_username(self):
86
my_config = InstrumentedConfig()
87
self.assertEqual('Robert Collins <robert.collins@example.org>',
89
self.assertEqual(['_get_user_id'], my_config._calls)
92
class TestConfigPath(TestCase):
95
super(TestConfigPath, self).setUp()
96
self.oldenv = os.environ.get('HOME', None)
97
os.environ['HOME'] = '/home/bogus'
100
os.environ['HOME'] = self.oldenv
102
def test_config_dir(self):
103
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
105
def test_config_filename(self):
106
self.assertEqual(config.config_filename(),
107
'/home/bogus/.bazaar/bazaar.conf')
109
def test_branches_config_filename(self):
110
self.assertEqual(config.branches_config_filename(),
111
'/home/bogus/.bazaar/branches.conf')
114
class TestGetConfig(TestCase):
116
def test_constructs(self):
117
my_config = config.GlobalConfig()
119
def test_from_fp(self):
120
config_file = StringIO(sample_config_text)
121
my_config = config.GlobalConfig()
123
isinstance(my_config._get_config_parser(file=config_file),
126
def test_cached(self):
127
config_file = StringIO(sample_config_text)
128
my_config = config.GlobalConfig()
129
parser = my_config._get_config_parser(file=config_file)
130
self.failUnless(my_config._get_config_parser() is parser)
132
def test_calls_read_filenames(self):
133
# replace the class that is constructured, to check its parameters
134
oldparserclass = config.ConfigParser
135
config.ConfigParser = InstrumentedConfigParser
136
my_config = config.GlobalConfig()
138
parser = my_config._get_config_parser()
140
config.ConfigParser = oldparserclass
141
self.failUnless(isinstance(parser, InstrumentedConfigParser))
142
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
145
class TestLocationConfig(TestCase):
147
def test_constructs(self):
148
my_config = config.LocationConfig('http://example.com')
149
self.assertRaises(TypeError, config.LocationConfig)
151
def test_cached(self):
152
config_file = StringIO(sample_config_text)
153
my_config = config.LocationConfig('http://example.com')
154
parser = my_config._get_branches_config_parser(file=config_file)
155
self.failUnless(my_config._get_branches_config_parser() is parser)
157
def test_branches_from_fp(self):
158
config_file = StringIO(sample_config_text)
159
my_config = config.LocationConfig('http://example.com')
160
self.failUnless(isinstance(
161
my_config._get_branches_config_parser(file=config_file),
164
def test_branch_calls_read_filenames(self):
165
# replace the class that is constructured, to check its parameters
166
oldparserclass = config.ConfigParser
167
config.ConfigParser = InstrumentedConfigParser
168
my_config = config.LocationConfig('http://www.example.com')
170
parser = my_config._get_branches_config_parser()
172
config.ConfigParser = oldparserclass
173
self.failUnless(isinstance(parser, InstrumentedConfigParser))
174
self.assertEqual(parser._calls, [('read', [config.branches_config_filename()])])
176
def test_get_global_config(self):
177
my_config = config.LocationConfig('http://example.com')
178
global_config = my_config._get_global_config()
179
self.failUnless(isinstance(global_config, config.GlobalConfig))
180
self.failUnless(global_config is my_config._get_global_config())
183
class TestBranchConfig(TestCaseInTempDir):
185
def test_constructs(self):
186
branch = FakeBranch()
187
my_config = config.BranchConfig(branch)
188
self.assertRaises(TypeError, config.BranchConfig)
190
def test_get_location_config(self):
191
branch = FakeBranch()
192
my_config = config.BranchConfig(branch)
193
location_config = my_config._get_location_config()
194
self.assertEqual(branch.base, location_config.location)
195
self.failUnless(location_config is my_config._get_location_config())
198
class TestConfigItems(TestCase):
201
super(TestConfigItems, self).setUp()
202
self.bzr_email = os.environ.get('BZREMAIL')
203
if self.bzr_email is not None:
204
del os.environ['BZREMAIL']
205
self.email = os.environ.get('EMAIL')
206
if self.email is not None:
207
del os.environ['EMAIL']
208
self.oldenv = os.environ.get('HOME', None)
209
os.environ['HOME'] = os.getcwd()
212
os.environ['HOME'] = self.oldenv
213
if os.environ.get('BZREMAIL') is not None:
214
del os.environ['BZREMAIL']
215
if self.bzr_email is not None:
216
os.environ['BZREMAIL'] = self.bzr_email
217
if self.email is not None:
218
os.environ['EMAIL'] = self.email
219
super(TestConfigItems, self).tearDown()
222
class TestGlobalConfigItems(TestConfigItems):
224
def test_user_id(self):
225
config_file = StringIO(sample_config_text)
226
my_config = config.GlobalConfig()
227
my_config._parser = my_config._get_config_parser(file=config_file)
228
self.assertEqual("Robert Collins <robertc@example.com>",
229
my_config._get_user_id())
231
def test_absent_user_id(self):
232
config_file = StringIO("")
233
my_config = config.GlobalConfig()
234
my_config._parser = my_config._get_config_parser(file=config_file)
235
self.assertEqual(None, my_config._get_user_id())
237
def test_configured_editor(self):
238
config_file = StringIO(sample_config_text)
239
my_config = config.GlobalConfig()
240
my_config._parser = my_config._get_config_parser(file=config_file)
241
self.assertEqual("vim", my_config.get_editor())
244
#class TestLocationConfigItems(TestConfigItems):
246
# def test_location_username(self):
249
#> signatures=check-if-available
250
#> signatures=require
254
class TestBranchConfigItems(TestConfigItems):
256
def test_user_id(self):
257
branch = FakeBranch()
258
my_config = config.BranchConfig(branch)
259
self.assertEqual("Robert Collins <robertc@example.net>",
260
my_config._get_user_id())
261
branch.email = "John"
262
self.assertEqual("John", my_config._get_user_id())
264
def test_not_set_in_branch(self):
265
branch = FakeBranch()
266
my_config = config.BranchConfig(branch)
268
config_file = StringIO(sample_config_text)
269
(my_config._get_location_config().
270
_get_global_config()._get_config_parser(config_file))
271
self.assertEqual("Robert Collins <robertc@example.com>",
272
my_config._get_user_id())
273
branch.email = "John"
274
self.assertEqual("John", my_config._get_user_id())
276
def test_BZREMAIL_OVERRIDES(self):
277
os.environ['BZREMAIL'] = "Robert Collins <robertc@example.org>"
278
branch = FakeBranch()
279
my_config = config.BranchConfig(branch)
280
self.assertEqual("Robert Collins <robertc@example.org>",
281
my_config.username())