~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2005-11-10 20:15:27 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20051110201527-8caa84ae529a1871
Added Daniel Silverstone to credits

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.upstream_import import (
16
 
    common_directory,
17
 
    import_archive,
18
 
    import_tar,
19
 
    import_zip,
20
 
    import_dir,
21
 
    top_path,
22
 
    ZipFileWrapper,
23
 
)
24
 
from bzrlib.tests import (
25
 
    TestCaseInTempDir,
26
 
    TestCaseWithTransport,
27
 
    )
28
 
 
29
 
 
30
 
def import_tar_broken(tree, tar_input):
31
 
    """
32
 
    Import a tarfile with names that that end in //, e.g. Feisty Python 2.5
33
 
    """
34
 
    tar_file = tarfile.open('lala', 'r', tar_input)
35
 
    for member in tar_file.members:
36
 
        if member.name.endswith('/'):
37
 
            member.name += '/'
38
 
    import_archive(tree, tar_file)
39
 
 
40
 
 
41
 
class DirFileWriter(object):
42
 
 
43
 
    def __init__(self, fileobj, mode):
44
 
        # We may be asked to 'append'.  If so, fileobj already has a path.
45
 
        # So we copy the existing tree, and overwrite afterward.
46
 
        fileobj.seek(0)
47
 
        existing = fileobj.read()
48
 
        fileobj.seek(0)
49
 
        path = tempfile.mkdtemp(dir=os.getcwd())
50
 
        if existing != '':
51
 
            # copytree requires the directory not to exist
52
 
            os.rmdir(path)
53
 
            copytree(existing, path)
54
 
        fileobj.write(path)
55
 
        self.root = path
56
 
 
57
 
    def add(self, path):
58
 
        target_path = os.path.join(self.root, path)
59
 
        parent = osutils.dirname(target_path)
60
 
        if not os.path.exists(parent):
61
 
            os.makedirs(parent)
62
 
        kind = osutils.file_kind(path)
63
 
        if kind == 'file':
64
 
            copy2(path, target_path)
65
 
        if kind == 'directory':
66
 
            os.mkdir(target_path)
67
 
 
68
 
    def close(self):
69
 
        pass
70
 
 
71
 
 
72
 
class TestImport(TestCaseInTempDir):
73
 
 
74
 
    def make_tar(self, mode='w'):
75
 
        def maker(fileobj):
76
 
            return tarfile.open('project-0.1.tar', mode, fileobj)
77
 
        return self.make_archive(maker)
78
 
 
79
 
    def make_archive(self, maker, subdir=True):
80
 
        result = StringIO()
81
 
        archive_file = maker(result)
82
 
        try:
83
 
            os.mkdir('project-0.1')
84
 
            if subdir:
85
 
                prefix='project-0.1/'
86
 
                archive_file.add('project-0.1')
87
 
            else:
88
 
                prefix=''
89
 
                os.chdir('project-0.1')
90
 
            os.mkdir(prefix + 'junk')
91
 
            archive_file.add(prefix + 'junk')
92
 
 
93
 
            f = file(prefix + 'README', 'wb')
94
 
            f.write('What?')
95
 
            f.close()
96
 
            archive_file.add(prefix + 'README')
97
 
 
98
 
            f = file(prefix + 'FEEDME', 'wb')
99
 
            f.write('Hungry!!')
100
 
            f.close()
101
 
            archive_file.add(prefix + 'FEEDME')
102
 
 
103
 
            archive_file.close()
104
 
        finally:
105
 
            if not subdir:
106
 
                os.chdir('..')
107
 
            rmtree('project-0.1')
108
 
        result.seek(0)
109
 
        return result
110
 
 
111
 
    def make_archive2(self, builder, subdir):
112
 
        result = StringIO()
113
 
        archive_file = builder(result)
114
 
        os.mkdir('project-0.2')
115
 
        try:
116
 
            if subdir:
117
 
                prefix='project-0.2/'
118
 
                archive_file.add('project-0.2')
119
 
            else:
120
 
                prefix=''
121
 
                os.chdir('project-0.2')
122
 
 
123
 
            os.mkdir(prefix + 'junk')
124
 
            archive_file.add(prefix + 'junk')
125
 
 
126
 
            f = file(prefix + 'README', 'wb')
127
 
            f.write('Now?')
128
 
            f.close()
129
 
            archive_file.add(prefix + 'README')
130
 
 
131
 
            f = file(prefix + 'README', 'wb')
132
 
            f.write('Wow?')
133
 
            f.close()
134
 
            # Add a second entry for README with different contents.
135
 
            archive_file.add(prefix + 'README')
136
 
            archive_file.close()
137
 
 
138
 
        finally:
139
 
            if not subdir:
140
 
                os.chdir('..')
141
 
        result.seek(0)
142
 
        return result
143
 
 
144
 
    def make_messed_tar(self):
145
 
        result = StringIO()
146
 
        tar_file = tarfile.open('project-0.1.tar', 'w', result)
147
 
        os.mkdir('project-0.1')
148
 
        tar_file.add('project-0.1')
149
 
 
150
 
        os.mkdir('project-0.2')
151
 
        tar_file.add('project-0.2')
152
 
 
153
 
        f = file('project-0.1/README', 'wb')
154
 
        f.write('What?')
155
 
        f.close()
156
 
        tar_file.add('project-0.1/README')
157
 
        tar_file.close()
158
 
        rmtree('project-0.1')
159
 
        result.seek(0)
160
 
        return result
161
 
 
162
 
    def make_zip(self):
163
 
        def maker(fileobj):
164
 
            return ZipFileWrapper(fileobj, 'w')
165
 
        return self.make_archive(maker)
166
 
 
167
 
    def make_tar_with_bzrdir(self):
