/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
    )
7490.49.1 by Jelmer Vernooij
Move more bzr tests.
29
from ..controldir import ControlDir
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())
7058.4.26 by Jelmer Vernooij
Fix upstream_import test.
69
        if existing != b'':
6637.1.2 by Jelmer Vernooij
Add tests.
70
            # copytree requires the directory not to exist
71
            os.rmdir(path)
72
            copytree(existing, path)
7058.4.26 by Jelmer Vernooij
Fix upstream_import test.
73
        fileobj.write(path.encode('utf-8'))
6637.1.2 by Jelmer Vernooij
Add tests.
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:
7143.15.2 by Jelmer Vernooij
Run autopep8.
104
                prefix = 'project-0.1/'
6637.1.2 by Jelmer Vernooij
Add tests.
105
                archive_file.add('project-0.1')
106
            else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
107
                prefix = ''
6637.1.2 by Jelmer Vernooij
Add tests.
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:
7143.15.2 by Jelmer Vernooij
Run autopep8.
134
                prefix = 'project-0.2/'
6637.1.2 by Jelmer Vernooij
Add tests.
135
                archive_file.add('project-0.2')
136
            else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
137
                prefix = ''
6637.1.2 by Jelmer Vernooij
Add tests.
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)
7490.49.1 by Jelmer Vernooij
Move more bzr tests.
225
        tree = ControlDir.create_standalone_workingtree('tree')
7058.4.26 by Jelmer Vernooij
Fix upstream_import test.
226
        with tree.lock_write():
6637.1.2 by Jelmer Vernooij
Add tests.
227
            importer(tree, archive_file)
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
228
            self.assertTrue(tree.is_versioned('README'))
229
            self.assertTrue(tree.is_versioned('FEEDME'))
6637.1.2 by Jelmer Vernooij
Add tests.
230
            self.assertTrue(os.path.isfile(tree.abspath('README')))
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
231
            self.assertEqual(tree.stored_kind('README'), 'file')
232
            self.assertEqual(tree.stored_kind('FEEDME'), 'file')
6973.7.5 by Jelmer Vernooij
s/file/open.
233
            with open(tree.abspath('junk/food'), 'wb') as f:
234
                f.write(b'I like food\n')
6637.1.2 by Jelmer Vernooij
Add tests.
235
6637.1.5 by Jelmer Vernooij
Ignore warning.
236
            with warnings.catch_warnings():
237
                warnings.simplefilter('ignore')
238
                archive_file = self.make_archive2(builder, subdir)
239
                importer(tree, archive_file)
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
240
            self.assertTrue(tree.is_versioned('README'))
6637.1.2 by Jelmer Vernooij
Add tests.
241
            # Ensure the second version of the file is used.
6973.7.3 by Jelmer Vernooij
Fix some more tests.
242
            self.assertEqual(tree.get_file_text('README'), b'Wow?')
6637.1.2 by Jelmer Vernooij
Add tests.
243
            self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
244
245
    def test_untar2(self):
246
        tar_file = self.make_messed_tar()
7490.49.1 by Jelmer Vernooij
Move more bzr tests.
247
        tree = ControlDir.create_standalone_workingtree('tree')
6637.1.2 by Jelmer Vernooij
Add tests.
248
        import_tar(tree, tar_file)
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
249
        self.assertTrue(tree.is_versioned('project-0.1/README'))
6637.1.2 by Jelmer Vernooij
Add tests.
250
251
    def test_untar_gzip(self):
252
        tar_file = self.make_tar(mode='w:gz')
7490.49.1 by Jelmer Vernooij
Move more bzr tests.
253
        tree = ControlDir.create_standalone_workingtree('tree')
6637.1.2 by Jelmer Vernooij
Add tests.
254
        import_tar(tree, tar_file)
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
255
        self.assertTrue(tree.is_versioned('README'))
6637.1.2 by Jelmer Vernooij
Add tests.
256
257
    def test_no_crash_with_bzrdir(self):
258
        tar_file = self.make_tar_with_bzrdir()
7490.49.1 by Jelmer Vernooij
Move more bzr tests.
259
        tree = ControlDir.create_standalone_workingtree('tree')
6637.1.2 by Jelmer Vernooij
Add tests.
260
        import_tar(tree, tar_file)
261
        # So long as it did not crash, that should be ok
262
263
    def test_get_archive_type(self):
264
        self.assertEqual(('tar', None), get_archive_type('foo.tar'))
265
        self.assertEqual(('zip', None), get_archive_type('foo.zip'))
266
        self.assertRaises(NotArchiveType, get_archive_type, 'foo.gif')
267
        self.assertEqual(('tar', 'gz'), get_archive_type('foo.tar.gz'))
268
        self.assertRaises(NotArchiveType, get_archive_type,
269
                          'foo.zip.gz')
270
        self.assertEqual(('tar', 'gz'), get_archive_type('foo.tgz'))
271
        self.assertEqual(('tar', 'lzma'), get_archive_type('foo.tar.lzma'))
272
        self.assertEqual(('tar', 'lzma'), get_archive_type('foo.tar.xz'))
273
        self.assertEqual(('tar', 'bz2'), get_archive_type('foo.tar.bz2'))
274
275
276
class TestWithStuff(TestCaseWithTransport):
277
278
    def transform_to_tar(self, tt):
7045.2.5 by Jelmer Vernooij
Fix po_merge and upstream_import tests.
279
        stream = BytesIO()
6637.1.2 by Jelmer Vernooij
Add tests.
280
        export(tt.get_preview_tree(), root='', fileobj=stream, format='tar',
281
               dest=None)
282
        return stream
283
284
    def get_empty_tt(self):
285
        b = self.make_repository('foo')
286
        null_tree = b.revision_tree(_mod_revision.NULL_REVISION)
7490.77.2 by Jelmer Vernooij
Split out git and bzr-specific transforms.
287
        tt = null_tree.preview_transform()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
288
        tt.new_directory('', transform.ROOT_PARENT, b'tree-root')
6637.1.2 by Jelmer Vernooij
Add tests.
289
        tt.fixup_new_roots()
290
        self.addCleanup(tt.finalize)
291
        return tt
292
293
    def test_nonascii_paths(self):
294
        self.requireFeature(UnicodeFilenameFeature)
295
        tt = self.get_empty_tt()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
296
        tt.new_file(u'\u1234file', tt.root, [b'contents'], b'new-file')
297
        tt.new_file('other', tt.root, [b'contents'], b'other-file')
6637.1.2 by Jelmer Vernooij
Add tests.
298
        tarfile = self.transform_to_tar(tt)
299
        tarfile.seek(0)
300
        tree = self.make_branch_and_tree('bar')
301
        import_tar(tree, tarfile)
302
        self.assertPathExists(u'bar/\u1234file')