You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/util/ctx/gopath_test.go

55 lines
1015 B
Go

package ctx
import (
"go/build"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/stringx"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func TestProjectFromGoPath(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
}
defer func() {
_ = os.RemoveAll(dir)
}()
ctx, err := projectFromGoPath(dir)
assert.Nil(t, err)
assert.Equal(t, dir, ctx.Dir)
assert.Equal(t, projectName, ctx.Path)
}
func TestProjectFromGoPathNotInGoSrc(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
}
defer func() {
_ = os.RemoveAll(dir)
}()
_, err = projectFromGoPath("testPath")
assert.NotNil(t, err)
}