~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/test_baz_import.py

  • Committer: Jelmer Vernooij
  • Date: 2011-06-24 13:35:53 UTC
  • mto: This revision was merged to the branch mainline in revision 771.
  • Revision ID: jelmer@samba.org-20110624133553-h9s8syvvvsvpd1o9
Add support for importing .tar.xz and .tar.lzma files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Limited
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
3
 
#
4
 
#    This program is free software; you can redistribute it and/or modify
5
 
#    it under the terms of the GNU General Public License as published by
6
 
#    the Free Software Foundation; either version 2 of the License, or
7
 
#    (at your option) any later version.
8
 
#
9
 
#    This program is distributed in the hope that it will be useful,
10
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
#    GNU General Public License for more details.
13
 
#
14
 
#    You should have received a copy of the GNU General Public License
15
 
#    along with this program; if not, write to the Free Software
16
 
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 
18
 
from bzrlib.tests import TestCaseInTempDir, TestCase
19
 
from bzrlib.osutils import has_symlinks
20
 
try:
21
 
    import pybaz
22
 
except ImportError:
23
 
    pybaz = None
24
 
import os
25
 
try:
26
 
    from bzrtools.baz_import import (import_version, revision_id,
27
 
                                     cmd_baz_import, make_archive, map_file_id)
28
 
except ImportError:
29
 
    from bzrlib.plugins.bzrtools.baz_import import (import_version, 
30
 
                                                    revision_id,
31
 
                                                    cmd_baz_import,
32
 
                                                    make_archive,
33
 
                                                    map_file_id)
34
 
import shutil
35
 
from StringIO import StringIO
36
 
import tempfile
37
 
from testresources import (TestResource, TestLoader, OptimisingTestSuite,
38
 
                               ResourcedTestCase)
39
 
    
40
 
import bzrlib
41
 
from bzrlib.errors import NoSuchRevision
42
 
try:
43
 
    from bzrtools.fai import namespace_previous
44
 
except ImportError:
45
 
    from bzrlib.plugins.bzrtools.fai import namespace_previous
46
 
from bzrlib.branch import Branch
47
 
 
48
 
def test_suite():
49
 
    if pybaz is None:
50
 
        from unittest import TestSuite
51
 
        return TestSuite()
52
 
    return TestLoader().loadTestsFromName(__name__)
53
 
 
54
 
 
55
 
class BazTreeResource(TestResource):
56
 
 
57
 
    def cleanUp(self):
58
 
        os.environ['HOME'] = self._oldhome
59
 
        shutil.rmtree(self._tmpdir)
60
 
 
61
 
    def __init__(self):
62
 
        self._tmpdir = tempfile.mkdtemp()
63
 
        self._homedir = os.path.join(self._tmpdir, 'home')
64
 
        self._oldhome = os.environ['HOME']
65
 
        os.mkdir(self._homedir)
66
 
        os.environ['HOME'] = self._homedir
67
 
 
68
 
        self._archiveroot = os.path.join(self._tmpdir, 'archive')
69
 
        self._archive = make_archive('demo@DONOTUSE', str(self._archiveroot))
70
 
        pybaz.set_my_id("Test User<test@example.org>")
71
 
 
72
 
        self.make_empty_import()
73
 
 
74
 
        self._empty_tag = 'demo@DONOTUSE/c--empty-tag--0'
75
 
        self._empty_tag_bzr = revision_id(self._empty_tag + '--base-0')
76
 
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
77
 
            pybaz.Version(self._empty_tag))
78
 
 
79
 
        self._empty_merged_tag = 'demo@DONOTUSE/c--empty-merged-tag--0'
80
 
        self._empty_merged_tag_bzr_base = revision_id(self._empty_merged_tag 
81
 
                                                 + '--base-0')
82
 
        self._empty_merged_tag_bzr = revision_id(self._empty_merged_tag 
83
 
                                                 + '--patch-1')
84
 
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
85
 
            pybaz.Version(self._empty_merged_tag))
86
 
        tree = pybaz.Revision(self._empty_merged_tag + '--base-0').get(
87
 
            os.path.join(self._tmpdir, 'tree'))
88
 
        tree.star_merge(self._empty_tag,
89
 
                        pybaz.Version('demo@DONOTUSE/c--import--0'))
90
 
        msg = tree.log_message()
91
 
        msg["summary"]="did a merge, yarh"
92
 
        tree.commit(msg)
93
 
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
94
 
        
95
 
        # tree, two commits, includes merge of other branch
96
 
        self._empty_merged_tag_2 = 'demo@DONOTUSE/c--empty-tag-2--0'
