/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
6968.2.7 by Jelmer Vernooij
Add breezy.archive.
30
from ..archive.tar import tarball_generator
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
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
6964.1.1 by Jelmer Vernooij
Support exporting nested trees.
61
    def test_nested_tree(self):
62
        wt = self.make_branch_and_tree('.', format='development-subtree')
63
        subtree = self.make_branch_and_tree('subtree')
64
        self.build_tree(['subtree/file'])
65
        subtree.add(['file'])
66
        wt.add(['subtree'])
67
        export.export(wt, 'target', format="dir")
68
        self.assertPathExists('target/subtree')
69
        # TODO(jelmer): Once iter_entries_by_dir supports nested tree iteration:
70
        # self.assertPathExists('target/subtree/file')
71
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
72
    def test_to_existing_empty_dir_success(self):
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
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")
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
79
        self.assertPathExists('target/a')
80
        self.assertPathExists('target/b')
81
        self.assertPathExists('target/b/c')
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
82
5809.3.1 by Aaron Bentley
Switch to iter_entries_by_dir.
83
    def test_empty_subdir(self):
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/'])
89
        export.export(wt, 'target', format="dir", subdir='')
90
        self.assertPathExists('target/a')
91
        self.assertPathExists('target/b')
92
        self.assertPathExists('target/b/c')
93
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
94
    def test_to_existing_nonempty_dir_fail(self):
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
95
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
96
        wt = self.make_branch_and_tree('source')
97
        wt.add(['a', 'b', 'b/c'])
98
        wt.commit('1')
99
        self.build_tree(['target/', 'target/foo'])
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
100
        self.assertRaises(errors.BzrError,
101
            export.export, wt, 'target', format="dir")
4988.10.1 by michal
- bug #511987 fixed, export of single file
102
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
103
    def test_existing_single_file(self):
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
104
        self.build_tree([
105
            'dir1/', 'dir1/dir2/', 'dir1/first', 'dir1/dir2/second'])
4988.10.1 by michal
- bug #511987 fixed, export of single file
106
        wtree = self.make_branch_and_tree('dir1')
107
        wtree.add(['dir2', 'first', 'dir2/second'])
108
        wtree.commit('1')
109
        export.export(wtree, 'target1', format='dir', subdir='first')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
110
        self.assertPathExists('target1/first')
4988.10.1 by michal
- bug #511987 fixed, export of single file
111
        export.export(wtree, 'target2', format='dir', subdir='dir2/second')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
112
        self.assertPathExists('target2/second')
5718.5.7 by Jelmer Vernooij
Support bzr zip exporting to stdout.
113
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
114
    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.
115
        builder = self.make_branch_builder('source')
116
        builder.start_series()
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
117
        builder.build_snapshot(None, [
6973.11.1 by Jelmer Vernooij
Fix export_pot.
118
            ('add', ('', b'root-id', 'directory', '')),
119
            ('add', ('a', b'a-id', 'file', b'content\n'))])
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
120
        builder.build_snapshot(None, [
6973.11.1 by Jelmer Vernooij
Fix export_pot.
121
            ('add', ('b', b'b-id', 'file', b'content\n'))])
4996.1.1 by Robert Collins
Set the file mtime when exporting to a directory to prevent triggering make rebuilds unnecessarily.
122
        builder.finish_series()
123
        b = builder.get_branch()
124
        b.lock_read()
125
        self.addCleanup(b.unlock)
126
        tree = b.basis_tree()
127
        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.
128
4996.1.1 by Robert Collins
Set the file mtime when exporting to a directory to prevent triggering make rebuilds unnecessarily.
129
        # Make iter_files_bytes slower, so we provoke mtime skew
130
        def iter_files_bytes(to_fetch):
131
            for thing in orig_iter_files_bytes(to_fetch):
132
                yield thing
133
                time.sleep(1)
134
        tree.iter_files_bytes = iter_files_bytes
135
        export.export(tree, 'target', format='dir')
136
        t = self.get_transport('target')
137
        st_a = t.stat('a')
138
        st_b = t.stat('b')
139
        # All files must be given the same mtime.
140
        self.assertEqual(st_a.st_mtime, st_b.st_mtime)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
141
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
142
    def test_files_per_file_timestamps(self):
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
143
        builder = self.make_branch_builder('source')
144
        builder.start_series()
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
145
        # Earliest allowable date on FAT32 filesystems is 1980-01-01
146
        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.
