/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,
3734.2.4 by Vincent Ladeuil
Fix python2.6 deprecation warnings related to hashlib.
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
2305.2.3 by Andrew Bennetts
Bring across test_repository improvements from the hpss branch to fix the last test failures.
473
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.4.1 by Andrew Bennetts
Add _find_revision_outside_set.
693
class TestDevelopment3FindRevisionOutsideSet(TestCaseWithTransport):
694
    """Tests for _find_revision_outside_set."""
695
696
    def setUp(self):
697
        super(TestDevelopment3FindRevisionOutsideSet, self).setUp()
698
        self.builder = self.make_branch_builder('source', format='development3')
699
        self.builder.start_series()
700
        self.builder.build_snapshot('initial', None,
701
            [('add', ('', 'tree-root', 'directory', None))])
702
        self.repo = self.builder.get_branch().repository
703
        self.addCleanup(self.builder.finish_series)
704
        
705
    def assertRevisionOutsideSet(self, expected_result, rev_set):
706
        self.assertEqual(
707
            expected_result, self.repo._find_revision_outside_set(rev_set))
708
709
    def test_simple(self):
710
        self.builder.build_snapshot('revid1', None, [])
711
        self.builder.build_snapshot('revid2', None, [])
712
        rev_set = ['revid2']
713
        self.assertRevisionOutsideSet('revid1', rev_set)
714
715
    def test_not_first_parent(self):
716
        self.builder.build_snapshot('revid1', None, [])
717
        self.builder.build_snapshot('revid2', None, [])
718
        self.builder.build_snapshot('revid3', None, [])
719
        rev_set = ['revid3', 'revid2']
720
        self.assertRevisionOutsideSet('revid1', rev_set)
721
722
    def test_not_null(self):
723
        rev_set = ['initial']
724
        self.assertRevisionOutsideSet(_mod_revision.NULL_REVISION, rev_set)
725
726
    def test_not_null_set(self):
727
        self.builder.build_snapshot('revid1', None, [])
728
        rev_set = [_mod_revision.NULL_REVISION]
729
        self.assertRevisionOutsideSet(_mod_revision.NULL_REVISION, rev_set)
730
731
    def test_ghost(self):
732
        self.builder.build_snapshot('revid1', None, [])
733
        rev_set = ['ghost', 'revid1']
734
        self.assertRevisionOutsideSet('initial', rev_set)
735
736
    def test_ghost_parent(self):
737
        self.builder.build_snapshot('revid1', None, [])
738
        self.builder.build_snapshot('revid2', ['revid1', 'ghost'], [])
739
        rev_set = ['revid2', 'revid1']
740
        self.assertRevisionOutsideSet('initial', rev_set)
741
742
    def test_righthand_parent(self):
743
        self.builder.build_snapshot('revid1', None, [])
744
        self.builder.build_snapshot('revid2a', ['revid1'], [])
745
        self.builder.build_snapshot('revid2b', ['revid1'], [])
746
        self.builder.build_snapshot('revid3', ['revid2a', 'revid2b'], [])
747
        rev_set = ['revid3', 'revid2a']
748
        self.assertRevisionOutsideSet('revid2b', rev_set)
