~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_version_info.py

  • Committer: Vincent Ladeuil
  • Date: 2012-03-13 17:25:29 UTC
  • mfrom: (6499 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6501.
  • Revision ID: v.ladeuil+lp@free.fr-20120313172529-i0suyjnepsor25i7
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
24
24
from bzrlib import (
25
25
    errors,
26
26
    registry,
27
 
    symbol_versioning,
28
27
    tests,
29
28
    version_info_formats,
30
29
    )
36
35
from bzrlib.version_info_formats.format_python import PythonVersionInfoBuilder
37
36
 
38
37
 
39
 
class TestVersionInfo(TestCaseWithTransport):
 
38
class VersionInfoTestCase(TestCaseWithTransport):
40
39
 
41
40
    def create_branch(self):
42
41
        wt = self.make_branch_and_tree('branch')
54
53
 
55
54
        return wt
56
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
 
 
76
class TestVersionInfoRio(VersionInfoTestCase):
 
77
 
57
78
    def test_rio_null(self):
58
79
        wt = self.make_branch_and_tree('branch')
59
80
 
64
85
        self.assertContainsRe(val, 'build-date:')
65
86
        self.assertContainsRe(val, 'revno: 0')
66
87
 
67
 
    def test_rio_version_text(self):
 
88
    def test_rio_dotted_revno(self):
 
89
        wt = self.create_tree_with_dotted_revno()
 
90
 
 
91
        sio = StringIO()
 
92
        builder = RioVersionInfoBuilder(wt.branch, working_tree=wt)
 
93
        builder.generate(sio)
 
94
        val = sio.getvalue()
 
95
        self.assertContainsRe(val, 'revno: 1.1.1')
 
96
 
 
97
    def regen_text(self, wt, **kwargs):
 
98
        sio = StringIO()
 
99
        builder = RioVersionInfoBuilder(wt.branch, working_tree=wt, **kwargs)
 
100
        builder.generate(sio)
 
101
        val = sio.getvalue()
 
102
        return val
 
103
 
 
104
    def test_simple(self):
68
105
        wt = self.create_branch()
69
106
 
70
 
        def regen(**kwargs):
71
 
            sio = StringIO()
72
 
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
73
 
                                            **kwargs)
74
 
            builder.generate(sio)
75
 
            val = sio.getvalue()
76
 
            return val
77
 
 
78
 
        val = regen()
 
107
        val = self.regen_text(wt)
79
108
        self.assertContainsRe(val, 'build-date:')
80
109
        self.assertContainsRe(val, 'date:')
81
110
        self.assertContainsRe(val, 'revno: 3')
82
111
        self.assertContainsRe(val, 'revision-id: r3')
83
112
 
84
 
        val = regen(check_for_clean=True)
 
113
    def test_clean(self):
 
114
        wt = self.create_branch()
 
115
        val = self.regen_text(wt, check_for_clean=True)
85
116
        self.assertContainsRe(val, 'clean: True')
86
117
 
 
118
    def test_no_clean(self):
 
119
        wt = self.create_branch()
87
120
        self.build_tree(['branch/c'])
88
 
        val = regen(check_for_clean=True)
 
121
        val = self.regen_text(wt, check_for_clean=True)
89
122
        self.assertContainsRe(val, 'clean: False')
90
 
        os.remove('branch/c')
91
 
 
92
 
        val = regen(include_revision_history=True)
 
123
 
 
124
    def test_history(self):
 
125
        wt = self.create_branch()
 
126
 
 
127
        val = self.regen_text(wt, include_revision_history=True)
93
128
        self.assertContainsRe(val, 'id: r1')
94
129
        self.assertContainsRe(val, 'message: a')
95
130
        self.assertContainsRe(val, 'id: r2')
97
132
        self.assertContainsRe(val, 'id: r3')
98
133
        self.assertContainsRe(val, 'message: \xc3\xa52') # utf8 encoding '\xe5'
99
134
 
 
135
    def regen(self, wt, **kwargs):
 
136
        sio = StringIO()
 
137
        builder = RioVersionInfoBuilder(wt.branch, working_tree=wt, **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
 
100
144
    def test_rio_version_hook(self):
101
145
        def update_stanza(rev, stanza):
102
146
            stanza.add('bla', 'bloe')
104
148
            'revision', update_stanza, None)
105
149
        wt = self.create_branch()
106
150
 
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()
 
151
        stanza = self.regen(wt)
118
152
        self.assertEqual(['bloe'], stanza.get_all('bla'))
119
153
 
