/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
1
# Copyright (C) 2005-2013, 2016 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.82.7 by John Arbash Meinel
Adding patches.py into bzrlib, including the tests into the test suite.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.82.7 by John Arbash Meinel
Adding patches.py into bzrlib, including the tests into the test suite.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.82.7 by John Arbash Meinel
Adding patches.py into bzrlib, including the tests into the test suite.
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
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
16
1711.7.27 by John Arbash Meinel
Investigating why test_bundle fails, something isn't transmitting properly.
17
import os
6621.25.1 by Martin
Adapt to SocketServer module name change
18
try:
19
    import socketserver
20
except ImportError:
21
    import SocketServer as socketserver
1711.7.34 by John Arbash Meinel
Include a test to ensure bundles handle trailing whitespace.
22
import sys
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
23
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from .. import (
4241.14.13 by Vincent Ladeuil
Some more cleanup.
25
    diff,
1996.3.20 by John Arbash Meinel
[merge] bzr.dev 2063
26
    errors,
4241.14.13 by Vincent Ladeuil
Some more cleanup.
27
    merge,
3638.3.2 by Vincent Ladeuil
Fix all calls to tempfile.mkdtemp to osutils.mkdtemp.
28
    osutils,
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
29
    revision as _mod_revision,
4241.14.13 by Vincent Ladeuil
Some more cleanup.
30
    tests,
1910.2.64 by Aaron Bentley
Changes from review
31
    treebuilder,
32
    )
