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 (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/cache"
|
|
|
|
"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())
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:40:52 -07:00
|
|
|
func TestModfileRemainsUnchanged(t *testing.T) {
|
|
|
|
ctx := tests.Context(t)
|
2020-03-05 17:54:11 -07:00
|
|
|
cache := cache.New(ctx, nil)
|
2020-02-28 08:30:03 -07:00
|
|
|
session := cache.NewSession(ctx)
|
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)
|
|
|
|
}
|
2020-02-12 14:36:46 -07:00
|
|
|
_, snapshot, err := session.NewView(ctx, "diagnostics_test", span.URIFromPath(folder), options)
|
2020-01-09 18:40:52 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-03 09:12:57 -07:00
|
|
|
if _, t := snapshot.View().ModFiles(); t == "" {
|
2020-01-09 18:40:52 -07:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|