From 74f2e25a0cd9c081444dd202e6506af162c82db3 Mon Sep 17 00:00:00 2001 From: Geoffrey Gilmore Date: Sat, 11 May 2024 01:37:29 -0700 Subject: [PATCH] gitserver: grpc: create proto roundtrip tests for GetObject[Request/Response] (#62564) Closes https://github.com/sourcegraph/sourcegraph/issues/60411 We didn't have any round trip tests for the GetCommit request or response type. This PR adds them. ## Test plan New unit tests --- internal/gitserver/protocol/BUILD.bazel | 1 + internal/gitserver/protocol/gitserver.go | 1 - internal/gitserver/protocol/gitserver_test.go | 60 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/internal/gitserver/protocol/BUILD.bazel b/internal/gitserver/protocol/BUILD.bazel index 9331ac622e5..1d285f1dbc9 100644 --- a/internal/gitserver/protocol/BUILD.bazel +++ b/internal/gitserver/protocol/BUILD.bazel @@ -32,6 +32,7 @@ go_test( embed = [":protocol"], deps = [ "//internal/api", + "//internal/gitserver/gitdomain", "//internal/search/result", "@com_github_google_go_cmp//cmp", "@com_github_stretchr_testify//require", diff --git a/internal/gitserver/protocol/gitserver.go b/internal/gitserver/protocol/gitserver.go index f55402a69b4..793e1c974ff 100644 --- a/internal/gitserver/protocol/gitserver.go +++ b/internal/gitserver/protocol/gitserver.go @@ -543,7 +543,6 @@ func (r *GetObjectResponse) FromProto(p *proto.GetObjectResponse) { *r = GetObjectResponse{ Object: gitObj, } - } // IsPerforcePathCloneableRequest is the request to check if a Perforce path is cloneable. diff --git a/internal/gitserver/protocol/gitserver_test.go b/internal/gitserver/protocol/gitserver_test.go index 41175835f3a..93dcc2afb92 100644 --- a/internal/gitserver/protocol/gitserver_test.go +++ b/internal/gitserver/protocol/gitserver_test.go @@ -1,6 +1,8 @@ package protocol import ( + "math/rand" + "reflect" "testing" "testing/quick" "time" @@ -9,6 +11,7 @@ import ( "github.com/stretchr/testify/require" "github.com/sourcegraph/sourcegraph/internal/api" + "github.com/sourcegraph/sourcegraph/internal/gitserver/gitdomain" "github.com/sourcegraph/sourcegraph/internal/search/result" ) @@ -79,3 +82,60 @@ func TestCommitMatchProtoRoundtrip(t *testing.T) { roundtripped := CommitMatchFromProto(protoReq) require.Equal(t, req, roundtripped) } + +func TestGetObjectRequestProtoRoundtrip(t *testing.T) { + var diff string + + fn := func(original GetObjectRequest) bool { + protoReq := original.ToProto() + + var converted GetObjectRequest + converted.FromProto(protoReq) + + if diff = cmp.Diff(original, converted); diff != "" { + return false + } + + return true + } + + if err := quick.Check(fn, nil); err != nil { + t.Errorf("GetObjectRequest proto roundtrip failed (-want +got):\n%s", diff) + } +} + +func TestGetObjectResponseProtoRoundtrip(t *testing.T) { + var diff string + + fn := func(id [20]byte, typ fuzzObjectType) bool { + original := GetObjectResponse{ + Object: gitdomain.GitObject{ + ID: id, + Type: gitdomain.ObjectType(typ), + }, + } + protoResp := original.ToProto() + + var converted GetObjectResponse + converted.FromProto(protoResp) + + if diff = cmp.Diff(original, converted); diff != "" { + return false + } + + return true + } + + if err := quick.Check(fn, nil); err != nil { + t.Errorf("GetObjectResponse proto roundtrip failed (-want +got):\n%s", diff) + } +} + +type fuzzObjectType gitdomain.ObjectType + +func (fuzzObjectType) Generate(r *rand.Rand, _ int) reflect.Value { + validValues := []gitdomain.ObjectType{gitdomain.ObjectTypeCommit, gitdomain.ObjectTypeTag, gitdomain.ObjectTypeTree, gitdomain.ObjectTypeBlob} + return reflect.ValueOf(fuzzObjectType(validValues[r.Intn(len(validValues))])) +} + +var _ quick.Generator = fuzzObjectType(gitdomain.ObjectTypeCommit)