97
 
        self._empty_merged_tag_2_bzr_base = revision_id(
98
 
            self._empty_merged_tag_2 + '--base-0')
99
 
        self._empty_merged_tag_2_bzr = revision_id(
100
 
            self._empty_merged_tag_2 + '--patch-1')
101
 
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
102
 
            pybaz.Version(self._empty_merged_tag_2))
103
 
        tree = pybaz.Revision(self._empty_merged_tag_2 + '--base-0').get (
104
 
            os.path.join(self._tmpdir, 'tree'))
105
 
        tree.star_merge(self._empty_merged_tag,
106
 
                        pybaz.Version('demo@DONOTUSE/c--import--0'))
107
 
        msg = tree.log_message()
108
 
        msg["summary"] = "merge in a merged tree."
109
 
        tree.commit(msg)
110
 
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
111
 
 
112
 
        self._bad_id_tag = 'demo@DONOTUSE/c--bad-id--0'
113
 
        self._bad_id_tag_bzr_base = revision_id(self._bad_id_tag + '--base-0')
114
 
        self._bad_id_tag_bzr = revision_id(self._bad_id_tag + '--patch-1')
115
 
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
116
 
            pybaz.Version(self._bad_id_tag))
117
 
        tree = pybaz.Revision(self._bad_id_tag + '--base-0').get(
118
 
            os.path.join(self._tmpdir, 'tree'))
119
 
        from bzrlib.plugins.bzrtools.baz_import import add_file
120
 
        add_file(os.path.join(self._tmpdir,'tree/path'), 'text', 
121
 
                 'this_id/needs%escaping')
122
 
        msg = tree.log_message()
123
 
        msg["summary"] = "commit something which needs escaping."
124
 
        tree.commit(msg)
125
 
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
126
 
 
127
 
        self.make_import_symlink()
128
 
        self.make_missing_ancestor()
129
 
        self.make_inbranch_continuation()
130
 
 
131
 
    def make_import_symlink(self):
132
 
        self._import_symlink = 'demo@DONOTUSE/c--import-symlink--0'
133
 
        self._import_symlink_bzr = revision_id(
134
 
            self._import_symlink + '--base-0')
135
 
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
136
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
137
 
                               self._import_symlink)
138
 
        os.symlink('missing-file-name',
139
 
                   os.path.join(self._tmpdir, 'tree', 'alink'))
140
 
        tree.add_tag('alink')
141
 
        id_file = open(os.path.join(tree, '.arch-ids', 'alink.id'), 'w')
142
 
        id_file.write('symlink_tag\n')
143
 
        id_file.close()
144
 
        msg = tree.log_message()
145
 
        msg["summary"] = "Import with a symlink"
146
 
        tree.import_(msg)
147
 
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
148
 
 
149
 
    def make_empty_import(self):
150
 
        self._import = 'demo@DONOTUSE/c--import--0'
151
 
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
152
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), 
153
 
                               self._import)
154
 
        msg = tree.log_message()
155
 
        msg["summary"] = "I am importing now"
156
 
        tree.import_(msg)
157
 
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
158
 
 
159
 
    def make_missing_ancestor(self):
160
 
        self._archivegoneroot = os.path.join(self._tmpdir, 'archivegone')
161
 
        self._archive = make_archive('demo-gone@DONOTUSE',
162
 
                                     str(self._archivegoneroot))
163
 
        self._missing_import = 'demo-gone@DONOTUSE/c--import--0'
164
 
        self._missing_import_bzr = revision_id(self._missing_import 
165
 
                                                 + '--base-0')
166
 
        self._missing_ancestor = 'demo@DONOTUSE/c--gone--0'
167
 
        self._missing_ancestor_bzr = revision_id(self._missing_ancestor 
168
 
                                                 + '--base-0')
169
 
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
170
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), 
171
 
                               self._missing_import)
172
 
        msg = tree.log_message()
173
 
        msg["summary"] = "I am importing now"
174
 
        tree.import_(msg)
175
 
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
176
 
        # tag into the kept archive
177
 
        pybaz.Revision(self._missing_import + '--base-0').make_continuation(
178
 
            pybaz.Version(self._missing_ancestor))
179
 
 
180
 
        # make an import for testing history-reuse logic.
181
 
        # note the use of a namespace layout here.
182
 
        self._missing_import_imported = os.path.join(self._tmpdir, 
183
 
                                                     'archivegone-bzr')
184
 
        os.mkdir(os.path.join(self._tmpdir, 'archivegone-bzr'))
185
 
        os.mkdir(os.path.join(self._tmpdir, 'archivegone-bzr', 'c'))
