~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: 2012-01-05 16:03:11 UTC
  • mfrom: (6432 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6433.
  • Revision ID: jelmer@samba.org-20120105160311-12o5p67kin1v3zps
Merge bzr.dev.

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, **kwargs)
 
100
        builder.generate(sio)
 
101
        val = sio.getvalue()
 
102
        return val
 
103
 
 
104
    def test_simple(self):
95
105
        wt = self.create_branch()
96
106
 
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()
 
107
        val = self.regen_text(wt)
106
108
        self.assertContainsRe(val, 'build-date:')
107
109
        self.assertContainsRe(val, 'date:')
108
110
        self.assertContainsRe(val, 'revno: 3')
109
111
        self.assertContainsRe(val, 'revision-id: r3')
110
112
 
111
 
        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)
112
116
        self.assertContainsRe(val, 'clean: True')
113
117
 
 
118
    def test_no_clean(self):
 
119
        wt = self.create_branch()
114
120
        self.build_tree(['branch/c'])
115
 
        val = regen(check_for_clean=True)
 
121
        val = self.regen_text(wt, check_for_clean=True)
116
122
        self.assertContainsRe(val, 'clean: False')
117
 
        os.remove('branch/c')
118
 
 
119
 
        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)
120
128
        self.assertContainsRe(val, 'id: r1')
121
129
        self.assertContainsRe(val, 'message: a')
122
130
        self.assertContainsRe(val, 'id: r2')
124
132
        self.assertContainsRe(val, 'id: r3')
125
133
        self.assertContainsRe(val, 'message: \xc3\xa52') # utf8 encoding '\xe5'
126
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
 
127
144
    def test_rio_version_hook(self):
128
145
        def update_stanza(rev, stanza):
129
146
            stanza.add('bla', 'bloe')
131
148
            'revision', update_stanza, None)
132
149
        wt = self.create_branch()
133
150
 
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()
 
151
        stanza = self.regen(wt)
145
152
        self.assertEqual(['bloe'], stanza.get_all('bla'))
146
153
 
147
 
    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):
148
160
        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()
 
161
        stanza = self.regen(wt)
167
162
        self.assertTrue('date' in stanza)
168
163
        self.assertTrue('build-date' in stanza)
169
164
        self.assertEqual(['3'], stanza.get_all('revno'))
170
165
        self.assertEqual(['r3'], stanza.get_all('revision-id'))
171
166
 
172
 
        stanza = regen(check_for_clean=True)
173
 
        self.assertEqual(['True'], stanza.get_all('clean'))
174
 
 
 
167
    def test_not_clean(self):
 
168
        wt = self.create_branch()
175
169
        self.build_tree(['branch/c'])
176
 
        stanza = regen(check_for_clean=True, include_file_revisions=True)
 
170
        stanza = self.regen(wt, check_for_clean=True, include_file_revisions=True)
177
171
        self.assertEqual(['False'], stanza.get_all('clean'))
178
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)
179
177
        # This assumes it's being run against a tree that does not update the
180
178
        # root revision on every commit.
181
 
        file_rev_stanza = get_one_stanza(stanza, 'file-revisions')
 
179
        file_rev_stanza = self.get_one_stanza(stanza, 'file-revisions')
182
180
        self.assertEqual(['', 'a', 'b', 'c'], file_rev_stanza.get_all('path'))
183
181
        self.assertEqual(['r1', 'r3', 'r2', 'unversioned'],
184
182
            file_rev_stanza.get_all('revision'))
185
 
        os.remove('branch/c')
186
183
 
187
 
        stanza = regen(include_revision_history=True)
188
 
        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')
189
188
        self.assertEqual(['r1', 'r2', 'r3'], revision_stanza.get_all('id'))
190
189
        self.assertEqual(['a', 'b', u'\xe52'], revision_stanza.get_all('message'))
191
190
        self.assertEqual(3, len(revision_stanza.get_all('date')))
192
191
 
 
192
    def test_file_revisions_with_rename(self):
193
193
        # a was modified, so it should show up modified again
 
194
        wt = self.create_branch()
194
195
        self.build_tree(['branch/a', 'branch/c'])
195
196
        wt.add('c')
196
197
        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')
 
198
        stanza = self.regen(wt, check_for_clean=True, include_file_revisions=True)
 
199
        file_rev_stanza = self.get_one_stanza(stanza, 'file-revisions')
199
200
        self.assertEqual(['', 'a', 'b', 'c', 'd'],
200
201
                          file_rev_stanza.get_all('path'))
201
202
        self.assertEqual(['r1', 'modified', 'renamed to d', 'new',
202
203
                          'renamed from b'],
203
204
                         file_rev_stanza.get_all('revision'))
204
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
 
205
212
        wt.commit('modified', rev_id='r4')
 
213
 
206
214
        wt.remove(['c', 'd'])
207
215
        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')
 
216
        stanza = self.regen(wt, check_for_clean=True, include_file_revisions=True)
 
