forked from ruiaylin/pgparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
174 lines (157 loc) · 4.76 KB
/
build.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package buildutil
import (
"go/build"
"runtime"
"strings"
"testing"
"github.com/cockroachdb/errors"
)
func short(in string) string {
return strings.Replace(in, "github.com/ruiaylin/pgparser/pkg/", "./pkg/", -1)
}
// VerifyNoImports verifies that a package doesn't depend (directly or
// indirectly) on forbidden packages. The forbidden packages are specified as
// either exact matches or prefix matches.
// A match is not reported if the package that includes the forbidden package
// is listed in the allowlist.
// If GOPATH isn't set, it is an indication that the source is not available and
// the test is skipped.
func VerifyNoImports(
t testing.TB,
pkgPath string,
cgo bool,
forbiddenPkgs, forbiddenPrefixes []string,
allowlist ...string,
) {
// Skip test if source is not available.
if build.Default.GOPATH == "" {
t.Skip("GOPATH isn't set")
}
buildContext := build.Default
buildContext.CgoEnabled = cgo
checked := make(map[string]struct{})
var check func(string) error
check = func(path string) error {
pkg, err := buildContext.Import(path, "", build.FindOnly)
if err != nil {
t.Fatal(err)
}
for _, imp := range pkg.Imports {
for _, forbidden := range forbiddenPkgs {
if forbidden == imp {
allowlisted := false
for _, w := range allowlist {
if path == w {
allowlisted = true
break
}
}
if !allowlisted {
return errors.Errorf("%s imports %s, which is forbidden", short(path), short(imp))
}
}
if forbidden == "c-deps" &&
imp == "C" &&
strings.HasPrefix(path, "github.com/ruiaylin/pgparser/pkg") &&
path != "github.com/ruiaylin/pgparser/types/geo/geoproj" {
for _, name := range pkg.CgoFiles {
if strings.Contains(name, "zcgo_flags") {
return errors.Errorf("%s imports %s (%s), which is forbidden", short(path), short(imp), name)
}
}
}
}
for _, prefix := range forbiddenPrefixes {
if strings.HasPrefix(imp, prefix) {
return errors.Errorf("%s imports %s which has prefix %s, which is forbidden", short(path), short(imp), prefix)
}
}
// https://github.com/golang/tools/blob/master/refactor/importgraph/graph.go#L159
if imp == "C" {
continue // "C" is fake
}
importPkg, err := buildContext.Import(imp, pkg.Dir, build.FindOnly)
if err != nil {
// go/build does not know that gccgo's standard packages don't have
// source, and will report an error saying that it can not find them.
//
// See https://github.com/golang/go/issues/16701
// and https://github.com/golang/go/issues/23607.
if runtime.Compiler == "gccgo" {
continue
}
t.Fatal(err)
}
imp = importPkg.ImportPath
if _, ok := checked[imp]; ok {
continue
}
if err := check(imp); err != nil {
return errors.Wrapf(err, "%s depends on", short(path))
}
checked[pkg.ImportPath] = struct{}{}
}
return nil
}
if err := check(pkgPath); err != nil {
t.Fatal(err)
}
}
// VerifyTransitiveAllowlist checks that the entire set of transitive
// dependencies of the given package is in a allowlist. Vendored and stdlib
// packages are always allowed.
func VerifyTransitiveAllowlist(t testing.TB, pkg string, allowedPkgs []string) {
// Skip test if source is not available.
if build.Default.GOPATH == "" {
t.Skip("GOPATH isn't set")
}
checked := make(map[string]struct{})
allowed := make(map[string]struct{}, len(allowedPkgs))
for _, allowedPkg := range allowedPkgs {
allowed[allowedPkg] = struct{}{}
}
var check func(string)
check = func(path string) {
if _, ok := checked[path]; ok {
return
}
checked[path] = struct{}{}
if strings.HasPrefix(path, "github.com/ruiaylin/pgparser/vendor") {
return
}
pkg, err := build.Default.Import(path, "", 0)
if err != nil {
t.Fatal(err)
}
for _, imp := range pkg.Imports {
if !strings.HasPrefix(imp, "github.com/ruiaylin/pgparser/") {
continue
}
if _, ok := allowed[imp]; !ok {
t.Errorf("%s imports %s, which is forbidden", short(path), short(imp))
// If we can't have this package, don't bother recursively checking the
// deps, they'll just be noise.
continue
}
// https://github.com/golang/tools/blob/master/refactor/importgraph/graph.go#L159
if imp == "C" {
continue // "C" is fake
}
importPkg, err := build.Default.Import(imp, pkg.Dir, build.FindOnly)
if err != nil {
t.Fatal(err)
}
check(importPkg.ImportPath)
}
}
check(pkg)
}