| 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. |
| 4 | |
| 5 | package unitchecker_test |
| 6 | |
| 7 | import ( |
| 8 | "encoding/json" |
| 9 | "flag" |
| 10 | "fmt" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "path/filepath" |
| 14 | "runtime" |
| 15 | "strings" |
| 16 | "testing" |
| 17 | |
| 18 | "golang.org/x/tools/go/analysis/passes/assign" |
| 19 | "golang.org/x/tools/go/analysis/passes/findcall" |
| 20 | "golang.org/x/tools/go/analysis/passes/printf" |
| 21 | "golang.org/x/tools/go/analysis/unitchecker" |
| 22 | "golang.org/x/tools/internal/testenv" |
| 23 | "golang.org/x/tools/internal/testfiles" |
| 24 | "golang.org/x/tools/txtar" |
| 25 | ) |
| 26 | |
| 27 | func TestMain(m *testing.M) { |
| 28 | // child process? |
| 29 | switch os.Getenv("ENTRYPOINT") { |
| 30 | case "vet": |
| 31 | vet() |
| 32 | panic("unreachable") |
| 33 | case "minivet": |
| 34 | minivet() |
| 35 | panic("unreachable") |
| 36 | case "worker": |
| 37 | worker() // see ExampleSeparateAnalysis |
| 38 | panic("unreachable") |
| 39 | } |
| 40 | |
| 41 | // test process |
| 42 | flag.Parse() |
| 43 | os.Exit(m.Run()) |
| 44 | } |
| 45 | |
| 46 | // minivet is a vet-like tool with a few analyzers, for testing. |
| 47 | func minivet() { |
| 48 | unitchecker.Main( |
| 49 | findcall.Analyzer, |
| 50 | printf.Analyzer, |
| 51 | assign.Analyzer, |
| 52 | ) |
| 53 | } |
| 54 | |
| 55 | // This is a very basic integration test of modular |
| 56 | // analysis with facts using unitchecker under "go vet". |
| 57 | // It fork/execs the main function above. |
| 58 | func TestIntegration(t *testing.T) { |
| 59 | if runtime.GOOS != "linux" && runtime.GOOS != "darwin" { |
| 60 | t.Skipf("skipping fork/exec test on this platform") |
| 61 | } |
| 62 | |
| 63 | const src = ` |
| 64 | -- go.mod -- |
| 65 | module golang.org/fake |
| 66 | go 1.24 |
| 67 | |
| 68 | -- a/a.go -- |
| 69 | package a |
| 70 | |
| 71 | func _() { |
| 72 | MyFunc123() |
| 73 | } |
| 74 | |
| 75 | func MyFunc123() {} |
| 76 | |
| 77 | -- b/b.go -- |
| 78 | package b |
| 79 | |
| 80 | import "golang.org/fake/a" |
| 81 | |
| 82 | func _() { |
| 83 | a.MyFunc123() |
| 84 | MyFunc123() |
| 85 | } |
| 86 | |
| 87 | func MyFunc123() {} |
| 88 | |
| 89 | -- c/c.go -- |
| 90 | package c |
| 91 | |
| 92 | func _() { |
| 93 | i := 5 |
| 94 | i = i |
| 95 | } |
| 96 | |
| 97 | -- d/d.go -- |
| 98 | package d |
| 99 | |
| 100 | import "fmt" |
| 101 | |
| 102 | var ( |
| 103 | msg string |
| 104 | _ = fmt.Sprintf(msg) |
| 105 | ) |
| 106 | |
| 107 | -- d/dgen.go -- |
| 108 | // Code generated by hand. DO NOT EDIT. |
| 109 | |
| 110 | package d |
| 111 | |
| 112 | import "fmt" |
| 113 | |
| 114 | var _ = fmt.Sprintf(msg) |
| 115 | ` |
| 116 | // Expand archive into tmp tree. |
| 117 | fs, err := txtar.FS(txtar.Parse([]byte(src))) |
| 118 | if err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | tmpdir := testfiles.CopyToTmp(t, fs) |
| 122 | |
| 123 | // -- operators -- |
| 124 | |
| 125 | // vet runs "go vet" with the specified arguments (plus -findcall.name=MyFunc123). |
| 126 | vet := func(t *testing.T, args ...string) (exitcode int, stdout, stderr string) { |
| 127 | cmd := exec.Command("go", "vet", "-vettool="+os.Args[0], "-findcall.name=MyFunc123") |
| 128 | cmd.Stdout = new(strings.Builder) |
| 129 | cmd.Stderr = new(strings.Builder) |
| 130 | cmd.Args = append(cmd.Args, args...) |
| 131 | cmd.Env = append(os.Environ(), "ENTRYPOINT=minivet") |
| 132 | cmd.Dir = tmpdir |
| 133 | if err := cmd.Run(); err != nil { |
| 134 | exitErr, ok := err.(*exec.ExitError) |
| 135 | if !ok { |
| 136 | t.Fatalf("couldn't exec %v: %v", cmd, err) |
| 137 | } |
| 138 | exitcode = exitErr.ExitCode() |
| 139 | } |
| 140 | |
| 141 | // Sanitize filenames; this is imperfect due to |
| 142 | // (e.g.) /private/tmp -> /tmp symlink on macOS. |
| 143 | stdout = strings.ReplaceAll(fmt.Sprint(cmd.Stdout), tmpdir, "TMPDIR") |
| 144 | stderr = strings.ReplaceAll(fmt.Sprint(cmd.Stderr), tmpdir, "TMPDIR") |
| 145 | |
| 146 | // Show vet information on failure. |
| 147 | t.Cleanup(func() { |
| 148 | if t.Failed() { |
| 149 | t.Logf("command: %v", cmd) |
| 150 | t.Logf("exit code: %d", exitcode) |
| 151 | t.Logf("stdout: %s", stdout) |
| 152 | t.Logf("stderr: %s", stderr) |
| 153 | } |
| 154 | }) |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | // exitcode asserts that the exit code was "want". |
| 159 | exitcode := func(t *testing.T, got, want int) { |
| 160 | if got != want { |
| 161 | t.Fatalf("vet tool exit code was %d", got) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // parseJSON parses the JSON diagnostics into a simple line-oriented form. |
| 166 | parseJSON := func(t *testing.T, stdout string) string { |
| 167 | var v map[string]map[string][]map[string]any |
| 168 | if err := json.Unmarshal([]byte(stdout), &v); err != nil { |
| 169 | t.Fatalf("invalid JSON: %v", err) |
| 170 | } |
| 171 | var res strings.Builder |
| 172 | for pkgpath, v := range v { |
| 173 | for analyzer, v := range v { |
| 174 | for _, v := range v { |
| 175 | fmt.Fprintf(&res, "%s: [%s@%s] %v\n", |
| 176 | v["posn"], |
| 177 | analyzer, pkgpath, |
| 178 | v["message"]) |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | // Show parsed JSON information on failure. |
| 183 | t.Cleanup(func() { |
| 184 | if t.Failed() { |
| 185 | t.Logf("json: %s", &res) |
| 186 | } |
| 187 | }) |
| 188 | return res.String() |
| 189 | } |
| 190 | |
| 191 | // substring asserts that the labeled output contained the substring. |
| 192 | substring := func(t *testing.T, label, output, substr string) { |
| 193 | if !strings.Contains(output, substr) { |
| 194 | t.Fatalf("%s: expected substring %q", label, substr) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // contains asserts that the specified file contains the substring. |
| 199 | contains := func(t *testing.T, filename, substr string) { |
| 200 | content, err := os.ReadFile(filepath.Join(tmpdir, filename)) |
| 201 | if err != nil { |
| 202 | t.Fatalf("can't read %s: %v", filename, err) |
| 203 | } |
| 204 | t.Cleanup(func() { |
| 205 | if t.Failed() { |
| 206 | t.Logf("content of %s: <<%s>>", filename, content) |
| 207 | } |
| 208 | }) |
| 209 | substring(t, filename, string(content), substr) |
| 210 | } |
| 211 | |
| 212 | // -- scenarios -- |
| 213 | |
| 214 | t.Run("a", func(t *testing.T) { |
| 215 | code, _, stderr := vet(t, "golang.org/fake/a") |
| 216 | exitcode(t, code, 1) |
| 217 | substring(t, "stderr", stderr, "a/a.go:4:11: call of MyFunc123") |
| 218 | }) |
| 219 | t.Run("b", func(t *testing.T) { |
| 220 | code, _, stderr := vet(t, "golang.org/fake/b") |
| 221 | exitcode(t, code, 1) |
| 222 | substring(t, "stderr", stderr, "b/b.go:6:13: call of MyFunc123") |
| 223 | substring(t, "stderr", stderr, "b/b.go:7:11: call of MyFunc123") |
| 224 | }) |
| 225 | t.Run("c", func(t *testing.T) { |
| 226 | code, _, stderr := vet(t, "golang.org/fake/c") |
| 227 | exitcode(t, code, 1) |
| 228 | substring(t, "stderr", stderr, "c/c.go:5:5: self-assignment of i") |
| 229 | }) |
| 230 | t.Run("ab", func(t *testing.T) { |
| 231 | code, _, stderr := vet(t, "golang.org/fake/a", "golang.org/fake/b") |
| 232 | exitcode(t, code, 1) |
| 233 | substring(t, "stderr", stderr, "a/a.go:4:11: call of MyFunc123") |
| 234 | substring(t, "stderr", stderr, "b/b.go:6:13: call of MyFunc123") |
| 235 | substring(t, "stderr", stderr, "b/b.go:7:11: call of MyFunc123") |
| 236 | }) |
| 237 | t.Run("a-json", func(t *testing.T) { |
| 238 | code, stdout, _ := vet(t, "-json", "golang.org/fake/a") |
| 239 | exitcode(t, code, 0) |
| 240 | testenv.NeedsGo1Point(t, 26) // depends on CL 702815 (go vet -json => stdout) |
| 241 | json := parseJSON(t, stdout) |
| 242 | substring(t, "json", json, "a/a.go:4:11: [[email protected]/fake/a] call of MyFunc123") |
| 243 | }) |
| 244 | t.Run("c-json", func(t *testing.T) { |
| 245 | code, stdout, _ := vet(t, "-json", "golang.org/fake/c") |
| 246 | exitcode(t, code, 0) |
| 247 | testenv.NeedsGo1Point(t, 26) // depends on CL 702815 (go vet -json => stdout) |
| 248 | json := parseJSON(t, stdout) |
| 249 | substring(t, "json", json, "c/c.go:5:5: [[email protected]/fake/c] self-assignment of i") |
| 250 | }) |
| 251 | t.Run("a-context", func(t *testing.T) { |
| 252 | code, _, stderr := vet(t, "-c=0", "golang.org/fake/a") |
| 253 | exitcode(t, code, 1) |
| 254 | substring(t, "stderr", stderr, "a/a.go:4:11: call of MyFunc123") |
| 255 | substring(t, "stderr", stderr, "4 MyFunc123") |
| 256 | }) |
| 257 | t.Run("d-fix", func(t *testing.T) { |
| 258 | testenv.NeedsGo1Point(t, 26) |
| 259 | code, _, stderr := vet(t, "-fix", "-v", "golang.org/fake/d") |
| 260 | exitcode(t, code, 0) |
| 261 | contains(t, "d/d.go", `fmt.Sprintf("%s", msg)`) // fixed |
| 262 | contains(t, "d/dgen.go", `fmt.Sprintf(msg)`) // fix not applied to generated file |
| 263 | // TODO(adonovan): plumb -v from go vet/fix down to unitchecker. |
| 264 | if false { |
| 265 | substring(t, "stderr", stderr, "skipped 1 fix that would edit generated files") |
| 266 | substring(t, "stderr", stderr, "applied 1 of 2 fixes; 1 files updated") |
| 267 | } |
| 268 | }) |
| 269 | } |