~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_export.py

  • Committer: Martin Pool
  • Date: 2011-03-28 01:28:09 UTC
  • mto: (5425.4.19 220464-stale-locks)
  • mto: This revision was merged to the branch mainline in revision 5970.
  • Revision ID: mbp@canonical.com-20110328012809-frw003r09tcrxkiz
Represent lock held info as an object, not just a dict

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
16
16
 
17
 
"""Tests for bzrlib.export."""
18
 
 
19
 
from cStringIO import StringIO
20
17
import os
21
 
import tarfile
22
18
import time
23
 
import zipfile
24
19
 
25
20
from bzrlib import (
26
21
    errors,
27
22
    export,
28
23
    tests,
29
24
    )
30
 
from bzrlib.export import get_root_name
31
 
from bzrlib.export.tar_exporter import export_tarball
32
 
from bzrlib.tests import features
33
 
 
34
 
 
35
 
class TestDirExport(tests.TestCaseWithTransport):
36
 
 
37
 
    def test_missing_file(self):
 
25
 
 
26
 
 
27
class TestExport(tests.TestCaseWithTransport):
 
28
 
 
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'])
41
33
        os.unlink('a/c')
42
34
        export.export(wt, 'target', format="dir")
43
 
        self.assertPathExists('target/a/b')
44
 
        self.assertPathDoesNotExist('target/a/c')
45
 
 
46
 
    def test_empty(self):
47
 
        wt = self.make_branch_and_tree('.')
48
 
        export.export(wt, 'target', format="dir")
49
 
        self.assertEquals([], os.listdir("target"))
50
 
 
51
 
    def test_symlink(self):
 
35
        self.failUnlessExists('target/a/b')
 
36
        self.failIfExists('target/a/c')
 
37
 
 
38
    def test_dir_export_symlink(self):
52
39
        self.requireFeature(tests.SymlinkFeature)
53
40
        wt = self.make_branch_and_tree('.')
54
41
        os.symlink('source', 'link')
55
42
        wt.add(['link'])
56
43
        export.export(wt, 'target', format="dir")
57
 
        self.assertPathExists('target/link')
 
44
        self.failUnlessExists('target/link')
58
45
 
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'])
63
50
        wt.commit('1')
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')
69
 
 
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'])
74
 
        wt.commit('1')
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')
80
 
 
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')
 
56
 
 
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
63
        self.assertRaises(errors.BzrError, export.export, wt, 'target', format="dir")
88
64
 
89
 
    def test_existing_single_file(self):
 
65
    def test_dir_export_existing_single_file(self):
90
66
        self.build_tree(['dir1/', 'dir1/dir2/', 'dir1/first', 'dir1/dir2/second'])
91
67
        wtree = self.make_branch_and_tree('dir1')
92
68
        wtree.add(['dir2', 'first', 'dir2/second'])
93
69
        wtree.commit('1')
94
70
        export.export(wtree, 'target1', format='dir', subdir='first')
95
 
        self.assertPathExists('target1/first')
 
71
        self.failUnlessExists('target1/first')
96
72
        export.export(wtree, 'target2', format='dir', subdir='dir2/second')
97
 
        self.assertPathExists('target2/second')
98
 
 
99
 
    def test_files_same_timestamp(self):
 
73
        self.failUnlessExists('target2/second')
 
74
        
 
75
    def test_dir_export_files_same_timestamp(self):
100
76
        builder = self.make_branch_builder('source')
101
77
        builder.start_series()
