~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_version_info.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-30 12:52:35 UTC
  • mto: This revision was merged to the branch mainline in revision 6418.
  • Revision ID: jelmer@samba.org-20111230125235-1xzbx91pljttr9t7
Split up version info tests.

Show diffs side-by-side

added added

removed removed

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