186
 
        import_version(os.path.join(self._tmpdir, 'archivegone-bzr', 
187
 
                                    'c', 'import'),
188
 
                       pybaz.Version(self._missing_import))
189
 
        # and make it inaccessible
190
 
        pybaz.Archive('demo-gone@DONOTUSE').unregister()
191
 
 
192
 
    def make_inbranch_continuation(self):
193
 
        self._inbranch_tag = 'demo@DONOTUSE/c--inbranch-tag--0'
194
 
        self._inbranch_tag_base = self._inbranch_tag + '--base-0'
195
 
        self._inbranch_tag_base_bzr = revision_id(self._inbranch_tag_base)
196
 
        pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
197
 
            pybaz.Version(self._inbranch_tag))
198
 
        self._inbranch_tag_head = self._inbranch_tag + '--patch-1'
199
 
        self._inbranch_tag_head_bzr = revision_id(self._inbranch_tag_head)
200
 
        pybaz.Revision(self._inbranch_tag_base).make_continuation(
201
 
            pybaz.Version(self._inbranch_tag))
202
 
 
203
 
    @classmethod
204
 
    def _makeResource(self):
205
 
        return BazTreeResource()
206
 
 
207
 
    @classmethod
208
 
    def _cleanResource(self, resource):
209
 
        resource.cleanUp()
210
 
 
211
 
 
212
 
class TestImportBranch(TestCaseInTempDir):
213
 
 
214
 
    _resources = [("_baz", BazTreeResource)]
215
 
 
216
 
    def setUp(self):
217
 
        TestCaseInTempDir.setUp(self)
218
 
        ResourcedTestCase.setUpResources(self)
219
 
        os.environ['HOME'] = self._baz._homedir
220
 
 
221
 
    def tearDown(self):
222
 
        ResourcedTestCase.tearDownResources(self)
223
 
        TestCaseInTempDir.tearDown(self)
224
 
 
225
 
    def test_import_empty(self):
226
 
        import_version('output', pybaz.Version(self._baz._import))
227
 
        # expected results:
228
 
        # one commit, no files, revision identifier of 
229
 
        # 'demo@DONOTUSE_c--import--0--base-0'
230
 
        branch = Branch.open('output')
231
 
        repo = branch.repository
232
 
        self.assertEqual(branch.revision_history(),
233
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
234
 
        rev = repo.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
235
 
        # and again.
236
 
        import_version('output2', pybaz.Version('demo@DONOTUSE/c--import--0'))
237
 
        branch2 = Branch.open('output2')
238
 
        repo2 = branch2.repository
239
 
        self.assertEqual(branch.revision_history(), branch2.revision_history())
240
 
        rev2 = repo2.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
241
 
        # they must be the same
242
 
        self.assertEqual(rev, rev2)
243
 
 
244
 
        # and we should get some expected values:
245
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
246
 
        self.assertEqual(rev.message, "I am importing now")
247
 
        self.assertEqual(rev.revision_id,
248
 
                         "Arch-1:demo@DONOTUSE%c--import--0--base-0")
249
 
 
250
 
    def test_empty_tagged(self):
251
 
        import_version('output', pybaz.Version(self._baz._empty_tag))
252
 
        # expected results:
253
 
        # two commits, no files, revision identifiers of 
254
 
        # 'demo@DONOTUSE_c--import--0--base-0' and
255
 
        # self._baz._empty_tag_bzr
256
 
        branch = Branch.open('output')
257
 
        self.assertEqual(branch.revision_history(),
258
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
259
 
                          self._baz._empty_tag_bzr])
260
 
        rev = branch.repository.get_revision(self._baz._empty_tag_bzr)
261
 
        # and again.
262
 
        import_version('output2', pybaz.Version(self._baz._empty_tag))
263
 
        branch2 = Branch.open('output2')
264
 
        self.assertEqual(branch.revision_history(), branch2.revision_history())
265
 
        rev2 = branch2.repository.get_revision(self._baz._empty_tag_bzr)
266
 
        # they must be the same
267
 
        self.assertEqual(rev, rev2)
268
 
 
269
 
        # and we should get some expected values:
270
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
271
 
        self.assertEqual(rev.message, 
272
 
                         "tag of demo@DONOTUSE/c--import--0--base-0")
273
 
        self.assertEqual(rev.revision_id, self._baz._empty_tag_bzr)
274
 
 
275
 
    def test_empty_merged_tagged(self):
276
 
        import_version('output', pybaz.Version(self._baz._empty_merged_tag))
277
 
        # expected results:
278
 
        # two commits, no files, revision identifiers of 
279
 
        # 'demo@DONOTUSE_c--import--0--base-0' and
280
 
        # self._baz._empty_merged_tag_bzr_base
