/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4988.10.3 by John Arbash Meinel
Merge bzr.dev 5007, resolve conflict, update NEWS
1
# Copyright (C) 2009, 2010 Canonical Ltd
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
16
5718.5.12 by Jelmer Vernooij
Add test for export_tarball.
17
"""Tests for bzrlib.export."""
18
19
from cStringIO import StringIO
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
20
import os
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
21
import tarfile
4996.1.1 by Robert Collins
Set the file mtime when exporting to a directory to prevent triggering make rebuilds unnecessarily.
22
import time
5718.5.14 by Jelmer Vernooij
Add test for per-file-timestamp zipfiles.
23
import zipfile
4241.9.5 by Vincent Ladeuil
Fix unicode related OSX failures.
24
25
from bzrlib import (
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
26
    errors,
4241.9.5 by Vincent Ladeuil
Fix unicode related OSX failures.
27
    export,
28
    tests,
29
    )
5718.5.18 by Jelmer Vernooij
Don't export to '-', but rather to ''.
30
from bzrlib.export import get_root_name
5718.5.12 by Jelmer Vernooij
Add test for export_tarball.
31
from bzrlib.export.tar_exporter import export_tarball
5718.5.19 by Jelmer Vernooij
Use features.
32
from bzrlib.tests import features
4241.9.5 by Vincent Ladeuil
Fix unicode related OSX failures.
33
34
5718.5.9 by Jelmer Vernooij
Add test for export zip to stdout.
35
class TestDirExport(tests.TestCaseWithTransport):
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
36
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
37
    def test_missing_file(self):
4010.2.1 by James Westby
Handle files that are not present in the tree when exporting (#174539)
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')
4241.9.5 by Vincent Ladeuil
Fix unicode related OSX failures.
42
        export.export(wt, 'target', format="dir")
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
43
        self.assertPathExists('target/a/b')
44
        self.assertPathDoesNotExist('target/a/c')
4241.9.5 by Vincent Ladeuil
Fix unicode related OSX failures.
45
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
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):
4562.1.2 by Jelmer Vernooij
Use self.requireFeature(tests.SymlinkFeature) per John's review.
52
        self.requireFeature(tests.SymlinkFeature)
4562.1.1 by Jelmer Vernooij
Support exporting symlinks when exporting from a working tree.
53
        wt = self.make_branch_and_tree('.')
54
        os.symlink('source', 'link')
55
        wt.add(['link'])
56
        export.export(wt, 'target', format="dir")
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
57
        self.assertPathExists('target/link')
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
58
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
59
    def test_to_existing_empty_dir_success(self):
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
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")
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
66
        self.assertPathExists('target/a')
67
        self.assertPathExists('target/b')
68
        self.assertPathExists('target/b/c')
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
69
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
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
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
81
    def test_to_existing_nonempty_dir_fail(self):
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
82
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
83
        wt = self.make_branch_and_tree('source')
84
        wt.add(['a', 'b', 'b/c'])
85
        wt.commit('1')
86
        self.build_tree(['target/', 'target/foo'])
87
        self.assertRaises(errors.BzrError, export.export, wt, 'target', format="dir")
4988.10.1 by michal
- bug #511987 fixed, export of single file
88
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
89
    def test_existing_single_file(self):
4988.10.1 by michal
- bug #511987 fixed, export of single file
90
        self.build_tree(['dir1/', 'dir1/dir2/', 'dir1/first', 'dir1/dir2/second'])
91
        wtree = self.make_branch_and_tree('dir1')
92
        wtree.add(['dir2', 'first', 'dir2/second'])
93
        wtree.commit('1')
94
        export.export(wtree, 'target1', format='dir', subdir='first')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
95
        self.assertPathExists('target1/first')
4988.10.1 by michal
- bug #511987 fixed, export of single file
96
        export.export(wtree, 'target2', format='dir', subdir='dir2/second')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
97
        self.assertPathExists('target2/second')
5718.5.7 by Jelmer Vernooij
Support bzr zip exporting to stdout.
98
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
99
    def test_files_same_timestamp(self):
4996.1.1 by Robert Collins
Set the file mtime when exporting to a directory to prevent triggering make rebuilds unnecessarily.
100
        builder = self.make_branch_builder('source')
101
        builder.start_series()
102
        builder.build_snapshot(None, None, [
103
            ('add', ('', 'root-id', 'directory', '')),
104
            ('add', ('a', 'a-id', 'file', 'content\n'))])
105
        builder.build_snapshot(None, None, [
106
            ('add', ('b', 'b-id', 'file', 'content\n'))])
107
        builder.finish_series()
108
        b = builder.get_branch()
