| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package workspace |
| 6 |
|
| 7 |
import ( |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
func TestFixImportDecl(t *testing.T) { |
| 18 |
const src = ` |
| 19 |
-- go.mod -- |
| 20 |
module mod.test |
| 21 |
|
| 22 |
go 1.12 |
| 23 |
-- p.go -- |
| 24 |
package p |
| 25 |
|
| 26 |
import ( |
| 27 |
_ "fmt" |
| 28 |
|
| 29 |
const C = 42 |
| 30 |
` |
| 31 |
|
| 32 |
Run(t, src, func(t *testing.T, env *Env) { |
| 33 |
env.OpenFile("p.go") |
| 34 |
env.RegexpReplace("p.go", "\"fmt\"", "\"fmt\"\n)") |
| 35 |
env.AfterChange( |
| 36 |
NoDiagnostics(ForFile("p.go")), |
| 37 |
) |
| 38 |
}) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
func TestIgnoreFile(t *testing.T) { |
| 44 |
const src = ` |
| 45 |
-- go.mod -- |
| 46 |
module mod.test |
| 47 |
|
| 48 |
go 1.12 |
| 49 |
-- foo.go -- |
| 50 |
package main |
| 51 |
|
| 52 |
func main() {} |
| 53 |
-- bar.go -- |
| 54 |
package main |
| 55 |
|
| 56 |
func main() {} |
| 57 |
` |
| 58 |
|
| 59 |
Run(t, src, func(t *testing.T, env *Env) { |
| 60 |
env.OpenFile("foo.go") |
| 61 |
env.OpenFile("bar.go") |
| 62 |
env.OnceMet( |
| 63 |
env.DoneWithOpen(), |
| 64 |
Diagnostics(env.AtRegexp("foo.go", "func (main)")), |
| 65 |
Diagnostics(env.AtRegexp("bar.go", "func (main)")), |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
env.RegexpReplace("bar.go", "package main", "//go:build ignore\n\npackage main") |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
env.AfterChange( |
| 79 |
NoDiagnostics(ForFile("foo.go")), |
| 80 |
NoDiagnostics(ForFile("bar.go")), |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
env.RegexpReplace("bar.go", "ignore", "excluded") |
| 87 |
env.AfterChange( |
| 88 |
Diagnostics(env.AtRegexp("bar.go", "package (main)"), WithMessage("excluded due to its build tags")), |
| 89 |
) |
| 90 |
}) |
| 91 |
} |
| 92 |
|
| 93 |
func TestReinitializeRepeatedly(t *testing.T) { |
| 94 |
const multiModule = ` |
| 95 |
-- go.work -- |
| 96 |
go 1.18 |
| 97 |
|
| 98 |
use ( |
| 99 |
moda/a |
| 100 |
modb |
| 101 |
) |
| 102 |
-- moda/a/go.mod -- |
| 103 |
module a.com |
| 104 |
|
| 105 |
require b.com v1.2.3 |
| 106 |
-- moda/a/go.sum -- |
| 107 |
b.com v1.2.3 h1:tXrlXP0rnjRpKNmkbLYoWBdq0ikb3C3bKK9//moAWBI= |
| 108 |
b.com v1.2.3/go.mod h1:D+J7pfFBZK5vdIdZEFquR586vKKIkqG7Qjw9AxG5BQ8= |
| 109 |
-- moda/a/a.go -- |
| 110 |
package a |
| 111 |
|
| 112 |
import ( |
| 113 |
"b.com/b" |
| 114 |
) |
| 115 |
|
| 116 |
func main() { |
| 117 |
var x int |
| 118 |
_ = b.Hello() |
| 119 |
// AAA |
| 120 |
} |
| 121 |
-- modb/go.mod -- |
| 122 |
module b.com |
| 123 |
|
| 124 |
-- modb/b/b.go -- |
| 125 |
package b |
| 126 |
|
| 127 |
func Hello() int { |
| 128 |
var x int |
| 129 |
} |
| 130 |
` |
| 131 |
WithOptions( |
| 132 |
ProxyFiles(workspaceModuleProxy), |
| 133 |
Settings{ |
| 134 |
|
| 135 |
|
| 136 |
"diagnosticsDelay": "0", |
| 137 |
}, |
| 138 |
).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 139 |
env.OpenFile("moda/a/a.go") |
| 140 |
env.AfterChange() |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
env.WriteWorkspaceFile("go.work", "go 1.18\nuse moda/a") |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
env.Await(env.StartedChangeWatchedFiles()) |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
env.RegexpReplace("moda/a/a.go", "AAA", "BBB") |
| 157 |
env.AfterChange() |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
loc := env.FirstDefinition(env.RegexpSearch("moda/a/a.go", "Hello")) |
| 162 |
got := env.Sandbox.Workdir.URIToPath(loc.URI) |
| 163 |
if want := "[email protected]/b/b.go"; !strings.HasSuffix(got, want) { |
| 164 |
t.Errorf("expected %s, got %v", want, got) |
| 165 |
} |
| 166 |
}) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
func TestNestedModuleLoading_Issue59458(t *testing.T) { |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
const proxy = ` |
| 182 |
-- [email protected]/go.mod -- |
| 183 |
module b.com |
| 184 |
|
| 185 |
go 1.18 |
| 186 |
-- [email protected]/b/b.go -- |
| 187 |
package b |
| 188 |
|
| 189 |
func Hello() {} |
| 190 |
|
| 191 |
-- b.com/[email protected]/go.mod -- |
| 192 |
module b.com/other |
| 193 |
|
| 194 |
go 1.18 |
| 195 |
|
| 196 |
require b.com v1.2.3 |
| 197 |
-- b.com/[email protected]/go.sun -- |
| 198 |
b.com v1.2.3 h1:AGjCxWRJLUuJiZ21IUTByr9buoa6+B6Qh5LFhVLKpn4= |
| 199 |
-- b.com/[email protected]/bar/bar.go -- |
| 200 |
package bar |
| 201 |
|
| 202 |
import "b.com/b" |
| 203 |
|
| 204 |
func _() { |
| 205 |
b.Hello() |
| 206 |
} |
| 207 |
-- b.com/[email protected]/foo/foo.go -- |
| 208 |
package foo |
| 209 |
|
| 210 |
const Foo = 0 |
| 211 |
` |
| 212 |
|
| 213 |
const files = ` |
| 214 |
-- go.mod -- |
| 215 |
module b.com/nested |
| 216 |
|
| 217 |
go 1.18 |
| 218 |
|
| 219 |
require b.com/other v1.4.6 |
| 220 |
-- nested.go -- |
| 221 |
package nested |
| 222 |
|
| 223 |
import "b.com/other/foo" |
| 224 |
|
| 225 |
const C = foo.Foo |
| 226 |
` |
| 227 |
WithOptions( |
| 228 |
WriteGoSum("."), |
| 229 |
ProxyFiles(proxy), |
| 230 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 231 |
env.OnceMet( |
| 232 |
InitialWorkspaceLoad, |
| 233 |
NoDiagnostics(), |
| 234 |
) |
| 235 |
}) |
| 236 |
} |