~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/test_baz_import.py

  • Committer: Aaron Bentley
  • Date: 2008-04-11 00:03:51 UTC
  • Revision ID: aaron@aaronbentley.com-20080411000351-dmbvgmanygnphzgp
Add escaping to HTML output

Show diffs side-by-side

added added

removed removed

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