~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2011-06-28 07:59:00 UTC
  • Revision ID: aaron@aaronbentley.com-20110628075900-9vg08zbpwvl52hat
Fix docstring.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
from StringIO import StringIO
 
3
from shutil import rmtree, copy2, copytree
 
4
import tarfile
 
5
import tempfile
 
6
from unittest import makeSuite
 
7
 
 
8
from bzrlib import (
 
9
    osutils,
 
10
    revision as _mod_revision,
 
11
    transform
 
12
    )
 
13
from bzrlib.bzrdir import BzrDir
 
14
from bzrlib.export.tar_exporter import export_tarball
 
15
from bzrlib.plugins.bzrtools import errors
 
16
from bzrlib.plugins.bzrtools.upstream_import import (
 
17
    common_directory,
 
18
    get_archive_type,
 
19
    import_archive,
 
20
    import_tar,
 
21
    import_zip,
 
22
    import_dir,
 
23
    top_path,
 
24
    ZipFileWrapper,
 
25
)
 
26
from bzrlib.tests import (
 
27
    TestCaseInTempDir,
 
28
    TestCaseWithTransport,
 
29
    UnicodeFilenameFeature,
 
30
    )
 
31
 
 
32
 
 
33
def import_tar_broken(tree, tar_input):
 
34
    """
 
35
    Import a tarfile with names that that end in //, e.g. Feisty Python 2.5
 
36
    """
 
37
    tar_file = tarfile.open('lala', 'r', tar_input)
 
38
    for member in tar_file.members:
 
39
        if member.name.endswith('/'):
 
40
            member.name += '/'
 
41
    import_archive(tree, tar_file)
 
42
 
 
43
 
 
44
class DirFileWriter(object):
 
45
 
 
46
    def __init__(self, fileobj, mode):
 
47
        # We may be asked to 'append'.  If so, fileobj already has a path.
 
48
        # So we copy the existing tree, and overwrite afterward.
 
49
        fileobj.seek(0)
 
50
        existing = fileobj.read()
 
51
        fileobj.seek(0)
 
52
        path = tempfile.mkdtemp(dir=os.getcwd())
 
53
        if existing != '':
 
54
            # copytree requires the directory not to exist
 
55
            os.rmdir(path)
 
56
            copytree(existing, path)
 
57
        fileobj.write(path)
 
58
        self.root = path
 
59
 
 
60
    def add(self, path):
 
61
        target_path = os.path.join(self.root, path)
 
62
        parent = osutils.dirname(target_path)
 
63
        if not os.path.exists(parent):
 
64
            os.makedirs(parent)
 
65
        kind = osutils.file_kind(path)
 
66
        if kind == 'file':
 
67
            copy2(path, target_path)
 
68
        if kind == 'directory':
 
69
            os.mkdir(target_path)
 
70
 
 
71
    def close(self):
 
72
        pass
 
73
 
 
74
 
 
75
class TestImport(TestCaseInTempDir):
 
76
 
 
77
    def make_tar(self, mode='w'):
 
78
        def maker(fileobj):
 
79
            return tarfile.open('project-0.1.tar', mode, fileobj)
 
80
        return self.make_archive(maker)
 
81
 
 
82
    def make_archive(self, maker, subdir=True):
 
83
        result = StringIO()
 
84
        archive_file = maker(result)
 
85
        try:
 
86
            os.mkdir('project-0.1')
 
87
            if subdir:
 
88
                prefix='project-0.1/'
 
89
                archive_file.add('project-0.1')
 
90
            else:
 
91
                prefix=''
 
92
                os.chdir('project-0.1')
 
93
            os.mkdir(prefix + 'junk')
 
94
            archive_file.add(prefix + 'junk')
 
95
 
 
96
            f = file(prefix + 'README', 'wb')
 
97
            f.write('What?')
 
98
            f.close()
 
99
            archive_file.add(prefix + 'README')
 
100
 
 
101
            f = file(prefix + 'FEEDME', 'wb')
 
102
            f.write('Hungry!!')
 
103
            f.close()
 
104
            archive_file.add(prefix + 'FEEDME')
 
105
 
 
106
            archive_file.close()
 
107
        finally:
 
108
            if not subdir:
 
109
                os.chdir('..')
 
110
            rmtree('project-0.1')
 
111
        result.seek(0)
 
112
        return result
 
113
 
 
114
    def make_archive2(self, builder, subdir):
 
115
        result = StringIO()
 
116
        archive_file = builder(result)
 
117
        os.mkdir('project-0.2')
 
118
        try:
 
119
            if subdir:
 
120
                prefix='project-0.2/'
 
121
                archive_file.add('project-0.2')
 
122
            else:
 
