Skip to content

Commit a429a54

Browse files
committed
Add testing of exec Runners
1 parent d99328f commit a429a54

File tree

2 files changed

+96
-88
lines changed

2 files changed

+96
-88
lines changed

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ MAKEFLAGS += --warn-undefined-variables
77
MAKEFLAGS += --no-builtin-rules
88
MAKEFLAGS += --no-print-directory
99
BIN := .tmp/bin
10-
export PATH := $(BIN):$(PATH)
10+
export PATH := $(abspath $(BIN)):$(PATH)
1111
export GOBIN := $(abspath $(BIN))
1212
COPYRIGHT_YEARS := 2024
1313
LICENSE_IGNORE := --ignore /testdata/
@@ -32,7 +32,7 @@ clean: ## Delete intermediate build artifacts
3232
git clean -Xdf
3333

3434
.PHONY: test
35-
test: build ## Run unit tests
35+
test: build $(BIN)/example-plugin ## Run unit tests
3636
go test -vet=off -race -cover ./...
3737

3838
.PHONY: build
@@ -90,3 +90,8 @@ $(BIN)/protoc-gen-go: Makefile go.mod
9090
$(BIN)/protoc-gen-pluginrpc-go:
9191
@mkdir -p $(@D)
9292
go build -o $(@) ./cmd/protoc-gen-pluginrpc-go
93+
94+
.PHONY: $(BIN)/example-plugin
95+
$(BIN)/example-plugin:
96+
@mkdir -p $(@D)
97+
go build -o $(@) ./internal/example/cmd/example-plugin

pluginrpc_test.go

Lines changed: 89 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"errors"
2020
"slices"
21+
"strconv"
2122
"testing"
2223

2324
pluginrpcv1 "buf.build/gen/go/pluginrpc/pluginrpc/protocolbuffers/go/pluginrpc/v1"
@@ -27,113 +28,115 @@ import (
2728
"pluginrpc.com/pluginrpc/internal/example/gen/pluginrpc/example/v1/examplev1pluginrpc"
2829
)
2930

31+
const echoServicePluginProgramName = "example-plugin"
32+
3033
// We want to append 0 so that we call pluginrpc.ClientWithFormat with the default Format.
3134
var allTestFormats = append(slices.Clone(pluginrpc.AllFormats), 0)
3235

3336
func TestEchoRequest(t *testing.T) {
3437
t.Parallel()
35-
server, err := newServer()
36-
require.NoError(t, err)
37-
for _, format := range allTestFormats {
38-
format := format
39-
t.Run(
40-
format.String(),
41-
func(t *testing.T) {
42-
t.Parallel()
43-
echoServiceClient, err := examplev1pluginrpc.NewEchoServiceClient(newClient(server, pluginrpc.ClientWithFormat(format)))
44-
require.NoError(t, err)
45-
response, err := echoServiceClient.EchoRequest(
46-
context.Background(),
47-
&examplev1.EchoRequestRequest{
48-
Message: "hello",
49-
},
50-
)
51-
require.NoError(t, err)
52-
require.NotNil(t, response)
53-
require.Equal(t, "hello", response.GetMessage())
54-
},
55-
)
56-
}
38+
forEachDimension(
39+
t,
40+
func(t *testing.T, client pluginrpc.Client) {
41+
echoServiceClient, err := examplev1pluginrpc.NewEchoServiceClient(client)
42+
require.NoError(t, err)
43+
response, err := echoServiceClient.EchoRequest(
44+
context.Background(),
45+
&examplev1.EchoRequestRequest{
46+
Message: "hello",
47+
},
48+
)
49+
require.NoError(t, err)
50+
require.NotNil(t, response)
51+
require.Equal(t, "hello", response.GetMessage())
52+
},
53+
)
5754
}
5855

5956
func TestEchoRequestNil(t *testing.T) {
6057
t.Parallel()
61-
server, err := newServer()
62-
require.NoError(t, err)
63-
for _, format := range allTestFormats {
64-
format := format
65-
t.Run(
66-
format.String(),
67-
func(t *testing.T) {
68-
t.Parallel()
69-
echoServiceClient, err := examplev1pluginrpc.NewEchoServiceClient(newClient(server, pluginrpc.ClientWithFormat(format)))
70-
require.NoError(t, err)
71-
response, err := echoServiceClient.EchoRequest(
72-
context.Background(),
73-
nil,
74-
)
75-
require.NoError(t, err)
76-
require.NotNil(t, response)
77-
require.Equal(t, "", response.GetMessage())
78-
},
79-
)
80-
}
58+
forEachDimension(
59+
t,
60+
func(t *testing.T, client pluginrpc.Client) {
61+
echoServiceClient, err := examplev1pluginrpc.NewEchoServiceClient(client)
62+
require.NoError(t, err)
63+
response, err := echoServiceClient.EchoRequest(context.Background(), nil)
64+
require.NoError(t, err)
65+
require.NotNil(t, response)
66+
require.Equal(t, "", response.GetMessage())
67+
},
68+
)
8169
}
8270

