~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-01 16:04:07 UTC
  • mto: (147.4.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: abentley@panoramicfeedback.com-20060301160407-c8db0a2c02699dd2
Stopped using deprecated PyBaz functionality

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