123
                prefix=''
 
124
                os.chdir('project-0.2')
 
125
 
 
126
            os.mkdir(prefix + 'junk')
 
127
            archive_file.add(prefix + 'junk')
 
128
 
 
129
            f = file(prefix + 'README', 'wb')
 
130
            f.write('Now?')
 
131
            f.close()
 
132
            archive_file.add(prefix + 'README')
 
133
 
 
134
            f = file(prefix + 'README', 'wb')
 
135
            f.write('Wow?')
 
136
            f.close()
 
137
            # Add a second entry for README with different contents.
 
138
            archive_file.add(prefix + 'README')
 
139
            archive_file.close()
 
140
 
 
141
        finally:
 
142
            if not subdir:
 
143
                os.chdir('..')
 
144
        result.seek(0)
 
145
        return result
 
146
 
 
147
    def make_messed_tar(self):
 
148
        result = StringIO()
 
149
        tar_file = tarfile.open('project-0.1.tar', 'w', result)
 
150
        os.mkdir('project-0.1')
 
151
        tar_file.add('project-0.1')
 
152
 
 
153
        os.mkdir('project-0.2')
 
154
        tar_file.add('project-0.2')
 
155
 
 
156
        f = file('project-0.1/README', 'wb')
 
157
        f.write('What?')
 
158
        f.close()
 
159
        tar_file.add('project-0.1/README')
 
160
        tar_file.close()
 
161
        rmtree('project-0.1')
 
162
        result.seek(0)
 
163
        return result
 
164
 
 
165
    def make_zip(self):
 
166
        def maker(fileobj):
 
167
            return ZipFileWrapper(fileobj, 'w')
 
168
        return self.make_archive(maker)
 
169
 
 
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')
 
186
 
 
187
    def test_common_directory(self):
 
188
        self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
 
189
        self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
 
190
        self.assertEqual('FEEDME', common_directory(['FEEDME']))
 
191
 
 
192
    def test_untar(self):
 
193
        def builder(fileobj, mode='w'):
 
194
            return tarfile.open('project-0.1.tar', mode, fileobj)
 
195
        self.archive_test(builder, import_tar)
 
196
 
 
197
    def test_broken_tar(self):
 
198
        def builder(fileobj, mode='w'):
 
199
            return tarfile.open('project-0.1.tar', mode, fileobj)
 
200
        self.archive_test(builder, import_tar_broken, subdir=True)
 
201
 
 
202
    def test_unzip(self):
 
203
        def builder(fileobj, mode='w'):
 
204
            return ZipFileWrapper(fileobj, mode)
 
205
        self.archive_test(builder, import_zip)
 
206
 
 
207
    def test_copydir_nosub(self):
 
208
        def builder(fileobj, mode='w'):
 
209
            return DirFileWriter(fileobj, mode)
 
210
        # It would be bogus to test with the result in a subdirectory,
 
211
        # because for directories, the input root is always the output root.
 
212
        self.archive_test(builder, import_dir)
 
213
 
 
214
    def archive_test(self, builder, importer, subdir=False):
 
215
        archive_file = self.make_archive(builder, subdir)
 
216
        tree = BzrDir.create_standalone_workingtree('tree')
 
217
        tree.lock_write()
 
218
        try:
 
219
            importer(tree, archive_file)
 
220
            self.assertTrue(tree.path2id('README') is not None)
 
221
            self.assertTrue(tree.path2id('FEEDME') is not None)
 
222
            self.assertTrue(os.path.isfile(tree.abspath('README')))
 
223
            self.assertEqual(tree.inventory[tree.path2id('README')].kind,
 
224
                'file')
 
225
            self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind,
 
226
                'file')
 
227
            f = file(tree.abspath('junk/food'), 'wb')
 
228
            f.write('I like food\n')
 
229
            f.close()
 
230
 
 
231
            archive_file = self.make_archive2(builder, subdir)
 
232
            importer(tree, archive_file)
 
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?')
 
237
            self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
 
238
        finally:
 
239
            tree.unlock()
 
240
 
 
241
 
 
242
    def test_untar2(self):
 
243
        tar_file = self.make_messed_tar()
 
244
        tree = BzrDir.create_standalone_workingtree('tree')
 
245
        import_tar(tree, tar_file)
 
246
        self.assertTrue(tree.path2id('project-0.1/README') is not None)
 
247
 
 
248
    def test_untar_gzip(self):
 
249
        tar_file = self.make_tar(mode='w:gz')
 
250
        tree = BzrDir.create_standalone_workingtree('tree')
 
251
        import_tar(tree, tar_file)
 
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
 
 
303
 
 
304
def test_suite():
 
305
    return makeSuite(TestImport)