749
750
2535.3.57 by Andrew Bennetts
Perform some sanity checking of data streams rather than blindly inserting them into our repository.
751
class TestWithBrokenRepo(TestCaseWithTransport):
2592.3.214 by Robert Collins
Merge bzr.dev.
752
    """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.
753
754
    def make_broken_repository(self):
755
        # XXX: This function is borrowed from Aaron's "Reconcile can fix bad
756
        # parent references" branch which is due to land in bzr.dev soon.  Once
757
        # it does, this duplication should be removed.
758
        repo = self.make_repository('broken-repo')
759
        cleanups = []
760
        try:
761
            repo.lock_write()
762
            cleanups.append(repo.unlock)
763
            repo.start_write_group()
764
            cleanups.append(repo.commit_write_group)
765
            # make rev1a: A well-formed revision, containing 'file1'
766
            inv = inventory.Inventory(revision_id='rev1a')
767
            inv.root.revision = 'rev1a'
768
            self.add_file(repo, inv, 'file1', 'rev1a', [])
769
            repo.add_inventory('rev1a', inv, [])
770
            revision = _mod_revision.Revision('rev1a',
771
                committer='jrandom@example.com', timestamp=0,
772
                inventory_sha1='', timezone=0, message='foo', parent_ids=[])
773
            repo.add_revision('rev1a',revision, inv)
774
775
            # make rev1b, which has no Revision, but has an Inventory, and
776
            # file1
777
            inv = inventory.Inventory(revision_id='rev1b')
778
            inv.root.revision = 'rev1b'
779
            self.add_file(repo, inv, 'file1', 'rev1b', [])
780
            repo.add_inventory('rev1b', inv, [])
781
782
            # make rev2, with file1 and file2
783
            # file2 is sane
784
            # file1 has 'rev1b' as an ancestor, even though this is not
785
            # mentioned by 'rev1a', making it an unreferenced ancestor
786
            inv = inventory.Inventory()
787
            self.add_file(repo, inv, 'file1', 'rev2', ['rev1a', 'rev1b'])
788
            self.add_file(repo, inv, 'file2', 'rev2', [])
789
            self.add_revision(repo, 'rev2', inv, ['rev1a'])
790
791
            # make ghost revision rev1c
792
            inv = inventory.Inventory()
793
            self.add_file(repo, inv, 'file2', 'rev1c', [])
794
795
            # make rev3 with file2
796
            # file2 refers to 'rev1c', which is a ghost in this repository, so
797
            # file2 cannot have rev1c as its ancestor.
798
            inv = inventory.Inventory()
799
            self.add_file(repo, inv, 'file2', 'rev3', ['rev1c'])
800
            self.add_revision(repo, 'rev3', inv, ['rev1c'])
801
            return repo
802
        finally:
803
            for cleanup in reversed(cleanups):
804
                cleanup()
805
806
    def add_revision(self, repo, revision_id, inv, parent_ids):
807
        inv.revision_id = revision_id
808
        inv.root.revision = revision_id
809
        repo.add_inventory(revision_id, inv, parent_ids)
810
        revision = _mod_revision.Revision(revision_id,
811
            committer='jrandom@example.com', timestamp=0, inventory_sha1='',
812
            timezone=0, message='foo', parent_ids=parent_ids)
813
        repo.add_revision(revision_id,revision, inv)
814
815
    def add_file(self, repo, inv, filename, revision, parents):
816
        file_id = filename + '-id'
817
        entry = inventory.InventoryFile(file_id, filename, 'TREE_ROOT')
818
        entry.revision = revision
2535.4.10 by Andrew Bennetts
Fix one failing test, disable another.
819
        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.
820
        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.
821
        text_key = (file_id, revision)
822
        parent_keys = [(file_id, parent) for parent in parents]
823
        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.
824
825
    def test_insert_from_broken_repo(self):
826
        """Inserting a data stream from a broken repository won't silently
827
        corrupt the target repository.
