~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2011-09-25 01:18:55 UTC
  • mfrom: (776.2.1 unused-imports)
  • Revision ID: aaron@aaronbentley.com-20110925011855-3dil4ijgluvzq7q5
Remove unused imports, fix the importing of two error classes. (jelmer)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import tempfile
6
6
from unittest import makeSuite
7
7
 
8
 
from bzrlib import osutils
 
8
from bzrlib import (
 
9
    osutils,
 
10
    revision as _mod_revision,
 
11
    transform
 
12
    )
9
13
from bzrlib.bzrdir import BzrDir
 
14
from bzrlib.export import export
 
15
from bzrlib.plugins.bzrtools import errors
10
16
from bzrlib.plugins.bzrtools.upstream_import import (
11
17
    common_directory,
 
18
    get_archive_type,
12
19
    import_archive,
13
20
    import_tar,
14
21
    import_zip,
15
22
    import_dir,
16
 
    top_directory,
 
23
    top_path,
17
24
    ZipFileWrapper,
18
25
)
19
 
from bzrlib.tests import TestCaseInTempDir
 
26
from bzrlib.tests import (
 
27
    TestCaseInTempDir,
 
28
    TestCaseWithTransport,
 
29
    )
 
30
try:
 
31
    from bzrlib.tests.features import UnicodeFilenameFeature
 
32
except ImportError: # bzr < 2.5
 
33
    from bzrlib.tests import UnicodeFilenameFeature
20
34
 
21
35
 
22
36
def import_tar_broken(tree, tar_input):
119
133
            f.write('Now?')
120
134
            f.close()
121
135
            archive_file.add(prefix + 'README')
122
 
            archive_file.close()
123
136
 
124
 
            archive_file = builder(result, 'a')
 
137
            f = file(prefix + 'README', 'wb')
 
138
            f.write('Wow?')
 
139
            f.close()
 
140
            # Add a second entry for README with different contents.
125
141
            archive_file.add(prefix + 'README')
126
142
            archive_file.close()
127
143
 
154
170
            return ZipFileWrapper(fileobj, 'w')
155
171
        return self.make_archive(maker)
156
172
 
157
 
    def test_top_directory(self):
158
 
        self.assertEqual(top_directory('ab/b/c'), 'ab')
159
 
        self.assertEqual(top_directory('/etc'), '/')
 
173
    def make_tar_with_bzrdir(self):
 
174
        result = StringIO()
 
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')
 
180
        tar_file.close()
 
181
        rmtree('toplevel-dir')
 
182
        result.seek(0)
 
183
        return result
 
184
 
 
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')
160
189
 
161
190
    def test_common_directory(self):
162
191
        self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
163
192
        self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
164
 
        self.assertIs(None, common_directory(['FEEDME']))
 
193
        self.assertEqual('FEEDME', common_directory(['FEEDME']))
165
194
 
166
195
    def test_untar(self):
167
196
        def builder(fileobj, mode='w'):
205
234
            archive_file = self.make_archive2(builder, subdir)
206
235
            importer(tree, archive_file)
207
236
            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')),
 
239
                             'Wow?')
208
240
            self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
209
241
        finally:
210
242
            tree.unlock()
222
254
        import_tar(tree, tar_file)
223
255
        self.assertTrue(tree.path2id('README') is not None)
224
256
 
 
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
 
262
 
 
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,
 
269
                          'foo.zip.gz')
 
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'))
 
274
 
 
275
 
 
276
class TestWithStuff(TestCaseWithTransport):
 
277
 
 
278
    def transform_to_tar(self, tt):
 
279
        stream = StringIO()
 
280
        export(tt.get_preview_tree(), root='', fileobj=stream, format='tar',
 
281
               dest=None)
 
282
        return stream
 
283
 
 
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')
 
289
        tt.fixup_new_roots()
 
290
        self.addCleanup(tt.finalize)
 
291
        return tt
 
292
 
 
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)
 
301
        tarfile.seek(0)
 
302
        tree = self.make_branch_and_tree('bar')
 
303
        import_tar(tree, tarfile)
 
304
        self.assertPathExists(u'bar/\u1234file')
 
305
 
 
306
 
225
307
def test_suite():
226
308
    return makeSuite(TestImport)