2020-07-20 23:34:22 -06:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package regtest
|
|
|
|
|
|
|
|
import (
|
2020-07-28 16:18:43 -06:00
|
|
|
"fmt"
|
2020-07-20 23:34:22 -06:00
|
|
|
"testing"
|
2020-07-28 16:18:43 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp"
|
2020-07-20 23:34:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const workspaceProxy = `
|
|
|
|
-- example.com@v1.2.3/go.mod --
|
|
|
|
module example.com
|
|
|
|
|
|
|
|
go 1.12
|
|
|
|
-- example.com@v1.2.3/blah/blah.go --
|
|
|
|
package blah
|
|
|
|
|
|
|
|
func SaySomething() {
|
|
|
|
fmt.Println("something")
|
|
|
|
}
|
2020-07-28 16:18:43 -06:00
|
|
|
-- random.org@v1.2.3/go.mod --
|
|
|
|
module random.org
|
|
|
|
|
|
|
|
go 1.12
|
|
|
|
-- random.org@v1.2.3/bye/bye.go --
|
|
|
|
package bye
|
|
|
|
|
|
|
|
func Goodbye() {
|
|
|
|
println("Bye")
|
|
|
|
}
|
2020-07-20 23:34:22 -06:00
|
|
|
`
|
|
|
|
|
|
|
|
// TODO: Add a replace directive.
|
|
|
|
const workspaceModule = `
|
2020-07-28 16:18:43 -06:00
|
|
|
-- pkg/go.mod --
|
2020-07-20 23:34:22 -06:00
|
|
|
module mod.com
|
|
|
|
|
|
|
|
go 1.14
|
|
|
|
|
2020-07-28 16:18:43 -06:00
|
|
|
require (
|
|
|
|
example.com v1.2.3
|
|
|
|
random.org v1.2.3
|
|
|
|
)
|
|
|
|
-- pkg/main.go --
|
2020-07-20 23:34:22 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"example.com/blah"
|
|
|
|
"mod.com/inner"
|
2020-07-28 16:18:43 -06:00
|
|
|
"random.org/bye"
|
2020-07-20 23:34:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
blah.SaySomething()
|
|
|
|
inner.Hi()
|
2020-07-28 16:18:43 -06:00
|
|
|
bye.Goodbye()
|
2020-07-20 23:34:22 -06:00
|
|
|
}
|
2020-07-28 16:18:43 -06:00
|
|
|
-- pkg/main2.go --
|
2020-07-20 23:34:22 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func _() {
|
|
|
|
fmt.Print("%s")
|
|
|
|
}
|
2020-07-28 16:18:43 -06:00
|
|
|
-- pkg/inner/inner.go --
|
2020-07-20 23:34:22 -06:00
|
|
|
package inner
|
|
|
|
|
|
|
|
import "example.com/blah"
|
|
|
|
|
|
|
|
func Hi() {
|
|
|
|
blah.SaySomething()
|
|
|
|
}
|
2020-07-28 16:18:43 -06:00
|
|
|
-- goodbye/bye/bye.go --
|
|
|
|
package bye
|
|
|
|
|
|
|
|
func Bye() {}
|
|
|
|
-- goodbye/go.mod --
|
|
|
|
module random.org
|
|
|
|
|
|
|
|
go 1.12
|
2020-07-20 23:34:22 -06:00
|
|
|
`
|
|
|
|
|
|
|
|
// Confirm that find references returns all of the references in the module,
|
|
|
|
// regardless of what the workspace root is.
|
|
|
|
func TestReferences(t *testing.T) {
|
|
|
|
for _, tt := range []struct {
|
|
|
|
name, rootPath string
|
|
|
|
}{
|
|
|
|
{
|
2020-07-28 16:18:43 -06:00
|
|
|
name: "module root",
|
|
|
|
rootPath: "pkg",
|
2020-07-20 23:34:22 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "subdirectory",
|
2020-07-28 16:18:43 -06:00
|
|
|
rootPath: "pkg/inner",
|
2020-07-20 23:34:22 -06:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-07-22 19:15:22 -06:00
|
|
|
opts := []RunOption{WithProxyFiles(workspaceProxy)}
|
2020-07-20 23:34:22 -06:00
|
|
|
if tt.rootPath != "" {
|
|
|
|
opts = append(opts, WithRootPath(tt.rootPath))
|
|
|
|
}
|
|
|
|
withOptions(opts...).run(t, workspaceModule, func(t *testing.T, env *Env) {
|
2020-08-17 12:36:26 -06:00
|
|
|
f := "pkg/inner/inner.go"
|
|
|
|
env.OpenFile(f)
|
|
|
|
locations := env.References(f, env.RegexpSearch(f, `SaySomething`))
|
2020-07-20 23:34:22 -06:00
|
|
|
want := 3
|
|
|
|
if got := len(locations); got != want {
|
|
|
|
t.Fatalf("expected %v locations, got %v", want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that analysis diagnostics are cleared for the whole package when
|
|
|
|
// the only opened file is closed. This test was inspired by the experience in
|
|
|
|
// VS Code, where clicking on a reference result triggers a
|
|
|
|
// textDocument/didOpen without a corresponding textDocument/didClose.
|
|
|
|
func TestClearAnalysisDiagnostics(t *testing.T) {
|
2020-07-28 16:18:43 -06:00
|
|
|
withOptions(WithProxyFiles(workspaceProxy), WithRootPath("pkg/inner")).run(t, workspaceModule, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("pkg/main.go")
|
|
|
|
env.Await(
|
|
|
|
env.DiagnosticAtRegexp("pkg/main2.go", "fmt.Print"),
|
|
|
|
)
|
|
|
|
env.CloseBuffer("pkg/main.go")
|
|
|
|
env.Await(
|
|
|
|
EmptyDiagnostics("pkg/main2.go"),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks that gopls updates the set of files it watches when a
|
|
|
|
// replace target is added to the go.mod.
|
|
|
|
func TestWatchReplaceTargets(t *testing.T) {
|
|
|
|
withOptions(WithProxyFiles(workspaceProxy), WithRootPath("pkg")).run(t, workspaceModule, func(t *testing.T, env *Env) {
|
2020-07-20 23:34:22 -06:00
|
|
|
env.Await(
|
2020-07-28 16:18:43 -06:00
|
|
|
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
|
2020-07-20 23:34:22 -06:00
|
|
|
)
|
2020-07-28 16:18:43 -06:00
|
|
|
// Add a replace directive and expect the files that gopls is watching
|
|
|
|
// to change.
|
|
|
|
dir := env.Sandbox.Workdir.URI("goodbye").SpanURI().Filename()
|
|
|
|
goModWithReplace := fmt.Sprintf(`%s
|
|
|
|
replace random.org => %s
|
|
|
|
`, env.ReadWorkspaceFile("pkg/go.mod"), dir)
|
|
|
|
env.WriteWorkspaceFile("pkg/go.mod", goModWithReplace)
|
2020-07-20 23:34:22 -06:00
|
|
|
env.Await(
|
2020-07-28 16:18:43 -06:00
|
|
|
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChangeWatchedFiles), 1),
|
|
|
|
UnregistrationMatching("didChangeWatchedFiles"),
|
|
|
|
RegistrationMatching("didChangeWatchedFiles"),
|
2020-07-20 23:34:22 -06:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|