828
        """
829
        broken_repo = self.make_broken_repository()
830
        empty_repo = self.make_repository('empty-repo')
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.
831
        self.assertRaises(errors.RevisionNotPresent, empty_repo.fetch, broken_repo)
2592.3.214 by Robert Collins
Merge bzr.dev.
832
833
2592.3.84 by Robert Collins
Start of autopacking logic.
834
class TestRepositoryPackCollection(TestCaseWithTransport):
835
836
    def get_format(self):
3010.3.3 by Martin Pool
Merge trunk
837
        return bzrdir.format_registry.make_bzrdir('pack-0.92')
2592.3.84 by Robert Collins
Start of autopacking logic.
838
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
839
    def get_packs(self):
840
        format = self.get_format()
841
        repo = self.make_repository('.', format=format)
842
        return repo._pack_collection
843
2592.3.84 by Robert Collins
Start of autopacking logic.
844
    def test__max_pack_count(self):
2592.3.219 by Robert Collins
Review feedback.
845
        """The maximum pack count is a function of the number of revisions."""
2592.3.84 by Robert Collins
Start of autopacking logic.
846
        # no revisions - one pack, so that we can have a revision free repo
847
        # without it blowing up
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
848
        packs = self.get_packs()
2592.3.84 by Robert Collins
Start of autopacking logic.
849
        self.assertEqual(1, packs._max_pack_count(0))
850
        # after that the sum of the digits, - check the first 1-9
851
        self.assertEqual(1, packs._max_pack_count(1))
852
        self.assertEqual(2, packs._max_pack_count(2))
853
        self.assertEqual(3, packs._max_pack_count(3))
854
        self.assertEqual(4, packs._max_pack_count(4))
855
        self.assertEqual(5, packs._max_pack_count(5))
856
        self.assertEqual(6, packs._max_pack_count(6))
857
        self.assertEqual(7, packs._max_pack_count(7))
858
        self.assertEqual(8, packs._max_pack_count(8))
859
        self.assertEqual(9, packs._max_pack_count(9))
860
        # check the boundary cases with two digits for the next decade
861
        self.assertEqual(1, packs._max_pack_count(10))
862
        self.assertEqual(2, packs._max_pack_count(11))
863
        self.assertEqual(10, packs._max_pack_count(19))
864
        self.assertEqual(2, packs._max_pack_count(20))
865
        self.assertEqual(3, packs._max_pack_count(21))
866
        # check some arbitrary big numbers
867
        self.assertEqual(25, packs._max_pack_count(112894))
868
869
    def test_pack_distribution_zero(self):
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
870
        packs = self.get_packs()
2592.3.84 by Robert Collins
Start of autopacking logic.
871
        self.assertEqual([0], packs.pack_distribution(0))
3052.1.6 by John Arbash Meinel
Change the lock check to raise ObjectNotLocked.
872
873
    def test_ensure_loaded_unlocked(self):
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
874
        packs = self.get_packs()
3052.1.6 by John Arbash Meinel
Change the lock check to raise ObjectNotLocked.
875
        self.assertRaises(errors.ObjectNotLocked,
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
876
                          packs.ensure_loaded)
3052.1.6 by John Arbash Meinel
Change the lock check to raise ObjectNotLocked.
877
2592.3.84 by Robert Collins
Start of autopacking logic.
878
    def test_pack_distribution_one_to_nine(self):
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
879
        packs = self.get_packs()
2592.3.84 by Robert Collins
Start of autopacking logic.
880
        self.assertEqual([1],
881
            packs.pack_distribution(1))
882
        self.assertEqual([1, 1],
883
            packs.pack_distribution(2))
884
        self.assertEqual([1, 1, 1],
885
            packs.pack_distribution(3))
886
        self.assertEqual([1, 1, 1, 1],
887
            packs.pack_distribution(4))
888
        self.assertEqual([1, 1, 1, 1, 1],
889
            packs.pack_distribution(5))
890
        self.assertEqual([1, 1, 1, 1, 1, 1],
891
            packs.pack_distribution(6))
892
        self.assertEqual([1, 1, 1, 1, 1, 1, 1],
893
            packs.pack_distribution(7))
894
        self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1],
895
            packs.pack_distribution(8))
896
        self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1, 1],
897
            packs.pack_distribution(9))
898
899
    def test_pack_distribution_stable_at_boundaries(self):
900
        """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,
901
        packs = self.get_packs()
2592.3.84 by Robert Collins
Start of autopacking logic.
902
        # in 10s:
903
        self.assertEqual([10], packs.pack_distribution(10))
904
        self.assertEqual([10, 1], packs.pack_distribution(11))
905
        self.assertEqual([10, 10], packs.pack_distribution(20))
906
        self.assertEqual([10, 10, 1], packs.pack_distribution(21))
907
        # 100s
