/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6637.1.2 by Jelmer Vernooij
Add tests.
1
# Copyright (C) 2006-2012 Aaron Bentley
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
import os
7045.2.5 by Jelmer Vernooij
Fix po_merge and upstream_import tests.
18
from io import BytesIO
6637.1.2 by Jelmer Vernooij
Add tests.
19
from shutil import rmtree, copy2, copytree
20
import tarfile
21
import tempfile
6637.1.5 by Jelmer Vernooij
Ignore warning.
22
import warnings
6637.1.2 by Jelmer Vernooij
Add tests.
23
24
from .. import (
25
    osutils,
26
    revision as _mod_revision,
27
    transform
28
    )
6670.4.1 by Jelmer Vernooij
Update imports.
29
from ..bzr.bzrdir import BzrDir
6637.1.2 by Jelmer Vernooij
Add tests.
30
from ..export import export
31
from ..upstream_import import (
32
    common_directory,
33
    get_archive_type,
34
    import_archive,
35
    import_tar,
36
    import_zip,
37
    import_dir,
38
    NotArchiveType,
39
    top_path,
40
    ZipFileWrapper,
41
)
42
from . import (
43
    TestCaseInTempDir,
44
    TestCaseWithTransport,
45
    )
46
from .features import UnicodeFilenameFeature
47
48
49
def import_tar_broken(tree, tar_input):
50
    """
51
    Import a tarfile with names that that end in //, e.g. Feisty Python 2.5
52
    """
53
    tar_file = tarfile.open('lala', 'r', tar_input)
54
    for member in tar_file.members:
55
        if member.name.endswith('/'):
56
            member.name += '/'
57
    import_archive(tree, tar_file)
58
59
60
class DirFileWriter(object):
61
62
    def __init__(self, fileobj, mode):
63
        # We may be asked to 'append'.  If so, fileobj already has a path.
64
        # So we copy the existing tree, and overwrite afterward.
65
        fileobj.seek(0)
66
        existing = fileobj.read()
67
        fileobj.seek(0)
68
        path = tempfile.mkdtemp(dir=os.getcwd())
69
        if existing != '':
70
            # copytree requires the directory not to exist
71
            os.rmdir(path)
72
            copytree(existing, path)
73
        fileobj.write(path)
74
        self.root = path
75
76
    def add(self, path):
77
        target_path = os.path.join(self.root, path)
78
        parent = osutils.dirname(target_path)
79
        if not os.path.exists(parent):
80
            os.makedirs(parent)
81
        kind = osutils.file_kind(path)
82
        if kind == 'file':
83
            copy2(path, target_path)
84
        if kind == 'directory':
85
            os.mkdir(target_path)
86
87
    def close(self):
88
        pass
89
90
91
class TestImport(TestCaseInTempDir):
92
93
    def make_tar(self, mode='w'):
94
        def maker(fileobj):
95
            return tarfile.open('project-0.1.tar', mode, fileobj)
96
        return self.make_archive(maker)
97
98
    def make_archive(self, maker, subdir=True):
7045.2.5 by Jelmer Vernooij
Fix po_merge and upstream_import tests.
99
        result = BytesIO()
6637.1.2 by Jelmer Vernooij
Add tests.
100
        archive_file = maker(result)
101
        try:
102
            os.mkdir('project-0.1')
103
            if subdir:
104
                prefix='project-0.1/'
105
                archive_file.add('project-0.1')
106
            else:
107
                prefix=''
108
                os.chdir('project-0.1')
109
            os.mkdir(prefix + 'junk')
110
            archive_file.add(prefix + 'junk')
111
6973.7.5 by Jelmer Vernooij
s/file/open.
112
            with open(prefix + 'README', 'wb') as f:
113
                f.write(b'What?')
6637.1.2 by Jelmer Vernooij
Add tests.
114
            archive_file.add(prefix + 'README')
115
6973.7.5 by Jelmer Vernooij
s/file/open.
116
            with open(prefix + 'FEEDME', 'wb') as f:
117
                f.write(b'Hungry!!')
6637.1.2 by Jelmer Vernooij
Add tests.
118
            archive_file.add(prefix + 'FEEDME')
119
120
            archive_file.close()
121
        finally:
122
            if not subdir:
123
                os.chdir('..')
124
            rmtree('project-0.1')
125
        result.seek(0)
126
        return result
127
128
    def make_archive2(self, builder, subdir):
7045.2.5 by Jelmer Vernooij
Fix po_merge and upstream_import tests.
129
        result = BytesIO()
6637.1.2 by Jelmer Vernooij
Add tests.
130
        archive_file = builder(result)
