1
0
mirror of https://github.com/golang/go synced 2024-11-19 04:44:41 -07:00
go/internal/lsp/regtest/cgo_test.go
Heschi Kreinick d3bf790afa internal/lsp: add Regenerate Cgo code lens
Now that we support authoring cgo packages better, we need a way to
regenerate the C definitions. Doing it automatically is very difficult;
in particular, referencing a new symbol from the C package may require
regeneration, but we don't want to do that for every typo.

For now, give the user a button and make them push it. We attach a
code lens to the import "C" line. This is vulnerable to the usual
user-didn't-save glitches.

Updates golang/go#35721.

Change-Id: Iaa3540a9a12bbd8705e7f0e43ad0be1b22e87067
Reviewed-on: https://go-review.googlesource.com/c/tools/+/234103
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2020-05-15 22:01:28 +00:00

61 lines
1.5 KiB
Go

//+build go1.15,cgo
package regtest
import (
"runtime"
"testing"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
)
func TestRegenerateCgo(t *testing.T) {
// The android builders have a complex setup which causes this test to fail. See discussion on
// golang.org/cl/214943 for more details.
if runtime.GOOS == "android" {
t.Skip("android not supported")
}
const workspace = `
-- go.mod --
module example.com
-- cgo.go --
package x
/*
int fortythree() { return 42; }
*/
import "C"
func Foo() {
print(C.fortytwo())
}
`
runner.Run(t, workspace, func(t *testing.T, env *Env) {
// Open the file. We should have a nonexistant symbol.
env.OpenFile("cgo.go")
env.Await(env.DiagnosticAtRegexp("cgo.go", `C\.(fortytwo)`)) // could not determine kind of name for C.fortytwo
// Fix the C function name. We haven't regenerated cgo, so nothing should be fixed.
env.RegexpReplace("cgo.go", `int fortythree`, "int fortytwo")
env.SaveBuffer("cgo.go")
env.Await(env.DiagnosticAtRegexp("cgo.go", `C\.(fortytwo)`))
// Regenerate cgo, fixing the diagnostic.
lenses := env.CodeLens("cgo.go")
var lens protocol.CodeLens
for _, l := range lenses {
if l.Command.Command == source.CommandRegenerateCgo {
lens = l
}
}
if _, err := env.Editor.Server.ExecuteCommand(env.Ctx, &protocol.ExecuteCommandParams{
Command: lens.Command.Command,
Arguments: lens.Command.Arguments,
}); err != nil {
t.Fatal(err)
}
env.Await(EmptyDiagnostics("cgo.go"))
})
}