147
        b_time = time.mktime((1980, 0o1, 0o1, 0, 0, 0, 0, 0, 0))
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
148
        builder.build_snapshot(None, [
6973.11.1 by Jelmer Vernooij
Fix export_pot.
149
            ('add', ('', b'root-id', 'directory', '')),
150
            ('add', ('a', b'a-id', 'file', b'content\n'))],
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
151
            timestamp=a_time)
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
152
        builder.build_snapshot(None, [
6973.11.1 by Jelmer Vernooij
Fix export_pot.
153
            ('add', ('b', b'b-id', 'file', b'content\n'))],
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
154
            timestamp=b_time)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
155
        builder.finish_series()
156
        b = builder.get_branch()
157
        b.lock_read()
158
        self.addCleanup(b.unlock)
159
        tree = b.basis_tree()
5076.2.3 by Jelmer Vernooij
Review comments from Rob.
160
        export.export(tree, 'target', format='dir', per_file_timestamps=True)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
161
        t = self.get_transport('target')
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
162
        self.assertEqual(a_time, t.stat('a').st_mtime)
163
        self.assertEqual(b_time, t.stat('b').st_mtime)
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
164
5966.1.1 by Szilveszter Farkas
Fixed exporting subdirectories with per file timestamps.
165
    def test_subdir_files_per_timestamps(self):
166
        builder = self.make_branch_builder('source')
167
        builder.start_series()
168
        foo_time = time.mktime((1999, 12, 12, 0, 0, 0, 0, 0, 0))
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
169
        builder.build_snapshot(None, [
6973.11.1 by Jelmer Vernooij
Fix export_pot.
170
            ('add', ('', b'root-id', 'directory', '')),
171
            ('add', ('subdir', b'subdir-id', 'directory', '')),
172
            ('add', ('subdir/foo.txt', b'foo-id', 'file', b'content\n'))],
5966.1.1 by Szilveszter Farkas
Fixed exporting subdirectories with per file timestamps.
173
            timestamp=foo_time)
174
        builder.finish_series()
175
        b = builder.get_branch()
176
        b.lock_read()
177
        self.addCleanup(b.unlock)
178
        tree = b.basis_tree()
179
        export.export(tree, 'target', format='dir', subdir='subdir',
180
            per_file_timestamps=True)
181
        t = self.get_transport('target')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
182
        self.assertEqual(foo_time, t.stat('foo.txt').st_mtime)
5966.1.1 by Szilveszter Farkas
Fixed exporting subdirectories with per file timestamps.
183
5718.5.10 by Jelmer Vernooij
Support creating .tar.xz files.
184
185
class TarExporterTests(tests.TestCaseWithTransport):
186
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
187
    def test_xz(self):
5718.5.19 by Jelmer Vernooij
Use features.
188
        self.requireFeature(features.lzma)
189
        import lzma
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
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.xz', format="txz")
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
195
        tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.xz'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
196
        self.assertEqual(["target/a"], tf.getnames())
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
197
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
198
    def test_lzma(self):
5718.5.19 by Jelmer Vernooij
Use features.
199
        self.requireFeature(features.lzma)
200
        import lzma
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
201
        wt = self.make_branch_and_tree('.')
202
        self.build_tree(['a'])
203
        wt.add(["a"])
204
        wt.commit("1")