131
        os.mkdir('project-0.2')
132
        try:
133
            if subdir:
134
                prefix='project-0.2/'
135
                archive_file.add('project-0.2')
136
            else:
137
                prefix=''
138
                os.chdir('project-0.2')
139
140
            os.mkdir(prefix + 'junk')
141
            archive_file.add(prefix + 'junk')
142
6973.7.5 by Jelmer Vernooij
s/file/open.
143
            with open(prefix + 'README', 'wb') as f:
144
                f.write(b'Now?')
6637.1.2 by Jelmer Vernooij
Add tests.
145
            archive_file.add(prefix + 'README')
146
6973.7.5 by Jelmer Vernooij
s/file/open.
147
            with open(prefix + 'README', 'wb') as f:
148
                f.write(b'Wow?')
6637.1.2 by Jelmer Vernooij
Add tests.
149
            # Add a second entry for README with different contents.
150
            archive_file.add(prefix + 'README')
151
            archive_file.close()
152
153
        finally:
154
            if not subdir:
155
                os.chdir('..')
156
        result.seek(0)
157
        return result
158
159
    def make_messed_tar(self):
7045.2.5 by Jelmer Vernooij
Fix po_merge and upstream_import tests.
160
        result = BytesIO()
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
161
        with tarfile.open('project-0.1.tar', 'w', result) as tar_file:
162
            os.mkdir('project-0.1')
163
            tar_file.add('project-0.1')
164
165
            os.mkdir('project-0.2')
166
            tar_file.add('project-0.2')
167
6973.7.5 by Jelmer Vernooij
s/file/open.
168
            with open('project-0.1/README', 'wb') as f:
169
                f.write(b'What?')
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
170
            tar_file.add('project-0.1/README')
6637.1.2 by Jelmer Vernooij
Add tests.
171
        rmtree('project-0.1')
172
        result.seek(0)
173
        return result
174
175
    def make_zip(self):
176
        def maker(fileobj):
177
            return ZipFileWrapper(fileobj, 'w')
178
        return self.make_archive(maker)
179
180
    def make_tar_with_bzrdir(self):
7045.2.5 by Jelmer Vernooij
Fix po_merge and upstream_import tests.
181
        result = BytesIO()
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
182
        with tarfile.open('tar-with-bzrdir.tar', 'w', result) as tar_file:
183
            os.mkdir('toplevel-dir')
184
            tar_file.add('toplevel-dir')
185
            os.mkdir('toplevel-dir/.bzr')
186
            tar_file.add('toplevel-dir/.bzr')
6637.1.2 by Jelmer Vernooij
Add tests.
187
        rmtree('toplevel-dir')
188
        result.seek(0)
189
        return result
190
191
    def test_top_path(self):
192
        self.assertEqual(top_path('ab/b/c'), 'ab')
193
        self.assertEqual(top_path('etc'), 'etc')
194
        self.assertEqual(top_path('project-0.1'), 'project-0.1')
195
196
    def test_common_directory(self):
197
        self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
198
        self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
199
        self.assertEqual('FEEDME', common_directory(['FEEDME']))
200
201
    def test_untar(self):
202
        def builder(fileobj, mode='w'):
203
            return tarfile.open('project-0.1.tar', mode, fileobj)
204
        self.archive_test(builder, import_tar)
205
206
    def test_broken_tar(self):
207
        def builder(fileobj, mode='w'):
208
            return tarfile.open('project-0.1.tar', mode, fileobj)
209
        self.archive_test(builder, import_tar_broken, subdir=True)
210
211
    def test_unzip(self):
212
        def builder(fileobj, mode='w'):
213
            return ZipFileWrapper(fileobj, mode)
214
        self.archive_test(builder, import_zip)
215
216
    def test_copydir_nosub(self):
217
        def builder(fileobj, mode='w'):
218
            return DirFileWriter(fileobj, mode)
219
        # It would be bogus to test with the result in a subdirectory,
220
        # because for directories, the input root is always the output root.
221
        self.archive_test(builder, import_dir)
222
223
    def archive_test(self, builder, importer, subdir=False):
224
        archive_file = self.make_archive(builder, subdir)
225
        tree = BzrDir.create_standalone_workingtree('tree')
226
        tree.lock_write()
227
        try:
228
            importer(tree, archive_file)
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
229
            self.assertTrue(tree.is_versioned('README'))
230
            self.assertTrue(tree.is_versioned('FEEDME'))
6637.1.2 by Jelmer Vernooij
Add tests.
231
            self.assertTrue(os.path.isfile(tree.abspath('README')))
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
232
            self.assertEqual(tree.stored_kind('README'), 'file')