6670.4.1 by Jelmer Vernooij
Update imports.
33
from ..bzr import (
34
    bzrdir,
35
    inventory,
36
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
37
from ..bundle import read_mergeable_from_url
38
from ..bundle.apply_bundle import install_bundle, merge_bundle
39
from ..bundle.bundle_data import BundleTree
40
from ..directory_service import directories
41
from ..bundle.serializer import write_bundle, read_bundle, v09, v4
42
from ..bundle.serializer.v08 import BundleSerializerV08
43
from ..bundle.serializer.v09 import BundleSerializerV09
44
from ..bundle.serializer.v4 import BundleSerializerV4
6670.4.5 by Jelmer Vernooij
Move breezy.repofmt contents to breezy.bzr.
45
from ..bzr import knitrepo
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
46
from ..sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
47
    BytesIO,
48
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
49
from . import (
6206.1.9 by Vincent Ladeuil
Simpler fix for test_smart_server_connection_reset re-using more of the existing test server infrastructure.
50
    features,
51
    test_commit,
3251.4.11 by Aaron Bentley
Fix wrong local lookups
52
    test_read_bundle,
6206.1.9 by Vincent Ladeuil
Simpler fix for test_smart_server_connection_reset re-using more of the existing test server infrastructure.
53
    test_server,
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
54
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
55
from ..transform import TreeTransform
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
56
1185.82.90 by Aaron Bentley
Reorganized test suite
57
4543.2.9 by John Arbash Meinel
Down to 2 failing tests.
58
def get_text(vf, key):
59
    """Get the fulltext for a given revision id that is present in the vf"""
60
    stream = vf.get_record_stream([key], 'unordered', True)
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
61
    record = next(stream)
4543.2.9 by John Arbash Meinel
Down to 2 failing tests.
62
    return record.get_bytes_as('fulltext')
63
64
65
def get_inventory_text(repo, revision_id):
66
    """Get the fulltext for the inventory at revision id"""
67
    repo.lock_read()
68
    try:
69
        return get_text(repo.inventories, (revision_id,))
70
    finally:
71
        repo.unlock()
72
73
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
74
class MockTree(object):
5837.2.5 by Jelmer Vernooij
Fix two warnings.
75
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
76
    def __init__(self):
6670.4.3 by Jelmer Vernooij
Fix more imports.
77
        from ..bzr.inventory import InventoryDirectory, ROOT_ID
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
78
        object.__init__(self)
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
79
        self.paths = {ROOT_ID: ""}
80
        self.ids = {"": ROOT_ID}
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
81
        self.contents = {}
1731.1.4 by Aaron Bentley
merge from bzr.dev
82
        self.root = InventoryDirectory(ROOT_ID, '', None)
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
83
84
    inventory = property(lambda x:x)
6405.2.9 by Jelmer Vernooij
More test fixes.
85
    root_inventory = property(lambda x:x)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
86
6405.2.10 by Jelmer Vernooij
Fix more tests.
87
    def get_root_id(self):
88
        return self.root.file_id
89
5837.2.5 by Jelmer Vernooij
Fix two warnings.
90
    def all_file_ids(self):
91
        return set(self.paths.keys())
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
92
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
93
    def is_executable(self, path, file_id):
6445.2.6 by Jelmer Vernooij
Review feedback.
94
        # Not all the files are executable.
6405.2.9 by Jelmer Vernooij
More test fixes.
95
        return False
96
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
97
    def __getitem__(self, file_id):
98
        if file_id == self.root.file_id:
99
            return self.root
100
        else:
101
            return self.make_entry(file_id, self.paths[file_id])
102
103
    def parent_id(self, file_id):
1711.7.27 by John Arbash Meinel
Investigating why test_bundle fails, something isn't transmitting properly.
104
        parent_dir = os.path.dirname(self.paths[file_id])
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
105
        if parent_dir == "":
106
            return None
107
        return self.ids[parent_dir]
108
109
    def iter_entries(self):
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
110
        for path, file_id in self.ids.items():
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
111
            yield path, self[file_id]
112
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
113
    def kind(self, path, file_id=None):
114
        if file_id is None:
115
            file_id = self.path2id(path)
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
116
        if file_id in self.contents:
117
            kind = 'file'
118
        else:
119
            kind = 'directory'
120
        return kind
121
122
    def make_entry(self, file_id, path):
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
123
        from ..bzr.inventory import (InventoryFile, InventoryDirectory,
6468.2.8 by Jelmer Vernooij
Fix remaining test.
124
            InventoryLink)
1711.7.27 by John Arbash Meinel
Investigating why test_bundle fails, something isn't transmitting properly.
125
        name = os.path.basename(path)
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
126
        kind = self.kind(path, file_id)
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
127
        parent_id = self.parent_id(file_id)
6809.4.9 by Jelmer Vernooij
Fix some more tests.
128
        text_sha_1, text_size = self.contents_stats(path, file_id)
0.5.119 by John Arbash Meinel
Recreated the factory. We really need InventoryEntry.create()
129
        if kind == 'directory':
130
            ie = InventoryDirectory(file_id, name, parent_id)
131
        elif kind == 'file':
132
            ie = InventoryFile(file_id, name, parent_id)
5365.2.2 by Andrew Bennetts
Fix test failures for bundles and upgrades.
133
            ie.text_sha1 = text_sha_1
134
            ie.text_size = text_size
0.5.119 by John Arbash Meinel
Recreated the factory. We really need InventoryEntry.create()
135
        elif kind == 'symlink':
136
            ie = InventoryLink(file_id, name, parent_id)
137
        else:
3638.3.16 by Vincent Ladeuil
Remove XFAIL from test_unicode_bundle.
138
            raise errors.BzrError('unknown kind %r' % kind)
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
139
        return ie
140
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
141
    def add_dir(self, file_id, path):
142
        self.paths[file_id] = path
143
        self.ids[path] = file_id
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
144
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
145
    def add_file(self, file_id, path, contents):
146
        self.add_dir(file_id, path)
147
        self.contents[file_id] = contents
148
149
    def path2id(self, path):
150
        return self.ids.get(path)
151
152
    def id2path(self, file_id):
153
        return self.paths.get(file_id)
154
155
    def has_id(self, file_id):
156
        return self.id2path(file_id) is not None
157
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
158
    def get_file(self, path, file_id=None):
159
        if file_id is None:
160
            file_id = self.path2id(path)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
161
        result = BytesIO()
6809.4.9 by Jelmer Vernooij
Fix some more tests.
162
        try:
163
            result.write(self.contents[file_id])
164
        except KeyError:
165
            raise errors.NoSuchFile(path)
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
166
        result.seek(0, 0)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
167
        return result
168
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
169
    def get_file_revision(self, path, file_id=None):
170
        if file_id is None:
171
            file_id = self.path2id(path)
5793.2.6 by Jelmer Vernooij
Add MockTree.get_file_revision.
172
        return self.inventory[file_id].revision
173
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
174
    def get_file_size(self, path, file_id=None):
175
        if file_id is None:
176
            file_id = self.path2id(path)
6468.2.8 by Jelmer Vernooij
Fix remaining test.
177
        return self.inventory[file_id].text_size
178
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
179
    def get_file_sha1(self, path, file_id=None):
180
        if file_id is None:
181
            file_id = self.path2id(path)
6468.2.8 by Jelmer Vernooij
Fix remaining test.
182
        return self.inventory[file_id].text_sha1
183
6809.4.9 by Jelmer Vernooij
Fix some more tests.
184
    def contents_stats(self, path, file_id):
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
185
        if file_id not in self.contents:
186
            return None, None
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
187
        text_sha1 = osutils.sha_file(self.get_file(path, file_id))
0.6.1 by Aaron Bentley
Fleshed out MockTree, fixed all test failures
188
        return text_sha1, len(self.contents[file_id])
189
190
4241.14.13 by Vincent Ladeuil
Some more cleanup.
191
class BTreeTester(tests.TestCase):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
192
    """A simple unittest tester for the BundleTree class."""
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
193
194
    def make_tree_1(self):
195
        mtree = MockTree()
196
        mtree.add_dir("a", "grandparent")
197
        mtree.add_dir("b", "grandparent/parent")
198
        mtree.add_file("c", "grandparent/parent/file", "Hello\n")
199
        mtree.add_dir("d", "grandparent/alt_parent")
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
200
        return BundleTree(mtree, ''), mtree
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
201
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
202
    def test_renames(self):
203
        """Ensure that file renames have the proper effect on children"""
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
204
        btree = self.make_tree_1()[0]
205
        self.assertEqual(btree.old_path("grandparent"), "grandparent")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
206
        self.assertEqual(btree.old_path("grandparent/parent"),
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
207
                         "grandparent/parent")
208
        self.assertEqual(btree.old_path("grandparent/parent/file"),
209
                         "grandparent/parent/file")
210
211
        self.assertEqual(btree.id2path("a"), "grandparent")
212
        self.assertEqual(btree.id2path("b"), "grandparent/parent")
213
        self.assertEqual(btree.id2path("c"), "grandparent/parent/file")
214
215
        self.assertEqual(btree.path2id("grandparent"), "a")
216
        self.assertEqual(btree.path2id("grandparent/parent"), "b")
217
        self.assertEqual(btree.path2id("grandparent/parent/file"), "c")
218
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
219
        self.assertTrue(btree.path2id("grandparent2") is None)
220
        self.assertTrue(btree.path2id("grandparent2/parent") is None)
221
        self.assertTrue(btree.path2id("grandparent2/parent/file") is None)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
222
223
        btree.note_rename("grandparent", "grandparent2")
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
224
        self.assertTrue(btree.old_path("grandparent") is None)
225
        self.assertTrue(btree.old_path("grandparent/parent") is None)
226
        self.assertTrue(btree.old_path("grandparent/parent/file") is None)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
227
228
        self.assertEqual(btree.id2path("a"), "grandparent2")
229
        self.assertEqual(btree.id2path("b"), "grandparent2/parent")
230
        self.assertEqual(btree.id2path("c"), "grandparent2/parent/file")
231
232
        self.assertEqual(btree.path2id("grandparent2"), "a")
233
        self.assertEqual(btree.path2id("grandparent2/parent"), "b")
234
        self.assertEqual(btree.path2id("grandparent2/parent/file"), "c")
235
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
236
        self.assertTrue(btree.path2id("grandparent") is None)
237
        self.assertTrue(btree.path2id("grandparent/parent") is None)
238
        self.assertTrue(btree.path2id("grandparent/parent/file") is None)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
239
240
        btree.note_rename("grandparent/parent", "grandparent2/parent2")
241
        self.assertEqual(btree.id2path("a"), "grandparent2")
242
        self.assertEqual(btree.id2path("b"), "grandparent2/parent2")
243
        self.assertEqual(btree.id2path("c"), "grandparent2/parent2/file")
244
245
        self.assertEqual(btree.path2id("grandparent2"), "a")
246
        self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
247
        self.assertEqual(btree.path2id("grandparent2/parent2/file"), "c")
248
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
249
        self.assertTrue(btree.path2id("grandparent2/parent") is None)
250
        self.assertTrue(btree.path2id("grandparent2/parent/file") is None)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
251
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
252
        btree.note_rename("grandparent/parent/file",
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
253
                          "grandparent2/parent2/file2")
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
254
        self.assertEqual(btree.id2path("a"), "grandparent2")
255
        self.assertEqual(btree.id2path("b"), "grandparent2/parent2")
256
        self.assertEqual(btree.id2path("c"), "grandparent2/parent2/file2")
257
258
        self.assertEqual(btree.path2id("grandparent2"), "a")
259
        self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
260
        self.assertEqual(btree.path2id("grandparent2/parent2/file2"), "c")
261
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
262
        self.assertTrue(btree.path2id("grandparent2/parent2/file") is None)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
263
264
    def test_moves(self):
265
        """Ensure that file moves have the proper effect on children"""
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
266
        btree = self.make_tree_1()[0]
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
267
        btree.note_rename("grandparent/parent/file",
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
268
                          "grandparent/alt_parent/file")
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
269
        self.assertEqual(btree.id2path("c"), "grandparent/alt_parent/file")
270
        self.assertEqual(btree.path2id("grandparent/alt_parent/file"), "c")
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
271
        self.assertTrue(btree.path2id("grandparent/parent/file") is None)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
272
273
    def unified_diff(self, old, new):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
274
        out = BytesIO()
4241.14.13 by Vincent Ladeuil
Some more cleanup.
275
        diff.internal_diff("old", old, "new", new, out)
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
276
        out.seek(0, 0)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
277
        return out.read()
278
279
    def make_tree_2(self):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
280
        btree = self.make_tree_1()[0]
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
281
        btree.note_rename("grandparent/parent/file",
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
282
                          "grandparent/alt_parent/file")
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
283
        self.assertTrue(btree.id2path("e") is None)
284
        self.assertTrue(btree.path2id("grandparent/parent/file") is None)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
285
        btree.note_id("e", "grandparent/parent/file")
286
        return btree
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
287
288
    def test_adds(self):
289
        """File/inventory adds"""
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
290
        btree = self.make_tree_2()
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
291
        add_patch = self.unified_diff([], ["Extra cheese\n"])
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
292
        btree.note_patch("grandparent/parent/file", add_patch)
293
        btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
294
        btree.note_target('grandparent/parent/symlink', 'venus')
295
        self.adds_test(btree)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
296
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
297
    def adds_test(self, btree):
6809.4.9 by Jelmer Vernooij
Fix some more tests.
298
        self.assertEqual(btree.id2path("e"), "grandparent/parent/file")
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
299
        self.assertEqual(btree.path2id("grandparent/parent/file"), "e")
6809.4.9 by Jelmer Vernooij
Fix some more tests.
300
        self.assertEqual(btree.get_file("grandparent/parent/file").read(),
301
                         "Extra cheese\n")
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
302
        self.assertEqual(
303
            btree.get_symlink_target('grandparent/parent/symlink'), 'venus')
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
304
305
    def test_adds2(self):
306
        """File/inventory adds, with patch-compatibile renames"""
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
307
        btree = self.make_tree_2()
308
        btree.contents_by_id = False
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
309
        add_patch = self.unified_diff(["Hello\n"], ["Extra cheese\n"])
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
310
        btree.note_patch("grandparent/parent/file", add_patch)
311
        btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
312
        btree.note_target('grandparent/parent/symlink', 'venus')
313
        self.adds_test(btree)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
314
315
    def make_tree_3(self):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
316
        btree, mtree = self.make_tree_1()
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
317
        mtree.add_file("e", "grandparent/parent/topping", "Anchovies\n")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
318
        btree.note_rename("grandparent/parent/file",
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
319
                          "grandparent/alt_parent/file")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
320
        btree.note_rename("grandparent/parent/topping",
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
321
                          "grandparent/alt_parent/stopping")
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
322
        return btree
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
323
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
324
    def get_file_test(self, btree):
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
325
        self.assertEqual(btree.get_file(btree.id2path("e")).read(), "Lemon\n")
326
        self.assertEqual(btree.get_file(btree.id2path("c")).read(), "Hello\n")
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
327
328
    def test_get_file(self):
329
        """Get file contents"""
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
330
        btree = self.make_tree_3()
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
331
        mod_patch = self.unified_diff(["Anchovies\n"], ["Lemon\n"])
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
332
        btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
333
        self.get_file_test(btree)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
334
335
    def test_get_file2(self):
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
336
        """Get file contents, with patch-compatible renames"""
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
337
        btree = self.make_tree_3()
338
        btree.contents_by_id = False
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
339
        mod_patch = self.unified_diff([], ["Lemon\n"])
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
340
        btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
341
        mod_patch = self.unified_diff([], ["Hello\n"])
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
342
        btree.note_patch("grandparent/alt_parent/file", mod_patch)
343
        self.get_file_test(btree)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
344
345
    def test_delete(self):
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
346
        "Deletion by bundle"
347
        btree = self.make_tree_1()[0]
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
348
        self.assertEqual(btree.get_file(btree.id2path("c")).read(), "Hello\n")
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
349
        btree.note_deletion("grandparent/parent/file")
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
350
        self.assertTrue(btree.id2path("c") is None)
351
        self.assertTrue(btree.path2id("grandparent/parent/file") is None)
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
352
353
    def sorted_ids(self, tree):
6619.3.18 by Jelmer Vernooij
Run 2to3 idioms fixer.
354
        ids = sorted(tree.all_file_ids())
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
355
        return ids
356
357
    def test_iteration(self):
358
        """Ensure that iteration through ids works properly"""
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
359
        btree = self.make_tree_1()[0]
1852.6.3 by Robert Collins
Make iter(Tree) consistent for all tree types.
360
        self.assertEqual(self.sorted_ids(btree),
361
            [inventory.ROOT_ID, 'a', 'b', 'c', 'd'])
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
362
        btree.note_deletion("grandparent/parent/file")
363
        btree.note_id("e", "grandparent/alt_parent/fool", kind="directory")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
364
        btree.note_last_changed("grandparent/alt_parent/fool",
1185.82.95 by Aaron Bentley
Restore path-orientation of ChangesetTree
365
                                "revisionidiguess")
1852.6.3 by Robert Collins
Make iter(Tree) consistent for all tree types.
366
        self.assertEqual(self.sorted_ids(btree),
367
            [inventory.ROOT_ID, 'a', 'b', 'd', 'e'])
0.5.66 by John Arbash Meinel
Refactoring, moving test code into test (switching back to assert is None)
368
1185.82.7 by John Arbash Meinel
Adding patches.py into bzrlib, including the tests into the test suite.
369
4241.14.13 by Vincent Ladeuil
Some more cleanup.
370
class BundleTester1(tests.TestCaseWithTransport):
1910.2.49 by Aaron Bentley
Ensure that 0.8 bundles aren't used with KnitRepository2
371
372
    def test_mismatched_bundle(self):
373
        format = bzrdir.BzrDirMetaFormat1()
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
374
        format.repository_format = knitrepo.RepositoryFormatKnit3()
1910.2.49 by Aaron Bentley
Ensure that 0.8 bundles aren't used with KnitRepository2
375
        serializer = BundleSerializerV08('0.8')
376
        b = self.make_branch('.', format=format)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
377
        self.assertRaises(errors.IncompatibleBundleFormat, serializer.write,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
378
                          b.repository, [], {}, BytesIO())
1910.2.49 by Aaron Bentley
Ensure that 0.8 bundles aren't used with KnitRepository2
379
1910.2.51 by Aaron Bentley
Bundles now corrupt repositories
380
    def test_matched_bundle(self):
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
381
        """Don't raise IncompatibleBundleFormat for knit2 and bundle0.9"""
1910.2.51 by Aaron Bentley
Bundles now corrupt repositories
382
        format = bzrdir.BzrDirMetaFormat1()
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
383
        format.repository_format = knitrepo.RepositoryFormatKnit3()
1910.2.51 by Aaron Bentley
Bundles now corrupt repositories
384
        serializer = BundleSerializerV09('0.9')
385
        b = self.make_branch('.', format=format)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
386
        serializer.write(b.repository, [], {}, BytesIO())
1910.2.51 by Aaron Bentley
Bundles now corrupt repositories
387
1910.2.60 by Aaron Bentley
Ensure that new-model revisions aren't installed into old-model repos
388
    def test_mismatched_model(self):
389
        """Try copying a bundle from knit2 to knit1"""
390
        format = bzrdir.BzrDirMetaFormat1()
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
391
        format.repository_format = knitrepo.RepositoryFormatKnit3()
1910.2.60 by Aaron Bentley
Ensure that new-model revisions aren't installed into old-model repos
392
        source = self.make_branch_and_tree('source', format=format)
393
        source.commit('one', rev_id='one-id')
394
        source.commit('two', rev_id='two-id')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
395
        text = BytesIO()
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
396
        write_bundle(source.branch.repository, 'two-id', 'null:', text,
1910.2.60 by Aaron Bentley
Ensure that new-model revisions aren't installed into old-model repos
397
                     format='0.9')
398
        text.seek(0)
399
400
        format = bzrdir.BzrDirMetaFormat1()
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
401
        format.repository_format = knitrepo.RepositoryFormatKnit1()
1910.2.60 by Aaron Bentley
Ensure that new-model revisions aren't installed into old-model repos
402
        target = self.make_branch('target', format=format)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
403
        self.assertRaises(errors.IncompatibleRevision, install_bundle,
1910.2.60 by Aaron Bentley
Ensure that new-model revisions aren't installed into old-model repos
404
                          target.repository, read_bundle(text))
405
1910.2.51 by Aaron Bentley
Bundles now corrupt repositories
406
2520.4.43 by Aaron Bentley
Fix test suite
407
class BundleTester(object):
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
408
409
    def bzrdir_format(self):
410
        format = bzrdir.BzrDirMetaFormat1()
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
411
        format.repository_format = knitrepo.RepositoryFormatKnit1()
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
412
        return format
413
414
    def make_branch_and_tree(self, path, format=None):
415
        if format is None:
416
            format = self.bzrdir_format()
4241.14.13 by Vincent Ladeuil
Some more cleanup.
417
        return tests.TestCaseWithTransport.make_branch_and_tree(
418
            self, path, format)
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
419
420
    def make_branch(self, path, format=None):
421
        if format is None:
422
            format = self.bzrdir_format()
4241.14.13 by Vincent Ladeuil
Some more cleanup.
423
        return tests.TestCaseWithTransport.make_branch(self, path, format)
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
424
1793.3.1 by John Arbash Meinel
Clean up the bundle tests a little bit.
425
    def create_bundle_text(self, base_rev_id, rev_id):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
426
        bundle_txt = BytesIO()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
427
        rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
428
                               bundle_txt, format=self.format)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
429
        bundle_txt.seek(0)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
430
        self.assertEqual(bundle_txt.readline(),
1910.2.51 by Aaron Bentley
Bundles now corrupt repositories
431
                         '# Bazaar revision bundle v%s\n' % self.format)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
432
        self.assertEqual(bundle_txt.readline(), '#\n')
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
433
1185.82.14 by Aaron Bentley
API updates
434
        rev = self.b1.repository.get_revision(rev_id)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
435
        self.assertEqual(bundle_txt.readline().decode('utf-8'),
436
                         u'# message:\n')
437
        bundle_txt.seek(0)
1793.3.1 by John Arbash Meinel
Clean up the bundle tests a little bit.
438
        return bundle_txt, rev_ids
439
440
    def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
441
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
442
        Make sure that the text generated is valid, and that it
443
        can be applied against the base, and generate the same information.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
444
445
        :return: The in-memory bundle
1793.3.1 by John Arbash Meinel
Clean up the bundle tests a little bit.
446
        """
447
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
448
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
449
        # This should also validate the generated bundle
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
450
        bundle = read_bundle(bundle_txt)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
451
        repository = self.b1.repository
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
452
        for bundle_rev in bundle.real_revisions:
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
453
            # These really should have already been checked when we read the
454
            # bundle, since it computes the sha1 hash for the revision, which
455
            # only will match if everything is okay, but lets be explicit about
456
            # it
457
            branch_rev = repository.get_revision(bundle_rev.revision_id)
1185.82.33 by Aaron Bentley
Strengthen tests
458
            for a in ('inventory_sha1', 'revision_id', 'parent_ids',
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
459
                      'timestamp', 'timezone', 'message', 'committer',
1185.82.33 by Aaron Bentley
Strengthen tests
460
                      'parent_ids', 'properties'):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
461
                self.assertEqual(getattr(branch_rev, a),
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
462
                                 getattr(bundle_rev, a))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
463
            self.assertEqual(len(branch_rev.parent_ids),
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
464
                             len(bundle_rev.parent_ids))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
465
        self.assertEqual(rev_ids,
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
466
                         [r.revision_id for r in bundle.real_revisions])
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
467
        self.valid_apply_bundle(base_rev_id, bundle,
1185.82.89 by Aaron Bentley
Remove auto_commit stuff
468
                                   checkout_dir=checkout_dir)
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
469
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
470
        return bundle
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
471
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
472
    def get_invalid_bundle(self, base_rev_id, rev_id):
473
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
1185.82.118 by Aaron Bentley
Ensure that StrictTestament handles execute bit differences
474
        Munge the text so that it's invalid.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
475
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
476
        :return: The in-memory bundle
1185.82.118 by Aaron Bentley
Ensure that StrictTestament handles execute bit differences
477
        """
1793.3.1 by John Arbash Meinel
Clean up the bundle tests a little bit.
478
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
479
        new_text = bundle_txt.getvalue().replace('executable:no',
1185.82.118 by Aaron Bentley
Ensure that StrictTestament handles execute bit differences
480
                                               'executable:yes')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
481
        bundle_txt = BytesIO(new_text)
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
482
        bundle = read_bundle(bundle_txt)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
483
        self.valid_apply_bundle(base_rev_id, bundle)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
484
        return bundle
1185.82.118 by Aaron Bentley
Ensure that StrictTestament handles execute bit differences
485
1185.82.139 by Aaron Bentley
Raise NotABundle when a non-bundle is supplied
486
    def test_non_bundle(self):
3638.3.16 by Vincent Ladeuil
Remove XFAIL from test_unicode_bundle.
487
        self.assertRaises(errors.NotABundle,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
488
                          read_bundle, BytesIO(b'#!/bin/sh\n'))
1185.82.139 by Aaron Bentley
Raise NotABundle when a non-bundle is supplied
489
1793.2.7 by Aaron Bentley
Fix reporting of malformed, (especially, crlf) bundles
490
    def test_malformed(self):
3638.3.16 by Vincent Ladeuil
Remove XFAIL from test_unicode_bundle.
491
        self.assertRaises(errors.BadBundle, read_bundle,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
492
                          BytesIO(b'# Bazaar revision bundle v'))
1793.2.7 by Aaron Bentley
Fix reporting of malformed, (especially, crlf) bundles
493
494
    def test_crlf_bundle(self):
1793.2.9 by Aaron Bentley
Don't use assertNotRaises-- instead, catch BadBundle and pass
495
        try:
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
496
            read_bundle(BytesIO(b'# Bazaar revision bundle v0.8\r\n'))
3638.3.16 by Vincent Ladeuil
Remove XFAIL from test_unicode_bundle.
497
        except errors.BadBundle:
1793.2.9 by Aaron Bentley
Don't use assertNotRaises-- instead, catch BadBundle and pass
498
            # It is currently permitted for bundles with crlf line endings to
499
            # make read_bundle raise a BadBundle, but this should be fixed.
1793.2.10 by Aaron Bentley
Whitespace/comment fix
500
            # Anything else, especially NotABundle, is an error.
1793.2.9 by Aaron Bentley
Don't use assertNotRaises-- instead, catch BadBundle and pass
501
            pass
502
0.5.88 by John Arbash Meinel
Fixed a bug in the rename code, added more tests.
503
    def get_checkout(self, rev_id, checkout_dir=None):
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
504
        """Get a new tree, with the specified revision in it.
505
        """
506
0.5.88 by John Arbash Meinel
Fixed a bug in the rename code, added more tests.
507
        if checkout_dir is None:
3638.3.2 by Vincent Ladeuil
Fix all calls to tempfile.mkdtemp to osutils.mkdtemp.
508
            checkout_dir = osutils.mkdtemp(prefix='test-branch-', dir='.')
0.5.89 by John Arbash Meinel
Updating for explicitly defined directories.
509
        else:
510
            if not os.path.exists(checkout_dir):
511
                os.mkdir(checkout_dir)
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
512
        tree = self.make_branch_and_tree(checkout_dir)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
513
        s = BytesIO()
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
514
        ancestors = write_bundle(self.b1.repository, rev_id, 'null:', s,
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
515
                                 format=self.format)
1185.82.40 by Aaron Bentley
Started work on testing install_revisions/handling empty changesets
516
        s.seek(0)
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
517
        self.assertIsInstance(s.getvalue(), str)
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
518
        install_bundle(tree.branch.repository, read_bundle(s))
1185.82.41 by Aaron Bentley
More work on installing changesets
519
        for ancestor in ancestors:
520
            old = self.b1.repository.revision_tree(ancestor)
521
            new = tree.branch.repository.revision_tree(ancestor)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
522
            old.lock_read()
523
            new.lock_read()
524
            try:
525
                # Check that there aren't any inventory level changes
526
                delta = new.changes_from(old)
527
                self.assertFalse(delta.has_changed(),
528
                                 'Revision %s not copied correctly.'
529
                                 % (ancestor,))
530
531
                # Now check that the file contents are all correct
5837.2.2 by Jelmer Vernooij
Fix more uses of Tree.__iter__
532
                for inventory_id in old.all_file_ids():
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
533
                    try:
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
534
                        old_file = old.get_file(old.id2path(inventory_id))
3638.3.16 by Vincent Ladeuil
Remove XFAIL from test_unicode_bundle.
535
                    except errors.NoSuchFile:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
536
                        continue
537
                    if old_file is None:
538
                        continue
6809.4.21 by Jelmer Vernooij
Fix long lines.
539
                    self.assertEqual(
540
                            old_file.read(),
541
                            new.get_file(new.id2path(inventory_id)).read())
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
542
            finally:
543
                new.unlock()
544
                old.unlock()
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
545
        if not _mod_revision.is_null(rev_id):
6165.4.19 by Jelmer Vernooij
Avoid all iter_reverse_revision_history calls.
546
            tree.branch.generate_revision_history(rev_id)
1185.82.44 by Aaron Bentley
Switch to merge_changeset in test suite
547
            tree.update()
1852.10.3 by Robert Collins
Remove all uses of compare_trees and replace with Tree.changes_from throughout bzrlib.
548
            delta = tree.changes_from(self.b1.repository.revision_tree(rev_id))
1711.7.32 by John Arbash Meinel
Switch from a trailing space to a beginning space, which is supported everywhere.
549
            self.assertFalse(delta.has_changed(),
2255.10.5 by John Arbash Meinel
Fix a small bug when we have a symlink that does not need to be re-read.
550
                             'Working tree has modifications: %s' % delta)
1185.82.40 by Aaron Bentley
Started work on testing install_revisions/handling empty changesets
551
        return tree
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
552
1793.2.2 by Aaron Bentley
Move BundleReader into v07 serializer
553
    def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
554
        """Get the base revision, apply the changes, and make
555
        sure everything matches the builtin branch.
556
        """
1185.82.17 by Aaron Bentley
More API updates
557
        to_tree = self.get_checkout(base_rev_id, checkout_dir=checkout_dir)
3146.4.11 by Aaron Bentley
Fix lock errors in bundle tests
558
        to_tree.lock_write()
559
        try:
560
            self._valid_apply_bundle(base_rev_id, info, to_tree)
561
        finally:
562
            to_tree.unlock()
563
564
    def _valid_apply_bundle(self, base_rev_id, info, to_tree):
1908.6.4 by Robert Collins
Update to replaced parent checking api bzrlib/merge.py
565
        original_parents = to_tree.get_parent_ids()
1185.82.40 by Aaron Bentley
Started work on testing install_revisions/handling empty changesets
566
        repository = to_tree.branch.repository
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
567
        original_parents = to_tree.get_parent_ids()
1185.82.41 by Aaron Bentley
More work on installing changesets
568
        self.assertIs(repository.has_revision(base_rev_id), True)
1185.82.40 by Aaron Bentley
Started work on testing install_revisions/handling empty changesets
569
        for rev in info.real_revisions:
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
570
            self.assertTrue(not repository.has_revision(rev.revision_id),
571
                            'Revision {%s} present before applying bundle'
572
                            % rev.revision_id)
4241.14.13 by Vincent Ladeuil
Some more cleanup.
573
        merge_bundle(info, to_tree, True, merge.Merge3Merger, False, False)
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
574
575
        for rev in info.real_revisions:
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
576
            self.assertTrue(repository.has_revision(rev.revision_id),
577
                            'Missing revision {%s} after applying bundle'
578
                            % rev.revision_id)
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
579
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
580
        self.assertTrue(to_tree.branch.repository.has_revision(info.target))
0.5.117 by John Arbash Meinel
Almost there. Just need to track down a few remaining bugs.
581
        # Do we also want to verify that all the texts have been added?
0.5.86 by John Arbash Meinel
Updated the auto-commit functionality, and adding to pending-merges, more testing.
582
1908.6.4 by Robert Collins
Update to replaced parent checking api bzrlib/merge.py
583
        self.assertEqual(original_parents + [info.target],
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
584
                         to_tree.get_parent_ids())
0.5.86 by John Arbash Meinel
Updated the auto-commit functionality, and adding to pending-merges, more testing.
585
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
586
        rev = info.real_revisions[-1]
1185.82.17 by Aaron Bentley
More API updates
587
        base_tree = self.b1.repository.revision_tree(rev.revision_id)
588
        to_tree = to_tree.branch.repository.revision_tree(rev.revision_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
589
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
590
        # TODO: make sure the target tree is identical to base tree
0.5.86 by John Arbash Meinel
Updated the auto-commit functionality, and adding to pending-merges, more testing.
591
        #       we might also check the working tree.
592
593
        base_files = list(base_tree.list_files())
594
        to_files = list(to_tree.list_files())
595
        self.assertEqual(len(base_files), len(to_files))
1185.82.66 by Aaron Bentley
Handle new executable files
596
        for base_file, to_file in zip(base_files, to_files):
597
            self.assertEqual(base_file, to_file)
0.5.86 by John Arbash Meinel
Updated the auto-commit functionality, and adding to pending-merges, more testing.
598
0.5.117 by John Arbash Meinel
Almost there. Just need to track down a few remaining bugs.
599
        for path, status, kind, fileid, entry in base_files:
0.5.86 by John Arbash Meinel
Updated the auto-commit functionality, and adding to pending-merges, more testing.
600
            # Check that the meta information is the same
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
601
            self.assertEqual(base_tree.get_file_size(path, fileid),
602
                    to_tree.get_file_size(to_tree.id2path(fileid)))
603
            self.assertEqual(base_tree.get_file_sha1(path, fileid),
604
                    to_tree.get_file_sha1(to_tree.id2path(fileid)))
0.5.86 by John Arbash Meinel
Updated the auto-commit functionality, and adding to pending-merges, more testing.
605
            # Check that the contents are the same
606
            # This is pretty expensive
607
            # self.assertEqual(base_tree.get_file(fileid).read(),
608
            #         to_tree.get_file(fileid).read())
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
609
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
610
    def test_bundle(self):
1711.7.34 by John Arbash Meinel
Include a test to ensure bundles handle trailing whitespace.
611
        self.tree1 = self.make_branch_and_tree('b1')
1185.82.14 by Aaron Bentley
API updates
612
        self.b1 = self.tree1.branch
0.5.78 by John Arbash Meinel
Working on test cases, starting with the empty project issues.
613
4543.2.11 by John Arbash Meinel
Use a fixed file-id for the root to make the tests more stable.
614
        self.build_tree_contents([('b1/one', 'one\n')])
615
        self.tree1.add('one', 'one-id')
616
        self.tree1.set_root_id('root-id')
1185.82.14 by Aaron Bentley
API updates
617
        self.tree1.commit('add one', rev_id='a@cset-0-1')
0.5.78 by John Arbash Meinel
Working on test cases, starting with the empty project issues.
618
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
619
        bundle = self.get_valid_bundle('null:', 'a@cset-0-1')
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
620
621
        # Make sure we can handle files with spaces, tabs, other
622
        # bogus characters
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
623
        self.build_tree([
0.5.84 by John Arbash Meinel
(broken) problem with removes.
624
                'b1/with space.txt'
625
                , 'b1/dir/'
626
                , 'b1/dir/filein subdir.c'
627
                , 'b1/dir/WithCaps.txt'
1711.7.32 by John Arbash Meinel
Switch from a trailing space to a beginning space, which is supported everywhere.
628
                , 'b1/dir/ pre space'
0.5.84 by John Arbash Meinel
(broken) problem with removes.
629
                , 'b1/sub/'
630
                , 'b1/sub/sub/'
631
                , 'b1/sub/sub/nonempty.txt'
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
632
                ])
4543.2.11 by John Arbash Meinel
Use a fixed file-id for the root to make the tests more stable.
633
        self.build_tree_contents([('b1/sub/sub/emptyfile.txt', ''),
634
                                  ('b1/dir/nolastnewline.txt', 'bloop')])
1185.82.66 by Aaron Bentley
Handle new executable files
635
        tt = TreeTransform(self.tree1)
636
        tt.new_file('executable', tt.root, '#!/bin/sh\n', 'exe-1', True)
637
        tt.apply()
2520.4.84 by Aaron Bentley
Fix heisenbug record-rewriting test
638
        # have to fix length of file-id so that we can predictably rewrite
639
        # a (length-prefixed) record containing it later.
640
        self.tree1.add('with space.txt', 'withspace-id')
1185.82.14 by Aaron Bentley
API updates
641
        self.tree1.add([
2520.4.84 by Aaron Bentley
Fix heisenbug record-rewriting test
642
                  'dir'
0.5.84 by John Arbash Meinel
(broken) problem with removes.
643
                , 'dir/filein subdir.c'
644
                , 'dir/WithCaps.txt'
1711.7.32 by John Arbash Meinel
Switch from a trailing space to a beginning space, which is supported everywhere.
645
                , 'dir/ pre space'
0.5.94 by Aaron Bentley
Switched to native patch application, added tests for terminating newlines
646
                , 'dir/nolastnewline.txt'
0.5.84 by John Arbash Meinel
(broken) problem with removes.
647
                , 'sub'
648
                , 'sub/sub'
649
                , 'sub/sub/nonempty.txt'
650
                , 'sub/sub/emptyfile.txt'
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
651
                ])
1185.82.14 by Aaron Bentley
API updates
652
        self.tree1.commit('add whitespace', rev_id='a@cset-0-2')
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
653
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
654
        bundle = self.get_valid_bundle('a@cset-0-1', 'a@cset-0-2')
0.5.117 by John Arbash Meinel
Almost there. Just need to track down a few remaining bugs.
655
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
656
        # Check a rollup bundle
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
657
        bundle = self.get_valid_bundle('null:', 'a@cset-0-2')
0.5.84 by John Arbash Meinel
(broken) problem with removes.
658
659
        # Now delete entries
1185.82.21 by Aaron Bentley
Stop using deprecated function
660
        self.tree1.remove(
0.5.118 by John Arbash Meinel
Got most of test_changeset to work. Still needs work for Aaron's test code.
661
                ['sub/sub/nonempty.txt'
0.5.84 by John Arbash Meinel
(broken) problem with removes.
662
                , 'sub/sub/emptyfile.txt'
0.5.118 by John Arbash Meinel
Got most of test_changeset to work. Still needs work for Aaron's test code.
663
                , 'sub/sub'
664
                ])
1185.82.68 by Aaron Bentley
Handle execute bit on modified files
665
        tt = TreeTransform(self.tree1)
666
        trans_id = tt.trans_id_tree_file_id('exe-1')
667
        tt.set_executability(False, trans_id)
668
        tt.apply()
1185.82.19 by Aaron Bentley
More API updates
669
        self.tree1.commit('removed', rev_id='a@cset-0-3')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
670
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
671
        bundle = self.get_valid_bundle('a@cset-0-2', 'a@cset-0-3')
3638.3.16 by Vincent Ladeuil
Remove XFAIL from test_unicode_bundle.
672
        self.assertRaises((errors.TestamentMismatch,
4543.2.8 by John Arbash Meinel
Add a custom get_invalid_bundle and allow BadBundle to be
673
            errors.VersionedFileInvalidChecksum,
674
            errors.BadBundle), self.get_invalid_bundle,
2520.4.71 by Aaron Bentley
Update test to accept VersionedFileInvalidChecksum instead of TestamentMismatch
675
            'a@cset-0-2', 'a@cset-0-3')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
676
        # Check a rollup bundle
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
677
        bundle = self.get_valid_bundle('null:', 'a@cset-0-3')
0.5.84 by John Arbash Meinel
(broken) problem with removes.
678
679
        # Now move the directory
1185.82.19 by Aaron Bentley
More API updates
680
        self.tree1.rename_one('dir', 'sub/dir')
681
        self.tree1.commit('rename dir', rev_id='a@cset-0-4')
0.5.84 by John Arbash Meinel
(broken) problem with removes.
682
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
683
        bundle = self.get_valid_bundle('a@cset-0-3', 'a@cset-0-4')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
684
        # Check a rollup bundle
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
685
        bundle = self.get_valid_bundle('null:', 'a@cset-0-4')
0.5.84 by John Arbash Meinel
(broken) problem with removes.
686
0.5.87 by John Arbash Meinel
Handling international characters, added more test cases.
687
        # Modified files
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
688
        with open('b1/sub/dir/WithCaps.txt', 'ab') as f: f.write('\nAdding some text\n')
689
        with open('b1/sub/dir/ pre space', 'ab') as f: f.write(
2520.4.83 by Aaron Bentley
Clean up tests
690
             '\r\nAdding some\r\nDOS format lines\r\n')
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
691
        with open('b1/sub/dir/nolastnewline.txt', 'ab') as f: f.write('\n')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
692
        self.tree1.rename_one('sub/dir/ pre space',
1711.7.32 by John Arbash Meinel
Switch from a trailing space to a beginning space, which is supported everywhere.
693
                              'sub/ start space')
1185.82.19 by Aaron Bentley
More API updates
694
        self.tree1.commit('Modified files', rev_id='a@cset-0-5')
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
695
        bundle = self.get_valid_bundle('a@cset-0-4', 'a@cset-0-5')
0.5.87 by John Arbash Meinel
Handling international characters, added more test cases.
696
1185.82.70 by Aaron Bentley
Handle renamed files better
697
        self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
698
        self.tree1.rename_one('with space.txt', 'WithCaps.txt')
699
        self.tree1.rename_one('temp', 'with space.txt')
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
700
        self.tree1.commit(u'swap filenames', rev_id='a@cset-0-6',
701
                          verbose=False)
702
        bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
703
        other = self.get_checkout('a@cset-0-5')
4543.2.9 by John Arbash Meinel
Down to 2 failing tests.
704
        tree1_inv = get_inventory_text(self.tree1.branch.repository,
705
                                       'a@cset-0-5')
706
        tree2_inv = get_inventory_text(other.branch.repository,
707
                                       'a@cset-0-5')
1910.2.54 by Aaron Bentley
Implement testament format 3 strict
708
        self.assertEqualDiff(tree1_inv, tree2_inv)
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
709
        other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
710
        other.commit('rename file', rev_id='a@cset-0-6b')
1551.15.72 by Aaron Bentley
remove builtins._merge_helper
711
        self.tree1.merge_from_branch(other.branch)
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
712
        self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
1185.82.70 by Aaron Bentley
Handle renamed files better
713
                          verbose=False)
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
714
        bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
1185.82.72 by Aaron Bentley
Always use leftmost base for changesets
715
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
716
    def _test_symlink_bundle(self, link_name, link_target, new_link_target):
717
        link_id = 'link-1'
718
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
719
        self.requireFeature(features.SymlinkFeature)
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
720
        self.tree1 = self.make_branch_and_tree('b1')
1185.82.87 by Aaron Bentley
Got symlink adding working
721
        self.b1 = self.tree1.branch
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
722
1185.82.87 by Aaron Bentley
Got symlink adding working
723
        tt = TreeTransform(self.tree1)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
724
        tt.new_symlink(link_name, tt.root, link_target, link_id)
1185.82.87 by Aaron Bentley
Got symlink adding working
725
        tt.apply()
726
        self.tree1.commit('add symlink', rev_id='l@cset-0-1')
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
727
        bundle = self.get_valid_bundle('null:', 'l@cset-0-1')
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
728
        if getattr(bundle, 'revision_tree', None) is not None:
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
729
            # Not all bundle formats supports revision_tree
730
            bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-1')
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
731
            self.assertEqual(link_target, bund_tree.get_symlink_target(link_name))
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
732
1185.82.88 by Aaron Bentley
Get symlink modification, renames and deletion under test
733
        tt = TreeTransform(self.tree1)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
734
        trans_id = tt.trans_id_tree_file_id(link_id)
1185.82.88 by Aaron Bentley
Get symlink modification, renames and deletion under test
735
        tt.adjust_path('link2', tt.root, trans_id)
736
        tt.delete_contents(trans_id)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
737
        tt.create_symlink(new_link_target, trans_id)
1185.82.88 by Aaron Bentley
Get symlink modification, renames and deletion under test
738
        tt.apply()
739
        self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
740
        bundle = self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
741
        if getattr(bundle, 'revision_tree', None) is not None:
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
742
            # Not all bundle formats supports revision_tree
743
            bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-2')
744
            self.assertEqual(new_link_target,
6809.4.7 by Jelmer Vernooij
Swap arguments for get_symlink_target and kind/stored_kind.
745
                             bund_tree.get_symlink_target('link2'))
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
746
1185.82.88 by Aaron Bentley
Get symlink modification, renames and deletion under test
747
        tt = TreeTransform(self.tree1)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
748
        trans_id = tt.trans_id_tree_file_id(link_id)
1185.82.88 by Aaron Bentley
Get symlink modification, renames and deletion under test
749
        tt.delete_contents(trans_id)
750
        tt.create_symlink('jupiter', trans_id)
751
        tt.apply()
752
        self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
753
        bundle = self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
754
1185.82.88 by Aaron Bentley
Get symlink modification, renames and deletion under test
755
        tt = TreeTransform(self.tree1)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
756
        trans_id = tt.trans_id_tree_file_id(link_id)
1185.82.88 by Aaron Bentley
Get symlink modification, renames and deletion under test
757
        tt.delete_contents(trans_id)
758
        tt.apply()
759
        self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
760
        bundle = self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
761
762
    def test_symlink_bundle(self):
763
        self._test_symlink_bundle('link', 'bar/foo', 'mars')
764
765
    def test_unicode_symlink_bundle(self):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
766
        self.requireFeature(features.UnicodeFilenameFeature)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
767
        self._test_symlink_bundle(u'\N{Euro Sign}link',
768
                                  u'bar/\N{Euro Sign}foo',
769
                                  u'mars\N{Euro Sign}')
1185.82.96 by Aaron Bentley
Got first binary test passing
770
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
771
    def test_binary_bundle(self):
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
772
        self.tree1 = self.make_branch_and_tree('b1')
1185.82.96 by Aaron Bentley
Got first binary test passing
773
        self.b1 = self.tree1.branch
774
        tt = TreeTransform(self.tree1)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
775
1848.1.1 by John Arbash Meinel
fix bug in bundle handling of binary files with just '\r' in them.
776
        # Add
777
        tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
2520.4.83 by Aaron Bentley
Clean up tests
778
        tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
779
            'binary-2')