5718.5.19 by Jelmer Vernooij
Use features.
205
        export.export(wt, 'target.tar.lzma', format="tlzma")
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
206
        tf = tarfile.open(fileobj=lzma.LZMAFile('target.tar.lzma'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
207
        self.assertEqual(["target/a"], tf.getnames())
5718.5.17 by Jelmer Vernooij
Support tar.lzma.
208
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
209
    def test_tgz(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.gz', format="tgz")
215
        tf = tarfile.open('target.tar.gz')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
216
        self.assertEqual(["target/a"], tf.getnames())
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
217
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
218
    def test_tgz_ignores_dest_path(self):
219
        # The target path should not be a part of the target file.
220
        # (bug #102234)
221
        wt = self.make_branch_and_tree('.')
222
        self.build_tree(['a'])
223
        wt.add(["a"])
224
        wt.commit("1")
225
        os.mkdir("testdir1")
226
        os.mkdir("testdir2")
227
        export.export(wt, 'testdir1/target.tar.gz', format="tgz",
228
            per_file_timestamps=True)
229
        export.export(wt, 'testdir2/target.tar.gz', format="tgz",
230
            per_file_timestamps=True)
6973.11.1 by Jelmer Vernooij
Fix export_pot.
231
        file1 = open('testdir1/target.tar.gz', 'rb')
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
232
        self.addCleanup(file1.close)
6973.11.1 by Jelmer Vernooij
Fix export_pot.
233
        file2 = open('testdir1/target.tar.gz', 'rb')
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
234
        self.addCleanup(file2.close)
235
        content1 = file1.read()
236
        content2 = file2.read()
237
        self.assertEqualDiff(content1, content2)
238
        # the gzip module doesn't have a way to read back to the original
239
        # filename, but it's stored as-is in the tarfile.
6973.11.1 by Jelmer Vernooij
Fix export_pot.
240
        self.assertFalse(b"testdir1" in content1)
241
        self.assertFalse(b"target.tar.gz" in content1)
242
        self.assertTrue(b"target.tar" in content1)
5718.5.15 by Jelmer Vernooij
Only write out basename of the tarfile to the gzip file.
243
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
244
    def test_tbz2(self):
245
        wt = self.make_branch_and_tree('.')
246
        self.build_tree(['a'])
247
        wt.add(["a"])
248
        wt.commit("1")
249
        export.export(wt, 'target.tar.bz2', format="tbz2")
250
        tf = tarfile.open('target.tar.bz2')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
251
        self.assertEqual(["target/a"], tf.getnames())
5718.5.11 by Jelmer Vernooij
Tests, tests, tests.
252
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
253
    def test_export_tarball_generator(self):
5718.5.12 by Jelmer Vernooij
Add test for export_tarball.
254
        wt = self.make_branch_and_tree('.')
255
        self.build_tree(['a'])
256
        wt.add(["a"])
257
        wt.commit("1", timestamp=42)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
258
        target = BytesIO()
6968.2.7 by Jelmer Vernooij
Add breezy.archive.
259
        with wt.lock_read():
260
            target.writelines(tarball_generator(wt, "bar"))
5967.6.3 by Martin Pool
Further shrink export code
261
        # Ball should now be closed.
262
        target.seek(0)
263
        ball2 = tarfile.open(None, "r", target)
264
        self.addCleanup(ball2.close)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
265
        self.assertEqual(["bar/a"], ball2.getnames())
5718.5.14 by Jelmer Vernooij
Add test for per-file-timestamp zipfiles.
266
267
268
class ZipExporterTests(tests.TestCaseWithTransport):
269
270
    def test_per_file_timestamps(self):
271
        tree = self.make_branch_and_tree('.')
6855.4.1 by Jelmer Vernooij
Yet more bees.
272
        self.build_tree_contents([('har', b'foo')])
5718.5.14 by Jelmer Vernooij
Add test for per-file-timestamp zipfiles.
273
        tree.add('har')
274
        # Earliest allowable date on FAT32 filesystems is 1980-01-01
5718.5.26 by Jelmer Vernooij
Work around zip info time zone issues.
275
        timestamp = 347151600
276
        tree.commit('setup', timestamp=timestamp)
5718.5.14 by Jelmer Vernooij
Add test for per-file-timestamp zipfiles.
277
        export.export(tree.basis_tree(), 'test.zip', format='zip',
278
            per_file_timestamps=True)
279
        zfile = zipfile.ZipFile('test.zip')
280
        info = zfile.getinfo("test/har")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
281
        self.assertEqual(time.localtime(timestamp)[:6], info.date_time)
5718.5.18 by Jelmer Vernooij
Don't export to '-', but rather to ''.
282
283
284
class RootNameTests(tests.TestCase):
285
286
    def test_root_name(self):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
287
        self.assertEqual('mytest', get_root_name('../mytest.tar'))
288
        self.assertEqual('mytar', get_root_name('mytar.tar'))
289
        self.assertEqual('mytar', get_root_name('mytar.tar.bz2'))
290
        self.assertEqual('tar.tar.tar', get_root_name('tar.tar.tar.tgz'))
291
        self.assertEqual('bzr-0.0.5', get_root_name('bzr-0.0.5.tar.gz'))
292
        self.assertEqual('bzr-0.0.5', get_root_name('bzr-0.0.5.zip'))
293
        self.assertEqual('bzr-0.0.5', get_root_name('bzr-0.0.5'))
294
        self.assertEqual('mytar', get_root_name('a/long/path/mytar.tgz'))
295
        self.assertEqual('other',
5718.5.18 by Jelmer Vernooij
Don't export to '-', but rather to ''.
296
            get_root_name('../parent/../dir/other.tbz2'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
297
        self.assertEqual('', get_root_name('-'))