109
        b.lock_read()
110
        self.addCleanup(b.unlock)
111
        tree = b.basis_tree()
112
        orig_iter_files_bytes = tree.iter_files_bytes
113
        # Make iter_files_bytes slower, so we provoke mtime skew
114
        def iter_files_bytes(to_fetch):
115
            for thing in orig_iter_files_bytes(to_fetch):
116
                yield thing
117
                time.sleep(1)
118
        tree.iter_files_bytes = iter_files_bytes
119
        export.export(tree, 'target', format='dir')
120
        t = self.get_transport('target')
121
        st_a = t.stat('a')
122
        st_b = t.stat('b')
123
        # All files must be given the same mtime.
124
        self.assertEqual(st_a.st_mtime, st_b.st_mtime)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
125
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
126
    def test_files_per_file_timestamps(self):
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
127
        builder = self.make_branch_builder('source')
128
        builder.start_series()
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
129
        # Earliest allowable date on FAT32 filesystems is 1980-01-01
130
        a_time = time.mktime((1999, 12, 12, 0, 0, 0, 0, 0, 0))
131
        b_time = time.mktime((1980, 01, 01, 0, 0, 0, 0, 0, 0))
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
132
        builder.build_snapshot(None, None, [
133
            ('add', ('', 'root-id', 'directory', '')),
134
            ('add', ('a', 'a-id', 'file', 'content\n'))],
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
135
            timestamp=a_time)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
136
        builder.build_snapshot(None, None, [
137
            ('add', ('b', 'b-id', 'file', 'content\n'))],
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
138
            timestamp=b_time)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
139
        builder.finish_series()
140
        b = builder.get_branch()
141
        b.lock_read()
142
        self.addCleanup(b.unlock)
143
        tree = b.basis_tree()
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
144
        export.export(tree, 'target', format='dir', per_file_timestamps=True)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
145
        t = self.get_transport('target')
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
146
        self.assertEqual(a_time, t.stat('a').st_mtime)
147
        self.assertEqual(b_time, t.stat('b').st_mtime)
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
148
5966.1.1 by Szilveszter Farkas
Fixed exporting subdirectories with per file timestamps.
149
    def test_subdir_files_per_timestamps(self):
150
        builder = self.make_branch_builder('source')
151
        builder.start_series()
152
        foo_time = time.mktime((1999, 12, 12, 0, 0, 0, 0, 0, 0))
153
        builder.build_snapshot(None, None, [
154
            ('add', ('', 'root-id', 'directory', '')),
155
            ('add', ('subdir', 'subdir-id', 'directory', '')),
156
            ('add', ('subdir/foo.txt', 'foo-id', 'file', 'content\n'))],
157
            timestamp=foo_time)
158
        builder.finish_series()
159
        b = builder.get_branch()
160
        b.lock_read()
161
        self.addCleanup(b.unlock)
162
        tree = b.basis_tree()
163
        export.export(tree, 'target', format='dir', subdir='subdir',
164
            per_file_timestamps=True)
165
        t = self.get_transport('target')
166
        self.assertEquals(foo_time, t.stat('foo.txt').st_mtime)
167
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
168
169
class TarExporterTests(tests.TestCaseWithTransport):
170
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
171
    def test_xz(self):
5718.5.19 by Jelmer Vernooij
Use features.
172
        self.requireFeature(features.lzma)
173
        import lzma
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
174
        wt = self.make_branch_and_tree('.')
175
        self.build_tree(['a'])
176
        wt.add(["a"])
177
        wt.commit("1")
5718.5.19 by Jelmer Vernooij
Use features.
178
        export.export(wt, 'target.tar.xz', format="txz")
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
179
        tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.xz'))
180
        self.assertEquals(["target/a"], tf.getnames())
181
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
182
    def test_lzma(self):
5718.5.19 by Jelmer Vernooij
Use features.
183
        self.requireFeature(features.lzma)
184
        import lzma
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
185
        wt = self.make_branch_and_tree('.')
186
        self.build_tree(['a'])
187
        wt.add(["a"])
188
        wt.commit("1")
5718.5.19 by Jelmer Vernooij
Use features.
189
        export.export(wt, 'target.tar.lzma', format="tlzma")
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
190
        tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.lzma'))
191
        self.assertEquals(["target/a"], tf.getnames())
192
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
193
    def test_tgz(self):
194
        wt = self.make_branch_and_tree('.')
195
        self.build_tree(['a'])
196
        wt.add(["a"])
197
        wt.commit("1")
198
        export.export(wt, 'target.tar.gz', format="tgz")
199
        tf = tarfile.open('target.tar.gz')
200
        self.assertEquals(["target/a"], tf.getnames())
