3 package multichecker_test
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"
19 fail := &analysis.Analyzer{
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")
29 multichecker.Main(findcall.Analyzer, fail)
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")
39 if os.Getenv("MULTICHECKER_CHILD") == "1" {
42 // replace [progname -test.run=TestExitCode -- ...]
50 testenv.NeedsTool(t, "go")
52 for _, test := range []struct {
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'
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},
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()
75 t.Logf("%s: out=<<%s>>", test.args, out)
78 if err, ok := err.(*exec.ExitError); ok {
79 exitcode = err.ExitCode() // requires go1.12
81 if exitcode != test.want {
82 t.Errorf("%s: exited %d, want %d", test.args, exitcode, test.want)