1
# Copyright (C) 2005 Canonical Limited
2
# Authors: Robert Collins <robert.collins@canonical.com>
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.
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.
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
18
from bzrlib.tests import TestCaseInTempDir, TestCase
19
from bzrlib.osutils import has_symlinks
26
from bzrtools.baz_import import (import_version, revision_id,
27
cmd_baz_import, make_archive, map_file_id)
29
from bzrlib.plugins.bzrtools.baz_import import (import_version,
35
from StringIO import StringIO
37
from testresources import (TestResource, TestLoader, OptimisingTestSuite,
41
from bzrlib.errors import NoSuchRevision
43
from bzrtools.fai import namespace_previous
45
from bzrlib.plugins.bzrtools.fai import namespace_previous
46
from bzrlib.branch import Branch
50
from unittest import TestSuite
52
return TestLoader().loadTestsFromName(__name__)
55
class BazTreeResource(TestResource):
58
os.environ['HOME'] = self._oldhome
59
shutil.rmtree(self._tmpdir)
62
self._tmpdir = tempfile.mkdtemp()
63
self._homedir = os.path.join(self._tmpdir, 'home')
64
self._oldhome = os.environ['HOME']
65
os.mkdir(self._homedir)
66
os.environ['HOME'] = self._homedir
68
self._archiveroot = os.path.join(self._tmpdir, 'archive')
69
self._archive = make_archive('demo@DONOTUSE', str(self._archiveroot))
70
pybaz.set_my_id("Test User<test@example.org>")
72
self.make_empty_import()
74
self._empty_tag = 'demo@DONOTUSE/c--empty-tag--0'
75
self._empty_tag_bzr = revision_id(self._empty_tag + '--base-0')
76
pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
77
pybaz.Version(self._empty_tag))
79
self._empty_merged_tag = 'demo@DONOTUSE/c--empty-merged-tag--0'
80
self._empty_merged_tag_bzr_base = revision_id(self._empty_merged_tag
82
self._empty_merged_tag_bzr = revision_id(self._empty_merged_tag
84
pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
85
pybaz.Version(self._empty_merged_tag))
86
tree = pybaz.Revision(self._empty_merged_tag + '--base-0').get(
87
os.path.join(self._tmpdir, 'tree'))
88
tree.star_merge(self._empty_tag,
89
pybaz.Version('demo@DONOTUSE/c--import--0'))
90
msg = tree.log_message()
91
msg["summary"]="did a merge, yarh"
93
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
95
# tree, two commits, includes merge of other branch
96
self._empty_merged_tag_2 = 'demo@DONOTUSE/c--empty-tag-2--0'
97
self._empty_merged_tag_2_bzr_base = revision_id(
98
self._empty_merged_tag_2 + '--base-0')
99
self._empty_merged_tag_2_bzr = revision_id(
100
self._empty_merged_tag_2 + '--patch-1')
101
pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
102
pybaz.Version(self._empty_merged_tag_2))
103
tree = pybaz.Revision(self._empty_merged_tag_2 + '--base-0').get (
104
os.path.join(self._tmpdir, 'tree'))
105
tree.star_merge(self._empty_merged_tag,
106
pybaz.Version('demo@DONOTUSE/c--import--0'))
107
msg = tree.log_message()
108
msg["summary"] = "merge in a merged tree."
110
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
112
self._bad_id_tag = 'demo@DONOTUSE/c--bad-id--0'
113
self._bad_id_tag_bzr_base = revision_id(self._bad_id_tag + '--base-0')
114
self._bad_id_tag_bzr = revision_id(self._bad_id_tag + '--patch-1')
115
pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
116
pybaz.Version(self._bad_id_tag))
117
tree = pybaz.Revision(self._bad_id_tag + '--base-0').get(
118
os.path.join(self._tmpdir, 'tree'))
119
from bzrlib.plugins.bzrtools.baz_import import add_file
120
add_file(os.path.join(self._tmpdir,'tree/path'), 'text',
121
'this_id/needs%escaping')
122
msg = tree.log_message()
123
msg["summary"] = "commit something which needs escaping."
125
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
127
self.make_import_symlink()
128
self.make_missing_ancestor()
129
self.make_inbranch_continuation()
131
def make_import_symlink(self):
132
self._import_symlink = 'demo@DONOTUSE/c--import-symlink--0'
133
self._import_symlink_bzr = revision_id(
134
self._import_symlink + '--base-0')
135
os.mkdir(os.path.join(self._tmpdir, 'tree'))
136
tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
137
self._import_symlink)
138
link_path = os.path.join(self._tmpdir, 'tree', 'alink')
139
os.symlink('missing-file-name', link_path)
140
tree.add_tag('alink')
141
id_file = open(os.path.join(tree, '.arch-ids', 'alink.id'), 'w')
142
id_file.write('symlink_tag\n')
144
msg = tree.log_message()
145
msg["summary"] = "Import with a symlink"
148
f = file(link_path, 'w')
149
f.write('Not a symlink no more!')
151
msg = tree.log_message()
152
msg["summary"] = "Turn a symlink into a file"
154
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
156
def make_empty_import(self):
157
self._import = 'demo@DONOTUSE/c--import--0'
158
os.mkdir(os.path.join(self._tmpdir, 'tree'))
159
tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
161
msg = tree.log_message()
162
msg["summary"] = "I am importing now"
164
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
166
def make_missing_ancestor(self):
167
self._archivegoneroot = os.path.join(self._tmpdir, 'archivegone')
168
self._archive = make_archive('demo-gone@DONOTUSE',
169
str(self._archivegoneroot))
170
self._missing_import = 'demo-gone@DONOTUSE/c--import--0'
171
self._missing_import_bzr = revision_id(self._missing_import
173
self._missing_ancestor = 'demo@DONOTUSE/c--gone--0'
174
self._missing_ancestor_bzr = revision_id(self._missing_ancestor
176
os.mkdir(os.path.join(self._tmpdir, 'tree'))
177
tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
178
self._missing_import)
179
msg = tree.log_message()
180
msg["summary"] = "I am importing now"
182
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
183
# tag into the kept archive
184
pybaz.Revision(self._missing_import + '--base-0').make_continuation(
185
pybaz.Version(self._missing_ancestor))
187
# make an import for testing history-reuse logic.
188
# note the use of a namespace layout here.
189
self._missing_import_imported = os.path.join(self._tmpdir,
191
os.mkdir(os.path.join(self._tmpdir, 'archivegone-bzr'))
192
os.mkdir(os.path.join(self._tmpdir, 'archivegone-bzr', 'c'))
193
import_version(os.path.join(self._tmpdir, 'archivegone-bzr',
195
pybaz.Version(self._missing_import))
196
# and make it inaccessible
197
pybaz.Archive('demo-gone@DONOTUSE').unregister()
199
def make_inbranch_continuation(self):
200
self._inbranch_tag = 'demo@DONOTUSE/c--inbranch-tag--0'
201
self._inbranch_tag_base = self._inbranch_tag + '--base-0'
202
self._inbranch_tag_base_bzr = revision_id(self._inbranch_tag_base)
203
pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
204
pybaz.Version(self._inbranch_tag))
205
self._inbranch_tag_head = self._inbranch_tag + '--patch-1'
206
self._inbranch_tag_head_bzr = revision_id(self._inbranch_tag_head)
207
pybaz.Revision(self._inbranch_tag_base).make_continuation(
208
pybaz.Version(self._inbranch_tag))
211
def _makeResource(self):
212
return BazTreeResource()
215
def _cleanResource(self, resource):
219
class TestImportBranch(TestCaseInTempDir):
221
_resources = [("_baz", BazTreeResource)]
224
TestCaseInTempDir.setUp(self)
225
ResourcedTestCase.setUpResources(self)
226
os.environ['HOME'] = self._baz._homedir
229
ResourcedTestCase.tearDownResources(self)
230
TestCaseInTempDir.tearDown(self)
232
def test_import_empty(self):
233
import_version('output', pybaz.Version(self._baz._import))
235
# one commit, no files, revision identifier of
236
# 'demo@DONOTUSE_c--import--0--base-0'
237
branch = Branch.open('output')
238
repo = branch.repository
239
self.assertEqual(branch.revision_history(),
240
['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
241
rev = repo.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
243
import_version('output2', pybaz.Version('demo@DONOTUSE/c--import--0'))
244
branch2 = Branch.open('output2')
245
repo2 = branch2.repository
246
self.assertEqual(branch.revision_history(), branch2.revision_history())
247
rev2 = repo2.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
248
# they must be the same
249
self.assertEqual(rev, rev2)
251
# and we should get some expected values:
252
self.assertEqual(rev.committer, "Test User<test@example.org>")
253
self.assertEqual(rev.message, "I am importing now")
254
self.assertEqual(rev.revision_id,
255
"Arch-1:demo@DONOTUSE%c--import--0--base-0")
257
def test_empty_tagged(self):
258
import_version('output', pybaz.Version(self._baz._empty_tag))
260
# two commits, no files, revision identifiers of
261
# 'demo@DONOTUSE_c--import--0--base-0' and
262
# self._baz._empty_tag_bzr
263
branch = Branch.open('output')
264
self.assertEqual(branch.revision_history(),
265
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
266
self._baz._empty_tag_bzr])
267
rev = branch.repository.get_revision(self._baz._empty_tag_bzr)
269
import_version('output2', pybaz.Version(self._baz._empty_tag))
270
branch2 = Branch.open('output2')
271
self.assertEqual(branch.revision_history(), branch2.revision_history())
272
rev2 = branch2.repository.get_revision(self._baz._empty_tag_bzr)
273
# they must be the same
274
self.assertEqual(rev, rev2)
276
# and we should get some expected values:
277
self.assertEqual(rev.committer, "Test User<test@example.org>")
278
self.assertEqual(rev.message,
279
"tag of demo@DONOTUSE/c--import--0--base-0")
280
self.assertEqual(rev.revision_id, self._baz._empty_tag_bzr)
282
def test_empty_merged_tagged(self):
283
import_version('output', pybaz.Version(self._baz._empty_merged_tag))
285
# two commits, no files, revision identifiers of
286
# 'demo@DONOTUSE_c--import--0--base-0' and
287
# self._baz._empty_merged_tag_bzr_base
288
# self._baz._empty_merged_tag_bzr
289
# and a merged revision from the latter of
290
# self._baz._empty_tag_bzr
291
branch = Branch.open('output')
292
repo = branch.repository
293
self.assertEqual(branch.revision_history(),
294
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
295
self._baz._empty_merged_tag_bzr_base,
296
self._baz._empty_merged_tag_bzr])
298
import_version('output2', pybaz.Version(self._baz._empty_merged_tag))
299
branch2 = Branch.open('output2')
300
repo2 = branch2.repository
301
# and import what we should be merged up against for checking with.
302
import_version('output3', pybaz.Version(self._baz._empty_tag))
303
branch3 = Branch.open('output3')
305
self.assertEqual(branch.revision_history(), branch2.revision_history())
306
self.assertNotEqual(branch.revision_history(),
307
branch3.revision_history())
308
# check revisions in the history.
309
rev = repo.get_revision(self._baz._empty_merged_tag_bzr_base)
310
rev2 = repo2.get_revision(self._baz._empty_merged_tag_bzr_base)
311
# they must be the same
312
self.assertEqual(rev, rev2)
313
# and we should get some expected values:
314
self.assertEqual(rev.committer, "Test User<test@example.org>")
315
self.assertEqual(rev.message,
316
"tag of demo@DONOTUSE/c--import--0--base-0")
317
self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_bzr_base)
318
self.assertEqual(['Arch-1:demo@DONOTUSE%c--import--0--base-0'],
321
# check next revisions in the history.
322
rev = repo.get_revision(self._baz._empty_merged_tag_bzr)
323
rev2 = repo2.get_revision(self._baz._empty_merged_tag_bzr)
324
# they must be the same
325
self.assertEqual(rev, rev2)
326
# and we should get some expected values:
327
self.assertEqual(rev.committer, "Test User<test@example.org>")
328
self.assertEqual(rev.message, "did a merge, yarh")
329
self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_bzr)
330
self.assertEqual(rev.parent_ids[0],
331
self._baz._empty_merged_tag_bzr_base)
332
self.assertEqual(rev.parent_ids[1], self._baz._empty_tag_bzr)
334
# this tree should have nothing missing from that tree.
335
# FIXME there is no code for this right now.
336
# self.assertEqual(branch.missing_revisions(branch3), [])
338
def test_merge_branch_with_merges(self):
339
import_version('output', pybaz.Version(self._baz._empty_merged_tag_2))
341
# two commits, no files, revision identifiers of
342
# 'demo@DONOTUSE_c--import--0--base-0' and
343
# self._baz._empty_merged_tag_2_bzr_base
344
# self._baz._empty_merged_tag_2_bzr
345
# and a merged revision from the latter of
346
# self._baz._empty_merged_tag_bzr
347
branch = Branch.open('output')
348
repo = branch.repository
349
self.assertEqual(branch.revision_history(),
350
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
351
self._baz._empty_merged_tag_2_bzr_base,
352
self._baz._empty_merged_tag_2_bzr])
354
import_version('output2', pybaz.Version(self._baz._empty_merged_tag_2))
355
branch2 = Branch.open('output2')
356
repo2 = branch2.repository
357
# and import what we should be merged up against for checking with.
358
import_version('output3', pybaz.Version(self._baz._empty_merged_tag))
359
branch3 = Branch.open('output3')
361
self.assertEqual(branch.revision_history(), branch2.revision_history())
362
self.assertNotEqual(branch.revision_history(),
363
branch3.revision_history())
364
# check revisions in the history.
365
rev = repo.get_revision(self._baz._empty_merged_tag_2_bzr_base)
366
rev2 = repo2.get_revision(self._baz._empty_merged_tag_2_bzr_base)
367
# they must be the same
368
self.assertEqual(rev, rev2)
369
# and we should get some expected values:
370
self.assertEqual(rev.committer, "Test User<test@example.org>")
371
self.assertEqual(rev.message,
372
"tag of demo@DONOTUSE/c--import--0--base-0")
373
self.assertEqual(rev.revision_id,
374
self._baz._empty_merged_tag_2_bzr_base)
376
# check next revisions in the history.
377
rev = repo.get_revision(self._baz._empty_merged_tag_2_bzr)
378
rev2 = repo2.get_revision(self._baz._empty_merged_tag_2_bzr)
379
# they must be the same
380
self.assertEqual(rev, rev2)
381
# and we should get some expected values:
382
self.assertEqual(rev.committer, "Test User<test@example.org>")
383
self.assertEqual(rev.message, "merge in a merged tree.")
384
self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_2_bzr)
385
self.assertEqual(rev.parent_ids[0],
386
self._baz._empty_merged_tag_2_bzr_base)
387
self.assertEqual(rev.parent_ids[1],
388
self._baz._empty_merged_tag_bzr)
390
# this tree should have nothing missing from that tree.
391
# FIXME there is no code for this right now.
392
# self.assertEqual(branch.missing_revisions(branch3), [])
394
def test_import_symlink(self):
395
import_version('output', pybaz.Version(self._baz._import_symlink),
398
# two commits, no files, revision identifier of
399
# 'demo@DONOTUSE_c--import--0--base-0'
400
branch = Branch.open('output')
401
self.assertEqual(branch.revision_history(),
402
[self._baz._import_symlink_bzr])
403
rev = branch.repository.get_revision(self._baz._import_symlink_bzr)
405
import_version('output2', pybaz.Version(self._baz._import_symlink),
407
branch2 = Branch.open('output2')
408
self.assertEqual(branch.revision_history(), branch2.revision_history())
409
rev2 = branch2.repository.get_revision(self._baz._import_symlink_bzr)
410
# they must be the same
411
self.assertEqual(rev, rev2)
413
# and we should get some expected values:
414
self.assertEqual(rev.committer, "Test User<test@example.org>")
415
self.assertEqual(rev.message, "Import with a symlink")
416
self.assertEqual(rev.revision_id, self._baz._import_symlink_bzr)
418
# and we want the symlink alink with target 'missing-file-name'
419
inv = branch.repository.get_inventory(rev.revision_id)
420
self.assertEqual(inv.path2id('alink'), 'x_symlink_tag')
421
entry = inv['x_symlink_tag']
422
self.assertEqual(entry.kind, 'symlink')
423
self.assertEqual(entry.symlink_target, 'missing-file-name')
425
# current bzr doesn't handle type changes
426
self.assertRaises(AssertionError, import_version, 'output3',
427
pybaz.Version(self._baz._import_symlink))
429
def test_missing_ancestor(self):
430
import_version('output', pybaz.Version(self._baz._missing_ancestor))
432
# one commits, no files, revision identifiers of
433
# 'demo@DONOTUSE_c--gone--0--base-0' and
434
# a merge of demo-gone@DONOTUSE%c--import--0
435
branch = Branch.open('output')
436
self.assertEqual(branch.revision_history(),
437
[self._baz._missing_ancestor_bzr])
438
rev = branch.repository.get_revision(self._baz._missing_ancestor_bzr)
440
import_version('output2', pybaz.Version(self._baz._missing_ancestor))
441
branch2 = Branch.open('output2')
442
self.assertEqual(branch.revision_history(), branch2.revision_history())
443
rev2 = branch2.repository.get_revision(self._baz._missing_ancestor_bzr)
444
# they must be the same
445
self.assertEqual(rev, rev2)
447
# and we should get some expected values:
448
self.assertEqual(rev.committer, "Test User<test@example.org>")
449
self.assertEqual(rev.message,
450
"tag of demo-gone@DONOTUSE/c--import--0--base-0")
451
self.assertEqual(rev.revision_id, self._baz._missing_ancestor_bzr)
452
self.assertEqual(rev.parent_ids[0], self._baz._missing_import_bzr)
453
self.assertEqual(1, len(rev.parent_ids))
455
# must NOT be able to get the merged evision
456
self.assertRaises(NoSuchRevision, branch.repository.get_revision,
457
self._baz._missing_import_bzr)
459
def test_missing_ancestor_reusing_history(self):
460
import_version('output', pybaz.Version(self._baz._missing_ancestor),
461
reuse_history_from=[self._baz._missing_import_imported])
463
# one commits, no files, revision identifiers of
464
# 'demo-gone@DONOTUSE%c--import--0--base-0' and
465
# 'demo@DONOTUSE%c--gone--0--base-0'
466
branch = Branch.open('output')
467
self.assertEqual(branch.revision_history(),
468
[self._baz._missing_import_bzr,
469
self._baz._missing_ancestor_bzr])
470
rev = branch.repository.get_revision(self._baz._missing_ancestor_bzr)
472
import_version('output2', pybaz.Version(self._baz._missing_ancestor),
473
reuse_history_from=[self._baz._missing_import_imported])
474
branch2 = Branch.open('output2')
475
self.assertEqual(branch.revision_history(), branch2.revision_history())
476
rev2 = branch2.repository.get_revision(self._baz._missing_ancestor_bzr)
477
# they must be the same
478
self.assertEqual(rev, rev2)
480
# must be able to get the missing base revision
481
branch.repository.get_revision(self._baz._missing_import_bzr)
483
# and we should get some expected values:
484
self.assertEqual(rev.committer, "Test User<test@example.org>")
485
self.assertEqual(rev.message,
486
"tag of demo-gone@DONOTUSE/c--import--0--base-0")
487
self.assertEqual(rev.revision_id, self._baz._missing_ancestor_bzr)
488
self.assertEqual(rev.parent_ids[0], self._baz._missing_import_bzr)
489
self.assertEqual(1, len(rev.parent_ids))
491
def test_bad_file_id(self):
492
import_version('output', pybaz.Version(self._baz._bad_id_tag))
494
# three commits, one files, revision identifiers of
495
# 'demo@DONOTUSE_c--import--0--base-0' ,
496
# 'demo@DONOTUSE/c--bad-id--0--base-0' ,
497
# ''demo@DONOTUSE/c--bad-id--0--patch-1'
498
branch = Branch.open('output')
499
self.assertEqual(branch.revision_history(),
500
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
501
self._baz._bad_id_tag_bzr_base,
502
self._baz._bad_id_tag_bzr])
503
rev = branch.repository.get_revision(self._baz._bad_id_tag_bzr)
504
inv = branch.repository.get_inventory(self._baz._bad_id_tag_bzr)
505
self.assertEqual(inv.path2id('path'), 'x_this_id%2fneeds%25escaping')
506
self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
508
def test_appending_revisions_already_present(self):
509
import_version('output', pybaz.Version(self._baz._bad_id_tag),
512
# three commits, one files, revision identifiers of
513
# 'demo@DONOTUSE_c--import--0--base-0' ,
514
# 'demo@DONOTUSE/c--bad-id--0--base-0' ,
515
# ''demo@DONOTUSE/c--bad-id--0--patch-1'
516
branch = Branch.open('output')
517
self.assertEqual(branch.revision_history(),
518
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
519
self._baz._bad_id_tag_bzr_base])
520
branch.set_revision_history(
521
['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
523
branch = Branch.open('output')
524
self.assertEqual(branch.revision_history(),
525
['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
527
import_version('output', pybaz.Version(self._baz._bad_id_tag))
528
branch = Branch.open('output')
529
self.assertEqual(branch.revision_history(),
530
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
531
self._baz._bad_id_tag_bzr_base,
532
self._baz._bad_id_tag_bzr])
533
rev = branch.repository.get_revision(self._baz._bad_id_tag_bzr)
534
inv = branch.repository.get_inventory(self._baz._bad_id_tag_bzr)
535
self.assertEqual(inv.path2id('path'), 'x_this_id%2fneeds%25escaping')
536
self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
538
def test_appending_revisions_all_already_present(self):
539
import_version('output', pybaz.Version(self._baz._bad_id_tag))
541
# three commits, one files, revision identifiers of
542
# 'demo@DONOTUSE_c--import--0--base-0' ,
543
# 'demo@DONOTUSE/c--bad-id--0--base-0' ,
544
# ''demo@DONOTUSE/c--bad-id--0--patch-1'
545
branch = Branch.open('output')
546
self.assertEqual(branch.revision_history(),
547
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
548
self._baz._bad_id_tag_bzr_base,
549
self._baz._bad_id_tag_bzr])
550
branch.set_revision_history(
551
['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
553
branch = Branch.open('output')
554
self.assertEqual(branch.revision_history(),
555
['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
557
import_version('output', pybaz.Version(self._baz._bad_id_tag))
558
branch = Branch.open('output')
559
self.assertEqual(branch.revision_history(),
560
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
561
self._baz._bad_id_tag_bzr_base,
562
self._baz._bad_id_tag_bzr])
563
rev = branch.repository.get_revision(self._baz._bad_id_tag_bzr)
564
inv = branch.repository.get_inventory(self._baz._bad_id_tag_bzr)
565
self.assertEqual(inv.path2id('path'), 'x_this_id%2fneeds%25escaping')
566
self.assertEqual('path', inv.id2path('x_this_id%2fneeds%25escaping'))
568
def test_inbranch_conversion(self):
569
import_version('output', pybaz.Version(self._baz._inbranch_tag))
571
# three commits, no files, revision identifiers of
572
# 'demo@DONOTUSE_c--import--0--base-0' and
573
# self._baz._inbranch_tag_base_bzr
574
# self._baz._inbranch_tag_head_bzr
576
branch = Branch.open('output')
577
self.assertEqual(branch.revision_history(),
578
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
579
self._baz._inbranch_tag_base_bzr,
580
self._baz._inbranch_tag_head_bzr])
582
import_version('output2', pybaz.Version(self._baz._inbranch_tag))
583
branch2 = Branch.open('output2')
585
self.assertEqual(branch.revision_history(), branch2.revision_history())
586
# check revisions in the history.
587
rev = branch.repository.get_revision(self._baz._inbranch_tag_base_bzr)
588
rev2 = branch2.repository.get_revision(self._baz._inbranch_tag_base_bzr)
589
# they must be the same
590
self.assertEqual(rev, rev2)
591
# and we should get some expected values:
592
self.assertEqual(rev.committer, "Test User<test@example.org>")
593
self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--import--0--base-0")
594
self.assertEqual(rev.revision_id, self._baz._inbranch_tag_base_bzr)
596
# check next revisions in the history.
597
rev = branch.repository.get_revision(self._baz._inbranch_tag_head_bzr)
598
rev2 = branch2.repository.get_revision(self._baz._inbranch_tag_head_bzr)
599
# they must be the same
600
self.assertEqual(rev, rev2)
601
# and we should get some expected values:
602
self.assertEqual(rev.committer, "Test User<test@example.org>")
603
self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--inbranch-tag--0--base-0")
604
self.assertEqual(rev.revision_id, self._baz._inbranch_tag_head_bzr)
605
self.assertEqual(rev.parent_ids,
606
[self._baz._inbranch_tag_base_bzr])
608
def test_no_commits_same_as_missing(self):
611
branch = bzrlib.bzrdir.BzrDir.create_branch_convenience(path)
612
import_version(path, pybaz.Version(self._baz._import))
614
# one commit, revision identifier of
615
# 'demo@DONOTUSE_c--import--0--base-0'
616
self.assertEqual(branch.revision_history(),
617
['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
620
class TestNamespacePrevious(TestCase):
624
self.version = pybaz.Version('foo@example.com/c--b--0')
626
def test_base0_none(self):
627
self.assertEqual(namespace_previous(self.version['base-0']), None)
629
def test_patch1_base0(self):
630
self.assertEqual(namespace_previous(self.version['patch-1']),
631
self.version['base-0'])
633
def test_patch3000_patch2999(self):
634
self.assertEqual(namespace_previous(self.version['patch-3000']),
635
self.version['patch-2999'])
637
def test_version0_raises(self):
638
self.assertRaises(RuntimeError, namespace_previous,
639
self.version['version-0'])
641
def test_version1_version0(self):
642
self.assertEqual(namespace_previous(self.version['versionfix-1']),
643
self.version['version-0'])
645
def test_version3000_patch2999(self):
646
self.assertEqual(namespace_previous(self.version['versionfix-3000']),
647
self.version['versionfix-2999'])
650
class TestNamespaceMapping(TestCase):
652
def test_namespace_mapping_branch(self):
653
from bzrlib.plugins.bzrtools.baz_import import map_namespace
654
branch = pybaz.Branch('foo@example.com/c--b')
655
self.assertRaises(pybaz.errors.NamespaceError, map_namespace, branch)
656
self.assertEqual('c/b', map_namespace(branch['0']))
657
self.assertEqual('c/0.1/b', map_namespace(branch['0.1']))
659
def test_namespace_mapping_no_branch(self):
660
from bzrlib.plugins.bzrtools.baz_import import map_namespace
661
category = pybaz.Category('foo@example.com/c')
662
self.assertRaises(pybaz.errors.NamespaceError, map_namespace, category)
663
self.assertEqual('c/+trunk',
664
map_namespace(pybaz.Version("%s--0" % category)))
665
self.assertEqual('c/0.1/+trunk',
666
map_namespace(pybaz.Version('%s--0.1' % category)))
669
class TestFileIdMapping(TestCase):
671
def test_slash(self):
672
self.assertEqual('c%2fc', map_file_id('c/c'))
673
self.assertEqual('c%25c', map_file_id('c%c'))
676
class TestImport(TestCaseInTempDir):
679
TestCaseInTempDir.setUp(self)
680
self._oldhome = os.environ['HOME']
681
self._tmpdir = tempfile.mkdtemp()
682
self._homedir = os.path.join(self._tmpdir, 'home')
683
os.mkdir(self._homedir)
684
os.environ['HOME'] = self._homedir
685
self._archiveroot = os.path.join(self._tmpdir, 'archive')
686
self._archive = make_archive('demo@DONOTUSE', str(self._archiveroot))
689
os.environ['HOME'] = self._oldhome
690
shutil.rmtree(self._tmpdir)
691
TestCaseInTempDir.tearDown(self)
693
def make_import(self, namespace):
694
self._import = 'demo@DONOTUSE/%s' % namespace
695
os.mkdir(os.path.join(self._tmpdir, 'tree'))
696
tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
698
msg = tree.log_message()
699
msg["summary"] = "I am importing now"
701
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
703
def test_cmd_exists(self):
704
from bzrlib.plugins.bzrtools.baz_import import cmd_baz_import
706
def test_empty_archive(self):
707
command = cmd_baz_import()
708
command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
709
self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output')))
710
walk_len = len(list(os.walk(os.path.join(self._tmpdir,'output'))))
711
self.assertEqual(7, walk_len)
713
def test_two_branches(self):
714
self.make_import('c--0')
715
self.make_import('c1--branch--0.2')
716
command = cmd_baz_import()
717
command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
718
self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output')))
719
self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output',
721
self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output',
722
'c1', '0.2','branch')))
723
walk_len = len(list(os.walk(os.path.join(self._tmpdir,'output'))))
724
self.assertEqual(20, walk_len)
726
def test_run_twice(self):
727
self.make_import('c--0')
728
command = cmd_baz_import()
729
command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
730
command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
732
def test_accepts_reuse_history(self):
733
self.make_import('c--0')
734
self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),
735
'demo@DONOTUSE', '.', '.')
737
def test_does_not_need_reuse_history(self):
738
self.make_import('c--0')
739
self.run_bzr('baz-import', os.path.join(self._tmpdir, 'output'),