/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3602.2.1 by Martin Pool
Fix and test for problem upgrading stacked branches
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
1685.1.63 by Martin Pool
Small Transport fixups
2
#
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
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.
1685.1.63 by Martin Pool
Small Transport fixups
7
#
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
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.
1685.1.63 by Martin Pool
Small Transport fixups
12
#
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for the Repository facility that are not interface tests.
18
3689.1.4 by John Arbash Meinel
Doc strings that reference repository_implementations
19
For interface tests see tests/per_repository/*.py.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
20
21
For concrete class tests see this file, and for storage formats tests
22
also see this file.
23
"""
24
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
25
from stat import S_ISDIR
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
26
from StringIO import StringIO
27
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
28
import bzrlib
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
29
from bzrlib.errors import (NotBranchError,
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
30
                           NoSuchFile,
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
31
                           UnknownFormatError,
32
                           UnsupportedFormatError,
33
                           )
3184.1.9 by Robert Collins
* ``Repository.get_data_stream`` is now deprecated in favour of
34
from bzrlib import graph
3735.4.1 by Andrew Bennetts
Add _find_revision_outside_set.
35
from bzrlib.branchbuilder import BranchBuilder
3735.1.1 by Robert Collins
Add development2 formats using BTree indices.
36
from bzrlib.btree_index import BTreeBuilder, BTreeGraphIndex
2592.3.192 by Robert Collins
Move new revision index management to NewPack.
37
from bzrlib.index import GraphIndex, InMemoryGraphIndex
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
38
from bzrlib.repository import RepositoryFormat
2535.3.41 by Andrew Bennetts
Add tests for InterRemoteToOther.is_compatible.
39
from bzrlib.smart import server
2670.3.5 by Andrew Bennetts
Remove get_stream_as_bytes from KnitVersionedFile's API, make it a function in knitrepo.py instead.
40
from bzrlib.tests import (
41
    TestCase,
42
    TestCaseWithTransport,
3446.2.1 by Martin Pool
Failure to delete an obsolete pack file should not be fatal.
43
    TestSkipped,
2670.3.5 by Andrew Bennetts
Remove get_stream_as_bytes from KnitVersionedFile's API, make it a function in knitrepo.py instead.
44
    test_knit,
45
    )
3446.2.1 by Martin Pool
Failure to delete an obsolete pack file should not be fatal.
46
from bzrlib.transport import (
47
    fakenfs,
48
    get_transport,
49
    )
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
50
from bzrlib.transport.memory import MemoryServer
2535.3.53 by Andrew Bennetts
Remove get_stream_as_bytes from KnitVersionedFile's API, make it a function in knitrepo.py instead.
51
from bzrlib.util import bencode
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
52
from bzrlib import (
2535.3.41 by Andrew Bennetts
Add tests for InterRemoteToOther.is_compatible.
53
    bzrdir,
54
    errors,
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
55
    inventory,
2929.3.5 by Vincent Ladeuil
New files, same warnings, same fixes.
56
    osutils,
3146.6.1 by Aaron Bentley
InterDifferingSerializer shows a progress bar
57
    progress,
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
58
    repository,
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
59
    revision as _mod_revision,
2535.3.41 by Andrew Bennetts
Add tests for InterRemoteToOther.is_compatible.
60
    symbol_versioning,
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
61
    upgrade,
62
    workingtree,
63
    )
2592.3.173 by Robert Collins
Basic implementation of all_packs.
64
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
65
66
67
class TestDefaultFormat(TestCase):
68
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
69
    def test_get_set_default_format(self):
2204.5.3 by Aaron Bentley
zap old repository default handling
70
        old_default = bzrdir.format_registry.get('default')
71
        private_default = old_default().repository_format.__class__
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
72
        old_format = repository.RepositoryFormat.get_default_format()
1910.2.33 by Aaron Bentley
Fix default format test
73
        self.assertTrue(isinstance(old_format, private_default))
2204.5.3 by Aaron Bentley
zap old repository default handling
74
        def make_sample_bzrdir():
75
            my_bzrdir = bzrdir.BzrDirMetaFormat1()
76
            my_bzrdir.repository_format = SampleRepositoryFormat()
77
            return my_bzrdir
78
        bzrdir.format_registry.remove('default')
79
        bzrdir.format_registry.register('sample', make_sample_bzrdir, '')
80
        bzrdir.format_registry.set_default('sample')
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
81
        # creating a repository should now create an instrumented dir.
82
        try:
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
83
            # the default branch format is used by the meta dir format
84
            # which is not the default bzrdir format at this point
1685.1.63 by Martin Pool
Small Transport fixups
85
            dir = bzrdir.BzrDirMetaFormat1().initialize('memory:///')
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
86
            result = dir.create_repository()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
87
            self.assertEqual(result, 'A bzr repository dir')
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
88
        finally:
2204.5.3 by Aaron Bentley
zap old repository default handling
89
            bzrdir.format_registry.remove('default')
2363.5.14 by Aaron Bentley
Prevent repository.get_set_default_format from corrupting inventory
90
            bzrdir.format_registry.remove('sample')
2204.5.3 by Aaron Bentley
zap old repository default handling
91
            bzrdir.format_registry.register('default', old_default, '')
92
        self.assertIsInstance(repository.RepositoryFormat.get_default_format(),
93
                              old_format.__class__)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
94
95
96
class SampleRepositoryFormat(repository.RepositoryFormat):
97
    """A sample format
98
99
    this format is initializable, unsupported to aid in testing the 
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
100
    open and open(unsupported=True) routines.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
101
    """
102
103
    def get_format_string(self):
104
        """See RepositoryFormat.get_format_string()."""
105
        return "Sample .bzr repository format."
106
1534.6.1 by Robert Collins
allow API creation of shared repositories
107
    def initialize(self, a_bzrdir, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
108
        """Initialize a repository in a BzrDir"""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
109
        t = a_bzrdir.get_repository_transport(self)
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
110
        t.put_bytes('format', self.get_format_string())
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
111
        return 'A bzr repository dir'
112
113
    def is_supported(self):
114
        return False
115
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
116
    def open(self, a_bzrdir, _found=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
117
        return "opened repository."
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
118
119
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
120
class TestRepositoryFormat(TestCaseWithTransport):
121
    """Tests for the Repository format detection used by the bzr meta dir facility.BzrBranchFormat facility."""
122
123
    def test_find_format(self):
124
        # is the right format object found for a repository?
125
        # create a branch with a few known format objects.
126
        # this is not quite the same as 
127
        self.build_tree(["foo/", "bar/"])
128
        def check_format(format, url):
129
            dir = format._matchingbzrdir.initialize(url)
130
            format.initialize(dir)
131
            t = get_transport(url)
132
            found_format = repository.RepositoryFormat.find_format(dir)
133
            self.failUnless(isinstance(found_format, format.__class__))
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
134
        check_format(weaverepo.RepositoryFormat7(), "bar")
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
135
        
136
    def test_find_format_no_repository(self):
137
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
138
        self.assertRaises(errors.NoRepositoryPresent,
139
                          repository.RepositoryFormat.find_format,
140
                          dir)
141
142
    def test_find_format_unknown_format(self):
143
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
144
        SampleRepositoryFormat().initialize(dir)
145
        self.assertRaises(UnknownFormatError,
146
                          repository.RepositoryFormat.find_format,
147
                          dir)
148
149
    def test_register_unregister_format(self):
150
        format = SampleRepositoryFormat()
151
        # make a control dir
152
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
153
        # make a repo
154
        format.initialize(dir)
155
        # register a format for it.
156
        repository.RepositoryFormat.register_format(format)
157
        # which repository.Open will refuse (not supported)
158
        self.assertRaises(UnsupportedFormatError, repository.Repository.open, self.get_url())
159
        # but open(unsupported) will work
160
        self.assertEqual(format.open(dir), "opened repository.")
161
        # unregister the format
162
        repository.RepositoryFormat.unregister_format(format)
163
164
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
165
class TestFormat6(TestCaseWithTransport):
166
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
167
    def test_attribute__fetch_order(self):
168
        """Weaves need topological data insertion."""
169
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
170
        repo = weaverepo.RepositoryFormat6().initialize(control)
171
        self.assertEqual('topological', repo._fetch_order)
172
173
    def test_attribute__fetch_uses_deltas(self):
174
        """Weaves do not reuse deltas."""
175
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
176
        repo = weaverepo.RepositoryFormat6().initialize(control)
177
        self.assertEqual(False, repo._fetch_uses_deltas)
178
3565.3.4 by Robert Collins
Defer decision to reconcile to the repository being fetched into.
179
    def test_attribute__fetch_reconcile(self):
180
        """Weave repositories need a reconcile after fetch."""
181
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
182
        repo = weaverepo.RepositoryFormat6().initialize(control)
183
        self.assertEqual(True, repo._fetch_reconcile)
184
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
185
    def test_no_ancestry_weave(self):
186
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
187
        repo = weaverepo.RepositoryFormat6().initialize(control)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
188
        # We no longer need to create the ancestry.weave file
189
        # since it is *never* used.
190
        self.assertRaises(NoSuchFile,
191
                          control.transport.get,
192
                          'ancestry.weave')
193
3221.3.1 by Robert Collins
* Repository formats have a new supported-feature attribute
194
    def test_supports_external_lookups(self):
195
        control = bzrdir.BzrDirFormat6().initialize(self.get_url())
196
        repo = weaverepo.RepositoryFormat6().initialize(control)
197
        self.assertFalse(repo._format.supports_external_lookups)
198
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
199
200
class TestFormat7(TestCaseWithTransport):
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
201
202
    def test_attribute__fetch_order(self):
203
        """Weaves need topological data insertion."""
204
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
205
        repo = weaverepo.RepositoryFormat7().initialize(control)
206
        self.assertEqual('topological', repo._fetch_order)
207
208
    def test_attribute__fetch_uses_deltas(self):
209
        """Weaves do not reuse deltas."""
210
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
211
        repo = weaverepo.RepositoryFormat7().initialize(control)
212
        self.assertEqual(False, repo._fetch_uses_deltas)
213
3565.3.4 by Robert Collins
Defer decision to reconcile to the repository being fetched into.
214
    def test_attribute__fetch_reconcile(self):
215
        """Weave repositories need a reconcile after fetch."""
216
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
217
        repo = weaverepo.RepositoryFormat7().initialize(control)
218
        self.assertEqual(True, repo._fetch_reconcile)
219
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
220
    def test_disk_layout(self):
221
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
222
        repo = weaverepo.RepositoryFormat7().initialize(control)
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
223
        # in case of side effects of locking.
224
        repo.lock_write()
225
        repo.unlock()
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
226
        # we want:
227
        # format 'Bazaar-NG Repository format 7'
228
        # lock ''
229
        # inventory.weave == empty_weave
230
        # empty revision-store directory
231
        # empty weaves directory
232
        t = control.get_repository_transport(None)
233
        self.assertEqualDiff('Bazaar-NG Repository format 7',
234
                             t.get('format').read())
235
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
236
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
237
        self.assertEqualDiff('# bzr weave file v5\n'
238
                             'w\n'
239
                             'W\n',
240
                             t.get('inventory.weave').read())
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
241
        # Creating a file with id Foo:Bar results in a non-escaped file name on
242
        # disk.
243
        control.create_branch()
244
        tree = control.create_workingtree()
245
        tree.add(['foo'], ['Foo:Bar'], ['file'])
246
        tree.put_file_bytes_non_atomic('Foo:Bar', 'content\n')
247
        tree.commit('first post', rev_id='first')
248
        self.assertEqualDiff(
249
            '# bzr weave file v5\n'
250
            'i\n'
251
            '1 7fe70820e08a1aac0ef224d9c66ab66831cc4ab1\n'
252
            'n first\n'
253
            '\n'
254
            'w\n'
255
            '{ 0\n'
256
            '. content\n'
257
            '}\n'
258
            'W\n',
259
            t.get('weaves/74/Foo%3ABar.weave').read())
1534.6.1 by Robert Collins
allow API creation of shared repositories
260
261
    def test_shared_disk_layout(self):
262
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
263
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
1534.6.1 by Robert Collins
allow API creation of shared repositories
264
        # we want:
265
        # format 'Bazaar-NG Repository format 7'
266
        # inventory.weave == empty_weave
267
        # empty revision-store directory
268
        # empty weaves directory
269
        # a 'shared-storage' marker file.
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
270
        # lock is not present when unlocked
1534.6.1 by Robert Collins
allow API creation of shared repositories
271
        t = control.get_repository_transport(None)
272
        self.assertEqualDiff('Bazaar-NG Repository format 7',
273
                             t.get('format').read())
274
        self.assertEqualDiff('', t.get('shared-storage').read())
275
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
276
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
277
        self.assertEqualDiff('# bzr weave file v5\n'
278
                             'w\n'
279
                             'W\n',
280
                             t.get('inventory.weave').read())
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
281
        self.assertFalse(t.has('branch-lock'))
282
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
283
    def test_creates_lockdir(self):
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
284
        """Make sure it appears to be controlled by a LockDir existence"""
285
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
286
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
287
        t = control.get_repository_transport(None)
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
288
        # TODO: Should check there is a 'lock' toplevel directory, 
289
        # regardless of contents
290
        self.assertFalse(t.has('lock/held/info'))
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
291
        repo.lock_write()
1658.1.4 by Martin Pool
Quieten warning from TestFormat7.test_creates_lockdir about failing to unlock
292
        try:
293
            self.assertTrue(t.has('lock/held/info'))
294
        finally:
295
            # unlock so we don't get a warning about failing to do so
296
            repo.unlock()
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
297
298
    def test_uses_lockdir(self):
299
        """repo format 7 actually locks on lockdir"""
300
        base_url = self.get_url()
301
        control = bzrdir.BzrDirMetaFormat1().initialize(base_url)
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
302
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
303
        t = control.get_repository_transport(None)
304
        repo.lock_write()
305
        repo.unlock()
306
        del repo
307
        # make sure the same lock is created by opening it
308
        repo = repository.Repository.open(base_url)
309
        repo.lock_write()
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
310
        self.assertTrue(t.has('lock/held/info'))
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
311
        repo.unlock()
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
312
        self.assertFalse(t.has('lock/held/info'))
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
313
314
    def test_shared_no_tree_disk_layout(self):
315
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
316
        repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
317
        repo.set_make_working_trees(False)
318
        # we want:
319
        # format 'Bazaar-NG Repository format 7'
320
        # lock ''
321
        # inventory.weave == empty_weave
322
        # empty revision-store directory
323
        # empty weaves directory
324
        # a 'shared-storage' marker file.
325
        t = control.get_repository_transport(None)
326
        self.assertEqualDiff('Bazaar-NG Repository format 7',
327
                             t.get('format').read())
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
328
        ## self.assertEqualDiff('', t.get('lock').read())
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
329
        self.assertEqualDiff('', t.get('shared-storage').read())
330
        self.assertEqualDiff('', t.get('no-working-trees').read())
331
        repo.set_make_working_trees(True)
332
        self.assertFalse(t.has('no-working-trees'))
333
        self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
334
        self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
335
        self.assertEqualDiff('# bzr weave file v5\n'
336
                             'w\n'
337
                             'W\n',
338
                             t.get('inventory.weave').read())
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
339
3221.3.1 by Robert Collins
* Repository formats have a new supported-feature attribute
340
    def test_supports_external_lookups(self):
341
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
342
        repo = weaverepo.RepositoryFormat7().initialize(control)
343
        self.assertFalse(repo._format.supports_external_lookups)
344
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
345
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
346
class TestFormatKnit1(TestCaseWithTransport):
347
    
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
348
    def test_attribute__fetch_order(self):
349
        """Knits need topological data insertion."""
350
        repo = self.make_repository('.',
351
                format=bzrdir.format_registry.get('knit')())
352
        self.assertEqual('topological', repo._fetch_order)
353
354
    def test_attribute__fetch_uses_deltas(self):
355
        """Knits reuse deltas."""
356
        repo = self.make_repository('.',
357
                format=bzrdir.format_registry.get('knit')())
358
        self.assertEqual(True, repo._fetch_uses_deltas)
359
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
360
    def test_disk_layout(self):
361
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
362
        repo = knitrepo.RepositoryFormatKnit1().initialize(control)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
363
        # in case of side effects of locking.
364
        repo.lock_write()
365
        repo.unlock()
366
        # we want:
367
        # format 'Bazaar-NG Knit Repository Format 1'
1553.5.62 by Martin Pool
Add tests that MetaDir repositories use LockDirs
368
        # lock: is a directory
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
369
        # inventory.weave == empty_weave
370
        # empty revision-store directory
371
        # empty weaves directory
372
        t = control.get_repository_transport(None)
373
        self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
374
                             t.get('format').read())
1553.5.57 by Martin Pool
[merge] sync from bzr.dev
375
        # XXX: no locks left when unlocked at the moment
376
        # self.assertEqualDiff('', t.get('lock').read())
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
377
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
378
        self.check_knits(t)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
379
        # Check per-file knits.
380
        branch = control.create_branch()
381
        tree = control.create_workingtree()
382
        tree.add(['foo'], ['Nasty-IdC:'], ['file'])
383
        tree.put_file_bytes_non_atomic('Nasty-IdC:', '')
384
        tree.commit('1st post', rev_id='foo')
385
        self.assertHasKnit(t, 'knits/e8/%254easty-%2549d%2543%253a',
386
            '\nfoo fulltext 0 81  :')
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
387
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
388
    def assertHasKnit(self, t, knit_name, extra_content=''):
1654.1.3 by Robert Collins
Refactor repository knit tests slightly to remove duplication - add a assertHasKnit method.
389
        """Assert that knit_name exists on t."""
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
390
        self.assertEqualDiff('# bzr knit index 8\n' + extra_content,
1654.1.3 by Robert Collins
Refactor repository knit tests slightly to remove duplication - add a assertHasKnit method.
391
                             t.get(knit_name + '.kndx').read())
392
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
393
    def check_knits(self, t):
394
        """check knit content for a repository."""
1654.1.3 by Robert Collins
Refactor repository knit tests slightly to remove duplication - add a assertHasKnit method.
395
        self.assertHasKnit(t, 'inventory')
396
        self.assertHasKnit(t, 'revisions')
397
        self.assertHasKnit(t, 'signatures')
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
398
399
    def test_shared_disk_layout(self):
400
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
401
        repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
402
        # we want:
403
        # format 'Bazaar-NG Knit Repository Format 1'
1553.5.62 by Martin Pool
Add tests that MetaDir repositories use LockDirs
404
        # lock: is a directory
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
405
        # inventory.weave == empty_weave
406
        # empty revision-store directory
407
        # empty weaves directory
408
        # a 'shared-storage' marker file.
409
        t = control.get_repository_transport(None)
410
        self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
411
                             t.get('format').read())
1553.5.57 by Martin Pool
[merge] sync from bzr.dev
412
        # XXX: no locks left when unlocked at the moment
413
        # self.assertEqualDiff('', t.get('lock').read())
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
414
        self.assertEqualDiff('', t.get('shared-storage').read())
415
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
416
        self.check_knits(t)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
417
418
    def test_shared_no_tree_disk_layout(self):
419
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
420
        repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
421
        repo.set_make_working_trees(False)
422
        # we want:
423
        # format 'Bazaar-NG Knit Repository Format 1'
424
        # lock ''
425
        # inventory.weave == empty_weave
426
        # empty revision-store directory
427
        # empty weaves directory
428
        # a 'shared-storage' marker file.
429
        t = control.get_repository_transport(None)
430
        self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
431
                             t.get('format').read())
1553.5.57 by Martin Pool
[merge] sync from bzr.dev
432
        # XXX: no locks left when unlocked at the moment
433
        # self.assertEqualDiff('', t.get('lock').read())
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
434
        self.assertEqualDiff('', t.get('shared-storage').read())
435
        self.assertEqualDiff('', t.get('no-working-trees').read())
436
        repo.set_make_working_trees(True)
437
        self.assertFalse(t.has('no-working-trees'))
438
        self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
439
        self.check_knits(t)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
440
2917.2.1 by John Arbash Meinel
Fix bug #152360. The xml5 serializer should be using
441
    def test_deserialise_sets_root_revision(self):
442
        """We must have a inventory.root.revision
443
444
        Old versions of the XML5 serializer did not set the revision_id for
445
        the whole inventory. So we grab the one from the expected text. Which
446
        is valid when the api is not being abused.
447
        """
448
        repo = self.make_repository('.',
449
                format=bzrdir.format_registry.get('knit')())
450
        inv_xml = '<inventory format="5">\n</inventory>\n'
451
        inv = repo.deserialise_inventory('test-rev-id', inv_xml)
452
        self.assertEqual('test-rev-id', inv.root.revision)
453
454
    def test_deserialise_uses_global_revision_id(self):
455
        """If it is set, then we re-use the global revision id"""
456
        repo = self.make_repository('.',
457
                format=bzrdir.format_registry.get('knit')())
458
        inv_xml = ('<inventory format="5" revision_id="other-rev-id">\n'
459
                   '</inventory>\n')
460
        # Arguably, the deserialise_inventory should detect a mismatch, and
461
        # raise an error, rather than silently using one revision_id over the
462
        # other.
3169.2.2 by Robert Collins
Add a test to Repository.deserialise_inventory that the resulting ivnentory is the one asked for, and update relevant tests. Also tweak the model 1 to 2 regenerate inventories logic to use the revision trees parent marker which is more accurate in some cases.
463
        self.assertRaises(AssertionError, repo.deserialise_inventory,
464
            'test-rev-id', inv_xml)
465
        inv = repo.deserialise_inventory('other-rev-id', inv_xml)
2917.2.1 by John Arbash Meinel
Fix bug #152360. The xml5 serializer should be using
466
        self.assertEqual('other-rev-id', inv.root.revision)
467
3221.3.1 by Robert Collins
* Repository formats have a new supported-feature attribute
468
    def test_supports_external_lookups(self):
469
        repo = self.make_repository('.',
470
                format=bzrdir.format_registry.get('knit')())
471
        self.assertFalse(repo._format.supports_external_lookups)
472
2535.3.53 by Andrew Bennetts
Remove get_stream_as_bytes from KnitVersionedFile's API, make it a function in knitrepo.py instead.
473
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
474
class DummyRepository(object):
475
    """A dummy repository for testing."""
476
3452.2.11 by Andrew Bennetts
Merge thread.
477
    _format = None
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
478
    _serializer = None
479
480
    def supports_rich_root(self):
481
        return False
482
3709.5.10 by Andrew Bennetts
Fix test failure caused by missing attributes on DummyRepository.
483
    def get_graph(self):
484
        raise NotImplementedError
485
486
    def get_parent_map(self, revision_ids):
487
        raise NotImplementedError
488
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
489
490
class InterDummy(repository.InterRepository):
491
    """An inter-repository optimised code path for DummyRepository.
492
493
    This is for use during testing where we use DummyRepository as repositories
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
494
    so that none of the default regsitered inter-repository classes will
2818.4.2 by Robert Collins
Review feedback.
495
    MATCH.
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
496
    """
497
498
    @staticmethod
499
    def is_compatible(repo_source, repo_target):
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
500
        """InterDummy is compatible with DummyRepository."""
501
        return (isinstance(repo_source, DummyRepository) and 
502
            isinstance(repo_target, DummyRepository))
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
503
504
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
505
class TestInterRepository(TestCaseWithTransport):
506
507
    def test_get_default_inter_repository(self):
508
        # test that the InterRepository.get(repo_a, repo_b) probes
509
        # for a inter_repo class where is_compatible(repo_a, repo_b) returns
510
        # true and returns a default inter_repo otherwise.
511
        # This also tests that the default registered optimised interrepository
512
        # classes do not barf inappropriately when a surprising repository type
513
        # is handed to them.
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
514
        dummy_a = DummyRepository()
515
        dummy_b = DummyRepository()
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
516
        self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
517
518
    def assertGetsDefaultInterRepository(self, repo_a, repo_b):
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
519
        """Asserts that InterRepository.get(repo_a, repo_b) -> the default.
520
        
521
        The effective default is now InterSameDataRepository because there is
522
        no actual sane default in the presence of incompatible data models.
523
        """
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
524
        inter_repo = repository.InterRepository.get(repo_a, repo_b)
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
525
        self.assertEqual(repository.InterSameDataRepository,
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
526
                         inter_repo.__class__)
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
527
        self.assertEqual(repo_a, inter_repo.source)
528
        self.assertEqual(repo_b, inter_repo.target)
529
530
    def test_register_inter_repository_class(self):
531
        # test that a optimised code path provider - a
532
        # InterRepository subclass can be registered and unregistered
533
        # and that it is correctly selected when given a repository
534
        # pair that it returns true on for the is_compatible static method
535
        # check
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
536
        dummy_a = DummyRepository()
537
        dummy_b = DummyRepository()
538
        repo = self.make_repository('.')
539
        # hack dummies to look like repo somewhat.
540
        dummy_a._serializer = repo._serializer
541
        dummy_b._serializer = repo._serializer
542
        repository.InterRepository.register_optimiser(InterDummy)
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
543
        try:
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
544
            # we should get the default for something InterDummy returns False
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
545
            # to
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
546
            self.assertFalse(InterDummy.is_compatible(dummy_a, repo))
547
            self.assertGetsDefaultInterRepository(dummy_a, repo)
548
            # and we should get an InterDummy for a pair it 'likes'
549
            self.assertTrue(InterDummy.is_compatible(dummy_a, dummy_b))
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
550
            inter_repo = repository.InterRepository.get(dummy_a, dummy_b)
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
551
            self.assertEqual(InterDummy, inter_repo.__class__)
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
552
            self.assertEqual(dummy_a, inter_repo.source)
553
            self.assertEqual(dummy_b, inter_repo.target)
554
        finally:
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
555
            repository.InterRepository.unregister_optimiser(InterDummy)
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
556
        # now we should get the default InterRepository object again.
557
        self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
558
2241.1.17 by Martin Pool
Restore old InterWeave tests
559
560
class TestInterWeaveRepo(TestCaseWithTransport):
561
562
    def test_is_compatible_and_registered(self):
563
        # InterWeaveRepo is compatible when either side
564
        # is a format 5/6/7 branch
2241.1.20 by mbp at sourcefrog
update tests for new locations of weave repos
565
        from bzrlib.repofmt import knitrepo, weaverepo
566
        formats = [weaverepo.RepositoryFormat5(),
567
                   weaverepo.RepositoryFormat6(),
568
                   weaverepo.RepositoryFormat7()]
569
        incompatible_formats = [weaverepo.RepositoryFormat4(),
570
                                knitrepo.RepositoryFormatKnit1(),
2241.1.17 by Martin Pool
Restore old InterWeave tests
571
                                ]
572
        repo_a = self.make_repository('a')
573
        repo_b = self.make_repository('b')
574
        is_compatible = repository.InterWeaveRepo.is_compatible
575
        for source in incompatible_formats:
576
            # force incompatible left then right
577
            repo_a._format = source
578
            repo_b._format = formats[0]
579
            self.assertFalse(is_compatible(repo_a, repo_b))
580
            self.assertFalse(is_compatible(repo_b, repo_a))
581
        for source in formats:
582
            repo_a._format = source
583
            for target in formats:
584
                repo_b._format = target
585
                self.assertTrue(is_compatible(repo_a, repo_b))
586
        self.assertEqual(repository.InterWeaveRepo,
587
                         repository.InterRepository.get(repo_a,
588
                                                        repo_b).__class__)
589
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
590
591
class TestRepositoryConverter(TestCaseWithTransport):
592
593
    def test_convert_empty(self):
594
        t = get_transport(self.get_url('.'))
595
        t.mkdir('repository')
596
        repo_dir = bzrdir.BzrDirMetaFormat1().initialize('repository')
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
597
        repo = weaverepo.RepositoryFormat7().initialize(repo_dir)
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
598
        target_format = knitrepo.RepositoryFormatKnit1()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
599
        converter = repository.CopyConverter(target_format)
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
600
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
601
        try:
602
            converter.convert(repo, pb)
603
        finally:
604
            pb.finished()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
605
        repo = repo_dir.open_repository()
606
        self.assertTrue(isinstance(target_format, repo._format.__class__))
1843.2.5 by Aaron Bentley
Add test of _unescape_xml
607
608
609
class TestMisc(TestCase):
610
    
611
    def test_unescape_xml(self):
612
        """We get some kind of error when malformed entities are passed"""
613
        self.assertRaises(KeyError, repository._unescape_xml, 'foo&bar;') 
1910.2.13 by Aaron Bentley
Start work on converter
614
615
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
616
class TestRepositoryFormatKnit3(TestCaseWithTransport):
1910.2.13 by Aaron Bentley
Start work on converter
617
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
618
    def test_attribute__fetch_order(self):
619
        """Knits need topological data insertion."""
620
        format = bzrdir.BzrDirMetaFormat1()
621
        format.repository_format = knitrepo.RepositoryFormatKnit3()
622
        repo = self.make_repository('.', format=format)
623
        self.assertEqual('topological', repo._fetch_order)
624
625
    def test_attribute__fetch_uses_deltas(self):
626
        """Knits reuse deltas."""
627
        format = bzrdir.BzrDirMetaFormat1()
628
        format.repository_format = knitrepo.RepositoryFormatKnit3()
629
        repo = self.make_repository('.', format=format)
630
        self.assertEqual(True, repo._fetch_uses_deltas)
631
1910.2.13 by Aaron Bentley
Start work on converter
632
    def test_convert(self):
633
        """Ensure the upgrade adds weaves for roots"""
1910.2.35 by Aaron Bentley
Better fix for convesion test
634
        format = bzrdir.BzrDirMetaFormat1()
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
635
        format.repository_format = knitrepo.RepositoryFormatKnit1()
1910.2.35 by Aaron Bentley
Better fix for convesion test
636
        tree = self.make_branch_and_tree('.', format)
1910.2.13 by Aaron Bentley
Start work on converter
637
        tree.commit("Dull commit", rev_id="dull")
638
        revision_tree = tree.branch.repository.revision_tree('dull')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
639
        revision_tree.lock_read()
640
        try:
641
            self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
642
                revision_tree.inventory.root.file_id)
643
        finally:
644
            revision_tree.unlock()
1910.2.13 by Aaron Bentley
Start work on converter
645
        format = bzrdir.BzrDirMetaFormat1()
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
646
        format.repository_format = knitrepo.RepositoryFormatKnit3()
1910.2.13 by Aaron Bentley
Start work on converter
647
        upgrade.Convert('.', format)
1910.2.27 by Aaron Bentley
Fixed conversion test
648
        tree = workingtree.WorkingTree.open('.')
1910.2.13 by Aaron Bentley
Start work on converter
649
        revision_tree = tree.branch.repository.revision_tree('dull')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
650
        revision_tree.lock_read()
651
        try:
652
            revision_tree.get_file_lines(revision_tree.inventory.root.file_id)
653
        finally:
654
            revision_tree.unlock()
1910.2.27 by Aaron Bentley
Fixed conversion test
655
        tree.commit("Another dull commit", rev_id='dull2')
656
        revision_tree = tree.branch.repository.revision_tree('dull2')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
657
        revision_tree.lock_read()
658
        self.addCleanup(revision_tree.unlock)
1910.2.27 by Aaron Bentley
Fixed conversion test
659
        self.assertEqual('dull', revision_tree.inventory.root.revision)
2220.2.2 by Martin Pool
Add tag command and basic implementation
660
3221.3.1 by Robert Collins
* Repository formats have a new supported-feature attribute
661
    def test_supports_external_lookups(self):
662
        format = bzrdir.BzrDirMetaFormat1()
663
        format.repository_format = knitrepo.RepositoryFormatKnit3()
664
        repo = self.make_repository('.', format=format)
665
        self.assertFalse(repo._format.supports_external_lookups)
666
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
667
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
668
class TestDevelopment3(TestCaseWithTransport):
669
670
    def test_add_inventory_uses_chk_map(self):
671
        repo = self.make_repository('repo', format="development3")
672
        source = self.make_branch_and_tree("source", format="pack-0.92")
673
        revid = source.commit("foo", rev_id="foo")
674
        # get the inventory from the committed revision
675
        basis = source.basis_tree()
676
        basis.lock_read()
677
        self.addCleanup(basis.unlock)
678
        inv = basis.inventory
679
        repo.lock_write()
680
        self.addCleanup(repo.unlock)
681
        repo.start_write_group()
682
        self.addCleanup(repo.abort_write_group)
683
        repo.add_inventory(revid, inv, [])
684
        self.assertEqual(set([(revid,)]), repo.inventories.keys())
685
        self.assertEqual(
3735.2.27 by Robert Collins
Use 4K pages for development3 repositories.
686
            set([('sha1:6210160e6bc65e395d08bb63cc0aa2f47434631a',)]),
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
687
            repo.chk_bytes.keys())
3735.2.27 by Robert Collins
Use 4K pages for development3 repositories.
688
        inv = repo.get_inventory(revid)
689
        inv.id_to_entry._ensure_root()
690
        self.assertEqual(4096, inv.id_to_entry._root_node.maximum_size)
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
691
692
3735.2.40 by Robert Collins
Add development4 which has a parent_id to basename index on CHKInventory objects.
693
class TestDevelopment4(TestCaseWithTransport):
694
695
    def test_inventories_use_chk_map_with_parent_base_dict(self):
696
        tree = self.make_branch_and_tree('repo', format="development4")
697
        revid = tree.commit("foo")
698
        tree.lock_read()
699
        self.addCleanup(tree.unlock)
700
        inv = tree.branch.repository.get_inventory(revid)
3735.2.41 by Robert Collins
Make the parent_id_basename index be updated during CHKInventory.apply_delta.
701
        self.assertNotEqual(None, inv.parent_id_basename_to_file_id)
702
        inv.parent_id_basename_to_file_id._ensure_root()
3735.2.40 by Robert Collins
Add development4 which has a parent_id to basename index on CHKInventory objects.
703
        inv.id_to_entry._ensure_root()
704
        self.assertEqual(4096, inv.id_to_entry._root_node.maximum_size)
705
        self.assertEqual(4096,
3735.2.41 by Robert Collins
Make the parent_id_basename index be updated during CHKInventory.apply_delta.
706
            inv.parent_id_basename_to_file_id._root_node.maximum_size)
3735.2.40 by Robert Collins
Add development4 which has a parent_id to basename index on CHKInventory objects.
707
708
3735.4.1 by Andrew Bennetts
Add _find_revision_outside_set.
709
class TestDevelopment3FindRevisionOutsideSet(TestCaseWithTransport):
710
    """Tests for _find_revision_outside_set."""
711
712
    def setUp(self):
713
        super(TestDevelopment3FindRevisionOutsideSet, self).setUp()
714
        self.builder = self.make_branch_builder('source', format='development3')
715
        self.builder.start_series()
716
        self.builder.build_snapshot('initial', None,
717
            [('add', ('', 'tree-root', 'directory', None))])
718
        self.repo = self.builder.get_branch().repository
719
        self.addCleanup(self.builder.finish_series)
720
        
721
    def assertRevisionOutsideSet(self, expected_result, rev_set):
722
        self.assertEqual(
723
            expected_result, self.repo._find_revision_outside_set(rev_set))
724
725
    def test_simple(self):
726
        self.builder.build_snapshot('revid1', None, [])
727
        self.builder.build_snapshot('revid2', None, [])
728
        rev_set = ['revid2']
729
        self.assertRevisionOutsideSet('revid1', rev_set)
730
731
    def test_not_first_parent(self):
732
        self.builder.build_snapshot('revid1', None, [])
733
        self.builder.build_snapshot('revid2', None, [])
734
        self.builder.build_snapshot('revid3', None, [])
735
        rev_set = ['revid3', 'revid2']
736
        self.assertRevisionOutsideSet('revid1', rev_set)
737
738
    def test_not_null(self):
739
        rev_set = ['initial']
740
        self.assertRevisionOutsideSet(_mod_revision.NULL_REVISION, rev_set)
741
742
    def test_not_null_set(self):
743
        self.builder.build_snapshot('revid1', None, [])
744
        rev_set = [_mod_revision.NULL_REVISION]
745
        self.assertRevisionOutsideSet(_mod_revision.NULL_REVISION, rev_set)
746
747
    def test_ghost(self):
748
        self.builder.build_snapshot('revid1', None, [])
749
        rev_set = ['ghost', 'revid1']
750
        self.assertRevisionOutsideSet('initial', rev_set)
751
752
    def test_ghost_parent(self):
753
        self.builder.build_snapshot('revid1', None, [])
754
        self.builder.build_snapshot('revid2', ['revid1', 'ghost'], [])
755
        rev_set = ['revid2', 'revid1']
756
        self.assertRevisionOutsideSet('initial', rev_set)
757
758
    def test_righthand_parent(self):
759
        self.builder.build_snapshot('revid1', None, [])
760
        self.builder.build_snapshot('revid2a', ['revid1'], [])
761
        self.builder.build_snapshot('revid2b', ['revid1'], [])
762
        self.builder.build_snapshot('revid3', ['revid2a', 'revid2b'], [])
763
        rev_set = ['revid3', 'revid2a']
764
        self.assertRevisionOutsideSet('revid2b', rev_set)
765
766
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
767
class TestWithBrokenRepo(TestCaseWithTransport):
2592.3.214 by Robert Collins
Merge bzr.dev.
768
    """These tests seem to be more appropriate as interface tests?"""
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
769
770
    def make_broken_repository(self):
771
        # XXX: This function is borrowed from Aaron's "Reconcile can fix bad
772
        # parent references" branch which is due to land in bzr.dev soon.  Once
773
        # it does, this duplication should be removed.
774
        repo = self.make_repository('broken-repo')
775
        cleanups = []
776
        try:
777
            repo.lock_write()
778
            cleanups.append(repo.unlock)
779
            repo.start_write_group()
780
            cleanups.append(repo.commit_write_group)
781
            # make rev1a: A well-formed revision, containing 'file1'
782
            inv = inventory.Inventory(revision_id='rev1a')
783
            inv.root.revision = 'rev1a'
784
            self.add_file(repo, inv, 'file1', 'rev1a', [])
785
            repo.add_inventory('rev1a', inv, [])
786
            revision = _mod_revision.Revision('rev1a',
787
                committer='jrandom@example.com', timestamp=0,
788
                inventory_sha1='', timezone=0, message='foo', parent_ids=[])
789
            repo.add_revision('rev1a',revision, inv)
790
791
            # make rev1b, which has no Revision, but has an Inventory, and
792
            # file1
793
            inv = inventory.Inventory(revision_id='rev1b')
794
            inv.root.revision = 'rev1b'
795
            self.add_file(repo, inv, 'file1', 'rev1b', [])
796
            repo.add_inventory('rev1b', inv, [])
797
798
            # make rev2, with file1 and file2
799
            # file2 is sane
800
            # file1 has 'rev1b' as an ancestor, even though this is not
801
            # mentioned by 'rev1a', making it an unreferenced ancestor
802
            inv = inventory.Inventory()
803
            self.add_file(repo, inv, 'file1', 'rev2', ['rev1a', 'rev1b'])
804
            self.add_file(repo, inv, 'file2', 'rev2', [])
805
            self.add_revision(repo, 'rev2', inv, ['rev1a'])
806
807
            # make ghost revision rev1c
808
            inv = inventory.Inventory()
809
            self.add_file(repo, inv, 'file2', 'rev1c', [])
810
811
            # make rev3 with file2
812
            # file2 refers to 'rev1c', which is a ghost in this repository, so
813
            # file2 cannot have rev1c as its ancestor.
814
            inv = inventory.Inventory()
815
            self.add_file(repo, inv, 'file2', 'rev3', ['rev1c'])
816
            self.add_revision(repo, 'rev3', inv, ['rev1c'])
817
            return repo
818
        finally:
819
            for cleanup in reversed(cleanups):
820
                cleanup()
821
822
    def add_revision(self, repo, revision_id, inv, parent_ids):
823
        inv.revision_id = revision_id
824
        inv.root.revision = revision_id
825
        repo.add_inventory(revision_id, inv, parent_ids)
826
        revision = _mod_revision.Revision(revision_id,
827
            committer='jrandom@example.com', timestamp=0, inventory_sha1='',
828
            timezone=0, message='foo', parent_ids=parent_ids)
829
        repo.add_revision(revision_id,revision, inv)
830
831
    def add_file(self, repo, inv, filename, revision, parents):
832
        file_id = filename + '-id'
833
        entry = inventory.InventoryFile(file_id, filename, 'TREE_ROOT')
834
        entry.revision = revision
2535.4.10 by Andrew Bennetts
Fix one failing test, disable another.
835
        entry.text_size = 0
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
836
        inv.add(entry)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
837
        text_key = (file_id, revision)
838
        parent_keys = [(file_id, parent) for parent in parents]
839
        repo.texts.add_lines(text_key, parent_keys, ['line\n'])
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
840
841
    def test_insert_from_broken_repo(self):
842
        """Inserting a data stream from a broken repository won't silently
843
        corrupt the target repository.
844
        """
845
        broken_repo = self.make_broken_repository()
846
        empty_repo = self.make_repository('empty-repo')
3830.3.25 by John Arbash Meinel
We changed the error that is raised when fetching from a broken repo.
847
        self.assertRaises((errors.RevisionNotPresent, errors.BzrCheckError),
848
                          empty_repo.fetch, broken_repo)
2592.3.214 by Robert Collins
Merge bzr.dev.
849
850
2592.3.84 by Robert Collins
Start of autopacking logic.
851
class TestRepositoryPackCollection(TestCaseWithTransport):
852
853
    def get_format(self):
3010.3.3 by Martin Pool
Merge trunk
854
        return bzrdir.format_registry.make_bzrdir('pack-0.92')
2592.3.84 by Robert Collins
Start of autopacking logic.
855
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
856
    def get_packs(self):
857
        format = self.get_format()
858
        repo = self.make_repository('.', format=format)
859
        return repo._pack_collection
860
3789.2.20 by John Arbash Meinel
The autopack code can now trigger itself to retry when _copy_revision_texts fails.
861
    def make_packs_and_alt_repo(self, write_lock=False):
3789.2.19 by John Arbash Meinel
Refactor to make the tests a bit simpler
862
        """Create a pack repo with 3 packs, and access it via a second repo."""
863
        tree = self.make_branch_and_tree('.')
864
        tree.lock_write()
865
        self.addCleanup(tree.unlock)
866
        rev1 = tree.commit('one')
867
        rev2 = tree.commit('two')
868
        rev3 = tree.commit('three')
869
        r = repository.Repository.open('.')
3789.2.20 by John Arbash Meinel
The autopack code can now trigger itself to retry when _copy_revision_texts fails.
870
        if write_lock:
871
            r.lock_write()
872
        else:
873
            r.lock_read()
3789.2.19 by John Arbash Meinel
Refactor to make the tests a bit simpler
874
        self.addCleanup(r.unlock)
875
        packs = r._pack_collection
876
        packs.ensure_loaded()
877
        return tree, r, packs, [rev1, rev2, rev3]
878
2592.3.84 by Robert Collins
Start of autopacking logic.
879
    def test__max_pack_count(self):
2592.3.219 by Robert Collins
Review feedback.
880
        """The maximum pack count is a function of the number of revisions."""
2592.3.84 by Robert Collins
Start of autopacking logic.
881
        # no revisions - one pack, so that we can have a revision free repo
882
        # without it blowing up
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
883
        packs = self.get_packs()
2592.3.84 by Robert Collins
Start of autopacking logic.
884
        self.assertEqual(1, packs._max_pack_count(0))
885
        # after that the sum of the digits, - check the first 1-9
886
        self.assertEqual(1, packs._max_pack_count(1))
887
        self.assertEqual(2, packs._max_pack_count(2))
888
        self.assertEqual(3, packs._max_pack_count(3))
889
        self.assertEqual(4, packs._max_pack_count(4))
890
        self.assertEqual(5, packs._max_pack_count(5))
891
        self.assertEqual(6, packs._max_pack_count(6))
892
        self.assertEqual(7, packs._max_pack_count(7))
893
        self.assertEqual(8, packs._max_pack_count(8))
894
        self.assertEqual(9, packs._max_pack_count(9))
895
        # check the boundary cases with two digits for the next decade
896
        self.assertEqual(1, packs._max_pack_count(10))
897
        self.assertEqual(2, packs._max_pack_count(11))
898
        self.assertEqual(10, packs._max_pack_count(19))
899
        self.assertEqual(2, packs._max_pack_count(20))
900
        self.assertEqual(3, packs._max_pack_count(21))
901
        # check some arbitrary big numbers
902
        self.assertEqual(25, packs._max_pack_count(112894))
903
904
    def test_pack_distribution_zero(self):
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
905
        packs = self.get_packs()
2592.3.84 by Robert Collins
Start of autopacking logic.
906
        self.assertEqual([0], packs.pack_distribution(0))
3052.1.6 by John Arbash Meinel
Change the lock check to raise ObjectNotLocked.
907
908
    def test_ensure_loaded_unlocked(self):
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
909
        packs = self.get_packs()
3052.1.6 by John Arbash Meinel
Change the lock check to raise ObjectNotLocked.
910
        self.assertRaises(errors.ObjectNotLocked,
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
911
                          packs.ensure_loaded)
3052.1.6 by John Arbash Meinel
Change the lock check to raise ObjectNotLocked.
912
2592.3.84 by Robert Collins
Start of autopacking logic.
913
    def test_pack_distribution_one_to_nine(self):
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
914
        packs = self.get_packs()
2592.3.84 by Robert Collins
Start of autopacking logic.
915
        self.assertEqual([1],
916
            packs.pack_distribution(1))
917
        self.assertEqual([1, 1],
918
            packs.pack_distribution(2))
919
        self.assertEqual([1, 1, 1],
920
            packs.pack_distribution(3))
921
        self.assertEqual([1, 1, 1, 1],
922
            packs.pack_distribution(4))
923
        self.assertEqual([1, 1, 1, 1, 1],
924
            packs.pack_distribution(5))
925
        self.assertEqual([1, 1, 1, 1, 1, 1],
926
            packs.pack_distribution(6))
927
        self.assertEqual([1, 1, 1, 1, 1, 1, 1],
928
            packs.pack_distribution(7))
929
        self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1],
930
            packs.pack_distribution(8))
931
        self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1, 1],
932
            packs.pack_distribution(9))
933
934
    def test_pack_distribution_stable_at_boundaries(self):
935
        """When there are multi-rev packs the counts are stable."""
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
936
        packs = self.get_packs()
2592.3.84 by Robert Collins
Start of autopacking logic.
937
        # in 10s:
938
        self.assertEqual([10], packs.pack_distribution(10))
939
        self.assertEqual([10, 1], packs.pack_distribution(11))
940
        self.assertEqual([10, 10], packs.pack_distribution(20))
941
        self.assertEqual([10, 10, 1], packs.pack_distribution(21))
942
        # 100s
943
        self.assertEqual([100], packs.pack_distribution(100))
944
        self.assertEqual([100, 1], packs.pack_distribution(101))
945
        self.assertEqual([100, 10, 1], packs.pack_distribution(111))
946
        self.assertEqual([100, 100], packs.pack_distribution(200))
947
        self.assertEqual([100, 100, 1], packs.pack_distribution(201))
948
        self.assertEqual([100, 100, 10, 1], packs.pack_distribution(211))
949
2592.3.85 by Robert Collins
Finish autopack corner cases.
950
    def test_plan_pack_operations_2009_revisions_skip_all_packs(self):
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
951
        packs = self.get_packs()
2592.3.85 by Robert Collins
Finish autopack corner cases.
952
        existing_packs = [(2000, "big"), (9, "medium")]
953
        # rev count - 2009 -> 2x1000 + 9x1
954
        pack_operations = packs.plan_autopack_combinations(
955
            existing_packs, [1000, 1000, 1, 1, 1, 1, 1, 1, 1, 1, 1])
956
        self.assertEqual([], pack_operations)
957
958
    def test_plan_pack_operations_2010_revisions_skip_all_packs(self):
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
959
        packs = self.get_packs()
2592.3.85 by Robert Collins
Finish autopack corner cases.
960
        existing_packs = [(2000, "big"), (9, "medium"), (1, "single")]
961
        # rev count - 2010 -> 2x1000 + 1x10
962
        pack_operations = packs.plan_autopack_combinations(
963
            existing_packs, [1000, 1000, 10])
964
        self.assertEqual([], pack_operations)
965
966
    def test_plan_pack_operations_2010_combines_smallest_two(self):
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
967
        packs = self.get_packs()
2592.3.85 by Robert Collins
Finish autopack corner cases.
968
        existing_packs = [(1999, "big"), (9, "medium"), (1, "single2"),
969
            (1, "single1")]
970
        # rev count - 2010 -> 2x1000 + 1x10 (3)
971
        pack_operations = packs.plan_autopack_combinations(
972
            existing_packs, [1000, 1000, 10])
3711.4.2 by John Arbash Meinel
Change the logic to solve it in a different way.
973
        self.assertEqual([[2, ["single2", "single1"]]], pack_operations)
2592.3.85 by Robert Collins
Finish autopack corner cases.
974
3711.4.2 by John Arbash Meinel
Change the logic to solve it in a different way.
975
    def test_plan_pack_operations_creates_a_single_op(self):
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
976
        packs = self.get_packs()
3711.4.2 by John Arbash Meinel
Change the logic to solve it in a different way.
977
        existing_packs = [(50, 'a'), (40, 'b'), (30, 'c'), (10, 'd'),
978
                          (10, 'e'), (6, 'f'), (4, 'g')]
979
        # rev count 150 -> 1x100 and 5x10
980
        # The two size 10 packs do not need to be touched. The 50, 40, 30 would
981
        # be combined into a single 120 size pack, and the 6 & 4 would
982
        # becombined into a size 10 pack. However, if we have to rewrite them,
983
        # we save a pack file with no increased I/O by putting them into the
984
        # same file.
985
        distribution = packs.pack_distribution(150)
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
986
        pack_operations = packs.plan_autopack_combinations(existing_packs,
3711.4.2 by John Arbash Meinel
Change the logic to solve it in a different way.
987
                                                           distribution)
988
        self.assertEqual([[130, ['a', 'b', 'c', 'f', 'g']]], pack_operations)
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
989
2592.3.173 by Robert Collins
Basic implementation of all_packs.
990
    def test_all_packs_none(self):
991
        format = self.get_format()
992
        tree = self.make_branch_and_tree('.', format=format)
993
        tree.lock_read()
994
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
995
        packs = tree.branch.repository._pack_collection
2592.3.173 by Robert Collins
Basic implementation of all_packs.
996
        packs.ensure_loaded()
997
        self.assertEqual([], packs.all_packs())
998
999
    def test_all_packs_one(self):
1000
        format = self.get_format()
1001
        tree = self.make_branch_and_tree('.', format=format)
1002
        tree.commit('start')
1003
        tree.lock_read()
1004
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1005
        packs = tree.branch.repository._pack_collection
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1006
        packs.ensure_loaded()
2592.3.176 by Robert Collins
Various pack refactorings.
1007
        self.assertEqual([
1008
            packs.get_pack_by_name(packs.names()[0])],
1009
            packs.all_packs())
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1010
1011
    def test_all_packs_two(self):
1012
        format = self.get_format()
1013
        tree = self.make_branch_and_tree('.', format=format)
1014
        tree.commit('start')
1015
        tree.commit('continue')
1016
        tree.lock_read()
1017
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1018
        packs = tree.branch.repository._pack_collection
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1019
        packs.ensure_loaded()
1020
        self.assertEqual([
2592.3.176 by Robert Collins
Various pack refactorings.
1021
            packs.get_pack_by_name(packs.names()[0]),
1022
            packs.get_pack_by_name(packs.names()[1]),
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1023
            ], packs.all_packs())
1024
2592.3.176 by Robert Collins
Various pack refactorings.
1025
    def test_get_pack_by_name(self):
1026
        format = self.get_format()
1027
        tree = self.make_branch_and_tree('.', format=format)
1028
        tree.commit('start')
1029
        tree.lock_read()
1030
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
1031
        packs = tree.branch.repository._pack_collection
2592.3.176 by Robert Collins
Various pack refactorings.
1032
        packs.ensure_loaded()
1033
        name = packs.names()[0]
1034
        pack_1 = packs.get_pack_by_name(name)
1035
        # the pack should be correctly initialised
3517.4.5 by Martin Pool
Correct use of packs._names in test_get_pack_by_name
1036
        sizes = packs._names[name]
3221.12.4 by Robert Collins
Implement basic repository supporting external references.
1037
        rev_index = GraphIndex(packs._index_transport, name + '.rix', sizes[0])
1038
        inv_index = GraphIndex(packs._index_transport, name + '.iix', sizes[1])
1039
        txt_index = GraphIndex(packs._index_transport, name + '.tix', sizes[2])
1040
        sig_index = GraphIndex(packs._index_transport, name + '.six', sizes[3])
2592.3.191 by Robert Collins
Give Pack responsibility for index naming, and two concrete classes - NewPack for new packs and ExistingPack for packs we read from disk.
1041
        self.assertEqual(pack_repo.ExistingPack(packs._pack_transport,
2592.3.219 by Robert Collins
Review feedback.
1042
            name, rev_index, inv_index, txt_index, sig_index), pack_1)
2592.3.176 by Robert Collins
Various pack refactorings.
1043
        # and the same instance should be returned on successive calls.
1044
        self.assertTrue(pack_1 is packs.get_pack_by_name(name))
1045
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1046
    def test_reload_pack_names_new_entry(self):
3789.2.19 by John Arbash Meinel
Refactor to make the tests a bit simpler
1047
        tree, r, packs, revs = self.make_packs_and_alt_repo()
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1048
        names = packs.names()
1049
        # Add a new pack file into the repository
3789.2.19 by John Arbash Meinel
Refactor to make the tests a bit simpler
1050
        rev4 = tree.commit('four')
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1051
        new_names = tree.branch.repository._pack_collection.names()
1052
        new_name = set(new_names).difference(names)
1053
        self.assertEqual(1, len(new_name))
1054
        new_name = new_name.pop()
1055
        # The old collection hasn't noticed yet
1056
        self.assertEqual(names, packs.names())
3789.1.8 by John Arbash Meinel
Change the api of reload_pack_names().
1057
        self.assertTrue(packs.reload_pack_names())
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1058
        self.assertEqual(new_names, packs.names())
1059
        # And the repository can access the new revision
3789.2.19 by John Arbash Meinel
Refactor to make the tests a bit simpler
1060
        self.assertEqual({rev4:(revs[-1],)}, r.get_parent_map([rev4]))
3789.1.8 by John Arbash Meinel
Change the api of reload_pack_names().
1061
        self.assertFalse(packs.reload_pack_names())
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1062
1063
    def test_reload_pack_names_added_and_removed(self):
3789.2.19 by John Arbash Meinel
Refactor to make the tests a bit simpler
1064
        tree, r, packs, revs = self.make_packs_and_alt_repo()
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1065
        names = packs.names()
1066
        # Now repack the whole thing
1067
        tree.branch.repository.pack()
1068
        new_names = tree.branch.repository._pack_collection.names()
1069
        # The other collection hasn't noticed yet
1070
        self.assertEqual(names, packs.names())
3789.1.8 by John Arbash Meinel
Change the api of reload_pack_names().
1071
        self.assertTrue(packs.reload_pack_names())
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1072
        self.assertEqual(new_names, packs.names())
3789.2.19 by John Arbash Meinel
Refactor to make the tests a bit simpler
1073
        self.assertEqual({revs[-1]:(revs[-2],)}, r.get_parent_map([revs[-1]]))
3789.1.8 by John Arbash Meinel
Change the api of reload_pack_names().
1074
        self.assertFalse(packs.reload_pack_names())
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1075
3789.2.20 by John Arbash Meinel
The autopack code can now trigger itself to retry when _copy_revision_texts fails.
1076
    def test_autopack_reloads_and_stops(self):
1077
        tree, r, packs, revs = self.make_packs_and_alt_repo(write_lock=True)
1078
        # After we have determined what needs to be autopacked, trigger a
1079
        # full-pack via the other repo which will cause us to re-evaluate and
1080
        # decide we don't need to do anything
1081
        orig_execute = packs._execute_pack_operations
1082
        def _munged_execute_pack_ops(*args, **kwargs):
1083
            tree.branch.repository.pack()
1084
            return orig_execute(*args, **kwargs)
1085
        packs._execute_pack_operations = _munged_execute_pack_ops
1086
        packs._max_pack_count = lambda x: 1
1087
        packs.pack_distribution = lambda x: [10]
1088
        self.assertFalse(packs.autopack())
1089
        self.assertEqual(1, len(packs.names()))
1090
        self.assertEqual(tree.branch.repository._pack_collection.names(),
1091
                         packs.names())
1092
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1093
1094
class TestPack(TestCaseWithTransport):
1095
    """Tests for the Pack object."""
1096
1097
    def assertCurrentlyEqual(self, left, right):
1098
        self.assertTrue(left == right)
1099
        self.assertTrue(right == left)
1100
        self.assertFalse(left != right)
1101
        self.assertFalse(right != left)
1102
1103
    def assertCurrentlyNotEqual(self, left, right):
1104
        self.assertFalse(left == right)
1105
        self.assertFalse(right == left)
1106
        self.assertTrue(left != right)
1107
        self.assertTrue(right != left)
1108
1109
    def test___eq____ne__(self):
2592.3.191 by Robert Collins
Give Pack responsibility for index naming, and two concrete classes - NewPack for new packs and ExistingPack for packs we read from disk.
1110
        left = pack_repo.ExistingPack('', '', '', '', '', '')
1111
        right = pack_repo.ExistingPack('', '', '', '', '', '')
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1112
        self.assertCurrentlyEqual(left, right)
1113
        # change all attributes and ensure equality changes as we do.
1114
        left.revision_index = 'a'
1115
        self.assertCurrentlyNotEqual(left, right)
1116
        right.revision_index = 'a'
1117
        self.assertCurrentlyEqual(left, right)
1118
        left.inventory_index = 'a'
1119
        self.assertCurrentlyNotEqual(left, right)
1120
        right.inventory_index = 'a'
1121
        self.assertCurrentlyEqual(left, right)
1122
        left.text_index = 'a'
1123
        self.assertCurrentlyNotEqual(left, right)
1124
        right.text_index = 'a'
1125
        self.assertCurrentlyEqual(left, right)
1126
        left.signature_index = 'a'
1127
        self.assertCurrentlyNotEqual(left, right)
1128
        right.signature_index = 'a'
1129
        self.assertCurrentlyEqual(left, right)
1130
        left.name = 'a'
1131
        self.assertCurrentlyNotEqual(left, right)
1132
        right.name = 'a'
1133
        self.assertCurrentlyEqual(left, right)
1134
        left.transport = 'a'
1135
        self.assertCurrentlyNotEqual(left, right)
1136
        right.transport = 'a'
1137
        self.assertCurrentlyEqual(left, right)
2592.3.179 by Robert Collins
Generate the revision_index_map for packing during the core operation, from the pack objects.
1138
1139
    def test_file_name(self):
2592.3.191 by Robert Collins
Give Pack responsibility for index naming, and two concrete classes - NewPack for new packs and ExistingPack for packs we read from disk.
1140
        pack = pack_repo.ExistingPack('', 'a_name', '', '', '', '')
2592.3.179 by Robert Collins
Generate the revision_index_map for packing during the core operation, from the pack objects.
1141
        self.assertEqual('a_name.pack', pack.file_name())
2592.3.192 by Robert Collins
Move new revision index management to NewPack.
1142
1143
1144
class TestNewPack(TestCaseWithTransport):
1145
    """Tests for pack_repo.NewPack."""
1146
2592.3.193 by Robert Collins
Move hash tracking of new packs into NewPack.
1147
    def test_new_instance_attributes(self):
2592.3.194 by Robert Collins
Output the revision index from NewPack.finish
1148
        upload_transport = self.get_transport('upload')
1149
        pack_transport = self.get_transport('pack')
1150
        index_transport = self.get_transport('index')
1151
        upload_transport.mkdir('.')
3830.3.1 by Martin Pool
NewPack should be constructed from the PackCollection, rather than attributes of it
1152
        collection = pack_repo.RepositoryPackCollection(repo=None,
1153
            transport=self.get_transport('.'),
1154
            index_transport=index_transport,
1155
            upload_transport=upload_transport,
1156
            pack_transport=pack_transport,
1157
            index_builder_class=BTreeBuilder,
3735.1.1 by Robert Collins
Add development2 formats using BTree indices.
1158
            index_class=BTreeGraphIndex)
3830.3.1 by Martin Pool
NewPack should be constructed from the PackCollection, rather than attributes of it
1159
        pack = pack_repo.NewPack(collection)
3735.1.1 by Robert Collins
Add development2 formats using BTree indices.
1160
        self.assertIsInstance(pack.revision_index, BTreeBuilder)
1161
        self.assertIsInstance(pack.inventory_index, BTreeBuilder)
2929.3.5 by Vincent Ladeuil
New files, same warnings, same fixes.
1162
        self.assertIsInstance(pack._hash, type(osutils.md5()))
2592.3.194 by Robert Collins
Output the revision index from NewPack.finish
1163
        self.assertTrue(pack.upload_transport is upload_transport)
1164
        self.assertTrue(pack.index_transport is index_transport)
1165
        self.assertTrue(pack.pack_transport is pack_transport)
1166
        self.assertEqual(None, pack.index_sizes)
1167
        self.assertEqual(20, len(pack.random_name))
1168
        self.assertIsInstance(pack.random_name, str)
1169
        self.assertIsInstance(pack.start_time, float)
2951.1.2 by Robert Collins
Partial refactoring of pack_repo to create a Packer object for packing.
1170
1171
1172
class TestPacker(TestCaseWithTransport):
1173
    """Tests for the packs repository Packer class."""
2951.1.10 by Robert Collins
Peer review feedback with Ian.
1174
3824.2.4 by John Arbash Meinel
Add a test that ensures the pack ordering changes as part of calling .pack()
1175
    def test_pack_optimizes_pack_order(self):
1176
        builder = self.make_branch_builder('.')
1177
        builder.start_series()
1178
        builder.build_snapshot('A', None, [
1179
            ('add', ('', 'root-id', 'directory', None)),
1180
            ('add', ('f', 'f-id', 'file', 'content\n'))])
1181
        builder.build_snapshot('B', ['A'],
1182
            [('modify', ('f-id', 'new-content\n'))])
1183
        builder.build_snapshot('C', ['B'],
1184
            [('modify', ('f-id', 'third-content\n'))])
1185
        builder.build_snapshot('D', ['C'],
1186
            [('modify', ('f-id', 'fourth-content\n'))])
1187
        b = builder.get_branch()
1188
        b.lock_read()
1189
        builder.finish_series()
1190
        self.addCleanup(b.unlock)
1191
        # At this point, we should have 4 pack files available
1192
        # Because of how they were built, they correspond to
1193
        # ['D', 'C', 'B', 'A']
1194
        packs = b.repository._pack_collection.packs
1195
        packer = pack_repo.Packer(b.repository._pack_collection,
1196
                                  packs, 'testing',
1197
                                  revision_ids=['B', 'C'])
1198
        # Now, when we are copying the B & C revisions, their pack files should
1199
        # be moved to the front of the stack
3824.2.5 by Andrew Bennetts
Minor tweaks to comments etc.
1200
        # The new ordering moves B & C to the front of the .packs attribute,
1201
        # and leaves the others in the original order.
3824.2.4 by John Arbash Meinel
Add a test that ensures the pack ordering changes as part of calling .pack()
1202
        new_packs = [packs[1], packs[2], packs[0], packs[3]]
1203
        new_pack = packer.pack()
1204
        self.assertEqual(new_packs, packer.packs)
3146.6.1 by Aaron Bentley
InterDifferingSerializer shows a progress bar
1205
1206
3777.5.4 by John Arbash Meinel
OptimisingPacker now sets the optimize flags for the indexes being built.
1207
class TestOptimisingPacker(TestCaseWithTransport):
1208
    """Tests for the OptimisingPacker class."""
1209
1210
    def get_pack_collection(self):
1211
        repo = self.make_repository('.')
1212
        return repo._pack_collection
1213
1214
    def test_open_pack_will_optimise(self):
1215
        packer = pack_repo.OptimisingPacker(self.get_pack_collection(),
1216
                                            [], '.test')
1217
        new_pack = packer.open_pack()
1218
        self.assertIsInstance(new_pack, pack_repo.NewPack)
1219
        self.assertTrue(new_pack.revision_index._optimize_for_size)
1220
        self.assertTrue(new_pack.inventory_index._optimize_for_size)
1221
        self.assertTrue(new_pack.text_index._optimize_for_size)
1222
        self.assertTrue(new_pack.signature_index._optimize_for_size)
1223
1224
3146.6.1 by Aaron Bentley
InterDifferingSerializer shows a progress bar
1225
class TestInterDifferingSerializer(TestCaseWithTransport):
1226
1227
    def test_progress_bar(self):
1228
        tree = self.make_branch_and_tree('tree')
1229
        tree.commit('rev1', rev_id='rev-1')
1230
        tree.commit('rev2', rev_id='rev-2')
1231
        tree.commit('rev3', rev_id='rev-3')
1232
        repo = self.make_repository('repo')
1233
        inter_repo = repository.InterDifferingSerializer(
1234
            tree.branch.repository, repo)
1235
        pb = progress.InstrumentedProgress(to_file=StringIO())
1236
        pb.never_throttle = True
1237
        inter_repo.fetch('rev-1', pb)
1238
        self.assertEqual('Transferring revisions', pb.last_msg)
1239
        self.assertEqual(1, pb.last_cnt)
1240
        self.assertEqual(1, pb.last_total)
1241
        inter_repo.fetch('rev-3', pb)
1242
        self.assertEqual(2, pb.last_cnt)
1243
        self.assertEqual(2, pb.last_total)