1185.82.96 by Aaron Bentley
Got first binary test passing
780
        tt.apply()
781
        self.tree1.commit('add binary', rev_id='b@cset-0-1')
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
782
        self.get_valid_bundle('null:', 'b@cset-0-1')
1848.1.1 by John Arbash Meinel
fix bug in bundle handling of binary files with just '\r' in them.
783
784
        # Delete
1185.82.97 by Aaron Bentley
Got binary files working for adds, renames, modifications
785
        tt = TreeTransform(self.tree1)
786
        trans_id = tt.trans_id_tree_file_id('binary-1')
787
        tt.delete_contents(trans_id)
788
        tt.apply()
789
        self.tree1.commit('delete binary', rev_id='b@cset-0-2')
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
790
        self.get_valid_bundle('b@cset-0-1', 'b@cset-0-2')
1848.1.1 by John Arbash Meinel
fix bug in bundle handling of binary files with just '\r' in them.
791
792
        # Rename & modify
1185.82.97 by Aaron Bentley
Got binary files working for adds, renames, modifications
793
        tt = TreeTransform(self.tree1)
794
        trans_id = tt.trans_id_tree_file_id('binary-2')
795
        tt.adjust_path('file3', tt.root, trans_id)
796
        tt.delete_contents(trans_id)
