~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/test_baz_import.py

  • Committer: Aaron Bentley
  • Date: 2006-03-07 15:23:15 UTC
  • mfrom: (321.1.2 bzrtools)
  • mto: (147.4.31 trunk)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: abentley@panoramicfeedback.com-20060307152315-42337454a0ad956b
MergeĀ fromĀ mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
from bzrlib.tests import TestCaseInTempDir, TestCase
19
 
from bzrlib import repository
20
19
from bzrlib.osutils import has_symlinks
21
20
try:
22
21
    import pybaz
23
22
except ImportError:
24
23
    pybaz = None
25
24
import os
26
 
from bzrlib.plugins.bzrtools import cmd_baz_import
27
 
from bzrlib.plugins.bzrtools.baz_import import (import_version,
28
 
                                                revision_id,
29
 
                                                make_archive,
30
 
                                                map_file_id)
 
25
from bzrlib.plugins.bzrtools.baz_import import (import_version, revision_id, 
 
26
                                                cmd_baz_import, make_archive)
 
27
from bzrlib.errors import NoSuchRevision
31
28
import shutil
32
29
from StringIO import StringIO
33
30
import tempfile
34
31
from testresources import (TestResource, TestLoader, OptimisingTestSuite,
35
32
                               ResourcedTestCase)
36
 
 
37
 
import bzrlib
38
 
from bzrlib.errors import NoSuchRevision
 
33
    
39
34
from bzrlib.plugins.bzrtools.fai import namespace_previous
40
35
from bzrlib.branch import Branch
41
36
 
44
39
        from unittest import TestSuite
45
40
        return TestSuite()
46
41
    return TestLoader().loadTestsFromName(__name__)
47
 
 
 
42
 
48
43
 
49
44
class BazTreeResource(TestResource):
50
45
 
64
59
        pybaz.set_my_id("Test User<test@example.org>")
65
60
 
66
61
        self.make_empty_import()
67
 
        self.make_utf8_log()
68
62
 
69
63
        self._empty_tag = 'demo@DONOTUSE/c--empty-tag--0'
70
 
        self._empty_tag_bzr = revision_id(self._empty_tag + '--base-0', None)
 
64
        self._empty_tag_bzr = revision_id(self._empty_tag + '--base-0')
71
65
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
72
66
            pybaz.Version(self._empty_tag))
73
67
 
74
68
        self._empty_merged_tag = 'demo@DONOTUSE/c--empty-merged-tag--0'
75
 
        self._empty_merged_tag_bzr_base = revision_id(self._empty_merged_tag
76
 
                                                 + '--base-0', None)
77
 
        self._empty_merged_tag_bzr = revision_id(self._empty_merged_tag
78
 
                                                 + '--patch-1', None)
 
69
        self._empty_merged_tag_bzr_base = revision_id(self._empty_merged_tag 
 
70
                                                 + '--base-0')
 
71
        self._empty_merged_tag_bzr = revision_id(self._empty_merged_tag 
 
72
                                                 + '--patch-1')
79
73
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
80
74
            pybaz.Version(self._empty_merged_tag))
81
75
        tree = pybaz.Revision(self._empty_merged_tag + '--base-0').get(
86
80
        msg["summary"]="did a merge, yarh"
87
81
        tree.commit(msg)
88
82
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
89
 
 
 
83
        
90
84
        # tree, two commits, includes merge of other branch
91
85
        self._empty_merged_tag_2 = 'demo@DONOTUSE/c--empty-tag-2--0'
92
86
        self._empty_merged_tag_2_bzr_base = revision_id(
93
 
            self._empty_merged_tag_2 + '--base-0', None)
 
87
            self._empty_merged_tag_2 + '--base-0')
94
88
        self._empty_merged_tag_2_bzr = revision_id(
95
 
            self._empty_merged_tag_2 + '--patch-1', None)
 
89
            self._empty_merged_tag_2 + '--patch-1')
96
90
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
97
91
            pybaz.Version(self._empty_merged_tag_2))
98
92
        tree = pybaz.Revision(self._empty_merged_tag_2 + '--base-0').get (
105
99
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
106
100
 
107
101
        self._bad_id_tag = 'demo@DONOTUSE/c--bad-id--0'
108
 
        self._bad_id_tag_bzr_base = revision_id(self._bad_id_tag + '--base-0',
109
 
                                                None)
110
 
        self._bad_id_tag_bzr = revision_id(self._bad_id_tag + '--patch-1',
111
 
                                           None)
 
102
        self._bad_id_tag_bzr_base = revision_id(self._bad_id_tag + '--base-0')
 
103
        self._bad_id_tag_bzr = revision_id(self._bad_id_tag + '--patch-1')
112
104
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
113
105
            pybaz.Version(self._bad_id_tag))
