~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_version_info.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-06-30 17:12:32 UTC
  • mfrom: (4398.3.2 register-serializer9)
  • Revision ID: pqm@pqm.ubuntu.com-20090630171232-h997nd3w5w89v60t
(Jelmer) Pass create_prefix paremeter to BzrDir.push_branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Tests for version_info"""
 
18
 
 
19
from cStringIO import StringIO
 
20
import imp
 
21
import os
 
22
import sys
 
23
 
 
24
from bzrlib import (
 
25
    errors,
 
26
    symbol_versioning,
 
27
    tests,
 
28
    version_info_formats,
 
29
    )
 
30
from bzrlib.tests import TestCaseWithTransport
 
31
from bzrlib.rio import read_stanzas
 
32
 
 
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
 
36
 
 
37
 
 
38
class TestVersionInfo(TestCaseWithTransport):
 
39
 
 
40
    def create_branch(self):
 
41
        wt = self.make_branch_and_tree('branch')
 
42
 
 
43
        self.build_tree(['branch/a'])
 
44
        wt.add('a')
 
45
        wt.commit('a', rev_id='r1')
 
46
 
 
47
        self.build_tree(['branch/b'])
 
48
        wt.add('b')
 
49
        wt.commit('b', rev_id='r2')
 
50
 
 
51
        self.build_tree_contents([('branch/a', 'new contents\n')])
 
52
        wt.commit(u'\xe52', rev_id='r3')
 
53
 
 
54
        return wt
 
55
 
 
56
    def test_rio_null(self):
 
57
        wt = self.make_branch_and_tree('branch')
 
58
 
 
59
        sio = StringIO()
 
60
        builder = RioVersionInfoBuilder(wt.branch, working_tree=wt)
 
61
        builder.generate(sio)
 
62
        val = sio.getvalue()
 
63
        self.assertContainsRe(val, 'build-date:')
 
64
        self.assertContainsRe(val, 'revno: 0')
 
65
 
 
66
    def test_rio_version_text(self):
 
67
        wt = self.create_branch()
 
68
 
 
69
        def regen(**kwargs):
 
70
            sio = StringIO()
 
71
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
 
72
                                            **kwargs)
 
73
            builder.generate(sio)
 
74
            val = sio.getvalue()
 
75
            return val
 
76
 
 
77
        val = regen()
 
78
        self.assertContainsRe(val, 'build-date:')
 
79
        self.assertContainsRe(val, 'date:')
 
80
        self.assertContainsRe(val, 'revno: 3')
 
81
        self.assertContainsRe(val, 'revision-id: r3')
 
82
 
 
83
        val = regen(check_for_clean=True)
 
84
        self.assertContainsRe(val, 'clean: True')
 
85
 
 
86
        self.build_tree(['branch/c'])
 
87
        val = regen(check_for_clean=True)
 
88
        self.assertContainsRe(val, 'clean: False')
 
89
        os.remove('branch/c')
 
90
 
 
91
        val = regen(include_revision_history=True)
 
92
        self.assertContainsRe(val, 'id: r1')
 
93
        self.assertContainsRe(val, 'message: a')
 
94
        self.assertContainsRe(val, 'id: r2')
 
95
        self.assertContainsRe(val, 'message: b')
 
96
        self.assertContainsRe(val, 'id: r3')
 
97
        self.assertContainsRe(val, 'message: \xc3\xa52') # utf8 encoding '\xe5'
 
98
 
 
99
    def test_rio_version_hook(self):
 
100
        def update_stanza(rev, stanza):
 
101
            stanza.add('bla', 'bloe')
 
102
        RioVersionInfoBuilder.hooks.install_named_hook(
 
103
            'revision', update_stanza, None)
 
104
        wt = self.create_branch()
 
105
 
 
106
        def regen(**kwargs):
 
107
            sio = StringIO()
 
108
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
 
109
                                            **kwargs)
 
110
            builder.generate(sio)
 
111
            sio.seek(0)
 
112
            stanzas = list(read_stanzas(sio))
 
113
            self.assertEqual(1, len(stanzas))
 
114
            return stanzas[0]
 
115
 
 
116
        stanza = regen()
 
117
        self.assertEqual(['bloe'], stanza.get_all('bla'))
 
118
 
 
119
    def test_rio_version(self):
 
120
        wt = self.create_branch()
 
121
 
 
122
        def regen(**kwargs):
 
123
            sio = StringIO()
 
124
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
 
125
                                            **kwargs)
 
126
            builder.generate(sio)
 
127
            sio.seek(0)
 
128
            stanzas = list(read_stanzas(sio))
 
129
            self.assertEqual(1, len(stanzas))
 
130
            return stanzas[0]
 
131
 
 
132
        def get_one_stanza(stanza, key):
 
133
            new_stanzas = list(read_stanzas(
 
134
                                StringIO(stanza[key].encode('utf8'))))
 
135
            self.assertEqual(1, len(new_stanzas))
 
136
            return new_stanzas[0]
 
137
 
 
138
        stanza = regen()
 
139
        self.failUnless('date' in stanza)
 
140
        self.failUnless('build-date' in stanza)
 
141
        self.assertEqual(['3'], stanza.get_all('revno'))
 
142
        self.assertEqual(['r3'], stanza.get_all('revision-id'))
 
143
 
 
144
        stanza = regen(check_for_clean=True)
 
145
        self.assertEqual(['True'], stanza.get_all('clean'))
 
146
 
 
147
        self.build_tree(['branch/c'])
 
148
        stanza = regen(check_for_clean=True, include_file_revisions=True)
 
149
        self.assertEqual(['False'], stanza.get_all('clean'))
 
150
 
 
151
        # XXX: This assumes it's being run against a repository that updates
 
152
        # the root revision on every commit.  Newer ones that use
 
153
        # RootCommitBuilder won't update it on each commit.
 
154
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
 
155
        self.assertEqual(['', 'a', 'b', 'c'], file_rev_stanza.get_all('path'))
 
156
        self.assertEqual(['r3', 'r3', 'r2', 'unversioned'],
 
157
            file_rev_stanza.get_all('revision'))
 
158
        os.remove('branch/c')
 
159
 
 
160
        stanza = regen(include_revision_history=True)
 
161
        revision_stanza = get_one_stanza(stanza, 'revisions')
 
162
        self.assertEqual(['r1', 'r2', 'r3'], revision_stanza.get_all('id'))
 
163
        self.assertEqual(['a', 'b', u'\xe52'], revision_stanza.get_all('message'))
 
164
        self.assertEqual(3, len(revision_stanza.get_all('date')))
 
165
 
 
166
        # a was modified, so it should show up modified again
 
167
        self.build_tree(['branch/a', 'branch/c'])
 
168
        wt.add('c')
 
169
        wt.rename_one('b', 'd')
 
170
        stanza = regen(check_for_clean=True, include_file_revisions=True)
 
171
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
 
172
        self.assertEqual(['', 'a', 'b', 'c', 'd'],
 
173
                          file_rev_stanza.get_all('path'))
 
174
        self.assertEqual(['r3', 'modified', 'renamed to d', 'new',
 
175
                          'renamed from b'],
 
176
                         file_rev_stanza.get_all('revision'))
 
177
 
 
178
        wt.commit('modified', rev_id='r4')
 
179
        wt.remove(['c', 'd'])
 
180
        os.remove('branch/d')
 
181
        stanza = regen(check_for_clean=True, include_file_revisions=True)
 
182
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
 
183
        self.assertEqual(['', 'a', 'c', 'd'], file_rev_stanza.get_all('path'))
 
184
        self.assertEqual(['r4', 'r4', 'unversioned', 'removed'],
 
185
                         file_rev_stanza.get_all('revision'))
 
186
 
 
187
    def test_python_null(self):
 
188
        wt = self.make_branch_and_tree('branch')
 
189
 
 
190
        sio = StringIO()
 
191
        builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt)
 
192
        builder.generate(sio)
 
193
        val = sio.getvalue()
 
194
        self.assertContainsRe(val, "'revision_id': None")
 
195
        self.assertContainsRe(val, "'revno': 0")
 
196
 
 
197
    def test_python_version(self):
 
198
        wt = self.create_branch()
 
199
 
 
200
        def regen(**kwargs):
 
201
            """Create a test module, import and return it"""
 
202
            outf = open('test_version_information.py', 'wb')
 
203
            try:
 
204
                builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt,
 
205
                                                   **kwargs)
 
206
                builder.generate(outf)
 
207
            finally:
 
208
                outf.close()
 
209
            module_info = imp.find_module('test_version_information',
 
210
                                          [os.getcwdu()])
 
211
            tvi = imp.load_module('tvi', *module_info)
 
212
            # Make sure the module isn't cached
 
213
            sys.modules.pop('tvi', None)
 
214
            sys.modules.pop('test_version_information', None)
 
215
            # Delete the compiled versions, because we are generating
 
216
            # a new file fast enough that python doesn't detect it
 
217
            # needs to recompile, and using sleep() just makes the
 
218
            # test slow
 
219
            if os.path.exists('test_version_information.pyc'):
 
220
                os.remove('test_version_information.pyc')
 
221
            if os.path.exists('test_version_information.pyo'):
 
222
                os.remove('test_version_information.pyo')
 
223
            return tvi
 
224
 
 
225
        tvi = regen()
 
226
        self.assertEqual(3, tvi.version_info['revno'])
 
227
        self.assertEqual('r3', tvi.version_info['revision_id'])
 
228
        self.failUnless(tvi.version_info.has_key('date'))
 
229
        self.assertEqual(None, tvi.version_info['clean'])
 
230
 
 
231
        tvi = regen(check_for_clean=True)
 
232
        self.assertEqual(True, tvi.version_info['clean'])
 
233
 
 
234
        self.build_tree(['branch/c'])
 
235
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
236
        self.assertEqual(False, tvi.version_info['clean'])
 
237
        self.assertEqual(['', 'a', 'b', 'c'],
 
238
                         sorted(tvi.file_revisions.keys()))
 
239
        self.assertEqual('r3', tvi.file_revisions['a'])
 
240
        self.assertEqual('r2', tvi.file_revisions['b'])
 
241
        self.assertEqual('unversioned', tvi.file_revisions['c'])
 
242
        os.remove('branch/c')
 
243
 
 
244
        tvi = regen(include_revision_history=True)
 
245
 
 
246
        rev_info = [(rev, message) for rev, message, timestamp, timezone
 
247
                                   in tvi.revisions]
 
248
        self.assertEqual([('r1', 'a'), ('r2', 'b'), ('r3', u'\xe52')], rev_info)
 
249
 
 
250
        # a was modified, so it should show up modified again
 
251
        self.build_tree(['branch/a', 'branch/c'])
 
252
        wt.add('c')
 
253
        wt.rename_one('b', 'd')
 
254
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
255
        self.assertEqual(['', 'a', 'b', 'c', 'd'],
 
256
                          sorted(tvi.file_revisions.keys()))
 
257
        self.assertEqual('modified', tvi.file_revisions['a'])
 
258
        self.assertEqual('renamed to d', tvi.file_revisions['b'])
 
259
        self.assertEqual('new', tvi.file_revisions['c'])
 
260
        self.assertEqual('renamed from b', tvi.file_revisions['d'])
 
261
 
 
262
        wt.commit('modified', rev_id='r4')
 
263
        wt.remove(['c', 'd'])
 
264
        os.remove('branch/d')
 
265
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
266
        self.assertEqual(['', 'a', 'c', 'd'],
 
267
                          sorted(tvi.file_revisions.keys()))
 
268
        self.assertEqual('r4', tvi.file_revisions['a'])
 
269
        self.assertEqual('unversioned', tvi.file_revisions['c'])
 
270
        self.assertEqual('removed', tvi.file_revisions['d'])
 
271
 
 
272
    def test_custom_null(self):
 
273
        sio = StringIO()
 
274
        wt = self.make_branch_and_tree('branch')
 
275
        builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
 
276
            template='revno: {revno}')
 
277
        builder.generate(sio)
 
278
        self.assertEquals("revno: 0", sio.getvalue())
 
279
 
 
280
        builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt, 
 
281
            template='{revno} revid: {revision_id}')
 
282
        # revision_id is not available yet
 
283
        self.assertRaises(errors.MissingTemplateVariable, 
 
284
            builder.generate, sio)
 
285
 
 
286
    def test_custom_version_text(self):
 
287
        wt = self.create_branch()
 
288
 
 
289
        def regen(tpl, **kwargs):
 
290
            sio = StringIO()
 
291
            builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
 
292
                                               template=tpl, **kwargs)
 
293
            builder.generate(sio)
 
294
            val = sio.getvalue()
 
295
            return val
 
296
 
 
297
        val = regen('build-date: "{build_date}"\ndate: "{date}"')
 
298
        self.assertContainsRe(val, 'build-date: "[0-9-+: ]+"')
 
299
        self.assertContainsRe(val, 'date: "[0-9-+: ]+"')
 
300
 
 
301
        val = regen('revno: {revno}')
 
302
        self.assertEqual(val, 'revno: 3')
 
303
 
 
304
        val = regen('revision-id: {revision_id}')
 
305
        self.assertEqual(val, 'revision-id: r3')
 
306
 
 
307
        val = regen('clean: {clean}', check_for_clean=True)
 
308
        self.assertEqual(val, 'clean: 1')
 
309
 
 
310
        self.build_tree(['branch/c'])
 
311
        val = regen('clean: {clean}', check_for_clean=True)
 
312
        self.assertEqual(val, 'clean: 0')
 
313
        os.remove('branch/c')
 
314
 
 
315
    def test_custom_without_template(self):
 
316
        builder = CustomVersionInfoBuilder(None)
 
317
        sio = StringIO()
 
318
        self.assertRaises(errors.NoTemplate, builder.generate, sio)
 
319
 
 
320
 
 
321
class TestBuilder(version_info_formats.VersionInfoBuilder):
 
322
    pass
 
323
 
 
324
 
 
325
class TestVersionInfoFormatRegistry(tests.TestCase):
 
326
 
 
327
    def setUp(self):
 
328
        super(TestVersionInfoFormatRegistry, self).setUp()
 
329
        registry = version_info_formats.format_registry
 
330
        self._default_key = registry._default_key
 
331
        self._dict = registry._dict.copy()
 
332
        self._help_dict = registry._help_dict.copy()
 
333
        self._info_dict = registry._info_dict.copy()
 
334
        self.addCleanup(self._cleanup)
 
335
 
 
336
    def _cleanup(self):
 
337
        # Restore the registry to pristine state after the test runs
 
338
        registry = version_info_formats.format_registry
 
339
        registry._default_key = self._default_key
 
340
        registry._dict = self._dict
 
341
        registry._help_dict = self._help_dict
 
342
        registry._info_dict = self._info_dict
 
343
 
 
344
    def test_register_remove(self):
 
345
        registry = version_info_formats.format_registry
 
346
        registry.register('testbuilder',
 
347
            TestBuilder, 'a simple test builder')
 
348
        self.assertIs(TestBuilder, registry.get('testbuilder'))
 
349
        self.assertEqual('a simple test builder',
 
350
                         registry.get_help('testbuilder'))
 
351
        registry.remove('testbuilder')
 
352
        self.assertRaises(KeyError, registry.get, 'testbuilder')