goconvey_1_8.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // +build !go1.9
  2. // To work correctly with out of GOPATH modules, some functions needed to
  3. // switch from using go/build to golang.org/x/tools/go/packages. But that
  4. // package depends on changes to go/types that were introduced in Go 1.9. Since
  5. // modules weren't introduced until Go 1.11, users of Go 1.8 or below can't be
  6. // using modules, so they can continue to use go/build.
  7. package main
  8. import (
  9. "go/build"
  10. "strings"
  11. )
  12. // This method exists because of a bug in the go cover tool that
  13. // causes an infinite loop when you try to run `go test -cover`
  14. // on a package that has an import cycle defined in one of it's
  15. // test files. Yuck.
  16. func testFilesImportTheirOwnPackage(packagePath string) bool {
  17. meta, err := build.ImportDir(packagePath, build.AllowBinary)
  18. if err != nil {
  19. return false
  20. }
  21. for _, dependency := range meta.TestImports {
  22. if dependency == meta.ImportPath {
  23. return true
  24. }
  25. }
  26. return false
  27. }
  28. func resolvePackageName(path string) string {
  29. pkg, err := build.ImportDir(path, build.FindOnly)
  30. if err == nil {
  31. return pkg.ImportPath
  32. }
  33. nameArr := strings.Split(path, endGoPath)
  34. return nameArr[len(nameArr)-1]
  35. }