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 (
|
2020-04-23 21:24:24 -06:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-02-06 17:50:37 -07:00
|
|
|
"testing"
|
2020-03-24 16:29:27 -06:00
|
|
|
|
2020-04-22 15:54:30 -06:00
|
|
|
"golang.org/x/tools/internal/lsp"
|
2020-03-24 16:29:27 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/fake"
|
2020-04-15 15:14:53 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2020-04-29 15:49:22 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/tests"
|
2020-02-06 17:50:37 -07:00
|
|
|
)
|
|
|
|
|
2020-03-24 16:29:27 -06:00
|
|
|
// Use mod.com for all go.mod files due to golang/go#35230.
|
2020-02-06 17:50:37 -07:00
|
|
|
const exampleProgram = `
|
|
|
|
-- go.mod --
|
2020-03-24 16:29:27 -06:00
|
|
|
module mod.com
|
2020-02-06 17:50:37 -07:00
|
|
|
|
|
|
|
go 1.12
|
|
|
|
-- main.go --
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("Hello World.")
|
|
|
|
}`
|
|
|
|
|
|
|
|
func TestDiagnosticErrorInEditedFile(t *testing.T) {
|
2020-04-24 12:22:13 -06:00
|
|
|
// This test is very basic: start with a clean Go program, make an error, and
|
|
|
|
// get a diagnostic for that error. However, it also demonstrates how to
|
|
|
|
// combine Expectations to await more complex state in the editor.
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, exampleProgram, func(t *testing.T, 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)", "")
|
2020-04-15 15:14:53 -06:00
|
|
|
env.Await(
|
2020-04-24 12:22:13 -06:00
|
|
|
// Once we have gotten diagnostics for the change above, we should
|
|
|
|
// satisfy the DiagnosticAtRegexp assertion.
|
|
|
|
OnceMet(
|
|
|
|
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChange), 1),
|
|
|
|
env.DiagnosticAtRegexp("main.go", "Printl"),
|
|
|
|
),
|
2020-04-15 15:14:53 -06:00
|
|
|
// Assert that this test has sent no error logs to the client. This is not
|
|
|
|
// strictly necessary for testing this regression, but is included here
|
|
|
|
// as an example of using the NoErrorLogs() expectation. Feel free to
|
|
|
|
// delete.
|
|
|
|
NoErrorLogs(),
|
|
|
|
)
|
2020-02-06 17:50:37 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:15:53 -07:00
|
|
|
const onlyMod = `
|
|
|
|
-- go.mod --
|
2020-03-24 16:29:27 -06:00
|
|
|
module mod.com
|
2020-02-26 15:15:53 -07:00
|
|
|
|
|
|
|
go 1.12
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestMissingImportDiagsClearOnFirstFile(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, onlyMod, func(t *testing.T, env *Env) {
|
2020-04-16 21:25:57 -06:00
|
|
|
env.CreateBuffer("main.go", `package main
|
|
|
|
|
|
|
|
func m() {
|
|
|
|
log.Println()
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
env.Await(
|
|
|
|
env.DiagnosticAtRegexp("main.go", "log"),
|
|
|
|
)
|
2020-02-26 15:15:53 -07:00
|
|
|
env.SaveBuffer("main.go")
|
2020-04-16 21:25:57 -06:00
|
|
|
env.Await(
|
|
|
|
EmptyDiagnostics("main.go"),
|
|
|
|
)
|
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-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, brokenFile, func(t *testing.T, 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 --
|
2020-03-24 16:29:27 -06:00
|
|
|
module mod.com
|
2020-02-06 17:50:37 -07:00
|
|
|
|
|
|
|
go 1.12
|
|
|
|
-- a.go --
|
|
|
|
package consts
|
|
|
|
|
|
|
|
const a = 1
|
|
|
|
-- b.go --
|
|
|
|
package consts
|
|
|
|
|
|
|
|
const a = 2
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestDiagnosticClearingOnEdit(t *testing.T) {
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, badPackage, func(t *testing.T, 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-04-15 15:14:53 -06:00
|
|
|
env.Await(
|
|
|
|
EmptyDiagnostics("a.go"),
|
|
|
|
EmptyDiagnostics("b.go"),
|
|
|
|
)
|
2020-02-06 17:50:37 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-01 15:14:17 -06:00
|
|
|
func TestDiagnosticClearingOnDelete_Issue37049(t *testing.T) {
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, badPackage, func(t *testing.T, 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-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, badPackage, func(t *testing.T, 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
|
|
|
})
|
|
|
|
}
|
2020-03-24 16:29:27 -06:00
|
|
|
|
2020-04-29 15:01:30 -06:00
|
|
|
// Tests golang/go#37978.
|
2020-03-24 16:29:27 -06:00
|
|
|
func TestIssue37978(t *testing.T) {
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, exampleProgram, func(t *testing.T, env *Env) {
|
2020-03-24 16:29:27 -06:00
|
|
|
// 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"),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2020-03-27 11:11:41 -06:00
|
|
|
|
2020-04-29 15:01:30 -06:00
|
|
|
// TestNoMod confirms that gopls continues to work when a user adds a go.mod
|
|
|
|
// file to their workspace.
|
|
|
|
func TestNoMod(t *testing.T) {
|
|
|
|
const noMod = `
|
2020-03-27 11:11:41 -06:00
|
|
|
-- main.go --
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "mod.com/bob"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
bob.Hello()
|
|
|
|
}
|
2020-03-30 14:26:04 -06:00
|
|
|
-- bob/bob.go --
|
|
|
|
package bob
|
2020-03-27 11:11:41 -06:00
|
|
|
|
|
|
|
func Hello() {
|
|
|
|
var x int
|
|
|
|
}
|
2020-03-30 14:26:04 -06:00
|
|
|
`
|
2020-03-27 11:11:41 -06:00
|
|
|
|
2020-03-30 14:26:04 -06:00
|
|
|
t.Run("manual", func(t *testing.T) {
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, noMod, func(t *testing.T, env *Env) {
|
2020-03-30 14:26:04 -06:00
|
|
|
env.Await(
|
|
|
|
env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`),
|
|
|
|
)
|
|
|
|
env.CreateBuffer("go.mod", `module mod.com
|
|
|
|
|
|
|
|
go 1.12
|
2020-03-27 11:11:41 -06:00
|
|
|
`)
|
2020-03-30 14:26:04 -06:00
|
|
|
env.SaveBuffer("go.mod")
|
|
|
|
env.Await(
|
|
|
|
EmptyDiagnostics("main.go"),
|
|
|
|
env.DiagnosticAtRegexp("bob/bob.go", "x"),
|
|
|
|
)
|
|
|
|
})
|
2020-03-27 11:11:41 -06:00
|
|
|
})
|
2020-03-30 14:26:04 -06:00
|
|
|
t.Run("initialized", func(t *testing.T) {
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, noMod, func(t *testing.T, env *Env) {
|
2020-03-30 14:26:04 -06:00
|
|
|
env.Await(
|
|
|
|
env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`),
|
|
|
|
)
|
2020-04-28 22:00:52 -06:00
|
|
|
if err := env.Sandbox.RunGoCommand(env.Ctx, "mod", "init", "mod.com"); err != nil {
|
2020-03-30 14:26:04 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
env.Await(
|
|
|
|
EmptyDiagnostics("main.go"),
|
|
|
|
env.DiagnosticAtRegexp("bob/bob.go", "x"),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2020-03-27 11:11:41 -06:00
|
|
|
}
|
2020-04-05 21:48:22 -06:00
|
|
|
|
2020-04-29 15:01:30 -06:00
|
|
|
// Tests golang/go#38267.
|
|
|
|
func TestIssue38267(t *testing.T) {
|
|
|
|
const testPackage = `
|
2020-04-05 21:48:22 -06:00
|
|
|
-- go.mod --
|
|
|
|
module mod.com
|
|
|
|
|
|
|
|
go 1.12
|
|
|
|
-- lib.go --
|
|
|
|
package lib
|
|
|
|
|
|
|
|
func Hello(x string) {
|
|
|
|
_ = x
|
|
|
|
}
|
|
|
|
-- lib_test.go --
|
|
|
|
package lib
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
type testStruct struct{
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHello(t *testing.T) {
|
|
|
|
testStructs := []*testStruct{
|
|
|
|
&testStruct{"hello"},
|
|
|
|
&testStruct{"goodbye"},
|
|
|
|
}
|
|
|
|
for y := range testStructs {
|
|
|
|
_ = y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, testPackage, func(t *testing.T, env *Env) {
|
2020-04-05 21:48:22 -06:00
|
|
|
env.OpenFile("lib_test.go")
|
|
|
|
env.Await(
|
|
|
|
DiagnosticAt("lib_test.go", 10, 2),
|
|
|
|
DiagnosticAt("lib_test.go", 11, 2),
|
|
|
|
)
|
|
|
|
env.OpenFile("lib.go")
|
|
|
|
env.RegexpReplace("lib.go", "_ = x", "var y int")
|
|
|
|
env.Await(
|
|
|
|
env.DiagnosticAtRegexp("lib.go", "y int"),
|
|
|
|
EmptyDiagnostics("lib_test.go"),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2020-04-09 08:01:18 -06:00
|
|
|
|
2020-04-29 15:01:30 -06:00
|
|
|
// Tests golang/go#38328.
|
|
|
|
func TestPackageChange_Issue38328(t *testing.T) {
|
|
|
|
const packageChange = `
|
2020-04-09 08:01:18 -06:00
|
|
|
-- go.mod --
|
|
|
|
module fake
|
|
|
|
-- a.go --
|
|
|
|
package foo
|
|
|
|
func main() {}
|
|
|
|
`
|
2020-04-14 11:40:37 -06:00
|
|
|
runner.Run(t, packageChange, func(t *testing.T, env *Env) {
|
2020-04-09 08:01:18 -06:00
|
|
|
env.OpenFile("a.go")
|
|
|
|
env.RegexpReplace("a.go", "foo", "foox")
|
2020-04-22 15:54:30 -06:00
|
|
|
env.Await(
|
|
|
|
// When the bug reported in #38328 was present, we didn't get erroneous
|
|
|
|
// file diagnostics until after the didChange message generated by the
|
|
|
|
// package renaming was fully processed. Therefore, in order for this
|
|
|
|
// test to actually exercise the bug, we must wait until that work has
|
|
|
|
// completed.
|
2020-04-23 21:24:24 -06:00
|
|
|
NoDiagnostics("a.go"),
|
2020-04-22 15:54:30 -06:00
|
|
|
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChange), 1),
|
|
|
|
)
|
2020-04-09 08:01:18 -06:00
|
|
|
})
|
|
|
|
}
|
2020-04-14 15:28:37 -06:00
|
|
|
|
|
|
|
const testPackageWithRequire = `
|
|
|
|
-- go.mod --
|
|
|
|
module mod.com
|
|
|
|
|
|
|
|
go 1.12
|
|
|
|
|
|
|
|
require (
|
|
|
|
foo.test v1.2.3
|
|
|
|
)
|
|
|
|
-- print.go --
|
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"foo.test/bar"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PrintAnswer() {
|
|
|
|
fmt.Printf("answer: %s", bar.Answer)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testPackageWithRequireProxy = `
|
|
|
|
-- foo.test@v1.2.3/go.mod --
|
|
|
|
module foo.test
|
|
|
|
|
|
|
|
go 1.12
|
|
|
|
-- foo.test@v1.2.3/bar/const.go --
|
|
|
|
package bar
|
|
|
|
|
|
|
|
const Answer = 42
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestResolveDiagnosticWithDownload(t *testing.T) {
|
|
|
|
runner.Run(t, testPackageWithRequire, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("print.go")
|
|
|
|
// Check that gopackages correctly loaded this dependency. We should get a
|
|
|
|
// diagnostic for the wrong formatting type.
|
|
|
|
// TODO: we should be able to easily also match the diagnostic message.
|
|
|
|
env.Await(env.DiagnosticAtRegexp("print.go", "fmt.Printf"))
|
|
|
|
}, WithProxy(testPackageWithRequireProxy))
|
|
|
|
}
|
2020-04-15 15:14:53 -06:00
|
|
|
|
|
|
|
func TestMissingDependency(t *testing.T) {
|
|
|
|
runner.Run(t, testPackageWithRequire, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("print.go")
|
|
|
|
env.Await(LogMatching(protocol.Error, "initial workspace load failed"))
|
|
|
|
})
|
|
|
|
}
|
2020-04-20 19:22:36 -06:00
|
|
|
|
2020-04-29 15:01:30 -06:00
|
|
|
// Tests golang/go#36951.
|
2020-04-28 12:06:04 -06:00
|
|
|
func TestAdHocPackages_Issue36951(t *testing.T) {
|
2020-04-20 19:22:36 -06:00
|
|
|
const adHoc = `
|
|
|
|
-- b/b.go --
|
|
|
|
package b
|
|
|
|
|
|
|
|
func Hello() {
|
|
|
|
var x int
|
|
|
|
}
|
|
|
|
`
|
|
|
|
runner.Run(t, adHoc, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("b/b.go")
|
|
|
|
env.Await(env.DiagnosticAtRegexp("b/b.go", "x"))
|
|
|
|
})
|
|
|
|
}
|
2020-04-20 18:32:11 -06:00
|
|
|
|
2020-04-29 15:01:30 -06:00
|
|
|
// Tests golang/go#37984.
|
2020-04-28 12:06:04 -06:00
|
|
|
func TestNoGOPATH_Issue37984(t *testing.T) {
|
2020-04-20 18:32:11 -06:00
|
|
|
const missingImport = `
|
|
|
|
-- main.go --
|
|
|
|
package main
|
|
|
|
|
|
|
|
func _() {
|
|
|
|
fmt.Println("Hello World")
|
|
|
|
}
|
|
|
|
`
|
|
|
|
runner.Run(t, missingImport, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("main.go")
|
|
|
|
env.Await(env.DiagnosticAtRegexp("main.go", "fmt"))
|
2020-04-28 22:00:52 -06:00
|
|
|
if err := env.Editor.OrganizeImports(env.Ctx, "main.go"); err == nil {
|
2020-04-20 18:32:11 -06:00
|
|
|
t.Fatalf("organize imports should fail with an empty GOPATH")
|
|
|
|
}
|
|
|
|
}, WithEnv("GOPATH="))
|
|
|
|
}
|
2020-04-28 12:06:04 -06:00
|
|
|
|
2020-04-29 15:01:30 -06:00
|
|
|
// Tests golang/go#38669.
|
2020-04-28 12:06:04 -06:00
|
|
|
func TestEqualInEnv_Issue38669(t *testing.T) {
|
|
|
|
const missingImport = `
|
|
|
|
-- go.mod --
|
|
|
|
module mod.com
|
|
|
|
|
|
|
|
-- main.go --
|
|
|
|
package main
|
|
|
|
|
|
|
|
var _ = x.X
|
|
|
|
-- x/x.go --
|
|
|
|
package x
|
|
|
|
|
|
|
|
var X = 0
|
|
|
|
`
|
|
|
|
runner.Run(t, missingImport, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("main.go")
|
|
|
|
env.OrganizeImports("main.go")
|
|
|
|
env.Await(EmptyDiagnostics("main.go"))
|
|
|
|
}, WithEnv("GOFLAGS=-tags=foo"))
|
|
|
|
}
|
2020-04-29 15:49:22 -06:00
|
|
|
|
2020-04-23 21:24:24 -06:00
|
|
|
// Tests golang/go#38467.
|
2020-04-29 15:49:22 -06:00
|
|
|
func TestNoSuggestedFixesForGeneratedFiles_Issue38467(t *testing.T) {
|
|
|
|
const generated = `
|
|
|
|
-- go.mod --
|
|
|
|
module mod.com
|
|
|
|
|
|
|
|
-- main.go --
|
|
|
|
package main
|
|
|
|
|
|
|
|
// Code generated by generator.go. DO NOT EDIT.
|
|
|
|
|
|
|
|
func _() {
|
|
|
|
for i, _ := range []string{} {
|
|
|
|
_ = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
runner.Run(t, generated, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("main.go")
|
|
|
|
original := env.ReadWorkspaceFile("main.go")
|
|
|
|
metBy := env.Await(
|
|
|
|
DiagnosticAt("main.go", 5, 8),
|
|
|
|
)
|
|
|
|
d, ok := metBy[0].(*protocol.PublishDiagnosticsParams)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected met by result %v (%T)", metBy, metBy)
|
|
|
|
}
|
|
|
|
// Apply fixes and save the buffer.
|
|
|
|
env.ApplyQuickFixes("main.go", d.Diagnostics)
|
|
|
|
env.SaveBuffer("main.go")
|
|
|
|
fixed := env.ReadWorkspaceFile("main.go")
|
|
|
|
if original != fixed {
|
|
|
|
t.Fatalf("generated file was changed by quick fixes:\n%s", tests.Diff(original, fixed))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-04-29 11:33:43 -06:00
|
|
|
|
2020-04-23 21:24:24 -06:00
|
|
|
// Expect a module/GOPATH error if there is an error in the file at startup.
|
|
|
|
// Tests golang/go#37279.
|
2020-04-29 11:33:43 -06:00
|
|
|
func TestShowMessage_Issue37279(t *testing.T) {
|
|
|
|
const noModule = `
|
|
|
|
-- a.go --
|
2020-04-23 21:24:24 -06:00
|
|
|
package foo
|
|
|
|
|
|
|
|
func f() {
|
|
|
|
fmt.Printl()
|
|
|
|
}
|
|
|
|
`
|
2020-04-29 11:33:43 -06:00
|
|
|
runner.Run(t, noModule, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("a.go")
|
|
|
|
env.Await(env.DiagnosticAtRegexp("a.go", "fmt.Printl"), SomeShowMessage(""))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:24:24 -06:00
|
|
|
// Expect no module/GOPATH error if there is no error in the file.
|
|
|
|
// Tests golang/go#37279.
|
2020-04-29 11:33:43 -06:00
|
|
|
func TestNoShowMessage_Issue37279(t *testing.T) {
|
|
|
|
const noModule = `
|
|
|
|
-- a.go --
|
2020-04-23 21:24:24 -06:00
|
|
|
package foo
|
|
|
|
|
|
|
|
func f() {
|
|
|
|
}
|
|
|
|
`
|
2020-04-29 11:33:43 -06:00
|
|
|
runner.Run(t, noModule, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("a.go")
|
2020-04-23 21:24:24 -06:00
|
|
|
env.Await(NoDiagnostics("a.go"), EmptyShowMessage(""))
|
2020-04-29 11:33:43 -06:00
|
|
|
// introduce an error, expect no Show Message
|
|
|
|
env.RegexpReplace("a.go", "func", "fun")
|
|
|
|
env.Await(env.DiagnosticAtRegexp("a.go", "fun"), EmptyShowMessage(""))
|
|
|
|
})
|
|
|
|
}
|
2020-04-23 21:24:24 -06:00
|
|
|
|
|
|
|
// Tests golang/go#38602.
|
|
|
|
func TestNonexistentFileDiagnostics_Issue38602(t *testing.T) {
|
|
|
|
const collision = `
|
|
|
|
-- x/x.go --
|
|
|
|
package x
|
|
|
|
|
|
|
|
import "x/hello"
|
|
|
|
|
|
|
|
func Hello() {
|
|
|
|
hello.HiThere()
|
|
|
|
}
|
|
|
|
-- x/main.go --
|
|
|
|
package main
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
`
|
|
|
|
runner.Run(t, collision, func(t *testing.T, env *Env) {
|
|
|
|
env.OpenFile("x/main.go")
|
|
|
|
env.Await(
|
|
|
|
env.DiagnosticAtRegexp("x/main.go", "fmt.Println"),
|
|
|
|
)
|
|
|
|
env.OrganizeImports("x/main.go")
|
|
|
|
// span.Parse misparses the error message when multiple packages are
|
|
|
|
// defined in the same directory, creating a garbage filename.
|
|
|
|
// Previously, we would send diagnostics for this nonexistent file.
|
|
|
|
// This test checks that we don't send diagnostics for this file.
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
badFile := fmt.Sprintf("%s/found packages main (main.go) and x (x.go) in %s/src/x", dir, env.Sandbox.GOPATH())
|
|
|
|
env.Await(
|
|
|
|
EmptyDiagnostics("x/main.go"),
|
|
|
|
NoDiagnostics(badFile),
|
|
|
|
)
|
|
|
|
}, InGOPATH())
|
|
|
|
}
|