120
 
    def test_rio_version(self):
 
154
    def get_one_stanza(self, stanza, key):
 
155
        new_stanzas = list(read_stanzas(StringIO(stanza[key].encode('utf8'))))
 
156
        self.assertEqual(1, len(new_stanzas))
 
157
        return new_stanzas[0]
 
158
 
 
159
    def test_build_date(self):
121
160
        wt = self.create_branch()
122
 
 
123
 
        def regen(**kwargs):
124
 
            sio = StringIO()
125
 
            builder = RioVersionInfoBuilder(wt.branch, working_tree=wt,
126
 
                                            **kwargs)
127
 
            builder.generate(sio)
128
 
            sio.seek(0)
129
 
            stanzas = list(read_stanzas(sio))
130
 
            self.assertEqual(1, len(stanzas))
131
 
            return stanzas[0]
132
 
 
133
 
        def get_one_stanza(stanza, key):
134
 
            new_stanzas = list(read_stanzas(
135
 
                                StringIO(stanza[key].encode('utf8'))))
136
 
            self.assertEqual(1, len(new_stanzas))
137
 
            return new_stanzas[0]
138
 
 
139
 
        stanza = regen()
140
 
        self.failUnless('date' in stanza)
141
 
        self.failUnless('build-date' in stanza)
 
161
        stanza = self.regen(wt)
 
162
        self.assertTrue('date' in stanza)
 
163
        self.assertTrue('build-date' in stanza)
142
164
        self.assertEqual(['3'], stanza.get_all('revno'))
143
165
        self.assertEqual(['r3'], stanza.get_all('revision-id'))
144
166
 
145
 
        stanza = regen(check_for_clean=True)
146
 
        self.assertEqual(['True'], stanza.get_all('clean'))
147
 
 
 
167
    def test_not_clean(self):
 
168
        wt = self.create_branch()
148
169
        self.build_tree(['branch/c'])
149
 
        stanza = regen(check_for_clean=True, include_file_revisions=True)
 
170
        stanza = self.regen(wt, check_for_clean=True, include_file_revisions=True)
150
171
        self.assertEqual(['False'], stanza.get_all('clean'))
151
172
 
 
173
    def test_file_revisions(self):
 
174
        wt = self.create_branch()
 
175
        self.build_tree(['branch/c'])
 
176
        stanza = self.regen(wt, check_for_clean=True, include_file_revisions=True)
152
177
        # This assumes it's being run against a tree that does not update the
153
178
        # root revision on every commit.
154
 
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
 
179
        file_rev_stanza = self.get_one_stanza(stanza, 'file-revisions')
155
180
        self.assertEqual(['', 'a', 'b', 'c'], file_rev_stanza.get_all('path'))
156
181
        self.assertEqual(['r1', 'r3', 'r2', 'unversioned'],
157
182
            file_rev_stanza.get_all('revision'))
158
 
        os.remove('branch/c')
159
183
 
160
 
        stanza = regen(include_revision_history=True)
161
 
        revision_stanza = get_one_stanza(stanza, 'revisions')
 
184
    def test_revision_history(self):
 
185
        wt = self.create_branch()
 
186
        stanza = self.regen(wt, include_revision_history=True)
 
187
        revision_stanza = self.get_one_stanza(stanza, 'revisions')
162
188
        self.assertEqual(['r1', 'r2', 'r3'], revision_stanza.get_all('id'))
163
189
        self.assertEqual(['a', 'b', u'\xe52'], revision_stanza.get_all('message'))
164
190
        self.assertEqual(3, len(revision_stanza.get_all('date')))
165
191
 
 
192
    def test_file_revisions_with_rename(self):
166
193
        # a was modified, so it should show up modified again
 
194
        wt = self.create_branch()
167
195
        self.build_tree(['branch/a', 'branch/c'])
168
196
        wt.add('c')
169
197
        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')
 
198
        stanza = self.regen(wt, check_for_clean=True, include_file_revisions=True)
 
199
        file_rev_stanza = self.get_one_stanza(stanza, 'file-revisions')
172
200
        self.assertEqual(['', 'a', 'b', 'c', 'd'],
173
201
                          file_rev_stanza.get_all('path'))
174
202
        self.assertEqual(['r1', 'modified', 'renamed to d', 'new',
175
203
                          'renamed from b'],
176
204
                         file_rev_stanza.get_all('revision'))
177
205
 
 
206
    def test_file_revisions_with_removal(self):
 
207
        wt = self.create_branch()
 
