/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test__chk_map.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-09-22 02:38:35 UTC
  • mfrom: (7058.5.4 upload-symlin)
  • Revision ID: breezy.the.bot@gmail.com-20180922023835-wb9nczxp63jpeudb
Add symlink support to 'brz upload'.

Merged from https://code.launchpad.net/~jelmer/brz/upload-symlinks/+merge/355061

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Tests for _chk_map_*."""
18
18
 
19
 
from bzrlib import (
 
19
from .. import (
 
20
    tests,
 
21
    )
 
22
from ..bzr import (
20
23
    chk_map,
21
 
    tests,
22
24
    )
23
 
from bzrlib.static_tuple import StaticTuple
 
25
from ..sixish import int2byte
 
26
from ..static_tuple import StaticTuple
24
27
stuple = StaticTuple
25
28
 
26
29
 
27
 
def load_tests(standard_tests, module, loader):
 
30
def load_tests(loader, standard_tests, pattern):
28
31
    suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
29
 
        'bzrlib._chk_map_py', 'bzrlib._chk_map_pyx')
 
32
        'breezy.bzr._chk_map_py', 'breezy.bzr._chk_map_pyx')
30
33
    return suite
31
34
 
32
35
 
42
45
        self.assertEqual(expected, actual, 'actual: %r' % (actual,))
43
46
 
44
47
    def test_simple_16(self):
45
 
        self.assertSearchKey16('8C736521', stuple('foo',))
46
 
        self.assertSearchKey16('8C736521\x008C736521', stuple('foo', 'foo'))
47
 
        self.assertSearchKey16('8C736521\x0076FF8CAA', stuple('foo', 'bar'))
48
 
        self.assertSearchKey16('ED82CD11', stuple('abcd',))
 
48
        self.assertSearchKey16(b'8C736521', stuple(b'foo',))
 
49
        self.assertSearchKey16(b'8C736521\x008C736521', stuple(b'foo', b'foo'))
 
50
        self.assertSearchKey16(b'8C736521\x0076FF8CAA', stuple(b'foo', b'bar'))
 
51
        self.assertSearchKey16(b'ED82CD11', stuple(b'abcd',))
49
52
 
50
53
    def test_simple_255(self):
51
 
        self.assertSearchKey255('\x8cse!', stuple('foo',))
52
 
        self.assertSearchKey255('\x8cse!\x00\x8cse!', stuple('foo', 'foo'))
53
 
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', stuple('foo', 'bar'))
 
54
        self.assertSearchKey255(b'\x8cse!', stuple(b'foo',))
 
55
        self.assertSearchKey255(b'\x8cse!\x00\x8cse!', stuple(b'foo', b'foo'))
 
56
        self.assertSearchKey255(b'\x8cse!\x00v\xff\x8c\xaa', stuple(b'foo', b'bar'))
54
57
        # The standard mapping for these would include '\n', so it should be
55
58
        # mapped to '_'
56
 
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', stuple('<', 'V'))
 
59
        self.assertSearchKey255(b'\xfdm\x93_\x00P_\x1bL', stuple(b'<', b'V'))
57
60
 
58
61
    def test_255_does_not_include_newline(self):
59
62
        # When mapping via _search_key_255, we should never have the '\n'
60
63
        # character, but all other 255 values should be present
61
64
        chars_used = set()
62
65
        for char_in in range(256):
63
 
            search_key = self.module._search_key_255(stuple(chr(char_in),))
64
 
            chars_used.update(search_key)
65
 
        all_chars = set([chr(x) for x in range(256)])
 
66
            search_key = self.module._search_key_255(stuple(int2byte(char_in),))
 
67
            chars_used.update(map(int2byte, bytearray(search_key)))
 
68
        all_chars = {int2byte(x) for x in range(256)}
66
69
        unused_chars = all_chars.symmetric_difference(chars_used)
67
 
        self.assertEqual(set('\n'), unused_chars)
 
70
        self.assertEqual({b'\n'}, unused_chars)
68
71
 
69
72
 
70
73
class TestDeserialiseLeafNode(tests.TestCase):
73
76
 
74
77
    def assertDeserialiseErrors(self, text):
75
78
        self.assertRaises((ValueError, IndexError),
76
 
            self.module._deserialise_leaf_node, text, 'not-a-real-sha')
 
79
            self.module._deserialise_leaf_node, text, b'not-a-real-sha')
77
80
 
78
81
    def test_raises_on_non_leaf(self):
79
 
        self.assertDeserialiseErrors('')
80
 
        self.assertDeserialiseErrors('short\n')
81
 
        self.assertDeserialiseErrors('chknotleaf:\n')
82
 
        self.assertDeserialiseErrors('chkleaf:x\n')
83
 
        self.assertDeserialiseErrors('chkleaf:\n')
84
 
        self.assertDeserialiseErrors('chkleaf:\nnotint\n')
85
 
        self.assertDeserialiseErrors('chkleaf:\n10\n')
86
 
        self.assertDeserialiseErrors('chkleaf:\n10\n256\n')
87
 
        self.assertDeserialiseErrors('chkleaf:\n10\n256\n10\n')
 
82
        self.assertDeserialiseErrors(b'')
 
83
        self.assertDeserialiseErrors(b'short\n')
 
84
        self.assertDeserialiseErrors(b'chknotleaf:\n')
 
85
        self.assertDeserialiseErrors(b'chkleaf:x\n')
 
86
        self.assertDeserialiseErrors(b'chkleaf:\n')
 
87
        self.assertDeserialiseErrors(b'chkleaf:\nnotint\n')
 
88
        self.assertDeserialiseErrors(b'chkleaf:\n10\n')
 
89
        self.assertDeserialiseErrors(b'chkleaf:\n10\n256\n')
 
90
        self.assertDeserialiseErrors(b'chkleaf:\n10\n256\n10\n')
88
91
 
89
92
    def test_deserialise_empty(self):
90
93
        node = self.module._deserialise_leaf_node(
91
 
            "chkleaf:\n10\n1\n0\n\n", stuple("sha1:1234",))
 
94
            b"chkleaf:\n10\n1\n0\n\n", stuple(b"sha1:1234",))
92
95
        self.assertEqual(0, len(node))
93
96
        self.assertEqual(10, node.maximum_size)
94
 
        self.assertEqual(("sha1:1234",), node.key())
 
97
        self.assertEqual((b"sha1:1234",), node.key())
95
98
        self.assertIsInstance(node.key(), StaticTuple)
96
99
        self.assertIs(None, node._search_prefix)
97
100
        self.assertIs(None, node._common_serialised_prefix)
98
101
 
99
102
    def test_deserialise_items(self):
100
103
        node = self.module._deserialise_leaf_node(
101
 
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
102
 
            ("sha1:1234",))
 
104
            b"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
 
105
            (b"sha1:1234",))
103
106
        self.assertEqual(2, len(node))
104
 
        self.assertEqual([(("foo bar",), "baz"), (("quux",), "blarh")],
 
107
        self.assertEqual([((b"foo bar",), b"baz"), ((b"quux",), b"blarh")],
105
108
            sorted(node.iteritems(None)))
106
109
 
107
110
    def test_deserialise_item_with_null_width_1(self):
108
111
        node = self.module._deserialise_leaf_node(
109
 
            "chkleaf:\n0\n1\n2\n\nfoo\x001\nbar\x00baz\nquux\x001\nblarh\n",
110
 
            ("sha1:1234",))
 
112
            b"chkleaf:\n0\n1\n2\n\nfoo\x001\nbar\x00baz\nquux\x001\nblarh\n",
 
113
            (b"sha1:1234",))
111
114
        self.assertEqual(2, len(node))
112
 
        self.assertEqual([(("foo",), "bar\x00baz"), (("quux",), "blarh")],
 
115
        self.assertEqual([((b"foo",), b"bar\x00baz"), ((b"quux",), b"blarh")],
113
116
            sorted(node.iteritems(None)))
114
117
 
115
118
    def test_deserialise_item_with_null_width_2(self):
116
119
        node = self.module._deserialise_leaf_node(
117
 
            "chkleaf:\n0\n2\n2\n\nfoo\x001\x001\nbar\x00baz\n"
118
 
            "quux\x00\x001\nblarh\n",
119
 
            ("sha1:1234",))
 
120
            b"chkleaf:\n0\n2\n2\n\nfoo\x001\x001\nbar\x00baz\n"
 
121
            b"quux\x00\x001\nblarh\n",
 
122
            (b"sha1:1234",))
120
123
        self.assertEqual(2, len(node))
121
 
        self.assertEqual([(("foo", "1"), "bar\x00baz"), (("quux", ""), "blarh")],
 
124
        self.assertEqual([((b"foo", b"1"), b"bar\x00baz"), ((b"quux", b""), b"blarh")],
122
125
            sorted(node.iteritems(None)))
123
126
 
124
127
    def test_iteritems_selected_one_of_two_items(self):
125
128
        node = self.module._deserialise_leaf_node(
126
 
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
127
 
            ("sha1:1234",))
 
129
            b"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
 
130
            (b"sha1:1234",))
128
131
        self.assertEqual(2, len(node))
129
 
        self.assertEqual([(("quux",), "blarh")],
130
 
            sorted(node.iteritems(None, [("quux",), ("qaz",)])))
 
132
        self.assertEqual([((b"quux",), b"blarh")],
 
133
            sorted(node.iteritems(None, [(b"quux",), (b"qaz",)])))
131
134
 
132
135
    def test_deserialise_item_with_common_prefix(self):
133
136
        node = self.module._deserialise_leaf_node(
134
 
            "chkleaf:\n0\n2\n2\nfoo\x00\n1\x001\nbar\x00baz\n2\x001\nblarh\n",
135
 
            ("sha1:1234",))
 
137
            b"chkleaf:\n0\n2\n2\nfoo\x00\n1\x001\nbar\x00baz\n2\x001\nblarh\n",
 
138
            (b"sha1:1234",))
136
139
        self.assertEqual(2, len(node))
137
 
        self.assertEqual([(("foo", "1"), "bar\x00baz"), (("foo", "2"), "blarh")],
 
140
        self.assertEqual([((b"foo", b"1"), b"bar\x00baz"), ((b"foo", b"2"), b"blarh")],
138
141
            sorted(node.iteritems(None)))
139
142
        self.assertIs(chk_map._unknown, node._search_prefix)
140
 
        self.assertEqual('foo\x00', node._common_serialised_prefix)
 
143
        self.assertEqual(b'foo\x00', node._common_serialised_prefix)
141
144
 
142
145
    def test_deserialise_multi_line(self):
143
146
        node = self.module._deserialise_leaf_node(
144
 
            "chkleaf:\n0\n2\n2\nfoo\x00\n1\x002\nbar\nbaz\n2\x002\nblarh\n\n",
145
 
            ("sha1:1234",))
 
147
            b"chkleaf:\n0\n2\n2\nfoo\x00\n1\x002\nbar\nbaz\n2\x002\nblarh\n\n",
 
148
            (b"sha1:1234",))
146
149
        self.assertEqual(2, len(node))
147
 
        self.assertEqual([(("foo", "1"), "bar\nbaz"),
148
 
                          (("foo", "2"), "blarh\n"),
 
150
        self.assertEqual([((b"foo", b"1"), b"bar\nbaz"),
 
151
                          ((b"foo", b"2"), b"blarh\n"),
149
152
                         ], sorted(node.iteritems(None)))
150
153
        self.assertIs(chk_map._unknown, node._search_prefix)
151
 
        self.assertEqual('foo\x00', node._common_serialised_prefix)
 
154
        self.assertEqual(b'foo\x00', node._common_serialised_prefix)
152
155
 
153
156
    def test_key_after_map(self):
154
157
        node = self.module._deserialise_leaf_node(
155
 
            "chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
156
 
        node.map(None, ("foo bar",), "baz quux")
 
158
            b"chkleaf:\n10\n1\n0\n\n", (b"sha1:1234",))
 
159
        node.map(None, (b"foo bar",), b"baz quux")
157
160
        self.assertEqual(None, node.key())
158
161
 
159
162
    def test_key_after_unmap(self):
160
163
        node = self.module._deserialise_leaf_node(
161
 
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
162
 
            ("sha1:1234",))
163
 
        node.unmap(None, ("foo bar",))
 
164
            b"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
 
165
            (b"sha1:1234",))
 
166
        node.unmap(None, (b"foo bar",))
164
167
        self.assertEqual(None, node.key())
165
168
 
166
169
 
171
174
    def assertDeserialiseErrors(self, text):
172
175
        self.assertRaises((ValueError, IndexError),
173
176
            self.module._deserialise_internal_node, text,
174
 
                stuple('not-a-real-sha',))
 
177
                stuple(b'not-a-real-sha',))
175
178
 
176
179
    def test_raises_on_non_internal(self):
177
 
        self.assertDeserialiseErrors('')
178
 
        self.assertDeserialiseErrors('short\n')
179
 
        self.assertDeserialiseErrors('chknotnode:\n')
180
 
        self.assertDeserialiseErrors('chknode:x\n')
181
 
        self.assertDeserialiseErrors('chknode:\n')
182
 
        self.assertDeserialiseErrors('chknode:\nnotint\n')
183
 
        self.assertDeserialiseErrors('chknode:\n10\n')
184
 
        self.assertDeserialiseErrors('chknode:\n10\n256\n')
185
 
        self.assertDeserialiseErrors('chknode:\n10\n256\n10\n')
 
180
        self.assertDeserialiseErrors(b'')
 
181
        self.assertDeserialiseErrors(b'short\n')
 
182
        self.assertDeserialiseErrors(b'chknotnode:\n')
 
183
        self.assertDeserialiseErrors(b'chknode:x\n')
 
184
        self.assertDeserialiseErrors(b'chknode:\n')
 
185
        self.assertDeserialiseErrors(b'chknode:\nnotint\n')
 
186
        self.assertDeserialiseErrors(b'chknode:\n10\n')
 
187
        self.assertDeserialiseErrors(b'chknode:\n10\n256\n')
 
188
        self.assertDeserialiseErrors(b'chknode:\n10\n256\n10\n')
186
189
        # no trailing newline
187
 
        self.assertDeserialiseErrors('chknode:\n10\n256\n0\n1\nfo')
 
190
        self.assertDeserialiseErrors(b'chknode:\n10\n256\n0\n1\nfo')
188
191
 
189
192
    def test_deserialise_one(self):
190
193
        node = self.module._deserialise_internal_node(
191
 
            "chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple('sha1:1234',))
 
194
            b"chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple(b'sha1:1234',))
192
195
        self.assertIsInstance(node, chk_map.InternalNode)
193
196
        self.assertEqual(1, len(node))
194
197
        self.assertEqual(10, node.maximum_size)
195
 
        self.assertEqual(("sha1:1234",), node.key())
196
 
        self.assertEqual('', node._search_prefix)
197
 
        self.assertEqual({'a': ('sha1:abcd',)}, node._items)
 
198
        self.assertEqual((b"sha1:1234",), node.key())
 
199
        self.assertEqual(b'', node._search_prefix)
 
200
        self.assertEqual({b'a': (b'sha1:abcd',)}, node._items)
198
201
 
199
202
    def test_deserialise_with_prefix(self):
200
203
        node = self.module._deserialise_internal_node(
201
 
            "chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", stuple('sha1:1234',))
 
204
            b"chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n",
 
205
            stuple(b'sha1:1234',))
202
206
        self.assertIsInstance(node, chk_map.InternalNode)
203
207
        self.assertEqual(1, len(node))
204
208
        self.assertEqual(10, node.maximum_size)
205
 
        self.assertEqual(("sha1:1234",), node.key())
206
 
        self.assertEqual('pref', node._search_prefix)
207
 
        self.assertEqual({'prefa': ('sha1:abcd',)}, node._items)
 
209
        self.assertEqual((b"sha1:1234",), node.key())
 
210
        self.assertEqual(b'pref', node._search_prefix)
 
211
        self.assertEqual({b'prefa': (b'sha1:abcd',)}, node._items)
208
212
 
209
213
        node = self.module._deserialise_internal_node(
210
 
            "chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", stuple('sha1:1234',))
 
214
            b"chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n",
 
215
            stuple(b'sha1:1234',))
211
216
        self.assertIsInstance(node, chk_map.InternalNode)
212
217
        self.assertEqual(1, len(node))
213
218
        self.assertEqual(10, node.maximum_size)
214
 
        self.assertEqual(("sha1:1234",), node.key())
215
 
        self.assertEqual('pref', node._search_prefix)
216
 
        self.assertEqual({'pref': ('sha1:abcd',)}, node._items)
 
219
        self.assertEqual((b"sha1:1234",), node.key())
 
220
        self.assertEqual(b'pref', node._search_prefix)
 
221
        self.assertEqual({b'pref': (b'sha1:abcd',)}, node._items)
217
222
 
218
223
    def test_deserialise_pref_with_null(self):
219
224
        node = self.module._deserialise_internal_node(
220
 
            "chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
221
 
            stuple('sha1:1234',))
 
225
            b"chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
 
226
            stuple(b'sha1:1234',))
222
227
        self.assertIsInstance(node, chk_map.InternalNode)
223
228
        self.assertEqual(1, len(node))
224
229
        self.assertEqual(10, node.maximum_size)
225
 
        self.assertEqual(("sha1:1234",), node.key())
226
 
        self.assertEqual('pref\x00fo', node._search_prefix)
227
 
        self.assertEqual({'pref\x00fo': ('sha1:abcd',)}, node._items)
 
230
        self.assertEqual((b"sha1:1234",), node.key())
 
231
        self.assertEqual(b'pref\x00fo', node._search_prefix)
 
232
        self.assertEqual({b'pref\x00fo': (b'sha1:abcd',)}, node._items)
228
233
 
229
234
    def test_deserialise_with_null_pref(self):
230
235
        node = self.module._deserialise_internal_node(
231
 
            "chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
232
 
            stuple('sha1:1234',))
 
236
            b"chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
 
237
            stuple(b'sha1:1234',))
233
238
        self.assertIsInstance(node, chk_map.InternalNode)
234
239
        self.assertEqual(1, len(node))
235
240
        self.assertEqual(10, node.maximum_size)
236
 
        self.assertEqual(("sha1:1234",), node.key())
237
 
        self.assertEqual('pref\x00fo', node._search_prefix)
238
 
        self.assertEqual({'pref\x00fo\x00': ('sha1:abcd',)}, node._items)
 
241
        self.assertEqual((b"sha1:1234",), node.key())
 
242
        self.assertEqual(b'pref\x00fo', node._search_prefix)
 
243
        self.assertEqual({b'pref\x00fo\x00': (b'sha1:abcd',)}, node._items)
 
244
 
 
245
 
 
246
class Test_BytesToTextKey(tests.TestCase):
 
247
 
 
248
    def assertBytesToTextKey(self, key, bytes):
 
249
        self.assertEqual(key,
 
250
                         self.module._bytes_to_text_key(bytes))
 
251
 
 
252
    def assertBytesToTextKeyRaises(self, bytes):
 
253
        # These are invalid bytes, and we want to make sure the code under test
 
254
        # raises an exception rather than segfaults, etc. We don't particularly
 
255
        # care what exception.
 
256
        self.assertRaises(Exception, self.module._bytes_to_text_key, bytes)
 
257
 
 
258
    def test_file(self):
 
259
        self.assertBytesToTextKey((b'file-id', b'revision-id'),
 
260
                 b'file: file-id\nparent-id\nname\nrevision-id\n'
 
261
                 b'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
 
262
 
 
263
    def test_invalid_no_kind(self):
 
264
        self.assertBytesToTextKeyRaises(
 
265
                 b'file  file-id\nparent-id\nname\nrevision-id\n'
 
266
                 b'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
 
267
 
 
268
    def test_invalid_no_space(self):
 
269
        self.assertBytesToTextKeyRaises(
 
270
                 b'file:file-id\nparent-id\nname\nrevision-id\n'
 
271
                 b'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
 
272
 
 
273
    def test_invalid_too_short_file_id(self):
 
274
        self.assertBytesToTextKeyRaises(b'file:file-id')
 
275
 
 
276
    def test_invalid_too_short_parent_id(self):
 
277
        self.assertBytesToTextKeyRaises(b'file:file-id\nparent-id')
 
278
 
 
279
    def test_invalid_too_short_name(self):
 
280
        self.assertBytesToTextKeyRaises(b'file:file-id\nparent-id\nname')
 
281
 
 
282
    def test_dir(self):
 
283
        self.assertBytesToTextKey((b'dir-id', b'revision-id'),
 
284
                 b'dir: dir-id\nparent-id\nname\nrevision-id')