23
from bzrlib.branch import Branch
25
from bzrlib.tests import TestSkipped
24
26
from bzrlib.tests.blackbox import ExternalBase
25
import tarfile, zipfile
27
29
class TestExport(ExternalBase):
29
31
def test_tar_export(self):
32
tree = self.make_branch_and_tree('tar')
33
self.build_tree(['tar/a'])
37
self.run_bzr('ignore', 'something')
35
open('a', 'wb').write('foo\n')
37
self.runbzr('ignore something')
38
self.runbzr('commit -m 1')
39
self.runbzr('export test.tar.gz')
40
self.failUnless(tree.has_filename('.bzrignore'))
41
self.run_bzr('export', 'test.tar.gz')
40
42
ball = tarfile.open('test.tar.gz')
41
for m in ball.getnames():
43
self.failIf(os.path.basename(m) == '.bzrignore', 'tar export contains .bzrignore')
43
# Make sure the tarball contains 'a', but does not contain
45
self.assertEqual(['test/a'], sorted(ball.getnames()))
47
def test_tar_export_unicode(self):
48
tree = self.make_branch_and_tree('tar')
51
self.build_tree(['tar/' + fname])
53
raise TestSkipped('Unable to represent path %r' % (fname,))
58
self.run_bzr('export', 'test.tar')
59
ball = tarfile.open('test.tar')
60
# all paths are prefixed with the base name of the tarball
61
self.assertEqual(['test/' + fname.encode('utf8')],
62
sorted(ball.getnames()))
45
64
def test_zip_export(self):
51
open('a', 'wb').write('foo\n')
53
self.runbzr('ignore something')
54
self.runbzr('commit -m 1')
55
self.runbzr('export test.zip')
56
ball = zipfile.ZipFile('test.zip')
57
for m in ball.namelist():
59
self.failIf(os.path.basename(m) == '.bzrignore', 'zip export contains .bzrignore')
65
tree = self.make_branch_and_tree('zip')
66
self.build_tree(['zip/a'])
70
self.run_bzr('ignore', 'something')
73
self.failUnless(tree.has_filename('.bzrignore'))
74
self.run_bzr('export', 'test.zip')
76
zfile = zipfile.ZipFile('test.zip')
77
# Make sure the zipfile contains 'a', but does not contain
79
self.assertEqual(['test/a'], sorted(zfile.namelist()))
81
def test_zip_export_unicode(self):
82
tree = self.make_branch_and_tree('zip')
85
self.build_tree(['zip/' + fname])
87
raise TestSkipped('Unable to represent path %r' % (fname,))
92
self.run_bzr('export', 'test.zip')
93
zfile = zipfile.ZipFile('test.zip')
94
# all paths are prefixed with the base name of the zipfile
95
self.assertEqual(['test/' + fname.encode('utf8')],
96
sorted(zfile.namelist()))
61
98
def test_dir_export(self):
99
tree = self.make_branch_and_tree('dir')
100
self.build_tree(['dir/a'])
104
self.run_bzr('ignore', 'something')
67
open('a', 'wb').write('foo\n')
69
self.runbzr('ignore something')
70
self.runbzr('commit -m 1')
71
self.runbzr('export direxport')
107
self.failUnless(tree.has_filename('.bzrignore'))
108
self.run_bzr('export', 'direxport')
73
110
files = sorted(os.listdir('direxport'))
74
self.assertEqual(['a'], files, 'dir contains .bzrignore')
111
# Make sure the exported directory contains 'a', but does not contain
113
self.assertEqual(['a'], files)
115
def example_branch(self):
116
tree = self.make_branch_and_tree('branch')
117
self.build_tree_contents([('branch/hello', 'foo')])
121
self.build_tree_contents([('branch/goodbye', 'baz')])
126
def test_basic_directory_export(self):
127
self.example_branch()
131
self.run_bzr('export', '../latest')
132
self.assertEqual(['goodbye', 'hello'], sorted(os.listdir('../latest')))
133
self.check_file_contents('../latest/goodbye', 'baz')
134
self.run_bzr('export', '../first', '-r', '1')
135
self.assertEqual(['hello'], sorted(os.listdir('../first')))
136
self.check_file_contents('../first/hello', 'foo')
138
# Even with .gz and .bz2 it is still a directory
139
self.run_bzr('export', '../first.gz', '-r', '1')
140
self.check_file_contents('../first.gz/hello', 'foo')
141
self.run_bzr('export', '../first.bz2', '-r', '1')
142
self.check_file_contents('../first.bz2/hello', 'foo')
144
def test_basic_tarfile_export(self):
145
self.example_branch()
148
self.run_bzr('export', '../first.tar', '-r', '1')
149
self.failUnless(os.path.isfile('../first.tar'))
150
tf = tarfile.open('../first.tar')
152
self.assertEqual(['first/hello'], sorted(tf.getnames()))
153
self.assertEqual('foo', tf.extractfile('first/hello').read())
157
self.run_bzr('export', '../first.tar.gz', '-r', '1')
158
self.failUnless(os.path.isfile('../first.tar.gz'))
159
self.run_bzr('export', '../first.tbz2', '-r', '1')
160
self.failUnless(os.path.isfile('../first.tbz2'))
161
self.run_bzr('export', '../first.tar.bz2', '-r', '1')
162
self.failUnless(os.path.isfile('../first.tar.bz2'))
163
self.run_bzr('export', '../first.tar.tbz2', '-r', '1')
164
self.failUnless(os.path.isfile('../first.tar.tbz2'))
166
tf = tarfile.open('../first.tar.tbz2', 'r:bz2')
168
self.assertEqual(['first.tar/hello'], sorted(tf.getnames()))
169
self.assertEqual('foo', tf.extractfile('first.tar/hello').read())
172
self.run_bzr('export', '../first2.tar', '-r', '1', '--root', 'pizza')
173
tf = tarfile.open('../first2.tar')
175
self.assertEqual(['pizza/hello'], sorted(tf.getnames()))
176
self.assertEqual('foo', tf.extractfile('pizza/hello').read())
180
def test_basic_zipfile_export(self):
181
self.example_branch()
184
self.run_bzr('export', '../first.zip', '-r', '1')
185
self.failUnlessExists('../first.zip')
186
zf = zipfile.ZipFile('../first.zip')
188
self.assertEqual(['first/hello'], sorted(zf.namelist()))
189
self.assertEqual('foo', zf.read('first/hello'))
193
self.run_bzr('export', '../first2.zip', '-r', '1', '--root', 'pizza')
194
zf = zipfile.ZipFile('../first2.zip')
196
self.assertEqual(['pizza/hello'], sorted(zf.namelist()))
197
self.assertEqual('foo', zf.read('pizza/hello'))
201
self.run_bzr('export', '../first-zip', '--format=zip', '-r', '1')
202
zf = zipfile.ZipFile('../first-zip')
204
self.assertEqual(['first-zip/hello'], sorted(zf.namelist()))
205
self.assertEqual('foo', zf.read('first-zip/hello'))