~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2011-06-27 23:07:10 UTC
  • Revision ID: aaron@aaronbentley.com-20110627230710-orth0tzf1kwknfen
Better handling of compound tar names.

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.tar_exporter import export_tarball
 
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,
17
 
    ZipFileWrapper, 
 
23
    top_path,
 
24
    ZipFileWrapper,
18
25
)
19
 
from bzrlib.tests import TestCaseInTempDir
 
26
from bzrlib.tests import (
 
27
    TestCaseInTempDir,
 
28
    TestCaseWithTransport,
 
29
    UnicodeFilenameFeature,
 
30
    )
20
31
 
21
32
 
22
33
def import_tar_broken(tree, tar_input):
81
92
                os.chdir('project-0.1')
82
93
            os.mkdir(prefix + 'junk')
83
94
            archive_file.add(prefix + 'junk')
84
 
            
 
95
 
85
96
            f = file(prefix + 'README', 'wb')
86
97
            f.write('What?')
87
98
            f.close()
111
122
            else:
112
123
                prefix=''
113
124
                os.chdir('project-0.2')
114
 
            
 
125
 
115
126
            os.mkdir(prefix + 'junk')
116
127
            archive_file.add(prefix + 'junk')
117
128
 
119
130
            f.write('Now?')
120
131
            f.close()
121
132
            archive_file.add(prefix + 'README')
122
 
            archive_file.close()
123
133
 
124
 
            archive_file = builder(result, 'a')
 
134
            f = file(prefix + 'README', 'wb')
 
135
            f.write('Wow?')
 
136
            f.close()
 
137
            # Add a second entry for README with different contents.
125
138
            archive_file.add(prefix + 'README')
126
139
            archive_file.close()
127
140
 
139
152
 
140
153
        os.mkdir('project-0.2')
141
154
        tar_file.add('project-0.2')
142
 
        
 
155
 
143
156
        f = file('project-0.1/README', 'wb')
144
157
        f.write('What?')
145
158
        f.close()
154
167
            return ZipFileWrapper(fileobj, 'w')
155
168
        return self.make_archive(maker)
156
169
 
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):
 
171
        result = StringIO()
 
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')
 
177
        tar_file.close()
 
178
        rmtree('toplevel-dir')
 
179
        result.seek(0)
 
180
        return result
 
181
 
 
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')
160
186
 
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']))
165
191
 
166
192
    def test_untar(self):
167
193
        def builder(fileobj, mode='w'):
205
231
            archive_file = self.make_archive2(builder, subdir)
206
232
            importer(tree, archive_file)
207
233
            self.assertTrue(tree.path2id('README') is not None)
 
234
            # Ensure the second version of the file is used.
 
235
            self.assertEqual(tree.get_file_text(tree.path2id('README')),
 
236
                             'Wow?')
208
237
            self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
209
238
        finally:
210
239
            tree.unlock()
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)
218
247
 
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)
 
253
 
 
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
 
259
 
 
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,
 
266
                          'foo.zip.gz')
 
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'))
 
271
 
 
272
 
 
273
class TestWithStuff(TestCaseWithTransport):
 
274
 
 
275
    def transform_to_tar(self, tt):
 
276
        stream = StringIO()
 
277
        tarball = tarfile.open(None, 'w|', stream)
 
278
        export_tarball(tt.get_preview_tree(), tarball, '')
 
279
        return stream
 
280
 
 
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')
 
286
        tt.fixup_new_roots()
 
287
        self.addCleanup(tt.finalize)
 
288
        return tt
 
289
 
 
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)
 
298
        tarfile.seek(0)
 
299
        tree = self.make_branch_and_tree('bar')
 
300
        import_tar(tree, tarfile)
 
301
        self.assertPathExists(u'bar/\u1234file')
 
302
 
224
303
 
225
304
def test_suite():
226
305
    return makeSuite(TestImport)