Debian Codegraph @

internal/refactor/imports_test.go

raw symbols SCIP
1 // Copyright 2024 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 refactor_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor_test`/
6
7 import (
8 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
9 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
10 "go/importerscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/"
11 "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/"
12 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
13 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
14 "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/"
15 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
16 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
17
18 "github.com/google/go-cmp/cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/"
19 "golang.org/x/tools/internal/refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/"
20 "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/"
21 )
22
23 func TestAddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor_test`/TestAddImport().(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#) {
24 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsDefaultImporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsDefaultImporter().(t)
25
26 descr := func(s string) string {
27 if _, _, line, ok := runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.Callerscip-go gomod github.com/golang/go/src go1.25.0 runtime/Caller().(1); ok {
28 return 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().("L%d %s", line, s)
29 }
30 panic("runtime.Caller failed")
31 }
32
33 // Each test case contains a «name pkgpath member»
34 // triple to be replaced with a valid qualified identifier
35 // to pkgpath.member, ideally of the specified name.
36 for _, test := range []struct {
37 descr, src, want string
38 }{
39 {
40 descr: descr("simple add import"),
41 src: `package a
42 func _() {
43 «fmt fmt Print»
44 }`,
45 want: `package a
46 import "fmt"
47
48 func _() {
49 fmt.Print
50 }`,
51 },
52 {
53 descr: descr("existing import"),
54 src: `package a
55
56 import "fmt"
57
58 func _(fmt.Stringer) {
59 «fmt fmt Print»
60 }`,
61 want: `package a
62
63 import "fmt"
64
65 func _(fmt.Stringer) {
66 fmt.Print
67 }`,
68 },
69 {
70 descr: descr("existing blank import"),
71 src: `package a
72
73 import _ "fmt"
74
75 func _() {
76 «fmt fmt Print»
77 }`,
78 want: `package a
79
80 import "fmt"
81
82 import _ "fmt"
83
84 func _() {
85 fmt.Print
86 }`,
87 },
88 {
89 descr: descr("existing renaming import"),
90 src: `package a
91
92 import fmtpkg "fmt"
93
94 var fmt int
95
96 func _(fmtpkg.Stringer) {
97 «fmt fmt Print»
98 }`,
99 want: `package a
100
101 import fmtpkg "fmt"
102
103 var fmt int
104
105 func _(fmtpkg.Stringer) {
106 fmtpkg.Print
107 }`,
108 },
109 {
110 descr: descr("existing import is shadowed"),
111 src: `package a
112
113 import "fmt"
114
115 var _ fmt.Stringer
116
117 func _(fmt int) {
118 «fmt fmt Print»
119 }`,
120 want: `package a
121
122 import fmt0 "fmt"
123
124 import "fmt"
125
126 var _ fmt.Stringer
127
128 func _(fmt int) {
129 fmt0.Print
130 }`,
131 },
132 {
133 descr: descr("preferred name is shadowed"),
134 src: `package a
135
136 import "fmt"
137
138 func _(fmt fmt.Stringer) {
139 «fmt fmt Print»
140 }`,
141 want: `package a
142
143 import fmt0 "fmt"
144
145 import "fmt"
146
147 func _(fmt fmt.Stringer) {
148 fmt0.Print
149 }`,
150 },
151 {
152 descr: descr("import inserted before doc comments"),
153 src: `package a
154
155 // hello
156 import "os"
157
158 // world
159 func _() {
160 «fmt fmt Print»
161 }`,
162 want: `package a
163
164 import "fmt"
165
166 // hello
167 import "os"
168
169 // world
170 func _() {
171 fmt.Print
172 }`,
173 },
174 {
175 descr: descr("arbitrary preferred name => renaming import"),
176 src: `package a
177
178 func _() {
179 «foo encoding/json Marshal»
180 }`,
181 want: `package a
182
183 import foo "encoding/json"
184
185 func _() {
186 foo.Marshal
187 }`,
188 },
189 {
190 descr: descr("dot import unshadowed"),
191 src: `package a
192
193 import . "fmt"
194
195 func _() {
196 «. fmt Print»
197 }`,
198 want: `package a
199
200 import . "fmt"
201
202 func _() {
203 Print
204 }`,
205 },
206 {
207 descr: descr("dot import shadowed"),
208 src: `package a
209
210 import . "fmt"
211
212 func _(Print fmt.Stringer) {
213 «fmt fmt Print»
214 }`,
215 want: `package a
216
217 import "fmt"
218
219 import . "fmt"
220
221 func _(Print fmt.Stringer) {
222 fmt.Print
223 }`,
224 },
225 {
226 descr: descr("add import to group"),
227 src: `package a
228
229 import (
230 "io"
231 )
232
233 func _(io.Reader) {
234 «fmt fmt Print»
235 }`,
236 want: `package a
237
238 import (
239 "fmt"
240 "io"
241 )
242
243 func _(io.Reader) {
244 fmt.Print
245 }`,
246 },
247 {
248 descr: descr("add import to group which imports std and a 3rd module"),
249 src: `package a
250
251 import (
252 "io"
253
254 "vendor/golang.org/x/net/dns/dnsmessage"
255 )
256
257 func _(io.Reader) {
258 «fmt fmt Print»
259 }`,
260 want: `package a
261
262 import (
263 "fmt"
264 "io"
265
266 "vendor/golang.org/x/net/dns/dnsmessage"
267 )
268
269 func _(io.Reader) {
270 fmt.Print
271 }`,
272 },
273 {
274 descr: descr("add import to group which imports std and a 3rd module without parens"),
275 src: `package a
276
277 import "io"
278
279 import "vendor/golang.org/x/net/dns/dnsmessage"
280
281 func _(io.Reader) {
282 «fmt fmt Print»
283 }`,
284 want: `package a
285
286 import "fmt"
287
288 import "io"
289
290 import "vendor/golang.org/x/net/dns/dnsmessage"
291
292 func _(io.Reader) {
293 fmt.Print
294 }`,
295 },
296 {
297 descr: descr("add import to group without std import"),
298 src: `package a
299
300 import (
301 "golang.org/x/tools/go/packages"
302 gossa "golang.org/x/tools/go/ssa"
303 )
304
305 func _(io.Reader) {
306 «fmt fmt Print»
307 }`,
308 want: `package a
309
310 import (
311 "fmt"
312
313 "golang.org/x/tools/go/packages"
314 gossa "golang.org/x/tools/go/ssa"
315 )
316
317 func _(io.Reader) {
318 fmt.Print
319 }`,
320 },
321 } {
322 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(test.descr, 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#) {
323 // splice marker (name pkgpath member)
324 before, mid, ok1 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(test.src, "«")
325 mid, after, ok2 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(mid, "»")
326 if !ok1 || !ok2 {
327 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("no «name path member» marker")
328 }
329 src := before + "/*!*/" + after
330 fields := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.25.0 strings/Fields().(mid)
331 if len(fields) != 3 {
332 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("splice marker needs 3 fields (got %q)", mid)
333 }
334 name, path, member := fields[0], fields[1], fields[2]
335
336 // parse
337 fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().()
338 f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "a.go", src, parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseCommentsscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseComments.)
339 if err != nil {
340 t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().(err)
341 }
342 pos := fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(f.FileStartscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#FileStart.).Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Pos().(len(before))
343
344 // type-check
345 info := &typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#{
346 Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.: make(map[astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#]typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeAndValuescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#),
347 Scopesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Scopes.: make(map[astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#),
348 Defsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Defs.: make(map[*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#]typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#),
349 Implicitsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Implicits.: make(map[astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#]typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#),
350 }
351 conf := &typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{
352 // We don't want to fail if there is an error during type checking:
353 // the error may be because we're missing an import, and adding imports
354 // is the whole point of AddImport.
355 Errorscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Error.: func(err error) { t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().(err) },
356 Importerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Importer.: importerscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/importer`/Default().(),
357 }
358 conf.Checkscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Check().(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., fset, []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f}, info)
359
360 prefix, edits := refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/.AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/AddImport().(info, f, name, path, member, pos)
361
362 var edit refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/.Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#
363 switch len(edits) {
364 case 0:
365 case 1:
366 edit = edits[0]
367 default:
368 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("expected at most one edit, got %d", len(edits))
369 }
370
371 // apply patch
372 start := fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(edit.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.)
373 end := fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.)
374 output := src[:start.Offsetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Offset.] + string(edit.NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.) + src[end.Offsetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Offset.:]
375 output = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(output, "/*!*/", prefix+member)
376 if output != test.want {
377 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("\n--got--\n%s\n--want--\n%s\n--diff--\n%s",
378 output, test.want, cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Diffscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Diff().(test.want, output))
379 }
380 })
381 }
382 }