Skip to content

Commit

Permalink
Add integration tests for protoc-gen-go-grpc (#3787)
Browse files Browse the repository at this point in the history
* Add integration tests for protoc-gen-go-grpc

* Update tests/integration/grpc_plugin/BUILD.bazel

Co-authored-by: Marcel <[email protected]>

* fix build

---------

Co-authored-by: Marcel <[email protected]>
  • Loading branch information
ryanpbrewster and mering authored Dec 14, 2023
1 parent 5df0c5e commit 995bd39
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/integration/grpc_plugin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

proto_library(
name = "hello_proto",
srcs = ["hello.proto"],
deps = [],
)

go_grpc_library(
name = "hello_go_proto",
importpath = "github.com/bazelbuild/rules_go/tests/integration/grpc_plugin/hello_proto",
proto = ":hello_proto",
deps = [],
)

go_library(
name = "hello",
srcs = ["hello.go"],
importpath = "github.com/bazelbuild/rules_go/tests/integration/grpc_plugin/hello",
deps = [
":hello_go_proto",
],
)

go_test(
name = "hello_test",
srcs = ["hello_test.go"],
deps = [
":hello",
":hello_go_proto",
"@org_golang_google_grpc//:go_default_library",
],
)
9 changes: 9 additions & 0 deletions tests/integration/grpc_plugin/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Testing that the protoc-gen-go-grpc plugin works
=======================================

hello_test
------------------

Verifies that the generated code for a simple gRPC service:
- exposes a `pb.UnimplementedGreetServer` style struct
- exposes a `pb.RegisterGreetServer` method that accepts a `grpc.ServiceRegistrar` instead of a raw `*grpc.Server`
33 changes: 33 additions & 0 deletions tests/integration/grpc_plugin/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2023 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hello

import (
"context"

pb "github.com/bazelbuild/rules_go/tests/integration/grpc_plugin/hello_proto"
)

type greeter struct {
pb.UnimplementedGreetServer
}

func GreetServer() pb.GreetServer {
return &greeter{}
}

func (g *greeter) Hello(ctx context.Context, r *pb.HelloRequest) (*pb.HelloResponse, error) {
return &pb.HelloResponse{}, nil
}
26 changes: 26 additions & 0 deletions tests/integration/grpc_plugin/hello.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2023 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package rules_go.tests.integration.grpc_plugin;

option go_package = "github.com/bazelbuild/rules_go/tests/integration/grpc_plugin/hello_proto";

service Greet {
rpc Hello(HelloRequest) returns (HelloResponse);
}

message HelloRequest {}
message HelloResponse {}
40 changes: 40 additions & 0 deletions tests/integration/grpc_plugin/hello_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2023 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hello_test

import (
"reflect"
"testing"

"github.com/bazelbuild/rules_go/tests/integration/grpc_plugin/hello"
pb "github.com/bazelbuild/rules_go/tests/integration/grpc_plugin/hello_proto"
"google.golang.org/grpc"
)

func Test_ServiceRegistrar(t *testing.T) {
fr := &fakeRegistrar{}
pb.RegisterGreetServer(fr, hello.GreetServer())
if got, want := fr.services, []string{"rules_go.tests.integration.grpc_plugin.Greet"}; !reflect.DeepEqual(got, want) {
t.Fatalf("got %v, want %v", got, want)
}
}

type fakeRegistrar struct {
services []string
}

func (fr *fakeRegistrar) RegisterService(desc *grpc.ServiceDesc, impl any) {
fr.services = append(fr.services, desc.ServiceName)
}

0 comments on commit 995bd39

Please sign in to comment.