208
        self.build_tree(['branch/a', 'branch/c'])
 
209
        wt.add('c')
 
210
        wt.rename_one('b', 'd')
 
211
 
178
212
        wt.commit('modified', rev_id='r4')
 
213
 
179
214
        wt.remove(['c', 'd'])
180
215
        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')
 
216
        stanza = self.regen(wt, check_for_clean=True, include_file_revisions=True)
 
217
        file_rev_stanza = self.get_one_stanza(stanza, 'file-revisions')
183
218
        self.assertEqual(['', 'a', 'c', 'd'], file_rev_stanza.get_all('path'))
184
219
        self.assertEqual(['r1', 'r4', 'unversioned', 'removed'],
185
220
                         file_rev_stanza.get_all('revision'))
186
221
 
 
222
    def test_revision(self):
 
223
        wt = self.create_branch()
 
224
        self.build_tree(['branch/a', 'branch/c'])
 
225
        wt.add('c')
 
226
        wt.rename_one('b', 'd')
 
227
 
 
228
        stanza = self.regen(wt, check_for_clean=True,
 
229
            include_file_revisions=True, revision_id=wt.last_revision())
 
230
        file_rev_stanza = self.get_one_stanza(stanza, 'file-revisions')
 
231
        self.assertEqual(['', 'a', 'b'], file_rev_stanza.get_all('path'))
 
232
        self.assertEqual(['r1', 'r3', 'r2'],
 
233
                         file_rev_stanza.get_all('revision'))
 
234
 
 
235
 
 
236
class PythonVersionInfoTests(VersionInfoTestCase):
 
237
 
187
238
    def test_python_null(self):
188
239
        wt = self.make_branch_and_tree('branch')
189
240
 
192
243
        builder.generate(sio)
193
244
        val = sio.getvalue()
194
245
        self.assertContainsRe(val, "'revision_id': None")
195
 
        self.assertContainsRe(val, "'revno': 0")
 
246
        self.assertContainsRe(val, "'revno': '0'")
 
247
        self.assertNotContainsString(val, '\n\n\n\n')
 
248
 
 
249
    def test_python_dotted_revno(self):
 
250
        wt = self.create_tree_with_dotted_revno()
 
251
 
 
252
        sio = StringIO()
 
253
        builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt)
 
254
        builder.generate(sio)
 
255
        val = sio.getvalue()
 
256
        self.assertContainsRe(val, "'revno': '1.1.1'")
 
257
 
 
258
    def regen(self, wt, **kwargs):
 
259
        """Create a test module, import and return it"""
 
260
        outf = open('test_version_information.py', 'wb')
 
261
        try:
 
262
            builder = PythonVersionInfoBuilder(wt.branch, working_tree=wt,
 
263
                                               **kwargs)
 
264
            builder.generate(outf)
 
265
        finally:
 
266
            outf.close()
 
267
        module_info = imp.find_module('test_version_information',
 
268
                                      [os.getcwdu()])
 
269
        tvi = imp.load_module('tvi', *module_info)
 
270
        # Make sure the module isn't cached
 
271
        sys.modules.pop('tvi', None)
 
272
        sys.modules.pop('test_version_information', None)
 
273
        # Delete the compiled versions, because we are generating
 
274
        # a new file fast enough that python doesn't detect it
 
275
        # needs to recompile, and using sleep() just makes the
 
276
        # test slow
 
277
        if os.path.exists('test_version_information.pyc'):
 
278
            os.remove('test_version_information.pyc')
 
279
        if os.path.exists('test_version_information.pyo'):
 
280
            os.remove('test_version_information.pyo')
 
281
        return tvi
196
282
 
197
283
    def test_python_version(self):
198
284
        wt = self.create_branch()
199
285
 
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'])
 
286
        tvi = self.regen(wt)
 
287
        self.assertEqual('3', tvi.version_info['revno'])
227
288
        self.assertEqual('r3', tvi.version_info['revision_id'])
228
 
        self.failUnless(tvi.version_info.has_key('date'))
 
289
        self.assertTrue(tvi.version_info.has_key('date'))
229
290
        self.assertEqual(None, tvi.version_info['clean'])
230
291
 
231
 
        tvi = regen(check_for_clean=True)
 
292
        tvi = self.regen(wt, check_for_clean=True)
232
293
        self.assertEqual(True, tvi.version_info['clean'])
233
294
 
234
295
        self.build_tree(['branch/c'])
235
 
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
296
        tvi = self.regen(wt, check_for_clean=True, include_file_revisions=True)
