~bzr-pqm/bzr/bzr.dev

3408.7.1 by Martin Pool
Support tarball export to stdout
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
"""Black-box tests for bzr export.
19
"""
20
3408.7.1 by Martin Pool
Support tarball export to stdout
21
from StringIO import StringIO
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
22
import os
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
23
import stat
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
24
import sys
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
25
import tarfile
26
import zipfile
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
27
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
28
from bzrlib.export import (
29
    zip_exporter,
30
    )
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
31
from bzrlib.tests import TestSkipped
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
32
from bzrlib.tests.blackbox import ExternalBase
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
33
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
34
35
class TestExport(ExternalBase):
36
37
    def test_tar_export(self):
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
38
        tree = self.make_branch_and_tree('tar')
39
        self.build_tree(['tar/a'])
40
        tree.add('a')
3577.3.1 by Ian Clatworthy
do not export .bzrrules
41
        self.build_tree_contents([('tar/.bzrrules', '')])
42
        tree.add('.bzrrules')
3577.3.2 by Ian Clatworthy
tweaks following jam's review
43
        self.build_tree(['tar/.bzr-adir/', 'tar/.bzr-adir/afile'])
44
        tree.add(['.bzr-adir/', '.bzr-adir/afile'])
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
45
46
        os.chdir('tar')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
47
        self.run_bzr('ignore something')
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
48
        tree.commit('1')
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
49
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
50
        self.failUnless(tree.has_filename('.bzrignore'))
3577.3.1 by Ian Clatworthy
do not export .bzrrules
51
        self.failUnless(tree.has_filename('.bzrrules'))
3577.3.2 by Ian Clatworthy
tweaks following jam's review
52
        self.failUnless(tree.has_filename('.bzr-adir'))
53
        self.failUnless(tree.has_filename('.bzr-adir/afile'))
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
54
        self.run_bzr('export test.tar.gz')
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
55
        ball = tarfile.open('test.tar.gz')
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
56
        # Make sure the tarball contains 'a', but does not contain
57
        # '.bzrignore'.
58
        self.assertEqual(['test/a'], sorted(ball.getnames()))
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
59
3638.3.9 by Vincent Ladeuil
Fix tar-related failing test (python-2.5.1 bug).
60
        if sys.version_info < (2, 5, 2) and sys.platform == 'darwin':
61
            raise tests.KnownFailure('python %r has a tar related bug, upgrade'
62
                                     % sys.version_info)
3408.7.1 by Martin Pool
Support tarball export to stdout
63
        out, err = self.run_bzr('export --format=tgz --root=test -')
64
        ball = tarfile.open('', fileobj=StringIO(out))
65
        self.assertEqual(['test/a'], sorted(ball.getnames()))
66
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
67
    def test_tar_export_unicode(self):
68
        tree = self.make_branch_and_tree('tar')
69
        fname = u'\xe5.txt'
70
        try:
71
            self.build_tree(['tar/' + fname])
72
        except UnicodeError:
73
            raise TestSkipped('Unable to represent path %r' % (fname,))
74
        tree.add([fname])
75
        tree.commit('first')
76
77
        os.chdir('tar')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
78
        self.run_bzr('export test.tar')
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
79
        ball = tarfile.open('test.tar')
80
        # all paths are prefixed with the base name of the tarball
81
        self.assertEqual(['test/' + fname.encode('utf8')],
82
                         sorted(ball.getnames()))
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
83
84
    def test_zip_export(self):
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
85
        tree = self.make_branch_and_tree('zip')
86
        self.build_tree(['zip/a'])
87
        tree.add('a')
3577.3.1 by Ian Clatworthy
do not export .bzrrules
88
        self.build_tree_contents([('zip/.bzrrules', '')])
89
        tree.add('.bzrrules')
3577.3.2 by Ian Clatworthy
tweaks following jam's review
90
        self.build_tree(['zip/.bzr-adir/', 'zip/.bzr-adir/afile'])
91
        tree.add(['.bzr-adir/', '.bzr-adir/afile'])
1185.61.1 by Jamie Wilkinson
add blackbox tests for ensuring .bzrignore is not exported
92
93
        os.chdir('zip')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
94
        self.run_bzr('ignore something')
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
95
        tree.commit('1')
96
97
        self.failUnless(tree.has_filename('.bzrignore'))
3577.3.1 by Ian Clatworthy
do not export .bzrrules
98
        self.failUnless(tree.has_filename('.bzrrules'))
3577.3.2 by Ian Clatworthy
tweaks following jam's review
99
        self.failUnless(tree.has_filename('.bzr-adir'))
100
        self.failUnless(tree.has_filename('.bzr-adir/afile'))
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
101
        self.run_bzr('export test.zip')
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
102
103
        zfile = zipfile.ZipFile('test.zip')
104
        # Make sure the zipfile contains 'a', but does not contain
105
        # '.bzrignore'.
106
        self.assertEqual(['test/a'], sorted(zfile.namelist()))
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
107
108
    def test_zip_export_unicode(self):
109
        tree = self.make_branch_and_tree('zip')
