Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / go / analysis / unitchecker / unitchecker_test.go
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.
4
5 // +build go1.12
6
7 package unitchecker_test
8
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).
12
13 import (
14         "flag"
15         "os"
16         "os/exec"
17         "regexp"
18         "runtime"
19         "strings"
20         "testing"
21
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"
26 )
27
28 func TestMain(m *testing.M) {
29         if os.Getenv("UNITCHECKER_CHILD") == "1" {
30                 // child process
31                 main()
32                 panic("unreachable")
33         }
34
35         flag.Parse()
36         os.Exit(m.Run())
37 }
38
39 func main() {
40         unitchecker.Main(
41                 findcall.Analyzer,
42                 printf.Analyzer,
43         )
44 }
45
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")
53         }
54
55         exported := packagestest.Export(t, exporter, []packagestest.Module{{
56                 Name: "golang.org/fake",
57                 Files: map[string]interface{}{
58                         "a/a.go": `package a
59
60 func _() {
61         MyFunc123()
62 }
63
64 func MyFunc123() {}
65 `,
66                         "b/b.go": `package b
67
68 import "golang.org/fake/a"
69
70 func _() {
71         a.MyFunc123()
72         MyFunc123()
73 }
74
75 func MyFunc123() {}
76 `,
77                 }}})
78         defer exported.Cleanup()
79
80         const wantA = `# golang.org/fake/a
81 ([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11: call of MyFunc123\(...\)
82 `
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\(...\)
86 `
87         const wantAJSON = `# golang.org/fake/a
88 \{
89         "golang.org/fake/a": \{
90                 "findcall": \[
91                         \{
92                                 "posn": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11",
93                                 "message": "call of MyFunc123\(...\)"
94                         \}
95                 \]
96         \}
97 \}
98 `
99
100         for _, test := range []struct {
101                 args     string
102                 wantOut  string
103                 wantExit int
104         }{
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},
110         } {
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
115
116                 out, err := cmd.CombinedOutput()
117                 exitcode := 0
118                 if exitErr, ok := err.(*exec.ExitError); ok {
119                         exitcode = exitErr.ExitCode()
120                 }
121                 if exitcode != test.wantExit {
122                         t.Errorf("%s: got exit code %d, want %d", test.args, exitcode, test.wantExit)
123                 }
124
125                 matched, err := regexp.Match(test.wantOut, out)
126                 if err != nil {
127                         t.Fatal(err)
128                 }
129                 if !matched {
130                         t.Errorf("%s: got <<%s>>, want match of regexp <<%s>>", test.args, out, test.wantOut)
131                 }
132         }
133 }