1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 package unitchecker_test
9 // This test depends on features such as
10 // go vet's support for vetx files (1.11) and
11 // the (*os.ProcessState).ExitCode method (1.12).
22 "golang.org/x/tools/go/analysis/passes/findcall"
23 "golang.org/x/tools/go/analysis/passes/printf"
24 "golang.org/x/tools/go/analysis/unitchecker"
25 "golang.org/x/tools/go/packages/packagestest"
28 func TestMain(m *testing.M) {
29 if os.Getenv("UNITCHECKER_CHILD") == "1" {
46 // This is a very basic integration test of modular
47 // analysis with facts using unitchecker under "go vet".
48 // It fork/execs the main function above.
49 func TestIntegration(t *testing.T) { packagestest.TestAll(t, testIntegration) }
50 func testIntegration(t *testing.T, exporter packagestest.Exporter) {
51 if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
52 t.Skipf("skipping fork/exec test on this platform")
55 exported := packagestest.Export(t, exporter, []packagestest.Module{{
56 Name: "golang.org/fake",
57 Files: map[string]interface{}{
68 import "golang.org/fake/a"
78 defer exported.Cleanup()
80 const wantA = `# golang.org/fake/a
81 ([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11: call of MyFunc123\(...\)
83 const wantB = `# golang.org/fake/b
84 ([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?b/b.go:6:13: call of MyFunc123\(...\)
85 ([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?b/b.go:7:11: call of MyFunc123\(...\)
87 const wantAJSON = `# golang.org/fake/a
89 "golang.org/fake/a": \{
92 "posn": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11",
93 "message": "call of MyFunc123\(...\)"
100 for _, test := range []struct {
105 {args: "golang.org/fake/a", wantOut: wantA, wantExit: 2},
106 {args: "golang.org/fake/b", wantOut: wantB, wantExit: 2},
107 {args: "golang.org/fake/a golang.org/fake/b", wantOut: wantA + wantB, wantExit: 2},
108 {args: "-json golang.org/fake/a", wantOut: wantAJSON, wantExit: 0},
109 {args: "-c=0 golang.org/fake/a", wantOut: wantA + "4 MyFunc123\\(\\)\n", wantExit: 2},
111 cmd := exec.Command("go", "vet", "-vettool="+os.Args[0], "-findcall.name=MyFunc123")
112 cmd.Args = append(cmd.Args, strings.Fields(test.args)...)
113 cmd.Env = append(exported.Config.Env, "UNITCHECKER_CHILD=1")
114 cmd.Dir = exported.Config.Dir
116 out, err := cmd.CombinedOutput()
118 if exitErr, ok := err.(*exec.ExitError); ok {
119 exitcode = exitErr.ExitCode()
121 if exitcode != test.wantExit {
122 t.Errorf("%s: got exit code %d, want %d", test.args, exitcode, test.wantExit)
125 matched, err := regexp.Match(test.wantOut, out)
130 t.Errorf("%s: got <<%s>>, want match of regexp <<%s>>", test.args, out, test.wantOut)