1848.1.1 by John Arbash Meinel
fix bug in bundle handling of binary files with just '\r' in them.
797
        tt.create_file('file\rcontents\x00\n\x00', trans_id)
1185.82.97 by Aaron Bentley
Got binary files working for adds, renames, modifications
798
        tt.apply()
799
        self.tree1.commit('rename and modify binary', rev_id='b@cset-0-3')
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
800
        self.get_valid_bundle('b@cset-0-2', 'b@cset-0-3')
1848.1.1 by John Arbash Meinel
fix bug in bundle handling of binary files with just '\r' in them.
801
802
        # Modify
1185.82.97 by Aaron Bentley
Got binary files working for adds, renames, modifications
803
        tt = TreeTransform(self.tree1)
804
        trans_id = tt.trans_id_tree_file_id('binary-2')
805
        tt.delete_contents(trans_id)
1848.1.1 by John Arbash Meinel
fix bug in bundle handling of binary files with just '\r' in them.
806
        tt.create_file('\x00file\rcontents', trans_id)
1185.82.97 by Aaron Bentley
Got binary files working for adds, renames, modifications
807
        tt.apply()
808
        self.tree1.commit('just modify binary', rev_id='b@cset-0-4')
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
809
        self.get_valid_bundle('b@cset-0-3', 'b@cset-0-4')
1185.82.115 by Aaron Bentley
Add test for last-changed special cases
810
1848.1.1 by John Arbash Meinel
fix bug in bundle handling of binary files with just '\r' in them.
811
        # Rollup
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
812
        self.get_valid_bundle('null:', 'b@cset-0-4')
1848.1.1 by John Arbash Meinel
fix bug in bundle handling of binary files with just '\r' in them.
813
1185.82.115 by Aaron Bentley
Add test for last-changed special cases
814
    def test_last_modified(self):
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
815
        self.tree1 = self.make_branch_and_tree('b1')
1185.82.115 by Aaron Bentley
Add test for last-changed special cases
816
        self.b1 = self.tree1.branch
817
        tt = TreeTransform(self.tree1)
818
        tt.new_file('file', tt.root, 'file', 'file')
819
        tt.apply()
820
        self.tree1.commit('create file', rev_id='a@lmod-0-1')
821
822
        tt = TreeTransform(self.tree1)
823
        trans_id = tt.trans_id_tree_file_id('file')
824
        tt.delete_contents(trans_id)
825
        tt.create_file('file2', trans_id)
826
        tt.apply()
827
        self.tree1.commit('modify text', rev_id='a@lmod-0-2a')
828
829
        other = self.get_checkout('a@lmod-0-1')
830
        tt = TreeTransform(other)
831
        trans_id = tt.trans_id_tree_file_id('file')
832
        tt.delete_contents(trans_id)
833
        tt.create_file('file2', trans_id)
834
        tt.apply()
835
        other.commit('modify text in another tree', rev_id='a@lmod-0-2b')
1551.15.72 by Aaron Bentley
remove builtins._merge_helper
836
        self.tree1.merge_from_branch(other.branch)
1185.82.115 by Aaron Bentley
Add test for last-changed special cases
837
        self.tree1.commit(u'Merge', rev_id='a@lmod-0-3',
838
                          verbose=False)
839
        self.tree1.commit(u'Merge', rev_id='a@lmod-0-4')
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
840
        bundle = self.get_valid_bundle('a@lmod-0-2a', 'a@lmod-0-4')
1185.84.3 by Aaron Bentley
Hide diffs for old revisions in bundles
841
842
    def test_hide_history(self):
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
843
        self.tree1 = self.make_branch_and_tree('b1')
1185.84.3 by Aaron Bentley
Hide diffs for old revisions in bundles
844
        self.b1 = self.tree1.branch
845
6437.19.1 by Wouter van Heyst
fix pypy failures
846
        with open('b1/one', 'wb') as f: f.write('one\n')
1185.84.3 by Aaron Bentley
Hide diffs for old revisions in bundles
847
        self.tree1.add('one')
848
        self.tree1.commit('add file', rev_id='a@cset-0-1')
6437.19.1 by Wouter van Heyst
fix pypy failures
849
        with open('b1/one', 'wb') as f: f.write('two\n')
1185.84.3 by Aaron Bentley
Hide diffs for old revisions in bundles
850
        self.tree1.commit('modify', rev_id='a@cset-0-2')
6437.19.1 by Wouter van Heyst
fix pypy failures
851
        with open('b1/one', 'wb') as f: f.write('three\n')
1185.84.3 by Aaron Bentley
Hide diffs for old revisions in bundles
852
        self.tree1.commit('modify', rev_id='a@cset-0-3')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
853
        bundle_file = BytesIO()
1185.84.3 by Aaron Bentley
Hide diffs for old revisions in bundles
854
        rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
1910.2.51 by Aaron Bentley
Bundles now corrupt repositories
855
                               'a@cset-0-1', bundle_file, format=self.format)
2382.1.1 by Ian Clatworthy
Fixes #98510 - bundle selftest fails if email has 'two' embedded
856
        self.assertNotContainsRe(bundle_file.getvalue(), '\btwo\b')
2520.4.32 by Aaron Bentley
Fix test case
857
        self.assertContainsRe(self.get_raw(bundle_file), 'one')
858
        self.assertContainsRe(self.get_raw(bundle_file), 'three')
859
2520.4.75 by Aaron Bentley
Fix traceback on empty bundles.
860
    def test_bundle_same_basis(self):
861
        """Ensure using the basis as the target doesn't cause an error"""
862
        self.tree1 = self.make_branch_and_tree('b1')
863
        self.tree1.commit('add file', rev_id='a@cset-0-1')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
864
        bundle_file = BytesIO()
2520.4.75 by Aaron Bentley
Fix traceback on empty bundles.
865
        rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-1',
866
                               'a@cset-0-1', bundle_file)
867
2520.4.32 by Aaron Bentley
Fix test case
868
    @staticmethod
869
    def get_raw(bundle_file):
870
        return bundle_file.getvalue()
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
871
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
872
    def test_unicode_bundle(self):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
873
        self.requireFeature(features.UnicodeFilenameFeature)
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
874
        # Handle international characters
875
        os.mkdir('b1')
4241.14.13 by Vincent Ladeuil
Some more cleanup.
876
        f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
877
878
        self.tree1 = self.make_branch_and_tree('b1')
879
        self.b1 = self.tree1.branch
880
881
        f.write((u'A file\n'
882
            u'With international man of mystery\n'
883
            u'William Dod\xe9\n').encode('utf-8'))
884
        f.close()
885
3638.3.18 by Vincent Ladeuil
Fixed as per jam's review.
886
        self.tree1.add([u'with Dod\N{Euro Sign}'], ['withdod-id'])
2386.1.1 by John Arbash Meinel
Update test_unicode_bundle, since we know how it fails on Mac OSX
887
        self.tree1.commit(u'i18n commit from William Dod\xe9',
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
888
                          rev_id='i18n-1', committer=u'William Dod\xe9')
889
890
        # Add
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
891
        bundle = self.get_valid_bundle('null:', 'i18n-1')
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
892
893
        # Modified
3638.3.18 by Vincent Ladeuil
Fixed as per jam's review.
894
        f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
895
        f.write(u'Modified \xb5\n'.encode('utf8'))
896
        f.close()
897
        self.tree1.commit(u'modified', rev_id='i18n-2')
898
899
        bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
3638.3.16 by Vincent Ladeuil
Remove XFAIL from test_unicode_bundle.
900
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
901
        # Renamed
3638.3.18 by Vincent Ladeuil
Fixed as per jam's review.
902
        self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
903
        self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
904
                          committer=u'Erik B\xe5gfors')
905
906
        bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
907
908
        # Removed
3638.3.18 by Vincent Ladeuil
Fixed as per jam's review.
909
        self.tree1.remove([u'B\N{Euro Sign}gfors'])
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
910
        self.tree1.commit(u'removed', rev_id='i18n-4')
911
912
        bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
913
914
        # Rollup
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
915
        bundle = self.get_valid_bundle('null:', 'i18n-4')
1711.7.35 by John Arbash Meinel
Factor out i18n bundle tests, so we don't always skip.
916
917
1711.7.34 by John Arbash Meinel
Include a test to ensure bundles handle trailing whitespace.
918
    def test_whitespace_bundle(self):
919
        if sys.platform in ('win32', 'cygwin'):
4241.14.13 by Vincent Ladeuil
Some more cleanup.
920
            raise tests.TestSkipped('Windows doesn\'t support filenames'
921
                                    ' with tabs or trailing spaces')
1711.7.34 by John Arbash Meinel
Include a test to ensure bundles handle trailing whitespace.
922
        self.tree1 = self.make_branch_and_tree('b1')
923
        self.b1 = self.tree1.branch
924
925
        self.build_tree(['b1/trailing space '])
926
        self.tree1.add(['trailing space '])
927
        # TODO: jam 20060701 Check for handling files with '\t' characters
928
        #       once we actually support them
929
930
        # Added
931
        self.tree1.commit('funky whitespace', rev_id='white-1')
932
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
933
        bundle = self.get_valid_bundle('null:', 'white-1')
1711.7.34 by John Arbash Meinel
Include a test to ensure bundles handle trailing whitespace.
934
935
        # Modified
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
936
        with open('b1/trailing space ', 'ab') as f: f.write('add some text\n')
1711.7.34 by John Arbash Meinel
Include a test to ensure bundles handle trailing whitespace.
937
        self.tree1.commit('add text', rev_id='white-2')
938
939
        bundle = self.get_valid_bundle('white-1', 'white-2')
940
941
        # Renamed
