gomod_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package ctx
  2. import (
  3. "bytes"
  4. "go/build"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/wuntsong-org/go-zero-plus/core/stringx"
  13. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/rpc/execx"
  14. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  15. )
  16. func TestProjectFromGoMod(t *testing.T) {
  17. dft := build.Default
  18. gp := dft.GOPATH
  19. if len(gp) == 0 {
  20. return
  21. }
  22. projectName := stringx.Rand()
  23. dir := filepath.Join(gp, "src", projectName)
  24. err := pathx.MkdirIfNotExist(dir)
  25. if err != nil {
  26. return
  27. }
  28. _, err = execx.Run("go mod init "+projectName, dir)
  29. assert.Nil(t, err)
  30. defer func() {
  31. _ = os.RemoveAll(dir)
  32. }()
  33. ctx, err := projectFromGoMod(dir)
  34. assert.Nil(t, err)
  35. assert.Equal(t, projectName, ctx.Path)
  36. assert.Equal(t, dir, ctx.Dir)
  37. }
  38. func Test_getRealModule(t *testing.T) {
  39. type args struct {
  40. workDir string
  41. execRun execx.RunFunc
  42. }
  43. tests := []struct {
  44. name string
  45. args args
  46. want *Module
  47. wantErr bool
  48. }{
  49. {
  50. name: "single module",
  51. args: args{
  52. workDir: "/home/foo",
  53. execRun: func(arg, dir string, in ...*bytes.Buffer) (string, error) {
  54. return `{
  55. "Path":"foo",
  56. "Dir":"/home/foo",
  57. "GoMod":"/home/foo/go.mod",
  58. "GoVersion":"go1.16"}`, nil
  59. },
  60. },
  61. want: &Module{
  62. Path: "foo",
  63. Dir: "/home/foo",
  64. GoMod: "/home/foo/go.mod",
  65. GoVersion: "go1.16",
  66. },
  67. },
  68. {
  69. name: "go work multiple modules",
  70. args: args{
  71. workDir: "/home/bar",
  72. execRun: func(arg, dir string, in ...*bytes.Buffer) (string, error) {
  73. return `
  74. {
  75. "Path":"foo",
  76. "Dir":"/home/foo",
  77. "GoMod":"/home/foo/go.mod",
  78. "GoVersion":"go1.18"
  79. }
  80. {
  81. "Path":"bar",
  82. "Dir":"/home/bar",
  83. "GoMod":"/home/bar/go.mod",
  84. "GoVersion":"go1.18"
  85. }`, nil
  86. },
  87. },
  88. want: &Module{
  89. Path: "bar",
  90. Dir: "/home/bar",
  91. GoMod: "/home/bar/go.mod",
  92. GoVersion: "go1.18",
  93. },
  94. },
  95. }
  96. for _, tt := range tests {
  97. t.Run(tt.name, func(t *testing.T) {
  98. got, err := getRealModule(tt.args.workDir, tt.args.execRun)
  99. if (err != nil) != tt.wantErr {
  100. t.Errorf("getRealModule() error = %v, wantErr %v", err, tt.wantErr)
  101. return
  102. }
  103. if !reflect.DeepEqual(got, tt.want) {
  104. t.Errorf("getRealModule() = %v, want %v", got, tt.want)
  105. }
  106. })
  107. }
  108. }
  109. func TestDecodePackages(t *testing.T) {
  110. tests := []struct {
  111. name string
  112. data io.Reader
  113. want []Module
  114. wantErr bool
  115. }{
  116. {
  117. name: "single module",
  118. data: strings.NewReader(`{
  119. "Path":"foo",
  120. "Dir":"/home/foo",
  121. "GoMod":"/home/foo/go.mod",
  122. "GoVersion":"go1.16"}`),
  123. want: []Module{
  124. {
  125. Path: "foo",
  126. Dir: "/home/foo",
  127. GoMod: "/home/foo/go.mod",
  128. GoVersion: "go1.16",
  129. },
  130. },
  131. },
  132. {
  133. name: "go work multiple modules",
  134. data: strings.NewReader(`
  135. {
  136. "Path":"foo",
  137. "Dir":"/home/foo",
  138. "GoMod":"/home/foo/go.mod",
  139. "GoVersion":"go1.18"
  140. }
  141. {
  142. "Path":"bar",
  143. "Dir":"/home/bar",
  144. "GoMod":"/home/bar/go.mod",
  145. "GoVersion":"go1.18"
  146. }`),
  147. want: []Module{
  148. {
  149. Path: "foo",
  150. Dir: "/home/foo",
  151. GoMod: "/home/foo/go.mod",
  152. GoVersion: "go1.18",
  153. },
  154. {
  155. Path: "bar",
  156. Dir: "/home/bar",
  157. GoMod: "/home/bar/go.mod",
  158. GoVersion: "go1.18",
  159. },
  160. },
  161. },
  162. {
  163. name: "There are extra characters at the beginning",
  164. data: strings.NewReader(`Active code page: 65001
  165. {
  166. "Path":"foo",
  167. "Dir":"/home/foo",
  168. "GoMod":"/home/foo/go.mod",
  169. "GoVersion":"go1.18"
  170. }`),
  171. want: []Module{
  172. {
  173. Path: "foo",
  174. Dir: "/home/foo",
  175. GoMod: "/home/foo/go.mod",
  176. GoVersion: "go1.18",
  177. },
  178. },
  179. },
  180. }
  181. for _, tt := range tests {
  182. t.Run(tt.name, func(t *testing.T) {
  183. result, err := decodePackages(tt.data)
  184. if err != nil {
  185. t.Errorf("decodePackages() error %v,wantErr = %v", err, tt.wantErr)
  186. }
  187. if !reflect.DeepEqual(result, tt.want) {
  188. t.Errorf("decodePackages() = %v,want %v", result, tt.want)
  189. }
  190. })
  191. }
  192. }