8371
func TestEchoList(t *testing.T) {
8472
t.Parallel()
85-
server, err := newServer()
86-
require.NoError(t, err)
87-
for _, format := range allTestFormats {
88-
format := format
89-
t.Run(
90-
format.String(),
91-
func(t *testing.T) {
92-
t.Parallel()
93-
echoServiceClient, err := examplev1pluginrpc.NewEchoServiceClient(newClient(server, pluginrpc.ClientWithFormat(format)))
94-
require.NoError(t, err)
95-
response, err := echoServiceClient.EchoList(context.Background(), nil)
96-
require.NoError(t, err)
97-
require.NotNil(t, response)
98-
require.Equal(t, []string{"foo", "bar"}, response.GetList())
99-
},
100-
)
101-
}
73+
forEachDimension(
74+
t,
75+
func(t *testing.T, client pluginrpc.Client) {
76+
echoServiceClient, err := examplev1pluginrpc.NewEchoServiceClient(client)
77+
require.NoError(t, err)
78+
response, err := echoServiceClient.EchoList(context.Background(), nil)
79+
require.NoError(t, err)
80+
require.NotNil(t, response)
81+
require.Equal(t, []string{"foo", "bar"}, response.GetList())
82+
},
83+
)
10284
}
10385

10486
func TestEchoError(t *testing.T) {
10587
t.Parallel()
106-
server, err := newServer()
107-
require.NoError(t, err)
88+
forEachDimension(
89+
t,
90+
func(t *testing.T, client pluginrpc.Client) {
91+
echoServiceClient, err := examplev1pluginrpc.NewEchoServiceClient(client)
92+
require.NoError(t, err)
93+
_, err = echoServiceClient.EchoError(
94+
context.Background(),
95+
&examplev1.EchoErrorRequest{
96+
Code: pluginrpcv1.Code_CODE_DEADLINE_EXCEEDED,
97+
Message: "hello",
98+
},
99+
)
100+
pluginrpcError := &pluginrpc.Error{}
101+
require.Error(t, err)
102+
require.ErrorAs(t, err, &pluginrpcError)
103+
require.Equal(t, pluginrpc.CodeDeadlineExceeded, pluginrpcError.Code())
104+
unwrappedErr := pluginrpcError.Unwrap()
105+
require.Error(t, unwrappedErr)
106+
require.Equal(t, "hello", unwrappedErr.Error())
107+
},
108+
)
109+
}
110+
111+
func forEachDimension(t *testing.T, f func(*testing.T, pluginrpc.Client)) {
108112
for _, format := range allTestFormats {
109-
format := format
110-
t.Run(
111-
format.String(),
112-
func(t *testing.T) {
113-
t.Parallel()
114-
echoServiceClient, err := examplev1pluginrpc.NewEchoServiceClient(newClient(server, pluginrpc.ClientWithFormat(format)))
115-
require.NoError(t, err)
116-
_, err = echoServiceClient.EchoError(
117-
context.Background(),
118-
&examplev1.EchoErrorRequest{
119-
Code: pluginrpcv1.Code_CODE_DEADLINE_EXCEEDED,
120-
Message: "hello",
121-
},
122-
)
123-
pluginrpcError := &pluginrpc.Error{}
124-
require.Error(t, err)
125-
require.ErrorAs(t, err, &pluginrpcError)
126-
require.Equal(t, pluginrpc.CodeDeadlineExceeded, pluginrpcError.Code())
127-
unwrappedErr := pluginrpcError.Unwrap()
128-
require.Error(t, unwrappedErr)
129-
require.Equal(t, "hello", unwrappedErr.Error())
130-
},
131-
)
113+
for j, newClient := range []func(...pluginrpc.ClientOption) (pluginrpc.Client, error){newExecRunnerClient, newServerRunnerClient} {
114+
j := j
115+
format := format
116+
newClient := newClient
117+
t.Run(
118+
format.String()+strconv.Itoa(j),
119+
func(t *testing.T) {
120+
t.Parallel()
121+
client, err := newClient(pluginrpc.ClientWithFormat(format))
122+
require.NoError(t, err)
123+
f(t, client)
124+
},
125+
)
126+
}
132127
}
133128
}
134129

135-
func newClient(server pluginrpc.Server, clientOptions ...pluginrpc.ClientOption) pluginrpc.Client {
136-
return pluginrpc.NewClient(pluginrpc.NewServerRunner(server), clientOptions...)
130+
func newExecRunnerClient(clientOptions ...pluginrpc.ClientOption) (pluginrpc.Client, error) {
131+
return pluginrpc.NewClient(pluginrpc.NewExecRunner(echoServicePluginProgramName), clientOptions...), nil
132+
}
133+
134+
func newServerRunnerClient(clientOptions ...pluginrpc.ClientOption) (pluginrpc.Client, error) {
135+
server, err := newServer()
136+
if err != nil {
137+
return nil, err
138+
}
139+
return pluginrpc.NewClient(pluginrpc.NewServerRunner(server), clientOptions...), nil
137140
}
138141

139142
func newServer() (pluginrpc.Server, error) {

0 commit comments

Comments
 (0)