~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_version_info.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 08:15:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101008081514-dviqzrdfwyzsqbz2
Split NEWS into per-release doc/en/release-notes/bzr-*.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for version_info"""
18
18
 
21
21
import os
22
22
import sys
23
23
 
 
24
from bzrlib import (
 
25
    errors,
 
26
    registry,
 
27
    symbol_versioning,
 
28
    tests,
 
29
    version_info_formats,
 
30
    )
24
31
from bzrlib.tests import TestCaseWithTransport
25
32
from bzrlib.rio import read_stanzas
26
33
 
 
34
from bzrlib.version_info_formats.format_custom import CustomVersionInfoBuilder
27
35
from bzrlib.version_info_formats.format_rio import RioVersionInfoBuilder
28
36
from bzrlib.version_info_formats.format_python import PythonVersionInfoBuilder
29
37
 
46
54
 
47
55
        return wt
48
56
 
 
57
    def test_rio_null(self):
 
58
        wt = self.make_branch_and_tree('branch')
 
59
 
 
60
        sio = StringIO()
 
61
        builder = RioVersionInfoBuilder(wt.branch, working_tree=wt)
 
62
        builder.generate(sio)
 
63
        val = sio.getvalue()
 
64
        self.assertContainsRe(val, 'build-date:')
 
65
        self.assertContainsRe(val, 'revno: 0')
 
66
 
49
67
    def test_rio_version_text(self):
50
68
        wt = self.create_branch()
51
69
 
79
97
        self.assertContainsRe(val, 'id: r3')
80
98
        self.assertContainsRe(val, 'message: \xc3\xa52') # utf8 encoding '\xe5'
81
99
 
 
100
    def test_rio_version_hook(self):
 
101
        def update_stanza(rev, stanza):
 
102
            stanza.add('bla', 'bloe')
 
103
        RioVersionInfoBuilder.hooks.install_named_hook(
 
104
            'revision', update_stanza, None)
 
105
        wt = self.create_branch()
 
106
 
 
107
        def regen(**kwargs):
 
108
            sio = StringIO()
 
109
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
 
110
                                            **kwargs)
 
111
            builder.generate(sio)
 
112
            sio.seek(0)
 
113
            stanzas = list(read_stanzas(sio))
 
114
            self.assertEqual(1, len(stanzas))
 
115
            return stanzas[0]
 
116
 
 
117
        stanza = regen()
 
118
        self.assertEqual(['bloe'], stanza.get_all('bla'))
 
119
 
82
120
    def test_rio_version(self):
83
121
        wt = self.create_branch()
84
122
 
111
149
        stanza = regen(check_for_clean=True, include_file_revisions=True)
112
150
        self.assertEqual(['False'], stanza.get_all('clean'))
113
151
 
 
152
        # This assumes it's being run against a tree that does not update the
 
153
        # root revision on every commit.
114
154
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
115
155
        self.assertEqual(['', 'a', 'b', 'c'], file_rev_stanza.get_all('path'))
116
 
        self.assertEqual(['r3', 'r3', 'r2', 'unversioned'],
 
156
        self.assertEqual(['r1', 'r3', 'r2', 'unversioned'],
117
157
            file_rev_stanza.get_all('revision'))
118
158
        os.remove('branch/c')
119
159
 
129
169
        wt.rename_one('b', 'd')
130
170
        stanza = regen(check_for_clean=True, include_file_revisions=True)
131
171
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
132
 
        self.assertEqual(['', 'a', 'b', 'c', 'd'], 
 
172
        self.assertEqual(['', 'a', 'b', 'c', 'd'],
133
173
                          file_rev_stanza.get_all('path'))
134
 
        self.assertEqual(['r3', 'modified', 'renamed to d', 'new', 
 
174
        self.assertEqual(['r1', 'modified', 'renamed to d', 'new',
135
175
                          'renamed from b'],
136
176
                         file_rev_stanza.get_all('revision'))
137
177
 
141
181
        stanza = regen(check_for_clean=True, include_file_revisions=True)
142
182
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
143
183
        self.assertEqual(['', 'a', 'c', 'd'], file_rev_stanza.get_all('path'))
144
 
        self.assertEqual(['r4', 'r4', 'unversioned', 'removed'],
 
184
        self.assertEqual(['r1', 'r4', 'unversioned', 'removed'],
145
185
                         file_rev_stanza.get_all('revision'))
146
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
 
147
197
    def test_python_version(self):
148
198
        wt = self.create_branch()
149
199
 
184
234
        self.build_tree(['branch/c'])
185
235
        tvi = regen(check_for_clean=True, include_file_revisions=True)
186
236
        self.assertEqual(False, tvi.version_info['clean'])
187
 
        self.assertEqual(['', 'a', 'b', 'c'], 
 
237
        self.assertEqual(['', 'a', 'b', 'c'],
188
238
                         sorted(tvi.file_revisions.keys()))
189
239
        self.assertEqual('r3', tvi.file_revisions['a'])
190
240
        self.assertEqual('r2', tvi.file_revisions['b'])
202
252
        wt.add('c')
203
253
        wt.rename_one('b', 'd')
204
254
        tvi = regen(check_for_clean=True, include_file_revisions=True)
205
 
        self.assertEqual(['', 'a', 'b', 'c', 'd'], 
 
255
        self.assertEqual(['', 'a', 'b', 'c', 'd'],
206
256
                          sorted(tvi.file_revisions.keys()))
207
257
        self.assertEqual('modified', tvi.file_revisions['a'])
208
258
        self.assertEqual('renamed to d', tvi.file_revisions['b'])
213
263
        wt.remove(['c', 'd'])
214
264
        os.remove('branch/d')
215
265
        tvi = regen(check_for_clean=True, include_file_revisions=True)
216
 
        self.assertEqual(['', 'a', 'c', 'd'], 
 
266
        self.assertEqual(['', 'a', 'c', 'd'],
217
267
                          sorted(tvi.file_revisions.keys()))
218
268
        self.assertEqual('r4', tvi.file_revisions['a'])
219
269
        self.assertEqual('unversioned', tvi.file_revisions['c'])
220
270
        self.assertEqual('removed', tvi.file_revisions['d'])
221
271
 
222
 
 
 
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
        self.overrideAttr(version_info_formats,
 
330
                          'format_registry', registry.Registry())
 
331
 
 
332
    def test_register_remove(self):
 
333
        registry = version_info_formats.format_registry
 
334
        registry.register('testbuilder',
 
335
            TestBuilder, 'a simple test builder')
 
336
        self.assertIs(TestBuilder, registry.get('testbuilder'))
 
337
        self.assertEqual('a simple test builder',
 
338
                         registry.get_help('testbuilder'))
 
339
        registry.remove('testbuilder')
 
340
        self.assertRaises(KeyError, registry.get, 'testbuilder')