Skip to content

Commit eac36cb

Browse files
committed
gopls/internal/regtest: port experimental workspace tests to go.work
Now that experimental workspace mode is deprecated, re-express tests that depended on this mode in terms of go.work files. For golang/go#55331 Change-Id: I90a876319321fd99982d13d258b7653c337f035a Reviewed-on: https://go-review.googlesource.com/c/tools/+/459785 Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 224a61b commit eac36cb

File tree

3 files changed

+111
-38
lines changed

3 files changed

+111
-38
lines changed

gopls/internal/regtest/modfile/modfile_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,15 @@ func main() {
763763
}
764764

765765
func TestMultiModuleModDiagnostics(t *testing.T) {
766+
testenv.NeedsGo1Point(t, 18) // uses go.work
766767
const mod = `
768+
-- go.work --
769+
go 1.18
770+
771+
use (
772+
a
773+
b
774+
)
767775
-- a/go.mod --
768776
module moda.com
769777
@@ -796,7 +804,6 @@ func main() {
796804
`
797805
WithOptions(
798806
ProxyFiles(workspaceProxy),
799-
Modes(Experimental),
800807
).Run(t, mod, func(t *testing.T, env *Env) {
801808
env.Await(
802809
env.DiagnosticAtRegexpWithMessage("a/go.mod", "example.com v1.2.3", "is not used"),

gopls/internal/regtest/watch/watch_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,16 @@ func main() {
572572
}
573573

574574
// Reproduces golang/go#40340.
575-
func TestSwitchFromGOPATHToModules(t *testing.T) {
575+
func TestSwitchFromGOPATHToModuleMode(t *testing.T) {
576576
const files = `
577577
-- foo/blah/blah.go --
578578
package blah
579579
580580
const Name = ""
581-
-- foo/main.go --
581+
-- main.go --
582582
package main
583583
584-
import "blah"
584+
import "foo/blah"
585585
586586
func main() {
587587
_ = blah.Name
@@ -590,16 +590,17 @@ func main() {
590590
WithOptions(
591591
InGOPATH(),
592592
EnvVars{"GO111MODULE": "auto"},
593-
Modes(Experimental), // module is in a subdirectory
594593
).Run(t, files, func(t *testing.T, env *Env) {
595-
env.OpenFile("foo/main.go")
596-
env.Await(env.DiagnosticAtRegexp("foo/main.go", `"blah"`))
594+
env.OpenFile("main.go")
595+
env.AfterChange(
596+
EmptyDiagnostics("main.go"),
597+
)
597598
if err := env.Sandbox.RunGoCommand(env.Ctx, "foo", "mod", []string{"init", "mod.com"}, true); err != nil {
598599
t.Fatal(err)
599600
}
600-
env.RegexpReplace("foo/main.go", `"blah"`, `"mod.com/blah"`)
601-
env.Await(
602-
EmptyDiagnostics("foo/main.go"),
601+
env.RegexpReplace("main.go", `"foo/blah"`, `"mod.com/foo/blah"`)
602+
env.AfterChange(
603+
EmptyDiagnostics("main.go"),
603604
)
604605
})
605606
}

gopls/internal/regtest/workspace/workspace_test.go

Lines changed: 93 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ func Hello() {}
234234
`
235235

236236
func TestAutomaticWorkspaceModule_Interdependent(t *testing.T) {
237+
testenv.NeedsGo1Point(t, 18) // uses go.work
237238
const multiModule = `
238239
-- moda/a/go.mod --
239240
module a.com
@@ -265,17 +266,18 @@ func Hello() int {
265266
`
266267
WithOptions(
267268
ProxyFiles(workspaceModuleProxy),
268-
Modes(Experimental),
269269
).Run(t, multiModule, func(t *testing.T, env *Env) {
270-
env.Await(
270+
env.RunGoCommand("work", "init")
271+
env.RunGoCommand("work", "use", "-r", ".")
272+
env.AfterChange(
271273
env.DiagnosticAtRegexp("moda/a/a.go", "x"),
272274
env.DiagnosticAtRegexp("modb/b/b.go", "x"),
273275
env.NoDiagnosticAtRegexp("moda/a/a.go", `"b.com/b"`),
274276
)
275277
})
276278
}
277279

278-
func TestMultiModuleWithExclude(t *testing.T) {
280+
func TestModuleWithExclude(t *testing.T) {
279281
const proxy = `
280282
-- [email protected]/go.mod --
281283
module c.com
@@ -323,7 +325,6 @@ func main() {
323325
`
324326
WithOptions(
325327
ProxyFiles(proxy),
326-
Modes(Experimental),
327328
).Run(t, multiModule, func(t *testing.T, env *Env) {
328329
env.Await(
329330
env.DiagnosticAtRegexp("main.go", "x"),
@@ -337,9 +338,15 @@ func main() {
337338
// TODO(golang/go#55331): delete this placeholder along with experimental
338339
// workspace module.
339340
func TestDeleteModule_Interdependent(t *testing.T) {
340-
t.Skip("golang/go#55331: the experimental workspace module is scheduled for deletion")
341-
341+
testenv.NeedsGo1Point(t, 18) // uses go.work
342342
const multiModule = `
343+
-- go.work --
344+
go 1.18
345+
346+
use (
347+
moda/a
348+
modb
349+
)
343350
-- moda/a/go.mod --
344351
module a.com
345352
@@ -370,7 +377,6 @@ func Hello() int {
370377
`
371378
WithOptions(
372379
ProxyFiles(workspaceModuleProxy),
373-
Modes(Experimental),
374380
).Run(t, multiModule, func(t *testing.T, env *Env) {
375381
env.OpenFile("moda/a/a.go")
376382
env.Await(env.DoneWithOpen())
@@ -380,13 +386,12 @@ func Hello() int {
380386
t.Errorf("expected %s, got %v", want, original)
381387
}
382388
env.CloseBuffer(original)
383-
env.Await(env.DoneWithClose())
389+
env.AfterChange()
384390

385391
env.RemoveWorkspaceFile("modb/b/b.go")
386392
env.RemoveWorkspaceFile("modb/go.mod")
387-
env.Await(
388-
env.DoneWithChangeWatchedFiles(),
389-
)
393+
env.WriteWorkspaceFile("go.work", "go 1.18\nuse moda/a")
394+
env.AfterChange()
390395

391396
got, _ := env.GoToDefinition("moda/a/a.go", env.RegexpSearch("moda/a/a.go", "Hello"))
392397
if want := "[email protected]/b/b.go"; !strings.HasSuffix(got, want) {
@@ -398,7 +403,14 @@ func Hello() int {
398403
// Tests that the version of the module used changes after it has been added
399404
// to the workspace.
400405
func TestCreateModule_Interdependent(t *testing.T) {
406+
testenv.NeedsGo1Point(t, 18) // uses go.work
401407
const multiModule = `
408+
-- go.work --
409+
go 1.18
410+
411+
use (
412+
moda/a
413+
)
402414
-- moda/a/go.mod --
403415
module a.com
404416
@@ -419,7 +431,6 @@ func main() {
419431
}
420432
`
421433
WithOptions(
422-
Modes(Experimental),
423434
ProxyFiles(workspaceModuleProxy),
424435
).Run(t, multiModule, func(t *testing.T, env *Env) {
425436
env.OpenFile("moda/a/a.go")
@@ -429,6 +440,13 @@ func main() {
429440
}
430441
env.CloseBuffer(original)
431442
env.WriteWorkspaceFiles(map[string]string{
443+
"go.work": `go 1.18
444+
445+
use (
446+
moda/a
447+
modb
448+
)
449+
`,
432450
"modb/go.mod": "module b.com",
433451
"modb/b/b.go": `package b
434452
@@ -437,12 +455,7 @@ func Hello() int {
437455
}
438456
`,
439457
})
440-
env.Await(
441-
OnceMet(
442-
env.DoneWithChangeWatchedFiles(),
443-
env.DiagnosticAtRegexp("modb/b/b.go", "x"),
444-
),
445-
)
458+
env.AfterChange(env.DiagnosticAtRegexp("modb/b/b.go", "x"))
446459
got, _ := env.GoToDefinition("moda/a/a.go", env.RegexpSearch("moda/a/a.go", "Hello"))
447460
if want := "modb/b/b.go"; !strings.HasSuffix(got, want) {
448461
t.Errorf("expected %s, got %v", want, original)
@@ -453,7 +466,17 @@ func Hello() int {
453466
// This test confirms that a gopls workspace can recover from initialization
454467
// with one invalid module.
455468
func TestOneBrokenModule(t *testing.T) {
469+
t.Skip("golang/go#55331: this test is temporarily broken as go.work handling tries to build the workspace module")
470+
471+
testenv.NeedsGo1Point(t, 18) // uses go.work
456472
const multiModule = `
473+
-- go.work --
474+
go 1.18
475+
476+
use (
477+
moda/a
478+
modb
479+
)
457480
-- moda/a/go.mod --
458481
module a.com
459482
@@ -482,7 +505,6 @@ func Hello() int {
482505
`
483506
WithOptions(
484507
ProxyFiles(workspaceModuleProxy),
485-
Modes(Experimental),
486508
).Run(t, multiModule, func(t *testing.T, env *Env) {
487509
env.OpenFile("modb/go.mod")
488510
env.Await(
@@ -941,8 +963,32 @@ var _ = fmt.Printf
941963
})
942964
}
943965

944-
func TestMultiModuleV2(t *testing.T) {
966+
func TestGoWork_V2Module(t *testing.T) {
967+
testenv.NeedsGo1Point(t, 18) // uses go.work
968+
// When using a go.work, we must have proxy content even if it is replaced.
969+
const proxy = `
970+
-- b.com/[email protected]/go.mod --
971+
module b.com/v2
972+
973+
go 1.12
974+
-- b.com/[email protected]/b/b.go --
975+
package b
976+
977+
func Ciao()() int {
978+
return 0
979+
}
980+
`
981+
945982
const multiModule = `
983+
-- go.work --
984+
go 1.18
985+
986+
use (
987+
moda/a
988+
modb
989+
modb/v2
990+
modc
991+
)
946992
-- moda/a/go.mod --
947993
module a.com
948994
@@ -985,22 +1031,42 @@ func main() {
9851031
var x int
9861032
}
9871033
`
1034+
9881035
WithOptions(
989-
Modes(Experimental),
1036+
ProxyFiles(proxy),
9901037
).Run(t, multiModule, func(t *testing.T, env *Env) {
9911038
env.Await(
992-
env.DiagnosticAtRegexp("moda/a/a.go", "x"),
993-
env.DiagnosticAtRegexp("modb/b/b.go", "x"),
994-
env.DiagnosticAtRegexp("modb/v2/b/b.go", "x"),
995-
env.DiagnosticAtRegexp("modc/main.go", "x"),
1039+
OnceMet(
1040+
InitialWorkspaceLoad,
1041+
// TODO(rfindley): assert on the full set of diagnostics here. We
1042+
// should ensure that we don't have a diagnostic at b.Hi in a.go.
1043+
env.DiagnosticAtRegexp("moda/a/a.go", "x"),
1044+
env.DiagnosticAtRegexp("modb/b/b.go", "x"),
1045+
env.DiagnosticAtRegexp("modb/v2/b/b.go", "x"),
1046+
env.DiagnosticAtRegexp("modc/main.go", "x"),
1047+
),
9961048
)
9971049
})
9981050
}
9991051

10001052
// Confirm that a fix for a tidy module will correct all modules in the
10011053
// workspace.
10021054
func TestMultiModule_OneBrokenModule(t *testing.T) {
1003-
const mod = `
1055+
// In the earlier 'experimental workspace mode', gopls would aggregate go.sum
1056+
// entries for the workspace module, allowing it to correctly associate
1057+
// missing go.sum with diagnostics. With go.work files, this doesn't work:
1058+
// the go.command will happily write go.work.sum.
1059+
t.Skip("golang/go#57509: go.mod diagnostics do not work in go.work mode")
1060+
testenv.NeedsGo1Point(t, 18) // uses go.work
1061+
const files = `
1062+
-- go.work --
1063+
go 1.18
1064+
1065+
use (
1066+
a
1067+
b
1068+
)
1069+
-- go.work.sum --
10041070
-- a/go.mod --
10051071
module a.com
10061072
@@ -1027,8 +1093,7 @@ func main() {
10271093
`
10281094
WithOptions(
10291095
ProxyFiles(workspaceProxy),
1030-
Modes(Experimental),
1031-
).Run(t, mod, func(t *testing.T, env *Env) {
1096+
).Run(t, files, func(t *testing.T, env *Env) {
10321097
params := &protocol.PublishDiagnosticsParams{}
10331098
env.OpenFile("b/go.mod")
10341099
env.Await(

0 commit comments

Comments
 (0)