/brz/remove-bazaar

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