Debian Codegraph @

gopls/internal/test/integration/diagnostics/analysis_test.go

raw symbols SCIP
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 diagnostics
6
7 import (
8 "fmt"
9 "testing"
10
11 "golang.org/x/tools/gopls/internal/cache"
12 "golang.org/x/tools/gopls/internal/protocol"
13 . "golang.org/x/tools/gopls/internal/test/integration"
14 "golang.org/x/tools/internal/testenv"
15 )
16
17 // Test for the timeformat analyzer, following golang/vscode-go#2406.
18 //
19 // This test checks that applying the suggested fix from the analyzer resolves
20 // the diagnostic warning.
21 func TestTimeFormatAnalyzer(t *testing.T) {
22 const files = `
23 -- go.mod --
24 module mod.com
25
26 go 1.18
27 -- main.go --
28 package main
29
30 import (
31 "fmt"
32 "time"
33 )
34
35 func main() {
36 now := time.Now()
37 fmt.Println(now.Format("2006-02-01"))
38 }`
39
40 Run(t, files, func(t *testing.T, env *Env) {
41 env.OpenFile("main.go")
42
43 var d protocol.PublishDiagnosticsParams
44 env.AfterChange(
45 Diagnostics(env.AtRegexp("main.go", "2006-02-01")),
46 ReadDiagnostics("main.go", &d),
47 )
48
49 env.ApplyQuickFixes("main.go", d.Diagnostics)
50 env.AfterChange(NoDiagnostics(ForFile("main.go")))
51 })
52 }
53
54 func TestAnalysisProgressReporting(t *testing.T) {
55 const files = `
56 -- go.mod --
57 module mod.com
58
59 go 1.18
60
61 -- main.go --
62 package main
63
64 func main() {
65 }`
66
67 tests := []struct {
68 setting bool
69 want Expectation
70 }{
71 {true, CompletedWork(cache.AnalysisProgressTitle, 1, true)},
72 {false, Not(CompletedWork(cache.AnalysisProgressTitle, 1, true))},
73 }
74
75 for _, test := range tests {
76 t.Run(fmt.Sprint(test.setting), func(t *testing.T) {
77 WithOptions(
78 Settings{
79 "reportAnalysisProgressAfter": "0s",
80 "analysisProgressReporting": test.setting,
81 },
82 ).Run(t, files, func(t *testing.T, env *Env) {
83 env.OpenFile("main.go")
84 env.AfterChange(test.want)
85 })
86 })
87 }
88 }
89
90 // Test the embed directive analyzer.
91 //
92 // There is a fix for missing imports, but it should not trigger for other
93 // kinds of issues reported by the analayzer, here the variable
94 // declaration following the embed directive is wrong.
95 func TestNoSuggestedFixesForEmbedDirectiveDeclaration(t *testing.T) {
96 const generated = `
97 -- go.mod --
98 module mod.com
99
100 go 1.20
101
102 -- foo.txt --
103 FOO
104
105 -- main.go --
106 package main
107
108 import _ "embed"
109
110 //go:embed foo.txt
111 var foo, Bar string
112
113 func main() {
114 _ = foo
115 }
116 `
117 Run(t, generated, func(t *testing.T, env *Env) {
118 env.OpenFile("main.go")
119 var d protocol.PublishDiagnosticsParams
120 env.AfterChange(
121 Diagnostics(env.AtRegexp("main.go", "//go:embed")),
122 ReadDiagnostics("main.go", &d),
123 )
124 if fixes := env.GetQuickFixes("main.go", d.Diagnostics); len(fixes) != 0 {
125 t.Errorf("got quick fixes %v, wanted none", fixes)
126 }
127 })
128 }
129
130 func TestAnalysisFiltering(t *testing.T) {
131 // This test checks that hint level diagnostics are only surfaced for open
132 // files.
133
134 const src = `
135 -- go.mod --
136 module mod.com
137
138 go 1.20
139
140 -- a.go --
141 package p
142
143 var X interface{}
144
145 -- b.go --
146 package p
147
148 var Y interface{}
149 `
150 Run(t, src, func(t *testing.T, env *Env) {
151 env.OpenFile("a.go")
152 env.AfterChange(
153 Diagnostics(ForFile("a.go"), WithMessage("replaced by any")),
154 NoDiagnostics(ForFile("b.go")),
155 )
156 })
157 }
158
159 func TestModernizationConsistency_Issue75000(t *testing.T) {
160 testenv.SkipAfterGoCommand1Point(t, 24)
161 testenv.NeedsGoCommand1Point(t, 22) // uses range-over-int
162
163 // This test checks that we don't offer modernization suggestions when the
164 // ambient Go version is older than the modernized APIs.
165 //
166 // The code is from golang/go#75000, where gopls suggested to use
167 // `waitgroup.Go` even though the user was on 1.24.
168
169 const src = `
170 -- main.go --
171 package main
172
173 import (
174 "fmt"
175 "sync"
176 )
177
178 func doit(i int) {
179 fmt.Println("i = ", i)
180 }
181
182 func main() {
183 var wg sync.WaitGroup
184 for i := range 5 {
185 wg.Add(1)
186 go func() {
187 defer wg.Done()
188 doit(i)
189 }()
190 }
191
192 wg.Wait()
193 }
194 `
195 Run(t, src, func(t *testing.T, env *Env) {
196 env.OpenFile("main.go")
197 env.AfterChange(NoDiagnostics())
198 })
199 }