~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/test_baz_import.py

  • Committer: Michael Ellerman
  • Date: 2005-11-29 07:12:26 UTC
  • mto: (0.3.1 shelf-dev) (325.1.2 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20051129071226-a04b3f827880025d
Unshelve --pick was broken, because we deleted the whole patch, even when only
part of it was unshelved. So now if we unshelve part of a patch, the patch is
replaced with a new patch that has just the unshelved parts. That's a long way
of saying it does what you'd expect.

Implementing this required changing HunkSelector to return both the selected,
and unselected hunks (ie. patches to shelve, and patches to keep).

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