feat(goctl): go work multi-module support (#1800)

* feat(goctl): go work multi-module support

Resolve: #1793

* chore: print log when getting project ctx fails
This commit is contained in:
Fyn
2022-04-18 20:36:41 +08:00
committed by GitHub
parent 92b450eb11
commit e62870e268
5 changed files with 118 additions and 17 deletions

View File

@@ -1,9 +1,11 @@
package ctx
import (
"bytes"
"go/build"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
@@ -36,3 +38,75 @@ func TestProjectFromGoMod(t *testing.T) {
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)
}
})
}
}