942
        self.tree1.rename_one('trailing space ', ' start and end space ')
943
        self.tree1.commit('rename', rev_id='white-3')
944
945
        bundle = self.get_valid_bundle('white-2', 'white-3')
946
947
        # Removed
948
        self.tree1.remove([' start and end space '])
949
        self.tree1.commit('removed', rev_id='white-4')
950
951
        bundle = self.get_valid_bundle('white-3', 'white-4')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
952
1711.7.34 by John Arbash Meinel
Include a test to ensure bundles handle trailing whitespace.
953
        # Now test a complet roll-up
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
954
        bundle = self.get_valid_bundle('null:', 'white-4')
1711.7.34 by John Arbash Meinel
Include a test to ensure bundles handle trailing whitespace.
955
1793.3.9 by John Arbash Meinel
Add a test to timezone for non integer tz offsets
956
    def test_alt_timezone_bundle(self):
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
957
        self.tree1 = self.make_branch_and_memory_tree('b1')
1793.3.9 by John Arbash Meinel
Add a test to timezone for non integer tz offsets
958
        self.b1 = self.tree1.branch
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
959
        builder = treebuilder.TreeBuilder()
1793.3.9 by John Arbash Meinel
Add a test to timezone for non integer tz offsets
960
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
961
        self.tree1.lock_write()
962
        builder.start_tree(self.tree1)
963
        builder.build(['newfile'])
964
        builder.finish_tree()
1793.3.9 by John Arbash Meinel
Add a test to timezone for non integer tz offsets
965
966
        # Asia/Colombo offset = 5 hours 30 minutes
967
        self.tree1.commit('non-hour offset timezone', rev_id='tz-1',
968
                          timezone=19800, timestamp=1152544886.0)
969
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
970
        bundle = self.get_valid_bundle('null:', 'tz-1')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
971
1793.3.9 by John Arbash Meinel
Add a test to timezone for non integer tz offsets
972
        rev = bundle.revisions[0]
973
        self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
974
        self.assertEqual(19800, rev.timezone)
975
        self.assertEqual(1152544886.0, rev.timestamp)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
976
        self.tree1.unlock()
1793.3.9 by John Arbash Meinel
Add a test to timezone for non integer tz offsets
977
1910.2.1 by Aaron Bentley
Ensure root entry always has a revision
978
    def test_bundle_root_id(self):
979
        self.tree1 = self.make_branch_and_tree('b1')
980
        self.b1 = self.tree1.branch
981
        self.tree1.commit('message', rev_id='revid1')
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
982
        bundle = self.get_valid_bundle('null:', 'revid1')
2520.4.79 by Aaron Bentley
Fixed up not-really-relevant munging tests
983
        tree = self.get_bundle_tree(bundle, 'revid1')
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
984
        root_revision = tree.get_file_revision(u'', tree.get_root_id())
6405.2.6 by Jelmer Vernooij
Lots of test fixes.
985
        self.assertEqual('revid1', root_revision)
1910.2.1 by Aaron Bentley
Ensure root entry always has a revision
986
1551.14.9 by Aaron Bentley
rename get_target_revision to install_revisions
987
    def test_install_revisions(self):
1551.14.4 by Aaron Bentley
Change bundle reader and merge directive to both be 'mergeables'
988
        self.tree1 = self.make_branch_and_tree('b1')
989
        self.b1 = self.tree1.branch
990
        self.tree1.commit('message', rev_id='rev2a')
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
991
        bundle = self.get_valid_bundle('null:', 'rev2a')
1551.14.4 by Aaron Bentley
Change bundle reader and merge directive to both be 'mergeables'
992
        branch2 = self.make_branch('b2')
993
        self.assertFalse(branch2.repository.has_revision('rev2a'))
1551.14.9 by Aaron Bentley
rename get_target_revision to install_revisions
994
        target_revision = bundle.install_revisions(branch2.repository)
1551.14.4 by Aaron Bentley
Change bundle reader and merge directive to both be 'mergeables'
995
        self.assertTrue(branch2.repository.has_revision('rev2a'))
1551.14.9 by Aaron Bentley
rename get_target_revision to install_revisions
996
        self.assertEqual('rev2a', target_revision)
1551.14.4 by Aaron Bentley
Change bundle reader and merge directive to both be 'mergeables'
997
2447.1.2 by John Arbash Meinel
Add tests that we handle empty values whether they end in ': \n' or ':\n'.
998
    def test_bundle_empty_property(self):
999
        """Test serializing revision properties with an empty value."""
1000
        tree = self.make_branch_and_memory_tree('tree')
1001
        tree.lock_write()
1002
        self.addCleanup(tree.unlock)
1003
        tree.add([''], ['TREE_ROOT'])
1004
        tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1005
        self.b1 = tree.branch
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
1006
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
2520.4.33 by Aaron Bentley
remove test dependencies on serialization minutia
1007
        bundle = read_bundle(bundle_sio)
1008
        revision_info = bundle.revisions[0]
1009
        self.assertEqual('rev1', revision_info.revision_id)
1010
        rev = revision_info.as_revision()
1011
        self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1012
                         rev.properties)
1013
1014
    def test_bundle_sorted_properties(self):
1015
        """For stability the writer should write properties in sorted order."""
1016
        tree = self.make_branch_and_memory_tree('tree')
1017
        tree.lock_write()
1018
        self.addCleanup(tree.unlock)
1019
1020
        tree.add([''], ['TREE_ROOT'])
1021
        tree.commit('One', rev_id='rev1',
1022
                    revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
1023
        self.b1 = tree.branch
2520.4.132 by Aaron Bentley
Merge from bzr.dev
1024
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
2520.4.33 by Aaron Bentley
remove test dependencies on serialization minutia
1025
        bundle = read_bundle(bundle_sio)
1026
        revision_info = bundle.revisions[0]
1027
        self.assertEqual('rev1', revision_info.revision_id)
1028
        rev = revision_info.as_revision()
1029
        self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1030
                          'd':'1'}, rev.properties)
1031
1032
    def test_bundle_unicode_properties(self):
1033
        """We should be able to round trip a non-ascii property."""
1034
        tree = self.make_branch_and_memory_tree('tree')
1035
        tree.lock_write()
1036
        self.addCleanup(tree.unlock)
1037
1038
        tree.add([''], ['TREE_ROOT'])
1039
        # Revisions themselves do not require anything about revision property
1040
        # keys, other than that they are a basestring, and do not contain
1041
        # whitespace.
1042
        # However, Testaments assert than they are str(), and thus should not
1043
        # be Unicode.
1044
        tree.commit('One', rev_id='rev1',
1045
                    revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1046
        self.b1 = tree.branch
2520.4.132 by Aaron Bentley
Merge from bzr.dev
1047
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
2520.4.33 by Aaron Bentley
remove test dependencies on serialization minutia
1048
        bundle = read_bundle(bundle_sio)
1049
        revision_info = bundle.revisions[0]
1050
        self.assertEqual('rev1', revision_info.revision_id)
1051
        rev = revision_info.as_revision()
1052
        self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1053
                          'alpha':u'\u03b1'}, rev.properties)
1054
2520.4.43 by Aaron Bentley
Fix test suite
1055
    def test_bundle_with_ghosts(self):
1056
        tree = self.make_branch_and_tree('tree')
1057
        self.b1 = tree.branch
1058
        self.build_tree_contents([('tree/file', 'content1')])
1059
        tree.add(['file'])
1060
        tree.commit('rev1')
1061
        self.build_tree_contents([('tree/file', 'content2')])
1062
        tree.add_parent_tree_id('ghost')
1063
        tree.commit('rev2', rev_id='rev2')
2520.4.132 by Aaron Bentley
Merge from bzr.dev
1064
        bundle = self.get_valid_bundle('null:', 'rev2')
2520.4.43 by Aaron Bentley
Fix test suite
1065
2520.4.97 by Aaron Bentley
Hack in support for inventory conversion
1066
    def make_simple_tree(self, format=None):
1067
        tree = self.make_branch_and_tree('b1', format=format)
1068
        self.b1 = tree.branch
1069
        self.build_tree(['b1/file'])
1070
        tree.add('file')
1071
        return tree
1072
1073
    def test_across_serializers(self):
1074
        tree = self.make_simple_tree('knit')
1075
        tree.commit('hello', rev_id='rev1')
2520.4.98 by Aaron Bentley
Support inventory conversion with parents
1076
        tree.commit('hello', rev_id='rev2')
2520.4.132 by Aaron Bentley
Merge from bzr.dev
1077
        bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
2520.4.97 by Aaron Bentley
Hack in support for inventory conversion
1078
        repo = self.make_repository('repo', format='dirstate-with-subtree')
1079
        bundle.install_revisions(repo)
4988.5.1 by Jelmer Vernooij
Rename Repository.get_inventory_xml -> Repository._get_inventory_xml.
1080
        inv_text = repo._get_inventory_xml('rev2')
2520.4.97 by Aaron Bentley
Hack in support for inventory conversion
1081
        self.assertNotContainsRe(inv_text, 'format="5"')
2520.4.99 by Aaron Bentley
Test conversion across models
1082
        self.assertContainsRe(inv_text, 'format="7"')
1083
3380.1.8 by Aaron Bentley
Test that the stored inventory hash is correct when bundles are used
1084
    def make_repo_with_installed_revisions(self):
1085
        tree = self.make_simple_tree('knit')
1086
        tree.commit('hello', rev_id='rev1')
1087
        tree.commit('hello', rev_id='rev2')
1088
        bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1089
        repo = self.make_repository('repo', format='dirstate-with-subtree')
1090
        bundle.install_revisions(repo)
1091
        return repo
1092
2520.4.99 by Aaron Bentley
Test conversion across models
1093
    def test_across_models(self):
3380.1.8 by Aaron Bentley
Test that the stored inventory hash is correct when bundles are used
1094
        repo = self.make_repo_with_installed_revisions()
3380.1.12 by Aaron Bentley
fix test
1095
        inv = repo.get_inventory('rev2')
2520.4.99 by Aaron Bentley
Test conversion across models
1096
        self.assertEqual('rev2', inv.root.revision)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1097
        root_id = inv.root.file_id
1098
        repo.lock_read()
1099
        self.addCleanup(repo.unlock)
1100
        self.assertEqual({(root_id, 'rev1'):(),
1101
            (root_id, 'rev2'):((root_id, 'rev1'),)},
1102
            repo.texts.get_parent_map([(root_id, 'rev1'), (root_id, 'rev2')]))
2520.4.99 by Aaron Bentley
Test conversion across models
1103
3380.1.8 by Aaron Bentley
Test that the stored inventory hash is correct when bundles are used
1104
    def test_inv_hash_across_serializers(self):
1105
        repo = self.make_repo_with_installed_revisions()
4988.3.1 by Jelmer Vernooij
Remove Repository.get_inventory_sha1().
1106
        recorded_inv_sha1 = repo.get_revision('rev2').inventory_sha1
4988.5.1 by Jelmer Vernooij
Rename Repository.get_inventory_xml -> Repository._get_inventory_xml.
1107
        xml = repo._get_inventory_xml('rev2')
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1108
        self.assertEqual(osutils.sha_string(xml), recorded_inv_sha1)
3380.1.8 by Aaron Bentley
Test that the stored inventory hash is correct when bundles are used
1109
2520.4.99 by Aaron Bentley
Test conversion across models
1110
    def test_across_models_incompatible(self):
1111
        tree = self.make_simple_tree('dirstate-with-subtree')
1112
        tree.commit('hello', rev_id='rev1')
1113
        tree.commit('hello', rev_id='rev2')
1114
        try:
2520.4.132 by Aaron Bentley
Merge from bzr.dev
1115
            bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
2520.4.99 by Aaron Bentley
Test conversion across models
1116
        except errors.IncompatibleBundleFormat:
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1117
            raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
2520.4.99 by Aaron Bentley
Test conversion across models
1118
        repo = self.make_repository('repo', format='knit')
1119
        bundle.install_revisions(repo)
1120
2520.4.132 by Aaron Bentley
Merge from bzr.dev
1121
        bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
2520.4.99 by Aaron Bentley
Test conversion across models
1122
        self.assertRaises(errors.IncompatibleRevision,
1123
                          bundle.install_revisions, repo)
2520.4.97 by Aaron Bentley
Hack in support for inventory conversion
1124
2520.4.109 by Aaron Bentley
start work on directive cherry-picking
1125
    def test_get_merge_request(self):
1126
        tree = self.make_simple_tree()
1127
        tree.commit('hello', rev_id='rev1')
1128
        tree.commit('hello', rev_id='rev2')
2520.4.132 by Aaron Bentley
Merge from bzr.dev
1129
        bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
2520.4.109 by Aaron Bentley
start work on directive cherry-picking
1130
        result = bundle.get_merge_request(tree.branch.repository)
1131
        self.assertEqual((None, 'rev1', 'inapplicable'), result)
1132
2520.5.1 by Aaron Bentley
Test installing revisions with subtrees
1133
    def test_with_subtree(self):
1134
        tree = self.make_branch_and_tree('tree',
1135
                                         format='dirstate-with-subtree')
1136
        self.b1 = tree.branch
1137
        subtree = self.make_branch_and_tree('tree/subtree',
1138
                                            format='dirstate-with-subtree')
1139
        tree.add('subtree')
1140
        tree.commit('hello', rev_id='rev1')
1141
        try:
2520.4.132 by Aaron Bentley
Merge from bzr.dev
1142
            bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
2520.5.1 by Aaron Bentley
Test installing revisions with subtrees
1143
        except errors.IncompatibleBundleFormat:
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1144
            raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
2520.5.1 by Aaron Bentley
Test installing revisions with subtrees
1145
        if isinstance(bundle, v09.BundleInfo09):
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1146
            raise tests.TestSkipped("Format 0.9 doesn't work with subtrees")
2520.5.1 by Aaron Bentley
Test installing revisions with subtrees
1147
        repo = self.make_repository('repo', format='knit')
1148
        self.assertRaises(errors.IncompatibleRevision,
1149
                          bundle.install_revisions, repo)
1150
        repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1151
        bundle.install_revisions(repo2)
1152
2520.4.127 by Aaron Bentley
Fix up name encoding to handle revision-ids with slashes
1153
    def test_revision_id_with_slash(self):
1154
        self.tree1 = self.make_branch_and_tree('tree')
1155
        self.b1 = self.tree1.branch
1156
        try:
1157
            self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
1158
        except ValueError:
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1159
            raise tests.TestSkipped(
1160
                "Repository doesn't support revision ids with slashes")
2520.4.132 by Aaron Bentley
Merge from bzr.dev
1161
        bundle = self.get_valid_bundle('null:', 'rev/id')
2520.4.127 by Aaron Bentley
Fix up name encoding to handle revision-ids with slashes
1162
2520.6.2 by Aaron Bentley
Fix bundle installation wrong-versionedfile bug
1163
    def test_skip_file(self):
