genpb_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package generator
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  10. )
  11. func Test_findPbFile(t *testing.T) {
  12. dir := t.TempDir()
  13. protoFile := filepath.Join(dir, "greet.proto")
  14. err := ioutil.WriteFile(protoFile, []byte(`
  15. syntax = "proto3";
  16. package greet;
  17. option go_package="./greet";
  18. message Req{}
  19. message Resp{}
  20. service Greeter {
  21. rpc greet(Req) returns (Resp);
  22. }
  23. `), 0o666)
  24. if err != nil {
  25. t.Log(err)
  26. return
  27. }
  28. t.Run("", func(t *testing.T) {
  29. output := t.TempDir()
  30. grpc := filepath.Join(output, "grpc")
  31. err := pathx.MkdirIfNotExist(grpc)
  32. if err != nil {
  33. t.Log(err)
  34. return
  35. }
  36. cmd := exec.Command("protoc", "-I="+filepath.Dir(protoFile), "--go_out="+output, "--go-grpc_out="+grpc, filepath.Base(protoFile))
  37. cmd.Dir = output
  38. cmd.Stdout = os.Stdout
  39. cmd.Stderr = os.Stderr
  40. err = cmd.Run()
  41. if err != nil {
  42. t.Log(err)
  43. return
  44. }
  45. pbDir, err := findPbFile(output, false)
  46. assert.Nil(t, err)
  47. pbGo := filepath.Join(pbDir, "greet.pb.go")
  48. assert.True(t, pathx.FileExists(pbGo))
  49. grpcDir, err := findPbFile(output, true)
  50. assert.Nil(t, err)
  51. grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
  52. assert.True(t, pathx.FileExists(grpcGo))
  53. })
  54. t.Run("", func(t *testing.T) {
  55. output := t.TempDir()
  56. redirect := filepath.Join(output, "pb")
  57. grpc := filepath.Join(output, "grpc")
  58. err := pathx.MkdirIfNotExist(grpc)
  59. if err != nil {
  60. t.Log(err)
  61. return
  62. }
  63. cmd := exec.Command("protoc", "-I="+filepath.Dir(protoFile), "--go_out="+output,
  64. "--go-grpc_out="+grpc, filepath.Base(protoFile), "--go_opt=M"+filepath.Base(protoFile)+"="+redirect)
  65. cmd.Dir = output
  66. cmd.Stdout = os.Stdout
  67. cmd.Stderr = os.Stderr
  68. err = cmd.Run()
  69. if err != nil {
  70. t.Log(err)
  71. return
  72. }
  73. pbDir, err := findPbFile(output, false)
  74. assert.Nil(t, err)
  75. pbGo := filepath.Join(pbDir, "greet.pb.go")
  76. assert.True(t, pathx.FileExists(pbGo))
  77. grpcDir, err := findPbFile(output, true)
  78. assert.Nil(t, err)
  79. grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
  80. assert.True(t, pathx.FileExists(grpcGo))
  81. })
  82. t.Run("", func(t *testing.T) {
  83. output := t.TempDir()
  84. pbeRedirect := filepath.Join(output, "redirect")
  85. grpc := filepath.Join(output, "grpc")
  86. grpcRedirect := filepath.Join(grpc, "redirect")
  87. err := pathx.MkdirIfNotExist(grpc)
  88. if err != nil {
  89. t.Log(err)
  90. return
  91. }
  92. cmd := exec.Command("protoc", "-I="+filepath.Dir(protoFile), "--go_out="+output,
  93. "--go-grpc_out="+grpc, filepath.Base(protoFile), "--go_opt=M"+filepath.Base(protoFile)+"="+pbeRedirect,
  94. "--go-grpc_opt=M"+filepath.Base(protoFile)+"="+grpcRedirect)
  95. cmd.Dir = output
  96. cmd.Stdout = os.Stdout
  97. cmd.Stderr = os.Stderr
  98. err = cmd.Run()
  99. if err != nil {
  100. t.Log(err)
  101. return
  102. }
  103. pbDir, err := findPbFile(output, false)
  104. assert.Nil(t, err)
  105. pbGo := filepath.Join(pbDir, "greet.pb.go")
  106. assert.True(t, pathx.FileExists(pbGo))
  107. grpcDir, err := findPbFile(output, true)
  108. assert.Nil(t, err)
  109. grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
  110. assert.True(t, pathx.FileExists(grpcGo))
  111. })
  112. t.Run("", func(t *testing.T) {
  113. output := t.TempDir()
  114. pbeRedirect := filepath.Join(output, "redirect")
  115. grpc := filepath.Join(output, "grpc")
  116. grpcRedirect := filepath.Join(grpc, "redirect")
  117. err := pathx.MkdirIfNotExist(grpc)
  118. if err != nil {
  119. t.Log(err)
  120. return
  121. }
  122. cmd := exec.Command("protoc", "-I="+filepath.Dir(protoFile), "--go_out="+output,
  123. "--go-grpc_out="+grpc, filepath.Base(protoFile), "--go_opt=M"+filepath.Base(protoFile)+"="+pbeRedirect,
  124. "--go-grpc_opt=M"+filepath.Base(protoFile)+"="+grpcRedirect, "--go_opt=paths=import", "--go-grpc_opt=paths=source_relative")
  125. cmd.Dir = output
  126. cmd.Stdout = os.Stdout
  127. cmd.Stderr = os.Stderr
  128. err = cmd.Run()
  129. if err != nil {
  130. t.Log(err)
  131. return
  132. }
  133. pbDir, err := findPbFile(output, false)
  134. assert.Nil(t, err)
  135. pbGo := filepath.Join(pbDir, "greet.pb.go")
  136. assert.True(t, pathx.FileExists(pbGo))
  137. grpcDir, err := findPbFile(output, true)
  138. assert.Nil(t, err)
  139. grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
  140. assert.True(t, pathx.FileExists(grpcGo))
  141. })
  142. t.Run("", func(t *testing.T) {
  143. output := t.TempDir()
  144. pbeRedirect := filepath.Join(output, "redirect")
  145. grpc := filepath.Join(output, "grpc")
  146. grpcRedirect := filepath.Join(grpc, "redirect")
  147. err := pathx.MkdirIfNotExist(grpc)
  148. if err != nil {
  149. t.Log(err)
  150. return
  151. }
  152. err = pathx.MkdirIfNotExist(pbeRedirect)
  153. if err != nil {
  154. t.Log(err)
  155. return
  156. }
  157. err = pathx.MkdirIfNotExist(grpcRedirect)
  158. if err != nil {
  159. t.Log(err)
  160. return
  161. }
  162. cmd := exec.Command("protoc", "-I="+filepath.Dir(protoFile), "--go_out="+output,
  163. "--go-grpc_out="+grpc, filepath.Base(protoFile), "--go_opt=M"+filepath.Base(protoFile)+"="+pbeRedirect,
  164. "--go-grpc_opt=M"+filepath.Base(protoFile)+"="+grpcRedirect, "--go_opt=paths=import", "--go-grpc_opt=paths=source_relative",
  165. "--go_out="+pbeRedirect, "--go-grpc_out="+grpcRedirect)
  166. cmd.Dir = output
  167. cmd.Stdout = os.Stdout
  168. cmd.Stderr = os.Stderr
  169. err = cmd.Run()
  170. if err != nil {
  171. t.Log(err)
  172. return
  173. }
  174. pbDir, err := findPbFile(output, false)
  175. assert.Nil(t, err)
  176. pbGo := filepath.Join(pbDir, "greet.pb.go")
  177. assert.True(t, pathx.FileExists(pbGo))
  178. grpcDir, err := findPbFile(output, true)
  179. assert.Nil(t, err)
  180. grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
  181. assert.True(t, pathx.FileExists(grpcGo))
  182. })
  183. }