mirror of
https://github.com/golang/go
synced 2024-11-18 16:04:44 -07:00
9c79f685b7
https://github.com/microsoft/vscode-go/issues/3076#issuecomment-605062933 inspired me to write a regression test for this case. Turns out we weren't handling it correctly after all... This change makes sure that we only rebuild the view once a new go.mod file is saved, not just created. It also preserves the snapshot ID number when the view is recreated so that diagnostic caching continues to work as expected. Change-Id: I63bd559c3bd33b91828171cd7ddb3d099c31cddb Reviewed-on: https://go-review.googlesource.com/c/tools/+/226017 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
186 lines
4.1 KiB
Go
186 lines
4.1 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/fake"
|
|
)
|
|
|
|
// Use mod.com for all go.mod files due to golang/go#35230.
|
|
const exampleProgram = `
|
|
-- go.mod --
|
|
module mod.com
|
|
|
|
go 1.12
|
|
-- main.go --
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
fmt.Println("Hello World.")
|
|
}`
|
|
|
|
func TestDiagnosticErrorInEditedFile(t *testing.T) {
|
|
runner.Run(t, exampleProgram, func(env *Env) {
|
|
// Deleting the 'n' at the end of Println should generate a single error
|
|
// diagnostic.
|
|
env.OpenFile("main.go")
|
|
env.RegexpReplace("main.go", "Printl(n)", "")
|
|
env.Await(env.DiagnosticAtRegexp("main.go", "Printl"))
|
|
})
|
|
}
|
|
|
|
const onlyMod = `
|
|
-- go.mod --
|
|
module mod.com
|
|
|
|
go 1.12
|
|
`
|
|
|
|
func TestMissingImportDiagsClearOnFirstFile(t *testing.T) {
|
|
t.Skip("skipping due to golang.org/issues/37195")
|
|
t.Parallel()
|
|
runner.Run(t, onlyMod, func(env *Env) {
|
|
env.CreateBuffer("main.go", "package main\n\nfunc m() {\nlog.Println()\n}")
|
|
env.SaveBuffer("main.go")
|
|
// TODO: this shouldn't actually happen
|
|
env.Await(env.DiagnosticAtRegexp("main.go", "Println"))
|
|
})
|
|
}
|
|
|
|
const brokenFile = `package main
|
|
|
|
const Foo = "abc
|
|
`
|
|
|
|
func TestDiagnosticErrorInNewFile(t *testing.T) {
|
|
runner.Run(t, brokenFile, func(env *Env) {
|
|
env.CreateBuffer("broken.go", brokenFile)
|
|
env.Await(env.DiagnosticAtRegexp("broken.go", "\"abc"))
|
|
})
|
|
}
|
|
|
|
// badPackage contains a duplicate definition of the 'a' const.
|
|
const badPackage = `
|
|
-- go.mod --
|
|
module mod.com
|
|
|
|
go 1.12
|
|
-- a.go --
|
|
package consts
|
|
|
|
const a = 1
|
|
-- b.go --
|
|
package consts
|
|
|
|
const a = 2
|
|
`
|
|
|
|
func TestDiagnosticClearingOnEdit(t *testing.T) {
|
|
runner.Run(t, badPackage, func(env *Env) {
|
|
env.OpenFile("b.go")
|
|
env.Await(env.DiagnosticAtRegexp("a.go", "a = 1"), env.DiagnosticAtRegexp("b.go", "a = 2"))
|
|
|
|
// Fix the error by editing the const name in b.go to `b`.
|
|
env.RegexpReplace("b.go", "(a) = 2", "b")
|
|
env.Await(EmptyDiagnostics("a.go"), EmptyDiagnostics("b.go"))
|
|
})
|
|
}
|
|
|
|
func TestDiagnosticClearingOnDelete(t *testing.T) {
|
|
runner.Run(t, badPackage, func(env *Env) {
|
|
env.OpenFile("a.go")
|
|
env.Await(env.DiagnosticAtRegexp("a.go", "a = 1"), env.DiagnosticAtRegexp("b.go", "a = 2"))
|
|
env.RemoveFileFromWorkspace("b.go")
|
|
|
|
env.Await(EmptyDiagnostics("a.go"), EmptyDiagnostics("b.go"))
|
|
})
|
|
}
|
|
|
|
func TestDiagnosticClearingOnClose(t *testing.T) {
|
|
runner.Run(t, badPackage, func(env *Env) {
|
|
env.CreateBuffer("c.go", `package consts
|
|
|
|
const a = 3`)
|
|
env.Await(
|
|
env.DiagnosticAtRegexp("a.go", "a = 1"),
|
|
env.DiagnosticAtRegexp("b.go", "a = 2"),
|
|
env.DiagnosticAtRegexp("c.go", "a = 3"))
|
|
env.CloseBuffer("c.go")
|
|
env.Await(
|
|
env.DiagnosticAtRegexp("a.go", "a = 1"),
|
|
env.DiagnosticAtRegexp("b.go", "a = 2"),
|
|
EmptyDiagnostics("c.go"))
|
|
})
|
|
}
|
|
|
|
func TestIssue37978(t *testing.T) {
|
|
runner.Run(t, exampleProgram, func(env *Env) {
|
|
// Create a new workspace-level directory and empty file.
|
|
env.CreateBuffer("c/c.go", "")
|
|
|
|
// Write the file contents with a missing import.
|
|
env.EditBuffer("c/c.go", fake.Edit{
|
|
Text: `package c
|
|
|
|
const a = http.MethodGet
|
|
`,
|
|
})
|
|
env.Await(
|
|
env.DiagnosticAtRegexp("c/c.go", "http.MethodGet"),
|
|
)
|
|
// Save file, which will organize imports, adding the expected import.
|
|
// Expect the diagnostics to clear.
|
|
env.SaveBuffer("c/c.go")
|
|
env.Await(
|
|
EmptyDiagnostics("c/c.go"),
|
|
)
|
|
})
|
|
}
|
|
|
|
const noMod = `
|
|
-- main.go --
|
|
package main
|
|
|
|
import "mod.com/bob"
|
|
|
|
func main() {
|
|
bob.Hello()
|
|
}
|
|
`
|
|
|
|
// TestNoMod confirms that gopls continues to work when a user adds a go.mod
|
|
// file to their workspace.
|
|
func TestNoMod(t *testing.T) {
|
|
runner.Run(t, noMod, func(env *Env) {
|
|
env.Await(
|
|
env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`),
|
|
)
|
|
env.CreateBuffer("bob/bob.go", `package bob
|
|
|
|
func Hello() {
|
|
var x int
|
|
}
|
|
`)
|
|
env.Await(
|
|
env.DiagnosticAtRegexp("bob/bob.go", "x"),
|
|
)
|
|
// Save this file because otherwise, the go command will not be able to
|
|
// resolve mod.com/bob as a package.
|
|
env.SaveBuffer("bob/bob.go")
|
|
env.CreateBuffer("go.mod", `module mod.com
|
|
|
|
go 1.12
|
|
`)
|
|
env.SaveBuffer("go.mod")
|
|
env.Await(
|
|
EmptyDiagnostics("main.go"),
|
|
)
|
|
})
|
|
}
|