281
 
        # self._baz._empty_merged_tag_bzr
282
 
        # and a merged revision from the latter of
283
 
        # self._baz._empty_tag_bzr
284
 
        branch = Branch.open('output')
285
 
        repo = branch.repository
286
 
        self.assertEqual(branch.revision_history(),
287
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
288
 
                          self._baz._empty_merged_tag_bzr_base,
289
 
                          self._baz._empty_merged_tag_bzr])
290
 
        # and again.
291
 
        import_version('output2', pybaz.Version(self._baz._empty_merged_tag))
292
 
        branch2 = Branch.open('output2')
293
 
        repo2 = branch2.repository
294
 
        # and import what we should be merged up against for checking with.
295
 
        import_version('output3', pybaz.Version(self._baz._empty_tag))
296
 
        branch3 = Branch.open('output3')
297
 
        
298
 
        self.assertEqual(branch.revision_history(), branch2.revision_history())
299
 
        self.assertNotEqual(branch.revision_history(), 
300
 
                            branch3.revision_history())
301
 
        # check revisions in the history.
302
 
        rev = repo.get_revision(self._baz._empty_merged_tag_bzr_base)
303
 
        rev2 = repo2.get_revision(self._baz._empty_merged_tag_bzr_base)
304
 
        # they must be the same
305
 
        self.assertEqual(rev, rev2)
306
 
        # and we should get some expected values:
307
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
308
 
        self.assertEqual(rev.message, 
309
 
                         "tag of demo@DONOTUSE/c--import--0--base-0")
310
 
        self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_bzr_base)
311
 
        self.assertEqual(['Arch-1:demo@DONOTUSE%c--import--0--base-0'],
312
 
                         rev.parent_ids)
313
 
 
314
 
        # check next revisions in the history.
315
 
        rev = repo.get_revision(self._baz._empty_merged_tag_bzr)
316
 
        rev2 = repo2.get_revision(self._baz._empty_merged_tag_bzr)
317
 
        # they must be the same
318
 
        self.assertEqual(rev, rev2)
319
 
        # and we should get some expected values:
320
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
321
 
        self.assertEqual(rev.message, "did a merge, yarh")
322
 
        self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_bzr)
323
 
        self.assertEqual(rev.parent_ids[0],
324
 
                         self._baz._empty_merged_tag_bzr_base)
325
 
        self.assertEqual(rev.parent_ids[1], self._baz._empty_tag_bzr)
326
 
 
327
 
        # this tree should have nothing missing from that tree.   
328
 
        # FIXME there is no code for this right now.
329
 
        # self.assertEqual(branch.missing_revisions(branch3), [])
330
 
        
331
 
    def test_merge_branch_with_merges(self):
332
 
        import_version('output', pybaz.Version(self._baz._empty_merged_tag_2))
333
 
        # expected results:
334
 
        # two commits, no files, revision identifiers of 
335
 
        # 'demo@DONOTUSE_c--import--0--base-0' and
336
 
        # self._baz._empty_merged_tag_2_bzr_base
337
 
        # self._baz._empty_merged_tag_2_bzr
338
 
        # and a merged revision from the latter of
339
 
        # self._baz._empty_merged_tag_bzr
340
 
        branch = Branch.open('output')
341
 
        repo = branch.repository
342
 
        self.assertEqual(branch.revision_history(),
343
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
344
 
                          self._baz._empty_merged_tag_2_bzr_base,
345
 
                          self._baz._empty_merged_tag_2_bzr])
346
 
        # and again.
347
 
        import_version('output2', pybaz.Version(self._baz._empty_merged_tag_2))
348
 
        branch2 = Branch.open('output2')
349
 
        repo2 = branch2.repository
350
 
        # and import what we should be merged up against for checking with.
351
 
        import_version('output3', pybaz.Version(self._baz._empty_merged_tag))
352
 
        branch3 = Branch.open('output3')
353
 
        
354
 
        self.assertEqual(branch.revision_history(), branch2.revision_history())
355
 
        self.assertNotEqual(branch.revision_history(), 
356
 
                            branch3.revision_history())
357
 
        # check revisions in the history.
358
 
        rev = repo.get_revision(self._baz._empty_merged_tag_2_bzr_base)
359
 
        rev2 = repo2.get_revision(self._baz._empty_merged_tag_2_bzr_base)
360
 
        # they must be the same
361
 
        self.assertEqual(rev, rev2)
362
 
        # and we should get some expected values:
363
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
364
 
        self.assertEqual(rev.message, 
365
 
                         "tag of demo@DONOTUSE/c--import--0--base-0")
