2
from StringIO import StringIO
3
from shutil import rmtree, copy2, copytree
6
from unittest import makeSuite
10
revision as _mod_revision,
13
from bzrlib.bzrdir import BzrDir
14
from bzrlib.export.tar_exporter import export_tarball
15
from bzrlib.plugins.bzrtools.upstream_import import (
24
from bzrlib.tests import (
26
TestCaseWithTransport,
27
UnicodeFilenameFeature,
31
def import_tar_broken(tree, tar_input):
33
Import a tarfile with names that that end in //, e.g. Feisty Python 2.5
35
tar_file = tarfile.open('lala', 'r', tar_input)
36
for member in tar_file.members:
37
if member.name.endswith('/'):
39
import_archive(tree, tar_file)
42
class DirFileWriter(object):
44
def __init__(self, fileobj, mode):
45
# We may be asked to 'append'. If so, fileobj already has a path.
46
# So we copy the existing tree, and overwrite afterward.
48
existing = fileobj.read()
50
path = tempfile.mkdtemp(dir=os.getcwd())
52
# copytree requires the directory not to exist
54
copytree(existing, path)
59
target_path = os.path.join(self.root, path)
60
parent = osutils.dirname(target_path)
61
if not os.path.exists(parent):
63
kind = osutils.file_kind(path)
65
copy2(path, target_path)
66
if kind == 'directory':
73
class TestImport(TestCaseInTempDir):
75
def make_tar(self, mode='w'):
77
return tarfile.open('project-0.1.tar', mode, fileobj)
78
return self.make_archive(maker)
80
def make_archive(self, maker, subdir=True):
82
archive_file = maker(result)
84
os.mkdir('project-0.1')
87
archive_file.add('project-0.1')
90
os.chdir('project-0.1')
91
os.mkdir(prefix + 'junk')
92
archive_file.add(prefix + 'junk')
94
f = file(prefix + 'README', 'wb')
97
archive_file.add(prefix + 'README')
99
f = file(prefix + 'FEEDME', 'wb')
102
archive_file.add(prefix + 'FEEDME')
108
rmtree('project-0.1')
112
def make_archive2(self, builder, subdir):
114
archive_file = builder(result)
115
os.mkdir('project-0.2')
118
prefix='project-0.2/'
119
archive_file.add('project-0.2')
122
os.chdir('project-0.2')
124
os.mkdir(prefix + 'junk')
125
archive_file.add(prefix + 'junk')
127
f = file(prefix + 'README', 'wb')
130
archive_file.add(prefix + 'README')
132
f = file(prefix + 'README', 'wb')
135
# Add a second entry for README with different contents.
136
archive_file.add(prefix + 'README')
145
def make_messed_tar(self):
147
tar_file = tarfile.open('project-0.1.tar', 'w', result)
148
os.mkdir('project-0.1')
149
tar_file.add('project-0.1')
151
os.mkdir('project-0.2')
152
tar_file.add('project-0.2')
154
f = file('project-0.1/README', 'wb')
157
tar_file.add('project-0.1/README')
159
rmtree('project-0.1')
165
return ZipFileWrapper(fileobj, 'w')
166
return self.make_archive(maker)
168
def make_tar_with_bzrdir(self):
170
tar_file = tarfile.open('tar-with-bzrdir.tar', 'w', result)
171
os.mkdir('toplevel-dir')
172
tar_file.add('toplevel-dir')
173
os.mkdir('toplevel-dir/.bzr')
174
tar_file.add('toplevel-dir/.bzr')
176
rmtree('toplevel-dir')
180
def test_top_path(self):
181
self.assertEqual(top_path('ab/b/c'), 'ab')
182
self.assertEqual(top_path('etc'), 'etc')
183
self.assertEqual(top_path('project-0.1'), 'project-0.1')
185
def test_common_directory(self):
186
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
187
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
188
self.assertEqual('FEEDME', common_directory(['FEEDME']))
190
def test_untar(self):
191
def builder(fileobj, mode='w'):
192
return tarfile.open('project-0.1.tar', mode, fileobj)
193
self.archive_test(builder, import_tar)
195
def test_broken_tar(self):
196
def builder(fileobj, mode='w'):
197
return tarfile.open('project-0.1.tar', mode, fileobj)
198
self.archive_test(builder, import_tar_broken, subdir=True)
200
def test_unzip(self):
201
def builder(fileobj, mode='w'):
202
return ZipFileWrapper(fileobj, mode)
203
self.archive_test(builder, import_zip)
205
def test_copydir_nosub(self):
206
def builder(fileobj, mode='w'):
207
return DirFileWriter(fileobj, mode)
208
# It would be bogus to test with the result in a subdirectory,
209
# because for directories, the input root is always the output root.
210
self.archive_test(builder, import_dir)
212
def archive_test(self, builder, importer, subdir=False):
213
archive_file = self.make_archive(builder, subdir)
214
tree = BzrDir.create_standalone_workingtree('tree')
217
importer(tree, archive_file)
218
self.assertTrue(tree.path2id('README') is not None)
219
self.assertTrue(tree.path2id('FEEDME') is not None)
220
self.assertTrue(os.path.isfile(tree.abspath('README')))
221
self.assertEqual(tree.inventory[tree.path2id('README')].kind,
223
self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind,
225
f = file(tree.abspath('junk/food'), 'wb')
226
f.write('I like food\n')
229
archive_file = self.make_archive2(builder, subdir)
230
importer(tree, archive_file)
231
self.assertTrue(tree.path2id('README') is not None)
232
# Ensure the second version of the file is used.
233
self.assertEqual(tree.get_file_text(tree.path2id('README')),
235
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
240
def test_untar2(self):
241
tar_file = self.make_messed_tar()
242
tree = BzrDir.create_standalone_workingtree('tree')
243
import_tar(tree, tar_file)
244
self.assertTrue(tree.path2id('project-0.1/README') is not None)
246
def test_untar_gzip(self):
247
tar_file = self.make_tar(mode='w:gz')
248
tree = BzrDir.create_standalone_workingtree('tree')
249
import_tar(tree, tar_file)
250
self.assertTrue(tree.path2id('README') is not None)
252
def test_no_crash_with_bzrdir(self):
253
tar_file = self.make_tar_with_bzrdir()
254
tree = BzrDir.create_standalone_workingtree('tree')
255
import_tar(tree, tar_file)
256
# So long as it did not crash, that should be ok
259
class TestWithStuff(TestCaseWithTransport):
261
def transform_to_tar(self, tt):
263
tarball = tarfile.open(None, 'w|', stream)
264
export_tarball(tt.get_preview_tree(), tarball, '')
267
def get_empty_tt(self):
268
b = self.make_repository('foo')
269
null_tree = b.revision_tree(_mod_revision.NULL_REVISION)
270
tt = transform.TransformPreview(null_tree)
271
root = tt.new_directory('', transform.ROOT_PARENT, 'tree-root')
273
self.addCleanup(tt.finalize)
276
def test_nonascii_paths(self):
277
self.requireFeature(UnicodeFilenameFeature)
278
tt = self.get_empty_tt()
279
encoded_file = tt.new_file(
280
u'\u1234file', tt.root, 'contents', 'new-file')
281
encoded_file = tt.new_file(
282
'other', tt.root, 'contents', 'other-file')
283
tarfile = self.transform_to_tar(tt)
285
tree = self.make_branch_and_tree('bar')
286
import_tar(tree, tarfile)
287
self.assertPathExists(u'bar/\u1234file')
291
return makeSuite(TestImport)