/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2005-2011, 2016 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
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
#
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
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
#
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
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.1.2 by Martin Pool
Import testsweet module adapted from bzr.
16
17
1083 by Martin Pool
- add space to store revision-id in weave files
18
# TODO: tests regarding version names
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
19
# TODO: rbc 20050108 test that join does not leave an inconsistent weave
1185.13.4 by Robert Collins
make reweave visible as a weave method, and quickly integrate into fetch
20
#       if it fails.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
21
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
22
"""test suite for weave algorithm"""
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
23
7479.2.1 by Jelmer Vernooij
Drop python2 support.
24
from io import BytesIO
1323 by Martin Pool
- caller can pass SHA-1 to Weave.add for efficiency
25
from pprint import pformat
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
26
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from .. import (
2024.1.1 by John Arbash Meinel
When a weave file is empty, we should get WeaveFormatError, not StopIteration
28
    errors,
29
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
30
from ..osutils import sha_string
31
from . import TestCase, TestCaseInTempDir
6729.2.1 by Jelmer Vernooij
Move weave errors to breezy.bzr.weave.
32
from ..bzr.weave import Weave, WeaveFormatError, WeaveInvalidChecksum
6670.4.1 by Jelmer Vernooij
Update imports.
33
from ..bzr.weavefile import write_weave, read_weave
0.1.66 by Martin Pool
Cope without set/frozenset classes
34
35
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
36
# texts for use in testing
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
37
TEXT_0 = [b"Hello world"]
38
TEXT_1 = [b"Hello world",
39
          b"A second line"]
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
40
41
1233 by Martin Pool
- fix up weave tests for new test framework
42
class TestBase(TestCase):
2776.1.1 by Robert Collins
* The ``add_lines`` methods on ``VersionedFile`` implementations has changed
43
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
44
    def check_read_write(self, k):
45
        """Check the weave k can be written & re-read."""
46
        from tempfile import TemporaryFile
47
        tf = TemporaryFile()
48
49
        write_weave(k, tf)
50
        tf.seek(0)
51
        k2 = read_weave(tf)
52
53
        if k != k2:
54
            tf.seek(0)
55
            self.log('serialized weave:')
56
            self.log(tf.read())
1083 by Martin Pool
- add space to store revision-id in weave files
57
58
            self.log('')
59
            self.log('parents: %s' % (k._parents == k2._parents))
60
            self.log('         %r' % k._parents)
61
            self.log('         %r' % k2._parents)
62
            self.log('')
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
63
            self.fail('read/write check failed')
1185.16.125 by Martin Pool
Test for 'name in weave'
64
65
66
class WeaveContains(TestBase):
67
    """Weave __contains__ operator"""
5582.10.32 by Jelmer Vernooij
Cleanups.
68
1185.16.125 by Martin Pool
Test for 'name in weave'
69
    def runTest(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
70
        k = Weave(get_scope=lambda: None)
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
71
        self.assertFalse(b'foo' in k)
72
        k.add_lines(b'foo', [], TEXT_1)
73
        self.assertTrue(b'foo' in k)
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
74
75
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
76
class Easy(TestBase):
5582.10.32 by Jelmer Vernooij
Cleanups.
77
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
78
    def runTest(self):
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
79
        Weave()
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
80
81
0.1.7 by Martin Pool
Add trivial annotate text
82
class AnnotateOne(TestBase):
5582.10.32 by Jelmer Vernooij
Cleanups.
83
0.1.7 by Martin Pool
Add trivial annotate text
84
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
85
        k = Weave()
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
86
        k.add_lines(b'text0', [], TEXT_0)
87
        self.assertEqual(k.annotate(b'text0'),
88
                         [(b'text0', TEXT_0[0])])
0.1.7 by Martin Pool
Add trivial annotate text
89
90
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
91
class InvalidAdd(TestBase):
92
    """Try to use invalid version number during add."""
5582.10.32 by Jelmer Vernooij
Cleanups.
93
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
94
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
95
        k = Weave()
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
96
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
97
        self.assertRaises(errors.RevisionNotPresent,
98
                          k.add_lines,
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
99
                          b'text0',
100
                          [b'69'],
101
                          [b'new text!'])
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
102
103
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
104
class RepeatedAdd(TestBase):
105
    """Add the same version twice; harmless."""
2776.1.1 by Robert Collins
* The ``add_lines`` methods on ``VersionedFile`` implementations has changed
106
107
    def test_duplicate_add(self):
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
108
        k = Weave()
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
109
        idx = k.add_lines(b'text0', [], TEXT_0)
110
        idx2 = k.add_lines(b'text0', [], TEXT_0)
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
111
        self.assertEqual(idx, idx2)
112
113
114
class InvalidRepeatedAdd(TestBase):
5582.10.32 by Jelmer Vernooij
Cleanups.
115
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
116
    def runTest(self):
117
        k = Weave()
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
118
        k.add_lines(b'basis', [], TEXT_0)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
119
        k.add_lines(b'text0', [], TEXT_0)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
120
        self.assertRaises(errors.RevisionAlreadyPresent,
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
121
                          k.add_lines,
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
122
                          b'text0',
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
123
                          [],
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
124
                          [b'not the same text'])
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
125
        self.assertRaises(errors.RevisionAlreadyPresent,
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
126
                          k.add_lines,
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
127
                          b'text0',
128
                          [b'basis'],         # not the right parents
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
129
                          TEXT_0)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
130
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
131
0.1.26 by Martin Pool
Refactor parameters to add command
132
class InsertLines(TestBase):
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
133
    """Store a revision that adds one line to the original.
134
135
    Look at the annotations to make sure that the first line is matched
136
    and not stored repeatedly."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
137
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
138
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
139
        k = Weave()
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
140
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
141
        k.add_lines(b'text0', [], [b'line 1'])
142
        k.add_lines(b'text1', [b'text0'], [b'line 1', b'line 2'])
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
143
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
144
        self.assertEqual(k.annotate(b'text0'),
145
                         [(b'text0', b'line 1')])
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
146
147
        self.assertEqual(k.get_lines(1),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
148
                         [b'line 1',
149
                          b'line 2'])
150
151
        self.assertEqual(k.annotate(b'text1'),
152
                         [(b'text0', b'line 1'),
153
                          (b'text1', b'line 2')])
154
155
        k.add_lines(b'text2', [b'text0'], [b'line 1', b'diverged line'])
156
157
        self.assertEqual(k.annotate(b'text2'),
158
                         [(b'text0', b'line 1'),
159
                          (b'text2', b'diverged line')])
160
161
        text3 = [b'line 1', b'middle line', b'line 2']
162
        k.add_lines(b'text3',
7143.15.2 by Jelmer Vernooij
Run autopep8.
163
                    [b'text0', b'text1'],
164
                    text3)
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
165
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
166
        # self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]),
167
        # text3))))
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
168
944 by Martin Pool
- refactor member names in Weave code
169
        self.log("k._weave=" + pformat(k._weave))
0.1.28 by Martin Pool
More tests for insertion of lines in new versions.
170
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
171
        self.assertEqual(k.annotate(b'text3'),
172
                         [(b'text0', b'line 1'),
173
                          (b'text3', b'middle line'),
174
                          (b'text1', b'line 2')])
0.1.28 by Martin Pool
More tests for insertion of lines in new versions.
175
0.1.31 by Martin Pool
Fix insertion of multiple regions, calculating the right line offset as we go.
176
        # now multiple insertions at different places
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
177
        k.add_lines(
178
            b'text4', [b'text0', b'text1', b'text3'],
179
            [b'line 1', b'aaa', b'middle line', b'bbb', b'line 2', b'ccc'])
0.1.31 by Martin Pool
Fix insertion of multiple regions, calculating the right line offset as we go.
180
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
181
        self.assertEqual(k.annotate(b'text4'),
182
                         [(b'text0', b'line 1'),
183
                          (b'text4', b'aaa'),
184
                          (b'text3', b'middle line'),
185
                          (b'text4', b'bbb'),
186
                          (b'text1', b'line 2'),
187
                          (b'text4', b'ccc')])
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
188
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
189
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
190
class DeleteLines(TestBase):
191
    """Deletion of lines from existing text.
192
193
    Try various texts all based on a common ancestor."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
194
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
195
    def runTest(self):
196
        k = Weave()
197
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
198
        base_text = [b'one', b'two', b'three', b'four']
199
200
        k.add_lines(b'text0', [], base_text)
201
202
        texts = [[b'one', b'two', b'three'],
203
                 [b'two', b'three', b'four'],
204
                 [b'one', b'four'],
205
                 [b'one', b'two', b'three', b'four'],
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
206
                 ]
207
1083 by Martin Pool
- add space to store revision-id in weave files
208
        i = 1
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
209
        for t in texts:
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
210
            k.add_lines(b'text%d' % i, [b'text0'], t)
1083 by Martin Pool
- add space to store revision-id in weave files
211
            i += 1
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
212
213
        self.log('final weave:')
944 by Martin Pool
- refactor member names in Weave code
214
        self.log('k._weave=' + pformat(k._weave))
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
215
216
        for i in range(len(texts)):
7143.15.2 by Jelmer Vernooij
Run autopep8.
217
            self.assertEqual(k.get_lines(i + 1),
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
218
                             texts[i])
219
220
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
221
class SuicideDelete(TestBase):
0.1.55 by Martin Pool
doc
222
    """Invalid weave which tries to add and delete simultaneously."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
223
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
224
    def runTest(self):
225
        k = Weave()
226
944 by Martin Pool
- refactor member names in Weave code
227
        k._parents = [(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
228
                      ]
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
229
        k._weave = [(b'{', 0),
7143.15.2 by Jelmer Vernooij
Run autopep8.
230
                    b'first line',
231
                    (b'[', 0),
232
                    b'deleted in 0',
233
                    (b']', 0),
234
                    (b'}', 0),
235
                    ]
236
        # SKIPPED
891 by Martin Pool
- fix up refactoring of weave
237
        # Weave.get doesn't trap this anymore
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
238
        return
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
239
240
        self.assertRaises(WeaveFormatError,
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
241
                          k.get_lines,
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
242
                          0)
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
243
244
0.1.48 by Martin Pool
Basic parsing of delete instructions.
245
class CannedDelete(TestBase):
246
    """Unpack canned weave with deleted lines."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
247
0.1.48 by Martin Pool
Basic parsing of delete instructions.
248
    def runTest(self):
249
        k = Weave()
250
944 by Martin Pool
- refactor member names in Weave code
251
        k._parents = [(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
252
                      frozenset([0]),
253
                      ]
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
254
        k._weave = [(b'{', 0),
7143.15.2 by Jelmer Vernooij
Run autopep8.
255
                    b'first line',
256
                    (b'[', 1),
257
                    b'line to be deleted',
258
                    (b']', 1),
259
                    b'last line',
260
                    (b'}', 0),
261
                    ]
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
262
        k._sha1s = [
263
            sha_string(b'first lineline to be deletedlast line'),
264
            sha_string(b'first linelast line')]
0.1.48 by Martin Pool
Basic parsing of delete instructions.
265
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
266
        self.assertEqual(k.get_lines(0),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
267
                         [b'first line',
268
                          b'line to be deleted',
269
                          b'last line',
0.1.48 by Martin Pool
Basic parsing of delete instructions.
270
                          ])
271
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
272
        self.assertEqual(k.get_lines(1),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
273
                         [b'first line',
274
                          b'last line',
0.1.50 by Martin Pool
Basic implementation of deletion markers
275
                          ])
276
0.1.48 by Martin Pool
Basic parsing of delete instructions.
277
0.1.51 by Martin Pool
Add test for replacement lines
278
class CannedReplacement(TestBase):
279
    """Unpack canned weave with deleted lines."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
280
0.1.51 by Martin Pool
Add test for replacement lines
281
    def runTest(self):
282
        k = Weave()
283
944 by Martin Pool
- refactor member names in Weave code
284
        k._parents = [frozenset(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
285
                      frozenset([0]),
286
                      ]
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
287
        k._weave = [(b'{', 0),
7143.15.2 by Jelmer Vernooij
Run autopep8.
288
                    b'first line',
289
                    (b'[', 1),
290
                    b'line to be deleted',
291
                    (b']', 1),
292
                    (b'{', 1),
293
                    b'replacement line',
294
                    (b'}', 1),
295
                    b'last line',
296
                    (b'}', 0),
297
                    ]
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
298
        k._sha1s = [
299
            sha_string(b'first lineline to be deletedlast line'),
300
            sha_string(b'first linereplacement linelast line')]
0.1.51 by Martin Pool
Add test for replacement lines
301
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
302
        self.assertEqual(k.get_lines(0),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
303
                         [b'first line',
304
                          b'line to be deleted',
305
                          b'last line',
0.1.51 by Martin Pool
Add test for replacement lines
306
                          ])
307
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
308
        self.assertEqual(k.get_lines(1),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
309
                         [b'first line',
310
                          b'replacement line',
311
                          b'last line',
0.1.51 by Martin Pool
Add test for replacement lines
312
                          ])
313
314
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
315
class BadWeave(TestBase):
316
    """Test that we trap an insert which should not occur."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
317
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
318
    def runTest(self):
319
        k = Weave()
320
944 by Martin Pool
- refactor member names in Weave code
321
        k._parents = [frozenset(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
322
                      ]
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
323
        k._weave = [b'bad line',
7143.15.2 by Jelmer Vernooij
Run autopep8.
324
                    (b'{', 0),
325
                    b'foo {',
326
                    (b'{', 1),
327
                    b'  added in version 1',
328
                    (b'{', 2),
329
                    b'  added in v2',
330
                    (b'}', 2),
331
                    b'  also from v1',
332
                    (b'}', 1),
333
                    b'}',
334
                    (b'}', 0)]
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
335
7143.15.2 by Jelmer Vernooij
Run autopep8.
336
        # SKIPPED
891 by Martin Pool
- fix up refactoring of weave
337
        # Weave.get doesn't trap this anymore
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
338
        return
891 by Martin Pool
- fix up refactoring of weave
339
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
340
        self.assertRaises(WeaveFormatError,
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
341
                          k.get,
342
                          0)
343
344
345
class BadInsert(TestBase):
346
    """Test that we trap an insert which should not occur."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
347
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
348
    def runTest(self):
349
        k = Weave()
350
944 by Martin Pool
- refactor member names in Weave code
351
        k._parents = [frozenset(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
352
                      frozenset([0]),
353
                      frozenset([0]),
354
                      frozenset([0, 1, 2]),
355
                      ]
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
356
        k._weave = [(b'{', 0),
7143.15.2 by Jelmer Vernooij
Run autopep8.
357
                    b'foo {',
358
                    (b'{', 1),
359
                    b'  added in version 1',
360
                    (b'{', 1),
361
                    b'  more in 1',
362
                    (b'}', 1),
363
                    (b'}', 1),
364
                    (b'}', 0)]
891 by Martin Pool
- fix up refactoring of weave
365
366
        # this is not currently enforced by get
7143.15.2 by Jelmer Vernooij
Run autopep8.
367
        return
891 by Martin Pool
- fix up refactoring of weave
368
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
369
        self.assertRaises(WeaveFormatError,
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
370
                          k.get,
371
                          0)
372
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
373
        self.assertRaises(WeaveFormatError,
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
374
                          k.get,
375
                          1)
376
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
377
378
class InsertNested(TestBase):
379
    """Insertion with nested instructions."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
380
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
381
    def runTest(self):
382
        k = Weave()
383
944 by Martin Pool
- refactor member names in Weave code
384
        k._parents = [frozenset(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
385
                      frozenset([0]),
386
                      frozenset([0]),
387
                      frozenset([0, 1, 2]),
388
                      ]
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
389
        k._weave = [(b'{', 0),
7143.15.2 by Jelmer Vernooij
Run autopep8.
390
                    b'foo {',
391
                    (b'{', 1),
392
                    b'  added in version 1',
393
                    (b'{', 2),
394
                    b'  added in v2',
395
                    (b'}', 2),
396
                    b'  also from v1',
397
                    (b'}', 1),
398
                    b'}',
399
                    (b'}', 0)]
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
400
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
401
        k._sha1s = [
402
            sha_string(b'foo {}'),
403
            sha_string(b'foo {  added in version 1  also from v1}'),
404
            sha_string(b'foo {  added in v2}'),
405
            sha_string(
406
                b'foo {  added in version 1  added in v2  also from v1}')
407
            ]
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
408
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
409
        self.assertEqual(k.get_lines(0),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
410
                         [b'foo {',
411
                          b'}'])
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
412
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
413
        self.assertEqual(k.get_lines(1),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
414
                         [b'foo {',
415
                          b'  added in version 1',
416
                          b'  also from v1',
417
                          b'}'])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
418
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
419
        self.assertEqual(k.get_lines(2),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
420
                         [b'foo {',
421
                          b'  added in v2',
422
                          b'}'])
0.1.44 by Martin Pool
More tests for nested insert instructions
423
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
424
        self.assertEqual(k.get_lines(3),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
425
                         [b'foo {',
426
                          b'  added in version 1',
427
                          b'  added in v2',
428
                          b'  also from v1',
429
                          b'}'])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
430
0.1.45 by Martin Pool
doc
431
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
432
class DeleteLines2(TestBase):
0.1.30 by Martin Pool
Start adding tests for line deletion
433
    """Test recording revisions that delete lines.
434
435
    This relies on the weave having a way to represent lines knocked
436
    out by a later revision."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
437
0.1.30 by Martin Pool
Start adding tests for line deletion
438
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
439
        k = Weave()
0.1.30 by Martin Pool
Start adding tests for line deletion
440
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
441
        k.add_lines(b'text0', [], [b"line the first",
7143.15.2 by Jelmer Vernooij
Run autopep8.
442
                                   b"line 2",
443
                                   b"line 3",
444
                                   b"fine"])
0.1.30 by Martin Pool
Start adding tests for line deletion
445
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
446
        self.assertEqual(len(k.get_lines(0)), 4)
0.1.30 by Martin Pool
Start adding tests for line deletion
447
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
448
        k.add_lines(b'text1', [b'text0'], [b"line the first",
7143.15.2 by Jelmer Vernooij
Run autopep8.
449
                                           b"fine"])
0.1.30 by Martin Pool
Start adding tests for line deletion
450
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
451
        self.assertEqual(k.get_lines(1),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
452
                         [b"line the first",
453
                          b"fine"])
0.1.30 by Martin Pool
Start adding tests for line deletion
454
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
455
        self.assertEqual(k.annotate(b'text1'),
456
                         [(b'text0', b"line the first"),
457
                          (b'text0', b"fine")])
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
458
0.1.30 by Martin Pool
Start adding tests for line deletion
459
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
460
class IncludeVersions(TestBase):
461
    """Check texts that are stored across multiple revisions.
462
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
463
    Here we manually create a weave with particular encoding and make
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
464
    sure it unpacks properly.
465
466
    Text 0 includes nothing; text 1 includes text 0 and adds some
467
    lines.
468
    """
469
470
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
471
        k = Weave()
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
472
944 by Martin Pool
- refactor member names in Weave code
473
        k._parents = [frozenset(), frozenset([0])]
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
474
        k._weave = [(b'{', 0),
7143.15.2 by Jelmer Vernooij
Run autopep8.
475
                    b"first line",
476
                    (b'}', 0),
477
                    (b'{', 1),
478
                    b"second line",
479
                    (b'}', 1)]
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
480
7143.15.2 by Jelmer Vernooij
Run autopep8.
481
        k._sha1s = [sha_string(b'first line'), sha_string(
482
            b'first linesecond line')]
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
483
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
484
        self.assertEqual(k.get_lines(1),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
485
                         [b"first line",
486
                          b"second line"])
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
487
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
488
        self.assertEqual(k.get_lines(0),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
489
                         [b"first line"])
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
490
0.1.5 by Martin Pool
Add test for storing two text versions.
491
0.1.14 by Martin Pool
Another test for version inclusion
492
class DivergedIncludes(TestBase):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
493
    """Weave with two diverged texts based on version 0.
0.1.14 by Martin Pool
Another test for version inclusion
494
    """
7143.15.2 by Jelmer Vernooij
Run autopep8.
495
0.1.14 by Martin Pool
Another test for version inclusion
496
    def runTest(self):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
497
        # FIXME make the weave, dont poke at it.
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
498
        k = Weave()
0.1.14 by Martin Pool
Another test for version inclusion
499
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
500
        k._names = [b'0', b'1', b'2']
7143.15.2 by Jelmer Vernooij
Run autopep8.
501
        k._name_map = {b'0': 0, b'1': 1, b'2': 2}
944 by Martin Pool
- refactor member names in Weave code
502
        k._parents = [frozenset(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
503
                      frozenset([0]),
504
                      frozenset([0]),
505
                      ]
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
506
        k._weave = [(b'{', 0),
7143.15.2 by Jelmer Vernooij
Run autopep8.
507
                    b"first line",
508
                    (b'}', 0),
509
                    (b'{', 1),
510
                    b"second line",
511
                    (b'}', 1),
512
                    (b'{', 2),
513
                    b"alternative second line",
514
                    (b'}', 2),
515
                    ]
0.1.14 by Martin Pool
Another test for version inclusion
516
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
517
        k._sha1s = [
518
            sha_string(b'first line'),
519
            sha_string(b'first linesecond line'),
520
            sha_string(b'first linealternative second line')]
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
521
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
522
        self.assertEqual(k.get_lines(0),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
523
                         [b"first line"])
0.1.14 by Martin Pool
Another test for version inclusion
524
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
525
        self.assertEqual(k.get_lines(1),
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
526
                         [b"first line",
527
                          b"second line"])
528
529
        self.assertEqual(k.get_lines(b'2'),
530
                         [b"first line",
531
                          b"alternative second line"])
532
533
        self.assertEqual(list(k.get_ancestry([b'2'])),
534
                         [b'0', b'2'])
0.1.77 by Martin Pool
New Weave.get_included() does transitive expansion
535
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
536
537
class ReplaceLine(TestBase):
538
    def runTest(self):
539
        k = Weave()
540
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
541
        text0 = [b'cheddar', b'stilton', b'gruyere']
542
        text1 = [b'cheddar', b'blue vein', b'neufchatel', b'chevre']
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
543
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
544
        k.add_lines(b'text0', [], text0)
545
        k.add_lines(b'text1', [b'text0'], text1)
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
546
944 by Martin Pool
- refactor member names in Weave code
547
        self.log('k._weave=' + pformat(k._weave))
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
548
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
549
        self.assertEqual(k.get_lines(0), text0)
550
        self.assertEqual(k.get_lines(1), text1)
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
551
0.1.64 by Martin Pool
Add test for merging versions
552
553
class Merge(TestBase):
0.1.95 by Martin Pool
- preliminary merge conflict detection
554
    """Storage of versions that merge diverged parents"""
5582.10.32 by Jelmer Vernooij
Cleanups.
555
0.1.64 by Martin Pool
Add test for merging versions
556
    def runTest(self):
557
        k = Weave()
558
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
559
        texts = [
560
            [b'header'],
561
            [b'header', b'', b'line from 1'],
562
            [b'header', b'', b'line from 2', b'more from 2'],
563
            [b'header', b'', b'line from 1', b'fixup line', b'line from 2'],
564
            ]
0.1.64 by Martin Pool
Add test for merging versions
565
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
566
        k.add_lines(b'text0', [], texts[0])
567
        k.add_lines(b'text1', [b'text0'], texts[1])
568
        k.add_lines(b'text2', [b'text0'], texts[2])
569
        k.add_lines(b'merge', [b'text0', b'text1', b'text2'], texts[3])
0.1.64 by Martin Pool
Add test for merging versions
570
571
        for i, t in enumerate(texts):
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
572
            self.assertEqual(k.get_lines(i), t)
0.1.64 by Martin Pool
Add test for merging versions
573
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
574
        self.assertEqual(k.annotate(b'merge'),
575
                         [(b'text0', b'header'),
576
                          (b'text1', b''),
577
                          (b'text1', b'line from 1'),
578
                          (b'merge', b'fixup line'),
579
                          (b'text2', b'line from 2'),
0.1.64 by Martin Pool
Add test for merging versions
580
                          ])
581
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
582
        self.assertEqual(list(k.get_ancestry([b'merge'])),
583
                         [b'text0', b'text1', b'text2', b'merge'])
0.1.77 by Martin Pool
New Weave.get_included() does transitive expansion
584
944 by Martin Pool
- refactor member names in Weave code
585
        self.log('k._weave=' + pformat(k._weave))
0.1.64 by Martin Pool
Add test for merging versions
586
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
587
        self.check_read_write(k)
0.1.65 by Martin Pool
Add Weave.merge_iter to get automerged lines
588
589
0.1.95 by Martin Pool
- preliminary merge conflict detection
590
class Conflicts(TestBase):
591
    """Test detection of conflicting regions during a merge.
592
593
    A base version is inserted, then two descendents try to
594
    insert different lines in the same place.  These should be
595
    reported as a possible conflict and forwarded to the user."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
596
0.1.95 by Martin Pool
- preliminary merge conflict detection
597
    def runTest(self):
598
        return  # NOT RUN
599
        k = Weave()
600
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
601
        k.add_lines([], [b'aaa', b'bbb'])
602
        k.add_lines([0], [b'aaa', b'111', b'bbb'])
603
        k.add_lines([1], [b'aaa', b'222', b'bbb'])
0.1.95 by Martin Pool
- preliminary merge conflict detection
604
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
605
        k.merge([1, 2])
0.1.95 by Martin Pool
- preliminary merge conflict detection
606
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
607
        self.assertEqual([[[b'aaa']],
7143.15.2 by Jelmer Vernooij
Run autopep8.
608
                          [[b'111'], [b'222']],
609
                          [[b'bbb']]])
0.1.95 by Martin Pool
- preliminary merge conflict detection
610
611
612
class NonConflict(TestBase):
613
    """Two descendants insert compatible changes.
614
615
    No conflict should be reported."""
7143.15.2 by Jelmer Vernooij
Run autopep8.
616
0.1.95 by Martin Pool
- preliminary merge conflict detection
617
    def runTest(self):
618
        return  # NOT RUN
619
        k = Weave()
620
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
621
        k.add_lines([], [b'aaa', b'bbb'])
622
        k.add_lines([0], [b'111', b'aaa', b'ccc', b'bbb'])
623
        k.add_lines([1], [b'aaa', b'ccc', b'bbb', b'222'])
0.1.95 by Martin Pool
- preliminary merge conflict detection
624
625
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
626
class Khayyam(TestBase):
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
627
    """Test changes to multi-line texts, and read/write"""
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
628
629
    def test_multi_line_merge(self):
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
630
        rawtexts = [
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
631
            b"""A Book of Verses underneath the Bough,
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
632
            A Jug of Wine, a Loaf of Bread, -- and Thou
633
            Beside me singing in the Wilderness --
634
            Oh, Wilderness were Paradise enow!""",
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
635
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
636
            b"""A Book of Verses underneath the Bough,
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
637
            A Jug of Wine, a Loaf of Bread, -- and Thou
638
            Beside me singing in the Wilderness --
639
            Oh, Wilderness were Paradise now!""",
0.1.59 by Martin Pool
More modification tests
640
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
641
            b"""A Book of poems underneath the tree,
0.1.59 by Martin Pool
More modification tests
642
            A Jug of Wine, a Loaf of Bread,
643
            and Thou
644
            Beside me singing in the Wilderness --
645
            Oh, Wilderness were Paradise now!
646
647
            -- O. Khayyam""",
648
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
649
            b"""A Book of Verses underneath the Bough,
0.1.59 by Martin Pool
More modification tests
650
            A Jug of Wine, a Loaf of Bread,
651
            and Thou
652
            Beside me singing in the Wilderness --
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
653
            Oh, Wilderness were Paradise now!""",
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
654
            ]
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
655
        texts = [[l.strip() for l in t.split(b'\n')] for t in rawtexts]
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
656
657
        k = Weave()
658
        parents = set()
1083 by Martin Pool
- add space to store revision-id in weave files
659
        i = 0
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
660
        for t in texts:
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
661
            k.add_lines(b'text%d' % i, list(parents), t)
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
662
            parents.add(b'text%d' % i)
1083 by Martin Pool
- add space to store revision-id in weave files
663
            i += 1
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
664
944 by Martin Pool
- refactor member names in Weave code
665
        self.log("k._weave=" + pformat(k._weave))
0.1.59 by Martin Pool
More modification tests
666
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
667
        for i, t in enumerate(texts):
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
668
            self.assertEqual(k.get_lines(i), t)
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
669
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
670
        self.check_read_write(k)
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
671
672
1393.1.48 by Martin Pool
- Add stub Weave.join() method
673
class JoinWeavesTests(TestBase):
5582.10.32 by Jelmer Vernooij
Cleanups.
674
1393.1.50 by Martin Pool
- more development of Weave.join()
675
    def setUp(self):
676
        super(JoinWeavesTests, self).setUp()
677
        self.weave1 = Weave()
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
678
        self.lines1 = [b'hello\n']
679
        self.lines3 = [b'hello\n', b'cruel\n', b'world\n']
680
        self.weave1.add_lines(b'v1', [], self.lines1)
681
        self.weave1.add_lines(b'v2', [b'v1'], [b'hello\n', b'world\n'])
682
        self.weave1.add_lines(b'v3', [b'v2'], self.lines3)
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
683
684
    def test_written_detection(self):
1185.50.29 by John Arbash Meinel
Whitespace and other formatting cleanups suggested by Robert.
685
        # Test detection of weave file corruption.
686
        #
687
        # Make sure that we can detect if a weave file has
688
        # been corrupted. This doesn't test all forms of corruption,
689
        # but it at least helps verify the data you get, is what you want.
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
690
691
        w = Weave()
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
692
        w.add_lines(b'v1', [], [b'hello\n'])
693
        w.add_lines(b'v2', [b'v1'], [b'hello\n', b'there\n'])
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
694
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
695
        tmpf = BytesIO()
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
696
        write_weave(w, tmpf)
697
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
698
        # Because we are corrupting, we need to make sure we have the exact
699
        # text
700
        self.assertEqual(
701
            b'# bzr weave file v5\n'
702
            b'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
703
            b'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
704
            b'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n',
705
            tmpf.getvalue())
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
706
707
        # Change a single letter
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
708
        tmpf = BytesIO(
709
            b'# bzr weave file v5\n'
710
            b'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
711
            b'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
712
            b'w\n{ 0\n. hello\n}\n{ 1\n. There\n}\nW\n')
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
713
714
        w = read_weave(tmpf)
715
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
716
        self.assertEqual(b'hello\n', w.get_text(b'v1'))
717
        self.assertRaises(WeaveInvalidChecksum, w.get_text, b'v2')
718
        self.assertRaises(WeaveInvalidChecksum, w.get_lines, b'v2')
6729.2.1 by Jelmer Vernooij
Move weave errors to breezy.bzr.weave.
719
        self.assertRaises(WeaveInvalidChecksum, w.check)
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
720
721
        # Change the sha checksum
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
722
        tmpf = BytesIO(
723
            b'# bzr weave file v5\n'
724
            b'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
725
            b'i 0\n1 f0f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
726
            b'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n')
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
727
728
        w = read_weave(tmpf)
729
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
730
        self.assertEqual(b'hello\n', w.get_text(b'v1'))
731
        self.assertRaises(WeaveInvalidChecksum, w.get_text, b'v2')
732
        self.assertRaises(WeaveInvalidChecksum, w.get_lines, b'v2')
6729.2.1 by Jelmer Vernooij
Move weave errors to breezy.bzr.weave.
733
        self.assertRaises(WeaveInvalidChecksum, w.check)
1185.50.23 by John Arbash Meinel
Adding sha1 check when weave extracts a text.
734
735
3514.2.2 by John Arbash Meinel
Restore a real weave merge to 'bzr merge --weave'.
736
class TestWeave(TestCase):
737
738
    def test_allow_reserved_false(self):
739
        w = Weave('name', allow_reserved=False)
740
        # Add lines is checked at the WeaveFile level, not at the Weave level
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
741
        w.add_lines(b'name:', [], TEXT_1)
3514.2.2 by John Arbash Meinel
Restore a real weave merge to 'bzr merge --weave'.
742
        # But get_lines is checked at this level
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
743
        self.assertRaises(errors.ReservedId, w.get_lines, b'name:')
3514.2.2 by John Arbash Meinel
Restore a real weave merge to 'bzr merge --weave'.
744
745
    def test_allow_reserved_true(self):
746
        w = Weave('name', allow_reserved=True)
6963.2.17 by Jelmer Vernooij
port breezy.bzr.weave to Python3.
747
        w.add_lines(b'name:', [], TEXT_1)
748
        self.assertEqual(TEXT_1, w.get_lines(b'name:'))
3514.2.2 by John Arbash Meinel
Restore a real weave merge to 'bzr merge --weave'.
749
750
1551.3.11 by Aaron Bentley
Merge from Robert
751
class InstrumentedWeave(Weave):
752
    """Keep track of how many times functions are called."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
753
1551.3.11 by Aaron Bentley
Merge from Robert
754
    def __init__(self, weave_name=None):
755
        self._extract_count = 0
756
        Weave.__init__(self, weave_name=weave_name)
757
758
    def _extract(self, versions):
759
        self._extract_count += 1
760
        return Weave._extract(self, versions)
761
762
1616.1.18 by Martin Pool
(weave-merge) don't treat killed-both lines as points of agreement;
763
class TestNeedsReweave(TestCase):
1563.2.24 by Robert Collins
Make join cheaper for compatibly inconsistent parents.
764
    """Internal corner cases for when reweave is needed."""
765
766
    def test_compatible_parents(self):
767
        w1 = Weave('a')
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
768
        my_parents = {1, 2, 3}
1563.2.24 by Robert Collins
Make join cheaper for compatibly inconsistent parents.
769
        # subsets are ok
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
770
        self.assertTrue(w1._compatible_parents(my_parents, {3}))
1563.2.24 by Robert Collins
Make join cheaper for compatibly inconsistent parents.
771
        # same sets
772
        self.assertTrue(w1._compatible_parents(my_parents, set(my_parents)))
773
        # same empty corner case
774
        self.assertTrue(w1._compatible_parents(set(), set()))
775
        # other cannot contain stuff my_parents does not
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
776
        self.assertFalse(w1._compatible_parents(set(), {1}))
777
        self.assertFalse(w1._compatible_parents(my_parents, {1, 2, 3, 4}))
778
        self.assertFalse(w1._compatible_parents(my_parents, {4}))
2024.1.1 by John Arbash Meinel
When a weave file is empty, we should get WeaveFormatError, not StopIteration
779
780
781
class TestWeaveFile(TestCaseInTempDir):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
782
2024.1.1 by John Arbash Meinel
When a weave file is empty, we should get WeaveFormatError, not StopIteration
783
    def test_empty_file(self):
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
784
        with open('empty.weave', 'wb+') as f:
6729.2.1 by Jelmer Vernooij
Move weave errors to breezy.bzr.weave.
785
            self.assertRaises(WeaveFormatError, read_weave, f)