~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/test_baz_import.py

  • Committer: Aaron Bentley
  • Date: 2006-03-10 20:15:17 UTC
  • mfrom: (147.1.73 bzrtools)
  • Revision ID: abentley@panoramicfeedback.com-20060310201517-958f8d9307a49b2e
Merged the Arch-1 bzrtools import

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