~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2006-12-12 16:50:31 UTC
  • Revision ID: abentley@panoramicfeedback.com-20061212165031-51w8gjy1eps1vnw0
update NEWS

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