1164
        """Make sure we don't accidentally write to the wrong versionedfile"""
1165
        self.tree1 = self.make_branch_and_tree('tree')
1166
        self.b1 = self.tree1.branch
1167
        # rev1 is not present in bundle, done by fetch
1168
        self.build_tree_contents([('tree/file2', 'contents1')])
1169
        self.tree1.add('file2', 'file2-id')
1170
        self.tree1.commit('rev1', rev_id='reva')
1171
        self.build_tree_contents([('tree/file3', 'contents2')])
1172
        # rev2 is present in bundle, and done by fetch
1173
        # having file1 in the bunle causes file1's versionedfile to be opened.
1174
        self.tree1.add('file3', 'file3-id')
1175
        self.tree1.commit('rev2')
1176
        # Updating file2 should not cause an attempt to add to file1's vf
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
1177
        target = self.tree1.controldir.sprout('target').open_workingtree()
2520.6.2 by Aaron Bentley
Fix bundle installation wrong-versionedfile bug
1178
        self.build_tree_contents([('tree/file2', 'contents3')])
1179
        self.tree1.commit('rev3', rev_id='rev3')
1180
        bundle = self.get_valid_bundle('reva', 'rev3')
2520.6.5 by Aaron Bentley
Skip for bundle formats that don't provide get_bundle_reader
1181
        if getattr(bundle, 'get_bundle_reader', None) is None:
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1182
            raise tests.TestSkipped('Bundle format cannot provide reader')
2520.6.2 by Aaron Bentley
Fix bundle installation wrong-versionedfile bug
1183
        # be sure that file1 comes before file2
1184
        for b, m, k, r, f in bundle.get_bundle_reader().iter_records():
1185
            if f == 'file3-id':
1186
                break
1187
            self.assertNotEqual(f, 'file2-id')
1188
        bundle.install_revisions(target.branch.repository)
1189
2520.4.43 by Aaron Bentley
Fix test suite
1190
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1191
class V08BundleTester(BundleTester, tests.TestCaseWithTransport):
2520.4.33 by Aaron Bentley
remove test dependencies on serialization minutia
1192
1193
    format = '0.8'
1194
1195
    def test_bundle_empty_property(self):
1196
        """Test serializing revision properties with an empty value."""
1197
        tree = self.make_branch_and_memory_tree('tree')
1198
        tree.lock_write()
1199
        self.addCleanup(tree.unlock)
1200
        tree.add([''], ['TREE_ROOT'])
1201
        tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1202
        self.b1 = tree.branch
2520.4.132 by Aaron Bentley
Merge from bzr.dev
1203
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
2447.1.2 by John Arbash Meinel
Add tests that we handle empty values whether they end in ': \n' or ':\n'.
1204
        self.assertContainsRe(bundle_sio.getvalue(),
1205
                              '# properties:\n'
1206
                              '#   branch-nick: tree\n'
2447.1.3 by John Arbash Meinel
Change the default serializer to include a trailing whitespace for empty properties.
1207
                              '#   empty: \n'
2447.1.2 by John Arbash Meinel
Add tests that we handle empty values whether they end in ': \n' or ':\n'.
1208
                              '#   one: two\n'
1209
                             )
1210
        bundle = read_bundle(bundle_sio)
1211
        revision_info = bundle.revisions[0]
1212
        self.assertEqual('rev1', revision_info.revision_id)
1213
        rev = revision_info.as_revision()
1214
        self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1215
                         rev.properties)
1216
2520.4.79 by Aaron Bentley
Fixed up not-really-relevant munging tests
1217
    def get_bundle_tree(self, bundle, revision_id):
1218
        repository = self.make_repository('repo')
1219
        return bundle.revision_tree(repository, 'revid1')
1220
2447.1.2 by John Arbash Meinel
Add tests that we handle empty values whether they end in ': \n' or ':\n'.
1221
    def test_bundle_empty_property_alt(self):
2447.1.3 by John Arbash Meinel
Change the default serializer to include a trailing whitespace for empty properties.
1222
        """Test serializing revision properties with an empty value.
1223
1224
        Older readers had a bug when reading an empty property.
1225
        They assumed that all keys ended in ': \n'. However they would write an
1226
        empty value as ':\n'. This tests make sure that all newer bzr versions
1227
        can handle th second form.
1228
        """
2447.1.2 by John Arbash Meinel
Add tests that we handle empty values whether they end in ': \n' or ':\n'.
1229
        tree = self.make_branch_and_memory_tree('tree')
1230
        tree.lock_write()
1231
        self.addCleanup(tree.unlock)
1232
        tree.add([''], ['TREE_ROOT'])
1233
        tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1234
        self.b1 = tree.branch
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
1235
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
2447.1.2 by John Arbash Meinel
Add tests that we handle empty values whether they end in ': \n' or ':\n'.
1236
        txt = bundle_sio.getvalue()
2447.1.3 by John Arbash Meinel
Change the default serializer to include a trailing whitespace for empty properties.
1237
        loc = txt.find('#   empty: ') + len('#   empty:')
1238
        # Create a new bundle, which strips the trailing space after empty
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1239
        bundle_sio = BytesIO(txt[:loc] + txt[loc+1:])
2447.1.2 by John Arbash Meinel
Add tests that we handle empty values whether they end in ': \n' or ':\n'.
1240
1241
        self.assertContainsRe(bundle_sio.getvalue(),
1242
                              '# properties:\n'
1243
                              '#   branch-nick: tree\n'
2447.1.3 by John Arbash Meinel
Change the default serializer to include a trailing whitespace for empty properties.
1244
                              '#   empty:\n'
2447.1.2 by John Arbash Meinel
Add tests that we handle empty values whether they end in ': \n' or ':\n'.
1245
                              '#   one: two\n'
1246
                             )
1247
        bundle = read_bundle(bundle_sio)
1248
        revision_info = bundle.revisions[0]
1249
        self.assertEqual('rev1', revision_info.revision_id)
1250
        rev = revision_info.as_revision()
1251
        self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1252
                         rev.properties)
1253
2447.1.1 by John Arbash Meinel
For stability and ease of testing, write properties in sorted order.
1254
    def test_bundle_sorted_properties(self):
1255
        """For stability the writer should write properties in sorted order."""
1256
        tree = self.make_branch_and_memory_tree('tree')
1257
        tree.lock_write()
1258
        self.addCleanup(tree.unlock)
1259
1260
        tree.add([''], ['TREE_ROOT'])
1261
        tree.commit('One', rev_id='rev1',
1262
                    revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
1263
        self.b1 = tree.branch
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
1264
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
2447.1.1 by John Arbash Meinel
For stability and ease of testing, write properties in sorted order.
1265
        self.assertContainsRe(bundle_sio.getvalue(),
1266
                              '# properties:\n'
1267
                              '#   a: 4\n'
1268
                              '#   b: 3\n'
1269
                              '#   branch-nick: tree\n'
1270
                              '#   c: 2\n'
1271
                              '#   d: 1\n'
1272
                             )
2447.1.4 by John Arbash Meinel
Add a test that we properly round-trip unicode properties.
1273
        bundle = read_bundle(bundle_sio)
1274
        revision_info = bundle.revisions[0]
1275
        self.assertEqual('rev1', revision_info.revision_id)
1276
        rev = revision_info.as_revision()
1277
        self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1278
                          'd':'1'}, rev.properties)
1279
1280
    def test_bundle_unicode_properties(self):
1281
        """We should be able to round trip a non-ascii property."""
1282
        tree = self.make_branch_and_memory_tree('tree')
1283
        tree.lock_write()
1284
        self.addCleanup(tree.unlock)
1285
1286
        tree.add([''], ['TREE_ROOT'])
1287
        # Revisions themselves do not require anything about revision property
1288
        # keys, other than that they are a basestring, and do not contain
1289
        # whitespace.
1290
        # However, Testaments assert than they are str(), and thus should not
1291
        # be Unicode.
1292
        tree.commit('One', rev_id='rev1',
1293
                    revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1294
        self.b1 = tree.branch
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
1295
        bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
2447.1.4 by John Arbash Meinel
Add a test that we properly round-trip unicode properties.
1296
        self.assertContainsRe(bundle_sio.getvalue(),
1297
                              '# properties:\n'
1298
                              '#   alpha: \xce\xb1\n'
1299
                              '#   branch-nick: tree\n'
1300
                              '#   omega: \xce\xa9\n'
1301
                             )
1302
        bundle = read_bundle(bundle_sio)
1303
        revision_info = bundle.revisions[0]
1304
        self.assertEqual('rev1', revision_info.revision_id)
1305
        rev = revision_info.as_revision()
1306
        self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1307
                          'alpha':u'\u03b1'}, rev.properties)
2447.1.1 by John Arbash Meinel
For stability and ease of testing, write properties in sorted order.
1308
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1309
1910.2.59 by Aaron Bentley
Test 0.9 bundles for knit format1 and knit format2
1310
class V09BundleKnit2Tester(V08BundleTester):
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
1311
1312
    format = '0.9'
1313
1314
    def bzrdir_format(self):
1315
        format = bzrdir.BzrDirMetaFormat1()
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
1316
        format.repository_format = knitrepo.RepositoryFormatKnit3()
1910.2.50 by Aaron Bentley
start work on format 0.9 serializer
1317
        return format
1318
1319
1910.2.59 by Aaron Bentley
Test 0.9 bundles for knit format1 and knit format2
1320
class V09BundleKnit1Tester(V08BundleTester):
1321
1322
    format = '0.9'
1323
1324
    def bzrdir_format(self):
1325
        format = bzrdir.BzrDirMetaFormat1()
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
1326
        format.repository_format = knitrepo.RepositoryFormatKnit1()
1910.2.59 by Aaron Bentley
Test 0.9 bundles for knit format1 and knit format2
1327
        return format
1328
1329
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1330
class V4BundleTester(BundleTester, tests.TestCaseWithTransport):
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1331
2520.4.136 by Aaron Bentley
Fix format strings
1332
    format = '4'
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1333
1334
    def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1335
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
1336
        Make sure that the text generated is valid, and that it
1337
        can be applied against the base, and generate the same information.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1338
1339
        :return: The in-memory bundle
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1340
        """
1341
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1342
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1343
        # This should also validate the generated bundle
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1344
        bundle = read_bundle(bundle_txt)
1345
        repository = self.b1.repository
1346
        for bundle_rev in bundle.real_revisions:
1347
            # These really should have already been checked when we read the
1348
            # bundle, since it computes the sha1 hash for the revision, which
1349
            # only will match if everything is okay, but lets be explicit about
1350
            # it
1351
            branch_rev = repository.get_revision(bundle_rev.revision_id)
1352
            for a in ('inventory_sha1', 'revision_id', 'parent_ids',
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1353
                      'timestamp', 'timezone', 'message', 'committer',
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1354
                      'parent_ids', 'properties'):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1355
                self.assertEqual(getattr(branch_rev, a),
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1356
                                 getattr(bundle_rev, a))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1357
            self.assertEqual(len(branch_rev.parent_ids),
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1358
                             len(bundle_rev.parent_ids))
2520.4.29 by Aaron Bentley
Reactivate some testing, fix topo_iter
1359
        self.assertEqual(set(rev_ids),
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1360
                         {r.revision_id for r in bundle.real_revisions})
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1361
        self.valid_apply_bundle(base_rev_id, bundle,
1362
                                   checkout_dir=checkout_dir)
1363
1364
        return bundle
1365
2520.4.34 by Aaron Bentley
Add signature support
1366
    def get_invalid_bundle(self, base_rev_id, rev_id):
1367
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
1368
        Munge the text so that it's invalid.
1369
1370
        :return: The in-memory bundle
1371
        """
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
1372
        from ..bundle import serializer
2520.4.34 by Aaron Bentley
Add signature support
1373
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1374
        new_text = self.get_raw(BytesIO(b''.join(bundle_txt)))
2520.4.34 by Aaron Bentley
Add signature support
1375
        new_text = new_text.replace('<file file_id="exe-1"',
1376
                                    '<file executable="y" file_id="exe-1"')
4543.2.11 by John Arbash Meinel
Use a fixed file-id for the root to make the tests more stable.
1377
        new_text = new_text.replace('B260', 'B275')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1378
        bundle_txt = BytesIO()
2520.4.136 by Aaron Bentley
Fix format strings
1379
        bundle_txt.write(serializer._get_bundle_header('4'))
2520.4.34 by Aaron Bentley
Add signature support
1380
        bundle_txt.write('\n')
2520.4.76 by Aaron Bentley
Move base64-encoding into merge directives
1381
        bundle_txt.write(new_text.encode('bz2'))
2520.4.34 by Aaron Bentley
Add signature support
1382
        bundle_txt.seek(0)
1383
        bundle = read_bundle(bundle_txt)
1384
        self.valid_apply_bundle(base_rev_id, bundle)
1385
        return bundle
1386
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1387
    def create_bundle_text(self, base_rev_id, rev_id):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1388
        bundle_txt = BytesIO()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1389
        rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1390
                               bundle_txt, format=self.format)
1391
        bundle_txt.seek(0)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1392
        self.assertEqual(bundle_txt.readline(),
2520.4.14 by Aaron Bentley
Get most tests passing, use format header
1393
                         '# Bazaar revision bundle v%s\n' % self.format)
1394
        self.assertEqual(bundle_txt.readline(), '#\n')
1395
        rev = self.b1.repository.get_revision(rev_id)
1396
        bundle_txt.seek(0)
1397
        return bundle_txt, rev_ids
2520.4.4 by Aaron Bentley
Get basis support for a new bundle format in place
1398
2520.4.79 by Aaron Bentley
Fixed up not-really-relevant munging tests
1399
    def get_bundle_tree(self, bundle, revision_id):
1400
        repository = self.make_repository('repo')
1401
        bundle.install_revisions(repository)
1402
        return repository.revision_tree(revision_id)
1403
2520.4.4 by Aaron Bentley
Get basis support for a new bundle format in place
1404
    def test_creation(self):
1405
        tree = self.make_branch_and_tree('tree')
2520.4.8 by Aaron Bentley
Serialize inventory
1406
        self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
2520.4.6 by Aaron Bentley
Get installation started
1407
        tree.add('file', 'fileid-2')
2520.4.4 by Aaron Bentley
Get basis support for a new bundle format in place
1408
        tree.commit('added file', rev_id='rev1')
2520.4.8 by Aaron Bentley
Serialize inventory
1409
        self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
2520.4.10 by Aaron Bentley
Enable installation of revisions
1410
        tree.commit('changed file', rev_id='rev2')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1411
        s = BytesIO()
2520.4.72 by Aaron Bentley
Rename format to 4alpha
1412
        serializer = BundleSerializerV4('1.0')