366
 
        self.assertEqual(rev.revision_id, 
367
 
                         self._baz._empty_merged_tag_2_bzr_base)
368
 
 
369
 
        # check next revisions in the history.
370
 
        rev = repo.get_revision(self._baz._empty_merged_tag_2_bzr)
371
 
        rev2 = repo2.get_revision(self._baz._empty_merged_tag_2_bzr)
372
 
        # they must be the same
373
 
        self.assertEqual(rev, rev2)
374
 
        # and we should get some expected values:
375
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
376
 
        self.assertEqual(rev.message, "merge in a merged tree.")
377
 
        self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_2_bzr)
378
 
        self.assertEqual(rev.parent_ids[0],
379
 
                         self._baz._empty_merged_tag_2_bzr_base)
380
 
        self.assertEqual(rev.parent_ids[1],
381
 
                         self._baz._empty_merged_tag_bzr)
382
 
 
383
 
        # this tree should have nothing missing from that tree.   
384
 
        # FIXME there is no code for this right now.
385
 
        # self.assertEqual(branch.missing_revisions(branch3), [])
386
 
        
387
 
    def test_import_symlink(self):
388
 
        import_version('output', pybaz.Version(self._baz._import_symlink))
389
 
        # expected results:
390
 
        # one commit, no files, revision identifier of 
391
 
        # 'demo@DONOTUSE_c--import--0--base-0'
392
 
        branch = Branch.open('output')
393
 
        self.assertEqual(branch.revision_history(),
394
 
                         [self._baz._import_symlink_bzr])
395
 
        rev = branch.repository.get_revision(self._baz._import_symlink_bzr)
396
 
        # and again.
397
 
        import_version('output2', pybaz.Version(self._baz._import_symlink))
398
 
        branch2 = Branch.open('output2')
399
 
        self.assertEqual(branch.revision_history(), branch2.revision_history())
400
 
        rev2 = branch2.repository.get_revision(self._baz._import_symlink_bzr)
401
 
        # they must be the same
402
 
        self.assertEqual(rev, rev2)
403
 
 
404
 
        # and we should get some expected values:
405
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
406
 
        self.assertEqual(rev.message, "Import with a symlink")
407
 
        self.assertEqual(rev.revision_id, self._baz._import_symlink_bzr)
408
 
 
409
 
        # and we want the symlink alink with target 'missing-file-name'
410
 
        inv = branch.repository.get_inventory(rev.revision_id)
411
 
        self.assertEqual(inv.path2id('alink'), 'x_symlink_tag')
412
 
        entry = inv['x_symlink_tag']
413
 
        self.assertEqual(entry.kind, 'symlink')
414
 
        self.assertEqual(entry.symlink_target, 'missing-file-name')
415
 
 
416
 
    def test_missing_ancestor(self):
417
 
        import_version('output', pybaz.Version(self._baz._missing_ancestor))
418
 
        # expected results:
419
 
        # one commits, no files, revision identifiers of 
420
 
        # 'demo@DONOTUSE_c--gone--0--base-0' and
421
 
        # a merge of demo-gone@DONOTUSE%c--import--0
422
 
        branch = Branch.open('output')
423
 
        self.assertEqual(branch.revision_history(),
424
 
                         [self._baz._missing_ancestor_bzr])
425
 
        rev = branch.repository.get_revision(self._baz._missing_ancestor_bzr)
426
 
        # and again.
427
 
        import_version('output2', pybaz.Version(self._baz._missing_ancestor))
428
 
        branch2 = Branch.open('output2')
429
 
        self.assertEqual(branch.revision_history(), branch2.revision_history())
430
 
        rev2 = branch2.repository.get_revision(self._baz._missing_ancestor_bzr)
431
 
        # they must be the same
432
 
        self.assertEqual(rev, rev2)
433
 
 
434
 
        # and we should get some expected values:
435
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
436
 
        self.assertEqual(rev.message, 
437
 
                         "tag of demo-gone@DONOTUSE/c--import--0--base-0")
438
 
        self.assertEqual(rev.revision_id, self._baz._missing_ancestor_bzr)
439
 
        self.assertEqual(rev.parent_ids[0], self._baz._missing_import_bzr)
440
 
        self.assertEqual(1, len(rev.parent_ids))
441
 
 
442
 
        # must NOT be able to get the merged evision
443
 
        self.assertRaises(NoSuchRevision, branch.repository.get_revision, 
444
 
                          self._baz._missing_import_bzr)
445
 
 
446
 
    def test_missing_ancestor_reusing_history(self):
447
 
        import_version('output', pybaz.Version(self._baz._missing_ancestor),
448
 
                       reuse_history_from=[self._baz._missing_import_imported])
