/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
#
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
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
#
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
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
#
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
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
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
16
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
17
"""Test Store implementations."""
18
1185.1.46 by Robert Collins
Aarons branch --basis patch
19
import os
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
20
import gzip
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
21
6686.2.1 by Jelmer Vernooij
Move breezy.store to breezy.plugins.weave_fmt, its only user.
22
from ... import errors as errors
23
from ...errors import BzrError
24
from ...sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
25
    BytesIO,
26
    )
6686.2.1 by Jelmer Vernooij
Move breezy.store to breezy.plugins.weave_fmt, its only user.
27
from .store import TransportStore
28
from .store.text import TextStore
29
from .store.versioned import VersionedFileStore
30
from ...tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
31
from ... import transactions
32
from ... import transport
33
from ...transport.memory import MemoryTransport
6670.4.18 by Jelmer Vernooij
Merge move-store.
34
from ...bzr.weave import WeaveFile
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
35
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
36
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
37
class TestStores(object):
1185.16.146 by Martin Pool
Fix up assert with sideeffects in CompressedTextStore._copy_one
38
    """Mixin template class that provides some common tests for stores"""
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
39
40
    def check_content(self, store, fileid, value):
1442.1.35 by Robert Collins
convert all users of __getitem__ into TransportStores to use .get instead
41
        f = store.get(fileid)
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
42
        self.assertEqual(f.read(), value)
43
44
    def fill_store(self, store):
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
45
        store.add(BytesIO(b'hello'), b'a')
46
        store.add(BytesIO(b'other'), b'b')
47
        store.add(BytesIO(b'something'), b'c')
48
        store.add(BytesIO(b'goodbye'), b'123123')
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
49
50
    def test_get(self):
51
        store = self.get_store()
52
        self.fill_store(store)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
53
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
54
        self.check_content(store, b'a', b'hello')
55
        self.check_content(store, b'b', b'other')
56
        self.check_content(store, b'c', b'something')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
57
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
58
        # Make sure that requesting a non-existing file fails
6855.3.1 by Jelmer Vernooij
Several more fixes.
59
        self.assertRaises(KeyError, self.check_content, store, b'd', None)
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
60
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
61
    def test_multiple_add(self):
62
        """Multiple add with same ID should raise a BzrError"""
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
63
        store = self.get_store()
64
        self.fill_store(store)
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
65
        self.assertRaises(BzrError, store.add, BytesIO(b'goodbye'), b'123123')
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
66
67
68
class TestCompressedTextStore(TestCaseInTempDir, TestStores):
69
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
70
    def get_store(self, path=u'.'):
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
71
        t = transport.get_transport_from_path(path)
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
72
        return TextStore(t, compressed=True)
