Skip to content

Commit 2caf765

Browse files
committed
internal/lsp/regtest: add a test that reproduces golang/go#38878
This test partially reproduces some strange behavior with creating new tests files. In particular, it creates a new x test in a package that already has a test variant and adds content with a missing import. In the test, the import is never added. However, in my own experience debugging this in VS Code, I see the import get added but the diagnostic never get removed. One thing at a time though... Updates golang/go#39315 Change-Id: I724a145688b915d04abd1f21efc6f9a7506be043 Reviewed-on: https://go-review.googlesource.com/c/tools/+/235581 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 52effbd commit 2caf765

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

internal/lsp/regtest/diagnostics_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -730,3 +730,76 @@ func _() {
730730
)
731731
})
732732
}
733+
734+
// This test tries to replicate the workflow of a user creating a new x test.
735+
// It also tests golang/go#39315.
736+
func TestManuallyCreatingXTest(t *testing.T) {
737+
t.Skipf("See golang/go#39315")
738+
739+
// Only for 1.15 because of golang/go#37971.
740+
testenv.NeedsGo1Point(t, 15)
741+
742+
// Create a package that already has a test variant (in-package test).
743+
const testVariant = `
744+
-- go.mod --
745+
module mod.com
746+
747+
go 1.15
748+
-- hello/hello.go --
749+
package hello
750+
751+
func Hello() {
752+
var x int
753+
}
754+
-- hello/hello_test.go --
755+
package hello
756+
757+
import "testing"
758+
759+
func TestHello(t *testing.T) {
760+
var x int
761+
Hello()
762+
}
763+
`
764+
runner.Run(t, testVariant, func(t *testing.T, env *Env) {
765+
// Open the file, triggering the workspace load.
766+
// There are errors in the code to ensure all is working as expected.
767+
env.OpenFile("hello/hello.go")
768+
env.Await(
769+
env.DiagnosticAtRegexp("hello/hello.go", "x"),
770+
env.DiagnosticAtRegexp("hello/hello_test.go", "x"),
771+
)
772+
773+
// Create an empty file with the intention of making it an x test.
774+
// This resembles a typical flow in an editor like VS Code, in which
775+
// a user would create an empty file and add content, saving
776+
// intermittently.
777+
// TODO(rstambler): There might be more edge cases here, as file
778+
// content can be added incrementally.
779+
env.CreateBuffer("hello/hello_x_test.go", ``)
780+
781+
// Save the empty file (no actions since formatting will fail).
782+
env.Editor.SaveBufferWithoutActions(env.Ctx, "hello/hello_x_test.go")
783+
784+
// Add the content. The missing import is for the package under test.
785+
env.EditBuffer("hello/hello_x_test.go", fake.NewEdit(0, 0, 0, 0, `package hello_test
786+
787+
import (
788+
"testing"
789+
)
790+
791+
func TestHello(t *testing.T) {
792+
hello.Hello()
793+
}
794+
`))
795+
// Expect a diagnostic for the missing import. Save, which should
796+
// trigger import organization. The diagnostic should clear.
797+
env.Await(
798+
env.DiagnosticAtRegexp("hello/hello_x_test.go", "hello.Hello"),
799+
)
800+
env.SaveBuffer("hello/hello_x_test.go")
801+
env.Await(
802+
EmptyDiagnostics("hello/hello_x_test.go"),
803+
)
804+
})
805+
}

0 commit comments

Comments
 (0)