|
| 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 analysisinternal_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/analysisinternal" |
| 20 | +) |
| 21 | + |
| 22 | +func TestAddImport(t *testing.T) { |
| 23 | + descr := func(s string) string { |
| 24 | + if _, _, line, ok := runtime.Caller(1); ok { |
| 25 | + return fmt.Sprintf("L%d %s", line, s) |
| 26 | + } |
| 27 | + panic("runtime.Caller failed") |
| 28 | + } |
| 29 | + |
| 30 | + // Each test case contains a «name pkgpath» |
| 31 | + // section to be replaced with a reference |
| 32 | + // to a valid import of pkgpath, |
| 33 | + // ideally of the specified name. |
| 34 | + for _, test := range []struct { |
| 35 | + descr, src, want string |
| 36 | + }{ |
| 37 | + { |
| 38 | + descr: descr("simple add import"), |
| 39 | + src: `package a |
| 40 | +func _() { |
| 41 | + «fmt fmt» |
| 42 | +}`, |
| 43 | + want: `package a |
| 44 | +import "fmt" |
| 45 | +
|
| 46 | +func _() { |
| 47 | + fmt |
| 48 | +}`, |
| 49 | + }, |
| 50 | + { |
| 51 | + descr: descr("existing import"), |
| 52 | + src: `package a |
| 53 | +
|
| 54 | +import "fmt" |
| 55 | +
|
| 56 | +func _(fmt.Stringer) { |
| 57 | + «fmt fmt» |
| 58 | +}`, |
| 59 | + want: `package a |
| 60 | +
|
| 61 | +import "fmt" |
| 62 | +
|
| 63 | +func _(fmt.Stringer) { |
| 64 | + fmt |
| 65 | +}`, |
| 66 | + }, |
| 67 | + { |
| 68 | + descr: descr("existing blank import"), |
| 69 | + src: `package a |
| 70 | +
|
| 71 | +import _ "fmt" |
| 72 | +
|
| 73 | +func _() { |
| 74 | + «fmt fmt» |
| 75 | +}`, |
| 76 | + want: `package a |
| 77 | +
|
| 78 | +import "fmt" |
| 79 | +
|
| 80 | +import _ "fmt" |
| 81 | +
|
| 82 | +func _() { |
| 83 | + fmt |
| 84 | +}`, |
| 85 | + }, |
| 86 | + { |
| 87 | + descr: descr("existing renaming import"), |
| 88 | + src: `package a |
| 89 | +
|
| 90 | +import fmtpkg "fmt" |
| 91 | +
|
| 92 | +var fmt int |
| 93 | +
|
| 94 | +func _(fmtpkg.Stringer) { |
| 95 | + «fmt fmt» |
| 96 | +}`, |
| 97 | + want: `package a |
| 98 | +
|
| 99 | +import fmtpkg "fmt" |
| 100 | +
|
| 101 | +var fmt int |
| 102 | +
|
| 103 | +func _(fmtpkg.Stringer) { |
| 104 | + fmtpkg |
| 105 | +}`, |
| 106 | + }, |
| 107 | + { |
| 108 | + descr: descr("existing import is shadowed"), |
| 109 | + src: `package a |
| 110 | +
|
| 111 | +import "fmt" |
| 112 | +
|
| 113 | +var _ fmt.Stringer |
| 114 | +
|
| 115 | +func _(fmt int) { |
| 116 | + «fmt fmt» |
| 117 | +}`, |
| 118 | + want: `package a |
| 119 | +
|
| 120 | +import fmt0 "fmt" |
| 121 | +
|
| 122 | +import "fmt" |
| 123 | +
|
| 124 | +var _ fmt.Stringer |
| 125 | +
|
| 126 | +func _(fmt int) { |
| 127 | + fmt0 |
| 128 | +}`, |
| 129 | + }, |
| 130 | + { |
| 131 | + descr: descr("preferred name is shadowed"), |
| 132 | + src: `package a |
| 133 | +
|
| 134 | +import "fmt" |
| 135 | +
|
| 136 | +func _(fmt fmt.Stringer) { |
| 137 | + «fmt fmt» |
| 138 | +}`, |
| 139 | + want: `package a |
| 140 | +
|
| 141 | +import fmt0 "fmt" |
| 142 | +
|
| 143 | +import "fmt" |
| 144 | +
|
| 145 | +func _(fmt fmt.Stringer) { |
| 146 | + fmt0 |
| 147 | +}`, |
| 148 | + }, |
| 149 | + { |
| 150 | + descr: descr("import inserted before doc comments"), |
| 151 | + src: `package a |
| 152 | +
|
| 153 | +// hello |
| 154 | +import () |
| 155 | +
|
| 156 | +// world |
| 157 | +func _() { |
| 158 | + «fmt fmt» |
| 159 | +}`, |
| 160 | + want: `package a |
| 161 | +
|
| 162 | +import "fmt" |
| 163 | +
|
| 164 | +// hello |
| 165 | +import () |
| 166 | +
|
| 167 | +// world |
| 168 | +func _() { |
| 169 | + fmt |
| 170 | +}`, |
| 171 | + }, |
| 172 | + { |
| 173 | + descr: descr("arbitrary preferred name => renaming import"), |
| 174 | + src: `package a |
| 175 | +
|
| 176 | +func _() { |
| 177 | + «foo encoding/json» |
| 178 | +}`, |
| 179 | + want: `package a |
| 180 | +
|
| 181 | +import foo "encoding/json" |
| 182 | +
|
| 183 | +func _() { |
| 184 | + foo |
| 185 | +}`, |
| 186 | + }, |
| 187 | + } { |
| 188 | + t.Run(test.descr, func(t *testing.T) { |
| 189 | + // splice marker |
| 190 | + before, mid, ok1 := strings.Cut(test.src, "«") |
| 191 | + mid, after, ok2 := strings.Cut(mid, "»") |
| 192 | + if !ok1 || !ok2 { |
| 193 | + t.Fatal("no «name path» marker") |
| 194 | + } |
| 195 | + src := before + "/*!*/" + after |
| 196 | + name, path, _ := strings.Cut(mid, " ") |
| 197 | + |
| 198 | + // parse |
| 199 | + fset := token.NewFileSet() |
| 200 | + f, err := parser.ParseFile(fset, "a.go", src, parser.ParseComments) |
| 201 | + if err != nil { |
| 202 | + t.Log(err) |
| 203 | + } |
| 204 | + pos := fset.File(f.Pos()).Pos(len(before)) |
| 205 | + |
| 206 | + // type-check |
| 207 | + info := &types.Info{ |
| 208 | + Types: make(map[ast.Expr]types.TypeAndValue), |
| 209 | + Scopes: make(map[ast.Node]*types.Scope), |
| 210 | + Defs: make(map[*ast.Ident]types.Object), |
| 211 | + Implicits: make(map[ast.Node]types.Object), |
| 212 | + } |
| 213 | + conf := &types.Config{ |
| 214 | + Error: func(err error) { t.Log(err) }, |
| 215 | + Importer: importer.Default(), |
| 216 | + } |
| 217 | + conf.Check(f.Name.Name, fset, []*ast.File{f}, info) |
| 218 | + |
| 219 | + // add import |
| 220 | + name, edit := analysisinternal.AddImport(info, f, pos, path, name) |
| 221 | + |
| 222 | + // apply patch |
| 223 | + start := fset.Position(edit.Pos) |
| 224 | + end := fset.Position(edit.End) |
| 225 | + output := src[:start.Offset] + string(edit.NewText) + src[end.Offset:] |
| 226 | + output = strings.ReplaceAll(output, "/*!*/", name) |
| 227 | + if output != test.want { |
| 228 | + t.Errorf("\n--got--\n%s\n--want--\n%s\n--diff--\n%s", |
| 229 | + output, test.want, cmp.Diff(test.want, output)) |
| 230 | + } |
| 231 | + }) |
| 232 | + } |
| 233 | +} |
0 commit comments