908
        self.assertEqual([100], packs.pack_distribution(100))
909
        self.assertEqual([100, 1], packs.pack_distribution(101))
910
        self.assertEqual([100, 10, 1], packs.pack_distribution(111))
911
        self.assertEqual([100, 100], packs.pack_distribution(200))
912
        self.assertEqual([100, 100, 1], packs.pack_distribution(201))
913
        self.assertEqual([100, 100, 10, 1], packs.pack_distribution(211))
914
2592.3.85 by Robert Collins
Finish autopack corner cases.
915
    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,
916
        packs = self.get_packs()
2592.3.85 by Robert Collins
Finish autopack corner cases.
917
        existing_packs = [(2000, "big"), (9, "medium")]
918
        # rev count - 2009 -> 2x1000 + 9x1
919
        pack_operations = packs.plan_autopack_combinations(
920
            existing_packs, [1000, 1000, 1, 1, 1, 1, 1, 1, 1, 1, 1])
921
        self.assertEqual([], pack_operations)
922
923
    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,
924
        packs = self.get_packs()
2592.3.85 by Robert Collins
Finish autopack corner cases.
925
        existing_packs = [(2000, "big"), (9, "medium"), (1, "single")]
926
        # rev count - 2010 -> 2x1000 + 1x10
927
        pack_operations = packs.plan_autopack_combinations(
928
            existing_packs, [1000, 1000, 10])
929
        self.assertEqual([], pack_operations)
930
931
    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,
932
        packs = self.get_packs()
2592.3.85 by Robert Collins
Finish autopack corner cases.
933
        existing_packs = [(1999, "big"), (9, "medium"), (1, "single2"),
934
            (1, "single1")]
935
        # rev count - 2010 -> 2x1000 + 1x10 (3)
936
        pack_operations = packs.plan_autopack_combinations(
937
            existing_packs, [1000, 1000, 10])
3711.4.2 by John Arbash Meinel
Change the logic to solve it in a different way.
938
        self.assertEqual([[2, ["single2", "single1"]]], pack_operations)
2592.3.85 by Robert Collins
Finish autopack corner cases.
939
3711.4.2 by John Arbash Meinel
Change the logic to solve it in a different way.
940
    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,
941
        packs = self.get_packs()
3711.4.2 by John Arbash Meinel
Change the logic to solve it in a different way.
942
        existing_packs = [(50, 'a'), (40, 'b'), (30, 'c'), (10, 'd'),
943
                          (10, 'e'), (6, 'f'), (4, 'g')]
944
        # rev count 150 -> 1x100 and 5x10
945
        # The two size 10 packs do not need to be touched. The 50, 40, 30 would
946
        # be combined into a single 120 size pack, and the 6 & 4 would
947
        # becombined into a size 10 pack. However, if we have to rewrite them,
948
        # we save a pack file with no increased I/O by putting them into the
949
        # same file.
950
        distribution = packs.pack_distribution(150)
3711.4.1 by John Arbash Meinel
Fix bug #242510, when determining the autopack sequence,
951
        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.
952
                                                           distribution)
953
        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,
954
2592.3.173 by Robert Collins
Basic implementation of all_packs.
955
    def test_all_packs_none(self):
956
        format = self.get_format()
957
        tree = self.make_branch_and_tree('.', format=format)
958
        tree.lock_read()
959
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
960
        packs = tree.branch.repository._pack_collection
2592.3.173 by Robert Collins
Basic implementation of all_packs.
961
        packs.ensure_loaded()
962
        self.assertEqual([], packs.all_packs())
963
964
    def test_all_packs_one(self):
965
        format = self.get_format()
966
        tree = self.make_branch_and_tree('.', format=format)
967
        tree.commit('start')
968
        tree.lock_read()
969
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
970
        packs = tree.branch.repository._pack_collection
2592.3.173 by Robert Collins
Basic implementation of all_packs.
971
        packs.ensure_loaded()