114
106
        tree = pybaz.Revision(self._bad_id_tag + '--base-0').get(
115
107
            os.path.join(self._tmpdir, 'tree'))
116
108
        from bzrlib.plugins.bzrtools.baz_import import add_file
117
 
        add_file(os.path.join(self._tmpdir,'tree/path'), 'text',
 
109
        add_file(os.path.join(self._tmpdir,'tree/path'), 'text', 
118
110
                 'this_id/needs%escaping')
119
111
        msg = tree.log_message()
120
112
        msg["summary"] = "commit something which needs escaping."
128
120
    def make_import_symlink(self):
129
121
        self._import_symlink = 'demo@DONOTUSE/c--import-symlink--0'
130
122
        self._import_symlink_bzr = revision_id(
131
 
            self._import_symlink + '--base-0', None)
 
123
            self._import_symlink + '--base-0')
132
124
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
133
125
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
134
126
                               self._import_symlink)
135
 
        link_path = os.path.join(self._tmpdir, 'tree', 'alink')
136
 
        os.symlink('missing-file-name', link_path)
 
127
        os.symlink('missing-file-name',
 
128
                   os.path.join(self._tmpdir, 'tree', 'alink'))
137
129
        tree.add_tag('alink')
138
130
        id_file = open(os.path.join(tree, '.arch-ids', 'alink.id'), 'w')
139
131
        id_file.write('symlink_tag\n')
141
133
        msg = tree.log_message()
142
134
        msg["summary"] = "Import with a symlink"
143
135
        tree.import_(msg)
144
 
        os.unlink(link_path)
145
 
        f = file(link_path, 'w')
146
 
        f.write('Not a symlink no more!')
147
 
        f.close()
148
 
        msg = tree.log_message()
149
 
        msg["summary"] = "Turn a symlink into a file"
150
 
        tree.commit(msg)
151
136
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
152
137
 
153
138
    def make_empty_import(self):
154
139
        self._import = 'demo@DONOTUSE/c--import--0'
155
140
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
156
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
 
141
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), 
157
142
                               self._import)
158
143
        msg = tree.log_message()
159
144
        msg["summary"] = "I am importing now"
160
145
        tree.import_(msg)
161
146
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
162
147
 
163
 
    def make_utf8_log(self):
164
 
        self._utf8 = 'demo@DONOTUSE/c--utf8--0'
165
 
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
166
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
167
 
                               self._utf8)
168
 
        msg = tree.log_message()
169
 
        msg["summary"] = u"I am importing now\u1234".encode('utf-8')
170
 
        tree.import_(msg)
171
 
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
172
 
 
173
148
    def make_missing_ancestor(self):
174
149
        self._archivegoneroot = os.path.join(self._tmpdir, 'archivegone')
175
150
        self._archive = make_archive('demo-gone@DONOTUSE',
176
151
                                     str(self._archivegoneroot))
177
152
        self._missing_import = 'demo-gone@DONOTUSE/c--import--0'
178
 
        self._missing_import_bzr = revision_id(self._missing_import
179
 
                                                 + '--base-0', None)
 
153
        self._missing_import_bzr = revision_id(self._missing_import 
 
154
                                                 + '--base-0')
180
155
        self._missing_ancestor = 'demo@DONOTUSE/c--gone--0'
181
 
        self._missing_ancestor_bzr = revision_id(self._missing_ancestor
182
 
                                                 + '--base-0', None)
 
156
        self._missing_ancestor_bzr = revision_id(self._missing_ancestor 
 
157
                                                 + '--base-0')
183
158
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
184
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
 
159
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), 
185
160
                               self._missing_import)
186
161
        msg = tree.log_message()
187
162
        msg["summary"] = "I am importing now"
192
167
            pybaz.Version(self._missing_ancestor))
193
168
 
194
169
        # make an import for testing history-reuse logic.
 
170
        def collect(text):
 
171
            pass
195
172
        # note the use of a namespace layout here.
