2
2
from StringIO import StringIO
3
from shutil import rmtree, copy2, copytree
3
from shutil import rmtree, copy2
6
5
from unittest import makeSuite
8
7
from bzrlib import osutils
9
8
from bzrlib.bzrdir import BzrDir
10
from bzrlib.plugins.bzrtools.upstream_import import (
10
from bzrlib.plugins.bzrtools.upstream_import import (
19
from bzrtools.upstream_import import (
19
26
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
29
class DirFileWriter(object):
35
31
def __init__(self, fileobj, mode):
36
# We may be asked to 'append'. If so, fileobj already has a path.
37
# So we copy the existing tree, and overwrite afterward.
39
existing = fileobj.read()
41
path = tempfile.mkdtemp(dir=os.getcwd())
43
# copytree requires the directory not to exist
45
copytree(existing, path)
34
self.root = os.path.realpath('tmp')
49
36
def add(self, path):
50
target_path = os.path.join(self.root, path)
51
parent = osutils.dirname(target_path)
52
if not os.path.exists(parent):
54
37
kind = osutils.file_kind(path)
56
copy2(path, target_path)
39
copy2(path, os.path.join(self.root, path))
57
40
if kind == 'directory':
41
os.mkdir(os.path.join(self.root, path))
103
def make_archive2(self, builder, subdir):
86
def make_archive2(self, builder):
104
87
result = StringIO()
105
archive_file = builder(result)
88
tar_file = builder(result)
106
89
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')
90
tar_file.add('project-0.2')
92
os.mkdir('project-0.2/junk')
93
tar_file.add('project-0.2/junk')
95
f = file('project-0.2/README', 'wb')
98
tar_file.add('project-0.2/README')
101
tar_file = builder(result, 'a')
102
tar_file.add('project-0.2/README')
104
rmtree('project-0.2')
134
107
def make_messed_tar(self):
168
141
return tarfile.open('project-0.1.tar', mode, fileobj)
169
142
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
144
def test_unzip(self):
177
145
def builder(fileobj, mode='w'):
178
146
return ZipFileWrapper(fileobj, mode)
181
149
def test_copydir_nosub(self):
182
150
def builder(fileobj, mode='w'):
183
151
return DirFileWriter(fileobj, mode)
184
# It would be bogus to test with the result in a subdirectory,
185
# because for directories, the input root is always the output root.
186
152
self.archive_test(builder, import_dir)
154
def test_copydir_sub(self):
155
def builder(fileobj, mode='w'):
156
return DirFileWriter(fileobj, mode)
157
self.archive_test(builder, import_dir, subdir=True)
159
def test_copydir_nosub(self):
160
def builder(fileobj, mode='w'):
161
return DirFileWriter(fileobj, mode)
162
self.archive_test(builder, import_dir, subdir=True)
188
164
def archive_test(self, builder, importer, subdir=False):
189
165
archive_file = self.make_archive(builder, subdir)
190
166
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')
167
importer(tree, archive_file)
168
self.assertTrue(tree.path2id('README') is not None)
169
self.assertTrue(tree.path2id('FEEDME') is not None)
170
self.assertTrue(os.path.isfile(tree.abspath('README')))
171
self.assertEqual(tree.inventory[tree.path2id('README')].kind, 'file')
172
self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind, 'file')
174
f = file(tree.abspath('junk/food'), 'wb')
175
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')))
178
archive_file = self.make_archive2(builder)
179
importer(tree, archive_file)
180
self.assertTrue(tree.path2id('README') is not None)
181
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
213
184
def test_untar2(self):
214
185
tar_file = self.make_messed_tar()
215
186
tree = BzrDir.create_standalone_workingtree('tree')
216
187
import_tar(tree, tar_file)
217
self.assertTrue(tree.path2id('project-0.1/README') is not None)
188
self.assertTrue(tree.path2id('project-0.1/README') is not None)
219
190
def test_untar_gzip(self):
220
191
tar_file = self.make_tar(mode='w:gz')
221
192
tree = BzrDir.create_standalone_workingtree('tree')
222
193
import_tar(tree, tar_file)
223
self.assertTrue(tree.path2id('README') is not None)
194
self.assertTrue(tree.path2id('README') is not None)
225
196
def test_suite():
226
197
return makeSuite(TestImport)