gopath_test.go 1001 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/util"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestProjectFromGoPath(t *testing.T) {
  12. dft := build.Default
  13. gp := dft.GOPATH
  14. if len(gp) == 0 {
  15. return
  16. }
  17. projectName := stringx.Rand()
  18. dir := filepath.Join(gp, "src", projectName)
  19. err := util.MkdirIfNotExist(dir)
  20. if err != nil {
  21. return
  22. }
  23. defer func() {
  24. _ = os.RemoveAll(dir)
  25. }()
  26. ctx, err := projectFromGoPath(dir)
  27. assert.Nil(t, err)
  28. assert.Equal(t, dir, ctx.Dir)
  29. assert.Equal(t, projectName, ctx.Path)
  30. }
  31. func TestProjectFromGoPathNotInGoSrc(t *testing.T) {
  32. dft := build.Default
  33. gp := dft.GOPATH
  34. if len(gp) == 0 {
  35. return
  36. }
  37. projectName := stringx.Rand()
  38. dir := filepath.Join(gp, "src", projectName)
  39. err := util.MkdirIfNotExist(dir)
  40. if err != nil {
  41. return
  42. }
  43. defer func() {
  44. _ = os.RemoveAll(dir)
  45. }()
  46. _, err = projectFromGoPath("testPath")
  47. assert.NotNil(t, err)
  48. }