196
 
        self._missing_import_imported = os.path.join(self._tmpdir,
 
173
        self._missing_import_imported = os.path.join(self._tmpdir, 
197
174
                                                     'archivegone-bzr')
198
175
        os.mkdir(os.path.join(self._tmpdir, 'archivegone-bzr'))
199
176
        os.mkdir(os.path.join(self._tmpdir, 'archivegone-bzr', 'c'))
200
 
        import_version(os.path.join(self._tmpdir, 'archivegone-bzr',
 
177
        import_version(os.path.join(self._tmpdir, 'archivegone-bzr', 
201
178
                                    'c', 'import'),
202
 
                       pybaz.Version(self._missing_import), None)
 
179
                       pybaz.Version(self._missing_import),
 
180
                       collect)
203
181
        # and make it inaccessible
204
182
        pybaz.Archive('demo-gone@DONOTUSE').unregister()
205
183
 
206
184
    def make_inbranch_continuation(self):
207
185
        self._inbranch_tag = 'demo@DONOTUSE/c--inbranch-tag--0'
208
186
        self._inbranch_tag_base = self._inbranch_tag + '--base-0'
209
 
        self._inbranch_tag_base_bzr = revision_id(self._inbranch_tag_base,
210
 
                                                  None)
 
187
        self._inbranch_tag_base_bzr = revision_id(self._inbranch_tag_base)
211
188
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
212
189
            pybaz.Version(self._inbranch_tag))
213
190
        self._inbranch_tag_head = self._inbranch_tag + '--patch-1'
214
 
        self._inbranch_tag_head_bzr = revision_id(self._inbranch_tag_head,
215
 
                                                  None)
 
191
        self._inbranch_tag_head_bzr = revision_id(self._inbranch_tag_head)
216
192
        pybaz.Revision(self._inbranch_tag_base).make_continuation(
217
193
            pybaz.Version(self._inbranch_tag))
218
194
 
233
209
        TestCaseInTempDir.setUp(self)
234
210
        ResourcedTestCase.setUpResources(self)
235
211
        os.environ['HOME'] = self._baz._homedir
 
212
        self.output = StringIO()
236
213
 
237
214
    def tearDown(self):
238
215
        ResourcedTestCase.tearDownResources(self)
239
216
        TestCaseInTempDir.tearDown(self)
240
 
 
241
 
    def test_import_utf8(self):
242
 
        import_version('output', pybaz.Version(self._baz._utf8), None)
243
 
        branch = Branch.open('output')
244
 
        plain_revid = 'Arch-1:demo@DONOTUSE%c--utf8--0--base-0'
245
 
        self.assertEqual([plain_revid], branch.revision_history())
246
 
        self.assertEqual(u'I am importing now\ufffd\ufffd\ufffd',
247
 
                         branch.repository.get_revision(plain_revid).message)
248
 
        import_version('output2', pybaz.Version(self._baz._utf8), 'utf-8')
249
 
        branch2 = Branch.open('output2')
250
 
        utf8_revid = 'Arch-1-utf-8:demo@DONOTUSE%c--utf8--0--base-0'
251
 
        self.assertEqual([utf8_revid], branch2.revision_history())
252
 
        self.assertEqual(u'I am importing now\u1234',
253
 
                         branch2.repository.get_revision(utf8_revid).message)
254
 
 
 
217
 
 
218
    def collect(self, text):
 
219
        self.output.write(text)
 
220
        self.output.write("\n")
 
221
 
255
222
    def test_import_empty(self):
256
 
        import_version('output', pybaz.Version(self._baz._import), None)
 
223
        import_version('output', pybaz.Version(self._baz._import), 
 
224
                       self.collect)
257
225
        # expected results:
258
 
        # one commit, no files, revision identifier of
 
226
        # one commit, no files, revision identifier of 
259
227
        # 'demo@DONOTUSE_c--import--0--base-0'
260
228
        branch = Branch.open('output')
261
229
        repo = branch.repository
264
232
        rev = repo.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
265
233
        # and again.
266
234
        import_version('output2', pybaz.Version('demo@DONOTUSE/c--import--0'),
267
 
                       None)
 
235
                       self.collect)
268
236
        branch2 = Branch.open('output2')
269
237
        repo2 = branch2.repository
270
238
        self.assertEqual(branch.revision_history(), branch2.revision_history())
279
247
                         "Arch-1:demo@DONOTUSE%c--import--0--base-0")
280
248
 
281
249
    def test_empty_tagged(self):