2592.3.176 by Robert Collins
Various pack refactorings.
972
        self.assertEqual([
973
            packs.get_pack_by_name(packs.names()[0])],
974
            packs.all_packs())
2592.3.173 by Robert Collins
Basic implementation of all_packs.
975
976
    def test_all_packs_two(self):
977
        format = self.get_format()
978
        tree = self.make_branch_and_tree('.', format=format)
979
        tree.commit('start')
980
        tree.commit('continue')
981
        tree.lock_read()
982
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
983
        packs = tree.branch.repository._pack_collection
2592.3.173 by Robert Collins
Basic implementation of all_packs.
984
        packs.ensure_loaded()
985
        self.assertEqual([
2592.3.176 by Robert Collins
Various pack refactorings.
986
            packs.get_pack_by_name(packs.names()[0]),
987
            packs.get_pack_by_name(packs.names()[1]),
2592.3.173 by Robert Collins
Basic implementation of all_packs.
988
            ], packs.all_packs())
989
2592.3.176 by Robert Collins
Various pack refactorings.
990
    def test_get_pack_by_name(self):
991
        format = self.get_format()
992
        tree = self.make_branch_and_tree('.', format=format)
993
        tree.commit('start')
994
        tree.lock_read()
995
        self.addCleanup(tree.unlock)
2592.3.232 by Martin Pool
Disambiguate two member variables called _packs into _packs_by_name and _pack_collection
996
        packs = tree.branch.repository._pack_collection
2592.3.176 by Robert Collins
Various pack refactorings.
997
        packs.ensure_loaded()
998
        name = packs.names()[0]
999
        pack_1 = packs.get_pack_by_name(name)
1000
        # the pack should be correctly initialised
3517.4.5 by Martin Pool
Correct use of packs._names in test_get_pack_by_name
1001
        sizes = packs._names[name]
3221.12.4 by Robert Collins
Implement basic repository supporting external references.
1002
        rev_index = GraphIndex(packs._index_transport, name + '.rix', sizes[0])
1003
        inv_index = GraphIndex(packs._index_transport, name + '.iix', sizes[1])
1004
        txt_index = GraphIndex(packs._index_transport, name + '.tix', sizes[2])
1005
        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.
1006
        self.assertEqual(pack_repo.ExistingPack(packs._pack_transport,
2592.3.219 by Robert Collins
Review feedback.
1007
            name, rev_index, inv_index, txt_index, sig_index), pack_1)
2592.3.176 by Robert Collins
Various pack refactorings.
1008
        # and the same instance should be returned on successive calls.
1009
        self.assertTrue(pack_1 is packs.get_pack_by_name(name))
1010
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1011
    def test_reload_pack_names_new_entry(self):
1012
        tree = self.make_branch_and_tree('.')
1013
        tree.lock_write()
1014
        self.addCleanup(tree.unlock)
1015
        rev1 = tree.commit('one')
1016
        rev2 = tree.commit('two')
1017
        r = repository.Repository.open('.')
1018
        r.lock_read()
1019
        self.addCleanup(r.unlock)
1020
        packs = r._pack_collection
1021
        packs.ensure_loaded()
1022
        names = packs.names()
1023
        # Add a new pack file into the repository
1024
        rev3 = tree.commit('three')
1025
        new_names = tree.branch.repository._pack_collection.names()
1026
        new_name = set(new_names).difference(names)
1027
        self.assertEqual(1, len(new_name))
1028
        new_name = new_name.pop()
1029
        # The old collection hasn't noticed yet
1030
        self.assertEqual(names, packs.names())
3789.1.8 by John Arbash Meinel
Change the api of reload_pack_names().
1031
        self.assertTrue(packs.reload_pack_names())
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1032
        self.assertEqual(new_names, packs.names())
1033
        # And the repository can access the new revision
1034
        self.assertEqual({rev3:(rev2,)}, r.get_parent_map([rev3]))
3789.1.8 by John Arbash Meinel
Change the api of reload_pack_names().
1035
        self.assertFalse(packs.reload_pack_names())
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1036
1037
    def test_reload_pack_names_added_and_removed(self):
