mirror of
https://github.com/golang/go
synced 2024-11-19 05:04:43 -07:00
8e7acdbce8
Many tool features, particularly modules-related, require particular Go versions. Build tags are unwieldy, requiring one-off test files which break up test organization. Add a suite of testenv functions that check what Go version is in use. Note that this is the logical Go version, as denoted by the release tags; it should be updated at the beginning of the release cycle per issue golang/go#38704. For ease of reviewing, I'll merge/delete files in a followup CL. Change-Id: Id85ce0f83387b3c45d68465161cf88447325d4f2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/234882 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
165 lines
4.0 KiB
Go
165 lines
4.0 KiB
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.
|
|
|
|
package regtest
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"golang.org/x/tools/internal/lsp"
|
|
"golang.org/x/tools/internal/lsp/fake"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/testenv"
|
|
)
|
|
|
|
const ardanLabsProxy = `
|
|
-- github.com/ardanlabs/conf@v1.2.3/go.mod --
|
|
module github.com/ardanlabs/conf
|
|
|
|
go 1.12
|
|
-- github.com/ardanlabs/conf@v1.2.3/conf.go --
|
|
package conf
|
|
|
|
var ErrHelpWanted error
|
|
`
|
|
|
|
// -modfile flag that is used to provide modfile diagnostics is only available
|
|
// with 1.14.
|
|
func Test_Issue38211(t *testing.T) {
|
|
testenv.NeedsGo1Point(t, 14)
|
|
const ardanLabs = `
|
|
-- go.mod --
|
|
module mod.com
|
|
|
|
go 1.14
|
|
-- main.go --
|
|
package main
|
|
|
|
import "github.com/ardanlabs/conf"
|
|
|
|
func main() {
|
|
_ = conf.ErrHelpWanted
|
|
}
|
|
`
|
|
runner.Run(t, ardanLabs, func(t *testing.T, env *Env) {
|
|
// Expect a diagnostic with a suggested fix to add
|
|
// "github.com/ardanlabs/conf" to the go.mod file.
|
|
env.OpenFile("go.mod")
|
|
env.OpenFile("main.go")
|
|
metBy := env.Await(
|
|
env.DiagnosticAtRegexp("main.go", `"github.com/ardanlabs/conf"`),
|
|
)
|
|
d, ok := metBy[0].(*protocol.PublishDiagnosticsParams)
|
|
if !ok {
|
|
t.Fatalf("unexpected type for metBy (%T)", metBy)
|
|
}
|
|
env.ApplyQuickFixes("main.go", d.Diagnostics)
|
|
env.SaveBuffer("go.mod")
|
|
env.Await(
|
|
EmptyDiagnostics("main.go"),
|
|
)
|
|
// Comment out the line that depends on conf and expect a
|
|
// diagnostic and a fix to remove the import.
|
|
env.RegexpReplace("main.go", "_ = conf.ErrHelpWanted", "//_ = conf.ErrHelpWanted")
|
|
env.Await(
|
|
env.DiagnosticAtRegexp("main.go", `"github.com/ardanlabs/conf"`),
|
|
)
|
|
env.SaveBuffer("main.go")
|
|
// Expect a diagnostic and fix to remove the dependency in the go.mod.
|
|
metBy = env.Await(
|
|
EmptyDiagnostics("main.go"),
|
|
env.DiagnosticAtRegexp("go.mod", "require github.com/ardanlabs/conf"),
|
|
)
|
|
d, ok = metBy[1].(*protocol.PublishDiagnosticsParams)
|
|
if !ok {
|
|
t.Fatalf("unexpected type for metBy (%T)", metBy)
|
|
}
|
|
env.ApplyQuickFixes("go.mod", d.Diagnostics)
|
|
env.SaveBuffer("go.mod")
|
|
env.Await(
|
|
EmptyDiagnostics("go.mod"),
|
|
)
|
|
// Uncomment the lines and expect a new diagnostic for the import.
|
|
env.RegexpReplace("main.go", "//_ = conf.ErrHelpWanted", "_ = conf.ErrHelpWanted")
|
|
env.SaveBuffer("main.go")
|
|
env.Await(
|
|
env.DiagnosticAtRegexp("main.go", `"github.com/ardanlabs/conf"`),
|
|
)
|
|
}, WithProxy(ardanLabsProxy))
|
|
}
|
|
|
|
// Test for golang/go#38207.
|
|
func TestNewModule_Issue38207(t *testing.T) {
|
|
testenv.NeedsGo1Point(t, 14)
|
|
const emptyFile = `
|
|
-- go.mod --
|
|
module mod.com
|
|
|
|
go 1.12
|
|
-- main.go --
|
|
`
|
|
runner.Run(t, emptyFile, func(t *testing.T, env *Env) {
|
|
env.OpenFile("main.go")
|
|
env.OpenFile("go.mod")
|
|
env.EditBuffer("main.go", fake.NewEdit(0, 0, 0, 0, `package main
|
|
|
|
import "github.com/ardanlabs/conf"
|
|
|
|
func main() {
|
|
_ = conf.ErrHelpWanted
|
|
}
|
|
`))
|
|
env.SaveBuffer("main.go")
|
|
metBy := env.Await(
|
|
env.DiagnosticAtRegexp("main.go", `"github.com/ardanlabs/conf"`),
|
|
)
|
|
d, ok := metBy[0].(*protocol.PublishDiagnosticsParams)
|
|
if !ok {
|
|
t.Fatalf("unexpected type for diagnostics (%T)", d)
|
|
}
|
|
env.ApplyQuickFixes("main.go", d.Diagnostics)
|
|
env.Await(
|
|
EmptyDiagnostics("main.go"),
|
|
)
|
|
}, WithProxy(ardanLabsProxy))
|
|
}
|
|
|
|
func TestNewFileBadImports_Issue36960(t *testing.T) {
|
|
testenv.NeedsGo1Point(t, 14)
|
|
const simplePackage = `
|
|
-- go.mod --
|
|
module mod.com
|
|
|
|
go 1.14
|
|
-- a/a1.go --
|
|
package a
|
|
|
|
import "fmt"
|
|
|
|
func _() {
|
|
fmt.Println("hi")
|
|
}
|
|
`
|
|
runner.Run(t, simplePackage, func(t *testing.T, env *Env) {
|
|
env.OpenFile("a/a1.go")
|
|
env.CreateBuffer("a/a2.go", ``)
|
|
if err := env.Editor.SaveBufferWithoutActions(env.Ctx, "a/a2.go"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
env.Await(
|
|
OnceMet(
|
|
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidSave), 1),
|
|
NoDiagnostics("a/a1.go"),
|
|
),
|
|
)
|
|
env.EditBuffer("a/a2.go", fake.NewEdit(0, 0, 0, 0, `package a`))
|
|
env.Await(
|
|
OnceMet(
|
|
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChange), 1),
|
|
NoDiagnostics("a/a1.go"),
|
|
),
|
|
)
|
|
})
|
|
}
|