modcheck_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 TestIsGoMod(t *testing.T) {
  13. // create mod project
  14. dft := build.Default
  15. gp := dft.GOPATH
  16. if len(gp) == 0 {
  17. return
  18. }
  19. projectName := stringx.Rand()
  20. dir := filepath.Join(gp, "src", projectName)
  21. err := util.MkdirIfNotExist(dir)
  22. if err != nil {
  23. return
  24. }
  25. _, err = execx.Run("go mod init "+projectName, dir)
  26. assert.Nil(t, err)
  27. defer func() {
  28. _ = os.RemoveAll(dir)
  29. }()
  30. isGoMod, err := IsGoMod(dir)
  31. assert.Nil(t, err)
  32. assert.Equal(t, true, isGoMod)
  33. }
  34. func TestIsGoModNot(t *testing.T) {
  35. dft := build.Default
  36. gp := dft.GOPATH
  37. if len(gp) == 0 {
  38. return
  39. }
  40. projectName := stringx.Rand()
  41. dir := filepath.Join(gp, "src", projectName)
  42. err := util.MkdirIfNotExist(dir)
  43. if err != nil {
  44. return
  45. }
  46. defer func() {
  47. _ = os.RemoveAll(dir)
  48. }()
  49. isGoMod, err := IsGoMod(dir)
  50. assert.Nil(t, err)
  51. assert.Equal(t, false, isGoMod)
  52. }
  53. func TestIsGoModWorkDirIsNil(t *testing.T) {
  54. _, err := IsGoMod("")
  55. assert.Equal(t, err.Error(), func() string {
  56. return "the work directory is not found"
  57. }())
  58. }