| 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 analysistest_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 10 | "logscip-go gomod github.com/golang/go/src go1.25.0 log/" |
| 11 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 12 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 13 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 14 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 15 | |
| 16 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 17 | "golang.org/x/tools/go/analysis/analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/" |
| 18 | "golang.org/x/tools/go/analysis/passes/findcallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/" |
| 19 | "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 20 | "golang.org/x/tools/internal/testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/" |
| 21 | "golang.org/x/tools/txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/" |
| 22 | ) |
| 23 | |
| 24 | func initscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest_test`/init().() { |
| 25 | // Run() decides when tests use GOPATH mode or modules. |
| 26 | // We turn off GOPROXY just for good measure. |
| 27 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.0 os/Setenv().("GOPROXY", "off"); err != nil { |
| 28 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Fatalscip-go gomod github.com/golang/go/src go1.25.0 log/Fatal().(err) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // TestTheTest tests the analysistest testing infrastructure. |
| 33 | func TestTheTestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest_test`/TestTheTest().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 34 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go") |
| 35 | |
| 36 | // We'll simulate a partly failing test of the findcall analysis, |
| 37 | // which (by default) reports calls to functions named 'println'. |
| 38 | findcallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/Analyzer..Flagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Flags..Setscip-go gomod github.com/golang/go/src go1.25.0 flag/FlagSet#Set().("name", "println") |
| 39 | |
| 40 | filemap := map[string]string{ |
| 41 | "a/b.go": `package main // want package:"found" |
| 42 | |
| 43 | func main() { |
| 44 | // The expectation is ill-formed: |
| 45 | print() // want: "diagnostic" |
| 46 | print() // want foo"fact" |
| 47 | print() // want foo: |
| 48 | print() // want "\xZZ scan error" |
| 49 | |
| 50 | // A diagnostic is reported at this line, but the expectation doesn't match: |
| 51 | println("hello, world") // want "wrong expectation text" |
| 52 | |
| 53 | // An unexpected diagnostic is reported at this line: |
| 54 | println() // trigger an unexpected diagnostic |
| 55 | |
| 56 | // No diagnostic is reported at this line: |
| 57 | print() // want "unsatisfied expectation" |
| 58 | |
| 59 | // OK |
| 60 | println("hello, world") // want "call of println" |
| 61 | |
| 62 | // OK /* */-form. |
| 63 | println("안녕, 세계") /* want "call of println" */ |
| 64 | |
| 65 | // OK (nested comment) |
| 66 | println("Γειά σου, Κόσμε") // some comment // want "call of println" |
| 67 | |
| 68 | // OK (nested comment in /**/) |
| 69 | println("你好,世界") /* some comment // want "call of println" */ |
| 70 | |
| 71 | // OK (multiple expectations on same line) |
| 72 | println(); println() // want "call of println(...)" "call of println(...)" |
| 73 | |
| 74 | // A Line that is not formatted correctly in the golden file. |
| 75 | } |
| 76 | |
| 77 | // OK (facts and diagnostics on same line) |
| 78 | func println(...interface{}) { println() } // want println:"found" "call of println(...)" |
| 79 | |
| 80 | `, |
| 81 | "a/b.go.golden": `package main // want package:"found" |
| 82 | |
| 83 | func main() { |
| 84 | // The expectation is ill-formed: |
| 85 | print() // want: "diagnostic" |
| 86 | print() // want foo"fact" |
| 87 | print() // want foo: |
| 88 | print() // want "\xZZ scan error" |
| 89 | |
| 90 | // A diagnostic is reported at this line, but the expectation doesn't match: |
| 91 | println_TEST_("hello, world") // want "wrong expectation text" |
| 92 | |
| 93 | // An unexpected diagnostic is reported at this line: |
| 94 | println_TEST_() // trigger an unexpected diagnostic |
| 95 | |
| 96 | // No diagnostic is reported at this line: |
| 97 | print() // want "unsatisfied expectation" |
| 98 | |
| 99 | // OK |
| 100 | println_TEST_("hello, world") // want "call of println" |
| 101 | |
| 102 | // OK /* */-form. |
| 103 | println_TEST_("안녕, 세계") /* want "call of println" */ |
| 104 | |
| 105 | // OK (nested comment) |
| 106 | println_TEST_("Γειά σου, Κόσμε") // some comment // want "call of println" |
| 107 | |
| 108 | // OK (nested comment in /**/) |
| 109 | println_TEST_("你好,世界") /* some comment // want "call of println" */ |
| 110 | |
| 111 | // OK (multiple expectations on same line) |
| 112 | println_TEST_() |
| 113 | println_TEST_() // want "call of println(...)" "call of println(...)" |
| 114 | |
| 115 | // A Line that is not formatted correctly in the golden file. |
| 116 | } |
| 117 | |
| 118 | // OK (facts and diagnostics on same line) |
| 119 | func println(...interface{}) { println_TEST_() } // want println:"found" "call of println(...)" |
| 120 | `, |
| 121 | "a/b_test.go": `package main |
| 122 | |
| 123 | // Test file shouldn't mess with things (issue #40574) |
| 124 | `, |
| 125 | } |
| 126 | dir, cleanup, err := analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.WriteFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/WriteFiles().(filemap) |
| 127 | if err != nil { |
| 128 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 129 | } |
| 130 | defer cleanup() |
| 131 | |
| 132 | var got []string |
| 133 | t2 := errorfuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest_test`/errorfunc#(func(s string) { got = append(got, s) }) // a fake *testing.T |
| 134 | analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.RunWithSuggestedFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/RunWithSuggestedFixes().(t2, dir, findcallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/Analyzer., "a") |
| 135 | |
| 136 | want := []string{ |
| 137 | `a/b.go:5: in 'want' comment: unexpected ":"`, |
| 138 | `a/b.go:6: in 'want' comment: got String after foo, want ':'`, |
| 139 | `a/b.go:7: in 'want' comment: got EOF, want regular expression`, |
| 140 | `a/b.go:8: in 'want' comment: invalid char escape`, |
| 141 | "a/b.go:11:9: diagnostic \"call of println(...)\" does not match pattern `wrong expectation text`", |
| 142 | `a/b.go:14:9: unexpected diagnostic: call of println(...)`, |
| 143 | "a/b.go:11: no diagnostic was reported matching `wrong expectation text`", |
| 144 | "a/b.go:17: no diagnostic was reported matching `unsatisfied expectation`", |
| 145 | // duplicate copies of each message from the test package (see issue #40574) |
| 146 | `a/b.go:5: in 'want' comment: unexpected ":"`, |
| 147 | `a/b.go:6: in 'want' comment: got String after foo, want ':'`, |
| 148 | `a/b.go:7: in 'want' comment: got EOF, want regular expression`, |
| 149 | `a/b.go:8: in 'want' comment: invalid char escape`, |
| 150 | "a/b.go:11:9: diagnostic \"call of println(...)\" does not match pattern `wrong expectation text`", |
| 151 | `a/b.go:14:9: unexpected diagnostic: call of println(...)`, |
| 152 | "a/b.go:11: no diagnostic was reported matching `wrong expectation text`", |
| 153 | "a/b.go:17: no diagnostic was reported matching `unsatisfied expectation`", |
| 154 | } |
| 155 | if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(got, want) { |
| 156 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got:\n%s\nwant:\n%s", |
| 157 | stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(got, "\n"), |
| 158 | stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(want, "\n")) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // TestNoEnd tests that a missing SuggestedFix.End position is |
| 163 | // correctly interpreted as if equal to SuggestedFix.Pos (see issue #64199). |
| 164 | func TestNoEndscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest_test`/TestNoEnd().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 165 | noend := &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 166 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "noend", |
| 167 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "inserts /*hello*/ before first decl", |
| 168 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: func(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 169 | decl := pass.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Files.[0].Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[0] |
| 170 | pass.Reportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Report.(analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Diagnosticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#{ |
| 171 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.: decl.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 172 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#End.: tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NoPosscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NoPos., |
| 173 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.: "say hello", |
| 174 | SuggestedFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#SuggestedFixes.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.SuggestedFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#{{ |
| 175 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.: "say hello", |
| 176 | TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{ |
| 177 | { |
| 178 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: decl.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 179 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NoPosscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NoPos., |
| 180 | NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte("/*hello*/"), |
| 181 | }, |
| 182 | }, |
| 183 | }}, |
| 184 | }) |
| 185 | return nil, nil |
| 186 | }, |
| 187 | } |
| 188 | |
| 189 | filemap := map[string]string{ |
| 190 | "a/a.go": `package a |
| 191 | |
| 192 | func F() {} // want "say hello"`, |
| 193 | "a/a.go.golden": `package a |
| 194 | |
| 195 | /*hello*/ |
| 196 | func F() {} // want "say hello"`, |
| 197 | } |
| 198 | dir, cleanup, err := analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.WriteFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/WriteFiles().(filemap) |
| 199 | if err != nil { |
| 200 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 201 | } |
| 202 | defer cleanup() |
| 203 | |
| 204 | analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.RunWithSuggestedFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/RunWithSuggestedFixes().(t, dir, noend, "a") |
| 205 | } |
| 206 | |
| 207 | func TestModulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest_test`/TestModule().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 208 | const content = ` |
| 209 | Test that analysis.pass.Module is populated. |
| 210 | |
| 211 | -- go.mod -- |
| 212 | module golang.org/fake/mod |
| 213 | |
| 214 | go 1.21 |
| 215 | |
| 216 | require golang.org/xyz/fake v0.12.34 |
| 217 | |
| 218 | -- mod.go -- |
| 219 | // We expect a module.Path and a module.GoVersion, but an empty module.Version. |
| 220 | |
| 221 | package mod // want "golang.org/fake/mod,,1.21" |
| 222 | |
| 223 | import "golang.org/xyz/fake/ver" |
| 224 | |
| 225 | var _ ver.T |
| 226 | |
| 227 | -- vendor/modules.txt -- |
| 228 | # golang.org/xyz/fake v0.12.34 |
| 229 | ## explicit; go 1.18 |
| 230 | golang.org/xyz/fake/ver |
| 231 | |
| 232 | -- vendor/golang.org/xyz/fake/ver/ver.go -- |
| 233 | // This package is vendored so that we can populate a non-empty |
| 234 | // Pass.Module.Version is in a test. |
| 235 | |
| 236 | package ver //want "golang.org/xyz/fake,v0.12.34,1.18" |
| 237 | |
| 238 | type T string |
| 239 | ` |
| 240 | fs, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.FSscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/FS().(txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Parse().([]byte(content))) |
| 241 | if err != nil { |
| 242 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 243 | } |
| 244 | dir := testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/.CopyToTmpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/CopyToTmp().(t, fs) |
| 245 | |
| 246 | filever := &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 247 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "mod", |
| 248 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "reports module information", |
| 249 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: func(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 250 | msg := "no module info" |
| 251 | if m := pass.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Module.; m != nil { |
| 252 | msg = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s,%s,%s", m.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#Path., m.Versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#Version., m.GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Module#GoVersion.) |
| 253 | } |
| 254 | for _, file := range pass.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Files. { |
| 255 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(file.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Package., "%s", msg) |
| 256 | } |
| 257 | return nil, nil |
| 258 | }, |
| 259 | } |
| 260 | analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Run().(t, dir, filever, "golang.org/fake/mod", "golang.org/xyz/fake/ver") |
| 261 | } |
| 262 | |
| 263 | type errorfuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest_test`/errorfunc# func(string) |
| 264 | |
| 265 | func (f errorfuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest_test`/errorfunc#) Errorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest_test`/errorfunc#Errorf().(format string, args ...any) { |
| 266 | f(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(format, args...)) |
| 267 | } |