8
8
from bzrlib import osutils
9
9
from bzrlib.bzrdir import BzrDir
10
from bzrlib.plugins.bzrtools.upstream_import import (
11
from bzrlib.plugins.bzrtools.upstream_import import (
20
from bzrtools.upstream_import import (
19
27
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)
33
30
class DirFileWriter(object):
35
32
def __init__(self, fileobj, mode):
103
def make_archive2(self, builder, subdir):
100
def make_archive2(self, builder):
104
101
result = StringIO()
105
archive_file = builder(result)
102
tar_file = builder(result)
106
103
os.mkdir('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')
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')
168
157
return tarfile.open('project-0.1.tar', mode, fileobj)
169
158
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)
176
160
def test_unzip(self):
177
161
def builder(fileobj, mode='w'):
178
162
return ZipFileWrapper(fileobj, mode)
188
172
def archive_test(self, builder, importer, subdir=False):
189
173
archive_file = self.make_archive(builder, subdir)
190
174
tree = BzrDir.create_standalone_workingtree('tree')
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')
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')
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')))
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')))
213
192
def test_untar2(self):
214
193
tar_file = self.make_messed_tar()
215
194
tree = BzrDir.create_standalone_workingtree('tree')
216
195
import_tar(tree, tar_file)
217
self.assertTrue(tree.path2id('project-0.1/README') is not None)
196
self.assertTrue(tree.path2id('project-0.1/README') is not None)
219
198
def test_untar_gzip(self):
220
199
tar_file = self.make_tar(mode='w:gz')
221
200
tree = BzrDir.create_standalone_workingtree('tree')
222
201
import_tar(tree, tar_file)
223
self.assertTrue(tree.path2id('README') is not None)
202
self.assertTrue(tree.path2id('README') is not None)
225
204
def test_suite():
226
205
return makeSuite(TestImport)