~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2007-03-09 18:33:22 UTC
  • mfrom: (517.1.4 bzrtools-0.15)
  • Revision ID: abentley@panoramicfeedback.com-20070309183322-lsjv4t29wkppzda7
Merge fixes from 0.15

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 (
9
 
    osutils,
10
 
    revision as _mod_revision,
11
 
    transform
12
 
    )
 
8
from bzrlib import osutils
13
9
from bzrlib.bzrdir import BzrDir
14
 
from bzrlib.export.tar_exporter import export_tarball
15
 
from bzrlib.plugins.bzrtools import errors
16
10
from bzrlib.plugins.bzrtools.upstream_import import (
17
11
    common_directory,
18
 
    get_archive_type,
19
12
    import_archive,
20
13
    import_tar,
21
14
    import_zip,
22
15
    import_dir,
23
 
    top_path,
24
 
    ZipFileWrapper,
 
16
    top_directory,
 
17
    ZipFileWrapper, 
25
18
)
26
 
from bzrlib.tests import (
27
 
    TestCaseInTempDir,
28
 
    TestCaseWithTransport,
29
 
    UnicodeFilenameFeature,
30
 
    )
 
19
from bzrlib.tests import TestCaseInTempDir
31
20
 
32
21
 
33
22
def import_tar_broken(tree, tar_input):
92
81
                os.chdir('project-0.1')
93
82
            os.mkdir(prefix + 'junk')
94
83
            archive_file.add(prefix + 'junk')
95
 
 
 
84
            
96
85
            f = file(prefix + 'README', 'wb')
97
86
            f.write('What?')
98
87
            f.close()
122
111
            else:
123
112
                prefix=''
124
113
                os.chdir('project-0.2')
125
 
 
 
114
            
126
115
            os.mkdir(prefix + 'junk')
127
116
            archive_file.add(prefix + 'junk')
128
117
 
130
119
            f.write('Now?')
131
120
            f.close()
132
121
            archive_file.add(prefix + 'README')
 
122
            archive_file.close()
133
123
 
134
 
            f = file(prefix + 'README', 'wb')
135
 
            f.write('Wow?')
136
 
            f.close()
137
 
            # Add a second entry for README with different contents.
 
124
            archive_file = builder(result, 'a')
138
125
            archive_file.add(prefix + 'README')
139
126
            archive_file.close()
140
127
 
152
139
 
153
140
        os.mkdir('project-0.2')
154
141
        tar_file.add('project-0.2')
155
 
 
 
142
        
156
143
        f = file('project-0.1/README', 'wb')
157
144
        f.write('What?')
158
145
        f.close()
167
154
            return ZipFileWrapper(fileobj, 'w')
168
155
        return self.make_archive(maker)
169
156
 
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')
 
157
    def test_top_directory(self):
 
158
        self.assertEqual(top_directory('ab/b/c'), 'ab')
 
159
        self.assertEqual(top_directory('/etc'), '/')
186
160
 
187
161
    def test_common_directory(self):
188
162
        self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
189
163
        self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
190
 
        self.assertEqual('FEEDME', common_directory(['FEEDME']))
 
164
        self.assertIs(None, common_directory(['FEEDME']))
191
165
 
192
166
    def test_untar(self):
193
167
        def builder(fileobj, mode='w'):
231
205
            archive_file = self.make_archive2(builder, subdir)
232
206
            importer(tree, archive_file)
233
207
            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
208
            self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
238
209
        finally:
239
210
            tree.unlock()
243
214
        tar_file = self.make_messed_tar()
244
215
        tree = BzrDir.create_standalone_workingtree('tree')
245
216
        import_tar(tree, tar_file)
246
 
        self.assertTrue(tree.path2id('project-0.1/README') is not None)
 
217
        self.assertTrue(tree.path2id('project-0.1/README') is not None) 
247
218
 
248
219
    def test_untar_gzip(self):
249
220
        tar_file = self.make_tar(mode='w:gz')
250
221
        tree = BzrDir.create_standalone_workingtree('tree')
251
222
        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
 
 
 
223
        self.assertTrue(tree.path2id('README') is not None) 
303
224
 
304
225
def test_suite():
305
226
    return makeSuite(TestImport)