~bzr-pqm/bzr/bzr.dev

0.8.21 by John Arbash Meinel
Splitting up the version info code into a lazy factory style.
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
#
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
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.
0.8.21 by John Arbash Meinel
Splitting up the version info code into a lazy factory style.
7
#
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
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.
0.8.21 by John Arbash Meinel
Splitting up the version info code into a lazy factory style.
12
#
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
16
0.8.21 by John Arbash Meinel
Splitting up the version info code into a lazy factory style.
17
"""Tests for version_info"""
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
18
2022.1.1 by John Arbash Meinel
[merge] version-info plugin, and cleanup for layout in bzr
19
from cStringIO import StringIO
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
20
import imp
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
21
import os
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
22
import sys
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
23
3138.1.1 by John Arbash Meinel
Fix bug #175886: version_info deprecated functions should be tested to still work.
24
from bzrlib import (
3207.1.1 by Lukáš Lalinský
Raise a proper error when 'version-info --custom' is used without a template
25
    errors,
3138.1.1 by John Arbash Meinel
Fix bug #175886: version_info deprecated functions should be tested to still work.
26
    symbol_versioning,
27
    tests,
28
    version_info_formats,
29
    )
2022.1.3 by John Arbash Meinel
Remove unused imports
30
from bzrlib.tests import TestCaseWithTransport
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
31
from bzrlib.rio import read_stanzas
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
32
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
33
from bzrlib.version_info_formats.format_custom import CustomVersionInfoBuilder
2022.1.1 by John Arbash Meinel
[merge] version-info plugin, and cleanup for layout in bzr
34
from bzrlib.version_info_formats.format_rio import RioVersionInfoBuilder
35
from bzrlib.version_info_formats.format_python import PythonVersionInfoBuilder
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
36
37
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
38
class TestVersionInfo(TestCaseWithTransport):
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
39
40
    def create_branch(self):
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
41
        wt = self.make_branch_and_tree('branch')
