/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to breezy/tests/test_foreign.py

  • Committer: Jelmer Vernooij
  • Date: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
from ..bzr import groupcompress_repo
40
40
 
41
 
# This is the dummy foreign revision control system, used
 
41
# This is the dummy foreign revision control system, used 
42
42
# mainly here in the testsuite to test the foreign VCS infrastructure.
43
 
# It is basically standard Bazaar with some minor modifications to
44
 
# make it "foreign".
45
 
#
 
43
# It is basically standard Bazaar with some minor modifications to 
 
44
# make it "foreign". 
 
45
46
46
# It has the following differences to "regular" Bazaar:
47
47
# - The control directory is named ".dummy", not ".bzr".
48
48
# - The revision ids are tuples, not strings.
56
56
        return isinstance(self, type(other))
57
57
 
58
58
    def revision_id_bzr_to_foreign(self, bzr_revid):
59
 
        return tuple(bzr_revid[len(b"dummy-v1:"):].split(b"-")), self
 
59
        return tuple(bzr_revid[len("dummy-v1:"):].split("-")), self
60
60
 
61
61
    def revision_id_foreign_to_bzr(self, foreign_revid):
62
 
        return b"dummy-v1:%s-%s-%s" % foreign_revid
 
62
        return "dummy-v1:%s-%s-%s" % foreign_revid
63
63
 
64
64
 
65
65
class DummyForeignVcsMappingRegistry(foreign.VcsMappingRegistry):
66
66
 
67
67
    def revision_id_bzr_to_foreign(self, revid):
68
 
        if not revid.startswith(b"dummy-"):
 
68
        if not revid.startswith("dummy-"):
69
69
            raise errors.InvalidRevisionId(revid, None)
70
 
        mapping_version = revid[len(b"dummy-"):len(b"dummy-vx")]
 
70
        mapping_version = revid[len("dummy-"):len("dummy-vx")]
71
71
        mapping = self.get(mapping_version)
72
72
        return mapping.revision_id_bzr_to_foreign(revid)
73
73
 
80
80
 
81
81
    def __init__(self):
82
82
        self.mapping_registry = DummyForeignVcsMappingRegistry()
83
 
        self.mapping_registry.register(b"v1", DummyForeignVcsMapping(self),
 
83
        self.mapping_registry.register("v1", DummyForeignVcsMapping(self),
84
84
                                       "Version 1")
85
85
        self.abbreviation = "dummy"
86
86
 
87
87
    def show_foreign_revid(self, foreign_revid):
88
 
        return {"dummy ding": "%s/%s\\%s" % foreign_revid}
 
88
        return { "dummy ding": "%s/%s\\%s" % foreign_revid }
89
89
 
90
90
    def serialize_foreign_revid(self, foreign_revid):
91
91
        return "%s|%s|%s" % foreign_revid
92
92
 
93
93
 
94
 
class DummyForeignVcsBranch(bzrbranch.BzrBranch6, foreign.ForeignBranch):
 
94
class DummyForeignVcsBranch(bzrbranch.BzrBranch6,foreign.ForeignBranch):
95
95
    """A Dummy VCS Branch."""
96
96
 
97
97
    @property
104
104
        self._ignore_fallbacks = False
105
105
        self.controldir = a_controldir
106
106
        foreign.ForeignBranch.__init__(self,
107
 
                                       DummyForeignVcsMapping(DummyForeignVcs()))
 
107
            DummyForeignVcsMapping(DummyForeignVcs()))
108
108
        bzrbranch.BzrBranch6.__init__(self, _format, _control_files, a_controldir,
109
 
                                      *args, **kwargs)
 
109
            *args, **kwargs)
110
110
 
111
111
    def _get_checkout_format(self, lightweight=False):
