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