| 1 | // Copyright 2019 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_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 9 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 10 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 11 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 12 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 13 | |
| 14 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 15 | "golang.org/x/tools/go/analysis/analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/" |
| 16 | "golang.org/x/tools/go/analysis/internal/analysisflagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/" |
| 17 | "golang.org/x/tools/go/analysis/internal/checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/" |
| 18 | "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/" |
| 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 TestApplyFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/TestApplyFixes().(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#) { |
| 25 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoPackages().(t) |
| 26 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.RedirectStderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/RedirectStderr().(t) // associated checker.Run output with this test |
| 27 | |
| 28 | files := map[string]string{ |
| 29 | "rename/test.go": `package rename |
| 30 | |
| 31 | func Foo() { |
| 32 | bar := 12 |
| 33 | _ = bar |
| 34 | } |
| 35 | |
| 36 | // the end |
| 37 | `} |
| 38 | want := `package rename |
| 39 | |
| 40 | func Foo() { |
| 41 | baz := 12 |
| 42 | _ = baz |
| 43 | } |
| 44 | |
| 45 | // the end |
| 46 | ` |
| 47 | |
| 48 | testdata, 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().(files) |
| 49 | if err != nil { |
| 50 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 51 | } |
| 52 | path := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(testdata, "src/rename/test.go") |
| 53 | |
| 54 | analysisflagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/.Fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/Fix. = true |
| 55 | checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Run().([]string{"file=" + path}, []*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#{renameAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/renameAnalyzer.}) |
| 56 | analysisflagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/.Fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/analysisflags`/Fix. = false |
| 57 | |
| 58 | contents, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(path) |
| 59 | if err != nil { |
| 60 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 61 | } |
| 62 | |
| 63 | got := string(contents) |
| 64 | if got != want { |
| 65 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("contents of rewritten file\ngot: %s\nwant: %s", got, want) |
| 66 | } |
| 67 | |
| 68 | defer cleanup() |
| 69 | } |
| 70 | |
| 71 | func TestRunDespiteErrorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/TestRunDespiteErrors().(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#) { |
| 72 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoPackages().(t) |
| 73 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.RedirectStderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/RedirectStderr().(t) // associate checker.Run output with this test |
| 74 | |
| 75 | files := map[string]string{ |
| 76 | "rderr/test.go": `package rderr |
| 77 | |
| 78 | // Foo deliberately has a type error |
| 79 | func Foo(s string) int { |
| 80 | return s + 1 |
| 81 | } |
| 82 | `, |
| 83 | "cperr/test.go": `package copyerr |
| 84 | |
| 85 | import "sync" |
| 86 | |
| 87 | func bar() { } // for renameAnalyzer |
| 88 | |
| 89 | type T struct{ mu sync.Mutex } |
| 90 | type T1 struct{ t *T } |
| 91 | |
| 92 | func NewT1() *T1 { return &T1{T} } |
| 93 | `, |
| 94 | } |
| 95 | |
| 96 | testdata, 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().(files) |
| 97 | if err != nil { |
| 98 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 99 | } |
| 100 | defer cleanup() |
| 101 | |
| 102 | rderrFile := "file=" + filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(testdata, "src/rderr/test.go") |
| 103 | cperrFile := "file=" + filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(testdata, "src/cperr/test.go") |
| 104 | |
| 105 | // A no-op analyzer that should finish regardless of |
| 106 | // parse or type errors in the code. |
| 107 | noop := &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#{ |
| 108 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "noop", |
| 109 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "noop", |
| 110 | Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires.: []*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#{inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer.}, |
| 111 | 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) { |
| 112 | return nil, nil |
| 113 | }, |
| 114 | RunDespiteErrorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#RunDespiteErrors.: true, |
| 115 | } |
| 116 | |
| 117 | // A no-op analyzer, with facts, that should finish |
| 118 | // regardless of parse or type errors in the code. |
| 119 | noopWithFact := &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#{ |
| 120 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "noopfact", |
| 121 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "noopfact", |
| 122 | Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires.: []*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#{inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer.}, |
| 123 | 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) { |
| 124 | return nil, nil |
| 125 | }, |
| 126 | RunDespiteErrorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#RunDespiteErrors.: true, |
| 127 | FactTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#FactTypes.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Factscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Fact#{&EmptyFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/EmptyFact#{}}, |
| 128 | } |
| 129 | |
| 130 | for _, test := range []struct { |
| 131 | name string |
| 132 | pattern []string |
| 133 | analyzers []*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# |
| 134 | code int |
| 135 | }{ |
| 136 | // parse/type errors |
| 137 | {name: "skip-error", pattern: []string{rderrFile}, analyzers: []*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#{renameAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/renameAnalyzer.}, code: 1}, |
| 138 | // RunDespiteErrors allows a driver to run an Analyzer even after parse/type errors. |
| 139 | // |
| 140 | // The noop analyzer doesn't use facts, so the driver loads only the root |
| 141 | // package from source. For the rest, it asks 'go list' for export data, |
| 142 | // which fails because the compiler encounters the type error. Since the |
| 143 | // errors come from 'go list', the driver doesn't run the analyzer. |
| 144 | {name: "despite-error", pattern: []string{rderrFile}, analyzers: []*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#{noop}, code: exitCodeFailedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeFailed.}, |
| 145 | // The noopfact analyzer does use facts, so the driver loads source for |
| 146 | // all dependencies, does type checking itself, recognizes the error as a |
| 147 | // type error, and runs the analyzer. |
| 148 | {name: "despite-error-fact", pattern: []string{rderrFile}, analyzers: []*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#{noopWithFact}, code: exitCodeFailedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeFailed.}, |
| 149 | // combination of parse/type errors and no errors |
| 150 | {name: "despite-error-and-no-error", pattern: []string{rderrFile, "sort"}, analyzers: []*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#{renameAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/renameAnalyzer., noop}, code: exitCodeFailedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeFailed.}, |
| 151 | // non-existing package error |
| 152 | {name: "no-package", pattern: []string{"xyz"}, analyzers: []*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#{renameAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/renameAnalyzer.}, code: exitCodeFailedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeFailed.}, |
| 153 | {name: "no-package-despite-error", pattern: []string{"abc"}, analyzers: []*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#{noop}, code: exitCodeFailedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeFailed.}, |
| 154 | {name: "no-multi-package-despite-error", pattern: []string{"xyz", "abc"}, analyzers: []*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#{noop}, code: exitCodeFailedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeFailed.}, |
| 155 | // combination of type/parsing and different errors |
| 156 | {name: "different-errors", pattern: []string{rderrFile, "xyz"}, analyzers: []*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#{renameAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/renameAnalyzer., noop}, code: exitCodeFailedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeFailed.}, |
| 157 | // non existing dir error |
| 158 | {name: "no-match-dir", pattern: []string{"file=non/existing/dir"}, analyzers: []*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#{renameAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/renameAnalyzer., noop}, code: exitCodeFailedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeFailed.}, |
| 159 | // no errors |
| 160 | {name: "no-errors", pattern: []string{"sort"}, analyzers: []*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#{renameAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/renameAnalyzer., noop}, code: exitCodeSuccessscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeSuccess.}, |
| 161 | // duplicate list error with no findings |
| 162 | {name: "list-error", pattern: []string{cperrFile}, analyzers: []*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#{noop}, code: exitCodeFailedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeFailed.}, |
| 163 | // duplicate list errors with findings (issue #67790) |
| 164 | {name: "list-error-findings", pattern: []string{cperrFile}, analyzers: []*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#{renameAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/renameAnalyzer.}, code: exitCodeDiagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/exitCodeDiagnostics.}, |
| 165 | } { |
| 166 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(test.name, func(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#) { |
| 167 | if got := checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker`/Run().(test.pattern, test.analyzers); got != test.code { |
| 168 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got incorrect exit code %d for test %s; want %d", got, test.name, test.code) |
| 169 | } |
| 170 | }) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | type EmptyFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/EmptyFact# struct{} |
| 175 | |
| 176 | func (f *EmptyFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/EmptyFact#) AFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/EmptyFact#AFact().() {} |
| 177 | |
| 178 | func TestURLscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/TestURL().(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#) { |
| 179 | // TestURL test that URLs get forwarded to diagnostics by internal/checker. |
| 180 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoPackages().(t) |
| 181 | |
| 182 | files := map[string]string{ |
| 183 | "p/test.go": `package p // want "package name is p"`, |
| 184 | } |
| 185 | pkgname := &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#{ |
| 186 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "pkgname", |
| 187 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "trivial analyzer that reports package names", |
| 188 | URLscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#URL.: "https://pkg.go.dev/golang.org/x/tools/go/analysis/internal/checker", |
| 189 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: func(p *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) { |
| 190 | for _, f := range p.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Files. { |
| 191 | p.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(f.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name., "package name is %s", f.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.) |
| 192 | } |
| 193 | return nil, nil |
| 194 | }, |
| 195 | } |
| 196 | |
| 197 | testdata, 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().(files) |
| 198 | if err != nil { |
| 199 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 200 | } |
| 201 | defer cleanup() |
| 202 | path := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(testdata, "src/p/test.go") |
| 203 | results := 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, testdata, pkgname, "file="+path) |
| 204 | |
| 205 | var urls []string |
| 206 | for _, r := range results { |
| 207 | for _, d := range r.Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Diagnostics. { |
| 208 | urls = append(urls, d.URLscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#URL.) |
| 209 | } |
| 210 | } |
| 211 | want := []string{"https://pkg.go.dev/golang.org/x/tools/go/analysis/internal/checker"} |
| 212 | 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().(urls, want) { |
| 213 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected Diagnostics.URLs %v. got %v", want, urls) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // TestPassReadFile exercises the Pass.ReadFile function. |
| 218 | func TestPassReadFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/internal/checker_test`/TestPassReadFile().(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#) { |
| 219 | cwd, _ := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getwdscip-go gomod github.com/golang/go/src go1.25.0 os/Getwd().() |
| 220 | |
| 221 | const src = ` |
| 222 | -- go.mod -- |
| 223 | module example.com |
| 224 | |
| 225 | -- p/file.go -- |
| 226 | package p |
| 227 | |
| 228 | -- p/ignored.go -- |
| 229 | //go:build darwin && mips64 |
| 230 | |
| 231 | package p |
| 232 | |
| 233 | hello from ignored |
| 234 | |
| 235 | -- p/other.s -- |
| 236 | hello from other |
| 237 | ` |
| 238 | |
| 239 | // Expand archive into tmp tree. |
| 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(src))) |
| 241 | if err != nil { |
| 242 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 243 | } |
| 244 | tmpdir := 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 | ran := false |
| 247 | a := &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#{ |
| 248 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "a", |
| 249 | Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires.: []*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#{inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer.}, |
| 250 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "doc", |
| 251 | 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) { |
| 252 | if len(pass.OtherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#OtherFiles.)+len(pass.IgnoredFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#IgnoredFiles.) == 0 { |
| 253 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("OtherFiles and IgnoredFiles are empty") |
| 254 | return nil, nil |
| 255 | } |
| 256 | |
| 257 | for _, test := range []struct { |
| 258 | filename string |
| 259 | want string // substring of file content or error message |
| 260 | }{ |
| 261 | { |
| 262 | pass.OtherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#OtherFiles.[0], // [other.s] |
| 263 | "hello from other", |
| 264 | }, |
| 265 | { |
| 266 | pass.IgnoredFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#IgnoredFiles.[0], // [ignored.go] |
| 267 | "hello from ignored", |
| 268 | }, |
| 269 | { |
| 270 | "nonesuch", |
| 271 | "nonesuch is not among OtherFiles, ", // etc |
| 272 | }, |
| 273 | { |
| 274 | filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(cwd, "checker_test.go"), |
| 275 | "checker_test.go is not among OtherFiles, ", // etc |
| 276 | }, |
| 277 | } { |
| 278 | content, err := pass.ReadFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReadFile.(test.filename) |
| 279 | var got string |
| 280 | if err != nil { |
| 281 | got = err.Error() |
| 282 | } else { |
| 283 | got = string(content) |
| 284 | if len(got) > 100 { |
| 285 | got = got[:100] + "..." |
| 286 | } |
| 287 | } |
| 288 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(got, test.want) { |
| 289 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Pass.ReadFile(%q) did not contain %q; got:\n%s", |
| 290 | test.filename, test.want, got) |
| 291 | } |
| 292 | } |
| 293 | ran = true |
| 294 | return nil, nil |
| 295 | }, |
| 296 | } |
| 297 | |
| 298 | 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, tmpdir, a, "example.com/p") |
| 299 | |
| 300 | if !ran { |
| 301 | t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("analyzer did not run") |
| 302 | } |
| 303 | |
| 304 | // TODO(adonovan): test that fixes are applied to the |
| 305 | // pass.ReadFile virtual file tree. |
| 306 | } |