goconvey_1_9.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, using
  6. // golang.org/x/tools/go/packages can safely be restricted to users of Go 1.9
  7. // or above.
  8. package main
  9. import (
  10. "fmt"
  11. "strings"
  12. "golang.org/x/tools/go/packages"
  13. )
  14. // This method exists because of a bug in the go cover tool that
  15. // causes an infinite loop when you try to run `go test -cover`
  16. // on a package that has an import cycle defined in one of it's
  17. // test files. Yuck.
  18. func testFilesImportTheirOwnPackage(packagePath string) bool {
  19. meta, err := packages.Load(
  20. &packages.Config{
  21. Mode: packages.NeedName | packages.NeedImports,
  22. Tests: true,
  23. },
  24. packagePath,
  25. )
  26. if err != nil {
  27. return false
  28. }
  29. testPackageID := fmt.Sprintf("%s [%s.test]", meta[0], meta[0])
  30. for _, testPackage := range meta[1:] {
  31. if testPackage.ID != testPackageID {
  32. continue
  33. }
  34. for dependency := range testPackage.Imports {
  35. if dependency == meta[0].PkgPath {
  36. return true
  37. }
  38. }
  39. break
  40. }
  41. return false
  42. }
  43. func resolvePackageName(path string) string {
  44. pkg, err := packages.Load(
  45. &packages.Config{
  46. Mode: packages.NeedName,
  47. },
  48. path,
  49. )
  50. if err == nil {
  51. return pkg[0].PkgPath
  52. }
  53. nameArr := strings.Split(path, endGoPath)
  54. return nameArr[len(nameArr)-1]
  55. }