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
25
20
from bzrlib import (
30
from bzrlib.export import get_root_name
31
from bzrlib.export.tar_exporter import export_tarball_generator
32
from bzrlib.tests import features
35
class TestDirExport(tests.TestCaseWithTransport):
37
def test_missing_file(self):
27
class TestExport(tests.TestCaseWithTransport):
29
def test_dir_export_missing_file(self):
38
30
self.build_tree(['a/', 'a/b', 'a/c'])
39
31
wt = self.make_branch_and_tree('.')
40
32
wt.add(['a', 'a/b', 'a/c'])
42
34
export.export(wt, 'target', format="dir")
43
self.assertPathExists('target/a/b')
44
self.assertPathDoesNotExist('target/a/c')
47
wt = self.make_branch_and_tree('.')
48
export.export(wt, 'target', format="dir")
49
self.assertEquals([], os.listdir("target"))
51
def test_symlink(self):
52
self.requireFeature(features.SymlinkFeature)
35
self.failUnlessExists('target/a/b')
36
self.failIfExists('target/a/c')
38
def test_dir_export_symlink(self):
39
self.requireFeature(tests.SymlinkFeature)
53
40
wt = self.make_branch_and_tree('.')
54
41
os.symlink('source', 'link')
56
43
export.export(wt, 'target', format="dir")
57
self.assertPathExists('target/link')
44
self.failUnlessExists('target/link')
59
def test_to_existing_empty_dir_success(self):
46
def test_dir_export_to_existing_empty_dir_success(self):
60
47
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
61
48
wt = self.make_branch_and_tree('source')
62
49
wt.add(['a', 'b', 'b/c'])
64
51
self.build_tree(['target/'])
65
52
export.export(wt, 'target', format="dir")
66
self.assertPathExists('target/a')
67
self.assertPathExists('target/b')
68
self.assertPathExists('target/b/c')
70
def test_empty_subdir(self):
71
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
72
wt = self.make_branch_and_tree('source')
73
wt.add(['a', 'b', 'b/c'])
75
self.build_tree(['target/'])
76
export.export(wt, 'target', format="dir", subdir='')
77
self.assertPathExists('target/a')
78
self.assertPathExists('target/b')
79
self.assertPathExists('target/b/c')
81
def test_to_existing_nonempty_dir_fail(self):
53
self.failUnlessExists('target/a')
54
self.failUnlessExists('target/b')
55
self.failUnlessExists('target/b/c')
57
def test_dir_export_to_existing_nonempty_dir_fail(self):
82
58
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
83
59
wt = self.make_branch_and_tree('source')
84
60
wt.add(['a', 'b', 'b/c'])
86
62
self.build_tree(['target/', 'target/foo'])
87
self.assertRaises(errors.BzrError,
88
export.export, wt, 'target', format="dir")
63
self.assertRaises(errors.BzrError, export.export, wt, 'target', format="dir")
90
def test_existing_single_file(self):
92
'dir1/', 'dir1/dir2/', 'dir1/first', 'dir1/dir2/second'])
65
def test_dir_export_existing_single_file(self):
66
self.build_tree(['dir1/', 'dir1/dir2/', 'dir1/first', 'dir1/dir2/second'])
93
67
wtree = self.make_branch_and_tree('dir1')
94
68
wtree.add(['dir2', 'first', 'dir2/second'])
96
70
export.export(wtree, 'target1', format='dir', subdir='first')
97
self.assertPathExists('target1/first')
71
self.failUnlessExists('target1/first')
98
72
export.export(wtree, 'target2', format='dir', subdir='dir2/second')
99
self.assertPathExists('target2/second')
101
def test_files_same_timestamp(self):
73
self.failUnlessExists('target2/second')
75
def test_dir_export_files_same_timestamp(self):
102
76
builder = self.make_branch_builder('source')
103
77
builder.start_series()
104
78
builder.build_snapshot(None, None, [
148
121
t = self.get_transport('target')
149
122
self.assertEqual(a_time, t.stat('a').st_mtime)
150
123
self.assertEqual(b_time, t.stat('b').st_mtime)
152
def test_subdir_files_per_timestamps(self):
153
builder = self.make_branch_builder('source')
154
builder.start_series()
155
foo_time = time.mktime((1999, 12, 12, 0, 0, 0, 0, 0, 0))
156
builder.build_snapshot(None, None, [
157
('add', ('', 'root-id', 'directory', '')),
158
('add', ('subdir', 'subdir-id', 'directory', '')),
159
('add', ('subdir/foo.txt', 'foo-id', 'file', 'content\n'))],
161
builder.finish_series()
162
b = builder.get_branch()
164
self.addCleanup(b.unlock)
165
tree = b.basis_tree()
166
export.export(tree, 'target', format='dir', subdir='subdir',
167
per_file_timestamps=True)
168
t = self.get_transport('target')
169
self.assertEquals(foo_time, t.stat('foo.txt').st_mtime)
172
class TarExporterTests(tests.TestCaseWithTransport):
175
self.requireFeature(features.lzma)
177
wt = self.make_branch_and_tree('.')
178
self.build_tree(['a'])
181
export.export(wt, 'target.tar.xz', format="txz")
182
tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.xz'))
183
self.assertEquals(["target/a"], tf.getnames())
186
self.requireFeature(features.lzma)
188
wt = self.make_branch_and_tree('.')
189
self.build_tree(['a'])
192
export.export(wt, 'target.tar.lzma', format="tlzma")
193
tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.lzma'))
194
self.assertEquals(["target/a"], tf.getnames())
197
wt = self.make_branch_and_tree('.')
198
self.build_tree(['a'])
201
export.export(wt, 'target.tar.gz', format="tgz")
202
tf = tarfile.open('target.tar.gz')
203
self.assertEquals(["target/a"], tf.getnames())
205
def test_tgz_ignores_dest_path(self):
206
# The target path should not be a part of the target file.
208
wt = self.make_branch_and_tree('.')
209
self.build_tree(['a'])
214
export.export(wt, 'testdir1/target.tar.gz', format="tgz",
215
per_file_timestamps=True)
216
export.export(wt, 'testdir2/target.tar.gz', format="tgz",
217
per_file_timestamps=True)
218
file1 = open('testdir1/target.tar.gz', 'r')
219
self.addCleanup(file1.close)
220
file2 = open('testdir1/target.tar.gz', 'r')
221
self.addCleanup(file2.close)
222
content1 = file1.read()
223
content2 = file2.read()
224
self.assertEqualDiff(content1, content2)
225
# the gzip module doesn't have a way to read back to the original
226
# filename, but it's stored as-is in the tarfile.
227
self.assertFalse("testdir1" in content1)
228
self.assertFalse("target.tar.gz" in content1)
229
self.assertTrue("target.tar" in content1)
232
wt = self.make_branch_and_tree('.')
233
self.build_tree(['a'])
236
export.export(wt, 'target.tar.bz2', format="tbz2")
237
tf = tarfile.open('target.tar.bz2')
238
self.assertEquals(["target/a"], tf.getnames())
240
def test_xz_stdout(self):
241
wt = self.make_branch_and_tree('.')
242
self.assertRaises(errors.BzrError, export.export, wt, '-',
245
def test_export_tarball_generator(self):
246
wt = self.make_branch_and_tree('.')
247
self.build_tree(['a'])
249
wt.commit("1", timestamp=42)
251
ball = tarfile.open(None, "w|", target)
254
for _ in export_tarball_generator(wt, ball, "bar"):
258
# Ball should now be closed.
260
ball2 = tarfile.open(None, "r", target)
261
self.addCleanup(ball2.close)
262
self.assertEquals(["bar/a"], ball2.getnames())
265
class ZipExporterTests(tests.TestCaseWithTransport):
267
def test_per_file_timestamps(self):
268
tree = self.make_branch_and_tree('.')
269
self.build_tree_contents([('har', 'foo')])
271
# Earliest allowable date on FAT32 filesystems is 1980-01-01
272
timestamp = 347151600
273
tree.commit('setup', timestamp=timestamp)
274
export.export(tree.basis_tree(), 'test.zip', format='zip',
275
per_file_timestamps=True)
276
zfile = zipfile.ZipFile('test.zip')
277
info = zfile.getinfo("test/har")
278
self.assertEquals(time.localtime(timestamp)[:6], info.date_time)
281
class RootNameTests(tests.TestCase):
283
def test_root_name(self):
284
self.assertEquals('mytest', get_root_name('../mytest.tar'))
285
self.assertEquals('mytar', get_root_name('mytar.tar'))
286
self.assertEquals('mytar', get_root_name('mytar.tar.bz2'))
287
self.assertEquals('tar.tar.tar', get_root_name('tar.tar.tar.tgz'))
288
self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5.tar.gz'))
289
self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5.zip'))
290
self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5'))
291
self.assertEquals('mytar', get_root_name('a/long/path/mytar.tgz'))
292
self.assertEquals('other',
293
get_root_name('../parent/../dir/other.tbz2'))
294
self.assertEquals('', get_root_name('-'))