1
# Copyright (C) 2005-2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for version_info"""
19
from cStringIO import StringIO
30
from bzrlib.tests import TestCaseWithTransport
31
from bzrlib.rio import read_stanzas
33
from bzrlib.version_info_formats.format_custom import CustomVersionInfoBuilder
34
from bzrlib.version_info_formats.format_rio import RioVersionInfoBuilder
35
from bzrlib.version_info_formats.format_python import PythonVersionInfoBuilder
38
class TestVersionInfo(TestCaseWithTransport):
40
def create_branch(self):
41
wt = self.make_branch_and_tree('branch')
43
self.build_tree(['branch/a'])
45
wt.commit('a', rev_id='r1')
47
self.build_tree(['branch/b'])
49
wt.commit('b', rev_id='r2')
51
self.build_tree_contents([('branch/a', 'new contents\n')])
52
wt.commit(u'\xe52', rev_id='r3')
56
def create_tree_with_dotted_revno(self):
57
wt = self.make_branch_and_tree('branch')
58
self.build_tree(['branch/a'])
60
wt.commit('a', rev_id='r1')
62
other = wt.bzrdir.sprout('other').open_workingtree()
63
self.build_tree(['other/b.a'])
65
other.commit('b.a', rev_id='o2')
68
self.run_bzr('merge ../other')
69
wt.commit('merge', rev_id='merge')
71
wt.update(revision='o2')
75
def test_rio_null(self):
76
wt = self.make_branch_and_tree('branch')
79
builder = RioVersionInfoBuilder(wt.branch, working_tree=wt)
82
self.assertContainsRe(val, 'build-date:')
83
self.assertContainsRe(val, 'revno: 0')
85
def test_rio_dotted_revno(self):
86
wt = self.create_tree_with_dotted_revno()
89
builder = RioVersionInfoBuilder(wt.branch, working_tree=wt)
92
self.assertContainsRe(val, 'revno: 1.1.1')
94
def test_rio_version_text(self):
95
wt = self.create_branch()
99
builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
101
builder.generate(sio)
106
self.assertContainsRe(val, 'build-date:')
107
self.assertContainsRe(val, 'date:')
108
self.assertContainsRe(val, 'revno: 3')
109
self.assertContainsRe(val, 'revision-id: r3')
111
val = regen(check_for_clean=True)
112
self.assertContainsRe(val, 'clean: True')
114
self.build_tree(['branch/c'])
115
val = regen(check_for_clean=True)
116
self.assertContainsRe(val, 'clean: False')
117
os.remove('branch/c')
119
val = regen(include_revision_history=True)
120
self.assertContainsRe(val, 'id: r1')
121
self.assertContainsRe(val, 'message: a')
122
self.assertContainsRe(val, 'id: r2')
123
self.assertContainsRe(val, 'message: b')
124
self.assertContainsRe(val, 'id: r3')
125
self.assertContainsRe(val, 'message: \xc3\xa52') # utf8 encoding '\xe5'
127
def test_rio_version_hook(self):
128
def update_stanza(rev, stanza):
129
stanza.add('bla', 'bloe')
130
RioVersionInfoBuilder.hooks.install_named_hook(
131
'revision', update_stanza, None)
132
wt = self.create_branch()
136
builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
138
builder.generate(sio)
140
stanzas = list(read_stanzas(sio))
141
self.assertEqual(1, len(stanzas))
145
self.assertEqual(['bloe'], stanza.get_all('bla'))
147
def test_rio_version(self):
148
wt = self.create_branch()
152
builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
154
builder.generate(sio)
156
stanzas = list(read_stanzas(sio))
157
self.assertEqual(1, len(stanzas))
160
def get_one_stanza(stanza, key):
161
new_stanzas = list(read_stanzas(
162
StringIO(stanza[key].encode('utf8'))))
163
self.assertEqual(1, len(new_stanzas))
164
return new_stanzas[0]
167
self.assertTrue('date' in stanza)
168
self.assertTrue('build-date' in stanza)
169
self.assertEqual(['3'], stanza.get_all('revno'))
170
self.assertEqual(['r3'], stanza.get_all('revision-id'))
172
stanza = regen(check_for_clean=True)
173
self.assertEqual(['True'], stanza.get_all('clean'))
175
self.build_tree(['branch/c'])
176
stanza = regen(check_for_clean=True, include_file_revisions=True)
177
self.assertEqual(['False'], stanza.get_all('clean'))
179
# This assumes it's being run against a tree that does not update the
180
# root revision on every commit.
181
file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
182
self.assertEqual(['', 'a', 'b', 'c'], file_rev_stanza.get_all('path'))
183
self.assertEqual(['r1', 'r3', 'r2', 'unversioned'],
184
file_rev_stanza.get_all('revision'))
185
os.remove('branch/c')
187
stanza = regen(include_revision_history=True)
188
revision_stanza = get_one_stanza(stanza, 'revisions')
189
self.assertEqual(['r1', 'r2', 'r3'], revision_stanza.get_all('id'))
190
self.assertEqual(['a', 'b', u'\xe52'], revision_stanza.get_all('message'))
191
self.assertEqual(3, len(revision_stanza.get_all('date')))
193
# a was modified, so it should show up modified again
194
self.build_tree(['branch/a', 'branch/c'])
196
wt.rename_one('b', 'd')
197
stanza = regen(check_for_clean=True, include_file_revisions=True)
198
file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
199
self.assertEqual(['', 'a', 'b', 'c', 'd'],
200
file_rev_stanza.get_all('path'))
201
self.assertEqual(['r1', 'modified', 'renamed to d', 'new',
203
file_rev_stanza.get_all('revision'))
205
wt.commit('modified', rev_id='r4')
206
wt.remove(['c', 'd'])
207
os.remove('branch/d')
208
stanza = regen(check_for_clean=True, include_file_revisions=True)
209
file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
210
self.assertEqual(['', 'a', 'c', 'd'], file_rev_stanza.get_all('path'))
211
self.assertEqual(['r1', 'r4', 'unversioned', 'removed'],
212
file_rev_stanza.get_all('revision'))
214
def test_python_null(self):
215
wt = self.make_branch_and_tree('branch')
218
builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt)
219
builder.generate(sio)
221
self.assertContainsRe(val, "'revision_id': None")
222
self.assertContainsRe(val, "'revno': '0'")
223
self.assertNotContainsString(val, '\n\n\n\n')
225
def test_python_dotted_revno(self):
226
wt = self.create_tree_with_dotted_revno()
229
builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt)
230
builder.generate(sio)
232
self.assertContainsRe(val, "'revno': '1.1.1'")
234
def test_python_version(self):
235
wt = self.create_branch()
238
"""Create a test module, import and return it"""
239
outf = open('test_version_information.py', 'wb')
241
builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt,
243
builder.generate(outf)
246
module_info = imp.find_module('test_version_information',
248
tvi = imp.load_module('tvi', *module_info)
249
# Make sure the module isn't cached
250
sys.modules.pop('tvi', None)
251
sys.modules.pop('test_version_information', None)
252
# Delete the compiled versions, because we are generating
253
# a new file fast enough that python doesn't detect it
254
# needs to recompile, and using sleep() just makes the
256
if os.path.exists('test_version_information.pyc'):
257
os.remove('test_version_information.pyc')
258
if os.path.exists('test_version_information.pyo'):
259
os.remove('test_version_information.pyo')
263
self.assertEqual('3', tvi.version_info['revno'])
264
self.assertEqual('r3', tvi.version_info['revision_id'])
265
self.assertTrue(tvi.version_info.has_key('date'))
266
self.assertEqual(None, tvi.version_info['clean'])
268
tvi = regen(check_for_clean=True)
269
self.assertEqual(True, tvi.version_info['clean'])
271
self.build_tree(['branch/c'])
272
tvi = regen(check_for_clean=True, include_file_revisions=True)
273
self.assertEqual(False, tvi.version_info['clean'])
274
self.assertEqual(['', 'a', 'b', 'c'],
275
sorted(tvi.file_revisions.keys()))
276
self.assertEqual('r3', tvi.file_revisions['a'])
277
self.assertEqual('r2', tvi.file_revisions['b'])
278
self.assertEqual('unversioned', tvi.file_revisions['c'])
279
os.remove('branch/c')
281
tvi = regen(include_revision_history=True)
283
rev_info = [(rev, message) for rev, message, timestamp, timezone
285
self.assertEqual([('r1', 'a'), ('r2', 'b'), ('r3', u'\xe52')], rev_info)
287
# a was modified, so it should show up modified again
288
self.build_tree(['branch/a', 'branch/c'])
290
wt.rename_one('b', 'd')
291
tvi = regen(check_for_clean=True, include_file_revisions=True)
292
self.assertEqual(['', 'a', 'b', 'c', 'd'],
293
sorted(tvi.file_revisions.keys()))
294
self.assertEqual('modified', tvi.file_revisions['a'])
295
self.assertEqual('renamed to d', tvi.file_revisions['b'])
296
self.assertEqual('new', tvi.file_revisions['c'])
297
self.assertEqual('renamed from b', tvi.file_revisions['d'])
299
wt.commit('modified', rev_id='r4')
300
wt.remove(['c', 'd'])
301
os.remove('branch/d')
302
tvi = regen(check_for_clean=True, include_file_revisions=True)
303
self.assertEqual(['', 'a', 'c', 'd'],
304
sorted(tvi.file_revisions.keys()))
305
self.assertEqual('r4', tvi.file_revisions['a'])
306
self.assertEqual('unversioned', tvi.file_revisions['c'])
307
self.assertEqual('removed', tvi.file_revisions['d'])
309
def test_custom_null(self):
311
wt = self.make_branch_and_tree('branch')
312
builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
313
template='revno: {revno}')
314
builder.generate(sio)
315
self.assertEquals("revno: 0", sio.getvalue())
317
builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
318
template='{revno} revid: {revision_id}')
319
# revision_id is not available yet
320
self.assertRaises(errors.MissingTemplateVariable,
321
builder.generate, sio)
323
def test_custom_dotted_revno(self):
325
wt = self.create_tree_with_dotted_revno()
326
builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
327
template='{revno} revid: {revision_id}')
328
builder.generate(sio)
329
self.assertEquals("1.1.1 revid: o2", sio.getvalue())
331
def test_custom_version_text(self):
332
wt = self.create_branch()
334
def regen(tpl, **kwargs):
336
builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
337
template=tpl, **kwargs)
338
builder.generate(sio)
342
val = regen('build-date: "{build_date}"\ndate: "{date}"')
343
self.assertContainsRe(val, 'build-date: "[0-9-+: ]+"')
344
self.assertContainsRe(val, 'date: "[0-9-+: ]+"')
346
val = regen('revno: {revno}')
347
self.assertEqual(val, 'revno: 3')
349
val = regen('revision-id: {revision_id}')
350
self.assertEqual(val, 'revision-id: r3')
352
val = regen('clean: {clean}', check_for_clean=True)
353
self.assertEqual(val, 'clean: 1')
355
self.build_tree(['branch/c'])
356
val = regen('clean: {clean}', check_for_clean=True)
357
self.assertEqual(val, 'clean: 0')
358
os.remove('branch/c')
360
def test_custom_without_template(self):
361
builder = CustomVersionInfoBuilder(None)
363
self.assertRaises(errors.NoTemplate, builder.generate, sio)
366
class TestBuilder(version_info_formats.VersionInfoBuilder):
370
class TestVersionInfoFormatRegistry(tests.TestCase):
373
super(TestVersionInfoFormatRegistry, self).setUp()
374
self.overrideAttr(version_info_formats,
375
'format_registry', registry.Registry())
377
def test_register_remove(self):
378
registry = version_info_formats.format_registry
379
registry.register('testbuilder',
380
TestBuilder, 'a simple test builder')
381
self.assertIs(TestBuilder, registry.get('testbuilder'))
382
self.assertEqual('a simple test builder',
383
registry.get_help('testbuilder'))
384
registry.remove('testbuilder')
385
self.assertRaises(KeyError, registry.get, 'testbuilder')