282
 
        import_version('output', pybaz.Version(self._baz._empty_tag), None)
 
250
        import_version('output', pybaz.Version(self._baz._empty_tag),
 
251
                       self.collect)
283
252
        # expected results:
284
 
        # two commits, no files, revision identifiers of
 
253
        # two commits, no files, revision identifiers of 
285
254
        # 'demo@DONOTUSE_c--import--0--base-0' and
286
255
        # self._baz._empty_tag_bzr
287
256
        branch = Branch.open('output')
290
259
                          self._baz._empty_tag_bzr])
291
260
        rev = branch.repository.get_revision(self._baz._empty_tag_bzr)
292
261
        # and again.
293
 
        import_version('output2', pybaz.Version(self._baz._empty_tag), None)
 
262
        import_version('output2', pybaz.Version(self._baz._empty_tag),
 
263
                       self.collect)
294
264
        branch2 = Branch.open('output2')
295
265
        self.assertEqual(branch.revision_history(), branch2.revision_history())
296
266
        rev2 = branch2.repository.get_revision(self._baz._empty_tag_bzr)
299
269
 
300
270
        # and we should get some expected values:
301
271
        self.assertEqual(rev.committer, "Test User<test@example.org>")
302
 
        self.assertEqual(rev.message,
 
272
        self.assertEqual(rev.message, 
303
273
                         "tag of demo@DONOTUSE/c--import--0--base-0")
304
274
        self.assertEqual(rev.revision_id, self._baz._empty_tag_bzr)
305
275
 
306
276
    def test_empty_merged_tagged(self):
307
277
        import_version('output', pybaz.Version(self._baz._empty_merged_tag),
308
 
                       None)
 
278
                       self.collect)
309
279
        # expected results:
310
 
        # two commits, no files, revision identifiers of
 
280
        # two commits, no files, revision identifiers of 
311
281
        # 'demo@DONOTUSE_c--import--0--base-0' and
312
282
        # self._baz._empty_merged_tag_bzr_base
313
283
        # self._baz._empty_merged_tag_bzr
321
291
                          self._baz._empty_merged_tag_bzr])
322
292
        # and again.
323
293
        import_version('output2', pybaz.Version(self._baz._empty_merged_tag),
324
 
                       None)
 
294
                       self.collect)
325
295
        branch2 = Branch.open('output2')
326
296
        repo2 = branch2.repository
327
297
        # and import what we should be merged up against for checking with.
328
 
        import_version('output3', pybaz.Version(self._baz._empty_tag), None)
 
298
        import_version('output3', pybaz.Version(self._baz._empty_tag),
 
299
                       self.collect)
329
300
        branch3 = Branch.open('output3')
330
 
 
 
301
        
331
302
        self.assertEqual(branch.revision_history(), branch2.revision_history())
332
 
        self.assertNotEqual(branch.revision_history(),
 
303
        self.assertNotEqual(branch.revision_history(), 
333
304
                            branch3.revision_history())
334
305
        # check revisions in the history.
335
306
        rev = repo.get_revision(self._baz._empty_merged_tag_bzr_base)
338
309
        self.assertEqual(rev, rev2)
339
310
        # and we should get some expected values:
340
311
        self.assertEqual(rev.committer, "Test User<test@example.org>")
341
 
        self.assertEqual(rev.message,
 
312
        self.assertEqual(rev.message, 
342
313
                         "tag of demo@DONOTUSE/c--import--0--base-0")
343
314
        self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_bzr_base)
344
315
        self.assertEqual(['Arch-1:demo@DONOTUSE%c--import--0--base-0'],
357
328
                         self._baz._empty_merged_tag_bzr_base)
358
329
        self.assertEqual(rev.parent_ids[1], self._baz._empty_tag_bzr)
359
330
 
360
 
        # this tree should have nothing missing from that tree.
 
331
        # this tree should have nothing missing from that tree.   
361
332
        # FIXME there is no code for this right now.
362
333
        # self.assertEqual(branch.missing_revisions(branch3), [])
363
 
 
 
334
        
364
335
    def test_merge_branch_with_merges(self):
365
336
        import_version('output', pybaz.Version(self._baz._empty_merged_tag_2),
366
 
                       None)
 
337
                       self.collect)
367
338
        # expected results:
368
 
        # two commits, no files, revision identifiers of
 
339
        # two commits, no files, revision identifiers of 
369
340
        # 'demo@DONOTUSE_c--import--0--base-0' and
