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 import errors
16
from bzrlib.plugins.bzrtools.upstream_import import (
26
from bzrlib.tests import (
28
TestCaseWithTransport,
29
UnicodeFilenameFeature,
33
def import_tar_broken(tree, tar_input):
35
Import a tarfile with names that that end in //, e.g. Feisty Python 2.5
37
tar_file = tarfile.open('lala', 'r', tar_input)
38
for member in tar_file.members:
39
if member.name.endswith('/'):
41
import_archive(tree, tar_file)
44
class DirFileWriter(object):
46
def __init__(self, fileobj, mode):
47
# We may be asked to 'append'. If so, fileobj already has a path.
48
# So we copy the existing tree, and overwrite afterward.
50
existing = fileobj.read()
52
path = tempfile.mkdtemp(dir=os.getcwd())
54
# copytree requires the directory not to exist
56
copytree(existing, path)
61
target_path = os.path.join(self.root, path)
62
parent = osutils.dirname(target_path)
63
if not os.path.exists(parent):
65
kind = osutils.file_kind(path)
67
copy2(path, target_path)
68
if kind == 'directory':
75
class TestImport(TestCaseInTempDir):
77
def make_tar(self, mode='w'):
79
return tarfile.open('project-0.1.tar', mode, fileobj)
80
return self.make_archive(maker)
82
def make_archive(self, maker, subdir=True):
84
archive_file = maker(result)
86
os.mkdir('project-0.1')
89
archive_file.add('project-0.1')
92
os.chdir('project-0.1')
93
os.mkdir(prefix + 'junk')
94
archive_file.add(prefix + 'junk')
96
f = file(prefix + 'README', 'wb')
99
archive_file.add(prefix + 'README')
101
f = file(prefix + 'FEEDME', 'wb')
104
archive_file.add(prefix + 'FEEDME')
110
rmtree('project-0.1')
114
def make_archive2(self, builder, subdir):
116
archive_file = builder(result)
117
os.mkdir('project-0.2')
120
prefix='project-0.2/'
121
archive_file.add('project-0.2')
124
os.chdir('project-0.2')
126
os.mkdir(prefix + 'junk')
127
archive_file.add(prefix + 'junk')
129
f = file(prefix + 'README', 'wb')
132
archive_file.add(prefix + 'README')
134
f = file(prefix + 'README', 'wb')
137
# Add a second entry for README with different contents.
138
archive_file.add(prefix + 'README')
147
def make_messed_tar(self):
149
tar_file = tarfile.open('project-0.1.tar', 'w', result)
150
os.mkdir('project-0.1')
151
tar_file.add('project-0.1')
153
os.mkdir('project-0.2')
154
tar_file.add('project-0.2')
156
f = file('project-0.1/README', 'wb')
159
tar_file.add('project-0.1/README')
161
rmtree('project-0.1')
167
return ZipFileWrapper(fileobj, 'w')
168
return self.make_archive(maker)
170
def make_tar_with_bzrdir(self):
172
tar_file = tarfile.open('tar-with-bzrdir.tar', 'w', result)
173
os.mkdir('toplevel-dir')
174
tar_file.add('toplevel-dir')
175
os.mkdir('toplevel-dir/.bzr')
176
tar_file.add('toplevel-dir/.bzr')
178
rmtree('toplevel-dir')
182
def test_top_path(self):
183
self.assertEqual(top_path('ab/b/c'), 'ab')
184
self.assertEqual(top_path('etc'), 'etc')
185
self.assertEqual(top_path('project-0.1'), 'project-0.1')
187
def test_common_directory(self):
188
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
189
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
190
self.assertEqual('FEEDME', common_directory(['FEEDME']))
192
def test_untar(self):
193
def builder(fileobj, mode='w'):
194
return tarfile.open('project-0.1.tar', mode, fileobj)
195
self.archive_test(builder, import_tar)
197
def test_broken_tar(self):
198
def builder(fileobj, mode='w'):
199
return tarfile.open('project-0.1.tar', mode, fileobj)
200
self.archive_test(builder, import_tar_broken, subdir=True)
202
def test_unzip(self):
203
def builder(fileobj, mode='w'):
204
return ZipFileWrapper(fileobj, mode)
205
self.archive_test(builder, import_zip)
207
def test_copydir_nosub(self):
208
def builder(fileobj, mode='w'):
209
return DirFileWriter(fileobj, mode)
210
# It would be bogus to test with the result in a subdirectory,
211
# because for directories, the input root is always the output root.
212
self.archive_test(builder, import_dir)
214
def archive_test(self, builder, importer, subdir=False):
215
archive_file = self.make_archive(builder, subdir)
216
tree = BzrDir.create_standalone_workingtree('tree')
219
importer(tree, archive_file)
220
self.assertTrue(tree.path2id('README') is not None)
221
self.assertTrue(tree.path2id('FEEDME') is not None)
222
self.assertTrue(os.path.isfile(tree.abspath('README')))
223
self.assertEqual(tree.inventory[tree.path2id('README')].kind,
225
self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind,
227
f = file(tree.abspath('junk/food'), 'wb')
228
f.write('I like food\n')
231
archive_file = self.make_archive2(builder, subdir)
232
importer(tree, archive_file)
233
self.assertTrue(tree.path2id('README') is not None)
234
# Ensure the second version of the file is used.
235
self.assertEqual(tree.get_file_text(tree.path2id('README')),
237
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
242
def test_untar2(self):
243
tar_file = self.make_messed_tar()
244
tree = BzrDir.create_standalone_workingtree('tree')
245
import_tar(tree, tar_file)
246
self.assertTrue(tree.path2id('project-0.1/README') is not None)
248
def test_untar_gzip(self):
249
tar_file = self.make_tar(mode='w:gz')
250
tree = BzrDir.create_standalone_workingtree('tree')
251
import_tar(tree, tar_file)
252
self.assertTrue(tree.path2id('README') is not None)
254
def test_no_crash_with_bzrdir(self):
255
tar_file = self.make_tar_with_bzrdir()
256
tree = BzrDir.create_standalone_workingtree('tree')
257
import_tar(tree, tar_file)
258
# So long as it did not crash, that should be ok
260
def test_get_archive_type(self):
261
self.assertEqual(('tar', None), get_archive_type('foo.tar'))
262
self.assertEqual(('zip', None), get_archive_type('foo.zip'))
263
self.assertRaises(errors.NotArchiveType, get_archive_type, 'foo.gif')
264
self.assertEqual(('tar', 'gz'), get_archive_type('foo.tar.gz'))
265
self.assertRaises(errors.NotArchiveType, get_archive_type,
267
self.assertEqual(('tar', 'gz'), get_archive_type('foo.tgz'))
268
self.assertEqual(('tar', 'lzma'), get_archive_type('foo.tar.lzma'))
269
self.assertEqual(('tar', 'lzma'), get_archive_type('foo.tar.xz'))
270
self.assertEqual(('tar', 'bz2'), get_archive_type('foo.tar.bz2'))
273
class TestWithStuff(TestCaseWithTransport):
275
def transform_to_tar(self, tt):
277
tarball = tarfile.open(None, 'w|', stream)
278
export_tarball(tt.get_preview_tree(), tarball, '')
281
def get_empty_tt(self):
282
b = self.make_repository('foo')
283
null_tree = b.revision_tree(_mod_revision.NULL_REVISION)
284
tt = transform.TransformPreview(null_tree)
285
root = tt.new_directory('', transform.ROOT_PARENT, 'tree-root')
287
self.addCleanup(tt.finalize)
290
def test_nonascii_paths(self):
291
self.requireFeature(UnicodeFilenameFeature)
292
tt = self.get_empty_tt()
293
encoded_file = tt.new_file(
294
u'\u1234file', tt.root, 'contents', 'new-file')
295
encoded_file = tt.new_file(
296
'other', tt.root, 'contents', 'other-file')
297
tarfile = self.transform_to_tar(tt)
299
tree = self.make_branch_and_tree('bar')
300
import_tar(tree, tarfile)
301
self.assertPathExists(u'bar/\u1234file')
305
return makeSuite(TestImport)