~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_version_info.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-13 05:47:58 UTC
  • Revision ID: mbp@sourcefrog.net-20050313054757-a86c3f5871069e22
fix up doctest for code rearrangement

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 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
 
    registry,
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 create_tree_with_dotted_revno(self):
57
 
        wt = self.make_branch_and_tree('branch')
58
 
        self.build_tree(['branch/a'])
59
 
        wt.add('a')
60
 
        wt.commit('a', rev_id='r1')
61
 
 
62
 
        other = wt.bzrdir.sprout('other').open_workingtree()
63
 
        self.build_tree(['other/b.a'])
64
 
        other.add(['b.a'])
65
 
        other.commit('b.a', rev_id='o2')
66
 
 
67
 
        os.chdir('branch')
68
 
        self.run_bzr('merge ../other')
69
 
        wt.commit('merge', rev_id='merge')
70
 
 
71
 
        wt.update(revision='o2')
72
 
 
73
 
        return wt
74
 
 
75
 
    def test_rio_null(self):
76
 
        wt = self.make_branch_and_tree('branch')
77
 
 
78
 
        sio = StringIO()
79
 
        builder = RioVersionInfoBuilder(wt.branch, working_tree=wt)
80
 
        builder.generate(sio)
81
 
        val = sio.getvalue()
82
 
        self.assertContainsRe(val, 'build-date:')
83
 
        self.assertContainsRe(val, 'revno: 0')
84
 
 
85
 
    def test_rio_dotted_revno(self):
86
 
        wt = self.create_tree_with_dotted_revno()
87
 
 
88
 
        sio = StringIO()
89
 
        builder = RioVersionInfoBuilder(wt.branch, working_tree=wt)
90
 
        builder.generate(sio)
91
 
        val = sio.getvalue()
92
 
        self.assertContainsRe(val, 'revno: 1.1.1')
93
 
 
94
 
    def test_rio_version_text(self):
95
 
        wt = self.create_branch()
96
 
 
97
 
        def regen(**kwargs):
98
 
            sio = StringIO()
99
 
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
100
 
                                            **kwargs)
101
 
            builder.generate(sio)
102
 
            val = sio.getvalue()
103
 
            return val
104
 
 
105
 
        val = regen()
106
 
        self.assertContainsRe(val, 'build-date:')
107
 
        self.assertContainsRe(val, 'date:')
108
 
        self.assertContainsRe(val, 'revno: 3')
109
 
        self.assertContainsRe(val, 'revision-id: r3')
110
 
 
111
 
        val = regen(check_for_clean=True)
112
 
        self.assertContainsRe(val, 'clean: True')
113
 
 
114
 
        self.build_tree(['branch/c'])
115
 
        val = regen(check_for_clean=True)
116
 
        self.assertContainsRe(val, 'clean: False')
117
 
        os.remove('branch/c')
118
 
 
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'
126
 
 
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()
133
 
 
134
 
        def regen(**kwargs):
135
 
            sio = StringIO()
136
 
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
137
 
                                            **kwargs)
138
 
            builder.generate(sio)
139
 
            sio.seek(0)
140
 
            stanzas = list(read_stanzas(sio))
141
 
            self.assertEqual(1, len(stanzas))
142
 
            return stanzas[0]
143
 
 
144
 
        stanza = regen()
145
 
        self.assertEqual(['bloe'], stanza.get_all('bla'))
146
 
 
147
 
    def test_rio_version(self):
148
 
        wt = self.create_branch()
149
 
 
150
 
        def regen(**kwargs):
151
 
            sio = StringIO()
152
 
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
153
 
                                            **kwargs)
154
 
            builder.generate(sio)
155
 
            sio.seek(0)
156
 
            stanzas = list(read_stanzas(sio))
157
 
            self.assertEqual(1, len(stanzas))
158
 
            return stanzas[0]
159
 
 
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]
165
 
 
166
 
        stanza = regen()
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'))
171
 
 
172
 
        stanza = regen(check_for_clean=True)
173
 
        self.assertEqual(['True'], stanza.get_all('clean'))
174
 
 
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'))
178
 
 
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')
186
 
 
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')))
192
 
 
193
 
        # a was modified, so it should show up modified again
194
 
        self.build_tree(['branch/a', 'branch/c'])
195
 
        wt.add('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',
202
 
                          'renamed from b'],
