test_commit.py 9.63 KB
Newer Older
1
2
3
4
5
6
7
8
# Copyright 2020 Georges Racinet <georges.racinet@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# SPDX-License-Identifier: GPL-2.0-or-later
import grpc
import pytest
9
import re
10
from hgitaly.stub.commit_pb2 import (
11
    ListFilesRequest,
12
    ListLastCommitsForTreeRequest,
13
    RawBlameRequest,
14
15
16
)
from hgitaly.stub.commit_pb2_grpc import CommitServiceStub

17
18
from . import skip_comparison_tests
if skip_comparison_tests():  # pragma no cover
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    pytestmark = pytest.mark.skip


def test_compare_list_last_commits_for_tree(gitaly_comparison):
    fixture = gitaly_comparison
    repo_message = fixture.gitaly_repo
    git_repo = fixture.git_repo

    wrapper = fixture.hg_repo_wrapper
    ctx0 = wrapper.write_commit('foo', message="Some foo")
    git_shas = {
        ctx0.hex(): git_repo.branches()[b'branch/default']['sha'],
    }

    sub = (wrapper.path / 'sub')
    sub.mkdir()
    subdir = (sub / 'dir')
    subdir.mkdir()
    (sub / 'bar').write_text('bar content')
    (sub / 'ba2').write_text('ba2 content')
    (subdir / 'bar').write_text('bar content')
    (subdir / 'ba2').write_text('ba2 content')
    # TODO OS indep for paths (actually TODO make wrapper.commit easier to
    # use, e.g., check how to make it accept patterns)
    ctx1 = wrapper.commit(rel_paths=['sub/bar', 'sub/ba2',
                                     'sub/dir/bar', 'sub/dir/ba2'],
                          message="zebar", add_remove=True)
    git_shas[ctx1.hex()] = git_repo.branches()[b'branch/default']['sha']
    ctx2 = wrapper.write_commit('sub/bar', message='default head')
    ctx3 = wrapper.write_commit('foo', parent=ctx1, branch='other',
                                message='other head')

    # mirror worked
    git_branches = git_repo.branches()
    assert set(git_branches) == {b'branch/default', b'branch/other'}

    # TODO check if we can access the hg-git map, would be easier
    git_shas[ctx2.hex()] = git_branches[b'branch/default']['sha']
    git_shas[ctx3.hex()] = git_branches[b'branch/other']['sha']

    hg_shas = {git: hg for hg, git in git_shas.items()}

    commit_stubs = dict(git=CommitServiceStub(fixture.gitaly_channel),
                        hg=CommitServiceStub(fixture.hgitaly_channel))

    def do_rpc(vcs, rev, path, offset=0, limit=None):
        if vcs == 'git' and len(rev) == 40:
            # defaulting useful for tests of unknown revs
            rev = git_shas.get(rev, rev)

        request = ListLastCommitsForTreeRequest(
            repository=repo_message,
            revision=rev,
            offset=offset,
            limit=1000 if limit is None else limit,
            path=path)

        def convert(sha):
            sha = sha.encode()
            if vcs == 'hg':
                return sha
            # fallback to incoming value for easier debugging than `None`
            return hg_shas.get(sha, sha)

        return [(cft.path_bytes, convert(cft.commit.id))
                for chunk in commit_stubs[vcs].ListLastCommitsForTree(request)
                for cft in chunk.commits]

    for path in (b'sub/dir', b'sub/dir/', b'', b'.', b'/', b'./',
                 b'sub', b'sub/', b'foo'):
        for rev in ('branch/default', 'branch/other', ctx2.hex(), ctx3.hex()):
90
            assert do_rpc('hg', rev, path) == do_rpc('git', rev, path)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

    assert (do_rpc('hg', 'branch/default', b'sub', offset=1)
            ==
            do_rpc('git', 'branch/default', b'sub', offset=1))

    rev, path = ctx2.hex(), b''
    assert (do_rpc('hg', rev, path, limit=0)
            ==
            do_rpc('git', rev, path, limit=0))

    with pytest.raises(grpc.RpcError) as exc_info_hg:
        do_rpc('hg', rev, path, limit=-1)
    with pytest.raises(grpc.RpcError) as exc_info_git:
        do_rpc('git', rev, path, limit=-1)
    assert exc_info_hg.value.code() == exc_info_git.value.code()
    assert exc_info_hg.value.details() == exc_info_git.value.details()

    with pytest.raises(grpc.RpcError) as exc_info_hg:
        do_rpc('hg', rev, path, offset=-1)
    with pytest.raises(grpc.RpcError) as exc_info_git:
        do_rpc('git', rev, path, offset=-1)
    assert exc_info_hg.value.code() == exc_info_git.value.code()
    assert exc_info_hg.value.details() == exc_info_git.value.details()

    # error won't be due to invalidity as a SHA
    # (let's not depend on Gitaly accepting revisions, here)
    rev = b'be0123ef' * 5
    with pytest.raises(grpc.RpcError) as exc_info_hg:
        do_rpc('hg', rev, path)
    with pytest.raises(grpc.RpcError) as exc_info_git:
        do_rpc('git', rev, path)
    assert exc_info_hg.value.code() == exc_info_git.value.code()
    assert exc_info_hg.value.details() == exc_info_git.value.details()
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193


