Skip to content

Commit c092c42

Browse files
committed
Add tests, update .golangci
Signed-off-by: Valery Piashchynski <piashchynski.valery@gmail.com>
1 parent 08d4aac commit c092c42

File tree

17 files changed

+416
-7
lines changed

17 files changed

+416
-7
lines changed

.golangci.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ linters: # All available linters list: <https://golangci-lint.run/usage/linters/
5252
- errorlint # find code that will cause problems with the error wrapping scheme introduced in Go 1.13
5353
- exhaustive # check exhaustiveness of enum switch statements
5454
- exportloopref # checks for pointers to enclosing loop variables
55-
- funlen # Tool for detection of long functions
5655
- gochecknoglobals # Checks that no globals are present in Go code
5756
- gochecknoinits # Checks that no init functions are present in Go code
5857
- gocognit # Computes and checks the cognitive complexity of functions
@@ -67,11 +66,8 @@ linters: # All available linters list: <https://golangci-lint.run/usage/linters/
6766
- gosimple # Linter for Go source code that specializes in simplifying a code
6867
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
6968
- ineffassign # Detects when assignments to existing variables are not used
70-
- lll # Reports long lines
7169
- misspell # Finds commonly misspelled English words in comments
7270
- nakedret # Finds naked returns in functions greater than a specified function length
73-
- nestif # Reports deeply nested if statements
74-
- nlreturn # checks for a new line before return and branch statements to increase code clarity
7571
- noctx # finds sending http request without context.Context
7672
- nolintlint # Reports ill-formed or insufficient nolint directives
7773
- prealloc # Finds slice declarations that could potentially be preallocated
@@ -86,7 +82,6 @@ linters: # All available linters list: <https://golangci-lint.run/usage/linters/
8682
- unused # Checks Go code for unused constants, variables, functions and types
8783
- varcheck # Finds unused global variables and constants
8884
- whitespace # Tool for detection of leading and trailing whitespace
89-
- wsl # Whitespace Linter - Forces you to use empty lines!
9085

9186
issues:
9287
exclude-rules:

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test:
2+
go test -v -race -cover -tags=debug ./...

config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type TLS struct {
5050
auth tls.ClientAuthType
5151
}
5252

