Skip to content

Commit 94cf731

Browse files
committed
typecheck support setting tags, skipping test code, and ignoring directories
1 parent 81af5ba commit 94cf731

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

test/typecheck/main.go

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ import (
4141
)
4242

4343
var (
44-
verbose = flag.Bool("verbose", false, "print more information")
45-
cross = flag.Bool("cross", true, "build for all platforms")
46-
platforms = flag.String("platform", "", "comma-separated list of platforms to typecheck")
47-
timings = flag.Bool("time", false, "output times taken for each phase")
48-
defuses = flag.Bool("defuse", false, "output defs/uses")
49-
serial = flag.Bool("serial", false, "don't type check platforms in parallel")
44+
verbose = flag.Bool("verbose", false, "print more information")
45+
cross = flag.Bool("cross", true, "build for all platforms")
46+
platforms = flag.String("platform", "", "comma-separated list of platforms to typecheck")
47+
timings = flag.Bool("time", false, "output times taken for each phase")
48+
defuses = flag.Bool("defuse", false, "output defs/uses")
49+
serial = flag.Bool("serial", false, "don't type check platforms in parallel")
50+
skipTest = flag.Bool("skip-test", false, "don't type check test code")
51+
tags = flag.String("tags", "", "comma-separated list of build tags to apply in addition to go's defaults")
52+
ignoreDirs = flag.String("ignore-dirs", "", "comma-separated list of directories to ignore in addition to the default hardcoded list including staging, vendor, and hidden dirs")
5053

5154
isTerminal = terminal.IsTerminal(int(os.Stdout.Fd()))
5255
logPrefix = ""
@@ -62,6 +65,24 @@ var (
6265
}
6366
darwinPlatString = "darwin/386,darwin/amd64"
6467
windowsPlatString = "windows/386,windows/amd64"
68+
69+
// directories we always ignore
70+
standardIgnoreDirs = []string{
71+
// Staging code is symlinked from vendor/k8s.io, and uses import
72+
// paths as if it were inside of vendor/. It fails typechecking
73+
// inside of staging/, but works when typechecked as part of vendor/.
74+
"staging",
75+
// OS-specific vendor code tends to be imported by OS-specific
76+
// packages. We recursively typecheck imported vendored packages for
77+
// each OS, but don't typecheck everything for every OS.
78+
"vendor",
79+
"_output",
80+
// This is a weird one. /testdata/ is *mostly* ignored by Go,
81+
// and this translates to kubernetes/vendor not working.
82+
// edit/record.go doesn't compile without gopkg.in/yaml.v2
83+
// in $GOSRC/$GOROOT (both typecheck and the shell script).
84+
"pkg/kubectl/cmd/testdata/edit",
85+
}
6586
)
6687

6788
type analyzer struct {
@@ -79,6 +100,10 @@ func newAnalyzer(platform string) *analyzer {
79100
platSplit := strings.Split(platform, "/")
80101
ctx.GOOS, ctx.GOARCH = platSplit[0], platSplit[1]
81102
ctx.CgoEnabled = true
103+
if *tags != "" {
104+
tagsSplit := strings.Split(*tags, ",")
105+
ctx.BuildTags = append(ctx.BuildTags, tagsSplit...)
106+
}
82107

83108
a := &analyzer{
84109
platform: platform,
@@ -158,6 +183,9 @@ func (a *analyzer) filterFiles(fs map[string]*ast.File) []*ast.File {
158183
files := []*ast.File{}
159184
for _, f := range fs {
160185
fpath := a.fset.File(f.Pos()).Name()
186+
if *skipTest && strings.HasSuffix(fpath, "_test.go") {
187+
continue
188+
}
161189
dir, name := filepath.Split(fpath)
162190
matches, err := a.ctx.MatchFile(dir, name)
163191
if err != nil {
@@ -219,7 +247,8 @@ func (a *analyzer) typeCheck(dir string, files []*ast.File) error {
219247
}
220248

221249
type collector struct {
222-
dirs []string
250+
dirs []string
251+
ignoreDirs []string
223252
}
224253

225254
// handlePath walks the filesystem recursively, collecting directories,
@@ -231,23 +260,14 @@ func (c *collector) handlePath(path string, info os.FileInfo, err error) error {
231260
}
232261
if info.IsDir() {
233262
// Ignore hidden directories (.git, .cache, etc)
234-
if len(path) > 1 && path[0] == '.' ||
235-
// Staging code is symlinked from vendor/k8s.io, and uses import
236-
// paths as if it were inside of vendor/. It fails typechecking
237-
// inside of staging/, but works when typechecked as part of vendor/.
238-
path == "staging" ||
239-
// OS-specific vendor code tends to be imported by OS-specific
240-
// packages. We recursively typecheck imported vendored packages for
241-
// each OS, but don't typecheck everything for every OS.
242-
path == "vendor" ||
243-
path == "_output" ||
244-
// This is a weird one. /testdata/ is *mostly* ignored by Go,
245-
// and this translates to kubernetes/vendor not working.
246-
// edit/record.go doesn't compile without gopkg.in/yaml.v2
247-
// in $GOSRC/$GOROOT (both typecheck and the shell script).
248-
path == "pkg/kubectl/cmd/testdata/edit" {
263+
if len(path) > 1 && path[0] == '.' {
249264
return filepath.SkipDir
250265
}
266+
for _, dir := range c.ignoreDirs {
267+
if path == dir {
268+
return filepath.SkipDir
269+
}
270+
}
251271
c.dirs = append(c.dirs, path)
252272
}
253273
return nil
@@ -310,7 +330,13 @@ func main() {
310330
args = append(args, ".")
311331
}
312332

313-
c := collector{}
333+
c := collector{
334+
ignoreDirs: append([]string(nil), standardIgnoreDirs...),
335+
}
336+
if *ignoreDirs != "" {
337+
c.ignoreDirs = append(c.ignoreDirs, strings.Split(*ignoreDirs, ",")...)
338+
}
339+
314340
for _, arg := range args {
315341
err := filepath.Walk(arg, c.handlePath)
316342
if err != nil {

test/typecheck/main_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ func TestHandlePackage(t *testing.T) {
146146
}
147147

148148
func TestHandlePath(t *testing.T) {
149-
c := collector{}
149+
c := collector{
150+
ignoreDirs: standardIgnoreDirs,
151+
}
150152
e := errors.New("ex")
151153
i, _ := os.Stat(".") // i.IsDir() == true
152154
if c.handlePath("foo", nil, e) != e {

0 commit comments

Comments
 (0)