~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/test_baz_import.py

  • Committer: Aaron Bentley
  • Date: 2006-10-24 14:39:46 UTC
  • Revision ID: abentley@panoramicfeedback.com-20061024143946-756da615bcfb92fa
Fix bug setting parent paths by letting Branch command do it

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