1038
        tree = self.make_branch_and_tree('.')
1039
        tree.lock_write()
1040
        self.addCleanup(tree.unlock)
1041
        rev1 = tree.commit('one')
1042
        rev2 = tree.commit('two')
1043
        r = repository.Repository.open('.')
1044
        r.lock_read()
1045
        self.addCleanup(r.unlock)
1046
        packs = r._pack_collection
1047
        packs.ensure_loaded()
1048
        names = packs.names()
1049
        # Now repack the whole thing
1050
        tree.branch.repository.pack()
1051
        new_names = tree.branch.repository._pack_collection.names()
1052
        # The other collection hasn't noticed yet
1053
        self.assertEqual(names, packs.names())
3789.1.8 by John Arbash Meinel
Change the api of reload_pack_names().
1054
        self.assertTrue(packs.reload_pack_names())
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1055
        self.assertEqual(new_names, packs.names())
1056
        self.assertEqual({rev2:(rev1,)}, r.get_parent_map([rev2]))
3789.1.8 by John Arbash Meinel
Change the api of reload_pack_names().
1057
        self.assertFalse(packs.reload_pack_names())
3789.1.2 by John Arbash Meinel
Add RepositoryPackCollection.reload_pack_names()
1058
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1059
1060
class TestPack(TestCaseWithTransport):
1061
    """Tests for the Pack object."""
1062
1063
    def assertCurrentlyEqual(self, left, right):
1064
        self.assertTrue(left == right)
1065
        self.assertTrue(right == left)
1066
        self.assertFalse(left != right)
1067
        self.assertFalse(right != left)
1068
1069
    def assertCurrentlyNotEqual(self, left, right):
1070
        self.assertFalse(left == right)
1071
        self.assertFalse(right == left)
1072
        self.assertTrue(left != right)
1073
        self.assertTrue(right != left)
1074
1075
    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.
1076
        left = pack_repo.ExistingPack('', '', '', '', '', '')
1077
        right = pack_repo.ExistingPack('', '', '', '', '', '')
2592.3.173 by Robert Collins
Basic implementation of all_packs.
1078
        self.assertCurrentlyEqual(left, right)
1079
        # change all attributes and ensure equality changes as we do.
1080
        left.revision_index = 'a'
1081
        self.assertCurrentlyNotEqual(left, right)
1082
        right.revision_index = 'a'
1083
        self.assertCurrentlyEqual(left, right)
1084
        left.inventory_index = 'a'
1085
        self.assertCurrentlyNotEqual(left, right)
1086
        right.inventory_index = 'a'
1087
        self.assertCurrentlyEqual(left, right)
1088
        left.text_index = 'a'
1089
        self.assertCurrentlyNotEqual(left, right)
1090
        right.text_index = 'a'
1091
        self.assertCurrentlyEqual(left, right)
1092
        left.signature_index = 'a'
1093
        self.assertCurrentlyNotEqual(left, right)
1094
        right.signature_index = 'a'
1095
        self.assertCurrentlyEqual(left, right)
1096
        left.name = 'a'
1097
        self.assertCurrentlyNotEqual(left, right)
1098
        right.name = 'a'
1099
        self.assertCurrentlyEqual(left, right)
1100
        left.transport = 'a'
1101
        self.assertCurrentlyNotEqual(left, right)
1102
        right.transport = 'a'
1103
        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.
1104
1105
    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.
1106
        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.
1107
        self.assertEqual('a_name.pack', pack.file_name())
2592.3.192 by Robert Collins
Move new revision index management to NewPack.
1108
1109
1110
class TestNewPack(TestCaseWithTransport):
1111
    """Tests for pack_repo.NewPack."""
1112
2592.3.193 by Robert Collins
Move hash tracking of new packs into NewPack.
1113
    def test_new_instance_attributes(self):
2592.3.194 by Robert Collins
Output the revision index from NewPack.finish
1114
        upload_transport = self.get_transport('upload')
