1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
# Copyright (C) 2005 Canonical Limited
# Authors: Robert Collins <robert.collins@canonical.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from bzrlib.selftest import TestCaseInTempDir, TestCase
from bzrlib.selftest.blackbox import has_symlinks
try:
import pybaz
except ImportError:
pybaz = None
import os
from baz_import import import_version, revision_id, cmd_baz_import
from bzrlib.branch import find_branch
from bzrlib.errors import NoSuchRevision
import shutil
from StringIO import StringIO
import tempfile
from testresources import (TestResource, TestLoader, OptimisingTestSuite,
ResourcedTestCase)
from fai import namespace_previous
def test_suite():
if pybaz is None:
return OptimisingTestSuite()
return TestLoader().loadTestsFromName(__name__)
class BazTreeResource(TestResource):
def cleanUp(self):
os.environ['HOME'] = self._oldhome
shutil.rmtree(self._tmpdir)
def __init__(self):
self._tmpdir = tempfile.mkdtemp()
self._homedir = os.path.join(self._tmpdir, 'home')
self._oldhome = os.environ['HOME']
os.mkdir(self._homedir)
os.environ['HOME'] = self._homedir
self._archiveroot = os.path.join(self._tmpdir, 'archive')
self._archive = pybaz.make_archive('demo@DONOTUSE', str(self._archiveroot))
pybaz.set_my_id("Test User<test@example.org>")
self.make_empty_import()
self._empty_tag = 'demo@DONOTUSE/c--empty-tag--0'
self._empty_tag_bzr = revision_id(self._empty_tag + '--base-0')
pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
pybaz.Version(self._empty_tag))
self._empty_merged_tag = 'demo@DONOTUSE/c--empty-merged-tag--0'
self._empty_merged_tag_bzr_base = revision_id(self._empty_merged_tag
+ '--base-0')
self._empty_merged_tag_bzr = revision_id(self._empty_merged_tag
+ '--patch-1')
pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
pybaz.Version(self._empty_merged_tag))
tree = pybaz.Revision(self._empty_merged_tag + '--base-0').get(
os.path.join(self._tmpdir, 'tree'))
tree.star_merge(self._empty_tag,
pybaz.Version('demo@DONOTUSE/c--import--0'))
msg = tree.log_message()
msg["summary"]="did a merge, yarh"
tree.commit(msg)
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
# tree, two commits, includes merge of other branch
self._empty_merged_tag_2 = 'demo@DONOTUSE/c--empty-tag-2--0'
self._empty_merged_tag_2_bzr_base = revision_id(self._empty_merged_tag_2 + '--base-0')
self._empty_merged_tag_2_bzr = revision_id(self._empty_merged_tag_2 + '--patch-1')
pybaz.Revision('demo@DONOTUSE/c--import--0--base-0').make_continuation(
pybaz.Version(self._empty_merged_tag_2))
tree = pybaz.Revision(self._empty_merged_tag_2 + '--base-0').get (
os.path.join(self._tmpdir, 'tree'))
tree.star_merge(self._empty_merged_tag,
pybaz.Version('demo@DONOTUSE/c--import--0'))
msg = tree.log_message()
msg["summary"] = "merge in a merged tree."
tree.commit(msg)
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
self.make_import_symlink()
self.make_missing_ancestor()
def make_import_symlink(self):
self._import_symlink = 'demo@DONOTUSE/c--import-symlink--0'
self._import_symlink_bzr = revision_id(self._import_symlink + '--base-0')
os.mkdir(os.path.join(self._tmpdir, 'tree'))
tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
self._import_symlink)
os.symlink('missing-file-name',
os.path.join(self._tmpdir, 'tree', 'alink'))
tree.add_tag('alink')
id_file = open(os.path.join(tree, '.arch-ids', 'alink.id'), 'w')
id_file.write('symlink_tag\n')
id_file.close()
msg = tree.log_message()
msg["summary"] = "Import with a symlink"
tree.import_(msg)
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
def make_empty_import(self):
self._import = 'demo@DONOTUSE/c--import--0'
os.mkdir(os.path.join(self._tmpdir, 'tree'))
tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), self._import)
msg = tree.log_message()
msg["summary"] = "I am importing now"
tree.import_(msg)
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
def make_missing_ancestor(self):
self._archivegoneroot = os.path.join(self._tmpdir, 'archivegone')
self._archive = pybaz.make_archive('demo-gone@DONOTUSE',
str(self._archivegoneroot))
self._missing_import = 'demo-gone@DONOTUSE/c--import--0'
self._missing_import_bzr = revision_id(self._missing_import
+ '--base-0')
self._missing_ancestor = 'demo@DONOTUSE/c--gone--0'
self._missing_ancestor_bzr = revision_id(self._missing_ancestor
+ '--base-0')
os.mkdir(os.path.join(self._tmpdir, 'tree'))
tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'),
self._missing_import)
msg = tree.log_message()
msg["summary"] = "I am importing now"
tree.import_(msg)
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
# tag into the kept archive
pybaz.Revision(self._missing_import + '--base-0').make_continuation(
pybaz.Version(self._missing_ancestor))
# and make it inaccessible
pybaz.Archive('demo-gone@DONOTUSE').unregister()
@classmethod
def _makeResource(self):
return BazTreeResource()
@classmethod
def _cleanResource(self, resource):
resource.cleanUp()
class TestImportBranch(TestCaseInTempDir):
_resources = [("_baz", BazTreeResource)]
def setUp(self):
TestCaseInTempDir.setUp(self)
ResourcedTestCase.setUpResources(self)
self.output = StringIO()
def tearDown(self):
ResourcedTestCase.tearDownResources(self)
TestCaseInTempDir.tearDown(self)
def collect(self, text):
self.output.write(text)
self.output.write("\n")
def test_import_empty(self):
import_version('output', pybaz.Version(self._baz._import), self.collect)
# expected results:
# one commit, no files, revision identifier of 'demo@DONOTUSE_c--import--0--base-0'
branch = find_branch('output', find_root=False)
self.assertEqual(branch.revision_history(),
['Arch-1:demo@DONOTUSE%c--import--0--base-0'])
rev = branch.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
# and again.
import_version('output2', pybaz.Version('demo@DONOTUSE/c--import--0'),
self.collect)
branch2 = find_branch('output2', find_root=False)
self.assertEqual(branch.revision_history(), branch2.revision_history())
rev2 = branch2.get_revision('Arch-1:demo@DONOTUSE%c--import--0--base-0')
# they must be the same
self.assertEqual(rev, rev2)
# and we should get some expected values:
self.assertEqual(rev.committer, "Test User<test@example.org>")
self.assertEqual(rev.message, "I am importing now")
self.assertEqual(rev.revision_id,
"Arch-1:demo@DONOTUSE%c--import--0--base-0")
def test_empty_tagged(self):
import_version('output', pybaz.Version(self._baz._empty_tag),
self.collect)
# expected results:
# two commits, no files, revision identifiers of
# 'demo@DONOTUSE_c--import--0--base-0' and
# self._baz._empty_tag_bzr
branch = find_branch('output', find_root=False)
self.assertEqual(branch.revision_history(),
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
self._baz._empty_tag_bzr])
rev = branch.get_revision(self._baz._empty_tag_bzr)
# and again.
import_version('output2', pybaz.Version(self._baz._empty_tag),
self.collect)
branch2 = find_branch('output2', find_root=False)
self.assertEqual(branch.revision_history(), branch2.revision_history())
rev2 = branch2.get_revision(self._baz._empty_tag_bzr)
# they must be the same
self.assertEqual(rev, rev2)
# and we should get some expected values:
self.assertEqual(rev.committer, "Test User<test@example.org>")
self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--import--0--base-0")
self.assertEqual(rev.revision_id, self._baz._empty_tag_bzr)
def test_empty_merged_tagged(self):
import_version('output', pybaz.Version(self._baz._empty_merged_tag),
self.collect)
# expected results:
# two commits, no files, revision identifiers of
# 'demo@DONOTUSE_c--import--0--base-0' and
# self._baz._empty_merged_tag_bzr_base
# self._baz._empty_merged_tag_bzr
# and a merged revision from the latter of
# self._baz._empty_tag_bzr
branch = find_branch('output', find_root=False)
self.assertEqual(branch.revision_history(),
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
self._baz._empty_merged_tag_bzr_base,
self._baz._empty_merged_tag_bzr])
# and again.
import_version('output2', pybaz.Version(self._baz._empty_merged_tag),
self.collect)
branch2 = find_branch('output2', find_root=False)
# and import what we should be merged up against for checking with.
import_version('output3', pybaz.Version(self._baz._empty_tag),
self.collect)
branch3 = find_branch('output3', find_root=False)
self.assertEqual(branch.revision_history(), branch2.revision_history())
self.assertNotEqual(branch.revision_history(), branch3.revision_history())
# check revisions in the history.
rev = branch.get_revision(self._baz._empty_merged_tag_bzr_base)
rev2 = branch2.get_revision(self._baz._empty_merged_tag_bzr_base)
# they must be the same
self.assertEqual(rev, rev2)
# and we should get some expected values:
self.assertEqual(rev.committer, "Test User<test@example.org>")
self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--import--0--base-0")
self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_bzr_base)
# check next revisions in the history.
rev = branch.get_revision(self._baz._empty_merged_tag_bzr)
rev2 = branch2.get_revision(self._baz._empty_merged_tag_bzr)
# they must be the same
self.assertEqual(rev, rev2)
# and we should get some expected values:
self.assertEqual(rev.committer, "Test User<test@example.org>")
self.assertEqual(rev.message, "did a merge, yarh")
self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_bzr)
self.assertEqual(rev.parents[0].revision_id,
self._baz._empty_merged_tag_bzr_base)
self.assertEqual(rev.parents[1].revision_id,
self._baz._empty_tag_bzr)
# this tree should have nothing missing from that tree.
# FIXME there is no code for this right now.
# self.assertEqual(branch.missing_revisions(branch3), [])
def test_merge_branch_with_merges(self):
import_version('output', pybaz.Version(self._baz._empty_merged_tag_2),
self.collect)
# expected results:
# two commits, no files, revision identifiers of
# 'demo@DONOTUSE_c--import--0--base-0' and
# self._baz._empty_merged_tag_2_bzr_base
# self._baz._empty_merged_tag_2_bzr
# and a merged revision from the latter of
# self._baz._empty_merged_tag_bzr
branch = find_branch('output', find_root=False)
self.assertEqual(branch.revision_history(),
['Arch-1:demo@DONOTUSE%c--import--0--base-0',
self._baz._empty_merged_tag_2_bzr_base,
self._baz._empty_merged_tag_2_bzr])
# and again.
import_version('output2', pybaz.Version(self._baz._empty_merged_tag_2),
self.collect)
branch2 = find_branch('output2', find_root=False)
# and import what we should be merged up against for checking with.
import_version('output3', pybaz.Version(self._baz._empty_merged_tag),
self.collect)
branch3 = find_branch('output3', find_root=False)
self.assertEqual(branch.revision_history(), branch2.revision_history())
self.assertNotEqual(branch.revision_history(), branch3.revision_history())
# check revisions in the history.
rev = branch.get_revision(self._baz._empty_merged_tag_2_bzr_base)
rev2 = branch2.get_revision(self._baz._empty_merged_tag_2_bzr_base)
# they must be the same
self.assertEqual(rev, rev2)
# and we should get some expected values:
self.assertEqual(rev.committer, "Test User<test@example.org>")
self.assertEqual(rev.message, "tag of demo@DONOTUSE/c--import--0--base-0")
self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_2_bzr_base)
# check next revisions in the history.
rev = branch.get_revision(self._baz._empty_merged_tag_2_bzr)
rev2 = branch2.get_revision(self._baz._empty_merged_tag_2_bzr)
# they must be the same
self.assertEqual(rev, rev2)
# and we should get some expected values:
self.assertEqual(rev.committer, "Test User<test@example.org>")
self.assertEqual(rev.message, "merge in a merged tree.")
self.assertEqual(rev.revision_id, self._baz._empty_merged_tag_2_bzr)
self.assertEqual(rev.parents[0].revision_id,
self._baz._empty_merged_tag_2_bzr_base)
self.assertEqual(rev.parents[1].revision_id,
self._baz._empty_merged_tag_bzr)
# this tree should have nothing missing from that tree.
# FIXME there is no code for this right now.
# self.assertEqual(branch.missing_revisions(branch3), [])
def test_import_symlink(self):
import_version('output', pybaz.Version(self._baz._import_symlink),
self.collect)
# expected results:
# one commit, no files, revision identifier of 'demo@DONOTUSE_c--import--0--base-0'
branch = find_branch('output', find_root=False)
self.assertEqual(branch.revision_history(),
[self._baz._import_symlink_bzr])
rev = branch.get_revision(self._baz._import_symlink_bzr)
# and again.
import_version('output2', pybaz.Version(self._baz._import_symlink),
self.collect)
branch2 = find_branch('output2', find_root=False)
self.assertEqual(branch.revision_history(), branch2.revision_history())
rev2 = branch2.get_revision(self._baz._import_symlink_bzr)
# they must be the same
self.assertEqual(rev, rev2)
# and we should get some expected values:
self.assertEqual(rev.committer, "Test User<test@example.org>")
self.assertEqual(rev.message, "Import with a symlink")
self.assertEqual(rev.revision_id, self._baz._import_symlink_bzr)
# and we want the symlink alink with target 'missing-file-name'
inv = branch.get_inventory(rev.inventory_id)
self.assertEqual(inv.path2id('alink'), 'x_symlink_tag')
entry = inv['x_symlink_tag']
self.assertEqual(entry.kind, 'symlink')
self.assertEqual(entry.symlink_target, 'missing-file-name')
def test_missing_ancestor(self):
import_version('output', pybaz.Version(self._baz._missing_ancestor),
self.collect)
# expected results:
# one commits, no files, revision identifiers of
# 'demo@DONOTUSE_c--gone--0--base-0' and
# a merge of demo-gone@DONOTUSE%c--import--0
branch = find_branch('output', find_root=False)
self.assertEqual(branch.revision_history(),
[self._baz._missing_ancestor_bzr])
rev = branch.get_revision(self._baz._missing_ancestor_bzr)
# and again.
import_version('output2', pybaz.Version(self._baz._missing_ancestor),
self.collect)
branch2 = find_branch('output2', find_root=False)
self.assertEqual(branch.revision_history(), branch2.revision_history())
rev2 = branch2.get_revision(self._baz._missing_ancestor_bzr)
# they must be the same
self.assertEqual(rev, rev2)
# and we should get some expected values:
self.assertEqual(rev.committer, "Test User<test@example.org>")
self.assertEqual(rev.message, "tag of demo-gone@DONOTUSE/c--import--0--base-0")
self.assertEqual(rev.revision_id, self._baz._missing_ancestor_bzr)
self.assertEqual(rev.parents[0].revision_id,
self._baz._missing_import_bzr)
self.assertEqual(1, len(rev.parents))
# must NOT be able to get the merged evision
self.assertRaises(NoSuchRevision, branch.get_revision,
self._baz._missing_import_bzr)
class TestNamespacePrevious(TestCase):
def setUp(self):
TestCase.setUp(self)
self.version = pybaz.Version('foo@example.com/c--b--0')
def test_base0_none(self):
self.assertEqual(namespace_previous(self.version['base-0']), None)
def test_patch1_base0(self):
self.assertEqual(namespace_previous(self.version['patch-1']),
self.version['base-0'])
def test_patch3000_patch2999(self):
self.assertEqual(namespace_previous(self.version['patch-3000']),
self.version['patch-2999'])
def test_version0_raises(self):
self.assertRaises(RuntimeError, namespace_previous,
self.version['version-0'])
def test_version1_version0(self):
self.assertEqual(namespace_previous(self.version['version-1']),
self.version['version-0'])
def test_version3000_patch2999(self):
self.assertEqual(namespace_previous(self.version['version-3000']),
self.version['version-2999'])
class TestNamespaceMapping(TestCase):
def test_namespace_mapping_branch(self):
from baz_import import map_namespace
branch = pybaz.Branch('foo@example.com/c--b')
self.assertRaises(pybaz.errors.NamespaceError, map_namespace, branch)
self.assertEqual('c/b', map_namespace(branch['0']))
self.assertEqual('c/0.1/b', map_namespace(branch['0.1']))
def test_namespace_mapping_no_branch(self):
from baz_import import map_namespace
category = pybaz.Category('foo@example.com/c')
self.assertRaises(pybaz.errors.NamespaceError, map_namespace, category)
self.assertEqual('c/+trunk',
map_namespace(pybaz.Version("%s--0" % category)))
self.assertEqual('c/0.1/+trunk',
map_namespace(pybaz.Version('%s--0.1' % category)))
class TestImport(TestCaseInTempDir):
def setUp(self):
TestCaseInTempDir.setUp(self)
self._oldhome = os.environ['HOME']
self._tmpdir = tempfile.mkdtemp()
self._homedir = os.path.join(self._tmpdir, 'home')
os.mkdir(self._homedir)
os.environ['HOME'] = self._homedir
self._archiveroot = os.path.join(self._tmpdir, 'archive')
self._archive = pybaz.make_archive('demo@DONOTUSE',
str(self._archiveroot))
def tearDown(self):
os.environ['HOME'] = self._oldhome
shutil.rmtree(self._tmpdir)
TestCaseInTempDir.tearDown(self)
def make_import(self, namespace):
self._import = 'demo@DONOTUSE/%s' % namespace
os.mkdir(os.path.join(self._tmpdir, 'tree'))
tree = pybaz.init_tree(os.path.join(self._tmpdir, 'tree'), self._import)
msg = tree.log_message()
msg["summary"] = "I am importing now"
tree.import_(msg)
shutil.rmtree(os.path.join(self._tmpdir, 'tree'))
def test_cmd_exists(self):
from baz_import import cmd_baz_import
def test_empty_archive(self):
command = cmd_baz_import()
command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output')))
self.assertEqual(1,
len(list(os.walk(os.path.join(self._tmpdir,'output')))))
def test_two_branches(self):
self.make_import('c--0')
self.make_import('c1--branch--0.2')
command = cmd_baz_import()
command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output')))
self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output', 'c','+trunk')))
self.failUnless(os.path.exists(os.path.join(self._tmpdir,'output', 'c1', '0.2','branch')))
self.assertEqual(14,
len(list(os.walk(os.path.join(self._tmpdir,'output')))))
def test_run_twice(self):
self.make_import('c--0')
command = cmd_baz_import()
command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
command.run(os.path.join(self._tmpdir, 'output'), 'demo@DONOTUSE')
|