0.8.23 by John Arbash Meinel
whitespace and formatting cleanups.
42
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
43
        self.build_tree(['branch/a'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
44
        wt.add('a')
45
        wt.commit('a', rev_id='r1')
46
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
47
        self.build_tree(['branch/b'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
48
        wt.add('b')
49
        wt.commit('b', rev_id='r2')
50
2022.1.4 by John Arbash Meinel
test feedback from Robert.
51
        self.build_tree_contents([('branch/a', 'new contents\n')])
2030.1.2 by John Arbash Meinel
Change the version-info --format=rio to support unicode messages
52
        wt.commit(u'\xe52', rev_id='r3')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
53
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
54
        return wt
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
55
4250.1.1 by Jelmer Vernooij
Fix version-info in empty branches.
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
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
66
    def test_rio_version_text(self):
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
67
        wt = self.create_branch()
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
68
69
        def regen(**kwargs):
70
            sio = StringIO()
0.8.23 by John Arbash Meinel
whitespace and formatting cleanups.
71
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
72
                                            **kwargs)
73
            builder.generate(sio)
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
74
            val = sio.getvalue()
75
            return val
76
77
        val = regen()
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
78
        self.assertContainsRe(val, 'build-date:')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
79
        self.assertContainsRe(val, 'date:')
80
        self.assertContainsRe(val, 'revno: 3')
0.8.16 by John Arbash Meinel
Using revision-id for rio, and revision_id for python
81
        self.assertContainsRe(val, 'revision-id: r3')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
82
83
        val = regen(check_for_clean=True)
84
        self.assertContainsRe(val, 'clean: True')
85
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
86
        self.build_tree(['branch/c'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
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)
0.8.6 by John Arbash Meinel
Updated the blackbox tests.
92
        self.assertContainsRe(val, 'id: r1')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
93
        self.assertContainsRe(val, 'message: a')
0.8.6 by John Arbash Meinel
Updated the blackbox tests.
94
        self.assertContainsRe(val, 'id: r2')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
95
        self.assertContainsRe(val, 'message: b')
0.8.6 by John Arbash Meinel
Updated the blackbox tests.
96
        self.assertContainsRe(val, 'id: r3')
2030.1.2 by John Arbash Meinel
Change the version-info --format=rio to support unicode messages
97
        self.assertContainsRe(val, 'message: \xc3\xa52') # utf8 encoding '\xe5'
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
98
4216.4.1 by Jelmer Vernooij
Allow hooks to add new entries to version-info.
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
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
119
    def test_rio_version(self):
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
120
        wt = self.create_branch()
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
121
122
        def regen(**kwargs):
123
            sio = StringIO()
0.8.23 by John Arbash Meinel
whitespace and formatting cleanups.
124
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
125
                                            **kwargs)
126
            builder.generate(sio)
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
127
            sio.seek(0)
128
            stanzas = list(read_stanzas(sio))
129
            self.assertEqual(1, len(stanzas))
130
            return stanzas[0]
131
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
132
        def get_one_stanza(stanza, key):
2022.1.1 by John Arbash Meinel
[merge] version-info plugin, and cleanup for layout in bzr
133
            new_stanzas = list(read_stanzas(
134
                                StringIO(stanza[key].encode('utf8'))))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
135
            self.assertEqual(1, len(new_stanzas))
136
            return new_stanzas[0]
137
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
138
        stanza = regen()
139
        self.failUnless('date' in stanza)
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
140
        self.failUnless('build-date' in stanza)
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
141
        self.assertEqual(['3'], stanza.get_all('revno'))
0.8.16 by John Arbash Meinel
Using revision-id for rio, and revision_id for python
142
        self.assertEqual(['r3'], stanza.get_all('revision-id'))
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
143
144
        stanza = regen(check_for_clean=True)
145
        self.assertEqual(['True'], stanza.get_all('clean'))
146
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
147
        self.build_tree(['branch/c'])
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
148
        stanza = regen(check_for_clean=True, include_file_revisions=True)
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
149
        self.assertEqual(['False'], stanza.get_all('clean'))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
150
4599.4.2 by Robert Collins
Update test_version_info for rich root test data.
151
        # This assumes it's being run against a tree that does not update the
152
        # root revision on every commit.
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
153
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
1731.1.50 by Aaron Bentley
Merge bzr.dev
154
        self.assertEqual(['', 'a', 'b', 'c'], file_rev_stanza.get_all('path'))
4599.4.2 by Robert Collins
Update test_version_info for rich root test data.
155
        self.assertEqual(['r1', 'r3', 'r2', 'unversioned'],
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
156
            file_rev_stanza.get_all('revision'))
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
157
        os.remove('branch/c')
158
159
        stanza = regen(include_revision_history=True)
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
160
        revision_stanza = get_one_stanza(stanza, 'revisions')
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
161
        self.assertEqual(['r1', 'r2', 'r3'], revision_stanza.get_all('id'))
2030.1.2 by John Arbash Meinel
Change the version-info --format=rio to support unicode messages
162
        self.assertEqual(['a', 'b', u'\xe52'], revision_stanza.get_all('message'))
0.8.15 by John Arbash Meinel
Including the date stamp for all revisions.
163
        self.assertEqual(3, len(revision_stanza.get_all('date')))
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
164
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
165
        # a was modified, so it should show up modified again
166
        self.build_tree(['branch/a', 'branch/c'])
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
167
        wt.add('c')
168
        wt.rename_one('b', 'd')
169
        stanza = regen(check_for_clean=True, include_file_revisions=True)
170
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
171
        self.assertEqual(['', 'a', 'b', 'c', 'd'],
1731.1.50 by Aaron Bentley
Merge bzr.dev
172
                          file_rev_stanza.get_all('path'))
4599.4.2 by Robert Collins
Update test_version_info for rich root test data.
173
        self.assertEqual(['r1', 'modified', 'renamed to d', 'new',
1731.1.50 by Aaron Bentley
Merge bzr.dev
174
                          'renamed from b'],
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
175
                         file_rev_stanza.get_all('revision'))
176
177
        wt.commit('modified', rev_id='r4')
178
        wt.remove(['c', 'd'])
179
        os.remove('branch/d')
180
        stanza = regen(check_for_clean=True, include_file_revisions=True)
181
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
1731.1.50 by Aaron Bentley
Merge bzr.dev
182
        self.assertEqual(['', 'a', 'c', 'd'], file_rev_stanza.get_all('path'))
4599.4.2 by Robert Collins
Update test_version_info for rich root test data.
183
        self.assertEqual(['r1', 'r4', 'unversioned', 'removed'],
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
184
                         file_rev_stanza.get_all('revision'))
185
4250.1.1 by Jelmer Vernooij
Fix version-info in empty branches.
186
    def test_python_null(self):
187
        wt = self.make_branch_and_tree('branch')
188
189
        sio = StringIO()
190
        builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt)
191
        builder.generate(sio)
192
        val = sio.getvalue()
193
        self.assertContainsRe(val, "'revision_id': None")
194
        self.assertContainsRe(val, "'revno': 0")
195
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
196
    def test_python_version(self):
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
197
        wt = self.create_branch()
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
198
199
        def regen(**kwargs):
2022.1.4 by John Arbash Meinel
test feedback from Robert.
200
            """Create a test module, import and return it"""
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
201
            outf = open('test_version_information.py', 'wb')
2022.1.4 by John Arbash Meinel
test feedback from Robert.
202
            try:
203
                builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt,
204
                                                   **kwargs)
205
                builder.generate(outf)
206
            finally:
207
                outf.close()
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
208
            module_info = imp.find_module('test_version_information',
209
                                          [os.getcwdu()])
210
            tvi = imp.load_module('tvi', *module_info)
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
211
            # Make sure the module isn't cached
212
            sys.modules.pop('tvi', None)
213
            sys.modules.pop('test_version_information', None)
214
            # Delete the compiled versions, because we are generating
215
            # a new file fast enough that python doesn't detect it
216
            # needs to recompile, and using sleep() just makes the
217
            # test slow
218
            if os.path.exists('test_version_information.pyc'):
219
                os.remove('test_version_information.pyc')
220
            if os.path.exists('test_version_information.pyo'):
221
                os.remove('test_version_information.pyo')
222
            return tvi
223
224
        tvi = regen()
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
225
        self.assertEqual(3, tvi.version_info['revno'])
226
        self.assertEqual('r3', tvi.version_info['revision_id'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
227
        self.failUnless(tvi.version_info.has_key('date'))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
228
        self.assertEqual(None, tvi.version_info['clean'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
229
230
        tvi = regen(check_for_clean=True)
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
231
        self.assertEqual(True, tvi.version_info['clean'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
232
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
233
        self.build_tree(['branch/c'])
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
234
        tvi = regen(check_for_clean=True, include_file_revisions=True)
235
        self.assertEqual(False, tvi.version_info['clean'])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
236
        self.assertEqual(['', 'a', 'b', 'c'],
1731.1.50 by Aaron Bentley
Merge bzr.dev
237
                         sorted(tvi.file_revisions.keys()))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
238
        self.assertEqual('r3', tvi.file_revisions['a'])
239
        self.assertEqual('r2', tvi.file_revisions['b'])
240
        self.assertEqual('unversioned', tvi.file_revisions['c'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
241
        os.remove('branch/c')
242
243
        tvi = regen(include_revision_history=True)
0.8.15 by John Arbash Meinel
Including the date stamp for all revisions.
244
0.8.23 by John Arbash Meinel
whitespace and formatting cleanups.
245
        rev_info = [(rev, message) for rev, message, timestamp, timezone
246
                                   in tvi.revisions]
2030.1.2 by John Arbash Meinel
Change the version-info --format=rio to support unicode messages
247
        self.assertEqual([('r1', 'a'), ('r2', 'b'), ('r3', u'\xe52')], rev_info)
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
248
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
249
        # a was modified, so it should show up modified again
250
        self.build_tree(['branch/a', 'branch/c'])
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
251
        wt.add('c')
252
        wt.rename_one('b', 'd')
253
        tvi = regen(check_for_clean=True, include_file_revisions=True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
254
        self.assertEqual(['', 'a', 'b', 'c', 'd'],
1731.1.50 by Aaron Bentley
Merge bzr.dev
255
                          sorted(tvi.file_revisions.keys()))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
256
        self.assertEqual('modified', tvi.file_revisions['a'])
257
        self.assertEqual('renamed to d', tvi.file_revisions['b'])
258
        self.assertEqual('new', tvi.file_revisions['c'])
259
        self.assertEqual('renamed from b', tvi.file_revisions['d'])
260
261
        wt.commit('modified', rev_id='r4')
262
        wt.remove(['c', 'd'])
263
        os.remove('branch/d')
264
        tvi = regen(check_for_clean=True, include_file_revisions=True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
265
        self.assertEqual(['', 'a', 'c', 'd'],
1731.1.50 by Aaron Bentley
Merge bzr.dev
266
                          sorted(tvi.file_revisions.keys()))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
267
        self.assertEqual('r4', tvi.file_revisions['a'])
268
        self.assertEqual('unversioned', tvi.file_revisions['c'])
269
        self.assertEqual('removed', tvi.file_revisions['d'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
270
4250.1.1 by Jelmer Vernooij
Fix version-info in empty branches.
271
    def test_custom_null(self):
272
        sio = StringIO()
273
        wt = self.make_branch_and_tree('branch')
274
        builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
275
            template='revno: {revno}')
276
        builder.generate(sio)
277
        self.assertEquals("revno: 0", sio.getvalue())
278
279
        builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt, 
280
            template='{revno} revid: {revision_id}')
281
        # revision_id is not available yet
282
        self.assertRaises(errors.MissingTemplateVariable, 
283
            builder.generate, sio)
284
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
285
    def test_custom_version_text(self):
286
        wt = self.create_branch()
287
288
        def regen(tpl, **kwargs):
289
            sio = StringIO()
290
            builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
291
                                               template=tpl, **kwargs)
292
            builder.generate(sio)
293
            val = sio.getvalue()
294
            return val
295
296
        val = regen('build-date: "{build_date}"\ndate: "{date}"')
297
        self.assertContainsRe(val, 'build-date: "[0-9-+: ]+"')
298
        self.assertContainsRe(val, 'date: "[0-9-+: ]+"')
299
300
        val = regen('revno: {revno}')
301
        self.assertEqual(val, 'revno: 3')
302
303
        val = regen('revision-id: {revision_id}')
304
        self.assertEqual(val, 'revision-id: r3')
305
306
        val = regen('clean: {clean}', check_for_clean=True)
307
        self.assertEqual(val, 'clean: 1')
308
309
        self.build_tree(['branch/c'])
310
        val = regen('clean: {clean}', check_for_clean=True)
311
        self.assertEqual(val, 'clean: 0')
312
        os.remove('branch/c')
3138.1.1 by John Arbash Meinel
Fix bug #175886: version_info deprecated functions should be tested to still work.
313
3207.1.1 by Lukáš Lalinský
Raise a proper error when 'version-info --custom' is used without a template
314
    def test_custom_without_template(self):
315
        builder = CustomVersionInfoBuilder(None)
316
        sio = StringIO()
317
        self.assertRaises(errors.NoTemplate, builder.generate, sio)
318
3138.1.1 by John Arbash Meinel
Fix bug #175886: version_info deprecated functions should be tested to still work.
319
320
class TestBuilder(version_info_formats.VersionInfoBuilder):
321
    pass
322
323
324
class TestVersionInfoFormatRegistry(tests.TestCase):
325
326
    def setUp(self):
327
        super(TestVersionInfoFormatRegistry, self).setUp()
328
        registry = version_info_formats.format_registry
329
        self._default_key = registry._default_key
330
        self._dict = registry._dict.copy()
331
        self._help_dict = registry._help_dict.copy()
332
        self._info_dict = registry._info_dict.copy()
333
        self.addCleanup(self._cleanup)
334
335
    def _cleanup(self):
336
        # Restore the registry to pristine state after the test runs
337
        registry = version_info_formats.format_registry
338
        registry._default_key = self._default_key
339
        registry._dict = self._dict
340
        registry._help_dict = self._help_dict
341
        registry._info_dict = self._info_dict
342
343
    def test_register_remove(self):
344
        registry = version_info_formats.format_registry
345
        registry.register('testbuilder',
346
            TestBuilder, 'a simple test builder')
347
        self.assertIs(TestBuilder, registry.get('testbuilder'))
348
        self.assertEqual('a simple test builder',
349
                         registry.get_help('testbuilder'))
350
        registry.remove('testbuilder')
351
        self.assertRaises(KeyError, registry.get, 'testbuilder')