zrpc_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. for _, e := range testData {
  76. cmd := strings.Join(removeGoctlFlag(e.source), " ")
  77. assert.Equal(t, e.expected, cmd)
  78. }
  79. }
  80. func Test_RemovePluginFlag(t *testing.T) {
  81. testData := []test{
  82. {
  83. source: strings.Fields("plugins=grpc:."),
  84. expected: ".",
  85. },
  86. {
  87. source: strings.Fields("plugins=g1,g2:."),
  88. expected: ".",
  89. },
  90. {
  91. source: strings.Fields("g1,g2:."),
  92. expected: ".",
  93. },
  94. {
  95. source: strings.Fields("plugins=g1,g2:foo"),
  96. expected: "foo",
  97. },
  98. }
  99. for _, e := range testData {
  100. data := removePluginFlag(e.source[0])
  101. assert.Equal(t, e.expected, data)
  102. }
  103. }