~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/test_baz_import.py

  • Committer: Aaron Bentley
  • Date: 2007-01-08 17:27:48 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070108172748-1b22qtszaadoby89
Improve bzr import docs

Show diffs side-by-side

added added

removed removed

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