forked from smartystreets/goconvey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goconvey_1_9.go
64 lines (55 loc) · 1.44 KB
/
goconvey_1_9.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// +build go1.9
// To work correctly with out of GOPATH modules, some functions needed to
// switch from using go/build to golang.org/x/tools/go/packages. But that
// package depends on changes to go/types that were introduced in Go 1.9. Since
// modules weren't introduced until Go 1.11, using
// golang.org/x/tools/go/packages can safely be restricted to users of Go 1.9
// or above.
package main
import (
"fmt"
"strings"
"golang.org/x/tools/go/packages"
)
// This method exists because of a bug in the go cover tool that
// causes an infinite loop when you try to run `go test -cover`
// on a package that has an import cycle defined in one of it's
// test files. Yuck.
func testFilesImportTheirOwnPackage(packagePath string) bool {
meta, err := packages.Load(
&packages.Config{
Mode: packages.NeedName | packages.NeedImports,
Tests: true,
},
packagePath,
)
if err != nil {
return false
}
testPackageID := fmt.Sprintf("%s [%s.test]", meta[0], meta[0])
for _, testPackage := range meta[1:] {
if testPackage.ID != testPackageID {
continue
}
for dependency := range testPackage.Imports {
if dependency == meta[0].PkgPath {
return true
}
}
break
}
return false
}
func resolvePackageName(path string) string {
pkg, err := packages.Load(
&packages.Config{
Mode: packages.NeedName,
},
path,
)
if err == nil {
return pkg[0].PkgPath
}
nameArr := strings.Split(path, endGoPath)
return nameArr[len(nameArr)-1]
}