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.
8 package unitchecker_test
10 // This test depends on features such as
11 // go vet's support for vetx files (1.11) and
12 // the (*os.ProcessState).ExitCode method (1.12).
23 "golang.org/x/tools/go/analysis/passes/findcall"
24 "golang.org/x/tools/go/analysis/passes/printf"
25 "golang.org/x/tools/go/analysis/unitchecker"
26 "golang.org/x/tools/go/packages/packagestest"
29 func TestMain(m *testing.M) {
30 if os.Getenv("UNITCHECKER_CHILD") == "1" {
47 // This is a very basic integration test of modular
48 // analysis with facts using unitchecker under "go vet".
49 // It fork/execs the main function above.
50 func TestIntegration(t *testing.T) { packagestest.TestAll(t, testIntegration) }
51 func testIntegration(t *testing.T, exporter packagestest.Exporter) {
52 if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
53 t.Skipf("skipping fork/exec test on this platform")
56 exported := packagestest.Export(t, exporter, []packagestest.Module{{
57 Name: "golang.org/fake",
58 Files: map[string]interface{}{
69 import "golang.org/fake/a"
79 defer exported.Cleanup()
81 const wantA = `# golang.org/fake/a
82 ([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11: call of MyFunc123\(...\)
84 const wantB = `# golang.org/fake/b
85 ([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?b/b.go:6:13: call of MyFunc123\(...\)
86 ([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?b/b.go:7:11: call of MyFunc123\(...\)
88 const wantAJSON = `# golang.org/fake/a
90 "golang.org/fake/a": \{
93 "posn": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11",
94 "message": "call of MyFunc123\(...\)"
101 for _, test := range []struct {
106 {args: "golang.org/fake/a", wantOut: wantA, wantExit: 2},
107 {args: "golang.org/fake/b", wantOut: wantB, wantExit: 2},
108 {args: "golang.org/fake/a golang.org/fake/b", wantOut: wantA + wantB, wantExit: 2},
109 {args: "-json golang.org/fake/a", wantOut: wantAJSON, wantExit: 0},
110 {args: "-c=0 golang.org/fake/a", wantOut: wantA + "4 MyFunc123\\(\\)\n", wantExit: 2},
112 cmd := exec.Command("go", "vet", "-vettool="+os.Args[0], "-findcall.name=MyFunc123")
113 cmd.Args = append(cmd.Args, strings.Fields(test.args)...)
114 cmd.Env = append(exported.Config.Env, "UNITCHECKER_CHILD=1")
115 cmd.Dir = exported.Config.Dir
117 out, err := cmd.CombinedOutput()
119 if exitErr, ok := err.(*exec.ExitError); ok {
120 exitcode = exitErr.ExitCode()
122 if exitcode != test.wantExit {
123 t.Errorf("%s: got exit code %d, want %d", test.args, exitcode, test.wantExit)
126 matched, err := regexp.Match(test.wantOut, out)
131 t.Errorf("%s: got <<%s>>, want match of regexp <<%s>>", test.args, out, test.wantOut)