/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 (
25
    symbol_versioning,
26
    tests,
27
    version_info_formats,
28
    )
2022.1.3 by John Arbash Meinel
Remove unused imports
29
from bzrlib.tests import TestCaseWithTransport
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
30
from bzrlib.rio import read_stanzas
0.8.1 by John Arbash Meinel
Creating a plugin to ease generating version information from a tree.
31
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
32
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
33
from bzrlib.version_info_formats.format_rio import RioVersionInfoBuilder
34
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.
35
36
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
37
class TestVersionInfo(TestCaseWithTransport):
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
38
39
    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.
40
        wt = self.make_branch_and_tree('branch')
0.8.23 by John Arbash Meinel
whitespace and formatting cleanups.
41
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
42
        self.build_tree(['branch/a'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
43
        wt.add('a')
44
        wt.commit('a', rev_id='r1')
45
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
46
        self.build_tree(['branch/b'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
47
        wt.add('b')
48
        wt.commit('b', rev_id='r2')
49
2022.1.4 by John Arbash Meinel
test feedback from Robert.
50
        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
51
        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
52
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
53
        return wt
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
54
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
55
    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.
56
        wt = self.create_branch()
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
57
58
        def regen(**kwargs):
59
            sio = StringIO()
0.8.23 by John Arbash Meinel
whitespace and formatting cleanups.
60
            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.
61
                                            **kwargs)
62
            builder.generate(sio)
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
63
            val = sio.getvalue()
64
            return val
65
66
        val = regen()
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
67
        self.assertContainsRe(val, 'build-date:')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
68
        self.assertContainsRe(val, 'date:')
69
        self.assertContainsRe(val, 'revno: 3')
0.8.16 by John Arbash Meinel
Using revision-id for rio, and revision_id for python
70
        self.assertContainsRe(val, 'revision-id: r3')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
71
72
        val = regen(check_for_clean=True)
73
        self.assertContainsRe(val, 'clean: True')
74
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
75
        self.build_tree(['branch/c'])
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
76
        val = regen(check_for_clean=True)
77
        self.assertContainsRe(val, 'clean: False')
78
        os.remove('branch/c')
79
80
        val = regen(include_revision_history=True)
0.8.6 by John Arbash Meinel
Updated the blackbox tests.
81
        self.assertContainsRe(val, 'id: r1')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
82
        self.assertContainsRe(val, 'message: a')
0.8.6 by John Arbash Meinel
Updated the blackbox tests.
83
        self.assertContainsRe(val, 'id: r2')
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
84
        self.assertContainsRe(val, 'message: b')
0.8.6 by John Arbash Meinel
Updated the blackbox tests.
85
        self.assertContainsRe(val, 'id: r3')
2030.1.2 by John Arbash Meinel
Change the version-info --format=rio to support unicode messages
86
        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
87
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
88
    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.
89
        wt = self.create_branch()
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
90
91
        def regen(**kwargs):
92
            sio = StringIO()
0.8.23 by John Arbash Meinel
whitespace and formatting cleanups.
93
            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.
94
                                            **kwargs)
95
            builder.generate(sio)
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
96
            sio.seek(0)
97
            stanzas = list(read_stanzas(sio))
98
            self.assertEqual(1, len(stanzas))
99
            return stanzas[0]
100
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
101
        def get_one_stanza(stanza, key):
2022.1.1 by John Arbash Meinel
[merge] version-info plugin, and cleanup for layout in bzr
102
            new_stanzas = list(read_stanzas(
103
                                StringIO(stanza[key].encode('utf8'))))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
104
            self.assertEqual(1, len(new_stanzas))
105
            return new_stanzas[0]
106
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
107
        stanza = regen()
108
        self.failUnless('date' in stanza)
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
109
        self.failUnless('build-date' in stanza)
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
110
        self.assertEqual(['3'], stanza.get_all('revno'))
0.8.16 by John Arbash Meinel
Using revision-id for rio, and revision_id for python
111
        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.
112
113
        stanza = regen(check_for_clean=True)
114
        self.assertEqual(['True'], stanza.get_all('clean'))
115
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
116
        self.build_tree(['branch/c'])
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
117
        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.
118
        self.assertEqual(['False'], stanza.get_all('clean'))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
119
2903.2.9 by Martin Pool
Review cleanups, mostly documentation
120
        # XXX: This assumes it's being run against a repository that updates
121
        # the root revision on every commit.  Newer ones that use
2903.2.2 by Martin Pool
doc
122
        # RootCommitBuilder won't update it on each commit.
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
123
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
1731.1.50 by Aaron Bentley
Merge bzr.dev
124
        self.assertEqual(['', 'a', 'b', 'c'], file_rev_stanza.get_all('path'))
125
        self.assertEqual(['r3', 'r3', 'r2', 'unversioned'],
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
126
            file_rev_stanza.get_all('revision'))
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
127
        os.remove('branch/c')
128
129
        stanza = regen(include_revision_history=True)
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
130
        revision_stanza = get_one_stanza(stanza, 'revisions')
0.8.7 by John Arbash Meinel
Adding tests for parsing the rio text back into rio.
131
        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
132
        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.
133
        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.
134
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
135
        # a was modified, so it should show up modified again
136
        self.build_tree(['branch/a', 'branch/c'])
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
137
        wt.add('c')
138
        wt.rename_one('b', 'd')
139
        stanza = regen(check_for_clean=True, include_file_revisions=True)
140
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
1731.1.50 by Aaron Bentley
Merge bzr.dev
141
        self.assertEqual(['', 'a', 'b', 'c', 'd'], 
142
                          file_rev_stanza.get_all('path'))
143
        self.assertEqual(['r3', 'modified', 'renamed to d', 'new', 
144
                          'renamed from b'],
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
145
                         file_rev_stanza.get_all('revision'))
146
147
        wt.commit('modified', rev_id='r4')
148
        wt.remove(['c', 'd'])
149
        os.remove('branch/d')
150
        stanza = regen(check_for_clean=True, include_file_revisions=True)
151
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
1731.1.50 by Aaron Bentley
Merge bzr.dev
152
        self.assertEqual(['', 'a', 'c', 'd'], file_rev_stanza.get_all('path'))
153
        self.assertEqual(['r4', 'r4', 'unversioned', 'removed'],
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
154
                         file_rev_stanza.get_all('revision'))
155
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
156
    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.
157
        wt = self.create_branch()
0.8.5 by John Arbash Meinel
Adding some whitebox tests for the output of generate_version_info
158
159
        def regen(**kwargs):
2022.1.4 by John Arbash Meinel
test feedback from Robert.
160
            """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
161
            outf = open('test_version_information.py', 'wb')
2022.1.4 by John Arbash Meinel
test feedback from Robert.
162
            try:
163
                builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt,
164
                                                   **kwargs)
165
                builder.generate(outf)
166
            finally:
167
                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.
168
            module_info = imp.find_module('test_version_information',
169
                                          [os.getcwdu()])
170
            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
171
            # Make sure the module isn't cached
172
            sys.modules.pop('tvi', None)
173
            sys.modules.pop('test_version_information', None)
174
            # Delete the compiled versions, because we are generating
175
            # a new file fast enough that python doesn't detect it
176
            # needs to recompile, and using sleep() just makes the
177
            # test slow
178
            if os.path.exists('test_version_information.pyc'):
179
                os.remove('test_version_information.pyc')
180
            if os.path.exists('test_version_information.pyo'):
181
                os.remove('test_version_information.pyo')
182
            return tvi
183
184
        tvi = regen()
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
185
        self.assertEqual(3, tvi.version_info['revno'])
186
        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
187
        self.failUnless(tvi.version_info.has_key('date'))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
188
        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
189
190
        tvi = regen(check_for_clean=True)
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
191
        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
192
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
193
        self.build_tree(['branch/c'])
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
194
        tvi = regen(check_for_clean=True, include_file_revisions=True)
195
        self.assertEqual(False, tvi.version_info['clean'])
1731.1.50 by Aaron Bentley
Merge bzr.dev
196
        self.assertEqual(['', 'a', 'b', 'c'], 
197
                         sorted(tvi.file_revisions.keys()))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
198
        self.assertEqual('r3', tvi.file_revisions['a'])
199
        self.assertEqual('r2', tvi.file_revisions['b'])
200
        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
201
        os.remove('branch/c')
202
203
        tvi = regen(include_revision_history=True)
0.8.15 by John Arbash Meinel
Including the date stamp for all revisions.
204
0.8.23 by John Arbash Meinel
whitespace and formatting cleanups.
205
        rev_info = [(rev, message) for rev, message, timestamp, timezone
206
                                   in tvi.revisions]
2030.1.2 by John Arbash Meinel
Change the version-info --format=rio to support unicode messages
207
        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.
208
0.8.20 by John Arbash Meinel
Updated version-info to the latest bzr.dev codebase. Changed to using VersionInfoBuilder, and made tests pass.
209
        # a was modified, so it should show up modified again
210
        self.build_tree(['branch/a', 'branch/c'])
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
211
        wt.add('c')
212
        wt.rename_one('b', 'd')
213
        tvi = regen(check_for_clean=True, include_file_revisions=True)
1731.1.50 by Aaron Bentley
Merge bzr.dev
214
        self.assertEqual(['', 'a', 'b', 'c', 'd'], 
215
                          sorted(tvi.file_revisions.keys()))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
216
        self.assertEqual('modified', tvi.file_revisions['a'])
217
        self.assertEqual('renamed to d', tvi.file_revisions['b'])
218
        self.assertEqual('new', tvi.file_revisions['c'])
219
        self.assertEqual('renamed from b', tvi.file_revisions['d'])
220
221
        wt.commit('modified', rev_id='r4')
222
        wt.remove(['c', 'd'])
223
        os.remove('branch/d')
224
        tvi = regen(check_for_clean=True, include_file_revisions=True)
1731.1.50 by Aaron Bentley
Merge bzr.dev
225
        self.assertEqual(['', 'a', 'c', 'd'], 
226
                          sorted(tvi.file_revisions.keys()))
0.8.13 by John Arbash Meinel
Including file-revisions fields, updated test suite.
227
        self.assertEqual('r4', tvi.file_revisions['a'])
228
        self.assertEqual('unversioned', tvi.file_revisions['c'])
229
        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
230
2948.4.1 by Lukáš Lalinský
Custom template-based version info formatter.
231
    def test_custom_version_text(self):
232
        wt = self.create_branch()
233
234
        def regen(tpl, **kwargs):
235
            sio = StringIO()
236
            builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
237
                                               template=tpl, **kwargs)
238
            builder.generate(sio)
239
            val = sio.getvalue()
240
            return val
241
242
        val = regen('build-date: "{build_date}"\ndate: "{date}"')
243
        self.assertContainsRe(val, 'build-date: "[0-9-+: ]+"')
244
        self.assertContainsRe(val, 'date: "[0-9-+: ]+"')
245
246
        val = regen('revno: {revno}')
247
        self.assertEqual(val, 'revno: 3')
248
249
        val = regen('revision-id: {revision_id}')
250
        self.assertEqual(val, 'revision-id: r3')
251
252
        val = regen('clean: {clean}', check_for_clean=True)
253
        self.assertEqual(val, 'clean: 1')
254
255
        self.build_tree(['branch/c'])
256
        val = regen('clean: {clean}', check_for_clean=True)
257
        self.assertEqual(val, 'clean: 0')
258
        os.remove('branch/c')
3138.1.1 by John Arbash Meinel
Fix bug #175886: version_info deprecated functions should be tested to still work.
259
260
261
class TestBuilder(version_info_formats.VersionInfoBuilder):
262
    pass
263
264
265
class TestVersionInfoFormatRegistry(tests.TestCase):
266
267
    def setUp(self):
268
        super(TestVersionInfoFormatRegistry, self).setUp()
269
        registry = version_info_formats.format_registry
270
        self._default_key = registry._default_key
271
        self._dict = registry._dict.copy()
272
        self._help_dict = registry._help_dict.copy()
273
        self._info_dict = registry._info_dict.copy()
274
        self.addCleanup(self._cleanup)
275
276
    def _cleanup(self):
277
        # Restore the registry to pristine state after the test runs
278
        registry = version_info_formats.format_registry
279
        registry._default_key = self._default_key
280
        registry._dict = self._dict
281
        registry._help_dict = self._help_dict
282
        registry._info_dict = self._info_dict
283
284
    def test_register_remove(self):
285
        registry = version_info_formats.format_registry
286
        registry.register('testbuilder',
287
            TestBuilder, 'a simple test builder')
288
        self.assertIs(TestBuilder, registry.get('testbuilder'))
289
        self.assertEqual('a simple test builder',
290
                         registry.get_help('testbuilder'))
291
        registry.remove('testbuilder')
292
        self.assertRaises(KeyError, registry.get, 'testbuilder')
293
294
    def test_old_functions(self):
295
        self.applyDeprecated(symbol_versioning.one_zero,
296
            version_info_formats.register_builder,
297
            'test-builder', __name__, 'TestBuilder')
298
        formats = self.applyDeprecated(symbol_versioning.one_zero,
299
            version_info_formats.get_builder_formats)
300
        self.failUnless('test-builder' in formats)
301
        self.assertIs(TestBuilder,
302
            self.applyDeprecated(symbol_versioning.one_zero,
303
                version_info_formats.get_builder, 'test-builder'))
304
        version_info_formats.format_registry.remove('test-builder')