233
            self.assertEqual(tree.stored_kind('FEEDME'), 'file')
6973.7.5 by Jelmer Vernooij
s/file/open.
234
            with open(tree.abspath('junk/food'), 'wb') as f:
235
                f.write(b'I like food\n')
6637.1.2 by Jelmer Vernooij
Add tests.
236
6637.1.5 by Jelmer Vernooij
Ignore warning.
237
            with warnings.catch_warnings():
238
                warnings.simplefilter('ignore')
239
                archive_file = self.make_archive2(builder, subdir)
240
                importer(tree, archive_file)
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
241
            self.assertTrue(tree.is_versioned('README'))
6637.1.2 by Jelmer Vernooij
Add tests.
242
            # Ensure the second version of the file is used.
6973.7.3 by Jelmer Vernooij
Fix some more tests.
243
            self.assertEqual(tree.get_file_text('README'), b'Wow?')
6637.1.2 by Jelmer Vernooij
Add tests.
244
            self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
245
        finally:
246
            tree.unlock()
247
248
249
    def test_untar2(self):
250
        tar_file = self.make_messed_tar()
251
        tree = BzrDir.create_standalone_workingtree('tree')
252
        import_tar(tree, tar_file)
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
253
        self.assertTrue(tree.is_versioned('project-0.1/README'))
6637.1.2 by Jelmer Vernooij
Add tests.
254
255
    def test_untar_gzip(self):
256
        tar_file = self.make_tar(mode='w:gz')
257
        tree = BzrDir.create_standalone_workingtree('tree')
258
        import_tar(tree, tar_file)
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
259
        self.assertTrue(tree.is_versioned('README'))
6637.1.2 by Jelmer Vernooij
Add tests.
260
261
    def test_no_crash_with_bzrdir(self):
262
        tar_file = self.make_tar_with_bzrdir()
263
        tree = BzrDir.create_standalone_workingtree('tree')
264
        import_tar(tree, tar_file)
265
        # So long as it did not crash, that should be ok
266
267
    def test_get_archive_type(self):
268
        self.assertEqual(('tar', None), get_archive_type('foo.tar'))
269
        self.assertEqual(('zip', None), get_archive_type('foo.zip'))
270
        self.assertRaises(NotArchiveType, get_archive_type, 'foo.gif')
271
        self.assertEqual(('tar', 'gz'), get_archive_type('foo.tar.gz'))
272
        self.assertRaises(NotArchiveType, get_archive_type,
273
                          'foo.zip.gz')
274
        self.assertEqual(('tar', 'gz'), get_archive_type('foo.tgz'))
275
        self.assertEqual(('tar', 'lzma'), get_archive_type('foo.tar.lzma'))
276
        self.assertEqual(('tar', 'lzma'), get_archive_type('foo.tar.xz'))
277
        self.assertEqual(('tar', 'bz2'), get_archive_type('foo.tar.bz2'))
278
279
280
class TestWithStuff(TestCaseWithTransport):
281
282
    def transform_to_tar(self, tt):
7045.2.5 by Jelmer Vernooij
Fix po_merge and upstream_import tests.
283
        stream = BytesIO()
6637.1.2 by Jelmer Vernooij
Add tests.
284
        export(tt.get_preview_tree(), root='', fileobj=stream, format='tar',
285
               dest=None)
286
        return stream
287
288
    def get_empty_tt(self):
289
        b = self.make_repository('foo')
290
        null_tree = b.revision_tree(_mod_revision.NULL_REVISION)
291
        tt = transform.TransformPreview(null_tree)
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
292
        root = tt.new_directory('', transform.ROOT_PARENT, b'tree-root')
6637.1.2 by Jelmer Vernooij
Add tests.
293
        tt.fixup_new_roots()
294
        self.addCleanup(tt.finalize)
295
        return tt
296
297
    def test_nonascii_paths(self):
298
        self.requireFeature(UnicodeFilenameFeature)
299
        tt = self.get_empty_tt()
300
        encoded_file = tt.new_file(
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
301
            u'\u1234file', tt.root, [b'contents'], b'new-file')
6637.1.2 by Jelmer Vernooij
Add tests.
302
        encoded_file = tt.new_file(
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
303
            'other', tt.root, [b'contents'], b'other-file')
6637.1.2 by Jelmer Vernooij
Add tests.
304
        tarfile = self.transform_to_tar(tt)
305
        tarfile.seek(0)
306
        tree = self.make_branch_and_tree('bar')
307
        import_tar(tree, tarfile)
308
        self.assertPathExists(u'bar/\u1234file')