217
        file_rev_stanza = self.get_one_stanza(stanza, 'file-revisions')
210
218
        self.assertEqual(['', 'a', 'c', 'd'], file_rev_stanza.get_all('path'))
211
219
        self.assertEqual(['r1', 'r4', 'unversioned', 'removed'],
212
220
                         file_rev_stanza.get_all('revision'))
213
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
 
214
238
    def test_python_null(self):
215
239
        wt = self.make_branch_and_tree('branch')
216
240
 
231
255
        val = sio.getvalue()
232
256
        self.assertContainsRe(val, "'revno': '1.1.1'")
233
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
 
282
 
234
283
    def test_python_version(self):
235
284
        wt = self.create_branch()
236
285
 
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()
 
286
        tvi = self.regen(wt)
263
287
        self.assertEqual('3', tvi.version_info['revno'])
264
288
        self.assertEqual('r3', tvi.version_info['revision_id'])
265
289
        self.assertTrue(tvi.version_info.has_key('date'))
266
290
        self.assertEqual(None, tvi.version_info['clean'])
267
291
 
268
 
        tvi = regen(check_for_clean=True)
 
292
        tvi = self.regen(wt, check_for_clean=True)
269
293
        self.assertEqual(True, tvi.version_info['clean'])
270
294
 
271
295
        self.build_tree(['branch/c'])
272
 
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
296
        tvi = self.regen(wt, check_for_clean=True, include_file_revisions=True)
273
297
        self.assertEqual(False, tvi.version_info['clean'])
274
298
        self.assertEqual(['', 'a', 'b', 'c'],
275
299
                         sorted(tvi.file_revisions.keys()))
278
302
        self.assertEqual('unversioned', tvi.file_revisions['c'])
279
303
        os.remove('branch/c')
280
304
 
281
 
        tvi = regen(include_revision_history=True)
 
305
        tvi = self.regen(wt, include_revision_history=True)
282
306
 
283
307
        rev_info = [(rev, message) for rev, message, timestamp, timezone
284
308
                                   in tvi.revisions]
288
312
        self.build_tree(['branch/a', 'branch/c'])
289
313
        wt.add('c')
290
314
        wt.rename_one('b', 'd')
291
 
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
315
        tvi = self.regen(wt, check_for_clean=True, include_file_revisions=True)
292
316
        self.assertEqual(['', 'a', 'b', 'c', 'd'],
293
317
                          sorted(tvi.file_revisions.keys()))
294
318
        self.assertEqual('modified', tvi.file_revisions['a'])
299
323
        wt.commit('modified', rev_id='r4')
300
324
        wt.remove(['c', 'd'])
301
325
        os.remove('branch/d')
302
 
        tvi = regen(check_for_clean=True, include_file_revisions=True)
 
326
        tvi = self.regen(wt, check_for_clean=True, include_file_revisions=True)
303
327
        self.assertEqual(['', 'a', 'c', 'd'],
304
328
                          sorted(tvi.file_revisions.keys()))
305
329
        self.assertEqual('r4', tvi.file_revisions['a'])
306
330
        self.assertEqual('unversioned', tvi.file_revisions['c'])
307
331
        self.assertEqual('removed', tvi.file_revisions['d'])
308
332
 
 
333
 
 
334
class CustomVersionInfoTests(VersionInfoTestCase):
 
335
 
309
336
    def test_custom_null(self):
310
337
        sio = StringIO()
311
338
        wt = self.make_branch_and_tree('branch')
328
355
        builder.generate(sio)
329
356
        self.assertEquals("1.1.1 revid: o2", sio.getvalue())
330
357
 
331
 
    def test_custom_version_text(self):
 
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):
332
367
        wt = self.create_branch()
333
368
 
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}"')
 
369
        val = self.regen(wt, 'build-date: "{build_date}"\ndate: "{date}"')
343
370
        self.assertContainsRe(val, 'build-date: "[0-9-+: ]+"')
344
371
        self.assertContainsRe(val, 'date: "[0-9-+: ]+"')
345
372
 
346
 
        val = regen('revno: {revno}')
 
373
    def test_revno(self):
 
374
        wt = self.create_branch()
 
375
        val = self.regen(wt, 'revno: {revno}')
347
376
        self.assertEqual(val, 'revno: 3')
348
377
 
349
 
        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}')
350
381
        self.assertEqual(val, 'revision-id: r3')
351
382
 
352
 
        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)
353
386
        self.assertEqual(val, 'clean: 1')
354
387
 
 
388
    def test_not_clean(self):
 
389
        wt = self.create_branch()
 
390
 
355
391
        self.build_tree(['branch/c'])
356
 
        val = regen('clean: {clean}', check_for_clean=True)
 
392
        val = self.regen(wt, 'clean: {clean}', check_for_clean=True)
357
393
        self.assertEqual(val, 'clean: 0')
358
394
        os.remove('branch/c')
359
395