203
 
                         file_rev_stanza.get_all('revision'))
204
 
 
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'))
213
 
 
214
 
    def test_python_null(self):
215
 
        wt = self.make_branch_and_tree('branch')
216
 
 
217
 
        sio = StringIO()
218
 
        builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt)
219
 
        builder.generate(sio)
220
 
        val = sio.getvalue()
221
 
        self.assertContainsRe(val, "'revision_id': None")
222
 
        self.assertContainsRe(val, "'revno': '0'")
223
 
        self.assertNotContainsString(val, '\n\n\n\n')
224
 
 
225
 
    def test_python_dotted_revno(self):
226
 
        wt = self.create_tree_with_dotted_revno()
227
 
 
228
 
        sio = StringIO()
229
 
        builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt)
230
 
        builder.generate(sio)
231
 
        val = sio.getvalue()
232
 
        self.assertContainsRe(val, "'revno': '1.1.1'")
233
 
 
234
 
    def test_python_version(self):
235
 
        wt = self.create_branch()
236
 
 
237
 
        def regen(**kwargs):
238
 
            """Create a test module, import and return it"""
239
 
            outf = open('test_version_information.py', 'wb')
240
 
            try:
241
 
                builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt,
242
 
                                                   **kwargs)
243
 
                builder.generate(outf)
244
 
            finally:
245
 
                outf.close()
246
 
            module_info = imp.find_module('test_version_information',
247
 
                                          [os.getcwdu()])
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
255
 
            # test slow
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')
260
 
            return tvi
261
 
 
262
 
        tvi = regen()
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'])
267
 
 
268
 
        tvi = regen(check_for_clean=True)
269
 
        self.assertEqual(True, tvi.version_info['clean'])
270
 
 
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')
280
 
 
281
 
        tvi = regen(include_revision_history=True)
282
 
 
283
 
        rev_info = [(rev, message) for rev, message, timestamp, timezone
284
 
                                   in tvi.revisions]
285
 
        self.assertEqual([('r1', 'a'), ('r2', 'b'), ('r3', u'\xe52')], rev_info)
286
 
 
287
 
        # a was modified, so it should show up modified again
288
 
        self.build_tree(['branch/a', 'branch/c'])
289
 
        wt.add('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'])
298
 
 
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'])
308
 
 
309
 
    def test_custom_null(self):
310
 
        sio = StringIO()
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())
316
 
 
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)
322
 
 
323
 
    def test_custom_dotted_revno(self):
324
 
        sio = StringIO()
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())
330
 
 
331
 
    def test_custom_version_text(self):
332
 
        wt = self.create_branch()
333
 
 
334
 
        def regen(tpl, **kwargs):
335
 
            sio = StringIO()
336
 
            builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
337
 
                                               template=tpl, **kwargs)
338
 
            builder.generate(sio)
339
 
            val = sio.getvalue()
340
 
            return val
341
 
 
342
 
        val = regen('build-date: "{build_date}"\ndate: "{date}"')
343
 
        self.assertContainsRe(val, 'build-date: "[0-9-+: ]+"')
344
 
        self.assertContainsRe(val, 'date: "[0-9-+: ]+"')
345
 
 
346
 
        val = regen('revno: {revno}')
347
 
        self.assertEqual(val, 'revno: 3')
348
 
 
349
 
        val = regen('revision-id: {revision_id}')
350
 
        self.assertEqual(val, 'revision-id: r3')
351
 
 
352
 
        val = regen('clean: {clean}', check_for_clean=True)
353
 
        self.assertEqual(val, 'clean: 1')
354
 
 
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')
359
 
 
360
 
    def test_custom_without_template(self):
361
 
        builder = CustomVersionInfoBuilder(None)
362
 
        sio = StringIO()
363
 
        self.assertRaises(errors.NoTemplate, builder.generate, sio)
364
 
 
365
 
 
366
 
class TestBuilder(version_info_formats.VersionInfoBuilder):
367
 
    pass
368
 
 
369
 
 
370
 
class TestVersionInfoFormatRegistry(tests.TestCase):
371
 
 
372
 
    def setUp(self):
373
 
        super(TestVersionInfoFormatRegistry, self).setUp()
374
 
        self.overrideAttr(version_info_formats,
375
 
                          'format_registry', registry.Registry())
376
 
 
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')