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 multichecker_test
16 "golang.org/x/tools/go/analysis"
17 "golang.org/x/tools/go/analysis/multichecker"
18 "golang.org/x/tools/go/analysis/passes/findcall"
19 "golang.org/x/tools/internal/testenv"
23 fail := &analysis.Analyzer{
25 Doc: "always fail on a package 'sort'",
26 Run: func(pass *analysis.Pass) (interface{}, error) {
27 if pass.Pkg.Path() == "sort" {
28 return nil, fmt.Errorf("failed")
33 multichecker.Main(findcall.Analyzer, fail)
36 // TestExitCode ensures that analysis failures are reported correctly.
37 // This test fork/execs the main function above.
38 func TestExitCode(t *testing.T) {
39 if runtime.GOOS != "linux" {
40 t.Skipf("skipping fork/exec test on this platform")
43 if os.Getenv("MULTICHECKER_CHILD") == "1" {
46 // replace [progname -test.run=TestExitCode -- ...]
54 testenv.NeedsTool(t, "go")
56 for _, test := range []struct {
60 {[]string{"nosuchdir/..."}, 1}, // matched no packages
61 {[]string{"nosuchpkg"}, 1}, // matched no packages
62 {[]string{"-unknownflag"}, 2}, // flag error
63 {[]string{"-findcall.name=panic", "io"}, 3}, // finds diagnostics
64 {[]string{"-findcall=0", "io"}, 0}, // no checkers
65 {[]string{"-findcall.name=nosuchfunc", "io"}, 0}, // no diagnostics
66 {[]string{"-findcall.name=panic", "sort", "io"}, 1}, // 'fail' failed on 'sort'
68 // -json: exits zero even in face of diagnostics or package errors.
69 {[]string{"-findcall.name=panic", "-json", "io"}, 0},
70 {[]string{"-findcall.name=panic", "-json", "io"}, 0},
71 {[]string{"-findcall.name=panic", "-json", "sort", "io"}, 0},
73 args := []string{"-test.run=TestExitCode", "--"}
74 args = append(args, test.args...)
75 cmd := exec.Command(os.Args[0], args...)
76 cmd.Env = append(os.Environ(), "MULTICHECKER_CHILD=1")
77 out, err := cmd.CombinedOutput()
79 t.Logf("%s: out=<<%s>>", test.args, out)
82 if err, ok := err.(*exec.ExitError); ok {
83 exitcode = err.ExitCode() // requires go1.12
85 if exitcode != test.want {
86 t.Errorf("%s: exited %d, want %d", test.args, exitcode, test.want)