| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package workspace |
| 6 |
|
| 7 |
import ( |
| 8 |
"sort" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/google/go-cmp/cmp" |
| 13 |
"golang.org/x/tools/gopls/internal/protocol" |
| 14 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 15 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 16 |
) |
| 17 |
|
| 18 |
func TestPackages(t *testing.T) { |
| 19 |
const files = ` |
| 20 |
-- go.mod -- |
| 21 |
module foo |
| 22 |
|
| 23 |
-- foo.go -- |
| 24 |
package foo |
| 25 |
func Foo() |
| 26 |
|
| 27 |
-- bar/bar.go -- |
| 28 |
package bar |
| 29 |
func Bar() |
| 30 |
|
| 31 |
-- baz/go.mod -- |
| 32 |
module baz |
| 33 |
|
| 34 |
-- baz/baz.go -- |
| 35 |
package baz |
| 36 |
func Baz() |
| 37 |
` |
| 38 |
|
| 39 |
t.Run("file", func(t *testing.T) { |
| 40 |
Run(t, files, func(t *testing.T, env *Env) { |
| 41 |
checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI("foo.go")}, false, 0, []command.Package{ |
| 42 |
{ |
| 43 |
Path: "foo", |
| 44 |
ModulePath: "foo", |
| 45 |
}, |
| 46 |
}, map[string]command.Module{ |
| 47 |
"foo": { |
| 48 |
Path: "foo", |
| 49 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 50 |
}, |
| 51 |
}, []string{}) |
| 52 |
}) |
| 53 |
}) |
| 54 |
|
| 55 |
t.Run("package", func(t *testing.T) { |
| 56 |
Run(t, files, func(t *testing.T, env *Env) { |
| 57 |
checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI("bar")}, false, 0, []command.Package{ |
| 58 |
{ |
| 59 |
Path: "foo/bar", |
| 60 |
ModulePath: "foo", |
| 61 |
}, |
| 62 |
}, map[string]command.Module{ |
| 63 |
"foo": { |
| 64 |
Path: "foo", |
| 65 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 66 |
}, |
| 67 |
}, []string{}) |
| 68 |
}) |
| 69 |
}) |
| 70 |
|
| 71 |
t.Run("workspace", func(t *testing.T) { |
| 72 |
Run(t, files, func(t *testing.T, env *Env) { |
| 73 |
checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI("")}, true, 0, []command.Package{ |
| 74 |
{ |
| 75 |
Path: "foo", |
| 76 |
ModulePath: "foo", |
| 77 |
}, |
| 78 |
{ |
| 79 |
Path: "foo/bar", |
| 80 |
ModulePath: "foo", |
| 81 |
}, |
| 82 |
}, map[string]command.Module{ |
| 83 |
"foo": { |
| 84 |
Path: "foo", |
| 85 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 86 |
}, |
| 87 |
}, []string{}) |
| 88 |
}) |
| 89 |
}) |
| 90 |
|
| 91 |
t.Run("nested module", func(t *testing.T) { |
| 92 |
Run(t, files, func(t *testing.T, env *Env) { |
| 93 |
|
| 94 |
env.OpenFile("baz/baz.go") |
| 95 |
|
| 96 |
|
| 97 |
checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI("baz")}, true, 0, []command.Package{ |
| 98 |
{ |
| 99 |
Path: "baz", |
| 100 |
ModulePath: "baz", |
| 101 |
}, |
| 102 |
}, map[string]command.Module{ |
| 103 |
"baz": { |
| 104 |
Path: "baz", |
| 105 |
GoMod: env.Editor.DocumentURI("baz/go.mod"), |
| 106 |
}, |
| 107 |
}, []string{}) |
| 108 |
}) |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
func TestPackagesWithTests(t *testing.T) { |
| 113 |
const files = ` |
| 114 |
-- go.mod -- |
| 115 |
module foo |
| 116 |
|
| 117 |
-- foo.go -- |
| 118 |
package foo |
| 119 |
import "testing" |
| 120 |
func Foo() |
| 121 |
func TestFoo2(t *testing.T) |
| 122 |
func foo() |
| 123 |
|
| 124 |
-- foo_test.go -- |
| 125 |
package foo |
| 126 |
import "testing" |
| 127 |
func TestFoo(t *testing.T) |
| 128 |
func Issue70927(*error) |
| 129 |
func Test_foo(t *testing.T) |
| 130 |
|
| 131 |
-- foo2_test.go -- |
| 132 |
package foo_test |
| 133 |
import "testing" |
| 134 |
func TestBar(t *testing.T) {} |
| 135 |
|
| 136 |
-- baz/baz_test.go -- |
| 137 |
package baz |
| 138 |
import "testing" |
| 139 |
func TestBaz(*testing.T) |
| 140 |
func BenchmarkBaz(*testing.B) |
| 141 |
func FuzzBaz(*testing.F) |
| 142 |
func ExampleBaz() |
| 143 |
|
| 144 |
-- bat/go.mod -- |
| 145 |
module bat |
| 146 |
|
| 147 |
-- bat/bat_test.go -- |
| 148 |
package bat |
| 149 |
import "testing" |
| 150 |
func Test(*testing.T) |
| 151 |
` |
| 152 |
|
| 153 |
t.Run("file", func(t *testing.T) { |
| 154 |
Run(t, files, func(t *testing.T, env *Env) { |
| 155 |
checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI("foo_test.go")}, false, command.NeedTests, []command.Package{ |
| 156 |
{ |
| 157 |
Path: "foo", |
| 158 |
ModulePath: "foo", |
| 159 |
}, |
| 160 |
{ |
| 161 |
Path: "foo", |
| 162 |
ForTest: "foo", |
| 163 |
ModulePath: "foo", |
| 164 |
TestFiles: []command.TestFile{ |
| 165 |
{ |
| 166 |
URI: env.Editor.DocumentURI("foo_test.go"), |
| 167 |
Tests: []command.TestCase{ |
| 168 |
{Name: "TestFoo"}, |
| 169 |
{Name: "Test_foo"}, |
| 170 |
}, |
| 171 |
}, |
| 172 |
}, |
| 173 |
}, |
| 174 |
{ |
| 175 |
Path: "foo_test", |
| 176 |
ForTest: "foo", |
| 177 |
ModulePath: "foo", |
| 178 |
TestFiles: []command.TestFile{ |
| 179 |
{ |
| 180 |
URI: env.Editor.DocumentURI("foo2_test.go"), |
| 181 |
Tests: []command.TestCase{ |
| 182 |
{Name: "TestBar"}, |
| 183 |
}, |
| 184 |
}, |
| 185 |
}, |
| 186 |
}, |
| 187 |
}, map[string]command.Module{ |
| 188 |
"foo": { |
| 189 |
Path: "foo", |
| 190 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 191 |
}, |
| 192 |
}, []string{ |
| 193 |
"func TestFoo(t *testing.T)", |
| 194 |
"func Test_foo(t *testing.T)", |
| 195 |
"func TestBar(t *testing.T) {}", |
| 196 |
}) |
| 197 |
}) |
| 198 |
}) |
| 199 |
|
| 200 |
t.Run("package", func(t *testing.T) { |
| 201 |
Run(t, files, func(t *testing.T, env *Env) { |
| 202 |
checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI("baz")}, false, command.NeedTests, []command.Package{ |
| 203 |
{ |
| 204 |
Path: "foo/baz", |
| 205 |
ForTest: "foo/baz", |
| 206 |
ModulePath: "foo", |
| 207 |
TestFiles: []command.TestFile{ |
| 208 |
{ |
| 209 |
URI: env.Editor.DocumentURI("baz/baz_test.go"), |
| 210 |
Tests: []command.TestCase{ |
| 211 |
{Name: "TestBaz"}, |
| 212 |
{Name: "BenchmarkBaz"}, |
| 213 |
{Name: "FuzzBaz"}, |
| 214 |
{Name: "ExampleBaz"}, |
| 215 |
}, |
| 216 |
}, |
| 217 |
}, |
| 218 |
}, |
| 219 |
}, map[string]command.Module{ |
| 220 |
"foo": { |
| 221 |
Path: "foo", |
| 222 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 223 |
}, |
| 224 |
}, []string{ |
| 225 |
"func TestBaz(*testing.T)", |
| 226 |
"func BenchmarkBaz(*testing.B)", |
| 227 |
"func FuzzBaz(*testing.F)", |
| 228 |
"func ExampleBaz()", |
| 229 |
}) |
| 230 |
}) |
| 231 |
}) |
| 232 |
|
| 233 |
t.Run("workspace", func(t *testing.T) { |
| 234 |
Run(t, files, func(t *testing.T, env *Env) { |
| 235 |
checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI(".")}, true, command.NeedTests, []command.Package{ |
| 236 |
{ |
| 237 |
Path: "foo", |
| 238 |
ModulePath: "foo", |
| 239 |
}, |
| 240 |
{ |
| 241 |
Path: "foo", |
| 242 |
ForTest: "foo", |
| 243 |
ModulePath: "foo", |
| 244 |
TestFiles: []command.TestFile{ |
| 245 |
{ |
| 246 |
URI: env.Editor.DocumentURI("foo_test.go"), |
| 247 |
Tests: []command.TestCase{ |
| 248 |
{Name: "TestFoo"}, |
| 249 |
{Name: "Test_foo"}, |
| 250 |
}, |
| 251 |
}, |
| 252 |
}, |
| 253 |
}, |
| 254 |
{ |
| 255 |
Path: "foo/baz", |
| 256 |
ForTest: "foo/baz", |
| 257 |
ModulePath: "foo", |
| 258 |
TestFiles: []command.TestFile{ |
| 259 |
{ |
| 260 |
URI: env.Editor.DocumentURI("baz/baz_test.go"), |
| 261 |
Tests: []command.TestCase{ |
| 262 |
{Name: "TestBaz"}, |
| 263 |
{Name: "BenchmarkBaz"}, |
| 264 |
{Name: "FuzzBaz"}, |
| 265 |
{Name: "ExampleBaz"}, |
| 266 |
}, |
| 267 |
}, |
| 268 |
}, |
| 269 |
}, |
| 270 |
{ |
| 271 |
Path: "foo_test", |
| 272 |
ForTest: "foo", |
| 273 |
ModulePath: "foo", |
| 274 |
TestFiles: []command.TestFile{ |
| 275 |
{ |
| 276 |
URI: env.Editor.DocumentURI("foo2_test.go"), |
| 277 |
Tests: []command.TestCase{ |
| 278 |
{Name: "TestBar"}, |
| 279 |
}, |
| 280 |
}, |
| 281 |
}, |
| 282 |
}, |
| 283 |
}, map[string]command.Module{ |
| 284 |
"foo": { |
| 285 |
Path: "foo", |
| 286 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 287 |
}, |
| 288 |
}, []string{ |
| 289 |
"func TestFoo(t *testing.T)", |
| 290 |
"func Test_foo(t *testing.T)", |
| 291 |
"func TestBaz(*testing.T)", |
| 292 |
"func BenchmarkBaz(*testing.B)", |
| 293 |
"func FuzzBaz(*testing.F)", |
| 294 |
"func ExampleBaz()", |
| 295 |
"func TestBar(t *testing.T) {}", |
| 296 |
}) |
| 297 |
}) |
| 298 |
}) |
| 299 |
|
| 300 |
t.Run("nested module", func(t *testing.T) { |
| 301 |
Run(t, files, func(t *testing.T, env *Env) { |
| 302 |
|
| 303 |
env.OpenFile("bat/bat_test.go") |
| 304 |
|
| 305 |
|
| 306 |
checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI("bat")}, true, command.NeedTests, []command.Package{ |
| 307 |
{ |
| 308 |
Path: "bat", |
| 309 |
ForTest: "bat", |
| 310 |
ModulePath: "bat", |
| 311 |
TestFiles: []command.TestFile{ |
| 312 |
{ |
| 313 |
URI: env.Editor.DocumentURI("bat/bat_test.go"), |
| 314 |
Tests: []command.TestCase{ |
| 315 |
{Name: "Test"}, |
| 316 |
}, |
| 317 |
}, |
| 318 |
}, |
| 319 |
}, |
| 320 |
}, map[string]command.Module{ |
| 321 |
"bat": { |
| 322 |
Path: "bat", |
| 323 |
GoMod: env.Editor.DocumentURI("bat/go.mod"), |
| 324 |
}, |
| 325 |
}, []string{ |
| 326 |
"func Test(*testing.T)", |
| 327 |
}) |
| 328 |
}) |
| 329 |
}) |
| 330 |
} |
| 331 |
|
| 332 |
func TestPackagesWithSubtests(t *testing.T) { |
| 333 |
const files = ` |
| 334 |
-- go.mod -- |
| 335 |
module foo |
| 336 |
|
| 337 |
-- foo_test.go -- |
| 338 |
package foo |
| 339 |
|
| 340 |
import "testing" |
| 341 |
|
| 342 |
// Verify that examples don't break subtest detection |
| 343 |
func ExampleFoo() {} |
| 344 |
|
| 345 |
func TestFoo(t *testing.T) { |
| 346 |
t.Run("Bar", func(t *testing.T) { |
| 347 |
t.Run("Baz", func(t *testing.T) {}) |
| 348 |
}) |
| 349 |
t.Run("Bar", func(t *testing.T) {}) |
| 350 |
t.Run("Bar", func(t *testing.T) {}) |
| 351 |
t.Run("with space", func(t *testing.T) {}) |
| 352 |
|
| 353 |
var x X |
| 354 |
y := func(t *testing.T) { |
| 355 |
t.Run("VarSub", func(t *testing.T) {}) |
| 356 |
} |
| 357 |
t.Run("SubtestFunc", SubtestFunc) |
| 358 |
t.Run("SubtestMethod", x.SubtestMethod) |
| 359 |
t.Run("SubtestVar", y) |
| 360 |
} |
| 361 |
|
| 362 |
func SubtestFunc(t *testing.T) { |
| 363 |
t.Run("FuncSub", func(t *testing.T) {}) |
| 364 |
} |
| 365 |
|
| 366 |
type X int |
| 367 |
func (X) SubtestMethod(t *testing.T) { |
| 368 |
t.Run("MethodSub", func(t *testing.T) {}) |
| 369 |
} |
| 370 |
` |
| 371 |
|
| 372 |
Run(t, files, func(t *testing.T, env *Env) { |
| 373 |
checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI("foo_test.go")}, false, command.NeedTests, []command.Package{ |
| 374 |
{ |
| 375 |
Path: "foo", |
| 376 |
ForTest: "foo", |
| 377 |
ModulePath: "foo", |
| 378 |
TestFiles: []command.TestFile{ |
| 379 |
{ |
| 380 |
URI: env.Editor.DocumentURI("foo_test.go"), |
| 381 |
Tests: []command.TestCase{ |
| 382 |
{Name: "ExampleFoo"}, |
| 383 |
{Name: "TestFoo"}, |
| 384 |
{Name: "TestFoo/Bar"}, |
| 385 |
{Name: "TestFoo/Bar/Baz"}, |
| 386 |
{Name: "TestFoo/Bar#01"}, |
| 387 |
{Name: "TestFoo/Bar#02"}, |
| 388 |
{Name: "TestFoo/with_space"}, |
| 389 |
{Name: "TestFoo/SubtestFunc"}, |
| 390 |
{Name: "TestFoo/SubtestFunc/FuncSub"}, |
| 391 |
{Name: "TestFoo/SubtestMethod"}, |
| 392 |
{Name: "TestFoo/SubtestMethod/MethodSub"}, |
| 393 |
{Name: "TestFoo/SubtestVar"}, |
| 394 |
|
| 395 |
}, |
| 396 |
}, |
| 397 |
}, |
| 398 |
}, |
| 399 |
}, map[string]command.Module{ |
| 400 |
"foo": { |
| 401 |
Path: "foo", |
| 402 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 403 |
}, |
| 404 |
}, []string{ |
| 405 |
"func ExampleFoo() {}", |
| 406 |
`func TestFoo(t *testing.T) { |
| 407 |
t.Run("Bar", func(t *testing.T) { |
| 408 |
t.Run("Baz", func(t *testing.T) {}) |
| 409 |
}) |
| 410 |
t.Run("Bar", func(t *testing.T) {}) |
| 411 |
t.Run("Bar", func(t *testing.T) {}) |
| 412 |
t.Run("with space", func(t *testing.T) {}) |
| 413 |
|
| 414 |
var x X |
| 415 |
y := func(t *testing.T) { |
| 416 |
t.Run("VarSub", func(t *testing.T) {}) |
| 417 |
} |
| 418 |
t.Run("SubtestFunc", SubtestFunc) |
| 419 |
t.Run("SubtestMethod", x.SubtestMethod) |
| 420 |
t.Run("SubtestVar", y) |
| 421 |
}`, |
| 422 |
"t.Run(\"Bar\", func(t *testing.T) {\n\t\tt.Run(\"Baz\", func(t *testing.T) {})\n\t})", |
| 423 |
`t.Run("Baz", func(t *testing.T) {})`, |
| 424 |
`t.Run("Bar", func(t *testing.T) {})`, |
| 425 |
`t.Run("Bar", func(t *testing.T) {})`, |
| 426 |
`t.Run("with space", func(t *testing.T) {})`, |
| 427 |
`t.Run("SubtestFunc", SubtestFunc)`, |
| 428 |
`t.Run("FuncSub", func(t *testing.T) {})`, |
| 429 |
`t.Run("SubtestMethod", x.SubtestMethod)`, |
| 430 |
`t.Run("MethodSub", func(t *testing.T) {})`, |
| 431 |
`t.Run("SubtestVar", y)`, |
| 432 |
}) |
| 433 |
}) |
| 434 |
} |
| 435 |
|
| 436 |
func TestRecursiveSubtest(t *testing.T) { |
| 437 |
const files = ` |
| 438 |
-- go.mod -- |
| 439 |
module foo |
| 440 |
|
| 441 |
-- foo_test.go -- |
| 442 |
package foo |
| 443 |
|
| 444 |
import "testing" |
| 445 |
|
| 446 |
func TestFoo(t *testing.T) { t.Run("Foo", TestFoo) } |
| 447 |
func TestBar(t *testing.T) { t.Run("Foo", TestFoo) } |
| 448 |
|
| 449 |
func TestBaz(t *testing.T) { |
| 450 |
var sub func(t *testing.T) |
| 451 |
sub = func(t *testing.T) { t.Run("Sub", sub) } |
| 452 |
t.Run("Sub", sub) |
| 453 |
} |
| 454 |
` |
| 455 |
|
| 456 |
Run(t, files, func(t *testing.T, env *Env) { |
| 457 |
checkPackages(t, env, []protocol.DocumentURI{env.Editor.DocumentURI("foo_test.go")}, false, command.NeedTests, []command.Package{ |
| 458 |
{ |
| 459 |
Path: "foo", |
| 460 |
ForTest: "foo", |
| 461 |
ModulePath: "foo", |
| 462 |
TestFiles: []command.TestFile{ |
| 463 |
{ |
| 464 |
URI: env.Editor.DocumentURI("foo_test.go"), |
| 465 |
Tests: []command.TestCase{ |
| 466 |
{Name: "TestFoo"}, |
| 467 |
{Name: "TestFoo/Foo"}, |
| 468 |
{Name: "TestBar"}, |
| 469 |
{Name: "TestBar/Foo"}, |
| 470 |
{Name: "TestBaz"}, |
| 471 |
{Name: "TestBaz/Sub"}, |
| 472 |
}, |
| 473 |
}, |
| 474 |
}, |
| 475 |
}, |
| 476 |
}, map[string]command.Module{ |
| 477 |
"foo": { |
| 478 |
Path: "foo", |
| 479 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 480 |
}, |
| 481 |
}, []string{ |
| 482 |
`func TestFoo(t *testing.T) { t.Run("Foo", TestFoo) }`, |
| 483 |
`t.Run("Foo", TestFoo)`, |
| 484 |
`func TestBar(t *testing.T) { t.Run("Foo", TestFoo) }`, |
| 485 |
`t.Run("Foo", TestFoo)`, |
| 486 |
`func TestBaz(t *testing.T) { |
| 487 |
var sub func(t *testing.T) |
| 488 |
sub = func(t *testing.T) { t.Run("Sub", sub) } |
| 489 |
t.Run("Sub", sub) |
| 490 |
}`, |
| 491 |
`t.Run("Sub", sub)`, |
| 492 |
}) |
| 493 |
}) |
| 494 |
} |
| 495 |
|
| 496 |
func checkPackages(t testing.TB, env *Env, files []protocol.DocumentURI, recursive bool, mode command.PackagesMode, wantPkg []command.Package, wantModule map[string]command.Module, wantSource []string) { |
| 497 |
t.Helper() |
| 498 |
|
| 499 |
cmd := command.NewPackagesCommand("Packages", command.PackagesArgs{Files: files, Recursive: recursive, Mode: mode}) |
| 500 |
var result command.PackagesResult |
| 501 |
env.ExecuteCommand(&protocol.ExecuteCommandParams{ |
| 502 |
Command: command.Packages.String(), |
| 503 |
Arguments: cmd.Arguments, |
| 504 |
}, &result) |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
sort.Slice(result.Packages, func(i, j int) bool { |
| 509 |
a, b := result.Packages[i], result.Packages[j] |
| 510 |
c := strings.Compare(a.Path, b.Path) |
| 511 |
if c != 0 { |
| 512 |
return c < 0 |
| 513 |
} |
| 514 |
return strings.Compare(a.ForTest, b.ForTest) < 0 |
| 515 |
}) |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
gotSource := []string{} |
| 521 |
for i := range result.Packages { |
| 522 |
pkg := &result.Packages[i] |
| 523 |
for i := range pkg.TestFiles { |
| 524 |
file := &pkg.TestFiles[i] |
| 525 |
env.OpenFile(file.URI.Path()) |
| 526 |
|
| 527 |
for i := range file.Tests { |
| 528 |
test := &file.Tests[i] |
| 529 |
gotSource = append(gotSource, env.FileContentAt(test.Loc)) |
| 530 |
test.Loc = protocol.Location{} |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
if diff := cmp.Diff(wantPkg, result.Packages); diff != "" { |
| 536 |
t.Errorf("Packages(%v) returned unexpected packages (-want +got):\n%s", files, diff) |
| 537 |
} |
| 538 |
|
| 539 |
if diff := cmp.Diff(wantModule, result.Module); diff != "" { |
| 540 |
t.Errorf("Packages(%v) returned unexpected modules (-want +got):\n%s", files, diff) |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
if !t.Failed() { |
| 545 |
if diff := cmp.Diff(wantSource, gotSource); diff != "" { |
| 546 |
t.Errorf("Packages(%v) returned unexpected test case ranges (-want +got):\n%s", files, diff) |
| 547 |
} |
| 548 |
} |
| 549 |
} |