14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for bzrlib.export."""
19
from cStringIO import StringIO
20
25
from bzrlib import (
27
class TestExport(tests.TestCaseWithTransport):
29
def test_dir_export_missing_file(self):
30
from bzrlib.export import get_root_name
31
from bzrlib.export.tar_exporter import export_tarball
32
from bzrlib.tests import features
35
class TestDirExport(tests.TestCaseWithTransport):
37
def test_missing_file(self):
30
38
self.build_tree(['a/', 'a/b', 'a/c'])
31
39
wt = self.make_branch_and_tree('.')
32
40
wt.add(['a', 'a/b', 'a/c'])
121
134
t = self.get_transport('target')
122
135
self.assertEqual(a_time, t.stat('a').st_mtime)
123
136
self.assertEqual(b_time, t.stat('b').st_mtime)
139
class TarExporterTests(tests.TestCaseWithTransport):
142
self.requireFeature(features.lzma)
144
wt = self.make_branch_and_tree('.')
145
self.build_tree(['a'])
148
export.export(wt, 'target.tar.xz', format="txz")
149
tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.xz'))
150
self.assertEquals(["target/a"], tf.getnames())
153
self.requireFeature(features.lzma)
155
wt = self.make_branch_and_tree('.')
156
self.build_tree(['a'])
159
export.export(wt, 'target.tar.lzma', format="tlzma")
160
tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.lzma'))
161
self.assertEquals(["target/a"], tf.getnames())
164
wt = self.make_branch_and_tree('.')
165
self.build_tree(['a'])
168
export.export(wt, 'target.tar.gz', format="tgz")
169
tf = tarfile.open('target.tar.gz')
170
self.assertEquals(["target/a"], tf.getnames())
172
def test_tgz_ignores_dest_path(self):
173
# The target path should not be a part of the target file.
175
wt = self.make_branch_and_tree('.')
176
self.build_tree(['a'])
181
export.export(wt, 'testdir1/target.tar.gz', format="tgz",
182
per_file_timestamps=True)
183
export.export(wt, 'testdir2/target.tar.gz', format="tgz",
184
per_file_timestamps=True)
185
file1 = open('testdir1/target.tar.gz', 'r')
186
self.addCleanup(file1.close)
187
file2 = open('testdir1/target.tar.gz', 'r')
188
self.addCleanup(file2.close)
189
content1 = file1.read()
190
content2 = file2.read()
191
self.assertEqualDiff(content1, content2)
192
# the gzip module doesn't have a way to read back to the original
193
# filename, but it's stored as-is in the tarfile.
194
self.assertFalse("testdir1" in content1)
195
self.assertFalse("target.tar.gz" in content1)
196
self.assertTrue("target.tar" in content1)
199
wt = self.make_branch_and_tree('.')
200
self.build_tree(['a'])
203
export.export(wt, 'target.tar.bz2', format="tbz2")
204
tf = tarfile.open('target.tar.bz2')
205
self.assertEquals(["target/a"], tf.getnames())
207
def test_xz_stdout(self):
208
wt = self.make_branch_and_tree('.')
209
self.assertRaises(errors.BzrError, export.export, wt, '-',
212
def test_export_tarball(self):
213
wt = self.make_branch_and_tree('.')
214
self.build_tree(['a'])
216
wt.commit("1", timestamp=42)
218
ball = tarfile.open(None, "w|", target)
221
export_tarball(wt, ball, "bar")
224
self.assertEquals(["bar/a"], ball.getnames())
228
class ZipExporterTests(tests.TestCaseWithTransport):
230
def test_per_file_timestamps(self):
231
tree = self.make_branch_and_tree('.')
232
self.build_tree_contents([('har', 'foo')])
234
# Earliest allowable date on FAT32 filesystems is 1980-01-01
235
timestamp = 347151600
236
tree.commit('setup', timestamp=timestamp)
237
export.export(tree.basis_tree(), 'test.zip', format='zip',
238
per_file_timestamps=True)
239
zfile = zipfile.ZipFile('test.zip')
240
info = zfile.getinfo("test/har")
241
self.assertEquals(time.localtime(timestamp)[:6], info.date_time)
244
class RootNameTests(tests.TestCase):
246
def test_root_name(self):
247
self.assertEquals('mytest', get_root_name('../mytest.tar'))
248
self.assertEquals('mytar', get_root_name('mytar.tar'))
249
self.assertEquals('mytar', get_root_name('mytar.tar.bz2'))
250
self.assertEquals('tar.tar.tar', get_root_name('tar.tar.tar.tgz'))
251
self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5.tar.gz'))
252
self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5.zip'))
253
self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5'))
254
self.assertEquals('mytar', get_root_name('a/long/path/mytar.tgz'))
255
self.assertEquals('other',
256
get_root_name('../parent/../dir/other.tbz2'))
257
self.assertEquals('', get_root_name('-'))