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 (
27
from bzrlib.tests import TestCaseInTempDir
30
class DirFileWriter(object):
32
def __init__(self, fileobj, mode):
33
# We may be asked to 'append'. If so, fileobj already has a path.
34
# So we copy the existing tree, and overwrite afterward.
36
existing = fileobj.read()
38
path = tempfile.mkdtemp(dir=os.getcwd())
40
# copytree requires the directory not to exist
42
copytree(existing, path)
47
target_path = os.path.join(self.root, path)
48
parent = osutils.dirname(target_path)
49
if not os.path.exists(parent):
51
kind = osutils.file_kind(path)
53
copy2(path, target_path)
54
if kind == 'directory':
61
class TestImport(TestCaseInTempDir):
63
def make_tar(self, mode='w'):
65
return tarfile.open('project-0.1.tar', mode, fileobj)
66
return self.make_archive(maker)
68
def make_archive(self, maker, subdir=True):
70
archive_file = maker(result)
72
os.mkdir('project-0.1')
75
archive_file.add('project-0.1')
78
os.chdir('project-0.1')
79
os.mkdir(prefix + 'junk')
80
archive_file.add(prefix + 'junk')
82
f = file(prefix + 'README', 'wb')
85
archive_file.add(prefix + 'README')
87
f = file(prefix + 'FEEDME', 'wb')
90
archive_file.add(prefix + 'FEEDME')
100
def make_archive2(self, builder):
102
tar_file = builder(result)
103
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')
123
def make_messed_tar(self):
125
tar_file = tarfile.open('project-0.1.tar', 'w', result)
126
os.mkdir('project-0.1')
127
tar_file.add('project-0.1')
129
os.mkdir('project-0.2')
130
tar_file.add('project-0.2')
132
f = file('project-0.1/README', 'wb')
135
tar_file.add('project-0.1/README')
137
rmtree('project-0.1')
143
return ZipFileWrapper(fileobj, 'w')
144
return self.make_archive(maker)
146
def test_top_directory(self):
147
self.assertEqual(top_directory('ab/b/c'), 'ab')
148
self.assertEqual(top_directory('/etc'), '/')
150
def test_common_directory(self):
151
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
152
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
153
self.assertIs(None, common_directory(['FEEDME']))
155
def test_untar(self):
156
def builder(fileobj, mode='w'):
157
return tarfile.open('project-0.1.tar', mode, fileobj)
158
self.archive_test(builder, import_tar)
160
def test_unzip(self):
161
def builder(fileobj, mode='w'):
162
return ZipFileWrapper(fileobj, mode)
163
self.archive_test(builder, import_zip)
165
def test_copydir_nosub(self):
166
def builder(fileobj, mode='w'):
167
return DirFileWriter(fileobj, mode)
168
# It would be bogus to test with the result in a subdirectory,
169
# because for directories, the input root is always the output root.
170
self.archive_test(builder, import_dir)
172
def archive_test(self, builder, importer, subdir=False):
173
archive_file = self.make_archive(builder, subdir)
174
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')
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')))
192
def test_untar2(self):
193
tar_file = self.make_messed_tar()
194
tree = BzrDir.create_standalone_workingtree('tree')
195
import_tar(tree, tar_file)
196
self.assertTrue(tree.path2id('project-0.1/README') is not None)
198
def test_untar_gzip(self):
199
tar_file = self.make_tar(mode='w:gz')
200
tree = BzrDir.create_standalone_workingtree('tree')
201
import_tar(tree, tar_file)
202
self.assertTrue(tree.path2id('README') is not None)
205
return makeSuite(TestImport)