1
# Copyright (C) 2005, 2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for version_info"""
19
from cStringIO import StringIO
24
from bzrlib.tests import TestCaseWithTransport
25
from bzrlib.rio import read_stanzas
27
from bzrlib.version_info_formats.format_rio import RioVersionInfoBuilder
28
from bzrlib.version_info_formats.format_python import PythonVersionInfoBuilder
31
class TestVersionInfo(TestCaseWithTransport):
33
def create_branch(self):
34
wt = self.make_branch_and_tree('branch')
36
self.build_tree(['branch/a'])
38
wt.commit('a', rev_id='r1')
40
self.build_tree(['branch/b'])
42
wt.commit('b', rev_id='r2')
44
self.build_tree_contents([('branch/a', 'new contents\n')])
45
wt.commit(u'\xe52', rev_id='r3')
49
def test_rio_version_text(self):
50
wt = self.create_branch()
54
builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
61
self.assertContainsRe(val, 'build-date:')
62
self.assertContainsRe(val, 'date:')
63
self.assertContainsRe(val, 'revno: 3')
64
self.assertContainsRe(val, 'revision-id: r3')
66
val = regen(check_for_clean=True)
67
self.assertContainsRe(val, 'clean: True')
69
self.build_tree(['branch/c'])
70
val = regen(check_for_clean=True)
71
self.assertContainsRe(val, 'clean: False')
74
val = regen(include_revision_history=True)
75
self.assertContainsRe(val, 'id: r1')
76
self.assertContainsRe(val, 'message: a')
77
self.assertContainsRe(val, 'id: r2')
78
self.assertContainsRe(val, 'message: b')
79
self.assertContainsRe(val, 'id: r3')
80
self.assertContainsRe(val, 'message: \xc3\xa52') # utf8 encoding '\xe5'
82
def test_rio_version(self):
83
wt = self.create_branch()
87
builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
91
stanzas = list(read_stanzas(sio))
92
self.assertEqual(1, len(stanzas))
95
def get_one_stanza(stanza, key):
96
new_stanzas = list(read_stanzas(
97
StringIO(stanza[key].encode('utf8'))))
98
self.assertEqual(1, len(new_stanzas))
102
self.failUnless('date' in stanza)
103
self.failUnless('build-date' in stanza)
104
self.assertEqual(['3'], stanza.get_all('revno'))
105
self.assertEqual(['r3'], stanza.get_all('revision-id'))
107
stanza = regen(check_for_clean=True)
108
self.assertEqual(['True'], stanza.get_all('clean'))
110
self.build_tree(['branch/c'])
111
stanza = regen(check_for_clean=True, include_file_revisions=True)
112
self.assertEqual(['False'], stanza.get_all('clean'))
114
file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
115
self.assertEqual(['', 'a', 'b', 'c'], file_rev_stanza.get_all('path'))
116
self.assertEqual(['r3', 'r3', 'r2', 'unversioned'],
117
file_rev_stanza.get_all('revision'))
118
os.remove('branch/c')
120
stanza = regen(include_revision_history=True)
121
revision_stanza = get_one_stanza(stanza, 'revisions')
122
self.assertEqual(['r1', 'r2', 'r3'], revision_stanza.get_all('id'))
123
self.assertEqual(['a', 'b', u'\xe52'], revision_stanza.get_all('message'))
124
self.assertEqual(3, len(revision_stanza.get_all('date')))
126
# a was modified, so it should show up modified again
127
self.build_tree(['branch/a', 'branch/c'])
129
wt.rename_one('b', 'd')
130
stanza = regen(check_for_clean=True, include_file_revisions=True)
131
file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
132
self.assertEqual(['', 'a', 'b', 'c', 'd'],
133
file_rev_stanza.get_all('path'))
134
self.assertEqual(['r3', 'modified', 'renamed to d', 'new',
136
file_rev_stanza.get_all('revision'))
138
wt.commit('modified', rev_id='r4')
139
wt.remove(['c', 'd'])
140
os.remove('branch/d')
141
stanza = regen(check_for_clean=True, include_file_revisions=True)
142
file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
143
self.assertEqual(['', 'a', 'c', 'd'], file_rev_stanza.get_all('path'))
144
self.assertEqual(['r4', 'r4', 'unversioned', 'removed'],
145
file_rev_stanza.get_all('revision'))
147
def test_python_version(self):
148
wt = self.create_branch()
151
"""Create a test module, import and return it"""
152
outf = open('test_version_information.py', 'wb')
154
builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt,
156
builder.generate(outf)
159
module_info = imp.find_module('test_version_information',
161
tvi = imp.load_module('tvi', *module_info)
162
# Make sure the module isn't cached
163
sys.modules.pop('tvi', None)
164
sys.modules.pop('test_version_information', None)
165
# Delete the compiled versions, because we are generating
166
# a new file fast enough that python doesn't detect it
167
# needs to recompile, and using sleep() just makes the
169
if os.path.exists('test_version_information.pyc'):
170
os.remove('test_version_information.pyc')
171
if os.path.exists('test_version_information.pyo'):
172
os.remove('test_version_information.pyo')
176
self.assertEqual(3, tvi.version_info['revno'])
177
self.assertEqual('r3', tvi.version_info['revision_id'])
178
self.failUnless(tvi.version_info.has_key('date'))
179
self.assertEqual(None, tvi.version_info['clean'])
181
tvi = regen(check_for_clean=True)
182
self.assertEqual(True, tvi.version_info['clean'])
184
self.build_tree(['branch/c'])
185
tvi = regen(check_for_clean=True, include_file_revisions=True)
186
self.assertEqual(False, tvi.version_info['clean'])
187
self.assertEqual(['', 'a', 'b', 'c'],
188
sorted(tvi.file_revisions.keys()))
189
self.assertEqual('r3', tvi.file_revisions['a'])
190
self.assertEqual('r2', tvi.file_revisions['b'])
191
self.assertEqual('unversioned', tvi.file_revisions['c'])
192
os.remove('branch/c')
194
tvi = regen(include_revision_history=True)
196
rev_info = [(rev, message) for rev, message, timestamp, timezone
198
self.assertEqual([('r1', 'a'), ('r2', 'b'), ('r3', u'\xe52')], rev_info)
200
# a was modified, so it should show up modified again
201
self.build_tree(['branch/a', 'branch/c'])
203
wt.rename_one('b', 'd')
204
tvi = regen(check_for_clean=True, include_file_revisions=True)
205
self.assertEqual(['', 'a', 'b', 'c', 'd'],
206
sorted(tvi.file_revisions.keys()))
207
self.assertEqual('modified', tvi.file_revisions['a'])
208
self.assertEqual('renamed to d', tvi.file_revisions['b'])
209
self.assertEqual('new', tvi.file_revisions['c'])
210
self.assertEqual('renamed from b', tvi.file_revisions['d'])
212
wt.commit('modified', rev_id='r4')
213
wt.remove(['c', 'd'])
214
os.remove('branch/d')
215
tvi = regen(check_for_clean=True, include_file_revisions=True)
216
self.assertEqual(['', 'a', 'c', 'd'],
217
sorted(tvi.file_revisions.keys()))
218
self.assertEqual('r4', tvi.file_revisions['a'])
219
self.assertEqual('unversioned', tvi.file_revisions['c'])
220
self.assertEqual('removed', tvi.file_revisions['d'])