gen_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package generator
  2. import (
  3. "fmt"
  4. "go/build"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. "github.com/zeromicro/go-zero/core/stringx"
  12. "github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
  13. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  14. )
  15. func TestRpcGenerate(t *testing.T) {
  16. _ = Clean()
  17. g := NewGenerator("gozero", true)
  18. err := g.Prepare()
  19. if err != nil {
  20. logx.Error(err)
  21. return
  22. }
  23. projectName := stringx.Rand()
  24. src := filepath.Join(build.Default.GOPATH, "src")
  25. _, err = os.Stat(src)
  26. if err != nil {
  27. return
  28. }
  29. projectDir := filepath.Join(src, projectName)
  30. srcDir := projectDir
  31. defer func() {
  32. _ = os.RemoveAll(srcDir)
  33. }()
  34. common, err := filepath.Abs(".")
  35. assert.Nil(t, err)
  36. // case go path
  37. t.Run("GOPATH", func(t *testing.T) {
  38. ctx := &ZRpcContext{
  39. Src: "./test.proto",
  40. ProtocCmd: fmt.Sprintf("protoc -I=%s test.proto --go_out=%s --go_opt=Mbase/common.proto=./base --go-grpc_out=%s", common, projectDir, projectDir),
  41. IsGooglePlugin: true,
  42. GoOutput: projectDir,
  43. GrpcOutput: projectDir,
  44. Output: projectDir,
  45. }
  46. err = g.Generate(ctx)
  47. assert.Nil(t, err)
  48. _, err = execx.Run("go test "+projectName, projectDir)
  49. if err != nil {
  50. assert.True(t, func() bool {
  51. return strings.Contains(err.Error(), "not in GOROOT") || strings.Contains(err.Error(), "cannot find package")
  52. }())
  53. }
  54. })
  55. // case go mod
  56. t.Run("GOMOD", func(t *testing.T) {
  57. workDir := pathx.MustTempDir()
  58. name := filepath.Base(workDir)
  59. _, err = execx.Run("go mod init "+name, workDir)
  60. if err != nil {
  61. logx.Error(err)
  62. return
  63. }
  64. projectDir = filepath.Join(workDir, projectName)
  65. ctx := &ZRpcContext{
  66. Src: "./test.proto",
  67. ProtocCmd: fmt.Sprintf("protoc -I=%s test.proto --go_out=%s --go_opt=Mbase/common.proto=./base --go-grpc_out=%s", common, projectDir, projectDir),
  68. IsGooglePlugin: true,
  69. GoOutput: projectDir,
  70. GrpcOutput: projectDir,
  71. Output: projectDir,
  72. }
  73. err = g.Generate(ctx)
  74. assert.Nil(t, err)
  75. _, err = execx.Run("go test "+projectName, projectDir)
  76. if err != nil {
  77. assert.True(t, func() bool {
  78. return strings.Contains(err.Error(), "not in GOROOT") || strings.Contains(err.Error(), "cannot find package")
  79. }())
  80. }
  81. })
  82. // case not in go mod and go path
  83. t.Run("OTHER", func(t *testing.T) {
  84. ctx := &ZRpcContext{
  85. Src: "./test.proto",
  86. ProtocCmd: fmt.Sprintf("protoc -I=%s test.proto --go_out=%s --go_opt=Mbase/common.proto=./base --go-grpc_out=%s", common, projectDir, projectDir),
  87. IsGooglePlugin: true,
  88. GoOutput: projectDir,
  89. GrpcOutput: projectDir,
  90. Output: projectDir,
  91. }
  92. err = g.Generate(ctx)
  93. assert.Nil(t, err)
  94. _, err = execx.Run("go test "+projectName, projectDir)
  95. if err != nil {
  96. assert.True(t, func() bool {
  97. return strings.Contains(err.Error(), "not in GOROOT") || strings.Contains(err.Error(), "cannot find package")
  98. }())
  99. }
  100. })
  101. }