/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
1
# Copyright (C) 2005, 2006 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
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 InterRepository implementastions."""
18
1711.7.18 by John Arbash Meinel
Old repository formats didn't support double locking on win32, don't raise errors
19
import sys
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
20
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
21
import bzrlib
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
22
import bzrlib.bzrdir as bzrdir
23
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
24
import bzrlib.errors as errors
25
from bzrlib.errors import (FileExists,
26
                           NoSuchRevision,
27
                           NoSuchFile,
28
                           UninitializableFormat,
29
                           NotBranchError,
30
                           )
1731.1.1 by Aaron Bentley
Make root entry an InventoryDirectory, make EmptyTree really empty
31
from bzrlib.inventory import Inventory
2321.2.3 by Alexander Belchenko
fix for check_repo_format_for_funky_id_on_win32 after Martin's refactoring of repository format
32
import bzrlib.repofmt.weaverepo as weaverepo
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
33
import bzrlib.repository as repository
1694.2.6 by Martin Pool
[merge] bzr.dev
34
from bzrlib.revision import NULL_REVISION, Revision
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
35
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
36
from bzrlib.tests.bzrdir_implementations.test_bzrdir import TestCaseWithBzrDir
37
from bzrlib.transport import get_transport
38
39
40
class TestCaseWithInterRepository(TestCaseWithBzrDir):
41
42
    def setUp(self):
43
        super(TestCaseWithInterRepository, self).setUp()
44
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
45
    def make_branch(self, relpath, format=None):
46
        repo = self.make_repository(relpath, format=format)
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
47
        return repo.bzrdir.create_branch()
48
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
49
    def make_bzrdir(self, relpath, format=None):
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
50
        try:
51
            url = self.get_url(relpath)
52
            segments = url.split('/')
53
            if segments and segments[-1] not in ('', '.'):
54
                parent = '/'.join(segments[:-1])
55
                t = get_transport(parent)
56
                try:
57
                    t.mkdir(segments[-1])
58
                except FileExists:
59
                    pass
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
60
            if format is None:
61
                format = self.repository_format._matchingbzrdir
62
            return format.initialize(url)
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
63
        except UninitializableFormat:
1684.1.4 by Martin Pool
(patch) better warnings when tests are skipped (Alexander)
64
            raise TestSkipped("Format %s is not initializable." % format)
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
65
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
66
    def make_repository(self, relpath, format=None):
67
        made_control = self.make_bzrdir(relpath, format=format)
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
68
        return self.repository_format.initialize(made_control)
69
70
    def make_to_repository(self, relpath):
71
        made_control = self.make_bzrdir(relpath,
72
            self.repository_format_to._matchingbzrdir)
1534.1.30 by Robert Collins
Test that we get the right optimiser back in the InterRepository tests.
73
        return self.repository_format_to.initialize(made_control)
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
74
75
1711.7.18 by John Arbash Meinel
Old repository formats didn't support double locking on win32, don't raise errors
76
def check_old_format_lock_error(repository_format):
77
    """Potentially ignore LockError on old formats.
78
79
    On win32, with the old OS locks, we get a failure of double-lock when
80
    we open a object in 2 objects and try to lock both.
81
82
    On new formats, LockError would be invalid, but for old formats
83
    this was not supported on Win32.