1185.11.1 by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet.
73
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
74
    def test_total_size(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
75
        store = self.get_store(u'.')
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
76
        store.register_suffix('dsc')
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
77
        store.add(BytesIO(b'goodbye'), b'123123')
78
        store.add(BytesIO(b'goodbye2'), b'123123', 'dsc')
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
79
        # these get gzipped - content should be stable
80
        self.assertEqual(store.total_size(), (2, 55))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
81
1442.1.32 by Robert Collins
Teach CompressedTextStore._relpath to support file suffixes too.
82
    def test__relpath_suffixed(self):
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
83
        my_store = TextStore(MockTransport(),
84
                             prefixed=True, compressed=True)
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
85
        my_store.register_suffix('dsc')
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
86
        self.assertEqual('45/foo.dsc', my_store._relpath(b'foo', ['dsc']))
1442.1.32 by Robert Collins
Teach CompressedTextStore._relpath to support file suffixes too.
87
1404 by Robert Collins
only pull remote text weaves once per fetch operation
88
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
89
class TestMemoryStore(TestCase):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
90
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
91
    def get_store(self):
1651.1.7 by Martin Pool
Move small ImmutableMemoryStore class into test module,
92
        return TextStore(MemoryTransport())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
93
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
94
    def test_add_and_retrieve(self):
95
        store = self.get_store()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
96
        store.add(BytesIO(b'hello'), b'aa')
97
        self.assertNotEqual(store.get(b'aa'), None)
98
        self.assertEqual(store.get(b'aa').read(), b'hello')
99
        store.add(BytesIO(b'hello world'), b'bb')
100
        self.assertNotEqual(store.get(b'bb'), None)
101
        self.assertEqual(store.get(b'bb').read(), b'hello world')
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
102
103
    def test_missing_is_absent(self):
104
        store = self.get_store()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
105
        self.assertNotIn(b'aa', store)
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
106
107
    def test_adding_fails_when_present(self):
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
108
        my_store = self.get_store()
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
109
        my_store.add(BytesIO(b'hello'), b'aa')
1442.1.44 by Robert Collins
Many transport related tweaks:
110
        self.assertRaises(BzrError,
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
111
                          my_store.add, BytesIO(b'hello'), b'aa')
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
112
113
    def test_total_size(self):
114
        store = self.get_store()
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
115
        store.add(BytesIO(b'goodbye'), b'123123')
116
        store.add(BytesIO(b'goodbye2'), b'123123.dsc')
1092.2.1 by Robert Collins
minor refactors to store, create an ImmutableMemoryStore for testing or other such operations
117
        self.assertEqual(store.total_size(), (2, 15))
1393.2.1 by John Arbash Meinel
Merged in split-storage-2 branch. Need to cleanup a little bit more still.
118
        # TODO: Switch the exception form UnlistableStore to
119
        #       or make Stores throw UnlistableStore if their
120
        #       Transport doesn't support listing
121
        # store_c = RemoteStore('http://example.com/')
122
        # self.assertRaises(UnlistableStore, copy_all, store_c, store_b)
123
1404 by Robert Collins
only pull remote text weaves once per fetch operation
124
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
125
class TestTextStore(TestCaseInTempDir, TestStores):
126
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
127
    def get_store(self, path=u'.'):
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
128
        t = transport.get_transport_from_path(path)
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
129
        return TextStore(t, compressed=False)
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
130
131
    def test_total_size(self):
132
        store = self.get_store()
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
133
        store.add(BytesIO(b'goodbye'), b'123123')
134
        store.add(BytesIO(b'goodbye2'), b'123123.dsc')
1442.1.34 by Robert Collins
Remove the Store.get multiple-get interface which was not used, and remove much spurious duplication in teststore.py.
135
        self.assertEqual(store.total_size(), (2, 15))
1393.2.2 by John Arbash Meinel
Updated stores to use Transport
136
        # TODO: Switch the exception form UnlistableStore to
137
        #       or make Stores throw UnlistableStore if their
138
        #       Transport doesn't support listing
139
        # store_c = RemoteStore('http://example.com/')
140
        # self.assertRaises(UnlistableStore, copy_all, store_c, store_b)
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
141
142
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
143
class TestMixedTextStore(TestCaseInTempDir, TestStores):
144
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
145
    def get_store(self, path=u'.', compressed=True):
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
146
        t = transport.get_transport_from_path(path)
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
147
        return TextStore(t, compressed=compressed)
148
149
    def test_get_mixed(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
150
        cs = self.get_store(u'.', compressed=True)
151
        s = self.get_store(u'.', compressed=False)
6973.7.5 by Jelmer Vernooij
s/file/open.
152
        cs.add(BytesIO(b'hello there'), b'a')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
153
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
154
        self.assertPathExists('a.gz')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
155
        self.assertFalse(os.path.lexists('a'))
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
156
6973.7.5 by Jelmer Vernooij
s/file/open.
157
        with gzip.GzipFile('a.gz') as f:
158
            self.assertEqual(f.read(), b'hello there')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
159
6973.7.5 by Jelmer Vernooij
s/file/open.
160
        self.assertEqual(cs.has_id(b'a'), True)
161
        self.assertEqual(s.has_id(b'a'), True)
162
        self.assertEqual(cs.get(b'a').read(), b'hello there')
163
        self.assertEqual(s.get(b'a').read(), b'hello there')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
164
6973.7.6 by Jelmer Vernooij
Fix more tests.
165
        self.assertRaises(BzrError, s.add, BytesIO(b'goodbye'), b'a')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
166
6973.7.6 by Jelmer Vernooij
Fix more tests.
167
        s.add(BytesIO(b'goodbye'), b'b')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
168
        self.assertPathExists('b')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
169
        self.assertFalse(os.path.lexists('b.gz'))
6973.7.5 by Jelmer Vernooij
s/file/open.
170
        with open('b', 'rb') as f:
171
            self.assertEqual(f.read(), b'goodbye')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
172
6973.7.5 by Jelmer Vernooij
s/file/open.
173
        self.assertEqual(cs.has_id(b'b'), True)
174
        self.assertEqual(s.has_id(b'b'), True)
175
        self.assertEqual(cs.get(b'b').read(), b'goodbye')
176
        self.assertEqual(s.get(b'b').read(), b'goodbye')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
177
6973.7.6 by Jelmer Vernooij
Fix more tests.
178
        self.assertRaises(BzrError, cs.add, BytesIO(b'again'), b'b')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
179
6973.7.5 by Jelmer Vernooij
s/file/open.
180
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
181
class MockTransport(transport.Transport):
182
    """A fake transport for testing with."""
183
1442.1.30 by Robert Collins
Add stuf has and mkdir to MockTransport to enable testing store adds
184
    def has(self, filename):
185
        return False
186
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
187
    def __init__(self, url=None):
188
        if url is None:
189
            url = "http://example.com"
190
        super(MockTransport, self).__init__(url)
191
1442.1.30 by Robert Collins
Add stuf has and mkdir to MockTransport to enable testing store adds
192
    def mkdir(self, filename):
193
        return
194
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
195
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
196
class InstrumentedTransportStore(TransportStore):
1442.1.29 by Robert Collins
create an instrumented transport store for testing common logic.
197
    """An instrumented TransportStore.
198
199
    Here we replace template method worker methods with calls that record the
200
    expected results.
201
    """
202
203
    def _add(self, filename, file):
204
        self._calls.append(("_add", filename, file))
205
206
    def __init__(self, transport, prefixed=False):
207
        super(InstrumentedTransportStore, self).__init__(transport, prefixed)
208
        self._calls = []
209
210
211
class TestInstrumentedTransportStore(TestCase):
212
213
    def test__add_records(self):
214
        my_store = InstrumentedTransportStore(MockTransport())
1442.1.30 by Robert Collins
Add stuf has and mkdir to MockTransport to enable testing store adds
215
        my_store._add("filename", "file")
1442.1.29 by Robert Collins
create an instrumented transport store for testing common logic.
216
        self.assertEqual([("_add", "filename", "file")], my_store._calls)
217
218
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
219
class TestMockTransport(TestCase):
220
221
    def test_isinstance(self):
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
222
        self.assertIsInstance(MockTransport(), transport.Transport)
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
223
1442.1.30 by Robert Collins
Add stuf has and mkdir to MockTransport to enable testing store adds
224
    def test_has(self):
225
        self.assertEqual(False, MockTransport().has('foo'))
226
227
    def test_mkdir(self):
228
        MockTransport().mkdir('45')
229
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
230
231
class TestTransportStore(TestCase):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
232
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
233
    def test__relpath_invalid(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
234
        my_store = TransportStore(MockTransport())
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
235
        self.assertRaises(ValueError, my_store._relpath, b'/foo')
236
        self.assertRaises(ValueError, my_store._relpath, b'foo/')
1442.1.24 by Robert Collins
Pull up _check_id and _relpath from Text and CompressedText stores into TransportStore
237
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
238
    def test_register_invalid_suffixes(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
239
        my_store = TransportStore(MockTransport())
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
240
        self.assertRaises(ValueError, my_store.register_suffix, '/')
241
        self.assertRaises(ValueError, my_store.register_suffix, '.gz/bar')
242
243
    def test__relpath_unregister_suffixes(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
244
        my_store = TransportStore(MockTransport())
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
245
        self.assertRaises(ValueError, my_store._relpath, b'foo', [b'gz'])
7143.15.2 by Jelmer Vernooij
Run autopep8.
246
        self.assertRaises(ValueError, my_store._relpath,
247
                          b'foo', [b'dsc', b'gz'])
1442.1.27 by Robert Collins
Check that file suffixes in TransportStore are also valid
248
1442.1.25 by Robert Collins
Test TransportStore._relpath for simple cases: pull up _prefixed attribute as a result.
249
    def test__relpath_simple(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
250
        my_store = TransportStore(MockTransport())
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
251
        self.assertEqual("foo", my_store._relpath(b'foo'))
1442.1.26 by Robert Collins
Pull up _relpath with gz suffix for CompressedTextStore into TransportStore
252
253
    def test__relpath_prefixed(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
254
        my_store = TransportStore(MockTransport(), True)
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
255
        self.assertEqual('45/foo', my_store._relpath(b'foo'))
1442.1.26 by Robert Collins
Pull up _relpath with gz suffix for CompressedTextStore into TransportStore
256
257
    def test__relpath_simple_suffixed(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
258
        my_store = TransportStore(MockTransport())
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
259
        my_store.register_suffix('bar')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
260
        my_store.register_suffix('baz')
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
261
        self.assertEqual('foo.baz', my_store._relpath(b'foo', ['baz']))
7143.15.2 by Jelmer Vernooij
Run autopep8.
262
        self.assertEqual('foo.bar.baz', my_store._relpath(
263
            b'foo', ['bar', 'baz']))
1442.1.27 by Robert Collins
Check that file suffixes in TransportStore are also valid
264
265
    def test__relpath_prefixed_suffixed(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
266
        my_store = TransportStore(MockTransport(), True)
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
267
        my_store.register_suffix('bar')
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
268
        my_store.register_suffix('baz')
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
269
        self.assertEqual('45/foo.baz', my_store._relpath(b'foo', ['baz']))
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
270
        self.assertEqual('45/foo.bar.baz',
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
271
                         my_store._relpath(b'foo', ['bar', 'baz']))
1442.1.27 by Robert Collins
Check that file suffixes in TransportStore are also valid
272
1442.1.31 by Robert Collins
test that TransportStore.add calls _add appropriately.
273
    def test_add_simple(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
274
        stream = BytesIO(b"content")
1442.1.31 by Robert Collins
test that TransportStore.add calls _add appropriately.
275
        my_store = InstrumentedTransportStore(MockTransport())
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
276
        my_store.add(stream, b"foo")
1442.1.31 by Robert Collins
test that TransportStore.add calls _add appropriately.
277
        self.assertEqual([("_add", "foo", stream)], my_store._calls)
278
279
    def test_add_prefixed(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
280
        stream = BytesIO(b"content")
1442.1.31 by Robert Collins
test that TransportStore.add calls _add appropriately.
281
        my_store = InstrumentedTransportStore(MockTransport(), True)
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
282
        my_store.add(stream, b"foo")
1442.1.31 by Robert Collins
test that TransportStore.add calls _add appropriately.
283
        self.assertEqual([("_add", "45/foo", stream)], my_store._calls)
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
284
285
    def test_add_simple_suffixed(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
286
        stream = BytesIO(b"content")
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
287
        my_store = InstrumentedTransportStore(MockTransport())
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
288
        my_store.register_suffix('dsc')
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
289
        my_store.add(stream, b"foo", b'dsc')
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
290
        self.assertEqual([("_add", "foo.dsc", stream)], my_store._calls)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
291
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
292
    def test_add_simple_suffixed(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
293
        stream = BytesIO(b"content")
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
294
        my_store = InstrumentedTransportStore(MockTransport(), True)
1442.1.43 by Robert Collins
add registration of suffixes, in preparation for ensuring iteration is regular
295
        my_store.register_suffix('dsc')
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
296
        my_store.add(stream, b"foo", 'dsc')
1442.1.33 by Robert Collins
teach TransportStore.add to accept an optional file suffix, which does not alter the fileid.
297
        self.assertEqual([("_add", "45/foo.dsc", stream)], my_store._calls)
1442.1.46 by Robert Collins
test simple has_id
298
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
299
    def get_populated_store(self, prefixed=False,
7143.15.2 by Jelmer Vernooij
Run autopep8.
300
                            store_class=TextStore, compressed=False):
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
301
        my_store = store_class(MemoryTransport(), prefixed,
302
                               compressed=compressed)
1442.1.46 by Robert Collins
test simple has_id
303
        my_store.register_suffix('sig')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
304
        stream = BytesIO(b"signature")
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
305
        my_store.add(stream, b"foo", 'sig')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
306
        stream = BytesIO(b"content")
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
307
        my_store.add(stream, b"foo")
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
308
        stream = BytesIO(b"signature for missing base")
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
309
        my_store.add(stream, b"missing", 'sig')
1442.1.46 by Robert Collins
test simple has_id
310
        return my_store
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
311
1442.1.46 by Robert Collins
test simple has_id
312
    def test_has_simple(self):
313
        my_store = self.get_populated_store()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
314
        self.assertEqual(True, my_store.has_id(b'foo'))
1442.1.47 by Robert Collins
test for has with suffixed files
315
        my_store = self.get_populated_store(True)
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
316
        self.assertEqual(True, my_store.has_id(b'foo'))
1442.1.47 by Robert Collins
test for has with suffixed files
317
318
    def test_has_suffixed(self):
319
        my_store = self.get_populated_store()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
320
        self.assertEqual(True, my_store.has_id(b'foo', 'sig'))
1442.1.47 by Robert Collins
test for has with suffixed files
321
        my_store = self.get_populated_store(True)
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
322
        self.assertEqual(True, my_store.has_id(b'foo', 'sig'))
1442.1.48 by Robert Collins
test that the presence of a signature does not make a missing base file magically appear present
323
324
    def test_has_suffixed_no_base(self):
325
        my_store = self.get_populated_store()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
326
        self.assertEqual(False, my_store.has_id(b'missing'))
1442.1.48 by Robert Collins
test that the presence of a signature does not make a missing base file magically appear present
327
        my_store = self.get_populated_store(True)
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
328
        self.assertEqual(False, my_store.has_id(b'missing'))
1442.1.50 by Robert Collins
test get with suffixes
329
330
    def test_get_simple(self):
331
        my_store = self.get_populated_store()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
332
        self.assertEqual(b'content', my_store.get(b'foo').read())
1442.1.50 by Robert Collins
test get with suffixes
333
        my_store = self.get_populated_store(True)
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
334
        self.assertEqual(b'content', my_store.get(b'foo').read())
1442.1.50 by Robert Collins
test get with suffixes
335
336
    def test_get_suffixed(self):
337
        my_store = self.get_populated_store()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
338
        self.assertEqual(b'signature', my_store.get(b'foo', 'sig').read())
1442.1.50 by Robert Collins
test get with suffixes
339
        my_store = self.get_populated_store(True)
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
340
        self.assertEqual(b'signature', my_store.get(b'foo', 'sig').read())
1442.1.50 by Robert Collins
test get with suffixes
341
342
    def test_get_suffixed_no_base(self):
343
        my_store = self.get_populated_store()
6855.3.1 by Jelmer Vernooij
Several more fixes.
344
        self.assertEqual(b'signature for missing base',
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
345
                         my_store.get(b'missing', 'sig').read())
1442.1.50 by Robert Collins
test get with suffixes
346
        my_store = self.get_populated_store(True)
6855.3.1 by Jelmer Vernooij
Several more fixes.
347
        self.assertEqual(b'signature for missing base',
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
348
                         my_store.get(b'missing', 'sig').read())
1442.1.51 by Robert Collins
teach iter about suffixes
349
350
    def test___iter__no_suffix(self):
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
351
        my_store = TextStore(MemoryTransport(),
352
                             prefixed=False, compressed=False)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
353
        stream = BytesIO(b"content")
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
354
        my_store.add(stream, b"foo")
355
        self.assertEqual({b'foo'},
1442.1.51 by Robert Collins
teach iter about suffixes
356
                         set(my_store.__iter__()))
357
358
    def test___iter__(self):
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
359
        self.assertEqual({b'foo'},
1442.1.51 by Robert Collins
teach iter about suffixes
360
                         set(self.get_populated_store().__iter__()))
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
361
        self.assertEqual({b'foo'},
1442.1.51 by Robert Collins
teach iter about suffixes
362
                         set(self.get_populated_store(True).__iter__()))
363
364
    def test___iter__compressed(self):
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
365
        self.assertEqual({b'foo'},
1442.1.51 by Robert Collins
teach iter about suffixes
366
                         set(self.get_populated_store(
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
367
                             compressed=True).__iter__()))
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
368
        self.assertEqual({b'foo'},
1442.1.51 by Robert Collins
teach iter about suffixes
369
                         set(self.get_populated_store(
1185.16.157 by John Arbash Meinel
Added ability for TextStore to handle both compressed and uncompressed, it just looks for one type first
370
                             True, compressed=True).__iter__()))
1442.1.51 by Robert Collins
teach iter about suffixes
371
372
    def test___len__(self):
373
        self.assertEqual(1, len(self.get_populated_store()))
1442.1.54 by Robert Collins
Teach store.copy_all about fileid suffixes
374
1469 by Robert Collins
Change Transport.* to work with URL's.
375
    def test_relpath_escaped(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
376
        my_store = TransportStore(MemoryTransport())
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
377
        self.assertEqual('%25', my_store._relpath(b'%'))
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
378
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
379
    def test_escaped_uppercase(self):
380
        """Uppercase letters are escaped for safety on Windows"""
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
381
        my_store = TransportStore(MemoryTransport(), prefixed=True,
7143.15.2 by Jelmer Vernooij
Run autopep8.
382
                                  escaped=True)
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
383
        # a particularly perverse file-id! :-)
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
384
        self.assertEqual(my_store._relpath(b'C:<>'), 'be/%2543%253a%253c%253e')
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
385
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
386
387
class TestVersionFileStore(TestCaseWithTransport):
388
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
389
    def get_scope(self):
390
        return self._transaction
391
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
392
    def setUp(self):
393
        super(TestVersionFileStore, self).setUp()
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
394
        self.vfstore = VersionedFileStore(MemoryTransport(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
395
                                          versionedfile_class=WeaveFile)
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
396
        self.vfstore.get_scope = self.get_scope
397
        self._transaction = None
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
398
399
    def test_get_weave_registers_dirty_in_write(self):
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
400
        self._transaction = transactions.WriteTransaction()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
401
        vf = self.vfstore.get_weave_or_empty(b'id', self._transaction)
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
402
        self._transaction.finish()
403
        self._transaction = None
7143.15.2 by Jelmer Vernooij
Run autopep8.
404
        self.assertRaises(errors.OutSideTransaction,
405
                          vf.add_lines, b'b', [], [])
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
406
        self._transaction = transactions.WriteTransaction()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
407
        vf = self.vfstore.get_weave(b'id', self._transaction)
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
408
        self._transaction.finish()
409
        self._transaction = None
7143.15.2 by Jelmer Vernooij
Run autopep8.
410
        self.assertRaises(errors.OutSideTransaction,
411
                          vf.add_lines, b'b', [], [])
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
412
413
    def test_get_weave_readonly_cant_write(self):
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
414
        self._transaction = transactions.WriteTransaction()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
415
        vf = self.vfstore.get_weave_or_empty(b'id', self._transaction)
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
416
        self._transaction.finish()
417
        self._transaction = transactions.ReadOnlyTransaction()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
418
        vf = self.vfstore.get_weave_or_empty(b'id', self._transaction)
419
        self.assertRaises(errors.ReadOnlyError, vf.add_lines, b'b', [], [])
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
420
421
    def test___iter__escaped(self):
5582.9.21 by Jelmer Vernooij
Remove weave references from VersionedfileStore.
422
        self.vfstore = VersionedFileStore(MemoryTransport(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
423
                                          prefixed=True, escaped=True, versionedfile_class=WeaveFile)
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
424
        self.vfstore.get_scope = self.get_scope
425
        self._transaction = transactions.WriteTransaction()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
426
        vf = self.vfstore.get_weave_or_empty(b' ', self._transaction)
427
        vf.add_lines(b'a', [], [])
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
428
        del vf
429
        self._transaction.finish()
6963.2.18 by Jelmer Vernooij
Add bees to some of bp.weave_fmt.
430
        self.assertEqual([b' '], list(self.vfstore))