| 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_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"go/ast" |
| 10 |
"go/importer" |
| 11 |
"go/parser" |
| 12 |
"go/token" |
| 13 |
"go/types" |
| 14 |
"runtime" |
| 15 |
"strings" |
| 16 |
"testing" |
| 17 |
|
| 18 |
"github.com/google/go-cmp/cmp" |
| 19 |
"golang.org/x/tools/internal/refactor" |
| 20 |
"golang.org/x/tools/internal/testenv" |
| 21 |
) |
| 22 |
|
| 23 |
func TestAddImport(t *testing.T) { |
| 24 |
testenv.NeedsDefaultImporter(t) |
| 25 |
|
| 26 |
descr := func(s string) string { |
| 27 |
if _, _, line, ok := runtime.Caller(1); ok { |
| 28 |
return 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.Run(test.descr, func(t *testing.T) { |
| 323 |
// splice marker (name pkgpath member) |
| 324 |
before, mid, ok1 := strings.Cut(test.src, "«") |
| 325 |
mid, after, ok2 := strings.Cut(mid, "»") |
| 326 |
if !ok1 || !ok2 { |
| 327 |
t.Fatal("no «name path member» marker") |
| 328 |
} |
| 329 |
src := before + "/*!*/" + after |
| 330 |
fields := strings.Fields(mid) |
| 331 |
if len(fields) != 3 { |
| 332 |
t.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 := token.NewFileSet() |
| 338 |
f, err := parser.ParseFile(fset, "a.go", src, parser.ParseComments) |
| 339 |
if err != nil { |
| 340 |
t.Log(err) |
| 341 |
} |
| 342 |
pos := fset.File(f.FileStart).Pos(len(before)) |
| 343 |
|
| 344 |
// type-check |
| 345 |
info := &types.Info{ |
| 346 |
Types: make(map[ast.Expr]types.TypeAndValue), |
| 347 |
Scopes: make(map[ast.Node]*types.Scope), |
| 348 |
Defs: make(map[*ast.Ident]types.Object), |
| 349 |
Implicits: make(map[ast.Node]types.Object), |
| 350 |
} |
| 351 |
conf := &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 |
Error: func(err error) { t.Log(err) }, |
| 356 |
Importer: importer.Default(), |
| 357 |
} |
| 358 |
conf.Check(f.Name.Name, fset, []*ast.File{f}, info) |
| 359 |
|
| 360 |
prefix, edits := refactor.AddImport(info, f, name, path, member, pos) |
| 361 |
|
| 362 |
var edit refactor.Edit |
| 363 |
switch len(edits) { |
| 364 |
case 0: |
| 365 |
case 1: |
| 366 |
edit = edits[0] |
| 367 |
default: |
| 368 |
t.Fatalf("expected at most one edit, got %d", len(edits)) |
| 369 |
} |
| 370 |
|
| 371 |
// apply patch |
| 372 |
start := fset.Position(edit.Pos) |
| 373 |
end := fset.Position(edit.End) |
| 374 |
output := src[:start.Offset] + string(edit.NewText) + src[end.Offset:] |
| 375 |
output = strings.ReplaceAll(output, "/*!*/", prefix+member) |
| 376 |
if output != test.want { |
| 377 |
t.Errorf("\n--got--\n%s\n--want--\n%s\n--diff--\n%s", |
| 378 |
output, test.want, cmp.Diff(test.want, output)) |
| 379 |
} |
| 380 |
}) |
| 381 |
} |
| 382 |
} |