42
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)
45
92
class TestConfigPath(TestCase):
59
106
self.assertEqual(config.config_filename(),
60
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')
63
114
class TestGetConfig(TestCase):
116
def test_constructs(self):
117
my_config = config.GlobalConfig()
65
119
def test_from_fp(self):
66
120
config_file = StringIO(sample_config_text)
67
self.failUnless(isinstance(config._get_config_parser(file=config_file),
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)
70
132
def test_calls_read_filenames(self):
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
133
# replace the class that is constructured, to check its parameters
74
134
oldparserclass = config.ConfigParser
75
135
config.ConfigParser = InstrumentedConfigParser
136
my_config = config.GlobalConfig()
77
parser = config._get_config_parser()
138
parser = my_config._get_config_parser()
79
140
config.ConfigParser = oldparserclass
80
141
self.failUnless(isinstance(parser, InstrumentedConfigParser))
81
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())
84
198
class TestConfigItems(TestCase):
97
211
def tearDown(self):
98
212
os.environ['HOME'] = self.oldenv
213
if os.environ.get('BZREMAIL') is not None:
214
del os.environ['BZREMAIL']
99
215
if self.bzr_email is not None:
100
216
os.environ['BZREMAIL'] = bzr_email
101
217
if self.email is not None:
102
218
os.environ['EMAIL'] = email
103
219
super(TestConfigItems, self).tearDown()
222
class TestGlobalConfigItems(TestConfigItems):
105
224
def test_user_id(self):
106
225
config_file = StringIO(sample_config_text)
107
parser = config._get_config_parser(file=config_file)
226
my_config = config.GlobalConfig()
227
my_config._parser = my_config._get_config_parser(file=config_file)
108
228
self.assertEqual("Robert Collins <robertc@example.com>",
109
config._get_user_id(parser = parser))
229
my_config._get_user_id())
111
231
def test_absent_user_id(self):
112
232
config_file = StringIO("")
113
parser = config._get_config_parser(file=config_file)
114
self.assertEqual(None,
115
config._get_user_id(parser = parser))
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))
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())