84
    """
85
    if sys.platform != 'win32':
86
        raise
87
88
    description = repository_format.get_format_description()
89
    if description in ("Repository format 4",
90
                       "Weave repository format 5",
91
                       "Weave repository format 6"):
1711.7.30 by John Arbash Meinel
Switch to using TestSkipped for old win32 problems
92
        # jam 20060701
93
        # win32 OS locks are not re-entrant. So one process cannot
94
        # open the same repository twice and lock them both.
95
        raise TestSkipped('%s on win32 cannot open the same'
96
                          ' repository twice in different objects'
97
                          % description)
1711.7.18 by John Arbash Meinel
Old repository formats didn't support double locking on win32, don't raise errors
98
    raise
99
100
2240.1.3 by Alexander Belchenko
win32: skip tests that try to use funky chars in fileid in Weave-based repositories
101
def check_repo_format_for_funky_id_on_win32(repo):
2321.2.3 by Alexander Belchenko
fix for check_repo_format_for_funky_id_on_win32 after Martin's refactoring of repository format
102
    if (isinstance(repo, (weaverepo.AllInOneRepository,
103
                          weaverepo.WeaveMetaDirRepository))
104
        and sys.platform == 'win32'):
2240.1.3 by Alexander Belchenko
win32: skip tests that try to use funky chars in fileid in Weave-based repositories
105
            raise TestSkipped("funky chars does not permitted"
106
                              " on this platform in repository"
107
                              " %s" % repo.__class__.__name__)
108
109
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
110
class TestInterRepository(TestCaseWithInterRepository):
111
1534.1.30 by Robert Collins
Test that we get the right optimiser back in the InterRepository tests.
112
    def test_interrepository_get_returns_correct_optimiser(self):
113
        # we assume the optimising code paths are triggered
114
        # by the type of the repo not the transport - at this point.
115
        # we may need to update this test if this changes.
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
116
        #
117
        # XXX: This code tests that we get an InterRepository when we try to
118
        # convert between the two repositories that it wants to be tested with
119
        # -- but that's not necessarily correct.  So for now this is disabled.
120
        # mbp 20070206
121
        ## source_repo = self.make_repository("source")
122
        ## target_repo = self.make_to_repository("target")
123
        ## interrepo = repository.InterRepository.get(source_repo, target_repo)
124
        ## self.assertEqual(self.interrepo_class, interrepo.__class__)
125
        pass
1534.1.30 by Robert Collins
Test that we get the right optimiser back in the InterRepository tests.
126
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
127
    def test_fetch(self):
128
        tree_a = self.make_branch_and_tree('a')
129
        self.build_tree(['a/foo'])
130
        tree_a.add('foo', 'file1')
131
        tree_a.commit('rev1', rev_id='rev1')
132
        def check_push_rev1(repo):
133
            # ensure the revision is missing.
134
            self.assertRaises(NoSuchRevision, repo.get_revision, 'rev1')
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
135
            # fetch with a limit of NULL_REVISION and an explicit progress bar.
136
            repo.fetch(tree_a.branch.repository,
137
                       revision_id=NULL_REVISION,
138
                       pb=bzrlib.progress.DummyProgress())
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
139
            # nothing should have been pushed
140
            self.assertFalse(repo.has_revision('rev1'))
141
            # fetch with a default limit (grab everything)
142
            repo.fetch(tree_a.branch.repository)
143
            # check that b now has all the data from a's first commit.
144
            rev = repo.get_revision('rev1')
145
            tree = repo.revision_tree('rev1')
146
            tree.get_file_text('file1')
147
            for file_id in tree:
148
                if tree.inventory[file_id].kind == "file":
149
                    tree.get_file(file_id).read()
150
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
151
        # makes a target version repo 
152
        repo_b = self.make_to_repository('b')
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
153
        check_push_rev1(repo_b)
154
        
155
    def test_fetch_missing_revision_same_location_fails(self):
156
        repo_a = self.make_repository('.')
157
        repo_b = repository.Repository.open('.')
1711.7.18 by John Arbash Meinel
Old repository formats didn't support double locking on win32, don't raise errors
158
        try:
159
            self.assertRaises(errors.NoSuchRevision, repo_b.fetch, repo_a, revision_id='XXX')
160
        except errors.LockError, e:
161
            check_old_format_lock_error(self.repository_format)
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
162
163
    def test_fetch_same_location_trivial_works(self):
164
        repo_a = self.make_repository('.')
165
        repo_b = repository.Repository.open('.')
1711.7.18 by John Arbash Meinel
Old repository formats didn't support double locking on win32, don't raise errors
166
        try:
167
            repo_a.fetch(repo_b)
168
        except errors.LockError, e:
169
            check_old_format_lock_error(self.repository_format)
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
170
1694.2.6 by Martin Pool
[merge] bzr.dev
171
    def test_fetch_missing_text_other_location_fails(self):
172
        source_tree = self.make_branch_and_tree('source')
173
        source = source_tree.branch.repository
174
        target = self.make_to_repository('target')
175
    
176
        # start by adding a file so the data for hte file exists.
177
        self.build_tree(['source/id'])
178
        source_tree.add(['id'], ['id'])
179
        source_tree.commit('a', rev_id='a')
180
        # now we manually insert a revision with an inventory referencing
181
        # 'id' at revision 'b', but we do not insert revision b.
182
        # this should ensure that the new versions of files are being checked
183
        # for during pull operations
184
        inv = source.get_inventory('a')
185
        inv['id'].revision = 'b'
1740.2.6 by Aaron Bentley
Update test for new interface
186
        inv.revision_id = 'b'
1694.2.6 by Martin Pool
[merge] bzr.dev
187
        sha1 = source.add_inventory('b', inv, ['a'])
188
        rev = Revision(timestamp=0,
189
                       timezone=None,
190
                       committer="Foo Bar <foo@example.com>",
191
                       message="Message",
192
                       inventory_sha1=sha1,
193
                       revision_id='b')
194
        rev.parent_ids = ['a']
195
        source.add_revision('b', rev)
196
        self.assertRaises(errors.RevisionNotPresent, target.fetch, source)
197
        self.assertFalse(target.has_revision('b'))
198
1843.2.1 by Aaron Bentley
Add failing tests for funky ids
199
    def test_fetch_funky_file_id(self):
200
        from_tree = self.make_branch_and_tree('tree')
2240.1.3 by Alexander Belchenko
win32: skip tests that try to use funky chars in fileid in Weave-based repositories
201
        if sys.platform == 'win32':
202
            from_repo = from_tree.branch.repository
203
            check_repo_format_for_funky_id_on_win32(from_repo)
1843.2.1 by Aaron Bentley
Add failing tests for funky ids
204
        self.build_tree(['tree/filename'])
205
        from_tree.add('filename', 'funky-chars<>%&;"\'')
206
        from_tree.commit('commit filename')
207
        to_repo = self.make_to_repository('to')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
208
        to_repo.fetch(from_tree.branch.repository, from_tree.get_parent_ids()[0])
1843.2.1 by Aaron Bentley
Add failing tests for funky ids
209
1910.8.1 by Aaron Bentley
Handle inventories with no revision_ids
210
    def test_fetch_no_inventory_revision(self):
211
        """Old inventories lack revision_ids, so simulate this"""
212
        from_tree = self.make_branch_and_tree('tree')
2240.1.3 by Alexander Belchenko
win32: skip tests that try to use funky chars in fileid in Weave-based repositories
213
        if sys.platform == 'win32':
214
            from_repo = from_tree.branch.repository
215
            check_repo_format_for_funky_id_on_win32(from_repo)
1910.8.1 by Aaron Bentley
Handle inventories with no revision_ids
216
        self.build_tree(['tree/filename'])
217
        from_tree.add('filename', 'funky-chars<>%&;"\'')
218
        from_tree.commit('commit filename')
219
        old_deserialise = from_tree.branch.repository.deserialise_inventory
220
        def deserialise(revision_id, text):
221
            inventory = old_deserialise(revision_id, text)
222
            inventory.revision_id = None
223
            return inventory
224
        from_tree.branch.repository.deserialise_inventory = deserialise
225
        to_repo = self.make_to_repository('to')
226
        to_repo.fetch(from_tree.branch.repository, from_tree.last_revision())
227
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
228
229
class TestCaseWithComplexRepository(TestCaseWithInterRepository):
230
231
    def setUp(self):
232
        super(TestCaseWithComplexRepository, self).setUp()
233
        tree_a = self.make_branch_and_tree('a')
234
        self.bzrdir = tree_a.branch.bzrdir
235
        # add a corrupt inventory 'orphan'
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
236
        inv_file = tree_a.branch.repository.control_weaves.get_weave(
237
            'inventory', 
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
238
            tree_a.branch.repository.get_transaction())
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
239
        inv_file.add_lines('orphan', [], [])
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
240
        # add a real revision 'rev1'
241
        tree_a.commit('rev1', rev_id='rev1', allow_pointless=True)
242
        # add a real revision 'rev2' based on rev1
243
        tree_a.commit('rev2', rev_id='rev2', allow_pointless=True)
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.
244
        # and sign 'rev2'
245
        tree_a.branch.repository.sign_revision('rev2',
246
            bzrlib.gpg.LoopbackGPGStrategy(None))
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
247
248
    def test_missing_revision_ids(self):
249
        # revision ids in repository A but not B are returned, fake ones
250
        # are stripped. (fake meaning no revision object, but an inventory 
251
        # as some formats keyed off inventory data in the past.
252
        # make a repository to compare against that claims to have rev1
253
        repo_b = self.make_to_repository('rev1_only')
254
        repo_a = self.bzrdir.open_repository()
255
        repo_b.fetch(repo_a, 'rev1')
256
        # check the test will be valid
257
        self.assertFalse(repo_b.has_revision('rev2'))
258
        self.assertEqual(['rev2'],
259
                         repo_b.missing_revision_ids(repo_a))
260
261
    def test_missing_revision_ids_revision_limited(self):
262
        # revision ids in repository A that are not referenced by the
263
        # requested revision are not returned.
264
        # make a repository to compare against that is empty
265
        repo_b = self.make_to_repository('empty')
266
        repo_a = self.bzrdir.open_repository()
267
        self.assertEqual(['rev1'],
268
                         repo_b.missing_revision_ids(repo_a, revision_id='rev1'))
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.
269
        
270
    def test_fetch_preserves_signatures(self):
271
        from_repo = self.bzrdir.open_repository()
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
272
        from_signature = from_repo.get_signature_text('rev2')
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.
273
        to_repo = self.make_to_repository('target')
274
        to_repo.fetch(from_repo)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
275
        to_signature = to_repo.get_signature_text('rev2')
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.
276
        self.assertEqual(from_signature, to_signature)
277
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
278
279
class TestCaseWithGhosts(TestCaseWithInterRepository):
280
281
    def setUp(self):
282
        super(TestCaseWithGhosts, self).setUp()
283
        # we want two repositories at this point
284
        # one with a revision that is a ghost in the other
285
        # repository.
286
287
        # 'ghost' is a ghost in missing_ghost and not in with_ghost_rev
1910.2.26 by Aaron Bentley
Fix up some test cases
288
        inv = Inventory(revision_id='ghost')
289
        inv.root.revision = 'ghost'
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
290
        repo = self.make_repository('with_ghost_rev')
1907.1.1 by Aaron Bentley
Unshelved all changes except those related to removing RootEntry
291
        sha1 = repo.add_inventory('ghost', inv, [])
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
292
        rev = bzrlib.revision.Revision(timestamp=0,
293
                                       timezone=None,
294
                                       committer="Foo Bar <foo@example.com>",
295
                                       message="Message",
296
                                       inventory_sha1=sha1,
297
                                       revision_id='ghost')
298
        rev.parent_ids = []
299
        repo.add_revision('ghost', rev)
300
         
301
        repo = self.make_to_repository('missing_ghost')
1910.2.26 by Aaron Bentley
Fix up some test cases
302
        inv = Inventory(revision_id='with_ghost')
303
        inv.root.revision = 'with_ghost'
1907.1.1 by Aaron Bentley
Unshelved all changes except those related to removing RootEntry
304
        sha1 = repo.add_inventory('with_ghost', inv, [])
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
305
        rev = bzrlib.revision.Revision(timestamp=0,
306
                                       timezone=None,
307
                                       committer="Foo Bar <foo@example.com>",
308
                                       message="Message",
309
                                       inventory_sha1=sha1,
310
                                       revision_id='with_ghost')
311
        rev.parent_ids = ['ghost']
312
        repo.add_revision('with_ghost', rev)
313
314
    def test_fetch_all_fixes_up_ghost(self):
315
        # fetching from a repo with a current ghost unghosts it in referencing
316
        # revisions.
317
        repo = repository.Repository.open('missing_ghost')
318
        rev = repo.get_revision('with_ghost')
319
        from_repo = repository.Repository.open('with_ghost_rev')
320
        repo.fetch(from_repo)
321
        # rev must not be corrupt now
322
        rev = repo.get_revision('with_ghost')
323
        self.assertEqual([None, 'ghost', 'with_ghost'], repo.get_ancestry('with_ghost'))