123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package ctx
- import (
- "bytes"
- "go/build"
- "io"
- "os"
- "path/filepath"
- "reflect"
- "strings"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/wuntsong-org/go-zero-plus/core/stringx"
- "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/rpc/execx"
- "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
- )
- func TestProjectFromGoMod(t *testing.T) {
- dft := build.Default
- gp := dft.GOPATH
- if len(gp) == 0 {
- return
- }
- projectName := stringx.Rand()
- dir := filepath.Join(gp, "src", projectName)
- err := pathx.MkdirIfNotExist(dir)
- if err != nil {
- return
- }
- _, err = execx.Run("go mod init "+projectName, dir)
- assert.Nil(t, err)
- defer func() {
- _ = os.RemoveAll(dir)
- }()
- ctx, err := projectFromGoMod(dir)
- assert.Nil(t, err)
- assert.Equal(t, projectName, ctx.Path)
- assert.Equal(t, dir, ctx.Dir)
- }
- func Test_getRealModule(t *testing.T) {
- type args struct {
- workDir string
- execRun execx.RunFunc
- }
- tests := []struct {
- name string
- args args
- want *Module
- wantErr bool
- }{
- {
- name: "single module",
- args: args{
- workDir: "/home/foo",
- execRun: func(arg, dir string, in ...*bytes.Buffer) (string, error) {
- return `{
- "Path":"foo",
- "Dir":"/home/foo",
- "GoMod":"/home/foo/go.mod",
- "GoVersion":"go1.16"}`, nil
- },
- },
- want: &Module{
- Path: "foo",
- Dir: "/home/foo",
- GoMod: "/home/foo/go.mod",
- GoVersion: "go1.16",
- },
- },
- {
- name: "go work multiple modules",
- args: args{
- workDir: "/home/bar",
- execRun: func(arg, dir string, in ...*bytes.Buffer) (string, error) {
- return `
- {
- "Path":"foo",
- "Dir":"/home/foo",
- "GoMod":"/home/foo/go.mod",
- "GoVersion":"go1.18"
- }
- {
- "Path":"bar",
- "Dir":"/home/bar",
- "GoMod":"/home/bar/go.mod",
- "GoVersion":"go1.18"
- }`, nil
- },
- },
- want: &Module{
- Path: "bar",
- Dir: "/home/bar",
- GoMod: "/home/bar/go.mod",
- GoVersion: "go1.18",
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := getRealModule(tt.args.workDir, tt.args.execRun)
- if (err != nil) != tt.wantErr {
- t.Errorf("getRealModule() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("getRealModule() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestDecodePackages(t *testing.T) {
- tests := []struct {
- name string
- data io.Reader
- want []Module
- wantErr bool
- }{
- {
- name: "single module",
- data: strings.NewReader(`{
- "Path":"foo",
- "Dir":"/home/foo",
- "GoMod":"/home/foo/go.mod",
- "GoVersion":"go1.16"}`),
- want: []Module{
- {
- Path: "foo",
- Dir: "/home/foo",
- GoMod: "/home/foo/go.mod",
- GoVersion: "go1.16",
- },
- },
- },
- {
- name: "go work multiple modules",
- data: strings.NewReader(`
- {
- "Path":"foo",
- "Dir":"/home/foo",
- "GoMod":"/home/foo/go.mod",
- "GoVersion":"go1.18"
- }
- {
- "Path":"bar",
- "Dir":"/home/bar",
- "GoMod":"/home/bar/go.mod",
- "GoVersion":"go1.18"
- }`),
- want: []Module{
- {
- Path: "foo",
- Dir: "/home/foo",
- GoMod: "/home/foo/go.mod",
- GoVersion: "go1.18",
- },
- {
- Path: "bar",
- Dir: "/home/bar",
- GoMod: "/home/bar/go.mod",
- GoVersion: "go1.18",
- },
- },
- },
- {
- name: "There are extra characters at the beginning",
- data: strings.NewReader(`Active code page: 65001
- {
- "Path":"foo",
- "Dir":"/home/foo",
- "GoMod":"/home/foo/go.mod",
- "GoVersion":"go1.18"
- }`),
- want: []Module{
- {
- Path: "foo",
- Dir: "/home/foo",
- GoMod: "/home/foo/go.mod",
- GoVersion: "go1.18",
- },
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result, err := decodePackages(tt.data)
- if err != nil {
- t.Errorf("decodePackages() error %v,wantErr = %v", err, tt.wantErr)
- }
- if !reflect.DeepEqual(result, tt.want) {
- t.Errorf("decodePackages() = %v,want %v", result, tt.want)
- }
- })
- }
- }
|