6
6
from unittest import makeSuite
10
revision as _mod_revision,
8
from bzrlib import osutils
13
9
from bzrlib.bzrdir import BzrDir
14
from bzrlib.export import export
15
from bzrlib.plugins.bzrtools import errors
16
10
from bzrlib.plugins.bzrtools.upstream_import import (
26
from bzrlib.tests import (
28
TestCaseWithTransport,
31
from bzrlib.tests.features import UnicodeFilenameFeature
32
except ImportError: # bzr < 2.5
33
from bzrlib.tests import UnicodeFilenameFeature
19
from bzrlib.tests import TestCaseInTempDir
36
22
def import_tar_broken(tree, tar_input):
170
154
return ZipFileWrapper(fileobj, 'w')
171
155
return self.make_archive(maker)
173
def make_tar_with_bzrdir(self):
175
tar_file = tarfile.open('tar-with-bzrdir.tar', 'w', result)
176
os.mkdir('toplevel-dir')
177
tar_file.add('toplevel-dir')
178
os.mkdir('toplevel-dir/.bzr')
179
tar_file.add('toplevel-dir/.bzr')
181
rmtree('toplevel-dir')
185
def test_top_path(self):
186
self.assertEqual(top_path('ab/b/c'), 'ab')
187
self.assertEqual(top_path('etc'), 'etc')
188
self.assertEqual(top_path('project-0.1'), 'project-0.1')
157
def test_top_directory(self):
158
self.assertEqual(top_directory('ab/b/c'), 'ab')
159
self.assertEqual(top_directory('/etc'), '/')
190
161
def test_common_directory(self):
191
162
self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
192
163
self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
193
self.assertEqual('FEEDME', common_directory(['FEEDME']))
164
self.assertIs(None, common_directory(['FEEDME']))
195
166
def test_untar(self):
196
167
def builder(fileobj, mode='w'):
234
205
archive_file = self.make_archive2(builder, subdir)
235
206
importer(tree, archive_file)
236
207
self.assertTrue(tree.path2id('README') is not None)
237
# Ensure the second version of the file is used.
238
self.assertEqual(tree.get_file_text(tree.path2id('README')),
240
208
self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
254
222
import_tar(tree, tar_file)
255
223
self.assertTrue(tree.path2id('README') is not None)
257
def test_no_crash_with_bzrdir(self):
258
tar_file = self.make_tar_with_bzrdir()
259
tree = BzrDir.create_standalone_workingtree('tree')
260
import_tar(tree, tar_file)
261
# So long as it did not crash, that should be ok
263
def test_get_archive_type(self):
264
self.assertEqual(('tar', None), get_archive_type('foo.tar'))
265
self.assertEqual(('zip', None), get_archive_type('foo.zip'))
266
self.assertRaises(errors.NotArchiveType, get_archive_type, 'foo.gif')
267
self.assertEqual(('tar', 'gz'), get_archive_type('foo.tar.gz'))
268
self.assertRaises(errors.NotArchiveType, get_archive_type,
270
self.assertEqual(('tar', 'gz'), get_archive_type('foo.tgz'))
271
self.assertEqual(('tar', 'lzma'), get_archive_type('foo.tar.lzma'))
272
self.assertEqual(('tar', 'lzma'), get_archive_type('foo.tar.xz'))
273
self.assertEqual(('tar', 'bz2'), get_archive_type('foo.tar.bz2'))
276
class TestWithStuff(TestCaseWithTransport):
278
def transform_to_tar(self, tt):
280
export(tt.get_preview_tree(), root='', fileobj=stream, format='tar',
284
def get_empty_tt(self):
285
b = self.make_repository('foo')
286
null_tree = b.revision_tree(_mod_revision.NULL_REVISION)
287
tt = transform.TransformPreview(null_tree)
288
root = tt.new_directory('', transform.ROOT_PARENT, 'tree-root')
290
self.addCleanup(tt.finalize)
293
def test_nonascii_paths(self):
294
self.requireFeature(UnicodeFilenameFeature)
295
tt = self.get_empty_tt()
296
encoded_file = tt.new_file(
297
u'\u1234file', tt.root, 'contents', 'new-file')
298
encoded_file = tt.new_file(
299
'other', tt.root, 'contents', 'other-file')
300
tarfile = self.transform_to_tar(tt)
302
tree = self.make_branch_and_tree('bar')
303
import_tar(tree, tarfile)
304
self.assertPathExists(u'bar/\u1234file')
307
225
def test_suite():
308
226
return makeSuite(TestImport)