236
297
        self.assertEqual(False, tvi.version_info['clean'])
237
298
        self.assertEqual(['', 'a', 'b', 'c'],
238
299
                         sorted(tvi.file_revisions.keys()))
241
302
        self.assertEqual('unversioned', tvi.file_revisions['c'])
242
303
        os.remove('branch/c')
243
304
 
244
 
        tvi = regen(include_revision_history=True)
 
305
        tvi = self.regen(wt, include_revision_history=True)
245
306
 
246
307
        rev_info = [(rev, message) for rev, message, timestamp, timezone
247
308
                                   in tvi.revisions]
251
312
        self.build_tree(['branch/a', 'branch/c'])
252
313
        wt.add('c')
253
314
        wt.rename_one('b', 'd')
254
 
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
315
        tvi = self.regen(wt, check_for_clean=True, include_file_revisions=True)
255
316
        self.assertEqual(['', 'a', 'b', 'c', 'd'],
256
317
                          sorted(tvi.file_revisions.keys()))
257
318
        self.assertEqual('modified', tvi.file_revisions['a'])
262
323
        wt.commit('modified', rev_id='r4')
263
324
        wt.remove(['c', 'd'])
264
325
        os.remove('branch/d')
265
 
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
326
        tvi = self.regen(wt, check_for_clean=True, include_file_revisions=True)
266
327
        self.assertEqual(['', 'a', 'c', 'd'],
267
328
                          sorted(tvi.file_revisions.keys()))
268
329
        self.assertEqual('r4', tvi.file_revisions['a'])
269
330
        self.assertEqual('unversioned', tvi.file_revisions['c'])
270
331
        self.assertEqual('removed', tvi.file_revisions['d'])
271
332
 
 
333
 
 
334
class CustomVersionInfoTests(VersionInfoTestCase):
 
335
 
272
336
    def test_custom_null(self):
273
337
        sio = StringIO()
274
338
        wt = self.make_branch_and_tree('branch')
283
347
        self.assertRaises(errors.MissingTemplateVariable, 
284
348
            builder.generate, sio)
285
349
 
286
 
    def test_custom_version_text(self):
 
350
    def test_custom_dotted_revno(self):
 
351
        sio = StringIO()
 
352
        wt = self.create_tree_with_dotted_revno()
 
353
        builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt, 
 
354
            template='{revno} revid: {revision_id}')
 
355
        builder.generate(sio)
 
356
        self.assertEquals("1.1.1 revid: o2", sio.getvalue())
 
357
 
 
358
    def regen(self, wt, tpl, **kwargs):
 
359
        sio = StringIO()
 
360
        builder = CustomVersionInfoBuilder(wt.branch, working_tree=wt,
 
361
                                           template=tpl, **kwargs)
 
362
        builder.generate(sio)
 
363
        val = sio.getvalue()
 
364
        return val
 
365
 
 
366
    def test_build_date(self):
287
367
        wt = self.create_branch()
288
368
 
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}"')
 
369
        val = self.regen(wt, 'build-date: "{build_date}"\ndate: "{date}"')
298
370
        self.assertContainsRe(val, 'build-date: "[0-9-+: ]+"')
299
371
        self.assertContainsRe(val, 'date: "[0-9-+: ]+"')
300
372
 
301
 
        val = regen('revno: {revno}')
 
373
    def test_revno(self):
 
374
        wt = self.create_branch()
 
375
        val = self.regen(wt, 'revno: {revno}')
302
376
        self.assertEqual(val, 'revno: 3')
303
377
 
304
 
        val = regen('revision-id: {revision_id}')
 
378
    def test_revision_id(self):
 
379
        wt = self.create_branch()
 
380
        val = self.regen(wt, 'revision-id: {revision_id}')
305
381
        self.assertEqual(val, 'revision-id: r3')
306
382
 
307
 
        val = regen('clean: {clean}', check_for_clean=True)
 
383
    def test_clean(self):
 
384
        wt = self.create_branch()
 
385
        val = self.regen(wt, 'clean: {clean}', check_for_clean=True)
308
386
        self.assertEqual(val, 'clean: 1')
309
387
 
 
388
    def test_not_clean(self):
 
389
        wt = self.create_branch()
 
390
 
310
391
        self.build_tree(['branch/c'])
311
 
        val = regen('clean: {clean}', check_for_clean=True)
 
392
        val = self.regen(wt, 'clean: {clean}', check_for_clean=True)
312
393
        self.assertEqual(val, 'clean: 0')
313
394
        os.remove('branch/c')
314
395