1115
        pack_transport = self.get_transport('pack')
1116
        index_transport = self.get_transport('index')
1117
        upload_transport.mkdir('.')
1118
        pack = pack_repo.NewPack(upload_transport, index_transport,
3735.1.1 by Robert Collins
Add development2 formats using BTree indices.
1119
            pack_transport, index_builder_class=BTreeBuilder,
1120
            index_class=BTreeGraphIndex)
1121
        self.assertIsInstance(pack.revision_index, BTreeBuilder)
1122
        self.assertIsInstance(pack.inventory_index, BTreeBuilder)
3734.2.4 by Vincent Ladeuil
Fix python2.6 deprecation warnings related to hashlib.
1123
        self.assertIsInstance(pack._hash, type(osutils.md5()))
2592.3.194 by Robert Collins
Output the revision index from NewPack.finish
1124
        self.assertTrue(pack.upload_transport is upload_transport)
1125
        self.assertTrue(pack.index_transport is index_transport)
1126
        self.assertTrue(pack.pack_transport is pack_transport)
1127
        self.assertEqual(None, pack.index_sizes)
1128
        self.assertEqual(20, len(pack.random_name))
1129
        self.assertIsInstance(pack.random_name, str)
1130
        self.assertIsInstance(pack.start_time, float)
2951.1.2 by Robert Collins
Partial refactoring of pack_repo to create a Packer object for packing.
1131
1132
1133
class TestPacker(TestCaseWithTransport):
1134
    """Tests for the packs repository Packer class."""
2951.1.10 by Robert Collins
Peer review feedback with Ian.
1135
1136
    # To date, this class has been factored out and nothing new added to it;
1137
    # thus there are not yet any tests.
3146.6.1 by Aaron Bentley
InterDifferingSerializer shows a progress bar
1138
1139
3777.5.4 by John Arbash Meinel
OptimisingPacker now sets the optimize flags for the indexes being built.
1140
class TestOptimisingPacker(TestCaseWithTransport):
1141
    """Tests for the OptimisingPacker class."""
1142
1143
    def get_pack_collection(self):
1144
        repo = self.make_repository('.')
1145
        return repo._pack_collection
1146
1147
    def test_open_pack_will_optimise(self):
1148
        packer = pack_repo.OptimisingPacker(self.get_pack_collection(),
1149
                                            [], '.test')
1150
        new_pack = packer.open_pack()
1151
        self.assertIsInstance(new_pack, pack_repo.NewPack)
1152
        self.assertTrue(new_pack.revision_index._optimize_for_size)
1153
        self.assertTrue(new_pack.inventory_index._optimize_for_size)
1154
        self.assertTrue(new_pack.text_index._optimize_for_size)
1155
        self.assertTrue(new_pack.signature_index._optimize_for_size)
1156
1157
3146.6.1 by Aaron Bentley
InterDifferingSerializer shows a progress bar
1158
class TestInterDifferingSerializer(TestCaseWithTransport):
1159
1160
    def test_progress_bar(self):
1161
        tree = self.make_branch_and_tree('tree')
1162
        tree.commit('rev1', rev_id='rev-1')
1163
        tree.commit('rev2', rev_id='rev-2')
1164
        tree.commit('rev3', rev_id='rev-3')
1165
        repo = self.make_repository('repo')
1166
        inter_repo = repository.InterDifferingSerializer(
1167
            tree.branch.repository, repo)
1168
        pb = progress.InstrumentedProgress(to_file=StringIO())
1169
        pb.never_throttle = True
1170
        inter_repo.fetch('rev-1', pb)
1171
        self.assertEqual('Transferring revisions', pb.last_msg)
1172
        self.assertEqual(1, pb.last_cnt)
1173
        self.assertEqual(1, pb.last_total)
1174
        inter_repo.fetch('rev-3', pb)
1175
        self.assertEqual(2, pb.last_cnt)
1176
        self.assertEqual(2, pb.last_total)