1 | // Copyright 2022 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 | package checker_test |
6 | |
7 | import ( |
8 | "flag" |
9 | "io/ioutil" |
10 | "os" |
11 | "os/exec" |
12 | "path" |
13 | "runtime" |
14 | "testing" |
15 | |
16 | "golang.org/x/tools/go/analysis" |
17 | "golang.org/x/tools/go/analysis/analysistest" |
18 | "golang.org/x/tools/go/analysis/internal/checker" |
19 | "golang.org/x/tools/internal/testenv" |
20 | ) |
21 | |
22 | func main() { |
23 | checker.Fix = true |
24 | patterns := flag.Args() |
25 | |
26 | code := checker.Run(patterns, []*analysis.Analyzer{analyzer}) |
27 | os.Exit(code) |
28 | } |
29 | |
30 | // TestFixes ensures that checker.Run applies fixes correctly. |
31 | // This test fork/execs the main function above. |
32 | func TestFixes(t *testing.T) { |
33 | oses := map[string]bool{"darwin": true, "linux": true} |
34 | if !oses[runtime.GOOS] { |
35 | t.Skipf("skipping fork/exec test on this platform") |
36 | } |
37 | |
38 | if os.Getenv("TESTFIXES_CHILD") == "1" { |
39 | // child process |
40 | |
41 | // replace [progname -test.run=TestFixes -- ...] |
42 | // by [progname ...] |
43 | os.Args = os.Args[2:] |
44 | os.Args[0] = "vet" |
45 | main() |
46 | panic("unreachable") |
47 | } |
48 | |
49 | testenv.NeedsTool(t, "go") |
50 | |
51 | files := map[string]string{ |
52 | "rename/foo.go": `package rename |
53 | |
54 | func Foo() { |
55 | bar := 12 |
56 | _ = bar |
57 | } |
58 | |
59 | // the end |
60 | `, |
61 | "rename/intestfile_test.go": `package rename |
62 | |
63 | func InTestFile() { |
64 | bar := 13 |
65 | _ = bar |
66 | } |
67 | |
68 | // the end |
69 | `, |
70 | "rename/foo_test.go": `package rename_test |
71 | |
72 | func Foo() { |
73 | bar := 14 |
74 | _ = bar |
75 | } |
76 | |
77 | // the end |
78 | `, |
79 | } |
80 | fixed := map[string]string{ |
81 | "rename/foo.go": `package rename |
82 | |
83 | func Foo() { |
84 | baz := 12 |
85 | _ = baz |
86 | } |
87 | |
88 | // the end |
89 | `, |
90 | "rename/intestfile_test.go": `package rename |
91 | |
92 | func InTestFile() { |
93 | baz := 13 |
94 | _ = baz |
95 | } |
96 | |
97 | // the end |
98 | `, |
99 | "rename/foo_test.go": `package rename_test |
100 | |
101 | func Foo() { |
102 | baz := 14 |
103 | _ = baz |
104 | } |
105 | |
106 | // the end |
107 | `, |
108 | } |
109 | dir, cleanup, err := analysistest.WriteFiles(files) |
110 | if err != nil { |
111 | t.Fatalf("Creating test files failed with %s", err) |
112 | } |
113 | defer cleanup() |
114 | |
115 | args := []string{"-test.run=TestFixes", "--", "rename"} |
116 | cmd := exec.Command(os.Args[0], args...) |
117 | cmd.Env = append(os.Environ(), "TESTFIXES_CHILD=1", "GOPATH="+dir, "GO111MODULE=off", "GOPROXY=off") |
118 | |
119 | out, err := cmd.CombinedOutput() |
120 | if len(out) > 0 { |
121 | t.Logf("%s: out=<<%s>>", args, out) |
122 | } |
123 | var exitcode int |
124 | if err, ok := err.(*exec.ExitError); ok { |
125 | exitcode = err.ExitCode() // requires go1.12 |
126 | } |
127 | |
128 | const diagnosticsExitCode = 3 |
129 | if exitcode != diagnosticsExitCode { |
130 | t.Errorf("%s: exited %d, want %d", args, exitcode, diagnosticsExitCode) |
131 | } |
132 | |
133 | for name, want := range fixed { |
134 | path := path.Join(dir, "src", name) |
135 | contents, err := ioutil.ReadFile(path) |
136 | if err != nil { |
137 | t.Errorf("error reading %s: %v", path, err) |
138 | } |
139 | if got := string(contents); got != want { |
140 | t.Errorf("contents of %s file did not match expectations. got=%s, want=%s", path, got, want) |
141 | } |
142 | } |
143 | } |
144 |
Members