8
8
from bzrlib import osutils
9
9
from bzrlib.bzrdir import BzrDir
11
from bzrlib.plugins.bzrtools.upstream_import import (
20
from bzrtools.upstream_import import (
10
from bzrlib.plugins.bzrtools.upstream_import import (
27
19
from bzrlib.tests import TestCaseInTempDir
22
def import_tar_broken(tree, tar_input):
24
Import a tarfile with names that that end in //, e.g. Feisty Python 2.5
26
tar_file = tarfile.open('lala', 'r', tar_input)
27
for member in tar_file.members:
28
if member.name.endswith('/'):
30
import_archive(tree, tar_file)
30
33
class DirFileWriter(object):
32
35
def __init__(self, fileobj, mode):
100
def make_archive2(self, builder):
103
def make_archive2(self, builder, subdir):
101
104
result = StringIO()
102
tar_file = builder(result)
105
archive_file = builder(result)
103
106
os.mkdir('project-0.2')
104
tar_file.add('project-0.2')
106
os.mkdir('project-0.2/junk')
107
tar_file.add('project-0.2/junk')
109
f = file('project-0.2/README', 'wb')
112
tar_file.add('project-0.2/README')
115
tar_file = builder(result, 'a')
116
tar_file.add('project-0.2/README')
118
rmtree('project-0.2')
109
prefix='project-0.2/'
110
archive_file.add('project-0.2')
113
os.chdir('project-0.2')
115
os.mkdir(prefix + 'junk')
116
archive_file.add(prefix + 'junk')
118
f = file(prefix + 'README', 'wb')
121
archive_file.add(prefix + 'README')
124
archive_file = builder(result, 'a')
125
archive_file.add(prefix + 'README')
157
168
return tarfile.open('project-0.1.tar', mode, fileobj)
158
169
self.archive_test(builder, import_tar)
171
def test_broken_tar(self):
172
def builder(fileobj, mode='w'):
173
return tarfile.open('project-0.1.tar', mode, fileobj)
174
self.archive_test(builder, import_tar_broken, subdir=True)
160
176
def test_unzip(self):
161
177
def builder(fileobj, mode='w'):
162
178
return ZipFileWrapper(fileobj, mode)
172
188
def archive_test(self, builder, importer, subdir=False):
173
189
archive_file = self.make_archive(builder, subdir)
174
190
tree = BzrDir.create_standalone_workingtree('tree')
175
importer(tree, archive_file)
176
self.assertTrue(tree.path2id('README') is not None)
177
self.assertTrue(tree.path2id('FEEDME') is not None)
178
self.assertTrue(os.path.isfile(tree.abspath('README')))
179
self.assertEqual(tree.inventory[tree.path2id('README')].kind, 'file')
180
self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind, 'file')
182
f = file(tree.abspath('junk/food'), 'wb')
183
f.write('I like food\n')
193
importer(tree, archive_file)
194
self.assertTrue(tree.path2id('README') is not None)
195
self.assertTrue(tree.path2id('FEEDME') is not None)
196
self.assertTrue(os.path.isfile(tree.abspath('README')))
197
self.assertEqual(tree.inventory[tree.path2id('README')].kind,
199
self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind,
201
f = file(tree.abspath('junk/food'), 'wb')
202
f.write('I like food\n')
186
archive_file = self.make_archive2(builder)
187
importer(tree, archive_file)
188
self.assertTrue(tree.path2id('README') is not None)
189
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
205
archive_file = self.make_archive2(builder, subdir)
206
importer(tree, archive_file)
207
self.assertTrue(tree.path2id('README') is not None)
208
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
192
213
def test_untar2(self):
193
214
tar_file = self.make_messed_tar()
194
215
tree = BzrDir.create_standalone_workingtree('tree')
195
216
import_tar(tree, tar_file)
196
self.assertTrue(tree.path2id('project-0.1/README') is not None)
217
self.assertTrue(tree.path2id('project-0.1/README') is not None)
198
219
def test_untar_gzip(self):
199
220
tar_file = self.make_tar(mode='w:gz')
200
221
tree = BzrDir.create_standalone_workingtree('tree')
201
222
import_tar(tree, tar_file)
202
self.assertTrue(tree.path2id('README') is not None)
223
self.assertTrue(tree.path2id('README') is not None)
204
225
def test_suite():
205
226
return makeSuite(TestImport)