102
78
        builder.build_snapshot(None, None, [
123
99
        # All files must be given the same mtime.
124
100
        self.assertEqual(st_a.st_mtime, st_b.st_mtime)
125
101
 
126
 
    def test_files_per_file_timestamps(self):
 
102
    def test_dir_export_files_per_file_timestamps(self):
127
103
        builder = self.make_branch_builder('source')
128
104
        builder.start_series()
129
105
        # Earliest allowable date on FAT32 filesystems is 1980-01-01
145
121
        t = self.get_transport('target')
146
122
        self.assertEqual(a_time, t.stat('a').st_mtime)
147
123
        self.assertEqual(b_time, t.stat('b').st_mtime)
148
 
 
149
 
 
150
 
class TarExporterTests(tests.TestCaseWithTransport):
151
 
 
152
 
    def test_xz(self):
153
 
        self.requireFeature(features.lzma)
154
 
        import lzma
155
 
        wt = self.make_branch_and_tree('.')
156
 
        self.build_tree(['a'])
157
 
        wt.add(["a"])
158
 
        wt.commit("1")
159
 
        export.export(wt, 'target.tar.xz', format="txz")
160
 
        tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.xz'))
161
 
        self.assertEquals(["target/a"], tf.getnames())
162
 
 
163
 
    def test_lzma(self):
164
 
        self.requireFeature(features.lzma)
165
 
        import lzma
166
 
        wt = self.make_branch_and_tree('.')
167
 
        self.build_tree(['a'])
168
 
        wt.add(["a"])
169
 
        wt.commit("1")
170
 
        export.export(wt, 'target.tar.lzma', format="tlzma")
171
 
        tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.lzma'))
172
 
        self.assertEquals(["target/a"], tf.getnames())
173
 
 
174
 
    def test_tgz(self):
175
 
        wt = self.make_branch_and_tree('.')
176
 
        self.build_tree(['a'])
177
 
        wt.add(["a"])
178
 
        wt.commit("1")
179
 
        export.export(wt, 'target.tar.gz', format="tgz")
180
 
        tf = tarfile.open('target.tar.gz')
181
 
        self.assertEquals(["target/a"], tf.getnames())
182
 
 
183
 
    def test_tgz_ignores_dest_path(self):
184
 
        # The target path should not be a part of the target file.
185
 
        # (bug #102234)
186
 
        wt = self.make_branch_and_tree('.')
187
 
        self.build_tree(['a'])
188
 
        wt.add(["a"])
189
 
        wt.commit("1")
190
 
        os.mkdir("testdir1")
191
 
        os.mkdir("testdir2")
192
 
        export.export(wt, 'testdir1/target.tar.gz', format="tgz",
193
 
            per_file_timestamps=True)
194
 
        export.export(wt, 'testdir2/target.tar.gz', format="tgz",
195
 
            per_file_timestamps=True)
196
 
        file1 = open('testdir1/target.tar.gz', 'r')
197
 
        self.addCleanup(file1.close)
198
 
        file2 = open('testdir1/target.tar.gz', 'r')
199
 
        self.addCleanup(file2.close)
200
 
        content1 = file1.read()
201
 
        content2 = file2.read()
202
 
        self.assertEqualDiff(content1, content2)
203
 
        # the gzip module doesn't have a way to read back to the original
204
 
        # filename, but it's stored as-is in the tarfile.
205
 
        self.assertFalse("testdir1" in content1)
206
 
        self.assertFalse("target.tar.gz" in content1)
207
 
        self.assertTrue("target.tar" in content1)
208
 
 
209
 
    def test_tbz2(self):
210
 
        wt = self.make_branch_and_tree('.')
211
 
        self.build_tree(['a'])
212
 
        wt.add(["a"])
213
 
        wt.commit("1")
214
 
        export.export(wt, 'target.tar.bz2', format="tbz2")
215
 
        tf = tarfile.open('target.tar.bz2')
216
 
        self.assertEquals(["target/a"], tf.getnames())
217
 
 
218
 
    def test_xz_stdout(self):
219
 
        wt = self.make_branch_and_tree('.')
220
 
        self.assertRaises(errors.BzrError, export.export, wt, '-',
221
 
            format="txz")
222
 
 
223
 
    def test_export_tarball(self):
224
 
        wt = self.make_branch_and_tree('.')
225
 
        self.build_tree(['a'])
226
 
        wt.add(["a"])
227
 
        wt.commit("1", timestamp=42)
228
 
        target = StringIO()
229
 
        ball = tarfile.open(None, "w|", target)
230
 
        wt.lock_read()
231
 
        try:
232
 
            export_tarball(wt, ball, "bar")
233
 
        finally:
234
 
            wt.unlock()
235
 
        self.assertEquals(["bar/a"], ball.getnames())
236
 
        ball.close()
237
 
 
238
 
 
239
 
class ZipExporterTests(tests.TestCaseWithTransport):
240
 
 
241
 
    def test_per_file_timestamps(self):
242
 
        tree = self.make_branch_and_tree('.')
243
 
        self.build_tree_contents([('har', 'foo')])
244
 
        tree.add('har')
245
 
        # Earliest allowable date on FAT32 filesystems is 1980-01-01
246
 
        timestamp = 347151600
247
 
        tree.commit('setup', timestamp=timestamp)
248
 
        export.export(tree.basis_tree(), 'test.zip', format='zip',
249
 
            per_file_timestamps=True)
250
 
        zfile = zipfile.ZipFile('test.zip')
251
 
        info = zfile.getinfo("test/har")
252
 
        self.assertEquals(time.localtime(timestamp)[:6], info.date_time)
253
 
 
254
 
 
255
 
class RootNameTests(tests.TestCase):
256
 
 
257
 
    def test_root_name(self):
258
 
        self.assertEquals('mytest', get_root_name('../mytest.tar'))
259
 
        self.assertEquals('mytar', get_root_name('mytar.tar'))
260
 
        self.assertEquals('mytar', get_root_name('mytar.tar.bz2'))
261
 
        self.assertEquals('tar.tar.tar', get_root_name('tar.tar.tar.tgz'))
262
 
        self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5.tar.gz'))
263
 
        self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5.zip'))
264
 
        self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5'))
265
 
        self.assertEquals('mytar', get_root_name('a/long/path/mytar.tgz'))
266
 
        self.assertEquals('other',
267
 
            get_root_name('../parent/../dir/other.tbz2'))
268
 
        self.assertEquals('', get_root_name('-'))