def test_compare_raw_blame(gitaly_comparison):
    fixture = gitaly_comparison
    repo_message = fixture.gitaly_repo
    git_repo = fixture.git_repo

    wrapper = fixture.hg_repo_wrapper
    ctx0 = wrapper.commit_file('foo',
                               content='second_line\n'
                                       'third_line\n')
    git_shas = {
        ctx0.hex(): git_repo.branches()[b'branch/default']['sha'],
    }
    ctx1 = wrapper.commit_file('foo',
                               content='first_line\n'
                                       'second_line\n'
                                       'third_line\n'
                                       'forth_line\n')
    git_shas[ctx1.hex()] = git_repo.branches()[b'branch/default']['sha']
    hg_shas = {git: hg for hg, git in git_shas.items()}
    commit_stubs = dict(git=CommitServiceStub(fixture.gitaly_channel),
                        hg=CommitServiceStub(fixture.hgitaly_channel))

    def convert_sha(vcs, sha):
        if vcs == 'hg':
            return sha
        # fallback to incoming value for easier debugging than `None`
        return hg_shas.get(sha, sha)

    def convert_chunk(from_vcs, chunk):
        RAW_BLAME_LINE_REGEXP = re.compile(br'(\w{40}) (\d+) (\d+)')
        lines = chunk.splitlines(True)
        final = []
        for line in lines:
            hash_line = RAW_BLAME_LINE_REGEXP.match(line)
            if hash_line is not None:
                hash_id = convert_sha(from_vcs, hash_line.group(1))
                line_no = hash_line.group(2)
                old_line_no = hash_line.group(2)
                final.append((hash_id, line_no, old_line_no))
            elif line.startswith(b'\t'):
                final.append(line)
        return final

    def do_rpc(vcs, rev, path):
        if vcs == 'git' and len(rev) == 40:
            # defaulting useful for tests of unknown revs
            rev = git_shas.get(rev, rev)
        request = RawBlameRequest(
            repository=repo_message,
            revision=rev,
            path=path)
        response = commit_stubs[vcs].RawBlame(request)
        data = b''.join(resp.data for resp in response)
        return convert_chunk(vcs, data)

    def assert_compare_for(rev, fname):
        assert do_rpc('hg', rev, fname) == do_rpc('git', rev, fname)

    assert_compare_for(ctx0.hex(), b'foo')
    assert_compare_for(ctx1.hex(), b'foo')

    # error cases with empty path
    with pytest.raises(grpc.RpcError) as exc_info_hg:
        do_rpc('hg', ctx1.hex(), b'')
    with pytest.raises(grpc.RpcError) as exc_info_git:
        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()
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252


def test_compare_list_files(gitaly_comparison):
    fixture = gitaly_comparison
    repo_message = fixture.gitaly_repo
    git_repo = fixture.git_repo

    wrapper = fixture.hg_repo_wrapper
    ctx0 = wrapper.write_commit('foo', message="Some foo")
    git_shas = {
        ctx0.hex(): git_repo.branches()[b'branch/default']['sha'],
    }

    sub = (wrapper.path / 'sub')
    sub.mkdir()
    subdir = (sub / 'dir')
    subdir.mkdir()
    (sub / 'bar').write_text('bar content')
    (sub / 'ba2').write_text('ba2 content')
    (subdir / 'bar').write_text('bar content')
    (subdir / 'ba2').write_text('ba2 content')
    # TODO OS indep for paths (actually TODO make wrapper.commit easier to
    # use, e.g., check how to make it accept patterns)
    ctx1 = wrapper.commit(rel_paths=['sub/bar', 'sub/ba2',
                                     'sub/dir/bar', 'sub/dir/ba2'],
                          message="zebar", add_remove=True)
    git_shas[ctx1.hex()] = git_repo.branches()[b'branch/default']['sha']
    ctx2 = wrapper.write_commit('sub/bar', message='default head')
    ctx3 = wrapper.write_commit('zoo', parent=ctx0, branch='other',
                                message='other head')

    # mirror worked
    git_branches = git_repo.branches()
    assert set(git_branches) == {b'branch/default', b'branch/other'}

    # TODO check if we can access the hg-git map, would be easier
    git_shas[ctx2.hex()] = git_branches[b'branch/default']['sha']
    git_shas[ctx3.hex()] = git_branches[b'branch/other']['sha']

    commit_stubs = dict(git=CommitServiceStub(fixture.gitaly_channel),
                        hg=CommitServiceStub(fixture.hgitaly_channel))

    def do_rpc(vcs, rev):
        if vcs == 'git' and len(rev) == 40:
            # defaulting useful for tests of unknown revs
            rev = git_shas.get(rev, rev)
        request = ListFilesRequest(
            repository=repo_message,
            revision=rev)
        response = commit_stubs[vcs].ListFiles(request)
        final = []
        for resp in response:
            final.extend(resp.paths)
        return final

    not_exists = b'65face65' * 5
    for rev in [ctx0.hex(), ctx1.hex(), ctx2.hex(), ctx3.hex(),
                not_exists, b'branch/default', b'branch/other']:
        assert do_rpc('hg', rev) == do_rpc('git', rev)