53-
func (c *Config) InitDefaults() error { //nolint:gocognit
53+
func (c *Config) InitDefaults() error { //nolint:gocyclo,gocognit
5454
const op = errors.Op("grpc_plugin_config")
5555
if c.GrpcPool == nil {
5656
c.GrpcPool = &pool.Config{}

plugin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package grpc
22

33
import (
44
"context"
5+
stderr "errors"
56
"sync"
67

78
"github.com/roadrunner-server/api/v2/plugins/config"
@@ -112,7 +113,7 @@ func (p *Plugin) Serve() chan error {
112113
err = p.server.Serve(l)
113114
if err != nil {
114115
// skip errors when stopping the server
115-
if err == grpc.ErrServerStopped {
116+
if stderr.Is(err, grpc.ErrServerStopped) {
116117
return
117118
}
118119

protoc_plugins/protoc_test.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package protoc_plugins //nolint:revive,stylecheck
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func build() error {
15+
cmd := exec.Command("go", "build", "-o", "plugin", "../protoc_plugins/protoc-gen-php-grpc")
16+
return cmd.Run()
17+
}
18+
19+
func protoc(t *testing.T, args []string) {
20+
cmd := exec.Command("protoc", "--plugin=protoc-gen-php-grpc=./plugin")
21+
cmd.Args = append(cmd.Args, args...)
22+
out, err := cmd.CombinedOutput()
23+
24+
if len(out) > 0 || err != nil {
25+
t.Log("RUNNING: ", strings.Join(cmd.Args, " "))
26+
}
27+
28+
if len(out) > 0 {
29+
t.Log(string(out))
30+
}
31+
32+
if err != nil {
33+
t.Fatalf("protoc: %v", err)
34+
}
35+
}
36+
37+
func Test_Simple(t *testing.T) {
38+
workdir, _ := os.Getwd()
39+
tmpdir, err := ioutil.TempDir("", "proto-test")
40+
require.NoError(t, err)
41+
42+
require.NoError(t, build())
43+
44+
defer func() {
45+
assert.NoError(t, os.RemoveAll(tmpdir))
46+
}()
47+
48+
args := []string{
49+
"-Itestdata",
50+
"--php-grpc_out=" + tmpdir,
51+
"simple/simple.proto",
52+
}
53+
54+
protoc(t, args)
55+
56+
assertEqualFiles(
57+
t,
58+
workdir+"/testdata/simple/TestSimple/SimpleServiceInterface.php",
59+
tmpdir+"/TestSimple/SimpleServiceInterface.php",
60+
)
61+
assert.NoError(t, os.RemoveAll("plugin"))
62+
}
63+
64+
func Test_PhpNamespaceOption(t *testing.T) {
65+
workdir, _ := os.Getwd()
66+
tmpdir, err := ioutil.TempDir("", "proto-test")
67+
require.NoError(t, err)
68+
69+
require.NoError(t, build())
70+
71+
defer func() {
72+
assert.NoError(t, os.RemoveAll(tmpdir))
73+
}()
74+
75+
args := []string{
76+
"-Itestdata",
77+
"--php-grpc_out=" + tmpdir,
78+
"php_namespace/service.proto",
79+
}
80+
protoc(t, args)
81+
82+
assertEqualFiles(
83+
t,
84+
workdir+"/testdata/php_namespace/Test/CustomNamespace/ServiceInterface.php",
85+
tmpdir+"/Test/CustomNamespace/ServiceInterface.php",
86+
)
87+
assert.NoError(t, os.RemoveAll("plugin"))
88+
}
89+
90+
func Test_UseImportedMessage(t *testing.T) {
91+
workdir, _ := os.Getwd()
92+
tmpdir, err := ioutil.TempDir("", "proto-test")
93+
require.NoError(t, err)
94+
95+
require.NoError(t, build())
96+
97+
defer func() {
98+
assert.NoError(t, os.RemoveAll(tmpdir))
99+
}()
100+
101+
args := []string{
102+
"-Itestdata",
103+
"--php-grpc_out=" + tmpdir,
104+
"import/service.proto",
105+
}
106+
protoc(t, args)
107+
108+
assertEqualFiles(
109+
t,
110+
workdir+"/testdata/import/Import/ServiceInterface.php",
111+
tmpdir+"/Import/ServiceInterface.php",
112+
)
113+
assert.NoError(t, os.RemoveAll("plugin"))
114+
}
115+
116+
func Test_PhpNamespaceOptionInUse(t *testing.T) {
117+
workdir, _ := os.Getwd()
118+
tmpdir, err := ioutil.TempDir("", "proto-test")
119+
require.NoError(t, err)
120+
121+
require.NoError(t, build())
122+
123+
defer func() {
124+
assert.NoError(t, os.RemoveAll(tmpdir))
125+
}()
126+
127+
args := []string{
128+
"-Itestdata",
129+
"--php-grpc_out=" + tmpdir,
130+
"import_custom/service.proto",
131+
}
132+
protoc(t, args)
133+
134+
assertEqualFiles(
135+
t,
136+
workdir+"/testdata/import_custom/Test/CustomImport/ServiceInterface.php",
137+
tmpdir+"/Test/CustomImport/ServiceInterface.php",
138+
)
139+
assert.NoError(t, os.RemoveAll("plugin"))
140+
}
141+
142+
func Test_UseOfGoogleEmptyMessage(t *testing.T) {
143+
workdir, _ := os.Getwd()
144+
tmpdir, err := ioutil.TempDir("", "proto-test")
145+
require.NoError(t, err)
146+
147+
require.NoError(t, build())
148+
149+
defer func() {
150+
assert.NoError(t, os.RemoveAll(tmpdir))
151+
}()
152+
153+
args := []string{
154+
"-Itestdata",
155+
"--php-grpc_out=" + tmpdir,
156+
"use_empty/service.proto",
157+
}
158+
protoc(t, args)
159+
160+
assertEqualFiles(
161+
t,
162+
workdir+"/testdata/use_empty/Test/ServiceInterface.php",
163+
tmpdir+"/Test/ServiceInterface.php",
164+
)
165+
166+
assert.NoError(t, os.RemoveAll("plugin"))
167+
}
168+
169+
func assertEqualFiles(t *testing.T, original, generated string) {
170+
assert.FileExists(t, generated)
171+
172+
originalData, err := ioutil.ReadFile(original)
173+
if err != nil {
174+
t.Fatal("Can't find original file for comparison")
175+
}
176+
177+
generatedData, err := ioutil.ReadFile(generated)
178+
if err != nil {
179+
t.Fatal("Can't find generated file for comparison")
180+
}
181+
182+
// every OS has a special boy
183+
r := strings.NewReplacer("\r\n", "", "\n", "")
184+
assert.Equal(t, r.Replace(string(originalData)), r.Replace(string(generatedData)))
185+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
# Generated by the protocol buffer compiler (spiral/php-grpc). DO NOT EDIT!
3+
# source: import/service.proto
4+
5+
namespace Import;
6+
7+
use Spiral\RoadRunner\GRPC;
8+
use Import\Sub;
9+
10+
interface ServiceInterface extends GRPC\ServiceInterface
11+
{
12+
// GRPC specific service name.
13+
public const NAME = "import.Service";
14+
15+
/**
16+
* @param GRPC\ContextInterface $ctx
17+
* @param Message $in
18+
* @return Message
19+
*
20+
* @throws GRPC\Exception\InvokeException
21+
*/
22+
public function SimpleMethod(GRPC\ContextInterface $ctx, Message $in): Message;
23+
24+
/**
25+
* @param GRPC\ContextInterface $ctx
26+
* @param Sub\Message $in
27+
* @return Sub\Message
28+
*
29+
* @throws GRPC\Exception\InvokeException
30+
*/
31+
public function ImportMethod(GRPC\ContextInterface $ctx, Sub\Message $in): Sub\Message;
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
syntax = "proto3";
2+
3+
package import;
4+
5+
import "import/sub/message.proto";
6+
7+
service Service {
8+
rpc SimpleMethod (Message) returns (Message) {
9+
}
10+
11+
rpc ImportMethod (import.sub.Message) returns (import.sub.Message) {
12+
}
13+
}
14+
15+
message Message {
16+
int64 id = 1;
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package import.sub;
4+
5+
message Message {
6+
int64 id = 1;
7+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
# Generated by the protocol buffer compiler (spiral/php-grpc). DO NOT EDIT!
3+
# source: import_custom/service.proto
4+
5+
namespace Test\CustomImport;
6+
7+
use Spiral\RoadRunner\GRPC;
8+
use Test\CustomImport\Message;
9+
10+
interface ServiceInterface extends GRPC\ServiceInterface
11+
{
12+
// GRPC specific service name.
13+
public const NAME = "import.Service";
14+
15+
/**
16+
* @param GRPC\ContextInterface $ctx
17+
* @param Message $in
18+
* @return Message
19+
*
20+
* @throws GRPC\Exception\InvokeException
21+
*/
22+
public function SimpleMethod(GRPC\ContextInterface $ctx, Message $in): Message;
23+
24+
/**
25+
* @param GRPC\ContextInterface $ctx
26+
* @param Message\Message $in
27+
* @return Message\Message
28+
*
29+
* @throws GRPC\Exception\InvokeException
30+
*/
31+
public function ImportMethod(GRPC\ContextInterface $ctx, Message\Message $in): Message\Message;
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
syntax = "proto3";
2+
3+
package import;
4+
5+
option php_namespace = "Test\\CustomImport";
6+
7+
import "import_custom/sub/message.proto";
8+
9+
service Service {
10+
rpc SimpleMethod (Message) returns (Message) {
11+
}
12+
13+
rpc ImportMethod (import.sub.Message) returns (import.sub.Message) {
14+
}
15+
}
16+
17+
message Message {
18+
int64 id = 1;
19+
}

0 commit comments

Comments
 (0)