110
        fname = u'\xe5.txt'
111
        try:
112
            self.build_tree(['zip/' + fname])
113
        except UnicodeError:
114
            raise TestSkipped('Unable to represent path %r' % (fname,))
115
        tree.add([fname])
116
        tree.commit('first')
117
118
        os.chdir('zip')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
119
        self.run_bzr('export test.zip')
2024.2.1 by John Arbash Meinel
Fix bug #56815 by exporting paths in utf8 to tarfile and zipfile
120
        zfile = zipfile.ZipFile('test.zip')
121
        # all paths are prefixed with the base name of the zipfile
122
        self.assertEqual(['test/' + fname.encode('utf8')],
123
                         sorted(zfile.namelist()))
1185.61.5 by Jamie Wilkinson
add test for directory export
124
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
125
    def test_zip_export_directories(self):
126
        tree = self.make_branch_and_tree('zip')
127
        self.build_tree(['zip/a', 'zip/b/', 'zip/b/c', 'zip/d/'])
128
        tree.add(['a', 'b', 'b/c', 'd'])
129
        tree.commit('init')
130
131
        os.chdir('zip')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
132
        self.run_bzr('export test.zip')
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
133
        zfile = zipfile.ZipFile('test.zip')
134
        names = sorted(zfile.namelist())
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
135
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
136
        # even on win32, zipfile.ZipFile changes all names to use
137
        # forward slashes
138
        self.assertEqual(['test/a', 'test/b/', 'test/b/c', 'test/d/'], names)
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
139
2024.2.8 by John Arbash Meinel
paths are always forward slashed for python zipfiles
140
        file_attr = stat.S_IFREG
141
        dir_attr = stat.S_IFDIR | zip_exporter.ZIP_DIRECTORY_BIT
2024.2.7 by John Arbash Meinel
Set the external_attr bits so Windows respects our directories
142
143
        a_info = zfile.getinfo(names[0])
144
        self.assertEqual(file_attr, a_info.external_attr)
145
146
        b_info = zfile.getinfo(names[1])
147
        self.assertEqual(dir_attr, b_info.external_attr)
148
149
        c_info = zfile.getinfo(names[2])
150
        self.assertEqual(file_attr, c_info.external_attr)
151
152
        d_info = zfile.getinfo(names[3])
153
        self.assertEqual(dir_attr, d_info.external_attr)
2024.2.6 by John Arbash Meinel
In zip files, directories must have trailing slashes
154
1185.61.5 by Jamie Wilkinson
add test for directory export
155
    def test_dir_export(self):
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
156
        tree = self.make_branch_and_tree('dir')
157
        self.build_tree(['dir/a'])
158
        tree.add('a')
3577.3.1 by Ian Clatworthy
do not export .bzrrules
159
        self.build_tree_contents([('dir/.bzrrules', '')])
160
        tree.add('.bzrrules')
3577.3.2 by Ian Clatworthy
tweaks following jam's review
161
        self.build_tree(['dir/.bzr-adir/', 'dir/.bzr-adir/afile'])
162
        tree.add(['.bzr-adir/', '.bzr-adir/afile'])
1185.61.5 by Jamie Wilkinson
add test for directory export
163
164
        os.chdir('dir')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
165
        self.run_bzr('ignore something')
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
166
        tree.commit('1')
1185.61.5 by Jamie Wilkinson
add test for directory export
167
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
168
        self.failUnless(tree.has_filename('.bzrignore'))
3577.3.1 by Ian Clatworthy
do not export .bzrrules
169
        self.failUnless(tree.has_filename('.bzrrules'))
3577.3.2 by Ian Clatworthy
tweaks following jam's review
170
        self.failUnless(tree.has_filename('.bzr-adir'))
171
        self.failUnless(tree.has_filename('.bzr-adir/afile'))
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
172
        self.run_bzr('export direxport')
1185.61.5 by Jamie Wilkinson
add test for directory export
173
174
        files = sorted(os.listdir('direxport'))
2024.2.2 by John Arbash Meinel
Cleanup the export tests to act more like the current api.
175
        # Make sure the exported directory contains 'a', but does not contain
176
        # '.bzrignore'.
177
        self.assertEqual(['a'], files)
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
178
179
    def example_branch(self):
3613.2.1 by Robert Collins
Teach export how to export a subdirectory. (Robert Collins)
180
        """Create a branch a 'branch' containing hello and goodbye."""
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
181
        tree = self.make_branch_and_tree('branch')
2024.2.4 by John Arbash Meinel
Use build_tree_contents instead of manually opening the file
182
        self.build_tree_contents([('branch/hello', 'foo')])
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
183
        tree.add('hello')
184
        tree.commit('setup')
185
2024.2.4 by John Arbash Meinel
Use build_tree_contents instead of manually opening the file
186
        self.build_tree_contents([('branch/goodbye', 'baz')])
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
187
        tree.add('goodbye')
188
        tree.commit('setup')
189
        return tree
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
190
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
191
    def test_basic_directory_export(self):
192
        self.example_branch()
193
        os.chdir('branch')
