6
6
from unittest import makeSuite
8
from bzrlib import osutils
10
revision as _mod_revision,
9
13
from bzrlib.bzrdir import BzrDir
14
from bzrlib.export.tar_exporter import export_tarball
15
from bzrlib.plugins.bzrtools import errors
10
16
from bzrlib.plugins.bzrtools.upstream_import import (
19
from bzrlib.tests import TestCaseInTempDir
26
from bzrlib.tests import (
28
TestCaseWithTransport,
29
UnicodeFilenameFeature,
22
33
def import_tar_broken(tree, tar_input):
154
167
return ZipFileWrapper(fileobj, 'w')
155
168
return self.make_archive(maker)
157
def test_top_directory(self):
158
self.assertEqual(top_directory('ab/b/c'), 'ab')
159
self.assertEqual(top_directory('/etc'), '/')
170
def make_tar_with_bzrdir(self):
172
tar_file = tarfile.open('tar-with-bzrdir.tar', 'w', result)
173
os.mkdir('toplevel-dir')
174
tar_file.add('toplevel-dir')
175
os.mkdir('toplevel-dir/.bzr')
176
tar_file.add('toplevel-dir/.bzr')
178
rmtree('toplevel-dir')
182
def test_top_path(self):
183
self.assertEqual(top_path('ab/b/c'), 'ab')
184
self.assertEqual(top_path('etc'), 'etc')
185
self.assertEqual(top_path('project-0.1'), 'project-0.1')
161
187
def test_common_directory(self):
162
188
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
163
189
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
164
self.assertIs(None, common_directory(['FEEDME']))
190
self.assertEqual('FEEDME', common_directory(['FEEDME']))
166
192
def test_untar(self):
167
193
def builder(fileobj, mode='w'):
214
243
tar_file = self.make_messed_tar()
215
244
tree = BzrDir.create_standalone_workingtree('tree')
216
245
import_tar(tree, tar_file)
217
self.assertTrue(tree.path2id('project-0.1/README') is not None)
246
self.assertTrue(tree.path2id('project-0.1/README') is not None)
219
248
def test_untar_gzip(self):
220
249
tar_file = self.make_tar(mode='w:gz')
221
250
tree = BzrDir.create_standalone_workingtree('tree')
222
251
import_tar(tree, tar_file)
223
self.assertTrue(tree.path2id('README') is not None)
252
self.assertTrue(tree.path2id('README') is not None)
254
def test_no_crash_with_bzrdir(self):
255
tar_file = self.make_tar_with_bzrdir()
256
tree = BzrDir.create_standalone_workingtree('tree')
257
import_tar(tree, tar_file)
258
# So long as it did not crash, that should be ok
260
def test_get_archive_type(self):
261
self.assertEqual(('tar', None), get_archive_type('foo.tar'))
262
self.assertEqual(('zip', None), get_archive_type('foo.zip'))
263
self.assertRaises(errors.NotArchiveType, get_archive_type, 'foo.gif')
264
self.assertEqual(('tar', 'gz'), get_archive_type('foo.tar.gz'))
265
self.assertRaises(errors.NotArchiveType, get_archive_type,
267
self.assertEqual(('tar', 'gz'), get_archive_type('foo.tgz'))
268
self.assertEqual(('tar', 'lzma'), get_archive_type('foo.tar.lzma'))
269
self.assertEqual(('tar', 'lzma'), get_archive_type('foo.tar.xz'))
270
self.assertEqual(('tar', 'bz2'), get_archive_type('foo.tar.bz2'))
273
class TestWithStuff(TestCaseWithTransport):
275
def transform_to_tar(self, tt):
277
tarball = tarfile.open(None, 'w|', stream)
278
export_tarball(tt.get_preview_tree(), tarball, '')
281
def get_empty_tt(self):
282
b = self.make_repository('foo')
283
null_tree = b.revision_tree(_mod_revision.NULL_REVISION)
284
tt = transform.TransformPreview(null_tree)
285
root = tt.new_directory('', transform.ROOT_PARENT, 'tree-root')
287
self.addCleanup(tt.finalize)
290
def test_nonascii_paths(self):
291
self.requireFeature(UnicodeFilenameFeature)
292
tt = self.get_empty_tt()
293
encoded_file = tt.new_file(
294
u'\u1234file', tt.root, 'contents', 'new-file')
295
encoded_file = tt.new_file(
296
'other', tt.root, 'contents', 'other-file')
297
tarfile = self.transform_to_tar(tt)
299
tree = self.make_branch_and_tree('bar')
300
import_tar(tree, tarfile)
301
self.assertPathExists(u'bar/\u1234file')
225
304
def test_suite():
226
305
return makeSuite(TestImport)