2019-12-17 14:13:33 -07:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
2019-12-17 14:13:33 -07:00
|
|
|
package mod
|
2019-12-17 14:13:33 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/cache"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/lsp/tests"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
"golang.org/x/tools/internal/testenv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
testenv.ExitIfSmallMachine()
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:13:33 -07:00
|
|
|
// TODO(golang/go#36091): This file can be refactored to look like lsp_test.go
|
|
|
|
// when marker support gets added for go.mod files.
|
2020-01-09 18:40:52 -07:00
|
|
|
func TestModfileRemainsUnchanged(t *testing.T) {
|
|
|
|
ctx := tests.Context(t)
|
|
|
|
cache := cache.New(nil)
|
2020-01-21 14:01:59 -07:00
|
|
|
session := cache.NewSession()
|
2020-01-09 18:40:52 -07:00
|
|
|
options := tests.DefaultOptions()
|
|
|
|
options.TempModfile = true
|
|
|
|
options.Env = append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT=")
|
|
|
|
|
|
|
|
// Make sure to copy the test directory to a temporary directory so we do not
|
|
|
|
// modify the test code or add go.sum files when we run the tests.
|
2019-12-17 14:13:33 -07:00
|
|
|
folder, err := tests.CopyFolderToTempDir(filepath.Join("testdata", "unchanged"))
|
2020-01-09 18:40:52 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(folder)
|
|
|
|
|
|
|
|
before, err := ioutil.ReadFile(filepath.Join(folder, "go.mod"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, snapshot, err := session.NewView(ctx, "diagnostics_test", span.FileURI(folder), options)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !hasTempModfile(ctx, snapshot) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
after, err := ioutil.ReadFile(filepath.Join(folder, "go.mod"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(before) != string(after) {
|
|
|
|
t.Errorf("the real go.mod file was changed even when tempModfile=true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:13:33 -07:00
|
|
|
// TODO(golang/go#36091): This file can be refactored to look like lsp_test.go
|
|
|
|
// when marker support gets added for go.mod files.
|
2019-12-17 14:13:33 -07:00
|
|
|
func TestDiagnostics(t *testing.T) {
|
|
|
|
ctx := tests.Context(t)
|
|
|
|
cache := cache.New(nil)
|
2020-01-21 14:01:59 -07:00
|
|
|
session := cache.NewSession()
|
2019-12-17 14:13:33 -07:00
|
|
|
options := tests.DefaultOptions()
|
|
|
|
options.TempModfile = true
|
|
|
|
options.Env = append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT=")
|
|
|
|
|
|
|
|
for _, tt := range []struct {
|
|
|
|
testdir string
|
|
|
|
want []source.Diagnostic
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
testdir: "indirect",
|
|
|
|
want: []source.Diagnostic{
|
|
|
|
{
|
2019-12-17 14:13:33 -07:00
|
|
|
Message: "golang.org/x/tools should be a direct dependency.",
|
2019-12-17 14:13:33 -07:00
|
|
|
Source: "go mod tidy",
|
|
|
|
// TODO(golang/go#36091): When marker support gets added for go.mod files, we
|
|
|
|
// can remove these hard coded positions.
|
2019-12-17 14:13:33 -07:00
|
|
|
Range: protocol.Range{Start: getRawPos(4, 62), End: getRawPos(4, 73)},
|
2019-12-17 14:13:33 -07:00
|
|
|
Severity: protocol.SeverityWarning,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testdir: "unused",
|
|
|
|
want: []source.Diagnostic{
|
|
|
|
{
|
|
|
|
Message: "golang.org/x/tools is not used in this module.",
|
|
|
|
Source: "go mod tidy",
|
2019-12-17 14:13:33 -07:00
|
|
|
Range: protocol.Range{Start: getRawPos(4, 0), End: getRawPos(4, 61)},
|
2019-12-17 14:13:33 -07:00
|
|
|
Severity: protocol.SeverityWarning,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-12-17 14:13:33 -07:00
|
|
|
{
|
|
|
|
testdir: "invalidrequire",
|
|
|
|
want: []source.Diagnostic{
|
|
|
|
{
|
|
|
|
Message: "usage: require module/path v1.2.3",
|
2020-01-10 11:20:44 -07:00
|
|
|
Source: "syntax",
|
2020-01-16 12:32:09 -07:00
|
|
|
Range: protocol.Range{Start: getRawPos(4, 0), End: getRawPos(4, 16)},
|
2019-12-17 14:13:33 -07:00
|
|
|
Severity: protocol.SeverityError,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testdir: "invalidgo",
|
|
|
|
want: []source.Diagnostic{
|
|
|
|
{
|
|
|
|
Message: "usage: go 1.23",
|
2020-01-10 11:20:44 -07:00
|
|
|
Source: "syntax",
|
2020-01-16 12:32:09 -07:00
|
|
|
Range: protocol.Range{Start: getRawPos(2, 0), End: getRawPos(2, 3)},
|
2019-12-17 14:13:33 -07:00
|
|
|
Severity: protocol.SeverityError,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testdir: "unknowndirective",
|
|
|
|
want: []source.Diagnostic{
|
|
|
|
{
|
|
|
|
Message: "unknown directive: yo",
|
2020-01-10 11:20:44 -07:00
|
|
|
Source: "syntax",
|
2020-01-16 12:32:09 -07:00
|
|
|
Range: protocol.Range{Start: getRawPos(6, 0), End: getRawPos(6, 1)},
|
2019-12-17 14:13:33 -07:00
|
|
|
Severity: protocol.SeverityError,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-12-17 14:13:33 -07:00
|
|
|
} {
|
|
|
|
t.Run(tt.testdir, func(t *testing.T) {
|
2019-12-17 14:13:33 -07:00
|
|
|
// TODO: Once we refactor this to work with go/packages/packagestest. We do not
|
|
|
|
// need to copy to a temporary directory.
|
|
|
|
folder, err := tests.CopyFolderToTempDir(filepath.Join("testdata", tt.testdir))
|
2019-12-17 14:13:33 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(folder)
|
|
|
|
_, snapshot, err := session.NewView(ctx, "diagnostics_test", span.FileURI(folder), options)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// TODO: Add testing for when the -modfile flag is turned off and we still get diagnostics.
|
|
|
|
if !hasTempModfile(ctx, snapshot) {
|
|
|
|
return
|
|
|
|
}
|
2020-01-24 08:14:25 -07:00
|
|
|
reports, _, err := Diagnostics(ctx, snapshot)
|
2019-12-17 14:13:33 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-01-10 11:20:44 -07:00
|
|
|
if len(reports) != 1 {
|
2020-01-14 22:22:42 -07:00
|
|
|
t.Errorf("expected 1 diagnostic, got %d", len(reports))
|
2020-01-10 11:20:44 -07:00
|
|
|
}
|
|
|
|
for fh, got := range reports {
|
|
|
|
if diff := tests.DiffDiagnostics(fh.URI, tt.want, got); diff != "" {
|
|
|
|
t.Error(diff)
|
|
|
|
}
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasTempModfile(ctx context.Context, snapshot source.Snapshot) bool {
|
2020-01-23 14:54:21 -07:00
|
|
|
_, t := snapshot.View().ModFiles()
|
2020-01-16 12:32:09 -07:00
|
|
|
return t != ""
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:13:33 -07:00
|
|
|
func getRawPos(line, character int) protocol.Position {
|
2019-12-17 14:13:33 -07:00
|
|
|
return protocol.Position{
|
|
|
|
Line: float64(line),
|
|
|
|
Character: float64(character),
|
|
|
|
}
|
|
|
|
}
|