194
195
        # Directory exports
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
196
        self.run_bzr('export ../latest')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
197
        self.assertEqual(['goodbye', 'hello'], sorted(os.listdir('../latest')))
198
        self.check_file_contents('../latest/goodbye', 'baz')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
199
        self.run_bzr('export ../first -r 1')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
200
        self.assertEqual(['hello'], sorted(os.listdir('../first')))
201
        self.check_file_contents('../first/hello', 'foo')
202
203
        # Even with .gz and .bz2 it is still a directory
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
204
        self.run_bzr('export ../first.gz -r 1')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
205
        self.check_file_contents('../first.gz/hello', 'foo')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
206
        self.run_bzr('export ../first.bz2 -r 1')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
207
        self.check_file_contents('../first.bz2/hello', 'foo')
208
209
    def test_basic_tarfile_export(self):
210
        self.example_branch()
211
        os.chdir('branch')
212
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
213
        self.run_bzr('export ../first.tar -r 1')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
214
        self.failUnless(os.path.isfile('../first.tar'))
215
        tf = tarfile.open('../first.tar')
216
        try:
217
            self.assertEqual(['first/hello'], sorted(tf.getnames()))
218
            self.assertEqual('foo', tf.extractfile('first/hello').read())
219
        finally:
220
            tf.close()
221
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
222
        self.run_bzr('export ../first.tar.gz -r 1')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
223
        self.failUnless(os.path.isfile('../first.tar.gz'))
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
224
        self.run_bzr('export ../first.tbz2 -r 1')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
225
        self.failUnless(os.path.isfile('../first.tbz2'))
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
226
        self.run_bzr('export ../first.tar.bz2 -r 1')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
227
        self.failUnless(os.path.isfile('../first.tar.bz2'))
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
228
        self.run_bzr('export ../first.tar.tbz2 -r 1')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
229
        self.failUnless(os.path.isfile('../first.tar.tbz2'))
230
231
        tf = tarfile.open('../first.tar.tbz2', 'r:bz2')
232
        try:
233
            self.assertEqual(['first.tar/hello'], sorted(tf.getnames()))
234
            self.assertEqual('foo', tf.extractfile('first.tar/hello').read())
235
        finally:
236
            tf.close()
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
237
        self.run_bzr('export ../first2.tar -r 1 --root pizza')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
238
        tf = tarfile.open('../first2.tar')
239
        try:
240
            self.assertEqual(['pizza/hello'], sorted(tf.getnames()))
241
            self.assertEqual('foo', tf.extractfile('pizza/hello').read())
242
        finally:
243
            tf.close()
244
245
    def test_basic_zipfile_export(self):
246
        self.example_branch()
247
        os.chdir('branch')
248
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
249
        self.run_bzr('export ../first.zip -r 1')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
250
        self.failUnlessExists('../first.zip')
251
        zf = zipfile.ZipFile('../first.zip')
252
        try:
253
            self.assertEqual(['first/hello'], sorted(zf.namelist()))
254
            self.assertEqual('foo', zf.read('first/hello'))
255
        finally:
256
            zf.close()
257
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
258
        self.run_bzr('export ../first2.zip -r 1 --root pizza')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
259
        zf = zipfile.ZipFile('../first2.zip')
260
        try:
261
            self.assertEqual(['pizza/hello'], sorted(zf.namelist()))
262
            self.assertEqual('foo', zf.read('pizza/hello'))
263
        finally:
264
            zf.close()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
265
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
266
        self.run_bzr('export ../first-zip --format=zip -r 1')
2024.2.3 by John Arbash Meinel
Move out export tests from test_too_much, refactor
267
        zf = zipfile.ZipFile('../first-zip')
268
        try:
269
            self.assertEqual(['first-zip/hello'], sorted(zf.namelist()))
270
            self.assertEqual('foo', zf.read('first-zip/hello'))
271
        finally:
272
            zf.close()
273
2099.1.1 by Daniel Silverstone
Add source branch support to export command
274
    def test_export_from_outside_branch(self):
275
        self.example_branch()
276
277
        # Use directory exports to test stating the branch location
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
278
        self.run_bzr('export latest branch')
2099.1.1 by Daniel Silverstone
Add source branch support to export command
279
        self.assertEqual(['goodbye', 'hello'], sorted(os.listdir('latest')))
280
        self.check_file_contents('latest/goodbye', 'baz')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
281
        self.run_bzr('export first -r 1 branch')
2099.1.1 by Daniel Silverstone
Add source branch support to export command
282
        self.assertEqual(['hello'], sorted(os.listdir('first')))
283
        self.check_file_contents('first/hello', 'foo')
3613.2.1 by Robert Collins
Teach export how to export a subdirectory. (Robert Collins)
284
285
    def test_export_partial_tree(self):
286
        tree = self.example_branch()
287
        self.build_tree(['branch/subdir/', 'branch/subdir/foo.txt'])
288
        tree.smart_add(['branch'])
289
        tree.commit('more setup')
290
        out, err = self.run_bzr('export exported branch/subdir')
291
        self.assertEqual(['foo.txt'], os.listdir('exported'))