gomod_test.go 747 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package ctx
  2. import (
  3. "go/build"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "git.i2edu.net/i2/go-zero/core/stringx"
  8. "git.i2edu.net/i2/go-zero/tools/goctl/rpc/execx"
  9. "git.i2edu.net/i2/go-zero/tools/goctl/util"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestProjectFromGoMod(t *testing.T) {
  13. dft := build.Default
  14. gp := dft.GOPATH
  15. if len(gp) == 0 {
  16. return
  17. }
  18. projectName := stringx.Rand()
  19. dir := filepath.Join(gp, "src", projectName)
  20. err := util.MkdirIfNotExist(dir)
  21. if err != nil {
  22. return
  23. }
  24. _, err = execx.Run("go mod init "+projectName, dir)
  25. assert.Nil(t, err)
  26. defer func() {
  27. _ = os.RemoveAll(dir)
  28. }()
  29. ctx, err := projectFromGoMod(dir)
  30. assert.Nil(t, err)
  31. assert.Equal(t, projectName, ctx.Path)
  32. assert.Equal(t, dir, ctx.Dir)
  33. }