370
341
        # self._baz._empty_merged_tag_2_bzr_base
371
342
        # self._baz._empty_merged_tag_2_bzr
379
350
                          self._baz._empty_merged_tag_2_bzr])
380
351
        # and again.
381
352
        import_version('output2', pybaz.Version(self._baz._empty_merged_tag_2),
382
 
                       None)
 
353
                       self.collect)
383
354
        branch2 = Branch.open('output2')
384
355
        repo2 = branch2.repository
385
356
        # and import what we should be merged up against for checking with.
386
357
        import_version('output3', pybaz.Version(self._baz._empty_merged_tag),
387
 
                       None)
 
358
                       self.collect)
388
359
        branch3 = Branch.open('output3')
389
 
 
 
360
        
390
361
        self.assertEqual(branch.revision_history(), branch2.revision_history())
391
 
        self.assertNotEqual(branch.revision_history(),
 
362
        self.assertNotEqual(branch.revision_history(), 
392
363
                            branch3.revision_history())
393
364
        # check revisions in the history.
394
365
        rev = repo.get_revision(self._baz._empty_merged_tag_2_bzr_base)
397
368
        self.assertEqual(rev, rev2)
398
369
        # and we should get some expected values:
399
370
        self.assertEqual(rev.committer, "Test User<test@example.org>")
400
 
        self.assertEqual(rev.message,
 
371
        self.assertEqual(rev.message, 
401
372
                         "tag of demo@DONOTUSE/c--import--0--base-0")
402
 
        self.assertEqual(rev.revision_id,
 
373
        self.assertEqual(rev.revision_id, 
403
374
                         self._baz._empty_merged_tag_2_bzr_base)
404
375
 
405
376
        # check next revisions in the history.
416
387
        self.assertEqual(rev.parent_ids[1],
417
388
                         self._baz._empty_merged_tag_bzr)
418
389
 
419
 
        # this tree should have nothing missing from that tree.
 
390
        # this tree should have nothing missing from that tree.   
420
391
        # FIXME there is no code for this right now.
421
392
        # self.assertEqual(branch.missing_revisions(branch3), [])
422
 
 
 
393
        
423
394
    def test_import_symlink(self):
424
395
        import_version('output', pybaz.Version(self._baz._import_symlink),
425
 
                       None, max_count=1)
 
396
                       self.collect)
426
397
        # expected results:
427
 
        # two commits, no files, revision identifier of
 
398
        # one commit, no files, revision identifier of 
428
399
        # 'demo@DONOTUSE_c--import--0--base-0'
429
400
        branch = Branch.open('output')
430
401
        self.assertEqual(branch.revision_history(),
432
403
        rev = branch.repository.get_revision(self._baz._import_symlink_bzr)
433
404
        # and again.
434
405
        import_version('output2', pybaz.Version(self._baz._import_symlink),
435
 
                       None, max_count=1)
 
406
                       self.collect)
436
407
        branch2 = Branch.open('output2')
437
408
        self.assertEqual(branch.revision_history(), branch2.revision_history())
438
409
        rev2 = branch2.repository.get_revision(self._baz._import_symlink_bzr)
451
422
        self.assertEqual(entry.kind, 'symlink')
452
423
        self.assertEqual(entry.symlink_target, 'missing-file-name')
453
424
 
454
 
        # Test kind change for import
455
 
        import_version('output3', pybaz.Version(self._baz._import_symlink),
456
 
                       None)
457
 
 
458
425
    def test_missing_ancestor(self):
459
426
        import_version('output', pybaz.Version(self._baz._missing_ancestor),
460
 
                       None)
 
427
                       self.collect)
461
428
        # expected results:
462
 
        # one commits, no files, revision identifiers of
 
429
        # one commits, no files, revision identifiers of 
463
430
        # 'demo@DONOTUSE_c--gone--0--base-0' and
464
431
        # a merge of demo-gone@DONOTUSE%c--import--0
465
432
        branch = Branch.open('output')
468
435
        rev = branch.repository.get_revision(self._baz._missing_ancestor_bzr)
469
436
        # and again.
470
437
        import_version('output2', pybaz.Version(self._baz._missing_ancestor),
471
 
                       None)
 
438
                       self.collect)
472
439
        branch2 = Branch.open('output2')
473
440
        self.assertEqual(branch.revision_history(), branch2.revision_history())
