1
0
mirror of https://github.com/golang/go synced 2024-11-18 15:04:44 -07:00
go/internal/lsp/regtest/generate_test.go
Rob Findley 1d9c21a7db internal/lsp/regtest: add support for testing generate commands
Our editor interaction for running `go generate` was untested. Add
support for triggering generate from the fake editor, and a simple test.

To enable this, some helpers were added to list Workspace files and
check for file state changes, to avoid having to synthetically create
file events. This workaround is not ideal as it results in a leaky
abstraction: in other cases the regtest may assume that FileEvents are
triggered by workspace interactions (e.g. ws.WriteFile), but in this
case it cannot. Unfortunately the only real solution for this would be
to make file watching more realistic, by polling file state on an
interval or using an actual file watching library. Neither of those
options seemed worthwhile just to keep the fake.Editor API pristine.

A new debugging option is added, SkipCleanup, to allow inspecting
regtest working directories after a test with minimal code change.

Change-Id: I64dceeb21a4eb9eff2b6936e44f80f4bd24b82da
Reviewed-on: https://go-review.googlesource.com/c/tools/+/230313
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-05-04 19:25:30 +00:00

55 lines
1013 B
Go

// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// TODO(rfindley): figure out why go generate fails on android builders.
// +build !android
package regtest
import (
"testing"
"golang.org/x/tools/internal/lsp"
)
func TestGenerateProgress(t *testing.T) {
const generatedWorkspace = `
-- go.mod --
module fake.test
go 1.14
-- generate.go --
// +build ignore
package main
import "io/ioutil"
func main() {
ioutil.WriteFile("generated.go", []byte("package lib\n\nconst answer = 42"), 0644)
}
-- lib.go --
package lib
func GetAnswer() int {
return answer
}
//go:generate go run generate.go
`
runner.Run(t, generatedWorkspace, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexp("lib.go", "answer"),
)
env.RunGenerate(".")
env.Await(
OnceMet(
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChangeWatchedFiles), 1),
EmptyDiagnostics("lib.go")),
)
})
}