2520.4.8 by Aaron Bentley
Serialize inventory
1413
        serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
2520.4.5 by Aaron Bentley
Start work on reading mpbundles
1414
        s.seek(0)
2520.4.6 by Aaron Bentley
Get installation started
1415
        tree2 = self.make_branch_and_tree('target')
1416
        target_repo = tree2.branch.repository
1417
        install_bundle(target_repo, serializer.read(s))
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1418
        target_repo.lock_read()
1419
        self.addCleanup(target_repo.unlock)
4202.1.1 by John Arbash Meinel
Update Repository.iter_files_bytes() to return an iterable of bytestrings.
1420
        # Turn the 'iterators_of_bytes' back into simple strings for comparison
1421
        repo_texts = dict((i, ''.join(content)) for i, content
1422
                          in target_repo.iter_files_bytes(
1423
                                [('fileid-2', 'rev1', '1'),
1424
                                 ('fileid-2', 'rev2', '2')]))
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1425
        self.assertEqual({'1':'contents1\nstatic\n',
4202.1.1 by John Arbash Meinel
Update Repository.iter_files_bytes() to return an iterable of bytestrings.
1426
                          '2':'contents2\nstatic\n'},
1427
                         repo_texts)
2520.4.8 by Aaron Bentley
Serialize inventory
1428
        rtree = target_repo.revision_tree('rev2')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1429
        inventory_vf = target_repo.inventories
1430
        # If the inventory store has a graph, it must match the revision graph.
1431
        self.assertSubset(
1432
            [inventory_vf.get_parent_map([('rev2',)])[('rev2',)]],
1433
            [None, (('rev1',),)])
2520.4.10 by Aaron Bentley
Enable installation of revisions
1434
        self.assertEqual('changed file',
1435
                         target_repo.get_revision('rev2').message)
2520.4.6 by Aaron Bentley
Get installation started
1436
2520.4.32 by Aaron Bentley
Fix test case
1437
    @staticmethod
1438
    def get_raw(bundle_file):
1439
        bundle_file.seek(0)
2520.4.70 by Aaron Bentley
Yank patch-handling functionality
1440
        line = bundle_file.readline()
1441
        line = bundle_file.readline()
2520.4.32 by Aaron Bentley
Fix test case
1442
        lines = bundle_file.readlines()
2520.4.76 by Aaron Bentley
Move base64-encoding into merge directives
1443
        return ''.join(lines).decode('bz2')
2520.4.32 by Aaron Bentley
Fix test case
1444
2520.4.34 by Aaron Bentley
Add signature support
1445
    def test_copy_signatures(self):
1446
        tree_a = self.make_branch_and_tree('tree_a')
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
1447
        import breezy.gpg
1448
        import breezy.commit as commit
1449
        oldstrategy = breezy.gpg.GPGStrategy
2520.4.34 by Aaron Bentley
Add signature support
1450
        branch = tree_a.branch
1451
        repo_a = branch.repository
1452
        tree_a.commit("base", allow_pointless=True, rev_id='A')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
1453
        self.assertFalse(branch.repository.has_signature_for_revision_id('A'))
2520.4.34 by Aaron Bentley
Add signature support
1454
        try:
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
1455
            from ..testament import Testament
2520.4.34 by Aaron Bentley
Add signature support
1456
            # monkey patch gpg signing mechanism
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
1457
            breezy.gpg.GPGStrategy = breezy.gpg.LoopbackGPGStrategy
6393.1.1 by Vincent Ladeuil
Provides MemoryStack to simplify configuration setup in tests
1458
            new_config = test_commit.MustSignConfig()
6351.3.3 by Jelmer Vernooij
Convert more stuff to use config stacks.
1459
            commit.Commit(config_stack=new_config).commit(message="base",
2520.4.34 by Aaron Bentley
Add signature support
1460
                                                    allow_pointless=True,
1461
                                                    rev_id='B',
1462
                                                    working_tree=tree_a)
1463
            def sign(text):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
1464
                return breezy.gpg.LoopbackGPGStrategy(None).sign(text)
2520.4.34 by Aaron Bentley
Add signature support
1465
            self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1466
        finally:
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
1467
            breezy.gpg.GPGStrategy = oldstrategy
2520.4.34 by Aaron Bentley
Add signature support
1468
        tree_b = self.make_branch_and_tree('tree_b')
1469
        repo_b = tree_b.branch.repository
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1470
        s = BytesIO()
2520.4.136 by Aaron Bentley
Fix format strings
1471
        serializer = BundleSerializerV4('4')
2520.4.34 by Aaron Bentley
Add signature support
1472
        serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1473
        s.seek(0)
1474
        install_bundle(repo_b, serializer.read(s))
1475
        self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1476
        self.assertEqual(repo_b.get_signature_text('B'),
1477
                         repo_a.get_signature_text('B'))
2520.4.100 by Aaron Bentley
Fix repeat signature installs
1478
        s.seek(0)
1479
        # ensure repeat installs are harmless
1480
        install_bundle(repo_b, serializer.read(s))
2520.4.34 by Aaron Bentley
Add signature support
1481
2520.4.4 by Aaron Bentley
Get basis support for a new bundle format in place
1482
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1483
class V4_2aBundleTester(V4BundleTester):
1484
1485
    def bzrdir_format(self):
1486
        return '2a'
1487
4543.2.8 by John Arbash Meinel
Add a custom get_invalid_bundle and allow BadBundle to be
1488
    def get_invalid_bundle(self, base_rev_id, rev_id):
1489
        """Create a bundle from base_rev_id -> rev_id in built-in branch.
1490
        Munge the text so that it's invalid.
1491
1492
        :return: The in-memory bundle
1493
        """
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
1494
        from ..bundle import serializer
4543.2.8 by John Arbash Meinel
Add a custom get_invalid_bundle and allow BadBundle to be
1495
        bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1496
        new_text = self.get_raw(BytesIO(b''.join(bundle_txt)))
4543.2.9 by John Arbash Meinel
Down to 2 failing tests.
1497
        # We are going to be replacing some text to set the executable bit on a
1498
        # file. Make sure the text replacement actually works correctly.
4543.2.11 by John Arbash Meinel
Use a fixed file-id for the root to make the tests more stable.
1499
        self.assertContainsRe(new_text, '(?m)B244\n\ni 1\n<inventory')
4543.2.8 by John Arbash Meinel
Add a custom get_invalid_bundle and allow BadBundle to be
1500
        new_text = new_text.replace('<file file_id="exe-1"',
1501
                                    '<file executable="y" file_id="exe-1"')
4543.2.11 by John Arbash Meinel
Use a fixed file-id for the root to make the tests more stable.
1502
        new_text = new_text.replace('B244', 'B259')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1503
        bundle_txt = BytesIO()
4543.2.8 by John Arbash Meinel
Add a custom get_invalid_bundle and allow BadBundle to be
1504
        bundle_txt.write(serializer._get_bundle_header('4'))
1505
        bundle_txt.write('\n')
1506
        bundle_txt.write(new_text.encode('bz2'))
1507
        bundle_txt.seek(0)
1508
        bundle = read_bundle(bundle_txt)
1509
        self.valid_apply_bundle(base_rev_id, bundle)
1510
        return bundle
1511
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1512
    def make_merged_branch(self):
4543.2.6 by John Arbash Meinel
redefinning self.bzrdir_format() automatically sets the default format.
1513
        builder = self.make_branch_builder('source')
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1514
        builder.start_series()
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
1515
        builder.build_snapshot(None, [
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1516
            ('add', ('', 'root-id', 'directory', None)),
1517
            ('add', ('file', 'file-id', 'file', 'original content\n')),
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
1518
            ], revision_id='a@cset-0-1')
1519
        builder.build_snapshot(['a@cset-0-1'], [
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1520
            ('modify', ('file-id', 'new-content\n')),
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
1521
            ], revision_id='a@cset-0-2a')
1522
        builder.build_snapshot(['a@cset-0-1'], [
1523
            ('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1524
            ], revision_id='a@cset-0-2b')
1525
        builder.build_snapshot(['a@cset-0-2a', 'a@cset-0-2b'], [
1526
            ('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1527
            ], revision_id='a@cset-0-3')
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1528
        builder.finish_series()
1529
        self.b1 = builder.get_branch()
1530
        self.b1.lock_read()
1531
        self.addCleanup(self.b1.unlock)
1532
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1533
    def make_bundle_just_inventories(self, base_revision_id,
1534
                                     target_revision_id,
1535
                                     revision_ids):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1536
        sio = BytesIO()
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1537
        writer = v4.BundleWriteOperation(base_revision_id, target_revision_id,
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1538
                                         self.b1.repository, sio)
1539
        writer.bundle.begin()
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1540
        writer._add_inventory_mpdiffs_from_serializer(revision_ids)
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1541
        writer.bundle.end()
1542
        sio.seek(0)
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1543
        return sio
1544
1545
    def test_single_inventory_multiple_parents_as_xml(self):
1546
        self.make_merged_branch()
1547
        sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1548
                                                ['a@cset-0-3'])
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1549
        reader = v4.BundleReader(sio, stream_input=False)
1550
        records = list(reader.iter_records())
1551
        self.assertEqual(1, len(records))
1552
        (bytes, metadata, repo_kind, revision_id,
1553
         file_id) = records[0]
1554
        self.assertIs(None, file_id)
1555
        self.assertEqual('a@cset-0-3', revision_id)
1556
        self.assertEqual('inventory', repo_kind)
1557
        self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1558
                          'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1559
                          'storage_kind': 'mpdiff',
1560
                         }, metadata)
1561
        # We should have an mpdiff that takes some lines from both parents.
1562
        self.assertEqualDiff(
1563
            'i 1\n'
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1564
            '<inventory format="10" revision_id="a@cset-0-3">\n'
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1565
            '\n'
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1566
            'c 0 1 1 2\n'
1567
            'c 1 3 3 2\n', bytes)
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1568
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1569
    def test_single_inv_no_parents_as_xml(self):
1570
        self.make_merged_branch()
1571
        sio = self.make_bundle_just_inventories('null:', 'a@cset-0-1',
1572
                                                ['a@cset-0-1'])
1573
        reader = v4.BundleReader(sio, stream_input=False)
1574
        records = list(reader.iter_records())
1575
        self.assertEqual(1, len(records))
1576
        (bytes, metadata, repo_kind, revision_id,
1577
         file_id) = records[0]
1578
        self.assertIs(None, file_id)
1579
        self.assertEqual('a@cset-0-1', revision_id)
1580
        self.assertEqual('inventory', repo_kind)
1581
        self.assertEqual({'parents': [],
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1582
                          'sha1': 'a13f42b142d544aac9b085c42595d304150e31a2',
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1583
                          'storage_kind': 'mpdiff',
1584
                         }, metadata)
1585
        # We should have an mpdiff that takes some lines from both parents.
1586
        self.assertEqualDiff(
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1587
            'i 4\n'
1588
            '<inventory format="10" revision_id="a@cset-0-1">\n'
1589
            '<directory file_id="root-id" name=""'
1590
                ' revision="a@cset-0-1" />\n'
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1591
            '<file file_id="file-id" name="file" parent_id="root-id"'
1592
                ' revision="a@cset-0-1"'
1593
                ' text_sha1="09c2f8647e14e49e922b955c194102070597c2d1"'
1594
                ' text_size="17" />\n'
1595
            '</inventory>\n'
1596
            '\n', bytes)
1597
1598
    def test_multiple_inventories_as_xml(self):
1599
        self.make_merged_branch()
1600
        sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1601
            ['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'])
1602
        reader = v4.BundleReader(sio, stream_input=False)
1603
        records = list(reader.iter_records())
1604
        self.assertEqual(3, len(records))
1605
        revision_ids = [rev_id for b, m, k, rev_id, f in records]
1606
        self.assertEqual(['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'],
1607
                         revision_ids)
1608
        metadata_2a = records[0][1]
1609
        self.assertEqual({'parents': ['a@cset-0-1'],
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1610
                          'sha1': '1e105886d62d510763e22885eec733b66f5f09bf',
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1611
                          'storage_kind': 'mpdiff',
1612
                         }, metadata_2a)
1613
        metadata_2b = records[1][1]
1614
        self.assertEqual({'parents': ['a@cset-0-1'],
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1615
                          'sha1': 'f03f12574bdb5ed2204c28636c98a8547544ccd8',
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1616
                          'storage_kind': 'mpdiff',
1617
                         }, metadata_2b)
1618
        metadata_3 = records[2][1]
1619
        self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1620
                          'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1621
                          'storage_kind': 'mpdiff',
1622
                         }, metadata_3)
1623
        bytes_2a = records[0][0]
1624
        self.assertEqualDiff(
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1625
            'i 1\n'
1626
            '<inventory format="10" revision_id="a@cset-0-2a">\n'
1627
            '\n'
1628
            'c 0 1 1 1\n'
1629
            'i 1\n'
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1630
            '<file file_id="file-id" name="file" parent_id="root-id"'
1631
                ' revision="a@cset-0-2a"'
1632
                ' text_sha1="50f545ff40e57b6924b1f3174b267ffc4576e9a9"'
1633
                ' text_size="12" />\n'
1634
            '\n'
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1635
            'c 0 3 3 1\n', bytes_2a)
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1636
        bytes_2b = records[1][0]
1637
        self.assertEqualDiff(
1638
            'i 1\n'
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1639
            '<inventory format="10" revision_id="a@cset-0-2b">\n'
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1640
            '\n'
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1641
            'c 0 1 1 2\n'
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1642
            'i 1\n'
1643
            '<file file_id="file2-id" name="other-file" parent_id="root-id"'
1644
                ' revision="a@cset-0-2b"'
1645
                ' text_sha1="b46c0c8ea1e5ef8e46fc8894bfd4752a88ec939e"'
1646
                ' text_size="14" />\n'
1647
            '\n'
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1648
            'c 0 3 4 1\n', bytes_2b)
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1649
        bytes_3 = records[2][0]
1650
        self.assertEqualDiff(
1651
            'i 1\n'
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1652
            '<inventory format="10" revision_id="a@cset-0-3">\n'
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1653
            '\n'
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1654
            'c 0 1 1 2\n'
1655
            'c 1 3 3 2\n', bytes_3)
1656
1657
    def test_creating_bundle_preserves_chk_pages(self):
1658
        self.make_merged_branch()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
1659
        target = self.b1.controldir.sprout('target',
4543.2.7 by John Arbash Meinel
It turns out CHKSerializer was inheriting from xml5
1660
                                       revision_id='a@cset-0-2a').open_branch()
1661
        bundle_txt, rev_ids = self.create_bundle_text('a@cset-0-2a',
1662
                                                      'a@cset-0-3')
1663
        self.assertEqual(['a@cset-0-2b', 'a@cset-0-3'], rev_ids)
1664
        bundle = read_bundle(bundle_txt)