201
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
202
    def test_tgz_ignores_dest_path(self):
203
        # The target path should not be a part of the target file.
204
        # (bug #102234)
205
        wt = self.make_branch_and_tree('.')
206
        self.build_tree(['a'])
207
        wt.add(["a"])
208
        wt.commit("1")
209
        os.mkdir("testdir1")
210
        os.mkdir("testdir2")
211
        export.export(wt, 'testdir1/target.tar.gz', format="tgz",
212
            per_file_timestamps=True)
213
        export.export(wt, 'testdir2/target.tar.gz', format="tgz",
214
            per_file_timestamps=True)
215
        file1 = open('testdir1/target.tar.gz', 'r')
216
        self.addCleanup(file1.close)
217
        file2 = open('testdir1/target.tar.gz', 'r')
218
        self.addCleanup(file2.close)
219
        content1 = file1.read()
220
        content2 = file2.read()
221
        self.assertEqualDiff(content1, content2)
222
        # the gzip module doesn't have a way to read back to the original
223
        # filename, but it's stored as-is in the tarfile.
224
        self.assertFalse("testdir1" in content1)
225
        self.assertFalse("target.tar.gz" in content1)
226
        self.assertTrue("target.tar" in content1)
227
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
228
    def test_tbz2(self):
229
        wt = self.make_branch_and_tree('.')
230
        self.build_tree(['a'])
231
        wt.add(["a"])
232
        wt.commit("1")
233
        export.export(wt, 'target.tar.bz2', format="tbz2")
234
        tf = tarfile.open('target.tar.bz2')
235
        self.assertEquals(["target/a"], tf.getnames())
236
237
    def test_xz_stdout(self):
238
        wt = self.make_branch_and_tree('.')
239
        self.assertRaises(errors.BzrError, export.export, wt, '-',
240
            format="txz")
5718.5.12 by Jelmer Vernooij
Add test for export_tarball.
241
242
    def test_export_tarball(self):
243
        wt = self.make_branch_and_tree('.')
244
        self.build_tree(['a'])
245
        wt.add(["a"])
246
        wt.commit("1", timestamp=42)
247
        target = StringIO()
248
        ball = tarfile.open(None, "w|", target)
249
        wt.lock_read()
250
        try:
5718.5.20 by Jelmer Vernooij
Force timezone to UTC in zip test.
251
            export_tarball(wt, ball, "bar")
5718.5.12 by Jelmer Vernooij
Add test for export_tarball.
252
        finally:
253
            wt.unlock()
254
        self.assertEquals(["bar/a"], ball.getnames())
255
        ball.close()
5718.5.14 by Jelmer Vernooij
Add test for per-file-timestamp zipfiles.
256
257
258
class ZipExporterTests(tests.TestCaseWithTransport):
259
260
    def test_per_file_timestamps(self):
261
        tree = self.make_branch_and_tree('.')
262
        self.build_tree_contents([('har', 'foo')])
263
        tree.add('har')
264
        # Earliest allowable date on FAT32 filesystems is 1980-01-01
5718.5.26 by Jelmer Vernooij
Work around zip info time zone issues.
265
        timestamp = 347151600
266
        tree.commit('setup', timestamp=timestamp)
5718.5.14 by Jelmer Vernooij
Add test for per-file-timestamp zipfiles.
267
        export.export(tree.basis_tree(), 'test.zip', format='zip',
268
            per_file_timestamps=True)
269
        zfile = zipfile.ZipFile('test.zip')
270
        info = zfile.getinfo("test/har")
5718.5.26 by Jelmer Vernooij
Work around zip info time zone issues.
271
        self.assertEquals(time.localtime(timestamp)[:6], info.date_time)
5718.5.18 by Jelmer Vernooij
Don't export to '-', but rather to ''.
272
273
274
class RootNameTests(tests.TestCase):
275
276
    def test_root_name(self):
277
        self.assertEquals('mytest', get_root_name('../mytest.tar'))
278
        self.assertEquals('mytar', get_root_name('mytar.tar'))
279
        self.assertEquals('mytar', get_root_name('mytar.tar.bz2'))
280
        self.assertEquals('tar.tar.tar', get_root_name('tar.tar.tar.tgz'))
281
        self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5.tar.gz'))
282
        self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5.zip'))
283
        self.assertEquals('bzr-0.0.5', get_root_name('bzr-0.0.5'))
284
        self.assertEquals('mytar', get_root_name('a/long/path/mytar.tgz'))
285
        self.assertEquals('other',
286
            get_root_name('../parent/../dir/other.tbz2'))
287
        self.assertEquals('', get_root_name('-'))