~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_export.py

  • Committer: INADA Naoki
  • Date: 2011-05-05 13:03:12 UTC
  • mto: This revision was merged to the branch mainline in revision 5891.
  • Revision ID: songofacandy@gmail.com-20110505130312-ozvqmfjt7cuwi72k
Run build_mo when build is called.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for bzrlib.export."""
 
18
 
 
19
from cStringIO import StringIO
 
20
import os
 
21
import tarfile
 
22
import time
 
23
import zipfile
 
24
 
 
25
from bzrlib import (
 
26
    errors,
 
27
    export,
 
28
    tests,
 
29
    )
 
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):
 
38
        self.build_tree(['a/', 'a/b', 'a/c'])
 
39
        wt = self.make_branch_and_tree('.')
 
40
        wt.add(['a', 'a/b', 'a/c'])
 
41
        os.unlink('a/c')
 
42
        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):
 
52
        self.requireFeature(tests.SymlinkFeature)
 
53
        wt = self.make_branch_and_tree('.')
 
54
        os.symlink('source', 'link')
 
55
        wt.add(['link'])
 
56
        export.export(wt, 'target', format="dir")
 
57
        self.assertPathExists('target/link')
 
58
 
 
59
    def test_to_existing_empty_dir_success(self):
 
60
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
 
61
        wt = self.make_branch_and_tree('source')
 
62
        wt.add(['a', 'b', 'b/c'])
 
63
        wt.commit('1')
 
64
        self.build_tree(['target/'])
 
65
        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_to_existing_nonempty_dir_fail(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/', 'target/foo'])
 
76
        self.assertRaises(errors.BzrError, export.export, wt, 'target', format="dir")
 
77
 
 
78
    def test_existing_single_file(self):
 
79
        self.build_tree(['dir1/', 'dir1/dir2/', 'dir1/first', 'dir1/dir2/second'])
 
80
        wtree = self.make_branch_and_tree('dir1')
 
81
        wtree.add(['dir2', 'first', 'dir2/second'])
 
82
        wtree.commit('1')
 
83
        export.export(wtree, 'target1', format='dir', subdir='first')
 
84
        self.assertPathExists('target1/first')
 
85
        export.export(wtree, 'target2', format='dir', subdir='dir2/second')
 
86
        self.assertPathExists('target2/second')
 
87
 
 
88
    def test_files_same_timestamp(self):
 
89
        builder = self.make_branch_builder('source')
 
90
        builder.start_series()
 
91
        builder.build_snapshot(None, None, [
 
92
            ('add', ('', 'root-id', 'directory', '')),
 
93
            ('add', ('a', 'a-id', 'file', 'content\n'))])
 
94
        builder.build_snapshot(None, None, [
 
95
            ('add', ('b', 'b-id', 'file', 'content\n'))])
 
96
        builder.finish_series()
 
97
        b = builder.get_branch()
 
98
        b.lock_read()
 
99
        self.addCleanup(b.unlock)
 
100
        tree = b.basis_tree()
 
101
        orig_iter_files_bytes = tree.iter_files_bytes
 
102
        # Make iter_files_bytes slower, so we provoke mtime skew
 
103
        def iter_files_bytes(to_fetch):
 
104
            for thing in orig_iter_files_bytes(to_fetch):
 
105
                yield thing
 
106
                time.sleep(1)
 
107
        tree.iter_files_bytes = iter_files_bytes
 
108
        export.export(tree, 'target', format='dir')
 
109
        t = self.get_transport('target')
 
110
        st_a = t.stat('a')
 
111
        st_b = t.stat('b')
 
112
        # All files must be given the same mtime.
 
113
        self.assertEqual(st_a.st_mtime, st_b.st_mtime)
 
114
 
 
115
    def test_files_per_file_timestamps(self):
 
116
        builder = self.make_branch_builder('source')
 
117
        builder.start_series()
 
118
        # Earliest allowable date on FAT32 filesystems is 1980-01-01
 
119
        a_time = time.mktime((1999, 12, 12, 0, 0, 0, 0, 0, 0))
 
120
        b_time = time.mktime((1980, 01, 01, 0, 0, 0, 0, 0, 0))
 
121
        builder.build_snapshot(None, None, [
 
122
            ('add', ('', 'root-id', 'directory', '')),
 
123
            ('add', ('a', 'a-id', 'file', 'content\n'))],
 
124
            timestamp=a_time)
 
125
        builder.build_snapshot(None, None, [
 
126
            ('add', ('b', 'b-id', 'file', 'content\n'))],
 
127
            timestamp=b_time)
 
128
        builder.finish_series()
 
129
        b = builder.get_branch()
 
130
        b.lock_read()
 
131
        self.addCleanup(b.unlock)
 
132
        tree = b.basis_tree()
 
133
        export.export(tree, 'target', format='dir', per_file_timestamps=True)
 
134
        t = self.get_transport('target')
 
135
        self.assertEqual(a_time, t.stat('a').st_mtime)
 
136
        self.assertEqual(b_time, t.stat('b').st_mtime)
 
137
 
 
138
 
 
139
class TarExporterTests(tests.TestCaseWithTransport):
 
140
 
 
141
    def test_xz(self):
 
142
        self.requireFeature(features.lzma)
 
143
        import lzma
 
144
        wt = self.make_branch_and_tree('.')
 
145
        self.build_tree(['a'])
 
146
        wt.add(["a"])
 
147
        wt.commit("1")
 
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())
 
151
 
 
152
    def test_lzma(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.lzma', format="tlzma")
 
160
        tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.lzma'))
 
161
        self.assertEquals(["target/a"], tf.getnames())
 
162
 
 
163
    def test_tgz(self):
 
164
        wt = self.make_branch_and_tree('.')
 
165
        self.build_tree(['a'])
 
166
        wt.add(["a"])
 
167
        wt.commit("1")
 
168
        export.export(wt, 'target.tar.gz', format="tgz")
 
169
        tf = tarfile.open('target.tar.gz')
 
170
        self.assertEquals(["target/a"], tf.getnames())
 
171
 
 
172
    def test_tgz_ignores_dest_path(self):
 
173
        # The target path should not be a part of the target file.
 
174
        # (bug #102234)
 
175
        wt = self.make_branch_and_tree('.')
 
176
        self.build_tree(['a'])
 
177
        wt.add(["a"])
 
178
        wt.commit("1")
 
179
        os.mkdir("testdir1")
 
180
        os.mkdir("testdir2")
 
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)
 
197
 
 
198
    def test_tbz2(self):
 
199
        wt = self.make_branch_and_tree('.')
 
200
        self.build_tree(['a'])
 
201
        wt.add(["a"])
 
202
        wt.commit("1")
 
203
        export.export(wt, 'target.tar.bz2', format="tbz2")
 
204
        tf = tarfile.open('target.tar.bz2')
 
205
        self.assertEquals(["target/a"], tf.getnames())
 
206
 
 
207
    def test_xz_stdout(self):
 
208
        wt = self.make_branch_and_tree('.')
 
209
        self.assertRaises(errors.BzrError, export.export, wt, '-',
 
210
            format="txz")
 
211
 
 
212
    def test_export_tarball(self):
 
213
        wt = self.make_branch_and_tree('.')
 
214
        self.build_tree(['a'])
 
215
        wt.add(["a"])
 
216
        wt.commit("1", timestamp=42)
 
217
        target = StringIO()
 
218
        ball = tarfile.open(None, "w|", target)
 
219
        wt.lock_read()
 
220
        try:
 
221
            export_tarball(wt, ball, "bar")
 
222
        finally:
 
223
            wt.unlock()
 
224
        self.assertEquals(["bar/a"], ball.getnames())
 
225
        ball.close()
 
226
 
 
227
 
 
228
class ZipExporterTests(tests.TestCaseWithTransport):
 
229
 
 
230
    def test_per_file_timestamps(self):
 
231
        tree = self.make_branch_and_tree('.')
 
232
        self.build_tree_contents([('har', 'foo')])
 
233
        tree.add('har')
 
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)
 
242
 
 
243
 
 
244
class RootNameTests(tests.TestCase):
 
245
 
 
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('-'))