168
 
        result = StringIO()
169
 
        tar_file = tarfile.open('tar-with-bzrdir.tar', 'w', result)
170
 
        os.mkdir('toplevel-dir')
171
 
        tar_file.add('toplevel-dir')
172
 
        os.mkdir('toplevel-dir/.bzr')
173
 
        tar_file.add('toplevel-dir/.bzr')
174
 
        tar_file.close()
175
 
        rmtree('toplevel-dir')
176
 
        result.seek(0)
177
 
        return result
178
 
 
179
 
    def test_top_path(self):
180
 
        self.assertEqual(top_path('ab/b/c'), 'ab')
181
 
        self.assertEqual(top_path('etc'), 'etc')
182
 
        self.assertEqual(top_path('project-0.1'), 'project-0.1')
183
 
 
184
 
    def test_common_directory(self):
185
 
        self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
186
 
        self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
187
 
        self.assertEqual('FEEDME', common_directory(['FEEDME']))
188
 
 
189
 
    def test_untar(self):
190
 
        def builder(fileobj, mode='w'):
191
 
            return tarfile.open('project-0.1.tar', mode, fileobj)
192
 
        self.archive_test(builder, import_tar)
193
 
 
194
 
    def test_broken_tar(self):
195
 
        def builder(fileobj, mode='w'):
196
 
            return tarfile.open('project-0.1.tar', mode, fileobj)
197
 
        self.archive_test(builder, import_tar_broken, subdir=True)
198
 
 
199
 
    def test_unzip(self):
200
 
        def builder(fileobj, mode='w'):
201
 
            return ZipFileWrapper(fileobj, mode)
202
 
        self.archive_test(builder, import_zip)
203
 
 
204
 
    def test_copydir_nosub(self):
205
 
        def builder(fileobj, mode='w'):
206
 
            return DirFileWriter(fileobj, mode)
207
 
        # It would be bogus to test with the result in a subdirectory,
208
 
        # because for directories, the input root is always the output root.
209
 
        self.archive_test(builder, import_dir)
210
 
 
211
 
    def archive_test(self, builder, importer, subdir=False):
212
 
        archive_file = self.make_archive(builder, subdir)
213
 
        tree = BzrDir.create_standalone_workingtree('tree')
214
 
        tree.lock_write()
215
 
        try:
216
 
            importer(tree, archive_file)
217
 
            self.assertTrue(tree.path2id('README') is not None)
218
 
            self.assertTrue(tree.path2id('FEEDME') is not None)
219
 
            self.assertTrue(os.path.isfile(tree.abspath('README')))
220
 
            self.assertEqual(tree.inventory[tree.path2id('README')].kind,
221
 
                'file')
222
 
            self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind,
223
 
                'file')
224
 
            f = file(tree.abspath('junk/food'), 'wb')
225
 
            f.write('I like food\n')
226
 
            f.close()
227
 
 
228
 
            archive_file = self.make_archive2(builder, subdir)
229
 
            importer(tree, archive_file)
230
 
            self.assertTrue(tree.path2id('README') is not None)
231
 
            # Ensure the second version of the file is used.
232
 
            self.assertEqual(tree.get_file_text(tree.path2id('README')),
233
 
                             'Wow?')
234
 
            self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
235
 
        finally:
236
 
            tree.unlock()
237
 
 
238
 
 
239
 
    def test_untar2(self):
240
 
        tar_file = self.make_messed_tar()
241
 
        tree = BzrDir.create_standalone_workingtree('tree')
242
 
        import_tar(tree, tar_file)
243
 
        self.assertTrue(tree.path2id('project-0.1/README') is not None)
244
 
 
245
 
    def test_untar_gzip(self):
246
 
        tar_file = self.make_tar(mode='w:gz')
247
 
        tree = BzrDir.create_standalone_workingtree('tree')
248
 
        import_tar(tree, tar_file)
249
 
        self.assertTrue(tree.path2id('README') is not None)
250
 
 
251
 
    def test_no_crash_with_bzrdir(self):
252
 
        tar_file = self.make_tar_with_bzrdir()
253
 
        tree = BzrDir.create_standalone_workingtree('tree')
254
 
        import_tar(tree, tar_file)
255
 
        # So long as it did not crash, that should be ok
256
 
 
257
 
 
258
 
class TestWithStuff(TestCaseWithTransport):
259
 
 
260
 
    def transform_to_tar(self, tt):
261
 
        stream = StringIO()
262
 
        tarball = tarfile.open(None, 'w|', stream)
263
 
        export_tarball(tt.get_preview_tree(), tarball, '')
264
 
        return stream
265
 
 
266
 
    def get_empty_tt(self):
267
 
        b = self.make_repository('foo')
268
 
        null_tree = b.revision_tree(_mod_revision.NULL_REVISION)
269
 
        tt = transform.TransformPreview(null_tree)
270
 
        root = tt.new_directory('', transform.ROOT_PARENT, 'tree-root')
271
 
        tt.fixup_new_roots()
272
 
        self.addCleanup(tt.finalize)
273
 
        return tt
274
 
 
275
 
    def test_nonascii_paths(self):
276
 
        tt = self.get_empty_tt()
277
 
        encoded_file = tt.new_file(
278
 
            u'\u1234file', tt.root, 'contents', 'new-file')
279
 
        encoded_file = tt.new_file(
280
 
            'other', tt.root, 'contents', 'other-file')
281
 
        tarfile = self.transform_to_tar(tt)
282
 
        tarfile.seek(0)
283
 
        tree = self.make_branch_and_tree('bar')
284
 
        import_tar(tree, tarfile)
285
 
        self.assertPathExists(u'bar/\u1234file')
286
 
 
287
 
 
288
 
def test_suite():
289
 
    return makeSuite(TestImport)