Commit f66b019b authored by Sushil Khanchi's avatar Sushil Khanchi 🐨
Browse files

CommitService: add gitaly comparison tests for FindCommits

parent 9bececdd2ee3
Pipeline #21004 passed with stage
in 39 seconds
......@@ -7,4 +7,5 @@
import grpc
import pytest
import re
import time
from hgitaly.stub.commit_pb2 import (
......@@ -10,5 +11,6 @@
from hgitaly.stub.commit_pb2 import (
FindCommitsRequest,
ListLastCommitsForTreeRequest,
RawBlameRequest,
)
from hgitaly.stub.commit_pb2_grpc import CommitServiceStub
......@@ -11,7 +13,9 @@
ListLastCommitsForTreeRequest,
RawBlameRequest,
)
from hgitaly.stub.commit_pb2_grpc import CommitServiceStub
from google.protobuf.timestamp_pb2 import Timestamp
from mercurial import pycompat
from . import skip_comparison_tests
if skip_comparison_tests(): # pragma no cover
......@@ -190,3 +194,164 @@
do_rpc('git', ctx1.hex(), b'')
assert exc_info_hg.value.code() == exc_info_git.value.code()
assert exc_info_hg.value.details() == exc_info_git.value.details()
def test_compare_find_commits(gitaly_comparison):
fixture = gitaly_comparison
repo_message = fixture.gitaly_repo
git_repo = fixture.git_repo
wrapper = fixture.hg_repo_wrapper
# set_default_gitlab_branch(wrapper.repo, b'branch/default')
# prepare repo as:
#
# @ 4 (branch/default) merge with stable
# |\
# | o 3 creates 'animal' (branch/stable)
# | |
# o | 2 rename 'foo' to 'zoo' (user: testuser)
# |/
# | 1 changes 'foo' (topic: sampletop)
# |/
# o 0 creates 'foo'
#
ctx0 = wrapper.commit_file('foo')
git_shas = {
ctx0.hex(): git_repo.branches()[b'branch/default']['sha'],
}
ctx1 = wrapper.commit_file('foo', topic='sampletop')
git_shas[ctx1.hex()] = (
git_repo.branches()[b'topic/default/sampletop']['sha']
)
wrapper.update(ctx0.rev())
wrapper.command(b'mv', wrapper.repo.root + b'/foo',
wrapper.repo.root + b'/zoo')
ctx2 = wrapper.commit([b'foo', b'zoo'], message=b"rename foo to zoo")
git_shas[ctx2.hex()] = git_repo.branches()[b'branch/default']['sha']
# commits with different date/time, to test with 'date' filter
ts = int(time.time())
ctx3 = wrapper.write_commit('animals', branch='stable', parent=ctx0,
utc_timestamp=ts+10, user='testuser')
git_shas[ctx3.hex()] = git_repo.branches()[b'branch/stable']['sha']
wrapper.update(2)
ctx4 = wrapper.merge_commit(ctx3, message=b'merge with stable',
utc_timestamp=ts+20)
git_shas[ctx4.hex()] = git_repo.branches()[b'branch/default']['sha']
commit_stubs = dict(git=CommitServiceStub(fixture.gitaly_channel),
hg=CommitServiceStub(fixture.hgitaly_channel))
def convert(sha, vcs):
"""Convert an Hg sha to Git, if apply"""
sha = pycompat.sysbytes(sha)
if vcs == 'git':
return sha
return git_shas.get(sha)
def do_rpc(vcs, return_sorted=False, limit=10, **opts):
request = FindCommitsRequest(repository=repo_message,
limit=limit,
**opts)
resp = commit_stubs[vcs].FindCommits(request)
final = [
convert(commit.id, vcs)
for chunk in resp for commit in chunk.commits
]
if return_sorted:
# for the special cases where, we have two commits diverging
# and Git order the commits arbitrarily
# for e.g.
#
# B
# | C Here, if selecting from bottom to top, order
# | / can be: (A, B, C) or (A, C, B)
# A
#
return sorted(final)
return final
# when `revision` is provided as <revspec>
all_revs = [ctx0.hex(), ctx1.hex(), ctx2.hex(), ctx3.hex(), ctx4.hex()]
for range_str in (b'..', b'...'):
for r1 in all_revs:
for r2 in all_revs:
revision = r1 + range_str + r2
git_revision = git_shas[r1] + range_str + git_shas[r2]
assert (
do_rpc('hg', revision=revision, return_sorted=True)
==
do_rpc('git', revision=git_revision, return_sorted=True)
)
# when `revision` is provided as a ref to a single commit
refs = [b'', ctx0.hex(), b'topic/default/sampletop', ctx2.hex(),
b'branch/stable', b'branch/default']
for ref in refs:
git_ref = git_shas[ref] if len(ref) == 40 else ref
assert do_rpc('hg', revision=ref) == do_rpc('git', revision=git_ref)
# with `path` and `follow` options
test_paths = [
# Note: we are not including [b'foo'] here, because of a special case:
# in a rename-cset (foo -> zoo), Git consider the cset but Hg doesn't,
# as 'foo' is not present in rename-cset.
[b'zoo'],
[b'foo', b'zoo'],
]
for follow in [True, False]:
for paths in test_paths:
if len(paths) > 1:
# In Git, 'follow' doesn't work with multiple paths
follow = False
assert (
do_rpc('hg', paths=paths, follow=follow)
==
do_rpc('git', paths=paths, follow=follow)
)
# with `all` option
assert (
do_rpc('hg', all=True, return_sorted=True)
==
do_rpc('git', all=True, return_sorted=True)
)
# with `author` option
assert (
do_rpc('hg', author=b'testuser') == do_rpc('git', author=b'testuser')
)
# with `skip_merges` option
assert (
do_rpc('hg', skip_merges=True) == do_rpc('git', skip_merges=True)
)
# with `limit` and `offset` options
for limit in range(0, 5):
for offset in range(0, 5):
assert (
do_rpc('hg', offset=offset, limit=limit)
==
do_rpc('git', offset=offset, limit=limit)
)
# with `order` option
assert (
do_rpc('hg', order=FindCommitsRequest.Order.TOPO)
==
do_rpc('git', order=FindCommitsRequest.Order.TOPO)
)
# with `after` and `before` options for dates
date1, date2 = Timestamp(), Timestamp()
date1.FromSeconds(ts+10)
date2.FromSeconds(ts+20)
for date in [date1, date2]:
assert do_rpc('hg', after=date) == do_rpc('git', after=date)
assert do_rpc('hg', before=date) == do_rpc('git', before=date)
assert (
do_rpc('hg', before=date, after=date)
==
do_rpc('git', before=date, after=date)
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment