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
10
from bzrlib.plugins.bzrtools.upstream_import import (
18
from bzrlib.tests import TestCaseInTempDir
20
class DirFileWriter(object):
22
def __init__(self, fileobj, mode):
23
# We may be asked to 'append'. If so, fileobj already has a path.
24
# So we copy the existing tree, and overwrite afterward.
26
existing = fileobj.read()
28
path = tempfile.mkdtemp(dir=os.getcwd())
30
# copytree requires the directory not to exist
32
copytree(existing, path)
37
target_path = os.path.join(self.root, path)
38
parent = osutils.dirname(target_path)
39
if not os.path.exists(parent):
41
kind = osutils.file_kind(path)
43
copy2(path, target_path)
44
if kind == 'directory':
51
class TestImport(TestCaseInTempDir):
53
def make_tar(self, mode='w'):
55
return tarfile.open('project-0.1.tar', mode, fileobj)
56
return self.make_archive(maker)
58
def make_archive(self, maker, subdir=True):
60
archive_file = maker(result)
62
os.mkdir('project-0.1')
65
archive_file.add('project-0.1')
68
os.chdir('project-0.1')
69
os.mkdir(prefix + 'junk')
70
archive_file.add(prefix + 'junk')
72
f = file(prefix + 'README', 'wb')
75
archive_file.add(prefix + 'README')
77
f = file(prefix + 'FEEDME', 'wb')
80
archive_file.add(prefix + 'FEEDME')
90
def make_archive2(self, builder, subdir):
92
archive_file = builder(result)
93
os.mkdir('project-0.2')
97
archive_file.add('project-0.2')
100
os.chdir('project-0.2')
102
os.mkdir(prefix + 'junk')
103
archive_file.add(prefix + 'junk')
105
f = file(prefix + 'README', 'wb')
108
archive_file.add(prefix + 'README')
111
archive_file = builder(result, 'a')
112
archive_file.add(prefix + 'README')
121
def make_messed_tar(self):
123
tar_file = tarfile.open('project-0.1.tar', 'w', result)
124
os.mkdir('project-0.1')
125
tar_file.add('project-0.1')
127
os.mkdir('project-0.2')
128
tar_file.add('project-0.2')
130
f = file('project-0.1/README', 'wb')
133
tar_file.add('project-0.1/README')
135
rmtree('project-0.1')
141
return ZipFileWrapper(fileobj, 'w')
142
return self.make_archive(maker)
144
def test_top_directory(self):
145
self.assertEqual(top_directory('ab/b/c'), 'ab')
146
self.assertEqual(top_directory('/etc'), '/')
148
def test_common_directory(self):
149
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
150
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
151
self.assertIs(None, common_directory(['FEEDME']))
153
def test_untar(self):
154
def builder(fileobj, mode='w'):
155
return tarfile.open('project-0.1.tar', mode, fileobj)
156
self.archive_test(builder, import_tar)
158
def test_unzip(self):
159
def builder(fileobj, mode='w'):
160
return ZipFileWrapper(fileobj, mode)
161
self.archive_test(builder, import_zip)
163
def test_copydir_nosub(self):
164
def builder(fileobj, mode='w'):
165
return DirFileWriter(fileobj, mode)
166
# It would be bogus to test with the result in a subdirectory,
167
# because for directories, the input root is always the output root.
168
self.archive_test(builder, import_dir)
170
def archive_test(self, builder, importer, subdir=False):
171
archive_file = self.make_archive(builder, subdir)
172
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,
181
self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind,
183
f = file(tree.abspath('junk/food'), 'wb')
184
f.write('I like food\n')
187
archive_file = self.make_archive2(builder, subdir)
188
importer(tree, archive_file)
189
self.assertTrue(tree.path2id('README') is not None)
190
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
195
def test_untar2(self):
196
tar_file = self.make_messed_tar()
197
tree = BzrDir.create_standalone_workingtree('tree')
198
import_tar(tree, tar_file)
199
self.assertTrue(tree.path2id('project-0.1/README') is not None)
201
def test_untar_gzip(self):
202
tar_file = self.make_tar(mode='w:gz')
203
tree = BzrDir.create_standalone_workingtree('tree')
204
import_tar(tree, tar_file)
205
self.assertTrue(tree.path2id('README') is not None)
208
return makeSuite(TestImport)