~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-11-27 17:42:25 UTC
  • mto: This revision was merged to the branch mainline in revision 6311.
  • Revision ID: jelmer@samba.org-20111127174225-tspfeewl0gwxxumt
Add possible_transports in a couple more places.

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