2
from StringIO import StringIO
3
from shutil import rmtree, copy2, copytree
6
from unittest import makeSuite
8
from bzrlib import osutils
9
from bzrlib.bzrdir import BzrDir
11
from bzrlib.plugins.bzrtools.upstream_import import (
20
from bzrtools.upstream_import import (
28
from bzrlib.tests import TestCaseInTempDir
31
class DirFileWriter(object):
33
def __init__(self, fileobj, mode):
34
# We may be asked to 'append'. If so, fileobj already has a path.
35
# So we copy the existing tree, and overwrite afterward.
37
existing = fileobj.read()
39
path = tempfile.mkdtemp(dir=os.getcwd())
41
# copytree requires the directory not to exist
43
copytree(existing, path)
48
target_path = os.path.join(self.root, path)
49
parent = osutils.dirname(target_path)
50
if not os.path.exists(parent):
52
kind = osutils.file_kind(path)
54
copy2(path, target_path)
55
if kind == 'directory':
62
class TestImport(TestCaseInTempDir):
64
def make_tar(self, mode='w'):
66
return tarfile.open('project-0.1.tar', mode, fileobj)
67
return self.make_archive(maker)
69
def make_archive(self, maker, subdir=True):
71
archive_file = maker(result)
73
os.mkdir('project-0.1')
76
archive_file.add('project-0.1')
79
os.chdir('project-0.1')
80
os.mkdir(prefix + 'junk')
81
archive_file.add(prefix + 'junk')
83
f = file(prefix + 'README', 'wb')
86
archive_file.add(prefix + 'README')
88
f = file(prefix + 'FEEDME', 'wb')
91
archive_file.add(prefix + 'FEEDME')
101
def make_archive2(self, builder, subdir):
103
archive_file = builder(result)
104
os.mkdir('project-0.2')
107
prefix='project-0.2/'
108
archive_file.add('project-0.2')
111
os.chdir('project-0.2')
113
os.mkdir(prefix + 'junk')
114
archive_file.add(prefix + 'junk')
116
f = file(prefix + 'README', 'wb')
119
archive_file.add(prefix + 'README')
122
archive_file = builder(result, 'a')
123
archive_file.add(prefix + 'README')
132
def make_messed_tar(self):
134
tar_file = tarfile.open('project-0.1.tar', 'w', result)
135
os.mkdir('project-0.1')
136
tar_file.add('project-0.1')
138
os.mkdir('project-0.2')
139
tar_file.add('project-0.2')
141
f = file('project-0.1/README', 'wb')
144
tar_file.add('project-0.1/README')
146
rmtree('project-0.1')
152
return ZipFileWrapper(fileobj, 'w')
153
return self.make_archive(maker)
155
def test_top_directory(self):
156
self.assertEqual(top_directory('ab/b/c'), 'ab')
157
self.assertEqual(top_directory('/etc'), '/')
159
def test_common_directory(self):
160
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
161
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
162
self.assertIs(None, common_directory(['FEEDME']))
164
def test_untar(self):
165
def builder(fileobj, mode='w'):
166
return tarfile.open('project-0.1.tar', mode, fileobj)
167
self.archive_test(builder, import_tar)
169
def test_unzip(self):
170
def builder(fileobj, mode='w'):
171
return ZipFileWrapper(fileobj, mode)
172
self.archive_test(builder, import_zip)
174
def test_copydir_nosub(self):
175
def builder(fileobj, mode='w'):
176
return DirFileWriter(fileobj, mode)
177
# It would be bogus to test with the result in a subdirectory,
178
# because for directories, the input root is always the output root.
179
self.archive_test(builder, import_dir)
181
def archive_test(self, builder, importer, subdir=False):
182
archive_file = self.make_archive(builder, subdir)
183
tree = BzrDir.create_standalone_workingtree('tree')
184
importer(tree, archive_file)
185
self.assertTrue(tree.path2id('README') is not None)
186
self.assertTrue(tree.path2id('FEEDME') is not None)
187
self.assertTrue(os.path.isfile(tree.abspath('README')))
188
self.assertEqual(tree.inventory[tree.path2id('README')].kind, 'file')
189
self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind, 'file')
191
f = file(tree.abspath('junk/food'), 'wb')
192
f.write('I like food\n')
195
archive_file = self.make_archive2(builder, subdir)
196
importer(tree, archive_file)
197
self.assertTrue(tree.path2id('README') is not None)
198
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
201
def test_untar2(self):
202
tar_file = self.make_messed_tar()
203
tree = BzrDir.create_standalone_workingtree('tree')
204
import_tar(tree, tar_file)
205
self.assertTrue(tree.path2id('project-0.1/README') is not None)
207
def test_untar_gzip(self):
208
tar_file = self.make_tar(mode='w:gz')
209
tree = BzrDir.create_standalone_workingtree('tree')
210
import_tar(tree, tar_file)
211
self.assertTrue(tree.path2id('README') is not None)
214
return makeSuite(TestImport)