zrpc_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package cli
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/zeromicro/go-zero/tools/goctl/util/console"
  9. )
  10. type test struct {
  11. source []string
  12. expected string
  13. expectedErr error
  14. }
  15. func Test_GetSourceProto(t *testing.T) {
  16. pwd, err := os.Getwd()
  17. if err != nil {
  18. console.Error(err.Error())
  19. return
  20. }
  21. testData := []test{
  22. {
  23. source: []string{"a.proto"},
  24. expected: filepath.Join(pwd, "a.proto"),
  25. },
  26. {
  27. source: []string{"/foo/bar/a.proto"},
  28. expected: "/foo/bar/a.proto",
  29. },
  30. {
  31. source: []string{"a.proto", "b.proto"},
  32. expectedErr: errMultiInput,
  33. },
  34. {
  35. source: []string{"", "--go_out=."},
  36. expectedErr: errInvalidInput,
  37. },
  38. }
  39. for _, d := range testData {
  40. ret, err := getSourceProto(d.source, pwd)
  41. if d.expectedErr != nil {
  42. assert.Equal(t, d.expectedErr, err)
  43. continue
  44. }
  45. assert.Equal(t, d.expected, ret)
  46. }
  47. }
  48. func Test_RemoveGoctlFlag(t *testing.T) {
  49. testData := []test{
  50. {
  51. source: strings.Fields("protoc foo.proto --go_out=. --go_opt=bar --zrpc_out=. --style go-zero --home=foo"),
  52. expected: "protoc foo.proto --go_out=. --go_opt=bar",
  53. },
  54. {
  55. source: strings.Fields("foo bar foo.proto"),
  56. expected: "foo bar foo.proto",
  57. },
  58. {
  59. source: strings.Fields("protoc foo.proto --go_out . --style=go_zero --home ."),
  60. expected: "protoc foo.proto --go_out .",
  61. },
  62. {
  63. source: strings.Fields(`protoc foo.proto --go_out . --style="go_zero" --home="."`),
  64. expected: "protoc foo.proto --go_out .",
  65. },
  66. {
  67. source: strings.Fields(`protoc foo.proto --go_opt=. --zrpc_out . --style=goZero --home=bar`),
  68. expected: "protoc foo.proto --go_opt=.",
  69. },
  70. {
  71. source: strings.Fields(`protoc foo.proto --go_opt=. --zrpc_out="bar" --style=goZero --home=bar`),
  72. expected: "protoc foo.proto --go_opt=.",
  73. },
  74. {
  75. source: strings.Fields(`protoc --go_opt=. --go-grpc_out=. --zrpc_out=. foo.proto`),
  76. expected: "protoc --go_opt=. --go-grpc_out=. foo.proto",
  77. },
  78. {
  79. source: strings.Fields(`protoc --go_opt=. --go-grpc_out=. --zrpc_out=. --remote=foo --branch=bar foo.proto`),
  80. expected: "protoc --go_opt=. --go-grpc_out=. foo.proto",
  81. },
  82. {
  83. source: strings.Fields(`protoc --go_opt=. --go-grpc_out=. --zrpc_out=. --remote foo --branch bar foo.proto`),
  84. expected: "protoc --go_opt=. --go-grpc_out=. foo.proto",
  85. },
  86. }
  87. for _, e := range testData {
  88. cmd := strings.Join(removeGoctlFlag(e.source), " ")
  89. assert.Equal(t, e.expected, cmd)
  90. }
  91. }
  92. func Test_RemovePluginFlag(t *testing.T) {
  93. testData := []test{
  94. {
  95. source: strings.Fields("plugins=grpc:."),
  96. expected: ".",
  97. },
  98. {
  99. source: strings.Fields("plugins=g1,g2:."),
  100. expected: ".",
  101. },
  102. {
  103. source: strings.Fields("g1,g2:."),
  104. expected: ".",
  105. },
  106. {
  107. source: strings.Fields("plugins=g1,g2:foo"),
  108. expected: "foo",
  109. },
  110. }
  111. for _, e := range testData {
  112. data := removePluginFlag(e.source[0])
  113. assert.Equal(t, e.expected, data)
  114. }
  115. }