/brz/remove-bazaar

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