449
 
        # expected results:
450
 
        # one commits, no files, revision identifiers of 
451
 
        # 'demo-gone@DONOTUSE%c--import--0--base-0' and 
452
 
        # 'demo@DONOTUSE%c--gone--0--base-0'
453
 
        branch = Branch.open('output')
454
 
        self.assertEqual(branch.revision_history(),
455
 
                         [self._baz._missing_import_bzr,
456
 
                          self._baz._missing_ancestor_bzr])
457
 
        rev = branch.repository.get_revision(self._baz._missing_ancestor_bzr)
458
 
        # and again.
459
 
        import_version('output2', pybaz.Version(self._baz._missing_ancestor),
460
 
                       reuse_history_from=[self._baz._missing_import_imported])
461
 
        branch2 = Branch.open('output2')
462
 
        self.assertEqual(branch.revision_history(), branch2.revision_history())
463
 
        rev2 = branch2.repository.get_revision(self._baz._missing_ancestor_bzr)
464
 
        # they must be the same
465
 
        self.assertEqual(rev, rev2)
466
 
 
467
 
        # must be able to get the missing base revision
468
 
        branch.repository.get_revision(self._baz._missing_import_bzr)
469
 
 
470
 
        # and we should get some expected values:
471
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
472
 
        self.assertEqual(rev.message, 
473
 
                         "tag of demo-gone@DONOTUSE/c--import--0--base-0")
474
 
        self.assertEqual(rev.revision_id, self._baz._missing_ancestor_bzr)
475
 
        self.assertEqual(rev.parent_ids[0], self._baz._missing_import_bzr)
476
 
        self.assertEqual(1, len(rev.parent_ids))
477
 
 
478
 
    def test_bad_file_id(self):
479
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag))
480
 
        # expected results:
481
 
        # three commits, one files, revision identifiers of 
482
 
        # 'demo@DONOTUSE_c--import--0--base-0' ,
483
 
        # 'demo@DONOTUSE/c--bad-id--0--base-0' ,
484
 
        # ''demo@DONOTUSE/c--bad-id--0--patch-1'
485
 
        branch = Branch.open('output')
486
 
        self.assertEqual(branch.revision_history(),
487
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
488
 
                          self._baz._bad_id_tag_bzr_base,
489
 
                          self._baz._bad_id_tag_bzr])
490
 
        rev = branch.repository.get_revision(self._baz._bad_id_tag_bzr)
491
 
        inv = branch.repository.get_inventory(self._baz._bad_id_tag_bzr)
492
 
        self.assertEqual(inv.path2id('path'), 'x_this_id%2fneeds%25escaping')
493
 
        self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
494
 
 
495
 
    def test_appending_revisions_already_present(self):
496
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag),
497
 
                       max_count=2)
498
 
        # expected results:
499
 
        # three commits, one files, revision identifiers of 
500
 
        # 'demo@DONOTUSE_c--import--0--base-0' ,
501
 
        # 'demo@DONOTUSE/c--bad-id--0--base-0' ,
502
 
        # ''demo@DONOTUSE/c--bad-id--0--patch-1'
503
 
        branch = Branch.open('output')
504
 
        self.assertEqual(branch.revision_history(),
505
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
506
 
                          self._baz._bad_id_tag_bzr_base])
507
 
        branch.set_revision_history(
508
 
            ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
509
 
        del branch
510
 
        branch = Branch.open('output')
511
 
        self.assertEqual(branch.revision_history(),
512
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
513
 
        del branch
514
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag))
515
 
        branch = Branch.open('output')
516
 
        self.assertEqual(branch.revision_history(),
517
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
518
 
                          self._baz._bad_id_tag_bzr_base,
519
 
                          self._baz._bad_id_tag_bzr])
520
 
        rev = branch.repository.get_revision(self._baz._bad_id_tag_bzr)
521
 
        inv = branch.repository.get_inventory(self._baz._bad_id_tag_bzr)
522
 
        self.assertEqual(inv.path2id('path'), 'x_this_id%2fneeds%25escaping')
523
 
        self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
524
 
 
525
 
    def test_appending_revisions_all_already_present(self):
526
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag))
527
 
        # expected results:
528
 
        # three commits, one files, revision identifiers of 
529
 
        # 'demo@DONOTUSE_c--import--0--base-0' ,
530
 
        # 'demo@DONOTUSE/c--bad-id--0--base-0' ,
531
 
        # ''demo@DONOTUSE/c--bad-id--0--patch-1'
532
 
        branch = Branch.open('output')
533
 
        self.assertEqual(branch.revision_history(),
534
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
535
 
                          self._baz._bad_id_tag_bzr_base,
536
 
                          self._baz._bad_id_tag_bzr])
