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,
30
def import_tar_broken(tree, tar_input):
32
Import a tarfile with names that that end in //, e.g. Feisty Python 2.5
34
tar_file = tarfile.open('lala', 'r', tar_input)
35
for member in tar_file.members:
36
if member.name.endswith('/'):
38
import_archive(tree, tar_file)
41
class DirFileWriter(object):
43
def __init__(self, fileobj, mode):
44
# We may be asked to 'append'. If so, fileobj already has a path.
45
# So we copy the existing tree, and overwrite afterward.
47
existing = fileobj.read()
49
path = tempfile.mkdtemp(dir=os.getcwd())
51
# copytree requires the directory not to exist
53
copytree(existing, path)
58
target_path = os.path.join(self.root, path)
59
parent = osutils.dirname(target_path)
60
if not os.path.exists(parent):
62
kind = osutils.file_kind(path)
64
copy2(path, target_path)
65
if kind == 'directory':
72
class TestImport(TestCaseInTempDir):
74
def make_tar(self, mode='w'):
76
return tarfile.open('project-0.1.tar', mode, fileobj)
77
return self.make_archive(maker)
79
def make_archive(self, maker, subdir=True):
81
archive_file = maker(result)
83
os.mkdir('project-0.1')
86
archive_file.add('project-0.1')
89
os.chdir('project-0.1')
90
os.mkdir(prefix + 'junk')
91
archive_file.add(prefix + 'junk')
93
f = file(prefix + 'README', 'wb')
96
archive_file.add(prefix + 'README')
98
f = file(prefix + 'FEEDME', 'wb')
101
archive_file.add(prefix + 'FEEDME')
107
rmtree('project-0.1')
111
def make_archive2(self, builder, subdir):
113
archive_file = builder(result)
114
os.mkdir('project-0.2')
117
prefix='project-0.2/'
118
archive_file.add('project-0.2')
121
os.chdir('project-0.2')
123
os.mkdir(prefix + 'junk')
124
archive_file.add(prefix + 'junk')
126
f = file(prefix + 'README', 'wb')
129
archive_file.add(prefix + 'README')
131
f = file(prefix + 'README', 'wb')
134
# Add a second entry for README with different contents.
135
archive_file.add(prefix + 'README')
144
def make_messed_tar(self):
146
tar_file = tarfile.open('project-0.1.tar', 'w', result)
147
os.mkdir('project-0.1')
148
tar_file.add('project-0.1')
150
os.mkdir('project-0.2')
151
tar_file.add('project-0.2')
153
f = file('project-0.1/README', 'wb')
156
tar_file.add('project-0.1/README')
158
rmtree('project-0.1')
164
return ZipFileWrapper(fileobj, 'w')
165
return self.make_archive(maker)
167
def make_tar_with_bzrdir(self):
169
tar_file = tarfile.open('tar-with-bzrdir.tar', 'w', result)
170
os.mkdir('toplevel-dir')
171
tar_file.add('toplevel-dir')
172
os.mkdir('toplevel-dir/.bzr')
173
tar_file.add('toplevel-dir/.bzr')
175
rmtree('toplevel-dir')
179
def test_top_path(self):
180
self.assertEqual(top_path('ab/b/c'), 'ab')
181
self.assertEqual(top_path('etc'), 'etc')
182
self.assertEqual(top_path('project-0.1'), 'project-0.1')
184
def test_common_directory(self):
185
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
186
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
187
self.assertEqual('FEEDME', common_directory(['FEEDME']))
189
def test_untar(self):
190
def builder(fileobj, mode='w'):
191
return tarfile.open('project-0.1.tar', mode, fileobj)
192
self.archive_test(builder, import_tar)
194
def test_broken_tar(self):
195
def builder(fileobj, mode='w'):
196
return tarfile.open('project-0.1.tar', mode, fileobj)
197
self.archive_test(builder, import_tar_broken, subdir=True)
199
def test_unzip(self):
200
def builder(fileobj, mode='w'):
201
return ZipFileWrapper(fileobj, mode)
202
self.archive_test(builder, import_zip)
204
def test_copydir_nosub(self):
205
def builder(fileobj, mode='w'):
206
return DirFileWriter(fileobj, mode)
207
# It would be bogus to test with the result in a subdirectory,
208
# because for directories, the input root is always the output root.
209
self.archive_test(builder, import_dir)
211
def archive_test(self, builder, importer, subdir=False):
212
archive_file = self.make_archive(builder, subdir)
213
tree = BzrDir.create_standalone_workingtree('tree')
216
importer(tree, archive_file)
217
self.assertTrue(tree.path2id('README') is not None)
218
self.assertTrue(tree.path2id('FEEDME') is not None)
219
self.assertTrue(os.path.isfile(tree.abspath('README')))
220
self.assertEqual(tree.inventory[tree.path2id('README')].kind,
222
self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind,
224
f = file(tree.abspath('junk/food'), 'wb')
225
f.write('I like food\n')
228
archive_file = self.make_archive2(builder, subdir)
229
importer(tree, archive_file)
230
self.assertTrue(tree.path2id('README') is not None)
231
# Ensure the second version of the file is used.
232
self.assertEqual(tree.get_file_text(tree.path2id('README')),
234
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
239
def test_untar2(self):
240
tar_file = self.make_messed_tar()
241
tree = BzrDir.create_standalone_workingtree('tree')
242
import_tar(tree, tar_file)
243
self.assertTrue(tree.path2id('project-0.1/README') is not None)
245
def test_untar_gzip(self):
246
tar_file = self.make_tar(mode='w:gz')
247
tree = BzrDir.create_standalone_workingtree('tree')
248
import_tar(tree, tar_file)
249
self.assertTrue(tree.path2id('README') is not None)
251
def test_no_crash_with_bzrdir(self):
252
tar_file = self.make_tar_with_bzrdir()
253
tree = BzrDir.create_standalone_workingtree('tree')
254
import_tar(tree, tar_file)
255
# So long as it did not crash, that should be ok
258
class TestWithStuff(TestCaseWithTransport):
260
def transform_to_tar(self, tt):
262
tarball = tarfile.open(None, 'w|', stream)
263
export_tarball(tt.get_preview_tree(), tarball, '')
266
def get_empty_tt(self):
267
b = self.make_repository('foo')
268
null_tree = b.revision_tree(_mod_revision.NULL_REVISION)
269
tt = transform.TransformPreview(null_tree)
270
root = tt.new_directory('', transform.ROOT_PARENT, 'tree-root')
272
self.addCleanup(tt.finalize)
275
def test_nonascii_paths(self):
276
tt = self.get_empty_tt()
277
encoded_file = tt.new_file(
278
u'\u1234file', tt.root, 'contents', 'new-file')
279
encoded_file = tt.new_file(
280
'other', tt.root, 'contents', 'other-file')
281
tarfile = self.transform_to_tar(tt)
283
tree = self.make_branch_and_tree('bar')
284
import_tar(tree, tarfile)
285
self.assertPathExists(u'bar/\u1234file')
289
return makeSuite(TestImport)