2020-02-06 17:50:37 -07: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 (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const exampleProgram = `
|
|
|
|
-- go.mod --
|
|
|
|
module mod
|
|
|
|
|
|
|
|
go 1.12
|
|
|
|
-- main.go --
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("Hello World.")
|
|
|
|
}`
|
|
|
|
|
|
|
|
func TestDiagnosticErrorInEditedFile(t *testing.T) {
|
2020-03-23 15:26:05 -06:00
|
|
|
runner.Run(t, exampleProgram, func(env *Env) {
|
2020-02-06 17:50:37 -07:00
|
|
|
// Deleting the 'n' at the end of Println should generate a single error
|
|
|
|
// diagnostic.
|
|
|
|
env.OpenFile("main.go")
|
2020-02-28 12:56:20 -07:00
|
|
|
env.RegexpReplace("main.go", "Printl(n)", "")
|
|
|
|
env.Await(env.DiagnosticAtRegexp("main.go", "Printl"))
|
2020-02-06 17:50:37 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:15:53 -07:00
|
|
|
const onlyMod = `
|
|
|
|
-- go.mod --
|
|
|
|
module mod
|
|
|
|
|
|
|
|
go 1.12
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestMissingImportDiagsClearOnFirstFile(t *testing.T) {
|
|
|
|
t.Skip("skipping due to golang.org/issues/37195")
|
|
|
|
t.Parallel()
|
2020-03-23 15:26:05 -06:00
|
|
|
runner.Run(t, onlyMod, func(env *Env) {
|
2020-02-26 15:15:53 -07:00
|
|
|
env.CreateBuffer("main.go", "package main\n\nfunc m() {\nlog.Println()\n}")
|
|
|
|
env.SaveBuffer("main.go")
|
|
|
|
// TODO: this shouldn't actually happen
|
2020-02-28 12:56:20 -07:00
|
|
|
env.Await(env.DiagnosticAtRegexp("main.go", "Println"))
|
2020-02-26 15:15:53 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-06 17:50:37 -07:00
|
|
|
const brokenFile = `package main
|
|
|
|
|
|
|
|
const Foo = "abc
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestDiagnosticErrorInNewFile(t *testing.T) {
|
2020-03-23 15:26:05 -06:00
|
|
|
runner.Run(t, brokenFile, func(env *Env) {
|
2020-02-06 17:50:37 -07:00
|
|
|
env.CreateBuffer("broken.go", brokenFile)
|
2020-02-28 12:56:20 -07:00
|
|
|
env.Await(env.DiagnosticAtRegexp("broken.go", "\"abc"))
|
2020-02-06 17:50:37 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// badPackage contains a duplicate definition of the 'a' const.
|
|
|
|
const badPackage = `
|
|
|
|
-- go.mod --
|
|
|
|
module mod
|
|
|
|
|
|
|
|
go 1.12
|
|
|
|
-- a.go --
|
|
|
|
package consts
|
|
|
|
|
|
|
|
const a = 1
|
|
|
|
-- b.go --
|
|
|
|
package consts
|
|
|
|
|
|
|
|
const a = 2
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestDiagnosticClearingOnEdit(t *testing.T) {
|
2020-03-23 15:26:05 -06:00
|
|
|
runner.Run(t, badPackage, func(env *Env) {
|
2020-02-06 17:50:37 -07:00
|
|
|
env.OpenFile("b.go")
|
2020-02-28 12:56:20 -07:00
|
|
|
env.Await(env.DiagnosticAtRegexp("a.go", "a = 1"), env.DiagnosticAtRegexp("b.go", "a = 2"))
|
2020-02-06 17:50:37 -07:00
|
|
|
|
|
|
|
// Fix the error by editing the const name in b.go to `b`.
|
2020-02-28 12:56:20 -07:00
|
|
|
env.RegexpReplace("b.go", "(a) = 2", "b")
|
2020-02-06 17:50:37 -07:00
|
|
|
env.Await(EmptyDiagnostics("a.go"), EmptyDiagnostics("b.go"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiagnosticClearingOnDelete(t *testing.T) {
|
2020-03-23 15:26:05 -06:00
|
|
|
runner.Run(t, badPackage, func(env *Env) {
|
2020-02-06 17:50:37 -07:00
|
|
|
env.OpenFile("a.go")
|
2020-02-28 12:56:20 -07:00
|
|
|
env.Await(env.DiagnosticAtRegexp("a.go", "a = 1"), env.DiagnosticAtRegexp("b.go", "a = 2"))
|
2020-02-06 17:50:37 -07:00
|
|
|
env.RemoveFileFromWorkspace("b.go")
|
|
|
|
|
|
|
|
env.Await(EmptyDiagnostics("a.go"), EmptyDiagnostics("b.go"))
|
|
|
|
})
|
|
|
|
}
|
2020-02-19 12:25:12 -07:00
|
|
|
|
|
|
|
func TestDiagnosticClearingOnClose(t *testing.T) {
|
2020-03-23 15:26:05 -06:00
|
|
|
runner.Run(t, badPackage, func(env *Env) {
|
2020-02-25 09:00:37 -07:00
|
|
|
env.CreateBuffer("c.go", `package consts
|
2020-02-19 12:25:12 -07:00
|
|
|
|
|
|
|
const a = 3`)
|
2020-02-28 12:56:20 -07:00
|
|
|
env.Await(
|
|
|
|
env.DiagnosticAtRegexp("a.go", "a = 1"),
|
|
|
|
env.DiagnosticAtRegexp("b.go", "a = 2"),
|
|
|
|
env.DiagnosticAtRegexp("c.go", "a = 3"))
|
2020-02-25 09:00:37 -07:00
|
|
|
env.CloseBuffer("c.go")
|
2020-02-28 12:56:20 -07:00
|
|
|
env.Await(
|
|
|
|
env.DiagnosticAtRegexp("a.go", "a = 1"),
|
|
|
|
env.DiagnosticAtRegexp("b.go", "a = 2"),
|
|
|
|
EmptyDiagnostics("c.go"))
|
2020-02-19 12:25:12 -07:00
|
|
|
})
|
|
|
|
}
|