1665
        target.lock_write()
1666
        self.addCleanup(target.unlock)
1667
        install_bundle(target.repository, bundle)
1668
        inv1 = self.b1.repository.inventories.get_record_stream([
1669
            ('a@cset-0-3',)], 'unordered',
1670
            True).next().get_bytes_as('fulltext')
1671
        inv2 = target.repository.inventories.get_record_stream([
1672
            ('a@cset-0-3',)], 'unordered',
1673
            True).next().get_bytes_as('fulltext')
1674
        self.assertEqualDiff(inv1, inv2)
4543.2.5 by John Arbash Meinel
Fix issues with keys/ids and ghost handling.
1675
4543.2.4 by John Arbash Meinel
Start working on code that will use Repository._serializer.write_inventory_to_strig.
1676
2520.4.79 by Aaron Bentley
Fixed up not-really-relevant munging tests
1677
class MungedBundleTester(object):
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1678
1679
    def build_test_bundle(self):
1680
        wt = self.make_branch_and_tree('b1')
1681
1682
        self.build_tree(['b1/one'])
1683
        wt.add('one')
1684
        wt.commit('add one', rev_id='a@cset-0-1')
1685
        self.build_tree(['b1/two'])
1686
        wt.add('two')
1793.3.14 by John Arbash Meinel
Actually fix the bug with missing trailing newline bug #49182
1687
        wt.commit('add two', rev_id='a@cset-0-2',
1688
                  revprops={'branch-nick':'test'})
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1689
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1690
        bundle_txt = BytesIO()
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1691
        rev_ids = write_bundle(wt.branch.repository, 'a@cset-0-2',
2520.4.79 by Aaron Bentley
Fixed up not-really-relevant munging tests
1692
                               'a@cset-0-1', bundle_txt, self.format)
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1693
        self.assertEqual({'a@cset-0-2'}, set(rev_ids))
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1694
        bundle_txt.seek(0, 0)
1695
        return bundle_txt
1696
1697
    def check_valid(self, bundle):
1698
        """Check that after whatever munging, the final object is valid."""
1793.3.14 by John Arbash Meinel
Actually fix the bug with missing trailing newline bug #49182
1699
        self.assertEqual(['a@cset-0-2'],
1793.3.4 by John Arbash Meinel
[merge] bzr.dev 1804 and fix conflicts.
1700
            [r.revision_id for r in bundle.real_revisions])
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1701
1702
    def test_extra_whitespace(self):
1703
        bundle_txt = self.build_test_bundle()
1704
1705
        # Seek to the end of the file
1706
        # Adding one extra newline used to give us
1707
        # TypeError: float() argument must be a string or a number
1708
        bundle_txt.seek(0, 2)
1709
        bundle_txt.write('\n')
1710
        bundle_txt.seek(0)
1711
1793.3.4 by John Arbash Meinel
[merge] bzr.dev 1804 and fix conflicts.
1712
        bundle = read_bundle(bundle_txt)
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1713
        self.check_valid(bundle)
1714
1715
    def test_extra_whitespace_2(self):
1716
        bundle_txt = self.build_test_bundle()
1717
1718
        # Seek to the end of the file
1719
        # Adding two extra newlines used to give us
1720
        # MalformedPatches: The first line of all patches should be ...
1721
        bundle_txt.seek(0, 2)
1722
        bundle_txt.write('\n\n')
1723
        bundle_txt.seek(0)
1724
1793.3.4 by John Arbash Meinel
[merge] bzr.dev 1804 and fix conflicts.
1725
        bundle = read_bundle(bundle_txt)
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1726
        self.check_valid(bundle)
1727
2520.4.79 by Aaron Bentley
Fixed up not-really-relevant munging tests
1728
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1729
class MungedBundleTesterV09(tests.TestCaseWithTransport, MungedBundleTester):
2520.4.79 by Aaron Bentley
Fixed up not-really-relevant munging tests
1730
1731
    format = '0.9'
1732
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1733
    def test_missing_trailing_whitespace(self):
1734
        bundle_txt = self.build_test_bundle()
1735
1736
        # Remove a trailing newline, it shouldn't kill the parser
1737
        raw = bundle_txt.getvalue()
1738
        # The contents of the bundle don't have to be this, but this
1739
        # test is concerned with the exact case where the serializer
1740
        # creates a blank line at the end, and fails if that
1741
        # line is stripped
1742
        self.assertEqual('\n\n', raw[-2:])
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1743
        bundle_txt = BytesIO(raw[:-1])
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1744
1793.3.4 by John Arbash Meinel
[merge] bzr.dev 1804 and fix conflicts.
1745
        bundle = read_bundle(bundle_txt)
1793.3.2 by John Arbash Meinel
(failing) add some tests which munge trailing whitespace
1746
        self.check_valid(bundle)
1793.3.14 by John Arbash Meinel
Actually fix the bug with missing trailing newline bug #49182
1747
1793.3.16 by John Arbash Meinel
Add tests to ensure that we gracefully handle opening and trailing non-bundle text.
1748
    def test_opening_text(self):
1749
        bundle_txt = self.build_test_bundle()
1750
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1751
        bundle_txt = BytesIO(
1752
            b"Some random\nemail comments\n" + bundle_txt.getvalue())
1793.3.16 by John Arbash Meinel
Add tests to ensure that we gracefully handle opening and trailing non-bundle text.
1753
1754
        bundle = read_bundle(bundle_txt)
1755
        self.check_valid(bundle)
1756
1757
    def test_trailing_text(self):
1758
        bundle_txt = self.build_test_bundle()
1759
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1760
        bundle_txt = BytesIO(
1761
            bundle_txt.getvalue() + b"Some trailing\nrandom\ntext\n")
1793.3.16 by John Arbash Meinel
Add tests to ensure that we gracefully handle opening and trailing non-bundle text.
1762
1763
        bundle = read_bundle(bundle_txt)
1764
        self.check_valid(bundle)
2520.4.56 by Aaron Bentley
Begin adding support for arbitrary metadata
1765
1766
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1767
class MungedBundleTesterV4(tests.TestCaseWithTransport, MungedBundleTester):
2520.4.79 by Aaron Bentley
Fixed up not-really-relevant munging tests
1768
2520.4.136 by Aaron Bentley
Fix format strings
1769
    format = '4'
2520.4.79 by Aaron Bentley
Fixed up not-really-relevant munging tests
1770
1771
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1772
class TestBundleWriterReader(tests.TestCase):
2520.4.56 by Aaron Bentley
Begin adding support for arbitrary metadata
1773
1774
    def test_roundtrip_record(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1775
        fileobj = BytesIO()
2520.4.72 by Aaron Bentley
Rename format to 4alpha
1776
        writer = v4.BundleWriter(fileobj)
2520.4.56 by Aaron Bentley
Begin adding support for arbitrary metadata
1777
        writer.begin()
2520.4.95 by Aaron Bentley
Add support for header/info records
1778
        writer.add_info_record(foo='bar')
1779
        writer._add_record("Record body", {'parents': ['1', '3'],
1780
            'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
2520.4.56 by Aaron Bentley
Begin adding support for arbitrary metadata
1781
        writer.end()
1782
        fileobj.seek(0)
2520.4.148 by Aaron Bentley
Updates from review
1783
        reader = v4.BundleReader(fileobj, stream_input=True)
2520.4.145 by Aaron Bentley
Add memory_friendly toggle, be memory-unfriendly for merge directives
1784
        record_iter = reader.iter_records()
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
1785
        record = next(record_iter)
2520.4.145 by Aaron Bentley
Add memory_friendly toggle, be memory-unfriendly for merge directives
1786
        self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1787
            'info', None, None), record)
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
1788
        record = next(record_iter)
2520.4.145 by Aaron Bentley
Add memory_friendly toggle, be memory-unfriendly for merge directives
1789
        self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1790
                          'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1791
                          record)
1792
1793
    def test_roundtrip_record_memory_hungry(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1794
        fileobj = BytesIO()
2520.4.145 by Aaron Bentley
Add memory_friendly toggle, be memory-unfriendly for merge directives
1795
        writer = v4.BundleWriter(fileobj)
1796
        writer.begin()
1797
        writer.add_info_record(foo='bar')
1798
        writer._add_record("Record body", {'parents': ['1', '3'],
1799
            'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1800
        writer.end()
1801
        fileobj.seek(0)
2520.4.148 by Aaron Bentley
Updates from review
1802
        reader = v4.BundleReader(fileobj, stream_input=False)
2520.4.145 by Aaron Bentley
Add memory_friendly toggle, be memory-unfriendly for merge directives
1803
        record_iter = reader.iter_records()
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
1804
        record = next(record_iter)
2520.4.95 by Aaron Bentley
Add support for header/info records
1805
        self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1806
            'info', None, None), record)
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
1807
        record = next(record_iter)
2520.4.95 by Aaron Bentley
Add support for header/info records
1808
        self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1809
                          'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1810
                          record)
1811
2520.4.127 by Aaron Bentley
Fix up name encoding to handle revision-ids with slashes
1812
    def test_encode_name(self):
2520.4.95 by Aaron Bentley
Add support for header/info records
1813
        self.assertEqual('revision/rev1',
1814
            v4.BundleWriter.encode_name('revision', 'rev1'))
2520.4.127 by Aaron Bentley
Fix up name encoding to handle revision-ids with slashes
1815
        self.assertEqual('file/rev//1/file-id-1',
1816
            v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
2520.4.95 by Aaron Bentley
Add support for header/info records
1817
        self.assertEqual('info',
1818
            v4.BundleWriter.encode_name('info', None, None))
1819
2520.4.127 by Aaron Bentley
Fix up name encoding to handle revision-ids with slashes
1820
    def test_decode_name(self):
2520.4.95 by Aaron Bentley
Add support for header/info records
1821
        self.assertEqual(('revision', 'rev1', None),
1822
            v4.BundleReader.decode_name('revision/rev1'))
2520.4.127 by Aaron Bentley
Fix up name encoding to handle revision-ids with slashes
1823
        self.assertEqual(('file', 'rev/1', 'file-id-1'),
1824
            v4.BundleReader.decode_name('file/rev//1/file-id-1'))
2520.4.95 by Aaron Bentley
Add support for header/info records
1825
        self.assertEqual(('info', None, None),
1826
                         v4.BundleReader.decode_name('info'))
2520.4.131 by Aaron Bentley
Raise BadBundle for records with wrong number of names
1827
1828
    def test_too_many_names(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1829
        fileobj = BytesIO()
2520.4.131 by Aaron Bentley
Raise BadBundle for records with wrong number of names
1830
        writer = v4.BundleWriter(fileobj)
1831
        writer.begin()
1832
        writer.add_info_record(foo='bar')
1833
        writer._container.add_bytes_record('blah', ['two', 'names'])
1834
        writer.end()
1835
        fileobj.seek(0)
1836
        record_iter = v4.BundleReader(fileobj).iter_records()
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
1837
        record = next(record_iter)
2520.4.131 by Aaron Bentley
Raise BadBundle for records with wrong number of names
1838
        self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1839
            'info', None, None), record)
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
1840
        self.assertRaises(errors.BadBundle, next, record_iter)
3251.4.11 by Aaron Bentley
Fix wrong local lookups
1841
1842
4241.14.13 by Vincent Ladeuil
Some more cleanup.
1843
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
3251.4.11 by Aaron Bentley
Fix wrong local lookups
1844
1845
    def test_read_mergeable_skips_local(self):
1846
        """A local bundle named like the URL should not be read.
1847
        """
1848
        out, wt = test_read_bundle.create_bundle_file(self)
1849
        class FooService(object):
1850
            """A directory service that always returns source"""
1851
1852
            def look_up(self, name, url):
1853
                return 'source'
3251.4.12 by Aaron Bentley
Updates from review
1854
        directories.register('foo:', FooService, 'Testing directory service')
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
1855
        self.addCleanup(directories.remove, 'foo:')
3251.4.11 by Aaron Bentley
Fix wrong local lookups
1856
        self.build_tree_contents([('./foo:bar', out.getvalue())])
1857
        self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1858
                          'foo:bar')
3703.2.1 by Andrew Bennetts
Allow ConnectionReset to propagate from read_mergeable_from_url.
1859
4547.2.2 by Andrew Bennetts
Add test for read_mergeable_from_transport raising NotABundle when TooManyRedirections happens.
1860
    def test_infinite_redirects_are_not_a_bundle(self):
1861
        """If a URL causes TooManyRedirections then NotABundle is raised.
1862
        """
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
1863
        from .blackbox.test_push import RedirectingMemoryServer
4547.2.2 by Andrew Bennetts
Add test for read_mergeable_from_transport raising NotABundle when TooManyRedirections happens.
1864
        server = RedirectingMemoryServer()
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
1865
        self.start_server(server)
4547.2.2 by Andrew Bennetts
Add test for read_mergeable_from_transport raising NotABundle when TooManyRedirections happens.
1866
        url = server.get_url() + 'infinite-loop'
1867
        self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
1868
3703.2.1 by Andrew Bennetts
Allow ConnectionReset to propagate from read_mergeable_from_url.
1869
    def test_smart_server_connection_reset(self):
1870
        """If a smart server connection fails during the attempt to read a
1871
        bundle, then the ConnectionReset error should be propagated.
1872
        """
1873
        # Instantiate a server that will provoke a ConnectionReset
6206.1.9 by Vincent Ladeuil
Simpler fix for test_smart_server_connection_reset re-using more of the existing test server infrastructure.
1874
        sock_server = DisconnectingServer()
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
1875
        self.start_server(sock_server)
3750.1.3 by Vincent Ladeuil
Cleanups.
1876
        # We don't really care what the url is since the server will close the
1877
        # connection without interpreting it
3703.2.1 by Andrew Bennetts
Allow ConnectionReset to propagate from read_mergeable_from_url.
1878
        url = sock_server.get_url()
1879
        self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
6206.1.9 by Vincent Ladeuil
Simpler fix for test_smart_server_connection_reset re-using more of the existing test server infrastructure.
1880
1881
6621.25.1 by Martin
Adapt to SocketServer module name change
1882
class DisconnectingHandler(socketserver.BaseRequestHandler):
6206.1.9 by Vincent Ladeuil
Simpler fix for test_smart_server_connection_reset re-using more of the existing test server infrastructure.
1883
    """A request handler that immediately closes any connection made to it."""
1884
1885
    def handle(self):
1886
        self.request.close()
1887
1888
1889
class DisconnectingServer(test_server.TestingTCPServerInAThread):
1890
1891
    def __init__(self):
1892
        super(DisconnectingServer, self).__init__(
1893
            ('127.0.0.1', 0),
1894
            test_server.TestingTCPServer,
1895
            DisconnectingHandler)
1896
1897
    def get_url(self):
1898
        """Return the url of the server"""
1899
        return "bzr://%s:%d/" % self.server.server_address