474
441
        rev2 = branch2.repository.get_revision(self._baz._missing_ancestor_bzr)
477
444
 
478
445
        # and we should get some expected values:
479
446
        self.assertEqual(rev.committer, "Test User<test@example.org>")
480
 
        self.assertEqual(rev.message,
 
447
        self.assertEqual(rev.message, 
481
448
                         "tag of demo-gone@DONOTUSE/c--import--0--base-0")
482
449
        self.assertEqual(rev.revision_id, self._baz._missing_ancestor_bzr)
483
450
        self.assertEqual(rev.parent_ids[0], self._baz._missing_import_bzr)
484
451
        self.assertEqual(1, len(rev.parent_ids))
485
452
 
486
453
        # must NOT be able to get the merged evision
487
 
        self.assertRaises(NoSuchRevision, branch.repository.get_revision,
 
454
        self.assertRaises(NoSuchRevision, branch.repository.get_revision, 
488
455
                          self._baz._missing_import_bzr)
489
456
 
490
457
    def test_missing_ancestor_reusing_history(self):
491
458
        import_version('output', pybaz.Version(self._baz._missing_ancestor),
492
 
                       None,
 
459
                       self.collect,
493
460
                       reuse_history_from=[self._baz._missing_import_imported])
494
461
        # expected results:
495
 
        # one commits, no files, revision identifiers of
496
 
        # 'demo-gone@DONOTUSE%c--import--0--base-0' and
 
462
        # one commits, no files, revision identifiers of 
 
463
        # 'demo-gone@DONOTUSE%c--import--0--base-0' and 
497
464
        # 'demo@DONOTUSE%c--gone--0--base-0'
498
465
        branch = Branch.open('output')
499
466
        self.assertEqual(branch.revision_history(),
502
469
        rev = branch.repository.get_revision(self._baz._missing_ancestor_bzr)
503
470
        # and again.
504
471
        import_version('output2', pybaz.Version(self._baz._missing_ancestor),
505
 
                       None,
 
472
                       self.collect,
506
473
                       reuse_history_from=[self._baz._missing_import_imported])
507
474
        branch2 = Branch.open('output2')
508
475
        self.assertEqual(branch.revision_history(), branch2.revision_history())
515
482
 
516
483
        # and we should get some expected values:
517
484
        self.assertEqual(rev.committer, "Test User<test@example.org>")
518
 
        self.assertEqual(rev.message,
 
485
        self.assertEqual(rev.message, 
519
486
                         "tag of demo-gone@DONOTUSE/c--import--0--base-0")
520
487
        self.assertEqual(rev.revision_id, self._baz._missing_ancestor_bzr)
521
488
        self.assertEqual(rev.parent_ids[0], self._baz._missing_import_bzr)
522
489
        self.assertEqual(1, len(rev.parent_ids))
523
490
 
524
491
    def test_bad_file_id(self):
525
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag), None)
 
492
        import_version('output', pybaz.Version(self._baz._bad_id_tag),
 
493
                       self.collect)
526
494
        # expected results:
527
 
        # three commits, one files, revision identifiers of
 
495
        # three commits, one files, revision identifiers of 
528
496
        # 'demo@DONOTUSE_c--import--0--base-0' ,
529
497
        # 'demo@DONOTUSE/c--bad-id--0--base-0' ,
530
498
        # ''demo@DONOTUSE/c--bad-id--0--patch-1'
539
507
        self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
540
508
 
541
509
    def test_appending_revisions_already_present(self):
542
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag), None,
543
 
                       max_count=2)
 
510
        import_version('output', pybaz.Version(self._baz._bad_id_tag),
 
511
                       self.collect, max_count=2)
544
512
        # expected results:
545
 
        # three commits, one files, revision identifiers of
 
513
        # three commits, one files, revision identifiers of 
546
514
        # 'demo@DONOTUSE_c--import--0--base-0' ,
547
515
        # 'demo@DONOTUSE/c--bad-id--0--base-0' ,
548
516
        # ''demo@DONOTUSE/c--bad-id--0--patch-1'
557
525
        self.assertEqual(branch.revision_history(),
558
526
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
559
527
        del branch
560
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag), None)
 
528
        import_version('output', pybaz.Version(self._baz._bad_id_tag),
 
529
                       self.collect)
561
530
        branch = Branch.open('output')
562
531
        self.assertEqual(branch.revision_history(),
563
532
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
569
538
        self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
570
539
 
571
540
    def test_appending_revisions_all_already_present(self):
572
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag), None)
 
