golang.org/x/tools/gopls@v0.15.3/internal/test/integration/diagnostics/analysis_test.go (about) 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 ) 15 16 // Test for the timeformat analyzer, following golang/vscode-go#2406. 17 // 18 // This test checks that applying the suggested fix from the analyzer resolves 19 // the diagnostic warning. 20 func TestTimeFormatAnalyzer(t *testing.T) { 21 const files = ` 22 -- go.mod -- 23 module mod.com 24 25 go 1.18 26 -- main.go -- 27 package main 28 29 import ( 30 "fmt" 31 "time" 32 ) 33 34 func main() { 35 now := time.Now() 36 fmt.Println(now.Format("2006-02-01")) 37 }` 38 39 Run(t, files, func(t *testing.T, env *Env) { 40 env.OpenFile("main.go") 41 42 var d protocol.PublishDiagnosticsParams 43 env.AfterChange( 44 Diagnostics(env.AtRegexp("main.go", "2006-02-01")), 45 ReadDiagnostics("main.go", &d), 46 ) 47 48 env.ApplyQuickFixes("main.go", d.Diagnostics) 49 env.AfterChange(NoDiagnostics(ForFile("main.go"))) 50 }) 51 } 52 53 func TestAnalysisProgressReporting(t *testing.T) { 54 const files = ` 55 -- go.mod -- 56 module mod.com 57 58 go 1.18 59 60 -- main.go -- 61 package main 62 63 func main() { 64 }` 65 66 tests := []struct { 67 setting bool 68 want Expectation 69 }{ 70 {true, CompletedWork(cache.AnalysisProgressTitle, 1, true)}, 71 {false, Not(CompletedWork(cache.AnalysisProgressTitle, 1, true))}, 72 } 73 74 for _, test := range tests { 75 t.Run(fmt.Sprint(test.setting), func(t *testing.T) { 76 WithOptions( 77 Settings{ 78 "reportAnalysisProgressAfter": "0s", 79 "analysisProgressReporting": test.setting, 80 }, 81 ).Run(t, files, func(t *testing.T, env *Env) { 82 env.OpenFile("main.go") 83 env.AfterChange(test.want) 84 }) 85 }) 86 } 87 } 88 89 // Test the embed directive analyzer. 90 // 91 // There is a fix for missing imports, but it should not trigger for other 92 // kinds of issues reported by the analayzer, here the variable 93 // declaration following the embed directive is wrong. 94 func TestNoSuggestedFixesForEmbedDirectiveDeclaration(t *testing.T) { 95 const generated = ` 96 -- go.mod -- 97 module mod.com 98 99 go 1.20 100 101 -- foo.txt -- 102 FOO 103 104 -- main.go -- 105 package main 106 107 import _ "embed" 108 109 //go:embed foo.txt 110 var foo, bar string 111 112 func main() { 113 _ = foo 114 } 115 ` 116 Run(t, generated, func(t *testing.T, env *Env) { 117 env.OpenFile("main.go") 118 var d protocol.PublishDiagnosticsParams 119 env.AfterChange( 120 Diagnostics(env.AtRegexp("main.go", "//go:embed")), 121 ReadDiagnostics("main.go", &d), 122 ) 123 if fixes := env.GetQuickFixes("main.go", d.Diagnostics); len(fixes) != 0 { 124 t.Errorf("got quick fixes %v, wanted none", fixes) 125 } 126 }) 127 }