537
 
        branch.set_revision_history(
538
 
            ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
539
 
        del branch
540
 
        branch = Branch.open('output')
541
 
        self.assertEqual(branch.revision_history(),
542
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
543
 
        del branch
544
 
        import_version('output', pybaz.Version(self._baz._bad_id_tag))
545
 
        branch = Branch.open('output')
546
 
        self.assertEqual(branch.revision_history(),
547
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
548
 
                          self._baz._bad_id_tag_bzr_base,
549
 
                          self._baz._bad_id_tag_bzr])
550
 
        rev = branch.repository.get_revision(self._baz._bad_id_tag_bzr)
551
 
        inv = branch.repository.get_inventory(self._baz._bad_id_tag_bzr)
552
 
        self.assertEqual(inv.path2id('path'), 'x_this_id%2fneeds%25escaping')
553
 
        self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
554
 
 
555
 
    def test_inbranch_conversion(self):
556
 
        import_version('output', pybaz.Version(self._baz._inbranch_tag))
557
 
        # expected results:
558
 
        # three commits, no files, revision identifiers of 
559
 
        # 'demo@DONOTUSE_c--import--0--base-0' and
560
 
        # self._baz._inbranch_tag_base_bzr
561
 
        # self._baz._inbranch_tag_head_bzr
562
 
        # with no merges.
563
 
        branch = Branch.open('output')
564
 
        self.assertEqual(branch.revision_history(),
565
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0',
566
 
                          self._baz._inbranch_tag_base_bzr,
567
 
                          self._baz._inbranch_tag_head_bzr])
568
 
        # and again.
569
 
        import_version('output2', pybaz.Version(self._baz._inbranch_tag))
570
 
        branch2 = Branch.open('output2')
571
 
        
572
 
        self.assertEqual(branch.revision_history(), branch2.revision_history())
573
 
        # check revisions in the history.
574
 
        rev = branch.repository.get_revision(self._baz._inbranch_tag_base_bzr)
575
 
        rev2 = branch2.repository.get_revision(self._baz._inbranch_tag_base_bzr)
576
 
        # they must be the same
577
 
        self.assertEqual(rev, rev2)
578
 
        # and we should get some expected values:
579
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
580
 
        self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--import--0--base-0")
581
 
        self.assertEqual(rev.revision_id, self._baz._inbranch_tag_base_bzr)
582
 
 
583
 
        # check next revisions in the history.
584
 
        rev = branch.repository.get_revision(self._baz._inbranch_tag_head_bzr)
585
 
        rev2 = branch2.repository.get_revision(self._baz._inbranch_tag_head_bzr)
586
 
        # they must be the same
587
 
        self.assertEqual(rev, rev2)
588
 
        # and we should get some expected values:
589
 
        self.assertEqual(rev.committer, "Test User<test@example.org>")
590
 
        self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--inbranch-tag--0--base-0")
591
 
        self.assertEqual(rev.revision_id, self._baz._inbranch_tag_head_bzr)
592
 
        self.assertEqual(rev.parent_ids,
593
 
                         [self._baz._inbranch_tag_base_bzr])
594
 
 
595
 
    def test_no_commits_same_as_missing(self):
596
 
        path = 'output'
597
 
        os.mkdir(path)
598
 
        branch = bzrlib.bzrdir.BzrDir.create_branch_convenience(path)
599
 
        import_version(path, pybaz.Version(self._baz._import))
600
 
        # expected results:
601
 
        # one commit, revision identifier of 
602
 
        # 'demo@DONOTUSE_c--import--0--base-0'
