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 / multichecker / multichecker_test.go
1 // +build go1.12
2
3 package multichecker_test
4
5 import (
6         "fmt"
7         "os"
8         "os/exec"
9         "runtime"
10         "testing"
11
12         "golang.org/x/tools/go/analysis"
13         "golang.org/x/tools/go/analysis/multichecker"
14         "golang.org/x/tools/go/analysis/passes/findcall"
15         "golang.org/x/tools/internal/testenv"
16 )
17
18 func main() {
19         fail := &analysis.Analyzer{
20                 Name: "fail",
21                 Doc:  "always fail on a package 'sort'",
22                 Run: func(pass *analysis.Pass) (interface{}, error) {
23                         if pass.Pkg.Path() == "sort" {
24                                 return nil, fmt.Errorf("failed")
25                         }
26                         return nil, nil
27                 },
28         }
29         multichecker.Main(findcall.Analyzer, fail)
30 }
31
32 // TestExitCode ensures that analysis failures are reported correctly.
33 // This test fork/execs the main function above.
34 func TestExitCode(t *testing.T) {
35         if runtime.GOOS != "linux" {
36                 t.Skipf("skipping fork/exec test on this platform")
37         }
38
39         if os.Getenv("MULTICHECKER_CHILD") == "1" {
40                 // child process
41
42                 // replace [progname -test.run=TestExitCode -- ...]
43                 //      by [progname ...]
44                 os.Args = os.Args[2:]
45                 os.Args[0] = "vet"
46                 main()
47                 panic("unreachable")
48         }
49
50         testenv.NeedsTool(t, "go")
51
52         for _, test := range []struct {
53                 args []string
54                 want int
55         }{
56                 {[]string{"nosuchdir/..."}, 1},                      // matched no packages
57                 {[]string{"nosuchpkg"}, 1},                          // matched no packages
58                 {[]string{"-unknownflag"}, 2},                       // flag error
59                 {[]string{"-findcall.name=panic", "io"}, 3},         // finds diagnostics
60                 {[]string{"-findcall=0", "io"}, 0},                  // no checkers
61                 {[]string{"-findcall.name=nosuchfunc", "io"}, 0},    // no diagnostics
62                 {[]string{"-findcall.name=panic", "sort", "io"}, 1}, // 'fail' failed on 'sort'
63
64                 // -json: exits zero even in face of diagnostics or package errors.
65                 {[]string{"-findcall.name=panic", "-json", "io"}, 0},
66                 {[]string{"-findcall.name=panic", "-json", "io"}, 0},
67                 {[]string{"-findcall.name=panic", "-json", "sort", "io"}, 0},
68         } {
69                 args := []string{"-test.run=TestExitCode", "--"}
70                 args = append(args, test.args...)
71                 cmd := exec.Command(os.Args[0], args...)
72                 cmd.Env = append(os.Environ(), "MULTICHECKER_CHILD=1")
73                 out, err := cmd.CombinedOutput()
74                 if len(out) > 0 {
75                         t.Logf("%s: out=<<%s>>", test.args, out)
76                 }
77                 var exitcode int
78                 if err, ok := err.(*exec.ExitError); ok {
79                         exitcode = err.ExitCode() // requires go1.12
80                 }
81                 if exitcode != test.want {
82                         t.Errorf("%s: exited %d, want %d", test.args, exitcode, test.want)
83                 }
84         }
85 }