112
112
        """Return the most suitable metadir for a checkout of this branch.
123
123
        return (revno, revid)
124
124
 
125
125
 
126
 
class DummyForeignCommitBuilder(vf_repository.VersionedFileCommitBuilder):
 
126
class DummyForeignCommitBuilder(vf_repository.VersionedFileRootCommitBuilder):
127
127
 
128
 
    def _generate_revision_if_needed(self, revid):
 
128
    def _generate_revision_if_needed(self):
129
129
        mapping = DummyForeignVcsMapping(DummyForeignVcs())
130
130
        if self._lossy:
131
131
            self._new_revision_id = mapping.revision_id_foreign_to_bzr(
132
 
                (b'%d' % self._timestamp,
133
 
                 str(self._timezone).encode('ascii'), b"UNKNOWN"))
 
132
                (str(self._timestamp), str(self._timezone), "UNKNOWN"))
134
133
            self.random_revid = False
135
 
        elif revid is not None:
136
 
            self._new_revision_id = revid
 
134
        elif self._new_revision_id is not None:
137
135
            self.random_revid = False
138
136
        else:
139
137
            self._new_revision_id = self._gen_revision_id()
141
139
 
142
140
 
143
141
class DummyForeignVcsRepository(groupcompress_repo.CHKInventoryRepository,
144
 
                                foreign.ForeignRepository):
 
142
    foreign.ForeignRepository):
145
143
    """Dummy foreign vcs repository."""
146
144
 
147
145
 
152
150
 
153
151
    @classmethod
154
152
    def get_format_string(cls):
155
 
        return b"Dummy Foreign Vcs Repository"
 
153
        return "Dummy Foreign Vcs Repository"
156
154
 
157
155
    def get_format_description(self):
158
156
        return "Dummy Foreign Vcs Repository"
160
158
 
161
159
def branch_history(graph, revid):
162
160
    ret = list(graph.iter_lefthand_ancestry(revid,
163
 
                                            (revision.NULL_REVISION,)))
 
161
        (revision.NULL_REVISION,)))
164
162
    ret.reverse()
165
163
    return ret
166
164
 
171
169
    def is_compatible(source, target):
172
170
        return isinstance(target, DummyForeignVcsBranch)
173
171
 
174
 
    def push(self, overwrite=False, stop_revision=None, lossy=False, tag_selector=None):
 
172
    def push(self, overwrite=False, stop_revision=None, lossy=False):
175
173
        if not lossy:
176
174
            raise errors.NoRoundtrippingSupport(self.source, self.target)
177
175
        result = branch.BranchPushResult()
183
181
            graph = self.source.repository.get_graph()
184
182
            # This just handles simple cases, but that's good enough for tests
185
183
            my_history = branch_history(self.target.repository.get_graph(),
186
 
                                        result.old_revid)
 
184
                result.old_revid)
187
185
            if stop_revision is None:
188
186
                stop_revision = self.source.last_revision()
189
187
            their_history = branch_history(graph, stop_revision)
194
192
            for revid in todo:
195
193
                rev = self.source.repository.get_revision(revid)
196
194
                tree = self.source.repository.revision_tree(revid)
197
 
                def get_file_with_stat(path):
198
 
                    return (tree.get_file(path), None)
 
195
                def get_file_with_stat(file_id, path=None):
 
196
                    return (tree.get_file(file_id), None)
199
197
                tree.get_file_with_stat = get_file_with_stat
200
198
                new_revid = self.target.mapping.revision_id_foreign_to_bzr(
201
 
                    (b'%d' % rev.timestamp, str(rev.timezone).encode('ascii'),
202
 
                        str(self.target.revno()).encode('ascii')))
203
 
                parent_revno, parent_revid = self.target.last_revision_info()
 
199
                    (str(rev.timestamp), str(rev.timezone),
 
200
                        str(self.target.revno())))
 
201
                parent_revno, parent_revid= self.target.last_revision_info()
204
202
                if parent_revid == revision.NULL_REVISION:
205
203
                    parent_revids = []
206
204
                else:
207
205
                    parent_revids = [parent_revid]
208
 
                builder = self.target.get_commit_builder(
209
 
                    parent_revids, self.target.get_config_stack(), rev.timestamp,
210
 
                    rev.timezone, rev.committer, rev.properties,
211
 
                    new_revid)
 
206
                builder = self.target.get_commit_builder(parent_revids,
 
207
                        self.target.get_config_stack(), rev.timestamp,
 
208
                        rev.timezone, rev.committer, rev.properties,
 
209
                        new_revid)
212
210
                try:
213
211
                    parent_tree = self.target.repository.revision_tree(
214
212
                        parent_revid)
220
218
                    builder.abort()
221
219
                    raise
222
220
                revidmap[revid] = builder.commit(rev.message)
223
 
                self.target.set_last_revision_info(parent_revno + 1,
224
 
                                                   revidmap[revid])
 
221
                self.target.set_last_revision_info(parent_revno+1,
 
222
                    revidmap[revid])
225
223
                trace.mutter('lossily pushed revision %s -> %s',
226
 
                             revid, revidmap[revid])
 
224
                    revid, revidmap[revid])
227
225
        finally:
228
226
            self.source.unlock()
229
227
        result.new_revno, result.new_revid = self.target.last_revision_info()
235
233
 
236
234
    @classmethod
237
235
    def get_format_string(cls):
238
 
        return b"Branch for Testing"
 
236
        return "Branch for Testing"
239
237
 
240
238
    @property
241
 
    def _matchingcontroldir(self):
 
239
    def _matchingbzrdir(self):
242
240
        return DummyForeignVcsDirFormat()
243
241
 
244
242
    def open(self, a_controldir, name=None, _found=False, ignore_fallbacks=False,
245
 
             found_repository=None):
 
243
            found_repository=None):
246
244
        if name is None:
247
245
            name = a_controldir._get_selected_branch()
248
246
        if not _found:
254
252
            if found_repository is None:
255
253
                found_repository = a_controldir.find_repository()
256
254
            return DummyForeignVcsBranch(_format=self,
257
 
                                         _control_files=control_files,
258
 
                                         a_controldir=a_controldir,
259
 
                                         _repository=found_repository,
260
 
                                         name=name)
 
255
                              _control_files=control_files,
 
256
                              a_controldir=a_controldir,
 
257
                              _repository=found_repository,
 
258
                              name=name)
261
259
        except errors.NoSuchFile:
262
260
            raise errors.NotBranchError(path=transport.base)
263
261
 
267
265
 
268
266
    @classmethod
269
267
    def get_format_string(cls):
270
 
        return b"A Dummy VCS Dir"
 
268
        return "A Dummy VCS Dir"
271
269
 
272
270
    @classmethod
273
271
    def get_format_description(cls):
289
287
        # Since we don't have a .bzr directory, inherit the
290
288
        # mode from the root directory
291
289
        temp_control = lockable_files.LockableFiles(transport,
292
 
                                                    '', lockable_files.TransportLock)
 
290
                            '', lockable_files.TransportLock)
293
291
        temp_control._transport.mkdir('.dummy',
294
292
                                      # FIXME: RBC 20060121 don't peek under
295
293
                                      # the covers
298
296
        bzrdir_transport = transport.clone('.dummy')
299
297
        # NB: no need to escape relative paths that are url safe.
300
298
        control_files = lockable_files.LockableFiles(bzrdir_transport,
301
 
                                                     self._lock_file_name, self._lock_class)
 
299
            self._lock_file_name, self._lock_class)
302
300
        control_files.create_lock()
303
301
        return self.open(transport, _found=True)
304
302
 
314
312
        self.root_transport = _transport
315
313
        self._mode_check_done = False
316
314
        self._control_files = lockable_files.LockableFiles(self.transport,
317
 
                                                           "lock", lockable_files.TransportLock)
 
315
            "lock", lockable_files.TransportLock)
318
316
 
319
317
    def create_workingtree(self):
320
318
        # dirstate requires a ".bzr" entry to exist
321
 
        self.root_transport.put_bytes(".bzr", b"foo")
 
319
        self.root_transport.put_bytes(".bzr", "foo")
322
320
        return super(DummyForeignVcsDir, self).create_workingtree()
323
321
 
324
322
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True,
377
375
 
378
376
    @classmethod
379
377
    def known_formats(cls):
380
 
        return [DummyForeignVcsDirFormat()]
 
378
        return {DummyForeignVcsDirFormat()}
381
379
 
382
380
 
383
381
class ForeignVcsRegistryTests(tests.TestCase):
386
384
    def test_parse_revision_id_no_dash(self):
387
385
        reg = foreign.ForeignVcsRegistry()
388
386
        self.assertRaises(errors.InvalidRevisionId,
389
 
                          reg.parse_revision_id, b"invalid")
 
387
                          reg.parse_revision_id, "invalid")
390
388
 
391
389
    def test_parse_revision_id_unknown_mapping(self):
392
390
        reg = foreign.ForeignVcsRegistry()
393
391
        self.assertRaises(errors.InvalidRevisionId,
394
 
                          reg.parse_revision_id, b"unknown-foreignrevid")
 
392
                          reg.parse_revision_id, "unknown-foreignrevid")
395
393
 
396
394
    def test_parse_revision_id(self):
397
395
        reg = foreign.ForeignVcsRegistry()
398
396
        vcs = DummyForeignVcs()
399
397
        reg.register("dummy", vcs, "Dummy VCS")
400
398
        self.assertEqual((
401
 
            (b"some", b"foreign", b"revid"), DummyForeignVcsMapping(vcs)),
402
 
            reg.parse_revision_id(b"dummy-v1:some-foreign-revid"))
 
399
            ("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
 
400
            reg.parse_revision_id("dummy-v1:some-foreign-revid"))
403
401
 
404
402
 
405
403
class ForeignRevisionTests(tests.TestCase):
407
405
 
408
406
    def test_create(self):
409
407
        mapp = DummyForeignVcsMapping(DummyForeignVcs())
410
 
        rev = foreign.ForeignRevision((b"a", b"foreign", b"revid"),
411
 
                                      mapp, b"roundtripped-revid")
412
 
        self.assertEqual(b"", rev.inventory_sha1)
413
 
        self.assertEqual((b"a", b"foreign", b"revid"), rev.foreign_revid)
 
408
        rev = foreign.ForeignRevision(("a", "foreign", "revid"),
 
409
                                      mapp, "roundtripped-revid")
 
410
        self.assertEqual("", rev.inventory_sha1)
 
411
        self.assertEqual(("a", "foreign", "revid"), rev.foreign_revid)
414
412
        self.assertEqual(mapp, rev.mapping)
415
413
 
416
414
 
 
415
class WorkingTreeFileUpdateTests(tests.TestCaseWithTransport):
 
416
    """Tests for update_workingtree_fileids()."""
 
417
 
 
418
    def test_update_workingtree(self):
 
419
        wt = self.make_branch_and_tree('br1')
 
420
        self.build_tree_contents([('br1/bla', 'original contents\n')])
 
421
        wt.add('bla', 'bla-a')
 
422
        wt.commit('bla-a')
 
423
        root_id = wt.get_root_id()
 
424
        target = wt.controldir.sprout('br2').open_workingtree()
 
425
        target.unversion(['bla-a'])
 
426
        target.add('bla', 'bla-b')
 
427
        target.commit('bla-b')
 
428
        target_basis = target.basis_tree()
 
429
        target_basis.lock_read()
 
430
        self.addCleanup(target_basis.unlock)
 
431
        foreign.update_workingtree_fileids(wt, target_basis)
 
432
        wt.lock_read()
 
433
        try:
 
434
            self.assertEqual({root_id, "bla-b"}, set(wt.all_file_ids()))
 
435
        finally:
 
436
            wt.unlock()
 
437
 
 
438
 
417
439
class DummyForeignVcsTests(tests.TestCaseWithTransport):
418
440
    """Very basic test for DummyForeignVcs."""
419
441
 
425
447
        """Test we can create dummies."""
426
448
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
427
449
        dir = controldir.ControlDir.open("d")
428
 
        self.assertEqual(b"A Dummy VCS Dir", dir._format.get_format_string())
 
450
        self.assertEqual("A Dummy VCS Dir", dir._format.get_format_string())
429
451
        dir.open_repository()
430
452
        dir.open_branch()
431
453
        dir.open_workingtree()
435
457
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
436
458
        dir = controldir.ControlDir.open("d")
437
459
        newdir = dir.sprout("e")
438
 
        self.assertNotEqual(b"A Dummy VCS Dir",
 
460
        self.assertNotEqual("A Dummy VCS Dir",
439
461
                            newdir._format.get_format_string())
440
462
 
441
463
    def test_push_not_supported(self):