541
        import_version('output', pybaz.Version(self._baz._bad_id_tag),
 
542
                       self.collect)
573
543
        # expected results:
574
 
        # three commits, one files, revision identifiers of
 
544
        # three commits, one files, revision identifiers of 
575
545
        # 'demo@DONOTUSE_c--import--0--base-0' ,
576
546
        # 'demo@DONOTUSE/c--bad-id--0--base-0' ,
577
547
        # ''demo@DONOTUSE/c--bad-id--0--patch-1'
587
557
        self.assertEqual(branch.revision_history(),
588
558
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
589
559
        del branch
590
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag), None)
 
560
        import_version('output', pybaz.Version(self._baz._bad_id_tag),
 
561
                       self.collect)
591
562
        branch = Branch.open('output')
592
563
        self.assertEqual(branch.revision_history(),
593
564
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
599
570
        self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
600
571
 
601
572
    def test_inbranch_conversion(self):
602
 
        import_version('output', pybaz.Version(self._baz._inbranch_tag), None)
 
573
        import_version('output', pybaz.Version(self._baz._inbranch_tag),
 
574
                       self.collect)
603
575
        # expected results:
604
 
        # three commits, no files, revision identifiers of
 
576
        # three commits, no files, revision identifiers of 
605
577
        # 'demo@DONOTUSE_c--import--0--base-0' and
606
578
        # self._baz._inbranch_tag_base_bzr
607
579
        # self._baz._inbranch_tag_head_bzr
612
584
                          self._baz._inbranch_tag_base_bzr,
613
585
                          self._baz._inbranch_tag_head_bzr])
614
586
        # and again.
615
 
        import_version('output2', pybaz.Version(self._baz._inbranch_tag), None)
 
587
        import_version('output2', pybaz.Version(self._baz._inbranch_tag),
 
588
                       self.collect)
616
589
        branch2 = Branch.open('output2')
617
 
 
 
590
        
618
591
        self.assertEqual(branch.revision_history(), branch2.revision_history())
619
592
        # check revisions in the history.
620
593
        rev = branch.repository.get_revision(self._baz._inbranch_tag_base_bzr)
638
611
        self.assertEqual(rev.parent_ids,
639
612
                         [self._baz._inbranch_tag_base_bzr])
640
613
 
641
 
    def test_no_commits_same_as_missing(self):
642
 
        path = 'output'
643
 
        os.mkdir(path)
644
 
        branch = bzrlib.bzrdir.BzrDir.create_branch_convenience(path)
645
 
        import_version(path, pybaz.Version(self._baz._import), None)
646
 
        # expected results:
647
 
        # one commit, revision identifier of
648
 
        # 'demo@DONOTUSE_c--import--0--base-0'
649
 
        self.assertEqual(branch.revision_history(),
650
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
651
 
 
652
614
 
653
615
class TestNamespacePrevious(TestCase):
654
616
 
662
624
    def test_patch1_base0(self):
663
625
        self.assertEqual(namespace_previous(self.version['patch-1']),
664
626
                         self.version['base-0'])
665
 
 
 
627
        
666
628
    def test_patch3000_patch2999(self):
667
629
        self.assertEqual(namespace_previous(self.version['patch-3000']),
668
630
                         self.version['patch-2999'])
669
 
 
 
631
        
670
632
    def test_version0_raises(self):
671
633
        self.assertRaises(RuntimeError, namespace_previous,
672
634
                          self.version['version-0'])
679
641
        self.assertEqual(namespace_previous(self.version['versionfix-3000']),
680
642
                         self.version['versionfix-2999'])
681
643
 
682
 
 
683
644
class TestNamespaceMapping(TestCase):
684
645
 
685
646
    def test_namespace_mapping_branch(self):
693
654
        from bzrlib.plugins.bzrtools.baz_import import map_namespace
694
655
        category = pybaz.Category('foo@example.com/c')
695
656
        self.assertRaises(pybaz.errors.NamespaceError, map_namespace, category)
696
 
        self.assertEqual('c/+trunk',
 
657
        self.assertEqual('c/+trunk', 
697
658
                         map_namespace(pybaz.Version("%s--0" % category)))
698
659
        self.assertEqual('c/0.1/+trunk',
699
660
                         map_namespace(pybaz.Version('%s--0.1' % category)))
702
663
class TestFileIdMapping(TestCase):
703
664
 
704
665
    def test_slash(self):
 
666
        from bzrlib.plugins.bzrtools.baz_import import map_file_id
705
667
        self.assertEqual('c%2fc', map_file_id('c/c'))
706
668
        self.assertEqual('c%25c', map_file_id('c%c'))
707
669
 
726
688
    def make_import(self, namespace):
727
689
        self._import = 'demo@DONOTUSE/%s' % namespace
728
690
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
729
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
 
691
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), 
730
692
                               self._import)