603
 
        self.assertEqual(branch.revision_history(),
604
 
                         ['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
605
 
 
606
 
 
607
 
class TestNamespacePrevious(TestCase):
608
 
 
609
 
    def setUp(self):
610
 
        TestCase.setUp(self)
611
 
        self.version = pybaz.Version('foo@example.com/c--b--0')
612
 
 
613
 
    def test_base0_none(self):
614
 
        self.assertEqual(namespace_previous(self.version['base-0']), None)
615
 
 
616
 
    def test_patch1_base0(self):
617
 
        self.assertEqual(namespace_previous(self.version['patch-1']),
618
 
                         self.version['base-0'])
619
 
        
620
 
    def test_patch3000_patch2999(self):
621
 
        self.assertEqual(namespace_previous(self.version['patch-3000']),
622
 
                         self.version['patch-2999'])
623
 
        
624
 
    def test_version0_raises(self):
625
 
        self.assertRaises(RuntimeError, namespace_previous,
626
 
                          self.version['version-0'])
627
 
 
628
 
    def test_version1_version0(self):
629
 
        self.assertEqual(namespace_previous(self.version['versionfix-1']),
630
 
                         self.version['version-0'])
631
 
 
632
 
    def test_version3000_patch2999(self):
633
 
        self.assertEqual(namespace_previous(self.version['versionfix-3000']),
634
 
                         self.version['versionfix-2999'])
635
 
 
636
 
 
637
 
class TestNamespaceMapping(TestCase):
638
 
 
639
 
    def test_namespace_mapping_branch(self):
640
 
        from bzrlib.plugins.bzrtools.baz_import import map_namespace
641
 
        branch = pybaz.Branch('foo@example.com/c--b')
642
 
        self.assertRaises(pybaz.errors.NamespaceError, map_namespace, branch)
643
 
        self.assertEqual('c/b', map_namespace(branch['0']))
644
 
        self.assertEqual('c/0.1/b', map_namespace(branch['0.1']))
645
 
 
646
 
    def test_namespace_mapping_no_branch(self):
647
 
        from bzrlib.plugins.bzrtools.baz_import import map_namespace
648
 
        category = pybaz.Category('foo@example.com/c')
649
 
        self.assertRaises(pybaz.errors.NamespaceError, map_namespace, category)
650
 
        self.assertEqual('c/+trunk', 
651
 
                         map_namespace(pybaz.Version("%s--0" % category)))
652
 
        self.assertEqual('c/0.1/+trunk',
653
 
                         map_namespace(pybaz.Version('%s--0.1' % category)))
654
 
 
655
 
 
656
 
class TestFileIdMapping(TestCase):
657
 
 
658
 
    def test_slash(self):
659
 
        self.assertEqual('c%2fc', map_file_id('c/c'))
660
 
        self.assertEqual('c%25c', map_file_id('c%c'))
661
 
 
662
 
 
663
 
class TestImport(TestCaseInTempDir):
664
 
 
665
 
    def setUp(self):
666
 
        TestCaseInTempDir.setUp(self)
667
 
        self._oldhome = os.environ['HOME']
668
 
        self._tmpdir = tempfile.mkdtemp()
669
 
        self._homedir = os.path.join(self._tmpdir, 'home')
670
 
        os.mkdir(self._homedir)
671
 
        os.environ['HOME'] = self._homedir
672
 
        self._archiveroot = os.path.join(self._tmpdir, 'archive')
673
 
        self._archive = make_archive('demo@DONOTUSE', str(self._archiveroot))
674
 
 
675
 
    def tearDown(self):
676
 
        os.environ['HOME'] = self._oldhome
677
 
        shutil.rmtree(self._tmpdir)
678
 
        TestCaseInTempDir.tearDown(self)
679
 
 
680
 
    def make_import(self, namespace):
681
 
        self._import = 'demo@DONOTUSE/%s' % namespace
682
 
        os.mkdir(os.path.join(self._tmpdir, 'tree'))
683
 
        tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), 
684
 
                               self._import)
685
 
        msg = tree.log_message()
686
 
        msg["summary"] = "I am importing now"
687
 
        tree.import_(msg)
688
 
        shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
689
 
 
690
 
    def test_cmd_exists(self):
691
 
        from bzrlib.plugins.bzrtools.baz_import import cmd_baz_import
692
 
 
693
 
    def test_empty_archive(self):
694
 
        command = cmd_baz_import()
695
 
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
696
 
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output')))
697
 
        walk_len = len(list(os.walk(os.path.join(self._tmpdir,'output'))))
698
 
        self.assertEqual(7, walk_len)
699
 
 
700
 
    def test_two_branches(self):
701
 
        self.make_import('c--0')
702
 
        self.make_import('c1--branch--0.2')
703
 
        command = cmd_baz_import()
704
 
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
705
 
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output')))
706
 
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output', 
707
 
                                       'c','+trunk')))
708
 
        self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output', 
709
 
                                                    'c1', '0.2','branch')))
710
 
        walk_len = len(list(os.walk(os.path.join(self._tmpdir,'output'))))
711
 
        self.assertEqual(20, walk_len)
712
 
 
713
 
    def test_run_twice(self):
714
 
        self.make_import('c--0')
715
 
        command = cmd_baz_import()
716
 
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
717
 
        command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
718
 
        
719
 
    def test_accepts_reuse_history(self):
720
 
        self.make_import('c--0')
721
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
722
 
                     'demo@DONOTUSE', '.', '.')
723
 
        
724
 
    def test_does_not_need_reuse_history(self):
725
 
        self.make_import('c--0')
726
 
        self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
727
 
                     'demo@DONOTUSE')
728