diff options
author | Valery Piashchynski <[email protected]> | 2021-09-16 21:24:13 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2021-09-16 21:24:13 +0300 |
commit | 337d292dd2d6ff0a555098b1970d8194d8df8bc2 (patch) | |
tree | a2ab31666f95813a592bea2b207f2db0ba188c92 /tests/plugins/grpc/plugin_test.go | |
parent | 5d2cd55ab522d4f1e65a833f91146444465a32ac (diff) | |
parent | cc56349f3ad19aa54ae7900c50e018d757305804 (diff) |
[#783]: feat(grpc): update GRPC plugin to RR `v2`
[#783]: feat(grpc): update GRPC plugin to RR `v2`
Diffstat (limited to 'tests/plugins/grpc/plugin_test.go')
-rw-r--r-- | tests/plugins/grpc/plugin_test.go | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/tests/plugins/grpc/plugin_test.go b/tests/plugins/grpc/plugin_test.go new file mode 100644 index 00000000..cfbe0121 --- /dev/null +++ b/tests/plugins/grpc/plugin_test.go @@ -0,0 +1,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))) +} |