731
693
        msg = tree.log_message()
732
694
        msg["summary"] = "I am importing now"
734
696
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
735
697
 
736
698
    def test_cmd_exists(self):
737
 
        from bzrlib.plugins.bzrtools import cmd_baz_import
 
699
        from bzrlib.plugins.bzrtools.baz_import import cmd_baz_import
738
700
 
739
701
    def test_empty_archive(self):
740
702
        command = cmd_baz_import()
741
703
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
742
704
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output')))
743
705
        walk_len = len(list(os.walk(os.path.join(self._tmpdir,'output'))))
744
 
        self.assertEqual(7, walk_len)
 
706
        self.assertEqual(6, walk_len)
745
707
 
746
708
    def test_two_branches(self):
747
709
        self.make_import('c--0')
749
711
        command = cmd_baz_import()
750
712
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
751
713
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output')))
752
 
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output',
 
714
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output', 
753
715
                                       'c','+trunk')))
754
 
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output',
 
716
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output', 
755
717
                                                    'c1', '0.2','branch')))
756
 
        default_format = repository.RepositoryFormat.get_default_format()
757
 
        if getattr(default_format, 'rich_root_data', False):
758
 
            num_files = 21
759
 
        else:
760
 
            num_files = 20
761
718
        walk_len = len(list(os.walk(os.path.join(self._tmpdir,'output'))))
762
 
        self.assertEqual(num_files, walk_len)
 
719
        self.assertEqual(17, walk_len)
763
720
 
764
721
    def test_run_twice(self):
765
722
        self.make_import('c--0')
766
723
        command = cmd_baz_import()
767
724
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
768
725
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
769
 
 
 
726
        
770
727
    def test_accepts_reuse_history(self):
771
728
        self.make_import('c--0')
772
729
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
773
730
                     'demo@DONOTUSE', '.', '.')
774
 
 
775
 
    def test_does_not_need_reuse_history(self):
776
 
        self.make_import('c--0')
777
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
778
 
                     'demo@DONOTUSE')
779
 
 
780
 
    def test_does_not_need_reuse_history(self):
781
 
        self.make_import('c--0')
782
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
783
 
                     'demo@DONOTUSE')
784
 
 
785
 
    def test_encoding_flag(self):
786
 
        self.make_import('c--0')
787
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
788
 
                     'demo@DONOTUSE')
789
 
        self.assertEqual(['Arch-1:demo@DONOTUSE%c--0--base-0'],
790
 
                         Branch.open(os.path.join(self._tmpdir,
791
 
                                     'output/c/+trunk')).revision_history())
792
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output2'),
793
 
                     'demo@DONOTUSE', '--encoding', 'utf-8')
794
 
        self.assertEqual(['Arch-1-utf-8:demo@DONOTUSE%c--0--base-0'],
795
 
                         Branch.open(os.path.join(self._tmpdir,
796
 
                                     'output2/c/+trunk')).revision_history())
797
 
        self.run_bzr('baz-import-branch', os.path.join(self._tmpdir, 'output3'),
798
 
                     'demo@DONOTUSE/c--0')
799
 
        self.assertEqual(['Arch-1:demo@DONOTUSE%c--0--base-0'],
800
 
                         Branch.open(os.path.join(self._tmpdir,
801
 
                                     'output3')).revision_history())
802
 
        self.run_bzr('baz-import-branch', os.path.join(self._tmpdir,
803
 
                      'output4'), 'demo@DONOTUSE/c--0', '--encoding', 'utf-8')
804
 
        self.assertEqual(['Arch-1-utf-8:demo@DONOTUSE%c--0--base-0'],
805
 
                         Branch.open(os.path.join(self._tmpdir,
806
 
                                     'output4')).revision_history())
 
731
        
 
732
    def test_does_not_need_reuse_history(self):
 
733
        self.make_import('c--0')
 
734
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
 
735
                     'demo@DONOTUSE')