1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
90
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
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
|
package grpc
import (
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func init() {
err := build()
if err != nil {
panic(err)
}
}
func build() error {
cmd := exec.Command("go", "build", "-o", "plugin", "../../../plugins/grpc/protoc_plugins/protoc-gen-php-grpc")
return cmd.Run()
}
func protoc(t *testing.T, args []string) {
cmd := exec.Command("protoc", "--plugin=protoc-gen-php-grpc=./plugin")
cmd.Args = append(cmd.Args, args...)
out, err := cmd.CombinedOutput()
if len(out) > 0 || err != nil {
t.Log("RUNNING: ", strings.Join(cmd.Args, " "))
}
if len(out) > 0 {
t.Log(string(out))
}
if err != nil {
t.Fatalf("protoc: %v", err)
}
}
func Test_Simple(t *testing.T) {
workdir, _ := os.Getwd()
tmpdir, err := ioutil.TempDir("", "proto-test")
require.NoError(t, err)
defer func() {
assert.NoError(t, os.RemoveAll(tmpdir))
}()
args := []string{
"-Itestdata",
"--php-grpc_out=" + tmpdir,
"simple/simple.proto",
}
protoc(t, args)
assertEqualFiles(
t,
workdir+"/testdata/simple/TestSimple/SimpleServiceInterface.php",
tmpdir+"/TestSimple/SimpleServiceInterface.php",
)
}
func Test_PhpNamespaceOption(t *testing.T) {
workdir, _ := os.Getwd()
tmpdir, err := ioutil.TempDir("", "proto-test")
require.NoError(t, err)
defer func() {
assert.NoError(t, os.RemoveAll(tmpdir))
}()
args := []string{
"-Itestdata",
"--php-grpc_out=" + tmpdir,
"php_namespace/service.proto",
}
protoc(t, args)
assertEqualFiles(
t,
workdir+"/testdata/php_namespace/Test/CustomNamespace/ServiceInterface.php",
tmpdir+"/Test/CustomNamespace/ServiceInterface.php",
)
}
func Test_UseImportedMessage(t *testing.T) {
workdir, _ := os.Getwd()
tmpdir, err := ioutil.TempDir("", "proto-test")
require.NoError(t, err)
defer func() {
assert.NoError(t, os.RemoveAll(tmpdir))
}()
args := []string{
"-Itestdata",
"--php-grpc_out=" + tmpdir,
"import/service.proto",
}
protoc(t, args)
assertEqualFiles(
t,
workdir+"/testdata/import/Import/ServiceInterface.php",
tmpdir+"/Import/ServiceInterface.php",
)
}
func Test_PhpNamespaceOptionInUse(t *testing.T) {
workdir, _ := os.Getwd()
tmpdir, err := ioutil.TempDir("", "proto-test")
require.NoError(t, err)
defer func() {
assert.NoError(t, os.RemoveAll(tmpdir))
}()
args := []string{
"-Itestdata",
"--php-grpc_out=" + tmpdir,
"import_custom/service.proto",
}
protoc(t, args)
assertEqualFiles(
t,
workdir+"/testdata/import_custom/Test/CustomImport/ServiceInterface.php",
tmpdir+"/Test/CustomImport/ServiceInterface.php",
)
}
func Test_UseOfGoogleEmptyMessage(t *testing.T) {
workdir, _ := os.Getwd()
tmpdir, err := ioutil.TempDir("", "proto-test")
require.NoError(t, err)
defer func() {
assert.NoError(t, os.RemoveAll(tmpdir))
}()
args := []string{
"-Itestdata",
"--php-grpc_out=" + tmpdir,
"use_empty/service.proto",
}
protoc(t, args)
assertEqualFiles(
t,
workdir+"/testdata/use_empty/Test/ServiceInterface.php",
tmpdir+"/Test/ServiceInterface.php",
)
assert.NoError(t, os.RemoveAll("plugin"))
}
func assertEqualFiles(t *testing.T, original, generated string) {
assert.FileExists(t, generated)
originalData, err := ioutil.ReadFile(original)
if err != nil {
t.Fatal("Can't find original file for comparison")
}
generatedData, err := ioutil.ReadFile(generated)
if err != nil {
t.Fatal("Can't find generated file for comparison")
}
// every OS has a special boy
r := strings.NewReplacer("\r\n", "", "\n", "")
assert.Equal(t, r.Replace(string(originalData)), r.Replace(string(generatedData)))
}
|